12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 41da177e4SLinus Torvalds * 5113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 61da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Fixes: 91da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 101da177e4SLinus Torvalds * balancer bugs. 111da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 121da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 131da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 141da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 151da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 161da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 171da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 181da177e4SLinus Torvalds * only put in the headers 191da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 201da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 211da177e4SLinus Torvalds * Andi Kleen : slabified it. 221da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * NOTE: 251da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 261da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 271da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 281da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 35e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 36e005d193SJoe Perches 371da177e4SLinus Torvalds #include <linux/module.h> 381da177e4SLinus Torvalds #include <linux/types.h> 391da177e4SLinus Torvalds #include <linux/kernel.h> 401da177e4SLinus Torvalds #include <linux/mm.h> 411da177e4SLinus Torvalds #include <linux/interrupt.h> 421da177e4SLinus Torvalds #include <linux/in.h> 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/slab.h> 45de960aa9SFlorian Westphal #include <linux/tcp.h> 46de960aa9SFlorian Westphal #include <linux/udp.h> 4790017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 501da177e4SLinus Torvalds #include <net/pkt_sched.h> 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #include <linux/string.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 549c55e01cSJens Axboe #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/cache.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <linux/init.h> 58716ea3a7SDavid Howells #include <linux/scatterlist.h> 59ac45f602SPatrick Ohly #include <linux/errqueue.h> 60268bb0ceSLinus Torvalds #include <linux/prefetch.h> 610d5501c1SVlad Yasevich #include <linux/if_vlan.h> 622a2ea508SJohn Hurley #include <linux/mpls.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #include <net/protocol.h> 651da177e4SLinus Torvalds #include <net/dst.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/checksum.h> 68ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 691da177e4SLinus Torvalds #include <net/xfrm.h> 708822e270SJohn Hurley #include <net/mpls.h> 713ee17bc7SMat Martineau #include <net/mptcp.h> 721da177e4SLinus Torvalds 737c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 74ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7551c56b00SEric Dumazet #include <linux/highmem.h> 76b245be1fSWillem de Bruijn #include <linux/capability.h> 77b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 782544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 79a1f8e7f7SAl Viro 807b7ed885SBart Van Assche #include "datagram.h" 817b7ed885SBart Van Assche 8208009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8308009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 84df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 85df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 86df5042f4SFlorian Westphal #endif 875f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 885f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /** 91f05de73bSJean Sacren * skb_panic - private function for out-of-line support 921da177e4SLinus Torvalds * @skb: buffer 931da177e4SLinus Torvalds * @sz: size 94f05de73bSJean Sacren * @addr: address 9599d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 961da177e4SLinus Torvalds * 97f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 98f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 99f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 100f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1011da177e4SLinus Torvalds */ 102f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 10399d5851eSJames Hogan const char msg[]) 1041da177e4SLinus Torvalds { 105e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 10699d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1074305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10826095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1091da177e4SLinus Torvalds BUG(); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 112f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1131da177e4SLinus Torvalds { 114f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 117f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 118f05de73bSJean Sacren { 119f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 120f05de73bSJean Sacren } 121c93bdd0eSMel Gorman 122c93bdd0eSMel Gorman /* 123c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 124c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 125c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 126c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 127c93bdd0eSMel Gorman * memory is free 128c93bdd0eSMel Gorman */ 129c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 130c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 13161c5e88aSstephen hemminger 13261c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 13361c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 134c93bdd0eSMel Gorman { 135c93bdd0eSMel Gorman void *obj; 136c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 137c93bdd0eSMel Gorman 138c93bdd0eSMel Gorman /* 139c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 140c93bdd0eSMel Gorman * to the reserves, fail. 141c93bdd0eSMel Gorman */ 142c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 143c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 144c93bdd0eSMel Gorman node); 145c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 146c93bdd0eSMel Gorman goto out; 147c93bdd0eSMel Gorman 148c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 149c93bdd0eSMel Gorman ret_pfmemalloc = true; 150c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 151c93bdd0eSMel Gorman 152c93bdd0eSMel Gorman out: 153c93bdd0eSMel Gorman if (pfmemalloc) 154c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 155c93bdd0eSMel Gorman 156c93bdd0eSMel Gorman return obj; 157c93bdd0eSMel Gorman } 158c93bdd0eSMel Gorman 1591da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1601da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1611da177e4SLinus Torvalds * [BEEP] leaks. 1621da177e4SLinus Torvalds * 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /** 166d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1671da177e4SLinus Torvalds * @size: size to allocate 1681da177e4SLinus Torvalds * @gfp_mask: allocation mask 169c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 170c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 171c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 172c93bdd0eSMel Gorman * allocations in case the data is required for writeback 173b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1741da177e4SLinus Torvalds * 1751da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 17694b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 17794b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1781da177e4SLinus Torvalds * 1791da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1801da177e4SLinus Torvalds * %GFP_ATOMIC. 1811da177e4SLinus Torvalds */ 182dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 183c93bdd0eSMel Gorman int flags, int node) 1841da177e4SLinus Torvalds { 185e18b890bSChristoph Lameter struct kmem_cache *cache; 1864947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1871da177e4SLinus Torvalds struct sk_buff *skb; 1881da177e4SLinus Torvalds u8 *data; 189c93bdd0eSMel Gorman bool pfmemalloc; 1901da177e4SLinus Torvalds 191c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 192c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 193c93bdd0eSMel Gorman 194c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 195c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1968798b3fbSHerbert Xu 1971da177e4SLinus Torvalds /* Get the HEAD */ 198b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1991da177e4SLinus Torvalds if (!skb) 2001da177e4SLinus Torvalds goto out; 201ec7d2f2cSEric Dumazet prefetchw(skb); 2021da177e4SLinus Torvalds 20387fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 20487fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 20587fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 20687fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 20787fb4b7bSEric Dumazet */ 208bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 20987fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 210c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2111da177e4SLinus Torvalds if (!data) 2121da177e4SLinus Torvalds goto nodata; 21387fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 21487fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 21587fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 21687fb4b7bSEric Dumazet */ 21787fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 218ec7d2f2cSEric Dumazet prefetchw(data + size); 2191da177e4SLinus Torvalds 220ca0605a7SArnaldo Carvalho de Melo /* 221c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 222c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 223c8005785SJohannes Berg * the tail pointer in struct sk_buff! 224ca0605a7SArnaldo Carvalho de Melo */ 225ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 22687fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 22787fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 228c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 22963354797SReshetova, Elena refcount_set(&skb->users, 1); 2301da177e4SLinus Torvalds skb->head = data; 2311da177e4SLinus Torvalds skb->data = data; 23227a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2334305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 23435d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 23535d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 23619633e12SStephen Hemminger 2374947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2384947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 239ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2404947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 2414947d3efSBenjamin LaHaise 242c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 243d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2441da177e4SLinus Torvalds 245d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 246d0bf4a9eSEric Dumazet 247d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 2482638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 1); 249d179cd12SDavid S. Miller 2506ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 251d179cd12SDavid S. Miller } 2521da177e4SLinus Torvalds out: 2531da177e4SLinus Torvalds return skb; 2541da177e4SLinus Torvalds nodata: 2558798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2561da177e4SLinus Torvalds skb = NULL; 2571da177e4SLinus Torvalds goto out; 2581da177e4SLinus Torvalds } 259b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2601da177e4SLinus Torvalds 261ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */ 262ba0509b6SJesper Dangaard Brouer static struct sk_buff *__build_skb_around(struct sk_buff *skb, 263ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 264ba0509b6SJesper Dangaard Brouer { 265ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 266ba0509b6SJesper Dangaard Brouer unsigned int size = frag_size ? : ksize(data); 267ba0509b6SJesper Dangaard Brouer 268ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 269ba0509b6SJesper Dangaard Brouer 270ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 271ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 272ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 273ba0509b6SJesper Dangaard Brouer skb->head = data; 274ba0509b6SJesper Dangaard Brouer skb->data = data; 275ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 276ba0509b6SJesper Dangaard Brouer skb->end = skb->tail + size; 277ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 278ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 279ba0509b6SJesper Dangaard Brouer 280ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 281ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 282ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 283ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 284ba0509b6SJesper Dangaard Brouer 285ba0509b6SJesper Dangaard Brouer return skb; 286ba0509b6SJesper Dangaard Brouer } 287ba0509b6SJesper Dangaard Brouer 2881da177e4SLinus Torvalds /** 2892ea2f62cSEric Dumazet * __build_skb - build a network buffer 290b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2912ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 292b2b5ce9dSEric Dumazet * 293b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 294deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2952ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2962ea2f62cSEric Dumazet * or vmalloc() 297b2b5ce9dSEric Dumazet * The return is the new skb buffer. 298b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 299b2b5ce9dSEric Dumazet * Notes : 300b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 301b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 302b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 303b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 304b2b5ce9dSEric Dumazet * before giving packet to stack. 305b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 306b2b5ce9dSEric Dumazet */ 3072ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 308b2b5ce9dSEric Dumazet { 309b2b5ce9dSEric Dumazet struct sk_buff *skb; 310b2b5ce9dSEric Dumazet 311b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 312ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 313b2b5ce9dSEric Dumazet return NULL; 314b2b5ce9dSEric Dumazet 315b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 316b2b5ce9dSEric Dumazet 317ba0509b6SJesper Dangaard Brouer return __build_skb_around(skb, data, frag_size); 318b2b5ce9dSEric Dumazet } 3192ea2f62cSEric Dumazet 3202ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3212ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3222ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3232ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3242ea2f62cSEric Dumazet */ 3252ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3262ea2f62cSEric Dumazet { 3272ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3282ea2f62cSEric Dumazet 3292ea2f62cSEric Dumazet if (skb && frag_size) { 3302ea2f62cSEric Dumazet skb->head_frag = 1; 3312f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3322ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3332ea2f62cSEric Dumazet } 3342ea2f62cSEric Dumazet return skb; 3352ea2f62cSEric Dumazet } 336b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 337b2b5ce9dSEric Dumazet 338ba0509b6SJesper Dangaard Brouer /** 339ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 340ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 341ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 342ba0509b6SJesper Dangaard Brouer * @frag_size: size of data, or 0 if head was kmalloced 343ba0509b6SJesper Dangaard Brouer */ 344ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 345ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 346ba0509b6SJesper Dangaard Brouer { 347ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 348ba0509b6SJesper Dangaard Brouer return NULL; 349ba0509b6SJesper Dangaard Brouer 350ba0509b6SJesper Dangaard Brouer skb = __build_skb_around(skb, data, frag_size); 351ba0509b6SJesper Dangaard Brouer 352ba0509b6SJesper Dangaard Brouer if (skb && frag_size) { 353ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 354ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 355ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 356ba0509b6SJesper Dangaard Brouer } 357ba0509b6SJesper Dangaard Brouer return skb; 358ba0509b6SJesper Dangaard Brouer } 359ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 360ba0509b6SJesper Dangaard Brouer 361795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 362795bb1c0SJesper Dangaard Brouer 363795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 364795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 365e0d7924aSAlexey Dobriyan unsigned int skb_count; 366795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 367795bb1c0SJesper Dangaard Brouer }; 368795bb1c0SJesper Dangaard Brouer 369b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 370795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 371ffde7328SAlexander Duyck 372ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 373ffde7328SAlexander Duyck { 374795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3759451980aSAlexander Duyck 3768c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 377ffde7328SAlexander Duyck } 378ffde7328SAlexander Duyck 379ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 380ffde7328SAlexander Duyck { 3813bed3cc4SAlexander Duyck fragsz = SKB_DATA_ALIGN(fragsz); 3823bed3cc4SAlexander Duyck 383453f85d4SMel Gorman return __napi_alloc_frag(fragsz, GFP_ATOMIC); 384ffde7328SAlexander Duyck } 385ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 386ffde7328SAlexander Duyck 3876f532612SEric Dumazet /** 3887ba7aeabSSebastian Andrzej Siewior * netdev_alloc_frag - allocate a page fragment 3897ba7aeabSSebastian Andrzej Siewior * @fragsz: fragment size 3907ba7aeabSSebastian Andrzej Siewior * 3917ba7aeabSSebastian Andrzej Siewior * Allocates a frag from a page for receive buffer. 3927ba7aeabSSebastian Andrzej Siewior * Uses GFP_ATOMIC allocations. 3937ba7aeabSSebastian Andrzej Siewior */ 3947ba7aeabSSebastian Andrzej Siewior void *netdev_alloc_frag(unsigned int fragsz) 3957ba7aeabSSebastian Andrzej Siewior { 3967ba7aeabSSebastian Andrzej Siewior struct page_frag_cache *nc; 3977ba7aeabSSebastian Andrzej Siewior void *data; 3987ba7aeabSSebastian Andrzej Siewior 3997ba7aeabSSebastian Andrzej Siewior fragsz = SKB_DATA_ALIGN(fragsz); 4007ba7aeabSSebastian Andrzej Siewior if (in_irq() || irqs_disabled()) { 4017ba7aeabSSebastian Andrzej Siewior nc = this_cpu_ptr(&netdev_alloc_cache); 4027ba7aeabSSebastian Andrzej Siewior data = page_frag_alloc(nc, fragsz, GFP_ATOMIC); 4037ba7aeabSSebastian Andrzej Siewior } else { 4047ba7aeabSSebastian Andrzej Siewior local_bh_disable(); 4057ba7aeabSSebastian Andrzej Siewior data = __napi_alloc_frag(fragsz, GFP_ATOMIC); 4067ba7aeabSSebastian Andrzej Siewior local_bh_enable(); 4077ba7aeabSSebastian Andrzej Siewior } 4087ba7aeabSSebastian Andrzej Siewior return data; 4097ba7aeabSSebastian Andrzej Siewior } 4107ba7aeabSSebastian Andrzej Siewior EXPORT_SYMBOL(netdev_alloc_frag); 4117ba7aeabSSebastian Andrzej Siewior 4127ba7aeabSSebastian Andrzej Siewior /** 413fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 414fd11a83dSAlexander Duyck * @dev: network device to receive on 415d7499160SMasanari Iida * @len: length to allocate 416fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 417fd11a83dSAlexander Duyck * 418fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 419fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 420fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 421fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 422fd11a83dSAlexander Duyck * 423fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 424fd11a83dSAlexander Duyck */ 4259451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4269451980aSAlexander Duyck gfp_t gfp_mask) 427fd11a83dSAlexander Duyck { 428b63ae8caSAlexander Duyck struct page_frag_cache *nc; 429fd11a83dSAlexander Duyck struct sk_buff *skb; 4309451980aSAlexander Duyck bool pfmemalloc; 4319451980aSAlexander Duyck void *data; 432fd11a83dSAlexander Duyck 4339451980aSAlexander Duyck len += NET_SKB_PAD; 434fd11a83dSAlexander Duyck 4359451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 436d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 437a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 438a080e7bdSAlexander Duyck if (!skb) 439a080e7bdSAlexander Duyck goto skb_fail; 440a080e7bdSAlexander Duyck goto skb_success; 441a080e7bdSAlexander Duyck } 4429451980aSAlexander Duyck 4439451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4449451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4459451980aSAlexander Duyck 4469451980aSAlexander Duyck if (sk_memalloc_socks()) 4479451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4489451980aSAlexander Duyck 44992dcabd7SSebastian Andrzej Siewior if (in_irq() || irqs_disabled()) { 4509451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4518c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4529451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 45392dcabd7SSebastian Andrzej Siewior } else { 45492dcabd7SSebastian Andrzej Siewior local_bh_disable(); 45592dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 45692dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 45792dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 45892dcabd7SSebastian Andrzej Siewior local_bh_enable(); 45992dcabd7SSebastian Andrzej Siewior } 4609451980aSAlexander Duyck 4619451980aSAlexander Duyck if (unlikely(!data)) 4629451980aSAlexander Duyck return NULL; 4639451980aSAlexander Duyck 4649451980aSAlexander Duyck skb = __build_skb(data, len); 4659451980aSAlexander Duyck if (unlikely(!skb)) { 466181edb2bSAlexander Duyck skb_free_frag(data); 4679451980aSAlexander Duyck return NULL; 4689451980aSAlexander Duyck } 4699451980aSAlexander Duyck 4709451980aSAlexander Duyck if (pfmemalloc) 4719451980aSAlexander Duyck skb->pfmemalloc = 1; 4729451980aSAlexander Duyck skb->head_frag = 1; 4739451980aSAlexander Duyck 474a080e7bdSAlexander Duyck skb_success: 4758af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4767b2e497aSChristoph Hellwig skb->dev = dev; 477fd11a83dSAlexander Duyck 478a080e7bdSAlexander Duyck skb_fail: 4798af27456SChristoph Hellwig return skb; 4808af27456SChristoph Hellwig } 481b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4821da177e4SLinus Torvalds 483fd11a83dSAlexander Duyck /** 484fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 485fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 486d7499160SMasanari Iida * @len: length to allocate 487fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 488fd11a83dSAlexander Duyck * 489fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 490fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 491fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 492fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 493fd11a83dSAlexander Duyck * 494fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 495fd11a83dSAlexander Duyck */ 4969451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4979451980aSAlexander Duyck gfp_t gfp_mask) 498fd11a83dSAlexander Duyck { 499795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 500fd11a83dSAlexander Duyck struct sk_buff *skb; 5019451980aSAlexander Duyck void *data; 502fd11a83dSAlexander Duyck 5039451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 504fd11a83dSAlexander Duyck 5059451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 506d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 507a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 508a080e7bdSAlexander Duyck if (!skb) 509a080e7bdSAlexander Duyck goto skb_fail; 510a080e7bdSAlexander Duyck goto skb_success; 511a080e7bdSAlexander Duyck } 5129451980aSAlexander Duyck 5139451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5149451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5159451980aSAlexander Duyck 5169451980aSAlexander Duyck if (sk_memalloc_socks()) 5179451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5189451980aSAlexander Duyck 5198c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 5209451980aSAlexander Duyck if (unlikely(!data)) 5219451980aSAlexander Duyck return NULL; 5229451980aSAlexander Duyck 5239451980aSAlexander Duyck skb = __build_skb(data, len); 5249451980aSAlexander Duyck if (unlikely(!skb)) { 525181edb2bSAlexander Duyck skb_free_frag(data); 5269451980aSAlexander Duyck return NULL; 5279451980aSAlexander Duyck } 5289451980aSAlexander Duyck 529795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5309451980aSAlexander Duyck skb->pfmemalloc = 1; 5319451980aSAlexander Duyck skb->head_frag = 1; 5329451980aSAlexander Duyck 533a080e7bdSAlexander Duyck skb_success: 534fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 535fd11a83dSAlexander Duyck skb->dev = napi->dev; 536fd11a83dSAlexander Duyck 537a080e7bdSAlexander Duyck skb_fail: 538fd11a83dSAlexander Duyck return skb; 539fd11a83dSAlexander Duyck } 540fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 541fd11a83dSAlexander Duyck 542654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 54350269e19SEric Dumazet int size, unsigned int truesize) 544654bed16SPeter Zijlstra { 545654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 546654bed16SPeter Zijlstra skb->len += size; 547654bed16SPeter Zijlstra skb->data_len += size; 54850269e19SEric Dumazet skb->truesize += truesize; 549654bed16SPeter Zijlstra } 550654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 551654bed16SPeter Zijlstra 552f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 553f8e617e1SJason Wang unsigned int truesize) 554f8e617e1SJason Wang { 555f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 556f8e617e1SJason Wang 557f8e617e1SJason Wang skb_frag_size_add(frag, size); 558f8e617e1SJason Wang skb->len += size; 559f8e617e1SJason Wang skb->data_len += size; 560f8e617e1SJason Wang skb->truesize += truesize; 561f8e617e1SJason Wang } 562f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 563f8e617e1SJason Wang 56427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5651da177e4SLinus Torvalds { 566bd8a7036SEric Dumazet kfree_skb_list(*listp); 56727b437c8SHerbert Xu *listp = NULL; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 57027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 57127b437c8SHerbert Xu { 57227b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 57327b437c8SHerbert Xu } 57427b437c8SHerbert Xu 5751da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds struct sk_buff *list; 5781da177e4SLinus Torvalds 579fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5801da177e4SLinus Torvalds skb_get(list); 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 583d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 584d3836f21SEric Dumazet { 585181edb2bSAlexander Duyck unsigned char *head = skb->head; 586181edb2bSAlexander Duyck 587d3836f21SEric Dumazet if (skb->head_frag) 588181edb2bSAlexander Duyck skb_free_frag(head); 589d3836f21SEric Dumazet else 590181edb2bSAlexander Duyck kfree(head); 591d3836f21SEric Dumazet } 592d3836f21SEric Dumazet 5935bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5941da177e4SLinus Torvalds { 595ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5961da177e4SLinus Torvalds int i; 597ff04a771SEric Dumazet 598ff04a771SEric Dumazet if (skb->cloned && 599ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 600ff04a771SEric Dumazet &shinfo->dataref)) 601ff04a771SEric Dumazet return; 602ff04a771SEric Dumazet 603ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 604ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 6051da177e4SLinus Torvalds 606ff04a771SEric Dumazet if (shinfo->frag_list) 607ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6081da177e4SLinus Torvalds 6091f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 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); 6491da177e4SLinus Torvalds if (skb->destructor) { 6509c2b3328SStephen Hemminger WARN_ON(in_irq()); 6511da177e4SLinus Torvalds skb->destructor(skb); 6521da177e4SLinus Torvalds } 653a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 654cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6552fc72c7bSKOVACS Krisztian #endif 656df5042f4SFlorian Westphal skb_ext_put(skb); 65704a4bb55SLennert Buytenhek } 65804a4bb55SLennert Buytenhek 65904a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 66004a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 66104a4bb55SLennert Buytenhek { 66204a4bb55SLennert Buytenhek skb_release_head_state(skb); 663a28b1b90SFlorian Westphal if (likely(skb->head)) 6642d4baff8SHerbert Xu skb_release_data(skb); 6652d4baff8SHerbert Xu } 6661da177e4SLinus Torvalds 6672d4baff8SHerbert Xu /** 6682d4baff8SHerbert Xu * __kfree_skb - private function 6692d4baff8SHerbert Xu * @skb: buffer 6702d4baff8SHerbert Xu * 6712d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6722d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6732d4baff8SHerbert Xu * always call kfree_skb 6742d4baff8SHerbert Xu */ 6752d4baff8SHerbert Xu 6762d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6772d4baff8SHerbert Xu { 6782d4baff8SHerbert Xu skb_release_all(skb); 6791da177e4SLinus Torvalds kfree_skbmem(skb); 6801da177e4SLinus Torvalds } 681b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds /** 684231d06aeSJörn Engel * kfree_skb - free an sk_buff 685231d06aeSJörn Engel * @skb: buffer to free 686231d06aeSJörn Engel * 687231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 688231d06aeSJörn Engel * hit zero. 689231d06aeSJörn Engel */ 690231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 691231d06aeSJörn Engel { 6923889a803SPaolo Abeni if (!skb_unref(skb)) 693231d06aeSJörn Engel return; 6943889a803SPaolo Abeni 695ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 696231d06aeSJörn Engel __kfree_skb(skb); 697231d06aeSJörn Engel } 698b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 699231d06aeSJörn Engel 700bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 701bd8a7036SEric Dumazet { 702bd8a7036SEric Dumazet while (segs) { 703bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 704bd8a7036SEric Dumazet 705bd8a7036SEric Dumazet kfree_skb(segs); 706bd8a7036SEric Dumazet segs = next; 707bd8a7036SEric Dumazet } 708bd8a7036SEric Dumazet } 709bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 710bd8a7036SEric Dumazet 7116413139dSWillem de Bruijn /* Dump skb information and contents. 7126413139dSWillem de Bruijn * 7136413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 7146413139dSWillem de Bruijn * 7156413139dSWillem de Bruijn * Dumps up to can_dump_full whole packets if full_pkt, headers otherwise. 7166413139dSWillem de Bruijn */ 7176413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 7186413139dSWillem de Bruijn { 7196413139dSWillem de Bruijn static atomic_t can_dump_full = ATOMIC_INIT(5); 7206413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 7216413139dSWillem de Bruijn struct net_device *dev = skb->dev; 7226413139dSWillem de Bruijn struct sock *sk = skb->sk; 7236413139dSWillem de Bruijn struct sk_buff *list_skb; 7246413139dSWillem de Bruijn bool has_mac, has_trans; 7256413139dSWillem de Bruijn int headroom, tailroom; 7266413139dSWillem de Bruijn int i, len, seg_len; 7276413139dSWillem de Bruijn 7286413139dSWillem de Bruijn if (full_pkt) 7296413139dSWillem de Bruijn full_pkt = atomic_dec_if_positive(&can_dump_full) >= 0; 7306413139dSWillem de Bruijn 7316413139dSWillem de Bruijn if (full_pkt) 7326413139dSWillem de Bruijn len = skb->len; 7336413139dSWillem de Bruijn else 7346413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 7356413139dSWillem de Bruijn 7366413139dSWillem de Bruijn headroom = skb_headroom(skb); 7376413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 7386413139dSWillem de Bruijn 7396413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 7406413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 7416413139dSWillem de Bruijn 7426413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 7436413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 7446413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 7456413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 7466413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 7476413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 7486413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 7496413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 7506413139dSWillem de Bruijn skb->network_header, 7516413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 7526413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 7536413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 7546413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 7556413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 7566413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 7576413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 7586413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 7596413139dSWillem de Bruijn 7606413139dSWillem de Bruijn if (dev) 7616413139dSWillem de Bruijn printk("%sdev name=%s feat=0x%pNF\n", 7626413139dSWillem de Bruijn level, dev->name, &dev->features); 7636413139dSWillem de Bruijn if (sk) 764db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 7656413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 7666413139dSWillem de Bruijn 7676413139dSWillem de Bruijn if (full_pkt && headroom) 7686413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 7696413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 7706413139dSWillem de Bruijn 7716413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 7726413139dSWillem de Bruijn if (seg_len) 7736413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 7746413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 7756413139dSWillem de Bruijn len -= seg_len; 7766413139dSWillem de Bruijn 7776413139dSWillem de Bruijn if (full_pkt && tailroom) 7786413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 7796413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 7806413139dSWillem de Bruijn 7816413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 7826413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 7836413139dSWillem de Bruijn u32 p_off, p_len, copied; 7846413139dSWillem de Bruijn struct page *p; 7856413139dSWillem de Bruijn u8 *vaddr; 7866413139dSWillem de Bruijn 787b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 7886413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 7896413139dSWillem de Bruijn copied) { 7906413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 7916413139dSWillem de Bruijn vaddr = kmap_atomic(p); 7926413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 7936413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 7946413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 7956413139dSWillem de Bruijn kunmap_atomic(vaddr); 7966413139dSWillem de Bruijn len -= seg_len; 7976413139dSWillem de Bruijn if (!len) 7986413139dSWillem de Bruijn break; 7996413139dSWillem de Bruijn } 8006413139dSWillem de Bruijn } 8016413139dSWillem de Bruijn 8026413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 8036413139dSWillem de Bruijn printk("skb fraglist:\n"); 8046413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 8056413139dSWillem de Bruijn skb_dump(level, list_skb, true); 8066413139dSWillem de Bruijn } 8076413139dSWillem de Bruijn } 8086413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 8096413139dSWillem de Bruijn 810d1a203eaSStephen Hemminger /** 81125121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 81225121173SMichael S. Tsirkin * @skb: buffer that triggered an error 81325121173SMichael S. Tsirkin * 81425121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 81525121173SMichael S. Tsirkin * skb must be freed afterwards. 81625121173SMichael S. Tsirkin */ 81725121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 81825121173SMichael S. Tsirkin { 8191f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 82025121173SMichael S. Tsirkin } 82125121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 82225121173SMichael S. Tsirkin 82325121173SMichael S. Tsirkin /** 824ead2ceb0SNeil Horman * consume_skb - free an skbuff 825ead2ceb0SNeil Horman * @skb: buffer to free 826ead2ceb0SNeil Horman * 827ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 828ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 829ead2ceb0SNeil Horman * is being dropped after a failure and notes that 830ead2ceb0SNeil Horman */ 831ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 832ead2ceb0SNeil Horman { 8333889a803SPaolo Abeni if (!skb_unref(skb)) 834ead2ceb0SNeil Horman return; 8353889a803SPaolo Abeni 83607dc22e7SKoki Sanagi trace_consume_skb(skb); 837ead2ceb0SNeil Horman __kfree_skb(skb); 838ead2ceb0SNeil Horman } 839ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 840ead2ceb0SNeil Horman 8410a463c78SPaolo Abeni /** 8420a463c78SPaolo Abeni * consume_stateless_skb - free an skbuff, assuming it is stateless 8430a463c78SPaolo Abeni * @skb: buffer to free 8440a463c78SPaolo Abeni * 845ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 846ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 8470a463c78SPaolo Abeni */ 848ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 8490a463c78SPaolo Abeni { 8500a463c78SPaolo Abeni trace_consume_skb(skb); 8510a463c78SPaolo Abeni skb_release_data(skb); 8520a463c78SPaolo Abeni kfree_skbmem(skb); 8530a463c78SPaolo Abeni } 8540a463c78SPaolo Abeni 855795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 856795bb1c0SJesper Dangaard Brouer { 857795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 858795bb1c0SJesper Dangaard Brouer 859795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 860795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 861795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 862795bb1c0SJesper Dangaard Brouer nc->skb_cache); 863795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 864795bb1c0SJesper Dangaard Brouer } 865795bb1c0SJesper Dangaard Brouer } 866795bb1c0SJesper Dangaard Brouer 86715fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 868795bb1c0SJesper Dangaard Brouer { 869795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 870795bb1c0SJesper Dangaard Brouer 871795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 872795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 873795bb1c0SJesper Dangaard Brouer 874795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 875795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 876795bb1c0SJesper Dangaard Brouer 877795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 878795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 879795bb1c0SJesper Dangaard Brouer prefetchw(skb); 880795bb1c0SJesper Dangaard Brouer #endif 881795bb1c0SJesper Dangaard Brouer 882795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 883795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 884795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 885795bb1c0SJesper Dangaard Brouer nc->skb_cache); 886795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 887795bb1c0SJesper Dangaard Brouer } 888795bb1c0SJesper Dangaard Brouer } 88915fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 89015fad714SJesper Dangaard Brouer { 89115fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 89215fad714SJesper Dangaard Brouer } 893795bb1c0SJesper Dangaard Brouer 894795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 895795bb1c0SJesper Dangaard Brouer { 896795bb1c0SJesper Dangaard Brouer if (unlikely(!skb)) 897795bb1c0SJesper Dangaard Brouer return; 898795bb1c0SJesper Dangaard Brouer 899885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 900795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 901885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 902795bb1c0SJesper Dangaard Brouer return; 903795bb1c0SJesper Dangaard Brouer } 904795bb1c0SJesper Dangaard Brouer 9057608894eSPaolo Abeni if (!skb_unref(skb)) 906795bb1c0SJesper Dangaard Brouer return; 9077608894eSPaolo Abeni 908795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 909795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 910795bb1c0SJesper Dangaard Brouer 911795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 912abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 913795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 914795bb1c0SJesper Dangaard Brouer return; 915795bb1c0SJesper Dangaard Brouer } 916795bb1c0SJesper Dangaard Brouer 91715fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 918795bb1c0SJesper Dangaard Brouer } 919795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 920795bb1c0SJesper Dangaard Brouer 921b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 922b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 923b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 924b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 925b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 926b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 927b1937227SEric Dumazet 928dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 929dec18810SHerbert Xu { 930dec18810SHerbert Xu new->tstamp = old->tstamp; 931b1937227SEric Dumazet /* We do not copy old->sk */ 932dec18810SHerbert Xu new->dev = old->dev; 933b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 9347fee226aSEric Dumazet skb_dst_copy(new, old); 935df5042f4SFlorian Westphal __skb_ext_copy(new, old); 936b1937227SEric Dumazet __nf_copy(new, old, false); 9376aa895b0SPatrick McHardy 938b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 939b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 940b1937227SEric Dumazet */ 941b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 94206021292SEliezer Tamir 943b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 944b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 945b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 946b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 947b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 948b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 949b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 950b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 951b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 952b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 953b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 954b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 955b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 956b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 957b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 958b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 959b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 960b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 961b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 962b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 963b1937227SEric Dumazet #endif 964e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 965b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 96606021292SEliezer Tamir #endif 9672bd82484SEric Dumazet #ifdef CONFIG_XPS 9682bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 9692bd82484SEric Dumazet #endif 970b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 971b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 972b1937227SEric Dumazet #endif 973b1937227SEric Dumazet 974dec18810SHerbert Xu } 975dec18810SHerbert Xu 97682c49a35SHerbert Xu /* 97782c49a35SHerbert Xu * You should not add any new code to this function. Add it to 97882c49a35SHerbert Xu * __copy_skb_header above instead. 97982c49a35SHerbert Xu */ 980e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds #define C(x) n->x = skb->x 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds n->next = n->prev = NULL; 9851da177e4SLinus Torvalds n->sk = NULL; 986dec18810SHerbert Xu __copy_skb_header(n, skb); 987dec18810SHerbert Xu 9881da177e4SLinus Torvalds C(len); 9891da177e4SLinus Torvalds C(data_len); 9903e6b3b2eSAlexey Dobriyan C(mac_len); 991334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 99202f1c89dSPaul Moore n->cloned = 1; 9931da177e4SLinus Torvalds n->nohdr = 0; 994b13dda9fSEric Dumazet n->peeked = 0; 995e78bfb07SStefano Brivio C(pfmemalloc); 9961da177e4SLinus Torvalds n->destructor = NULL; 9971da177e4SLinus Torvalds C(tail); 9981da177e4SLinus Torvalds C(end); 99902f1c89dSPaul Moore C(head); 1000d3836f21SEric Dumazet C(head_frag); 100102f1c89dSPaul Moore C(data); 100202f1c89dSPaul Moore C(truesize); 100363354797SReshetova, Elena refcount_set(&n->users, 1); 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 10061da177e4SLinus Torvalds skb->cloned = 1; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds return n; 1009e0053ec0SHerbert Xu #undef C 1010e0053ec0SHerbert Xu } 1011e0053ec0SHerbert Xu 1012e0053ec0SHerbert Xu /** 1013da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1014da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1015da29e4b4SJakub Kicinski */ 1016da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1017da29e4b4SJakub Kicinski { 1018da29e4b4SJakub Kicinski struct sk_buff *n; 1019da29e4b4SJakub Kicinski 1020da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1021da29e4b4SJakub Kicinski if (!n) 1022da29e4b4SJakub Kicinski return NULL; 1023da29e4b4SJakub Kicinski 1024da29e4b4SJakub Kicinski n->len = first->len; 1025da29e4b4SJakub Kicinski n->data_len = first->len; 1026da29e4b4SJakub Kicinski n->truesize = first->truesize; 1027da29e4b4SJakub Kicinski 1028da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1029da29e4b4SJakub Kicinski 1030da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1031da29e4b4SJakub Kicinski n->destructor = NULL; 1032da29e4b4SJakub Kicinski 1033da29e4b4SJakub Kicinski return n; 1034da29e4b4SJakub Kicinski } 1035da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1036da29e4b4SJakub Kicinski 1037da29e4b4SJakub Kicinski /** 1038e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1039e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1040e0053ec0SHerbert Xu * @src: the skb to supply the contents 1041e0053ec0SHerbert Xu * 1042e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1043e0053ec0SHerbert Xu * supplied by the user. 1044e0053ec0SHerbert Xu * 1045e0053ec0SHerbert Xu * The target skb is returned upon exit. 1046e0053ec0SHerbert Xu */ 1047e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1048e0053ec0SHerbert Xu { 10492d4baff8SHerbert Xu skb_release_all(dst); 1050e0053ec0SHerbert Xu return __skb_clone(dst, src); 1051e0053ec0SHerbert Xu } 1052e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1053e0053ec0SHerbert Xu 10546f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1055a91dbff5SWillem de Bruijn { 1056a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1057a91dbff5SWillem de Bruijn struct user_struct *user; 1058a91dbff5SWillem de Bruijn 1059a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1060a91dbff5SWillem de Bruijn return 0; 1061a91dbff5SWillem de Bruijn 1062a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1063a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1064a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1065a91dbff5SWillem de Bruijn 1066a91dbff5SWillem de Bruijn do { 1067a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 1068a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1069a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1070a91dbff5SWillem de Bruijn return -ENOBUFS; 1071a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 1072a91dbff5SWillem de Bruijn old_pg); 1073a91dbff5SWillem de Bruijn 1074a91dbff5SWillem de Bruijn if (!mmp->user) { 1075a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1076a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1077a91dbff5SWillem de Bruijn } else { 1078a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1079a91dbff5SWillem de Bruijn } 1080a91dbff5SWillem de Bruijn 1081a91dbff5SWillem de Bruijn return 0; 1082a91dbff5SWillem de Bruijn } 10836f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1084a91dbff5SWillem de Bruijn 10856f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1086a91dbff5SWillem de Bruijn { 1087a91dbff5SWillem de Bruijn if (mmp->user) { 1088a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1089a91dbff5SWillem de Bruijn free_uid(mmp->user); 1090a91dbff5SWillem de Bruijn } 1091a91dbff5SWillem de Bruijn } 10926f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1093a91dbff5SWillem de Bruijn 109452267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 109552267790SWillem de Bruijn { 109652267790SWillem de Bruijn struct ubuf_info *uarg; 109752267790SWillem de Bruijn struct sk_buff *skb; 109852267790SWillem de Bruijn 109952267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 110052267790SWillem de Bruijn 110152267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 110252267790SWillem de Bruijn if (!skb) 110352267790SWillem de Bruijn return NULL; 110452267790SWillem de Bruijn 110552267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 110652267790SWillem de Bruijn uarg = (void *)skb->cb; 1107a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1108a91dbff5SWillem de Bruijn 1109a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1110a91dbff5SWillem de Bruijn kfree_skb(skb); 1111a91dbff5SWillem de Bruijn return NULL; 1112a91dbff5SWillem de Bruijn } 111352267790SWillem de Bruijn 111452267790SWillem de Bruijn uarg->callback = sock_zerocopy_callback; 11154ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 11164ab6c99dSWillem de Bruijn uarg->len = 1; 11174ab6c99dSWillem de Bruijn uarg->bytelen = size; 111852267790SWillem de Bruijn uarg->zerocopy = 1; 1119c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 112052267790SWillem de Bruijn sock_hold(sk); 112152267790SWillem de Bruijn 112252267790SWillem de Bruijn return uarg; 112352267790SWillem de Bruijn } 112452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 112552267790SWillem de Bruijn 112652267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 112752267790SWillem de Bruijn { 112852267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 112952267790SWillem de Bruijn } 113052267790SWillem de Bruijn 11314ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 11324ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 11334ab6c99dSWillem de Bruijn { 11344ab6c99dSWillem de Bruijn if (uarg) { 11354ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 11364ab6c99dSWillem de Bruijn u32 bytelen, next; 11374ab6c99dSWillem de Bruijn 11384ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 11394ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 11404ab6c99dSWillem de Bruijn */ 11414ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 11424ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 11434ab6c99dSWillem de Bruijn return NULL; 11444ab6c99dSWillem de Bruijn } 11454ab6c99dSWillem de Bruijn 11464ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 11474ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 11484ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 11494ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 11504ab6c99dSWillem de Bruijn goto new_alloc; 11514ab6c99dSWillem de Bruijn return NULL; 11524ab6c99dSWillem de Bruijn } 11534ab6c99dSWillem de Bruijn 11544ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 11554ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 1156a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 1157a91dbff5SWillem de Bruijn return NULL; 11584ab6c99dSWillem de Bruijn uarg->len++; 11594ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 11604ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1161100f6d8eSWillem de Bruijn 1162100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1163100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 1164db5bce32SEric Dumazet sock_zerocopy_get(uarg); 1165100f6d8eSWillem de Bruijn 11664ab6c99dSWillem de Bruijn return uarg; 11674ab6c99dSWillem de Bruijn } 11684ab6c99dSWillem de Bruijn } 11694ab6c99dSWillem de Bruijn 11704ab6c99dSWillem de Bruijn new_alloc: 11714ab6c99dSWillem de Bruijn return sock_zerocopy_alloc(sk, size); 11724ab6c99dSWillem de Bruijn } 11734ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 11744ab6c99dSWillem de Bruijn 11754ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 11764ab6c99dSWillem de Bruijn { 11774ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 11784ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 11794ab6c99dSWillem de Bruijn u64 sum_len; 11804ab6c99dSWillem de Bruijn 11814ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 11824ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 11834ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 11844ab6c99dSWillem de Bruijn 11854ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 11864ab6c99dSWillem de Bruijn return false; 11874ab6c99dSWillem de Bruijn 11884ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 11894ab6c99dSWillem de Bruijn return false; 11904ab6c99dSWillem de Bruijn 11914ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 11924ab6c99dSWillem de Bruijn return true; 11934ab6c99dSWillem de Bruijn } 11944ab6c99dSWillem de Bruijn 119552267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) 119652267790SWillem de Bruijn { 11974ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 119852267790SWillem de Bruijn struct sock_exterr_skb *serr; 119952267790SWillem de Bruijn struct sock *sk = skb->sk; 12004ab6c99dSWillem de Bruijn struct sk_buff_head *q; 12014ab6c99dSWillem de Bruijn unsigned long flags; 12024ab6c99dSWillem de Bruijn u32 lo, hi; 12034ab6c99dSWillem de Bruijn u16 len; 120452267790SWillem de Bruijn 1205ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1206ccaffff1SWillem de Bruijn 12074ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 12084ab6c99dSWillem de Bruijn * so do not queue a completion notification 12094ab6c99dSWillem de Bruijn */ 12104ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 121152267790SWillem de Bruijn goto release; 121252267790SWillem de Bruijn 12134ab6c99dSWillem de Bruijn len = uarg->len; 12144ab6c99dSWillem de Bruijn lo = uarg->id; 12154ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 12164ab6c99dSWillem de Bruijn 121752267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 121852267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 121952267790SWillem de Bruijn serr->ee.ee_errno = 0; 122052267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 12214ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 12224ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 122352267790SWillem de Bruijn if (!success) 122452267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 122552267790SWillem de Bruijn 12264ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 12274ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 12284ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 12294ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 12304ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 12314ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 123252267790SWillem de Bruijn skb = NULL; 12334ab6c99dSWillem de Bruijn } 12344ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 123552267790SWillem de Bruijn 123652267790SWillem de Bruijn sk->sk_error_report(sk); 123752267790SWillem de Bruijn 123852267790SWillem de Bruijn release: 123952267790SWillem de Bruijn consume_skb(skb); 124052267790SWillem de Bruijn sock_put(sk); 124152267790SWillem de Bruijn } 124252267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 124352267790SWillem de Bruijn 124452267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg) 124552267790SWillem de Bruijn { 1246c1d1b437SEric Dumazet if (uarg && refcount_dec_and_test(&uarg->refcnt)) { 124752267790SWillem de Bruijn if (uarg->callback) 124852267790SWillem de Bruijn uarg->callback(uarg, uarg->zerocopy); 124952267790SWillem de Bruijn else 125052267790SWillem de Bruijn consume_skb(skb_from_uarg(uarg)); 125152267790SWillem de Bruijn } 125252267790SWillem de Bruijn } 125352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put); 125452267790SWillem de Bruijn 125552900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 125652267790SWillem de Bruijn { 125752267790SWillem de Bruijn if (uarg) { 125852267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 125952267790SWillem de Bruijn 126052267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 12614ab6c99dSWillem de Bruijn uarg->len--; 126252267790SWillem de Bruijn 126352900d22SWillem de Bruijn if (have_uref) 126452267790SWillem de Bruijn sock_zerocopy_put(uarg); 126552267790SWillem de Bruijn } 126652267790SWillem de Bruijn } 126752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 126852267790SWillem de Bruijn 1269b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) 1270b5947e5dSWillem de Bruijn { 1271b5947e5dSWillem de Bruijn return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); 1272b5947e5dSWillem de Bruijn } 1273b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); 1274b5947e5dSWillem de Bruijn 127552267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 127652267790SWillem de Bruijn struct msghdr *msg, int len, 127752267790SWillem de Bruijn struct ubuf_info *uarg) 127852267790SWillem de Bruijn { 12794ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 128052267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 128152267790SWillem de Bruijn int err, orig_len = skb->len; 128252267790SWillem de Bruijn 12834ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 12844ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 12854ab6c99dSWillem de Bruijn */ 12864ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 12874ab6c99dSWillem de Bruijn return -EEXIST; 12884ab6c99dSWillem de Bruijn 128952267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 129052267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 129154d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 129254d43117SWillem de Bruijn 129352267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 129452267790SWillem de Bruijn msg->msg_iter = orig_iter; 129554d43117SWillem de Bruijn skb->sk = sk; 129652267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 129754d43117SWillem de Bruijn skb->sk = save_sk; 129852267790SWillem de Bruijn return err; 129952267790SWillem de Bruijn } 130052267790SWillem de Bruijn 130152900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 130252267790SWillem de Bruijn return skb->len - orig_len; 130352267790SWillem de Bruijn } 130452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 130552267790SWillem de Bruijn 13061f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 130752267790SWillem de Bruijn gfp_t gfp_mask) 130852267790SWillem de Bruijn { 130952267790SWillem de Bruijn if (skb_zcopy(orig)) { 131052267790SWillem de Bruijn if (skb_zcopy(nskb)) { 131152267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 131252267790SWillem de Bruijn if (!gfp_mask) { 131352267790SWillem de Bruijn WARN_ON_ONCE(1); 131452267790SWillem de Bruijn return -ENOMEM; 131552267790SWillem de Bruijn } 131652267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 131752267790SWillem de Bruijn return 0; 131852267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 131952267790SWillem de Bruijn return -EIO; 132052267790SWillem de Bruijn } 132152900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 132252267790SWillem de Bruijn } 132352267790SWillem de Bruijn return 0; 132452267790SWillem de Bruijn } 132552267790SWillem de Bruijn 13262c53040fSBen Hutchings /** 13272c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 132848c83012SMichael S. Tsirkin * @skb: the skb to modify 132948c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 133048c83012SMichael S. Tsirkin * 133148c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 133248c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 133348c83012SMichael S. Tsirkin * to userspace pages. 133448c83012SMichael S. Tsirkin * 133548c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 133648c83012SMichael S. Tsirkin * %GFP_ATOMIC. 133748c83012SMichael S. Tsirkin * 133848c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 133948c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 134048c83012SMichael S. Tsirkin */ 134148c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1342a6686f2fSShirley Ma { 1343a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1344a6686f2fSShirley Ma struct page *page, *head = NULL; 13453ece7826SWillem de Bruijn int i, new_frags; 13463ece7826SWillem de Bruijn u32 d_off; 1347a6686f2fSShirley Ma 13483ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 13493ece7826SWillem de Bruijn return -EINVAL; 13503ece7826SWillem de Bruijn 1351f72c4ac6SWillem de Bruijn if (!num_frags) 1352f72c4ac6SWillem de Bruijn goto release; 1353f72c4ac6SWillem de Bruijn 13543ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 13553ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 135602756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1357a6686f2fSShirley Ma if (!page) { 1358a6686f2fSShirley Ma while (head) { 135940dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1360a6686f2fSShirley Ma put_page(head); 1361a6686f2fSShirley Ma head = next; 1362a6686f2fSShirley Ma } 1363a6686f2fSShirley Ma return -ENOMEM; 1364a6686f2fSShirley Ma } 13653ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 13663ece7826SWillem de Bruijn head = page; 13673ece7826SWillem de Bruijn } 13683ece7826SWillem de Bruijn 13693ece7826SWillem de Bruijn page = head; 13703ece7826SWillem de Bruijn d_off = 0; 13713ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 13723ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 13733ece7826SWillem de Bruijn u32 p_off, p_len, copied; 13743ece7826SWillem de Bruijn struct page *p; 13753ece7826SWillem de Bruijn u8 *vaddr; 1376c613c209SWillem de Bruijn 1377b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1378c613c209SWillem de Bruijn p, p_off, p_len, copied) { 13793ece7826SWillem de Bruijn u32 copy, done = 0; 1380c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 13813ece7826SWillem de Bruijn 13823ece7826SWillem de Bruijn while (done < p_len) { 13833ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 13843ece7826SWillem de Bruijn d_off = 0; 13853ece7826SWillem de Bruijn page = (struct page *)page_private(page); 13863ece7826SWillem de Bruijn } 13873ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 13883ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 13893ece7826SWillem de Bruijn vaddr + p_off + done, copy); 13903ece7826SWillem de Bruijn done += copy; 13913ece7826SWillem de Bruijn d_off += copy; 13923ece7826SWillem de Bruijn } 139351c56b00SEric Dumazet kunmap_atomic(vaddr); 1394c613c209SWillem de Bruijn } 1395a6686f2fSShirley Ma } 1396a6686f2fSShirley Ma 1397a6686f2fSShirley Ma /* skb frags release userspace buffers */ 139802756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1399a8605c60SIan Campbell skb_frag_unref(skb, i); 1400a6686f2fSShirley Ma 1401a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 14023ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 14033ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 140440dadff2SSunghan Suh head = (struct page *)page_private(head); 1405a6686f2fSShirley Ma } 14063ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 14073ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 140848c83012SMichael S. Tsirkin 1409b90ddd56SWillem de Bruijn release: 14101f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1411a6686f2fSShirley Ma return 0; 1412a6686f2fSShirley Ma } 1413dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1414a6686f2fSShirley Ma 1415e0053ec0SHerbert Xu /** 1416e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1417e0053ec0SHerbert Xu * @skb: buffer to clone 1418e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1419e0053ec0SHerbert Xu * 1420e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1421e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1422e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1423e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1424e0053ec0SHerbert Xu * 1425e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1426e0053ec0SHerbert Xu * %GFP_ATOMIC. 1427e0053ec0SHerbert Xu */ 1428e0053ec0SHerbert Xu 1429e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1430e0053ec0SHerbert Xu { 1431d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1432d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1433d0bf4a9eSEric Dumazet skb1); 14346ffe75ebSEric Dumazet struct sk_buff *n; 1435e0053ec0SHerbert Xu 143670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1437a6686f2fSShirley Ma return NULL; 1438a6686f2fSShirley Ma 1439e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 14402638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 14416ffe75ebSEric Dumazet n = &fclones->skb2; 14422638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1443e0053ec0SHerbert Xu } else { 1444c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1445c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1446c93bdd0eSMel Gorman 1447e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1448e0053ec0SHerbert Xu if (!n) 1449e0053ec0SHerbert Xu return NULL; 1450fe55f6d5SVegard Nossum 1451e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1452e0053ec0SHerbert Xu } 1453e0053ec0SHerbert Xu 1454e0053ec0SHerbert Xu return __skb_clone(n, skb); 14551da177e4SLinus Torvalds } 1456b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 14571da177e4SLinus Torvalds 1458b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1459f5b17294SPravin B Shelar { 1460030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1461030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1462030737bcSEric Dumazet skb->csum_start += off; 1463f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1464f5b17294SPravin B Shelar skb->transport_header += off; 1465f5b17294SPravin B Shelar skb->network_header += off; 1466f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1467f5b17294SPravin B Shelar skb->mac_header += off; 1468f5b17294SPravin B Shelar skb->inner_transport_header += off; 1469f5b17294SPravin B Shelar skb->inner_network_header += off; 1470aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1471f5b17294SPravin B Shelar } 1472b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1473f5b17294SPravin B Shelar 147408303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 14751da177e4SLinus Torvalds { 1476dec18810SHerbert Xu __copy_skb_header(new, old); 1477dec18810SHerbert Xu 14787967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 14797967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 14807967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 14811da177e4SLinus Torvalds } 148208303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 14831da177e4SLinus Torvalds 1484c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1485c93bdd0eSMel Gorman { 1486c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1487c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1488c93bdd0eSMel Gorman return 0; 1489c93bdd0eSMel Gorman } 1490c93bdd0eSMel Gorman 14911da177e4SLinus Torvalds /** 14921da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 14931da177e4SLinus Torvalds * @skb: buffer to copy 14941da177e4SLinus Torvalds * @gfp_mask: allocation priority 14951da177e4SLinus Torvalds * 14961da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 14971da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 14981da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 14991da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15001da177e4SLinus Torvalds * 15011da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 15021da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 15031da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 15041da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 15051da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 15061da177e4SLinus Torvalds */ 15071da177e4SLinus Torvalds 1508dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 15091da177e4SLinus Torvalds { 15106602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1511ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1512c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1513c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 15146602cebbSEric Dumazet 15151da177e4SLinus Torvalds if (!n) 15161da177e4SLinus Torvalds return NULL; 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds /* Set the data pointer */ 15191da177e4SLinus Torvalds skb_reserve(n, headerlen); 15201da177e4SLinus Torvalds /* Set the tail pointer and length */ 15211da177e4SLinus Torvalds skb_put(n, skb->len); 15221da177e4SLinus Torvalds 15239f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 15241da177e4SLinus Torvalds 152508303c18SIlya Lesokhin skb_copy_header(n, skb); 15261da177e4SLinus Torvalds return n; 15271da177e4SLinus Torvalds } 1528b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds /** 1531bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 15321da177e4SLinus Torvalds * @skb: buffer to copy 1533117632e6SEric Dumazet * @headroom: headroom of new skb 15341da177e4SLinus Torvalds * @gfp_mask: allocation priority 1535bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1536bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1537bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 15381da177e4SLinus Torvalds * 15391da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 15401da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 15411da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 15421da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 15431da177e4SLinus Torvalds * or the pointer to the buffer on success. 15441da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 15451da177e4SLinus Torvalds */ 15461da177e4SLinus Torvalds 1547bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1548bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 15491da177e4SLinus Torvalds { 1550117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1551bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1552bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 15536602cebbSEric Dumazet 15541da177e4SLinus Torvalds if (!n) 15551da177e4SLinus Torvalds goto out; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* Set the data pointer */ 1558117632e6SEric Dumazet skb_reserve(n, headroom); 15591da177e4SLinus Torvalds /* Set the tail pointer and length */ 15601da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 15611da177e4SLinus Torvalds /* Copy the bytes */ 1562d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 15631da177e4SLinus Torvalds 156425f484a6SHerbert Xu n->truesize += skb->data_len; 15651da177e4SLinus Torvalds n->data_len = skb->data_len; 15661da177e4SLinus Torvalds n->len = skb->len; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 15691da177e4SLinus Torvalds int i; 15701da177e4SLinus Torvalds 15711f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 15721f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 15731511022cSDan Carpenter kfree_skb(n); 15741511022cSDan Carpenter n = NULL; 1575a6686f2fSShirley Ma goto out; 1576a6686f2fSShirley Ma } 15771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15781da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1579ea2ab693SIan Campbell skb_frag_ref(skb, i); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 158421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 15851da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 15861da177e4SLinus Torvalds skb_clone_fraglist(n); 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 158908303c18SIlya Lesokhin skb_copy_header(n, skb); 15901da177e4SLinus Torvalds out: 15911da177e4SLinus Torvalds return n; 15921da177e4SLinus Torvalds } 1593bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds /** 15961da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 15971da177e4SLinus Torvalds * @skb: buffer to reallocate 15981da177e4SLinus Torvalds * @nhead: room to add at head 15991da177e4SLinus Torvalds * @ntail: room to add at tail 16001da177e4SLinus Torvalds * @gfp_mask: allocation priority 16011da177e4SLinus Torvalds * 1602bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1603bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 16041da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 16051da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 16061da177e4SLinus Torvalds * 16071da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 16081da177e4SLinus Torvalds * reloaded after call to this function. 16091da177e4SLinus Torvalds */ 16101da177e4SLinus Torvalds 161186a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1612dd0fc66fSAl Viro gfp_t gfp_mask) 16131da177e4SLinus Torvalds { 1614158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1615158f323bSEric Dumazet int size = osize + nhead + ntail; 16161da177e4SLinus Torvalds long off; 1617158f323bSEric Dumazet u8 *data; 16181da177e4SLinus Torvalds 16194edd87adSHerbert Xu BUG_ON(nhead < 0); 16204edd87adSHerbert Xu 16219f77fad3STim Hansen BUG_ON(skb_shared(skb)); 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 16241da177e4SLinus Torvalds 1625c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1626c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1627c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1628c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 16291da177e4SLinus Torvalds if (!data) 16301da177e4SLinus Torvalds goto nodata; 163187151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 16346602cebbSEric Dumazet * optimized for the cases when header is void. 16356602cebbSEric Dumazet */ 16366602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 16376602cebbSEric Dumazet 16386602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 16396602cebbSEric Dumazet skb_shinfo(skb), 1640fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 16411da177e4SLinus Torvalds 16423e24591aSAlexander Duyck /* 16433e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 16443e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 16453e24591aSAlexander Duyck * be since all we did is relocate the values 16463e24591aSAlexander Duyck */ 16473e24591aSAlexander Duyck if (skb_cloned(skb)) { 164870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1649a6686f2fSShirley Ma goto nofrags; 16501f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1651c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 16521da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1653ea2ab693SIan Campbell skb_frag_ref(skb, i); 16541da177e4SLinus Torvalds 165521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 16561da177e4SLinus Torvalds skb_clone_fraglist(skb); 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds skb_release_data(skb); 16593e24591aSAlexander Duyck } else { 16603e24591aSAlexander Duyck skb_free_head(skb); 16611fd63041SEric Dumazet } 16621da177e4SLinus Torvalds off = (data + nhead) - skb->head; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds skb->head = data; 1665d3836f21SEric Dumazet skb->head_frag = 0; 16661da177e4SLinus Torvalds skb->data += off; 16674305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 16684305b541SArnaldo Carvalho de Melo skb->end = size; 166956eb8882SPatrick McHardy off = nhead; 16704305b541SArnaldo Carvalho de Melo #else 16714305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 167256eb8882SPatrick McHardy #endif 167327a884dcSArnaldo Carvalho de Melo skb->tail += off; 1674b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 16751da177e4SLinus Torvalds skb->cloned = 0; 1676334a8132SPatrick McHardy skb->hdr_len = 0; 16771da177e4SLinus Torvalds skb->nohdr = 0; 16781da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1679158f323bSEric Dumazet 1680de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1681de8f3a83SDaniel Borkmann 1682158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1683158f323bSEric Dumazet * For the moment, we really care of rx path, or 1684158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1685158f323bSEric Dumazet */ 1686158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1687158f323bSEric Dumazet skb->truesize += size - osize; 1688158f323bSEric Dumazet 16891da177e4SLinus Torvalds return 0; 16901da177e4SLinus Torvalds 1691a6686f2fSShirley Ma nofrags: 1692a6686f2fSShirley Ma kfree(data); 16931da177e4SLinus Torvalds nodata: 16941da177e4SLinus Torvalds return -ENOMEM; 16951da177e4SLinus Torvalds } 1696b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 17011da177e4SLinus Torvalds { 17021da177e4SLinus Torvalds struct sk_buff *skb2; 17031da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds if (delta <= 0) 17061da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 17071da177e4SLinus Torvalds else { 17081da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 17091da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 17101da177e4SLinus Torvalds GFP_ATOMIC)) { 17111da177e4SLinus Torvalds kfree_skb(skb2); 17121da177e4SLinus Torvalds skb2 = NULL; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds return skb2; 17161da177e4SLinus Torvalds } 1717b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /** 17201da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 17211da177e4SLinus Torvalds * @skb: buffer to copy 17221da177e4SLinus Torvalds * @newheadroom: new free bytes at head 17231da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 17241da177e4SLinus Torvalds * @gfp_mask: allocation priority 17251da177e4SLinus Torvalds * 17261da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 17271da177e4SLinus Torvalds * allocate additional space. 17281da177e4SLinus Torvalds * 17291da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 17301da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 17311da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 17321da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 17331da177e4SLinus Torvalds * 17341da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 17351da177e4SLinus Torvalds * is called from an interrupt. 17361da177e4SLinus Torvalds */ 17371da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 173886a76cafSVictor Fusco int newheadroom, int newtailroom, 1739dd0fc66fSAl Viro gfp_t gfp_mask) 17401da177e4SLinus Torvalds { 17411da177e4SLinus Torvalds /* 17421da177e4SLinus Torvalds * Allocate the copy buffer 17431da177e4SLinus Torvalds */ 1744c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1745c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1746c93bdd0eSMel Gorman NUMA_NO_NODE); 1747efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 17481da177e4SLinus Torvalds int head_copy_len, head_copy_off; 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds if (!n) 17511da177e4SLinus Torvalds return NULL; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds skb_reserve(n, newheadroom); 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds /* Set the tail pointer and length */ 17561da177e4SLinus Torvalds skb_put(n, skb->len); 17571da177e4SLinus Torvalds 1758efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 17591da177e4SLinus Torvalds head_copy_off = 0; 17601da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 17611da177e4SLinus Torvalds head_copy_len = newheadroom; 17621da177e4SLinus Torvalds else 17631da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds /* Copy the linear header and data. */ 17669f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 17679f77fad3STim Hansen skb->len + head_copy_len)); 17681da177e4SLinus Torvalds 176908303c18SIlya Lesokhin skb_copy_header(n, skb); 17701da177e4SLinus Torvalds 1771030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1772efd1e8d5SPatrick McHardy 17731da177e4SLinus Torvalds return n; 17741da177e4SLinus Torvalds } 1775b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds /** 1778cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 17791da177e4SLinus Torvalds * @skb: buffer to pad 17801da177e4SLinus Torvalds * @pad: space to pad 1781cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 17821da177e4SLinus Torvalds * 17831da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 17841da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 17851da177e4SLinus Torvalds * beyond the buffer end onto the wire. 17861da177e4SLinus Torvalds * 1787cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1788cd0a137aSFlorian Fainelli * if @free_on_error is true. 17891da177e4SLinus Torvalds */ 17901da177e4SLinus Torvalds 1791cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 17921da177e4SLinus Torvalds { 17935b057c6bSHerbert Xu int err; 17945b057c6bSHerbert Xu int ntail; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 17975b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 17981da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 17995b057c6bSHerbert Xu return 0; 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 18024305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 18035b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 18045b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 18055b057c6bSHerbert Xu if (unlikely(err)) 18065b057c6bSHerbert Xu goto free_skb; 18075b057c6bSHerbert Xu } 18085b057c6bSHerbert Xu 18095b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 18105b057c6bSHerbert Xu * to be audited. 18115b057c6bSHerbert Xu */ 18125b057c6bSHerbert Xu err = skb_linearize(skb); 18135b057c6bSHerbert Xu if (unlikely(err)) 18145b057c6bSHerbert Xu goto free_skb; 18155b057c6bSHerbert Xu 18165b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 18175b057c6bSHerbert Xu return 0; 18185b057c6bSHerbert Xu 18195b057c6bSHerbert Xu free_skb: 1820cd0a137aSFlorian Fainelli if (free_on_error) 18211da177e4SLinus Torvalds kfree_skb(skb); 18225b057c6bSHerbert Xu return err; 18231da177e4SLinus Torvalds } 1824cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 18251da177e4SLinus Torvalds 18260dde3e16SIlpo Järvinen /** 18270c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 18280c7ddf36SMathias Krause * @skb: start of the buffer to use 18290c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 18300c7ddf36SMathias Krause * @len: amount of data to add 18310c7ddf36SMathias Krause * 18320c7ddf36SMathias Krause * This function extends the used data area of the potentially 18330c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 18340c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 18350c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 18360c7ddf36SMathias Krause * returned. 18370c7ddf36SMathias Krause */ 18380c7ddf36SMathias Krause 18394df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 18400c7ddf36SMathias Krause { 18410c7ddf36SMathias Krause if (tail != skb) { 18420c7ddf36SMathias Krause skb->data_len += len; 18430c7ddf36SMathias Krause skb->len += len; 18440c7ddf36SMathias Krause } 18450c7ddf36SMathias Krause return skb_put(tail, len); 18460c7ddf36SMathias Krause } 18470c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 18480c7ddf36SMathias Krause 18490c7ddf36SMathias Krause /** 18500dde3e16SIlpo Järvinen * skb_put - add data to a buffer 18510dde3e16SIlpo Järvinen * @skb: buffer to use 18520dde3e16SIlpo Järvinen * @len: amount of data to add 18530dde3e16SIlpo Järvinen * 18540dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 18550dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 18560dde3e16SIlpo Järvinen * first byte of the extra data is returned. 18570dde3e16SIlpo Järvinen */ 18584df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 18590dde3e16SIlpo Järvinen { 18604df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 18610dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 18620dde3e16SIlpo Järvinen skb->tail += len; 18630dde3e16SIlpo Järvinen skb->len += len; 18640dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 18650dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 18660dde3e16SIlpo Järvinen return tmp; 18670dde3e16SIlpo Järvinen } 18680dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 18690dde3e16SIlpo Järvinen 18706be8ac2fSIlpo Järvinen /** 1871c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1872c2aa270aSIlpo Järvinen * @skb: buffer to use 1873c2aa270aSIlpo Järvinen * @len: amount of data to add 1874c2aa270aSIlpo Järvinen * 1875c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1876c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1877c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1878c2aa270aSIlpo Järvinen */ 1879d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1880c2aa270aSIlpo Järvinen { 1881c2aa270aSIlpo Järvinen skb->data -= len; 1882c2aa270aSIlpo Järvinen skb->len += len; 1883c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 1884c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1885c2aa270aSIlpo Järvinen return skb->data; 1886c2aa270aSIlpo Järvinen } 1887c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1888c2aa270aSIlpo Järvinen 1889c2aa270aSIlpo Järvinen /** 18906be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 18916be8ac2fSIlpo Järvinen * @skb: buffer to use 18926be8ac2fSIlpo Järvinen * @len: amount of data to remove 18936be8ac2fSIlpo Järvinen * 18946be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 18956be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 18966be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 18976be8ac2fSIlpo Järvinen * the old data. 18986be8ac2fSIlpo Järvinen */ 1899af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 19006be8ac2fSIlpo Järvinen { 190147d29646SDavid S. Miller return skb_pull_inline(skb, len); 19026be8ac2fSIlpo Järvinen } 19036be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 19046be8ac2fSIlpo Järvinen 1905419ae74eSIlpo Järvinen /** 1906419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1907419ae74eSIlpo Järvinen * @skb: buffer to alter 1908419ae74eSIlpo Järvinen * @len: new length 1909419ae74eSIlpo Järvinen * 1910419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1911419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1912419ae74eSIlpo Järvinen * The skb must be linear. 1913419ae74eSIlpo Järvinen */ 1914419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1915419ae74eSIlpo Järvinen { 1916419ae74eSIlpo Järvinen if (skb->len > len) 1917419ae74eSIlpo Järvinen __skb_trim(skb, len); 1918419ae74eSIlpo Järvinen } 1919419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1920419ae74eSIlpo Järvinen 19213cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 19221da177e4SLinus Torvalds */ 19231da177e4SLinus Torvalds 19243cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 19251da177e4SLinus Torvalds { 192627b437c8SHerbert Xu struct sk_buff **fragp; 192727b437c8SHerbert Xu struct sk_buff *frag; 19281da177e4SLinus Torvalds int offset = skb_headlen(skb); 19291da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 19301da177e4SLinus Torvalds int i; 193127b437c8SHerbert Xu int err; 193227b437c8SHerbert Xu 193327b437c8SHerbert Xu if (skb_cloned(skb) && 193427b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 193527b437c8SHerbert Xu return err; 19361da177e4SLinus Torvalds 1937f4d26fb3SHerbert Xu i = 0; 1938f4d26fb3SHerbert Xu if (offset >= len) 1939f4d26fb3SHerbert Xu goto drop_pages; 1940f4d26fb3SHerbert Xu 1941f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 19429e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 194327b437c8SHerbert Xu 194427b437c8SHerbert Xu if (end < len) { 19451da177e4SLinus Torvalds offset = end; 194627b437c8SHerbert Xu continue; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19499e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 195027b437c8SHerbert Xu 1951f4d26fb3SHerbert Xu drop_pages: 195227b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 195327b437c8SHerbert Xu 195427b437c8SHerbert Xu for (; i < nfrags; i++) 1955ea2ab693SIan Campbell skb_frag_unref(skb, i); 195627b437c8SHerbert Xu 195721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 195827b437c8SHerbert Xu skb_drop_fraglist(skb); 1959f4d26fb3SHerbert Xu goto done; 196027b437c8SHerbert Xu } 196127b437c8SHerbert Xu 196227b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 196327b437c8SHerbert Xu fragp = &frag->next) { 196427b437c8SHerbert Xu int end = offset + frag->len; 196527b437c8SHerbert Xu 196627b437c8SHerbert Xu if (skb_shared(frag)) { 196727b437c8SHerbert Xu struct sk_buff *nfrag; 196827b437c8SHerbert Xu 196927b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 197027b437c8SHerbert Xu if (unlikely(!nfrag)) 197127b437c8SHerbert Xu return -ENOMEM; 197227b437c8SHerbert Xu 197327b437c8SHerbert Xu nfrag->next = frag->next; 197485bb2a60SEric Dumazet consume_skb(frag); 197527b437c8SHerbert Xu frag = nfrag; 197627b437c8SHerbert Xu *fragp = frag; 197727b437c8SHerbert Xu } 197827b437c8SHerbert Xu 197927b437c8SHerbert Xu if (end < len) { 198027b437c8SHerbert Xu offset = end; 198127b437c8SHerbert Xu continue; 198227b437c8SHerbert Xu } 198327b437c8SHerbert Xu 198427b437c8SHerbert Xu if (end > len && 198527b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 198627b437c8SHerbert Xu return err; 198727b437c8SHerbert Xu 198827b437c8SHerbert Xu if (frag->next) 198927b437c8SHerbert Xu skb_drop_list(&frag->next); 199027b437c8SHerbert Xu break; 199127b437c8SHerbert Xu } 199227b437c8SHerbert Xu 1993f4d26fb3SHerbert Xu done: 199427b437c8SHerbert Xu if (len > skb_headlen(skb)) { 19951da177e4SLinus Torvalds skb->data_len -= skb->len - len; 19961da177e4SLinus Torvalds skb->len = len; 19971da177e4SLinus Torvalds } else { 19981da177e4SLinus Torvalds skb->len = len; 19991da177e4SLinus Torvalds skb->data_len = 0; 200027a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 2003c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2004c21b48ccSEric Dumazet skb_condense(skb); 20051da177e4SLinus Torvalds return 0; 20061da177e4SLinus Torvalds } 2007b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 20081da177e4SLinus Torvalds 200988078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 201088078d98SEric Dumazet */ 201188078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 201288078d98SEric Dumazet { 201388078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 201488078d98SEric Dumazet int delta = skb->len - len; 201588078d98SEric Dumazet 2016d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2017d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2018d55bef50SDimitris Michailidis len); 201988078d98SEric Dumazet } 202088078d98SEric Dumazet return __pskb_trim(skb, len); 202188078d98SEric Dumazet } 202288078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 202388078d98SEric Dumazet 20241da177e4SLinus Torvalds /** 20251da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 20261da177e4SLinus Torvalds * @skb: buffer to reallocate 20271da177e4SLinus Torvalds * @delta: number of bytes to advance tail 20281da177e4SLinus Torvalds * 20291da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 20301da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 20311da177e4SLinus Torvalds * data from fragmented part. 20321da177e4SLinus Torvalds * 20331da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 20341da177e4SLinus Torvalds * 20351da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 20361da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 20371da177e4SLinus Torvalds * 20381da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20391da177e4SLinus Torvalds * reloaded after call to this function. 20401da177e4SLinus Torvalds */ 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 20431da177e4SLinus Torvalds * when it is necessary. 20441da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 20451da177e4SLinus Torvalds * 2. It may change skb pointers. 20461da177e4SLinus Torvalds * 20471da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 20481da177e4SLinus Torvalds */ 2049af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 20501da177e4SLinus Torvalds { 20511da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 20521da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 20531da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 20541da177e4SLinus Torvalds */ 20554305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 20581da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 20591da177e4SLinus Torvalds GFP_ATOMIC)) 20601da177e4SLinus Torvalds return NULL; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20639f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 20649f77fad3STim Hansen skb_tail_pointer(skb), delta)); 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 20671da177e4SLinus Torvalds * size of pulled pages. Superb. 20681da177e4SLinus Torvalds */ 206921dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 20701da177e4SLinus Torvalds goto pull_pages; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 20731da177e4SLinus Torvalds eat = delta; 20741da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20759e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 20769e903e08SEric Dumazet 20779e903e08SEric Dumazet if (size >= eat) 20781da177e4SLinus Torvalds goto pull_pages; 20799e903e08SEric Dumazet eat -= size; 20801da177e4SLinus Torvalds } 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 208309001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 20841da177e4SLinus Torvalds * but taking into account that pulling is expected to 20851da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 20861da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 20871da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 20881da177e4SLinus Torvalds */ 20891da177e4SLinus Torvalds if (eat) { 20901da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 20911da177e4SLinus Torvalds struct sk_buff *clone = NULL; 20921da177e4SLinus Torvalds struct sk_buff *insp = NULL; 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds do { 20951da177e4SLinus Torvalds if (list->len <= eat) { 20961da177e4SLinus Torvalds /* Eaten as whole. */ 20971da177e4SLinus Torvalds eat -= list->len; 20981da177e4SLinus Torvalds list = list->next; 20991da177e4SLinus Torvalds insp = list; 21001da177e4SLinus Torvalds } else { 21011da177e4SLinus Torvalds /* Eaten partially. */ 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds if (skb_shared(list)) { 21041da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 21051da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 21061da177e4SLinus Torvalds if (!clone) 21071da177e4SLinus Torvalds return NULL; 21081da177e4SLinus Torvalds insp = list->next; 21091da177e4SLinus Torvalds list = clone; 21101da177e4SLinus Torvalds } else { 21111da177e4SLinus Torvalds /* This may be pulled without 21121da177e4SLinus Torvalds * problems. */ 21131da177e4SLinus Torvalds insp = list; 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 21161da177e4SLinus Torvalds kfree_skb(clone); 21171da177e4SLinus Torvalds return NULL; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds break; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds } while (eat); 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds /* Free pulled out fragments. */ 21241da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 21251da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 21261da177e4SLinus Torvalds kfree_skb(list); 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds /* And insert new clone at head. */ 21291da177e4SLinus Torvalds if (clone) { 21301da177e4SLinus Torvalds clone->next = list; 21311da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds pull_pages: 21371da177e4SLinus Torvalds eat = delta; 21381da177e4SLinus Torvalds k = 0; 21391da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21409e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 21419e903e08SEric Dumazet 21429e903e08SEric Dumazet if (size <= eat) { 2143ea2ab693SIan Campbell skb_frag_unref(skb, i); 21449e903e08SEric Dumazet eat -= size; 21451da177e4SLinus Torvalds } else { 2146b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2147b54c9d5bSJonathan Lemon 2148b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 21491da177e4SLinus Torvalds if (eat) { 2150b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2151b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 21523ccc6c6fSlinzhang if (!i) 21533ccc6c6fSlinzhang goto end; 21541da177e4SLinus Torvalds eat = 0; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds k++; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 21601da177e4SLinus Torvalds 21613ccc6c6fSlinzhang end: 21621da177e4SLinus Torvalds skb->tail += delta; 21631da177e4SLinus Torvalds skb->data_len -= delta; 21641da177e4SLinus Torvalds 21651f8b977aSWillem de Bruijn if (!skb->data_len) 21661f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 21671f8b977aSWillem de Bruijn 216827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 21691da177e4SLinus Torvalds } 2170b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 21711da177e4SLinus Torvalds 217222019b17SEric Dumazet /** 217322019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 217422019b17SEric Dumazet * @skb: source skb 217522019b17SEric Dumazet * @offset: offset in source 217622019b17SEric Dumazet * @to: destination buffer 217722019b17SEric Dumazet * @len: number of bytes to copy 217822019b17SEric Dumazet * 217922019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 218022019b17SEric Dumazet * destination buffer. 218122019b17SEric Dumazet * 218222019b17SEric Dumazet * CAUTION ! : 218322019b17SEric Dumazet * If its prototype is ever changed, 218422019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 218522019b17SEric Dumazet * since it is called from BPF assembly code. 218622019b17SEric Dumazet */ 21871da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 21881da177e4SLinus Torvalds { 21891a028e50SDavid S. Miller int start = skb_headlen(skb); 2190fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2191fbb398a8SDavid S. Miller int i, copy; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds if (offset > (int)skb->len - len) 21941da177e4SLinus Torvalds goto fault; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds /* Copy header. */ 21971a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 21981da177e4SLinus Torvalds if (copy > len) 21991da177e4SLinus Torvalds copy = len; 2200d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 22011da177e4SLinus Torvalds if ((len -= copy) == 0) 22021da177e4SLinus Torvalds return 0; 22031da177e4SLinus Torvalds offset += copy; 22041da177e4SLinus Torvalds to += copy; 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22081a028e50SDavid S. Miller int end; 220951c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 22101da177e4SLinus Torvalds 2211547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22121a028e50SDavid S. Miller 221351c56b00SEric Dumazet end = start + skb_frag_size(f); 22141da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2215c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2216c613c209SWillem de Bruijn struct page *p; 22171da177e4SLinus Torvalds u8 *vaddr; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds if (copy > len) 22201da177e4SLinus Torvalds copy = len; 22211da177e4SLinus Torvalds 2222c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2223b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2224c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2225c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2226c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 222751c56b00SEric Dumazet kunmap_atomic(vaddr); 2228c613c209SWillem de Bruijn } 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds if ((len -= copy) == 0) 22311da177e4SLinus Torvalds return 0; 22321da177e4SLinus Torvalds offset += copy; 22331da177e4SLinus Torvalds to += copy; 22341da177e4SLinus Torvalds } 22351a028e50SDavid S. Miller start = end; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds 2238fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 22391a028e50SDavid S. Miller int end; 22401da177e4SLinus Torvalds 2241547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22421a028e50SDavid S. Miller 2243fbb398a8SDavid S. Miller end = start + frag_iter->len; 22441da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22451da177e4SLinus Torvalds if (copy > len) 22461da177e4SLinus Torvalds copy = len; 2247fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 22481da177e4SLinus Torvalds goto fault; 22491da177e4SLinus Torvalds if ((len -= copy) == 0) 22501da177e4SLinus Torvalds return 0; 22511da177e4SLinus Torvalds offset += copy; 22521da177e4SLinus Torvalds to += copy; 22531da177e4SLinus Torvalds } 22541a028e50SDavid S. Miller start = end; 22551da177e4SLinus Torvalds } 2256a6686f2fSShirley Ma 22571da177e4SLinus Torvalds if (!len) 22581da177e4SLinus Torvalds return 0; 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds fault: 22611da177e4SLinus Torvalds return -EFAULT; 22621da177e4SLinus Torvalds } 2263b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 22641da177e4SLinus Torvalds 22659c55e01cSJens Axboe /* 22669c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 22679c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 22689c55e01cSJens Axboe */ 22699c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 22709c55e01cSJens Axboe { 22718b9d3728SJarek Poplawski put_page(spd->pages[i]); 22728b9d3728SJarek Poplawski } 22739c55e01cSJens Axboe 2274a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 22754fb66994SJarek Poplawski unsigned int *offset, 227618aafc62SEric Dumazet struct sock *sk) 22778b9d3728SJarek Poplawski { 22785640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 22798b9d3728SJarek Poplawski 22805640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 22818b9d3728SJarek Poplawski return NULL; 22824fb66994SJarek Poplawski 22835640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 22844fb66994SJarek Poplawski 22855640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 22865640f768SEric Dumazet page_address(page) + *offset, *len); 22875640f768SEric Dumazet *offset = pfrag->offset; 22885640f768SEric Dumazet pfrag->offset += *len; 22894fb66994SJarek Poplawski 22905640f768SEric Dumazet return pfrag->page; 22919c55e01cSJens Axboe } 22929c55e01cSJens Axboe 229341c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 229441c73a0dSEric Dumazet struct page *page, 229541c73a0dSEric Dumazet unsigned int offset) 229641c73a0dSEric Dumazet { 229741c73a0dSEric Dumazet return spd->nr_pages && 229841c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 229941c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 230041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 230141c73a0dSEric Dumazet } 230241c73a0dSEric Dumazet 23039c55e01cSJens Axboe /* 23049c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 23059c55e01cSJens Axboe */ 2306a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 230735f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 23084fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 230918aafc62SEric Dumazet bool linear, 23107a67e56fSJarek Poplawski struct sock *sk) 23119c55e01cSJens Axboe { 231241c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2313a108d5f3SDavid S. Miller return true; 23149c55e01cSJens Axboe 23158b9d3728SJarek Poplawski if (linear) { 231618aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 23178b9d3728SJarek Poplawski if (!page) 2318a108d5f3SDavid S. Miller return true; 231941c73a0dSEric Dumazet } 232041c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 232141c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2322a108d5f3SDavid S. Miller return false; 232341c73a0dSEric Dumazet } 23248b9d3728SJarek Poplawski get_page(page); 23259c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 23264fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 23279c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 23289c55e01cSJens Axboe spd->nr_pages++; 23298b9d3728SJarek Poplawski 2330a108d5f3SDavid S. Miller return false; 23319c55e01cSJens Axboe } 23329c55e01cSJens Axboe 2333a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 23342870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 233518aafc62SEric Dumazet unsigned int *len, 2336d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 233735f3d14dSJens Axboe struct sock *sk, 233835f3d14dSJens Axboe struct pipe_inode_info *pipe) 23399c55e01cSJens Axboe { 23402870c43dSOctavian Purdila if (!*len) 2341a108d5f3SDavid S. Miller return true; 23429c55e01cSJens Axboe 23432870c43dSOctavian Purdila /* skip this segment if already processed */ 23442870c43dSOctavian Purdila if (*off >= plen) { 23452870c43dSOctavian Purdila *off -= plen; 2346a108d5f3SDavid S. Miller return false; 23472870c43dSOctavian Purdila } 23482870c43dSOctavian Purdila 23492870c43dSOctavian Purdila /* ignore any bits we already processed */ 23509ca1b22dSEric Dumazet poff += *off; 23519ca1b22dSEric Dumazet plen -= *off; 23522870c43dSOctavian Purdila *off = 0; 23532870c43dSOctavian Purdila 235418aafc62SEric Dumazet do { 235518aafc62SEric Dumazet unsigned int flen = min(*len, plen); 23562870c43dSOctavian Purdila 235718aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 235818aafc62SEric Dumazet linear, sk)) 2359a108d5f3SDavid S. Miller return true; 236018aafc62SEric Dumazet poff += flen; 236118aafc62SEric Dumazet plen -= flen; 23622870c43dSOctavian Purdila *len -= flen; 236318aafc62SEric Dumazet } while (*len && plen); 23642870c43dSOctavian Purdila 2365a108d5f3SDavid S. Miller return false; 2366db43a282SOctavian Purdila } 23679c55e01cSJens Axboe 23689c55e01cSJens Axboe /* 2369a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 23702870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 23719c55e01cSJens Axboe */ 2372a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 237335f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 237435f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 23752870c43dSOctavian Purdila { 23762870c43dSOctavian Purdila int seg; 2377fa9835e5STom Herbert struct sk_buff *iter; 23789c55e01cSJens Axboe 23791d0c0b32SEric Dumazet /* map the linear part : 23802996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 23812996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 23822996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 23839c55e01cSJens Axboe */ 23842870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 23852870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 23862870c43dSOctavian Purdila skb_headlen(skb), 238718aafc62SEric Dumazet offset, len, spd, 23883a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 23891d0c0b32SEric Dumazet sk, pipe)) 2390a108d5f3SDavid S. Miller return true; 23919c55e01cSJens Axboe 23929c55e01cSJens Axboe /* 23939c55e01cSJens Axboe * then map the fragments 23949c55e01cSJens Axboe */ 23959c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 23969c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 23979c55e01cSJens Axboe 2398ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2399b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 240018aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2401a108d5f3SDavid S. Miller return true; 24029c55e01cSJens Axboe } 24039c55e01cSJens Axboe 2404fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2405fa9835e5STom Herbert if (*offset >= iter->len) { 2406fa9835e5STom Herbert *offset -= iter->len; 2407fa9835e5STom Herbert continue; 2408fa9835e5STom Herbert } 2409fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2410fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2411fa9835e5STom Herbert * case. 2412fa9835e5STom Herbert */ 2413fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2414fa9835e5STom Herbert return true; 2415fa9835e5STom Herbert } 2416fa9835e5STom Herbert 2417a108d5f3SDavid S. Miller return false; 24189c55e01cSJens Axboe } 24199c55e01cSJens Axboe 24209c55e01cSJens Axboe /* 24219c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2422fa9835e5STom Herbert * the fragments, and the frag list. 24239c55e01cSJens Axboe */ 2424a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 24259c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 242625869262SAl Viro unsigned int flags) 24279c55e01cSJens Axboe { 242841c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 242941c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 24309c55e01cSJens Axboe struct splice_pipe_desc spd = { 24319c55e01cSJens Axboe .pages = pages, 24329c55e01cSJens Axboe .partial = partial, 2433047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 243428a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 24359c55e01cSJens Axboe .spd_release = sock_spd_release, 24369c55e01cSJens Axboe }; 243735f3d14dSJens Axboe int ret = 0; 243835f3d14dSJens Axboe 2439fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 24409c55e01cSJens Axboe 2441a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 244225869262SAl Viro ret = splice_to_pipe(pipe, &spd); 24439c55e01cSJens Axboe 244435f3d14dSJens Axboe return ret; 24459c55e01cSJens Axboe } 24462b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 24479c55e01cSJens Axboe 244820bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */ 244920bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 245020bf50deSTom Herbert int len) 245120bf50deSTom Herbert { 245220bf50deSTom Herbert unsigned int orig_len = len; 245320bf50deSTom Herbert struct sk_buff *head = skb; 245420bf50deSTom Herbert unsigned short fragidx; 245520bf50deSTom Herbert int slen, ret; 245620bf50deSTom Herbert 245720bf50deSTom Herbert do_frag_list: 245820bf50deSTom Herbert 245920bf50deSTom Herbert /* Deal with head data */ 246020bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 246120bf50deSTom Herbert struct kvec kv; 246220bf50deSTom Herbert struct msghdr msg; 246320bf50deSTom Herbert 246420bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 246520bf50deSTom Herbert kv.iov_base = skb->data + offset; 2466db5980d8SJohn Fastabend kv.iov_len = slen; 246720bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2468bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 246920bf50deSTom Herbert 247020bf50deSTom Herbert ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 247120bf50deSTom Herbert if (ret <= 0) 247220bf50deSTom Herbert goto error; 247320bf50deSTom Herbert 247420bf50deSTom Herbert offset += ret; 247520bf50deSTom Herbert len -= ret; 247620bf50deSTom Herbert } 247720bf50deSTom Herbert 247820bf50deSTom Herbert /* All the data was skb head? */ 247920bf50deSTom Herbert if (!len) 248020bf50deSTom Herbert goto out; 248120bf50deSTom Herbert 248220bf50deSTom Herbert /* Make offset relative to start of frags */ 248320bf50deSTom Herbert offset -= skb_headlen(skb); 248420bf50deSTom Herbert 248520bf50deSTom Herbert /* Find where we are in frag list */ 248620bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 248720bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 248820bf50deSTom Herbert 2489d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 249020bf50deSTom Herbert break; 249120bf50deSTom Herbert 2492d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 249320bf50deSTom Herbert } 249420bf50deSTom Herbert 249520bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 249620bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 249720bf50deSTom Herbert 2498d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 249920bf50deSTom Herbert 250020bf50deSTom Herbert while (slen) { 2501d8e18a51SMatthew Wilcox (Oracle) ret = kernel_sendpage_locked(sk, skb_frag_page(frag), 2502b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 250320bf50deSTom Herbert slen, MSG_DONTWAIT); 250420bf50deSTom Herbert if (ret <= 0) 250520bf50deSTom Herbert goto error; 250620bf50deSTom Herbert 250720bf50deSTom Herbert len -= ret; 250820bf50deSTom Herbert offset += ret; 250920bf50deSTom Herbert slen -= ret; 251020bf50deSTom Herbert } 251120bf50deSTom Herbert 251220bf50deSTom Herbert offset = 0; 251320bf50deSTom Herbert } 251420bf50deSTom Herbert 251520bf50deSTom Herbert if (len) { 251620bf50deSTom Herbert /* Process any frag lists */ 251720bf50deSTom Herbert 251820bf50deSTom Herbert if (skb == head) { 251920bf50deSTom Herbert if (skb_has_frag_list(skb)) { 252020bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 252120bf50deSTom Herbert goto do_frag_list; 252220bf50deSTom Herbert } 252320bf50deSTom Herbert } else if (skb->next) { 252420bf50deSTom Herbert skb = skb->next; 252520bf50deSTom Herbert goto do_frag_list; 252620bf50deSTom Herbert } 252720bf50deSTom Herbert } 252820bf50deSTom Herbert 252920bf50deSTom Herbert out: 253020bf50deSTom Herbert return orig_len - len; 253120bf50deSTom Herbert 253220bf50deSTom Herbert error: 253320bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 253420bf50deSTom Herbert } 253520bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 253620bf50deSTom Herbert 2537357b40a1SHerbert Xu /** 2538357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2539357b40a1SHerbert Xu * @skb: destination buffer 2540357b40a1SHerbert Xu * @offset: offset in destination 2541357b40a1SHerbert Xu * @from: source buffer 2542357b40a1SHerbert Xu * @len: number of bytes to copy 2543357b40a1SHerbert Xu * 2544357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2545357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2546357b40a1SHerbert Xu * traversing fragment lists and such. 2547357b40a1SHerbert Xu */ 2548357b40a1SHerbert Xu 25490c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2550357b40a1SHerbert Xu { 25511a028e50SDavid S. Miller int start = skb_headlen(skb); 2552fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2553fbb398a8SDavid S. Miller int i, copy; 2554357b40a1SHerbert Xu 2555357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2556357b40a1SHerbert Xu goto fault; 2557357b40a1SHerbert Xu 25581a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2559357b40a1SHerbert Xu if (copy > len) 2560357b40a1SHerbert Xu copy = len; 256127d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2562357b40a1SHerbert Xu if ((len -= copy) == 0) 2563357b40a1SHerbert Xu return 0; 2564357b40a1SHerbert Xu offset += copy; 2565357b40a1SHerbert Xu from += copy; 2566357b40a1SHerbert Xu } 2567357b40a1SHerbert Xu 2568357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2569357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 25701a028e50SDavid S. Miller int end; 2571357b40a1SHerbert Xu 2572547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25731a028e50SDavid S. Miller 25749e903e08SEric Dumazet end = start + skb_frag_size(frag); 2575357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2576c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2577c613c209SWillem de Bruijn struct page *p; 2578357b40a1SHerbert Xu u8 *vaddr; 2579357b40a1SHerbert Xu 2580357b40a1SHerbert Xu if (copy > len) 2581357b40a1SHerbert Xu copy = len; 2582357b40a1SHerbert Xu 2583c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2584b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2585c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2586c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2587c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 258851c56b00SEric Dumazet kunmap_atomic(vaddr); 2589c613c209SWillem de Bruijn } 2590357b40a1SHerbert Xu 2591357b40a1SHerbert Xu if ((len -= copy) == 0) 2592357b40a1SHerbert Xu return 0; 2593357b40a1SHerbert Xu offset += copy; 2594357b40a1SHerbert Xu from += copy; 2595357b40a1SHerbert Xu } 25961a028e50SDavid S. Miller start = end; 2597357b40a1SHerbert Xu } 2598357b40a1SHerbert Xu 2599fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26001a028e50SDavid S. Miller int end; 2601357b40a1SHerbert Xu 2602547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26031a028e50SDavid S. Miller 2604fbb398a8SDavid S. Miller end = start + frag_iter->len; 2605357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2606357b40a1SHerbert Xu if (copy > len) 2607357b40a1SHerbert Xu copy = len; 2608fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 26091a028e50SDavid S. Miller from, copy)) 2610357b40a1SHerbert Xu goto fault; 2611357b40a1SHerbert Xu if ((len -= copy) == 0) 2612357b40a1SHerbert Xu return 0; 2613357b40a1SHerbert Xu offset += copy; 2614357b40a1SHerbert Xu from += copy; 2615357b40a1SHerbert Xu } 26161a028e50SDavid S. Miller start = end; 2617357b40a1SHerbert Xu } 2618357b40a1SHerbert Xu if (!len) 2619357b40a1SHerbert Xu return 0; 2620357b40a1SHerbert Xu 2621357b40a1SHerbert Xu fault: 2622357b40a1SHerbert Xu return -EFAULT; 2623357b40a1SHerbert Xu } 2624357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2625357b40a1SHerbert Xu 26261da177e4SLinus Torvalds /* Checksum skb data. */ 26272817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 26282817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 26291da177e4SLinus Torvalds { 26301a028e50SDavid S. Miller int start = skb_headlen(skb); 26311a028e50SDavid S. Miller int i, copy = start - offset; 2632fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 26331da177e4SLinus Torvalds int pos = 0; 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds /* Checksum header. */ 26361da177e4SLinus Torvalds if (copy > 0) { 26371da177e4SLinus Torvalds if (copy > len) 26381da177e4SLinus Torvalds copy = len; 26392544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 26402544af03SMatteo Croce skb->data + offset, copy, csum); 26411da177e4SLinus Torvalds if ((len -= copy) == 0) 26421da177e4SLinus Torvalds return csum; 26431da177e4SLinus Torvalds offset += copy; 26441da177e4SLinus Torvalds pos = copy; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26481a028e50SDavid S. Miller int end; 264951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 26501da177e4SLinus Torvalds 2651547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26521a028e50SDavid S. Miller 265351c56b00SEric Dumazet end = start + skb_frag_size(frag); 26541da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2655c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2656c613c209SWillem de Bruijn struct page *p; 265744bb9363SAl Viro __wsum csum2; 26581da177e4SLinus Torvalds u8 *vaddr; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds if (copy > len) 26611da177e4SLinus Torvalds copy = len; 2662c613c209SWillem de Bruijn 2663c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2664b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2665c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2666c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 26672544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 26682544af03SMatteo Croce csum_partial_ext, 26692544af03SMatteo Croce vaddr + p_off, p_len, 0); 267051c56b00SEric Dumazet kunmap_atomic(vaddr); 26712544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 26722544af03SMatteo Croce csum_block_add_ext, csum, 26732544af03SMatteo Croce csum2, pos, p_len); 2674c613c209SWillem de Bruijn pos += p_len; 2675c613c209SWillem de Bruijn } 2676c613c209SWillem de Bruijn 26771da177e4SLinus Torvalds if (!(len -= copy)) 26781da177e4SLinus Torvalds return csum; 26791da177e4SLinus Torvalds offset += copy; 26801da177e4SLinus Torvalds } 26811a028e50SDavid S. Miller start = end; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds 2684fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26851a028e50SDavid S. Miller int end; 26861da177e4SLinus Torvalds 2687547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26881a028e50SDavid S. Miller 2689fbb398a8SDavid S. Miller end = start + frag_iter->len; 26901da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26915f92a738SAl Viro __wsum csum2; 26921da177e4SLinus Torvalds if (copy > len) 26931da177e4SLinus Torvalds copy = len; 26942817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 26952817a336SDaniel Borkmann copy, 0, ops); 26962544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 26972544af03SMatteo Croce csum, csum2, pos, copy); 26981da177e4SLinus Torvalds if ((len -= copy) == 0) 26991da177e4SLinus Torvalds return csum; 27001da177e4SLinus Torvalds offset += copy; 27011da177e4SLinus Torvalds pos += copy; 27021da177e4SLinus Torvalds } 27031a028e50SDavid S. Miller start = end; 27041da177e4SLinus Torvalds } 270509a62660SKris Katterjohn BUG_ON(len); 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds return csum; 27081da177e4SLinus Torvalds } 27092817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 27102817a336SDaniel Borkmann 27112817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 27122817a336SDaniel Borkmann int len, __wsum csum) 27132817a336SDaniel Borkmann { 27142817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2715cea80ea8SDaniel Borkmann .update = csum_partial_ext, 27162817a336SDaniel Borkmann .combine = csum_block_add_ext, 27172817a336SDaniel Borkmann }; 27182817a336SDaniel Borkmann 27192817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 27202817a336SDaniel Borkmann } 2721b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds /* Both of above in one bottle. */ 27241da177e4SLinus Torvalds 272581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 272681d77662SAl Viro u8 *to, int len, __wsum csum) 27271da177e4SLinus Torvalds { 27281a028e50SDavid S. Miller int start = skb_headlen(skb); 27291a028e50SDavid S. Miller int i, copy = start - offset; 2730fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 27311da177e4SLinus Torvalds int pos = 0; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds /* Copy header. */ 27341da177e4SLinus Torvalds if (copy > 0) { 27351da177e4SLinus Torvalds if (copy > len) 27361da177e4SLinus Torvalds copy = len; 27371da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 27381da177e4SLinus Torvalds copy, csum); 27391da177e4SLinus Torvalds if ((len -= copy) == 0) 27401da177e4SLinus Torvalds return csum; 27411da177e4SLinus Torvalds offset += copy; 27421da177e4SLinus Torvalds to += copy; 27431da177e4SLinus Torvalds pos = copy; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27471a028e50SDavid S. Miller int end; 27481da177e4SLinus Torvalds 2749547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27501a028e50SDavid S. Miller 27519e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 27521da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2753c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2754c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2755c613c209SWillem de Bruijn struct page *p; 27565084205fSAl Viro __wsum csum2; 27571da177e4SLinus Torvalds u8 *vaddr; 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds if (copy > len) 27601da177e4SLinus Torvalds copy = len; 2761c613c209SWillem de Bruijn 2762c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2763b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2764c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2765c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2766c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2767c613c209SWillem de Bruijn to + copied, 2768c613c209SWillem de Bruijn p_len, 0); 276951c56b00SEric Dumazet kunmap_atomic(vaddr); 27701da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2771c613c209SWillem de Bruijn pos += p_len; 2772c613c209SWillem de Bruijn } 2773c613c209SWillem de Bruijn 27741da177e4SLinus Torvalds if (!(len -= copy)) 27751da177e4SLinus Torvalds return csum; 27761da177e4SLinus Torvalds offset += copy; 27771da177e4SLinus Torvalds to += copy; 27781da177e4SLinus Torvalds } 27791a028e50SDavid S. Miller start = end; 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 2782fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 278381d77662SAl Viro __wsum csum2; 27841a028e50SDavid S. Miller int end; 27851da177e4SLinus Torvalds 2786547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27871a028e50SDavid S. Miller 2788fbb398a8SDavid S. Miller end = start + frag_iter->len; 27891da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 27901da177e4SLinus Torvalds if (copy > len) 27911da177e4SLinus Torvalds copy = len; 2792fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 27931a028e50SDavid S. Miller offset - start, 27941da177e4SLinus Torvalds to, copy, 0); 27951da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 27961da177e4SLinus Torvalds if ((len -= copy) == 0) 27971da177e4SLinus Torvalds return csum; 27981da177e4SLinus Torvalds offset += copy; 27991da177e4SLinus Torvalds to += copy; 28001da177e4SLinus Torvalds pos += copy; 28011da177e4SLinus Torvalds } 28021a028e50SDavid S. Miller start = end; 28031da177e4SLinus Torvalds } 280409a62660SKris Katterjohn BUG_ON(len); 28051da177e4SLinus Torvalds return csum; 28061da177e4SLinus Torvalds } 2807b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 28081da177e4SLinus Torvalds 280949f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 281049f8e832SCong Wang { 281149f8e832SCong Wang __sum16 sum; 281249f8e832SCong Wang 281349f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 281414641931SCong Wang /* See comments in __skb_checksum_complete(). */ 281549f8e832SCong Wang if (likely(!sum)) { 281649f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 281749f8e832SCong Wang !skb->csum_complete_sw) 28187fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 281949f8e832SCong Wang } 282049f8e832SCong Wang if (!skb_shared(skb)) 282149f8e832SCong Wang skb->csum_valid = !sum; 282249f8e832SCong Wang return sum; 282349f8e832SCong Wang } 282449f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 282549f8e832SCong Wang 282614641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 282714641931SCong Wang * which has been changed from the hardware checksum, for example, by 282814641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 282914641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 283014641931SCong Wang * 283114641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 283214641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 283314641931SCong Wang * shared. 283414641931SCong Wang */ 283549f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 283649f8e832SCong Wang { 283749f8e832SCong Wang __wsum csum; 283849f8e832SCong Wang __sum16 sum; 283949f8e832SCong Wang 284049f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 284149f8e832SCong Wang 284249f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 284314641931SCong Wang /* This check is inverted, because we already knew the hardware 284414641931SCong Wang * checksum is invalid before calling this function. So, if the 284514641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 284614641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 284714641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 284814641931SCong Wang * when moving skb->data around. 284914641931SCong Wang */ 285049f8e832SCong Wang if (likely(!sum)) { 285149f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 285249f8e832SCong Wang !skb->csum_complete_sw) 28537fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 285449f8e832SCong Wang } 285549f8e832SCong Wang 285649f8e832SCong Wang if (!skb_shared(skb)) { 285749f8e832SCong Wang /* Save full packet checksum */ 285849f8e832SCong Wang skb->csum = csum; 285949f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 286049f8e832SCong Wang skb->csum_complete_sw = 1; 286149f8e832SCong Wang skb->csum_valid = !sum; 286249f8e832SCong Wang } 286349f8e832SCong Wang 286449f8e832SCong Wang return sum; 286549f8e832SCong Wang } 286649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 286749f8e832SCong Wang 28689617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 28699617813dSDavide Caratti { 28709617813dSDavide Caratti net_warn_ratelimited( 28719617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 28729617813dSDavide Caratti __func__); 28739617813dSDavide Caratti return 0; 28749617813dSDavide Caratti } 28759617813dSDavide Caratti 28769617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 28779617813dSDavide Caratti int offset, int len) 28789617813dSDavide Caratti { 28799617813dSDavide Caratti net_warn_ratelimited( 28809617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 28819617813dSDavide Caratti __func__); 28829617813dSDavide Caratti return 0; 28839617813dSDavide Caratti } 28849617813dSDavide Caratti 28859617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 28869617813dSDavide Caratti .update = warn_crc32c_csum_update, 28879617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 28889617813dSDavide Caratti }; 28899617813dSDavide Caratti 28909617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 28919617813dSDavide Caratti &default_crc32c_ops; 28929617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 28939617813dSDavide Caratti 2894af2806f8SThomas Graf /** 2895af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2896af2806f8SThomas Graf * @from: source buffer 2897af2806f8SThomas Graf * 2898af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2899af2806f8SThomas Graf * into skb_zerocopy(). 2900af2806f8SThomas Graf */ 2901af2806f8SThomas Graf unsigned int 2902af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2903af2806f8SThomas Graf { 2904af2806f8SThomas Graf unsigned int hlen = 0; 2905af2806f8SThomas Graf 2906af2806f8SThomas Graf if (!from->head_frag || 2907af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2908af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2909af2806f8SThomas Graf hlen = skb_headlen(from); 2910af2806f8SThomas Graf 2911af2806f8SThomas Graf if (skb_has_frag_list(from)) 2912af2806f8SThomas Graf hlen = from->len; 2913af2806f8SThomas Graf 2914af2806f8SThomas Graf return hlen; 2915af2806f8SThomas Graf } 2916af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2917af2806f8SThomas Graf 2918af2806f8SThomas Graf /** 2919af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2920af2806f8SThomas Graf * @to: destination buffer 29217fceb4deSMasanari Iida * @from: source buffer 2922af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2923af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2924af2806f8SThomas Graf * 2925af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2926af2806f8SThomas Graf * to the frags in the source buffer. 2927af2806f8SThomas Graf * 2928af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2929af2806f8SThomas Graf * headroom in the `to` buffer. 293036d5fe6aSZoltan Kiss * 293136d5fe6aSZoltan Kiss * Return value: 293236d5fe6aSZoltan Kiss * 0: everything is OK 293336d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 293436d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2935af2806f8SThomas Graf */ 293636d5fe6aSZoltan Kiss int 293736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2938af2806f8SThomas Graf { 2939af2806f8SThomas Graf int i, j = 0; 2940af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 294136d5fe6aSZoltan Kiss int ret; 2942af2806f8SThomas Graf struct page *page; 2943af2806f8SThomas Graf unsigned int offset; 2944af2806f8SThomas Graf 2945af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2946af2806f8SThomas Graf 2947af2806f8SThomas Graf /* dont bother with small payloads */ 294836d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 294936d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2950af2806f8SThomas Graf 2951af2806f8SThomas Graf if (hlen) { 295236d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 295336d5fe6aSZoltan Kiss if (unlikely(ret)) 295436d5fe6aSZoltan Kiss return ret; 2955af2806f8SThomas Graf len -= hlen; 2956af2806f8SThomas Graf } else { 2957af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2958af2806f8SThomas Graf if (plen) { 2959af2806f8SThomas Graf page = virt_to_head_page(from->head); 2960af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2961af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2962af2806f8SThomas Graf get_page(page); 2963af2806f8SThomas Graf j = 1; 2964af2806f8SThomas Graf len -= plen; 2965af2806f8SThomas Graf } 2966af2806f8SThomas Graf } 2967af2806f8SThomas Graf 2968af2806f8SThomas Graf to->truesize += len + plen; 2969af2806f8SThomas Graf to->len += len + plen; 2970af2806f8SThomas Graf to->data_len += len + plen; 2971af2806f8SThomas Graf 297236d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 297336d5fe6aSZoltan Kiss skb_tx_error(from); 297436d5fe6aSZoltan Kiss return -ENOMEM; 297536d5fe6aSZoltan Kiss } 29761f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 297736d5fe6aSZoltan Kiss 2978af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2979d8e18a51SMatthew Wilcox (Oracle) int size; 2980d8e18a51SMatthew Wilcox (Oracle) 2981af2806f8SThomas Graf if (!len) 2982af2806f8SThomas Graf break; 2983af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2984d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 2985d8e18a51SMatthew Wilcox (Oracle) len); 2986d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 2987d8e18a51SMatthew Wilcox (Oracle) len -= size; 2988af2806f8SThomas Graf skb_frag_ref(to, j); 2989af2806f8SThomas Graf j++; 2990af2806f8SThomas Graf } 2991af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 299236d5fe6aSZoltan Kiss 299336d5fe6aSZoltan Kiss return 0; 2994af2806f8SThomas Graf } 2995af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2996af2806f8SThomas Graf 29971da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 29981da177e4SLinus Torvalds { 2999d3bc23e7SAl Viro __wsum csum; 30001da177e4SLinus Torvalds long csstart; 30011da177e4SLinus Torvalds 300284fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 300355508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 30041da177e4SLinus Torvalds else 30051da177e4SLinus Torvalds csstart = skb_headlen(skb); 30061da177e4SLinus Torvalds 300709a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 30081da177e4SLinus Torvalds 3009d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds csum = 0; 30121da177e4SLinus Torvalds if (csstart != skb->len) 30131da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 30141da177e4SLinus Torvalds skb->len - csstart, 0); 30151da177e4SLinus Torvalds 301684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3017ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 30181da177e4SLinus Torvalds 3019d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds } 3022b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds /** 30251da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 30261da177e4SLinus Torvalds * @list: list to dequeue from 30271da177e4SLinus Torvalds * 30281da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 30291da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 30301da177e4SLinus Torvalds * returned or %NULL if the list is empty. 30311da177e4SLinus Torvalds */ 30321da177e4SLinus Torvalds 30331da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 30341da177e4SLinus Torvalds { 30351da177e4SLinus Torvalds unsigned long flags; 30361da177e4SLinus Torvalds struct sk_buff *result; 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 30391da177e4SLinus Torvalds result = __skb_dequeue(list); 30401da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 30411da177e4SLinus Torvalds return result; 30421da177e4SLinus Torvalds } 3043b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds /** 30461da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 30471da177e4SLinus Torvalds * @list: list to dequeue from 30481da177e4SLinus Torvalds * 30491da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 30501da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 30511da177e4SLinus Torvalds * returned or %NULL if the list is empty. 30521da177e4SLinus Torvalds */ 30531da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 30541da177e4SLinus Torvalds { 30551da177e4SLinus Torvalds unsigned long flags; 30561da177e4SLinus Torvalds struct sk_buff *result; 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 30591da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 30601da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 30611da177e4SLinus Torvalds return result; 30621da177e4SLinus Torvalds } 3063b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds /** 30661da177e4SLinus Torvalds * skb_queue_purge - empty a list 30671da177e4SLinus Torvalds * @list: list to empty 30681da177e4SLinus Torvalds * 30691da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 30701da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 30711da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 30721da177e4SLinus Torvalds */ 30731da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 30741da177e4SLinus Torvalds { 30751da177e4SLinus Torvalds struct sk_buff *skb; 30761da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 30771da177e4SLinus Torvalds kfree_skb(skb); 30781da177e4SLinus Torvalds } 3079b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds /** 30829f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 30839f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3084385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 30859f5afeaeSYaogong Wang * 30869f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 30879f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 30889f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 30899f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 30909f5afeaeSYaogong Wang */ 3091385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 30929f5afeaeSYaogong Wang { 30937c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3094385114deSPeter Oskolkov unsigned int sum = 0; 30959f5afeaeSYaogong Wang 30967c90584cSEric Dumazet while (p) { 30977c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 30987c90584cSEric Dumazet 30997c90584cSEric Dumazet p = rb_next(p); 31007c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3101385114deSPeter Oskolkov sum += skb->truesize; 31029f5afeaeSYaogong Wang kfree_skb(skb); 31037c90584cSEric Dumazet } 3104385114deSPeter Oskolkov return sum; 31059f5afeaeSYaogong Wang } 31069f5afeaeSYaogong Wang 31079f5afeaeSYaogong Wang /** 31081da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 31091da177e4SLinus Torvalds * @list: list to use 31101da177e4SLinus Torvalds * @newsk: buffer to queue 31111da177e4SLinus Torvalds * 31121da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 31131da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 31141da177e4SLinus Torvalds * safely. 31151da177e4SLinus Torvalds * 31161da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31171da177e4SLinus Torvalds */ 31181da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 31191da177e4SLinus Torvalds { 31201da177e4SLinus Torvalds unsigned long flags; 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31231da177e4SLinus Torvalds __skb_queue_head(list, newsk); 31241da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31251da177e4SLinus Torvalds } 3126b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds /** 31291da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 31301da177e4SLinus Torvalds * @list: list to use 31311da177e4SLinus Torvalds * @newsk: buffer to queue 31321da177e4SLinus Torvalds * 31331da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 31341da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 31351da177e4SLinus Torvalds * safely. 31361da177e4SLinus Torvalds * 31371da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31381da177e4SLinus Torvalds */ 31391da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 31401da177e4SLinus Torvalds { 31411da177e4SLinus Torvalds unsigned long flags; 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31441da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 31451da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31461da177e4SLinus Torvalds } 3147b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 31488728b834SDavid S. Miller 31491da177e4SLinus Torvalds /** 31501da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 31511da177e4SLinus Torvalds * @skb: buffer to remove 31528728b834SDavid S. Miller * @list: list to use 31531da177e4SLinus Torvalds * 31548728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 31558728b834SDavid S. Miller * function is atomic with respect to other list locked calls 31561da177e4SLinus Torvalds * 31578728b834SDavid S. Miller * You must know what list the SKB is on. 31581da177e4SLinus Torvalds */ 31598728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 31601da177e4SLinus Torvalds { 31611da177e4SLinus Torvalds unsigned long flags; 31621da177e4SLinus Torvalds 31631da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31648728b834SDavid S. Miller __skb_unlink(skb, list); 31651da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31661da177e4SLinus Torvalds } 3167b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds /** 31701da177e4SLinus Torvalds * skb_append - append a buffer 31711da177e4SLinus Torvalds * @old: buffer to insert after 31721da177e4SLinus Torvalds * @newsk: buffer to insert 31738728b834SDavid S. Miller * @list: list to use 31741da177e4SLinus Torvalds * 31751da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 31761da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 31771da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31781da177e4SLinus Torvalds */ 31798728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 31801da177e4SLinus Torvalds { 31811da177e4SLinus Torvalds unsigned long flags; 31821da177e4SLinus Torvalds 31838728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 31847de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 31858728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 31861da177e4SLinus Torvalds } 3187b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 31881da177e4SLinus Torvalds 31891da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 31901da177e4SLinus Torvalds struct sk_buff* skb1, 31911da177e4SLinus Torvalds const u32 len, const int pos) 31921da177e4SLinus Torvalds { 31931da177e4SLinus Torvalds int i; 31941da177e4SLinus Torvalds 3195d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3196d626f62bSArnaldo Carvalho de Melo pos - len); 31971da177e4SLinus Torvalds /* And move data appendix as is. */ 31981da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 31991da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 32021da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 32031da177e4SLinus Torvalds skb1->data_len = skb->data_len; 32041da177e4SLinus Torvalds skb1->len += skb1->data_len; 32051da177e4SLinus Torvalds skb->data_len = 0; 32061da177e4SLinus Torvalds skb->len = len; 320727a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 32081da177e4SLinus Torvalds } 32091da177e4SLinus Torvalds 32101da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 32111da177e4SLinus Torvalds struct sk_buff* skb1, 32121da177e4SLinus Torvalds const u32 len, int pos) 32131da177e4SLinus Torvalds { 32141da177e4SLinus Torvalds int i, k = 0; 32151da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 32181da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 32191da177e4SLinus Torvalds skb->len = len; 32201da177e4SLinus Torvalds skb->data_len = len - pos; 32211da177e4SLinus Torvalds 32221da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 32239e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds if (pos + size > len) { 32261da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 32271da177e4SLinus Torvalds 32281da177e4SLinus Torvalds if (pos < len) { 32291da177e4SLinus Torvalds /* Split frag. 32301da177e4SLinus Torvalds * We have two variants in this case: 32311da177e4SLinus Torvalds * 1. Move all the frag to the second 32321da177e4SLinus Torvalds * part, if it is possible. F.e. 32331da177e4SLinus Torvalds * this approach is mandatory for TUX, 32341da177e4SLinus Torvalds * where splitting is expensive. 32351da177e4SLinus Torvalds * 2. Split is accurately. We make this. 32361da177e4SLinus Torvalds */ 3237ea2ab693SIan Campbell skb_frag_ref(skb, i); 3238b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 32399e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 32409e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 32411da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds k++; 32441da177e4SLinus Torvalds } else 32451da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 32461da177e4SLinus Torvalds pos += size; 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds /** 32521da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 32531da177e4SLinus Torvalds * @skb: the buffer to split 32541da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 32551da177e4SLinus Torvalds * @len: new length for skb 32561da177e4SLinus Torvalds */ 32571da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 32581da177e4SLinus Torvalds { 32591da177e4SLinus Torvalds int pos = skb_headlen(skb); 32601da177e4SLinus Torvalds 3261fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3262fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 32631f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 32641da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 32651da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 32661da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 32671da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 32681da177e4SLinus Torvalds } 3269b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 32701da177e4SLinus Torvalds 32719f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 32729f782db3SIlpo Järvinen * 32739f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 32749f782db3SIlpo Järvinen */ 3275832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3276832d11c5SIlpo Järvinen { 32770ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3278832d11c5SIlpo Järvinen } 3279832d11c5SIlpo Järvinen 3280832d11c5SIlpo Järvinen /** 3281832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3282832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3283832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3284832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3285832d11c5SIlpo Järvinen * 3286832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 328720e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3288832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3289832d11c5SIlpo Järvinen * 3290832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3291832d11c5SIlpo Järvinen * 3292832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3293832d11c5SIlpo Järvinen * to have non-paged data as well. 3294832d11c5SIlpo Järvinen * 3295832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3296832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3297832d11c5SIlpo Järvinen */ 3298832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3299832d11c5SIlpo Järvinen { 3300832d11c5SIlpo Järvinen int from, to, merge, todo; 3301d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3302832d11c5SIlpo Järvinen 3303832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3304f8071cdeSEric Dumazet 3305f8071cdeSEric Dumazet if (skb_headlen(skb)) 3306f8071cdeSEric Dumazet return 0; 33071f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 33081f8b977aSWillem de Bruijn return 0; 3309832d11c5SIlpo Järvinen 3310832d11c5SIlpo Järvinen todo = shiftlen; 3311832d11c5SIlpo Järvinen from = 0; 3312832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3313832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3314832d11c5SIlpo Järvinen 3315832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3316832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3317832d11c5SIlpo Järvinen */ 3318832d11c5SIlpo Järvinen if (!to || 3319ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3320b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3321832d11c5SIlpo Järvinen merge = -1; 3322832d11c5SIlpo Järvinen } else { 3323832d11c5SIlpo Järvinen merge = to - 1; 3324832d11c5SIlpo Järvinen 33259e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3326832d11c5SIlpo Järvinen if (todo < 0) { 3327832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3328832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3329832d11c5SIlpo Järvinen return 0; 3330832d11c5SIlpo Järvinen 33319f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 33329f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3333832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3334832d11c5SIlpo Järvinen 33359e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 33369e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3337b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3338832d11c5SIlpo Järvinen 3339832d11c5SIlpo Järvinen goto onlymerged; 3340832d11c5SIlpo Järvinen } 3341832d11c5SIlpo Järvinen 3342832d11c5SIlpo Järvinen from++; 3343832d11c5SIlpo Järvinen } 3344832d11c5SIlpo Järvinen 3345832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3346832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3347832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3348832d11c5SIlpo Järvinen return 0; 3349832d11c5SIlpo Järvinen 3350832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3351832d11c5SIlpo Järvinen return 0; 3352832d11c5SIlpo Järvinen 3353832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3354832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3355832d11c5SIlpo Järvinen return 0; 3356832d11c5SIlpo Järvinen 3357832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3358832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3359832d11c5SIlpo Järvinen 33609e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3361832d11c5SIlpo Järvinen *fragto = *fragfrom; 33629e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3363832d11c5SIlpo Järvinen from++; 3364832d11c5SIlpo Järvinen to++; 3365832d11c5SIlpo Järvinen 3366832d11c5SIlpo Järvinen } else { 3367ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3368b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3369b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 33709e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3371832d11c5SIlpo Järvinen 3372b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 33739e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3374832d11c5SIlpo Järvinen todo = 0; 3375832d11c5SIlpo Järvinen 3376832d11c5SIlpo Järvinen to++; 3377832d11c5SIlpo Järvinen break; 3378832d11c5SIlpo Järvinen } 3379832d11c5SIlpo Järvinen } 3380832d11c5SIlpo Järvinen 3381832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3382832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3383832d11c5SIlpo Järvinen 3384832d11c5SIlpo Järvinen if (merge >= 0) { 3385832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3386832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3387832d11c5SIlpo Järvinen 33889e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3389ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 3390832d11c5SIlpo Järvinen } 3391832d11c5SIlpo Järvinen 3392832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3393832d11c5SIlpo Järvinen to = 0; 3394832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3395832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3396832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3397832d11c5SIlpo Järvinen 3398832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3399832d11c5SIlpo Järvinen 3400832d11c5SIlpo Järvinen onlymerged: 3401832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3402832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3403832d11c5SIlpo Järvinen */ 3404832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3405832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3406832d11c5SIlpo Järvinen 3407832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3408832d11c5SIlpo Järvinen skb->len -= shiftlen; 3409832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3410832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3411832d11c5SIlpo Järvinen tgt->len += shiftlen; 3412832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3413832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3414832d11c5SIlpo Järvinen 3415832d11c5SIlpo Järvinen return shiftlen; 3416832d11c5SIlpo Järvinen } 3417832d11c5SIlpo Järvinen 3418677e90edSThomas Graf /** 3419677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3420677e90edSThomas Graf * @skb: the buffer to read 3421677e90edSThomas Graf * @from: lower offset of data to be read 3422677e90edSThomas Graf * @to: upper offset of data to be read 3423677e90edSThomas Graf * @st: state variable 3424677e90edSThomas Graf * 3425677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3426677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3427677e90edSThomas Graf */ 3428677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3429677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3430677e90edSThomas Graf { 3431677e90edSThomas Graf st->lower_offset = from; 3432677e90edSThomas Graf st->upper_offset = to; 3433677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3434677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3435677e90edSThomas Graf st->frag_data = NULL; 3436677e90edSThomas Graf } 3437b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3438677e90edSThomas Graf 3439677e90edSThomas Graf /** 3440677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3441677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3442677e90edSThomas Graf * @data: destination pointer for data to be returned 3443677e90edSThomas Graf * @st: state variable 3444677e90edSThomas Graf * 3445bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3446677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3447bc32383cSMathias Krause * the head of the data block to @data and returns the length 3448677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3449677e90edSThomas Graf * offset has been reached. 3450677e90edSThomas Graf * 3451677e90edSThomas Graf * The caller is not required to consume all of the data 3452bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3453677e90edSThomas Graf * of bytes already consumed and the next call to 3454677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3455677e90edSThomas Graf * 345625985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3457e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3458677e90edSThomas Graf * reads of potentially non linear data. 3459677e90edSThomas Graf * 3460bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3461677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3462677e90edSThomas Graf * a stack for this purpose. 3463677e90edSThomas Graf */ 3464677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3465677e90edSThomas Graf struct skb_seq_state *st) 3466677e90edSThomas Graf { 3467677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3468677e90edSThomas Graf skb_frag_t *frag; 3469677e90edSThomas Graf 3470aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3471aeb193eaSWedson Almeida Filho if (st->frag_data) { 3472aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3473aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3474aeb193eaSWedson Almeida Filho } 3475677e90edSThomas Graf return 0; 3476aeb193eaSWedson Almeida Filho } 3477677e90edSThomas Graf 3478677e90edSThomas Graf next_skb: 347995e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3480677e90edSThomas Graf 3481995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 348295e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3483677e90edSThomas Graf return block_limit - abs_offset; 3484677e90edSThomas Graf } 3485677e90edSThomas Graf 3486677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3487677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3488677e90edSThomas Graf 3489677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3490677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 34919e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 3492677e90edSThomas Graf 3493677e90edSThomas Graf if (abs_offset < block_limit) { 3494677e90edSThomas Graf if (!st->frag_data) 349551c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 3496677e90edSThomas Graf 3497b54c9d5bSJonathan Lemon *data = (u8 *) st->frag_data + skb_frag_off(frag) + 3498677e90edSThomas Graf (abs_offset - st->stepped_offset); 3499677e90edSThomas Graf 3500677e90edSThomas Graf return block_limit - abs_offset; 3501677e90edSThomas Graf } 3502677e90edSThomas Graf 3503677e90edSThomas Graf if (st->frag_data) { 350451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3505677e90edSThomas Graf st->frag_data = NULL; 3506677e90edSThomas Graf } 3507677e90edSThomas Graf 3508677e90edSThomas Graf st->frag_idx++; 35099e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 3510677e90edSThomas Graf } 3511677e90edSThomas Graf 35125b5a60daSOlaf Kirch if (st->frag_data) { 351351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 35145b5a60daSOlaf Kirch st->frag_data = NULL; 35155b5a60daSOlaf Kirch } 35165b5a60daSOlaf Kirch 351721dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3518677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 351995e3b24cSHerbert Xu st->frag_idx = 0; 3520677e90edSThomas Graf goto next_skb; 352171b3346dSShyam Iyer } else if (st->cur_skb->next) { 352271b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 352371b3346dSShyam Iyer st->frag_idx = 0; 3524677e90edSThomas Graf goto next_skb; 3525677e90edSThomas Graf } 3526677e90edSThomas Graf 3527677e90edSThomas Graf return 0; 3528677e90edSThomas Graf } 3529b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3530677e90edSThomas Graf 3531677e90edSThomas Graf /** 3532677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3533677e90edSThomas Graf * @st: state variable 3534677e90edSThomas Graf * 3535677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3536677e90edSThomas Graf * returned 0. 3537677e90edSThomas Graf */ 3538677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3539677e90edSThomas Graf { 3540677e90edSThomas Graf if (st->frag_data) 354151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3542677e90edSThomas Graf } 3543b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3544677e90edSThomas Graf 35453fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 35463fc7e8a6SThomas Graf 35473fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 35483fc7e8a6SThomas Graf struct ts_config *conf, 35493fc7e8a6SThomas Graf struct ts_state *state) 35503fc7e8a6SThomas Graf { 35513fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 35523fc7e8a6SThomas Graf } 35533fc7e8a6SThomas Graf 35543fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 35553fc7e8a6SThomas Graf { 35563fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 35573fc7e8a6SThomas Graf } 35583fc7e8a6SThomas Graf 35593fc7e8a6SThomas Graf /** 35603fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 35613fc7e8a6SThomas Graf * @skb: the buffer to look in 35623fc7e8a6SThomas Graf * @from: search offset 35633fc7e8a6SThomas Graf * @to: search limit 35643fc7e8a6SThomas Graf * @config: textsearch configuration 35653fc7e8a6SThomas Graf * 35663fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 35673fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 35683fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 35693fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 35703fc7e8a6SThomas Graf */ 35713fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3572059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 35733fc7e8a6SThomas Graf { 3574059a2440SBojan Prtvar struct ts_state state; 3575f72b948dSPhil Oester unsigned int ret; 3576f72b948dSPhil Oester 35773fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 35783fc7e8a6SThomas Graf config->finish = skb_ts_finish; 35793fc7e8a6SThomas Graf 3580059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 35813fc7e8a6SThomas Graf 3582059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3583f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 35843fc7e8a6SThomas Graf } 3585b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 35863fc7e8a6SThomas Graf 3587be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3588be12a1feSHannes Frederic Sowa int offset, size_t size) 3589be12a1feSHannes Frederic Sowa { 3590be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3591be12a1feSHannes Frederic Sowa 3592be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3593be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3594be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3595be12a1feSHannes Frederic Sowa get_page(page); 3596be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3597be12a1feSHannes Frederic Sowa } else { 3598be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3599be12a1feSHannes Frederic Sowa } 3600be12a1feSHannes Frederic Sowa 3601be12a1feSHannes Frederic Sowa return 0; 3602be12a1feSHannes Frederic Sowa } 3603be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3604be12a1feSHannes Frederic Sowa 3605cbb042f9SHerbert Xu /** 3606cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3607cbb042f9SHerbert Xu * @skb: buffer to update 3608cbb042f9SHerbert Xu * @len: length of data pulled 3609cbb042f9SHerbert Xu * 3610cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3611fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 361284fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 361384fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 361484fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3615cbb042f9SHerbert Xu */ 3616af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3617cbb042f9SHerbert Xu { 361831b33dfbSPravin B Shelar unsigned char *data = skb->data; 361931b33dfbSPravin B Shelar 3620cbb042f9SHerbert Xu BUG_ON(len > skb->len); 362131b33dfbSPravin B Shelar __skb_pull(skb, len); 362231b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 362331b33dfbSPravin B Shelar return skb->data; 3624cbb042f9SHerbert Xu } 3625f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3626f94691acSArnaldo Carvalho de Melo 362713acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 362813acc94eSYonghong Song { 362913acc94eSYonghong Song skb_frag_t head_frag; 363013acc94eSYonghong Song struct page *page; 363113acc94eSYonghong Song 363213acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 3633d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 3634b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 3635b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 3636d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 363713acc94eSYonghong Song return head_frag; 363813acc94eSYonghong Song } 363913acc94eSYonghong Song 36403a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 36413a1296a3SSteffen Klassert netdev_features_t features, 36423a1296a3SSteffen Klassert unsigned int offset) 36433a1296a3SSteffen Klassert { 36443a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 36453a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 36463a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 36473a1296a3SSteffen Klassert unsigned int delta_len = 0; 36483a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 36493a1296a3SSteffen Klassert struct sk_buff *nskb; 36503a1296a3SSteffen Klassert 36513a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 36523a1296a3SSteffen Klassert 36533a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 36543a1296a3SSteffen Klassert 36553a1296a3SSteffen Klassert do { 36563a1296a3SSteffen Klassert nskb = list_skb; 36573a1296a3SSteffen Klassert list_skb = list_skb->next; 36583a1296a3SSteffen Klassert 36593a1296a3SSteffen Klassert if (!tail) 36603a1296a3SSteffen Klassert skb->next = nskb; 36613a1296a3SSteffen Klassert else 36623a1296a3SSteffen Klassert tail->next = nskb; 36633a1296a3SSteffen Klassert 36643a1296a3SSteffen Klassert tail = nskb; 36653a1296a3SSteffen Klassert 36663a1296a3SSteffen Klassert delta_len += nskb->len; 36673a1296a3SSteffen Klassert delta_truesize += nskb->truesize; 36683a1296a3SSteffen Klassert 36693a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 36703a1296a3SSteffen Klassert 36713a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 36723a1296a3SSteffen Klassert 36733a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 36743a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 36753a1296a3SSteffen Klassert nskb->data - tnl_hlen, 36763a1296a3SSteffen Klassert offset + tnl_hlen); 36773a1296a3SSteffen Klassert 36783a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 36793a1296a3SSteffen Klassert __skb_linearize(nskb)) 36803a1296a3SSteffen Klassert goto err_linearize; 36813a1296a3SSteffen Klassert 36823a1296a3SSteffen Klassert } while (list_skb); 36833a1296a3SSteffen Klassert 36843a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 36853a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 36863a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 36873a1296a3SSteffen Klassert 36883a1296a3SSteffen Klassert skb_gso_reset(skb); 36893a1296a3SSteffen Klassert 36903a1296a3SSteffen Klassert skb->prev = tail; 36913a1296a3SSteffen Klassert 36923a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 36933a1296a3SSteffen Klassert __skb_linearize(skb)) 36943a1296a3SSteffen Klassert goto err_linearize; 36953a1296a3SSteffen Klassert 36963a1296a3SSteffen Klassert skb_get(skb); 36973a1296a3SSteffen Klassert 36983a1296a3SSteffen Klassert return skb; 36993a1296a3SSteffen Klassert 37003a1296a3SSteffen Klassert err_linearize: 37013a1296a3SSteffen Klassert kfree_skb_list(skb->next); 37023a1296a3SSteffen Klassert skb->next = NULL; 37033a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 37043a1296a3SSteffen Klassert } 37053a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 37063a1296a3SSteffen Klassert 37073a1296a3SSteffen Klassert int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb) 37083a1296a3SSteffen Klassert { 37093a1296a3SSteffen Klassert if (unlikely(p->len + skb->len >= 65536)) 37103a1296a3SSteffen Klassert return -E2BIG; 37113a1296a3SSteffen Klassert 37123a1296a3SSteffen Klassert if (NAPI_GRO_CB(p)->last == p) 37133a1296a3SSteffen Klassert skb_shinfo(p)->frag_list = skb; 37143a1296a3SSteffen Klassert else 37153a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last->next = skb; 37163a1296a3SSteffen Klassert 37173a1296a3SSteffen Klassert skb_pull(skb, skb_gro_offset(skb)); 37183a1296a3SSteffen Klassert 37193a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last = skb; 37203a1296a3SSteffen Klassert NAPI_GRO_CB(p)->count++; 37213a1296a3SSteffen Klassert p->data_len += skb->len; 37223a1296a3SSteffen Klassert p->truesize += skb->truesize; 37233a1296a3SSteffen Klassert p->len += skb->len; 37243a1296a3SSteffen Klassert 37253a1296a3SSteffen Klassert NAPI_GRO_CB(skb)->same_flow = 1; 37263a1296a3SSteffen Klassert 37273a1296a3SSteffen Klassert return 0; 37283a1296a3SSteffen Klassert } 37293a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_gro_receive_list); 37303a1296a3SSteffen Klassert 3731f4c50d99SHerbert Xu /** 3732f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3733df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3734576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3735f4c50d99SHerbert Xu * 3736f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 37374c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 37384c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3739f4c50d99SHerbert Xu */ 3740df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3741df5771ffSMichael S. Tsirkin netdev_features_t features) 3742f4c50d99SHerbert Xu { 3743f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3744f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 37451a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3746df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3747df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3748df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 37491fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3750f4c50d99SHerbert Xu unsigned int offset = doffset; 3751df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3752802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3753f4c50d99SHerbert Xu unsigned int headroom; 3754802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3755ec5f0615SPravin B Shelar __be16 proto; 375636c98382SAlexander Duyck bool csum, sg; 3757df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3758f4c50d99SHerbert Xu int err = -ENOMEM; 3759f4c50d99SHerbert Xu int i = 0; 3760f4c50d99SHerbert Xu int pos; 376153d6471cSVlad Yasevich int dummy; 3762f4c50d99SHerbert Xu 37633dcbdb13SShmulik Ladkani if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 37643dcbdb13SShmulik Ladkani (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 37653dcbdb13SShmulik Ladkani /* gso_size is untrusted, and we have a frag_list with a linear 37663dcbdb13SShmulik Ladkani * non head_frag head. 37673dcbdb13SShmulik Ladkani * 37683dcbdb13SShmulik Ladkani * (we assume checking the first list_skb member suffices; 37693dcbdb13SShmulik Ladkani * i.e if either of the list_skb members have non head_frag 37703dcbdb13SShmulik Ladkani * head, then the first one has too). 37713dcbdb13SShmulik Ladkani * 37723dcbdb13SShmulik Ladkani * If head_skb's headlen does not fit requested gso_size, it 37733dcbdb13SShmulik Ladkani * means that the frag_list members do NOT terminate on exact 37743dcbdb13SShmulik Ladkani * gso_size boundaries. Hence we cannot perform skb_frag_t page 37753dcbdb13SShmulik Ladkani * sharing. Therefore we must fallback to copying the frag_list 37763dcbdb13SShmulik Ladkani * skbs; we do so by disabling SG. 37773dcbdb13SShmulik Ladkani */ 37783dcbdb13SShmulik Ladkani if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 37793dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 37803dcbdb13SShmulik Ladkani } 37813dcbdb13SShmulik Ladkani 37825882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 378353d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3784ec5f0615SPravin B Shelar if (unlikely(!proto)) 3785ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3786ec5f0615SPravin B Shelar 378736c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3788f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 37897e2b10c1STom Herbert 379007b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 379107b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 379207b26c94SSteffen Klassert struct sk_buff *iter; 379343170c4eSIlan Tayari unsigned int frag_len; 379407b26c94SSteffen Klassert 379507b26c94SSteffen Klassert if (!list_skb || 379607b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 379707b26c94SSteffen Klassert goto normal; 379807b26c94SSteffen Klassert 379943170c4eSIlan Tayari /* If we get here then all the required 380043170c4eSIlan Tayari * GSO features except frag_list are supported. 380143170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 380243170c4eSIlan Tayari * with no frag_list. 380343170c4eSIlan Tayari * Currently we can do that only when the buffers don't 380443170c4eSIlan Tayari * have a linear part and all the buffers except 380543170c4eSIlan Tayari * the last are of the same length. 380607b26c94SSteffen Klassert */ 380743170c4eSIlan Tayari frag_len = list_skb->len; 380807b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 380943170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 381043170c4eSIlan Tayari goto normal; 3811eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 381207b26c94SSteffen Klassert goto normal; 381307b26c94SSteffen Klassert 381407b26c94SSteffen Klassert len -= iter->len; 381507b26c94SSteffen Klassert } 381643170c4eSIlan Tayari 381743170c4eSIlan Tayari if (len != frag_len) 381843170c4eSIlan Tayari goto normal; 381907b26c94SSteffen Klassert } 382007b26c94SSteffen Klassert 3821802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3822802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3823802ab55aSAlexander Duyck * now. 3824802ab55aSAlexander Duyck */ 3825802ab55aSAlexander Duyck partial_segs = len / mss; 3826d7fb5a80SAlexander Duyck if (partial_segs > 1) 3827802ab55aSAlexander Duyck mss *= partial_segs; 3828d7fb5a80SAlexander Duyck else 3829d7fb5a80SAlexander Duyck partial_segs = 0; 3830802ab55aSAlexander Duyck } 3831802ab55aSAlexander Duyck 383207b26c94SSteffen Klassert normal: 3833df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3834df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3835f4c50d99SHerbert Xu 3836f4c50d99SHerbert Xu do { 3837f4c50d99SHerbert Xu struct sk_buff *nskb; 38388cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3839c8884eddSHerbert Xu int hsize; 3840f4c50d99SHerbert Xu int size; 3841f4c50d99SHerbert Xu 38423953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 38433953c46cSMarcelo Ricardo Leitner len = list_skb->len; 38443953c46cSMarcelo Ricardo Leitner } else { 3845df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3846f4c50d99SHerbert Xu if (len > mss) 3847f4c50d99SHerbert Xu len = mss; 38483953c46cSMarcelo Ricardo Leitner } 3849f4c50d99SHerbert Xu 3850df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3851f4c50d99SHerbert Xu if (hsize < 0) 3852f4c50d99SHerbert Xu hsize = 0; 3853c8884eddSHerbert Xu if (hsize > len || !sg) 3854c8884eddSHerbert Xu hsize = len; 3855f4c50d99SHerbert Xu 38561a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 38571a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 38581a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 385989319d38SHerbert Xu 38609d8506ccSHerbert Xu i = 0; 38611a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 38621a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 38631fd819ecSMichael S. Tsirkin frag_skb = list_skb; 38641a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 38659d8506ccSHerbert Xu 38669d8506ccSHerbert Xu while (pos < offset + len) { 38679d8506ccSHerbert Xu BUG_ON(i >= nfrags); 38689d8506ccSHerbert Xu 38694e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 38709d8506ccSHerbert Xu if (pos + size > offset + len) 38719d8506ccSHerbert Xu break; 38729d8506ccSHerbert Xu 38739d8506ccSHerbert Xu i++; 38749d8506ccSHerbert Xu pos += size; 38754e1beba1SMichael S. Tsirkin frag++; 38769d8506ccSHerbert Xu } 38779d8506ccSHerbert Xu 38781a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 38791a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 388089319d38SHerbert Xu 3881f4c50d99SHerbert Xu if (unlikely(!nskb)) 3882f4c50d99SHerbert Xu goto err; 3883f4c50d99SHerbert Xu 38849d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 38859d8506ccSHerbert Xu kfree_skb(nskb); 38869d8506ccSHerbert Xu goto err; 38879d8506ccSHerbert Xu } 38889d8506ccSHerbert Xu 3889ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 389089319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 389189319d38SHerbert Xu kfree_skb(nskb); 389289319d38SHerbert Xu goto err; 389389319d38SHerbert Xu } 389489319d38SHerbert Xu 3895ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 389689319d38SHerbert Xu skb_release_head_state(nskb); 389789319d38SHerbert Xu __skb_push(nskb, doffset); 389889319d38SHerbert Xu } else { 3899c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3900df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3901c93bdd0eSMel Gorman NUMA_NO_NODE); 390289319d38SHerbert Xu 390389319d38SHerbert Xu if (unlikely(!nskb)) 390489319d38SHerbert Xu goto err; 390589319d38SHerbert Xu 390689319d38SHerbert Xu skb_reserve(nskb, headroom); 390789319d38SHerbert Xu __skb_put(nskb, doffset); 390889319d38SHerbert Xu } 390989319d38SHerbert Xu 3910f4c50d99SHerbert Xu if (segs) 3911f4c50d99SHerbert Xu tail->next = nskb; 3912f4c50d99SHerbert Xu else 3913f4c50d99SHerbert Xu segs = nskb; 3914f4c50d99SHerbert Xu tail = nskb; 3915f4c50d99SHerbert Xu 3916df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3917f4c50d99SHerbert Xu 3918030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3919fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 392068c33163SPravin B Shelar 3921df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 392268c33163SPravin B Shelar nskb->data - tnl_hlen, 392368c33163SPravin B Shelar doffset + tnl_hlen); 392489319d38SHerbert Xu 39259d8506ccSHerbert Xu if (nskb->len == len + doffset) 39261cdbcb79SSimon Horman goto perform_csum_check; 392789319d38SHerbert Xu 39287fbeffedSAlexander Duyck if (!sg) { 39291454c9faSYadu Kishore if (!csum) { 39307fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 39316f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 393276443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 393376443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 39341454c9faSYadu Kishore skb_put(nskb, 39351454c9faSYadu Kishore len), 3936f4c50d99SHerbert Xu len, 0); 39377e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3938de843723STom Herbert skb_headroom(nskb) + doffset; 39391454c9faSYadu Kishore } else { 39401454c9faSYadu Kishore skb_copy_bits(head_skb, offset, 39411454c9faSYadu Kishore skb_put(nskb, len), 39421454c9faSYadu Kishore len); 39431454c9faSYadu Kishore } 3944f4c50d99SHerbert Xu continue; 3945f4c50d99SHerbert Xu } 3946f4c50d99SHerbert Xu 39478cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3948f4c50d99SHerbert Xu 3949df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3950d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3951f4c50d99SHerbert Xu 3952fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3953df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3954cef401deSEric Dumazet 3955bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3956bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 3957bf5c25d6SWillem de Bruijn goto err; 3958bf5c25d6SWillem de Bruijn 39599d8506ccSHerbert Xu while (pos < offset + len) { 39609d8506ccSHerbert Xu if (i >= nfrags) { 39619d8506ccSHerbert Xu i = 0; 39621a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 39631a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 39641fd819ecSMichael S. Tsirkin frag_skb = list_skb; 396513acc94eSYonghong Song if (!skb_headlen(list_skb)) { 39669d8506ccSHerbert Xu BUG_ON(!nfrags); 396713acc94eSYonghong Song } else { 396813acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 39699d8506ccSHerbert Xu 397013acc94eSYonghong Song /* to make room for head_frag. */ 397113acc94eSYonghong Song i--; 397213acc94eSYonghong Song frag--; 397313acc94eSYonghong Song } 3974bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3975bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 3976bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 3977bf5c25d6SWillem de Bruijn goto err; 3978bf5c25d6SWillem de Bruijn 39791a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 39809d8506ccSHerbert Xu } 39819d8506ccSHerbert Xu 39829d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 39839d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 39849d8506ccSHerbert Xu net_warn_ratelimited( 39859d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 39869d8506ccSHerbert Xu pos, mss); 3987ff907a11SEric Dumazet err = -EINVAL; 39889d8506ccSHerbert Xu goto err; 39899d8506ccSHerbert Xu } 39909d8506ccSHerbert Xu 399113acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 39928cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 39938cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3994f4c50d99SHerbert Xu 3995f4c50d99SHerbert Xu if (pos < offset) { 3996b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 39978cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3998f4c50d99SHerbert Xu } 3999f4c50d99SHerbert Xu 400089319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4001f4c50d99SHerbert Xu 4002f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4003f4c50d99SHerbert Xu i++; 40044e1beba1SMichael S. Tsirkin frag++; 4005f4c50d99SHerbert Xu pos += size; 4006f4c50d99SHerbert Xu } else { 40078cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 400889319d38SHerbert Xu goto skip_fraglist; 4009f4c50d99SHerbert Xu } 4010f4c50d99SHerbert Xu 40118cb19905SMichael S. Tsirkin nskb_frag++; 4012f4c50d99SHerbert Xu } 4013f4c50d99SHerbert Xu 401489319d38SHerbert Xu skip_fraglist: 4015f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4016f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4017f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4018ec5f0615SPravin B Shelar 40191cdbcb79SSimon Horman perform_csum_check: 40207fbeffedSAlexander Duyck if (!csum) { 4021ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4022ff907a11SEric Dumazet __skb_linearize(nskb)) 4023ddff00d4SAlexander Duyck goto err; 4024ff907a11SEric Dumazet 40257fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4026ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 402776443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 402876443456SAlexander Duyck skb_checksum(nskb, doffset, 402976443456SAlexander Duyck nskb->len - doffset, 0); 40307e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 40317e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4032ec5f0615SPravin B Shelar } 4033df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4034f4c50d99SHerbert Xu 4035bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4036bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4037bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4038bec3cfdcSEric Dumazet */ 4039bec3cfdcSEric Dumazet segs->prev = tail; 4040432c856fSToshiaki Makita 4041802ab55aSAlexander Duyck if (partial_segs) { 404207b26c94SSteffen Klassert struct sk_buff *iter; 4043802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 404407b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4045802ab55aSAlexander Duyck 4046802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 404707b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4048802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4049802ab55aSAlexander Duyck 4050802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4051802ab55aSAlexander Duyck * our way back down the stack of protocols. 4052802ab55aSAlexander Duyck */ 405307b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 405407b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 405507b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 405607b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 405707b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 405807b26c94SSteffen Klassert } 405907b26c94SSteffen Klassert 406007b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 406107b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 406207b26c94SSteffen Klassert else if (tail != segs) 406307b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4064802ab55aSAlexander Duyck } 4065802ab55aSAlexander Duyck 4066432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4067432c856fSToshiaki Makita * using skb_set_owner_w(). 4068432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4069432c856fSToshiaki Makita */ 4070432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4071432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4072432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4073432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4074432c856fSToshiaki Makita } 4075f4c50d99SHerbert Xu return segs; 4076f4c50d99SHerbert Xu 4077f4c50d99SHerbert Xu err: 4078289dccbeSEric Dumazet kfree_skb_list(segs); 4079f4c50d99SHerbert Xu return ERR_PTR(err); 4080f4c50d99SHerbert Xu } 4081f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4082f4c50d99SHerbert Xu 4083d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) 408471d93b39SHerbert Xu { 40858a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 408667147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 408767147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 40888a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 4089715dc1f3SEric Dumazet unsigned int delta_truesize; 4090d4546c25SDavid Miller struct sk_buff *lp; 409171d93b39SHerbert Xu 40920ab03f35SSteffen Klassert if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush)) 409371d93b39SHerbert Xu return -E2BIG; 409471d93b39SHerbert Xu 409529e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 40968a29111cSEric Dumazet pinfo = skb_shinfo(lp); 40978a29111cSEric Dumazet 40988a29111cSEric Dumazet if (headlen <= offset) { 409942da6994SHerbert Xu skb_frag_t *frag; 410066e92fcfSHerbert Xu skb_frag_t *frag2; 41019aaa156cSHerbert Xu int i = skbinfo->nr_frags; 41029aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 410342da6994SHerbert Xu 410466e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 41058a29111cSEric Dumazet goto merge; 410681705ad1SHerbert Xu 41078a29111cSEric Dumazet offset -= headlen; 41089aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 41099aaa156cSHerbert Xu skbinfo->nr_frags = 0; 4110f5572068SHerbert Xu 41119aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 41129aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 411366e92fcfSHerbert Xu do { 411466e92fcfSHerbert Xu *--frag = *--frag2; 411566e92fcfSHerbert Xu } while (--i); 411666e92fcfSHerbert Xu 4117b54c9d5bSJonathan Lemon skb_frag_off_add(frag, offset); 41189e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 411966e92fcfSHerbert Xu 4120715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 4121ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 4122ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 4123715dc1f3SEric Dumazet 4124f5572068SHerbert Xu skb->truesize -= skb->data_len; 4125f5572068SHerbert Xu skb->len -= skb->data_len; 4126f5572068SHerbert Xu skb->data_len = 0; 4127f5572068SHerbert Xu 4128715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 41295d38a079SHerbert Xu goto done; 4130d7e8883cSEric Dumazet } else if (skb->head_frag) { 4131d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 4132d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 4133d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 4134d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 4135d7e8883cSEric Dumazet unsigned int first_offset; 4136d7e8883cSEric Dumazet 4137d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 41388a29111cSEric Dumazet goto merge; 4139d7e8883cSEric Dumazet 4140d7e8883cSEric Dumazet first_offset = skb->data - 4141d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 4142d7e8883cSEric Dumazet offset; 4143d7e8883cSEric Dumazet 4144d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 4145d7e8883cSEric Dumazet 4146d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(frag, page); 4147b54c9d5bSJonathan Lemon skb_frag_off_set(frag, first_offset); 4148d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 4149d7e8883cSEric Dumazet 4150d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 4151d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 4152d7e8883cSEric Dumazet 4153715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4154d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 4155d7e8883cSEric Dumazet goto done; 41568a29111cSEric Dumazet } 415771d93b39SHerbert Xu 415871d93b39SHerbert Xu merge: 4159715dc1f3SEric Dumazet delta_truesize = skb->truesize; 416067147ba9SHerbert Xu if (offset > headlen) { 4161d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 4162d1dc7abfSMichal Schmidt 4163b54c9d5bSJonathan Lemon skb_frag_off_add(&skbinfo->frags[0], eat); 41649e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 4165d1dc7abfSMichal Schmidt skb->data_len -= eat; 4166d1dc7abfSMichal Schmidt skb->len -= eat; 416767147ba9SHerbert Xu offset = headlen; 416856035022SHerbert Xu } 416956035022SHerbert Xu 417067147ba9SHerbert Xu __skb_pull(skb, offset); 417156035022SHerbert Xu 417229e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 41738a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 41748a29111cSEric Dumazet else 4175c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 4176c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 4177f4a775d1SEric Dumazet __skb_header_release(skb); 41788a29111cSEric Dumazet lp = p; 417971d93b39SHerbert Xu 41805d38a079SHerbert Xu done: 41815d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 418237fe4732SHerbert Xu p->data_len += len; 4183715dc1f3SEric Dumazet p->truesize += delta_truesize; 418437fe4732SHerbert Xu p->len += len; 41858a29111cSEric Dumazet if (lp != p) { 41868a29111cSEric Dumazet lp->data_len += len; 41878a29111cSEric Dumazet lp->truesize += delta_truesize; 41888a29111cSEric Dumazet lp->len += len; 41898a29111cSEric Dumazet } 419071d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 419171d93b39SHerbert Xu return 0; 419271d93b39SHerbert Xu } 419357c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive); 419471d93b39SHerbert Xu 4195df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4196df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4197df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4198df5042f4SFlorian Westphal 4199df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4200df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4201df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4202df5042f4SFlorian Westphal #endif 42034165079bSFlorian Westphal #ifdef CONFIG_XFRM 42044165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 42054165079bSFlorian Westphal #endif 420695a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 420795a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 420895a7233cSPaul Blakey #endif 42093ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 42103ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 42113ee17bc7SMat Martineau #endif 4212df5042f4SFlorian Westphal }; 4213df5042f4SFlorian Westphal 4214df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4215df5042f4SFlorian Westphal { 4216df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4217df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4218df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4219df5042f4SFlorian Westphal #endif 42204165079bSFlorian Westphal #ifdef CONFIG_XFRM 42214165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 42224165079bSFlorian Westphal #endif 422395a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 422495a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 422595a7233cSPaul Blakey #endif 42263ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 42273ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 42283ee17bc7SMat Martineau #endif 4229df5042f4SFlorian Westphal 0; 4230df5042f4SFlorian Westphal } 4231df5042f4SFlorian Westphal 4232df5042f4SFlorian Westphal static void skb_extensions_init(void) 4233df5042f4SFlorian Westphal { 4234df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4235df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4236df5042f4SFlorian Westphal 4237df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4238df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4239df5042f4SFlorian Westphal 0, 4240df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4241df5042f4SFlorian Westphal NULL); 4242df5042f4SFlorian Westphal } 4243df5042f4SFlorian Westphal #else 4244df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4245df5042f4SFlorian Westphal #endif 4246df5042f4SFlorian Westphal 42471da177e4SLinus Torvalds void __init skb_init(void) 42481da177e4SLinus Torvalds { 424979a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 42501da177e4SLinus Torvalds sizeof(struct sk_buff), 42511da177e4SLinus Torvalds 0, 4252e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 425379a8a642SKees Cook offsetof(struct sk_buff, cb), 425479a8a642SKees Cook sizeof_field(struct sk_buff, cb), 425520c2df83SPaul Mundt NULL); 4256d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4257d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4258d179cd12SDavid S. Miller 0, 4259e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 426020c2df83SPaul Mundt NULL); 4261df5042f4SFlorian Westphal skb_extensions_init(); 42621da177e4SLinus Torvalds } 42631da177e4SLinus Torvalds 426451c739d1SDavid S. Miller static int 426548a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 426648a1df65SJason A. Donenfeld unsigned int recursion_level) 4267716ea3a7SDavid Howells { 42681a028e50SDavid S. Miller int start = skb_headlen(skb); 42691a028e50SDavid S. Miller int i, copy = start - offset; 4270fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4271716ea3a7SDavid Howells int elt = 0; 4272716ea3a7SDavid Howells 427348a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 427448a1df65SJason A. Donenfeld return -EMSGSIZE; 427548a1df65SJason A. Donenfeld 4276716ea3a7SDavid Howells if (copy > 0) { 4277716ea3a7SDavid Howells if (copy > len) 4278716ea3a7SDavid Howells copy = len; 4279642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4280716ea3a7SDavid Howells elt++; 4281716ea3a7SDavid Howells if ((len -= copy) == 0) 4282716ea3a7SDavid Howells return elt; 4283716ea3a7SDavid Howells offset += copy; 4284716ea3a7SDavid Howells } 4285716ea3a7SDavid Howells 4286716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 42871a028e50SDavid S. Miller int end; 4288716ea3a7SDavid Howells 4289547b792cSIlpo Järvinen WARN_ON(start > offset + len); 42901a028e50SDavid S. Miller 42919e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4292716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4293716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 429448a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 429548a1df65SJason A. Donenfeld return -EMSGSIZE; 4296716ea3a7SDavid Howells 4297716ea3a7SDavid Howells if (copy > len) 4298716ea3a7SDavid Howells copy = len; 4299ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4300b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4301716ea3a7SDavid Howells elt++; 4302716ea3a7SDavid Howells if (!(len -= copy)) 4303716ea3a7SDavid Howells return elt; 4304716ea3a7SDavid Howells offset += copy; 4305716ea3a7SDavid Howells } 43061a028e50SDavid S. Miller start = end; 4307716ea3a7SDavid Howells } 4308716ea3a7SDavid Howells 4309fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 431048a1df65SJason A. Donenfeld int end, ret; 4311716ea3a7SDavid Howells 4312547b792cSIlpo Järvinen WARN_ON(start > offset + len); 43131a028e50SDavid S. Miller 4314fbb398a8SDavid S. Miller end = start + frag_iter->len; 4315716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 431648a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 431748a1df65SJason A. Donenfeld return -EMSGSIZE; 431848a1df65SJason A. Donenfeld 4319716ea3a7SDavid Howells if (copy > len) 4320716ea3a7SDavid Howells copy = len; 432148a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 432248a1df65SJason A. Donenfeld copy, recursion_level + 1); 432348a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 432448a1df65SJason A. Donenfeld return ret; 432548a1df65SJason A. Donenfeld elt += ret; 4326716ea3a7SDavid Howells if ((len -= copy) == 0) 4327716ea3a7SDavid Howells return elt; 4328716ea3a7SDavid Howells offset += copy; 4329716ea3a7SDavid Howells } 43301a028e50SDavid S. Miller start = end; 4331716ea3a7SDavid Howells } 4332716ea3a7SDavid Howells BUG_ON(len); 4333716ea3a7SDavid Howells return elt; 4334716ea3a7SDavid Howells } 4335716ea3a7SDavid Howells 433648a1df65SJason A. Donenfeld /** 433748a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 433848a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 433948a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 434048a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 434148a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 434248a1df65SJason A. Donenfeld * 434348a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 434448a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 434548a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 434648a1df65SJason A. Donenfeld * could not fit. 434748a1df65SJason A. Donenfeld */ 434848a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 434948a1df65SJason A. Donenfeld { 435048a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 435148a1df65SJason A. Donenfeld 435248a1df65SJason A. Donenfeld if (nsg <= 0) 435348a1df65SJason A. Donenfeld return nsg; 435448a1df65SJason A. Donenfeld 435548a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 435648a1df65SJason A. Donenfeld 435748a1df65SJason A. Donenfeld return nsg; 435848a1df65SJason A. Donenfeld } 435948a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 436048a1df65SJason A. Donenfeld 436125a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 436225a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 436325a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 436425a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 436525a91d8dSFan Du * 436625a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 436725a91d8dSFan Du * 1. sg_init_table 436825a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 436925a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 437025a91d8dSFan Du * 437125a91d8dSFan Du * This is equivalent to: 437225a91d8dSFan Du * 1. sg_init_table 437325a91d8dSFan Du * 2. skb_to_sgvec(payload1) 437425a91d8dSFan Du * 3. sg_unmark_end 437525a91d8dSFan Du * 4. skb_to_sgvec(payload2) 437625a91d8dSFan Du * 437725a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 437825a91d8dSFan Du * is more preferable. 437925a91d8dSFan Du */ 438025a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 438125a91d8dSFan Du int offset, int len) 438225a91d8dSFan Du { 438348a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 438425a91d8dSFan Du } 438525a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 438625a91d8dSFan Du 438751c739d1SDavid S. Miller 438851c739d1SDavid S. Miller 4389716ea3a7SDavid Howells /** 4390716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4391716ea3a7SDavid Howells * @skb: The socket buffer to check. 4392716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4393716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4394716ea3a7SDavid Howells * 4395716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4396716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4397716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4398716ea3a7SDavid Howells * 4399716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4400716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4401716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4402716ea3a7SDavid Howells * 4403716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4404716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4405716ea3a7SDavid Howells */ 4406716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4407716ea3a7SDavid Howells { 4408716ea3a7SDavid Howells int copyflag; 4409716ea3a7SDavid Howells int elt; 4410716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4411716ea3a7SDavid Howells 4412716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4413716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4414716ea3a7SDavid Howells * at the moment even if they are anonymous). 4415716ea3a7SDavid Howells */ 4416716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4417716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 4418716ea3a7SDavid Howells return -ENOMEM; 4419716ea3a7SDavid Howells 4420716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 442121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4422716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4423716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4424716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4425716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4426716ea3a7SDavid Howells 4427716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4428716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4429716ea3a7SDavid Howells return -ENOMEM; 4430716ea3a7SDavid Howells 4431716ea3a7SDavid Howells /* Voila! */ 4432716ea3a7SDavid Howells *trailer = skb; 4433716ea3a7SDavid Howells return 1; 4434716ea3a7SDavid Howells } 4435716ea3a7SDavid Howells 4436716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4437716ea3a7SDavid Howells 4438716ea3a7SDavid Howells elt = 1; 4439716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4440716ea3a7SDavid Howells copyflag = 0; 4441716ea3a7SDavid Howells 4442716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4443716ea3a7SDavid Howells int ntail = 0; 4444716ea3a7SDavid Howells 4445716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4446716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4447716ea3a7SDavid Howells * after it. */ 4448716ea3a7SDavid Howells 4449716ea3a7SDavid Howells if (skb_shared(skb1)) 4450716ea3a7SDavid Howells copyflag = 1; 4451716ea3a7SDavid Howells 4452716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4453716ea3a7SDavid Howells 4454716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4455716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 445621dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4457716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4458716ea3a7SDavid Howells ntail = tailbits + 128; 4459716ea3a7SDavid Howells } 4460716ea3a7SDavid Howells 4461716ea3a7SDavid Howells if (copyflag || 4462716ea3a7SDavid Howells skb_cloned(skb1) || 4463716ea3a7SDavid Howells ntail || 4464716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 446521dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4466716ea3a7SDavid Howells struct sk_buff *skb2; 4467716ea3a7SDavid Howells 4468716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4469716ea3a7SDavid Howells if (ntail == 0) 4470716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4471716ea3a7SDavid Howells else 4472716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4473716ea3a7SDavid Howells skb_headroom(skb1), 4474716ea3a7SDavid Howells ntail, 4475716ea3a7SDavid Howells GFP_ATOMIC); 4476716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4477716ea3a7SDavid Howells return -ENOMEM; 4478716ea3a7SDavid Howells 4479716ea3a7SDavid Howells if (skb1->sk) 4480716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4481716ea3a7SDavid Howells 4482716ea3a7SDavid Howells /* Looking around. Are we still alive? 4483716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4484716ea3a7SDavid Howells 4485716ea3a7SDavid Howells skb2->next = skb1->next; 4486716ea3a7SDavid Howells *skb_p = skb2; 4487716ea3a7SDavid Howells kfree_skb(skb1); 4488716ea3a7SDavid Howells skb1 = skb2; 4489716ea3a7SDavid Howells } 4490716ea3a7SDavid Howells elt++; 4491716ea3a7SDavid Howells *trailer = skb1; 4492716ea3a7SDavid Howells skb_p = &skb1->next; 4493716ea3a7SDavid Howells } 4494716ea3a7SDavid Howells 4495716ea3a7SDavid Howells return elt; 4496716ea3a7SDavid Howells } 4497b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4498716ea3a7SDavid Howells 4499b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4500b1faf566SEric Dumazet { 4501b1faf566SEric Dumazet struct sock *sk = skb->sk; 4502b1faf566SEric Dumazet 4503b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4504b1faf566SEric Dumazet } 4505b1faf566SEric Dumazet 45068605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 45078605330aSSoheil Hassas Yeganeh { 45088605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 45098605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 45108605330aSSoheil Hassas Yeganeh */ 45118605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 45128605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 45138605330aSSoheil Hassas Yeganeh } 45148605330aSSoheil Hassas Yeganeh 4515b1faf566SEric Dumazet /* 4516b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4517b1faf566SEric Dumazet */ 4518b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4519b1faf566SEric Dumazet { 4520b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4521ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4522b1faf566SEric Dumazet return -ENOMEM; 4523b1faf566SEric Dumazet 4524b1faf566SEric Dumazet skb_orphan(skb); 4525b1faf566SEric Dumazet skb->sk = sk; 4526b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4527b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 45288605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4529b1faf566SEric Dumazet 4530abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4531abb57ea4SEric Dumazet skb_dst_force(skb); 4532abb57ea4SEric Dumazet 4533b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4534b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 45356e5d58fdSVinicius Costa Gomes sk->sk_error_report(sk); 4536b1faf566SEric Dumazet return 0; 4537b1faf566SEric Dumazet } 4538b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4539b1faf566SEric Dumazet 454083a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 454183a1a1a7SSoheil Hassas Yeganeh { 454283a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 454383a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 454483a1a1a7SSoheil Hassas Yeganeh } 454583a1a1a7SSoheil Hassas Yeganeh 4546364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4547364a9e93SWillem de Bruijn { 4548364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 454983a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 455083a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4551997d5c3fSEric Dumazet unsigned long flags; 4552364a9e93SWillem de Bruijn 4553997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4554364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 455538b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 455683a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 455738b25793SSoheil Hassas Yeganeh if (icmp_next) 455838b25793SSoheil Hassas Yeganeh sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 455938b25793SSoheil Hassas Yeganeh } 4560997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4561364a9e93SWillem de Bruijn 456283a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 456383a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 456483a1a1a7SSoheil Hassas Yeganeh 456583a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4566364a9e93SWillem de Bruijn sk->sk_error_report(sk); 4567364a9e93SWillem de Bruijn 4568364a9e93SWillem de Bruijn return skb; 4569364a9e93SWillem de Bruijn } 4570364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4571364a9e93SWillem de Bruijn 4572cab41c47SAlexander Duyck /** 4573cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4574cab41c47SAlexander Duyck * @skb: the skb to clone 4575cab41c47SAlexander Duyck * 4576cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4577cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4578cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4579cab41c47SAlexander Duyck * 4580cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4581cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4582cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4583cab41c47SAlexander Duyck * the sk_error_queue. 4584cab41c47SAlexander Duyck */ 458562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 458662bccb8cSAlexander Duyck { 458762bccb8cSAlexander Duyck struct sock *sk = skb->sk; 458862bccb8cSAlexander Duyck struct sk_buff *clone; 458962bccb8cSAlexander Duyck 459041c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 459162bccb8cSAlexander Duyck return NULL; 459262bccb8cSAlexander Duyck 459362bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 459462bccb8cSAlexander Duyck if (!clone) { 459562bccb8cSAlexander Duyck sock_put(sk); 459662bccb8cSAlexander Duyck return NULL; 459762bccb8cSAlexander Duyck } 459862bccb8cSAlexander Duyck 459962bccb8cSAlexander Duyck clone->sk = sk; 460062bccb8cSAlexander Duyck clone->destructor = sock_efree; 460162bccb8cSAlexander Duyck 460262bccb8cSAlexander Duyck return clone; 460362bccb8cSAlexander Duyck } 460462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 460562bccb8cSAlexander Duyck 460637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 460737846ef0SAlexander Duyck struct sock *sk, 46084ef1b286SSoheil Hassas Yeganeh int tstype, 46094ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4610ac45f602SPatrick Ohly { 4611ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4612ac45f602SPatrick Ohly int err; 4613ac45f602SPatrick Ohly 46144ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 46154ef1b286SSoheil Hassas Yeganeh 4616ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4617ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4618ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4619ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4620e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 46214ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 46221862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 46234ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 462409c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4625ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4626ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 46274ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 46284ed2d765SWillem de Bruijn } 462929030374SEric Dumazet 4630ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 463129030374SEric Dumazet 4632ac45f602SPatrick Ohly if (err) 4633ac45f602SPatrick Ohly kfree_skb(skb); 4634ac45f602SPatrick Ohly } 463537846ef0SAlexander Duyck 4636b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4637b245be1fSWillem de Bruijn { 4638b245be1fSWillem de Bruijn bool ret; 4639b245be1fSWillem de Bruijn 4640b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4641b245be1fSWillem de Bruijn return true; 4642b245be1fSWillem de Bruijn 4643b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4644b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4645b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4646b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4647b245be1fSWillem de Bruijn return ret; 4648b245be1fSWillem de Bruijn } 4649b245be1fSWillem de Bruijn 465037846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 465137846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 465237846ef0SAlexander Duyck { 465337846ef0SAlexander Duyck struct sock *sk = skb->sk; 465437846ef0SAlexander Duyck 4655b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 465635b99dffSWillem de Bruijn goto err; 4657b245be1fSWillem de Bruijn 46589ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 46599ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 46609ac25fc0SEric Dumazet */ 466141c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 466237846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 46634ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 466437846ef0SAlexander Duyck sock_put(sk); 466535b99dffSWillem de Bruijn return; 466637846ef0SAlexander Duyck } 466735b99dffSWillem de Bruijn 466835b99dffSWillem de Bruijn err: 466935b99dffSWillem de Bruijn kfree_skb(skb); 46709ac25fc0SEric Dumazet } 467137846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 467237846ef0SAlexander Duyck 467337846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 467437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 467537846ef0SAlexander Duyck struct sock *sk, int tstype) 467637846ef0SAlexander Duyck { 467737846ef0SAlexander Duyck struct sk_buff *skb; 46784ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 467937846ef0SAlexander Duyck 46803a8dd971SWillem de Bruijn if (!sk) 46813a8dd971SWillem de Bruijn return; 46823a8dd971SWillem de Bruijn 4683b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4684b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4685b50a5c70SMiroslav Lichvar return; 4686b50a5c70SMiroslav Lichvar 46873a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 46883a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 468937846ef0SAlexander Duyck return; 469037846ef0SAlexander Duyck 46911c885808SFrancis Yan if (tsonly) { 46921c885808SFrancis Yan #ifdef CONFIG_INET 46931c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 46941c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 46954ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 46961c885808SFrancis Yan skb = tcp_get_timestamping_opt_stats(sk); 46974ef1b286SSoheil Hassas Yeganeh opt_stats = true; 46984ef1b286SSoheil Hassas Yeganeh } else 46991c885808SFrancis Yan #endif 47001c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 47011c885808SFrancis Yan } else { 470237846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 47031c885808SFrancis Yan } 470437846ef0SAlexander Duyck if (!skb) 470537846ef0SAlexander Duyck return; 470637846ef0SAlexander Duyck 470749ca0d8bSWillem de Bruijn if (tsonly) { 4708fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4709fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 471049ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 471149ca0d8bSWillem de Bruijn } 471249ca0d8bSWillem de Bruijn 471349ca0d8bSWillem de Bruijn if (hwtstamps) 471449ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 471549ca0d8bSWillem de Bruijn else 471649ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 471749ca0d8bSWillem de Bruijn 47184ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 471937846ef0SAlexander Duyck } 4720e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4721e7fd2885SWillem de Bruijn 4722e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4723e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4724e7fd2885SWillem de Bruijn { 4725e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4726e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4727e7fd2885SWillem de Bruijn } 4728ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4729ac45f602SPatrick Ohly 47306e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 47316e3e939fSJohannes Berg { 47326e3e939fSJohannes Berg struct sock *sk = skb->sk; 47336e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4734dd4f1072SEric Dumazet int err = 1; 47356e3e939fSJohannes Berg 47366e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 47376e3e939fSJohannes Berg skb->wifi_acked = acked; 47386e3e939fSJohannes Berg 47396e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 47406e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 47416e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 47426e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 47436e3e939fSJohannes Berg 4744dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4745dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4746dd4f1072SEric Dumazet */ 474741c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 47486e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4749dd4f1072SEric Dumazet sock_put(sk); 4750dd4f1072SEric Dumazet } 47516e3e939fSJohannes Berg if (err) 47526e3e939fSJohannes Berg kfree_skb(skb); 47536e3e939fSJohannes Berg } 47546e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 47556e3e939fSJohannes Berg 4756f35d9d8aSRusty Russell /** 4757f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4758f35d9d8aSRusty Russell * @skb: the skb to set 4759f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4760f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4761f35d9d8aSRusty Russell * 4762f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4763f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4764f35d9d8aSRusty Russell * 4765f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4766f35d9d8aSRusty Russell * returns false you should drop the packet. 4767f35d9d8aSRusty Russell */ 4768f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4769f35d9d8aSRusty Russell { 477052b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 477152b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 477252b5d6f5SEric Dumazet 477352b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 477452b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 477552b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4776f35d9d8aSRusty Russell return false; 4777f35d9d8aSRusty Russell } 4778f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 477952b5d6f5SEric Dumazet skb->csum_start = csum_start; 4780f35d9d8aSRusty Russell skb->csum_offset = off; 4781e5d5decaSJason Wang skb_set_transport_header(skb, start); 4782f35d9d8aSRusty Russell return true; 4783f35d9d8aSRusty Russell } 4784b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4785f35d9d8aSRusty Russell 4786ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4787ed1f50c3SPaul Durrant unsigned int max) 4788ed1f50c3SPaul Durrant { 4789ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4790ed1f50c3SPaul Durrant return 0; 4791ed1f50c3SPaul Durrant 4792ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4793ed1f50c3SPaul Durrant * won't need to do it again. 4794ed1f50c3SPaul Durrant */ 4795ed1f50c3SPaul Durrant if (max > skb->len) 4796ed1f50c3SPaul Durrant max = skb->len; 4797ed1f50c3SPaul Durrant 4798ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4799ed1f50c3SPaul Durrant return -ENOMEM; 4800ed1f50c3SPaul Durrant 4801ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4802ed1f50c3SPaul Durrant return -EPROTO; 4803ed1f50c3SPaul Durrant 4804ed1f50c3SPaul Durrant return 0; 4805ed1f50c3SPaul Durrant } 4806ed1f50c3SPaul Durrant 4807f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4808f9708b43SJan Beulich 4809f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4810f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4811f9708b43SJan Beulich unsigned int off) 4812f9708b43SJan Beulich { 4813f9708b43SJan Beulich int err; 4814f9708b43SJan Beulich 4815161d1792SKees Cook switch (proto) { 4816f9708b43SJan Beulich case IPPROTO_TCP: 4817f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4818f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4819f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4820f9708b43SJan Beulich offsetof(struct tcphdr, 4821f9708b43SJan Beulich check))) 4822f9708b43SJan Beulich err = -EPROTO; 4823f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4824f9708b43SJan Beulich 4825f9708b43SJan Beulich case IPPROTO_UDP: 4826f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4827f9708b43SJan Beulich off + sizeof(struct udphdr)); 4828f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4829f9708b43SJan Beulich offsetof(struct udphdr, 4830f9708b43SJan Beulich check))) 4831f9708b43SJan Beulich err = -EPROTO; 4832f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4833f9708b43SJan Beulich } 4834f9708b43SJan Beulich 4835f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4836f9708b43SJan Beulich } 4837f9708b43SJan Beulich 4838ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4839ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4840ed1f50c3SPaul Durrant */ 4841ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4842ed1f50c3SPaul Durrant 4843f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4844ed1f50c3SPaul Durrant { 4845ed1f50c3SPaul Durrant unsigned int off; 4846ed1f50c3SPaul Durrant bool fragment; 4847f9708b43SJan Beulich __sum16 *csum; 4848ed1f50c3SPaul Durrant int err; 4849ed1f50c3SPaul Durrant 4850ed1f50c3SPaul Durrant fragment = false; 4851ed1f50c3SPaul Durrant 4852ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4853ed1f50c3SPaul Durrant sizeof(struct iphdr), 4854ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4855ed1f50c3SPaul Durrant if (err < 0) 4856ed1f50c3SPaul Durrant goto out; 4857ed1f50c3SPaul Durrant 4858ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4859ed1f50c3SPaul Durrant fragment = true; 4860ed1f50c3SPaul Durrant 4861ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4862ed1f50c3SPaul Durrant 4863ed1f50c3SPaul Durrant err = -EPROTO; 4864ed1f50c3SPaul Durrant 4865ed1f50c3SPaul Durrant if (fragment) 4866ed1f50c3SPaul Durrant goto out; 4867ed1f50c3SPaul Durrant 4868f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4869f9708b43SJan Beulich if (IS_ERR(csum)) 4870f9708b43SJan Beulich return PTR_ERR(csum); 4871ed1f50c3SPaul Durrant 4872ed1f50c3SPaul Durrant if (recalculate) 4873f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4874ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4875ed1f50c3SPaul Durrant skb->len - off, 4876f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4877ed1f50c3SPaul Durrant err = 0; 4878ed1f50c3SPaul Durrant 4879ed1f50c3SPaul Durrant out: 4880ed1f50c3SPaul Durrant return err; 4881ed1f50c3SPaul Durrant } 4882ed1f50c3SPaul Durrant 4883ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4884ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4885ed1f50c3SPaul Durrant */ 4886ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4887ed1f50c3SPaul Durrant 4888ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4889ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4890ed1f50c3SPaul Durrant 4891ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4892ed1f50c3SPaul Durrant { 4893ed1f50c3SPaul Durrant int err; 4894ed1f50c3SPaul Durrant u8 nexthdr; 4895ed1f50c3SPaul Durrant unsigned int off; 4896ed1f50c3SPaul Durrant unsigned int len; 4897ed1f50c3SPaul Durrant bool fragment; 4898ed1f50c3SPaul Durrant bool done; 4899f9708b43SJan Beulich __sum16 *csum; 4900ed1f50c3SPaul Durrant 4901ed1f50c3SPaul Durrant fragment = false; 4902ed1f50c3SPaul Durrant done = false; 4903ed1f50c3SPaul Durrant 4904ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4905ed1f50c3SPaul Durrant 4906ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4907ed1f50c3SPaul Durrant if (err < 0) 4908ed1f50c3SPaul Durrant goto out; 4909ed1f50c3SPaul Durrant 4910ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4911ed1f50c3SPaul Durrant 4912ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4913ed1f50c3SPaul Durrant while (off <= len && !done) { 4914ed1f50c3SPaul Durrant switch (nexthdr) { 4915ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4916ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4917ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4918ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4919ed1f50c3SPaul Durrant 4920ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4921ed1f50c3SPaul Durrant off + 4922ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4923ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4924ed1f50c3SPaul Durrant if (err < 0) 4925ed1f50c3SPaul Durrant goto out; 4926ed1f50c3SPaul Durrant 4927ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4928ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4929ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4930ed1f50c3SPaul Durrant break; 4931ed1f50c3SPaul Durrant } 4932ed1f50c3SPaul Durrant case IPPROTO_AH: { 4933ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4934ed1f50c3SPaul Durrant 4935ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4936ed1f50c3SPaul Durrant off + 4937ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4938ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4939ed1f50c3SPaul Durrant if (err < 0) 4940ed1f50c3SPaul Durrant goto out; 4941ed1f50c3SPaul Durrant 4942ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4943ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4944ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4945ed1f50c3SPaul Durrant break; 4946ed1f50c3SPaul Durrant } 4947ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4948ed1f50c3SPaul Durrant struct frag_hdr *hp; 4949ed1f50c3SPaul Durrant 4950ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4951ed1f50c3SPaul Durrant off + 4952ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4953ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4954ed1f50c3SPaul Durrant if (err < 0) 4955ed1f50c3SPaul Durrant goto out; 4956ed1f50c3SPaul Durrant 4957ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4958ed1f50c3SPaul Durrant 4959ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4960ed1f50c3SPaul Durrant fragment = true; 4961ed1f50c3SPaul Durrant 4962ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4963ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4964ed1f50c3SPaul Durrant break; 4965ed1f50c3SPaul Durrant } 4966ed1f50c3SPaul Durrant default: 4967ed1f50c3SPaul Durrant done = true; 4968ed1f50c3SPaul Durrant break; 4969ed1f50c3SPaul Durrant } 4970ed1f50c3SPaul Durrant } 4971ed1f50c3SPaul Durrant 4972ed1f50c3SPaul Durrant err = -EPROTO; 4973ed1f50c3SPaul Durrant 4974ed1f50c3SPaul Durrant if (!done || fragment) 4975ed1f50c3SPaul Durrant goto out; 4976ed1f50c3SPaul Durrant 4977f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4978f9708b43SJan Beulich if (IS_ERR(csum)) 4979f9708b43SJan Beulich return PTR_ERR(csum); 4980ed1f50c3SPaul Durrant 4981ed1f50c3SPaul Durrant if (recalculate) 4982f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4983ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4984f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4985ed1f50c3SPaul Durrant err = 0; 4986ed1f50c3SPaul Durrant 4987ed1f50c3SPaul Durrant out: 4988ed1f50c3SPaul Durrant return err; 4989ed1f50c3SPaul Durrant } 4990ed1f50c3SPaul Durrant 4991ed1f50c3SPaul Durrant /** 4992ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4993ed1f50c3SPaul Durrant * @skb: the skb to set up 4994ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4995ed1f50c3SPaul Durrant */ 4996ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4997ed1f50c3SPaul Durrant { 4998ed1f50c3SPaul Durrant int err; 4999ed1f50c3SPaul Durrant 5000ed1f50c3SPaul Durrant switch (skb->protocol) { 5001ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5002f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5003ed1f50c3SPaul Durrant break; 5004ed1f50c3SPaul Durrant 5005ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5006ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5007ed1f50c3SPaul Durrant break; 5008ed1f50c3SPaul Durrant 5009ed1f50c3SPaul Durrant default: 5010ed1f50c3SPaul Durrant err = -EPROTO; 5011ed1f50c3SPaul Durrant break; 5012ed1f50c3SPaul Durrant } 5013ed1f50c3SPaul Durrant 5014ed1f50c3SPaul Durrant return err; 5015ed1f50c3SPaul Durrant } 5016ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5017ed1f50c3SPaul Durrant 50189afd85c9SLinus Lüssing /** 50199afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 50209afd85c9SLinus Lüssing * @skb: the skb to check 50219afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 50229afd85c9SLinus Lüssing * 50239afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 50249afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 50259afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 50269afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 50279afd85c9SLinus Lüssing * 5028a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5029a516993fSLinus Lüssing * differs from the provided skb. 50309afd85c9SLinus Lüssing */ 50319afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 50329afd85c9SLinus Lüssing unsigned int transport_len) 50339afd85c9SLinus Lüssing { 50349afd85c9SLinus Lüssing struct sk_buff *skb_chk; 50359afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 50369afd85c9SLinus Lüssing int ret; 50379afd85c9SLinus Lüssing 5038a516993fSLinus Lüssing if (skb->len < len) 50399afd85c9SLinus Lüssing return NULL; 5040a516993fSLinus Lüssing else if (skb->len == len) 50419afd85c9SLinus Lüssing return skb; 50429afd85c9SLinus Lüssing 50439afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 50449afd85c9SLinus Lüssing if (!skb_chk) 50459afd85c9SLinus Lüssing return NULL; 50469afd85c9SLinus Lüssing 50479afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 50489afd85c9SLinus Lüssing if (ret) { 50499afd85c9SLinus Lüssing kfree_skb(skb_chk); 50509afd85c9SLinus Lüssing return NULL; 50519afd85c9SLinus Lüssing } 50529afd85c9SLinus Lüssing 50539afd85c9SLinus Lüssing return skb_chk; 50549afd85c9SLinus Lüssing } 50559afd85c9SLinus Lüssing 50569afd85c9SLinus Lüssing /** 50579afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 50589afd85c9SLinus Lüssing * @skb: the skb to check 50599afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 50609afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 50619afd85c9SLinus Lüssing * 50629afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 50639afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 50649afd85c9SLinus Lüssing * 50659afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 50669afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 50679afd85c9SLinus Lüssing * 5068a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5069a516993fSLinus Lüssing * differs from the provided skb. 50709afd85c9SLinus Lüssing */ 50719afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 50729afd85c9SLinus Lüssing unsigned int transport_len, 50739afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 50749afd85c9SLinus Lüssing { 50759afd85c9SLinus Lüssing struct sk_buff *skb_chk; 50769afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5077fcba67c9SLinus Lüssing __sum16 ret; 50789afd85c9SLinus Lüssing 50799afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 50809afd85c9SLinus Lüssing if (!skb_chk) 5081a516993fSLinus Lüssing goto err; 50829afd85c9SLinus Lüssing 5083a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5084a516993fSLinus Lüssing goto err; 50859afd85c9SLinus Lüssing 50869b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 50879afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 50889b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 50899afd85c9SLinus Lüssing 5090a516993fSLinus Lüssing if (ret) 5091a516993fSLinus Lüssing goto err; 50929afd85c9SLinus Lüssing 50939afd85c9SLinus Lüssing return skb_chk; 5094a516993fSLinus Lüssing 5095a516993fSLinus Lüssing err: 5096a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5097a516993fSLinus Lüssing kfree_skb(skb_chk); 5098a516993fSLinus Lüssing 5099a516993fSLinus Lüssing return NULL; 5100a516993fSLinus Lüssing 51019afd85c9SLinus Lüssing } 51029afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 51039afd85c9SLinus Lüssing 51044497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 51054497b076SBen Hutchings { 5106e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5107e87cc472SJoe Perches skb->dev->name); 51084497b076SBen Hutchings } 51094497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5110bad43ca8SEric Dumazet 5111bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5112bad43ca8SEric Dumazet { 51133d861f66SEric Dumazet if (head_stolen) { 51143d861f66SEric Dumazet skb_release_head_state(skb); 5115bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 51163d861f66SEric Dumazet } else { 5117bad43ca8SEric Dumazet __kfree_skb(skb); 5118bad43ca8SEric Dumazet } 51193d861f66SEric Dumazet } 5120bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5121bad43ca8SEric Dumazet 5122bad43ca8SEric Dumazet /** 5123bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5124bad43ca8SEric Dumazet * @to: prior buffer 5125bad43ca8SEric Dumazet * @from: buffer to add 5126bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5127c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5128bad43ca8SEric Dumazet */ 5129bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5130bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5131bad43ca8SEric Dumazet { 5132c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5133bad43ca8SEric Dumazet int i, delta, len = from->len; 5134bad43ca8SEric Dumazet 5135bad43ca8SEric Dumazet *fragstolen = false; 5136bad43ca8SEric Dumazet 5137bad43ca8SEric Dumazet if (skb_cloned(to)) 5138bad43ca8SEric Dumazet return false; 5139bad43ca8SEric Dumazet 5140bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5141e93a0435SEric Dumazet if (len) 5142bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5143bad43ca8SEric Dumazet *delta_truesize = 0; 5144bad43ca8SEric Dumazet return true; 5145bad43ca8SEric Dumazet } 5146bad43ca8SEric Dumazet 5147c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5148c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5149c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5150bad43ca8SEric Dumazet return false; 51511f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 51521f8b977aSWillem de Bruijn return false; 5153bad43ca8SEric Dumazet 5154bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5155bad43ca8SEric Dumazet struct page *page; 5156bad43ca8SEric Dumazet unsigned int offset; 5157bad43ca8SEric Dumazet 5158c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5159c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5160bad43ca8SEric Dumazet return false; 5161bad43ca8SEric Dumazet 5162bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5163bad43ca8SEric Dumazet return false; 5164bad43ca8SEric Dumazet 5165bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5166bad43ca8SEric Dumazet 5167bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5168bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5169bad43ca8SEric Dumazet 5170c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5171bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5172bad43ca8SEric Dumazet *fragstolen = true; 5173bad43ca8SEric Dumazet } else { 5174c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5175c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5176bad43ca8SEric Dumazet return false; 5177bad43ca8SEric Dumazet 5178f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5179bad43ca8SEric Dumazet } 5180bad43ca8SEric Dumazet 5181bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5182bad43ca8SEric Dumazet 5183c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5184c818fa9eSEric Dumazet from_shinfo->frags, 5185c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5186c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5187bad43ca8SEric Dumazet 5188bad43ca8SEric Dumazet if (!skb_cloned(from)) 5189c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5190bad43ca8SEric Dumazet 51918ea853fdSLi RongQing /* if the skb is not cloned this does nothing 51928ea853fdSLi RongQing * since we set nr_frags to 0. 51938ea853fdSLi RongQing */ 5194c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5195c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5196bad43ca8SEric Dumazet 5197bad43ca8SEric Dumazet to->truesize += delta; 5198bad43ca8SEric Dumazet to->len += len; 5199bad43ca8SEric Dumazet to->data_len += len; 5200bad43ca8SEric Dumazet 5201bad43ca8SEric Dumazet *delta_truesize = delta; 5202bad43ca8SEric Dumazet return true; 5203bad43ca8SEric Dumazet } 5204bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5205621e84d6SNicolas Dichtel 5206621e84d6SNicolas Dichtel /** 52078b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5208621e84d6SNicolas Dichtel * 5209621e84d6SNicolas Dichtel * @skb: buffer to clean 52108b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5211621e84d6SNicolas Dichtel * 52128b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 52138b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 52148b27f277SNicolas Dichtel * operations. 52158b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 52168b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 52178b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5218621e84d6SNicolas Dichtel */ 52198b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5220621e84d6SNicolas Dichtel { 5221621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5222621e84d6SNicolas Dichtel skb->skb_iif = 0; 522360ff7467SWANG Cong skb->ignore_df = 0; 5224621e84d6SNicolas Dichtel skb_dst_drop(skb); 5225174e2381SFlorian Westphal skb_ext_reset(skb); 5226895b5c9fSFlorian Westphal nf_reset_ct(skb); 5227621e84d6SNicolas Dichtel nf_reset_trace(skb); 5228213dd74aSHerbert Xu 52296f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 52306f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5231875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 52326f9a5069SPetr Machata #endif 52336f9a5069SPetr Machata 5234213dd74aSHerbert Xu if (!xnet) 5235213dd74aSHerbert Xu return; 5236213dd74aSHerbert Xu 52372b5ec1a5SYe Yin ipvs_reset(skb); 5238213dd74aSHerbert Xu skb->mark = 0; 5239c47d8c2fSJesus Sanchez-Palencia skb->tstamp = 0; 5240621e84d6SNicolas Dichtel } 5241621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5242de960aa9SFlorian Westphal 5243de960aa9SFlorian Westphal /** 5244de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5245de960aa9SFlorian Westphal * 5246de960aa9SFlorian Westphal * @skb: GSO skb 5247de960aa9SFlorian Westphal * 5248de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5249de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5250de960aa9SFlorian Westphal * 5251de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5252de960aa9SFlorian Westphal */ 5253a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5254de960aa9SFlorian Westphal { 5255de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5256f993bc25SFlorian Westphal unsigned int thlen = 0; 5257f993bc25SFlorian Westphal 5258f993bc25SFlorian Westphal if (skb->encapsulation) { 5259f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5260f993bc25SFlorian Westphal skb_transport_header(skb); 5261de960aa9SFlorian Westphal 5262de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5263f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5264f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5265f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 52661dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 526790017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5268ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5269ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5270f993bc25SFlorian Westphal } 52716d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 52726d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 52736d39d589SFlorian Westphal * accounted for. 52746d39d589SFlorian Westphal */ 5275f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5276de960aa9SFlorian Westphal } 5277a4a77718SDaniel Axtens 5278a4a77718SDaniel Axtens /** 5279a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5280a4a77718SDaniel Axtens * 5281a4a77718SDaniel Axtens * @skb: GSO skb 5282a4a77718SDaniel Axtens * 5283a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5284a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5285a4a77718SDaniel Axtens * 5286a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5287a4a77718SDaniel Axtens */ 5288a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5289a4a77718SDaniel Axtens { 5290a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5291a4a77718SDaniel Axtens skb_network_header(skb); 5292a4a77718SDaniel Axtens 5293a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5294a4a77718SDaniel Axtens } 5295a4a77718SDaniel Axtens 5296a4a77718SDaniel Axtens /** 5297a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5298a4a77718SDaniel Axtens * 5299a4a77718SDaniel Axtens * @skb: GSO skb 5300a4a77718SDaniel Axtens * 5301a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5302a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5303a4a77718SDaniel Axtens * headers (TCP/UDP). 5304a4a77718SDaniel Axtens */ 5305a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5306a4a77718SDaniel Axtens { 5307a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5308a4a77718SDaniel Axtens 5309a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5310a4a77718SDaniel Axtens } 53110d5501c1SVlad Yasevich 5312ae7ef81eSMarcelo Ricardo Leitner /** 53132b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 53142b16f048SDaniel Axtens * 53152b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 53162b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 53172b16f048SDaniel Axtens * 53182b16f048SDaniel Axtens * We might want to check: 53192b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 53202b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 53212b16f048SDaniel Axtens * 53222b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 53232b16f048SDaniel Axtens * 532449682bfaSMathieu Malaterre * @skb: GSO skb 532549682bfaSMathieu Malaterre * 53262b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 53272b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 53282b16f048SDaniel Axtens * 53292b16f048SDaniel Axtens * @max_len: The maximum permissible length. 53302b16f048SDaniel Axtens * 53312b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 53322b16f048SDaniel Axtens */ 53332b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 53342b16f048SDaniel Axtens unsigned int seg_len, 53352b16f048SDaniel Axtens unsigned int max_len) { 53362b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 53372b16f048SDaniel Axtens const struct sk_buff *iter; 53382b16f048SDaniel Axtens 53392b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 53402b16f048SDaniel Axtens return seg_len <= max_len; 53412b16f048SDaniel Axtens 53422b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 53432b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 53442b16f048SDaniel Axtens 53452b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 53462b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 53472b16f048SDaniel Axtens return false; 53482b16f048SDaniel Axtens } 53492b16f048SDaniel Axtens 53502b16f048SDaniel Axtens return true; 53512b16f048SDaniel Axtens } 53522b16f048SDaniel Axtens 53532b16f048SDaniel Axtens /** 5354779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5355ae7ef81eSMarcelo Ricardo Leitner * 5356ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 535776f21b99SDavid S. Miller * @mtu: MTU to validate against 5358ae7ef81eSMarcelo Ricardo Leitner * 5359779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5360779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5361779b7931SDaniel Axtens * payload. 5362ae7ef81eSMarcelo Ricardo Leitner */ 5363779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5364ae7ef81eSMarcelo Ricardo Leitner { 53652b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5366ae7ef81eSMarcelo Ricardo Leitner } 5367779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5368ae7ef81eSMarcelo Ricardo Leitner 53692b16f048SDaniel Axtens /** 53702b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 53712b16f048SDaniel Axtens * 53722b16f048SDaniel Axtens * @skb: GSO skb 53732b16f048SDaniel Axtens * @len: length to validate against 53742b16f048SDaniel Axtens * 53752b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 53762b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 53772b16f048SDaniel Axtens */ 53782b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 53792b16f048SDaniel Axtens { 53802b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 53812b16f048SDaniel Axtens } 53822b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 53832b16f048SDaniel Axtens 53840d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 53850d5501c1SVlad Yasevich { 5386d85e8be2SYuya Kusakabe int mac_len, meta_len; 5387d85e8be2SYuya Kusakabe void *meta; 53884bbb3e0eSToshiaki Makita 53890d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 53900d5501c1SVlad Yasevich kfree_skb(skb); 53910d5501c1SVlad Yasevich return NULL; 53920d5501c1SVlad Yasevich } 53930d5501c1SVlad Yasevich 53944bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5395ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 53964bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 53974bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5398ae474573SToshiaki Makita } 5399d85e8be2SYuya Kusakabe 5400d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5401d85e8be2SYuya Kusakabe if (meta_len) { 5402d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5403d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5404d85e8be2SYuya Kusakabe } 5405d85e8be2SYuya Kusakabe 54060d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 54070d5501c1SVlad Yasevich return skb; 54080d5501c1SVlad Yasevich } 54090d5501c1SVlad Yasevich 54100d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 54110d5501c1SVlad Yasevich { 54120d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 54130d5501c1SVlad Yasevich u16 vlan_tci; 54140d5501c1SVlad Yasevich 5415df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 54160d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 54170d5501c1SVlad Yasevich return skb; 54180d5501c1SVlad Yasevich } 54190d5501c1SVlad Yasevich 54200d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 54210d5501c1SVlad Yasevich if (unlikely(!skb)) 54220d5501c1SVlad Yasevich goto err_free; 54230d5501c1SVlad Yasevich 54240d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 54250d5501c1SVlad Yasevich goto err_free; 54260d5501c1SVlad Yasevich 54270d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 54280d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 54290d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 54300d5501c1SVlad Yasevich 54310d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 54320d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 54330d5501c1SVlad Yasevich 54340d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 54350d5501c1SVlad Yasevich if (unlikely(!skb)) 54360d5501c1SVlad Yasevich goto err_free; 54370d5501c1SVlad Yasevich 54380d5501c1SVlad Yasevich skb_reset_network_header(skb); 54390d5501c1SVlad Yasevich skb_reset_transport_header(skb); 54400d5501c1SVlad Yasevich skb_reset_mac_len(skb); 54410d5501c1SVlad Yasevich 54420d5501c1SVlad Yasevich return skb; 54430d5501c1SVlad Yasevich 54440d5501c1SVlad Yasevich err_free: 54450d5501c1SVlad Yasevich kfree_skb(skb); 54460d5501c1SVlad Yasevich return NULL; 54470d5501c1SVlad Yasevich } 54480d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 54492e4e4410SEric Dumazet 5450e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 5451e2195121SJiri Pirko { 5452e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5453e2195121SJiri Pirko return -ENOMEM; 5454e2195121SJiri Pirko 5455e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5456e2195121SJiri Pirko return 0; 5457e2195121SJiri Pirko 5458e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5459e2195121SJiri Pirko } 5460e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5461e2195121SJiri Pirko 5462bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5463bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5464bfca4c52SShmulik Ladkani */ 5465bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 546693515d53SJiri Pirko { 546793515d53SJiri Pirko struct vlan_hdr *vhdr; 5468b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 546993515d53SJiri Pirko int err; 547093515d53SJiri Pirko 5471b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5472b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5473b6a79208SShmulik Ladkani offset)) { 5474b6a79208SShmulik Ladkani return -EINVAL; 5475b6a79208SShmulik Ladkani } 5476b6a79208SShmulik Ladkani 547793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 547893515d53SJiri Pirko if (unlikely(err)) 5479b6a79208SShmulik Ladkani return err; 548093515d53SJiri Pirko 548193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 548293515d53SJiri Pirko 548393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 548493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 548593515d53SJiri Pirko 548693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 548793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 548893515d53SJiri Pirko 548993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 549093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 549193515d53SJiri Pirko 549293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 549393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 549493515d53SJiri Pirko 549593515d53SJiri Pirko skb_reset_mac_len(skb); 549693515d53SJiri Pirko 549793515d53SJiri Pirko return err; 549893515d53SJiri Pirko } 5499bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 550093515d53SJiri Pirko 5501b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5502b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5503b6a79208SShmulik Ladkani */ 550493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 550593515d53SJiri Pirko { 550693515d53SJiri Pirko u16 vlan_tci; 550793515d53SJiri Pirko __be16 vlan_proto; 550893515d53SJiri Pirko int err; 550993515d53SJiri Pirko 5510df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5511b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 551293515d53SJiri Pirko } else { 5513ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 551493515d53SJiri Pirko return 0; 551593515d53SJiri Pirko 551693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 551793515d53SJiri Pirko if (err) 551893515d53SJiri Pirko return err; 551993515d53SJiri Pirko } 552093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5521ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 552293515d53SJiri Pirko return 0; 552393515d53SJiri Pirko 552493515d53SJiri Pirko vlan_proto = skb->protocol; 552593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 552693515d53SJiri Pirko if (unlikely(err)) 552793515d53SJiri Pirko return err; 552893515d53SJiri Pirko 552993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 553093515d53SJiri Pirko return 0; 553193515d53SJiri Pirko } 553293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 553393515d53SJiri Pirko 5534b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5535b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5536b6a79208SShmulik Ladkani */ 553793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 553893515d53SJiri Pirko { 5539df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5540b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 554193515d53SJiri Pirko int err; 554293515d53SJiri Pirko 5543b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5544b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5545b6a79208SShmulik Ladkani offset)) { 5546b6a79208SShmulik Ladkani return -EINVAL; 5547b6a79208SShmulik Ladkani } 5548b6a79208SShmulik Ladkani 554993515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5550df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5551b6a79208SShmulik Ladkani if (err) 555293515d53SJiri Pirko return err; 55539241e2dfSDaniel Borkmann 555493515d53SJiri Pirko skb->protocol = skb->vlan_proto; 555593515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 555693515d53SJiri Pirko 55576b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 555893515d53SJiri Pirko } 555993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 556093515d53SJiri Pirko return 0; 556193515d53SJiri Pirko } 556293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 556393515d53SJiri Pirko 55648822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 55658822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 55668822e270SJohn Hurley __be16 ethertype) 55678822e270SJohn Hurley { 55688822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 55698822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 55708822e270SJohn Hurley 55718822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 55728822e270SJohn Hurley } 55738822e270SJohn Hurley 55748822e270SJohn Hurley hdr->h_proto = ethertype; 55758822e270SJohn Hurley } 55768822e270SJohn Hurley 55778822e270SJohn Hurley /** 5578e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5579e7dbfed1SMartin Varghese * the packet 55808822e270SJohn Hurley * 55818822e270SJohn Hurley * @skb: buffer 55828822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 55838822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5584fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 5585e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5586e7dbfed1SMartin Varghese * ethernet 55878822e270SJohn Hurley * 55888822e270SJohn Hurley * Expects skb->data at mac header. 55898822e270SJohn Hurley * 55908822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 55918822e270SJohn Hurley */ 5592fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5593d04ac224SMartin Varghese int mac_len, bool ethernet) 55948822e270SJohn Hurley { 55958822e270SJohn Hurley struct mpls_shim_hdr *lse; 55968822e270SJohn Hurley int err; 55978822e270SJohn Hurley 55988822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 55998822e270SJohn Hurley return -EINVAL; 56008822e270SJohn Hurley 56018822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 56028822e270SJohn Hurley if (skb->encapsulation) 56038822e270SJohn Hurley return -EINVAL; 56048822e270SJohn Hurley 56058822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 56068822e270SJohn Hurley if (unlikely(err)) 56078822e270SJohn Hurley return err; 56088822e270SJohn Hurley 56098822e270SJohn Hurley if (!skb->inner_protocol) { 5610e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 56118822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 56128822e270SJohn Hurley } 56138822e270SJohn Hurley 56148822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 56158822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5616fa4e0f88SDavide Caratti mac_len); 56178822e270SJohn Hurley skb_reset_mac_header(skb); 5618fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5619e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 56208822e270SJohn Hurley 56218822e270SJohn Hurley lse = mpls_hdr(skb); 56228822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 56238822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 56248822e270SJohn Hurley 5625d04ac224SMartin Varghese if (ethernet) 56268822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 56278822e270SJohn Hurley skb->protocol = mpls_proto; 56288822e270SJohn Hurley 56298822e270SJohn Hurley return 0; 56308822e270SJohn Hurley } 56318822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 56328822e270SJohn Hurley 56332e4e4410SEric Dumazet /** 5634ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 5635ed246ceeSJohn Hurley * 5636ed246ceeSJohn Hurley * @skb: buffer 5637ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 5638fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 563976f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 5640ed246ceeSJohn Hurley * 5641ed246ceeSJohn Hurley * Expects skb->data at mac header. 5642ed246ceeSJohn Hurley * 5643ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 5644ed246ceeSJohn Hurley */ 5645040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 5646040b5cfbSMartin Varghese bool ethernet) 5647ed246ceeSJohn Hurley { 5648ed246ceeSJohn Hurley int err; 5649ed246ceeSJohn Hurley 5650ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5651dedc5a08SDavide Caratti return 0; 5652ed246ceeSJohn Hurley 5653fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 5654ed246ceeSJohn Hurley if (unlikely(err)) 5655ed246ceeSJohn Hurley return err; 5656ed246ceeSJohn Hurley 5657ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 5658ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 5659fa4e0f88SDavide Caratti mac_len); 5660ed246ceeSJohn Hurley 5661ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 5662ed246ceeSJohn Hurley skb_reset_mac_header(skb); 5663fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5664ed246ceeSJohn Hurley 5665040b5cfbSMartin Varghese if (ethernet) { 5666ed246ceeSJohn Hurley struct ethhdr *hdr; 5667ed246ceeSJohn Hurley 5668ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 5669ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 5670ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 5671ed246ceeSJohn Hurley } 5672ed246ceeSJohn Hurley skb->protocol = next_proto; 5673ed246ceeSJohn Hurley 5674ed246ceeSJohn Hurley return 0; 5675ed246ceeSJohn Hurley } 5676ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 5677ed246ceeSJohn Hurley 5678ed246ceeSJohn Hurley /** 5679d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 5680d27cf5c5SJohn Hurley * 5681d27cf5c5SJohn Hurley * @skb: buffer 5682d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 5683d27cf5c5SJohn Hurley * 5684d27cf5c5SJohn Hurley * Expects skb->data at mac header. 5685d27cf5c5SJohn Hurley * 5686d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 5687d27cf5c5SJohn Hurley */ 5688d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 5689d27cf5c5SJohn Hurley { 5690d27cf5c5SJohn Hurley int err; 5691d27cf5c5SJohn Hurley 5692d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5693d27cf5c5SJohn Hurley return -EINVAL; 5694d27cf5c5SJohn Hurley 5695d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 5696d27cf5c5SJohn Hurley if (unlikely(err)) 5697d27cf5c5SJohn Hurley return err; 5698d27cf5c5SJohn Hurley 5699d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 5700d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 5701d27cf5c5SJohn Hurley 5702d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5703d27cf5c5SJohn Hurley } 5704d27cf5c5SJohn Hurley 5705d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 5706d27cf5c5SJohn Hurley 5707d27cf5c5SJohn Hurley return 0; 5708d27cf5c5SJohn Hurley } 5709d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 5710d27cf5c5SJohn Hurley 5711d27cf5c5SJohn Hurley /** 57122a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 57132a2ea508SJohn Hurley * 57142a2ea508SJohn Hurley * @skb: buffer 57152a2ea508SJohn Hurley * 57162a2ea508SJohn Hurley * Expects skb->data at mac header. 57172a2ea508SJohn Hurley * 57182a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 57192a2ea508SJohn Hurley */ 57202a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 57212a2ea508SJohn Hurley { 57222a2ea508SJohn Hurley u32 lse; 57232a2ea508SJohn Hurley u8 ttl; 57242a2ea508SJohn Hurley 57252a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 57262a2ea508SJohn Hurley return -EINVAL; 57272a2ea508SJohn Hurley 57282a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 57292a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 57302a2ea508SJohn Hurley if (!--ttl) 57312a2ea508SJohn Hurley return -EINVAL; 57322a2ea508SJohn Hurley 57332a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 57342a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 57352a2ea508SJohn Hurley 57362a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 57372a2ea508SJohn Hurley } 57382a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 57392a2ea508SJohn Hurley 57402a2ea508SJohn Hurley /** 57412e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 57422e4e4410SEric Dumazet * 5743de3f0d0eSMasanari Iida * @header_len: size of linear part 5744de3f0d0eSMasanari Iida * @data_len: needed length in frags 5745de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5746de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5747de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 57482e4e4410SEric Dumazet * 57492e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 57502e4e4410SEric Dumazet */ 57512e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 57522e4e4410SEric Dumazet unsigned long data_len, 57532e4e4410SEric Dumazet int max_page_order, 57542e4e4410SEric Dumazet int *errcode, 57552e4e4410SEric Dumazet gfp_t gfp_mask) 57562e4e4410SEric Dumazet { 57572e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 57582e4e4410SEric Dumazet unsigned long chunk; 57592e4e4410SEric Dumazet struct sk_buff *skb; 57602e4e4410SEric Dumazet struct page *page; 57612e4e4410SEric Dumazet int i; 57622e4e4410SEric Dumazet 57632e4e4410SEric Dumazet *errcode = -EMSGSIZE; 57642e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 57652e4e4410SEric Dumazet * high order pages... 57662e4e4410SEric Dumazet */ 57672e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 57682e4e4410SEric Dumazet return NULL; 57692e4e4410SEric Dumazet 57702e4e4410SEric Dumazet *errcode = -ENOBUFS; 5771f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 57722e4e4410SEric Dumazet if (!skb) 57732e4e4410SEric Dumazet return NULL; 57742e4e4410SEric Dumazet 57752e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 57762e4e4410SEric Dumazet 57772e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 57782e4e4410SEric Dumazet int order = max_page_order; 57792e4e4410SEric Dumazet 57802e4e4410SEric Dumazet while (order) { 57812e4e4410SEric Dumazet if (npages >= 1 << order) { 5782d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 57832e4e4410SEric Dumazet __GFP_COMP | 5784d14b56f5SMichal Hocko __GFP_NOWARN, 57852e4e4410SEric Dumazet order); 57862e4e4410SEric Dumazet if (page) 57872e4e4410SEric Dumazet goto fill_page; 57882e4e4410SEric Dumazet /* Do not retry other high order allocations */ 57892e4e4410SEric Dumazet order = 1; 57902e4e4410SEric Dumazet max_page_order = 0; 57912e4e4410SEric Dumazet } 57922e4e4410SEric Dumazet order--; 57932e4e4410SEric Dumazet } 57942e4e4410SEric Dumazet page = alloc_page(gfp_mask); 57952e4e4410SEric Dumazet if (!page) 57962e4e4410SEric Dumazet goto failure; 57972e4e4410SEric Dumazet fill_page: 57982e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 57992e4e4410SEric Dumazet PAGE_SIZE << order); 58002e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 58012e4e4410SEric Dumazet data_len -= chunk; 58022e4e4410SEric Dumazet npages -= 1 << order; 58032e4e4410SEric Dumazet } 58042e4e4410SEric Dumazet return skb; 58052e4e4410SEric Dumazet 58062e4e4410SEric Dumazet failure: 58072e4e4410SEric Dumazet kfree_skb(skb); 58082e4e4410SEric Dumazet return NULL; 58092e4e4410SEric Dumazet } 58102e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 58116fa01ccdSSowmini Varadhan 58126fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 58136fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 58146fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 58156fa01ccdSSowmini Varadhan { 58166fa01ccdSSowmini Varadhan int i; 58176fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 58186fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 58196fa01ccdSSowmini Varadhan u8 *data; 58206fa01ccdSSowmini Varadhan 58216fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 58226fa01ccdSSowmini Varadhan 58236fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 58246fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 58256fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 58266fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 58276fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 58286fa01ccdSSowmini Varadhan if (!data) 58296fa01ccdSSowmini Varadhan return -ENOMEM; 58306fa01ccdSSowmini Varadhan 58316fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 58326fa01ccdSSowmini Varadhan 58336fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 58346fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 58356fa01ccdSSowmini Varadhan skb->len -= off; 58366fa01ccdSSowmini Varadhan 58376fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 58386fa01ccdSSowmini Varadhan skb_shinfo(skb), 58396fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 58406fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 58416fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 58426fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 58436fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 58446fa01ccdSSowmini Varadhan kfree(data); 58456fa01ccdSSowmini Varadhan return -ENOMEM; 58466fa01ccdSSowmini Varadhan } 58476fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 58486fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 58496fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 58506fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 58516fa01ccdSSowmini Varadhan skb_release_data(skb); 58526fa01ccdSSowmini Varadhan } else { 58536fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 58546fa01ccdSSowmini Varadhan * relocate values 58556fa01ccdSSowmini Varadhan */ 58566fa01ccdSSowmini Varadhan skb_free_head(skb); 58576fa01ccdSSowmini Varadhan } 58586fa01ccdSSowmini Varadhan 58596fa01ccdSSowmini Varadhan skb->head = data; 58606fa01ccdSSowmini Varadhan skb->data = data; 58616fa01ccdSSowmini Varadhan skb->head_frag = 0; 58626fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 58636fa01ccdSSowmini Varadhan skb->end = size; 58646fa01ccdSSowmini Varadhan #else 58656fa01ccdSSowmini Varadhan skb->end = skb->head + size; 58666fa01ccdSSowmini Varadhan #endif 58676fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 58686fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 58696fa01ccdSSowmini Varadhan skb->cloned = 0; 58706fa01ccdSSowmini Varadhan skb->hdr_len = 0; 58716fa01ccdSSowmini Varadhan skb->nohdr = 0; 58726fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 58736fa01ccdSSowmini Varadhan 58746fa01ccdSSowmini Varadhan return 0; 58756fa01ccdSSowmini Varadhan } 58766fa01ccdSSowmini Varadhan 58776fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 58786fa01ccdSSowmini Varadhan 58796fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 58806fa01ccdSSowmini Varadhan * pskb_carve() 58816fa01ccdSSowmini Varadhan */ 58826fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 58836fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 58846fa01ccdSSowmini Varadhan gfp_t gfp_mask) 58856fa01ccdSSowmini Varadhan { 58866fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 58876fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 58886fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 58896fa01ccdSSowmini Varadhan 58906fa01ccdSSowmini Varadhan do { 58916fa01ccdSSowmini Varadhan if (!list) { 58926fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 58936fa01ccdSSowmini Varadhan return -EFAULT; 58946fa01ccdSSowmini Varadhan } 58956fa01ccdSSowmini Varadhan if (list->len <= eat) { 58966fa01ccdSSowmini Varadhan /* Eaten as whole. */ 58976fa01ccdSSowmini Varadhan eat -= list->len; 58986fa01ccdSSowmini Varadhan list = list->next; 58996fa01ccdSSowmini Varadhan insp = list; 59006fa01ccdSSowmini Varadhan } else { 59016fa01ccdSSowmini Varadhan /* Eaten partially. */ 59026fa01ccdSSowmini Varadhan if (skb_shared(list)) { 59036fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 59046fa01ccdSSowmini Varadhan if (!clone) 59056fa01ccdSSowmini Varadhan return -ENOMEM; 59066fa01ccdSSowmini Varadhan insp = list->next; 59076fa01ccdSSowmini Varadhan list = clone; 59086fa01ccdSSowmini Varadhan } else { 59096fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 59106fa01ccdSSowmini Varadhan insp = list; 59116fa01ccdSSowmini Varadhan } 59126fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 59136fa01ccdSSowmini Varadhan kfree_skb(clone); 59146fa01ccdSSowmini Varadhan return -ENOMEM; 59156fa01ccdSSowmini Varadhan } 59166fa01ccdSSowmini Varadhan break; 59176fa01ccdSSowmini Varadhan } 59186fa01ccdSSowmini Varadhan } while (eat); 59196fa01ccdSSowmini Varadhan 59206fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 59216fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 59226fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 59236fa01ccdSSowmini Varadhan kfree_skb(list); 59246fa01ccdSSowmini Varadhan } 59256fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 59266fa01ccdSSowmini Varadhan if (clone) { 59276fa01ccdSSowmini Varadhan clone->next = list; 59286fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 59296fa01ccdSSowmini Varadhan } 59306fa01ccdSSowmini Varadhan return 0; 59316fa01ccdSSowmini Varadhan } 59326fa01ccdSSowmini Varadhan 59336fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 59346fa01ccdSSowmini Varadhan * non-linear part of skb 59356fa01ccdSSowmini Varadhan */ 59366fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 59376fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 59386fa01ccdSSowmini Varadhan { 59396fa01ccdSSowmini Varadhan int i, k = 0; 59406fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 59416fa01ccdSSowmini Varadhan u8 *data; 59426fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 59436fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 59446fa01ccdSSowmini Varadhan 59456fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 59466fa01ccdSSowmini Varadhan 59476fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 59486fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 59496fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 59506fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 59516fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 59526fa01ccdSSowmini Varadhan if (!data) 59536fa01ccdSSowmini Varadhan return -ENOMEM; 59546fa01ccdSSowmini Varadhan 59556fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 59566fa01ccdSSowmini Varadhan 59576fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 59586fa01ccdSSowmini Varadhan skb_shinfo(skb), offsetof(struct skb_shared_info, 59596fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 59606fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 59616fa01ccdSSowmini Varadhan kfree(data); 59626fa01ccdSSowmini Varadhan return -ENOMEM; 59636fa01ccdSSowmini Varadhan } 59646fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 59656fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 59666fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 59676fa01ccdSSowmini Varadhan 59686fa01ccdSSowmini Varadhan if (pos + fsize > off) { 59696fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 59706fa01ccdSSowmini Varadhan 59716fa01ccdSSowmini Varadhan if (pos < off) { 59726fa01ccdSSowmini Varadhan /* Split frag. 59736fa01ccdSSowmini Varadhan * We have two variants in this case: 59746fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 59756fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 59766fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 59776fa01ccdSSowmini Varadhan * where splitting is expensive. 59786fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 59796fa01ccdSSowmini Varadhan */ 5980b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 59816fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 59826fa01ccdSSowmini Varadhan } 59836fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 59846fa01ccdSSowmini Varadhan k++; 59856fa01ccdSSowmini Varadhan } 59866fa01ccdSSowmini Varadhan pos += fsize; 59876fa01ccdSSowmini Varadhan } 59886fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 59896fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 59906fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 59916fa01ccdSSowmini Varadhan 59926fa01ccdSSowmini Varadhan if (k == 0) { 59936fa01ccdSSowmini Varadhan /* split line is in frag list */ 59946fa01ccdSSowmini Varadhan pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 59956fa01ccdSSowmini Varadhan } 59966fa01ccdSSowmini Varadhan skb_release_data(skb); 59976fa01ccdSSowmini Varadhan 59986fa01ccdSSowmini Varadhan skb->head = data; 59996fa01ccdSSowmini Varadhan skb->head_frag = 0; 60006fa01ccdSSowmini Varadhan skb->data = data; 60016fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 60026fa01ccdSSowmini Varadhan skb->end = size; 60036fa01ccdSSowmini Varadhan #else 60046fa01ccdSSowmini Varadhan skb->end = skb->head + size; 60056fa01ccdSSowmini Varadhan #endif 60066fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 60076fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 60086fa01ccdSSowmini Varadhan skb->cloned = 0; 60096fa01ccdSSowmini Varadhan skb->hdr_len = 0; 60106fa01ccdSSowmini Varadhan skb->nohdr = 0; 60116fa01ccdSSowmini Varadhan skb->len -= off; 60126fa01ccdSSowmini Varadhan skb->data_len = skb->len; 60136fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 60146fa01ccdSSowmini Varadhan return 0; 60156fa01ccdSSowmini Varadhan } 60166fa01ccdSSowmini Varadhan 60176fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 60186fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 60196fa01ccdSSowmini Varadhan { 60206fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 60216fa01ccdSSowmini Varadhan 60226fa01ccdSSowmini Varadhan if (len < headlen) 60236fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 60246fa01ccdSSowmini Varadhan else 60256fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 60266fa01ccdSSowmini Varadhan } 60276fa01ccdSSowmini Varadhan 60286fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 60296fa01ccdSSowmini Varadhan * a new skb 60306fa01ccdSSowmini Varadhan */ 60316fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 60326fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 60336fa01ccdSSowmini Varadhan { 60346fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 60356fa01ccdSSowmini Varadhan 60366fa01ccdSSowmini Varadhan if (!clone) 60376fa01ccdSSowmini Varadhan return NULL; 60386fa01ccdSSowmini Varadhan 60396fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 60406fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 60416fa01ccdSSowmini Varadhan kfree_skb(clone); 60426fa01ccdSSowmini Varadhan return NULL; 60436fa01ccdSSowmini Varadhan } 60446fa01ccdSSowmini Varadhan return clone; 60456fa01ccdSSowmini Varadhan } 60466fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6047c8c8b127SEric Dumazet 6048c8c8b127SEric Dumazet /** 6049c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6050c8c8b127SEric Dumazet * @skb: buffer 6051c8c8b127SEric Dumazet * 6052c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6053c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6054c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6055c8c8b127SEric Dumazet * truesize. 6056c8c8b127SEric Dumazet * Notes: 6057c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6058c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6059c8c8b127SEric Dumazet */ 6060c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6061c8c8b127SEric Dumazet { 60623174fed9SEric Dumazet if (skb->data_len) { 60633174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6064c8c8b127SEric Dumazet skb_cloned(skb)) 6065c8c8b127SEric Dumazet return; 6066c8c8b127SEric Dumazet 6067c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6068c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 60693174fed9SEric Dumazet } 60703174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 60713174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 60723174fed9SEric Dumazet * their truesize. 60733174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 60743174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 60753174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6076c8c8b127SEric Dumazet */ 6077c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6078c8c8b127SEric Dumazet } 6079df5042f4SFlorian Westphal 6080df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6081df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6082df5042f4SFlorian Westphal { 6083df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6084df5042f4SFlorian Westphal } 6085df5042f4SFlorian Westphal 60868b69a803SPaolo Abeni /** 60878b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 60888b69a803SPaolo Abeni * 60898b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 60908b69a803SPaolo Abeni * skb via __skb_ext_set(). 60918b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 60928b69a803SPaolo Abeni */ 60938b69a803SPaolo Abeni struct skb_ext *__skb_ext_alloc(void) 6094df5042f4SFlorian Westphal { 6095df5042f4SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6096df5042f4SFlorian Westphal 6097df5042f4SFlorian Westphal if (new) { 6098df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6099df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6100df5042f4SFlorian Westphal } 6101df5042f4SFlorian Westphal 6102df5042f4SFlorian Westphal return new; 6103df5042f4SFlorian Westphal } 6104df5042f4SFlorian Westphal 61054165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 61064165079bSFlorian Westphal unsigned int old_active) 6107df5042f4SFlorian Westphal { 6108df5042f4SFlorian Westphal struct skb_ext *new; 6109df5042f4SFlorian Westphal 6110df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6111df5042f4SFlorian Westphal return old; 6112df5042f4SFlorian Westphal 6113df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6114df5042f4SFlorian Westphal if (!new) 6115df5042f4SFlorian Westphal return NULL; 6116df5042f4SFlorian Westphal 6117df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6118df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6119df5042f4SFlorian Westphal 61204165079bSFlorian Westphal #ifdef CONFIG_XFRM 61214165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 61224165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 61234165079bSFlorian Westphal unsigned int i; 61244165079bSFlorian Westphal 61254165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 61264165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 61274165079bSFlorian Westphal } 61284165079bSFlorian Westphal #endif 6129df5042f4SFlorian Westphal __skb_ext_put(old); 6130df5042f4SFlorian Westphal return new; 6131df5042f4SFlorian Westphal } 6132df5042f4SFlorian Westphal 6133df5042f4SFlorian Westphal /** 61348b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 61358b69a803SPaolo Abeni * @skb: buffer 61368b69a803SPaolo Abeni * @id: extension id 61378b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 61388b69a803SPaolo Abeni * 61398b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 61408b69a803SPaolo Abeni * 61418b69a803SPaolo Abeni * Returns the pointer to the extension. 61428b69a803SPaolo Abeni */ 61438b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 61448b69a803SPaolo Abeni struct skb_ext *ext) 61458b69a803SPaolo Abeni { 61468b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 61478b69a803SPaolo Abeni 61488b69a803SPaolo Abeni skb_ext_put(skb); 61498b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 61508b69a803SPaolo Abeni ext->chunks = newlen; 61518b69a803SPaolo Abeni ext->offset[id] = newoff; 61528b69a803SPaolo Abeni skb->extensions = ext; 61538b69a803SPaolo Abeni skb->active_extensions = 1 << id; 61548b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 61558b69a803SPaolo Abeni } 61568b69a803SPaolo Abeni 61578b69a803SPaolo Abeni /** 6158df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6159df5042f4SFlorian Westphal * @skb: buffer 6160df5042f4SFlorian Westphal * @id: extension to allocate space for 6161df5042f4SFlorian Westphal * 6162df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6163df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6164df5042f4SFlorian Westphal * is returned. 6165df5042f4SFlorian Westphal * 6166df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6167df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6168df5042f4SFlorian Westphal * 6169df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6170df5042f4SFlorian Westphal */ 6171df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6172df5042f4SFlorian Westphal { 6173df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6174df5042f4SFlorian Westphal unsigned int newlen, newoff; 6175df5042f4SFlorian Westphal 6176df5042f4SFlorian Westphal if (skb->active_extensions) { 6177df5042f4SFlorian Westphal old = skb->extensions; 6178df5042f4SFlorian Westphal 61794165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6180df5042f4SFlorian Westphal if (!new) 6181df5042f4SFlorian Westphal return NULL; 6182df5042f4SFlorian Westphal 6183682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6184df5042f4SFlorian Westphal goto set_active; 6185df5042f4SFlorian Westphal 6186e94e50bdSPaolo Abeni newoff = new->chunks; 6187df5042f4SFlorian Westphal } else { 6188df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6189df5042f4SFlorian Westphal 61908b69a803SPaolo Abeni new = __skb_ext_alloc(); 6191df5042f4SFlorian Westphal if (!new) 6192df5042f4SFlorian Westphal return NULL; 6193df5042f4SFlorian Westphal } 6194df5042f4SFlorian Westphal 6195df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6196df5042f4SFlorian Westphal new->chunks = newlen; 6197df5042f4SFlorian Westphal new->offset[id] = newoff; 6198df5042f4SFlorian Westphal set_active: 6199682ec859SPaolo Abeni skb->extensions = new; 6200df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6201df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6202df5042f4SFlorian Westphal } 6203df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6204df5042f4SFlorian Westphal 62054165079bSFlorian Westphal #ifdef CONFIG_XFRM 62064165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 62074165079bSFlorian Westphal { 62084165079bSFlorian Westphal unsigned int i; 62094165079bSFlorian Westphal 62104165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 62114165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 62124165079bSFlorian Westphal } 62134165079bSFlorian Westphal #endif 62144165079bSFlorian Westphal 6215df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6216df5042f4SFlorian Westphal { 6217df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6218df5042f4SFlorian Westphal 6219df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6220df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6221df5042f4SFlorian Westphal skb->extensions = NULL; 6222df5042f4SFlorian Westphal __skb_ext_put(ext); 62234165079bSFlorian Westphal #ifdef CONFIG_XFRM 62244165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 62254165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 62264165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 62274165079bSFlorian Westphal 62284165079bSFlorian Westphal skb_ext_put_sp(sp); 62294165079bSFlorian Westphal sp->len = 0; 62304165079bSFlorian Westphal #endif 6231df5042f4SFlorian Westphal } 6232df5042f4SFlorian Westphal } 6233df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6234df5042f4SFlorian Westphal 6235df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6236df5042f4SFlorian Westphal { 6237df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6238df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6239df5042f4SFlorian Westphal */ 6240df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6241df5042f4SFlorian Westphal goto free_now; 6242df5042f4SFlorian Westphal 6243df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6244df5042f4SFlorian Westphal return; 6245df5042f4SFlorian Westphal free_now: 62464165079bSFlorian Westphal #ifdef CONFIG_XFRM 62474165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 62484165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 62494165079bSFlorian Westphal #endif 62504165079bSFlorian Westphal 6251df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6252df5042f4SFlorian Westphal } 6253df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6254df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 6255