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> 5290017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 531da177e4SLinus Torvalds #include <linux/netdevice.h> 541da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 551da177e4SLinus Torvalds #include <net/pkt_sched.h> 561da177e4SLinus Torvalds #endif 571da177e4SLinus Torvalds #include <linux/string.h> 581da177e4SLinus Torvalds #include <linux/skbuff.h> 599c55e01cSJens Axboe #include <linux/splice.h> 601da177e4SLinus Torvalds #include <linux/cache.h> 611da177e4SLinus Torvalds #include <linux/rtnetlink.h> 621da177e4SLinus Torvalds #include <linux/init.h> 63716ea3a7SDavid Howells #include <linux/scatterlist.h> 64ac45f602SPatrick Ohly #include <linux/errqueue.h> 65268bb0ceSLinus Torvalds #include <linux/prefetch.h> 660d5501c1SVlad Yasevich #include <linux/if_vlan.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <net/protocol.h> 691da177e4SLinus Torvalds #include <net/dst.h> 701da177e4SLinus Torvalds #include <net/sock.h> 711da177e4SLinus Torvalds #include <net/checksum.h> 72ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 731da177e4SLinus Torvalds #include <net/xfrm.h> 741da177e4SLinus Torvalds 757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 76ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7751c56b00SEric Dumazet #include <linux/highmem.h> 78b245be1fSWillem de Bruijn #include <linux/capability.h> 79b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 80a1f8e7f7SAl Viro 81d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 82e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 835f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 845f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /** 87f05de73bSJean Sacren * skb_panic - private function for out-of-line support 881da177e4SLinus Torvalds * @skb: buffer 891da177e4SLinus Torvalds * @sz: size 90f05de73bSJean Sacren * @addr: address 9199d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 921da177e4SLinus Torvalds * 93f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 94f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 95f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 96f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 971da177e4SLinus Torvalds */ 98f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 9999d5851eSJames Hogan const char msg[]) 1001da177e4SLinus Torvalds { 101e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 10299d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1034305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10426095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1051da177e4SLinus Torvalds BUG(); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 108f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1091da177e4SLinus Torvalds { 110f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 113f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 114f05de73bSJean Sacren { 115f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 116f05de73bSJean Sacren } 117c93bdd0eSMel Gorman 118c93bdd0eSMel Gorman /* 119c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 120c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 121c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 122c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 123c93bdd0eSMel Gorman * memory is free 124c93bdd0eSMel Gorman */ 125c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 126c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12761c5e88aSstephen hemminger 12861c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 12961c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 130c93bdd0eSMel Gorman { 131c93bdd0eSMel Gorman void *obj; 132c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 133c93bdd0eSMel Gorman 134c93bdd0eSMel Gorman /* 135c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 136c93bdd0eSMel Gorman * to the reserves, fail. 137c93bdd0eSMel Gorman */ 138c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 139c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 140c93bdd0eSMel Gorman node); 141c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 142c93bdd0eSMel Gorman goto out; 143c93bdd0eSMel Gorman 144c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 145c93bdd0eSMel Gorman ret_pfmemalloc = true; 146c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 147c93bdd0eSMel Gorman 148c93bdd0eSMel Gorman out: 149c93bdd0eSMel Gorman if (pfmemalloc) 150c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 151c93bdd0eSMel Gorman 152c93bdd0eSMel Gorman return obj; 153c93bdd0eSMel Gorman } 154c93bdd0eSMel Gorman 1551da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1561da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1571da177e4SLinus Torvalds * [BEEP] leaks. 1581da177e4SLinus Torvalds * 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds 1610ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) 1620ebd0ac5SPatrick McHardy { 1630ebd0ac5SPatrick McHardy struct sk_buff *skb; 1640ebd0ac5SPatrick McHardy 1650ebd0ac5SPatrick McHardy /* Get the HEAD */ 1660ebd0ac5SPatrick McHardy skb = kmem_cache_alloc_node(skbuff_head_cache, 1670ebd0ac5SPatrick McHardy gfp_mask & ~__GFP_DMA, node); 1680ebd0ac5SPatrick McHardy if (!skb) 1690ebd0ac5SPatrick McHardy goto out; 1700ebd0ac5SPatrick McHardy 1710ebd0ac5SPatrick McHardy /* 1720ebd0ac5SPatrick McHardy * Only clear those fields we need to clear, not those that we will 1730ebd0ac5SPatrick McHardy * actually initialise below. Hence, don't put any more fields after 1740ebd0ac5SPatrick McHardy * the tail pointer in struct sk_buff! 1750ebd0ac5SPatrick McHardy */ 1760ebd0ac5SPatrick McHardy memset(skb, 0, offsetof(struct sk_buff, tail)); 1775e71d9d7SPablo Neira skb->head = NULL; 1780ebd0ac5SPatrick McHardy skb->truesize = sizeof(struct sk_buff); 17963354797SReshetova, Elena refcount_set(&skb->users, 1); 1800ebd0ac5SPatrick McHardy 18135d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 1820ebd0ac5SPatrick McHardy out: 1830ebd0ac5SPatrick McHardy return skb; 1840ebd0ac5SPatrick McHardy } 1850ebd0ac5SPatrick McHardy 1861da177e4SLinus Torvalds /** 187d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1881da177e4SLinus Torvalds * @size: size to allocate 1891da177e4SLinus Torvalds * @gfp_mask: allocation mask 190c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 191c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 192c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 193c93bdd0eSMel Gorman * allocations in case the data is required for writeback 194b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1951da177e4SLinus Torvalds * 1961da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 19794b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 19894b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 2011da177e4SLinus Torvalds * %GFP_ATOMIC. 2021da177e4SLinus Torvalds */ 203dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 204c93bdd0eSMel Gorman int flags, int node) 2051da177e4SLinus Torvalds { 206e18b890bSChristoph Lameter struct kmem_cache *cache; 2074947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 2081da177e4SLinus Torvalds struct sk_buff *skb; 2091da177e4SLinus Torvalds u8 *data; 210c93bdd0eSMel Gorman bool pfmemalloc; 2111da177e4SLinus Torvalds 212c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 213c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 214c93bdd0eSMel Gorman 215c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 216c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 2178798b3fbSHerbert Xu 2181da177e4SLinus Torvalds /* Get the HEAD */ 219b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 2201da177e4SLinus Torvalds if (!skb) 2211da177e4SLinus Torvalds goto out; 222ec7d2f2cSEric Dumazet prefetchw(skb); 2231da177e4SLinus Torvalds 22487fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 22587fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 22687fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 22787fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 22887fb4b7bSEric Dumazet */ 229bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 23087fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 231c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2321da177e4SLinus Torvalds if (!data) 2331da177e4SLinus Torvalds goto nodata; 23487fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 23587fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 23687fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 23787fb4b7bSEric Dumazet */ 23887fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 239ec7d2f2cSEric Dumazet prefetchw(data + size); 2401da177e4SLinus Torvalds 241ca0605a7SArnaldo Carvalho de Melo /* 242c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 243c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 244c8005785SJohannes Berg * the tail pointer in struct sk_buff! 245ca0605a7SArnaldo Carvalho de Melo */ 246ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 24787fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 24887fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 249c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 25063354797SReshetova, Elena refcount_set(&skb->users, 1); 2511da177e4SLinus Torvalds skb->head = data; 2521da177e4SLinus Torvalds skb->data = data; 25327a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2544305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 25535d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 25635d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 25719633e12SStephen Hemminger 2584947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2594947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 260ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2614947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 262c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2634947d3efSBenjamin LaHaise 264c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 265d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2661da177e4SLinus Torvalds 267d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 268d0bf4a9eSEric Dumazet 269d0bf4a9eSEric Dumazet kmemcheck_annotate_bitfield(&fclones->skb2, flags1); 270d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 2712638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 1); 272d179cd12SDavid S. Miller 2736ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 274d179cd12SDavid S. Miller } 2751da177e4SLinus Torvalds out: 2761da177e4SLinus Torvalds return skb; 2771da177e4SLinus Torvalds nodata: 2788798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2791da177e4SLinus Torvalds skb = NULL; 2801da177e4SLinus Torvalds goto out; 2811da177e4SLinus Torvalds } 282b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /** 2852ea2f62cSEric Dumazet * __build_skb - build a network buffer 286b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2872ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 288b2b5ce9dSEric Dumazet * 289b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 290deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2912ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2922ea2f62cSEric Dumazet * or vmalloc() 293b2b5ce9dSEric Dumazet * The return is the new skb buffer. 294b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 295b2b5ce9dSEric Dumazet * Notes : 296b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 297b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 298b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 299b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 300b2b5ce9dSEric Dumazet * before giving packet to stack. 301b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 302b2b5ce9dSEric Dumazet */ 3032ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 304b2b5ce9dSEric Dumazet { 305b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 306b2b5ce9dSEric Dumazet struct sk_buff *skb; 307d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 308b2b5ce9dSEric Dumazet 309b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 310b2b5ce9dSEric Dumazet if (!skb) 311b2b5ce9dSEric Dumazet return NULL; 312b2b5ce9dSEric Dumazet 313d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 314b2b5ce9dSEric Dumazet 315b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 316b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 31763354797SReshetova, Elena refcount_set(&skb->users, 1); 318b2b5ce9dSEric Dumazet skb->head = data; 319b2b5ce9dSEric Dumazet skb->data = data; 320b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 321b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 32235d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 32335d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 324b2b5ce9dSEric Dumazet 325b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 326b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 327b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 328b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 329b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 330b2b5ce9dSEric Dumazet 331b2b5ce9dSEric Dumazet return skb; 332b2b5ce9dSEric Dumazet } 3332ea2f62cSEric Dumazet 3342ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3352ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3362ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3372ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3382ea2f62cSEric Dumazet */ 3392ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3402ea2f62cSEric Dumazet { 3412ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3422ea2f62cSEric Dumazet 3432ea2f62cSEric Dumazet if (skb && frag_size) { 3442ea2f62cSEric Dumazet skb->head_frag = 1; 3452f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3462ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3472ea2f62cSEric Dumazet } 3482ea2f62cSEric Dumazet return skb; 3492ea2f62cSEric Dumazet } 350b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 351b2b5ce9dSEric Dumazet 352795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 353795bb1c0SJesper Dangaard Brouer 354795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 355795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 356e0d7924aSAlexey Dobriyan unsigned int skb_count; 357795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 358795bb1c0SJesper Dangaard Brouer }; 359795bb1c0SJesper Dangaard Brouer 360b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 361795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 362ffde7328SAlexander Duyck 363ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 364ffde7328SAlexander Duyck { 365b63ae8caSAlexander Duyck struct page_frag_cache *nc; 366ffde7328SAlexander Duyck unsigned long flags; 367ffde7328SAlexander Duyck void *data; 368ffde7328SAlexander Duyck 369ffde7328SAlexander Duyck local_irq_save(flags); 3709451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3718c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, fragsz, gfp_mask); 3726f532612SEric Dumazet local_irq_restore(flags); 3736f532612SEric Dumazet return data; 3746f532612SEric Dumazet } 375c93bdd0eSMel Gorman 376c93bdd0eSMel Gorman /** 377c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 378c93bdd0eSMel Gorman * @fragsz: fragment size 379c93bdd0eSMel Gorman * 380c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 381c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 382c93bdd0eSMel Gorman */ 383c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 384c93bdd0eSMel Gorman { 385c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 386c93bdd0eSMel Gorman } 3876f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3886f532612SEric Dumazet 389ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 390ffde7328SAlexander Duyck { 391795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3929451980aSAlexander Duyck 3938c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 394ffde7328SAlexander Duyck } 395ffde7328SAlexander Duyck 396ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 397ffde7328SAlexander Duyck { 398ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 399ffde7328SAlexander Duyck } 400ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 401ffde7328SAlexander Duyck 4026f532612SEric Dumazet /** 403fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 404fd11a83dSAlexander Duyck * @dev: network device to receive on 405d7499160SMasanari Iida * @len: length to allocate 406fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 407fd11a83dSAlexander Duyck * 408fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 409fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 410fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 411fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 412fd11a83dSAlexander Duyck * 413fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 414fd11a83dSAlexander Duyck */ 4159451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4169451980aSAlexander Duyck gfp_t gfp_mask) 417fd11a83dSAlexander Duyck { 418b63ae8caSAlexander Duyck struct page_frag_cache *nc; 4199451980aSAlexander Duyck unsigned long flags; 420fd11a83dSAlexander Duyck struct sk_buff *skb; 4219451980aSAlexander Duyck bool pfmemalloc; 4229451980aSAlexander Duyck void *data; 423fd11a83dSAlexander Duyck 4249451980aSAlexander Duyck len += NET_SKB_PAD; 425fd11a83dSAlexander Duyck 4269451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 427d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 428a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 429a080e7bdSAlexander Duyck if (!skb) 430a080e7bdSAlexander Duyck goto skb_fail; 431a080e7bdSAlexander Duyck goto skb_success; 432a080e7bdSAlexander Duyck } 4339451980aSAlexander Duyck 4349451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4359451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4369451980aSAlexander Duyck 4379451980aSAlexander Duyck if (sk_memalloc_socks()) 4389451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4399451980aSAlexander Duyck 4409451980aSAlexander Duyck local_irq_save(flags); 4419451980aSAlexander Duyck 4429451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4438c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4449451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4459451980aSAlexander Duyck 4469451980aSAlexander Duyck local_irq_restore(flags); 4479451980aSAlexander Duyck 4489451980aSAlexander Duyck if (unlikely(!data)) 4499451980aSAlexander Duyck return NULL; 4509451980aSAlexander Duyck 4519451980aSAlexander Duyck skb = __build_skb(data, len); 4529451980aSAlexander Duyck if (unlikely(!skb)) { 453181edb2bSAlexander Duyck skb_free_frag(data); 4549451980aSAlexander Duyck return NULL; 4559451980aSAlexander Duyck } 4569451980aSAlexander Duyck 4579451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4589451980aSAlexander Duyck if (pfmemalloc) 4599451980aSAlexander Duyck skb->pfmemalloc = 1; 4609451980aSAlexander Duyck skb->head_frag = 1; 4619451980aSAlexander Duyck 462a080e7bdSAlexander Duyck skb_success: 4638af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4647b2e497aSChristoph Hellwig skb->dev = dev; 465fd11a83dSAlexander Duyck 466a080e7bdSAlexander Duyck skb_fail: 4678af27456SChristoph Hellwig return skb; 4688af27456SChristoph Hellwig } 469b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4701da177e4SLinus Torvalds 471fd11a83dSAlexander Duyck /** 472fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 473fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 474d7499160SMasanari Iida * @len: length to allocate 475fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 476fd11a83dSAlexander Duyck * 477fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 478fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 479fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 480fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 481fd11a83dSAlexander Duyck * 482fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 483fd11a83dSAlexander Duyck */ 4849451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4859451980aSAlexander Duyck gfp_t gfp_mask) 486fd11a83dSAlexander Duyck { 487795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 488fd11a83dSAlexander Duyck struct sk_buff *skb; 4899451980aSAlexander Duyck void *data; 490fd11a83dSAlexander Duyck 4919451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 492fd11a83dSAlexander Duyck 4939451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 494d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 495a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 496a080e7bdSAlexander Duyck if (!skb) 497a080e7bdSAlexander Duyck goto skb_fail; 498a080e7bdSAlexander Duyck goto skb_success; 499a080e7bdSAlexander Duyck } 5009451980aSAlexander Duyck 5019451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5029451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5039451980aSAlexander Duyck 5049451980aSAlexander Duyck if (sk_memalloc_socks()) 5059451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5069451980aSAlexander Duyck 5078c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 5089451980aSAlexander Duyck if (unlikely(!data)) 5099451980aSAlexander Duyck return NULL; 5109451980aSAlexander Duyck 5119451980aSAlexander Duyck skb = __build_skb(data, len); 5129451980aSAlexander Duyck if (unlikely(!skb)) { 513181edb2bSAlexander Duyck skb_free_frag(data); 5149451980aSAlexander Duyck return NULL; 5159451980aSAlexander Duyck } 5169451980aSAlexander Duyck 5179451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 518795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5199451980aSAlexander Duyck skb->pfmemalloc = 1; 5209451980aSAlexander Duyck skb->head_frag = 1; 5219451980aSAlexander Duyck 522a080e7bdSAlexander Duyck skb_success: 523fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 524fd11a83dSAlexander Duyck skb->dev = napi->dev; 525fd11a83dSAlexander Duyck 526a080e7bdSAlexander Duyck skb_fail: 527fd11a83dSAlexander Duyck return skb; 528fd11a83dSAlexander Duyck } 529fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 530fd11a83dSAlexander Duyck 531654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 53250269e19SEric Dumazet int size, unsigned int truesize) 533654bed16SPeter Zijlstra { 534654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 535654bed16SPeter Zijlstra skb->len += size; 536654bed16SPeter Zijlstra skb->data_len += size; 53750269e19SEric Dumazet skb->truesize += truesize; 538654bed16SPeter Zijlstra } 539654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 540654bed16SPeter Zijlstra 541f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 542f8e617e1SJason Wang unsigned int truesize) 543f8e617e1SJason Wang { 544f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 545f8e617e1SJason Wang 546f8e617e1SJason Wang skb_frag_size_add(frag, size); 547f8e617e1SJason Wang skb->len += size; 548f8e617e1SJason Wang skb->data_len += size; 549f8e617e1SJason Wang skb->truesize += truesize; 550f8e617e1SJason Wang } 551f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 552f8e617e1SJason Wang 55327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5541da177e4SLinus Torvalds { 555bd8a7036SEric Dumazet kfree_skb_list(*listp); 55627b437c8SHerbert Xu *listp = NULL; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 55927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 56027b437c8SHerbert Xu { 56127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 56227b437c8SHerbert Xu } 56327b437c8SHerbert Xu 5641da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5651da177e4SLinus Torvalds { 5661da177e4SLinus Torvalds struct sk_buff *list; 5671da177e4SLinus Torvalds 568fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5691da177e4SLinus Torvalds skb_get(list); 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 572d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 573d3836f21SEric Dumazet { 574181edb2bSAlexander Duyck unsigned char *head = skb->head; 575181edb2bSAlexander Duyck 576d3836f21SEric Dumazet if (skb->head_frag) 577181edb2bSAlexander Duyck skb_free_frag(head); 578d3836f21SEric Dumazet else 579181edb2bSAlexander Duyck kfree(head); 580d3836f21SEric Dumazet } 581d3836f21SEric Dumazet 5825bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5831da177e4SLinus Torvalds { 584ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5851da177e4SLinus Torvalds int i; 586ff04a771SEric Dumazet 587ff04a771SEric Dumazet if (skb->cloned && 588ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 589ff04a771SEric Dumazet &shinfo->dataref)) 590ff04a771SEric Dumazet return; 591ff04a771SEric Dumazet 592ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 593ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5941da177e4SLinus Torvalds 595a6686f2fSShirley Ma /* 596a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 597a6686f2fSShirley Ma * the lower device DMA has done; 598a6686f2fSShirley Ma */ 599ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 600a6686f2fSShirley Ma struct ubuf_info *uarg; 601a6686f2fSShirley Ma 602ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 603a6686f2fSShirley Ma if (uarg->callback) 604e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 605a6686f2fSShirley Ma } 606a6686f2fSShirley Ma 607ff04a771SEric Dumazet if (shinfo->frag_list) 608ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6091da177e4SLinus Torvalds 610d3836f21SEric Dumazet skb_free_head(skb); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* 6141da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6151da177e4SLinus Torvalds */ 6162d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6171da177e4SLinus Torvalds { 618d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 619d179cd12SDavid S. Miller 620d179cd12SDavid S. Miller switch (skb->fclone) { 621d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6221da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6236ffe75ebSEric Dumazet return; 624d179cd12SDavid S. Miller 625d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 626d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6276ffe75ebSEric Dumazet 6286ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6296ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6306ffe75ebSEric Dumazet * while here, branch prediction will be good. 6316ffe75ebSEric Dumazet */ 6322638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 6336ffe75ebSEric Dumazet goto fastpath; 634d179cd12SDavid S. Miller break; 635d179cd12SDavid S. Miller 6366ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 637d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 638d179cd12SDavid S. Miller break; 6393ff50b79SStephen Hemminger } 6402638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 6416ffe75ebSEric Dumazet return; 6426ffe75ebSEric Dumazet fastpath: 6436ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6460a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 6471da177e4SLinus Torvalds { 648adf30907SEric Dumazet skb_dst_drop(skb); 6490a463c78SPaolo Abeni secpath_reset(skb); 6501da177e4SLinus Torvalds if (skb->destructor) { 6519c2b3328SStephen Hemminger WARN_ON(in_irq()); 6521da177e4SLinus Torvalds skb->destructor(skb); 6531da177e4SLinus Torvalds } 654a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 655cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6562fc72c7bSKOVACS Krisztian #endif 6571109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6581da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6591da177e4SLinus Torvalds #endif 66004a4bb55SLennert Buytenhek } 66104a4bb55SLennert Buytenhek 66204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 66304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 66404a4bb55SLennert Buytenhek { 66504a4bb55SLennert Buytenhek skb_release_head_state(skb); 6665e71d9d7SPablo Neira if (likely(skb->head)) 6672d4baff8SHerbert Xu skb_release_data(skb); 6682d4baff8SHerbert Xu } 6691da177e4SLinus Torvalds 6702d4baff8SHerbert Xu /** 6712d4baff8SHerbert Xu * __kfree_skb - private function 6722d4baff8SHerbert Xu * @skb: buffer 6732d4baff8SHerbert Xu * 6742d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6752d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6762d4baff8SHerbert Xu * always call kfree_skb 6772d4baff8SHerbert Xu */ 6782d4baff8SHerbert Xu 6792d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6802d4baff8SHerbert Xu { 6812d4baff8SHerbert Xu skb_release_all(skb); 6821da177e4SLinus Torvalds kfree_skbmem(skb); 6831da177e4SLinus Torvalds } 684b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /** 687231d06aeSJörn Engel * kfree_skb - free an sk_buff 688231d06aeSJörn Engel * @skb: buffer to free 689231d06aeSJörn Engel * 690231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 691231d06aeSJörn Engel * hit zero. 692231d06aeSJörn Engel */ 693231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 694231d06aeSJörn Engel { 6953889a803SPaolo Abeni if (!skb_unref(skb)) 696231d06aeSJörn Engel return; 6973889a803SPaolo Abeni 698ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 699231d06aeSJörn Engel __kfree_skb(skb); 700231d06aeSJörn Engel } 701b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 702231d06aeSJörn Engel 703bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 704bd8a7036SEric Dumazet { 705bd8a7036SEric Dumazet while (segs) { 706bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 707bd8a7036SEric Dumazet 708bd8a7036SEric Dumazet kfree_skb(segs); 709bd8a7036SEric Dumazet segs = next; 710bd8a7036SEric Dumazet } 711bd8a7036SEric Dumazet } 712bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 713bd8a7036SEric Dumazet 714d1a203eaSStephen Hemminger /** 71525121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 71625121173SMichael S. Tsirkin * @skb: buffer that triggered an error 71725121173SMichael S. Tsirkin * 71825121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 71925121173SMichael S. Tsirkin * skb must be freed afterwards. 72025121173SMichael S. Tsirkin */ 72125121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 72225121173SMichael S. Tsirkin { 72325121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 72425121173SMichael S. Tsirkin struct ubuf_info *uarg; 72525121173SMichael S. Tsirkin 72625121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 72725121173SMichael S. Tsirkin if (uarg->callback) 72825121173SMichael S. Tsirkin uarg->callback(uarg, false); 72925121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 73025121173SMichael S. Tsirkin } 73125121173SMichael S. Tsirkin } 73225121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 73325121173SMichael S. Tsirkin 73425121173SMichael S. Tsirkin /** 735ead2ceb0SNeil Horman * consume_skb - free an skbuff 736ead2ceb0SNeil Horman * @skb: buffer to free 737ead2ceb0SNeil Horman * 738ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 739ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 740ead2ceb0SNeil Horman * is being dropped after a failure and notes that 741ead2ceb0SNeil Horman */ 742ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 743ead2ceb0SNeil Horman { 7443889a803SPaolo Abeni if (!skb_unref(skb)) 745ead2ceb0SNeil Horman return; 7463889a803SPaolo Abeni 74707dc22e7SKoki Sanagi trace_consume_skb(skb); 748ead2ceb0SNeil Horman __kfree_skb(skb); 749ead2ceb0SNeil Horman } 750ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 751ead2ceb0SNeil Horman 7520a463c78SPaolo Abeni /** 7530a463c78SPaolo Abeni * consume_stateless_skb - free an skbuff, assuming it is stateless 7540a463c78SPaolo Abeni * @skb: buffer to free 7550a463c78SPaolo Abeni * 7560a463c78SPaolo Abeni * Works like consume_skb(), but this variant assumes that all the head 7570a463c78SPaolo Abeni * states have been already dropped. 7580a463c78SPaolo Abeni */ 7590a463c78SPaolo Abeni void consume_stateless_skb(struct sk_buff *skb) 7600a463c78SPaolo Abeni { 7610a463c78SPaolo Abeni if (!skb_unref(skb)) 7620a463c78SPaolo Abeni return; 7630a463c78SPaolo Abeni 7640a463c78SPaolo Abeni trace_consume_skb(skb); 7650a463c78SPaolo Abeni if (likely(skb->head)) 7660a463c78SPaolo Abeni skb_release_data(skb); 7670a463c78SPaolo Abeni kfree_skbmem(skb); 7680a463c78SPaolo Abeni } 7690a463c78SPaolo Abeni 770795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 771795bb1c0SJesper Dangaard Brouer { 772795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 773795bb1c0SJesper Dangaard Brouer 774795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 775795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 776795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 777795bb1c0SJesper Dangaard Brouer nc->skb_cache); 778795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 779795bb1c0SJesper Dangaard Brouer } 780795bb1c0SJesper Dangaard Brouer } 781795bb1c0SJesper Dangaard Brouer 78215fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 783795bb1c0SJesper Dangaard Brouer { 784795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 785795bb1c0SJesper Dangaard Brouer 786795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 787795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 788795bb1c0SJesper Dangaard Brouer 789795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 790795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 791795bb1c0SJesper Dangaard Brouer 792795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 793795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 794795bb1c0SJesper Dangaard Brouer prefetchw(skb); 795795bb1c0SJesper Dangaard Brouer #endif 796795bb1c0SJesper Dangaard Brouer 797795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 798795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 799795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 800795bb1c0SJesper Dangaard Brouer nc->skb_cache); 801795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 802795bb1c0SJesper Dangaard Brouer } 803795bb1c0SJesper Dangaard Brouer } 80415fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 80515fad714SJesper Dangaard Brouer { 80615fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 80715fad714SJesper Dangaard Brouer } 808795bb1c0SJesper Dangaard Brouer 809795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 810795bb1c0SJesper Dangaard Brouer { 811795bb1c0SJesper Dangaard Brouer if (unlikely(!skb)) 812795bb1c0SJesper Dangaard Brouer return; 813795bb1c0SJesper Dangaard Brouer 814885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 815795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 816885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 817795bb1c0SJesper Dangaard Brouer return; 818795bb1c0SJesper Dangaard Brouer } 819795bb1c0SJesper Dangaard Brouer 8207608894eSPaolo Abeni if (!skb_unref(skb)) 821795bb1c0SJesper Dangaard Brouer return; 8227608894eSPaolo Abeni 823795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 824795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 825795bb1c0SJesper Dangaard Brouer 826795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 827abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 828795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 829795bb1c0SJesper Dangaard Brouer return; 830795bb1c0SJesper Dangaard Brouer } 831795bb1c0SJesper Dangaard Brouer 83215fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 833795bb1c0SJesper Dangaard Brouer } 834795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 835795bb1c0SJesper Dangaard Brouer 836b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 837b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 838b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 839b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 840b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 841b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 842b1937227SEric Dumazet 843dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 844dec18810SHerbert Xu { 845dec18810SHerbert Xu new->tstamp = old->tstamp; 846b1937227SEric Dumazet /* We do not copy old->sk */ 847dec18810SHerbert Xu new->dev = old->dev; 848b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 8497fee226aSEric Dumazet skb_dst_copy(new, old); 850def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 851dec18810SHerbert Xu new->sp = secpath_get(old->sp); 852dec18810SHerbert Xu #endif 853b1937227SEric Dumazet __nf_copy(new, old, false); 8546aa895b0SPatrick McHardy 855b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 856b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 857b1937227SEric Dumazet */ 858b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 85906021292SEliezer Tamir 860b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 861b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 862b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 863b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 864b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 865b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 866b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 867b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 868b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 869b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 870b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 871b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 872b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 873b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 874b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 875b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 876b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 877b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 878b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 879b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 880b1937227SEric Dumazet #endif 881e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 882b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 88306021292SEliezer Tamir #endif 8842bd82484SEric Dumazet #ifdef CONFIG_XPS 8852bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8862bd82484SEric Dumazet #endif 887b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 888b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 889b1937227SEric Dumazet #endif 890b1937227SEric Dumazet 891dec18810SHerbert Xu } 892dec18810SHerbert Xu 89382c49a35SHerbert Xu /* 89482c49a35SHerbert Xu * You should not add any new code to this function. Add it to 89582c49a35SHerbert Xu * __copy_skb_header above instead. 89682c49a35SHerbert Xu */ 897e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds #define C(x) n->x = skb->x 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds n->next = n->prev = NULL; 9021da177e4SLinus Torvalds n->sk = NULL; 903dec18810SHerbert Xu __copy_skb_header(n, skb); 904dec18810SHerbert Xu 9051da177e4SLinus Torvalds C(len); 9061da177e4SLinus Torvalds C(data_len); 9073e6b3b2eSAlexey Dobriyan C(mac_len); 908334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 90902f1c89dSPaul Moore n->cloned = 1; 9101da177e4SLinus Torvalds n->nohdr = 0; 9111da177e4SLinus Torvalds n->destructor = NULL; 9121da177e4SLinus Torvalds C(tail); 9131da177e4SLinus Torvalds C(end); 91402f1c89dSPaul Moore C(head); 915d3836f21SEric Dumazet C(head_frag); 91602f1c89dSPaul Moore C(data); 91702f1c89dSPaul Moore C(truesize); 91863354797SReshetova, Elena refcount_set(&n->users, 1); 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 9211da177e4SLinus Torvalds skb->cloned = 1; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds return n; 924e0053ec0SHerbert Xu #undef C 925e0053ec0SHerbert Xu } 926e0053ec0SHerbert Xu 927e0053ec0SHerbert Xu /** 928e0053ec0SHerbert Xu * skb_morph - morph one skb into another 929e0053ec0SHerbert Xu * @dst: the skb to receive the contents 930e0053ec0SHerbert Xu * @src: the skb to supply the contents 931e0053ec0SHerbert Xu * 932e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 933e0053ec0SHerbert Xu * supplied by the user. 934e0053ec0SHerbert Xu * 935e0053ec0SHerbert Xu * The target skb is returned upon exit. 936e0053ec0SHerbert Xu */ 937e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 938e0053ec0SHerbert Xu { 9392d4baff8SHerbert Xu skb_release_all(dst); 940e0053ec0SHerbert Xu return __skb_clone(dst, src); 941e0053ec0SHerbert Xu } 942e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 943e0053ec0SHerbert Xu 9442c53040fSBen Hutchings /** 9452c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 94648c83012SMichael S. Tsirkin * @skb: the skb to modify 94748c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 94848c83012SMichael S. Tsirkin * 94948c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 95048c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 95148c83012SMichael S. Tsirkin * to userspace pages. 95248c83012SMichael S. Tsirkin * 95348c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 95448c83012SMichael S. Tsirkin * %GFP_ATOMIC. 95548c83012SMichael S. Tsirkin * 95648c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 95748c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 95848c83012SMichael S. Tsirkin */ 95948c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 960a6686f2fSShirley Ma { 961a6686f2fSShirley Ma int i; 962a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 963a6686f2fSShirley Ma struct page *page, *head = NULL; 964a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 965a6686f2fSShirley Ma 966a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 967a6686f2fSShirley Ma u8 *vaddr; 968a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 969a6686f2fSShirley Ma 97002756ed4SKrishna Kumar page = alloc_page(gfp_mask); 971a6686f2fSShirley Ma if (!page) { 972a6686f2fSShirley Ma while (head) { 97340dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 974a6686f2fSShirley Ma put_page(head); 975a6686f2fSShirley Ma head = next; 976a6686f2fSShirley Ma } 977a6686f2fSShirley Ma return -ENOMEM; 978a6686f2fSShirley Ma } 97951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 980a6686f2fSShirley Ma memcpy(page_address(page), 9819e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 98251c56b00SEric Dumazet kunmap_atomic(vaddr); 98340dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 984a6686f2fSShirley Ma head = page; 985a6686f2fSShirley Ma } 986a6686f2fSShirley Ma 987a6686f2fSShirley Ma /* skb frags release userspace buffers */ 98802756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 989a8605c60SIan Campbell skb_frag_unref(skb, i); 990a6686f2fSShirley Ma 991e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 992a6686f2fSShirley Ma 993a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 99402756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 99502756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 99602756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 99740dadff2SSunghan Suh head = (struct page *)page_private(head); 998a6686f2fSShirley Ma } 99948c83012SMichael S. Tsirkin 100048c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 1001a6686f2fSShirley Ma return 0; 1002a6686f2fSShirley Ma } 1003dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1004a6686f2fSShirley Ma 1005e0053ec0SHerbert Xu /** 1006e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1007e0053ec0SHerbert Xu * @skb: buffer to clone 1008e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1009e0053ec0SHerbert Xu * 1010e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1011e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1012e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1013e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1014e0053ec0SHerbert Xu * 1015e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1016e0053ec0SHerbert Xu * %GFP_ATOMIC. 1017e0053ec0SHerbert Xu */ 1018e0053ec0SHerbert Xu 1019e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1020e0053ec0SHerbert Xu { 1021d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1022d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1023d0bf4a9eSEric Dumazet skb1); 10246ffe75ebSEric Dumazet struct sk_buff *n; 1025e0053ec0SHerbert Xu 102670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1027a6686f2fSShirley Ma return NULL; 1028a6686f2fSShirley Ma 1029e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 10302638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 10316ffe75ebSEric Dumazet n = &fclones->skb2; 10322638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1033e0053ec0SHerbert Xu } else { 1034c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1035c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1036c93bdd0eSMel Gorman 1037e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1038e0053ec0SHerbert Xu if (!n) 1039e0053ec0SHerbert Xu return NULL; 1040fe55f6d5SVegard Nossum 1041fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 1042e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1043e0053ec0SHerbert Xu } 1044e0053ec0SHerbert Xu 1045e0053ec0SHerbert Xu return __skb_clone(n, skb); 10461da177e4SLinus Torvalds } 1047b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 10481da177e4SLinus Torvalds 1049f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 1050f5b17294SPravin B Shelar { 1051030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1052030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1053030737bcSEric Dumazet skb->csum_start += off; 1054f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1055f5b17294SPravin B Shelar skb->transport_header += off; 1056f5b17294SPravin B Shelar skb->network_header += off; 1057f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1058f5b17294SPravin B Shelar skb->mac_header += off; 1059f5b17294SPravin B Shelar skb->inner_transport_header += off; 1060f5b17294SPravin B Shelar skb->inner_network_header += off; 1061aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1062f5b17294SPravin B Shelar } 1063f5b17294SPravin B Shelar 10641da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 10651da177e4SLinus Torvalds { 1066dec18810SHerbert Xu __copy_skb_header(new, old); 1067dec18810SHerbert Xu 10687967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 10697967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 10707967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 1073c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1074c93bdd0eSMel Gorman { 1075c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1076c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1077c93bdd0eSMel Gorman return 0; 1078c93bdd0eSMel Gorman } 1079c93bdd0eSMel Gorman 10801da177e4SLinus Torvalds /** 10811da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 10821da177e4SLinus Torvalds * @skb: buffer to copy 10831da177e4SLinus Torvalds * @gfp_mask: allocation priority 10841da177e4SLinus Torvalds * 10851da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 10861da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 10871da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 10881da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10891da177e4SLinus Torvalds * 10901da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 10911da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 10921da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 10931da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 10941da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 10951da177e4SLinus Torvalds */ 10961da177e4SLinus Torvalds 1097dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 10981da177e4SLinus Torvalds { 10996602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1100ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1101c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1102c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 11036602cebbSEric Dumazet 11041da177e4SLinus Torvalds if (!n) 11051da177e4SLinus Torvalds return NULL; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds /* Set the data pointer */ 11081da177e4SLinus Torvalds skb_reserve(n, headerlen); 11091da177e4SLinus Torvalds /* Set the tail pointer and length */ 11101da177e4SLinus Torvalds skb_put(n, skb->len); 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 11131da177e4SLinus Torvalds BUG(); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds copy_skb_header(n, skb); 11161da177e4SLinus Torvalds return n; 11171da177e4SLinus Torvalds } 1118b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds /** 1121bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 11221da177e4SLinus Torvalds * @skb: buffer to copy 1123117632e6SEric Dumazet * @headroom: headroom of new skb 11241da177e4SLinus Torvalds * @gfp_mask: allocation priority 1125bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1126bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1127bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 11281da177e4SLinus Torvalds * 11291da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 11301da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 11311da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 11321da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 11331da177e4SLinus Torvalds * or the pointer to the buffer on success. 11341da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds 1137bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1138bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 11391da177e4SLinus Torvalds { 1140117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1141bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1142bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 11436602cebbSEric Dumazet 11441da177e4SLinus Torvalds if (!n) 11451da177e4SLinus Torvalds goto out; 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds /* Set the data pointer */ 1148117632e6SEric Dumazet skb_reserve(n, headroom); 11491da177e4SLinus Torvalds /* Set the tail pointer and length */ 11501da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 11511da177e4SLinus Torvalds /* Copy the bytes */ 1152d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 11531da177e4SLinus Torvalds 115425f484a6SHerbert Xu n->truesize += skb->data_len; 11551da177e4SLinus Torvalds n->data_len = skb->data_len; 11561da177e4SLinus Torvalds n->len = skb->len; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 11591da177e4SLinus Torvalds int i; 11601da177e4SLinus Torvalds 116170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 11621511022cSDan Carpenter kfree_skb(n); 11631511022cSDan Carpenter n = NULL; 1164a6686f2fSShirley Ma goto out; 1165a6686f2fSShirley Ma } 11661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11671da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1168ea2ab693SIan Campbell skb_frag_ref(skb, i); 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 117321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 11741da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 11751da177e4SLinus Torvalds skb_clone_fraglist(n); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds copy_skb_header(n, skb); 11791da177e4SLinus Torvalds out: 11801da177e4SLinus Torvalds return n; 11811da177e4SLinus Torvalds } 1182bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /** 11851da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 11861da177e4SLinus Torvalds * @skb: buffer to reallocate 11871da177e4SLinus Torvalds * @nhead: room to add at head 11881da177e4SLinus Torvalds * @ntail: room to add at tail 11891da177e4SLinus Torvalds * @gfp_mask: allocation priority 11901da177e4SLinus Torvalds * 1191bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1192bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 11931da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 11941da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 11951da177e4SLinus Torvalds * 11961da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11971da177e4SLinus Torvalds * reloaded after call to this function. 11981da177e4SLinus Torvalds */ 11991da177e4SLinus Torvalds 120086a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1201dd0fc66fSAl Viro gfp_t gfp_mask) 12021da177e4SLinus Torvalds { 1203158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1204158f323bSEric Dumazet int size = osize + nhead + ntail; 12051da177e4SLinus Torvalds long off; 1206158f323bSEric Dumazet u8 *data; 12071da177e4SLinus Torvalds 12084edd87adSHerbert Xu BUG_ON(nhead < 0); 12094edd87adSHerbert Xu 12101da177e4SLinus Torvalds if (skb_shared(skb)) 12111da177e4SLinus Torvalds BUG(); 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 12141da177e4SLinus Torvalds 1215c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1216c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1217c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1218c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 12191da177e4SLinus Torvalds if (!data) 12201da177e4SLinus Torvalds goto nodata; 122187151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 12246602cebbSEric Dumazet * optimized for the cases when header is void. 12256602cebbSEric Dumazet */ 12266602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 12276602cebbSEric Dumazet 12286602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 12296602cebbSEric Dumazet skb_shinfo(skb), 1230fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 12311da177e4SLinus Torvalds 12323e24591aSAlexander Duyck /* 12333e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 12343e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 12353e24591aSAlexander Duyck * be since all we did is relocate the values 12363e24591aSAlexander Duyck */ 12373e24591aSAlexander Duyck if (skb_cloned(skb)) { 1238a6686f2fSShirley Ma /* copy this zero copy skb frags */ 123970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1240a6686f2fSShirley Ma goto nofrags; 12411da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1242ea2ab693SIan Campbell skb_frag_ref(skb, i); 12431da177e4SLinus Torvalds 124421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 12451da177e4SLinus Torvalds skb_clone_fraglist(skb); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds skb_release_data(skb); 12483e24591aSAlexander Duyck } else { 12493e24591aSAlexander Duyck skb_free_head(skb); 12501fd63041SEric Dumazet } 12511da177e4SLinus Torvalds off = (data + nhead) - skb->head; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds skb->head = data; 1254d3836f21SEric Dumazet skb->head_frag = 0; 12551da177e4SLinus Torvalds skb->data += off; 12564305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 12574305b541SArnaldo Carvalho de Melo skb->end = size; 125856eb8882SPatrick McHardy off = nhead; 12594305b541SArnaldo Carvalho de Melo #else 12604305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 126156eb8882SPatrick McHardy #endif 126227a884dcSArnaldo Carvalho de Melo skb->tail += off; 1263b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 12641da177e4SLinus Torvalds skb->cloned = 0; 1265334a8132SPatrick McHardy skb->hdr_len = 0; 12661da177e4SLinus Torvalds skb->nohdr = 0; 12671da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1268158f323bSEric Dumazet 1269158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1270158f323bSEric Dumazet * For the moment, we really care of rx path, or 1271158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1272158f323bSEric Dumazet */ 1273158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1274158f323bSEric Dumazet skb->truesize += size - osize; 1275158f323bSEric Dumazet 12761da177e4SLinus Torvalds return 0; 12771da177e4SLinus Torvalds 1278a6686f2fSShirley Ma nofrags: 1279a6686f2fSShirley Ma kfree(data); 12801da177e4SLinus Torvalds nodata: 12811da177e4SLinus Torvalds return -ENOMEM; 12821da177e4SLinus Torvalds } 1283b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 12881da177e4SLinus Torvalds { 12891da177e4SLinus Torvalds struct sk_buff *skb2; 12901da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds if (delta <= 0) 12931da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 12941da177e4SLinus Torvalds else { 12951da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 12961da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 12971da177e4SLinus Torvalds GFP_ATOMIC)) { 12981da177e4SLinus Torvalds kfree_skb(skb2); 12991da177e4SLinus Torvalds skb2 = NULL; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds return skb2; 13031da177e4SLinus Torvalds } 1304b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds /** 13071da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 13081da177e4SLinus Torvalds * @skb: buffer to copy 13091da177e4SLinus Torvalds * @newheadroom: new free bytes at head 13101da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 13111da177e4SLinus Torvalds * @gfp_mask: allocation priority 13121da177e4SLinus Torvalds * 13131da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 13141da177e4SLinus Torvalds * allocate additional space. 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 13171da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 13181da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 13191da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 13201da177e4SLinus Torvalds * 13211da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 13221da177e4SLinus Torvalds * is called from an interrupt. 13231da177e4SLinus Torvalds */ 13241da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 132586a76cafSVictor Fusco int newheadroom, int newtailroom, 1326dd0fc66fSAl Viro gfp_t gfp_mask) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds /* 13291da177e4SLinus Torvalds * Allocate the copy buffer 13301da177e4SLinus Torvalds */ 1331c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1332c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1333c93bdd0eSMel Gorman NUMA_NO_NODE); 1334efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 13351da177e4SLinus Torvalds int head_copy_len, head_copy_off; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds if (!n) 13381da177e4SLinus Torvalds return NULL; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds skb_reserve(n, newheadroom); 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds /* Set the tail pointer and length */ 13431da177e4SLinus Torvalds skb_put(n, skb->len); 13441da177e4SLinus Torvalds 1345efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 13461da177e4SLinus Torvalds head_copy_off = 0; 13471da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 13481da177e4SLinus Torvalds head_copy_len = newheadroom; 13491da177e4SLinus Torvalds else 13501da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* Copy the linear header and data. */ 13531da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 13541da177e4SLinus Torvalds skb->len + head_copy_len)) 13551da177e4SLinus Torvalds BUG(); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds copy_skb_header(n, skb); 13581da177e4SLinus Torvalds 1359030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1360efd1e8d5SPatrick McHardy 13611da177e4SLinus Torvalds return n; 13621da177e4SLinus Torvalds } 1363b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /** 1366cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 13671da177e4SLinus Torvalds * @skb: buffer to pad 13681da177e4SLinus Torvalds * @pad: space to pad 1369cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 13701da177e4SLinus Torvalds * 13711da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 13721da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 13731da177e4SLinus Torvalds * beyond the buffer end onto the wire. 13741da177e4SLinus Torvalds * 1375cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1376cd0a137aSFlorian Fainelli * if @free_on_error is true. 13771da177e4SLinus Torvalds */ 13781da177e4SLinus Torvalds 1379cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 13801da177e4SLinus Torvalds { 13815b057c6bSHerbert Xu int err; 13825b057c6bSHerbert Xu int ntail; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 13855b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 13861da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 13875b057c6bSHerbert Xu return 0; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13904305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 13915b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 13925b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 13935b057c6bSHerbert Xu if (unlikely(err)) 13945b057c6bSHerbert Xu goto free_skb; 13955b057c6bSHerbert Xu } 13965b057c6bSHerbert Xu 13975b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 13985b057c6bSHerbert Xu * to be audited. 13995b057c6bSHerbert Xu */ 14005b057c6bSHerbert Xu err = skb_linearize(skb); 14015b057c6bSHerbert Xu if (unlikely(err)) 14025b057c6bSHerbert Xu goto free_skb; 14035b057c6bSHerbert Xu 14045b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 14055b057c6bSHerbert Xu return 0; 14065b057c6bSHerbert Xu 14075b057c6bSHerbert Xu free_skb: 1408cd0a137aSFlorian Fainelli if (free_on_error) 14091da177e4SLinus Torvalds kfree_skb(skb); 14105b057c6bSHerbert Xu return err; 14111da177e4SLinus Torvalds } 1412cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 14131da177e4SLinus Torvalds 14140dde3e16SIlpo Järvinen /** 14150c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 14160c7ddf36SMathias Krause * @skb: start of the buffer to use 14170c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 14180c7ddf36SMathias Krause * @len: amount of data to add 14190c7ddf36SMathias Krause * 14200c7ddf36SMathias Krause * This function extends the used data area of the potentially 14210c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 14220c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 14230c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 14240c7ddf36SMathias Krause * returned. 14250c7ddf36SMathias Krause */ 14260c7ddf36SMathias Krause 14274df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 14280c7ddf36SMathias Krause { 14290c7ddf36SMathias Krause if (tail != skb) { 14300c7ddf36SMathias Krause skb->data_len += len; 14310c7ddf36SMathias Krause skb->len += len; 14320c7ddf36SMathias Krause } 14330c7ddf36SMathias Krause return skb_put(tail, len); 14340c7ddf36SMathias Krause } 14350c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 14360c7ddf36SMathias Krause 14370c7ddf36SMathias Krause /** 14380dde3e16SIlpo Järvinen * skb_put - add data to a buffer 14390dde3e16SIlpo Järvinen * @skb: buffer to use 14400dde3e16SIlpo Järvinen * @len: amount of data to add 14410dde3e16SIlpo Järvinen * 14420dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 14430dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 14440dde3e16SIlpo Järvinen * first byte of the extra data is returned. 14450dde3e16SIlpo Järvinen */ 14464df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 14470dde3e16SIlpo Järvinen { 14484df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 14490dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 14500dde3e16SIlpo Järvinen skb->tail += len; 14510dde3e16SIlpo Järvinen skb->len += len; 14520dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 14530dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 14540dde3e16SIlpo Järvinen return tmp; 14550dde3e16SIlpo Järvinen } 14560dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 14570dde3e16SIlpo Järvinen 14586be8ac2fSIlpo Järvinen /** 1459c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1460c2aa270aSIlpo Järvinen * @skb: buffer to use 1461c2aa270aSIlpo Järvinen * @len: amount of data to add 1462c2aa270aSIlpo Järvinen * 1463c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1464c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1465c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1466c2aa270aSIlpo Järvinen */ 1467d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1468c2aa270aSIlpo Järvinen { 1469c2aa270aSIlpo Järvinen skb->data -= len; 1470c2aa270aSIlpo Järvinen skb->len += len; 1471c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1472c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1473c2aa270aSIlpo Järvinen return skb->data; 1474c2aa270aSIlpo Järvinen } 1475c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1476c2aa270aSIlpo Järvinen 1477c2aa270aSIlpo Järvinen /** 14786be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 14796be8ac2fSIlpo Järvinen * @skb: buffer to use 14806be8ac2fSIlpo Järvinen * @len: amount of data to remove 14816be8ac2fSIlpo Järvinen * 14826be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 14836be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 14846be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 14856be8ac2fSIlpo Järvinen * the old data. 14866be8ac2fSIlpo Järvinen */ 1487af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 14886be8ac2fSIlpo Järvinen { 148947d29646SDavid S. Miller return skb_pull_inline(skb, len); 14906be8ac2fSIlpo Järvinen } 14916be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 14926be8ac2fSIlpo Järvinen 1493419ae74eSIlpo Järvinen /** 1494419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1495419ae74eSIlpo Järvinen * @skb: buffer to alter 1496419ae74eSIlpo Järvinen * @len: new length 1497419ae74eSIlpo Järvinen * 1498419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1499419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1500419ae74eSIlpo Järvinen * The skb must be linear. 1501419ae74eSIlpo Järvinen */ 1502419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1503419ae74eSIlpo Järvinen { 1504419ae74eSIlpo Järvinen if (skb->len > len) 1505419ae74eSIlpo Järvinen __skb_trim(skb, len); 1506419ae74eSIlpo Järvinen } 1507419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1508419ae74eSIlpo Järvinen 15093cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 15101da177e4SLinus Torvalds */ 15111da177e4SLinus Torvalds 15123cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 15131da177e4SLinus Torvalds { 151427b437c8SHerbert Xu struct sk_buff **fragp; 151527b437c8SHerbert Xu struct sk_buff *frag; 15161da177e4SLinus Torvalds int offset = skb_headlen(skb); 15171da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 15181da177e4SLinus Torvalds int i; 151927b437c8SHerbert Xu int err; 152027b437c8SHerbert Xu 152127b437c8SHerbert Xu if (skb_cloned(skb) && 152227b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 152327b437c8SHerbert Xu return err; 15241da177e4SLinus Torvalds 1525f4d26fb3SHerbert Xu i = 0; 1526f4d26fb3SHerbert Xu if (offset >= len) 1527f4d26fb3SHerbert Xu goto drop_pages; 1528f4d26fb3SHerbert Xu 1529f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 15309e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 153127b437c8SHerbert Xu 153227b437c8SHerbert Xu if (end < len) { 15331da177e4SLinus Torvalds offset = end; 153427b437c8SHerbert Xu continue; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15379e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 153827b437c8SHerbert Xu 1539f4d26fb3SHerbert Xu drop_pages: 154027b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 154127b437c8SHerbert Xu 154227b437c8SHerbert Xu for (; i < nfrags; i++) 1543ea2ab693SIan Campbell skb_frag_unref(skb, i); 154427b437c8SHerbert Xu 154521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 154627b437c8SHerbert Xu skb_drop_fraglist(skb); 1547f4d26fb3SHerbert Xu goto done; 154827b437c8SHerbert Xu } 154927b437c8SHerbert Xu 155027b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 155127b437c8SHerbert Xu fragp = &frag->next) { 155227b437c8SHerbert Xu int end = offset + frag->len; 155327b437c8SHerbert Xu 155427b437c8SHerbert Xu if (skb_shared(frag)) { 155527b437c8SHerbert Xu struct sk_buff *nfrag; 155627b437c8SHerbert Xu 155727b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 155827b437c8SHerbert Xu if (unlikely(!nfrag)) 155927b437c8SHerbert Xu return -ENOMEM; 156027b437c8SHerbert Xu 156127b437c8SHerbert Xu nfrag->next = frag->next; 156285bb2a60SEric Dumazet consume_skb(frag); 156327b437c8SHerbert Xu frag = nfrag; 156427b437c8SHerbert Xu *fragp = frag; 156527b437c8SHerbert Xu } 156627b437c8SHerbert Xu 156727b437c8SHerbert Xu if (end < len) { 156827b437c8SHerbert Xu offset = end; 156927b437c8SHerbert Xu continue; 157027b437c8SHerbert Xu } 157127b437c8SHerbert Xu 157227b437c8SHerbert Xu if (end > len && 157327b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 157427b437c8SHerbert Xu return err; 157527b437c8SHerbert Xu 157627b437c8SHerbert Xu if (frag->next) 157727b437c8SHerbert Xu skb_drop_list(&frag->next); 157827b437c8SHerbert Xu break; 157927b437c8SHerbert Xu } 158027b437c8SHerbert Xu 1581f4d26fb3SHerbert Xu done: 158227b437c8SHerbert Xu if (len > skb_headlen(skb)) { 15831da177e4SLinus Torvalds skb->data_len -= skb->len - len; 15841da177e4SLinus Torvalds skb->len = len; 15851da177e4SLinus Torvalds } else { 15861da177e4SLinus Torvalds skb->len = len; 15871da177e4SLinus Torvalds skb->data_len = 0; 158827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 1591c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1592c21b48ccSEric Dumazet skb_condense(skb); 15931da177e4SLinus Torvalds return 0; 15941da177e4SLinus Torvalds } 1595b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /** 15981da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 15991da177e4SLinus Torvalds * @skb: buffer to reallocate 16001da177e4SLinus Torvalds * @delta: number of bytes to advance tail 16011da177e4SLinus Torvalds * 16021da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 16031da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 16041da177e4SLinus Torvalds * data from fragmented part. 16051da177e4SLinus Torvalds * 16061da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 16071da177e4SLinus Torvalds * 16081da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 16091da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 16101da177e4SLinus Torvalds * 16111da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 16121da177e4SLinus Torvalds * reloaded after call to this function. 16131da177e4SLinus Torvalds */ 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 16161da177e4SLinus Torvalds * when it is necessary. 16171da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 16181da177e4SLinus Torvalds * 2. It may change skb pointers. 16191da177e4SLinus Torvalds * 16201da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 16211da177e4SLinus Torvalds */ 1622af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 16251da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 16261da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 16271da177e4SLinus Torvalds */ 16284305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 16311da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 16321da177e4SLinus Torvalds GFP_ATOMIC)) 16331da177e4SLinus Torvalds return NULL; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 163627a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 16371da177e4SLinus Torvalds BUG(); 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 16401da177e4SLinus Torvalds * size of pulled pages. Superb. 16411da177e4SLinus Torvalds */ 164221dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 16431da177e4SLinus Torvalds goto pull_pages; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 16461da177e4SLinus Torvalds eat = delta; 16471da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16489e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16499e903e08SEric Dumazet 16509e903e08SEric Dumazet if (size >= eat) 16511da177e4SLinus Torvalds goto pull_pages; 16529e903e08SEric Dumazet eat -= size; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 16561da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 16571da177e4SLinus Torvalds * but taking into account that pulling is expected to 16581da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 16591da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 16601da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 16611da177e4SLinus Torvalds */ 16621da177e4SLinus Torvalds if (eat) { 16631da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16641da177e4SLinus Torvalds struct sk_buff *clone = NULL; 16651da177e4SLinus Torvalds struct sk_buff *insp = NULL; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds do { 166809a62660SKris Katterjohn BUG_ON(!list); 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds if (list->len <= eat) { 16711da177e4SLinus Torvalds /* Eaten as whole. */ 16721da177e4SLinus Torvalds eat -= list->len; 16731da177e4SLinus Torvalds list = list->next; 16741da177e4SLinus Torvalds insp = list; 16751da177e4SLinus Torvalds } else { 16761da177e4SLinus Torvalds /* Eaten partially. */ 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds if (skb_shared(list)) { 16791da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 16801da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 16811da177e4SLinus Torvalds if (!clone) 16821da177e4SLinus Torvalds return NULL; 16831da177e4SLinus Torvalds insp = list->next; 16841da177e4SLinus Torvalds list = clone; 16851da177e4SLinus Torvalds } else { 16861da177e4SLinus Torvalds /* This may be pulled without 16871da177e4SLinus Torvalds * problems. */ 16881da177e4SLinus Torvalds insp = list; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 16911da177e4SLinus Torvalds kfree_skb(clone); 16921da177e4SLinus Torvalds return NULL; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds break; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds } while (eat); 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds /* Free pulled out fragments. */ 16991da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 17001da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 17011da177e4SLinus Torvalds kfree_skb(list); 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds /* And insert new clone at head. */ 17041da177e4SLinus Torvalds if (clone) { 17051da177e4SLinus Torvalds clone->next = list; 17061da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds pull_pages: 17121da177e4SLinus Torvalds eat = delta; 17131da177e4SLinus Torvalds k = 0; 17141da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17159e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 17169e903e08SEric Dumazet 17179e903e08SEric Dumazet if (size <= eat) { 1718ea2ab693SIan Campbell skb_frag_unref(skb, i); 17199e903e08SEric Dumazet eat -= size; 17201da177e4SLinus Torvalds } else { 17211da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 17221da177e4SLinus Torvalds if (eat) { 17231da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 17249e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 17251da177e4SLinus Torvalds eat = 0; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds k++; 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds skb->tail += delta; 17331da177e4SLinus Torvalds skb->data_len -= delta; 17341da177e4SLinus Torvalds 173527a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 17361da177e4SLinus Torvalds } 1737b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 17381da177e4SLinus Torvalds 173922019b17SEric Dumazet /** 174022019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 174122019b17SEric Dumazet * @skb: source skb 174222019b17SEric Dumazet * @offset: offset in source 174322019b17SEric Dumazet * @to: destination buffer 174422019b17SEric Dumazet * @len: number of bytes to copy 174522019b17SEric Dumazet * 174622019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 174722019b17SEric Dumazet * destination buffer. 174822019b17SEric Dumazet * 174922019b17SEric Dumazet * CAUTION ! : 175022019b17SEric Dumazet * If its prototype is ever changed, 175122019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 175222019b17SEric Dumazet * since it is called from BPF assembly code. 175322019b17SEric Dumazet */ 17541da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 17551da177e4SLinus Torvalds { 17561a028e50SDavid S. Miller int start = skb_headlen(skb); 1757fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1758fbb398a8SDavid S. Miller int i, copy; 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds if (offset > (int)skb->len - len) 17611da177e4SLinus Torvalds goto fault; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds /* Copy header. */ 17641a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 17651da177e4SLinus Torvalds if (copy > len) 17661da177e4SLinus Torvalds copy = len; 1767d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 17681da177e4SLinus Torvalds if ((len -= copy) == 0) 17691da177e4SLinus Torvalds return 0; 17701da177e4SLinus Torvalds offset += copy; 17711da177e4SLinus Torvalds to += copy; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17751a028e50SDavid S. Miller int end; 177651c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17771da177e4SLinus Torvalds 1778547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17791a028e50SDavid S. Miller 178051c56b00SEric Dumazet end = start + skb_frag_size(f); 17811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17821da177e4SLinus Torvalds u8 *vaddr; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds if (copy > len) 17851da177e4SLinus Torvalds copy = len; 17861da177e4SLinus Torvalds 178751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 17881da177e4SLinus Torvalds memcpy(to, 178951c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 179051c56b00SEric Dumazet copy); 179151c56b00SEric Dumazet kunmap_atomic(vaddr); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds if ((len -= copy) == 0) 17941da177e4SLinus Torvalds return 0; 17951da177e4SLinus Torvalds offset += copy; 17961da177e4SLinus Torvalds to += copy; 17971da177e4SLinus Torvalds } 17981a028e50SDavid S. Miller start = end; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 1801fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 18021a028e50SDavid S. Miller int end; 18031da177e4SLinus Torvalds 1804547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18051a028e50SDavid S. Miller 1806fbb398a8SDavid S. Miller end = start + frag_iter->len; 18071da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 18081da177e4SLinus Torvalds if (copy > len) 18091da177e4SLinus Torvalds copy = len; 1810fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 18111da177e4SLinus Torvalds goto fault; 18121da177e4SLinus Torvalds if ((len -= copy) == 0) 18131da177e4SLinus Torvalds return 0; 18141da177e4SLinus Torvalds offset += copy; 18151da177e4SLinus Torvalds to += copy; 18161da177e4SLinus Torvalds } 18171a028e50SDavid S. Miller start = end; 18181da177e4SLinus Torvalds } 1819a6686f2fSShirley Ma 18201da177e4SLinus Torvalds if (!len) 18211da177e4SLinus Torvalds return 0; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds fault: 18241da177e4SLinus Torvalds return -EFAULT; 18251da177e4SLinus Torvalds } 1826b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 18271da177e4SLinus Torvalds 18289c55e01cSJens Axboe /* 18299c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 18309c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 18319c55e01cSJens Axboe */ 18329c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 18339c55e01cSJens Axboe { 18348b9d3728SJarek Poplawski put_page(spd->pages[i]); 18358b9d3728SJarek Poplawski } 18369c55e01cSJens Axboe 1837a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 18384fb66994SJarek Poplawski unsigned int *offset, 183918aafc62SEric Dumazet struct sock *sk) 18408b9d3728SJarek Poplawski { 18415640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 18428b9d3728SJarek Poplawski 18435640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 18448b9d3728SJarek Poplawski return NULL; 18454fb66994SJarek Poplawski 18465640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 18474fb66994SJarek Poplawski 18485640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 18495640f768SEric Dumazet page_address(page) + *offset, *len); 18505640f768SEric Dumazet *offset = pfrag->offset; 18515640f768SEric Dumazet pfrag->offset += *len; 18524fb66994SJarek Poplawski 18535640f768SEric Dumazet return pfrag->page; 18549c55e01cSJens Axboe } 18559c55e01cSJens Axboe 185641c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 185741c73a0dSEric Dumazet struct page *page, 185841c73a0dSEric Dumazet unsigned int offset) 185941c73a0dSEric Dumazet { 186041c73a0dSEric Dumazet return spd->nr_pages && 186141c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 186241c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 186341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 186441c73a0dSEric Dumazet } 186541c73a0dSEric Dumazet 18669c55e01cSJens Axboe /* 18679c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 18689c55e01cSJens Axboe */ 1869a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 187035f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 18714fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 187218aafc62SEric Dumazet bool linear, 18737a67e56fSJarek Poplawski struct sock *sk) 18749c55e01cSJens Axboe { 187541c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1876a108d5f3SDavid S. Miller return true; 18779c55e01cSJens Axboe 18788b9d3728SJarek Poplawski if (linear) { 187918aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 18808b9d3728SJarek Poplawski if (!page) 1881a108d5f3SDavid S. Miller return true; 188241c73a0dSEric Dumazet } 188341c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 188441c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1885a108d5f3SDavid S. Miller return false; 188641c73a0dSEric Dumazet } 18878b9d3728SJarek Poplawski get_page(page); 18889c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 18894fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 18909c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 18919c55e01cSJens Axboe spd->nr_pages++; 18928b9d3728SJarek Poplawski 1893a108d5f3SDavid S. Miller return false; 18949c55e01cSJens Axboe } 18959c55e01cSJens Axboe 1896a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 18972870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 189818aafc62SEric Dumazet unsigned int *len, 1899d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 190035f3d14dSJens Axboe struct sock *sk, 190135f3d14dSJens Axboe struct pipe_inode_info *pipe) 19029c55e01cSJens Axboe { 19032870c43dSOctavian Purdila if (!*len) 1904a108d5f3SDavid S. Miller return true; 19059c55e01cSJens Axboe 19062870c43dSOctavian Purdila /* skip this segment if already processed */ 19072870c43dSOctavian Purdila if (*off >= plen) { 19082870c43dSOctavian Purdila *off -= plen; 1909a108d5f3SDavid S. Miller return false; 19102870c43dSOctavian Purdila } 19112870c43dSOctavian Purdila 19122870c43dSOctavian Purdila /* ignore any bits we already processed */ 19139ca1b22dSEric Dumazet poff += *off; 19149ca1b22dSEric Dumazet plen -= *off; 19152870c43dSOctavian Purdila *off = 0; 19162870c43dSOctavian Purdila 191718aafc62SEric Dumazet do { 191818aafc62SEric Dumazet unsigned int flen = min(*len, plen); 19192870c43dSOctavian Purdila 192018aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 192118aafc62SEric Dumazet linear, sk)) 1922a108d5f3SDavid S. Miller return true; 192318aafc62SEric Dumazet poff += flen; 192418aafc62SEric Dumazet plen -= flen; 19252870c43dSOctavian Purdila *len -= flen; 192618aafc62SEric Dumazet } while (*len && plen); 19272870c43dSOctavian Purdila 1928a108d5f3SDavid S. Miller return false; 1929db43a282SOctavian Purdila } 19309c55e01cSJens Axboe 19319c55e01cSJens Axboe /* 1932a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 19332870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 19349c55e01cSJens Axboe */ 1935a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 193635f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 193735f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 19382870c43dSOctavian Purdila { 19392870c43dSOctavian Purdila int seg; 1940fa9835e5STom Herbert struct sk_buff *iter; 19419c55e01cSJens Axboe 19421d0c0b32SEric Dumazet /* map the linear part : 19432996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 19442996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 19452996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 19469c55e01cSJens Axboe */ 19472870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 19482870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 19492870c43dSOctavian Purdila skb_headlen(skb), 195018aafc62SEric Dumazet offset, len, spd, 19513a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 19521d0c0b32SEric Dumazet sk, pipe)) 1953a108d5f3SDavid S. Miller return true; 19549c55e01cSJens Axboe 19559c55e01cSJens Axboe /* 19569c55e01cSJens Axboe * then map the fragments 19579c55e01cSJens Axboe */ 19589c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 19599c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 19609c55e01cSJens Axboe 1961ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 19629e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 196318aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1964a108d5f3SDavid S. Miller return true; 19659c55e01cSJens Axboe } 19669c55e01cSJens Axboe 1967fa9835e5STom Herbert skb_walk_frags(skb, iter) { 1968fa9835e5STom Herbert if (*offset >= iter->len) { 1969fa9835e5STom Herbert *offset -= iter->len; 1970fa9835e5STom Herbert continue; 1971fa9835e5STom Herbert } 1972fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 1973fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 1974fa9835e5STom Herbert * case. 1975fa9835e5STom Herbert */ 1976fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 1977fa9835e5STom Herbert return true; 1978fa9835e5STom Herbert } 1979fa9835e5STom Herbert 1980a108d5f3SDavid S. Miller return false; 19819c55e01cSJens Axboe } 19829c55e01cSJens Axboe 19839c55e01cSJens Axboe /* 19849c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 1985fa9835e5STom Herbert * the fragments, and the frag list. 19869c55e01cSJens Axboe */ 1987a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 19889c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 198925869262SAl Viro unsigned int flags) 19909c55e01cSJens Axboe { 199141c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 199241c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 19939c55e01cSJens Axboe struct splice_pipe_desc spd = { 19949c55e01cSJens Axboe .pages = pages, 19959c55e01cSJens Axboe .partial = partial, 1996047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 199728a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 19989c55e01cSJens Axboe .spd_release = sock_spd_release, 19999c55e01cSJens Axboe }; 200035f3d14dSJens Axboe int ret = 0; 200135f3d14dSJens Axboe 2002fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 20039c55e01cSJens Axboe 2004a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 200525869262SAl Viro ret = splice_to_pipe(pipe, &spd); 20069c55e01cSJens Axboe 200735f3d14dSJens Axboe return ret; 20089c55e01cSJens Axboe } 20092b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 20109c55e01cSJens Axboe 2011357b40a1SHerbert Xu /** 2012357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2013357b40a1SHerbert Xu * @skb: destination buffer 2014357b40a1SHerbert Xu * @offset: offset in destination 2015357b40a1SHerbert Xu * @from: source buffer 2016357b40a1SHerbert Xu * @len: number of bytes to copy 2017357b40a1SHerbert Xu * 2018357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2019357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2020357b40a1SHerbert Xu * traversing fragment lists and such. 2021357b40a1SHerbert Xu */ 2022357b40a1SHerbert Xu 20230c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2024357b40a1SHerbert Xu { 20251a028e50SDavid S. Miller int start = skb_headlen(skb); 2026fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2027fbb398a8SDavid S. Miller int i, copy; 2028357b40a1SHerbert Xu 2029357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2030357b40a1SHerbert Xu goto fault; 2031357b40a1SHerbert Xu 20321a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2033357b40a1SHerbert Xu if (copy > len) 2034357b40a1SHerbert Xu copy = len; 203527d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2036357b40a1SHerbert Xu if ((len -= copy) == 0) 2037357b40a1SHerbert Xu return 0; 2038357b40a1SHerbert Xu offset += copy; 2039357b40a1SHerbert Xu from += copy; 2040357b40a1SHerbert Xu } 2041357b40a1SHerbert Xu 2042357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2043357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20441a028e50SDavid S. Miller int end; 2045357b40a1SHerbert Xu 2046547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20471a028e50SDavid S. Miller 20489e903e08SEric Dumazet end = start + skb_frag_size(frag); 2049357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2050357b40a1SHerbert Xu u8 *vaddr; 2051357b40a1SHerbert Xu 2052357b40a1SHerbert Xu if (copy > len) 2053357b40a1SHerbert Xu copy = len; 2054357b40a1SHerbert Xu 205551c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20561a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 20571a028e50SDavid S. Miller from, copy); 205851c56b00SEric Dumazet kunmap_atomic(vaddr); 2059357b40a1SHerbert Xu 2060357b40a1SHerbert Xu if ((len -= copy) == 0) 2061357b40a1SHerbert Xu return 0; 2062357b40a1SHerbert Xu offset += copy; 2063357b40a1SHerbert Xu from += copy; 2064357b40a1SHerbert Xu } 20651a028e50SDavid S. Miller start = end; 2066357b40a1SHerbert Xu } 2067357b40a1SHerbert Xu 2068fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20691a028e50SDavid S. Miller int end; 2070357b40a1SHerbert Xu 2071547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20721a028e50SDavid S. Miller 2073fbb398a8SDavid S. Miller end = start + frag_iter->len; 2074357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2075357b40a1SHerbert Xu if (copy > len) 2076357b40a1SHerbert Xu copy = len; 2077fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 20781a028e50SDavid S. Miller from, copy)) 2079357b40a1SHerbert Xu goto fault; 2080357b40a1SHerbert Xu if ((len -= copy) == 0) 2081357b40a1SHerbert Xu return 0; 2082357b40a1SHerbert Xu offset += copy; 2083357b40a1SHerbert Xu from += copy; 2084357b40a1SHerbert Xu } 20851a028e50SDavid S. Miller start = end; 2086357b40a1SHerbert Xu } 2087357b40a1SHerbert Xu if (!len) 2088357b40a1SHerbert Xu return 0; 2089357b40a1SHerbert Xu 2090357b40a1SHerbert Xu fault: 2091357b40a1SHerbert Xu return -EFAULT; 2092357b40a1SHerbert Xu } 2093357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2094357b40a1SHerbert Xu 20951da177e4SLinus Torvalds /* Checksum skb data. */ 20962817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 20972817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 20981da177e4SLinus Torvalds { 20991a028e50SDavid S. Miller int start = skb_headlen(skb); 21001a028e50SDavid S. Miller int i, copy = start - offset; 2101fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21021da177e4SLinus Torvalds int pos = 0; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds /* Checksum header. */ 21051da177e4SLinus Torvalds if (copy > 0) { 21061da177e4SLinus Torvalds if (copy > len) 21071da177e4SLinus Torvalds copy = len; 21082817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 21091da177e4SLinus Torvalds if ((len -= copy) == 0) 21101da177e4SLinus Torvalds return csum; 21111da177e4SLinus Torvalds offset += copy; 21121da177e4SLinus Torvalds pos = copy; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21161a028e50SDavid S. Miller int end; 211751c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 21181da177e4SLinus Torvalds 2119547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21201a028e50SDavid S. Miller 212151c56b00SEric Dumazet end = start + skb_frag_size(frag); 21221da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 212344bb9363SAl Viro __wsum csum2; 21241da177e4SLinus Torvalds u8 *vaddr; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds if (copy > len) 21271da177e4SLinus Torvalds copy = len; 212851c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21292817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 21301a028e50SDavid S. Miller offset - start, copy, 0); 213151c56b00SEric Dumazet kunmap_atomic(vaddr); 21322817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21331da177e4SLinus Torvalds if (!(len -= copy)) 21341da177e4SLinus Torvalds return csum; 21351da177e4SLinus Torvalds offset += copy; 21361da177e4SLinus Torvalds pos += copy; 21371da177e4SLinus Torvalds } 21381a028e50SDavid S. Miller start = end; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 2141fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 21421a028e50SDavid S. Miller int end; 21431da177e4SLinus Torvalds 2144547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21451a028e50SDavid S. Miller 2146fbb398a8SDavid S. Miller end = start + frag_iter->len; 21471da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21485f92a738SAl Viro __wsum csum2; 21491da177e4SLinus Torvalds if (copy > len) 21501da177e4SLinus Torvalds copy = len; 21512817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 21522817a336SDaniel Borkmann copy, 0, ops); 21532817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21541da177e4SLinus Torvalds if ((len -= copy) == 0) 21551da177e4SLinus Torvalds return csum; 21561da177e4SLinus Torvalds offset += copy; 21571da177e4SLinus Torvalds pos += copy; 21581da177e4SLinus Torvalds } 21591a028e50SDavid S. Miller start = end; 21601da177e4SLinus Torvalds } 216109a62660SKris Katterjohn BUG_ON(len); 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds return csum; 21641da177e4SLinus Torvalds } 21652817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 21662817a336SDaniel Borkmann 21672817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 21682817a336SDaniel Borkmann int len, __wsum csum) 21692817a336SDaniel Borkmann { 21702817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2171cea80ea8SDaniel Borkmann .update = csum_partial_ext, 21722817a336SDaniel Borkmann .combine = csum_block_add_ext, 21732817a336SDaniel Borkmann }; 21742817a336SDaniel Borkmann 21752817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 21762817a336SDaniel Borkmann } 2177b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds /* Both of above in one bottle. */ 21801da177e4SLinus Torvalds 218181d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 218281d77662SAl Viro u8 *to, int len, __wsum csum) 21831da177e4SLinus Torvalds { 21841a028e50SDavid S. Miller int start = skb_headlen(skb); 21851a028e50SDavid S. Miller int i, copy = start - offset; 2186fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21871da177e4SLinus Torvalds int pos = 0; 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds /* Copy header. */ 21901da177e4SLinus Torvalds if (copy > 0) { 21911da177e4SLinus Torvalds if (copy > len) 21921da177e4SLinus Torvalds copy = len; 21931da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 21941da177e4SLinus Torvalds copy, csum); 21951da177e4SLinus Torvalds if ((len -= copy) == 0) 21961da177e4SLinus Torvalds return csum; 21971da177e4SLinus Torvalds offset += copy; 21981da177e4SLinus Torvalds to += copy; 21991da177e4SLinus Torvalds pos = copy; 22001da177e4SLinus Torvalds } 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22031a028e50SDavid S. Miller int end; 22041da177e4SLinus Torvalds 2205547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22061a028e50SDavid S. Miller 22079e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 22081da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22095084205fSAl Viro __wsum csum2; 22101da177e4SLinus Torvalds u8 *vaddr; 22111da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds if (copy > len) 22141da177e4SLinus Torvalds copy = len; 221551c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 22161da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 22171a028e50SDavid S. Miller frag->page_offset + 22181a028e50SDavid S. Miller offset - start, to, 22191a028e50SDavid S. Miller copy, 0); 222051c56b00SEric Dumazet kunmap_atomic(vaddr); 22211da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 22221da177e4SLinus Torvalds if (!(len -= copy)) 22231da177e4SLinus Torvalds return csum; 22241da177e4SLinus Torvalds offset += copy; 22251da177e4SLinus Torvalds to += copy; 22261da177e4SLinus Torvalds pos += copy; 22271da177e4SLinus Torvalds } 22281a028e50SDavid S. Miller start = end; 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds 2231fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 223281d77662SAl Viro __wsum csum2; 22331a028e50SDavid S. Miller int end; 22341da177e4SLinus Torvalds 2235547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22361a028e50SDavid S. Miller 2237fbb398a8SDavid S. Miller end = start + frag_iter->len; 22381da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22391da177e4SLinus Torvalds if (copy > len) 22401da177e4SLinus Torvalds copy = len; 2241fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 22421a028e50SDavid S. Miller offset - start, 22431da177e4SLinus Torvalds to, copy, 0); 22441da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 22451da177e4SLinus Torvalds if ((len -= copy) == 0) 22461da177e4SLinus Torvalds return csum; 22471da177e4SLinus Torvalds offset += copy; 22481da177e4SLinus Torvalds to += copy; 22491da177e4SLinus Torvalds pos += copy; 22501da177e4SLinus Torvalds } 22511a028e50SDavid S. Miller start = end; 22521da177e4SLinus Torvalds } 225309a62660SKris Katterjohn BUG_ON(len); 22541da177e4SLinus Torvalds return csum; 22551da177e4SLinus Torvalds } 2256b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 22571da177e4SLinus Torvalds 22589617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 22599617813dSDavide Caratti { 22609617813dSDavide Caratti net_warn_ratelimited( 22619617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 22629617813dSDavide Caratti __func__); 22639617813dSDavide Caratti return 0; 22649617813dSDavide Caratti } 22659617813dSDavide Caratti 22669617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 22679617813dSDavide Caratti int offset, int len) 22689617813dSDavide Caratti { 22699617813dSDavide Caratti net_warn_ratelimited( 22709617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 22719617813dSDavide Caratti __func__); 22729617813dSDavide Caratti return 0; 22739617813dSDavide Caratti } 22749617813dSDavide Caratti 22759617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 22769617813dSDavide Caratti .update = warn_crc32c_csum_update, 22779617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 22789617813dSDavide Caratti }; 22799617813dSDavide Caratti 22809617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 22819617813dSDavide Caratti &default_crc32c_ops; 22829617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 22839617813dSDavide Caratti 2284af2806f8SThomas Graf /** 2285af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2286af2806f8SThomas Graf * @from: source buffer 2287af2806f8SThomas Graf * 2288af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2289af2806f8SThomas Graf * into skb_zerocopy(). 2290af2806f8SThomas Graf */ 2291af2806f8SThomas Graf unsigned int 2292af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2293af2806f8SThomas Graf { 2294af2806f8SThomas Graf unsigned int hlen = 0; 2295af2806f8SThomas Graf 2296af2806f8SThomas Graf if (!from->head_frag || 2297af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2298af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2299af2806f8SThomas Graf hlen = skb_headlen(from); 2300af2806f8SThomas Graf 2301af2806f8SThomas Graf if (skb_has_frag_list(from)) 2302af2806f8SThomas Graf hlen = from->len; 2303af2806f8SThomas Graf 2304af2806f8SThomas Graf return hlen; 2305af2806f8SThomas Graf } 2306af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2307af2806f8SThomas Graf 2308af2806f8SThomas Graf /** 2309af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2310af2806f8SThomas Graf * @to: destination buffer 23117fceb4deSMasanari Iida * @from: source buffer 2312af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2313af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2314af2806f8SThomas Graf * 2315af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2316af2806f8SThomas Graf * to the frags in the source buffer. 2317af2806f8SThomas Graf * 2318af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2319af2806f8SThomas Graf * headroom in the `to` buffer. 232036d5fe6aSZoltan Kiss * 232136d5fe6aSZoltan Kiss * Return value: 232236d5fe6aSZoltan Kiss * 0: everything is OK 232336d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 232436d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2325af2806f8SThomas Graf */ 232636d5fe6aSZoltan Kiss int 232736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2328af2806f8SThomas Graf { 2329af2806f8SThomas Graf int i, j = 0; 2330af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 233136d5fe6aSZoltan Kiss int ret; 2332af2806f8SThomas Graf struct page *page; 2333af2806f8SThomas Graf unsigned int offset; 2334af2806f8SThomas Graf 2335af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2336af2806f8SThomas Graf 2337af2806f8SThomas Graf /* dont bother with small payloads */ 233836d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 233936d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2340af2806f8SThomas Graf 2341af2806f8SThomas Graf if (hlen) { 234236d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 234336d5fe6aSZoltan Kiss if (unlikely(ret)) 234436d5fe6aSZoltan Kiss return ret; 2345af2806f8SThomas Graf len -= hlen; 2346af2806f8SThomas Graf } else { 2347af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2348af2806f8SThomas Graf if (plen) { 2349af2806f8SThomas Graf page = virt_to_head_page(from->head); 2350af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2351af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2352af2806f8SThomas Graf get_page(page); 2353af2806f8SThomas Graf j = 1; 2354af2806f8SThomas Graf len -= plen; 2355af2806f8SThomas Graf } 2356af2806f8SThomas Graf } 2357af2806f8SThomas Graf 2358af2806f8SThomas Graf to->truesize += len + plen; 2359af2806f8SThomas Graf to->len += len + plen; 2360af2806f8SThomas Graf to->data_len += len + plen; 2361af2806f8SThomas Graf 236236d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 236336d5fe6aSZoltan Kiss skb_tx_error(from); 236436d5fe6aSZoltan Kiss return -ENOMEM; 236536d5fe6aSZoltan Kiss } 236636d5fe6aSZoltan Kiss 2367af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2368af2806f8SThomas Graf if (!len) 2369af2806f8SThomas Graf break; 2370af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2371af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2372af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2373af2806f8SThomas Graf skb_frag_ref(to, j); 2374af2806f8SThomas Graf j++; 2375af2806f8SThomas Graf } 2376af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 237736d5fe6aSZoltan Kiss 237836d5fe6aSZoltan Kiss return 0; 2379af2806f8SThomas Graf } 2380af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2381af2806f8SThomas Graf 23821da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 23831da177e4SLinus Torvalds { 2384d3bc23e7SAl Viro __wsum csum; 23851da177e4SLinus Torvalds long csstart; 23861da177e4SLinus Torvalds 238784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 238855508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 23891da177e4SLinus Torvalds else 23901da177e4SLinus Torvalds csstart = skb_headlen(skb); 23911da177e4SLinus Torvalds 239209a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 23931da177e4SLinus Torvalds 2394d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds csum = 0; 23971da177e4SLinus Torvalds if (csstart != skb->len) 23981da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 23991da177e4SLinus Torvalds skb->len - csstart, 0); 24001da177e4SLinus Torvalds 240184fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2402ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 24031da177e4SLinus Torvalds 2404d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 24051da177e4SLinus Torvalds } 24061da177e4SLinus Torvalds } 2407b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds /** 24101da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 24111da177e4SLinus Torvalds * @list: list to dequeue from 24121da177e4SLinus Torvalds * 24131da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 24141da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 24151da177e4SLinus Torvalds * returned or %NULL if the list is empty. 24161da177e4SLinus Torvalds */ 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 24191da177e4SLinus Torvalds { 24201da177e4SLinus Torvalds unsigned long flags; 24211da177e4SLinus Torvalds struct sk_buff *result; 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24241da177e4SLinus Torvalds result = __skb_dequeue(list); 24251da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24261da177e4SLinus Torvalds return result; 24271da177e4SLinus Torvalds } 2428b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds /** 24311da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 24321da177e4SLinus Torvalds * @list: list to dequeue from 24331da177e4SLinus Torvalds * 24341da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 24351da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 24361da177e4SLinus Torvalds * returned or %NULL if the list is empty. 24371da177e4SLinus Torvalds */ 24381da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 24391da177e4SLinus Torvalds { 24401da177e4SLinus Torvalds unsigned long flags; 24411da177e4SLinus Torvalds struct sk_buff *result; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24441da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 24451da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24461da177e4SLinus Torvalds return result; 24471da177e4SLinus Torvalds } 2448b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds /** 24511da177e4SLinus Torvalds * skb_queue_purge - empty a list 24521da177e4SLinus Torvalds * @list: list to empty 24531da177e4SLinus Torvalds * 24541da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 24551da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 24561da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 24571da177e4SLinus Torvalds */ 24581da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 24591da177e4SLinus Torvalds { 24601da177e4SLinus Torvalds struct sk_buff *skb; 24611da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 24621da177e4SLinus Torvalds kfree_skb(skb); 24631da177e4SLinus Torvalds } 2464b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds /** 24679f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 24689f5afeaeSYaogong Wang * @root: root of the rbtree to empty 24699f5afeaeSYaogong Wang * 24709f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 24719f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 24729f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 24739f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 24749f5afeaeSYaogong Wang */ 24759f5afeaeSYaogong Wang void skb_rbtree_purge(struct rb_root *root) 24769f5afeaeSYaogong Wang { 24779f5afeaeSYaogong Wang struct sk_buff *skb, *next; 24789f5afeaeSYaogong Wang 24799f5afeaeSYaogong Wang rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode) 24809f5afeaeSYaogong Wang kfree_skb(skb); 24819f5afeaeSYaogong Wang 24829f5afeaeSYaogong Wang *root = RB_ROOT; 24839f5afeaeSYaogong Wang } 24849f5afeaeSYaogong Wang 24859f5afeaeSYaogong Wang /** 24861da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 24871da177e4SLinus Torvalds * @list: list to use 24881da177e4SLinus Torvalds * @newsk: buffer to queue 24891da177e4SLinus Torvalds * 24901da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 24911da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24921da177e4SLinus Torvalds * safely. 24931da177e4SLinus Torvalds * 24941da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24951da177e4SLinus Torvalds */ 24961da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 24971da177e4SLinus Torvalds { 24981da177e4SLinus Torvalds unsigned long flags; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 25011da177e4SLinus Torvalds __skb_queue_head(list, newsk); 25021da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 25031da177e4SLinus Torvalds } 2504b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds /** 25071da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 25081da177e4SLinus Torvalds * @list: list to use 25091da177e4SLinus Torvalds * @newsk: buffer to queue 25101da177e4SLinus Torvalds * 25111da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 25121da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 25131da177e4SLinus Torvalds * safely. 25141da177e4SLinus Torvalds * 25151da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25161da177e4SLinus Torvalds */ 25171da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 25181da177e4SLinus Torvalds { 25191da177e4SLinus Torvalds unsigned long flags; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 25221da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 25231da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 25241da177e4SLinus Torvalds } 2525b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 25268728b834SDavid S. Miller 25271da177e4SLinus Torvalds /** 25281da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 25291da177e4SLinus Torvalds * @skb: buffer to remove 25308728b834SDavid S. Miller * @list: list to use 25311da177e4SLinus Torvalds * 25328728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 25338728b834SDavid S. Miller * function is atomic with respect to other list locked calls 25341da177e4SLinus Torvalds * 25358728b834SDavid S. Miller * You must know what list the SKB is on. 25361da177e4SLinus Torvalds */ 25378728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 25381da177e4SLinus Torvalds { 25391da177e4SLinus Torvalds unsigned long flags; 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 25428728b834SDavid S. Miller __skb_unlink(skb, list); 25431da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 25441da177e4SLinus Torvalds } 2545b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds /** 25481da177e4SLinus Torvalds * skb_append - append a buffer 25491da177e4SLinus Torvalds * @old: buffer to insert after 25501da177e4SLinus Torvalds * @newsk: buffer to insert 25518728b834SDavid S. Miller * @list: list to use 25521da177e4SLinus Torvalds * 25531da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 25541da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 25551da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25561da177e4SLinus Torvalds */ 25578728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 25581da177e4SLinus Torvalds { 25591da177e4SLinus Torvalds unsigned long flags; 25601da177e4SLinus Torvalds 25618728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 25627de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 25638728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 25641da177e4SLinus Torvalds } 2565b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds /** 25681da177e4SLinus Torvalds * skb_insert - insert a buffer 25691da177e4SLinus Torvalds * @old: buffer to insert before 25701da177e4SLinus Torvalds * @newsk: buffer to insert 25718728b834SDavid S. Miller * @list: list to use 25721da177e4SLinus Torvalds * 25738728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 25748728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 25758728b834SDavid S. Miller * calls. 25768728b834SDavid S. Miller * 25771da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25781da177e4SLinus Torvalds */ 25798728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 25801da177e4SLinus Torvalds { 25811da177e4SLinus Torvalds unsigned long flags; 25821da177e4SLinus Torvalds 25838728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 25848728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 25858728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 25861da177e4SLinus Torvalds } 2587b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 25901da177e4SLinus Torvalds struct sk_buff* skb1, 25911da177e4SLinus Torvalds const u32 len, const int pos) 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds int i; 25941da177e4SLinus Torvalds 2595d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2596d626f62bSArnaldo Carvalho de Melo pos - len); 25971da177e4SLinus Torvalds /* And move data appendix as is. */ 25981da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 25991da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 26021da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 26031da177e4SLinus Torvalds skb1->data_len = skb->data_len; 26041da177e4SLinus Torvalds skb1->len += skb1->data_len; 26051da177e4SLinus Torvalds skb->data_len = 0; 26061da177e4SLinus Torvalds skb->len = len; 260727a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 26111da177e4SLinus Torvalds struct sk_buff* skb1, 26121da177e4SLinus Torvalds const u32 len, int pos) 26131da177e4SLinus Torvalds { 26141da177e4SLinus Torvalds int i, k = 0; 26151da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 26181da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 26191da177e4SLinus Torvalds skb->len = len; 26201da177e4SLinus Torvalds skb->data_len = len - pos; 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 26239e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds if (pos + size > len) { 26261da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 26271da177e4SLinus Torvalds 26281da177e4SLinus Torvalds if (pos < len) { 26291da177e4SLinus Torvalds /* Split frag. 26301da177e4SLinus Torvalds * We have two variants in this case: 26311da177e4SLinus Torvalds * 1. Move all the frag to the second 26321da177e4SLinus Torvalds * part, if it is possible. F.e. 26331da177e4SLinus Torvalds * this approach is mandatory for TUX, 26341da177e4SLinus Torvalds * where splitting is expensive. 26351da177e4SLinus Torvalds * 2. Split is accurately. We make this. 26361da177e4SLinus Torvalds */ 2637ea2ab693SIan Campbell skb_frag_ref(skb, i); 26381da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 26399e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 26409e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 26411da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds k++; 26441da177e4SLinus Torvalds } else 26451da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 26461da177e4SLinus Torvalds pos += size; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26511da177e4SLinus Torvalds /** 26521da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 26531da177e4SLinus Torvalds * @skb: the buffer to split 26541da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 26551da177e4SLinus Torvalds * @len: new length for skb 26561da177e4SLinus Torvalds */ 26571da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 26581da177e4SLinus Torvalds { 26591da177e4SLinus Torvalds int pos = skb_headlen(skb); 26601da177e4SLinus Torvalds 2661fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 2662fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 26631da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 26641da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 26651da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 26661da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 26671da177e4SLinus Torvalds } 2668b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 26691da177e4SLinus Torvalds 26709f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 26719f782db3SIlpo Järvinen * 26729f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 26739f782db3SIlpo Järvinen */ 2674832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2675832d11c5SIlpo Järvinen { 26760ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2677832d11c5SIlpo Järvinen } 2678832d11c5SIlpo Järvinen 2679832d11c5SIlpo Järvinen /** 2680832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2681832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2682832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2683832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2684832d11c5SIlpo Järvinen * 2685832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 268620e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2687832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2688832d11c5SIlpo Järvinen * 2689832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2690832d11c5SIlpo Järvinen * 2691832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2692832d11c5SIlpo Järvinen * to have non-paged data as well. 2693832d11c5SIlpo Järvinen * 2694832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2695832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2696832d11c5SIlpo Järvinen */ 2697832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2698832d11c5SIlpo Järvinen { 2699832d11c5SIlpo Järvinen int from, to, merge, todo; 2700832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2701832d11c5SIlpo Järvinen 2702832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2703f8071cdeSEric Dumazet 2704f8071cdeSEric Dumazet if (skb_headlen(skb)) 2705f8071cdeSEric Dumazet return 0; 2706832d11c5SIlpo Järvinen 2707832d11c5SIlpo Järvinen todo = shiftlen; 2708832d11c5SIlpo Järvinen from = 0; 2709832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2710832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2711832d11c5SIlpo Järvinen 2712832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2713832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2714832d11c5SIlpo Järvinen */ 2715832d11c5SIlpo Järvinen if (!to || 2716ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2717ea2ab693SIan Campbell fragfrom->page_offset)) { 2718832d11c5SIlpo Järvinen merge = -1; 2719832d11c5SIlpo Järvinen } else { 2720832d11c5SIlpo Järvinen merge = to - 1; 2721832d11c5SIlpo Järvinen 27229e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2723832d11c5SIlpo Järvinen if (todo < 0) { 2724832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2725832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2726832d11c5SIlpo Järvinen return 0; 2727832d11c5SIlpo Järvinen 27289f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 27299f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2730832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2731832d11c5SIlpo Järvinen 27329e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 27339e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2734832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2735832d11c5SIlpo Järvinen 2736832d11c5SIlpo Järvinen goto onlymerged; 2737832d11c5SIlpo Järvinen } 2738832d11c5SIlpo Järvinen 2739832d11c5SIlpo Järvinen from++; 2740832d11c5SIlpo Järvinen } 2741832d11c5SIlpo Järvinen 2742832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2743832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2744832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2745832d11c5SIlpo Järvinen return 0; 2746832d11c5SIlpo Järvinen 2747832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2748832d11c5SIlpo Järvinen return 0; 2749832d11c5SIlpo Järvinen 2750832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2751832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2752832d11c5SIlpo Järvinen return 0; 2753832d11c5SIlpo Järvinen 2754832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2755832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2756832d11c5SIlpo Järvinen 27579e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2758832d11c5SIlpo Järvinen *fragto = *fragfrom; 27599e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2760832d11c5SIlpo Järvinen from++; 2761832d11c5SIlpo Järvinen to++; 2762832d11c5SIlpo Järvinen 2763832d11c5SIlpo Järvinen } else { 2764ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2765832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2766832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 27679e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2768832d11c5SIlpo Järvinen 2769832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 27709e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2771832d11c5SIlpo Järvinen todo = 0; 2772832d11c5SIlpo Järvinen 2773832d11c5SIlpo Järvinen to++; 2774832d11c5SIlpo Järvinen break; 2775832d11c5SIlpo Järvinen } 2776832d11c5SIlpo Järvinen } 2777832d11c5SIlpo Järvinen 2778832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2779832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2780832d11c5SIlpo Järvinen 2781832d11c5SIlpo Järvinen if (merge >= 0) { 2782832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2783832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2784832d11c5SIlpo Järvinen 27859e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2786ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2787832d11c5SIlpo Järvinen } 2788832d11c5SIlpo Järvinen 2789832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2790832d11c5SIlpo Järvinen to = 0; 2791832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2792832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2793832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2794832d11c5SIlpo Järvinen 2795832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2796832d11c5SIlpo Järvinen 2797832d11c5SIlpo Järvinen onlymerged: 2798832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2799832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2800832d11c5SIlpo Järvinen */ 2801832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2802832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2803832d11c5SIlpo Järvinen 2804832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2805832d11c5SIlpo Järvinen skb->len -= shiftlen; 2806832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2807832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2808832d11c5SIlpo Järvinen tgt->len += shiftlen; 2809832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2810832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2811832d11c5SIlpo Järvinen 2812832d11c5SIlpo Järvinen return shiftlen; 2813832d11c5SIlpo Järvinen } 2814832d11c5SIlpo Järvinen 2815677e90edSThomas Graf /** 2816677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2817677e90edSThomas Graf * @skb: the buffer to read 2818677e90edSThomas Graf * @from: lower offset of data to be read 2819677e90edSThomas Graf * @to: upper offset of data to be read 2820677e90edSThomas Graf * @st: state variable 2821677e90edSThomas Graf * 2822677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2823677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2824677e90edSThomas Graf */ 2825677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2826677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2827677e90edSThomas Graf { 2828677e90edSThomas Graf st->lower_offset = from; 2829677e90edSThomas Graf st->upper_offset = to; 2830677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2831677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2832677e90edSThomas Graf st->frag_data = NULL; 2833677e90edSThomas Graf } 2834b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2835677e90edSThomas Graf 2836677e90edSThomas Graf /** 2837677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2838677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2839677e90edSThomas Graf * @data: destination pointer for data to be returned 2840677e90edSThomas Graf * @st: state variable 2841677e90edSThomas Graf * 2842bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2843677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2844bc32383cSMathias Krause * the head of the data block to @data and returns the length 2845677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2846677e90edSThomas Graf * offset has been reached. 2847677e90edSThomas Graf * 2848677e90edSThomas Graf * The caller is not required to consume all of the data 2849bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2850677e90edSThomas Graf * of bytes already consumed and the next call to 2851677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2852677e90edSThomas Graf * 285325985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2854e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2855677e90edSThomas Graf * reads of potentially non linear data. 2856677e90edSThomas Graf * 2857bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2858677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2859677e90edSThomas Graf * a stack for this purpose. 2860677e90edSThomas Graf */ 2861677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2862677e90edSThomas Graf struct skb_seq_state *st) 2863677e90edSThomas Graf { 2864677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2865677e90edSThomas Graf skb_frag_t *frag; 2866677e90edSThomas Graf 2867aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2868aeb193eaSWedson Almeida Filho if (st->frag_data) { 2869aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2870aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2871aeb193eaSWedson Almeida Filho } 2872677e90edSThomas Graf return 0; 2873aeb193eaSWedson Almeida Filho } 2874677e90edSThomas Graf 2875677e90edSThomas Graf next_skb: 287695e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2877677e90edSThomas Graf 2878995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 287995e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2880677e90edSThomas Graf return block_limit - abs_offset; 2881677e90edSThomas Graf } 2882677e90edSThomas Graf 2883677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2884677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2885677e90edSThomas Graf 2886677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2887677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 28889e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2889677e90edSThomas Graf 2890677e90edSThomas Graf if (abs_offset < block_limit) { 2891677e90edSThomas Graf if (!st->frag_data) 289251c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2893677e90edSThomas Graf 2894677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2895677e90edSThomas Graf (abs_offset - st->stepped_offset); 2896677e90edSThomas Graf 2897677e90edSThomas Graf return block_limit - abs_offset; 2898677e90edSThomas Graf } 2899677e90edSThomas Graf 2900677e90edSThomas Graf if (st->frag_data) { 290151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2902677e90edSThomas Graf st->frag_data = NULL; 2903677e90edSThomas Graf } 2904677e90edSThomas Graf 2905677e90edSThomas Graf st->frag_idx++; 29069e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2907677e90edSThomas Graf } 2908677e90edSThomas Graf 29095b5a60daSOlaf Kirch if (st->frag_data) { 291051c56b00SEric Dumazet kunmap_atomic(st->frag_data); 29115b5a60daSOlaf Kirch st->frag_data = NULL; 29125b5a60daSOlaf Kirch } 29135b5a60daSOlaf Kirch 291421dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2915677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 291695e3b24cSHerbert Xu st->frag_idx = 0; 2917677e90edSThomas Graf goto next_skb; 291871b3346dSShyam Iyer } else if (st->cur_skb->next) { 291971b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 292071b3346dSShyam Iyer st->frag_idx = 0; 2921677e90edSThomas Graf goto next_skb; 2922677e90edSThomas Graf } 2923677e90edSThomas Graf 2924677e90edSThomas Graf return 0; 2925677e90edSThomas Graf } 2926b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2927677e90edSThomas Graf 2928677e90edSThomas Graf /** 2929677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2930677e90edSThomas Graf * @st: state variable 2931677e90edSThomas Graf * 2932677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2933677e90edSThomas Graf * returned 0. 2934677e90edSThomas Graf */ 2935677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2936677e90edSThomas Graf { 2937677e90edSThomas Graf if (st->frag_data) 293851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2939677e90edSThomas Graf } 2940b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2941677e90edSThomas Graf 29423fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 29433fc7e8a6SThomas Graf 29443fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 29453fc7e8a6SThomas Graf struct ts_config *conf, 29463fc7e8a6SThomas Graf struct ts_state *state) 29473fc7e8a6SThomas Graf { 29483fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 29493fc7e8a6SThomas Graf } 29503fc7e8a6SThomas Graf 29513fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 29523fc7e8a6SThomas Graf { 29533fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 29543fc7e8a6SThomas Graf } 29553fc7e8a6SThomas Graf 29563fc7e8a6SThomas Graf /** 29573fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 29583fc7e8a6SThomas Graf * @skb: the buffer to look in 29593fc7e8a6SThomas Graf * @from: search offset 29603fc7e8a6SThomas Graf * @to: search limit 29613fc7e8a6SThomas Graf * @config: textsearch configuration 29623fc7e8a6SThomas Graf * 29633fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 29643fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 29653fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 29663fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 29673fc7e8a6SThomas Graf */ 29683fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2969059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 29703fc7e8a6SThomas Graf { 2971059a2440SBojan Prtvar struct ts_state state; 2972f72b948dSPhil Oester unsigned int ret; 2973f72b948dSPhil Oester 29743fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 29753fc7e8a6SThomas Graf config->finish = skb_ts_finish; 29763fc7e8a6SThomas Graf 2977059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 29783fc7e8a6SThomas Graf 2979059a2440SBojan Prtvar ret = textsearch_find(config, &state); 2980f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 29813fc7e8a6SThomas Graf } 2982b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 29833fc7e8a6SThomas Graf 2984e89e9cf5SAnanda Raju /** 29852c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2986e89e9cf5SAnanda Raju * @sk: sock structure 2987e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2988e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2989e89e9cf5SAnanda Raju * @from: pointer to user message iov 2990e89e9cf5SAnanda Raju * @length: length of the iov message 2991e89e9cf5SAnanda Raju * 2992e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2993e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2994e89e9cf5SAnanda Raju */ 2995e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2996dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2997e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2998e89e9cf5SAnanda Raju void *from, int length) 2999e89e9cf5SAnanda Raju { 3000b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 3001b2111724SEric Dumazet int copy; 3002e89e9cf5SAnanda Raju int offset = 0; 3003e89e9cf5SAnanda Raju int ret; 3004b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 3005e89e9cf5SAnanda Raju 3006e89e9cf5SAnanda Raju do { 3007e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 3008e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 3009b2111724SEric Dumazet return -EMSGSIZE; 3010e89e9cf5SAnanda Raju 3011b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 3012e89e9cf5SAnanda Raju return -ENOMEM; 3013e89e9cf5SAnanda Raju 3014e89e9cf5SAnanda Raju /* copy the user data to page */ 3015b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 3016e89e9cf5SAnanda Raju 3017b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 3018e89e9cf5SAnanda Raju offset, copy, 0, skb); 3019e89e9cf5SAnanda Raju if (ret < 0) 3020e89e9cf5SAnanda Raju return -EFAULT; 3021e89e9cf5SAnanda Raju 3022e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 3023b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 3024b2111724SEric Dumazet copy); 3025b2111724SEric Dumazet frg_cnt++; 3026b2111724SEric Dumazet pfrag->offset += copy; 3027b2111724SEric Dumazet get_page(pfrag->page); 3028b2111724SEric Dumazet 3029b2111724SEric Dumazet skb->truesize += copy; 303014afee4bSReshetova, Elena refcount_add(copy, &sk->sk_wmem_alloc); 3031e89e9cf5SAnanda Raju skb->len += copy; 3032e89e9cf5SAnanda Raju skb->data_len += copy; 3033e89e9cf5SAnanda Raju offset += copy; 3034e89e9cf5SAnanda Raju length -= copy; 3035e89e9cf5SAnanda Raju 3036e89e9cf5SAnanda Raju } while (length > 0); 3037e89e9cf5SAnanda Raju 3038e89e9cf5SAnanda Raju return 0; 3039e89e9cf5SAnanda Raju } 3040b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 3041e89e9cf5SAnanda Raju 3042be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3043be12a1feSHannes Frederic Sowa int offset, size_t size) 3044be12a1feSHannes Frederic Sowa { 3045be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3046be12a1feSHannes Frederic Sowa 3047be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3048be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3049be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3050be12a1feSHannes Frederic Sowa get_page(page); 3051be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3052be12a1feSHannes Frederic Sowa } else { 3053be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3054be12a1feSHannes Frederic Sowa } 3055be12a1feSHannes Frederic Sowa 3056be12a1feSHannes Frederic Sowa return 0; 3057be12a1feSHannes Frederic Sowa } 3058be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3059be12a1feSHannes Frederic Sowa 3060cbb042f9SHerbert Xu /** 3061cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3062cbb042f9SHerbert Xu * @skb: buffer to update 3063cbb042f9SHerbert Xu * @len: length of data pulled 3064cbb042f9SHerbert Xu * 3065cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3066fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 306784fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 306884fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 306984fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3070cbb042f9SHerbert Xu */ 3071af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3072cbb042f9SHerbert Xu { 307331b33dfbSPravin B Shelar unsigned char *data = skb->data; 307431b33dfbSPravin B Shelar 3075cbb042f9SHerbert Xu BUG_ON(len > skb->len); 307631b33dfbSPravin B Shelar __skb_pull(skb, len); 307731b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 307831b33dfbSPravin B Shelar return skb->data; 3079cbb042f9SHerbert Xu } 3080f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3081f94691acSArnaldo Carvalho de Melo 3082f4c50d99SHerbert Xu /** 3083f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3084df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3085576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3086f4c50d99SHerbert Xu * 3087f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 30884c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 30894c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3090f4c50d99SHerbert Xu */ 3091df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3092df5771ffSMichael S. Tsirkin netdev_features_t features) 3093f4c50d99SHerbert Xu { 3094f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3095f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 30961a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3097df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3098df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3099df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 31001fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3101f4c50d99SHerbert Xu unsigned int offset = doffset; 3102df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3103802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3104f4c50d99SHerbert Xu unsigned int headroom; 3105802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3106ec5f0615SPravin B Shelar __be16 proto; 310736c98382SAlexander Duyck bool csum, sg; 3108df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3109f4c50d99SHerbert Xu int err = -ENOMEM; 3110f4c50d99SHerbert Xu int i = 0; 3111f4c50d99SHerbert Xu int pos; 311253d6471cSVlad Yasevich int dummy; 3113f4c50d99SHerbert Xu 31145882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 311553d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3116ec5f0615SPravin B Shelar if (unlikely(!proto)) 3117ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3118ec5f0615SPravin B Shelar 311936c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3120f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 31217e2b10c1STom Herbert 312207b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 312307b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 312407b26c94SSteffen Klassert struct sk_buff *iter; 312543170c4eSIlan Tayari unsigned int frag_len; 312607b26c94SSteffen Klassert 312707b26c94SSteffen Klassert if (!list_skb || 312807b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 312907b26c94SSteffen Klassert goto normal; 313007b26c94SSteffen Klassert 313143170c4eSIlan Tayari /* If we get here then all the required 313243170c4eSIlan Tayari * GSO features except frag_list are supported. 313343170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 313443170c4eSIlan Tayari * with no frag_list. 313543170c4eSIlan Tayari * Currently we can do that only when the buffers don't 313643170c4eSIlan Tayari * have a linear part and all the buffers except 313743170c4eSIlan Tayari * the last are of the same length. 313807b26c94SSteffen Klassert */ 313943170c4eSIlan Tayari frag_len = list_skb->len; 314007b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 314143170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 314243170c4eSIlan Tayari goto normal; 3143eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 314407b26c94SSteffen Klassert goto normal; 314507b26c94SSteffen Klassert 314607b26c94SSteffen Klassert len -= iter->len; 314707b26c94SSteffen Klassert } 314843170c4eSIlan Tayari 314943170c4eSIlan Tayari if (len != frag_len) 315043170c4eSIlan Tayari goto normal; 315107b26c94SSteffen Klassert } 315207b26c94SSteffen Klassert 3153802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3154802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3155802ab55aSAlexander Duyck * now. 3156802ab55aSAlexander Duyck */ 3157802ab55aSAlexander Duyck partial_segs = len / mss; 3158d7fb5a80SAlexander Duyck if (partial_segs > 1) 3159802ab55aSAlexander Duyck mss *= partial_segs; 3160d7fb5a80SAlexander Duyck else 3161d7fb5a80SAlexander Duyck partial_segs = 0; 3162802ab55aSAlexander Duyck } 3163802ab55aSAlexander Duyck 316407b26c94SSteffen Klassert normal: 3165df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3166df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3167f4c50d99SHerbert Xu 3168f4c50d99SHerbert Xu do { 3169f4c50d99SHerbert Xu struct sk_buff *nskb; 31708cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3171c8884eddSHerbert Xu int hsize; 3172f4c50d99SHerbert Xu int size; 3173f4c50d99SHerbert Xu 31743953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 31753953c46cSMarcelo Ricardo Leitner len = list_skb->len; 31763953c46cSMarcelo Ricardo Leitner } else { 3177df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3178f4c50d99SHerbert Xu if (len > mss) 3179f4c50d99SHerbert Xu len = mss; 31803953c46cSMarcelo Ricardo Leitner } 3181f4c50d99SHerbert Xu 3182df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3183f4c50d99SHerbert Xu if (hsize < 0) 3184f4c50d99SHerbert Xu hsize = 0; 3185c8884eddSHerbert Xu if (hsize > len || !sg) 3186c8884eddSHerbert Xu hsize = len; 3187f4c50d99SHerbert Xu 31881a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 31891a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 31901a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 319189319d38SHerbert Xu 31929d8506ccSHerbert Xu i = 0; 31931a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 31941a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 31951fd819ecSMichael S. Tsirkin frag_skb = list_skb; 31961a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 31979d8506ccSHerbert Xu 31989d8506ccSHerbert Xu while (pos < offset + len) { 31999d8506ccSHerbert Xu BUG_ON(i >= nfrags); 32009d8506ccSHerbert Xu 32014e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 32029d8506ccSHerbert Xu if (pos + size > offset + len) 32039d8506ccSHerbert Xu break; 32049d8506ccSHerbert Xu 32059d8506ccSHerbert Xu i++; 32069d8506ccSHerbert Xu pos += size; 32074e1beba1SMichael S. Tsirkin frag++; 32089d8506ccSHerbert Xu } 32099d8506ccSHerbert Xu 32101a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 32111a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 321289319d38SHerbert Xu 3213f4c50d99SHerbert Xu if (unlikely(!nskb)) 3214f4c50d99SHerbert Xu goto err; 3215f4c50d99SHerbert Xu 32169d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 32179d8506ccSHerbert Xu kfree_skb(nskb); 32189d8506ccSHerbert Xu goto err; 32199d8506ccSHerbert Xu } 32209d8506ccSHerbert Xu 3221ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 322289319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 322389319d38SHerbert Xu kfree_skb(nskb); 322489319d38SHerbert Xu goto err; 322589319d38SHerbert Xu } 322689319d38SHerbert Xu 3227ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 322889319d38SHerbert Xu skb_release_head_state(nskb); 322989319d38SHerbert Xu __skb_push(nskb, doffset); 323089319d38SHerbert Xu } else { 3231c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3232df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3233c93bdd0eSMel Gorman NUMA_NO_NODE); 323489319d38SHerbert Xu 323589319d38SHerbert Xu if (unlikely(!nskb)) 323689319d38SHerbert Xu goto err; 323789319d38SHerbert Xu 323889319d38SHerbert Xu skb_reserve(nskb, headroom); 323989319d38SHerbert Xu __skb_put(nskb, doffset); 324089319d38SHerbert Xu } 324189319d38SHerbert Xu 3242f4c50d99SHerbert Xu if (segs) 3243f4c50d99SHerbert Xu tail->next = nskb; 3244f4c50d99SHerbert Xu else 3245f4c50d99SHerbert Xu segs = nskb; 3246f4c50d99SHerbert Xu tail = nskb; 3247f4c50d99SHerbert Xu 3248df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3249f4c50d99SHerbert Xu 3250030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3251fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 325268c33163SPravin B Shelar 3253df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 325468c33163SPravin B Shelar nskb->data - tnl_hlen, 325568c33163SPravin B Shelar doffset + tnl_hlen); 325689319d38SHerbert Xu 32579d8506ccSHerbert Xu if (nskb->len == len + doffset) 32581cdbcb79SSimon Horman goto perform_csum_check; 325989319d38SHerbert Xu 32607fbeffedSAlexander Duyck if (!sg) { 32617fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 32626f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 326376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 326476443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3265f4c50d99SHerbert Xu skb_put(nskb, len), 3266f4c50d99SHerbert Xu len, 0); 32677e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3268de843723STom Herbert skb_headroom(nskb) + doffset; 3269f4c50d99SHerbert Xu continue; 3270f4c50d99SHerbert Xu } 3271f4c50d99SHerbert Xu 32728cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3273f4c50d99SHerbert Xu 3274df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3275d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3276f4c50d99SHerbert Xu 3277fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3278df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3279cef401deSEric Dumazet 32809d8506ccSHerbert Xu while (pos < offset + len) { 32819d8506ccSHerbert Xu if (i >= nfrags) { 32821a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 32839d8506ccSHerbert Xu 32849d8506ccSHerbert Xu i = 0; 32851a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 32861a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 32871fd819ecSMichael S. Tsirkin frag_skb = list_skb; 32889d8506ccSHerbert Xu 32899d8506ccSHerbert Xu BUG_ON(!nfrags); 32909d8506ccSHerbert Xu 32911a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 32929d8506ccSHerbert Xu } 32939d8506ccSHerbert Xu 32949d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 32959d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 32969d8506ccSHerbert Xu net_warn_ratelimited( 32979d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 32989d8506ccSHerbert Xu pos, mss); 32999d8506ccSHerbert Xu goto err; 33009d8506ccSHerbert Xu } 33019d8506ccSHerbert Xu 33021fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 33031fd819ecSMichael S. Tsirkin goto err; 33041fd819ecSMichael S. Tsirkin 33054e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 33068cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 33078cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3308f4c50d99SHerbert Xu 3309f4c50d99SHerbert Xu if (pos < offset) { 33108cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 33118cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3312f4c50d99SHerbert Xu } 3313f4c50d99SHerbert Xu 331489319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3315f4c50d99SHerbert Xu 3316f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3317f4c50d99SHerbert Xu i++; 33184e1beba1SMichael S. Tsirkin frag++; 3319f4c50d99SHerbert Xu pos += size; 3320f4c50d99SHerbert Xu } else { 33218cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 332289319d38SHerbert Xu goto skip_fraglist; 3323f4c50d99SHerbert Xu } 3324f4c50d99SHerbert Xu 33258cb19905SMichael S. Tsirkin nskb_frag++; 3326f4c50d99SHerbert Xu } 3327f4c50d99SHerbert Xu 332889319d38SHerbert Xu skip_fraglist: 3329f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3330f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3331f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3332ec5f0615SPravin B Shelar 33331cdbcb79SSimon Horman perform_csum_check: 33347fbeffedSAlexander Duyck if (!csum) { 3335ddff00d4SAlexander Duyck if (skb_has_shared_frag(nskb)) { 3336ddff00d4SAlexander Duyck err = __skb_linearize(nskb); 3337ddff00d4SAlexander Duyck if (err) 3338ddff00d4SAlexander Duyck goto err; 3339ddff00d4SAlexander Duyck } 33407fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3341ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 334276443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 334376443456SAlexander Duyck skb_checksum(nskb, doffset, 334476443456SAlexander Duyck nskb->len - doffset, 0); 33457e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 33467e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3347ec5f0615SPravin B Shelar } 3348df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3349f4c50d99SHerbert Xu 3350bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3351bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3352bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3353bec3cfdcSEric Dumazet */ 3354bec3cfdcSEric Dumazet segs->prev = tail; 3355432c856fSToshiaki Makita 3356802ab55aSAlexander Duyck if (partial_segs) { 335707b26c94SSteffen Klassert struct sk_buff *iter; 3358802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 335907b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 3360802ab55aSAlexander Duyck 3361802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 336207b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 3363802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 3364802ab55aSAlexander Duyck 3365802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 3366802ab55aSAlexander Duyck * our way back down the stack of protocols. 3367802ab55aSAlexander Duyck */ 336807b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 336907b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 337007b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 337107b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 337207b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 337307b26c94SSteffen Klassert } 337407b26c94SSteffen Klassert 337507b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 337607b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 337707b26c94SSteffen Klassert else if (tail != segs) 337807b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 3379802ab55aSAlexander Duyck } 3380802ab55aSAlexander Duyck 3381432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3382432c856fSToshiaki Makita * using skb_set_owner_w(). 3383432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3384432c856fSToshiaki Makita */ 3385432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3386432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3387432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3388432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3389432c856fSToshiaki Makita } 3390f4c50d99SHerbert Xu return segs; 3391f4c50d99SHerbert Xu 3392f4c50d99SHerbert Xu err: 3393289dccbeSEric Dumazet kfree_skb_list(segs); 3394f4c50d99SHerbert Xu return ERR_PTR(err); 3395f4c50d99SHerbert Xu } 3396f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3397f4c50d99SHerbert Xu 339871d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 339971d93b39SHerbert Xu { 34008a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 340167147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 340267147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 34038a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 340458025e46SEric Dumazet struct sk_buff *lp, *p = *head; 3405715dc1f3SEric Dumazet unsigned int delta_truesize; 340671d93b39SHerbert Xu 34078a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 340871d93b39SHerbert Xu return -E2BIG; 340971d93b39SHerbert Xu 341029e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 34118a29111cSEric Dumazet pinfo = skb_shinfo(lp); 34128a29111cSEric Dumazet 34138a29111cSEric Dumazet if (headlen <= offset) { 341442da6994SHerbert Xu skb_frag_t *frag; 341566e92fcfSHerbert Xu skb_frag_t *frag2; 34169aaa156cSHerbert Xu int i = skbinfo->nr_frags; 34179aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 341842da6994SHerbert Xu 341966e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 34208a29111cSEric Dumazet goto merge; 342181705ad1SHerbert Xu 34228a29111cSEric Dumazet offset -= headlen; 34239aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 34249aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3425f5572068SHerbert Xu 34269aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 34279aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 342866e92fcfSHerbert Xu do { 342966e92fcfSHerbert Xu *--frag = *--frag2; 343066e92fcfSHerbert Xu } while (--i); 343166e92fcfSHerbert Xu 343266e92fcfSHerbert Xu frag->page_offset += offset; 34339e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 343466e92fcfSHerbert Xu 3435715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3436ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3437ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3438715dc1f3SEric Dumazet 3439f5572068SHerbert Xu skb->truesize -= skb->data_len; 3440f5572068SHerbert Xu skb->len -= skb->data_len; 3441f5572068SHerbert Xu skb->data_len = 0; 3442f5572068SHerbert Xu 3443715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 34445d38a079SHerbert Xu goto done; 3445d7e8883cSEric Dumazet } else if (skb->head_frag) { 3446d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3447d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3448d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3449d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3450d7e8883cSEric Dumazet unsigned int first_offset; 3451d7e8883cSEric Dumazet 3452d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 34538a29111cSEric Dumazet goto merge; 3454d7e8883cSEric Dumazet 3455d7e8883cSEric Dumazet first_offset = skb->data - 3456d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3457d7e8883cSEric Dumazet offset; 3458d7e8883cSEric Dumazet 3459d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3460d7e8883cSEric Dumazet 3461d7e8883cSEric Dumazet frag->page.p = page; 3462d7e8883cSEric Dumazet frag->page_offset = first_offset; 3463d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3464d7e8883cSEric Dumazet 3465d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3466d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3467d7e8883cSEric Dumazet 3468715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3469d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3470d7e8883cSEric Dumazet goto done; 34718a29111cSEric Dumazet } 347271d93b39SHerbert Xu 347371d93b39SHerbert Xu merge: 3474715dc1f3SEric Dumazet delta_truesize = skb->truesize; 347567147ba9SHerbert Xu if (offset > headlen) { 3476d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3477d1dc7abfSMichal Schmidt 3478d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 34799e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3480d1dc7abfSMichal Schmidt skb->data_len -= eat; 3481d1dc7abfSMichal Schmidt skb->len -= eat; 348267147ba9SHerbert Xu offset = headlen; 348356035022SHerbert Xu } 348456035022SHerbert Xu 348567147ba9SHerbert Xu __skb_pull(skb, offset); 348656035022SHerbert Xu 348729e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 34888a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 34898a29111cSEric Dumazet else 3490c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3491c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3492f4a775d1SEric Dumazet __skb_header_release(skb); 34938a29111cSEric Dumazet lp = p; 349471d93b39SHerbert Xu 34955d38a079SHerbert Xu done: 34965d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 349737fe4732SHerbert Xu p->data_len += len; 3498715dc1f3SEric Dumazet p->truesize += delta_truesize; 349937fe4732SHerbert Xu p->len += len; 35008a29111cSEric Dumazet if (lp != p) { 35018a29111cSEric Dumazet lp->data_len += len; 35028a29111cSEric Dumazet lp->truesize += delta_truesize; 35038a29111cSEric Dumazet lp->len += len; 35048a29111cSEric Dumazet } 350571d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 350671d93b39SHerbert Xu return 0; 350771d93b39SHerbert Xu } 350857c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive); 350971d93b39SHerbert Xu 35101da177e4SLinus Torvalds void __init skb_init(void) 35111da177e4SLinus Torvalds { 35121da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 35131da177e4SLinus Torvalds sizeof(struct sk_buff), 35141da177e4SLinus Torvalds 0, 3515e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 351620c2df83SPaul Mundt NULL); 3517d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3518d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3519d179cd12SDavid S. Miller 0, 3520e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 352120c2df83SPaul Mundt NULL); 35221da177e4SLinus Torvalds } 35231da177e4SLinus Torvalds 352451c739d1SDavid S. Miller static int 352548a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 352648a1df65SJason A. Donenfeld unsigned int recursion_level) 3527716ea3a7SDavid Howells { 35281a028e50SDavid S. Miller int start = skb_headlen(skb); 35291a028e50SDavid S. Miller int i, copy = start - offset; 3530fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3531716ea3a7SDavid Howells int elt = 0; 3532716ea3a7SDavid Howells 353348a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 353448a1df65SJason A. Donenfeld return -EMSGSIZE; 353548a1df65SJason A. Donenfeld 3536716ea3a7SDavid Howells if (copy > 0) { 3537716ea3a7SDavid Howells if (copy > len) 3538716ea3a7SDavid Howells copy = len; 3539642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3540716ea3a7SDavid Howells elt++; 3541716ea3a7SDavid Howells if ((len -= copy) == 0) 3542716ea3a7SDavid Howells return elt; 3543716ea3a7SDavid Howells offset += copy; 3544716ea3a7SDavid Howells } 3545716ea3a7SDavid Howells 3546716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 35471a028e50SDavid S. Miller int end; 3548716ea3a7SDavid Howells 3549547b792cSIlpo Järvinen WARN_ON(start > offset + len); 35501a028e50SDavid S. Miller 35519e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3552716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3553716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 355448a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 355548a1df65SJason A. Donenfeld return -EMSGSIZE; 3556716ea3a7SDavid Howells 3557716ea3a7SDavid Howells if (copy > len) 3558716ea3a7SDavid Howells copy = len; 3559ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3560642f1490SJens Axboe frag->page_offset+offset-start); 3561716ea3a7SDavid Howells elt++; 3562716ea3a7SDavid Howells if (!(len -= copy)) 3563716ea3a7SDavid Howells return elt; 3564716ea3a7SDavid Howells offset += copy; 3565716ea3a7SDavid Howells } 35661a028e50SDavid S. Miller start = end; 3567716ea3a7SDavid Howells } 3568716ea3a7SDavid Howells 3569fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 357048a1df65SJason A. Donenfeld int end, ret; 3571716ea3a7SDavid Howells 3572547b792cSIlpo Järvinen WARN_ON(start > offset + len); 35731a028e50SDavid S. Miller 3574fbb398a8SDavid S. Miller end = start + frag_iter->len; 3575716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 357648a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 357748a1df65SJason A. Donenfeld return -EMSGSIZE; 357848a1df65SJason A. Donenfeld 3579716ea3a7SDavid Howells if (copy > len) 3580716ea3a7SDavid Howells copy = len; 358148a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 358248a1df65SJason A. Donenfeld copy, recursion_level + 1); 358348a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 358448a1df65SJason A. Donenfeld return ret; 358548a1df65SJason A. Donenfeld elt += ret; 3586716ea3a7SDavid Howells if ((len -= copy) == 0) 3587716ea3a7SDavid Howells return elt; 3588716ea3a7SDavid Howells offset += copy; 3589716ea3a7SDavid Howells } 35901a028e50SDavid S. Miller start = end; 3591716ea3a7SDavid Howells } 3592716ea3a7SDavid Howells BUG_ON(len); 3593716ea3a7SDavid Howells return elt; 3594716ea3a7SDavid Howells } 3595716ea3a7SDavid Howells 359648a1df65SJason A. Donenfeld /** 359748a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 359848a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 359948a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 360048a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 360148a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 360248a1df65SJason A. Donenfeld * 360348a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 360448a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 360548a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 360648a1df65SJason A. Donenfeld * could not fit. 360748a1df65SJason A. Donenfeld */ 360848a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 360948a1df65SJason A. Donenfeld { 361048a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 361148a1df65SJason A. Donenfeld 361248a1df65SJason A. Donenfeld if (nsg <= 0) 361348a1df65SJason A. Donenfeld return nsg; 361448a1df65SJason A. Donenfeld 361548a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 361648a1df65SJason A. Donenfeld 361748a1df65SJason A. Donenfeld return nsg; 361848a1df65SJason A. Donenfeld } 361948a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 362048a1df65SJason A. Donenfeld 362125a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 362225a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 362325a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 362425a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 362525a91d8dSFan Du * 362625a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 362725a91d8dSFan Du * 1. sg_init_table 362825a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 362925a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 363025a91d8dSFan Du * 363125a91d8dSFan Du * This is equivalent to: 363225a91d8dSFan Du * 1. sg_init_table 363325a91d8dSFan Du * 2. skb_to_sgvec(payload1) 363425a91d8dSFan Du * 3. sg_unmark_end 363525a91d8dSFan Du * 4. skb_to_sgvec(payload2) 363625a91d8dSFan Du * 363725a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 363825a91d8dSFan Du * is more preferable. 363925a91d8dSFan Du */ 364025a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 364125a91d8dSFan Du int offset, int len) 364225a91d8dSFan Du { 364348a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 364425a91d8dSFan Du } 364525a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 364625a91d8dSFan Du 364751c739d1SDavid S. Miller 364851c739d1SDavid S. Miller 3649716ea3a7SDavid Howells /** 3650716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3651716ea3a7SDavid Howells * @skb: The socket buffer to check. 3652716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3653716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3654716ea3a7SDavid Howells * 3655716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3656716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3657716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3658716ea3a7SDavid Howells * 3659716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3660716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3661716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3662716ea3a7SDavid Howells * 3663716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3664716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3665716ea3a7SDavid Howells */ 3666716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3667716ea3a7SDavid Howells { 3668716ea3a7SDavid Howells int copyflag; 3669716ea3a7SDavid Howells int elt; 3670716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3671716ea3a7SDavid Howells 3672716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3673716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3674716ea3a7SDavid Howells * at the moment even if they are anonymous). 3675716ea3a7SDavid Howells */ 3676716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3677716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3678716ea3a7SDavid Howells return -ENOMEM; 3679716ea3a7SDavid Howells 3680716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 368121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3682716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3683716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3684716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3685716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3686716ea3a7SDavid Howells 3687716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3688716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3689716ea3a7SDavid Howells return -ENOMEM; 3690716ea3a7SDavid Howells 3691716ea3a7SDavid Howells /* Voila! */ 3692716ea3a7SDavid Howells *trailer = skb; 3693716ea3a7SDavid Howells return 1; 3694716ea3a7SDavid Howells } 3695716ea3a7SDavid Howells 3696716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3697716ea3a7SDavid Howells 3698716ea3a7SDavid Howells elt = 1; 3699716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3700716ea3a7SDavid Howells copyflag = 0; 3701716ea3a7SDavid Howells 3702716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3703716ea3a7SDavid Howells int ntail = 0; 3704716ea3a7SDavid Howells 3705716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3706716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3707716ea3a7SDavid Howells * after it. */ 3708716ea3a7SDavid Howells 3709716ea3a7SDavid Howells if (skb_shared(skb1)) 3710716ea3a7SDavid Howells copyflag = 1; 3711716ea3a7SDavid Howells 3712716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3713716ea3a7SDavid Howells 3714716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3715716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 371621dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3717716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3718716ea3a7SDavid Howells ntail = tailbits + 128; 3719716ea3a7SDavid Howells } 3720716ea3a7SDavid Howells 3721716ea3a7SDavid Howells if (copyflag || 3722716ea3a7SDavid Howells skb_cloned(skb1) || 3723716ea3a7SDavid Howells ntail || 3724716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 372521dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3726716ea3a7SDavid Howells struct sk_buff *skb2; 3727716ea3a7SDavid Howells 3728716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3729716ea3a7SDavid Howells if (ntail == 0) 3730716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3731716ea3a7SDavid Howells else 3732716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3733716ea3a7SDavid Howells skb_headroom(skb1), 3734716ea3a7SDavid Howells ntail, 3735716ea3a7SDavid Howells GFP_ATOMIC); 3736716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3737716ea3a7SDavid Howells return -ENOMEM; 3738716ea3a7SDavid Howells 3739716ea3a7SDavid Howells if (skb1->sk) 3740716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3741716ea3a7SDavid Howells 3742716ea3a7SDavid Howells /* Looking around. Are we still alive? 3743716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3744716ea3a7SDavid Howells 3745716ea3a7SDavid Howells skb2->next = skb1->next; 3746716ea3a7SDavid Howells *skb_p = skb2; 3747716ea3a7SDavid Howells kfree_skb(skb1); 3748716ea3a7SDavid Howells skb1 = skb2; 3749716ea3a7SDavid Howells } 3750716ea3a7SDavid Howells elt++; 3751716ea3a7SDavid Howells *trailer = skb1; 3752716ea3a7SDavid Howells skb_p = &skb1->next; 3753716ea3a7SDavid Howells } 3754716ea3a7SDavid Howells 3755716ea3a7SDavid Howells return elt; 3756716ea3a7SDavid Howells } 3757b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3758716ea3a7SDavid Howells 3759b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3760b1faf566SEric Dumazet { 3761b1faf566SEric Dumazet struct sock *sk = skb->sk; 3762b1faf566SEric Dumazet 3763b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3764b1faf566SEric Dumazet } 3765b1faf566SEric Dumazet 37668605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 37678605330aSSoheil Hassas Yeganeh { 37688605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 37698605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 37708605330aSSoheil Hassas Yeganeh */ 37718605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 37728605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 37738605330aSSoheil Hassas Yeganeh } 37748605330aSSoheil Hassas Yeganeh 3775b1faf566SEric Dumazet /* 3776b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3777b1faf566SEric Dumazet */ 3778b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3779b1faf566SEric Dumazet { 3780b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 378195c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3782b1faf566SEric Dumazet return -ENOMEM; 3783b1faf566SEric Dumazet 3784b1faf566SEric Dumazet skb_orphan(skb); 3785b1faf566SEric Dumazet skb->sk = sk; 3786b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3787b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 37888605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 3789b1faf566SEric Dumazet 3790abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3791abb57ea4SEric Dumazet skb_dst_force(skb); 3792abb57ea4SEric Dumazet 3793b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3794b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3795676d2369SDavid S. Miller sk->sk_data_ready(sk); 3796b1faf566SEric Dumazet return 0; 3797b1faf566SEric Dumazet } 3798b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3799b1faf566SEric Dumazet 380083a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 380183a1a1a7SSoheil Hassas Yeganeh { 380283a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 380383a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 380483a1a1a7SSoheil Hassas Yeganeh } 380583a1a1a7SSoheil Hassas Yeganeh 3806364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3807364a9e93SWillem de Bruijn { 3808364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 380983a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 381083a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 3811997d5c3fSEric Dumazet unsigned long flags; 3812364a9e93SWillem de Bruijn 3813997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 3814364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 381538b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 381683a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 381738b25793SSoheil Hassas Yeganeh if (icmp_next) 381838b25793SSoheil Hassas Yeganeh sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 381938b25793SSoheil Hassas Yeganeh } 3820997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 3821364a9e93SWillem de Bruijn 382283a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 382383a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 382483a1a1a7SSoheil Hassas Yeganeh 382583a1a1a7SSoheil Hassas Yeganeh if (skb_next) 3826364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3827364a9e93SWillem de Bruijn 3828364a9e93SWillem de Bruijn return skb; 3829364a9e93SWillem de Bruijn } 3830364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3831364a9e93SWillem de Bruijn 3832cab41c47SAlexander Duyck /** 3833cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3834cab41c47SAlexander Duyck * @skb: the skb to clone 3835cab41c47SAlexander Duyck * 3836cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3837cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3838cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3839cab41c47SAlexander Duyck * 3840cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3841cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3842cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3843cab41c47SAlexander Duyck * the sk_error_queue. 3844cab41c47SAlexander Duyck */ 384562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 384662bccb8cSAlexander Duyck { 384762bccb8cSAlexander Duyck struct sock *sk = skb->sk; 384862bccb8cSAlexander Duyck struct sk_buff *clone; 384962bccb8cSAlexander Duyck 385041c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 385162bccb8cSAlexander Duyck return NULL; 385262bccb8cSAlexander Duyck 385362bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 385462bccb8cSAlexander Duyck if (!clone) { 385562bccb8cSAlexander Duyck sock_put(sk); 385662bccb8cSAlexander Duyck return NULL; 385762bccb8cSAlexander Duyck } 385862bccb8cSAlexander Duyck 385962bccb8cSAlexander Duyck clone->sk = sk; 386062bccb8cSAlexander Duyck clone->destructor = sock_efree; 386162bccb8cSAlexander Duyck 386262bccb8cSAlexander Duyck return clone; 386362bccb8cSAlexander Duyck } 386462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 386562bccb8cSAlexander Duyck 386637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 386737846ef0SAlexander Duyck struct sock *sk, 38684ef1b286SSoheil Hassas Yeganeh int tstype, 38694ef1b286SSoheil Hassas Yeganeh bool opt_stats) 3870ac45f602SPatrick Ohly { 3871ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3872ac45f602SPatrick Ohly int err; 3873ac45f602SPatrick Ohly 38744ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 38754ef1b286SSoheil Hassas Yeganeh 3876ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3877ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3878ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3879ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3880e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 38814ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 38821862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 38834ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 388409c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 3885ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 3886ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 38874ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 38884ed2d765SWillem de Bruijn } 388929030374SEric Dumazet 3890ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 389129030374SEric Dumazet 3892ac45f602SPatrick Ohly if (err) 3893ac45f602SPatrick Ohly kfree_skb(skb); 3894ac45f602SPatrick Ohly } 389537846ef0SAlexander Duyck 3896b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 3897b245be1fSWillem de Bruijn { 3898b245be1fSWillem de Bruijn bool ret; 3899b245be1fSWillem de Bruijn 3900b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 3901b245be1fSWillem de Bruijn return true; 3902b245be1fSWillem de Bruijn 3903b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 3904b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 3905b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 3906b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 3907b245be1fSWillem de Bruijn return ret; 3908b245be1fSWillem de Bruijn } 3909b245be1fSWillem de Bruijn 391037846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 391137846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 391237846ef0SAlexander Duyck { 391337846ef0SAlexander Duyck struct sock *sk = skb->sk; 391437846ef0SAlexander Duyck 3915b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 3916b245be1fSWillem de Bruijn return; 3917b245be1fSWillem de Bruijn 39189ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 39199ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 39209ac25fc0SEric Dumazet */ 392141c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 392237846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 39234ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 392437846ef0SAlexander Duyck sock_put(sk); 392537846ef0SAlexander Duyck } 39269ac25fc0SEric Dumazet } 392737846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 392837846ef0SAlexander Duyck 392937846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 393037846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 393137846ef0SAlexander Duyck struct sock *sk, int tstype) 393237846ef0SAlexander Duyck { 393337846ef0SAlexander Duyck struct sk_buff *skb; 39344ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 393537846ef0SAlexander Duyck 39363a8dd971SWillem de Bruijn if (!sk) 39373a8dd971SWillem de Bruijn return; 39383a8dd971SWillem de Bruijn 3939b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 3940b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 3941b50a5c70SMiroslav Lichvar return; 3942b50a5c70SMiroslav Lichvar 39433a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 39443a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 394537846ef0SAlexander Duyck return; 394637846ef0SAlexander Duyck 39471c885808SFrancis Yan if (tsonly) { 39481c885808SFrancis Yan #ifdef CONFIG_INET 39491c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 39501c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 39514ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 39521c885808SFrancis Yan skb = tcp_get_timestamping_opt_stats(sk); 39534ef1b286SSoheil Hassas Yeganeh opt_stats = true; 39544ef1b286SSoheil Hassas Yeganeh } else 39551c885808SFrancis Yan #endif 39561c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 39571c885808SFrancis Yan } else { 395837846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 39591c885808SFrancis Yan } 396037846ef0SAlexander Duyck if (!skb) 396137846ef0SAlexander Duyck return; 396237846ef0SAlexander Duyck 396349ca0d8bSWillem de Bruijn if (tsonly) { 3964fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 3965fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 396649ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 396749ca0d8bSWillem de Bruijn } 396849ca0d8bSWillem de Bruijn 396949ca0d8bSWillem de Bruijn if (hwtstamps) 397049ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 397149ca0d8bSWillem de Bruijn else 397249ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 397349ca0d8bSWillem de Bruijn 39744ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 397537846ef0SAlexander Duyck } 3976e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3977e7fd2885SWillem de Bruijn 3978e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3979e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3980e7fd2885SWillem de Bruijn { 3981e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3982e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3983e7fd2885SWillem de Bruijn } 3984ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3985ac45f602SPatrick Ohly 39866e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 39876e3e939fSJohannes Berg { 39886e3e939fSJohannes Berg struct sock *sk = skb->sk; 39896e3e939fSJohannes Berg struct sock_exterr_skb *serr; 3990dd4f1072SEric Dumazet int err = 1; 39916e3e939fSJohannes Berg 39926e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 39936e3e939fSJohannes Berg skb->wifi_acked = acked; 39946e3e939fSJohannes Berg 39956e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 39966e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 39976e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 39986e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 39996e3e939fSJohannes Berg 4000dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4001dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4002dd4f1072SEric Dumazet */ 400341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 40046e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4005dd4f1072SEric Dumazet sock_put(sk); 4006dd4f1072SEric Dumazet } 40076e3e939fSJohannes Berg if (err) 40086e3e939fSJohannes Berg kfree_skb(skb); 40096e3e939fSJohannes Berg } 40106e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 40116e3e939fSJohannes Berg 4012f35d9d8aSRusty Russell /** 4013f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4014f35d9d8aSRusty Russell * @skb: the skb to set 4015f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4016f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4017f35d9d8aSRusty Russell * 4018f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4019f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4020f35d9d8aSRusty Russell * 4021f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4022f35d9d8aSRusty Russell * returns false you should drop the packet. 4023f35d9d8aSRusty Russell */ 4024f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4025f35d9d8aSRusty Russell { 40265ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 40275ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 4028e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 40295ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 4030f35d9d8aSRusty Russell return false; 4031f35d9d8aSRusty Russell } 4032f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 4033f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 4034f35d9d8aSRusty Russell skb->csum_offset = off; 4035e5d5decaSJason Wang skb_set_transport_header(skb, start); 4036f35d9d8aSRusty Russell return true; 4037f35d9d8aSRusty Russell } 4038b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4039f35d9d8aSRusty Russell 4040ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4041ed1f50c3SPaul Durrant unsigned int max) 4042ed1f50c3SPaul Durrant { 4043ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4044ed1f50c3SPaul Durrant return 0; 4045ed1f50c3SPaul Durrant 4046ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4047ed1f50c3SPaul Durrant * won't need to do it again. 4048ed1f50c3SPaul Durrant */ 4049ed1f50c3SPaul Durrant if (max > skb->len) 4050ed1f50c3SPaul Durrant max = skb->len; 4051ed1f50c3SPaul Durrant 4052ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4053ed1f50c3SPaul Durrant return -ENOMEM; 4054ed1f50c3SPaul Durrant 4055ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4056ed1f50c3SPaul Durrant return -EPROTO; 4057ed1f50c3SPaul Durrant 4058ed1f50c3SPaul Durrant return 0; 4059ed1f50c3SPaul Durrant } 4060ed1f50c3SPaul Durrant 4061f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4062f9708b43SJan Beulich 4063f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4064f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4065f9708b43SJan Beulich unsigned int off) 4066f9708b43SJan Beulich { 4067f9708b43SJan Beulich switch (proto) { 4068f9708b43SJan Beulich int err; 4069f9708b43SJan Beulich 4070f9708b43SJan Beulich case IPPROTO_TCP: 4071f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4072f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4073f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4074f9708b43SJan Beulich offsetof(struct tcphdr, 4075f9708b43SJan Beulich check))) 4076f9708b43SJan Beulich err = -EPROTO; 4077f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4078f9708b43SJan Beulich 4079f9708b43SJan Beulich case IPPROTO_UDP: 4080f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4081f9708b43SJan Beulich off + sizeof(struct udphdr)); 4082f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4083f9708b43SJan Beulich offsetof(struct udphdr, 4084f9708b43SJan Beulich check))) 4085f9708b43SJan Beulich err = -EPROTO; 4086f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4087f9708b43SJan Beulich } 4088f9708b43SJan Beulich 4089f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4090f9708b43SJan Beulich } 4091f9708b43SJan Beulich 4092ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4093ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4094ed1f50c3SPaul Durrant */ 4095ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4096ed1f50c3SPaul Durrant 4097f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4098ed1f50c3SPaul Durrant { 4099ed1f50c3SPaul Durrant unsigned int off; 4100ed1f50c3SPaul Durrant bool fragment; 4101f9708b43SJan Beulich __sum16 *csum; 4102ed1f50c3SPaul Durrant int err; 4103ed1f50c3SPaul Durrant 4104ed1f50c3SPaul Durrant fragment = false; 4105ed1f50c3SPaul Durrant 4106ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4107ed1f50c3SPaul Durrant sizeof(struct iphdr), 4108ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4109ed1f50c3SPaul Durrant if (err < 0) 4110ed1f50c3SPaul Durrant goto out; 4111ed1f50c3SPaul Durrant 4112ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4113ed1f50c3SPaul Durrant fragment = true; 4114ed1f50c3SPaul Durrant 4115ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4116ed1f50c3SPaul Durrant 4117ed1f50c3SPaul Durrant err = -EPROTO; 4118ed1f50c3SPaul Durrant 4119ed1f50c3SPaul Durrant if (fragment) 4120ed1f50c3SPaul Durrant goto out; 4121ed1f50c3SPaul Durrant 4122f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4123f9708b43SJan Beulich if (IS_ERR(csum)) 4124f9708b43SJan Beulich return PTR_ERR(csum); 4125ed1f50c3SPaul Durrant 4126ed1f50c3SPaul Durrant if (recalculate) 4127f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4128ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4129ed1f50c3SPaul Durrant skb->len - off, 4130f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4131ed1f50c3SPaul Durrant err = 0; 4132ed1f50c3SPaul Durrant 4133ed1f50c3SPaul Durrant out: 4134ed1f50c3SPaul Durrant return err; 4135ed1f50c3SPaul Durrant } 4136ed1f50c3SPaul Durrant 4137ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4138ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4139ed1f50c3SPaul Durrant */ 4140ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4141ed1f50c3SPaul Durrant 4142ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4143ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4144ed1f50c3SPaul Durrant 4145ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4146ed1f50c3SPaul Durrant { 4147ed1f50c3SPaul Durrant int err; 4148ed1f50c3SPaul Durrant u8 nexthdr; 4149ed1f50c3SPaul Durrant unsigned int off; 4150ed1f50c3SPaul Durrant unsigned int len; 4151ed1f50c3SPaul Durrant bool fragment; 4152ed1f50c3SPaul Durrant bool done; 4153f9708b43SJan Beulich __sum16 *csum; 4154ed1f50c3SPaul Durrant 4155ed1f50c3SPaul Durrant fragment = false; 4156ed1f50c3SPaul Durrant done = false; 4157ed1f50c3SPaul Durrant 4158ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4159ed1f50c3SPaul Durrant 4160ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4161ed1f50c3SPaul Durrant if (err < 0) 4162ed1f50c3SPaul Durrant goto out; 4163ed1f50c3SPaul Durrant 4164ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4165ed1f50c3SPaul Durrant 4166ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4167ed1f50c3SPaul Durrant while (off <= len && !done) { 4168ed1f50c3SPaul Durrant switch (nexthdr) { 4169ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4170ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4171ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4172ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4173ed1f50c3SPaul Durrant 4174ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4175ed1f50c3SPaul Durrant off + 4176ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4177ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4178ed1f50c3SPaul Durrant if (err < 0) 4179ed1f50c3SPaul Durrant goto out; 4180ed1f50c3SPaul Durrant 4181ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4182ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4183ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4184ed1f50c3SPaul Durrant break; 4185ed1f50c3SPaul Durrant } 4186ed1f50c3SPaul Durrant case IPPROTO_AH: { 4187ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4188ed1f50c3SPaul Durrant 4189ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4190ed1f50c3SPaul Durrant off + 4191ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4192ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4193ed1f50c3SPaul Durrant if (err < 0) 4194ed1f50c3SPaul Durrant goto out; 4195ed1f50c3SPaul Durrant 4196ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4197ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4198ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4199ed1f50c3SPaul Durrant break; 4200ed1f50c3SPaul Durrant } 4201ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4202ed1f50c3SPaul Durrant struct frag_hdr *hp; 4203ed1f50c3SPaul Durrant 4204ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4205ed1f50c3SPaul Durrant off + 4206ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4207ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4208ed1f50c3SPaul Durrant if (err < 0) 4209ed1f50c3SPaul Durrant goto out; 4210ed1f50c3SPaul Durrant 4211ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4212ed1f50c3SPaul Durrant 4213ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4214ed1f50c3SPaul Durrant fragment = true; 4215ed1f50c3SPaul Durrant 4216ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4217ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4218ed1f50c3SPaul Durrant break; 4219ed1f50c3SPaul Durrant } 4220ed1f50c3SPaul Durrant default: 4221ed1f50c3SPaul Durrant done = true; 4222ed1f50c3SPaul Durrant break; 4223ed1f50c3SPaul Durrant } 4224ed1f50c3SPaul Durrant } 4225ed1f50c3SPaul Durrant 4226ed1f50c3SPaul Durrant err = -EPROTO; 4227ed1f50c3SPaul Durrant 4228ed1f50c3SPaul Durrant if (!done || fragment) 4229ed1f50c3SPaul Durrant goto out; 4230ed1f50c3SPaul Durrant 4231f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4232f9708b43SJan Beulich if (IS_ERR(csum)) 4233f9708b43SJan Beulich return PTR_ERR(csum); 4234ed1f50c3SPaul Durrant 4235ed1f50c3SPaul Durrant if (recalculate) 4236f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4237ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4238f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4239ed1f50c3SPaul Durrant err = 0; 4240ed1f50c3SPaul Durrant 4241ed1f50c3SPaul Durrant out: 4242ed1f50c3SPaul Durrant return err; 4243ed1f50c3SPaul Durrant } 4244ed1f50c3SPaul Durrant 4245ed1f50c3SPaul Durrant /** 4246ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4247ed1f50c3SPaul Durrant * @skb: the skb to set up 4248ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4249ed1f50c3SPaul Durrant */ 4250ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4251ed1f50c3SPaul Durrant { 4252ed1f50c3SPaul Durrant int err; 4253ed1f50c3SPaul Durrant 4254ed1f50c3SPaul Durrant switch (skb->protocol) { 4255ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4256f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4257ed1f50c3SPaul Durrant break; 4258ed1f50c3SPaul Durrant 4259ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4260ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4261ed1f50c3SPaul Durrant break; 4262ed1f50c3SPaul Durrant 4263ed1f50c3SPaul Durrant default: 4264ed1f50c3SPaul Durrant err = -EPROTO; 4265ed1f50c3SPaul Durrant break; 4266ed1f50c3SPaul Durrant } 4267ed1f50c3SPaul Durrant 4268ed1f50c3SPaul Durrant return err; 4269ed1f50c3SPaul Durrant } 4270ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4271ed1f50c3SPaul Durrant 42729afd85c9SLinus Lüssing /** 42739afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 42749afd85c9SLinus Lüssing * @skb: the skb to check 42759afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 42769afd85c9SLinus Lüssing * 42779afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 42789afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 42799afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 42809afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 42819afd85c9SLinus Lüssing * 4282a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4283a516993fSLinus Lüssing * differs from the provided skb. 42849afd85c9SLinus Lüssing */ 42859afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 42869afd85c9SLinus Lüssing unsigned int transport_len) 42879afd85c9SLinus Lüssing { 42889afd85c9SLinus Lüssing struct sk_buff *skb_chk; 42899afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 42909afd85c9SLinus Lüssing int ret; 42919afd85c9SLinus Lüssing 4292a516993fSLinus Lüssing if (skb->len < len) 42939afd85c9SLinus Lüssing return NULL; 4294a516993fSLinus Lüssing else if (skb->len == len) 42959afd85c9SLinus Lüssing return skb; 42969afd85c9SLinus Lüssing 42979afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 42989afd85c9SLinus Lüssing if (!skb_chk) 42999afd85c9SLinus Lüssing return NULL; 43009afd85c9SLinus Lüssing 43019afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 43029afd85c9SLinus Lüssing if (ret) { 43039afd85c9SLinus Lüssing kfree_skb(skb_chk); 43049afd85c9SLinus Lüssing return NULL; 43059afd85c9SLinus Lüssing } 43069afd85c9SLinus Lüssing 43079afd85c9SLinus Lüssing return skb_chk; 43089afd85c9SLinus Lüssing } 43099afd85c9SLinus Lüssing 43109afd85c9SLinus Lüssing /** 43119afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 43129afd85c9SLinus Lüssing * @skb: the skb to check 43139afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 43149afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 43159afd85c9SLinus Lüssing * 43169afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 43179afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 43189afd85c9SLinus Lüssing * 43199afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 43209afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 43219afd85c9SLinus Lüssing * 4322a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4323a516993fSLinus Lüssing * differs from the provided skb. 43249afd85c9SLinus Lüssing */ 43259afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 43269afd85c9SLinus Lüssing unsigned int transport_len, 43279afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 43289afd85c9SLinus Lüssing { 43299afd85c9SLinus Lüssing struct sk_buff *skb_chk; 43309afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4331fcba67c9SLinus Lüssing __sum16 ret; 43329afd85c9SLinus Lüssing 43339afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 43349afd85c9SLinus Lüssing if (!skb_chk) 4335a516993fSLinus Lüssing goto err; 43369afd85c9SLinus Lüssing 4337a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4338a516993fSLinus Lüssing goto err; 43399afd85c9SLinus Lüssing 43409b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 43419afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 43429b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 43439afd85c9SLinus Lüssing 4344a516993fSLinus Lüssing if (ret) 4345a516993fSLinus Lüssing goto err; 43469afd85c9SLinus Lüssing 43479afd85c9SLinus Lüssing return skb_chk; 4348a516993fSLinus Lüssing 4349a516993fSLinus Lüssing err: 4350a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4351a516993fSLinus Lüssing kfree_skb(skb_chk); 4352a516993fSLinus Lüssing 4353a516993fSLinus Lüssing return NULL; 4354a516993fSLinus Lüssing 43559afd85c9SLinus Lüssing } 43569afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 43579afd85c9SLinus Lüssing 43584497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 43594497b076SBen Hutchings { 4360e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4361e87cc472SJoe Perches skb->dev->name); 43624497b076SBen Hutchings } 43634497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4364bad43ca8SEric Dumazet 4365bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4366bad43ca8SEric Dumazet { 43673d861f66SEric Dumazet if (head_stolen) { 43683d861f66SEric Dumazet skb_release_head_state(skb); 4369bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 43703d861f66SEric Dumazet } else { 4371bad43ca8SEric Dumazet __kfree_skb(skb); 4372bad43ca8SEric Dumazet } 43733d861f66SEric Dumazet } 4374bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4375bad43ca8SEric Dumazet 4376bad43ca8SEric Dumazet /** 4377bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4378bad43ca8SEric Dumazet * @to: prior buffer 4379bad43ca8SEric Dumazet * @from: buffer to add 4380bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4381c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4382bad43ca8SEric Dumazet */ 4383bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4384bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4385bad43ca8SEric Dumazet { 4386bad43ca8SEric Dumazet int i, delta, len = from->len; 4387bad43ca8SEric Dumazet 4388bad43ca8SEric Dumazet *fragstolen = false; 4389bad43ca8SEric Dumazet 4390bad43ca8SEric Dumazet if (skb_cloned(to)) 4391bad43ca8SEric Dumazet return false; 4392bad43ca8SEric Dumazet 4393bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4394e93a0435SEric Dumazet if (len) 4395bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4396bad43ca8SEric Dumazet *delta_truesize = 0; 4397bad43ca8SEric Dumazet return true; 4398bad43ca8SEric Dumazet } 4399bad43ca8SEric Dumazet 4400bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4401bad43ca8SEric Dumazet return false; 4402bad43ca8SEric Dumazet 4403bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4404bad43ca8SEric Dumazet struct page *page; 4405bad43ca8SEric Dumazet unsigned int offset; 4406bad43ca8SEric Dumazet 4407bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4408bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4409bad43ca8SEric Dumazet return false; 4410bad43ca8SEric Dumazet 4411bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4412bad43ca8SEric Dumazet return false; 4413bad43ca8SEric Dumazet 4414bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4415bad43ca8SEric Dumazet 4416bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4417bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4418bad43ca8SEric Dumazet 4419bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4420bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4421bad43ca8SEric Dumazet *fragstolen = true; 4422bad43ca8SEric Dumazet } else { 4423bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4424bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4425bad43ca8SEric Dumazet return false; 4426bad43ca8SEric Dumazet 4427f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4428bad43ca8SEric Dumazet } 4429bad43ca8SEric Dumazet 4430bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4431bad43ca8SEric Dumazet 4432bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4433bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4434bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4435bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4436bad43ca8SEric Dumazet 4437bad43ca8SEric Dumazet if (!skb_cloned(from)) 4438bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4439bad43ca8SEric Dumazet 44408ea853fdSLi RongQing /* if the skb is not cloned this does nothing 44418ea853fdSLi RongQing * since we set nr_frags to 0. 44428ea853fdSLi RongQing */ 4443bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4444bad43ca8SEric Dumazet skb_frag_ref(from, i); 4445bad43ca8SEric Dumazet 4446bad43ca8SEric Dumazet to->truesize += delta; 4447bad43ca8SEric Dumazet to->len += len; 4448bad43ca8SEric Dumazet to->data_len += len; 4449bad43ca8SEric Dumazet 4450bad43ca8SEric Dumazet *delta_truesize = delta; 4451bad43ca8SEric Dumazet return true; 4452bad43ca8SEric Dumazet } 4453bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4454621e84d6SNicolas Dichtel 4455621e84d6SNicolas Dichtel /** 44568b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4457621e84d6SNicolas Dichtel * 4458621e84d6SNicolas Dichtel * @skb: buffer to clean 44598b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4460621e84d6SNicolas Dichtel * 44618b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 44628b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 44638b27f277SNicolas Dichtel * operations. 44648b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 44658b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 44668b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4467621e84d6SNicolas Dichtel */ 44688b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4469621e84d6SNicolas Dichtel { 44702456e855SThomas Gleixner skb->tstamp = 0; 4471621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4472621e84d6SNicolas Dichtel skb->skb_iif = 0; 447360ff7467SWANG Cong skb->ignore_df = 0; 4474621e84d6SNicolas Dichtel skb_dst_drop(skb); 4475621e84d6SNicolas Dichtel secpath_reset(skb); 4476621e84d6SNicolas Dichtel nf_reset(skb); 4477621e84d6SNicolas Dichtel nf_reset_trace(skb); 4478213dd74aSHerbert Xu 4479213dd74aSHerbert Xu if (!xnet) 4480213dd74aSHerbert Xu return; 4481213dd74aSHerbert Xu 4482213dd74aSHerbert Xu skb_orphan(skb); 4483213dd74aSHerbert Xu skb->mark = 0; 4484621e84d6SNicolas Dichtel } 4485621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4486de960aa9SFlorian Westphal 4487de960aa9SFlorian Westphal /** 4488de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4489de960aa9SFlorian Westphal * 4490de960aa9SFlorian Westphal * @skb: GSO skb 4491de960aa9SFlorian Westphal * 4492de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4493de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4494de960aa9SFlorian Westphal * 4495de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4496de960aa9SFlorian Westphal */ 4497de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4498de960aa9SFlorian Westphal { 4499de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4500f993bc25SFlorian Westphal unsigned int thlen = 0; 4501f993bc25SFlorian Westphal 4502f993bc25SFlorian Westphal if (skb->encapsulation) { 4503f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4504f993bc25SFlorian Westphal skb_transport_header(skb); 4505de960aa9SFlorian Westphal 4506de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4507f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4508f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4509f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 451090017accSMarcelo Ricardo Leitner } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) { 451190017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 4512f993bc25SFlorian Westphal } 45136d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 45146d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 45156d39d589SFlorian Westphal * accounted for. 45166d39d589SFlorian Westphal */ 4517f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4518de960aa9SFlorian Westphal } 4519de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 45200d5501c1SVlad Yasevich 4521ae7ef81eSMarcelo Ricardo Leitner /** 4522ae7ef81eSMarcelo Ricardo Leitner * skb_gso_validate_mtu - Return in case such skb fits a given MTU 4523ae7ef81eSMarcelo Ricardo Leitner * 4524ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 452576f21b99SDavid S. Miller * @mtu: MTU to validate against 4526ae7ef81eSMarcelo Ricardo Leitner * 4527ae7ef81eSMarcelo Ricardo Leitner * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU 4528ae7ef81eSMarcelo Ricardo Leitner * once split. 4529ae7ef81eSMarcelo Ricardo Leitner */ 4530ae7ef81eSMarcelo Ricardo Leitner bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu) 4531ae7ef81eSMarcelo Ricardo Leitner { 4532ae7ef81eSMarcelo Ricardo Leitner const struct skb_shared_info *shinfo = skb_shinfo(skb); 4533ae7ef81eSMarcelo Ricardo Leitner const struct sk_buff *iter; 4534ae7ef81eSMarcelo Ricardo Leitner unsigned int hlen; 4535ae7ef81eSMarcelo Ricardo Leitner 4536ae7ef81eSMarcelo Ricardo Leitner hlen = skb_gso_network_seglen(skb); 4537ae7ef81eSMarcelo Ricardo Leitner 4538ae7ef81eSMarcelo Ricardo Leitner if (shinfo->gso_size != GSO_BY_FRAGS) 4539ae7ef81eSMarcelo Ricardo Leitner return hlen <= mtu; 4540ae7ef81eSMarcelo Ricardo Leitner 4541ae7ef81eSMarcelo Ricardo Leitner /* Undo this so we can re-use header sizes */ 4542ae7ef81eSMarcelo Ricardo Leitner hlen -= GSO_BY_FRAGS; 4543ae7ef81eSMarcelo Ricardo Leitner 4544ae7ef81eSMarcelo Ricardo Leitner skb_walk_frags(skb, iter) { 4545ae7ef81eSMarcelo Ricardo Leitner if (hlen + skb_headlen(iter) > mtu) 4546ae7ef81eSMarcelo Ricardo Leitner return false; 4547ae7ef81eSMarcelo Ricardo Leitner } 4548ae7ef81eSMarcelo Ricardo Leitner 4549ae7ef81eSMarcelo Ricardo Leitner return true; 4550ae7ef81eSMarcelo Ricardo Leitner } 4551ae7ef81eSMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gso_validate_mtu); 4552ae7ef81eSMarcelo Ricardo Leitner 45530d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 45540d5501c1SVlad Yasevich { 45550d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 45560d5501c1SVlad Yasevich kfree_skb(skb); 45570d5501c1SVlad Yasevich return NULL; 45580d5501c1SVlad Yasevich } 45590d5501c1SVlad Yasevich 4560f6548615SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN, 4561a6e18ff1SVlad Yasevich 2 * ETH_ALEN); 45620d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 45630d5501c1SVlad Yasevich return skb; 45640d5501c1SVlad Yasevich } 45650d5501c1SVlad Yasevich 45660d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 45670d5501c1SVlad Yasevich { 45680d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 45690d5501c1SVlad Yasevich u16 vlan_tci; 45700d5501c1SVlad Yasevich 4571df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 45720d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 45730d5501c1SVlad Yasevich return skb; 45740d5501c1SVlad Yasevich } 45750d5501c1SVlad Yasevich 45760d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 45770d5501c1SVlad Yasevich if (unlikely(!skb)) 45780d5501c1SVlad Yasevich goto err_free; 45790d5501c1SVlad Yasevich 45800d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 45810d5501c1SVlad Yasevich goto err_free; 45820d5501c1SVlad Yasevich 45830d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 45840d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 45850d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 45860d5501c1SVlad Yasevich 45870d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 45880d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 45890d5501c1SVlad Yasevich 45900d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 45910d5501c1SVlad Yasevich if (unlikely(!skb)) 45920d5501c1SVlad Yasevich goto err_free; 45930d5501c1SVlad Yasevich 45940d5501c1SVlad Yasevich skb_reset_network_header(skb); 45950d5501c1SVlad Yasevich skb_reset_transport_header(skb); 45960d5501c1SVlad Yasevich skb_reset_mac_len(skb); 45970d5501c1SVlad Yasevich 45980d5501c1SVlad Yasevich return skb; 45990d5501c1SVlad Yasevich 46000d5501c1SVlad Yasevich err_free: 46010d5501c1SVlad Yasevich kfree_skb(skb); 46020d5501c1SVlad Yasevich return NULL; 46030d5501c1SVlad Yasevich } 46040d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 46052e4e4410SEric Dumazet 4606e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4607e2195121SJiri Pirko { 4608e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4609e2195121SJiri Pirko return -ENOMEM; 4610e2195121SJiri Pirko 4611e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4612e2195121SJiri Pirko return 0; 4613e2195121SJiri Pirko 4614e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4615e2195121SJiri Pirko } 4616e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4617e2195121SJiri Pirko 4618bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 4619bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 4620bfca4c52SShmulik Ladkani */ 4621bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 462293515d53SJiri Pirko { 462393515d53SJiri Pirko struct vlan_hdr *vhdr; 4624b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 462593515d53SJiri Pirko int err; 462693515d53SJiri Pirko 4627b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 4628b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 4629b6a79208SShmulik Ladkani offset)) { 4630b6a79208SShmulik Ladkani return -EINVAL; 4631b6a79208SShmulik Ladkani } 4632b6a79208SShmulik Ladkani 463393515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 463493515d53SJiri Pirko if (unlikely(err)) 4635b6a79208SShmulik Ladkani return err; 463693515d53SJiri Pirko 463793515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 463893515d53SJiri Pirko 463993515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 464093515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 464193515d53SJiri Pirko 464293515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 464393515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 464493515d53SJiri Pirko 464593515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 464693515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 464793515d53SJiri Pirko 464893515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 464993515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 465093515d53SJiri Pirko 465193515d53SJiri Pirko skb_reset_mac_len(skb); 465293515d53SJiri Pirko 465393515d53SJiri Pirko return err; 465493515d53SJiri Pirko } 4655bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 465693515d53SJiri Pirko 4657b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 4658b6a79208SShmulik Ladkani * Expects skb->data at mac header. 4659b6a79208SShmulik Ladkani */ 466093515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 466193515d53SJiri Pirko { 466293515d53SJiri Pirko u16 vlan_tci; 466393515d53SJiri Pirko __be16 vlan_proto; 466493515d53SJiri Pirko int err; 466593515d53SJiri Pirko 4666df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 466793515d53SJiri Pirko skb->vlan_tci = 0; 466893515d53SJiri Pirko } else { 4669ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 467093515d53SJiri Pirko return 0; 467193515d53SJiri Pirko 467293515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 467393515d53SJiri Pirko if (err) 467493515d53SJiri Pirko return err; 467593515d53SJiri Pirko } 467693515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 4677ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 467893515d53SJiri Pirko return 0; 467993515d53SJiri Pirko 468093515d53SJiri Pirko vlan_proto = skb->protocol; 468193515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 468293515d53SJiri Pirko if (unlikely(err)) 468393515d53SJiri Pirko return err; 468493515d53SJiri Pirko 468593515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 468693515d53SJiri Pirko return 0; 468793515d53SJiri Pirko } 468893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 468993515d53SJiri Pirko 4690b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 4691b6a79208SShmulik Ladkani * Expects skb->data at mac header. 4692b6a79208SShmulik Ladkani */ 469393515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 469493515d53SJiri Pirko { 4695df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 4696b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 469793515d53SJiri Pirko int err; 469893515d53SJiri Pirko 4699b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 4700b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 4701b6a79208SShmulik Ladkani offset)) { 4702b6a79208SShmulik Ladkani return -EINVAL; 4703b6a79208SShmulik Ladkani } 4704b6a79208SShmulik Ladkani 470593515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 4706df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 4707b6a79208SShmulik Ladkani if (err) 470893515d53SJiri Pirko return err; 47099241e2dfSDaniel Borkmann 471093515d53SJiri Pirko skb->protocol = skb->vlan_proto; 471193515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 471293515d53SJiri Pirko 47136b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 471493515d53SJiri Pirko } 471593515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 471693515d53SJiri Pirko return 0; 471793515d53SJiri Pirko } 471893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 471993515d53SJiri Pirko 47202e4e4410SEric Dumazet /** 47212e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 47222e4e4410SEric Dumazet * 4723de3f0d0eSMasanari Iida * @header_len: size of linear part 4724de3f0d0eSMasanari Iida * @data_len: needed length in frags 4725de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4726de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4727de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 47282e4e4410SEric Dumazet * 47292e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 47302e4e4410SEric Dumazet */ 47312e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 47322e4e4410SEric Dumazet unsigned long data_len, 47332e4e4410SEric Dumazet int max_page_order, 47342e4e4410SEric Dumazet int *errcode, 47352e4e4410SEric Dumazet gfp_t gfp_mask) 47362e4e4410SEric Dumazet { 47372e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 47382e4e4410SEric Dumazet unsigned long chunk; 47392e4e4410SEric Dumazet struct sk_buff *skb; 47402e4e4410SEric Dumazet struct page *page; 47412e4e4410SEric Dumazet gfp_t gfp_head; 47422e4e4410SEric Dumazet int i; 47432e4e4410SEric Dumazet 47442e4e4410SEric Dumazet *errcode = -EMSGSIZE; 47452e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 47462e4e4410SEric Dumazet * high order pages... 47472e4e4410SEric Dumazet */ 47482e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 47492e4e4410SEric Dumazet return NULL; 47502e4e4410SEric Dumazet 47512e4e4410SEric Dumazet gfp_head = gfp_mask; 4752d0164adcSMel Gorman if (gfp_head & __GFP_DIRECT_RECLAIM) 4753dcda9b04SMichal Hocko gfp_head |= __GFP_RETRY_MAYFAIL; 47542e4e4410SEric Dumazet 47552e4e4410SEric Dumazet *errcode = -ENOBUFS; 47562e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 47572e4e4410SEric Dumazet if (!skb) 47582e4e4410SEric Dumazet return NULL; 47592e4e4410SEric Dumazet 47602e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 47612e4e4410SEric Dumazet 47622e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 47632e4e4410SEric Dumazet int order = max_page_order; 47642e4e4410SEric Dumazet 47652e4e4410SEric Dumazet while (order) { 47662e4e4410SEric Dumazet if (npages >= 1 << order) { 4767d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 47682e4e4410SEric Dumazet __GFP_COMP | 47692e4e4410SEric Dumazet __GFP_NOWARN | 47702e4e4410SEric Dumazet __GFP_NORETRY, 47712e4e4410SEric Dumazet order); 47722e4e4410SEric Dumazet if (page) 47732e4e4410SEric Dumazet goto fill_page; 47742e4e4410SEric Dumazet /* Do not retry other high order allocations */ 47752e4e4410SEric Dumazet order = 1; 47762e4e4410SEric Dumazet max_page_order = 0; 47772e4e4410SEric Dumazet } 47782e4e4410SEric Dumazet order--; 47792e4e4410SEric Dumazet } 47802e4e4410SEric Dumazet page = alloc_page(gfp_mask); 47812e4e4410SEric Dumazet if (!page) 47822e4e4410SEric Dumazet goto failure; 47832e4e4410SEric Dumazet fill_page: 47842e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 47852e4e4410SEric Dumazet PAGE_SIZE << order); 47862e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 47872e4e4410SEric Dumazet data_len -= chunk; 47882e4e4410SEric Dumazet npages -= 1 << order; 47892e4e4410SEric Dumazet } 47902e4e4410SEric Dumazet return skb; 47912e4e4410SEric Dumazet 47922e4e4410SEric Dumazet failure: 47932e4e4410SEric Dumazet kfree_skb(skb); 47942e4e4410SEric Dumazet return NULL; 47952e4e4410SEric Dumazet } 47962e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 47976fa01ccdSSowmini Varadhan 47986fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 47996fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 48006fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 48016fa01ccdSSowmini Varadhan { 48026fa01ccdSSowmini Varadhan int i; 48036fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 48046fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 48056fa01ccdSSowmini Varadhan u8 *data; 48066fa01ccdSSowmini Varadhan 48076fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 48086fa01ccdSSowmini Varadhan 48096fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 48106fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 48116fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 48126fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 48136fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 48146fa01ccdSSowmini Varadhan if (!data) 48156fa01ccdSSowmini Varadhan return -ENOMEM; 48166fa01ccdSSowmini Varadhan 48176fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 48186fa01ccdSSowmini Varadhan 48196fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 48206fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 48216fa01ccdSSowmini Varadhan skb->len -= off; 48226fa01ccdSSowmini Varadhan 48236fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 48246fa01ccdSSowmini Varadhan skb_shinfo(skb), 48256fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 48266fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 48276fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 48286fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 48296fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 48306fa01ccdSSowmini Varadhan kfree(data); 48316fa01ccdSSowmini Varadhan return -ENOMEM; 48326fa01ccdSSowmini Varadhan } 48336fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 48346fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 48356fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 48366fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 48376fa01ccdSSowmini Varadhan skb_release_data(skb); 48386fa01ccdSSowmini Varadhan } else { 48396fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 48406fa01ccdSSowmini Varadhan * relocate values 48416fa01ccdSSowmini Varadhan */ 48426fa01ccdSSowmini Varadhan skb_free_head(skb); 48436fa01ccdSSowmini Varadhan } 48446fa01ccdSSowmini Varadhan 48456fa01ccdSSowmini Varadhan skb->head = data; 48466fa01ccdSSowmini Varadhan skb->data = data; 48476fa01ccdSSowmini Varadhan skb->head_frag = 0; 48486fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 48496fa01ccdSSowmini Varadhan skb->end = size; 48506fa01ccdSSowmini Varadhan #else 48516fa01ccdSSowmini Varadhan skb->end = skb->head + size; 48526fa01ccdSSowmini Varadhan #endif 48536fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 48546fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 48556fa01ccdSSowmini Varadhan skb->cloned = 0; 48566fa01ccdSSowmini Varadhan skb->hdr_len = 0; 48576fa01ccdSSowmini Varadhan skb->nohdr = 0; 48586fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 48596fa01ccdSSowmini Varadhan 48606fa01ccdSSowmini Varadhan return 0; 48616fa01ccdSSowmini Varadhan } 48626fa01ccdSSowmini Varadhan 48636fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 48646fa01ccdSSowmini Varadhan 48656fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 48666fa01ccdSSowmini Varadhan * pskb_carve() 48676fa01ccdSSowmini Varadhan */ 48686fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 48696fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 48706fa01ccdSSowmini Varadhan gfp_t gfp_mask) 48716fa01ccdSSowmini Varadhan { 48726fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 48736fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 48746fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 48756fa01ccdSSowmini Varadhan 48766fa01ccdSSowmini Varadhan do { 48776fa01ccdSSowmini Varadhan if (!list) { 48786fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 48796fa01ccdSSowmini Varadhan return -EFAULT; 48806fa01ccdSSowmini Varadhan } 48816fa01ccdSSowmini Varadhan if (list->len <= eat) { 48826fa01ccdSSowmini Varadhan /* Eaten as whole. */ 48836fa01ccdSSowmini Varadhan eat -= list->len; 48846fa01ccdSSowmini Varadhan list = list->next; 48856fa01ccdSSowmini Varadhan insp = list; 48866fa01ccdSSowmini Varadhan } else { 48876fa01ccdSSowmini Varadhan /* Eaten partially. */ 48886fa01ccdSSowmini Varadhan if (skb_shared(list)) { 48896fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 48906fa01ccdSSowmini Varadhan if (!clone) 48916fa01ccdSSowmini Varadhan return -ENOMEM; 48926fa01ccdSSowmini Varadhan insp = list->next; 48936fa01ccdSSowmini Varadhan list = clone; 48946fa01ccdSSowmini Varadhan } else { 48956fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 48966fa01ccdSSowmini Varadhan insp = list; 48976fa01ccdSSowmini Varadhan } 48986fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 48996fa01ccdSSowmini Varadhan kfree_skb(clone); 49006fa01ccdSSowmini Varadhan return -ENOMEM; 49016fa01ccdSSowmini Varadhan } 49026fa01ccdSSowmini Varadhan break; 49036fa01ccdSSowmini Varadhan } 49046fa01ccdSSowmini Varadhan } while (eat); 49056fa01ccdSSowmini Varadhan 49066fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 49076fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 49086fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 49096fa01ccdSSowmini Varadhan kfree_skb(list); 49106fa01ccdSSowmini Varadhan } 49116fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 49126fa01ccdSSowmini Varadhan if (clone) { 49136fa01ccdSSowmini Varadhan clone->next = list; 49146fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 49156fa01ccdSSowmini Varadhan } 49166fa01ccdSSowmini Varadhan return 0; 49176fa01ccdSSowmini Varadhan } 49186fa01ccdSSowmini Varadhan 49196fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 49206fa01ccdSSowmini Varadhan * non-linear part of skb 49216fa01ccdSSowmini Varadhan */ 49226fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 49236fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 49246fa01ccdSSowmini Varadhan { 49256fa01ccdSSowmini Varadhan int i, k = 0; 49266fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 49276fa01ccdSSowmini Varadhan u8 *data; 49286fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 49296fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 49306fa01ccdSSowmini Varadhan 49316fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 49326fa01ccdSSowmini Varadhan 49336fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 49346fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 49356fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 49366fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 49376fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 49386fa01ccdSSowmini Varadhan if (!data) 49396fa01ccdSSowmini Varadhan return -ENOMEM; 49406fa01ccdSSowmini Varadhan 49416fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 49426fa01ccdSSowmini Varadhan 49436fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 49446fa01ccdSSowmini Varadhan skb_shinfo(skb), offsetof(struct skb_shared_info, 49456fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 49466fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 49476fa01ccdSSowmini Varadhan kfree(data); 49486fa01ccdSSowmini Varadhan return -ENOMEM; 49496fa01ccdSSowmini Varadhan } 49506fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 49516fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 49526fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 49536fa01ccdSSowmini Varadhan 49546fa01ccdSSowmini Varadhan if (pos + fsize > off) { 49556fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 49566fa01ccdSSowmini Varadhan 49576fa01ccdSSowmini Varadhan if (pos < off) { 49586fa01ccdSSowmini Varadhan /* Split frag. 49596fa01ccdSSowmini Varadhan * We have two variants in this case: 49606fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 49616fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 49626fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 49636fa01ccdSSowmini Varadhan * where splitting is expensive. 49646fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 49656fa01ccdSSowmini Varadhan */ 49666fa01ccdSSowmini Varadhan shinfo->frags[0].page_offset += off - pos; 49676fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 49686fa01ccdSSowmini Varadhan } 49696fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 49706fa01ccdSSowmini Varadhan k++; 49716fa01ccdSSowmini Varadhan } 49726fa01ccdSSowmini Varadhan pos += fsize; 49736fa01ccdSSowmini Varadhan } 49746fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 49756fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 49766fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 49776fa01ccdSSowmini Varadhan 49786fa01ccdSSowmini Varadhan if (k == 0) { 49796fa01ccdSSowmini Varadhan /* split line is in frag list */ 49806fa01ccdSSowmini Varadhan pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 49816fa01ccdSSowmini Varadhan } 49826fa01ccdSSowmini Varadhan skb_release_data(skb); 49836fa01ccdSSowmini Varadhan 49846fa01ccdSSowmini Varadhan skb->head = data; 49856fa01ccdSSowmini Varadhan skb->head_frag = 0; 49866fa01ccdSSowmini Varadhan skb->data = data; 49876fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 49886fa01ccdSSowmini Varadhan skb->end = size; 49896fa01ccdSSowmini Varadhan #else 49906fa01ccdSSowmini Varadhan skb->end = skb->head + size; 49916fa01ccdSSowmini Varadhan #endif 49926fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 49936fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 49946fa01ccdSSowmini Varadhan skb->cloned = 0; 49956fa01ccdSSowmini Varadhan skb->hdr_len = 0; 49966fa01ccdSSowmini Varadhan skb->nohdr = 0; 49976fa01ccdSSowmini Varadhan skb->len -= off; 49986fa01ccdSSowmini Varadhan skb->data_len = skb->len; 49996fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 50006fa01ccdSSowmini Varadhan return 0; 50016fa01ccdSSowmini Varadhan } 50026fa01ccdSSowmini Varadhan 50036fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 50046fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 50056fa01ccdSSowmini Varadhan { 50066fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 50076fa01ccdSSowmini Varadhan 50086fa01ccdSSowmini Varadhan if (len < headlen) 50096fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 50106fa01ccdSSowmini Varadhan else 50116fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 50126fa01ccdSSowmini Varadhan } 50136fa01ccdSSowmini Varadhan 50146fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 50156fa01ccdSSowmini Varadhan * a new skb 50166fa01ccdSSowmini Varadhan */ 50176fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 50186fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 50196fa01ccdSSowmini Varadhan { 50206fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 50216fa01ccdSSowmini Varadhan 50226fa01ccdSSowmini Varadhan if (!clone) 50236fa01ccdSSowmini Varadhan return NULL; 50246fa01ccdSSowmini Varadhan 50256fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 50266fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 50276fa01ccdSSowmini Varadhan kfree_skb(clone); 50286fa01ccdSSowmini Varadhan return NULL; 50296fa01ccdSSowmini Varadhan } 50306fa01ccdSSowmini Varadhan return clone; 50316fa01ccdSSowmini Varadhan } 50326fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 5033c8c8b127SEric Dumazet 5034c8c8b127SEric Dumazet /** 5035c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 5036c8c8b127SEric Dumazet * @skb: buffer 5037c8c8b127SEric Dumazet * 5038c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 5039c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 5040c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 5041c8c8b127SEric Dumazet * truesize. 5042c8c8b127SEric Dumazet * Notes: 5043c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 5044c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 5045c8c8b127SEric Dumazet */ 5046c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 5047c8c8b127SEric Dumazet { 50483174fed9SEric Dumazet if (skb->data_len) { 50493174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 5050c8c8b127SEric Dumazet skb_cloned(skb)) 5051c8c8b127SEric Dumazet return; 5052c8c8b127SEric Dumazet 5053c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 5054c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 50553174fed9SEric Dumazet } 50563174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 50573174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 50583174fed9SEric Dumazet * their truesize. 50593174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 50603174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 50613174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 5062c8c8b127SEric Dumazet */ 5063c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 5064c8c8b127SEric Dumazet } 5065