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 { 10541a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px 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 } 2526370cc3bSAleksandr Nogikh 2536370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 2546370cc3bSAleksandr Nogikh 2551da177e4SLinus Torvalds out: 2561da177e4SLinus Torvalds return skb; 2571da177e4SLinus Torvalds nodata: 2588798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2591da177e4SLinus Torvalds skb = NULL; 2601da177e4SLinus Torvalds goto out; 2611da177e4SLinus Torvalds } 262b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2631da177e4SLinus Torvalds 264ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */ 265ba0509b6SJesper Dangaard Brouer static struct sk_buff *__build_skb_around(struct sk_buff *skb, 266ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 267ba0509b6SJesper Dangaard Brouer { 268ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 269ba0509b6SJesper Dangaard Brouer unsigned int size = frag_size ? : ksize(data); 270ba0509b6SJesper Dangaard Brouer 271ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 272ba0509b6SJesper Dangaard Brouer 273ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 274ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 275ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 276ba0509b6SJesper Dangaard Brouer skb->head = data; 277ba0509b6SJesper Dangaard Brouer skb->data = data; 278ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 279ba0509b6SJesper Dangaard Brouer skb->end = skb->tail + size; 280ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 281ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 282ba0509b6SJesper Dangaard Brouer 283ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 284ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 285ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 286ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 287ba0509b6SJesper Dangaard Brouer 2886370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 2896370cc3bSAleksandr Nogikh 290ba0509b6SJesper Dangaard Brouer return skb; 291ba0509b6SJesper Dangaard Brouer } 292ba0509b6SJesper Dangaard Brouer 2931da177e4SLinus Torvalds /** 2942ea2f62cSEric Dumazet * __build_skb - build a network buffer 295b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2962ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 297b2b5ce9dSEric Dumazet * 298b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 299deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 3002ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 3012ea2f62cSEric Dumazet * or vmalloc() 302b2b5ce9dSEric Dumazet * The return is the new skb buffer. 303b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 304b2b5ce9dSEric Dumazet * Notes : 305b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 306b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 307b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 308b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 309b2b5ce9dSEric Dumazet * before giving packet to stack. 310b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 311b2b5ce9dSEric Dumazet */ 3122ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 313b2b5ce9dSEric Dumazet { 314b2b5ce9dSEric Dumazet struct sk_buff *skb; 315b2b5ce9dSEric Dumazet 316b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 317ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 318b2b5ce9dSEric Dumazet return NULL; 319b2b5ce9dSEric Dumazet 320b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 321b2b5ce9dSEric Dumazet 322ba0509b6SJesper Dangaard Brouer return __build_skb_around(skb, data, frag_size); 323b2b5ce9dSEric Dumazet } 3242ea2f62cSEric Dumazet 3252ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3262ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3272ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3282ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3292ea2f62cSEric Dumazet */ 3302ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3312ea2f62cSEric Dumazet { 3322ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3332ea2f62cSEric Dumazet 3342ea2f62cSEric Dumazet if (skb && frag_size) { 3352ea2f62cSEric Dumazet skb->head_frag = 1; 3362f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3372ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3382ea2f62cSEric Dumazet } 3392ea2f62cSEric Dumazet return skb; 3402ea2f62cSEric Dumazet } 341b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 342b2b5ce9dSEric Dumazet 343ba0509b6SJesper Dangaard Brouer /** 344ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 345ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 346ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 347ba0509b6SJesper Dangaard Brouer * @frag_size: size of data, or 0 if head was kmalloced 348ba0509b6SJesper Dangaard Brouer */ 349ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 350ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 351ba0509b6SJesper Dangaard Brouer { 352ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 353ba0509b6SJesper Dangaard Brouer return NULL; 354ba0509b6SJesper Dangaard Brouer 355ba0509b6SJesper Dangaard Brouer skb = __build_skb_around(skb, data, frag_size); 356ba0509b6SJesper Dangaard Brouer 357ba0509b6SJesper Dangaard Brouer if (skb && frag_size) { 358ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 359ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 360ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 361ba0509b6SJesper Dangaard Brouer } 362ba0509b6SJesper Dangaard Brouer return skb; 363ba0509b6SJesper Dangaard Brouer } 364ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 365ba0509b6SJesper Dangaard Brouer 366795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 367795bb1c0SJesper Dangaard Brouer 368795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 369795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 370e0d7924aSAlexey Dobriyan unsigned int skb_count; 371795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 372795bb1c0SJesper Dangaard Brouer }; 373795bb1c0SJesper Dangaard Brouer 374b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 375795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 376ffde7328SAlexander Duyck 377ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 378ffde7328SAlexander Duyck { 379795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3809451980aSAlexander Duyck 3818c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 382ffde7328SAlexander Duyck } 383ffde7328SAlexander Duyck 384ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 385ffde7328SAlexander Duyck { 3863bed3cc4SAlexander Duyck fragsz = SKB_DATA_ALIGN(fragsz); 3873bed3cc4SAlexander Duyck 388453f85d4SMel Gorman return __napi_alloc_frag(fragsz, GFP_ATOMIC); 389ffde7328SAlexander Duyck } 390ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 391ffde7328SAlexander Duyck 3926f532612SEric Dumazet /** 3937ba7aeabSSebastian Andrzej Siewior * netdev_alloc_frag - allocate a page fragment 3947ba7aeabSSebastian Andrzej Siewior * @fragsz: fragment size 3957ba7aeabSSebastian Andrzej Siewior * 3967ba7aeabSSebastian Andrzej Siewior * Allocates a frag from a page for receive buffer. 3977ba7aeabSSebastian Andrzej Siewior * Uses GFP_ATOMIC allocations. 3987ba7aeabSSebastian Andrzej Siewior */ 3997ba7aeabSSebastian Andrzej Siewior void *netdev_alloc_frag(unsigned int fragsz) 4007ba7aeabSSebastian Andrzej Siewior { 4017ba7aeabSSebastian Andrzej Siewior struct page_frag_cache *nc; 4027ba7aeabSSebastian Andrzej Siewior void *data; 4037ba7aeabSSebastian Andrzej Siewior 4047ba7aeabSSebastian Andrzej Siewior fragsz = SKB_DATA_ALIGN(fragsz); 4057ba7aeabSSebastian Andrzej Siewior if (in_irq() || irqs_disabled()) { 4067ba7aeabSSebastian Andrzej Siewior nc = this_cpu_ptr(&netdev_alloc_cache); 4077ba7aeabSSebastian Andrzej Siewior data = page_frag_alloc(nc, fragsz, GFP_ATOMIC); 4087ba7aeabSSebastian Andrzej Siewior } else { 4097ba7aeabSSebastian Andrzej Siewior local_bh_disable(); 4107ba7aeabSSebastian Andrzej Siewior data = __napi_alloc_frag(fragsz, GFP_ATOMIC); 4117ba7aeabSSebastian Andrzej Siewior local_bh_enable(); 4127ba7aeabSSebastian Andrzej Siewior } 4137ba7aeabSSebastian Andrzej Siewior return data; 4147ba7aeabSSebastian Andrzej Siewior } 4157ba7aeabSSebastian Andrzej Siewior EXPORT_SYMBOL(netdev_alloc_frag); 4167ba7aeabSSebastian Andrzej Siewior 4177ba7aeabSSebastian Andrzej Siewior /** 418fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 419fd11a83dSAlexander Duyck * @dev: network device to receive on 420d7499160SMasanari Iida * @len: length to allocate 421fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 422fd11a83dSAlexander Duyck * 423fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 424fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 425fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 426fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 427fd11a83dSAlexander Duyck * 428fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 429fd11a83dSAlexander Duyck */ 4309451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4319451980aSAlexander Duyck gfp_t gfp_mask) 432fd11a83dSAlexander Duyck { 433b63ae8caSAlexander Duyck struct page_frag_cache *nc; 434fd11a83dSAlexander Duyck struct sk_buff *skb; 4359451980aSAlexander Duyck bool pfmemalloc; 4369451980aSAlexander Duyck void *data; 437fd11a83dSAlexander Duyck 4389451980aSAlexander Duyck len += NET_SKB_PAD; 439fd11a83dSAlexander Duyck 4409451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 441d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 442a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 443a080e7bdSAlexander Duyck if (!skb) 444a080e7bdSAlexander Duyck goto skb_fail; 445a080e7bdSAlexander Duyck goto skb_success; 446a080e7bdSAlexander Duyck } 4479451980aSAlexander Duyck 4489451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4499451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4509451980aSAlexander Duyck 4519451980aSAlexander Duyck if (sk_memalloc_socks()) 4529451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4539451980aSAlexander Duyck 45492dcabd7SSebastian Andrzej Siewior if (in_irq() || irqs_disabled()) { 4559451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4568c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4579451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 45892dcabd7SSebastian Andrzej Siewior } else { 45992dcabd7SSebastian Andrzej Siewior local_bh_disable(); 46092dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 46192dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 46292dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 46392dcabd7SSebastian Andrzej Siewior local_bh_enable(); 46492dcabd7SSebastian Andrzej Siewior } 4659451980aSAlexander Duyck 4669451980aSAlexander Duyck if (unlikely(!data)) 4679451980aSAlexander Duyck return NULL; 4689451980aSAlexander Duyck 4699451980aSAlexander Duyck skb = __build_skb(data, len); 4709451980aSAlexander Duyck if (unlikely(!skb)) { 471181edb2bSAlexander Duyck skb_free_frag(data); 4729451980aSAlexander Duyck return NULL; 4739451980aSAlexander Duyck } 4749451980aSAlexander Duyck 4759451980aSAlexander Duyck if (pfmemalloc) 4769451980aSAlexander Duyck skb->pfmemalloc = 1; 4779451980aSAlexander Duyck skb->head_frag = 1; 4789451980aSAlexander Duyck 479a080e7bdSAlexander Duyck skb_success: 4808af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4817b2e497aSChristoph Hellwig skb->dev = dev; 482fd11a83dSAlexander Duyck 483a080e7bdSAlexander Duyck skb_fail: 4848af27456SChristoph Hellwig return skb; 4858af27456SChristoph Hellwig } 486b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4871da177e4SLinus Torvalds 488fd11a83dSAlexander Duyck /** 489fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 490fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 491d7499160SMasanari Iida * @len: length to allocate 492fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 493fd11a83dSAlexander Duyck * 494fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 495fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 496fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 497fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 498fd11a83dSAlexander Duyck * 499fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 500fd11a83dSAlexander Duyck */ 5019451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 5029451980aSAlexander Duyck gfp_t gfp_mask) 503fd11a83dSAlexander Duyck { 504795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 505fd11a83dSAlexander Duyck struct sk_buff *skb; 5069451980aSAlexander Duyck void *data; 507fd11a83dSAlexander Duyck 5089451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 509fd11a83dSAlexander Duyck 5109451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 511d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 512a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 513a080e7bdSAlexander Duyck if (!skb) 514a080e7bdSAlexander Duyck goto skb_fail; 515a080e7bdSAlexander Duyck goto skb_success; 516a080e7bdSAlexander Duyck } 5179451980aSAlexander Duyck 5189451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5199451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5209451980aSAlexander Duyck 5219451980aSAlexander Duyck if (sk_memalloc_socks()) 5229451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5239451980aSAlexander Duyck 5248c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 5259451980aSAlexander Duyck if (unlikely(!data)) 5269451980aSAlexander Duyck return NULL; 5279451980aSAlexander Duyck 5289451980aSAlexander Duyck skb = __build_skb(data, len); 5299451980aSAlexander Duyck if (unlikely(!skb)) { 530181edb2bSAlexander Duyck skb_free_frag(data); 5319451980aSAlexander Duyck return NULL; 5329451980aSAlexander Duyck } 5339451980aSAlexander Duyck 534795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5359451980aSAlexander Duyck skb->pfmemalloc = 1; 5369451980aSAlexander Duyck skb->head_frag = 1; 5379451980aSAlexander Duyck 538a080e7bdSAlexander Duyck skb_success: 539fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 540fd11a83dSAlexander Duyck skb->dev = napi->dev; 541fd11a83dSAlexander Duyck 542a080e7bdSAlexander Duyck skb_fail: 543fd11a83dSAlexander Duyck return skb; 544fd11a83dSAlexander Duyck } 545fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 546fd11a83dSAlexander Duyck 547654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 54850269e19SEric Dumazet int size, unsigned int truesize) 549654bed16SPeter Zijlstra { 550654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 551654bed16SPeter Zijlstra skb->len += size; 552654bed16SPeter Zijlstra skb->data_len += size; 55350269e19SEric Dumazet skb->truesize += truesize; 554654bed16SPeter Zijlstra } 555654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 556654bed16SPeter Zijlstra 557f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 558f8e617e1SJason Wang unsigned int truesize) 559f8e617e1SJason Wang { 560f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 561f8e617e1SJason Wang 562f8e617e1SJason Wang skb_frag_size_add(frag, size); 563f8e617e1SJason Wang skb->len += size; 564f8e617e1SJason Wang skb->data_len += size; 565f8e617e1SJason Wang skb->truesize += truesize; 566f8e617e1SJason Wang } 567f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 568f8e617e1SJason Wang 56927b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5701da177e4SLinus Torvalds { 571bd8a7036SEric Dumazet kfree_skb_list(*listp); 57227b437c8SHerbert Xu *listp = NULL; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 57527b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 57627b437c8SHerbert Xu { 57727b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 57827b437c8SHerbert Xu } 57927b437c8SHerbert Xu 5801da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct sk_buff *list; 5831da177e4SLinus Torvalds 584fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5851da177e4SLinus Torvalds skb_get(list); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 588d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 589d3836f21SEric Dumazet { 590181edb2bSAlexander Duyck unsigned char *head = skb->head; 591181edb2bSAlexander Duyck 592d3836f21SEric Dumazet if (skb->head_frag) 593181edb2bSAlexander Duyck skb_free_frag(head); 594d3836f21SEric Dumazet else 595181edb2bSAlexander Duyck kfree(head); 596d3836f21SEric Dumazet } 597d3836f21SEric Dumazet 5985bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5991da177e4SLinus Torvalds { 600ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 6011da177e4SLinus Torvalds int i; 602ff04a771SEric Dumazet 603ff04a771SEric Dumazet if (skb->cloned && 604ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 605ff04a771SEric Dumazet &shinfo->dataref)) 606ff04a771SEric Dumazet return; 607ff04a771SEric Dumazet 608*70c43167SJonathan Lemon skb_zcopy_clear(skb, true); 609*70c43167SJonathan Lemon 610ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 611ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 6121da177e4SLinus Torvalds 613ff04a771SEric Dumazet if (shinfo->frag_list) 614ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6151da177e4SLinus Torvalds 616d3836f21SEric Dumazet skb_free_head(skb); 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds /* 6201da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6211da177e4SLinus Torvalds */ 6222d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6231da177e4SLinus Torvalds { 624d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 625d179cd12SDavid S. Miller 626d179cd12SDavid S. Miller switch (skb->fclone) { 627d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6281da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6296ffe75ebSEric Dumazet return; 630d179cd12SDavid S. Miller 631d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 632d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6336ffe75ebSEric Dumazet 6346ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6356ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6366ffe75ebSEric Dumazet * while here, branch prediction will be good. 6376ffe75ebSEric Dumazet */ 6382638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 6396ffe75ebSEric Dumazet goto fastpath; 640d179cd12SDavid S. Miller break; 641d179cd12SDavid S. Miller 6426ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 643d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 644d179cd12SDavid S. Miller break; 6453ff50b79SStephen Hemminger } 6462638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 6476ffe75ebSEric Dumazet return; 6486ffe75ebSEric Dumazet fastpath: 6496ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6520a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 6531da177e4SLinus Torvalds { 654adf30907SEric Dumazet skb_dst_drop(skb); 6551da177e4SLinus Torvalds if (skb->destructor) { 6569c2b3328SStephen Hemminger WARN_ON(in_irq()); 6571da177e4SLinus Torvalds skb->destructor(skb); 6581da177e4SLinus Torvalds } 659a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 660cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6612fc72c7bSKOVACS Krisztian #endif 662df5042f4SFlorian Westphal skb_ext_put(skb); 66304a4bb55SLennert Buytenhek } 66404a4bb55SLennert Buytenhek 66504a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 66604a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 66704a4bb55SLennert Buytenhek { 66804a4bb55SLennert Buytenhek skb_release_head_state(skb); 669a28b1b90SFlorian Westphal if (likely(skb->head)) 6702d4baff8SHerbert Xu skb_release_data(skb); 6712d4baff8SHerbert Xu } 6721da177e4SLinus Torvalds 6732d4baff8SHerbert Xu /** 6742d4baff8SHerbert Xu * __kfree_skb - private function 6752d4baff8SHerbert Xu * @skb: buffer 6762d4baff8SHerbert Xu * 6772d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6782d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6792d4baff8SHerbert Xu * always call kfree_skb 6802d4baff8SHerbert Xu */ 6812d4baff8SHerbert Xu 6822d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6832d4baff8SHerbert Xu { 6842d4baff8SHerbert Xu skb_release_all(skb); 6851da177e4SLinus Torvalds kfree_skbmem(skb); 6861da177e4SLinus Torvalds } 687b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds /** 690231d06aeSJörn Engel * kfree_skb - free an sk_buff 691231d06aeSJörn Engel * @skb: buffer to free 692231d06aeSJörn Engel * 693231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 694231d06aeSJörn Engel * hit zero. 695231d06aeSJörn Engel */ 696231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 697231d06aeSJörn Engel { 6983889a803SPaolo Abeni if (!skb_unref(skb)) 699231d06aeSJörn Engel return; 7003889a803SPaolo Abeni 701ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 702231d06aeSJörn Engel __kfree_skb(skb); 703231d06aeSJörn Engel } 704b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 705231d06aeSJörn Engel 706bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 707bd8a7036SEric Dumazet { 708bd8a7036SEric Dumazet while (segs) { 709bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 710bd8a7036SEric Dumazet 711bd8a7036SEric Dumazet kfree_skb(segs); 712bd8a7036SEric Dumazet segs = next; 713bd8a7036SEric Dumazet } 714bd8a7036SEric Dumazet } 715bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 716bd8a7036SEric Dumazet 7176413139dSWillem de Bruijn /* Dump skb information and contents. 7186413139dSWillem de Bruijn * 7196413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 7206413139dSWillem de Bruijn * 721302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 7226413139dSWillem de Bruijn */ 7236413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 7246413139dSWillem de Bruijn { 7256413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 7266413139dSWillem de Bruijn struct net_device *dev = skb->dev; 7276413139dSWillem de Bruijn struct sock *sk = skb->sk; 7286413139dSWillem de Bruijn struct sk_buff *list_skb; 7296413139dSWillem de Bruijn bool has_mac, has_trans; 7306413139dSWillem de Bruijn int headroom, tailroom; 7316413139dSWillem de Bruijn int i, len, seg_len; 7326413139dSWillem de Bruijn 7336413139dSWillem de Bruijn if (full_pkt) 7346413139dSWillem de Bruijn len = skb->len; 7356413139dSWillem de Bruijn else 7366413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 7376413139dSWillem de Bruijn 7386413139dSWillem de Bruijn headroom = skb_headroom(skb); 7396413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 7406413139dSWillem de Bruijn 7416413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 7426413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 7436413139dSWillem de Bruijn 7446413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 7456413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 7466413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 7476413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 7486413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 7496413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 7506413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 7516413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 7526413139dSWillem de Bruijn skb->network_header, 7536413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 7546413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 7556413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 7566413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 7576413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 7586413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 7596413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 7606413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 7616413139dSWillem de Bruijn 7626413139dSWillem de Bruijn if (dev) 7636413139dSWillem de Bruijn printk("%sdev name=%s feat=0x%pNF\n", 7646413139dSWillem de Bruijn level, dev->name, &dev->features); 7656413139dSWillem de Bruijn if (sk) 766db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 7676413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 7686413139dSWillem de Bruijn 7696413139dSWillem de Bruijn if (full_pkt && headroom) 7706413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 7716413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 7726413139dSWillem de Bruijn 7736413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 7746413139dSWillem de Bruijn if (seg_len) 7756413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 7766413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 7776413139dSWillem de Bruijn len -= seg_len; 7786413139dSWillem de Bruijn 7796413139dSWillem de Bruijn if (full_pkt && tailroom) 7806413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 7816413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 7826413139dSWillem de Bruijn 7836413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 7846413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 7856413139dSWillem de Bruijn u32 p_off, p_len, copied; 7866413139dSWillem de Bruijn struct page *p; 7876413139dSWillem de Bruijn u8 *vaddr; 7886413139dSWillem de Bruijn 789b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 7906413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 7916413139dSWillem de Bruijn copied) { 7926413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 7936413139dSWillem de Bruijn vaddr = kmap_atomic(p); 7946413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 7956413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 7966413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 7976413139dSWillem de Bruijn kunmap_atomic(vaddr); 7986413139dSWillem de Bruijn len -= seg_len; 7996413139dSWillem de Bruijn if (!len) 8006413139dSWillem de Bruijn break; 8016413139dSWillem de Bruijn } 8026413139dSWillem de Bruijn } 8036413139dSWillem de Bruijn 8046413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 8056413139dSWillem de Bruijn printk("skb fraglist:\n"); 8066413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 8076413139dSWillem de Bruijn skb_dump(level, list_skb, true); 8086413139dSWillem de Bruijn } 8096413139dSWillem de Bruijn } 8106413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 8116413139dSWillem de Bruijn 812d1a203eaSStephen Hemminger /** 81325121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 81425121173SMichael S. Tsirkin * @skb: buffer that triggered an error 81525121173SMichael S. Tsirkin * 81625121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 81725121173SMichael S. Tsirkin * skb must be freed afterwards. 81825121173SMichael S. Tsirkin */ 81925121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 82025121173SMichael S. Tsirkin { 8211f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 82225121173SMichael S. Tsirkin } 82325121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 82425121173SMichael S. Tsirkin 825be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 82625121173SMichael S. Tsirkin /** 827ead2ceb0SNeil Horman * consume_skb - free an skbuff 828ead2ceb0SNeil Horman * @skb: buffer to free 829ead2ceb0SNeil Horman * 830ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 831ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 832ead2ceb0SNeil Horman * is being dropped after a failure and notes that 833ead2ceb0SNeil Horman */ 834ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 835ead2ceb0SNeil Horman { 8363889a803SPaolo Abeni if (!skb_unref(skb)) 837ead2ceb0SNeil Horman return; 8383889a803SPaolo Abeni 83907dc22e7SKoki Sanagi trace_consume_skb(skb); 840ead2ceb0SNeil Horman __kfree_skb(skb); 841ead2ceb0SNeil Horman } 842ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 843be769db2SHerbert Xu #endif 844ead2ceb0SNeil Horman 8450a463c78SPaolo Abeni /** 846c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 8470a463c78SPaolo Abeni * @skb: buffer to free 8480a463c78SPaolo Abeni * 849ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 850ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 8510a463c78SPaolo Abeni */ 852ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 8530a463c78SPaolo Abeni { 8540a463c78SPaolo Abeni trace_consume_skb(skb); 8550a463c78SPaolo Abeni skb_release_data(skb); 8560a463c78SPaolo Abeni kfree_skbmem(skb); 8570a463c78SPaolo Abeni } 8580a463c78SPaolo Abeni 859795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 860795bb1c0SJesper Dangaard Brouer { 861795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 862795bb1c0SJesper Dangaard Brouer 863795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 864795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 865795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 866795bb1c0SJesper Dangaard Brouer nc->skb_cache); 867795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 868795bb1c0SJesper Dangaard Brouer } 869795bb1c0SJesper Dangaard Brouer } 870795bb1c0SJesper Dangaard Brouer 87115fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 872795bb1c0SJesper Dangaard Brouer { 873795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 874795bb1c0SJesper Dangaard Brouer 875795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 876795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 877795bb1c0SJesper Dangaard Brouer 878795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 879795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 880795bb1c0SJesper Dangaard Brouer 881795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 882795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 883795bb1c0SJesper Dangaard Brouer prefetchw(skb); 884795bb1c0SJesper Dangaard Brouer #endif 885795bb1c0SJesper Dangaard Brouer 886795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 887795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 888795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 889795bb1c0SJesper Dangaard Brouer nc->skb_cache); 890795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 891795bb1c0SJesper Dangaard Brouer } 892795bb1c0SJesper Dangaard Brouer } 89315fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 89415fad714SJesper Dangaard Brouer { 89515fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 89615fad714SJesper Dangaard Brouer } 897795bb1c0SJesper Dangaard Brouer 898795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 899795bb1c0SJesper Dangaard Brouer { 900885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 901795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 902885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 903795bb1c0SJesper Dangaard Brouer return; 904795bb1c0SJesper Dangaard Brouer } 905795bb1c0SJesper Dangaard Brouer 9066454eca8SYunsheng Lin lockdep_assert_in_softirq(); 9076454eca8SYunsheng Lin 9087608894eSPaolo Abeni if (!skb_unref(skb)) 909795bb1c0SJesper Dangaard Brouer return; 9107608894eSPaolo Abeni 911795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 912795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 913795bb1c0SJesper Dangaard Brouer 914795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 915abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 916795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 917795bb1c0SJesper Dangaard Brouer return; 918795bb1c0SJesper Dangaard Brouer } 919795bb1c0SJesper Dangaard Brouer 92015fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 921795bb1c0SJesper Dangaard Brouer } 922795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 923795bb1c0SJesper Dangaard Brouer 924b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 925b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 926b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 927b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 928b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 929b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 930b1937227SEric Dumazet 931dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 932dec18810SHerbert Xu { 933dec18810SHerbert Xu new->tstamp = old->tstamp; 934b1937227SEric Dumazet /* We do not copy old->sk */ 935dec18810SHerbert Xu new->dev = old->dev; 936b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 9377fee226aSEric Dumazet skb_dst_copy(new, old); 938df5042f4SFlorian Westphal __skb_ext_copy(new, old); 939b1937227SEric Dumazet __nf_copy(new, old, false); 9406aa895b0SPatrick McHardy 941b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 942b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 943b1937227SEric Dumazet */ 944b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 94506021292SEliezer Tamir 946b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 947b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 948b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 949b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 950b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 951b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 952b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 953b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 954b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 955b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 956b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 957b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 958b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 959b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 960b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 961b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 962b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 963b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 964b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 965b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 966b1937227SEric Dumazet #endif 967e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 968b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 96906021292SEliezer Tamir #endif 9702bd82484SEric Dumazet #ifdef CONFIG_XPS 9712bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 9722bd82484SEric Dumazet #endif 973b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 974b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 975b1937227SEric Dumazet #endif 976b1937227SEric Dumazet 977dec18810SHerbert Xu } 978dec18810SHerbert Xu 97982c49a35SHerbert Xu /* 98082c49a35SHerbert Xu * You should not add any new code to this function. Add it to 98182c49a35SHerbert Xu * __copy_skb_header above instead. 98282c49a35SHerbert Xu */ 983e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds #define C(x) n->x = skb->x 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds n->next = n->prev = NULL; 9881da177e4SLinus Torvalds n->sk = NULL; 989dec18810SHerbert Xu __copy_skb_header(n, skb); 990dec18810SHerbert Xu 9911da177e4SLinus Torvalds C(len); 9921da177e4SLinus Torvalds C(data_len); 9933e6b3b2eSAlexey Dobriyan C(mac_len); 994334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 99502f1c89dSPaul Moore n->cloned = 1; 9961da177e4SLinus Torvalds n->nohdr = 0; 997b13dda9fSEric Dumazet n->peeked = 0; 998e78bfb07SStefano Brivio C(pfmemalloc); 9991da177e4SLinus Torvalds n->destructor = NULL; 10001da177e4SLinus Torvalds C(tail); 10011da177e4SLinus Torvalds C(end); 100202f1c89dSPaul Moore C(head); 1003d3836f21SEric Dumazet C(head_frag); 100402f1c89dSPaul Moore C(data); 100502f1c89dSPaul Moore C(truesize); 100663354797SReshetova, Elena refcount_set(&n->users, 1); 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 10091da177e4SLinus Torvalds skb->cloned = 1; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds return n; 1012e0053ec0SHerbert Xu #undef C 1013e0053ec0SHerbert Xu } 1014e0053ec0SHerbert Xu 1015e0053ec0SHerbert Xu /** 1016da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1017da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1018da29e4b4SJakub Kicinski */ 1019da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1020da29e4b4SJakub Kicinski { 1021da29e4b4SJakub Kicinski struct sk_buff *n; 1022da29e4b4SJakub Kicinski 1023da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1024da29e4b4SJakub Kicinski if (!n) 1025da29e4b4SJakub Kicinski return NULL; 1026da29e4b4SJakub Kicinski 1027da29e4b4SJakub Kicinski n->len = first->len; 1028da29e4b4SJakub Kicinski n->data_len = first->len; 1029da29e4b4SJakub Kicinski n->truesize = first->truesize; 1030da29e4b4SJakub Kicinski 1031da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1032da29e4b4SJakub Kicinski 1033da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1034da29e4b4SJakub Kicinski n->destructor = NULL; 1035da29e4b4SJakub Kicinski 1036da29e4b4SJakub Kicinski return n; 1037da29e4b4SJakub Kicinski } 1038da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1039da29e4b4SJakub Kicinski 1040da29e4b4SJakub Kicinski /** 1041e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1042e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1043e0053ec0SHerbert Xu * @src: the skb to supply the contents 1044e0053ec0SHerbert Xu * 1045e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1046e0053ec0SHerbert Xu * supplied by the user. 1047e0053ec0SHerbert Xu * 1048e0053ec0SHerbert Xu * The target skb is returned upon exit. 1049e0053ec0SHerbert Xu */ 1050e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1051e0053ec0SHerbert Xu { 10522d4baff8SHerbert Xu skb_release_all(dst); 1053e0053ec0SHerbert Xu return __skb_clone(dst, src); 1054e0053ec0SHerbert Xu } 1055e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1056e0053ec0SHerbert Xu 10576f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1058a91dbff5SWillem de Bruijn { 1059a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1060a91dbff5SWillem de Bruijn struct user_struct *user; 1061a91dbff5SWillem de Bruijn 1062a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1063a91dbff5SWillem de Bruijn return 0; 1064a91dbff5SWillem de Bruijn 1065a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1066a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1067a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1068a91dbff5SWillem de Bruijn 1069a91dbff5SWillem de Bruijn do { 1070a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 1071a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1072a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1073a91dbff5SWillem de Bruijn return -ENOBUFS; 1074a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 1075a91dbff5SWillem de Bruijn old_pg); 1076a91dbff5SWillem de Bruijn 1077a91dbff5SWillem de Bruijn if (!mmp->user) { 1078a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1079a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1080a91dbff5SWillem de Bruijn } else { 1081a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1082a91dbff5SWillem de Bruijn } 1083a91dbff5SWillem de Bruijn 1084a91dbff5SWillem de Bruijn return 0; 1085a91dbff5SWillem de Bruijn } 10866f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1087a91dbff5SWillem de Bruijn 10886f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1089a91dbff5SWillem de Bruijn { 1090a91dbff5SWillem de Bruijn if (mmp->user) { 1091a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1092a91dbff5SWillem de Bruijn free_uid(mmp->user); 1093a91dbff5SWillem de Bruijn } 1094a91dbff5SWillem de Bruijn } 10956f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1096a91dbff5SWillem de Bruijn 109752267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 109852267790SWillem de Bruijn { 109952267790SWillem de Bruijn struct ubuf_info *uarg; 110052267790SWillem de Bruijn struct sk_buff *skb; 110152267790SWillem de Bruijn 110252267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 110352267790SWillem de Bruijn 110452267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 110552267790SWillem de Bruijn if (!skb) 110652267790SWillem de Bruijn return NULL; 110752267790SWillem de Bruijn 110852267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 110952267790SWillem de Bruijn uarg = (void *)skb->cb; 1110a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1111a91dbff5SWillem de Bruijn 1112a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1113a91dbff5SWillem de Bruijn kfree_skb(skb); 1114a91dbff5SWillem de Bruijn return NULL; 1115a91dbff5SWillem de Bruijn } 111652267790SWillem de Bruijn 111752267790SWillem de Bruijn uarg->callback = sock_zerocopy_callback; 11184ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 11194ab6c99dSWillem de Bruijn uarg->len = 1; 11204ab6c99dSWillem de Bruijn uarg->bytelen = size; 112152267790SWillem de Bruijn uarg->zerocopy = 1; 1122c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 112352267790SWillem de Bruijn sock_hold(sk); 112452267790SWillem de Bruijn 112552267790SWillem de Bruijn return uarg; 112652267790SWillem de Bruijn } 112752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 112852267790SWillem de Bruijn 112952267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 113052267790SWillem de Bruijn { 113152267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 113252267790SWillem de Bruijn } 113352267790SWillem de Bruijn 11344ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 11354ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 11364ab6c99dSWillem de Bruijn { 11374ab6c99dSWillem de Bruijn if (uarg) { 11384ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 11394ab6c99dSWillem de Bruijn u32 bytelen, next; 11404ab6c99dSWillem de Bruijn 11414ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 11424ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 11434ab6c99dSWillem de Bruijn */ 11444ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 11454ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 11464ab6c99dSWillem de Bruijn return NULL; 11474ab6c99dSWillem de Bruijn } 11484ab6c99dSWillem de Bruijn 11494ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 11504ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 11514ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 11524ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 11534ab6c99dSWillem de Bruijn goto new_alloc; 11544ab6c99dSWillem de Bruijn return NULL; 11554ab6c99dSWillem de Bruijn } 11564ab6c99dSWillem de Bruijn 11574ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 11584ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 1159a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 1160a91dbff5SWillem de Bruijn return NULL; 11614ab6c99dSWillem de Bruijn uarg->len++; 11624ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 11634ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1164100f6d8eSWillem de Bruijn 1165100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1166100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 1167e76d46cfSJonathan Lemon skb_zcopy_get(uarg); 1168100f6d8eSWillem de Bruijn 11694ab6c99dSWillem de Bruijn return uarg; 11704ab6c99dSWillem de Bruijn } 11714ab6c99dSWillem de Bruijn } 11724ab6c99dSWillem de Bruijn 11734ab6c99dSWillem de Bruijn new_alloc: 11744ab6c99dSWillem de Bruijn return sock_zerocopy_alloc(sk, size); 11754ab6c99dSWillem de Bruijn } 11764ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 11774ab6c99dSWillem de Bruijn 11784ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 11794ab6c99dSWillem de Bruijn { 11804ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 11814ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 11824ab6c99dSWillem de Bruijn u64 sum_len; 11834ab6c99dSWillem de Bruijn 11844ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 11854ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 11864ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 11874ab6c99dSWillem de Bruijn 11884ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 11894ab6c99dSWillem de Bruijn return false; 11904ab6c99dSWillem de Bruijn 11914ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 11924ab6c99dSWillem de Bruijn return false; 11934ab6c99dSWillem de Bruijn 11944ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 11954ab6c99dSWillem de Bruijn return true; 11964ab6c99dSWillem de Bruijn } 11974ab6c99dSWillem de Bruijn 119875518851SJonathan Lemon static void __sock_zerocopy_callback(struct ubuf_info *uarg) 119952267790SWillem de Bruijn { 12004ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 120152267790SWillem de Bruijn struct sock_exterr_skb *serr; 120252267790SWillem de Bruijn struct sock *sk = skb->sk; 12034ab6c99dSWillem de Bruijn struct sk_buff_head *q; 12044ab6c99dSWillem de Bruijn unsigned long flags; 12054ab6c99dSWillem de Bruijn u32 lo, hi; 12064ab6c99dSWillem de Bruijn u16 len; 120752267790SWillem de Bruijn 1208ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1209ccaffff1SWillem de Bruijn 12104ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 12114ab6c99dSWillem de Bruijn * so do not queue a completion notification 12124ab6c99dSWillem de Bruijn */ 12134ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 121452267790SWillem de Bruijn goto release; 121552267790SWillem de Bruijn 12164ab6c99dSWillem de Bruijn len = uarg->len; 12174ab6c99dSWillem de Bruijn lo = uarg->id; 12184ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 12194ab6c99dSWillem de Bruijn 122052267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 122152267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 122252267790SWillem de Bruijn serr->ee.ee_errno = 0; 122352267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 12244ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 12254ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 122675518851SJonathan Lemon if (!uarg->zerocopy) 122752267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 122852267790SWillem de Bruijn 12294ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 12304ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 12314ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 12324ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 12334ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 12344ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 123552267790SWillem de Bruijn skb = NULL; 12364ab6c99dSWillem de Bruijn } 12374ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 123852267790SWillem de Bruijn 123952267790SWillem de Bruijn sk->sk_error_report(sk); 124052267790SWillem de Bruijn 124152267790SWillem de Bruijn release: 124252267790SWillem de Bruijn consume_skb(skb); 124352267790SWillem de Bruijn sock_put(sk); 124452267790SWillem de Bruijn } 124575518851SJonathan Lemon 124636177832SJonathan Lemon void sock_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 124736177832SJonathan Lemon bool success) 124875518851SJonathan Lemon { 124975518851SJonathan Lemon uarg->zerocopy = uarg->zerocopy & success; 125075518851SJonathan Lemon 125175518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 125275518851SJonathan Lemon __sock_zerocopy_callback(uarg); 125375518851SJonathan Lemon } 125452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 125552267790SWillem de Bruijn 125652900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 125752267790SWillem de Bruijn { 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) 1264236a6b1cSJonathan Lemon sock_zerocopy_callback(NULL, uarg, true); 126552267790SWillem de Bruijn } 126652267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 126752267790SWillem de Bruijn 1268b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) 1269b5947e5dSWillem de Bruijn { 1270b5947e5dSWillem de Bruijn return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); 1271b5947e5dSWillem de Bruijn } 1272b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); 1273b5947e5dSWillem de Bruijn 127452267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 127552267790SWillem de Bruijn struct msghdr *msg, int len, 127652267790SWillem de Bruijn struct ubuf_info *uarg) 127752267790SWillem de Bruijn { 12784ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 127952267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 128052267790SWillem de Bruijn int err, orig_len = skb->len; 128152267790SWillem de Bruijn 12824ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 12834ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 12844ab6c99dSWillem de Bruijn */ 12854ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 12864ab6c99dSWillem de Bruijn return -EEXIST; 12874ab6c99dSWillem de Bruijn 128852267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 128952267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 129054d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 129154d43117SWillem de Bruijn 129252267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 129352267790SWillem de Bruijn msg->msg_iter = orig_iter; 129454d43117SWillem de Bruijn skb->sk = sk; 129552267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 129654d43117SWillem de Bruijn skb->sk = save_sk; 129752267790SWillem de Bruijn return err; 129852267790SWillem de Bruijn } 129952267790SWillem de Bruijn 130052900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 130152267790SWillem de Bruijn return skb->len - orig_len; 130252267790SWillem de Bruijn } 130352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 130452267790SWillem de Bruijn 13051f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 130652267790SWillem de Bruijn gfp_t gfp_mask) 130752267790SWillem de Bruijn { 130852267790SWillem de Bruijn if (skb_zcopy(orig)) { 130952267790SWillem de Bruijn if (skb_zcopy(nskb)) { 131052267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 131152267790SWillem de Bruijn if (!gfp_mask) { 131252267790SWillem de Bruijn WARN_ON_ONCE(1); 131352267790SWillem de Bruijn return -ENOMEM; 131452267790SWillem de Bruijn } 131552267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 131652267790SWillem de Bruijn return 0; 131752267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 131852267790SWillem de Bruijn return -EIO; 131952267790SWillem de Bruijn } 132052900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 132152267790SWillem de Bruijn } 132252267790SWillem de Bruijn return 0; 132352267790SWillem de Bruijn } 132452267790SWillem de Bruijn 13252c53040fSBen Hutchings /** 13262c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 132748c83012SMichael S. Tsirkin * @skb: the skb to modify 132848c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 132948c83012SMichael S. Tsirkin * 133048c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 133148c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 133248c83012SMichael S. Tsirkin * to userspace pages. 133348c83012SMichael S. Tsirkin * 133448c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 133548c83012SMichael S. Tsirkin * %GFP_ATOMIC. 133648c83012SMichael S. Tsirkin * 133748c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 133848c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 133948c83012SMichael S. Tsirkin */ 134048c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1341a6686f2fSShirley Ma { 1342a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1343a6686f2fSShirley Ma struct page *page, *head = NULL; 13443ece7826SWillem de Bruijn int i, new_frags; 13453ece7826SWillem de Bruijn u32 d_off; 1346a6686f2fSShirley Ma 13473ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 13483ece7826SWillem de Bruijn return -EINVAL; 13493ece7826SWillem de Bruijn 1350f72c4ac6SWillem de Bruijn if (!num_frags) 1351f72c4ac6SWillem de Bruijn goto release; 1352f72c4ac6SWillem de Bruijn 13533ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 13543ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 135502756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1356a6686f2fSShirley Ma if (!page) { 1357a6686f2fSShirley Ma while (head) { 135840dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1359a6686f2fSShirley Ma put_page(head); 1360a6686f2fSShirley Ma head = next; 1361a6686f2fSShirley Ma } 1362a6686f2fSShirley Ma return -ENOMEM; 1363a6686f2fSShirley Ma } 13643ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 13653ece7826SWillem de Bruijn head = page; 13663ece7826SWillem de Bruijn } 13673ece7826SWillem de Bruijn 13683ece7826SWillem de Bruijn page = head; 13693ece7826SWillem de Bruijn d_off = 0; 13703ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 13713ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 13723ece7826SWillem de Bruijn u32 p_off, p_len, copied; 13733ece7826SWillem de Bruijn struct page *p; 13743ece7826SWillem de Bruijn u8 *vaddr; 1375c613c209SWillem de Bruijn 1376b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1377c613c209SWillem de Bruijn p, p_off, p_len, copied) { 13783ece7826SWillem de Bruijn u32 copy, done = 0; 1379c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 13803ece7826SWillem de Bruijn 13813ece7826SWillem de Bruijn while (done < p_len) { 13823ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 13833ece7826SWillem de Bruijn d_off = 0; 13843ece7826SWillem de Bruijn page = (struct page *)page_private(page); 13853ece7826SWillem de Bruijn } 13863ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 13873ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 13883ece7826SWillem de Bruijn vaddr + p_off + done, copy); 13893ece7826SWillem de Bruijn done += copy; 13903ece7826SWillem de Bruijn d_off += copy; 13913ece7826SWillem de Bruijn } 139251c56b00SEric Dumazet kunmap_atomic(vaddr); 1393c613c209SWillem de Bruijn } 1394a6686f2fSShirley Ma } 1395a6686f2fSShirley Ma 1396a6686f2fSShirley Ma /* skb frags release userspace buffers */ 139702756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1398a8605c60SIan Campbell skb_frag_unref(skb, i); 1399a6686f2fSShirley Ma 1400a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 14013ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 14023ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 140340dadff2SSunghan Suh head = (struct page *)page_private(head); 1404a6686f2fSShirley Ma } 14053ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 14063ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 140748c83012SMichael S. Tsirkin 1408b90ddd56SWillem de Bruijn release: 14091f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1410a6686f2fSShirley Ma return 0; 1411a6686f2fSShirley Ma } 1412dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1413a6686f2fSShirley Ma 1414e0053ec0SHerbert Xu /** 1415e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1416e0053ec0SHerbert Xu * @skb: buffer to clone 1417e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1418e0053ec0SHerbert Xu * 1419e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1420e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1421e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1422e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1423e0053ec0SHerbert Xu * 1424e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1425e0053ec0SHerbert Xu * %GFP_ATOMIC. 1426e0053ec0SHerbert Xu */ 1427e0053ec0SHerbert Xu 1428e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1429e0053ec0SHerbert Xu { 1430d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1431d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1432d0bf4a9eSEric Dumazet skb1); 14336ffe75ebSEric Dumazet struct sk_buff *n; 1434e0053ec0SHerbert Xu 143570008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1436a6686f2fSShirley Ma return NULL; 1437a6686f2fSShirley Ma 1438e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 14392638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 14406ffe75ebSEric Dumazet n = &fclones->skb2; 14412638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1442e0053ec0SHerbert Xu } else { 1443c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1444c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1445c93bdd0eSMel Gorman 1446e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1447e0053ec0SHerbert Xu if (!n) 1448e0053ec0SHerbert Xu return NULL; 1449fe55f6d5SVegard Nossum 1450e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1451e0053ec0SHerbert Xu } 1452e0053ec0SHerbert Xu 1453e0053ec0SHerbert Xu return __skb_clone(n, skb); 14541da177e4SLinus Torvalds } 1455b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 14561da177e4SLinus Torvalds 1457b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1458f5b17294SPravin B Shelar { 1459030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1460030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1461030737bcSEric Dumazet skb->csum_start += off; 1462f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1463f5b17294SPravin B Shelar skb->transport_header += off; 1464f5b17294SPravin B Shelar skb->network_header += off; 1465f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1466f5b17294SPravin B Shelar skb->mac_header += off; 1467f5b17294SPravin B Shelar skb->inner_transport_header += off; 1468f5b17294SPravin B Shelar skb->inner_network_header += off; 1469aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1470f5b17294SPravin B Shelar } 1471b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1472f5b17294SPravin B Shelar 147308303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 14741da177e4SLinus Torvalds { 1475dec18810SHerbert Xu __copy_skb_header(new, old); 1476dec18810SHerbert Xu 14777967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 14787967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 14797967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 14801da177e4SLinus Torvalds } 148108303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 14821da177e4SLinus Torvalds 1483c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1484c93bdd0eSMel Gorman { 1485c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1486c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1487c93bdd0eSMel Gorman return 0; 1488c93bdd0eSMel Gorman } 1489c93bdd0eSMel Gorman 14901da177e4SLinus Torvalds /** 14911da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 14921da177e4SLinus Torvalds * @skb: buffer to copy 14931da177e4SLinus Torvalds * @gfp_mask: allocation priority 14941da177e4SLinus Torvalds * 14951da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 14961da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 14971da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 14981da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 14991da177e4SLinus Torvalds * 15001da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 15011da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 15021da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 15031da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 15041da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 15051da177e4SLinus Torvalds */ 15061da177e4SLinus Torvalds 1507dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 15081da177e4SLinus Torvalds { 15096602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1510ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1511c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1512c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 15136602cebbSEric Dumazet 15141da177e4SLinus Torvalds if (!n) 15151da177e4SLinus Torvalds return NULL; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds /* Set the data pointer */ 15181da177e4SLinus Torvalds skb_reserve(n, headerlen); 15191da177e4SLinus Torvalds /* Set the tail pointer and length */ 15201da177e4SLinus Torvalds skb_put(n, skb->len); 15211da177e4SLinus Torvalds 15229f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 15231da177e4SLinus Torvalds 152408303c18SIlya Lesokhin skb_copy_header(n, skb); 15251da177e4SLinus Torvalds return n; 15261da177e4SLinus Torvalds } 1527b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /** 1530bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 15311da177e4SLinus Torvalds * @skb: buffer to copy 1532117632e6SEric Dumazet * @headroom: headroom of new skb 15331da177e4SLinus Torvalds * @gfp_mask: allocation priority 1534bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1535bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1536bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 15371da177e4SLinus Torvalds * 15381da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 15391da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 15401da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 15411da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 15421da177e4SLinus Torvalds * or the pointer to the buffer on success. 15431da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 15441da177e4SLinus Torvalds */ 15451da177e4SLinus Torvalds 1546bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1547bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 15481da177e4SLinus Torvalds { 1549117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1550bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1551bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 15526602cebbSEric Dumazet 15531da177e4SLinus Torvalds if (!n) 15541da177e4SLinus Torvalds goto out; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds /* Set the data pointer */ 1557117632e6SEric Dumazet skb_reserve(n, headroom); 15581da177e4SLinus Torvalds /* Set the tail pointer and length */ 15591da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 15601da177e4SLinus Torvalds /* Copy the bytes */ 1561d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 15621da177e4SLinus Torvalds 156325f484a6SHerbert Xu n->truesize += skb->data_len; 15641da177e4SLinus Torvalds n->data_len = skb->data_len; 15651da177e4SLinus Torvalds n->len = skb->len; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 15681da177e4SLinus Torvalds int i; 15691da177e4SLinus Torvalds 15701f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 15711f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 15721511022cSDan Carpenter kfree_skb(n); 15731511022cSDan Carpenter n = NULL; 1574a6686f2fSShirley Ma goto out; 1575a6686f2fSShirley Ma } 15761da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15771da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1578ea2ab693SIan Campbell skb_frag_ref(skb, i); 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 15811da177e4SLinus Torvalds } 15821da177e4SLinus Torvalds 158321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 15841da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 15851da177e4SLinus Torvalds skb_clone_fraglist(n); 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 158808303c18SIlya Lesokhin skb_copy_header(n, skb); 15891da177e4SLinus Torvalds out: 15901da177e4SLinus Torvalds return n; 15911da177e4SLinus Torvalds } 1592bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /** 15951da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 15961da177e4SLinus Torvalds * @skb: buffer to reallocate 15971da177e4SLinus Torvalds * @nhead: room to add at head 15981da177e4SLinus Torvalds * @ntail: room to add at tail 15991da177e4SLinus Torvalds * @gfp_mask: allocation priority 16001da177e4SLinus Torvalds * 1601bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1602bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 16031da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 16041da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 16051da177e4SLinus Torvalds * 16061da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 16071da177e4SLinus Torvalds * reloaded after call to this function. 16081da177e4SLinus Torvalds */ 16091da177e4SLinus Torvalds 161086a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1611dd0fc66fSAl Viro gfp_t gfp_mask) 16121da177e4SLinus Torvalds { 1613158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1614158f323bSEric Dumazet int size = osize + nhead + ntail; 16151da177e4SLinus Torvalds long off; 1616158f323bSEric Dumazet u8 *data; 16171da177e4SLinus Torvalds 16184edd87adSHerbert Xu BUG_ON(nhead < 0); 16194edd87adSHerbert Xu 16209f77fad3STim Hansen BUG_ON(skb_shared(skb)); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 16231da177e4SLinus Torvalds 1624c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1625c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1626c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1627c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 16281da177e4SLinus Torvalds if (!data) 16291da177e4SLinus Torvalds goto nodata; 163087151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 16336602cebbSEric Dumazet * optimized for the cases when header is void. 16346602cebbSEric Dumazet */ 16356602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 16366602cebbSEric Dumazet 16376602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 16386602cebbSEric Dumazet skb_shinfo(skb), 1639fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 16401da177e4SLinus Torvalds 16413e24591aSAlexander Duyck /* 16423e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 16433e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 16443e24591aSAlexander Duyck * be since all we did is relocate the values 16453e24591aSAlexander Duyck */ 16463e24591aSAlexander Duyck if (skb_cloned(skb)) { 164770008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1648a6686f2fSShirley Ma goto nofrags; 16491f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1650c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 16511da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1652ea2ab693SIan Campbell skb_frag_ref(skb, i); 16531da177e4SLinus Torvalds 165421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 16551da177e4SLinus Torvalds skb_clone_fraglist(skb); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds skb_release_data(skb); 16583e24591aSAlexander Duyck } else { 16593e24591aSAlexander Duyck skb_free_head(skb); 16601fd63041SEric Dumazet } 16611da177e4SLinus Torvalds off = (data + nhead) - skb->head; 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds skb->head = data; 1664d3836f21SEric Dumazet skb->head_frag = 0; 16651da177e4SLinus Torvalds skb->data += off; 16664305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 16674305b541SArnaldo Carvalho de Melo skb->end = size; 166856eb8882SPatrick McHardy off = nhead; 16694305b541SArnaldo Carvalho de Melo #else 16704305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 167156eb8882SPatrick McHardy #endif 167227a884dcSArnaldo Carvalho de Melo skb->tail += off; 1673b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 16741da177e4SLinus Torvalds skb->cloned = 0; 1675334a8132SPatrick McHardy skb->hdr_len = 0; 16761da177e4SLinus Torvalds skb->nohdr = 0; 16771da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1678158f323bSEric Dumazet 1679de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1680de8f3a83SDaniel Borkmann 1681158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1682158f323bSEric Dumazet * For the moment, we really care of rx path, or 1683158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1684158f323bSEric Dumazet */ 1685158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1686158f323bSEric Dumazet skb->truesize += size - osize; 1687158f323bSEric Dumazet 16881da177e4SLinus Torvalds return 0; 16891da177e4SLinus Torvalds 1690a6686f2fSShirley Ma nofrags: 1691a6686f2fSShirley Ma kfree(data); 16921da177e4SLinus Torvalds nodata: 16931da177e4SLinus Torvalds return -ENOMEM; 16941da177e4SLinus Torvalds } 1695b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds struct sk_buff *skb2; 17021da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds if (delta <= 0) 17051da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 17061da177e4SLinus Torvalds else { 17071da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 17081da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 17091da177e4SLinus Torvalds GFP_ATOMIC)) { 17101da177e4SLinus Torvalds kfree_skb(skb2); 17111da177e4SLinus Torvalds skb2 = NULL; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds return skb2; 17151da177e4SLinus Torvalds } 1716b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /** 17191da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 17201da177e4SLinus Torvalds * @skb: buffer to copy 17211da177e4SLinus Torvalds * @newheadroom: new free bytes at head 17221da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 17231da177e4SLinus Torvalds * @gfp_mask: allocation priority 17241da177e4SLinus Torvalds * 17251da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 17261da177e4SLinus Torvalds * allocate additional space. 17271da177e4SLinus Torvalds * 17281da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 17291da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 17301da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 17311da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 17321da177e4SLinus Torvalds * 17331da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 17341da177e4SLinus Torvalds * is called from an interrupt. 17351da177e4SLinus Torvalds */ 17361da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 173786a76cafSVictor Fusco int newheadroom, int newtailroom, 1738dd0fc66fSAl Viro gfp_t gfp_mask) 17391da177e4SLinus Torvalds { 17401da177e4SLinus Torvalds /* 17411da177e4SLinus Torvalds * Allocate the copy buffer 17421da177e4SLinus Torvalds */ 1743c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1744c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1745c93bdd0eSMel Gorman NUMA_NO_NODE); 1746efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 17471da177e4SLinus Torvalds int head_copy_len, head_copy_off; 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds if (!n) 17501da177e4SLinus Torvalds return NULL; 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds skb_reserve(n, newheadroom); 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* Set the tail pointer and length */ 17551da177e4SLinus Torvalds skb_put(n, skb->len); 17561da177e4SLinus Torvalds 1757efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 17581da177e4SLinus Torvalds head_copy_off = 0; 17591da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 17601da177e4SLinus Torvalds head_copy_len = newheadroom; 17611da177e4SLinus Torvalds else 17621da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds /* Copy the linear header and data. */ 17659f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 17669f77fad3STim Hansen skb->len + head_copy_len)); 17671da177e4SLinus Torvalds 176808303c18SIlya Lesokhin skb_copy_header(n, skb); 17691da177e4SLinus Torvalds 1770030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1771efd1e8d5SPatrick McHardy 17721da177e4SLinus Torvalds return n; 17731da177e4SLinus Torvalds } 1774b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds /** 1777cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 17781da177e4SLinus Torvalds * @skb: buffer to pad 17791da177e4SLinus Torvalds * @pad: space to pad 1780cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 17811da177e4SLinus Torvalds * 17821da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 17831da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 17841da177e4SLinus Torvalds * beyond the buffer end onto the wire. 17851da177e4SLinus Torvalds * 1786cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1787cd0a137aSFlorian Fainelli * if @free_on_error is true. 17881da177e4SLinus Torvalds */ 17891da177e4SLinus Torvalds 1790cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 17911da177e4SLinus Torvalds { 17925b057c6bSHerbert Xu int err; 17935b057c6bSHerbert Xu int ntail; 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 17965b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 17971da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 17985b057c6bSHerbert Xu return 0; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 18014305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 18025b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 18035b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 18045b057c6bSHerbert Xu if (unlikely(err)) 18055b057c6bSHerbert Xu goto free_skb; 18065b057c6bSHerbert Xu } 18075b057c6bSHerbert Xu 18085b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 18095b057c6bSHerbert Xu * to be audited. 18105b057c6bSHerbert Xu */ 18115b057c6bSHerbert Xu err = skb_linearize(skb); 18125b057c6bSHerbert Xu if (unlikely(err)) 18135b057c6bSHerbert Xu goto free_skb; 18145b057c6bSHerbert Xu 18155b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 18165b057c6bSHerbert Xu return 0; 18175b057c6bSHerbert Xu 18185b057c6bSHerbert Xu free_skb: 1819cd0a137aSFlorian Fainelli if (free_on_error) 18201da177e4SLinus Torvalds kfree_skb(skb); 18215b057c6bSHerbert Xu return err; 18221da177e4SLinus Torvalds } 1823cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 18241da177e4SLinus Torvalds 18250dde3e16SIlpo Järvinen /** 18260c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 18270c7ddf36SMathias Krause * @skb: start of the buffer to use 18280c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 18290c7ddf36SMathias Krause * @len: amount of data to add 18300c7ddf36SMathias Krause * 18310c7ddf36SMathias Krause * This function extends the used data area of the potentially 18320c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 18330c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 18340c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 18350c7ddf36SMathias Krause * returned. 18360c7ddf36SMathias Krause */ 18370c7ddf36SMathias Krause 18384df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 18390c7ddf36SMathias Krause { 18400c7ddf36SMathias Krause if (tail != skb) { 18410c7ddf36SMathias Krause skb->data_len += len; 18420c7ddf36SMathias Krause skb->len += len; 18430c7ddf36SMathias Krause } 18440c7ddf36SMathias Krause return skb_put(tail, len); 18450c7ddf36SMathias Krause } 18460c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 18470c7ddf36SMathias Krause 18480c7ddf36SMathias Krause /** 18490dde3e16SIlpo Järvinen * skb_put - add data to a buffer 18500dde3e16SIlpo Järvinen * @skb: buffer to use 18510dde3e16SIlpo Järvinen * @len: amount of data to add 18520dde3e16SIlpo Järvinen * 18530dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 18540dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 18550dde3e16SIlpo Järvinen * first byte of the extra data is returned. 18560dde3e16SIlpo Järvinen */ 18574df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 18580dde3e16SIlpo Järvinen { 18594df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 18600dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 18610dde3e16SIlpo Järvinen skb->tail += len; 18620dde3e16SIlpo Järvinen skb->len += len; 18630dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 18640dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 18650dde3e16SIlpo Järvinen return tmp; 18660dde3e16SIlpo Järvinen } 18670dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 18680dde3e16SIlpo Järvinen 18696be8ac2fSIlpo Järvinen /** 1870c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1871c2aa270aSIlpo Järvinen * @skb: buffer to use 1872c2aa270aSIlpo Järvinen * @len: amount of data to add 1873c2aa270aSIlpo Järvinen * 1874c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1875c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1876c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1877c2aa270aSIlpo Järvinen */ 1878d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1879c2aa270aSIlpo Järvinen { 1880c2aa270aSIlpo Järvinen skb->data -= len; 1881c2aa270aSIlpo Järvinen skb->len += len; 1882c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 1883c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1884c2aa270aSIlpo Järvinen return skb->data; 1885c2aa270aSIlpo Järvinen } 1886c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1887c2aa270aSIlpo Järvinen 1888c2aa270aSIlpo Järvinen /** 18896be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 18906be8ac2fSIlpo Järvinen * @skb: buffer to use 18916be8ac2fSIlpo Järvinen * @len: amount of data to remove 18926be8ac2fSIlpo Järvinen * 18936be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 18946be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 18956be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 18966be8ac2fSIlpo Järvinen * the old data. 18976be8ac2fSIlpo Järvinen */ 1898af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 18996be8ac2fSIlpo Järvinen { 190047d29646SDavid S. Miller return skb_pull_inline(skb, len); 19016be8ac2fSIlpo Järvinen } 19026be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 19036be8ac2fSIlpo Järvinen 1904419ae74eSIlpo Järvinen /** 1905419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1906419ae74eSIlpo Järvinen * @skb: buffer to alter 1907419ae74eSIlpo Järvinen * @len: new length 1908419ae74eSIlpo Järvinen * 1909419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1910419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1911419ae74eSIlpo Järvinen * The skb must be linear. 1912419ae74eSIlpo Järvinen */ 1913419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1914419ae74eSIlpo Järvinen { 1915419ae74eSIlpo Järvinen if (skb->len > len) 1916419ae74eSIlpo Järvinen __skb_trim(skb, len); 1917419ae74eSIlpo Järvinen } 1918419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1919419ae74eSIlpo Järvinen 19203cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds 19233cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 19241da177e4SLinus Torvalds { 192527b437c8SHerbert Xu struct sk_buff **fragp; 192627b437c8SHerbert Xu struct sk_buff *frag; 19271da177e4SLinus Torvalds int offset = skb_headlen(skb); 19281da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 19291da177e4SLinus Torvalds int i; 193027b437c8SHerbert Xu int err; 193127b437c8SHerbert Xu 193227b437c8SHerbert Xu if (skb_cloned(skb) && 193327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 193427b437c8SHerbert Xu return err; 19351da177e4SLinus Torvalds 1936f4d26fb3SHerbert Xu i = 0; 1937f4d26fb3SHerbert Xu if (offset >= len) 1938f4d26fb3SHerbert Xu goto drop_pages; 1939f4d26fb3SHerbert Xu 1940f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 19419e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 194227b437c8SHerbert Xu 194327b437c8SHerbert Xu if (end < len) { 19441da177e4SLinus Torvalds offset = end; 194527b437c8SHerbert Xu continue; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19489e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 194927b437c8SHerbert Xu 1950f4d26fb3SHerbert Xu drop_pages: 195127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 195227b437c8SHerbert Xu 195327b437c8SHerbert Xu for (; i < nfrags; i++) 1954ea2ab693SIan Campbell skb_frag_unref(skb, i); 195527b437c8SHerbert Xu 195621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 195727b437c8SHerbert Xu skb_drop_fraglist(skb); 1958f4d26fb3SHerbert Xu goto done; 195927b437c8SHerbert Xu } 196027b437c8SHerbert Xu 196127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 196227b437c8SHerbert Xu fragp = &frag->next) { 196327b437c8SHerbert Xu int end = offset + frag->len; 196427b437c8SHerbert Xu 196527b437c8SHerbert Xu if (skb_shared(frag)) { 196627b437c8SHerbert Xu struct sk_buff *nfrag; 196727b437c8SHerbert Xu 196827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 196927b437c8SHerbert Xu if (unlikely(!nfrag)) 197027b437c8SHerbert Xu return -ENOMEM; 197127b437c8SHerbert Xu 197227b437c8SHerbert Xu nfrag->next = frag->next; 197385bb2a60SEric Dumazet consume_skb(frag); 197427b437c8SHerbert Xu frag = nfrag; 197527b437c8SHerbert Xu *fragp = frag; 197627b437c8SHerbert Xu } 197727b437c8SHerbert Xu 197827b437c8SHerbert Xu if (end < len) { 197927b437c8SHerbert Xu offset = end; 198027b437c8SHerbert Xu continue; 198127b437c8SHerbert Xu } 198227b437c8SHerbert Xu 198327b437c8SHerbert Xu if (end > len && 198427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 198527b437c8SHerbert Xu return err; 198627b437c8SHerbert Xu 198727b437c8SHerbert Xu if (frag->next) 198827b437c8SHerbert Xu skb_drop_list(&frag->next); 198927b437c8SHerbert Xu break; 199027b437c8SHerbert Xu } 199127b437c8SHerbert Xu 1992f4d26fb3SHerbert Xu done: 199327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 19941da177e4SLinus Torvalds skb->data_len -= skb->len - len; 19951da177e4SLinus Torvalds skb->len = len; 19961da177e4SLinus Torvalds } else { 19971da177e4SLinus Torvalds skb->len = len; 19981da177e4SLinus Torvalds skb->data_len = 0; 199927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 2002c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2003c21b48ccSEric Dumazet skb_condense(skb); 20041da177e4SLinus Torvalds return 0; 20051da177e4SLinus Torvalds } 2006b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 20071da177e4SLinus Torvalds 200888078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 200988078d98SEric Dumazet */ 201088078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 201188078d98SEric Dumazet { 201288078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 201388078d98SEric Dumazet int delta = skb->len - len; 201488078d98SEric Dumazet 2015d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2016d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2017d55bef50SDimitris Michailidis len); 201854970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 201954970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 202054970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 202154970a2fSVasily Averin 202254970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 202354970a2fSVasily Averin return -EINVAL; 202488078d98SEric Dumazet } 202588078d98SEric Dumazet return __pskb_trim(skb, len); 202688078d98SEric Dumazet } 202788078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 202888078d98SEric Dumazet 20291da177e4SLinus Torvalds /** 20301da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 20311da177e4SLinus Torvalds * @skb: buffer to reallocate 20321da177e4SLinus Torvalds * @delta: number of bytes to advance tail 20331da177e4SLinus Torvalds * 20341da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 20351da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 20361da177e4SLinus Torvalds * data from fragmented part. 20371da177e4SLinus Torvalds * 20381da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 20391da177e4SLinus Torvalds * 20401da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 20411da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 20421da177e4SLinus Torvalds * 20431da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20441da177e4SLinus Torvalds * reloaded after call to this function. 20451da177e4SLinus Torvalds */ 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 20481da177e4SLinus Torvalds * when it is necessary. 20491da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 20501da177e4SLinus Torvalds * 2. It may change skb pointers. 20511da177e4SLinus Torvalds * 20521da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 20531da177e4SLinus Torvalds */ 2054af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 20551da177e4SLinus Torvalds { 20561da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 20571da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 20581da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 20591da177e4SLinus Torvalds */ 20604305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 20631da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 20641da177e4SLinus Torvalds GFP_ATOMIC)) 20651da177e4SLinus Torvalds return NULL; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20689f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 20699f77fad3STim Hansen skb_tail_pointer(skb), delta)); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 20721da177e4SLinus Torvalds * size of pulled pages. Superb. 20731da177e4SLinus Torvalds */ 207421dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 20751da177e4SLinus Torvalds goto pull_pages; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 20781da177e4SLinus Torvalds eat = delta; 20791da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20809e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 20819e903e08SEric Dumazet 20829e903e08SEric Dumazet if (size >= eat) 20831da177e4SLinus Torvalds goto pull_pages; 20849e903e08SEric Dumazet eat -= size; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 208809001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 20891da177e4SLinus Torvalds * but taking into account that pulling is expected to 20901da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 20911da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 20921da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 20931da177e4SLinus Torvalds */ 20941da177e4SLinus Torvalds if (eat) { 20951da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 20961da177e4SLinus Torvalds struct sk_buff *clone = NULL; 20971da177e4SLinus Torvalds struct sk_buff *insp = NULL; 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds do { 21001da177e4SLinus Torvalds if (list->len <= eat) { 21011da177e4SLinus Torvalds /* Eaten as whole. */ 21021da177e4SLinus Torvalds eat -= list->len; 21031da177e4SLinus Torvalds list = list->next; 21041da177e4SLinus Torvalds insp = list; 21051da177e4SLinus Torvalds } else { 21061da177e4SLinus Torvalds /* Eaten partially. */ 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds if (skb_shared(list)) { 21091da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 21101da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 21111da177e4SLinus Torvalds if (!clone) 21121da177e4SLinus Torvalds return NULL; 21131da177e4SLinus Torvalds insp = list->next; 21141da177e4SLinus Torvalds list = clone; 21151da177e4SLinus Torvalds } else { 21161da177e4SLinus Torvalds /* This may be pulled without 21171da177e4SLinus Torvalds * problems. */ 21181da177e4SLinus Torvalds insp = list; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 21211da177e4SLinus Torvalds kfree_skb(clone); 21221da177e4SLinus Torvalds return NULL; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds break; 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds } while (eat); 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds /* Free pulled out fragments. */ 21291da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 21301da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 21311da177e4SLinus Torvalds kfree_skb(list); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds /* And insert new clone at head. */ 21341da177e4SLinus Torvalds if (clone) { 21351da177e4SLinus Torvalds clone->next = list; 21361da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds pull_pages: 21421da177e4SLinus Torvalds eat = delta; 21431da177e4SLinus Torvalds k = 0; 21441da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21459e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 21469e903e08SEric Dumazet 21479e903e08SEric Dumazet if (size <= eat) { 2148ea2ab693SIan Campbell skb_frag_unref(skb, i); 21499e903e08SEric Dumazet eat -= size; 21501da177e4SLinus Torvalds } else { 2151b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2152b54c9d5bSJonathan Lemon 2153b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 21541da177e4SLinus Torvalds if (eat) { 2155b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2156b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 21573ccc6c6fSlinzhang if (!i) 21583ccc6c6fSlinzhang goto end; 21591da177e4SLinus Torvalds eat = 0; 21601da177e4SLinus Torvalds } 21611da177e4SLinus Torvalds k++; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 21651da177e4SLinus Torvalds 21663ccc6c6fSlinzhang end: 21671da177e4SLinus Torvalds skb->tail += delta; 21681da177e4SLinus Torvalds skb->data_len -= delta; 21691da177e4SLinus Torvalds 21701f8b977aSWillem de Bruijn if (!skb->data_len) 21711f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 21721f8b977aSWillem de Bruijn 217327a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 21741da177e4SLinus Torvalds } 2175b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 21761da177e4SLinus Torvalds 217722019b17SEric Dumazet /** 217822019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 217922019b17SEric Dumazet * @skb: source skb 218022019b17SEric Dumazet * @offset: offset in source 218122019b17SEric Dumazet * @to: destination buffer 218222019b17SEric Dumazet * @len: number of bytes to copy 218322019b17SEric Dumazet * 218422019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 218522019b17SEric Dumazet * destination buffer. 218622019b17SEric Dumazet * 218722019b17SEric Dumazet * CAUTION ! : 218822019b17SEric Dumazet * If its prototype is ever changed, 218922019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 219022019b17SEric Dumazet * since it is called from BPF assembly code. 219122019b17SEric Dumazet */ 21921da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 21931da177e4SLinus Torvalds { 21941a028e50SDavid S. Miller int start = skb_headlen(skb); 2195fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2196fbb398a8SDavid S. Miller int i, copy; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds if (offset > (int)skb->len - len) 21991da177e4SLinus Torvalds goto fault; 22001da177e4SLinus Torvalds 22011da177e4SLinus Torvalds /* Copy header. */ 22021a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 22031da177e4SLinus Torvalds if (copy > len) 22041da177e4SLinus Torvalds copy = len; 2205d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 22061da177e4SLinus Torvalds if ((len -= copy) == 0) 22071da177e4SLinus Torvalds return 0; 22081da177e4SLinus Torvalds offset += copy; 22091da177e4SLinus Torvalds to += copy; 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22131a028e50SDavid S. Miller int end; 221451c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 22151da177e4SLinus Torvalds 2216547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22171a028e50SDavid S. Miller 221851c56b00SEric Dumazet end = start + skb_frag_size(f); 22191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2220c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2221c613c209SWillem de Bruijn struct page *p; 22221da177e4SLinus Torvalds u8 *vaddr; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (copy > len) 22251da177e4SLinus Torvalds copy = len; 22261da177e4SLinus Torvalds 2227c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2228b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2229c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2230c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2231c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 223251c56b00SEric Dumazet kunmap_atomic(vaddr); 2233c613c209SWillem de Bruijn } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds if ((len -= copy) == 0) 22361da177e4SLinus Torvalds return 0; 22371da177e4SLinus Torvalds offset += copy; 22381da177e4SLinus Torvalds to += copy; 22391da177e4SLinus Torvalds } 22401a028e50SDavid S. Miller start = end; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 2243fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 22441a028e50SDavid S. Miller int end; 22451da177e4SLinus Torvalds 2246547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22471a028e50SDavid S. Miller 2248fbb398a8SDavid S. Miller end = start + frag_iter->len; 22491da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22501da177e4SLinus Torvalds if (copy > len) 22511da177e4SLinus Torvalds copy = len; 2252fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 22531da177e4SLinus Torvalds goto fault; 22541da177e4SLinus Torvalds if ((len -= copy) == 0) 22551da177e4SLinus Torvalds return 0; 22561da177e4SLinus Torvalds offset += copy; 22571da177e4SLinus Torvalds to += copy; 22581da177e4SLinus Torvalds } 22591a028e50SDavid S. Miller start = end; 22601da177e4SLinus Torvalds } 2261a6686f2fSShirley Ma 22621da177e4SLinus Torvalds if (!len) 22631da177e4SLinus Torvalds return 0; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds fault: 22661da177e4SLinus Torvalds return -EFAULT; 22671da177e4SLinus Torvalds } 2268b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 22691da177e4SLinus Torvalds 22709c55e01cSJens Axboe /* 22719c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 22729c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 22739c55e01cSJens Axboe */ 22749c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 22759c55e01cSJens Axboe { 22768b9d3728SJarek Poplawski put_page(spd->pages[i]); 22778b9d3728SJarek Poplawski } 22789c55e01cSJens Axboe 2279a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 22804fb66994SJarek Poplawski unsigned int *offset, 228118aafc62SEric Dumazet struct sock *sk) 22828b9d3728SJarek Poplawski { 22835640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 22848b9d3728SJarek Poplawski 22855640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 22868b9d3728SJarek Poplawski return NULL; 22874fb66994SJarek Poplawski 22885640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 22894fb66994SJarek Poplawski 22905640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 22915640f768SEric Dumazet page_address(page) + *offset, *len); 22925640f768SEric Dumazet *offset = pfrag->offset; 22935640f768SEric Dumazet pfrag->offset += *len; 22944fb66994SJarek Poplawski 22955640f768SEric Dumazet return pfrag->page; 22969c55e01cSJens Axboe } 22979c55e01cSJens Axboe 229841c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 229941c73a0dSEric Dumazet struct page *page, 230041c73a0dSEric Dumazet unsigned int offset) 230141c73a0dSEric Dumazet { 230241c73a0dSEric Dumazet return spd->nr_pages && 230341c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 230441c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 230541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 230641c73a0dSEric Dumazet } 230741c73a0dSEric Dumazet 23089c55e01cSJens Axboe /* 23099c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 23109c55e01cSJens Axboe */ 2311a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 231235f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 23134fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 231418aafc62SEric Dumazet bool linear, 23157a67e56fSJarek Poplawski struct sock *sk) 23169c55e01cSJens Axboe { 231741c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2318a108d5f3SDavid S. Miller return true; 23199c55e01cSJens Axboe 23208b9d3728SJarek Poplawski if (linear) { 232118aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 23228b9d3728SJarek Poplawski if (!page) 2323a108d5f3SDavid S. Miller return true; 232441c73a0dSEric Dumazet } 232541c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 232641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2327a108d5f3SDavid S. Miller return false; 232841c73a0dSEric Dumazet } 23298b9d3728SJarek Poplawski get_page(page); 23309c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 23314fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 23329c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 23339c55e01cSJens Axboe spd->nr_pages++; 23348b9d3728SJarek Poplawski 2335a108d5f3SDavid S. Miller return false; 23369c55e01cSJens Axboe } 23379c55e01cSJens Axboe 2338a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 23392870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 234018aafc62SEric Dumazet unsigned int *len, 2341d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 234235f3d14dSJens Axboe struct sock *sk, 234335f3d14dSJens Axboe struct pipe_inode_info *pipe) 23449c55e01cSJens Axboe { 23452870c43dSOctavian Purdila if (!*len) 2346a108d5f3SDavid S. Miller return true; 23479c55e01cSJens Axboe 23482870c43dSOctavian Purdila /* skip this segment if already processed */ 23492870c43dSOctavian Purdila if (*off >= plen) { 23502870c43dSOctavian Purdila *off -= plen; 2351a108d5f3SDavid S. Miller return false; 23522870c43dSOctavian Purdila } 23532870c43dSOctavian Purdila 23542870c43dSOctavian Purdila /* ignore any bits we already processed */ 23559ca1b22dSEric Dumazet poff += *off; 23569ca1b22dSEric Dumazet plen -= *off; 23572870c43dSOctavian Purdila *off = 0; 23582870c43dSOctavian Purdila 235918aafc62SEric Dumazet do { 236018aafc62SEric Dumazet unsigned int flen = min(*len, plen); 23612870c43dSOctavian Purdila 236218aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 236318aafc62SEric Dumazet linear, sk)) 2364a108d5f3SDavid S. Miller return true; 236518aafc62SEric Dumazet poff += flen; 236618aafc62SEric Dumazet plen -= flen; 23672870c43dSOctavian Purdila *len -= flen; 236818aafc62SEric Dumazet } while (*len && plen); 23692870c43dSOctavian Purdila 2370a108d5f3SDavid S. Miller return false; 2371db43a282SOctavian Purdila } 23729c55e01cSJens Axboe 23739c55e01cSJens Axboe /* 2374a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 23752870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 23769c55e01cSJens Axboe */ 2377a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 237835f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 237935f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 23802870c43dSOctavian Purdila { 23812870c43dSOctavian Purdila int seg; 2382fa9835e5STom Herbert struct sk_buff *iter; 23839c55e01cSJens Axboe 23841d0c0b32SEric Dumazet /* map the linear part : 23852996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 23862996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 23872996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 23889c55e01cSJens Axboe */ 23892870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 23902870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 23912870c43dSOctavian Purdila skb_headlen(skb), 239218aafc62SEric Dumazet offset, len, spd, 23933a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 23941d0c0b32SEric Dumazet sk, pipe)) 2395a108d5f3SDavid S. Miller return true; 23969c55e01cSJens Axboe 23979c55e01cSJens Axboe /* 23989c55e01cSJens Axboe * then map the fragments 23999c55e01cSJens Axboe */ 24009c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 24019c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 24029c55e01cSJens Axboe 2403ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2404b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 240518aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2406a108d5f3SDavid S. Miller return true; 24079c55e01cSJens Axboe } 24089c55e01cSJens Axboe 2409fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2410fa9835e5STom Herbert if (*offset >= iter->len) { 2411fa9835e5STom Herbert *offset -= iter->len; 2412fa9835e5STom Herbert continue; 2413fa9835e5STom Herbert } 2414fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2415fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2416fa9835e5STom Herbert * case. 2417fa9835e5STom Herbert */ 2418fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2419fa9835e5STom Herbert return true; 2420fa9835e5STom Herbert } 2421fa9835e5STom Herbert 2422a108d5f3SDavid S. Miller return false; 24239c55e01cSJens Axboe } 24249c55e01cSJens Axboe 24259c55e01cSJens Axboe /* 24269c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2427fa9835e5STom Herbert * the fragments, and the frag list. 24289c55e01cSJens Axboe */ 2429a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 24309c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 243125869262SAl Viro unsigned int flags) 24329c55e01cSJens Axboe { 243341c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 243441c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 24359c55e01cSJens Axboe struct splice_pipe_desc spd = { 24369c55e01cSJens Axboe .pages = pages, 24379c55e01cSJens Axboe .partial = partial, 2438047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 243928a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 24409c55e01cSJens Axboe .spd_release = sock_spd_release, 24419c55e01cSJens Axboe }; 244235f3d14dSJens Axboe int ret = 0; 244335f3d14dSJens Axboe 2444fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 24459c55e01cSJens Axboe 2446a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 244725869262SAl Viro ret = splice_to_pipe(pipe, &spd); 24489c55e01cSJens Axboe 244935f3d14dSJens Axboe return ret; 24509c55e01cSJens Axboe } 24512b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 24529c55e01cSJens Axboe 245320bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */ 245420bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 245520bf50deSTom Herbert int len) 245620bf50deSTom Herbert { 245720bf50deSTom Herbert unsigned int orig_len = len; 245820bf50deSTom Herbert struct sk_buff *head = skb; 245920bf50deSTom Herbert unsigned short fragidx; 246020bf50deSTom Herbert int slen, ret; 246120bf50deSTom Herbert 246220bf50deSTom Herbert do_frag_list: 246320bf50deSTom Herbert 246420bf50deSTom Herbert /* Deal with head data */ 246520bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 246620bf50deSTom Herbert struct kvec kv; 246720bf50deSTom Herbert struct msghdr msg; 246820bf50deSTom Herbert 246920bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 247020bf50deSTom Herbert kv.iov_base = skb->data + offset; 2471db5980d8SJohn Fastabend kv.iov_len = slen; 247220bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2473bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 247420bf50deSTom Herbert 247520bf50deSTom Herbert ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 247620bf50deSTom Herbert if (ret <= 0) 247720bf50deSTom Herbert goto error; 247820bf50deSTom Herbert 247920bf50deSTom Herbert offset += ret; 248020bf50deSTom Herbert len -= ret; 248120bf50deSTom Herbert } 248220bf50deSTom Herbert 248320bf50deSTom Herbert /* All the data was skb head? */ 248420bf50deSTom Herbert if (!len) 248520bf50deSTom Herbert goto out; 248620bf50deSTom Herbert 248720bf50deSTom Herbert /* Make offset relative to start of frags */ 248820bf50deSTom Herbert offset -= skb_headlen(skb); 248920bf50deSTom Herbert 249020bf50deSTom Herbert /* Find where we are in frag list */ 249120bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 249220bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 249320bf50deSTom Herbert 2494d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 249520bf50deSTom Herbert break; 249620bf50deSTom Herbert 2497d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 249820bf50deSTom Herbert } 249920bf50deSTom Herbert 250020bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 250120bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 250220bf50deSTom Herbert 2503d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 250420bf50deSTom Herbert 250520bf50deSTom Herbert while (slen) { 2506d8e18a51SMatthew Wilcox (Oracle) ret = kernel_sendpage_locked(sk, skb_frag_page(frag), 2507b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 250820bf50deSTom Herbert slen, MSG_DONTWAIT); 250920bf50deSTom Herbert if (ret <= 0) 251020bf50deSTom Herbert goto error; 251120bf50deSTom Herbert 251220bf50deSTom Herbert len -= ret; 251320bf50deSTom Herbert offset += ret; 251420bf50deSTom Herbert slen -= ret; 251520bf50deSTom Herbert } 251620bf50deSTom Herbert 251720bf50deSTom Herbert offset = 0; 251820bf50deSTom Herbert } 251920bf50deSTom Herbert 252020bf50deSTom Herbert if (len) { 252120bf50deSTom Herbert /* Process any frag lists */ 252220bf50deSTom Herbert 252320bf50deSTom Herbert if (skb == head) { 252420bf50deSTom Herbert if (skb_has_frag_list(skb)) { 252520bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 252620bf50deSTom Herbert goto do_frag_list; 252720bf50deSTom Herbert } 252820bf50deSTom Herbert } else if (skb->next) { 252920bf50deSTom Herbert skb = skb->next; 253020bf50deSTom Herbert goto do_frag_list; 253120bf50deSTom Herbert } 253220bf50deSTom Herbert } 253320bf50deSTom Herbert 253420bf50deSTom Herbert out: 253520bf50deSTom Herbert return orig_len - len; 253620bf50deSTom Herbert 253720bf50deSTom Herbert error: 253820bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 253920bf50deSTom Herbert } 254020bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 254120bf50deSTom Herbert 2542357b40a1SHerbert Xu /** 2543357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2544357b40a1SHerbert Xu * @skb: destination buffer 2545357b40a1SHerbert Xu * @offset: offset in destination 2546357b40a1SHerbert Xu * @from: source buffer 2547357b40a1SHerbert Xu * @len: number of bytes to copy 2548357b40a1SHerbert Xu * 2549357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2550357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2551357b40a1SHerbert Xu * traversing fragment lists and such. 2552357b40a1SHerbert Xu */ 2553357b40a1SHerbert Xu 25540c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2555357b40a1SHerbert Xu { 25561a028e50SDavid S. Miller int start = skb_headlen(skb); 2557fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2558fbb398a8SDavid S. Miller int i, copy; 2559357b40a1SHerbert Xu 2560357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2561357b40a1SHerbert Xu goto fault; 2562357b40a1SHerbert Xu 25631a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2564357b40a1SHerbert Xu if (copy > len) 2565357b40a1SHerbert Xu copy = len; 256627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2567357b40a1SHerbert Xu if ((len -= copy) == 0) 2568357b40a1SHerbert Xu return 0; 2569357b40a1SHerbert Xu offset += copy; 2570357b40a1SHerbert Xu from += copy; 2571357b40a1SHerbert Xu } 2572357b40a1SHerbert Xu 2573357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2574357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 25751a028e50SDavid S. Miller int end; 2576357b40a1SHerbert Xu 2577547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25781a028e50SDavid S. Miller 25799e903e08SEric Dumazet end = start + skb_frag_size(frag); 2580357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2581c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2582c613c209SWillem de Bruijn struct page *p; 2583357b40a1SHerbert Xu u8 *vaddr; 2584357b40a1SHerbert Xu 2585357b40a1SHerbert Xu if (copy > len) 2586357b40a1SHerbert Xu copy = len; 2587357b40a1SHerbert Xu 2588c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2589b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2590c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2591c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2592c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 259351c56b00SEric Dumazet kunmap_atomic(vaddr); 2594c613c209SWillem de Bruijn } 2595357b40a1SHerbert Xu 2596357b40a1SHerbert Xu if ((len -= copy) == 0) 2597357b40a1SHerbert Xu return 0; 2598357b40a1SHerbert Xu offset += copy; 2599357b40a1SHerbert Xu from += copy; 2600357b40a1SHerbert Xu } 26011a028e50SDavid S. Miller start = end; 2602357b40a1SHerbert Xu } 2603357b40a1SHerbert Xu 2604fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26051a028e50SDavid S. Miller int end; 2606357b40a1SHerbert Xu 2607547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26081a028e50SDavid S. Miller 2609fbb398a8SDavid S. Miller end = start + frag_iter->len; 2610357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2611357b40a1SHerbert Xu if (copy > len) 2612357b40a1SHerbert Xu copy = len; 2613fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 26141a028e50SDavid S. Miller from, copy)) 2615357b40a1SHerbert Xu goto fault; 2616357b40a1SHerbert Xu if ((len -= copy) == 0) 2617357b40a1SHerbert Xu return 0; 2618357b40a1SHerbert Xu offset += copy; 2619357b40a1SHerbert Xu from += copy; 2620357b40a1SHerbert Xu } 26211a028e50SDavid S. Miller start = end; 2622357b40a1SHerbert Xu } 2623357b40a1SHerbert Xu if (!len) 2624357b40a1SHerbert Xu return 0; 2625357b40a1SHerbert Xu 2626357b40a1SHerbert Xu fault: 2627357b40a1SHerbert Xu return -EFAULT; 2628357b40a1SHerbert Xu } 2629357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2630357b40a1SHerbert Xu 26311da177e4SLinus Torvalds /* Checksum skb data. */ 26322817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 26332817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 26341da177e4SLinus Torvalds { 26351a028e50SDavid S. Miller int start = skb_headlen(skb); 26361a028e50SDavid S. Miller int i, copy = start - offset; 2637fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 26381da177e4SLinus Torvalds int pos = 0; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds /* Checksum header. */ 26411da177e4SLinus Torvalds if (copy > 0) { 26421da177e4SLinus Torvalds if (copy > len) 26431da177e4SLinus Torvalds copy = len; 26442544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 26452544af03SMatteo Croce skb->data + offset, copy, csum); 26461da177e4SLinus Torvalds if ((len -= copy) == 0) 26471da177e4SLinus Torvalds return csum; 26481da177e4SLinus Torvalds offset += copy; 26491da177e4SLinus Torvalds pos = copy; 26501da177e4SLinus Torvalds } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26531a028e50SDavid S. Miller int end; 265451c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 26551da177e4SLinus Torvalds 2656547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26571a028e50SDavid S. Miller 265851c56b00SEric Dumazet end = start + skb_frag_size(frag); 26591da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2660c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2661c613c209SWillem de Bruijn struct page *p; 266244bb9363SAl Viro __wsum csum2; 26631da177e4SLinus Torvalds u8 *vaddr; 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds if (copy > len) 26661da177e4SLinus Torvalds copy = len; 2667c613c209SWillem de Bruijn 2668c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2669b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2670c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2671c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 26722544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 26732544af03SMatteo Croce csum_partial_ext, 26742544af03SMatteo Croce vaddr + p_off, p_len, 0); 267551c56b00SEric Dumazet kunmap_atomic(vaddr); 26762544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 26772544af03SMatteo Croce csum_block_add_ext, csum, 26782544af03SMatteo Croce csum2, pos, p_len); 2679c613c209SWillem de Bruijn pos += p_len; 2680c613c209SWillem de Bruijn } 2681c613c209SWillem de Bruijn 26821da177e4SLinus Torvalds if (!(len -= copy)) 26831da177e4SLinus Torvalds return csum; 26841da177e4SLinus Torvalds offset += copy; 26851da177e4SLinus Torvalds } 26861a028e50SDavid S. Miller start = end; 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 2689fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26901a028e50SDavid S. Miller int end; 26911da177e4SLinus Torvalds 2692547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26931a028e50SDavid S. Miller 2694fbb398a8SDavid S. Miller end = start + frag_iter->len; 26951da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26965f92a738SAl Viro __wsum csum2; 26971da177e4SLinus Torvalds if (copy > len) 26981da177e4SLinus Torvalds copy = len; 26992817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 27002817a336SDaniel Borkmann copy, 0, ops); 27012544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 27022544af03SMatteo Croce csum, csum2, pos, copy); 27031da177e4SLinus Torvalds if ((len -= copy) == 0) 27041da177e4SLinus Torvalds return csum; 27051da177e4SLinus Torvalds offset += copy; 27061da177e4SLinus Torvalds pos += copy; 27071da177e4SLinus Torvalds } 27081a028e50SDavid S. Miller start = end; 27091da177e4SLinus Torvalds } 271009a62660SKris Katterjohn BUG_ON(len); 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds return csum; 27131da177e4SLinus Torvalds } 27142817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 27152817a336SDaniel Borkmann 27162817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 27172817a336SDaniel Borkmann int len, __wsum csum) 27182817a336SDaniel Borkmann { 27192817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2720cea80ea8SDaniel Borkmann .update = csum_partial_ext, 27212817a336SDaniel Borkmann .combine = csum_block_add_ext, 27222817a336SDaniel Borkmann }; 27232817a336SDaniel Borkmann 27242817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 27252817a336SDaniel Borkmann } 2726b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds /* Both of above in one bottle. */ 27291da177e4SLinus Torvalds 273081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 27318d5930dfSAl Viro u8 *to, int len) 27321da177e4SLinus Torvalds { 27331a028e50SDavid S. Miller int start = skb_headlen(skb); 27341a028e50SDavid S. Miller int i, copy = start - offset; 2735fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 27361da177e4SLinus Torvalds int pos = 0; 27378d5930dfSAl Viro __wsum csum = 0; 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds /* Copy header. */ 27401da177e4SLinus Torvalds if (copy > 0) { 27411da177e4SLinus Torvalds if (copy > len) 27421da177e4SLinus Torvalds copy = len; 27431da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 2744cc44c17bSAl Viro copy); 27451da177e4SLinus Torvalds if ((len -= copy) == 0) 27461da177e4SLinus Torvalds return csum; 27471da177e4SLinus Torvalds offset += copy; 27481da177e4SLinus Torvalds to += copy; 27491da177e4SLinus Torvalds pos = copy; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27531a028e50SDavid S. Miller int end; 27541da177e4SLinus Torvalds 2755547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27561a028e50SDavid S. Miller 27579e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 27581da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2759c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2760c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2761c613c209SWillem de Bruijn struct page *p; 27625084205fSAl Viro __wsum csum2; 27631da177e4SLinus Torvalds u8 *vaddr; 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds if (copy > len) 27661da177e4SLinus Torvalds copy = len; 2767c613c209SWillem de Bruijn 2768c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2769b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2770c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2771c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2772c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2773c613c209SWillem de Bruijn to + copied, 2774cc44c17bSAl Viro p_len); 277551c56b00SEric Dumazet kunmap_atomic(vaddr); 27761da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2777c613c209SWillem de Bruijn pos += p_len; 2778c613c209SWillem de Bruijn } 2779c613c209SWillem de Bruijn 27801da177e4SLinus Torvalds if (!(len -= copy)) 27811da177e4SLinus Torvalds return csum; 27821da177e4SLinus Torvalds offset += copy; 27831da177e4SLinus Torvalds to += copy; 27841da177e4SLinus Torvalds } 27851a028e50SDavid S. Miller start = end; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 2788fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 278981d77662SAl Viro __wsum csum2; 27901a028e50SDavid S. Miller int end; 27911da177e4SLinus Torvalds 2792547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27931a028e50SDavid S. Miller 2794fbb398a8SDavid S. Miller end = start + frag_iter->len; 27951da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 27961da177e4SLinus Torvalds if (copy > len) 27971da177e4SLinus Torvalds copy = len; 2798fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 27991a028e50SDavid S. Miller offset - start, 28008d5930dfSAl Viro to, copy); 28011da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 28021da177e4SLinus Torvalds if ((len -= copy) == 0) 28031da177e4SLinus Torvalds return csum; 28041da177e4SLinus Torvalds offset += copy; 28051da177e4SLinus Torvalds to += copy; 28061da177e4SLinus Torvalds pos += copy; 28071da177e4SLinus Torvalds } 28081a028e50SDavid S. Miller start = end; 28091da177e4SLinus Torvalds } 281009a62660SKris Katterjohn BUG_ON(len); 28111da177e4SLinus Torvalds return csum; 28121da177e4SLinus Torvalds } 2813b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 28141da177e4SLinus Torvalds 281549f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 281649f8e832SCong Wang { 281749f8e832SCong Wang __sum16 sum; 281849f8e832SCong Wang 281949f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 282014641931SCong Wang /* See comments in __skb_checksum_complete(). */ 282149f8e832SCong Wang if (likely(!sum)) { 282249f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 282349f8e832SCong Wang !skb->csum_complete_sw) 28247fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 282549f8e832SCong Wang } 282649f8e832SCong Wang if (!skb_shared(skb)) 282749f8e832SCong Wang skb->csum_valid = !sum; 282849f8e832SCong Wang return sum; 282949f8e832SCong Wang } 283049f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 283149f8e832SCong Wang 283214641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 283314641931SCong Wang * which has been changed from the hardware checksum, for example, by 283414641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 283514641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 283614641931SCong Wang * 283714641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 283814641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 283914641931SCong Wang * shared. 284014641931SCong Wang */ 284149f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 284249f8e832SCong Wang { 284349f8e832SCong Wang __wsum csum; 284449f8e832SCong Wang __sum16 sum; 284549f8e832SCong Wang 284649f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 284749f8e832SCong Wang 284849f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 284914641931SCong Wang /* This check is inverted, because we already knew the hardware 285014641931SCong Wang * checksum is invalid before calling this function. So, if the 285114641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 285214641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 285314641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 285414641931SCong Wang * when moving skb->data around. 285514641931SCong Wang */ 285649f8e832SCong Wang if (likely(!sum)) { 285749f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 285849f8e832SCong Wang !skb->csum_complete_sw) 28597fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 286049f8e832SCong Wang } 286149f8e832SCong Wang 286249f8e832SCong Wang if (!skb_shared(skb)) { 286349f8e832SCong Wang /* Save full packet checksum */ 286449f8e832SCong Wang skb->csum = csum; 286549f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 286649f8e832SCong Wang skb->csum_complete_sw = 1; 286749f8e832SCong Wang skb->csum_valid = !sum; 286849f8e832SCong Wang } 286949f8e832SCong Wang 287049f8e832SCong Wang return sum; 287149f8e832SCong Wang } 287249f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 287349f8e832SCong Wang 28749617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 28759617813dSDavide Caratti { 28769617813dSDavide Caratti net_warn_ratelimited( 28779617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 28789617813dSDavide Caratti __func__); 28799617813dSDavide Caratti return 0; 28809617813dSDavide Caratti } 28819617813dSDavide Caratti 28829617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 28839617813dSDavide Caratti int offset, int len) 28849617813dSDavide Caratti { 28859617813dSDavide Caratti net_warn_ratelimited( 28869617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 28879617813dSDavide Caratti __func__); 28889617813dSDavide Caratti return 0; 28899617813dSDavide Caratti } 28909617813dSDavide Caratti 28919617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 28929617813dSDavide Caratti .update = warn_crc32c_csum_update, 28939617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 28949617813dSDavide Caratti }; 28959617813dSDavide Caratti 28969617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 28979617813dSDavide Caratti &default_crc32c_ops; 28989617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 28999617813dSDavide Caratti 2900af2806f8SThomas Graf /** 2901af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2902af2806f8SThomas Graf * @from: source buffer 2903af2806f8SThomas Graf * 2904af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2905af2806f8SThomas Graf * into skb_zerocopy(). 2906af2806f8SThomas Graf */ 2907af2806f8SThomas Graf unsigned int 2908af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2909af2806f8SThomas Graf { 2910af2806f8SThomas Graf unsigned int hlen = 0; 2911af2806f8SThomas Graf 2912af2806f8SThomas Graf if (!from->head_frag || 2913af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2914af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2915af2806f8SThomas Graf hlen = skb_headlen(from); 2916af2806f8SThomas Graf 2917af2806f8SThomas Graf if (skb_has_frag_list(from)) 2918af2806f8SThomas Graf hlen = from->len; 2919af2806f8SThomas Graf 2920af2806f8SThomas Graf return hlen; 2921af2806f8SThomas Graf } 2922af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2923af2806f8SThomas Graf 2924af2806f8SThomas Graf /** 2925af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2926af2806f8SThomas Graf * @to: destination buffer 29277fceb4deSMasanari Iida * @from: source buffer 2928af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2929af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2930af2806f8SThomas Graf * 2931af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2932af2806f8SThomas Graf * to the frags in the source buffer. 2933af2806f8SThomas Graf * 2934af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2935af2806f8SThomas Graf * headroom in the `to` buffer. 293636d5fe6aSZoltan Kiss * 293736d5fe6aSZoltan Kiss * Return value: 293836d5fe6aSZoltan Kiss * 0: everything is OK 293936d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 294036d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2941af2806f8SThomas Graf */ 294236d5fe6aSZoltan Kiss int 294336d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2944af2806f8SThomas Graf { 2945af2806f8SThomas Graf int i, j = 0; 2946af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 294736d5fe6aSZoltan Kiss int ret; 2948af2806f8SThomas Graf struct page *page; 2949af2806f8SThomas Graf unsigned int offset; 2950af2806f8SThomas Graf 2951af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2952af2806f8SThomas Graf 2953af2806f8SThomas Graf /* dont bother with small payloads */ 295436d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 295536d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2956af2806f8SThomas Graf 2957af2806f8SThomas Graf if (hlen) { 295836d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 295936d5fe6aSZoltan Kiss if (unlikely(ret)) 296036d5fe6aSZoltan Kiss return ret; 2961af2806f8SThomas Graf len -= hlen; 2962af2806f8SThomas Graf } else { 2963af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2964af2806f8SThomas Graf if (plen) { 2965af2806f8SThomas Graf page = virt_to_head_page(from->head); 2966af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2967af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2968af2806f8SThomas Graf get_page(page); 2969af2806f8SThomas Graf j = 1; 2970af2806f8SThomas Graf len -= plen; 2971af2806f8SThomas Graf } 2972af2806f8SThomas Graf } 2973af2806f8SThomas Graf 2974af2806f8SThomas Graf to->truesize += len + plen; 2975af2806f8SThomas Graf to->len += len + plen; 2976af2806f8SThomas Graf to->data_len += len + plen; 2977af2806f8SThomas Graf 297836d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 297936d5fe6aSZoltan Kiss skb_tx_error(from); 298036d5fe6aSZoltan Kiss return -ENOMEM; 298136d5fe6aSZoltan Kiss } 29821f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 298336d5fe6aSZoltan Kiss 2984af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2985d8e18a51SMatthew Wilcox (Oracle) int size; 2986d8e18a51SMatthew Wilcox (Oracle) 2987af2806f8SThomas Graf if (!len) 2988af2806f8SThomas Graf break; 2989af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2990d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 2991d8e18a51SMatthew Wilcox (Oracle) len); 2992d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 2993d8e18a51SMatthew Wilcox (Oracle) len -= size; 2994af2806f8SThomas Graf skb_frag_ref(to, j); 2995af2806f8SThomas Graf j++; 2996af2806f8SThomas Graf } 2997af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 299836d5fe6aSZoltan Kiss 299936d5fe6aSZoltan Kiss return 0; 3000af2806f8SThomas Graf } 3001af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3002af2806f8SThomas Graf 30031da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 30041da177e4SLinus Torvalds { 3005d3bc23e7SAl Viro __wsum csum; 30061da177e4SLinus Torvalds long csstart; 30071da177e4SLinus Torvalds 300884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 300955508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 30101da177e4SLinus Torvalds else 30111da177e4SLinus Torvalds csstart = skb_headlen(skb); 30121da177e4SLinus Torvalds 301309a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 30141da177e4SLinus Torvalds 3015d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds csum = 0; 30181da177e4SLinus Torvalds if (csstart != skb->len) 30191da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 30208d5930dfSAl Viro skb->len - csstart); 30211da177e4SLinus Torvalds 302284fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3023ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 30241da177e4SLinus Torvalds 3025d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds } 3028b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds /** 30311da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 30321da177e4SLinus Torvalds * @list: list to dequeue from 30331da177e4SLinus Torvalds * 30341da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 30351da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 30361da177e4SLinus Torvalds * returned or %NULL if the list is empty. 30371da177e4SLinus Torvalds */ 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 30401da177e4SLinus Torvalds { 30411da177e4SLinus Torvalds unsigned long flags; 30421da177e4SLinus Torvalds struct sk_buff *result; 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 30451da177e4SLinus Torvalds result = __skb_dequeue(list); 30461da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 30471da177e4SLinus Torvalds return result; 30481da177e4SLinus Torvalds } 3049b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds /** 30521da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 30531da177e4SLinus Torvalds * @list: list to dequeue from 30541da177e4SLinus Torvalds * 30551da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 30561da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 30571da177e4SLinus Torvalds * returned or %NULL if the list is empty. 30581da177e4SLinus Torvalds */ 30591da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 30601da177e4SLinus Torvalds { 30611da177e4SLinus Torvalds unsigned long flags; 30621da177e4SLinus Torvalds struct sk_buff *result; 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 30651da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 30661da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 30671da177e4SLinus Torvalds return result; 30681da177e4SLinus Torvalds } 3069b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 30701da177e4SLinus Torvalds 30711da177e4SLinus Torvalds /** 30721da177e4SLinus Torvalds * skb_queue_purge - empty a list 30731da177e4SLinus Torvalds * @list: list to empty 30741da177e4SLinus Torvalds * 30751da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 30761da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 30771da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 30781da177e4SLinus Torvalds */ 30791da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 30801da177e4SLinus Torvalds { 30811da177e4SLinus Torvalds struct sk_buff *skb; 30821da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 30831da177e4SLinus Torvalds kfree_skb(skb); 30841da177e4SLinus Torvalds } 3085b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 30861da177e4SLinus Torvalds 30871da177e4SLinus Torvalds /** 30889f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 30899f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3090385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 30919f5afeaeSYaogong Wang * 30929f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 30939f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 30949f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 30959f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 30969f5afeaeSYaogong Wang */ 3097385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 30989f5afeaeSYaogong Wang { 30997c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3100385114deSPeter Oskolkov unsigned int sum = 0; 31019f5afeaeSYaogong Wang 31027c90584cSEric Dumazet while (p) { 31037c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 31047c90584cSEric Dumazet 31057c90584cSEric Dumazet p = rb_next(p); 31067c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3107385114deSPeter Oskolkov sum += skb->truesize; 31089f5afeaeSYaogong Wang kfree_skb(skb); 31097c90584cSEric Dumazet } 3110385114deSPeter Oskolkov return sum; 31119f5afeaeSYaogong Wang } 31129f5afeaeSYaogong Wang 31139f5afeaeSYaogong Wang /** 31141da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 31151da177e4SLinus Torvalds * @list: list to use 31161da177e4SLinus Torvalds * @newsk: buffer to queue 31171da177e4SLinus Torvalds * 31181da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 31191da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 31201da177e4SLinus Torvalds * safely. 31211da177e4SLinus Torvalds * 31221da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31231da177e4SLinus Torvalds */ 31241da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 31251da177e4SLinus Torvalds { 31261da177e4SLinus Torvalds unsigned long flags; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31291da177e4SLinus Torvalds __skb_queue_head(list, newsk); 31301da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31311da177e4SLinus Torvalds } 3132b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds /** 31351da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 31361da177e4SLinus Torvalds * @list: list to use 31371da177e4SLinus Torvalds * @newsk: buffer to queue 31381da177e4SLinus Torvalds * 31391da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 31401da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 31411da177e4SLinus Torvalds * safely. 31421da177e4SLinus Torvalds * 31431da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31441da177e4SLinus Torvalds */ 31451da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 31461da177e4SLinus Torvalds { 31471da177e4SLinus Torvalds unsigned long flags; 31481da177e4SLinus Torvalds 31491da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31501da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 31511da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31521da177e4SLinus Torvalds } 3153b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 31548728b834SDavid S. Miller 31551da177e4SLinus Torvalds /** 31561da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 31571da177e4SLinus Torvalds * @skb: buffer to remove 31588728b834SDavid S. Miller * @list: list to use 31591da177e4SLinus Torvalds * 31608728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 31618728b834SDavid S. Miller * function is atomic with respect to other list locked calls 31621da177e4SLinus Torvalds * 31638728b834SDavid S. Miller * You must know what list the SKB is on. 31641da177e4SLinus Torvalds */ 31658728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 31661da177e4SLinus Torvalds { 31671da177e4SLinus Torvalds unsigned long flags; 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31708728b834SDavid S. Miller __skb_unlink(skb, list); 31711da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31721da177e4SLinus Torvalds } 3173b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds /** 31761da177e4SLinus Torvalds * skb_append - append a buffer 31771da177e4SLinus Torvalds * @old: buffer to insert after 31781da177e4SLinus Torvalds * @newsk: buffer to insert 31798728b834SDavid S. Miller * @list: list to use 31801da177e4SLinus Torvalds * 31811da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 31821da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 31831da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 31841da177e4SLinus Torvalds */ 31858728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 31861da177e4SLinus Torvalds { 31871da177e4SLinus Torvalds unsigned long flags; 31881da177e4SLinus Torvalds 31898728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 31907de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 31918728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 31921da177e4SLinus Torvalds } 3193b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 31941da177e4SLinus Torvalds 31951da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 31961da177e4SLinus Torvalds struct sk_buff* skb1, 31971da177e4SLinus Torvalds const u32 len, const int pos) 31981da177e4SLinus Torvalds { 31991da177e4SLinus Torvalds int i; 32001da177e4SLinus Torvalds 3201d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3202d626f62bSArnaldo Carvalho de Melo pos - len); 32031da177e4SLinus Torvalds /* And move data appendix as is. */ 32041da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 32051da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 32081da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 32091da177e4SLinus Torvalds skb1->data_len = skb->data_len; 32101da177e4SLinus Torvalds skb1->len += skb1->data_len; 32111da177e4SLinus Torvalds skb->data_len = 0; 32121da177e4SLinus Torvalds skb->len = len; 321327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 32141da177e4SLinus Torvalds } 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 32171da177e4SLinus Torvalds struct sk_buff* skb1, 32181da177e4SLinus Torvalds const u32 len, int pos) 32191da177e4SLinus Torvalds { 32201da177e4SLinus Torvalds int i, k = 0; 32211da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 32241da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 32251da177e4SLinus Torvalds skb->len = len; 32261da177e4SLinus Torvalds skb->data_len = len - pos; 32271da177e4SLinus Torvalds 32281da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 32299e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds if (pos + size > len) { 32321da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 32331da177e4SLinus Torvalds 32341da177e4SLinus Torvalds if (pos < len) { 32351da177e4SLinus Torvalds /* Split frag. 32361da177e4SLinus Torvalds * We have two variants in this case: 32371da177e4SLinus Torvalds * 1. Move all the frag to the second 32381da177e4SLinus Torvalds * part, if it is possible. F.e. 32391da177e4SLinus Torvalds * this approach is mandatory for TUX, 32401da177e4SLinus Torvalds * where splitting is expensive. 32411da177e4SLinus Torvalds * 2. Split is accurately. We make this. 32421da177e4SLinus Torvalds */ 3243ea2ab693SIan Campbell skb_frag_ref(skb, i); 3244b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 32459e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 32469e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 32471da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 32481da177e4SLinus Torvalds } 32491da177e4SLinus Torvalds k++; 32501da177e4SLinus Torvalds } else 32511da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 32521da177e4SLinus Torvalds pos += size; 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds /** 32581da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 32591da177e4SLinus Torvalds * @skb: the buffer to split 32601da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 32611da177e4SLinus Torvalds * @len: new length for skb 32621da177e4SLinus Torvalds */ 32631da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 32641da177e4SLinus Torvalds { 32651da177e4SLinus Torvalds int pos = skb_headlen(skb); 32661da177e4SLinus Torvalds 3267fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3268fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 32691f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 32701da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 32711da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 32721da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 32731da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 32741da177e4SLinus Torvalds } 3275b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 32761da177e4SLinus Torvalds 32779f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 32789f782db3SIlpo Järvinen * 32799f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 32809f782db3SIlpo Järvinen */ 3281832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3282832d11c5SIlpo Järvinen { 32830ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3284832d11c5SIlpo Järvinen } 3285832d11c5SIlpo Järvinen 3286832d11c5SIlpo Järvinen /** 3287832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3288832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3289832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3290832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3291832d11c5SIlpo Järvinen * 3292832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 329320e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3294832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3295832d11c5SIlpo Järvinen * 3296832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3297832d11c5SIlpo Järvinen * 3298832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3299832d11c5SIlpo Järvinen * to have non-paged data as well. 3300832d11c5SIlpo Järvinen * 3301832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3302832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3303832d11c5SIlpo Järvinen */ 3304832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3305832d11c5SIlpo Järvinen { 3306832d11c5SIlpo Järvinen int from, to, merge, todo; 3307d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3308832d11c5SIlpo Järvinen 3309832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3310f8071cdeSEric Dumazet 3311f8071cdeSEric Dumazet if (skb_headlen(skb)) 3312f8071cdeSEric Dumazet return 0; 33131f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 33141f8b977aSWillem de Bruijn return 0; 3315832d11c5SIlpo Järvinen 3316832d11c5SIlpo Järvinen todo = shiftlen; 3317832d11c5SIlpo Järvinen from = 0; 3318832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3319832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3320832d11c5SIlpo Järvinen 3321832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3322832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3323832d11c5SIlpo Järvinen */ 3324832d11c5SIlpo Järvinen if (!to || 3325ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3326b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3327832d11c5SIlpo Järvinen merge = -1; 3328832d11c5SIlpo Järvinen } else { 3329832d11c5SIlpo Järvinen merge = to - 1; 3330832d11c5SIlpo Järvinen 33319e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3332832d11c5SIlpo Järvinen if (todo < 0) { 3333832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3334832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3335832d11c5SIlpo Järvinen return 0; 3336832d11c5SIlpo Järvinen 33379f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 33389f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3339832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3340832d11c5SIlpo Järvinen 33419e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 33429e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3343b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3344832d11c5SIlpo Järvinen 3345832d11c5SIlpo Järvinen goto onlymerged; 3346832d11c5SIlpo Järvinen } 3347832d11c5SIlpo Järvinen 3348832d11c5SIlpo Järvinen from++; 3349832d11c5SIlpo Järvinen } 3350832d11c5SIlpo Järvinen 3351832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3352832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3353832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3354832d11c5SIlpo Järvinen return 0; 3355832d11c5SIlpo Järvinen 3356832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3357832d11c5SIlpo Järvinen return 0; 3358832d11c5SIlpo Järvinen 3359832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3360832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3361832d11c5SIlpo Järvinen return 0; 3362832d11c5SIlpo Järvinen 3363832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3364832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3365832d11c5SIlpo Järvinen 33669e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3367832d11c5SIlpo Järvinen *fragto = *fragfrom; 33689e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3369832d11c5SIlpo Järvinen from++; 3370832d11c5SIlpo Järvinen to++; 3371832d11c5SIlpo Järvinen 3372832d11c5SIlpo Järvinen } else { 3373ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3374b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3375b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 33769e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3377832d11c5SIlpo Järvinen 3378b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 33799e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3380832d11c5SIlpo Järvinen todo = 0; 3381832d11c5SIlpo Järvinen 3382832d11c5SIlpo Järvinen to++; 3383832d11c5SIlpo Järvinen break; 3384832d11c5SIlpo Järvinen } 3385832d11c5SIlpo Järvinen } 3386832d11c5SIlpo Järvinen 3387832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3388832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3389832d11c5SIlpo Järvinen 3390832d11c5SIlpo Järvinen if (merge >= 0) { 3391832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3392832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3393832d11c5SIlpo Järvinen 33949e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3395ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 3396832d11c5SIlpo Järvinen } 3397832d11c5SIlpo Järvinen 3398832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3399832d11c5SIlpo Järvinen to = 0; 3400832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3401832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3402832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3403832d11c5SIlpo Järvinen 3404832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3405832d11c5SIlpo Järvinen 3406832d11c5SIlpo Järvinen onlymerged: 3407832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3408832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3409832d11c5SIlpo Järvinen */ 3410832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3411832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3412832d11c5SIlpo Järvinen 3413832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3414832d11c5SIlpo Järvinen skb->len -= shiftlen; 3415832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3416832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3417832d11c5SIlpo Järvinen tgt->len += shiftlen; 3418832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3419832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3420832d11c5SIlpo Järvinen 3421832d11c5SIlpo Järvinen return shiftlen; 3422832d11c5SIlpo Järvinen } 3423832d11c5SIlpo Järvinen 3424677e90edSThomas Graf /** 3425677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3426677e90edSThomas Graf * @skb: the buffer to read 3427677e90edSThomas Graf * @from: lower offset of data to be read 3428677e90edSThomas Graf * @to: upper offset of data to be read 3429677e90edSThomas Graf * @st: state variable 3430677e90edSThomas Graf * 3431677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3432677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3433677e90edSThomas Graf */ 3434677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3435677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3436677e90edSThomas Graf { 3437677e90edSThomas Graf st->lower_offset = from; 3438677e90edSThomas Graf st->upper_offset = to; 3439677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3440677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3441677e90edSThomas Graf st->frag_data = NULL; 3442677e90edSThomas Graf } 3443b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3444677e90edSThomas Graf 3445677e90edSThomas Graf /** 3446677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3447677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3448677e90edSThomas Graf * @data: destination pointer for data to be returned 3449677e90edSThomas Graf * @st: state variable 3450677e90edSThomas Graf * 3451bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3452677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3453bc32383cSMathias Krause * the head of the data block to @data and returns the length 3454677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3455677e90edSThomas Graf * offset has been reached. 3456677e90edSThomas Graf * 3457677e90edSThomas Graf * The caller is not required to consume all of the data 3458bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3459677e90edSThomas Graf * of bytes already consumed and the next call to 3460677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3461677e90edSThomas Graf * 346225985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3463e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3464677e90edSThomas Graf * reads of potentially non linear data. 3465677e90edSThomas Graf * 3466bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3467677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3468677e90edSThomas Graf * a stack for this purpose. 3469677e90edSThomas Graf */ 3470677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3471677e90edSThomas Graf struct skb_seq_state *st) 3472677e90edSThomas Graf { 3473677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3474677e90edSThomas Graf skb_frag_t *frag; 3475677e90edSThomas Graf 3476aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3477aeb193eaSWedson Almeida Filho if (st->frag_data) { 3478aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3479aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3480aeb193eaSWedson Almeida Filho } 3481677e90edSThomas Graf return 0; 3482aeb193eaSWedson Almeida Filho } 3483677e90edSThomas Graf 3484677e90edSThomas Graf next_skb: 348595e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3486677e90edSThomas Graf 3487995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 348895e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3489677e90edSThomas Graf return block_limit - abs_offset; 3490677e90edSThomas Graf } 3491677e90edSThomas Graf 3492677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3493677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3494677e90edSThomas Graf 3495677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3496677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 34979e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 3498677e90edSThomas Graf 3499677e90edSThomas Graf if (abs_offset < block_limit) { 3500677e90edSThomas Graf if (!st->frag_data) 350151c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 3502677e90edSThomas Graf 3503b54c9d5bSJonathan Lemon *data = (u8 *) st->frag_data + skb_frag_off(frag) + 3504677e90edSThomas Graf (abs_offset - st->stepped_offset); 3505677e90edSThomas Graf 3506677e90edSThomas Graf return block_limit - abs_offset; 3507677e90edSThomas Graf } 3508677e90edSThomas Graf 3509677e90edSThomas Graf if (st->frag_data) { 351051c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3511677e90edSThomas Graf st->frag_data = NULL; 3512677e90edSThomas Graf } 3513677e90edSThomas Graf 3514677e90edSThomas Graf st->frag_idx++; 35159e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 3516677e90edSThomas Graf } 3517677e90edSThomas Graf 35185b5a60daSOlaf Kirch if (st->frag_data) { 351951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 35205b5a60daSOlaf Kirch st->frag_data = NULL; 35215b5a60daSOlaf Kirch } 35225b5a60daSOlaf Kirch 352321dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3524677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 352595e3b24cSHerbert Xu st->frag_idx = 0; 3526677e90edSThomas Graf goto next_skb; 352771b3346dSShyam Iyer } else if (st->cur_skb->next) { 352871b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 352971b3346dSShyam Iyer st->frag_idx = 0; 3530677e90edSThomas Graf goto next_skb; 3531677e90edSThomas Graf } 3532677e90edSThomas Graf 3533677e90edSThomas Graf return 0; 3534677e90edSThomas Graf } 3535b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3536677e90edSThomas Graf 3537677e90edSThomas Graf /** 3538677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3539677e90edSThomas Graf * @st: state variable 3540677e90edSThomas Graf * 3541677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3542677e90edSThomas Graf * returned 0. 3543677e90edSThomas Graf */ 3544677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3545677e90edSThomas Graf { 3546677e90edSThomas Graf if (st->frag_data) 354751c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3548677e90edSThomas Graf } 3549b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3550677e90edSThomas Graf 35513fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 35523fc7e8a6SThomas Graf 35533fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 35543fc7e8a6SThomas Graf struct ts_config *conf, 35553fc7e8a6SThomas Graf struct ts_state *state) 35563fc7e8a6SThomas Graf { 35573fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 35583fc7e8a6SThomas Graf } 35593fc7e8a6SThomas Graf 35603fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 35613fc7e8a6SThomas Graf { 35623fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 35633fc7e8a6SThomas Graf } 35643fc7e8a6SThomas Graf 35653fc7e8a6SThomas Graf /** 35663fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 35673fc7e8a6SThomas Graf * @skb: the buffer to look in 35683fc7e8a6SThomas Graf * @from: search offset 35693fc7e8a6SThomas Graf * @to: search limit 35703fc7e8a6SThomas Graf * @config: textsearch configuration 35713fc7e8a6SThomas Graf * 35723fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 35733fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 35743fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 35753fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 35763fc7e8a6SThomas Graf */ 35773fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3578059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 35793fc7e8a6SThomas Graf { 3580059a2440SBojan Prtvar struct ts_state state; 3581f72b948dSPhil Oester unsigned int ret; 3582f72b948dSPhil Oester 35833fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 35843fc7e8a6SThomas Graf config->finish = skb_ts_finish; 35853fc7e8a6SThomas Graf 3586059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 35873fc7e8a6SThomas Graf 3588059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3589f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 35903fc7e8a6SThomas Graf } 3591b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 35923fc7e8a6SThomas Graf 3593be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3594be12a1feSHannes Frederic Sowa int offset, size_t size) 3595be12a1feSHannes Frederic Sowa { 3596be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3597be12a1feSHannes Frederic Sowa 3598be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3599be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3600be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3601be12a1feSHannes Frederic Sowa get_page(page); 3602be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3603be12a1feSHannes Frederic Sowa } else { 3604be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3605be12a1feSHannes Frederic Sowa } 3606be12a1feSHannes Frederic Sowa 3607be12a1feSHannes Frederic Sowa return 0; 3608be12a1feSHannes Frederic Sowa } 3609be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3610be12a1feSHannes Frederic Sowa 3611cbb042f9SHerbert Xu /** 3612cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3613cbb042f9SHerbert Xu * @skb: buffer to update 3614cbb042f9SHerbert Xu * @len: length of data pulled 3615cbb042f9SHerbert Xu * 3616cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3617fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 361884fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 361984fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 362084fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3621cbb042f9SHerbert Xu */ 3622af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3623cbb042f9SHerbert Xu { 362431b33dfbSPravin B Shelar unsigned char *data = skb->data; 362531b33dfbSPravin B Shelar 3626cbb042f9SHerbert Xu BUG_ON(len > skb->len); 362731b33dfbSPravin B Shelar __skb_pull(skb, len); 362831b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 362931b33dfbSPravin B Shelar return skb->data; 3630cbb042f9SHerbert Xu } 3631f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3632f94691acSArnaldo Carvalho de Melo 363313acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 363413acc94eSYonghong Song { 363513acc94eSYonghong Song skb_frag_t head_frag; 363613acc94eSYonghong Song struct page *page; 363713acc94eSYonghong Song 363813acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 3639d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 3640b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 3641b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 3642d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 364313acc94eSYonghong Song return head_frag; 364413acc94eSYonghong Song } 364513acc94eSYonghong Song 36463a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 36473a1296a3SSteffen Klassert netdev_features_t features, 36483a1296a3SSteffen Klassert unsigned int offset) 36493a1296a3SSteffen Klassert { 36503a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 36513a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 36523a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 36533a1296a3SSteffen Klassert unsigned int delta_len = 0; 36543a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 36553a1296a3SSteffen Klassert struct sk_buff *nskb; 36563a1296a3SSteffen Klassert 36573a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 36583a1296a3SSteffen Klassert 36593a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 36603a1296a3SSteffen Klassert 36613a1296a3SSteffen Klassert do { 36623a1296a3SSteffen Klassert nskb = list_skb; 36633a1296a3SSteffen Klassert list_skb = list_skb->next; 36643a1296a3SSteffen Klassert 36653a1296a3SSteffen Klassert if (!tail) 36663a1296a3SSteffen Klassert skb->next = nskb; 36673a1296a3SSteffen Klassert else 36683a1296a3SSteffen Klassert tail->next = nskb; 36693a1296a3SSteffen Klassert 36703a1296a3SSteffen Klassert tail = nskb; 36713a1296a3SSteffen Klassert 36723a1296a3SSteffen Klassert delta_len += nskb->len; 36733a1296a3SSteffen Klassert delta_truesize += nskb->truesize; 36743a1296a3SSteffen Klassert 36753a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 36763a1296a3SSteffen Klassert 3677cf673ed0SFlorian Westphal skb_release_head_state(nskb); 36783a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 36793a1296a3SSteffen Klassert 36803a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 36813a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 36823a1296a3SSteffen Klassert nskb->data - tnl_hlen, 36833a1296a3SSteffen Klassert offset + tnl_hlen); 36843a1296a3SSteffen Klassert 36853a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 36863a1296a3SSteffen Klassert __skb_linearize(nskb)) 36873a1296a3SSteffen Klassert goto err_linearize; 36883a1296a3SSteffen Klassert 36893a1296a3SSteffen Klassert } while (list_skb); 36903a1296a3SSteffen Klassert 36913a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 36923a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 36933a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 36943a1296a3SSteffen Klassert 36953a1296a3SSteffen Klassert skb_gso_reset(skb); 36963a1296a3SSteffen Klassert 36973a1296a3SSteffen Klassert skb->prev = tail; 36983a1296a3SSteffen Klassert 36993a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 37003a1296a3SSteffen Klassert __skb_linearize(skb)) 37013a1296a3SSteffen Klassert goto err_linearize; 37023a1296a3SSteffen Klassert 37033a1296a3SSteffen Klassert skb_get(skb); 37043a1296a3SSteffen Klassert 37053a1296a3SSteffen Klassert return skb; 37063a1296a3SSteffen Klassert 37073a1296a3SSteffen Klassert err_linearize: 37083a1296a3SSteffen Klassert kfree_skb_list(skb->next); 37093a1296a3SSteffen Klassert skb->next = NULL; 37103a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 37113a1296a3SSteffen Klassert } 37123a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 37133a1296a3SSteffen Klassert 37143a1296a3SSteffen Klassert int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb) 37153a1296a3SSteffen Klassert { 37163a1296a3SSteffen Klassert if (unlikely(p->len + skb->len >= 65536)) 37173a1296a3SSteffen Klassert return -E2BIG; 37183a1296a3SSteffen Klassert 37193a1296a3SSteffen Klassert if (NAPI_GRO_CB(p)->last == p) 37203a1296a3SSteffen Klassert skb_shinfo(p)->frag_list = skb; 37213a1296a3SSteffen Klassert else 37223a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last->next = skb; 37233a1296a3SSteffen Klassert 37243a1296a3SSteffen Klassert skb_pull(skb, skb_gro_offset(skb)); 37253a1296a3SSteffen Klassert 37263a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last = skb; 37273a1296a3SSteffen Klassert NAPI_GRO_CB(p)->count++; 37283a1296a3SSteffen Klassert p->data_len += skb->len; 37293a1296a3SSteffen Klassert p->truesize += skb->truesize; 37303a1296a3SSteffen Klassert p->len += skb->len; 37313a1296a3SSteffen Klassert 37323a1296a3SSteffen Klassert NAPI_GRO_CB(skb)->same_flow = 1; 37333a1296a3SSteffen Klassert 37343a1296a3SSteffen Klassert return 0; 37353a1296a3SSteffen Klassert } 37363a1296a3SSteffen Klassert 3737f4c50d99SHerbert Xu /** 3738f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3739df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3740576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3741f4c50d99SHerbert Xu * 3742f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 37434c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 37444c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3745f4c50d99SHerbert Xu */ 3746df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3747df5771ffSMichael S. Tsirkin netdev_features_t features) 3748f4c50d99SHerbert Xu { 3749f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3750f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 37511a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3752df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3753df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3754df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 37551fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3756f4c50d99SHerbert Xu unsigned int offset = doffset; 3757df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3758802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3759f4c50d99SHerbert Xu unsigned int headroom; 3760802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3761ec5f0615SPravin B Shelar __be16 proto; 376236c98382SAlexander Duyck bool csum, sg; 3763df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3764f4c50d99SHerbert Xu int err = -ENOMEM; 3765f4c50d99SHerbert Xu int i = 0; 3766f4c50d99SHerbert Xu int pos; 3767f4c50d99SHerbert Xu 37683dcbdb13SShmulik Ladkani if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 37693dcbdb13SShmulik Ladkani (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 37703dcbdb13SShmulik Ladkani /* gso_size is untrusted, and we have a frag_list with a linear 37713dcbdb13SShmulik Ladkani * non head_frag head. 37723dcbdb13SShmulik Ladkani * 37733dcbdb13SShmulik Ladkani * (we assume checking the first list_skb member suffices; 37743dcbdb13SShmulik Ladkani * i.e if either of the list_skb members have non head_frag 37753dcbdb13SShmulik Ladkani * head, then the first one has too). 37763dcbdb13SShmulik Ladkani * 37773dcbdb13SShmulik Ladkani * If head_skb's headlen does not fit requested gso_size, it 37783dcbdb13SShmulik Ladkani * means that the frag_list members do NOT terminate on exact 37793dcbdb13SShmulik Ladkani * gso_size boundaries. Hence we cannot perform skb_frag_t page 37803dcbdb13SShmulik Ladkani * sharing. Therefore we must fallback to copying the frag_list 37813dcbdb13SShmulik Ladkani * skbs; we do so by disabling SG. 37823dcbdb13SShmulik Ladkani */ 37833dcbdb13SShmulik Ladkani if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 37843dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 37853dcbdb13SShmulik Ladkani } 37863dcbdb13SShmulik Ladkani 37875882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 37882f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 3789ec5f0615SPravin B Shelar if (unlikely(!proto)) 3790ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3791ec5f0615SPravin B Shelar 379236c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3793f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 37947e2b10c1STom Herbert 379507b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 379607b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 379707b26c94SSteffen Klassert struct sk_buff *iter; 379843170c4eSIlan Tayari unsigned int frag_len; 379907b26c94SSteffen Klassert 380007b26c94SSteffen Klassert if (!list_skb || 380107b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 380207b26c94SSteffen Klassert goto normal; 380307b26c94SSteffen Klassert 380443170c4eSIlan Tayari /* If we get here then all the required 380543170c4eSIlan Tayari * GSO features except frag_list are supported. 380643170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 380743170c4eSIlan Tayari * with no frag_list. 380843170c4eSIlan Tayari * Currently we can do that only when the buffers don't 380943170c4eSIlan Tayari * have a linear part and all the buffers except 381043170c4eSIlan Tayari * the last are of the same length. 381107b26c94SSteffen Klassert */ 381243170c4eSIlan Tayari frag_len = list_skb->len; 381307b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 381443170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 381543170c4eSIlan Tayari goto normal; 3816eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 381707b26c94SSteffen Klassert goto normal; 381807b26c94SSteffen Klassert 381907b26c94SSteffen Klassert len -= iter->len; 382007b26c94SSteffen Klassert } 382143170c4eSIlan Tayari 382243170c4eSIlan Tayari if (len != frag_len) 382343170c4eSIlan Tayari goto normal; 382407b26c94SSteffen Klassert } 382507b26c94SSteffen Klassert 3826802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3827802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3828802ab55aSAlexander Duyck * now. 3829802ab55aSAlexander Duyck */ 3830802ab55aSAlexander Duyck partial_segs = len / mss; 3831d7fb5a80SAlexander Duyck if (partial_segs > 1) 3832802ab55aSAlexander Duyck mss *= partial_segs; 3833d7fb5a80SAlexander Duyck else 3834d7fb5a80SAlexander Duyck partial_segs = 0; 3835802ab55aSAlexander Duyck } 3836802ab55aSAlexander Duyck 383707b26c94SSteffen Klassert normal: 3838df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3839df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3840f4c50d99SHerbert Xu 3841f4c50d99SHerbert Xu do { 3842f4c50d99SHerbert Xu struct sk_buff *nskb; 38438cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3844c8884eddSHerbert Xu int hsize; 3845f4c50d99SHerbert Xu int size; 3846f4c50d99SHerbert Xu 38473953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 38483953c46cSMarcelo Ricardo Leitner len = list_skb->len; 38493953c46cSMarcelo Ricardo Leitner } else { 3850df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3851f4c50d99SHerbert Xu if (len > mss) 3852f4c50d99SHerbert Xu len = mss; 38533953c46cSMarcelo Ricardo Leitner } 3854f4c50d99SHerbert Xu 3855df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3856f4c50d99SHerbert Xu if (hsize < 0) 3857f4c50d99SHerbert Xu hsize = 0; 3858c8884eddSHerbert Xu if (hsize > len || !sg) 3859c8884eddSHerbert Xu hsize = len; 3860f4c50d99SHerbert Xu 38611a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 38621a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 38631a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 386489319d38SHerbert Xu 38659d8506ccSHerbert Xu i = 0; 38661a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 38671a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 38681fd819ecSMichael S. Tsirkin frag_skb = list_skb; 38691a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 38709d8506ccSHerbert Xu 38719d8506ccSHerbert Xu while (pos < offset + len) { 38729d8506ccSHerbert Xu BUG_ON(i >= nfrags); 38739d8506ccSHerbert Xu 38744e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 38759d8506ccSHerbert Xu if (pos + size > offset + len) 38769d8506ccSHerbert Xu break; 38779d8506ccSHerbert Xu 38789d8506ccSHerbert Xu i++; 38799d8506ccSHerbert Xu pos += size; 38804e1beba1SMichael S. Tsirkin frag++; 38819d8506ccSHerbert Xu } 38829d8506ccSHerbert Xu 38831a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 38841a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 388589319d38SHerbert Xu 3886f4c50d99SHerbert Xu if (unlikely(!nskb)) 3887f4c50d99SHerbert Xu goto err; 3888f4c50d99SHerbert Xu 38899d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 38909d8506ccSHerbert Xu kfree_skb(nskb); 38919d8506ccSHerbert Xu goto err; 38929d8506ccSHerbert Xu } 38939d8506ccSHerbert Xu 3894ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 389589319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 389689319d38SHerbert Xu kfree_skb(nskb); 389789319d38SHerbert Xu goto err; 389889319d38SHerbert Xu } 389989319d38SHerbert Xu 3900ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 390189319d38SHerbert Xu skb_release_head_state(nskb); 390289319d38SHerbert Xu __skb_push(nskb, doffset); 390389319d38SHerbert Xu } else { 3904c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3905df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3906c93bdd0eSMel Gorman NUMA_NO_NODE); 390789319d38SHerbert Xu 390889319d38SHerbert Xu if (unlikely(!nskb)) 390989319d38SHerbert Xu goto err; 391089319d38SHerbert Xu 391189319d38SHerbert Xu skb_reserve(nskb, headroom); 391289319d38SHerbert Xu __skb_put(nskb, doffset); 391389319d38SHerbert Xu } 391489319d38SHerbert Xu 3915f4c50d99SHerbert Xu if (segs) 3916f4c50d99SHerbert Xu tail->next = nskb; 3917f4c50d99SHerbert Xu else 3918f4c50d99SHerbert Xu segs = nskb; 3919f4c50d99SHerbert Xu tail = nskb; 3920f4c50d99SHerbert Xu 3921df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3922f4c50d99SHerbert Xu 3923030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3924fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 392568c33163SPravin B Shelar 3926df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 392768c33163SPravin B Shelar nskb->data - tnl_hlen, 392868c33163SPravin B Shelar doffset + tnl_hlen); 392989319d38SHerbert Xu 39309d8506ccSHerbert Xu if (nskb->len == len + doffset) 39311cdbcb79SSimon Horman goto perform_csum_check; 393289319d38SHerbert Xu 39337fbeffedSAlexander Duyck if (!sg) { 39341454c9faSYadu Kishore if (!csum) { 39357fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 39366f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 393776443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 393876443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 39391454c9faSYadu Kishore skb_put(nskb, 39401454c9faSYadu Kishore len), 39418d5930dfSAl Viro len); 39427e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3943de843723STom Herbert skb_headroom(nskb) + doffset; 39441454c9faSYadu Kishore } else { 39451454c9faSYadu Kishore skb_copy_bits(head_skb, offset, 39461454c9faSYadu Kishore skb_put(nskb, len), 39471454c9faSYadu Kishore len); 39481454c9faSYadu Kishore } 3949f4c50d99SHerbert Xu continue; 3950f4c50d99SHerbert Xu } 3951f4c50d99SHerbert Xu 39528cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3953f4c50d99SHerbert Xu 3954df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3955d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3956f4c50d99SHerbert Xu 3957fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3958df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3959cef401deSEric Dumazet 3960bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3961bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 3962bf5c25d6SWillem de Bruijn goto err; 3963bf5c25d6SWillem de Bruijn 39649d8506ccSHerbert Xu while (pos < offset + len) { 39659d8506ccSHerbert Xu if (i >= nfrags) { 39669d8506ccSHerbert Xu i = 0; 39671a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 39681a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 39691fd819ecSMichael S. Tsirkin frag_skb = list_skb; 397013acc94eSYonghong Song if (!skb_headlen(list_skb)) { 39719d8506ccSHerbert Xu BUG_ON(!nfrags); 397213acc94eSYonghong Song } else { 397313acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 39749d8506ccSHerbert Xu 397513acc94eSYonghong Song /* to make room for head_frag. */ 397613acc94eSYonghong Song i--; 397713acc94eSYonghong Song frag--; 397813acc94eSYonghong Song } 3979bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3980bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 3981bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 3982bf5c25d6SWillem de Bruijn goto err; 3983bf5c25d6SWillem de Bruijn 39841a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 39859d8506ccSHerbert Xu } 39869d8506ccSHerbert Xu 39879d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 39889d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 39899d8506ccSHerbert Xu net_warn_ratelimited( 39909d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 39919d8506ccSHerbert Xu pos, mss); 3992ff907a11SEric Dumazet err = -EINVAL; 39939d8506ccSHerbert Xu goto err; 39949d8506ccSHerbert Xu } 39959d8506ccSHerbert Xu 399613acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 39978cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 39988cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3999f4c50d99SHerbert Xu 4000f4c50d99SHerbert Xu if (pos < offset) { 4001b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 40028cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4003f4c50d99SHerbert Xu } 4004f4c50d99SHerbert Xu 400589319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4006f4c50d99SHerbert Xu 4007f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4008f4c50d99SHerbert Xu i++; 40094e1beba1SMichael S. Tsirkin frag++; 4010f4c50d99SHerbert Xu pos += size; 4011f4c50d99SHerbert Xu } else { 40128cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 401389319d38SHerbert Xu goto skip_fraglist; 4014f4c50d99SHerbert Xu } 4015f4c50d99SHerbert Xu 40168cb19905SMichael S. Tsirkin nskb_frag++; 4017f4c50d99SHerbert Xu } 4018f4c50d99SHerbert Xu 401989319d38SHerbert Xu skip_fraglist: 4020f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4021f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4022f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4023ec5f0615SPravin B Shelar 40241cdbcb79SSimon Horman perform_csum_check: 40257fbeffedSAlexander Duyck if (!csum) { 4026ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4027ff907a11SEric Dumazet __skb_linearize(nskb)) 4028ddff00d4SAlexander Duyck goto err; 4029ff907a11SEric Dumazet 40307fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4031ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 403276443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 403376443456SAlexander Duyck skb_checksum(nskb, doffset, 403476443456SAlexander Duyck nskb->len - doffset, 0); 40357e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 40367e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4037ec5f0615SPravin B Shelar } 4038df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4039f4c50d99SHerbert Xu 4040bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4041bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4042bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4043bec3cfdcSEric Dumazet */ 4044bec3cfdcSEric Dumazet segs->prev = tail; 4045432c856fSToshiaki Makita 4046802ab55aSAlexander Duyck if (partial_segs) { 404707b26c94SSteffen Klassert struct sk_buff *iter; 4048802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 404907b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4050802ab55aSAlexander Duyck 4051802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 405207b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4053802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4054802ab55aSAlexander Duyck 4055802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4056802ab55aSAlexander Duyck * our way back down the stack of protocols. 4057802ab55aSAlexander Duyck */ 405807b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 405907b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 406007b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 406107b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 406207b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 406307b26c94SSteffen Klassert } 406407b26c94SSteffen Klassert 406507b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 406607b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 406707b26c94SSteffen Klassert else if (tail != segs) 406807b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4069802ab55aSAlexander Duyck } 4070802ab55aSAlexander Duyck 4071432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4072432c856fSToshiaki Makita * using skb_set_owner_w(). 4073432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4074432c856fSToshiaki Makita */ 4075432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4076432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4077432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4078432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4079432c856fSToshiaki Makita } 4080f4c50d99SHerbert Xu return segs; 4081f4c50d99SHerbert Xu 4082f4c50d99SHerbert Xu err: 4083289dccbeSEric Dumazet kfree_skb_list(segs); 4084f4c50d99SHerbert Xu return ERR_PTR(err); 4085f4c50d99SHerbert Xu } 4086f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4087f4c50d99SHerbert Xu 4088d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) 408971d93b39SHerbert Xu { 40908a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 409167147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 409267147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 40938a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 4094715dc1f3SEric Dumazet unsigned int delta_truesize; 4095d4546c25SDavid Miller struct sk_buff *lp; 409671d93b39SHerbert Xu 40970ab03f35SSteffen Klassert if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush)) 409871d93b39SHerbert Xu return -E2BIG; 409971d93b39SHerbert Xu 410029e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 41018a29111cSEric Dumazet pinfo = skb_shinfo(lp); 41028a29111cSEric Dumazet 41038a29111cSEric Dumazet if (headlen <= offset) { 410442da6994SHerbert Xu skb_frag_t *frag; 410566e92fcfSHerbert Xu skb_frag_t *frag2; 41069aaa156cSHerbert Xu int i = skbinfo->nr_frags; 41079aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 410842da6994SHerbert Xu 410966e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 41108a29111cSEric Dumazet goto merge; 411181705ad1SHerbert Xu 41128a29111cSEric Dumazet offset -= headlen; 41139aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 41149aaa156cSHerbert Xu skbinfo->nr_frags = 0; 4115f5572068SHerbert Xu 41169aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 41179aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 411866e92fcfSHerbert Xu do { 411966e92fcfSHerbert Xu *--frag = *--frag2; 412066e92fcfSHerbert Xu } while (--i); 412166e92fcfSHerbert Xu 4122b54c9d5bSJonathan Lemon skb_frag_off_add(frag, offset); 41239e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 412466e92fcfSHerbert Xu 4125715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 4126ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 4127ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 4128715dc1f3SEric Dumazet 4129f5572068SHerbert Xu skb->truesize -= skb->data_len; 4130f5572068SHerbert Xu skb->len -= skb->data_len; 4131f5572068SHerbert Xu skb->data_len = 0; 4132f5572068SHerbert Xu 4133715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 41345d38a079SHerbert Xu goto done; 4135d7e8883cSEric Dumazet } else if (skb->head_frag) { 4136d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 4137d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 4138d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 4139d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 4140d7e8883cSEric Dumazet unsigned int first_offset; 4141d7e8883cSEric Dumazet 4142d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 41438a29111cSEric Dumazet goto merge; 4144d7e8883cSEric Dumazet 4145d7e8883cSEric Dumazet first_offset = skb->data - 4146d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 4147d7e8883cSEric Dumazet offset; 4148d7e8883cSEric Dumazet 4149d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 4150d7e8883cSEric Dumazet 4151d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(frag, page); 4152b54c9d5bSJonathan Lemon skb_frag_off_set(frag, first_offset); 4153d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 4154d7e8883cSEric Dumazet 4155d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 4156d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 4157d7e8883cSEric Dumazet 4158715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4159d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 4160d7e8883cSEric Dumazet goto done; 41618a29111cSEric Dumazet } 416271d93b39SHerbert Xu 416371d93b39SHerbert Xu merge: 4164715dc1f3SEric Dumazet delta_truesize = skb->truesize; 416567147ba9SHerbert Xu if (offset > headlen) { 4166d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 4167d1dc7abfSMichal Schmidt 4168b54c9d5bSJonathan Lemon skb_frag_off_add(&skbinfo->frags[0], eat); 41699e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 4170d1dc7abfSMichal Schmidt skb->data_len -= eat; 4171d1dc7abfSMichal Schmidt skb->len -= eat; 417267147ba9SHerbert Xu offset = headlen; 417356035022SHerbert Xu } 417456035022SHerbert Xu 417567147ba9SHerbert Xu __skb_pull(skb, offset); 417656035022SHerbert Xu 417729e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 41788a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 41798a29111cSEric Dumazet else 4180c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 4181c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 4182f4a775d1SEric Dumazet __skb_header_release(skb); 41838a29111cSEric Dumazet lp = p; 418471d93b39SHerbert Xu 41855d38a079SHerbert Xu done: 41865d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 418737fe4732SHerbert Xu p->data_len += len; 4188715dc1f3SEric Dumazet p->truesize += delta_truesize; 418937fe4732SHerbert Xu p->len += len; 41908a29111cSEric Dumazet if (lp != p) { 41918a29111cSEric Dumazet lp->data_len += len; 41928a29111cSEric Dumazet lp->truesize += delta_truesize; 41938a29111cSEric Dumazet lp->len += len; 41948a29111cSEric Dumazet } 419571d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 419671d93b39SHerbert Xu return 0; 419771d93b39SHerbert Xu } 419871d93b39SHerbert Xu 4199df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4200df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4201df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4202df5042f4SFlorian Westphal 4203df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4204df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4205df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4206df5042f4SFlorian Westphal #endif 42074165079bSFlorian Westphal #ifdef CONFIG_XFRM 42084165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 42094165079bSFlorian Westphal #endif 421095a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 421195a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 421295a7233cSPaul Blakey #endif 42133ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 42143ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 42153ee17bc7SMat Martineau #endif 4216df5042f4SFlorian Westphal }; 4217df5042f4SFlorian Westphal 4218df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4219df5042f4SFlorian Westphal { 4220df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4221df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4222df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4223df5042f4SFlorian Westphal #endif 42244165079bSFlorian Westphal #ifdef CONFIG_XFRM 42254165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 42264165079bSFlorian Westphal #endif 422795a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 422895a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 422995a7233cSPaul Blakey #endif 42303ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 42313ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 42323ee17bc7SMat Martineau #endif 4233df5042f4SFlorian Westphal 0; 4234df5042f4SFlorian Westphal } 4235df5042f4SFlorian Westphal 4236df5042f4SFlorian Westphal static void skb_extensions_init(void) 4237df5042f4SFlorian Westphal { 4238df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4239df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4240df5042f4SFlorian Westphal 4241df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4242df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4243df5042f4SFlorian Westphal 0, 4244df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4245df5042f4SFlorian Westphal NULL); 4246df5042f4SFlorian Westphal } 4247df5042f4SFlorian Westphal #else 4248df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4249df5042f4SFlorian Westphal #endif 4250df5042f4SFlorian Westphal 42511da177e4SLinus Torvalds void __init skb_init(void) 42521da177e4SLinus Torvalds { 425379a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 42541da177e4SLinus Torvalds sizeof(struct sk_buff), 42551da177e4SLinus Torvalds 0, 4256e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 425779a8a642SKees Cook offsetof(struct sk_buff, cb), 425879a8a642SKees Cook sizeof_field(struct sk_buff, cb), 425920c2df83SPaul Mundt NULL); 4260d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4261d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4262d179cd12SDavid S. Miller 0, 4263e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 426420c2df83SPaul Mundt NULL); 4265df5042f4SFlorian Westphal skb_extensions_init(); 42661da177e4SLinus Torvalds } 42671da177e4SLinus Torvalds 426851c739d1SDavid S. Miller static int 426948a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 427048a1df65SJason A. Donenfeld unsigned int recursion_level) 4271716ea3a7SDavid Howells { 42721a028e50SDavid S. Miller int start = skb_headlen(skb); 42731a028e50SDavid S. Miller int i, copy = start - offset; 4274fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4275716ea3a7SDavid Howells int elt = 0; 4276716ea3a7SDavid Howells 427748a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 427848a1df65SJason A. Donenfeld return -EMSGSIZE; 427948a1df65SJason A. Donenfeld 4280716ea3a7SDavid Howells if (copy > 0) { 4281716ea3a7SDavid Howells if (copy > len) 4282716ea3a7SDavid Howells copy = len; 4283642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4284716ea3a7SDavid Howells elt++; 4285716ea3a7SDavid Howells if ((len -= copy) == 0) 4286716ea3a7SDavid Howells return elt; 4287716ea3a7SDavid Howells offset += copy; 4288716ea3a7SDavid Howells } 4289716ea3a7SDavid Howells 4290716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 42911a028e50SDavid S. Miller int end; 4292716ea3a7SDavid Howells 4293547b792cSIlpo Järvinen WARN_ON(start > offset + len); 42941a028e50SDavid S. Miller 42959e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4296716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4297716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 429848a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 429948a1df65SJason A. Donenfeld return -EMSGSIZE; 4300716ea3a7SDavid Howells 4301716ea3a7SDavid Howells if (copy > len) 4302716ea3a7SDavid Howells copy = len; 4303ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4304b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4305716ea3a7SDavid Howells elt++; 4306716ea3a7SDavid Howells if (!(len -= copy)) 4307716ea3a7SDavid Howells return elt; 4308716ea3a7SDavid Howells offset += copy; 4309716ea3a7SDavid Howells } 43101a028e50SDavid S. Miller start = end; 4311716ea3a7SDavid Howells } 4312716ea3a7SDavid Howells 4313fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 431448a1df65SJason A. Donenfeld int end, ret; 4315716ea3a7SDavid Howells 4316547b792cSIlpo Järvinen WARN_ON(start > offset + len); 43171a028e50SDavid S. Miller 4318fbb398a8SDavid S. Miller end = start + frag_iter->len; 4319716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 432048a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 432148a1df65SJason A. Donenfeld return -EMSGSIZE; 432248a1df65SJason A. Donenfeld 4323716ea3a7SDavid Howells if (copy > len) 4324716ea3a7SDavid Howells copy = len; 432548a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 432648a1df65SJason A. Donenfeld copy, recursion_level + 1); 432748a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 432848a1df65SJason A. Donenfeld return ret; 432948a1df65SJason A. Donenfeld elt += ret; 4330716ea3a7SDavid Howells if ((len -= copy) == 0) 4331716ea3a7SDavid Howells return elt; 4332716ea3a7SDavid Howells offset += copy; 4333716ea3a7SDavid Howells } 43341a028e50SDavid S. Miller start = end; 4335716ea3a7SDavid Howells } 4336716ea3a7SDavid Howells BUG_ON(len); 4337716ea3a7SDavid Howells return elt; 4338716ea3a7SDavid Howells } 4339716ea3a7SDavid Howells 434048a1df65SJason A. Donenfeld /** 434148a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 434248a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 434348a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 434448a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 434548a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 434648a1df65SJason A. Donenfeld * 434748a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 434848a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 434948a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 435048a1df65SJason A. Donenfeld * could not fit. 435148a1df65SJason A. Donenfeld */ 435248a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 435348a1df65SJason A. Donenfeld { 435448a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 435548a1df65SJason A. Donenfeld 435648a1df65SJason A. Donenfeld if (nsg <= 0) 435748a1df65SJason A. Donenfeld return nsg; 435848a1df65SJason A. Donenfeld 435948a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 436048a1df65SJason A. Donenfeld 436148a1df65SJason A. Donenfeld return nsg; 436248a1df65SJason A. Donenfeld } 436348a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 436448a1df65SJason A. Donenfeld 436525a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 436625a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 436725a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 436825a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 436925a91d8dSFan Du * 437025a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 437125a91d8dSFan Du * 1. sg_init_table 437225a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 437325a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 437425a91d8dSFan Du * 437525a91d8dSFan Du * This is equivalent to: 437625a91d8dSFan Du * 1. sg_init_table 437725a91d8dSFan Du * 2. skb_to_sgvec(payload1) 437825a91d8dSFan Du * 3. sg_unmark_end 437925a91d8dSFan Du * 4. skb_to_sgvec(payload2) 438025a91d8dSFan Du * 438125a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 438225a91d8dSFan Du * is more preferable. 438325a91d8dSFan Du */ 438425a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 438525a91d8dSFan Du int offset, int len) 438625a91d8dSFan Du { 438748a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 438825a91d8dSFan Du } 438925a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 439025a91d8dSFan Du 439151c739d1SDavid S. Miller 439251c739d1SDavid S. Miller 4393716ea3a7SDavid Howells /** 4394716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4395716ea3a7SDavid Howells * @skb: The socket buffer to check. 4396716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4397716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4398716ea3a7SDavid Howells * 4399716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4400716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4401716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4402716ea3a7SDavid Howells * 4403716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4404716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4405716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4406716ea3a7SDavid Howells * 4407716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4408716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4409716ea3a7SDavid Howells */ 4410716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4411716ea3a7SDavid Howells { 4412716ea3a7SDavid Howells int copyflag; 4413716ea3a7SDavid Howells int elt; 4414716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4415716ea3a7SDavid Howells 4416716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4417716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4418716ea3a7SDavid Howells * at the moment even if they are anonymous). 4419716ea3a7SDavid Howells */ 4420716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4421c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4422716ea3a7SDavid Howells return -ENOMEM; 4423716ea3a7SDavid Howells 4424716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 442521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4426716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4427716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4428716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4429716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4430716ea3a7SDavid Howells 4431716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4432716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4433716ea3a7SDavid Howells return -ENOMEM; 4434716ea3a7SDavid Howells 4435716ea3a7SDavid Howells /* Voila! */ 4436716ea3a7SDavid Howells *trailer = skb; 4437716ea3a7SDavid Howells return 1; 4438716ea3a7SDavid Howells } 4439716ea3a7SDavid Howells 4440716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4441716ea3a7SDavid Howells 4442716ea3a7SDavid Howells elt = 1; 4443716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4444716ea3a7SDavid Howells copyflag = 0; 4445716ea3a7SDavid Howells 4446716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4447716ea3a7SDavid Howells int ntail = 0; 4448716ea3a7SDavid Howells 4449716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4450716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4451716ea3a7SDavid Howells * after it. */ 4452716ea3a7SDavid Howells 4453716ea3a7SDavid Howells if (skb_shared(skb1)) 4454716ea3a7SDavid Howells copyflag = 1; 4455716ea3a7SDavid Howells 4456716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4457716ea3a7SDavid Howells 4458716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4459716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 446021dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4461716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4462716ea3a7SDavid Howells ntail = tailbits + 128; 4463716ea3a7SDavid Howells } 4464716ea3a7SDavid Howells 4465716ea3a7SDavid Howells if (copyflag || 4466716ea3a7SDavid Howells skb_cloned(skb1) || 4467716ea3a7SDavid Howells ntail || 4468716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 446921dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4470716ea3a7SDavid Howells struct sk_buff *skb2; 4471716ea3a7SDavid Howells 4472716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4473716ea3a7SDavid Howells if (ntail == 0) 4474716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4475716ea3a7SDavid Howells else 4476716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4477716ea3a7SDavid Howells skb_headroom(skb1), 4478716ea3a7SDavid Howells ntail, 4479716ea3a7SDavid Howells GFP_ATOMIC); 4480716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4481716ea3a7SDavid Howells return -ENOMEM; 4482716ea3a7SDavid Howells 4483716ea3a7SDavid Howells if (skb1->sk) 4484716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4485716ea3a7SDavid Howells 4486716ea3a7SDavid Howells /* Looking around. Are we still alive? 4487716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4488716ea3a7SDavid Howells 4489716ea3a7SDavid Howells skb2->next = skb1->next; 4490716ea3a7SDavid Howells *skb_p = skb2; 4491716ea3a7SDavid Howells kfree_skb(skb1); 4492716ea3a7SDavid Howells skb1 = skb2; 4493716ea3a7SDavid Howells } 4494716ea3a7SDavid Howells elt++; 4495716ea3a7SDavid Howells *trailer = skb1; 4496716ea3a7SDavid Howells skb_p = &skb1->next; 4497716ea3a7SDavid Howells } 4498716ea3a7SDavid Howells 4499716ea3a7SDavid Howells return elt; 4500716ea3a7SDavid Howells } 4501b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4502716ea3a7SDavid Howells 4503b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4504b1faf566SEric Dumazet { 4505b1faf566SEric Dumazet struct sock *sk = skb->sk; 4506b1faf566SEric Dumazet 4507b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4508b1faf566SEric Dumazet } 4509b1faf566SEric Dumazet 45108605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 45118605330aSSoheil Hassas Yeganeh { 45128605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 45138605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 45148605330aSSoheil Hassas Yeganeh */ 45158605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 45168605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 45178605330aSSoheil Hassas Yeganeh } 45188605330aSSoheil Hassas Yeganeh 4519b1faf566SEric Dumazet /* 4520b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4521b1faf566SEric Dumazet */ 4522b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4523b1faf566SEric Dumazet { 4524b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4525ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4526b1faf566SEric Dumazet return -ENOMEM; 4527b1faf566SEric Dumazet 4528b1faf566SEric Dumazet skb_orphan(skb); 4529b1faf566SEric Dumazet skb->sk = sk; 4530b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4531b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 45328605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4533b1faf566SEric Dumazet 4534abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4535abb57ea4SEric Dumazet skb_dst_force(skb); 4536abb57ea4SEric Dumazet 4537b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4538b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 45396e5d58fdSVinicius Costa Gomes sk->sk_error_report(sk); 4540b1faf566SEric Dumazet return 0; 4541b1faf566SEric Dumazet } 4542b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4543b1faf566SEric Dumazet 454483a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 454583a1a1a7SSoheil Hassas Yeganeh { 454683a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 454783a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 454883a1a1a7SSoheil Hassas Yeganeh } 454983a1a1a7SSoheil Hassas Yeganeh 4550364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4551364a9e93SWillem de Bruijn { 4552364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 455383a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 455483a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4555997d5c3fSEric Dumazet unsigned long flags; 4556364a9e93SWillem de Bruijn 4557997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4558364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 455938b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 456083a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 456138b25793SSoheil Hassas Yeganeh if (icmp_next) 4562985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 456338b25793SSoheil Hassas Yeganeh } 4564997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4565364a9e93SWillem de Bruijn 456683a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 456783a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 456883a1a1a7SSoheil Hassas Yeganeh 456983a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4570364a9e93SWillem de Bruijn sk->sk_error_report(sk); 4571364a9e93SWillem de Bruijn 4572364a9e93SWillem de Bruijn return skb; 4573364a9e93SWillem de Bruijn } 4574364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4575364a9e93SWillem de Bruijn 4576cab41c47SAlexander Duyck /** 4577cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4578cab41c47SAlexander Duyck * @skb: the skb to clone 4579cab41c47SAlexander Duyck * 4580cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4581cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4582cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4583cab41c47SAlexander Duyck * 4584cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4585cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4586cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4587cab41c47SAlexander Duyck * the sk_error_queue. 4588cab41c47SAlexander Duyck */ 458962bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 459062bccb8cSAlexander Duyck { 459162bccb8cSAlexander Duyck struct sock *sk = skb->sk; 459262bccb8cSAlexander Duyck struct sk_buff *clone; 459362bccb8cSAlexander Duyck 459441c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 459562bccb8cSAlexander Duyck return NULL; 459662bccb8cSAlexander Duyck 459762bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 459862bccb8cSAlexander Duyck if (!clone) { 459962bccb8cSAlexander Duyck sock_put(sk); 460062bccb8cSAlexander Duyck return NULL; 460162bccb8cSAlexander Duyck } 460262bccb8cSAlexander Duyck 460362bccb8cSAlexander Duyck clone->sk = sk; 460462bccb8cSAlexander Duyck clone->destructor = sock_efree; 460562bccb8cSAlexander Duyck 460662bccb8cSAlexander Duyck return clone; 460762bccb8cSAlexander Duyck } 460862bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 460962bccb8cSAlexander Duyck 461037846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 461137846ef0SAlexander Duyck struct sock *sk, 46124ef1b286SSoheil Hassas Yeganeh int tstype, 46134ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4614ac45f602SPatrick Ohly { 4615ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4616ac45f602SPatrick Ohly int err; 4617ac45f602SPatrick Ohly 46184ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 46194ef1b286SSoheil Hassas Yeganeh 4620ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4621ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4622ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4623ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4624e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 46254ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 46261862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 46274ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 462809c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4629ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4630ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 46314ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 46324ed2d765SWillem de Bruijn } 463329030374SEric Dumazet 4634ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 463529030374SEric Dumazet 4636ac45f602SPatrick Ohly if (err) 4637ac45f602SPatrick Ohly kfree_skb(skb); 4638ac45f602SPatrick Ohly } 463937846ef0SAlexander Duyck 4640b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4641b245be1fSWillem de Bruijn { 4642b245be1fSWillem de Bruijn bool ret; 4643b245be1fSWillem de Bruijn 4644b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4645b245be1fSWillem de Bruijn return true; 4646b245be1fSWillem de Bruijn 4647b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4648b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4649b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4650b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4651b245be1fSWillem de Bruijn return ret; 4652b245be1fSWillem de Bruijn } 4653b245be1fSWillem de Bruijn 465437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 465537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 465637846ef0SAlexander Duyck { 465737846ef0SAlexander Duyck struct sock *sk = skb->sk; 465837846ef0SAlexander Duyck 4659b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 466035b99dffSWillem de Bruijn goto err; 4661b245be1fSWillem de Bruijn 46629ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 46639ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 46649ac25fc0SEric Dumazet */ 466541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 466637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 46674ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 466837846ef0SAlexander Duyck sock_put(sk); 466935b99dffSWillem de Bruijn return; 467037846ef0SAlexander Duyck } 467135b99dffSWillem de Bruijn 467235b99dffSWillem de Bruijn err: 467335b99dffSWillem de Bruijn kfree_skb(skb); 46749ac25fc0SEric Dumazet } 467537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 467637846ef0SAlexander Duyck 467737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 467837846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 467937846ef0SAlexander Duyck struct sock *sk, int tstype) 468037846ef0SAlexander Duyck { 468137846ef0SAlexander Duyck struct sk_buff *skb; 46824ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 468337846ef0SAlexander Duyck 46843a8dd971SWillem de Bruijn if (!sk) 46853a8dd971SWillem de Bruijn return; 46863a8dd971SWillem de Bruijn 4687b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4688b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4689b50a5c70SMiroslav Lichvar return; 4690b50a5c70SMiroslav Lichvar 46913a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 46923a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 469337846ef0SAlexander Duyck return; 469437846ef0SAlexander Duyck 46951c885808SFrancis Yan if (tsonly) { 46961c885808SFrancis Yan #ifdef CONFIG_INET 46971c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 46981c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 46994ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 470048040793SYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb); 47014ef1b286SSoheil Hassas Yeganeh opt_stats = true; 47024ef1b286SSoheil Hassas Yeganeh } else 47031c885808SFrancis Yan #endif 47041c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 47051c885808SFrancis Yan } else { 470637846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 47071c885808SFrancis Yan } 470837846ef0SAlexander Duyck if (!skb) 470937846ef0SAlexander Duyck return; 471037846ef0SAlexander Duyck 471149ca0d8bSWillem de Bruijn if (tsonly) { 4712fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4713fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 471449ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 471549ca0d8bSWillem de Bruijn } 471649ca0d8bSWillem de Bruijn 471749ca0d8bSWillem de Bruijn if (hwtstamps) 471849ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 471949ca0d8bSWillem de Bruijn else 472049ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 472149ca0d8bSWillem de Bruijn 47224ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 472337846ef0SAlexander Duyck } 4724e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4725e7fd2885SWillem de Bruijn 4726e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4727e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4728e7fd2885SWillem de Bruijn { 4729e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4730e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4731e7fd2885SWillem de Bruijn } 4732ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4733ac45f602SPatrick Ohly 47346e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 47356e3e939fSJohannes Berg { 47366e3e939fSJohannes Berg struct sock *sk = skb->sk; 47376e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4738dd4f1072SEric Dumazet int err = 1; 47396e3e939fSJohannes Berg 47406e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 47416e3e939fSJohannes Berg skb->wifi_acked = acked; 47426e3e939fSJohannes Berg 47436e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 47446e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 47456e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 47466e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 47476e3e939fSJohannes Berg 4748dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4749dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4750dd4f1072SEric Dumazet */ 475141c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 47526e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4753dd4f1072SEric Dumazet sock_put(sk); 4754dd4f1072SEric Dumazet } 47556e3e939fSJohannes Berg if (err) 47566e3e939fSJohannes Berg kfree_skb(skb); 47576e3e939fSJohannes Berg } 47586e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 47596e3e939fSJohannes Berg 4760f35d9d8aSRusty Russell /** 4761f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4762f35d9d8aSRusty Russell * @skb: the skb to set 4763f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4764f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4765f35d9d8aSRusty Russell * 4766f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4767f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4768f35d9d8aSRusty Russell * 4769f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4770f35d9d8aSRusty Russell * returns false you should drop the packet. 4771f35d9d8aSRusty Russell */ 4772f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4773f35d9d8aSRusty Russell { 477452b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 477552b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 477652b5d6f5SEric Dumazet 477752b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 477852b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 477952b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4780f35d9d8aSRusty Russell return false; 4781f35d9d8aSRusty Russell } 4782f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 478352b5d6f5SEric Dumazet skb->csum_start = csum_start; 4784f35d9d8aSRusty Russell skb->csum_offset = off; 4785e5d5decaSJason Wang skb_set_transport_header(skb, start); 4786f35d9d8aSRusty Russell return true; 4787f35d9d8aSRusty Russell } 4788b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4789f35d9d8aSRusty Russell 4790ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4791ed1f50c3SPaul Durrant unsigned int max) 4792ed1f50c3SPaul Durrant { 4793ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4794ed1f50c3SPaul Durrant return 0; 4795ed1f50c3SPaul Durrant 4796ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4797ed1f50c3SPaul Durrant * won't need to do it again. 4798ed1f50c3SPaul Durrant */ 4799ed1f50c3SPaul Durrant if (max > skb->len) 4800ed1f50c3SPaul Durrant max = skb->len; 4801ed1f50c3SPaul Durrant 4802ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4803ed1f50c3SPaul Durrant return -ENOMEM; 4804ed1f50c3SPaul Durrant 4805ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4806ed1f50c3SPaul Durrant return -EPROTO; 4807ed1f50c3SPaul Durrant 4808ed1f50c3SPaul Durrant return 0; 4809ed1f50c3SPaul Durrant } 4810ed1f50c3SPaul Durrant 4811f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4812f9708b43SJan Beulich 4813f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4814f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4815f9708b43SJan Beulich unsigned int off) 4816f9708b43SJan Beulich { 4817f9708b43SJan Beulich int err; 4818f9708b43SJan Beulich 4819161d1792SKees Cook switch (proto) { 4820f9708b43SJan Beulich case IPPROTO_TCP: 4821f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4822f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4823f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4824f9708b43SJan Beulich offsetof(struct tcphdr, 4825f9708b43SJan Beulich check))) 4826f9708b43SJan Beulich err = -EPROTO; 4827f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4828f9708b43SJan Beulich 4829f9708b43SJan Beulich case IPPROTO_UDP: 4830f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4831f9708b43SJan Beulich off + sizeof(struct udphdr)); 4832f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4833f9708b43SJan Beulich offsetof(struct udphdr, 4834f9708b43SJan Beulich check))) 4835f9708b43SJan Beulich err = -EPROTO; 4836f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4837f9708b43SJan Beulich } 4838f9708b43SJan Beulich 4839f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4840f9708b43SJan Beulich } 4841f9708b43SJan Beulich 4842ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4843ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4844ed1f50c3SPaul Durrant */ 4845ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4846ed1f50c3SPaul Durrant 4847f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4848ed1f50c3SPaul Durrant { 4849ed1f50c3SPaul Durrant unsigned int off; 4850ed1f50c3SPaul Durrant bool fragment; 4851f9708b43SJan Beulich __sum16 *csum; 4852ed1f50c3SPaul Durrant int err; 4853ed1f50c3SPaul Durrant 4854ed1f50c3SPaul Durrant fragment = false; 4855ed1f50c3SPaul Durrant 4856ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4857ed1f50c3SPaul Durrant sizeof(struct iphdr), 4858ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4859ed1f50c3SPaul Durrant if (err < 0) 4860ed1f50c3SPaul Durrant goto out; 4861ed1f50c3SPaul Durrant 486211f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 4863ed1f50c3SPaul Durrant fragment = true; 4864ed1f50c3SPaul Durrant 4865ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4866ed1f50c3SPaul Durrant 4867ed1f50c3SPaul Durrant err = -EPROTO; 4868ed1f50c3SPaul Durrant 4869ed1f50c3SPaul Durrant if (fragment) 4870ed1f50c3SPaul Durrant goto out; 4871ed1f50c3SPaul Durrant 4872f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4873f9708b43SJan Beulich if (IS_ERR(csum)) 4874f9708b43SJan Beulich return PTR_ERR(csum); 4875ed1f50c3SPaul Durrant 4876ed1f50c3SPaul Durrant if (recalculate) 4877f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4878ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4879ed1f50c3SPaul Durrant skb->len - off, 4880f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4881ed1f50c3SPaul Durrant err = 0; 4882ed1f50c3SPaul Durrant 4883ed1f50c3SPaul Durrant out: 4884ed1f50c3SPaul Durrant return err; 4885ed1f50c3SPaul Durrant } 4886ed1f50c3SPaul Durrant 4887ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4888ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4889ed1f50c3SPaul Durrant */ 4890ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4891ed1f50c3SPaul Durrant 4892ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4893ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4894ed1f50c3SPaul Durrant 4895ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4896ed1f50c3SPaul Durrant { 4897ed1f50c3SPaul Durrant int err; 4898ed1f50c3SPaul Durrant u8 nexthdr; 4899ed1f50c3SPaul Durrant unsigned int off; 4900ed1f50c3SPaul Durrant unsigned int len; 4901ed1f50c3SPaul Durrant bool fragment; 4902ed1f50c3SPaul Durrant bool done; 4903f9708b43SJan Beulich __sum16 *csum; 4904ed1f50c3SPaul Durrant 4905ed1f50c3SPaul Durrant fragment = false; 4906ed1f50c3SPaul Durrant done = false; 4907ed1f50c3SPaul Durrant 4908ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4909ed1f50c3SPaul Durrant 4910ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4911ed1f50c3SPaul Durrant if (err < 0) 4912ed1f50c3SPaul Durrant goto out; 4913ed1f50c3SPaul Durrant 4914ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4915ed1f50c3SPaul Durrant 4916ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4917ed1f50c3SPaul Durrant while (off <= len && !done) { 4918ed1f50c3SPaul Durrant switch (nexthdr) { 4919ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4920ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4921ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4922ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4923ed1f50c3SPaul Durrant 4924ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4925ed1f50c3SPaul Durrant off + 4926ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4927ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4928ed1f50c3SPaul Durrant if (err < 0) 4929ed1f50c3SPaul Durrant goto out; 4930ed1f50c3SPaul Durrant 4931ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4932ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4933ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4934ed1f50c3SPaul Durrant break; 4935ed1f50c3SPaul Durrant } 4936ed1f50c3SPaul Durrant case IPPROTO_AH: { 4937ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4938ed1f50c3SPaul Durrant 4939ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4940ed1f50c3SPaul Durrant off + 4941ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4942ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4943ed1f50c3SPaul Durrant if (err < 0) 4944ed1f50c3SPaul Durrant goto out; 4945ed1f50c3SPaul Durrant 4946ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4947ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4948ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4949ed1f50c3SPaul Durrant break; 4950ed1f50c3SPaul Durrant } 4951ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4952ed1f50c3SPaul Durrant struct frag_hdr *hp; 4953ed1f50c3SPaul Durrant 4954ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4955ed1f50c3SPaul Durrant off + 4956ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4957ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4958ed1f50c3SPaul Durrant if (err < 0) 4959ed1f50c3SPaul Durrant goto out; 4960ed1f50c3SPaul Durrant 4961ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4962ed1f50c3SPaul Durrant 4963ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4964ed1f50c3SPaul Durrant fragment = true; 4965ed1f50c3SPaul Durrant 4966ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4967ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4968ed1f50c3SPaul Durrant break; 4969ed1f50c3SPaul Durrant } 4970ed1f50c3SPaul Durrant default: 4971ed1f50c3SPaul Durrant done = true; 4972ed1f50c3SPaul Durrant break; 4973ed1f50c3SPaul Durrant } 4974ed1f50c3SPaul Durrant } 4975ed1f50c3SPaul Durrant 4976ed1f50c3SPaul Durrant err = -EPROTO; 4977ed1f50c3SPaul Durrant 4978ed1f50c3SPaul Durrant if (!done || fragment) 4979ed1f50c3SPaul Durrant goto out; 4980ed1f50c3SPaul Durrant 4981f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4982f9708b43SJan Beulich if (IS_ERR(csum)) 4983f9708b43SJan Beulich return PTR_ERR(csum); 4984ed1f50c3SPaul Durrant 4985ed1f50c3SPaul Durrant if (recalculate) 4986f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4987ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4988f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4989ed1f50c3SPaul Durrant err = 0; 4990ed1f50c3SPaul Durrant 4991ed1f50c3SPaul Durrant out: 4992ed1f50c3SPaul Durrant return err; 4993ed1f50c3SPaul Durrant } 4994ed1f50c3SPaul Durrant 4995ed1f50c3SPaul Durrant /** 4996ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4997ed1f50c3SPaul Durrant * @skb: the skb to set up 4998ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4999ed1f50c3SPaul Durrant */ 5000ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5001ed1f50c3SPaul Durrant { 5002ed1f50c3SPaul Durrant int err; 5003ed1f50c3SPaul Durrant 5004ed1f50c3SPaul Durrant switch (skb->protocol) { 5005ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5006f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5007ed1f50c3SPaul Durrant break; 5008ed1f50c3SPaul Durrant 5009ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5010ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5011ed1f50c3SPaul Durrant break; 5012ed1f50c3SPaul Durrant 5013ed1f50c3SPaul Durrant default: 5014ed1f50c3SPaul Durrant err = -EPROTO; 5015ed1f50c3SPaul Durrant break; 5016ed1f50c3SPaul Durrant } 5017ed1f50c3SPaul Durrant 5018ed1f50c3SPaul Durrant return err; 5019ed1f50c3SPaul Durrant } 5020ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5021ed1f50c3SPaul Durrant 50229afd85c9SLinus Lüssing /** 50239afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 50249afd85c9SLinus Lüssing * @skb: the skb to check 50259afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 50269afd85c9SLinus Lüssing * 50279afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 50289afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 50299afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 50309afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 50319afd85c9SLinus Lüssing * 5032a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5033a516993fSLinus Lüssing * differs from the provided skb. 50349afd85c9SLinus Lüssing */ 50359afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 50369afd85c9SLinus Lüssing unsigned int transport_len) 50379afd85c9SLinus Lüssing { 50389afd85c9SLinus Lüssing struct sk_buff *skb_chk; 50399afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 50409afd85c9SLinus Lüssing int ret; 50419afd85c9SLinus Lüssing 5042a516993fSLinus Lüssing if (skb->len < len) 50439afd85c9SLinus Lüssing return NULL; 5044a516993fSLinus Lüssing else if (skb->len == len) 50459afd85c9SLinus Lüssing return skb; 50469afd85c9SLinus Lüssing 50479afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 50489afd85c9SLinus Lüssing if (!skb_chk) 50499afd85c9SLinus Lüssing return NULL; 50509afd85c9SLinus Lüssing 50519afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 50529afd85c9SLinus Lüssing if (ret) { 50539afd85c9SLinus Lüssing kfree_skb(skb_chk); 50549afd85c9SLinus Lüssing return NULL; 50559afd85c9SLinus Lüssing } 50569afd85c9SLinus Lüssing 50579afd85c9SLinus Lüssing return skb_chk; 50589afd85c9SLinus Lüssing } 50599afd85c9SLinus Lüssing 50609afd85c9SLinus Lüssing /** 50619afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 50629afd85c9SLinus Lüssing * @skb: the skb to check 50639afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 50649afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 50659afd85c9SLinus Lüssing * 50669afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 50679afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 50689afd85c9SLinus Lüssing * 50699afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 50709afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 50719afd85c9SLinus Lüssing * 5072a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5073a516993fSLinus Lüssing * differs from the provided skb. 50749afd85c9SLinus Lüssing */ 50759afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 50769afd85c9SLinus Lüssing unsigned int transport_len, 50779afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 50789afd85c9SLinus Lüssing { 50799afd85c9SLinus Lüssing struct sk_buff *skb_chk; 50809afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5081fcba67c9SLinus Lüssing __sum16 ret; 50829afd85c9SLinus Lüssing 50839afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 50849afd85c9SLinus Lüssing if (!skb_chk) 5085a516993fSLinus Lüssing goto err; 50869afd85c9SLinus Lüssing 5087a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5088a516993fSLinus Lüssing goto err; 50899afd85c9SLinus Lüssing 50909b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 50919afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 50929b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 50939afd85c9SLinus Lüssing 5094a516993fSLinus Lüssing if (ret) 5095a516993fSLinus Lüssing goto err; 50969afd85c9SLinus Lüssing 50979afd85c9SLinus Lüssing return skb_chk; 5098a516993fSLinus Lüssing 5099a516993fSLinus Lüssing err: 5100a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5101a516993fSLinus Lüssing kfree_skb(skb_chk); 5102a516993fSLinus Lüssing 5103a516993fSLinus Lüssing return NULL; 5104a516993fSLinus Lüssing 51059afd85c9SLinus Lüssing } 51069afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 51079afd85c9SLinus Lüssing 51084497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 51094497b076SBen Hutchings { 5110e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5111e87cc472SJoe Perches skb->dev->name); 51124497b076SBen Hutchings } 51134497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5114bad43ca8SEric Dumazet 5115bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5116bad43ca8SEric Dumazet { 51173d861f66SEric Dumazet if (head_stolen) { 51183d861f66SEric Dumazet skb_release_head_state(skb); 5119bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 51203d861f66SEric Dumazet } else { 5121bad43ca8SEric Dumazet __kfree_skb(skb); 5122bad43ca8SEric Dumazet } 51233d861f66SEric Dumazet } 5124bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5125bad43ca8SEric Dumazet 5126bad43ca8SEric Dumazet /** 5127bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5128bad43ca8SEric Dumazet * @to: prior buffer 5129bad43ca8SEric Dumazet * @from: buffer to add 5130bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5131c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5132bad43ca8SEric Dumazet */ 5133bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5134bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5135bad43ca8SEric Dumazet { 5136c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5137bad43ca8SEric Dumazet int i, delta, len = from->len; 5138bad43ca8SEric Dumazet 5139bad43ca8SEric Dumazet *fragstolen = false; 5140bad43ca8SEric Dumazet 5141bad43ca8SEric Dumazet if (skb_cloned(to)) 5142bad43ca8SEric Dumazet return false; 5143bad43ca8SEric Dumazet 5144bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5145e93a0435SEric Dumazet if (len) 5146bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5147bad43ca8SEric Dumazet *delta_truesize = 0; 5148bad43ca8SEric Dumazet return true; 5149bad43ca8SEric Dumazet } 5150bad43ca8SEric Dumazet 5151c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5152c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5153c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5154bad43ca8SEric Dumazet return false; 51551f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 51561f8b977aSWillem de Bruijn return false; 5157bad43ca8SEric Dumazet 5158bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5159bad43ca8SEric Dumazet struct page *page; 5160bad43ca8SEric Dumazet unsigned int offset; 5161bad43ca8SEric Dumazet 5162c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5163c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5164bad43ca8SEric Dumazet return false; 5165bad43ca8SEric Dumazet 5166bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5167bad43ca8SEric Dumazet return false; 5168bad43ca8SEric Dumazet 5169bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5170bad43ca8SEric Dumazet 5171bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5172bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5173bad43ca8SEric Dumazet 5174c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5175bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5176bad43ca8SEric Dumazet *fragstolen = true; 5177bad43ca8SEric Dumazet } else { 5178c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5179c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5180bad43ca8SEric Dumazet return false; 5181bad43ca8SEric Dumazet 5182f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5183bad43ca8SEric Dumazet } 5184bad43ca8SEric Dumazet 5185bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5186bad43ca8SEric Dumazet 5187c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5188c818fa9eSEric Dumazet from_shinfo->frags, 5189c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5190c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5191bad43ca8SEric Dumazet 5192bad43ca8SEric Dumazet if (!skb_cloned(from)) 5193c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5194bad43ca8SEric Dumazet 51958ea853fdSLi RongQing /* if the skb is not cloned this does nothing 51968ea853fdSLi RongQing * since we set nr_frags to 0. 51978ea853fdSLi RongQing */ 5198c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5199c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5200bad43ca8SEric Dumazet 5201bad43ca8SEric Dumazet to->truesize += delta; 5202bad43ca8SEric Dumazet to->len += len; 5203bad43ca8SEric Dumazet to->data_len += len; 5204bad43ca8SEric Dumazet 5205bad43ca8SEric Dumazet *delta_truesize = delta; 5206bad43ca8SEric Dumazet return true; 5207bad43ca8SEric Dumazet } 5208bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5209621e84d6SNicolas Dichtel 5210621e84d6SNicolas Dichtel /** 52118b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5212621e84d6SNicolas Dichtel * 5213621e84d6SNicolas Dichtel * @skb: buffer to clean 52148b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5215621e84d6SNicolas Dichtel * 52168b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 52178b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 52188b27f277SNicolas Dichtel * operations. 52198b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 52208b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 52218b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5222621e84d6SNicolas Dichtel */ 52238b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5224621e84d6SNicolas Dichtel { 5225621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5226621e84d6SNicolas Dichtel skb->skb_iif = 0; 522760ff7467SWANG Cong skb->ignore_df = 0; 5228621e84d6SNicolas Dichtel skb_dst_drop(skb); 5229174e2381SFlorian Westphal skb_ext_reset(skb); 5230895b5c9fSFlorian Westphal nf_reset_ct(skb); 5231621e84d6SNicolas Dichtel nf_reset_trace(skb); 5232213dd74aSHerbert Xu 52336f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 52346f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5235875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 52366f9a5069SPetr Machata #endif 52376f9a5069SPetr Machata 5238213dd74aSHerbert Xu if (!xnet) 5239213dd74aSHerbert Xu return; 5240213dd74aSHerbert Xu 52412b5ec1a5SYe Yin ipvs_reset(skb); 5242213dd74aSHerbert Xu skb->mark = 0; 5243c47d8c2fSJesus Sanchez-Palencia skb->tstamp = 0; 5244621e84d6SNicolas Dichtel } 5245621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5246de960aa9SFlorian Westphal 5247de960aa9SFlorian Westphal /** 5248de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5249de960aa9SFlorian Westphal * 5250de960aa9SFlorian Westphal * @skb: GSO skb 5251de960aa9SFlorian Westphal * 5252de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5253de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5254de960aa9SFlorian Westphal * 5255de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5256de960aa9SFlorian Westphal */ 5257a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5258de960aa9SFlorian Westphal { 5259de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5260f993bc25SFlorian Westphal unsigned int thlen = 0; 5261f993bc25SFlorian Westphal 5262f993bc25SFlorian Westphal if (skb->encapsulation) { 5263f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5264f993bc25SFlorian Westphal skb_transport_header(skb); 5265de960aa9SFlorian Westphal 5266de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5267f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5268f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5269f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 52701dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 527190017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5272ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5273ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5274f993bc25SFlorian Westphal } 52756d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 52766d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 52776d39d589SFlorian Westphal * accounted for. 52786d39d589SFlorian Westphal */ 5279f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5280de960aa9SFlorian Westphal } 5281a4a77718SDaniel Axtens 5282a4a77718SDaniel Axtens /** 5283a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5284a4a77718SDaniel Axtens * 5285a4a77718SDaniel Axtens * @skb: GSO skb 5286a4a77718SDaniel Axtens * 5287a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5288a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5289a4a77718SDaniel Axtens * 5290a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5291a4a77718SDaniel Axtens */ 5292a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5293a4a77718SDaniel Axtens { 5294a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5295a4a77718SDaniel Axtens skb_network_header(skb); 5296a4a77718SDaniel Axtens 5297a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5298a4a77718SDaniel Axtens } 5299a4a77718SDaniel Axtens 5300a4a77718SDaniel Axtens /** 5301a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5302a4a77718SDaniel Axtens * 5303a4a77718SDaniel Axtens * @skb: GSO skb 5304a4a77718SDaniel Axtens * 5305a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5306a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5307a4a77718SDaniel Axtens * headers (TCP/UDP). 5308a4a77718SDaniel Axtens */ 5309a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5310a4a77718SDaniel Axtens { 5311a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5312a4a77718SDaniel Axtens 5313a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5314a4a77718SDaniel Axtens } 53150d5501c1SVlad Yasevich 5316ae7ef81eSMarcelo Ricardo Leitner /** 53172b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 53182b16f048SDaniel Axtens * 53192b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 53202b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 53212b16f048SDaniel Axtens * 53222b16f048SDaniel Axtens * We might want to check: 53232b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 53242b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 53252b16f048SDaniel Axtens * 53262b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 53272b16f048SDaniel Axtens * 532849682bfaSMathieu Malaterre * @skb: GSO skb 532949682bfaSMathieu Malaterre * 53302b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 53312b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 53322b16f048SDaniel Axtens * 53332b16f048SDaniel Axtens * @max_len: The maximum permissible length. 53342b16f048SDaniel Axtens * 53352b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 53362b16f048SDaniel Axtens */ 53372b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 53382b16f048SDaniel Axtens unsigned int seg_len, 53392b16f048SDaniel Axtens unsigned int max_len) { 53402b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 53412b16f048SDaniel Axtens const struct sk_buff *iter; 53422b16f048SDaniel Axtens 53432b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 53442b16f048SDaniel Axtens return seg_len <= max_len; 53452b16f048SDaniel Axtens 53462b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 53472b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 53482b16f048SDaniel Axtens 53492b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 53502b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 53512b16f048SDaniel Axtens return false; 53522b16f048SDaniel Axtens } 53532b16f048SDaniel Axtens 53542b16f048SDaniel Axtens return true; 53552b16f048SDaniel Axtens } 53562b16f048SDaniel Axtens 53572b16f048SDaniel Axtens /** 5358779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5359ae7ef81eSMarcelo Ricardo Leitner * 5360ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 536176f21b99SDavid S. Miller * @mtu: MTU to validate against 5362ae7ef81eSMarcelo Ricardo Leitner * 5363779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5364779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5365779b7931SDaniel Axtens * payload. 5366ae7ef81eSMarcelo Ricardo Leitner */ 5367779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5368ae7ef81eSMarcelo Ricardo Leitner { 53692b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5370ae7ef81eSMarcelo Ricardo Leitner } 5371779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5372ae7ef81eSMarcelo Ricardo Leitner 53732b16f048SDaniel Axtens /** 53742b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 53752b16f048SDaniel Axtens * 53762b16f048SDaniel Axtens * @skb: GSO skb 53772b16f048SDaniel Axtens * @len: length to validate against 53782b16f048SDaniel Axtens * 53792b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 53802b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 53812b16f048SDaniel Axtens */ 53822b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 53832b16f048SDaniel Axtens { 53842b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 53852b16f048SDaniel Axtens } 53862b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 53872b16f048SDaniel Axtens 53880d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 53890d5501c1SVlad Yasevich { 5390d85e8be2SYuya Kusakabe int mac_len, meta_len; 5391d85e8be2SYuya Kusakabe void *meta; 53924bbb3e0eSToshiaki Makita 53930d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 53940d5501c1SVlad Yasevich kfree_skb(skb); 53950d5501c1SVlad Yasevich return NULL; 53960d5501c1SVlad Yasevich } 53970d5501c1SVlad Yasevich 53984bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5399ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 54004bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 54014bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5402ae474573SToshiaki Makita } 5403d85e8be2SYuya Kusakabe 5404d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5405d85e8be2SYuya Kusakabe if (meta_len) { 5406d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5407d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5408d85e8be2SYuya Kusakabe } 5409d85e8be2SYuya Kusakabe 54100d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 54110d5501c1SVlad Yasevich return skb; 54120d5501c1SVlad Yasevich } 54130d5501c1SVlad Yasevich 54140d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 54150d5501c1SVlad Yasevich { 54160d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 54170d5501c1SVlad Yasevich u16 vlan_tci; 54180d5501c1SVlad Yasevich 5419df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 54200d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 54210d5501c1SVlad Yasevich return skb; 54220d5501c1SVlad Yasevich } 54230d5501c1SVlad Yasevich 54240d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 54250d5501c1SVlad Yasevich if (unlikely(!skb)) 54260d5501c1SVlad Yasevich goto err_free; 542755eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 542855eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 54290d5501c1SVlad Yasevich goto err_free; 54300d5501c1SVlad Yasevich 54310d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 54320d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 54330d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 54340d5501c1SVlad Yasevich 54350d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 54360d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 54370d5501c1SVlad Yasevich 54380d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 54390d5501c1SVlad Yasevich if (unlikely(!skb)) 54400d5501c1SVlad Yasevich goto err_free; 54410d5501c1SVlad Yasevich 54420d5501c1SVlad Yasevich skb_reset_network_header(skb); 54438be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 54440d5501c1SVlad Yasevich skb_reset_transport_header(skb); 54450d5501c1SVlad Yasevich skb_reset_mac_len(skb); 54460d5501c1SVlad Yasevich 54470d5501c1SVlad Yasevich return skb; 54480d5501c1SVlad Yasevich 54490d5501c1SVlad Yasevich err_free: 54500d5501c1SVlad Yasevich kfree_skb(skb); 54510d5501c1SVlad Yasevich return NULL; 54520d5501c1SVlad Yasevich } 54530d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 54542e4e4410SEric Dumazet 5455e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 5456e2195121SJiri Pirko { 5457e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5458e2195121SJiri Pirko return -ENOMEM; 5459e2195121SJiri Pirko 5460e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5461e2195121SJiri Pirko return 0; 5462e2195121SJiri Pirko 5463e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5464e2195121SJiri Pirko } 5465e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5466e2195121SJiri Pirko 5467bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5468bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5469bfca4c52SShmulik Ladkani */ 5470bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 547193515d53SJiri Pirko { 547293515d53SJiri Pirko struct vlan_hdr *vhdr; 5473b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 547493515d53SJiri Pirko int err; 547593515d53SJiri Pirko 5476b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5477b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5478b6a79208SShmulik Ladkani offset)) { 5479b6a79208SShmulik Ladkani return -EINVAL; 5480b6a79208SShmulik Ladkani } 5481b6a79208SShmulik Ladkani 548293515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 548393515d53SJiri Pirko if (unlikely(err)) 5484b6a79208SShmulik Ladkani return err; 548593515d53SJiri Pirko 548693515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 548793515d53SJiri Pirko 548893515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 548993515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 549093515d53SJiri Pirko 549193515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 549293515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 549393515d53SJiri Pirko 549493515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 549593515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 549693515d53SJiri Pirko 549793515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 549893515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 549993515d53SJiri Pirko 550093515d53SJiri Pirko skb_reset_mac_len(skb); 550193515d53SJiri Pirko 550293515d53SJiri Pirko return err; 550393515d53SJiri Pirko } 5504bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 550593515d53SJiri Pirko 5506b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5507b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5508b6a79208SShmulik Ladkani */ 550993515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 551093515d53SJiri Pirko { 551193515d53SJiri Pirko u16 vlan_tci; 551293515d53SJiri Pirko __be16 vlan_proto; 551393515d53SJiri Pirko int err; 551493515d53SJiri Pirko 5515df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5516b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 551793515d53SJiri Pirko } else { 5518ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 551993515d53SJiri Pirko return 0; 552093515d53SJiri Pirko 552193515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 552293515d53SJiri Pirko if (err) 552393515d53SJiri Pirko return err; 552493515d53SJiri Pirko } 552593515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5526ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 552793515d53SJiri Pirko return 0; 552893515d53SJiri Pirko 552993515d53SJiri Pirko vlan_proto = skb->protocol; 553093515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 553193515d53SJiri Pirko if (unlikely(err)) 553293515d53SJiri Pirko return err; 553393515d53SJiri Pirko 553493515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 553593515d53SJiri Pirko return 0; 553693515d53SJiri Pirko } 553793515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 553893515d53SJiri Pirko 5539b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5540b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5541b6a79208SShmulik Ladkani */ 554293515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 554393515d53SJiri Pirko { 5544df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5545b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 554693515d53SJiri Pirko int err; 554793515d53SJiri Pirko 5548b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5549b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5550b6a79208SShmulik Ladkani offset)) { 5551b6a79208SShmulik Ladkani return -EINVAL; 5552b6a79208SShmulik Ladkani } 5553b6a79208SShmulik Ladkani 555493515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5555df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5556b6a79208SShmulik Ladkani if (err) 555793515d53SJiri Pirko return err; 55589241e2dfSDaniel Borkmann 555993515d53SJiri Pirko skb->protocol = skb->vlan_proto; 556093515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 556193515d53SJiri Pirko 55626b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 556393515d53SJiri Pirko } 556493515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 556593515d53SJiri Pirko return 0; 556693515d53SJiri Pirko } 556793515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 556893515d53SJiri Pirko 556919fbcb36SGuillaume Nault /** 557019fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 557119fbcb36SGuillaume Nault * 557219fbcb36SGuillaume Nault * @skb: Socket buffer to modify 557319fbcb36SGuillaume Nault * 557419fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 557519fbcb36SGuillaume Nault * 557619fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 557719fbcb36SGuillaume Nault * present. 557819fbcb36SGuillaume Nault * 557919fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 558019fbcb36SGuillaume Nault */ 558119fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 558219fbcb36SGuillaume Nault { 558319fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 558419fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 558519fbcb36SGuillaume Nault return -EPROTO; 558619fbcb36SGuillaume Nault 558719fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 558819fbcb36SGuillaume Nault skb_reset_mac_header(skb); 558919fbcb36SGuillaume Nault skb_reset_mac_len(skb); 559019fbcb36SGuillaume Nault 559119fbcb36SGuillaume Nault return 0; 559219fbcb36SGuillaume Nault } 559319fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 559419fbcb36SGuillaume Nault 559519fbcb36SGuillaume Nault /** 559619fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 559719fbcb36SGuillaume Nault * 559819fbcb36SGuillaume Nault * @skb: Socket buffer to modify 559919fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 560019fbcb36SGuillaume Nault * @src: Source MAC address of the new header 560119fbcb36SGuillaume Nault * 560219fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 560319fbcb36SGuillaume Nault * 560419fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 560519fbcb36SGuillaume Nault * 560619fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 560719fbcb36SGuillaume Nault */ 560819fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 560919fbcb36SGuillaume Nault const unsigned char *src) 561019fbcb36SGuillaume Nault { 561119fbcb36SGuillaume Nault struct ethhdr *eth; 561219fbcb36SGuillaume Nault int err; 561319fbcb36SGuillaume Nault 561419fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 561519fbcb36SGuillaume Nault return -EPROTO; 561619fbcb36SGuillaume Nault 561719fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 561819fbcb36SGuillaume Nault if (err < 0) 561919fbcb36SGuillaume Nault return err; 562019fbcb36SGuillaume Nault 562119fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 562219fbcb36SGuillaume Nault skb_reset_mac_header(skb); 562319fbcb36SGuillaume Nault skb_reset_mac_len(skb); 562419fbcb36SGuillaume Nault 562519fbcb36SGuillaume Nault eth = eth_hdr(skb); 562619fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 562719fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 562819fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 562919fbcb36SGuillaume Nault 563019fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 563119fbcb36SGuillaume Nault 563219fbcb36SGuillaume Nault return 0; 563319fbcb36SGuillaume Nault } 563419fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 563519fbcb36SGuillaume Nault 56368822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 56378822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 56388822e270SJohn Hurley __be16 ethertype) 56398822e270SJohn Hurley { 56408822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 56418822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 56428822e270SJohn Hurley 56438822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 56448822e270SJohn Hurley } 56458822e270SJohn Hurley 56468822e270SJohn Hurley hdr->h_proto = ethertype; 56478822e270SJohn Hurley } 56488822e270SJohn Hurley 56498822e270SJohn Hurley /** 5650e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5651e7dbfed1SMartin Varghese * the packet 56528822e270SJohn Hurley * 56538822e270SJohn Hurley * @skb: buffer 56548822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 56558822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5656fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 5657e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5658e7dbfed1SMartin Varghese * ethernet 56598822e270SJohn Hurley * 56608822e270SJohn Hurley * Expects skb->data at mac header. 56618822e270SJohn Hurley * 56628822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 56638822e270SJohn Hurley */ 5664fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5665d04ac224SMartin Varghese int mac_len, bool ethernet) 56668822e270SJohn Hurley { 56678822e270SJohn Hurley struct mpls_shim_hdr *lse; 56688822e270SJohn Hurley int err; 56698822e270SJohn Hurley 56708822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 56718822e270SJohn Hurley return -EINVAL; 56728822e270SJohn Hurley 56738822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 56748822e270SJohn Hurley if (skb->encapsulation) 56758822e270SJohn Hurley return -EINVAL; 56768822e270SJohn Hurley 56778822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 56788822e270SJohn Hurley if (unlikely(err)) 56798822e270SJohn Hurley return err; 56808822e270SJohn Hurley 56818822e270SJohn Hurley if (!skb->inner_protocol) { 5682e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 56838822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 56848822e270SJohn Hurley } 56858822e270SJohn Hurley 56868822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 56878822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5688fa4e0f88SDavide Caratti mac_len); 56898822e270SJohn Hurley skb_reset_mac_header(skb); 5690fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5691e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 56928822e270SJohn Hurley 56938822e270SJohn Hurley lse = mpls_hdr(skb); 56948822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 56958822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 56968822e270SJohn Hurley 56974296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 56988822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 56998822e270SJohn Hurley skb->protocol = mpls_proto; 57008822e270SJohn Hurley 57018822e270SJohn Hurley return 0; 57028822e270SJohn Hurley } 57038822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 57048822e270SJohn Hurley 57052e4e4410SEric Dumazet /** 5706ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 5707ed246ceeSJohn Hurley * 5708ed246ceeSJohn Hurley * @skb: buffer 5709ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 5710fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 571176f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 5712ed246ceeSJohn Hurley * 5713ed246ceeSJohn Hurley * Expects skb->data at mac header. 5714ed246ceeSJohn Hurley * 5715ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 5716ed246ceeSJohn Hurley */ 5717040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 5718040b5cfbSMartin Varghese bool ethernet) 5719ed246ceeSJohn Hurley { 5720ed246ceeSJohn Hurley int err; 5721ed246ceeSJohn Hurley 5722ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5723dedc5a08SDavide Caratti return 0; 5724ed246ceeSJohn Hurley 5725fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 5726ed246ceeSJohn Hurley if (unlikely(err)) 5727ed246ceeSJohn Hurley return err; 5728ed246ceeSJohn Hurley 5729ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 5730ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 5731fa4e0f88SDavide Caratti mac_len); 5732ed246ceeSJohn Hurley 5733ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 5734ed246ceeSJohn Hurley skb_reset_mac_header(skb); 5735fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5736ed246ceeSJohn Hurley 57374296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 5738ed246ceeSJohn Hurley struct ethhdr *hdr; 5739ed246ceeSJohn Hurley 5740ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 5741ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 5742ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 5743ed246ceeSJohn Hurley } 5744ed246ceeSJohn Hurley skb->protocol = next_proto; 5745ed246ceeSJohn Hurley 5746ed246ceeSJohn Hurley return 0; 5747ed246ceeSJohn Hurley } 5748ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 5749ed246ceeSJohn Hurley 5750ed246ceeSJohn Hurley /** 5751d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 5752d27cf5c5SJohn Hurley * 5753d27cf5c5SJohn Hurley * @skb: buffer 5754d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 5755d27cf5c5SJohn Hurley * 5756d27cf5c5SJohn Hurley * Expects skb->data at mac header. 5757d27cf5c5SJohn Hurley * 5758d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 5759d27cf5c5SJohn Hurley */ 5760d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 5761d27cf5c5SJohn Hurley { 5762d27cf5c5SJohn Hurley int err; 5763d27cf5c5SJohn Hurley 5764d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5765d27cf5c5SJohn Hurley return -EINVAL; 5766d27cf5c5SJohn Hurley 5767d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 5768d27cf5c5SJohn Hurley if (unlikely(err)) 5769d27cf5c5SJohn Hurley return err; 5770d27cf5c5SJohn Hurley 5771d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 5772d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 5773d27cf5c5SJohn Hurley 5774d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5775d27cf5c5SJohn Hurley } 5776d27cf5c5SJohn Hurley 5777d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 5778d27cf5c5SJohn Hurley 5779d27cf5c5SJohn Hurley return 0; 5780d27cf5c5SJohn Hurley } 5781d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 5782d27cf5c5SJohn Hurley 5783d27cf5c5SJohn Hurley /** 57842a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 57852a2ea508SJohn Hurley * 57862a2ea508SJohn Hurley * @skb: buffer 57872a2ea508SJohn Hurley * 57882a2ea508SJohn Hurley * Expects skb->data at mac header. 57892a2ea508SJohn Hurley * 57902a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 57912a2ea508SJohn Hurley */ 57922a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 57932a2ea508SJohn Hurley { 57942a2ea508SJohn Hurley u32 lse; 57952a2ea508SJohn Hurley u8 ttl; 57962a2ea508SJohn Hurley 57972a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 57982a2ea508SJohn Hurley return -EINVAL; 57992a2ea508SJohn Hurley 580013de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 580113de4ed9SDavide Caratti return -ENOMEM; 580213de4ed9SDavide Caratti 58032a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 58042a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 58052a2ea508SJohn Hurley if (!--ttl) 58062a2ea508SJohn Hurley return -EINVAL; 58072a2ea508SJohn Hurley 58082a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 58092a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 58102a2ea508SJohn Hurley 58112a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 58122a2ea508SJohn Hurley } 58132a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 58142a2ea508SJohn Hurley 58152a2ea508SJohn Hurley /** 58162e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 58172e4e4410SEric Dumazet * 5818de3f0d0eSMasanari Iida * @header_len: size of linear part 5819de3f0d0eSMasanari Iida * @data_len: needed length in frags 5820de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5821de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5822de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 58232e4e4410SEric Dumazet * 58242e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 58252e4e4410SEric Dumazet */ 58262e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 58272e4e4410SEric Dumazet unsigned long data_len, 58282e4e4410SEric Dumazet int max_page_order, 58292e4e4410SEric Dumazet int *errcode, 58302e4e4410SEric Dumazet gfp_t gfp_mask) 58312e4e4410SEric Dumazet { 58322e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 58332e4e4410SEric Dumazet unsigned long chunk; 58342e4e4410SEric Dumazet struct sk_buff *skb; 58352e4e4410SEric Dumazet struct page *page; 58362e4e4410SEric Dumazet int i; 58372e4e4410SEric Dumazet 58382e4e4410SEric Dumazet *errcode = -EMSGSIZE; 58392e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 58402e4e4410SEric Dumazet * high order pages... 58412e4e4410SEric Dumazet */ 58422e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 58432e4e4410SEric Dumazet return NULL; 58442e4e4410SEric Dumazet 58452e4e4410SEric Dumazet *errcode = -ENOBUFS; 5846f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 58472e4e4410SEric Dumazet if (!skb) 58482e4e4410SEric Dumazet return NULL; 58492e4e4410SEric Dumazet 58502e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 58512e4e4410SEric Dumazet 58522e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 58532e4e4410SEric Dumazet int order = max_page_order; 58542e4e4410SEric Dumazet 58552e4e4410SEric Dumazet while (order) { 58562e4e4410SEric Dumazet if (npages >= 1 << order) { 5857d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 58582e4e4410SEric Dumazet __GFP_COMP | 5859d14b56f5SMichal Hocko __GFP_NOWARN, 58602e4e4410SEric Dumazet order); 58612e4e4410SEric Dumazet if (page) 58622e4e4410SEric Dumazet goto fill_page; 58632e4e4410SEric Dumazet /* Do not retry other high order allocations */ 58642e4e4410SEric Dumazet order = 1; 58652e4e4410SEric Dumazet max_page_order = 0; 58662e4e4410SEric Dumazet } 58672e4e4410SEric Dumazet order--; 58682e4e4410SEric Dumazet } 58692e4e4410SEric Dumazet page = alloc_page(gfp_mask); 58702e4e4410SEric Dumazet if (!page) 58712e4e4410SEric Dumazet goto failure; 58722e4e4410SEric Dumazet fill_page: 58732e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 58742e4e4410SEric Dumazet PAGE_SIZE << order); 58752e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 58762e4e4410SEric Dumazet data_len -= chunk; 58772e4e4410SEric Dumazet npages -= 1 << order; 58782e4e4410SEric Dumazet } 58792e4e4410SEric Dumazet return skb; 58802e4e4410SEric Dumazet 58812e4e4410SEric Dumazet failure: 58822e4e4410SEric Dumazet kfree_skb(skb); 58832e4e4410SEric Dumazet return NULL; 58842e4e4410SEric Dumazet } 58852e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 58866fa01ccdSSowmini Varadhan 58876fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 58886fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 58896fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 58906fa01ccdSSowmini Varadhan { 58916fa01ccdSSowmini Varadhan int i; 58926fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 58936fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 58946fa01ccdSSowmini Varadhan u8 *data; 58956fa01ccdSSowmini Varadhan 58966fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 58976fa01ccdSSowmini Varadhan 58986fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 58996fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 59006fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 59016fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 59026fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 59036fa01ccdSSowmini Varadhan if (!data) 59046fa01ccdSSowmini Varadhan return -ENOMEM; 59056fa01ccdSSowmini Varadhan 59066fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 59076fa01ccdSSowmini Varadhan 59086fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 59096fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 59106fa01ccdSSowmini Varadhan skb->len -= off; 59116fa01ccdSSowmini Varadhan 59126fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 59136fa01ccdSSowmini Varadhan skb_shinfo(skb), 59146fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 59156fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 59166fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 59176fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 59186fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 59196fa01ccdSSowmini Varadhan kfree(data); 59206fa01ccdSSowmini Varadhan return -ENOMEM; 59216fa01ccdSSowmini Varadhan } 59226fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 59236fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 59246fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 59256fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 59266fa01ccdSSowmini Varadhan skb_release_data(skb); 59276fa01ccdSSowmini Varadhan } else { 59286fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 59296fa01ccdSSowmini Varadhan * relocate values 59306fa01ccdSSowmini Varadhan */ 59316fa01ccdSSowmini Varadhan skb_free_head(skb); 59326fa01ccdSSowmini Varadhan } 59336fa01ccdSSowmini Varadhan 59346fa01ccdSSowmini Varadhan skb->head = data; 59356fa01ccdSSowmini Varadhan skb->data = data; 59366fa01ccdSSowmini Varadhan skb->head_frag = 0; 59376fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 59386fa01ccdSSowmini Varadhan skb->end = size; 59396fa01ccdSSowmini Varadhan #else 59406fa01ccdSSowmini Varadhan skb->end = skb->head + size; 59416fa01ccdSSowmini Varadhan #endif 59426fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 59436fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 59446fa01ccdSSowmini Varadhan skb->cloned = 0; 59456fa01ccdSSowmini Varadhan skb->hdr_len = 0; 59466fa01ccdSSowmini Varadhan skb->nohdr = 0; 59476fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 59486fa01ccdSSowmini Varadhan 59496fa01ccdSSowmini Varadhan return 0; 59506fa01ccdSSowmini Varadhan } 59516fa01ccdSSowmini Varadhan 59526fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 59536fa01ccdSSowmini Varadhan 59546fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 59556fa01ccdSSowmini Varadhan * pskb_carve() 59566fa01ccdSSowmini Varadhan */ 59576fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 59586fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 59596fa01ccdSSowmini Varadhan gfp_t gfp_mask) 59606fa01ccdSSowmini Varadhan { 59616fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 59626fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 59636fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 59646fa01ccdSSowmini Varadhan 59656fa01ccdSSowmini Varadhan do { 59666fa01ccdSSowmini Varadhan if (!list) { 59676fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 59686fa01ccdSSowmini Varadhan return -EFAULT; 59696fa01ccdSSowmini Varadhan } 59706fa01ccdSSowmini Varadhan if (list->len <= eat) { 59716fa01ccdSSowmini Varadhan /* Eaten as whole. */ 59726fa01ccdSSowmini Varadhan eat -= list->len; 59736fa01ccdSSowmini Varadhan list = list->next; 59746fa01ccdSSowmini Varadhan insp = list; 59756fa01ccdSSowmini Varadhan } else { 59766fa01ccdSSowmini Varadhan /* Eaten partially. */ 59776fa01ccdSSowmini Varadhan if (skb_shared(list)) { 59786fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 59796fa01ccdSSowmini Varadhan if (!clone) 59806fa01ccdSSowmini Varadhan return -ENOMEM; 59816fa01ccdSSowmini Varadhan insp = list->next; 59826fa01ccdSSowmini Varadhan list = clone; 59836fa01ccdSSowmini Varadhan } else { 59846fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 59856fa01ccdSSowmini Varadhan insp = list; 59866fa01ccdSSowmini Varadhan } 59876fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 59886fa01ccdSSowmini Varadhan kfree_skb(clone); 59896fa01ccdSSowmini Varadhan return -ENOMEM; 59906fa01ccdSSowmini Varadhan } 59916fa01ccdSSowmini Varadhan break; 59926fa01ccdSSowmini Varadhan } 59936fa01ccdSSowmini Varadhan } while (eat); 59946fa01ccdSSowmini Varadhan 59956fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 59966fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 59976fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 59986fa01ccdSSowmini Varadhan kfree_skb(list); 59996fa01ccdSSowmini Varadhan } 60006fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 60016fa01ccdSSowmini Varadhan if (clone) { 60026fa01ccdSSowmini Varadhan clone->next = list; 60036fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 60046fa01ccdSSowmini Varadhan } 60056fa01ccdSSowmini Varadhan return 0; 60066fa01ccdSSowmini Varadhan } 60076fa01ccdSSowmini Varadhan 60086fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 60096fa01ccdSSowmini Varadhan * non-linear part of skb 60106fa01ccdSSowmini Varadhan */ 60116fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 60126fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 60136fa01ccdSSowmini Varadhan { 60146fa01ccdSSowmini Varadhan int i, k = 0; 60156fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 60166fa01ccdSSowmini Varadhan u8 *data; 60176fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 60186fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 60196fa01ccdSSowmini Varadhan 60206fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 60216fa01ccdSSowmini Varadhan 60226fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 60236fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 60246fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 60256fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 60266fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 60276fa01ccdSSowmini Varadhan if (!data) 60286fa01ccdSSowmini Varadhan return -ENOMEM; 60296fa01ccdSSowmini Varadhan 60306fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 60316fa01ccdSSowmini Varadhan 60326fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6033e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 60346fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 60356fa01ccdSSowmini Varadhan kfree(data); 60366fa01ccdSSowmini Varadhan return -ENOMEM; 60376fa01ccdSSowmini Varadhan } 60386fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 60396fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 60406fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 60416fa01ccdSSowmini Varadhan 60426fa01ccdSSowmini Varadhan if (pos + fsize > off) { 60436fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 60446fa01ccdSSowmini Varadhan 60456fa01ccdSSowmini Varadhan if (pos < off) { 60466fa01ccdSSowmini Varadhan /* Split frag. 60476fa01ccdSSowmini Varadhan * We have two variants in this case: 60486fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 60496fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 60506fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 60516fa01ccdSSowmini Varadhan * where splitting is expensive. 60526fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 60536fa01ccdSSowmini Varadhan */ 6054b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 60556fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 60566fa01ccdSSowmini Varadhan } 60576fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 60586fa01ccdSSowmini Varadhan k++; 60596fa01ccdSSowmini Varadhan } 60606fa01ccdSSowmini Varadhan pos += fsize; 60616fa01ccdSSowmini Varadhan } 60626fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 60636fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 60646fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 60656fa01ccdSSowmini Varadhan 60666fa01ccdSSowmini Varadhan /* split line is in frag list */ 6067eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6068eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6069eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6070eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6071eabe8618SMiaohe Lin kfree(data); 6072eabe8618SMiaohe Lin return -ENOMEM; 60736fa01ccdSSowmini Varadhan } 60746fa01ccdSSowmini Varadhan skb_release_data(skb); 60756fa01ccdSSowmini Varadhan 60766fa01ccdSSowmini Varadhan skb->head = data; 60776fa01ccdSSowmini Varadhan skb->head_frag = 0; 60786fa01ccdSSowmini Varadhan skb->data = data; 60796fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 60806fa01ccdSSowmini Varadhan skb->end = size; 60816fa01ccdSSowmini Varadhan #else 60826fa01ccdSSowmini Varadhan skb->end = skb->head + size; 60836fa01ccdSSowmini Varadhan #endif 60846fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 60856fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 60866fa01ccdSSowmini Varadhan skb->cloned = 0; 60876fa01ccdSSowmini Varadhan skb->hdr_len = 0; 60886fa01ccdSSowmini Varadhan skb->nohdr = 0; 60896fa01ccdSSowmini Varadhan skb->len -= off; 60906fa01ccdSSowmini Varadhan skb->data_len = skb->len; 60916fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 60926fa01ccdSSowmini Varadhan return 0; 60936fa01ccdSSowmini Varadhan } 60946fa01ccdSSowmini Varadhan 60956fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 60966fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 60976fa01ccdSSowmini Varadhan { 60986fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 60996fa01ccdSSowmini Varadhan 61006fa01ccdSSowmini Varadhan if (len < headlen) 61016fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 61026fa01ccdSSowmini Varadhan else 61036fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 61046fa01ccdSSowmini Varadhan } 61056fa01ccdSSowmini Varadhan 61066fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 61076fa01ccdSSowmini Varadhan * a new skb 61086fa01ccdSSowmini Varadhan */ 61096fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 61106fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 61116fa01ccdSSowmini Varadhan { 61126fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 61136fa01ccdSSowmini Varadhan 61146fa01ccdSSowmini Varadhan if (!clone) 61156fa01ccdSSowmini Varadhan return NULL; 61166fa01ccdSSowmini Varadhan 61176fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 61186fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 61196fa01ccdSSowmini Varadhan kfree_skb(clone); 61206fa01ccdSSowmini Varadhan return NULL; 61216fa01ccdSSowmini Varadhan } 61226fa01ccdSSowmini Varadhan return clone; 61236fa01ccdSSowmini Varadhan } 61246fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6125c8c8b127SEric Dumazet 6126c8c8b127SEric Dumazet /** 6127c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6128c8c8b127SEric Dumazet * @skb: buffer 6129c8c8b127SEric Dumazet * 6130c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6131c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6132c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6133c8c8b127SEric Dumazet * truesize. 6134c8c8b127SEric Dumazet * Notes: 6135c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6136c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6137c8c8b127SEric Dumazet */ 6138c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6139c8c8b127SEric Dumazet { 61403174fed9SEric Dumazet if (skb->data_len) { 61413174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6142c8c8b127SEric Dumazet skb_cloned(skb)) 6143c8c8b127SEric Dumazet return; 6144c8c8b127SEric Dumazet 6145c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6146c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 61473174fed9SEric Dumazet } 61483174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 61493174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 61503174fed9SEric Dumazet * their truesize. 61513174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 61523174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 61533174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6154c8c8b127SEric Dumazet */ 6155c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6156c8c8b127SEric Dumazet } 6157df5042f4SFlorian Westphal 6158df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6159df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6160df5042f4SFlorian Westphal { 6161df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6162df5042f4SFlorian Westphal } 6163df5042f4SFlorian Westphal 61648b69a803SPaolo Abeni /** 61658b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 61668b69a803SPaolo Abeni * 61674930f483SFlorian Westphal * @flags: See kmalloc(). 61684930f483SFlorian Westphal * 61698b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 61708b69a803SPaolo Abeni * skb via __skb_ext_set(). 61718b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 61728b69a803SPaolo Abeni */ 61734930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6174df5042f4SFlorian Westphal { 61754930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6176df5042f4SFlorian Westphal 6177df5042f4SFlorian Westphal if (new) { 6178df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6179df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6180df5042f4SFlorian Westphal } 6181df5042f4SFlorian Westphal 6182df5042f4SFlorian Westphal return new; 6183df5042f4SFlorian Westphal } 6184df5042f4SFlorian Westphal 61854165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 61864165079bSFlorian Westphal unsigned int old_active) 6187df5042f4SFlorian Westphal { 6188df5042f4SFlorian Westphal struct skb_ext *new; 6189df5042f4SFlorian Westphal 6190df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6191df5042f4SFlorian Westphal return old; 6192df5042f4SFlorian Westphal 6193df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6194df5042f4SFlorian Westphal if (!new) 6195df5042f4SFlorian Westphal return NULL; 6196df5042f4SFlorian Westphal 6197df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6198df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6199df5042f4SFlorian Westphal 62004165079bSFlorian Westphal #ifdef CONFIG_XFRM 62014165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 62024165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 62034165079bSFlorian Westphal unsigned int i; 62044165079bSFlorian Westphal 62054165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 62064165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 62074165079bSFlorian Westphal } 62084165079bSFlorian Westphal #endif 6209df5042f4SFlorian Westphal __skb_ext_put(old); 6210df5042f4SFlorian Westphal return new; 6211df5042f4SFlorian Westphal } 6212df5042f4SFlorian Westphal 6213df5042f4SFlorian Westphal /** 62148b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 62158b69a803SPaolo Abeni * @skb: buffer 62168b69a803SPaolo Abeni * @id: extension id 62178b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 62188b69a803SPaolo Abeni * 62198b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 62208b69a803SPaolo Abeni * 62218b69a803SPaolo Abeni * Returns the pointer to the extension. 62228b69a803SPaolo Abeni */ 62238b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 62248b69a803SPaolo Abeni struct skb_ext *ext) 62258b69a803SPaolo Abeni { 62268b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 62278b69a803SPaolo Abeni 62288b69a803SPaolo Abeni skb_ext_put(skb); 62298b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 62308b69a803SPaolo Abeni ext->chunks = newlen; 62318b69a803SPaolo Abeni ext->offset[id] = newoff; 62328b69a803SPaolo Abeni skb->extensions = ext; 62338b69a803SPaolo Abeni skb->active_extensions = 1 << id; 62348b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 62358b69a803SPaolo Abeni } 62368b69a803SPaolo Abeni 62378b69a803SPaolo Abeni /** 6238df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6239df5042f4SFlorian Westphal * @skb: buffer 6240df5042f4SFlorian Westphal * @id: extension to allocate space for 6241df5042f4SFlorian Westphal * 6242df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6243df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6244df5042f4SFlorian Westphal * is returned. 6245df5042f4SFlorian Westphal * 6246df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6247df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6248df5042f4SFlorian Westphal * 6249df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6250df5042f4SFlorian Westphal */ 6251df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6252df5042f4SFlorian Westphal { 6253df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6254df5042f4SFlorian Westphal unsigned int newlen, newoff; 6255df5042f4SFlorian Westphal 6256df5042f4SFlorian Westphal if (skb->active_extensions) { 6257df5042f4SFlorian Westphal old = skb->extensions; 6258df5042f4SFlorian Westphal 62594165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6260df5042f4SFlorian Westphal if (!new) 6261df5042f4SFlorian Westphal return NULL; 6262df5042f4SFlorian Westphal 6263682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6264df5042f4SFlorian Westphal goto set_active; 6265df5042f4SFlorian Westphal 6266e94e50bdSPaolo Abeni newoff = new->chunks; 6267df5042f4SFlorian Westphal } else { 6268df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6269df5042f4SFlorian Westphal 62704930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6271df5042f4SFlorian Westphal if (!new) 6272df5042f4SFlorian Westphal return NULL; 6273df5042f4SFlorian Westphal } 6274df5042f4SFlorian Westphal 6275df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6276df5042f4SFlorian Westphal new->chunks = newlen; 6277df5042f4SFlorian Westphal new->offset[id] = newoff; 6278df5042f4SFlorian Westphal set_active: 6279682ec859SPaolo Abeni skb->extensions = new; 6280df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6281df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6282df5042f4SFlorian Westphal } 6283df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6284df5042f4SFlorian Westphal 62854165079bSFlorian Westphal #ifdef CONFIG_XFRM 62864165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 62874165079bSFlorian Westphal { 62884165079bSFlorian Westphal unsigned int i; 62894165079bSFlorian Westphal 62904165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 62914165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 62924165079bSFlorian Westphal } 62934165079bSFlorian Westphal #endif 62944165079bSFlorian Westphal 6295df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6296df5042f4SFlorian Westphal { 6297df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6298df5042f4SFlorian Westphal 6299df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6300df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6301df5042f4SFlorian Westphal skb->extensions = NULL; 6302df5042f4SFlorian Westphal __skb_ext_put(ext); 63034165079bSFlorian Westphal #ifdef CONFIG_XFRM 63044165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 63054165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 63064165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 63074165079bSFlorian Westphal 63084165079bSFlorian Westphal skb_ext_put_sp(sp); 63094165079bSFlorian Westphal sp->len = 0; 63104165079bSFlorian Westphal #endif 6311df5042f4SFlorian Westphal } 6312df5042f4SFlorian Westphal } 6313df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6314df5042f4SFlorian Westphal 6315df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6316df5042f4SFlorian Westphal { 6317df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6318df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6319df5042f4SFlorian Westphal */ 6320df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6321df5042f4SFlorian Westphal goto free_now; 6322df5042f4SFlorian Westphal 6323df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6324df5042f4SFlorian Westphal return; 6325df5042f4SFlorian Westphal free_now: 63264165079bSFlorian Westphal #ifdef CONFIG_XFRM 63274165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 63284165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 63294165079bSFlorian Westphal #endif 63304165079bSFlorian Westphal 6331df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6332df5042f4SFlorian Westphal } 6333df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6334df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 6335