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> 61*071c0fc6SJohannes Berg #include <linux/bitfield.h> 620d5501c1SVlad Yasevich #include <linux/if_vlan.h> 632a2ea508SJohn Hurley #include <linux/mpls.h> 64183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <net/protocol.h> 671da177e4SLinus Torvalds #include <net/dst.h> 681da177e4SLinus Torvalds #include <net/sock.h> 691da177e4SLinus Torvalds #include <net/checksum.h> 70ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 711da177e4SLinus Torvalds #include <net/xfrm.h> 728822e270SJohn Hurley #include <net/mpls.h> 733ee17bc7SMat Martineau #include <net/mptcp.h> 7478476d31SJeremy Kerr #include <net/mctp.h> 756a5bcd84SIlias Apalodimas #include <net/page_pool.h> 76*071c0fc6SJohannes Berg #include <net/dropreason.h> 771da177e4SLinus Torvalds 787c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 79ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 8051c56b00SEric Dumazet #include <linux/highmem.h> 81b245be1fSWillem de Bruijn #include <linux/capability.h> 82b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 832544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 842195e2a0SJakub Kicinski #include <linux/textsearch.h> 85a1f8e7f7SAl Viro 8639564c3fSEric Dumazet #include "dev.h" 877f678defSVasily Averin #include "sock_destructor.h" 887b7ed885SBart Van Assche 89025a785fSJakub Kicinski struct kmem_cache *skbuff_cache __ro_after_init; 9008009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 91df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 92df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 93df5042f4SFlorian Westphal #endif 94bf9f1baaSEric Dumazet 95bf9f1baaSEric Dumazet /* skb_small_head_cache and related code is only supported 96bf9f1baaSEric Dumazet * for CONFIG_SLAB and CONFIG_SLUB. 97bf9f1baaSEric Dumazet * As soon as SLOB is removed from the kernel, we can clean up this. 98bf9f1baaSEric Dumazet */ 99bf9f1baaSEric Dumazet #if !defined(CONFIG_SLOB) 100bf9f1baaSEric Dumazet # define HAVE_SKB_SMALL_HEAD_CACHE 1 101bf9f1baaSEric Dumazet #endif 102bf9f1baaSEric Dumazet 103bf9f1baaSEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 104bf9f1baaSEric Dumazet static struct kmem_cache *skb_small_head_cache __ro_after_init; 105bf9f1baaSEric Dumazet 106bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER) 107bf9f1baaSEric Dumazet 108bf9f1baaSEric Dumazet /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two. 109bf9f1baaSEric Dumazet * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique 110bf9f1baaSEric Dumazet * size, and we can differentiate heads from skb_small_head_cache 111bf9f1baaSEric Dumazet * vs system slabs by looking at their size (skb_end_offset()). 112bf9f1baaSEric Dumazet */ 113bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_CACHE_SIZE \ 114bf9f1baaSEric Dumazet (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \ 115bf9f1baaSEric Dumazet (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \ 116bf9f1baaSEric Dumazet SKB_SMALL_HEAD_SIZE) 117bf9f1baaSEric Dumazet 118bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_HEADROOM \ 119bf9f1baaSEric Dumazet SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE) 120bf9f1baaSEric Dumazet #endif /* HAVE_SKB_SMALL_HEAD_CACHE */ 121bf9f1baaSEric Dumazet 1225f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 1235f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 1241da177e4SLinus Torvalds 1259cb252c4SMenglong Dong #undef FN 1269cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 127*071c0fc6SJohannes Berg static const char * const drop_reasons[] = { 1280e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 1299cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 1309cb252c4SMenglong Dong }; 131*071c0fc6SJohannes Berg 132*071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = { 133*071c0fc6SJohannes Berg .reasons = drop_reasons, 134*071c0fc6SJohannes Berg .n_reasons = ARRAY_SIZE(drop_reasons), 135*071c0fc6SJohannes Berg }; 136*071c0fc6SJohannes Berg 137*071c0fc6SJohannes Berg const struct drop_reason_list __rcu * 138*071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = { 139*071c0fc6SJohannes Berg [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core), 140*071c0fc6SJohannes Berg }; 141*071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys); 142*071c0fc6SJohannes Berg 143*071c0fc6SJohannes Berg /** 144*071c0fc6SJohannes Berg * drop_reasons_register_subsys - register another drop reason subsystem 145*071c0fc6SJohannes Berg * @subsys: the subsystem to register, must not be the core 146*071c0fc6SJohannes Berg * @list: the list of drop reasons within the subsystem, must point to 147*071c0fc6SJohannes Berg * a statically initialized list 148*071c0fc6SJohannes Berg */ 149*071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys, 150*071c0fc6SJohannes Berg const struct drop_reason_list *list) 151*071c0fc6SJohannes Berg { 152*071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 153*071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 154*071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 155*071c0fc6SJohannes Berg return; 156*071c0fc6SJohannes Berg 157*071c0fc6SJohannes Berg /* must point to statically allocated memory, so INIT is OK */ 158*071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list); 159*071c0fc6SJohannes Berg } 160*071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys); 161*071c0fc6SJohannes Berg 162*071c0fc6SJohannes Berg /** 163*071c0fc6SJohannes Berg * drop_reasons_unregister_subsys - unregister a drop reason subsystem 164*071c0fc6SJohannes Berg * @subsys: the subsystem to remove, must not be the core 165*071c0fc6SJohannes Berg * 166*071c0fc6SJohannes Berg * Note: This will synchronize_rcu() to ensure no users when it returns. 167*071c0fc6SJohannes Berg */ 168*071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys) 169*071c0fc6SJohannes Berg { 170*071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 171*071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 172*071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 173*071c0fc6SJohannes Berg return; 174*071c0fc6SJohannes Berg 175*071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL); 176*071c0fc6SJohannes Berg 177*071c0fc6SJohannes Berg synchronize_rcu(); 178*071c0fc6SJohannes Berg } 179*071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys); 180ec43908dSMenglong Dong 1811da177e4SLinus Torvalds /** 182f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1831da177e4SLinus Torvalds * @skb: buffer 1841da177e4SLinus Torvalds * @sz: size 185f05de73bSJean Sacren * @addr: address 18699d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1871da177e4SLinus Torvalds * 188f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 189f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 190f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 191f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1921da177e4SLinus Torvalds */ 193f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 19499d5851eSJames Hogan const char msg[]) 1951da177e4SLinus Torvalds { 19641a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 19799d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1984305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 19926095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 2001da177e4SLinus Torvalds BUG(); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 203f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 2041da177e4SLinus Torvalds { 205f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 209f05de73bSJean Sacren { 210f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 211f05de73bSJean Sacren } 212c93bdd0eSMel Gorman 21350fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 214f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 215f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 21650fad4b5SAlexander Lobakin 217dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 218dbae2b06SPaolo Abeni 219dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 220dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 221dbae2b06SPaolo Abeni 222dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 223dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 224dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 225dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 226dbae2b06SPaolo Abeni */ 227dbae2b06SPaolo Abeni 228dbae2b06SPaolo Abeni struct page_frag_1k { 229dbae2b06SPaolo Abeni void *va; 230dbae2b06SPaolo Abeni u16 offset; 231dbae2b06SPaolo Abeni bool pfmemalloc; 232dbae2b06SPaolo Abeni }; 233dbae2b06SPaolo Abeni 234dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 235dbae2b06SPaolo Abeni { 236dbae2b06SPaolo Abeni struct page *page; 237dbae2b06SPaolo Abeni int offset; 238dbae2b06SPaolo Abeni 239dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 240dbae2b06SPaolo Abeni if (likely(offset >= 0)) 241dbae2b06SPaolo Abeni goto use_frag; 242dbae2b06SPaolo Abeni 243dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 244dbae2b06SPaolo Abeni if (!page) 245dbae2b06SPaolo Abeni return NULL; 246dbae2b06SPaolo Abeni 247dbae2b06SPaolo Abeni nc->va = page_address(page); 248dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 249dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 250dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 251dbae2b06SPaolo Abeni 252dbae2b06SPaolo Abeni use_frag: 253dbae2b06SPaolo Abeni nc->offset = offset; 254dbae2b06SPaolo Abeni return nc->va + offset; 255dbae2b06SPaolo Abeni } 256dbae2b06SPaolo Abeni #else 257dbae2b06SPaolo Abeni 258dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 259dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 260dbae2b06SPaolo Abeni */ 261dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 262dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 263dbae2b06SPaolo Abeni 264dbae2b06SPaolo Abeni struct page_frag_1k { 265dbae2b06SPaolo Abeni }; 266dbae2b06SPaolo Abeni 267dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 268dbae2b06SPaolo Abeni { 269dbae2b06SPaolo Abeni return NULL; 270dbae2b06SPaolo Abeni } 271dbae2b06SPaolo Abeni 272dbae2b06SPaolo Abeni #endif 273dbae2b06SPaolo Abeni 27450fad4b5SAlexander Lobakin struct napi_alloc_cache { 27550fad4b5SAlexander Lobakin struct page_frag_cache page; 276dbae2b06SPaolo Abeni struct page_frag_1k page_small; 27750fad4b5SAlexander Lobakin unsigned int skb_count; 27850fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 27950fad4b5SAlexander Lobakin }; 28050fad4b5SAlexander Lobakin 28150fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 28250fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 28350fad4b5SAlexander Lobakin 284dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 285dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 286dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 287dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 288dbae2b06SPaolo Abeni * does not accidentally come back. 289dbae2b06SPaolo Abeni */ 290dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 291dbae2b06SPaolo Abeni { 292dbae2b06SPaolo Abeni struct sk_buff *skb; 293dbae2b06SPaolo Abeni 294dbae2b06SPaolo Abeni local_bh_disable(); 295dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 296dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 297dbae2b06SPaolo Abeni napi_free_frags(napi); 298dbae2b06SPaolo Abeni local_bh_enable(); 299dbae2b06SPaolo Abeni } 300dbae2b06SPaolo Abeni 30132e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 30250fad4b5SAlexander Lobakin { 30350fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 30450fad4b5SAlexander Lobakin 30550fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 30650fad4b5SAlexander Lobakin 30732e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 30850fad4b5SAlexander Lobakin } 30950fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 31050fad4b5SAlexander Lobakin 31150fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 31250fad4b5SAlexander Lobakin { 31350fad4b5SAlexander Lobakin void *data; 31450fad4b5SAlexander Lobakin 31550fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 316afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 31732e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 31832e3573fSYajun Deng 31950fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 32050fad4b5SAlexander Lobakin } else { 32132e3573fSYajun Deng struct napi_alloc_cache *nc; 32232e3573fSYajun Deng 32350fad4b5SAlexander Lobakin local_bh_disable(); 32432e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 32532e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 32650fad4b5SAlexander Lobakin local_bh_enable(); 32750fad4b5SAlexander Lobakin } 32850fad4b5SAlexander Lobakin return data; 32950fad4b5SAlexander Lobakin } 33050fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 33150fad4b5SAlexander Lobakin 332f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 333f450d539SAlexander Lobakin { 334f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 335f450d539SAlexander Lobakin struct sk_buff *skb; 336f450d539SAlexander Lobakin 33749ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 338025a785fSJakub Kicinski nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache, 339f450d539SAlexander Lobakin GFP_ATOMIC, 340f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 341f450d539SAlexander Lobakin nc->skb_cache); 342f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 343f450d539SAlexander Lobakin return NULL; 34449ae83fcSSieng Piaw Liew } 345f450d539SAlexander Lobakin 346f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 347025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, skb); 348f450d539SAlexander Lobakin 349f450d539SAlexander Lobakin return skb; 350f450d539SAlexander Lobakin } 351f450d539SAlexander Lobakin 352ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 353ce098da1SKees Cook unsigned int size) 354ba0509b6SJesper Dangaard Brouer { 355ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 356ba0509b6SJesper Dangaard Brouer 357ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 358ba0509b6SJesper Dangaard Brouer 359ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 360ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 361ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 362ba0509b6SJesper Dangaard Brouer skb->head = data; 363ba0509b6SJesper Dangaard Brouer skb->data = data; 364ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 365763087daSEric Dumazet skb_set_end_offset(skb, size); 366ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 367ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 36868822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 369ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 370ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 371ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 372ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 373ba0509b6SJesper Dangaard Brouer 3746370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 375ba0509b6SJesper Dangaard Brouer } 376ba0509b6SJesper Dangaard Brouer 377ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 378ce098da1SKees Cook unsigned int *size) 379ce098da1SKees Cook { 380ce098da1SKees Cook void *resized; 381ce098da1SKees Cook 382ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */ 383ce098da1SKees Cook *size = ksize(data); 384ce098da1SKees Cook /* krealloc() will immediately return "data" when 385ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper 386ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note 387ce098da1SKees Cook * that this "new" pointer needs to be passed back to the 388ce098da1SKees Cook * caller for use so the __alloc_size hinting will be 389ce098da1SKees Cook * tracked correctly. 390ce098da1SKees Cook */ 391ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC); 392ce098da1SKees Cook WARN_ON_ONCE(resized != data); 393ce098da1SKees Cook return resized; 394ce098da1SKees Cook } 395ce098da1SKees Cook 396ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers. 397ce098da1SKees Cook * Note that this should be used sparingly as slab buffers 398ce098da1SKees Cook * cannot be combined efficiently by GRO! 399ce098da1SKees Cook */ 400ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data) 401ce098da1SKees Cook { 402ce098da1SKees Cook struct sk_buff *skb; 403ce098da1SKees Cook unsigned int size; 404ce098da1SKees Cook 405025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 406ce098da1SKees Cook if (unlikely(!skb)) 407ce098da1SKees Cook return NULL; 408ce098da1SKees Cook 409ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail)); 410ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 411ce098da1SKees Cook __finalize_skb_around(skb, data, size); 412ce098da1SKees Cook 413ce098da1SKees Cook return skb; 414ce098da1SKees Cook } 415ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb); 416ce098da1SKees Cook 417ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */ 418ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data, 419ce098da1SKees Cook unsigned int frag_size) 420ce098da1SKees Cook { 421ce098da1SKees Cook unsigned int size = frag_size; 422ce098da1SKees Cook 423ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers 424ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead. 425ce098da1SKees Cook */ 426ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 427ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 428ce098da1SKees Cook 429ce098da1SKees Cook __finalize_skb_around(skb, data, size); 430ce098da1SKees Cook } 431ce098da1SKees Cook 4321da177e4SLinus Torvalds /** 4332ea2f62cSEric Dumazet * __build_skb - build a network buffer 434b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 435ce098da1SKees Cook * @frag_size: size of data (must not be 0) 436b2b5ce9dSEric Dumazet * 437b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 438ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page 439ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 440ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb() 441ce098da1SKees Cook * instead.) 442b2b5ce9dSEric Dumazet * The return is the new skb buffer. 443b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 444b2b5ce9dSEric Dumazet * Notes : 445b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 446b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 447b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 448b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 449b2b5ce9dSEric Dumazet * before giving packet to stack. 450b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 451b2b5ce9dSEric Dumazet */ 4522ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 453b2b5ce9dSEric Dumazet { 454b2b5ce9dSEric Dumazet struct sk_buff *skb; 455b2b5ce9dSEric Dumazet 456025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 457ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 458b2b5ce9dSEric Dumazet return NULL; 459b2b5ce9dSEric Dumazet 460b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 461483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 462b2b5ce9dSEric Dumazet 463483126b3SAlexander Lobakin return skb; 464b2b5ce9dSEric Dumazet } 4652ea2f62cSEric Dumazet 4662ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 4672ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 4682ea2f62cSEric Dumazet */ 4692ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 4702ea2f62cSEric Dumazet { 4712ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 4722ea2f62cSEric Dumazet 4733c640126SGal Pressman if (likely(skb && frag_size)) { 4742ea2f62cSEric Dumazet skb->head_frag = 1; 475566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 4762ea2f62cSEric Dumazet } 4772ea2f62cSEric Dumazet return skb; 4782ea2f62cSEric Dumazet } 479b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 480b2b5ce9dSEric Dumazet 481ba0509b6SJesper Dangaard Brouer /** 482ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 483ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 484ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 48512c1604aSJakub Kicinski * @frag_size: size of data 486ba0509b6SJesper Dangaard Brouer */ 487ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 488ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 489ba0509b6SJesper Dangaard Brouer { 490ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 491ba0509b6SJesper Dangaard Brouer return NULL; 492ba0509b6SJesper Dangaard Brouer 493483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 494ba0509b6SJesper Dangaard Brouer 495483126b3SAlexander Lobakin if (frag_size) { 496ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 497566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 498ba0509b6SJesper Dangaard Brouer } 499ba0509b6SJesper Dangaard Brouer return skb; 500ba0509b6SJesper Dangaard Brouer } 501ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 502ba0509b6SJesper Dangaard Brouer 503f450d539SAlexander Lobakin /** 504f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 505f450d539SAlexander Lobakin * @data: data buffer provided by caller 50612c1604aSJakub Kicinski * @frag_size: size of data 507f450d539SAlexander Lobakin * 508f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 509f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 510f450d539SAlexander Lobakin * 511f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 512f450d539SAlexander Lobakin */ 513f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 514f450d539SAlexander Lobakin { 515f450d539SAlexander Lobakin struct sk_buff *skb; 516f450d539SAlexander Lobakin 517f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 518f450d539SAlexander Lobakin if (unlikely(!skb)) 519f450d539SAlexander Lobakin return NULL; 520f450d539SAlexander Lobakin 521f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 522f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 523f450d539SAlexander Lobakin 524f450d539SAlexander Lobakin return skb; 525f450d539SAlexander Lobakin } 526f450d539SAlexander Lobakin 527f450d539SAlexander Lobakin /** 528f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 529f450d539SAlexander Lobakin * @data: data buffer provided by caller 53012c1604aSJakub Kicinski * @frag_size: size of data 531f450d539SAlexander Lobakin * 532f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 533f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 534f450d539SAlexander Lobakin * 535f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 536f450d539SAlexander Lobakin */ 537f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 538f450d539SAlexander Lobakin { 539f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 540f450d539SAlexander Lobakin 541f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 542f450d539SAlexander Lobakin skb->head_frag = 1; 543f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 544f450d539SAlexander Lobakin } 545f450d539SAlexander Lobakin 546f450d539SAlexander Lobakin return skb; 547f450d539SAlexander Lobakin } 548f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 549f450d539SAlexander Lobakin 5505381b23dSAlexander Lobakin /* 5515381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 5525381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 5535381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 5545381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 5555381b23dSAlexander Lobakin * memory is free 5565381b23dSAlexander Lobakin */ 5575c0e820cSEric Dumazet static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, 558ef28095fSAlexander Lobakin bool *pfmemalloc) 5595381b23dSAlexander Lobakin { 5605381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 5615c0e820cSEric Dumazet unsigned int obj_size; 5625c0e820cSEric Dumazet void *obj; 5635381b23dSAlexander Lobakin 5645c0e820cSEric Dumazet obj_size = SKB_HEAD_ALIGN(*size); 565bf9f1baaSEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 566bf9f1baaSEric Dumazet if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE && 567bf9f1baaSEric Dumazet !(flags & KMALLOC_NOT_NORMAL_BITS)) { 568bf9f1baaSEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, 569bf9f1baaSEric Dumazet flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 570bf9f1baaSEric Dumazet node); 571bf9f1baaSEric Dumazet *size = SKB_SMALL_HEAD_CACHE_SIZE; 572880ce5f2SEric Dumazet if (obj || !(gfp_pfmemalloc_allowed(flags))) 573bf9f1baaSEric Dumazet goto out; 574880ce5f2SEric Dumazet /* Try again but now we are using pfmemalloc reserves */ 575880ce5f2SEric Dumazet ret_pfmemalloc = true; 576880ce5f2SEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node); 577880ce5f2SEric Dumazet goto out; 578bf9f1baaSEric Dumazet } 579bf9f1baaSEric Dumazet #endif 5805c0e820cSEric Dumazet *size = obj_size = kmalloc_size_roundup(obj_size); 5815381b23dSAlexander Lobakin /* 5825381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 5835381b23dSAlexander Lobakin * to the reserves, fail. 5845381b23dSAlexander Lobakin */ 5855c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, 5865381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 5875381b23dSAlexander Lobakin node); 5885381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 5895381b23dSAlexander Lobakin goto out; 5905381b23dSAlexander Lobakin 5915381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 5925381b23dSAlexander Lobakin ret_pfmemalloc = true; 5935c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, flags, node); 5945381b23dSAlexander Lobakin 5955381b23dSAlexander Lobakin out: 5965381b23dSAlexander Lobakin if (pfmemalloc) 5975381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5985381b23dSAlexander Lobakin 5995381b23dSAlexander Lobakin return obj; 6005381b23dSAlexander Lobakin } 6015381b23dSAlexander Lobakin 6025381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 6035381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 6045381b23dSAlexander Lobakin * [BEEP] leaks. 6055381b23dSAlexander Lobakin * 6065381b23dSAlexander Lobakin */ 6075381b23dSAlexander Lobakin 6085381b23dSAlexander Lobakin /** 6095381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 6105381b23dSAlexander Lobakin * @size: size to allocate 6115381b23dSAlexander Lobakin * @gfp_mask: allocation mask 6125381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 6135381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 6145381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 6155381b23dSAlexander Lobakin * allocations in case the data is required for writeback 6165381b23dSAlexander Lobakin * @node: numa node to allocate memory on 6175381b23dSAlexander Lobakin * 6185381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 6195381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 6205381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 6215381b23dSAlexander Lobakin * 6225381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 6235381b23dSAlexander Lobakin * %GFP_ATOMIC. 6245381b23dSAlexander Lobakin */ 6255381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 6265381b23dSAlexander Lobakin int flags, int node) 6275381b23dSAlexander Lobakin { 6285381b23dSAlexander Lobakin struct kmem_cache *cache; 6295381b23dSAlexander Lobakin struct sk_buff *skb; 6305381b23dSAlexander Lobakin bool pfmemalloc; 631a5df6333SLi RongQing u8 *data; 6325381b23dSAlexander Lobakin 6335381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 634025a785fSJakub Kicinski ? skbuff_fclone_cache : skbuff_cache; 6355381b23dSAlexander Lobakin 6365381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 6375381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 6385381b23dSAlexander Lobakin 6395381b23dSAlexander Lobakin /* Get the HEAD */ 640d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 641d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 642d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 643d13612b5SAlexander Lobakin else 644d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 645df1ae022SAlexander Lobakin if (unlikely(!skb)) 646df1ae022SAlexander Lobakin return NULL; 6475381b23dSAlexander Lobakin prefetchw(skb); 6485381b23dSAlexander Lobakin 6495381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 6505381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 6515381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 6525381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 6535381b23dSAlexander Lobakin */ 6545c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc); 655df1ae022SAlexander Lobakin if (unlikely(!data)) 6565381b23dSAlexander Lobakin goto nodata; 65712d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 6585381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 6595381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 6605381b23dSAlexander Lobakin */ 66165998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size)); 6625381b23dSAlexander Lobakin 6635381b23dSAlexander Lobakin /* 6645381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 6655381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 6665381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 6675381b23dSAlexander Lobakin */ 6685381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 66965998d2bSEric Dumazet __build_skb_around(skb, data, size); 6705381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 6715381b23dSAlexander Lobakin 6725381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 6735381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 6745381b23dSAlexander Lobakin 6755381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 6765381b23dSAlexander Lobakin 6775381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 6785381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 6795381b23dSAlexander Lobakin } 6805381b23dSAlexander Lobakin 6815381b23dSAlexander Lobakin return skb; 682df1ae022SAlexander Lobakin 6835381b23dSAlexander Lobakin nodata: 6845381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 685df1ae022SAlexander Lobakin return NULL; 6865381b23dSAlexander Lobakin } 6875381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 6885381b23dSAlexander Lobakin 6897ba7aeabSSebastian Andrzej Siewior /** 690fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 691fd11a83dSAlexander Duyck * @dev: network device to receive on 692d7499160SMasanari Iida * @len: length to allocate 693fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 694fd11a83dSAlexander Duyck * 695fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 696fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 697fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 698fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 699fd11a83dSAlexander Duyck * 700fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 701fd11a83dSAlexander Duyck */ 7029451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 7039451980aSAlexander Duyck gfp_t gfp_mask) 704fd11a83dSAlexander Duyck { 705b63ae8caSAlexander Duyck struct page_frag_cache *nc; 706fd11a83dSAlexander Duyck struct sk_buff *skb; 7079451980aSAlexander Duyck bool pfmemalloc; 7089451980aSAlexander Duyck void *data; 709fd11a83dSAlexander Duyck 7109451980aSAlexander Duyck len += NET_SKB_PAD; 711fd11a83dSAlexander Duyck 71266c55602SAlexander Lobakin /* If requested length is either too small or too big, 71366c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 71466c55602SAlexander Lobakin */ 71566c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 71666c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 717d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 718a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 719a080e7bdSAlexander Duyck if (!skb) 720a080e7bdSAlexander Duyck goto skb_fail; 721a080e7bdSAlexander Duyck goto skb_success; 722a080e7bdSAlexander Duyck } 7239451980aSAlexander Duyck 724115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 7259451980aSAlexander Duyck 7269451980aSAlexander Duyck if (sk_memalloc_socks()) 7279451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7289451980aSAlexander Duyck 729afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 7309451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 7318c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 7329451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 73392dcabd7SSebastian Andrzej Siewior } else { 73492dcabd7SSebastian Andrzej Siewior local_bh_disable(); 73592dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 73692dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 73792dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 73892dcabd7SSebastian Andrzej Siewior local_bh_enable(); 73992dcabd7SSebastian Andrzej Siewior } 7409451980aSAlexander Duyck 7419451980aSAlexander Duyck if (unlikely(!data)) 7429451980aSAlexander Duyck return NULL; 7439451980aSAlexander Duyck 7449451980aSAlexander Duyck skb = __build_skb(data, len); 7459451980aSAlexander Duyck if (unlikely(!skb)) { 746181edb2bSAlexander Duyck skb_free_frag(data); 7479451980aSAlexander Duyck return NULL; 7489451980aSAlexander Duyck } 7499451980aSAlexander Duyck 7509451980aSAlexander Duyck if (pfmemalloc) 7519451980aSAlexander Duyck skb->pfmemalloc = 1; 7529451980aSAlexander Duyck skb->head_frag = 1; 7539451980aSAlexander Duyck 754a080e7bdSAlexander Duyck skb_success: 7558af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 7567b2e497aSChristoph Hellwig skb->dev = dev; 757fd11a83dSAlexander Duyck 758a080e7bdSAlexander Duyck skb_fail: 7598af27456SChristoph Hellwig return skb; 7608af27456SChristoph Hellwig } 761b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 7621da177e4SLinus Torvalds 763fd11a83dSAlexander Duyck /** 764fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 765fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 766d7499160SMasanari Iida * @len: length to allocate 767fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 768fd11a83dSAlexander Duyck * 769fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 770fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 771fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 772fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 773fd11a83dSAlexander Duyck * 774fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 775fd11a83dSAlexander Duyck */ 7769451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 7779451980aSAlexander Duyck gfp_t gfp_mask) 778fd11a83dSAlexander Duyck { 7793226b158SEric Dumazet struct napi_alloc_cache *nc; 780fd11a83dSAlexander Duyck struct sk_buff *skb; 781dbae2b06SPaolo Abeni bool pfmemalloc; 7829451980aSAlexander Duyck void *data; 783fd11a83dSAlexander Duyck 784ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 7859451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 786fd11a83dSAlexander Duyck 7873226b158SEric Dumazet /* If requested length is either too small or too big, 7883226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 789dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 790dbae2b06SPaolo Abeni * for small fragments 7913226b158SEric Dumazet */ 792dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7933226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 794d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 795cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 796cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 797a080e7bdSAlexander Duyck if (!skb) 798a080e7bdSAlexander Duyck goto skb_fail; 799a080e7bdSAlexander Duyck goto skb_success; 800a080e7bdSAlexander Duyck } 8019451980aSAlexander Duyck 8023226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 8039451980aSAlexander Duyck 8049451980aSAlexander Duyck if (sk_memalloc_socks()) 8059451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 8069451980aSAlexander Duyck 807dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 808dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 809dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 810dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 811dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 812dbae2b06SPaolo Abeni * size above 512 bytes 813dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 814dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 815dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 816dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 817dbae2b06SPaolo Abeni */ 818dbae2b06SPaolo Abeni len = SZ_1K; 819dbae2b06SPaolo Abeni 820dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 821dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 822dbae2b06SPaolo Abeni } else { 823115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 824dbae2b06SPaolo Abeni 8258c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 826dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 827dbae2b06SPaolo Abeni } 828dbae2b06SPaolo Abeni 8299451980aSAlexander Duyck if (unlikely(!data)) 8309451980aSAlexander Duyck return NULL; 8319451980aSAlexander Duyck 832cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 8339451980aSAlexander Duyck if (unlikely(!skb)) { 834181edb2bSAlexander Duyck skb_free_frag(data); 8359451980aSAlexander Duyck return NULL; 8369451980aSAlexander Duyck } 8379451980aSAlexander Duyck 838dbae2b06SPaolo Abeni if (pfmemalloc) 8399451980aSAlexander Duyck skb->pfmemalloc = 1; 8409451980aSAlexander Duyck skb->head_frag = 1; 8419451980aSAlexander Duyck 842a080e7bdSAlexander Duyck skb_success: 843fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 844fd11a83dSAlexander Duyck skb->dev = napi->dev; 845fd11a83dSAlexander Duyck 846a080e7bdSAlexander Duyck skb_fail: 847fd11a83dSAlexander Duyck return skb; 848fd11a83dSAlexander Duyck } 849fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 850fd11a83dSAlexander Duyck 851654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 85250269e19SEric Dumazet int size, unsigned int truesize) 853654bed16SPeter Zijlstra { 854654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 855654bed16SPeter Zijlstra skb->len += size; 856654bed16SPeter Zijlstra skb->data_len += size; 85750269e19SEric Dumazet skb->truesize += truesize; 858654bed16SPeter Zijlstra } 859654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 860654bed16SPeter Zijlstra 861f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 862f8e617e1SJason Wang unsigned int truesize) 863f8e617e1SJason Wang { 864f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 865f8e617e1SJason Wang 866f8e617e1SJason Wang skb_frag_size_add(frag, size); 867f8e617e1SJason Wang skb->len += size; 868f8e617e1SJason Wang skb->data_len += size; 869f8e617e1SJason Wang skb->truesize += truesize; 870f8e617e1SJason Wang } 871f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 872f8e617e1SJason Wang 87327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 8741da177e4SLinus Torvalds { 875bd8a7036SEric Dumazet kfree_skb_list(*listp); 87627b437c8SHerbert Xu *listp = NULL; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 87927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 88027b437c8SHerbert Xu { 88127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 88227b437c8SHerbert Xu } 88327b437c8SHerbert Xu 8841da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 8851da177e4SLinus Torvalds { 8861da177e4SLinus Torvalds struct sk_buff *list; 8871da177e4SLinus Torvalds 888fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 8891da177e4SLinus Torvalds skb_get(list); 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 892b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) 8934727bab4SYunsheng Lin { 8944727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 8954727bab4SYunsheng Lin return false; 8968c48eea3SJakub Kicinski return page_pool_return_skb_page(virt_to_page(data), napi_safe); 8974727bab4SYunsheng Lin } 8984727bab4SYunsheng Lin 899bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset) 900bf9f1baaSEric Dumazet { 901bf9f1baaSEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 902bf9f1baaSEric Dumazet if (end_offset == SKB_SMALL_HEAD_HEADROOM) 903bf9f1baaSEric Dumazet kmem_cache_free(skb_small_head_cache, head); 904bf9f1baaSEric Dumazet else 905bf9f1baaSEric Dumazet #endif 906bf9f1baaSEric Dumazet kfree(head); 907bf9f1baaSEric Dumazet } 908bf9f1baaSEric Dumazet 909b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe) 910d3836f21SEric Dumazet { 911181edb2bSAlexander Duyck unsigned char *head = skb->head; 912181edb2bSAlexander Duyck 9136a5bcd84SIlias Apalodimas if (skb->head_frag) { 914b07a2d97SJakub Kicinski if (skb_pp_recycle(skb, head, napi_safe)) 9156a5bcd84SIlias Apalodimas return; 916181edb2bSAlexander Duyck skb_free_frag(head); 9176a5bcd84SIlias Apalodimas } else { 918bf9f1baaSEric Dumazet skb_kfree_head(head, skb_end_offset(skb)); 919d3836f21SEric Dumazet } 9206a5bcd84SIlias Apalodimas } 921d3836f21SEric Dumazet 922b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason, 923b07a2d97SJakub Kicinski bool napi_safe) 9241da177e4SLinus Torvalds { 925ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 9261da177e4SLinus Torvalds int i; 927ff04a771SEric Dumazet 928ff04a771SEric Dumazet if (skb->cloned && 929ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 930ff04a771SEric Dumazet &shinfo->dataref)) 9312cc3aeb5SIlias Apalodimas goto exit; 932ff04a771SEric Dumazet 933753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 934753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 935753f1ca4SPavel Begunkov 93670c43167SJonathan Lemon skb_zcopy_clear(skb, true); 937753f1ca4SPavel Begunkov if (skip_unref) 938753f1ca4SPavel Begunkov goto free_head; 939753f1ca4SPavel Begunkov } 94070c43167SJonathan Lemon 941ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 9428c48eea3SJakub Kicinski napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe); 9431da177e4SLinus Torvalds 944753f1ca4SPavel Begunkov free_head: 945ff04a771SEric Dumazet if (shinfo->frag_list) 946511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 9471da177e4SLinus Torvalds 948b07a2d97SJakub Kicinski skb_free_head(skb, napi_safe); 9492cc3aeb5SIlias Apalodimas exit: 9502cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 9512cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 9522cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 9532cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 9542cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 95558e61e41STom Rix * additional references to the fragmented part of the SKB. 9562cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 9572cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 9582cc3aeb5SIlias Apalodimas */ 9592cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 9641da177e4SLinus Torvalds */ 9652d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 9661da177e4SLinus Torvalds { 967d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 968d179cd12SDavid S. Miller 969d179cd12SDavid S. Miller switch (skb->fclone) { 970d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 971025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 9726ffe75ebSEric Dumazet return; 973d179cd12SDavid S. Miller 974d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 975d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 9766ffe75ebSEric Dumazet 9776ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 9786ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 9796ffe75ebSEric Dumazet * while here, branch prediction will be good. 9806ffe75ebSEric Dumazet */ 9812638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 9826ffe75ebSEric Dumazet goto fastpath; 983d179cd12SDavid S. Miller break; 984d179cd12SDavid S. Miller 9856ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 986d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 987d179cd12SDavid S. Miller break; 9883ff50b79SStephen Hemminger } 9892638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 9906ffe75ebSEric Dumazet return; 9916ffe75ebSEric Dumazet fastpath: 9926ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9950a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 9961da177e4SLinus Torvalds { 997adf30907SEric Dumazet skb_dst_drop(skb); 9981da177e4SLinus Torvalds if (skb->destructor) { 9997890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 10001da177e4SLinus Torvalds skb->destructor(skb); 10011da177e4SLinus Torvalds } 1002a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1003cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 10042fc72c7bSKOVACS Krisztian #endif 1005df5042f4SFlorian Westphal skb_ext_put(skb); 100604a4bb55SLennert Buytenhek } 100704a4bb55SLennert Buytenhek 100804a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 1009b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason, 1010b07a2d97SJakub Kicinski bool napi_safe) 101104a4bb55SLennert Buytenhek { 101204a4bb55SLennert Buytenhek skb_release_head_state(skb); 1013a28b1b90SFlorian Westphal if (likely(skb->head)) 1014b07a2d97SJakub Kicinski skb_release_data(skb, reason, napi_safe); 10152d4baff8SHerbert Xu } 10161da177e4SLinus Torvalds 10172d4baff8SHerbert Xu /** 10182d4baff8SHerbert Xu * __kfree_skb - private function 10192d4baff8SHerbert Xu * @skb: buffer 10202d4baff8SHerbert Xu * 10212d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 10222d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 10232d4baff8SHerbert Xu * always call kfree_skb 10242d4baff8SHerbert Xu */ 10252d4baff8SHerbert Xu 10262d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 10272d4baff8SHerbert Xu { 1028b07a2d97SJakub Kicinski skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false); 10291da177e4SLinus Torvalds kfree_skbmem(skb); 10301da177e4SLinus Torvalds } 1031b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 10321da177e4SLinus Torvalds 1033a4650da2SJesper Dangaard Brouer static __always_inline 1034a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1035a4650da2SJesper Dangaard Brouer { 1036a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 1037a4650da2SJesper Dangaard Brouer return false; 1038a4650da2SJesper Dangaard Brouer 1039*071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || 1040*071c0fc6SJohannes Berg u32_get_bits(reason, 1041*071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >= 1042*071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_NUM); 1043a4650da2SJesper Dangaard Brouer 1044a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 1045dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1046a4650da2SJesper Dangaard Brouer else 1047a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 1048a4650da2SJesper Dangaard Brouer return true; 1049a4650da2SJesper Dangaard Brouer } 1050a4650da2SJesper Dangaard Brouer 10511da177e4SLinus Torvalds /** 1052c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 1053231d06aeSJörn Engel * @skb: buffer to free 1054c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 1055231d06aeSJörn Engel * 1056231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 1057c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 1058c504e5c2SMenglong Dong * tracepoint. 1059231d06aeSJörn Engel */ 1060c205cc75SMenglong Dong void __fix_address 1061c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1062231d06aeSJörn Engel { 1063a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 1064231d06aeSJörn Engel __kfree_skb(skb); 1065231d06aeSJörn Engel } 1066c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 1067231d06aeSJörn Engel 1068eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 1069eedade12SJesper Dangaard Brouer 1070eedade12SJesper Dangaard Brouer struct skb_free_array { 1071eedade12SJesper Dangaard Brouer unsigned int skb_count; 1072eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 1073eedade12SJesper Dangaard Brouer }; 1074eedade12SJesper Dangaard Brouer 1075eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 1076eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 1077eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 1078eedade12SJesper Dangaard Brouer { 1079eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1080eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 1081eedade12SJesper Dangaard Brouer __kfree_skb(skb); 1082eedade12SJesper Dangaard Brouer return; 1083eedade12SJesper Dangaard Brouer } 1084eedade12SJesper Dangaard Brouer 1085b07a2d97SJakub Kicinski skb_release_all(skb, reason, false); 1086eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 1087eedade12SJesper Dangaard Brouer 1088eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 1089025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE, 1090eedade12SJesper Dangaard Brouer sa->skb_array); 1091eedade12SJesper Dangaard Brouer sa->skb_count = 0; 1092eedade12SJesper Dangaard Brouer } 1093eedade12SJesper Dangaard Brouer } 1094eedade12SJesper Dangaard Brouer 1095a4650da2SJesper Dangaard Brouer void __fix_address 1096a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 1097bd8a7036SEric Dumazet { 1098eedade12SJesper Dangaard Brouer struct skb_free_array sa; 1099eedade12SJesper Dangaard Brouer 1100eedade12SJesper Dangaard Brouer sa.skb_count = 0; 1101eedade12SJesper Dangaard Brouer 1102bd8a7036SEric Dumazet while (segs) { 1103bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 1104bd8a7036SEric Dumazet 11059dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) { 11069dde0cd3SJesper Dangaard Brouer skb_poison_list(segs); 1107eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 11089dde0cd3SJesper Dangaard Brouer } 1109eedade12SJesper Dangaard Brouer 1110bd8a7036SEric Dumazet segs = next; 1111bd8a7036SEric Dumazet } 1112eedade12SJesper Dangaard Brouer 1113eedade12SJesper Dangaard Brouer if (sa.skb_count) 1114025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array); 1115bd8a7036SEric Dumazet } 1116215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1117bd8a7036SEric Dumazet 11186413139dSWillem de Bruijn /* Dump skb information and contents. 11196413139dSWillem de Bruijn * 11206413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 11216413139dSWillem de Bruijn * 1122302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 11236413139dSWillem de Bruijn */ 11246413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 11256413139dSWillem de Bruijn { 11266413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 11276413139dSWillem de Bruijn struct net_device *dev = skb->dev; 11286413139dSWillem de Bruijn struct sock *sk = skb->sk; 11296413139dSWillem de Bruijn struct sk_buff *list_skb; 11306413139dSWillem de Bruijn bool has_mac, has_trans; 11316413139dSWillem de Bruijn int headroom, tailroom; 11326413139dSWillem de Bruijn int i, len, seg_len; 11336413139dSWillem de Bruijn 11346413139dSWillem de Bruijn if (full_pkt) 11356413139dSWillem de Bruijn len = skb->len; 11366413139dSWillem de Bruijn else 11376413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 11386413139dSWillem de Bruijn 11396413139dSWillem de Bruijn headroom = skb_headroom(skb); 11406413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 11416413139dSWillem de Bruijn 11426413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 11436413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 11446413139dSWillem de Bruijn 11456413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 11466413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 11476413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 11486413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 11496413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 11506413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 11516413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 11526413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 11536413139dSWillem de Bruijn skb->network_header, 11546413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 11556413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 11566413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 11576413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 11586413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 11596413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 11606413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 11616413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 11626413139dSWillem de Bruijn 11636413139dSWillem de Bruijn if (dev) 11648a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 11656413139dSWillem de Bruijn level, dev->name, &dev->features); 11666413139dSWillem de Bruijn if (sk) 1167db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 11686413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 11696413139dSWillem de Bruijn 11706413139dSWillem de Bruijn if (full_pkt && headroom) 11716413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 11726413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 11736413139dSWillem de Bruijn 11746413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 11756413139dSWillem de Bruijn if (seg_len) 11766413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 11776413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 11786413139dSWillem de Bruijn len -= seg_len; 11796413139dSWillem de Bruijn 11806413139dSWillem de Bruijn if (full_pkt && tailroom) 11816413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 11826413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 11836413139dSWillem de Bruijn 11846413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 11856413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 11866413139dSWillem de Bruijn u32 p_off, p_len, copied; 11876413139dSWillem de Bruijn struct page *p; 11886413139dSWillem de Bruijn u8 *vaddr; 11896413139dSWillem de Bruijn 1190b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 11916413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 11926413139dSWillem de Bruijn copied) { 11936413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 11946413139dSWillem de Bruijn vaddr = kmap_atomic(p); 11956413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 11966413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 11976413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 11986413139dSWillem de Bruijn kunmap_atomic(vaddr); 11996413139dSWillem de Bruijn len -= seg_len; 12006413139dSWillem de Bruijn if (!len) 12016413139dSWillem de Bruijn break; 12026413139dSWillem de Bruijn } 12036413139dSWillem de Bruijn } 12046413139dSWillem de Bruijn 12056413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 12066413139dSWillem de Bruijn printk("skb fraglist:\n"); 12076413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 12086413139dSWillem de Bruijn skb_dump(level, list_skb, true); 12096413139dSWillem de Bruijn } 12106413139dSWillem de Bruijn } 12116413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 12126413139dSWillem de Bruijn 1213d1a203eaSStephen Hemminger /** 121425121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 121525121173SMichael S. Tsirkin * @skb: buffer that triggered an error 121625121173SMichael S. Tsirkin * 121725121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 121825121173SMichael S. Tsirkin * skb must be freed afterwards. 121925121173SMichael S. Tsirkin */ 122025121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 122125121173SMichael S. Tsirkin { 1222753f1ca4SPavel Begunkov if (skb) { 1223753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 12241f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 122525121173SMichael S. Tsirkin } 1226753f1ca4SPavel Begunkov } 122725121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 122825121173SMichael S. Tsirkin 1229be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 123025121173SMichael S. Tsirkin /** 1231ead2ceb0SNeil Horman * consume_skb - free an skbuff 1232ead2ceb0SNeil Horman * @skb: buffer to free 1233ead2ceb0SNeil Horman * 1234ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1235ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1236ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1237ead2ceb0SNeil Horman */ 1238ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1239ead2ceb0SNeil Horman { 12403889a803SPaolo Abeni if (!skb_unref(skb)) 1241ead2ceb0SNeil Horman return; 12423889a803SPaolo Abeni 1243dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1244ead2ceb0SNeil Horman __kfree_skb(skb); 1245ead2ceb0SNeil Horman } 1246ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1247be769db2SHerbert Xu #endif 1248ead2ceb0SNeil Horman 12490a463c78SPaolo Abeni /** 1250c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 12510a463c78SPaolo Abeni * @skb: buffer to free 12520a463c78SPaolo Abeni * 1253ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1254ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 12550a463c78SPaolo Abeni */ 1256ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 12570a463c78SPaolo Abeni { 1258dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1259b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 12600a463c78SPaolo Abeni kfree_skbmem(skb); 12610a463c78SPaolo Abeni } 12620a463c78SPaolo Abeni 1263f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1264795bb1c0SJesper Dangaard Brouer { 1265795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1266f450d539SAlexander Lobakin u32 i; 1267795bb1c0SJesper Dangaard Brouer 1268025a785fSJakub Kicinski kasan_poison_object_data(skbuff_cache, skb); 1269795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1270795bb1c0SJesper Dangaard Brouer 1271795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1272f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1273025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, 1274f450d539SAlexander Lobakin nc->skb_cache[i]); 1275f450d539SAlexander Lobakin 1276025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF, 1277f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1278f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1279795bb1c0SJesper Dangaard Brouer } 1280795bb1c0SJesper Dangaard Brouer } 1281f450d539SAlexander Lobakin 12828fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason) 128315fad714SJesper Dangaard Brouer { 12848fa66e4aSJakub Kicinski skb_release_all(skb, reason, true); 12859243adfcSAlexander Lobakin napi_skb_cache_put(skb); 12869243adfcSAlexander Lobakin } 12879243adfcSAlexander Lobakin 12889243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 12899243adfcSAlexander Lobakin { 12909efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 12918550ff8dSPaul Blakey nf_reset_ct(skb); 12929243adfcSAlexander Lobakin skb_dst_drop(skb); 12939243adfcSAlexander Lobakin skb_ext_put(skb); 12945e10da53SPaolo Abeni skb_orphan(skb); 12959efb4b5bSPaolo Abeni skb->slow_gro = 0; 12969efb4b5bSPaolo Abeni } 1297f450d539SAlexander Lobakin napi_skb_cache_put(skb); 129815fad714SJesper Dangaard Brouer } 1299795bb1c0SJesper Dangaard Brouer 1300795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1301795bb1c0SJesper Dangaard Brouer { 1302885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1303795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1304885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1305795bb1c0SJesper Dangaard Brouer return; 1306795bb1c0SJesper Dangaard Brouer } 1307795bb1c0SJesper Dangaard Brouer 1308ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 13096454eca8SYunsheng Lin 13107608894eSPaolo Abeni if (!skb_unref(skb)) 1311795bb1c0SJesper Dangaard Brouer return; 13127608894eSPaolo Abeni 1313795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1314dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1315795bb1c0SJesper Dangaard Brouer 1316795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1317abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1318795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1319795bb1c0SJesper Dangaard Brouer return; 1320795bb1c0SJesper Dangaard Brouer } 1321795bb1c0SJesper Dangaard Brouer 1322b07a2d97SJakub Kicinski skb_release_all(skb, SKB_CONSUMED, !!budget); 1323f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1324795bb1c0SJesper Dangaard Brouer } 1325795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1326795bb1c0SJesper Dangaard Brouer 132703f61041SKees Cook /* Make sure a field is contained by headers group */ 1328b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 132903f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 133003f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1331b1937227SEric Dumazet 1332dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1333dec18810SHerbert Xu { 1334dec18810SHerbert Xu new->tstamp = old->tstamp; 1335b1937227SEric Dumazet /* We do not copy old->sk */ 1336dec18810SHerbert Xu new->dev = old->dev; 1337b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 13387fee226aSEric Dumazet skb_dst_copy(new, old); 1339df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1340b1937227SEric Dumazet __nf_copy(new, old, false); 13416aa895b0SPatrick McHardy 134203f61041SKees Cook /* Note : this field could be in the headers group. 1343b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1344b1937227SEric Dumazet */ 1345b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 134606021292SEliezer Tamir 134703f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1348b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1349b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1350b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1351b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1352b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1353b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1354b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1355b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1356b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1357b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1358b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1359b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1360b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1361b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1362b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1363b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1364b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1365b1937227SEric Dumazet #endif 1366e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1367b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 136806021292SEliezer Tamir #endif 136968822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 13702bd82484SEric Dumazet #ifdef CONFIG_XPS 13712bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 13722bd82484SEric Dumazet #endif 1373b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1374b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1375b1937227SEric Dumazet #endif 1376b1937227SEric Dumazet 1377dec18810SHerbert Xu } 1378dec18810SHerbert Xu 137982c49a35SHerbert Xu /* 138082c49a35SHerbert Xu * You should not add any new code to this function. Add it to 138182c49a35SHerbert Xu * __copy_skb_header above instead. 138282c49a35SHerbert Xu */ 1383e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds #define C(x) n->x = skb->x 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds n->next = n->prev = NULL; 13881da177e4SLinus Torvalds n->sk = NULL; 1389dec18810SHerbert Xu __copy_skb_header(n, skb); 1390dec18810SHerbert Xu 13911da177e4SLinus Torvalds C(len); 13921da177e4SLinus Torvalds C(data_len); 13933e6b3b2eSAlexey Dobriyan C(mac_len); 1394334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 139502f1c89dSPaul Moore n->cloned = 1; 13961da177e4SLinus Torvalds n->nohdr = 0; 1397b13dda9fSEric Dumazet n->peeked = 0; 1398e78bfb07SStefano Brivio C(pfmemalloc); 13996a5bcd84SIlias Apalodimas C(pp_recycle); 14001da177e4SLinus Torvalds n->destructor = NULL; 14011da177e4SLinus Torvalds C(tail); 14021da177e4SLinus Torvalds C(end); 140302f1c89dSPaul Moore C(head); 1404d3836f21SEric Dumazet C(head_frag); 140502f1c89dSPaul Moore C(data); 140602f1c89dSPaul Moore C(truesize); 140763354797SReshetova, Elena refcount_set(&n->users, 1); 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 14101da177e4SLinus Torvalds skb->cloned = 1; 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds return n; 1413e0053ec0SHerbert Xu #undef C 1414e0053ec0SHerbert Xu } 1415e0053ec0SHerbert Xu 1416e0053ec0SHerbert Xu /** 1417da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1418da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1419da29e4b4SJakub Kicinski */ 1420da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1421da29e4b4SJakub Kicinski { 1422da29e4b4SJakub Kicinski struct sk_buff *n; 1423da29e4b4SJakub Kicinski 1424da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1425da29e4b4SJakub Kicinski if (!n) 1426da29e4b4SJakub Kicinski return NULL; 1427da29e4b4SJakub Kicinski 1428da29e4b4SJakub Kicinski n->len = first->len; 1429da29e4b4SJakub Kicinski n->data_len = first->len; 1430da29e4b4SJakub Kicinski n->truesize = first->truesize; 1431da29e4b4SJakub Kicinski 1432da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1433da29e4b4SJakub Kicinski 1434da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1435da29e4b4SJakub Kicinski n->destructor = NULL; 1436da29e4b4SJakub Kicinski 1437da29e4b4SJakub Kicinski return n; 1438da29e4b4SJakub Kicinski } 1439da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1440da29e4b4SJakub Kicinski 1441da29e4b4SJakub Kicinski /** 1442e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1443e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1444e0053ec0SHerbert Xu * @src: the skb to supply the contents 1445e0053ec0SHerbert Xu * 1446e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1447e0053ec0SHerbert Xu * supplied by the user. 1448e0053ec0SHerbert Xu * 1449e0053ec0SHerbert Xu * The target skb is returned upon exit. 1450e0053ec0SHerbert Xu */ 1451e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1452e0053ec0SHerbert Xu { 1453b07a2d97SJakub Kicinski skb_release_all(dst, SKB_CONSUMED, false); 1454e0053ec0SHerbert Xu return __skb_clone(dst, src); 1455e0053ec0SHerbert Xu } 1456e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1457e0053ec0SHerbert Xu 14586f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1459a91dbff5SWillem de Bruijn { 146014ade6baSWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg, rlim; 1461a91dbff5SWillem de Bruijn struct user_struct *user; 1462a91dbff5SWillem de Bruijn 1463a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1464a91dbff5SWillem de Bruijn return 0; 1465a91dbff5SWillem de Bruijn 146614ade6baSWillem de Bruijn rlim = rlimit(RLIMIT_MEMLOCK); 146714ade6baSWillem de Bruijn if (rlim == RLIM_INFINITY) 146814ade6baSWillem de Bruijn return 0; 146914ade6baSWillem de Bruijn 1470a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 147114ade6baSWillem de Bruijn max_pg = rlim >> PAGE_SHIFT; 1472a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1473a91dbff5SWillem de Bruijn 1474a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 147557fc05e8SEric Dumazet do { 1476a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1477a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1478a91dbff5SWillem de Bruijn return -ENOBUFS; 147957fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1480a91dbff5SWillem de Bruijn 1481a91dbff5SWillem de Bruijn if (!mmp->user) { 1482a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1483a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1484a91dbff5SWillem de Bruijn } else { 1485a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1486a91dbff5SWillem de Bruijn } 1487a91dbff5SWillem de Bruijn 1488a91dbff5SWillem de Bruijn return 0; 1489a91dbff5SWillem de Bruijn } 14906f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1491a91dbff5SWillem de Bruijn 14926f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1493a91dbff5SWillem de Bruijn { 1494a91dbff5SWillem de Bruijn if (mmp->user) { 1495a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1496a91dbff5SWillem de Bruijn free_uid(mmp->user); 1497a91dbff5SWillem de Bruijn } 1498a91dbff5SWillem de Bruijn } 14996f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1500a91dbff5SWillem de Bruijn 1501c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 150252267790SWillem de Bruijn { 1503e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 150452267790SWillem de Bruijn struct sk_buff *skb; 150552267790SWillem de Bruijn 150652267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 150752267790SWillem de Bruijn 150852267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 150952267790SWillem de Bruijn if (!skb) 151052267790SWillem de Bruijn return NULL; 151152267790SWillem de Bruijn 151252267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 151352267790SWillem de Bruijn uarg = (void *)skb->cb; 1514a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1515a91dbff5SWillem de Bruijn 1516a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1517a91dbff5SWillem de Bruijn kfree_skb(skb); 1518a91dbff5SWillem de Bruijn return NULL; 1519a91dbff5SWillem de Bruijn } 152052267790SWillem de Bruijn 1521e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 15224ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 15234ab6c99dSWillem de Bruijn uarg->len = 1; 15244ab6c99dSWillem de Bruijn uarg->bytelen = size; 152552267790SWillem de Bruijn uarg->zerocopy = 1; 1526e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1527e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 152852267790SWillem de Bruijn sock_hold(sk); 152952267790SWillem de Bruijn 1530e7d2b510SPavel Begunkov return &uarg->ubuf; 153152267790SWillem de Bruijn } 153252267790SWillem de Bruijn 1533e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 153452267790SWillem de Bruijn { 153552267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 153652267790SWillem de Bruijn } 153752267790SWillem de Bruijn 15388c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 15394ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 15404ab6c99dSWillem de Bruijn { 15414ab6c99dSWillem de Bruijn if (uarg) { 1542e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 15434ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 15444ab6c99dSWillem de Bruijn u32 bytelen, next; 15454ab6c99dSWillem de Bruijn 15461b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 15471b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 15481b4b2b09SPavel Begunkov return NULL; 15491b4b2b09SPavel Begunkov 15504ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 15514ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 15524ab6c99dSWillem de Bruijn */ 15534ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 15544ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 15554ab6c99dSWillem de Bruijn return NULL; 15564ab6c99dSWillem de Bruijn } 15574ab6c99dSWillem de Bruijn 1558e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1559e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1560e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 15614ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 15624ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15634ab6c99dSWillem de Bruijn goto new_alloc; 15644ab6c99dSWillem de Bruijn return NULL; 15654ab6c99dSWillem de Bruijn } 15664ab6c99dSWillem de Bruijn 15674ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1568e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1569e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1570a91dbff5SWillem de Bruijn return NULL; 1571e7d2b510SPavel Begunkov uarg_zc->len++; 1572e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 15734ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1574100f6d8eSWillem de Bruijn 1575100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1576100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15778e044917SJonathan Lemon net_zcopy_get(uarg); 1578100f6d8eSWillem de Bruijn 15794ab6c99dSWillem de Bruijn return uarg; 15804ab6c99dSWillem de Bruijn } 15814ab6c99dSWillem de Bruijn } 15824ab6c99dSWillem de Bruijn 15834ab6c99dSWillem de Bruijn new_alloc: 15848c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 15854ab6c99dSWillem de Bruijn } 15868c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 15874ab6c99dSWillem de Bruijn 15884ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 15894ab6c99dSWillem de Bruijn { 15904ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 15914ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 15924ab6c99dSWillem de Bruijn u64 sum_len; 15934ab6c99dSWillem de Bruijn 15944ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 15954ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 15964ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 15974ab6c99dSWillem de Bruijn 15984ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 15994ab6c99dSWillem de Bruijn return false; 16004ab6c99dSWillem de Bruijn 16014ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 16024ab6c99dSWillem de Bruijn return false; 16034ab6c99dSWillem de Bruijn 16044ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 16054ab6c99dSWillem de Bruijn return true; 16064ab6c99dSWillem de Bruijn } 16074ab6c99dSWillem de Bruijn 1608e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 160952267790SWillem de Bruijn { 16104ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 161152267790SWillem de Bruijn struct sock_exterr_skb *serr; 161252267790SWillem de Bruijn struct sock *sk = skb->sk; 16134ab6c99dSWillem de Bruijn struct sk_buff_head *q; 16144ab6c99dSWillem de Bruijn unsigned long flags; 16153bdd5ee0SWillem de Bruijn bool is_zerocopy; 16164ab6c99dSWillem de Bruijn u32 lo, hi; 16174ab6c99dSWillem de Bruijn u16 len; 161852267790SWillem de Bruijn 1619ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1620ccaffff1SWillem de Bruijn 16214ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 16224ab6c99dSWillem de Bruijn * so do not queue a completion notification 16234ab6c99dSWillem de Bruijn */ 16244ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 162552267790SWillem de Bruijn goto release; 162652267790SWillem de Bruijn 16274ab6c99dSWillem de Bruijn len = uarg->len; 16284ab6c99dSWillem de Bruijn lo = uarg->id; 16294ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 16303bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 16314ab6c99dSWillem de Bruijn 163252267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 163352267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 163452267790SWillem de Bruijn serr->ee.ee_errno = 0; 163552267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 16364ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 16374ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 16383bdd5ee0SWillem de Bruijn if (!is_zerocopy) 163952267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 164052267790SWillem de Bruijn 16414ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 16424ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 16434ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 16444ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 16454ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 16464ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 164752267790SWillem de Bruijn skb = NULL; 16484ab6c99dSWillem de Bruijn } 16494ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 165052267790SWillem de Bruijn 1651e3ae2365SAlexander Aring sk_error_report(sk); 165252267790SWillem de Bruijn 165352267790SWillem de Bruijn release: 165452267790SWillem de Bruijn consume_skb(skb); 165552267790SWillem de Bruijn sock_put(sk); 165652267790SWillem de Bruijn } 165775518851SJonathan Lemon 16588c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 165936177832SJonathan Lemon bool success) 166075518851SJonathan Lemon { 1661e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1662e7d2b510SPavel Begunkov 1663e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 166475518851SJonathan Lemon 166575518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1666e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 166775518851SJonathan Lemon } 16688c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 166952267790SWillem de Bruijn 16708c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 167152267790SWillem de Bruijn { 1672e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 167352267790SWillem de Bruijn 167452267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1675e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 167652267790SWillem de Bruijn 167752900d22SWillem de Bruijn if (have_uref) 16788c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 167952267790SWillem de Bruijn } 16808c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 168152267790SWillem de Bruijn 168252267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 168352267790SWillem de Bruijn struct msghdr *msg, int len, 168452267790SWillem de Bruijn struct ubuf_info *uarg) 168552267790SWillem de Bruijn { 16864ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 168752267790SWillem de Bruijn int err, orig_len = skb->len; 168852267790SWillem de Bruijn 16894ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 16904ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 16914ab6c99dSWillem de Bruijn */ 16924ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 16934ab6c99dSWillem de Bruijn return -EEXIST; 16944ab6c99dSWillem de Bruijn 1695ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 169652267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 169754d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 169854d43117SWillem de Bruijn 169952267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1700829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 170154d43117SWillem de Bruijn skb->sk = sk; 170252267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 170354d43117SWillem de Bruijn skb->sk = save_sk; 170452267790SWillem de Bruijn return err; 170552267790SWillem de Bruijn } 170652267790SWillem de Bruijn 170752900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 170852267790SWillem de Bruijn return skb->len - orig_len; 170952267790SWillem de Bruijn } 171052267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 171152267790SWillem de Bruijn 1712753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1713753f1ca4SPavel Begunkov { 1714753f1ca4SPavel Begunkov int i; 1715753f1ca4SPavel Begunkov 1716753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1717753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1718753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1719753f1ca4SPavel Begunkov } 1720753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1721753f1ca4SPavel Begunkov 17221f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 172352267790SWillem de Bruijn gfp_t gfp_mask) 172452267790SWillem de Bruijn { 172552267790SWillem de Bruijn if (skb_zcopy(orig)) { 172652267790SWillem de Bruijn if (skb_zcopy(nskb)) { 172752267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 172852267790SWillem de Bruijn if (!gfp_mask) { 172952267790SWillem de Bruijn WARN_ON_ONCE(1); 173052267790SWillem de Bruijn return -ENOMEM; 173152267790SWillem de Bruijn } 173252267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 173352267790SWillem de Bruijn return 0; 173452267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 173552267790SWillem de Bruijn return -EIO; 173652267790SWillem de Bruijn } 173752900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 173852267790SWillem de Bruijn } 173952267790SWillem de Bruijn return 0; 174052267790SWillem de Bruijn } 174152267790SWillem de Bruijn 17422c53040fSBen Hutchings /** 17432c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 174448c83012SMichael S. Tsirkin * @skb: the skb to modify 174548c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 174648c83012SMichael S. Tsirkin * 174706b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 174848c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 174948c83012SMichael S. Tsirkin * to userspace pages. 175048c83012SMichael S. Tsirkin * 175148c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 175248c83012SMichael S. Tsirkin * %GFP_ATOMIC. 175348c83012SMichael S. Tsirkin * 175448c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 175548c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 175648c83012SMichael S. Tsirkin */ 175748c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1758a6686f2fSShirley Ma { 1759a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1760a6686f2fSShirley Ma struct page *page, *head = NULL; 17613ece7826SWillem de Bruijn int i, new_frags; 17623ece7826SWillem de Bruijn u32 d_off; 1763a6686f2fSShirley Ma 17643ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 17653ece7826SWillem de Bruijn return -EINVAL; 17663ece7826SWillem de Bruijn 1767f72c4ac6SWillem de Bruijn if (!num_frags) 1768f72c4ac6SWillem de Bruijn goto release; 1769f72c4ac6SWillem de Bruijn 17703ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 17713ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 177202756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1773a6686f2fSShirley Ma if (!page) { 1774a6686f2fSShirley Ma while (head) { 177540dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1776a6686f2fSShirley Ma put_page(head); 1777a6686f2fSShirley Ma head = next; 1778a6686f2fSShirley Ma } 1779a6686f2fSShirley Ma return -ENOMEM; 1780a6686f2fSShirley Ma } 17813ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 17823ece7826SWillem de Bruijn head = page; 17833ece7826SWillem de Bruijn } 17843ece7826SWillem de Bruijn 17853ece7826SWillem de Bruijn page = head; 17863ece7826SWillem de Bruijn d_off = 0; 17873ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 17883ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17893ece7826SWillem de Bruijn u32 p_off, p_len, copied; 17903ece7826SWillem de Bruijn struct page *p; 17913ece7826SWillem de Bruijn u8 *vaddr; 1792c613c209SWillem de Bruijn 1793b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1794c613c209SWillem de Bruijn p, p_off, p_len, copied) { 17953ece7826SWillem de Bruijn u32 copy, done = 0; 1796c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 17973ece7826SWillem de Bruijn 17983ece7826SWillem de Bruijn while (done < p_len) { 17993ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 18003ece7826SWillem de Bruijn d_off = 0; 18013ece7826SWillem de Bruijn page = (struct page *)page_private(page); 18023ece7826SWillem de Bruijn } 18033ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 18043ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 18053ece7826SWillem de Bruijn vaddr + p_off + done, copy); 18063ece7826SWillem de Bruijn done += copy; 18073ece7826SWillem de Bruijn d_off += copy; 18083ece7826SWillem de Bruijn } 180951c56b00SEric Dumazet kunmap_atomic(vaddr); 1810c613c209SWillem de Bruijn } 1811a6686f2fSShirley Ma } 1812a6686f2fSShirley Ma 1813a6686f2fSShirley Ma /* skb frags release userspace buffers */ 181402756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1815a8605c60SIan Campbell skb_frag_unref(skb, i); 1816a6686f2fSShirley Ma 1817a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 18183ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 18193ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 182040dadff2SSunghan Suh head = (struct page *)page_private(head); 1821a6686f2fSShirley Ma } 18223ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 18233ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 182448c83012SMichael S. Tsirkin 1825b90ddd56SWillem de Bruijn release: 18261f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1827a6686f2fSShirley Ma return 0; 1828a6686f2fSShirley Ma } 1829dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1830a6686f2fSShirley Ma 1831e0053ec0SHerbert Xu /** 1832e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1833e0053ec0SHerbert Xu * @skb: buffer to clone 1834e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1835e0053ec0SHerbert Xu * 1836e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1837e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1838e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1839e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1840e0053ec0SHerbert Xu * 1841e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1842e0053ec0SHerbert Xu * %GFP_ATOMIC. 1843e0053ec0SHerbert Xu */ 1844e0053ec0SHerbert Xu 1845e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1846e0053ec0SHerbert Xu { 1847d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1848d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1849d0bf4a9eSEric Dumazet skb1); 18506ffe75ebSEric Dumazet struct sk_buff *n; 1851e0053ec0SHerbert Xu 185270008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1853a6686f2fSShirley Ma return NULL; 1854a6686f2fSShirley Ma 1855e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 18562638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 18576ffe75ebSEric Dumazet n = &fclones->skb2; 18582638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1859c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1860e0053ec0SHerbert Xu } else { 1861c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1862c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1863c93bdd0eSMel Gorman 1864025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask); 1865e0053ec0SHerbert Xu if (!n) 1866e0053ec0SHerbert Xu return NULL; 1867fe55f6d5SVegard Nossum 1868e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1869e0053ec0SHerbert Xu } 1870e0053ec0SHerbert Xu 1871e0053ec0SHerbert Xu return __skb_clone(n, skb); 18721da177e4SLinus Torvalds } 1873b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 18741da177e4SLinus Torvalds 1875b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1876f5b17294SPravin B Shelar { 1877030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1878030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1879030737bcSEric Dumazet skb->csum_start += off; 1880f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1881f5b17294SPravin B Shelar skb->transport_header += off; 1882f5b17294SPravin B Shelar skb->network_header += off; 1883f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1884f5b17294SPravin B Shelar skb->mac_header += off; 1885f5b17294SPravin B Shelar skb->inner_transport_header += off; 1886f5b17294SPravin B Shelar skb->inner_network_header += off; 1887aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1888f5b17294SPravin B Shelar } 1889b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1890f5b17294SPravin B Shelar 189108303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 18921da177e4SLinus Torvalds { 1893dec18810SHerbert Xu __copy_skb_header(new, old); 1894dec18810SHerbert Xu 18957967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 18967967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 18977967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 18981da177e4SLinus Torvalds } 189908303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 19001da177e4SLinus Torvalds 1901c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1902c93bdd0eSMel Gorman { 1903c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1904c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1905c93bdd0eSMel Gorman return 0; 1906c93bdd0eSMel Gorman } 1907c93bdd0eSMel Gorman 19081da177e4SLinus Torvalds /** 19091da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 19101da177e4SLinus Torvalds * @skb: buffer to copy 19111da177e4SLinus Torvalds * @gfp_mask: allocation priority 19121da177e4SLinus Torvalds * 19131da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 19141da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 19151da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 19161da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 19191da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 19201da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 19211da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 19221da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 19231da177e4SLinus Torvalds */ 19241da177e4SLinus Torvalds 1925dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 19261da177e4SLinus Torvalds { 19276602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1928ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1929c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1930c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 19316602cebbSEric Dumazet 19321da177e4SLinus Torvalds if (!n) 19331da177e4SLinus Torvalds return NULL; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* Set the data pointer */ 19361da177e4SLinus Torvalds skb_reserve(n, headerlen); 19371da177e4SLinus Torvalds /* Set the tail pointer and length */ 19381da177e4SLinus Torvalds skb_put(n, skb->len); 19391da177e4SLinus Torvalds 19409f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 19411da177e4SLinus Torvalds 194208303c18SIlya Lesokhin skb_copy_header(n, skb); 19431da177e4SLinus Torvalds return n; 19441da177e4SLinus Torvalds } 1945b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /** 1948bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 19491da177e4SLinus Torvalds * @skb: buffer to copy 1950117632e6SEric Dumazet * @headroom: headroom of new skb 19511da177e4SLinus Torvalds * @gfp_mask: allocation priority 1952bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1953bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1954bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 19551da177e4SLinus Torvalds * 19561da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 19571da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 19581da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 19591da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 19601da177e4SLinus Torvalds * or the pointer to the buffer on success. 19611da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 19621da177e4SLinus Torvalds */ 19631da177e4SLinus Torvalds 1964bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1965bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 19661da177e4SLinus Torvalds { 1967117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1968bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1969bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 19706602cebbSEric Dumazet 19711da177e4SLinus Torvalds if (!n) 19721da177e4SLinus Torvalds goto out; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* Set the data pointer */ 1975117632e6SEric Dumazet skb_reserve(n, headroom); 19761da177e4SLinus Torvalds /* Set the tail pointer and length */ 19771da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 19781da177e4SLinus Torvalds /* Copy the bytes */ 1979d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 19801da177e4SLinus Torvalds 198125f484a6SHerbert Xu n->truesize += skb->data_len; 19821da177e4SLinus Torvalds n->data_len = skb->data_len; 19831da177e4SLinus Torvalds n->len = skb->len; 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 19861da177e4SLinus Torvalds int i; 19871da177e4SLinus Torvalds 19881f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 19891f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 19901511022cSDan Carpenter kfree_skb(n); 19911511022cSDan Carpenter n = NULL; 1992a6686f2fSShirley Ma goto out; 1993a6686f2fSShirley Ma } 19941da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19951da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1996ea2ab693SIan Campbell skb_frag_ref(skb, i); 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 200121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 20021da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 20031da177e4SLinus Torvalds skb_clone_fraglist(n); 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds 200608303c18SIlya Lesokhin skb_copy_header(n, skb); 20071da177e4SLinus Torvalds out: 20081da177e4SLinus Torvalds return n; 20091da177e4SLinus Torvalds } 2010bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds /** 20131da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 20141da177e4SLinus Torvalds * @skb: buffer to reallocate 20151da177e4SLinus Torvalds * @nhead: room to add at head 20161da177e4SLinus Torvalds * @ntail: room to add at tail 20171da177e4SLinus Torvalds * @gfp_mask: allocation priority 20181da177e4SLinus Torvalds * 2019bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 2020bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 20211da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 20221da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 20231da177e4SLinus Torvalds * 20241da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20251da177e4SLinus Torvalds * reloaded after call to this function. 20261da177e4SLinus Torvalds */ 20271da177e4SLinus Torvalds 202886a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 2029dd0fc66fSAl Viro gfp_t gfp_mask) 20301da177e4SLinus Torvalds { 203112d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 203212d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 20331da177e4SLinus Torvalds long off; 2034158f323bSEric Dumazet u8 *data; 203512d6c1d3SKees Cook int i; 20361da177e4SLinus Torvalds 20374edd87adSHerbert Xu BUG_ON(nhead < 0); 20384edd87adSHerbert Xu 20399f77fad3STim Hansen BUG_ON(skb_shared(skb)); 20401da177e4SLinus Torvalds 2041753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 2042753f1ca4SPavel Begunkov 2043c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 2044c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 204512d6c1d3SKees Cook 20465c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 20471da177e4SLinus Torvalds if (!data) 20481da177e4SLinus Torvalds goto nodata; 204912d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 20526602cebbSEric Dumazet * optimized for the cases when header is void. 20536602cebbSEric Dumazet */ 20546602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 20556602cebbSEric Dumazet 20566602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 20576602cebbSEric Dumazet skb_shinfo(skb), 2058fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 20591da177e4SLinus Torvalds 20603e24591aSAlexander Duyck /* 20613e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 20623e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 20633e24591aSAlexander Duyck * be since all we did is relocate the values 20643e24591aSAlexander Duyck */ 20653e24591aSAlexander Duyck if (skb_cloned(skb)) { 206670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 2067a6686f2fSShirley Ma goto nofrags; 20681f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 2069c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 20701da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2071ea2ab693SIan Campbell skb_frag_ref(skb, i); 20721da177e4SLinus Torvalds 207321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 20741da177e4SLinus Torvalds skb_clone_fraglist(skb); 20751da177e4SLinus Torvalds 2076b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 20773e24591aSAlexander Duyck } else { 2078b07a2d97SJakub Kicinski skb_free_head(skb, false); 20791fd63041SEric Dumazet } 20801da177e4SLinus Torvalds off = (data + nhead) - skb->head; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds skb->head = data; 2083d3836f21SEric Dumazet skb->head_frag = 0; 20841da177e4SLinus Torvalds skb->data += off; 2085763087daSEric Dumazet 2086763087daSEric Dumazet skb_set_end_offset(skb, size); 20874305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 208856eb8882SPatrick McHardy off = nhead; 208956eb8882SPatrick McHardy #endif 209027a884dcSArnaldo Carvalho de Melo skb->tail += off; 2091b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 20921da177e4SLinus Torvalds skb->cloned = 0; 2093334a8132SPatrick McHardy skb->hdr_len = 0; 20941da177e4SLinus Torvalds skb->nohdr = 0; 20951da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 2096158f323bSEric Dumazet 2097de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 2098de8f3a83SDaniel Borkmann 2099158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 2100158f323bSEric Dumazet * For the moment, we really care of rx path, or 2101158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 2102158f323bSEric Dumazet */ 2103158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2104158f323bSEric Dumazet skb->truesize += size - osize; 2105158f323bSEric Dumazet 21061da177e4SLinus Torvalds return 0; 21071da177e4SLinus Torvalds 2108a6686f2fSShirley Ma nofrags: 2109bf9f1baaSEric Dumazet skb_kfree_head(data, size); 21101da177e4SLinus Torvalds nodata: 21111da177e4SLinus Torvalds return -ENOMEM; 21121da177e4SLinus Torvalds } 2113b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 21181da177e4SLinus Torvalds { 21191da177e4SLinus Torvalds struct sk_buff *skb2; 21201da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds if (delta <= 0) 21231da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 21241da177e4SLinus Torvalds else { 21251da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 21261da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 21271da177e4SLinus Torvalds GFP_ATOMIC)) { 21281da177e4SLinus Torvalds kfree_skb(skb2); 21291da177e4SLinus Torvalds skb2 = NULL; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds } 21321da177e4SLinus Torvalds return skb2; 21331da177e4SLinus Torvalds } 2134b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 21351da177e4SLinus Torvalds 2136880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */ 21372b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 21382b88cba5SEric Dumazet { 21392b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 21402b88cba5SEric Dumazet struct skb_shared_info *shinfo; 21412b88cba5SEric Dumazet int res; 21422b88cba5SEric Dumazet 21432b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 21442b88cba5SEric Dumazet saved_truesize = skb->truesize; 21452b88cba5SEric Dumazet 21462b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 21472b88cba5SEric Dumazet if (res) 21482b88cba5SEric Dumazet return res; 21492b88cba5SEric Dumazet 21502b88cba5SEric Dumazet skb->truesize = saved_truesize; 21512b88cba5SEric Dumazet 21522b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 21532b88cba5SEric Dumazet return 0; 21542b88cba5SEric Dumazet 2155880ce5f2SEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 2156880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value 2157880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). 2158880ce5f2SEric Dumazet */ 2159880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || 2160880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { 2161880ce5f2SEric Dumazet /* We think this path should not be taken. 2162880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case. 2163880ce5f2SEric Dumazet */ 2164880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", 2165880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb)); 2166880ce5f2SEric Dumazet WARN_ON_ONCE(1); 2167880ce5f2SEric Dumazet return 0; 2168880ce5f2SEric Dumazet } 2169880ce5f2SEric Dumazet #endif 2170880ce5f2SEric Dumazet 21712b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 21722b88cba5SEric Dumazet 21732b88cba5SEric Dumazet /* We are about to change back skb->end, 21742b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 21752b88cba5SEric Dumazet */ 21762b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 21772b88cba5SEric Dumazet shinfo, 21782b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 21792b88cba5SEric Dumazet 21802b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 21812b88cba5SEric Dumazet 21822b88cba5SEric Dumazet return 0; 21832b88cba5SEric Dumazet } 21842b88cba5SEric Dumazet 21851da177e4SLinus Torvalds /** 2186f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2187f1260ff1SVasily Averin * @skb: buffer to reallocate 2188f1260ff1SVasily Averin * @headroom: needed headroom 2189f1260ff1SVasily Averin * 2190f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2191f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2192f1260ff1SVasily Averin * and frees original skb in case of failures. 2193f1260ff1SVasily Averin * 2194f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2195f1260ff1SVasily Averin */ 2196f1260ff1SVasily Averin 2197f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2198f1260ff1SVasily Averin { 2199f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 22007f678defSVasily Averin int osize = skb_end_offset(skb); 22017f678defSVasily Averin struct sock *sk = skb->sk; 2202f1260ff1SVasily Averin 2203f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2204f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2205f1260ff1SVasily Averin return skb; 2206f1260ff1SVasily Averin 22077f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 22087f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 22097f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2210f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2211f1260ff1SVasily Averin 22127f678defSVasily Averin if (unlikely(!nskb)) 22137f678defSVasily Averin goto fail; 22147f678defSVasily Averin 22157f678defSVasily Averin if (sk) 22167f678defSVasily Averin skb_set_owner_w(nskb, sk); 2217f1260ff1SVasily Averin consume_skb(skb); 2218f1260ff1SVasily Averin skb = nskb; 2219f1260ff1SVasily Averin } 22207f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 22217f678defSVasily Averin goto fail; 22227f678defSVasily Averin 22237f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 22247f678defSVasily Averin delta = skb_end_offset(skb) - osize; 22257f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 22267f678defSVasily Averin skb->truesize += delta; 2227f1260ff1SVasily Averin } 2228f1260ff1SVasily Averin return skb; 22297f678defSVasily Averin 22307f678defSVasily Averin fail: 22317f678defSVasily Averin kfree_skb(skb); 22327f678defSVasily Averin return NULL; 2233f1260ff1SVasily Averin } 2234f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2235f1260ff1SVasily Averin 2236f1260ff1SVasily Averin /** 22371da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 22381da177e4SLinus Torvalds * @skb: buffer to copy 22391da177e4SLinus Torvalds * @newheadroom: new free bytes at head 22401da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 22411da177e4SLinus Torvalds * @gfp_mask: allocation priority 22421da177e4SLinus Torvalds * 22431da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 22441da177e4SLinus Torvalds * allocate additional space. 22451da177e4SLinus Torvalds * 22461da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 22471da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 22481da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 22491da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 22501da177e4SLinus Torvalds * 22511da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 22521da177e4SLinus Torvalds * is called from an interrupt. 22531da177e4SLinus Torvalds */ 22541da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 225586a76cafSVictor Fusco int newheadroom, int newtailroom, 2256dd0fc66fSAl Viro gfp_t gfp_mask) 22571da177e4SLinus Torvalds { 22581da177e4SLinus Torvalds /* 22591da177e4SLinus Torvalds * Allocate the copy buffer 22601da177e4SLinus Torvalds */ 2261c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2262c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2263c93bdd0eSMel Gorman NUMA_NO_NODE); 2264efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 22651da177e4SLinus Torvalds int head_copy_len, head_copy_off; 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds if (!n) 22681da177e4SLinus Torvalds return NULL; 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds skb_reserve(n, newheadroom); 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds /* Set the tail pointer and length */ 22731da177e4SLinus Torvalds skb_put(n, skb->len); 22741da177e4SLinus Torvalds 2275efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 22761da177e4SLinus Torvalds head_copy_off = 0; 22771da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 22781da177e4SLinus Torvalds head_copy_len = newheadroom; 22791da177e4SLinus Torvalds else 22801da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds /* Copy the linear header and data. */ 22839f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 22849f77fad3STim Hansen skb->len + head_copy_len)); 22851da177e4SLinus Torvalds 228608303c18SIlya Lesokhin skb_copy_header(n, skb); 22871da177e4SLinus Torvalds 2288030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2289efd1e8d5SPatrick McHardy 22901da177e4SLinus Torvalds return n; 22911da177e4SLinus Torvalds } 2292b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds /** 2295cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 22961da177e4SLinus Torvalds * @skb: buffer to pad 22971da177e4SLinus Torvalds * @pad: space to pad 2298cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 22991da177e4SLinus Torvalds * 23001da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 23011da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 23021da177e4SLinus Torvalds * beyond the buffer end onto the wire. 23031da177e4SLinus Torvalds * 2304cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2305cd0a137aSFlorian Fainelli * if @free_on_error is true. 23061da177e4SLinus Torvalds */ 23071da177e4SLinus Torvalds 2308cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 23091da177e4SLinus Torvalds { 23105b057c6bSHerbert Xu int err; 23115b057c6bSHerbert Xu int ntail; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 23145b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 23151da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 23165b057c6bSHerbert Xu return 0; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23194305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 23205b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 23215b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 23225b057c6bSHerbert Xu if (unlikely(err)) 23235b057c6bSHerbert Xu goto free_skb; 23245b057c6bSHerbert Xu } 23255b057c6bSHerbert Xu 23265b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 23275b057c6bSHerbert Xu * to be audited. 23285b057c6bSHerbert Xu */ 23295b057c6bSHerbert Xu err = skb_linearize(skb); 23305b057c6bSHerbert Xu if (unlikely(err)) 23315b057c6bSHerbert Xu goto free_skb; 23325b057c6bSHerbert Xu 23335b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 23345b057c6bSHerbert Xu return 0; 23355b057c6bSHerbert Xu 23365b057c6bSHerbert Xu free_skb: 2337cd0a137aSFlorian Fainelli if (free_on_error) 23381da177e4SLinus Torvalds kfree_skb(skb); 23395b057c6bSHerbert Xu return err; 23401da177e4SLinus Torvalds } 2341cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 23421da177e4SLinus Torvalds 23430dde3e16SIlpo Järvinen /** 23440c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 23450c7ddf36SMathias Krause * @skb: start of the buffer to use 23460c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 23470c7ddf36SMathias Krause * @len: amount of data to add 23480c7ddf36SMathias Krause * 23490c7ddf36SMathias Krause * This function extends the used data area of the potentially 23500c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 23510c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 23520c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 23530c7ddf36SMathias Krause * returned. 23540c7ddf36SMathias Krause */ 23550c7ddf36SMathias Krause 23564df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 23570c7ddf36SMathias Krause { 23580c7ddf36SMathias Krause if (tail != skb) { 23590c7ddf36SMathias Krause skb->data_len += len; 23600c7ddf36SMathias Krause skb->len += len; 23610c7ddf36SMathias Krause } 23620c7ddf36SMathias Krause return skb_put(tail, len); 23630c7ddf36SMathias Krause } 23640c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 23650c7ddf36SMathias Krause 23660c7ddf36SMathias Krause /** 23670dde3e16SIlpo Järvinen * skb_put - add data to a buffer 23680dde3e16SIlpo Järvinen * @skb: buffer to use 23690dde3e16SIlpo Järvinen * @len: amount of data to add 23700dde3e16SIlpo Järvinen * 23710dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 23720dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 23730dde3e16SIlpo Järvinen * first byte of the extra data is returned. 23740dde3e16SIlpo Järvinen */ 23754df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 23760dde3e16SIlpo Järvinen { 23774df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 23780dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 23790dde3e16SIlpo Järvinen skb->tail += len; 23800dde3e16SIlpo Järvinen skb->len += len; 23810dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 23820dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 23830dde3e16SIlpo Järvinen return tmp; 23840dde3e16SIlpo Järvinen } 23850dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 23860dde3e16SIlpo Järvinen 23876be8ac2fSIlpo Järvinen /** 2388c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2389c2aa270aSIlpo Järvinen * @skb: buffer to use 2390c2aa270aSIlpo Järvinen * @len: amount of data to add 2391c2aa270aSIlpo Järvinen * 2392c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2393c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2394c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2395c2aa270aSIlpo Järvinen */ 2396d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2397c2aa270aSIlpo Järvinen { 2398c2aa270aSIlpo Järvinen skb->data -= len; 2399c2aa270aSIlpo Järvinen skb->len += len; 2400c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2401c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2402c2aa270aSIlpo Järvinen return skb->data; 2403c2aa270aSIlpo Järvinen } 2404c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2405c2aa270aSIlpo Järvinen 2406c2aa270aSIlpo Järvinen /** 24076be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 24086be8ac2fSIlpo Järvinen * @skb: buffer to use 24096be8ac2fSIlpo Järvinen * @len: amount of data to remove 24106be8ac2fSIlpo Järvinen * 24116be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 24126be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 24136be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 24146be8ac2fSIlpo Järvinen * the old data. 24156be8ac2fSIlpo Järvinen */ 2416af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 24176be8ac2fSIlpo Järvinen { 241847d29646SDavid S. Miller return skb_pull_inline(skb, len); 24196be8ac2fSIlpo Järvinen } 24206be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 24216be8ac2fSIlpo Järvinen 2422419ae74eSIlpo Järvinen /** 242313244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 242413244cccSLuiz Augusto von Dentz * original position. 242513244cccSLuiz Augusto von Dentz * @skb: buffer to use 242613244cccSLuiz Augusto von Dentz * @len: amount of data to remove 242713244cccSLuiz Augusto von Dentz * 242813244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 242913244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 243013244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 243113244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 243213244cccSLuiz Augusto von Dentz */ 243313244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 243413244cccSLuiz Augusto von Dentz { 243513244cccSLuiz Augusto von Dentz void *data = skb->data; 243613244cccSLuiz Augusto von Dentz 243713244cccSLuiz Augusto von Dentz if (skb->len < len) 243813244cccSLuiz Augusto von Dentz return NULL; 243913244cccSLuiz Augusto von Dentz 244013244cccSLuiz Augusto von Dentz skb_pull(skb, len); 244113244cccSLuiz Augusto von Dentz 244213244cccSLuiz Augusto von Dentz return data; 244313244cccSLuiz Augusto von Dentz } 244413244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 244513244cccSLuiz Augusto von Dentz 244613244cccSLuiz Augusto von Dentz /** 2447419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2448419ae74eSIlpo Järvinen * @skb: buffer to alter 2449419ae74eSIlpo Järvinen * @len: new length 2450419ae74eSIlpo Järvinen * 2451419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2452419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2453419ae74eSIlpo Järvinen * The skb must be linear. 2454419ae74eSIlpo Järvinen */ 2455419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2456419ae74eSIlpo Järvinen { 2457419ae74eSIlpo Järvinen if (skb->len > len) 2458419ae74eSIlpo Järvinen __skb_trim(skb, len); 2459419ae74eSIlpo Järvinen } 2460419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2461419ae74eSIlpo Järvinen 24623cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 24631da177e4SLinus Torvalds */ 24641da177e4SLinus Torvalds 24653cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 24661da177e4SLinus Torvalds { 246727b437c8SHerbert Xu struct sk_buff **fragp; 246827b437c8SHerbert Xu struct sk_buff *frag; 24691da177e4SLinus Torvalds int offset = skb_headlen(skb); 24701da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 24711da177e4SLinus Torvalds int i; 247227b437c8SHerbert Xu int err; 247327b437c8SHerbert Xu 247427b437c8SHerbert Xu if (skb_cloned(skb) && 247527b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 247627b437c8SHerbert Xu return err; 24771da177e4SLinus Torvalds 2478f4d26fb3SHerbert Xu i = 0; 2479f4d26fb3SHerbert Xu if (offset >= len) 2480f4d26fb3SHerbert Xu goto drop_pages; 2481f4d26fb3SHerbert Xu 2482f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 24839e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 248427b437c8SHerbert Xu 248527b437c8SHerbert Xu if (end < len) { 24861da177e4SLinus Torvalds offset = end; 248727b437c8SHerbert Xu continue; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 24909e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 249127b437c8SHerbert Xu 2492f4d26fb3SHerbert Xu drop_pages: 249327b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 249427b437c8SHerbert Xu 249527b437c8SHerbert Xu for (; i < nfrags; i++) 2496ea2ab693SIan Campbell skb_frag_unref(skb, i); 249727b437c8SHerbert Xu 249821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 249927b437c8SHerbert Xu skb_drop_fraglist(skb); 2500f4d26fb3SHerbert Xu goto done; 250127b437c8SHerbert Xu } 250227b437c8SHerbert Xu 250327b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 250427b437c8SHerbert Xu fragp = &frag->next) { 250527b437c8SHerbert Xu int end = offset + frag->len; 250627b437c8SHerbert Xu 250727b437c8SHerbert Xu if (skb_shared(frag)) { 250827b437c8SHerbert Xu struct sk_buff *nfrag; 250927b437c8SHerbert Xu 251027b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 251127b437c8SHerbert Xu if (unlikely(!nfrag)) 251227b437c8SHerbert Xu return -ENOMEM; 251327b437c8SHerbert Xu 251427b437c8SHerbert Xu nfrag->next = frag->next; 251585bb2a60SEric Dumazet consume_skb(frag); 251627b437c8SHerbert Xu frag = nfrag; 251727b437c8SHerbert Xu *fragp = frag; 251827b437c8SHerbert Xu } 251927b437c8SHerbert Xu 252027b437c8SHerbert Xu if (end < len) { 252127b437c8SHerbert Xu offset = end; 252227b437c8SHerbert Xu continue; 252327b437c8SHerbert Xu } 252427b437c8SHerbert Xu 252527b437c8SHerbert Xu if (end > len && 252627b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 252727b437c8SHerbert Xu return err; 252827b437c8SHerbert Xu 252927b437c8SHerbert Xu if (frag->next) 253027b437c8SHerbert Xu skb_drop_list(&frag->next); 253127b437c8SHerbert Xu break; 253227b437c8SHerbert Xu } 253327b437c8SHerbert Xu 2534f4d26fb3SHerbert Xu done: 253527b437c8SHerbert Xu if (len > skb_headlen(skb)) { 25361da177e4SLinus Torvalds skb->data_len -= skb->len - len; 25371da177e4SLinus Torvalds skb->len = len; 25381da177e4SLinus Torvalds } else { 25391da177e4SLinus Torvalds skb->len = len; 25401da177e4SLinus Torvalds skb->data_len = 0; 254127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 2544c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2545c21b48ccSEric Dumazet skb_condense(skb); 25461da177e4SLinus Torvalds return 0; 25471da177e4SLinus Torvalds } 2548b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 25491da177e4SLinus Torvalds 255088078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 255188078d98SEric Dumazet */ 255288078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 255388078d98SEric Dumazet { 255488078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 255588078d98SEric Dumazet int delta = skb->len - len; 255688078d98SEric Dumazet 2557d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2558d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2559d55bef50SDimitris Michailidis len); 256054970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 256154970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 256254970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 256354970a2fSVasily Averin 256454970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 256554970a2fSVasily Averin return -EINVAL; 256688078d98SEric Dumazet } 256788078d98SEric Dumazet return __pskb_trim(skb, len); 256888078d98SEric Dumazet } 256988078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 257088078d98SEric Dumazet 25711da177e4SLinus Torvalds /** 25721da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 25731da177e4SLinus Torvalds * @skb: buffer to reallocate 25741da177e4SLinus Torvalds * @delta: number of bytes to advance tail 25751da177e4SLinus Torvalds * 25761da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 25771da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 25781da177e4SLinus Torvalds * data from fragmented part. 25791da177e4SLinus Torvalds * 25801da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 25811da177e4SLinus Torvalds * 25821da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 25831da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 25841da177e4SLinus Torvalds * 25851da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 25861da177e4SLinus Torvalds * reloaded after call to this function. 25871da177e4SLinus Torvalds */ 25881da177e4SLinus Torvalds 25891da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 25901da177e4SLinus Torvalds * when it is necessary. 25911da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 25921da177e4SLinus Torvalds * 2. It may change skb pointers. 25931da177e4SLinus Torvalds * 25941da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 25951da177e4SLinus Torvalds */ 2596af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 25971da177e4SLinus Torvalds { 25981da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 25991da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 26001da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 26011da177e4SLinus Torvalds */ 26024305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 26051da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 26061da177e4SLinus Torvalds GFP_ATOMIC)) 26071da177e4SLinus Torvalds return NULL; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds 26109f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 26119f77fad3STim Hansen skb_tail_pointer(skb), delta)); 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 26141da177e4SLinus Torvalds * size of pulled pages. Superb. 26151da177e4SLinus Torvalds */ 261621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 26171da177e4SLinus Torvalds goto pull_pages; 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 26201da177e4SLinus Torvalds eat = delta; 26211da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26229e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26239e903e08SEric Dumazet 26249e903e08SEric Dumazet if (size >= eat) 26251da177e4SLinus Torvalds goto pull_pages; 26269e903e08SEric Dumazet eat -= size; 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 263009001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 26311da177e4SLinus Torvalds * but taking into account that pulling is expected to 26321da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 26331da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 26341da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 26351da177e4SLinus Torvalds */ 26361da177e4SLinus Torvalds if (eat) { 26371da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 26381da177e4SLinus Torvalds struct sk_buff *clone = NULL; 26391da177e4SLinus Torvalds struct sk_buff *insp = NULL; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds do { 26421da177e4SLinus Torvalds if (list->len <= eat) { 26431da177e4SLinus Torvalds /* Eaten as whole. */ 26441da177e4SLinus Torvalds eat -= list->len; 26451da177e4SLinus Torvalds list = list->next; 26461da177e4SLinus Torvalds insp = list; 26471da177e4SLinus Torvalds } else { 26481da177e4SLinus Torvalds /* Eaten partially. */ 26492d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 26502d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 26512d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds if (skb_shared(list)) { 26541da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 26551da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 26561da177e4SLinus Torvalds if (!clone) 26571da177e4SLinus Torvalds return NULL; 26581da177e4SLinus Torvalds insp = list->next; 26591da177e4SLinus Torvalds list = clone; 26601da177e4SLinus Torvalds } else { 26611da177e4SLinus Torvalds /* This may be pulled without 26621da177e4SLinus Torvalds * problems. */ 26631da177e4SLinus Torvalds insp = list; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 26661da177e4SLinus Torvalds kfree_skb(clone); 26671da177e4SLinus Torvalds return NULL; 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds break; 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds } while (eat); 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds /* Free pulled out fragments. */ 26741da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 26751da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2676ef527f96SEric Dumazet consume_skb(list); 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds /* And insert new clone at head. */ 26791da177e4SLinus Torvalds if (clone) { 26801da177e4SLinus Torvalds clone->next = list; 26811da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds pull_pages: 26871da177e4SLinus Torvalds eat = delta; 26881da177e4SLinus Torvalds k = 0; 26891da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26909e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26919e903e08SEric Dumazet 26929e903e08SEric Dumazet if (size <= eat) { 2693ea2ab693SIan Campbell skb_frag_unref(skb, i); 26949e903e08SEric Dumazet eat -= size; 26951da177e4SLinus Torvalds } else { 2696b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2697b54c9d5bSJonathan Lemon 2698b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 26991da177e4SLinus Torvalds if (eat) { 2700b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2701b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 27023ccc6c6fSlinzhang if (!i) 27033ccc6c6fSlinzhang goto end; 27041da177e4SLinus Torvalds eat = 0; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds k++; 27071da177e4SLinus Torvalds } 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 27101da177e4SLinus Torvalds 27113ccc6c6fSlinzhang end: 27121da177e4SLinus Torvalds skb->tail += delta; 27131da177e4SLinus Torvalds skb->data_len -= delta; 27141da177e4SLinus Torvalds 27151f8b977aSWillem de Bruijn if (!skb->data_len) 27161f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 27171f8b977aSWillem de Bruijn 271827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 27191da177e4SLinus Torvalds } 2720b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 27211da177e4SLinus Torvalds 272222019b17SEric Dumazet /** 272322019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 272422019b17SEric Dumazet * @skb: source skb 272522019b17SEric Dumazet * @offset: offset in source 272622019b17SEric Dumazet * @to: destination buffer 272722019b17SEric Dumazet * @len: number of bytes to copy 272822019b17SEric Dumazet * 272922019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 273022019b17SEric Dumazet * destination buffer. 273122019b17SEric Dumazet * 273222019b17SEric Dumazet * CAUTION ! : 273322019b17SEric Dumazet * If its prototype is ever changed, 273422019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 273522019b17SEric Dumazet * since it is called from BPF assembly code. 273622019b17SEric Dumazet */ 27371da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 27381da177e4SLinus Torvalds { 27391a028e50SDavid S. Miller int start = skb_headlen(skb); 2740fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2741fbb398a8SDavid S. Miller int i, copy; 27421da177e4SLinus Torvalds 27431da177e4SLinus Torvalds if (offset > (int)skb->len - len) 27441da177e4SLinus Torvalds goto fault; 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds /* Copy header. */ 27471a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 27481da177e4SLinus Torvalds if (copy > len) 27491da177e4SLinus Torvalds copy = len; 2750d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 27511da177e4SLinus Torvalds if ((len -= copy) == 0) 27521da177e4SLinus Torvalds return 0; 27531da177e4SLinus Torvalds offset += copy; 27541da177e4SLinus Torvalds to += copy; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27581a028e50SDavid S. Miller int end; 275951c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 27601da177e4SLinus Torvalds 2761547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27621a028e50SDavid S. Miller 276351c56b00SEric Dumazet end = start + skb_frag_size(f); 27641da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2765c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2766c613c209SWillem de Bruijn struct page *p; 27671da177e4SLinus Torvalds u8 *vaddr; 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds if (copy > len) 27701da177e4SLinus Torvalds copy = len; 27711da177e4SLinus Torvalds 2772c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2773b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2774c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2775c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2776c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 277751c56b00SEric Dumazet kunmap_atomic(vaddr); 2778c613c209SWillem de Bruijn } 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds if ((len -= copy) == 0) 27811da177e4SLinus Torvalds return 0; 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) { 27891a028e50SDavid S. Miller int end; 27901da177e4SLinus Torvalds 2791547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27921a028e50SDavid S. Miller 2793fbb398a8SDavid S. Miller end = start + frag_iter->len; 27941da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 27951da177e4SLinus Torvalds if (copy > len) 27961da177e4SLinus Torvalds copy = len; 2797fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 27981da177e4SLinus Torvalds goto fault; 27991da177e4SLinus Torvalds if ((len -= copy) == 0) 28001da177e4SLinus Torvalds return 0; 28011da177e4SLinus Torvalds offset += copy; 28021da177e4SLinus Torvalds to += copy; 28031da177e4SLinus Torvalds } 28041a028e50SDavid S. Miller start = end; 28051da177e4SLinus Torvalds } 2806a6686f2fSShirley Ma 28071da177e4SLinus Torvalds if (!len) 28081da177e4SLinus Torvalds return 0; 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds fault: 28111da177e4SLinus Torvalds return -EFAULT; 28121da177e4SLinus Torvalds } 2813b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 28141da177e4SLinus Torvalds 28159c55e01cSJens Axboe /* 28169c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 28179c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 28189c55e01cSJens Axboe */ 28199c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 28209c55e01cSJens Axboe { 28218b9d3728SJarek Poplawski put_page(spd->pages[i]); 28228b9d3728SJarek Poplawski } 28239c55e01cSJens Axboe 2824a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 28254fb66994SJarek Poplawski unsigned int *offset, 282618aafc62SEric Dumazet struct sock *sk) 28278b9d3728SJarek Poplawski { 28285640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 28298b9d3728SJarek Poplawski 28305640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 28318b9d3728SJarek Poplawski return NULL; 28324fb66994SJarek Poplawski 28335640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 28344fb66994SJarek Poplawski 28355640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 28365640f768SEric Dumazet page_address(page) + *offset, *len); 28375640f768SEric Dumazet *offset = pfrag->offset; 28385640f768SEric Dumazet pfrag->offset += *len; 28394fb66994SJarek Poplawski 28405640f768SEric Dumazet return pfrag->page; 28419c55e01cSJens Axboe } 28429c55e01cSJens Axboe 284341c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 284441c73a0dSEric Dumazet struct page *page, 284541c73a0dSEric Dumazet unsigned int offset) 284641c73a0dSEric Dumazet { 284741c73a0dSEric Dumazet return spd->nr_pages && 284841c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 284941c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 285041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 285141c73a0dSEric Dumazet } 285241c73a0dSEric Dumazet 28539c55e01cSJens Axboe /* 28549c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 28559c55e01cSJens Axboe */ 2856a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 285735f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 28584fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 285918aafc62SEric Dumazet bool linear, 28607a67e56fSJarek Poplawski struct sock *sk) 28619c55e01cSJens Axboe { 286241c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2863a108d5f3SDavid S. Miller return true; 28649c55e01cSJens Axboe 28658b9d3728SJarek Poplawski if (linear) { 286618aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 28678b9d3728SJarek Poplawski if (!page) 2868a108d5f3SDavid S. Miller return true; 286941c73a0dSEric Dumazet } 287041c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 287141c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2872a108d5f3SDavid S. Miller return false; 287341c73a0dSEric Dumazet } 28748b9d3728SJarek Poplawski get_page(page); 28759c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 28764fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 28779c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 28789c55e01cSJens Axboe spd->nr_pages++; 28798b9d3728SJarek Poplawski 2880a108d5f3SDavid S. Miller return false; 28819c55e01cSJens Axboe } 28829c55e01cSJens Axboe 2883a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 28842870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 288518aafc62SEric Dumazet unsigned int *len, 2886d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 288735f3d14dSJens Axboe struct sock *sk, 288835f3d14dSJens Axboe struct pipe_inode_info *pipe) 28899c55e01cSJens Axboe { 28902870c43dSOctavian Purdila if (!*len) 2891a108d5f3SDavid S. Miller return true; 28929c55e01cSJens Axboe 28932870c43dSOctavian Purdila /* skip this segment if already processed */ 28942870c43dSOctavian Purdila if (*off >= plen) { 28952870c43dSOctavian Purdila *off -= plen; 2896a108d5f3SDavid S. Miller return false; 28972870c43dSOctavian Purdila } 28982870c43dSOctavian Purdila 28992870c43dSOctavian Purdila /* ignore any bits we already processed */ 29009ca1b22dSEric Dumazet poff += *off; 29019ca1b22dSEric Dumazet plen -= *off; 29022870c43dSOctavian Purdila *off = 0; 29032870c43dSOctavian Purdila 290418aafc62SEric Dumazet do { 290518aafc62SEric Dumazet unsigned int flen = min(*len, plen); 29062870c43dSOctavian Purdila 290718aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 290818aafc62SEric Dumazet linear, sk)) 2909a108d5f3SDavid S. Miller return true; 291018aafc62SEric Dumazet poff += flen; 291118aafc62SEric Dumazet plen -= flen; 29122870c43dSOctavian Purdila *len -= flen; 291318aafc62SEric Dumazet } while (*len && plen); 29142870c43dSOctavian Purdila 2915a108d5f3SDavid S. Miller return false; 2916db43a282SOctavian Purdila } 29179c55e01cSJens Axboe 29189c55e01cSJens Axboe /* 2919a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 29202870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 29219c55e01cSJens Axboe */ 2922a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 292335f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 292435f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 29252870c43dSOctavian Purdila { 29262870c43dSOctavian Purdila int seg; 2927fa9835e5STom Herbert struct sk_buff *iter; 29289c55e01cSJens Axboe 29291d0c0b32SEric Dumazet /* map the linear part : 29302996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 29312996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 29322996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 29339c55e01cSJens Axboe */ 29342870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 29352870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 29362870c43dSOctavian Purdila skb_headlen(skb), 293718aafc62SEric Dumazet offset, len, spd, 29383a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 29391d0c0b32SEric Dumazet sk, pipe)) 2940a108d5f3SDavid S. Miller return true; 29419c55e01cSJens Axboe 29429c55e01cSJens Axboe /* 29439c55e01cSJens Axboe * then map the fragments 29449c55e01cSJens Axboe */ 29459c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 29469c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 29479c55e01cSJens Axboe 2948ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2949b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 295018aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2951a108d5f3SDavid S. Miller return true; 29529c55e01cSJens Axboe } 29539c55e01cSJens Axboe 2954fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2955fa9835e5STom Herbert if (*offset >= iter->len) { 2956fa9835e5STom Herbert *offset -= iter->len; 2957fa9835e5STom Herbert continue; 2958fa9835e5STom Herbert } 2959fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2960fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2961fa9835e5STom Herbert * case. 2962fa9835e5STom Herbert */ 2963fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2964fa9835e5STom Herbert return true; 2965fa9835e5STom Herbert } 2966fa9835e5STom Herbert 2967a108d5f3SDavid S. Miller return false; 29689c55e01cSJens Axboe } 29699c55e01cSJens Axboe 29709c55e01cSJens Axboe /* 29719c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2972fa9835e5STom Herbert * the fragments, and the frag list. 29739c55e01cSJens Axboe */ 2974a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 29759c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 297625869262SAl Viro unsigned int flags) 29779c55e01cSJens Axboe { 297841c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 297941c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 29809c55e01cSJens Axboe struct splice_pipe_desc spd = { 29819c55e01cSJens Axboe .pages = pages, 29829c55e01cSJens Axboe .partial = partial, 2983047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 298428a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 29859c55e01cSJens Axboe .spd_release = sock_spd_release, 29869c55e01cSJens Axboe }; 298735f3d14dSJens Axboe int ret = 0; 298835f3d14dSJens Axboe 2989fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 29909c55e01cSJens Axboe 2991a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 299225869262SAl Viro ret = splice_to_pipe(pipe, &spd); 29939c55e01cSJens Axboe 299435f3d14dSJens Axboe return ret; 29959c55e01cSJens Axboe } 29962b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 29979c55e01cSJens Axboe 29980739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 29990739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 30000739cd28SCong Wang { 30010739cd28SCong Wang struct socket *sock = sk->sk_socket; 30020739cd28SCong Wang 30030739cd28SCong Wang if (!sock) 30040739cd28SCong Wang return -EINVAL; 30050739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 30060739cd28SCong Wang } 30070739cd28SCong Wang 30080739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 30090739cd28SCong Wang size_t size, int flags) 30100739cd28SCong Wang { 30110739cd28SCong Wang struct socket *sock = sk->sk_socket; 30120739cd28SCong Wang 30130739cd28SCong Wang if (!sock) 30140739cd28SCong Wang return -EINVAL; 30150739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 30160739cd28SCong Wang } 30170739cd28SCong Wang 30180739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 30190739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 30200739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 30210739cd28SCong Wang size_t size, int flags); 30220739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 30230739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 302420bf50deSTom Herbert { 302520bf50deSTom Herbert unsigned int orig_len = len; 302620bf50deSTom Herbert struct sk_buff *head = skb; 302720bf50deSTom Herbert unsigned short fragidx; 302820bf50deSTom Herbert int slen, ret; 302920bf50deSTom Herbert 303020bf50deSTom Herbert do_frag_list: 303120bf50deSTom Herbert 303220bf50deSTom Herbert /* Deal with head data */ 303320bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 303420bf50deSTom Herbert struct kvec kv; 303520bf50deSTom Herbert struct msghdr msg; 303620bf50deSTom Herbert 303720bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 303820bf50deSTom Herbert kv.iov_base = skb->data + offset; 3039db5980d8SJohn Fastabend kv.iov_len = slen; 304020bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 3041bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 304220bf50deSTom Herbert 30430739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 30440739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 304520bf50deSTom Herbert if (ret <= 0) 304620bf50deSTom Herbert goto error; 304720bf50deSTom Herbert 304820bf50deSTom Herbert offset += ret; 304920bf50deSTom Herbert len -= ret; 305020bf50deSTom Herbert } 305120bf50deSTom Herbert 305220bf50deSTom Herbert /* All the data was skb head? */ 305320bf50deSTom Herbert if (!len) 305420bf50deSTom Herbert goto out; 305520bf50deSTom Herbert 305620bf50deSTom Herbert /* Make offset relative to start of frags */ 305720bf50deSTom Herbert offset -= skb_headlen(skb); 305820bf50deSTom Herbert 305920bf50deSTom Herbert /* Find where we are in frag list */ 306020bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 306120bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 306220bf50deSTom Herbert 3063d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 306420bf50deSTom Herbert break; 306520bf50deSTom Herbert 3066d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 306720bf50deSTom Herbert } 306820bf50deSTom Herbert 306920bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 307020bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 307120bf50deSTom Herbert 3072d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 307320bf50deSTom Herbert 307420bf50deSTom Herbert while (slen) { 30750739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 30760739cd28SCong Wang sendpage_unlocked, sk, 30770739cd28SCong Wang skb_frag_page(frag), 3078b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 307920bf50deSTom Herbert slen, MSG_DONTWAIT); 308020bf50deSTom Herbert if (ret <= 0) 308120bf50deSTom Herbert goto error; 308220bf50deSTom Herbert 308320bf50deSTom Herbert len -= ret; 308420bf50deSTom Herbert offset += ret; 308520bf50deSTom Herbert slen -= ret; 308620bf50deSTom Herbert } 308720bf50deSTom Herbert 308820bf50deSTom Herbert offset = 0; 308920bf50deSTom Herbert } 309020bf50deSTom Herbert 309120bf50deSTom Herbert if (len) { 309220bf50deSTom Herbert /* Process any frag lists */ 309320bf50deSTom Herbert 309420bf50deSTom Herbert if (skb == head) { 309520bf50deSTom Herbert if (skb_has_frag_list(skb)) { 309620bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 309720bf50deSTom Herbert goto do_frag_list; 309820bf50deSTom Herbert } 309920bf50deSTom Herbert } else if (skb->next) { 310020bf50deSTom Herbert skb = skb->next; 310120bf50deSTom Herbert goto do_frag_list; 310220bf50deSTom Herbert } 310320bf50deSTom Herbert } 310420bf50deSTom Herbert 310520bf50deSTom Herbert out: 310620bf50deSTom Herbert return orig_len - len; 310720bf50deSTom Herbert 310820bf50deSTom Herbert error: 310920bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 311020bf50deSTom Herbert } 31110739cd28SCong Wang 31120739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 31130739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 31140739cd28SCong Wang int len) 31150739cd28SCong Wang { 31160739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 31170739cd28SCong Wang kernel_sendpage_locked); 31180739cd28SCong Wang } 311920bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 312020bf50deSTom Herbert 31210739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 31220739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 31230739cd28SCong Wang { 31240739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 31250739cd28SCong Wang sendpage_unlocked); 31260739cd28SCong Wang } 31270739cd28SCong Wang 3128357b40a1SHerbert Xu /** 3129357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3130357b40a1SHerbert Xu * @skb: destination buffer 3131357b40a1SHerbert Xu * @offset: offset in destination 3132357b40a1SHerbert Xu * @from: source buffer 3133357b40a1SHerbert Xu * @len: number of bytes to copy 3134357b40a1SHerbert Xu * 3135357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3136357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3137357b40a1SHerbert Xu * traversing fragment lists and such. 3138357b40a1SHerbert Xu */ 3139357b40a1SHerbert Xu 31400c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3141357b40a1SHerbert Xu { 31421a028e50SDavid S. Miller int start = skb_headlen(skb); 3143fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3144fbb398a8SDavid S. Miller int i, copy; 3145357b40a1SHerbert Xu 3146357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3147357b40a1SHerbert Xu goto fault; 3148357b40a1SHerbert Xu 31491a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3150357b40a1SHerbert Xu if (copy > len) 3151357b40a1SHerbert Xu copy = len; 315227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3153357b40a1SHerbert Xu if ((len -= copy) == 0) 3154357b40a1SHerbert Xu return 0; 3155357b40a1SHerbert Xu offset += copy; 3156357b40a1SHerbert Xu from += copy; 3157357b40a1SHerbert Xu } 3158357b40a1SHerbert Xu 3159357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3160357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31611a028e50SDavid S. Miller int end; 3162357b40a1SHerbert Xu 3163547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31641a028e50SDavid S. Miller 31659e903e08SEric Dumazet end = start + skb_frag_size(frag); 3166357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3167c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3168c613c209SWillem de Bruijn struct page *p; 3169357b40a1SHerbert Xu u8 *vaddr; 3170357b40a1SHerbert Xu 3171357b40a1SHerbert Xu if (copy > len) 3172357b40a1SHerbert Xu copy = len; 3173357b40a1SHerbert Xu 3174c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3175b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3176c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3177c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3178c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 317951c56b00SEric Dumazet kunmap_atomic(vaddr); 3180c613c209SWillem de Bruijn } 3181357b40a1SHerbert Xu 3182357b40a1SHerbert Xu if ((len -= copy) == 0) 3183357b40a1SHerbert Xu return 0; 3184357b40a1SHerbert Xu offset += copy; 3185357b40a1SHerbert Xu from += copy; 3186357b40a1SHerbert Xu } 31871a028e50SDavid S. Miller start = end; 3188357b40a1SHerbert Xu } 3189357b40a1SHerbert Xu 3190fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31911a028e50SDavid S. Miller int end; 3192357b40a1SHerbert Xu 3193547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31941a028e50SDavid S. Miller 3195fbb398a8SDavid S. Miller end = start + frag_iter->len; 3196357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3197357b40a1SHerbert Xu if (copy > len) 3198357b40a1SHerbert Xu copy = len; 3199fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 32001a028e50SDavid S. Miller from, copy)) 3201357b40a1SHerbert Xu goto fault; 3202357b40a1SHerbert Xu if ((len -= copy) == 0) 3203357b40a1SHerbert Xu return 0; 3204357b40a1SHerbert Xu offset += copy; 3205357b40a1SHerbert Xu from += copy; 3206357b40a1SHerbert Xu } 32071a028e50SDavid S. Miller start = end; 3208357b40a1SHerbert Xu } 3209357b40a1SHerbert Xu if (!len) 3210357b40a1SHerbert Xu return 0; 3211357b40a1SHerbert Xu 3212357b40a1SHerbert Xu fault: 3213357b40a1SHerbert Xu return -EFAULT; 3214357b40a1SHerbert Xu } 3215357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3216357b40a1SHerbert Xu 32171da177e4SLinus Torvalds /* Checksum skb data. */ 32182817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 32192817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 32201da177e4SLinus Torvalds { 32211a028e50SDavid S. Miller int start = skb_headlen(skb); 32221a028e50SDavid S. Miller int i, copy = start - offset; 3223fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32241da177e4SLinus Torvalds int pos = 0; 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds /* Checksum header. */ 32271da177e4SLinus Torvalds if (copy > 0) { 32281da177e4SLinus Torvalds if (copy > len) 32291da177e4SLinus Torvalds copy = len; 32302544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 32312544af03SMatteo Croce skb->data + offset, copy, csum); 32321da177e4SLinus Torvalds if ((len -= copy) == 0) 32331da177e4SLinus Torvalds return csum; 32341da177e4SLinus Torvalds offset += copy; 32351da177e4SLinus Torvalds pos = copy; 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32391a028e50SDavid S. Miller int end; 324051c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32411da177e4SLinus Torvalds 3242547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32431a028e50SDavid S. Miller 324451c56b00SEric Dumazet end = start + skb_frag_size(frag); 32451da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3246c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3247c613c209SWillem de Bruijn struct page *p; 324844bb9363SAl Viro __wsum csum2; 32491da177e4SLinus Torvalds u8 *vaddr; 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds if (copy > len) 32521da177e4SLinus Torvalds copy = len; 3253c613c209SWillem de Bruijn 3254c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3255b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3256c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3257c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 32582544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 32592544af03SMatteo Croce csum_partial_ext, 32602544af03SMatteo Croce vaddr + p_off, p_len, 0); 326151c56b00SEric Dumazet kunmap_atomic(vaddr); 32622544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 32632544af03SMatteo Croce csum_block_add_ext, csum, 32642544af03SMatteo Croce csum2, pos, p_len); 3265c613c209SWillem de Bruijn pos += p_len; 3266c613c209SWillem de Bruijn } 3267c613c209SWillem de Bruijn 32681da177e4SLinus Torvalds if (!(len -= copy)) 32691da177e4SLinus Torvalds return csum; 32701da177e4SLinus Torvalds offset += copy; 32711da177e4SLinus Torvalds } 32721a028e50SDavid S. Miller start = end; 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 3275fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32761a028e50SDavid S. Miller int end; 32771da177e4SLinus Torvalds 3278547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32791a028e50SDavid S. Miller 3280fbb398a8SDavid S. Miller end = start + frag_iter->len; 32811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32825f92a738SAl Viro __wsum csum2; 32831da177e4SLinus Torvalds if (copy > len) 32841da177e4SLinus Torvalds copy = len; 32852817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 32862817a336SDaniel Borkmann copy, 0, ops); 32872544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 32882544af03SMatteo Croce csum, csum2, pos, copy); 32891da177e4SLinus Torvalds if ((len -= copy) == 0) 32901da177e4SLinus Torvalds return csum; 32911da177e4SLinus Torvalds offset += copy; 32921da177e4SLinus Torvalds pos += copy; 32931da177e4SLinus Torvalds } 32941a028e50SDavid S. Miller start = end; 32951da177e4SLinus Torvalds } 329609a62660SKris Katterjohn BUG_ON(len); 32971da177e4SLinus Torvalds 32981da177e4SLinus Torvalds return csum; 32991da177e4SLinus Torvalds } 33002817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 33012817a336SDaniel Borkmann 33022817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 33032817a336SDaniel Borkmann int len, __wsum csum) 33042817a336SDaniel Borkmann { 33052817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3306cea80ea8SDaniel Borkmann .update = csum_partial_ext, 33072817a336SDaniel Borkmann .combine = csum_block_add_ext, 33082817a336SDaniel Borkmann }; 33092817a336SDaniel Borkmann 33102817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 33112817a336SDaniel Borkmann } 3312b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds /* Both of above in one bottle. */ 33151da177e4SLinus Torvalds 331681d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 33178d5930dfSAl Viro u8 *to, int len) 33181da177e4SLinus Torvalds { 33191a028e50SDavid S. Miller int start = skb_headlen(skb); 33201a028e50SDavid S. Miller int i, copy = start - offset; 3321fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 33221da177e4SLinus Torvalds int pos = 0; 33238d5930dfSAl Viro __wsum csum = 0; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds /* Copy header. */ 33261da177e4SLinus Torvalds if (copy > 0) { 33271da177e4SLinus Torvalds if (copy > len) 33281da177e4SLinus Torvalds copy = len; 33291da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3330cc44c17bSAl Viro copy); 33311da177e4SLinus Torvalds if ((len -= copy) == 0) 33321da177e4SLinus Torvalds return csum; 33331da177e4SLinus Torvalds offset += copy; 33341da177e4SLinus Torvalds to += copy; 33351da177e4SLinus Torvalds pos = copy; 33361da177e4SLinus Torvalds } 33371da177e4SLinus Torvalds 33381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33391a028e50SDavid S. Miller int end; 33401da177e4SLinus Torvalds 3341547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33421a028e50SDavid S. Miller 33439e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 33441da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3345c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3346c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3347c613c209SWillem de Bruijn struct page *p; 33485084205fSAl Viro __wsum csum2; 33491da177e4SLinus Torvalds u8 *vaddr; 33501da177e4SLinus Torvalds 33511da177e4SLinus Torvalds if (copy > len) 33521da177e4SLinus Torvalds copy = len; 3353c613c209SWillem de Bruijn 3354c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3355b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3356c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3357c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3358c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3359c613c209SWillem de Bruijn to + copied, 3360cc44c17bSAl Viro p_len); 336151c56b00SEric Dumazet kunmap_atomic(vaddr); 33621da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3363c613c209SWillem de Bruijn pos += p_len; 3364c613c209SWillem de Bruijn } 3365c613c209SWillem de Bruijn 33661da177e4SLinus Torvalds if (!(len -= copy)) 33671da177e4SLinus Torvalds return csum; 33681da177e4SLinus Torvalds offset += copy; 33691da177e4SLinus Torvalds to += copy; 33701da177e4SLinus Torvalds } 33711a028e50SDavid S. Miller start = end; 33721da177e4SLinus Torvalds } 33731da177e4SLinus Torvalds 3374fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 337581d77662SAl Viro __wsum csum2; 33761a028e50SDavid S. Miller int end; 33771da177e4SLinus Torvalds 3378547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33791a028e50SDavid S. Miller 3380fbb398a8SDavid S. Miller end = start + frag_iter->len; 33811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 33821da177e4SLinus Torvalds if (copy > len) 33831da177e4SLinus Torvalds copy = len; 3384fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 33851a028e50SDavid S. Miller offset - start, 33868d5930dfSAl Viro to, copy); 33871da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 33881da177e4SLinus Torvalds if ((len -= copy) == 0) 33891da177e4SLinus Torvalds return csum; 33901da177e4SLinus Torvalds offset += copy; 33911da177e4SLinus Torvalds to += copy; 33921da177e4SLinus Torvalds pos += copy; 33931da177e4SLinus Torvalds } 33941a028e50SDavid S. Miller start = end; 33951da177e4SLinus Torvalds } 339609a62660SKris Katterjohn BUG_ON(len); 33971da177e4SLinus Torvalds return csum; 33981da177e4SLinus Torvalds } 3399b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 34001da177e4SLinus Torvalds 340149f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 340249f8e832SCong Wang { 340349f8e832SCong Wang __sum16 sum; 340449f8e832SCong Wang 340549f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 340614641931SCong Wang /* See comments in __skb_checksum_complete(). */ 340749f8e832SCong Wang if (likely(!sum)) { 340849f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 340949f8e832SCong Wang !skb->csum_complete_sw) 34107fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 341149f8e832SCong Wang } 341249f8e832SCong Wang if (!skb_shared(skb)) 341349f8e832SCong Wang skb->csum_valid = !sum; 341449f8e832SCong Wang return sum; 341549f8e832SCong Wang } 341649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 341749f8e832SCong Wang 341814641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 341914641931SCong Wang * which has been changed from the hardware checksum, for example, by 342014641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 342114641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 342214641931SCong Wang * 342314641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 342414641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 342514641931SCong Wang * shared. 342614641931SCong Wang */ 342749f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 342849f8e832SCong Wang { 342949f8e832SCong Wang __wsum csum; 343049f8e832SCong Wang __sum16 sum; 343149f8e832SCong Wang 343249f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 343349f8e832SCong Wang 343449f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 343514641931SCong Wang /* This check is inverted, because we already knew the hardware 343614641931SCong Wang * checksum is invalid before calling this function. So, if the 343714641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 343814641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 343914641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 344014641931SCong Wang * when moving skb->data around. 344114641931SCong Wang */ 344249f8e832SCong Wang if (likely(!sum)) { 344349f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 344449f8e832SCong Wang !skb->csum_complete_sw) 34457fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 344649f8e832SCong Wang } 344749f8e832SCong Wang 344849f8e832SCong Wang if (!skb_shared(skb)) { 344949f8e832SCong Wang /* Save full packet checksum */ 345049f8e832SCong Wang skb->csum = csum; 345149f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 345249f8e832SCong Wang skb->csum_complete_sw = 1; 345349f8e832SCong Wang skb->csum_valid = !sum; 345449f8e832SCong Wang } 345549f8e832SCong Wang 345649f8e832SCong Wang return sum; 345749f8e832SCong Wang } 345849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 345949f8e832SCong Wang 34609617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 34619617813dSDavide Caratti { 34629617813dSDavide Caratti net_warn_ratelimited( 34639617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34649617813dSDavide Caratti __func__); 34659617813dSDavide Caratti return 0; 34669617813dSDavide Caratti } 34679617813dSDavide Caratti 34689617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 34699617813dSDavide Caratti int offset, int len) 34709617813dSDavide Caratti { 34719617813dSDavide Caratti net_warn_ratelimited( 34729617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34739617813dSDavide Caratti __func__); 34749617813dSDavide Caratti return 0; 34759617813dSDavide Caratti } 34769617813dSDavide Caratti 34779617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 34789617813dSDavide Caratti .update = warn_crc32c_csum_update, 34799617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 34809617813dSDavide Caratti }; 34819617813dSDavide Caratti 34829617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 34839617813dSDavide Caratti &default_crc32c_ops; 34849617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 34859617813dSDavide Caratti 3486af2806f8SThomas Graf /** 3487af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3488af2806f8SThomas Graf * @from: source buffer 3489af2806f8SThomas Graf * 3490af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3491af2806f8SThomas Graf * into skb_zerocopy(). 3492af2806f8SThomas Graf */ 3493af2806f8SThomas Graf unsigned int 3494af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3495af2806f8SThomas Graf { 3496af2806f8SThomas Graf unsigned int hlen = 0; 3497af2806f8SThomas Graf 3498af2806f8SThomas Graf if (!from->head_frag || 3499af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3500a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3501af2806f8SThomas Graf hlen = skb_headlen(from); 3502a17ad096SPravin B Shelar if (!hlen) 3503a17ad096SPravin B Shelar hlen = from->len; 3504a17ad096SPravin B Shelar } 3505af2806f8SThomas Graf 3506af2806f8SThomas Graf if (skb_has_frag_list(from)) 3507af2806f8SThomas Graf hlen = from->len; 3508af2806f8SThomas Graf 3509af2806f8SThomas Graf return hlen; 3510af2806f8SThomas Graf } 3511af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3512af2806f8SThomas Graf 3513af2806f8SThomas Graf /** 3514af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3515af2806f8SThomas Graf * @to: destination buffer 35167fceb4deSMasanari Iida * @from: source buffer 3517af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3518af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3519af2806f8SThomas Graf * 3520af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3521af2806f8SThomas Graf * to the frags in the source buffer. 3522af2806f8SThomas Graf * 3523af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3524af2806f8SThomas Graf * headroom in the `to` buffer. 352536d5fe6aSZoltan Kiss * 352636d5fe6aSZoltan Kiss * Return value: 352736d5fe6aSZoltan Kiss * 0: everything is OK 352836d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 352936d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3530af2806f8SThomas Graf */ 353136d5fe6aSZoltan Kiss int 353236d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3533af2806f8SThomas Graf { 3534af2806f8SThomas Graf int i, j = 0; 3535af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 353636d5fe6aSZoltan Kiss int ret; 3537af2806f8SThomas Graf struct page *page; 3538af2806f8SThomas Graf unsigned int offset; 3539af2806f8SThomas Graf 3540af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3541af2806f8SThomas Graf 3542af2806f8SThomas Graf /* dont bother with small payloads */ 354336d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 354436d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3545af2806f8SThomas Graf 3546af2806f8SThomas Graf if (hlen) { 354736d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 354836d5fe6aSZoltan Kiss if (unlikely(ret)) 354936d5fe6aSZoltan Kiss return ret; 3550af2806f8SThomas Graf len -= hlen; 3551af2806f8SThomas Graf } else { 3552af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3553af2806f8SThomas Graf if (plen) { 3554af2806f8SThomas Graf page = virt_to_head_page(from->head); 3555af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3556af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3557af2806f8SThomas Graf get_page(page); 3558af2806f8SThomas Graf j = 1; 3559af2806f8SThomas Graf len -= plen; 3560af2806f8SThomas Graf } 3561af2806f8SThomas Graf } 3562af2806f8SThomas Graf 3563ede57d58SRichard Gobert skb_len_add(to, len + plen); 3564af2806f8SThomas Graf 356536d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 356636d5fe6aSZoltan Kiss skb_tx_error(from); 356736d5fe6aSZoltan Kiss return -ENOMEM; 356836d5fe6aSZoltan Kiss } 35691f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 357036d5fe6aSZoltan Kiss 3571af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3572d8e18a51SMatthew Wilcox (Oracle) int size; 3573d8e18a51SMatthew Wilcox (Oracle) 3574af2806f8SThomas Graf if (!len) 3575af2806f8SThomas Graf break; 3576af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3577d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3578d8e18a51SMatthew Wilcox (Oracle) len); 3579d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3580d8e18a51SMatthew Wilcox (Oracle) len -= size; 3581af2806f8SThomas Graf skb_frag_ref(to, j); 3582af2806f8SThomas Graf j++; 3583af2806f8SThomas Graf } 3584af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 358536d5fe6aSZoltan Kiss 358636d5fe6aSZoltan Kiss return 0; 3587af2806f8SThomas Graf } 3588af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3589af2806f8SThomas Graf 35901da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 35911da177e4SLinus Torvalds { 3592d3bc23e7SAl Viro __wsum csum; 35931da177e4SLinus Torvalds long csstart; 35941da177e4SLinus Torvalds 359584fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 359655508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 35971da177e4SLinus Torvalds else 35981da177e4SLinus Torvalds csstart = skb_headlen(skb); 35991da177e4SLinus Torvalds 360009a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 36011da177e4SLinus Torvalds 3602d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 36031da177e4SLinus Torvalds 36041da177e4SLinus Torvalds csum = 0; 36051da177e4SLinus Torvalds if (csstart != skb->len) 36061da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 36078d5930dfSAl Viro skb->len - csstart); 36081da177e4SLinus Torvalds 360984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3610ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 36111da177e4SLinus Torvalds 3612d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 36131da177e4SLinus Torvalds } 36141da177e4SLinus Torvalds } 3615b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds /** 36181da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 36191da177e4SLinus Torvalds * @list: list to dequeue from 36201da177e4SLinus Torvalds * 36211da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 36221da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 36231da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36241da177e4SLinus Torvalds */ 36251da177e4SLinus Torvalds 36261da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 36271da177e4SLinus Torvalds { 36281da177e4SLinus Torvalds unsigned long flags; 36291da177e4SLinus Torvalds struct sk_buff *result; 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36321da177e4SLinus Torvalds result = __skb_dequeue(list); 36331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36341da177e4SLinus Torvalds return result; 36351da177e4SLinus Torvalds } 3636b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 36371da177e4SLinus Torvalds 36381da177e4SLinus Torvalds /** 36391da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 36401da177e4SLinus Torvalds * @list: list to dequeue from 36411da177e4SLinus Torvalds * 36421da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 36431da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 36441da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36451da177e4SLinus Torvalds */ 36461da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 36471da177e4SLinus Torvalds { 36481da177e4SLinus Torvalds unsigned long flags; 36491da177e4SLinus Torvalds struct sk_buff *result; 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36521da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 36531da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36541da177e4SLinus Torvalds return result; 36551da177e4SLinus Torvalds } 3656b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds /** 36591da177e4SLinus Torvalds * skb_queue_purge - empty a list 36601da177e4SLinus Torvalds * @list: list to empty 36611da177e4SLinus Torvalds * 36621da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 36631da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 36641da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 36651da177e4SLinus Torvalds */ 36661da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 36671da177e4SLinus Torvalds { 36681da177e4SLinus Torvalds struct sk_buff *skb; 36691da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 36701da177e4SLinus Torvalds kfree_skb(skb); 36711da177e4SLinus Torvalds } 3672b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds /** 36759f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 36769f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3677385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 36789f5afeaeSYaogong Wang * 36799f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 36809f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 36819f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 36829f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 36839f5afeaeSYaogong Wang */ 3684385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 36859f5afeaeSYaogong Wang { 36867c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3687385114deSPeter Oskolkov unsigned int sum = 0; 36889f5afeaeSYaogong Wang 36897c90584cSEric Dumazet while (p) { 36907c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 36917c90584cSEric Dumazet 36927c90584cSEric Dumazet p = rb_next(p); 36937c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3694385114deSPeter Oskolkov sum += skb->truesize; 36959f5afeaeSYaogong Wang kfree_skb(skb); 36967c90584cSEric Dumazet } 3697385114deSPeter Oskolkov return sum; 36989f5afeaeSYaogong Wang } 36999f5afeaeSYaogong Wang 37009f5afeaeSYaogong Wang /** 37011da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 37021da177e4SLinus Torvalds * @list: list to use 37031da177e4SLinus Torvalds * @newsk: buffer to queue 37041da177e4SLinus Torvalds * 37051da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 37061da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37071da177e4SLinus Torvalds * safely. 37081da177e4SLinus Torvalds * 37091da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37101da177e4SLinus Torvalds */ 37111da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 37121da177e4SLinus Torvalds { 37131da177e4SLinus Torvalds unsigned long flags; 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37161da177e4SLinus Torvalds __skb_queue_head(list, newsk); 37171da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37181da177e4SLinus Torvalds } 3719b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 37201da177e4SLinus Torvalds 37211da177e4SLinus Torvalds /** 37221da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 37231da177e4SLinus Torvalds * @list: list to use 37241da177e4SLinus Torvalds * @newsk: buffer to queue 37251da177e4SLinus Torvalds * 37261da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 37271da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37281da177e4SLinus Torvalds * safely. 37291da177e4SLinus Torvalds * 37301da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37311da177e4SLinus Torvalds */ 37321da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 37331da177e4SLinus Torvalds { 37341da177e4SLinus Torvalds unsigned long flags; 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37371da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 37381da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37391da177e4SLinus Torvalds } 3740b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 37418728b834SDavid S. Miller 37421da177e4SLinus Torvalds /** 37431da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 37441da177e4SLinus Torvalds * @skb: buffer to remove 37458728b834SDavid S. Miller * @list: list to use 37461da177e4SLinus Torvalds * 37478728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 37488728b834SDavid S. Miller * function is atomic with respect to other list locked calls 37491da177e4SLinus Torvalds * 37508728b834SDavid S. Miller * You must know what list the SKB is on. 37511da177e4SLinus Torvalds */ 37528728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 37531da177e4SLinus Torvalds { 37541da177e4SLinus Torvalds unsigned long flags; 37551da177e4SLinus Torvalds 37561da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37578728b834SDavid S. Miller __skb_unlink(skb, list); 37581da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37591da177e4SLinus Torvalds } 3760b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds /** 37631da177e4SLinus Torvalds * skb_append - append a buffer 37641da177e4SLinus Torvalds * @old: buffer to insert after 37651da177e4SLinus Torvalds * @newsk: buffer to insert 37668728b834SDavid S. Miller * @list: list to use 37671da177e4SLinus Torvalds * 37681da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 37691da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 37701da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37711da177e4SLinus Torvalds */ 37728728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 37731da177e4SLinus Torvalds { 37741da177e4SLinus Torvalds unsigned long flags; 37751da177e4SLinus Torvalds 37768728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 37777de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 37788728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 37791da177e4SLinus Torvalds } 3780b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 37811da177e4SLinus Torvalds 37821da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 37831da177e4SLinus Torvalds struct sk_buff* skb1, 37841da177e4SLinus Torvalds const u32 len, const int pos) 37851da177e4SLinus Torvalds { 37861da177e4SLinus Torvalds int i; 37871da177e4SLinus Torvalds 3788d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3789d626f62bSArnaldo Carvalho de Melo pos - len); 37901da177e4SLinus Torvalds /* And move data appendix as is. */ 37911da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 37921da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 37931da177e4SLinus Torvalds 37941da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 37951da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 37961da177e4SLinus Torvalds skb1->data_len = skb->data_len; 37971da177e4SLinus Torvalds skb1->len += skb1->data_len; 37981da177e4SLinus Torvalds skb->data_len = 0; 37991da177e4SLinus Torvalds skb->len = len; 380027a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 38031da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 38041da177e4SLinus Torvalds struct sk_buff* skb1, 38051da177e4SLinus Torvalds const u32 len, int pos) 38061da177e4SLinus Torvalds { 38071da177e4SLinus Torvalds int i, k = 0; 38081da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 38091da177e4SLinus Torvalds 38101da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38111da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 38121da177e4SLinus Torvalds skb->len = len; 38131da177e4SLinus Torvalds skb->data_len = len - pos; 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 38169e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 38171da177e4SLinus Torvalds 38181da177e4SLinus Torvalds if (pos + size > len) { 38191da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 38201da177e4SLinus Torvalds 38211da177e4SLinus Torvalds if (pos < len) { 38221da177e4SLinus Torvalds /* Split frag. 38231da177e4SLinus Torvalds * We have two variants in this case: 38241da177e4SLinus Torvalds * 1. Move all the frag to the second 38251da177e4SLinus Torvalds * part, if it is possible. F.e. 38261da177e4SLinus Torvalds * this approach is mandatory for TUX, 38271da177e4SLinus Torvalds * where splitting is expensive. 38281da177e4SLinus Torvalds * 2. Split is accurately. We make this. 38291da177e4SLinus Torvalds */ 3830ea2ab693SIan Campbell skb_frag_ref(skb, i); 3831b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 38329e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 38339e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 38341da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38351da177e4SLinus Torvalds } 38361da177e4SLinus Torvalds k++; 38371da177e4SLinus Torvalds } else 38381da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38391da177e4SLinus Torvalds pos += size; 38401da177e4SLinus Torvalds } 38411da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 38421da177e4SLinus Torvalds } 38431da177e4SLinus Torvalds 38441da177e4SLinus Torvalds /** 38451da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 38461da177e4SLinus Torvalds * @skb: the buffer to split 38471da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 38481da177e4SLinus Torvalds * @len: new length for skb 38491da177e4SLinus Torvalds */ 38501da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 38511da177e4SLinus Torvalds { 38521da177e4SLinus Torvalds int pos = skb_headlen(skb); 38539b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 38541da177e4SLinus Torvalds 3855753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3856753f1ca4SPavel Begunkov 38579b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 38581f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 38591da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 38601da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 38611da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 38621da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 38631da177e4SLinus Torvalds } 3864b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 38651da177e4SLinus Torvalds 38669f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 38679f782db3SIlpo Järvinen * 38689f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 38699f782db3SIlpo Järvinen */ 3870832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3871832d11c5SIlpo Järvinen { 3872c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3873832d11c5SIlpo Järvinen } 3874832d11c5SIlpo Järvinen 3875832d11c5SIlpo Järvinen /** 3876832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3877832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3878832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3879832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3880832d11c5SIlpo Järvinen * 3881832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 388220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3883832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3884832d11c5SIlpo Järvinen * 3885832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3886832d11c5SIlpo Järvinen * 3887832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3888832d11c5SIlpo Järvinen * to have non-paged data as well. 3889832d11c5SIlpo Järvinen * 3890832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3891832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3892832d11c5SIlpo Järvinen */ 3893832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3894832d11c5SIlpo Järvinen { 3895832d11c5SIlpo Järvinen int from, to, merge, todo; 3896d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3897832d11c5SIlpo Järvinen 3898832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3899f8071cdeSEric Dumazet 3900f8071cdeSEric Dumazet if (skb_headlen(skb)) 3901f8071cdeSEric Dumazet return 0; 39021f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 39031f8b977aSWillem de Bruijn return 0; 3904832d11c5SIlpo Järvinen 3905832d11c5SIlpo Järvinen todo = shiftlen; 3906832d11c5SIlpo Järvinen from = 0; 3907832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3908832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3909832d11c5SIlpo Järvinen 3910832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3911832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3912832d11c5SIlpo Järvinen */ 3913832d11c5SIlpo Järvinen if (!to || 3914ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3915b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3916832d11c5SIlpo Järvinen merge = -1; 3917832d11c5SIlpo Järvinen } else { 3918832d11c5SIlpo Järvinen merge = to - 1; 3919832d11c5SIlpo Järvinen 39209e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3921832d11c5SIlpo Järvinen if (todo < 0) { 3922832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3923832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3924832d11c5SIlpo Järvinen return 0; 3925832d11c5SIlpo Järvinen 39269f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 39279f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3928832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3929832d11c5SIlpo Järvinen 39309e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 39319e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3932b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3933832d11c5SIlpo Järvinen 3934832d11c5SIlpo Järvinen goto onlymerged; 3935832d11c5SIlpo Järvinen } 3936832d11c5SIlpo Järvinen 3937832d11c5SIlpo Järvinen from++; 3938832d11c5SIlpo Järvinen } 3939832d11c5SIlpo Järvinen 3940832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3941832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3942832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3943832d11c5SIlpo Järvinen return 0; 3944832d11c5SIlpo Järvinen 3945832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3946832d11c5SIlpo Järvinen return 0; 3947832d11c5SIlpo Järvinen 3948832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3949832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3950832d11c5SIlpo Järvinen return 0; 3951832d11c5SIlpo Järvinen 3952832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3953832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3954832d11c5SIlpo Järvinen 39559e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3956832d11c5SIlpo Järvinen *fragto = *fragfrom; 39579e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3958832d11c5SIlpo Järvinen from++; 3959832d11c5SIlpo Järvinen to++; 3960832d11c5SIlpo Järvinen 3961832d11c5SIlpo Järvinen } else { 3962ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3963b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3964b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 39659e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3966832d11c5SIlpo Järvinen 3967b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 39689e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3969832d11c5SIlpo Järvinen todo = 0; 3970832d11c5SIlpo Järvinen 3971832d11c5SIlpo Järvinen to++; 3972832d11c5SIlpo Järvinen break; 3973832d11c5SIlpo Järvinen } 3974832d11c5SIlpo Järvinen } 3975832d11c5SIlpo Järvinen 3976832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3977832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3978832d11c5SIlpo Järvinen 3979832d11c5SIlpo Järvinen if (merge >= 0) { 3980832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3981832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3982832d11c5SIlpo Järvinen 39839e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 39846a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3985832d11c5SIlpo Järvinen } 3986832d11c5SIlpo Järvinen 3987832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3988832d11c5SIlpo Järvinen to = 0; 3989832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3990832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3991832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3992832d11c5SIlpo Järvinen 3993832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3994832d11c5SIlpo Järvinen 3995832d11c5SIlpo Järvinen onlymerged: 3996832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3997832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3998832d11c5SIlpo Järvinen */ 3999832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 4000832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 4001832d11c5SIlpo Järvinen 4002ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 4003ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 4004832d11c5SIlpo Järvinen 4005832d11c5SIlpo Järvinen return shiftlen; 4006832d11c5SIlpo Järvinen } 4007832d11c5SIlpo Järvinen 4008677e90edSThomas Graf /** 4009677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 4010677e90edSThomas Graf * @skb: the buffer to read 4011677e90edSThomas Graf * @from: lower offset of data to be read 4012677e90edSThomas Graf * @to: upper offset of data to be read 4013677e90edSThomas Graf * @st: state variable 4014677e90edSThomas Graf * 4015677e90edSThomas Graf * Initializes the specified state variable. Must be called before 4016677e90edSThomas Graf * invoking skb_seq_read() for the first time. 4017677e90edSThomas Graf */ 4018677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 4019677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 4020677e90edSThomas Graf { 4021677e90edSThomas Graf st->lower_offset = from; 4022677e90edSThomas Graf st->upper_offset = to; 4023677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 4024677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 4025677e90edSThomas Graf st->frag_data = NULL; 402697550f6fSWillem de Bruijn st->frag_off = 0; 4027677e90edSThomas Graf } 4028b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 4029677e90edSThomas Graf 4030677e90edSThomas Graf /** 4031677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 4032677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 4033677e90edSThomas Graf * @data: destination pointer for data to be returned 4034677e90edSThomas Graf * @st: state variable 4035677e90edSThomas Graf * 4036bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 4037677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 4038bc32383cSMathias Krause * the head of the data block to @data and returns the length 4039677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 4040677e90edSThomas Graf * offset has been reached. 4041677e90edSThomas Graf * 4042677e90edSThomas Graf * The caller is not required to consume all of the data 4043bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 4044677e90edSThomas Graf * of bytes already consumed and the next call to 4045677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 4046677e90edSThomas Graf * 404725985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 4048e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 4049677e90edSThomas Graf * reads of potentially non linear data. 4050677e90edSThomas Graf * 4051bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 4052677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 4053677e90edSThomas Graf * a stack for this purpose. 4054677e90edSThomas Graf */ 4055677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 4056677e90edSThomas Graf struct skb_seq_state *st) 4057677e90edSThomas Graf { 4058677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 4059677e90edSThomas Graf skb_frag_t *frag; 4060677e90edSThomas Graf 4061aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 4062aeb193eaSWedson Almeida Filho if (st->frag_data) { 4063aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 4064aeb193eaSWedson Almeida Filho st->frag_data = NULL; 4065aeb193eaSWedson Almeida Filho } 4066677e90edSThomas Graf return 0; 4067aeb193eaSWedson Almeida Filho } 4068677e90edSThomas Graf 4069677e90edSThomas Graf next_skb: 407095e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 4071677e90edSThomas Graf 4072995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 407395e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 4074677e90edSThomas Graf return block_limit - abs_offset; 4075677e90edSThomas Graf } 4076677e90edSThomas Graf 4077677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 4078677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 4079677e90edSThomas Graf 4080677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 408197550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 4082677e90edSThomas Graf 408397550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 408497550f6fSWillem de Bruijn 408597550f6fSWillem de Bruijn pg_idx = 0; 408697550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 408797550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 408897550f6fSWillem de Bruijn 408997550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 409097550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 409197550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 409297550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 409397550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 409497550f6fSWillem de Bruijn } 409597550f6fSWillem de Bruijn 409697550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 4097677e90edSThomas Graf if (abs_offset < block_limit) { 4098677e90edSThomas Graf if (!st->frag_data) 409997550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 4100677e90edSThomas Graf 410197550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 4102677e90edSThomas Graf (abs_offset - st->stepped_offset); 4103677e90edSThomas Graf 4104677e90edSThomas Graf return block_limit - abs_offset; 4105677e90edSThomas Graf } 4106677e90edSThomas Graf 4107677e90edSThomas Graf if (st->frag_data) { 410851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4109677e90edSThomas Graf st->frag_data = NULL; 4110677e90edSThomas Graf } 4111677e90edSThomas Graf 411297550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 411397550f6fSWillem de Bruijn st->frag_off += pg_sz; 411497550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 411597550f6fSWillem de Bruijn st->frag_off = 0; 4116677e90edSThomas Graf st->frag_idx++; 411797550f6fSWillem de Bruijn } 4118677e90edSThomas Graf } 4119677e90edSThomas Graf 41205b5a60daSOlaf Kirch if (st->frag_data) { 412151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 41225b5a60daSOlaf Kirch st->frag_data = NULL; 41235b5a60daSOlaf Kirch } 41245b5a60daSOlaf Kirch 412521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4126677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 412795e3b24cSHerbert Xu st->frag_idx = 0; 4128677e90edSThomas Graf goto next_skb; 412971b3346dSShyam Iyer } else if (st->cur_skb->next) { 413071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 413171b3346dSShyam Iyer st->frag_idx = 0; 4132677e90edSThomas Graf goto next_skb; 4133677e90edSThomas Graf } 4134677e90edSThomas Graf 4135677e90edSThomas Graf return 0; 4136677e90edSThomas Graf } 4137b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4138677e90edSThomas Graf 4139677e90edSThomas Graf /** 4140677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4141677e90edSThomas Graf * @st: state variable 4142677e90edSThomas Graf * 4143677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4144677e90edSThomas Graf * returned 0. 4145677e90edSThomas Graf */ 4146677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4147677e90edSThomas Graf { 4148677e90edSThomas Graf if (st->frag_data) 414951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4150677e90edSThomas Graf } 4151b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4152677e90edSThomas Graf 41533fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 41543fc7e8a6SThomas Graf 41553fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 41563fc7e8a6SThomas Graf struct ts_config *conf, 41573fc7e8a6SThomas Graf struct ts_state *state) 41583fc7e8a6SThomas Graf { 41593fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 41603fc7e8a6SThomas Graf } 41613fc7e8a6SThomas Graf 41623fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 41633fc7e8a6SThomas Graf { 41643fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 41653fc7e8a6SThomas Graf } 41663fc7e8a6SThomas Graf 41673fc7e8a6SThomas Graf /** 41683fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 41693fc7e8a6SThomas Graf * @skb: the buffer to look in 41703fc7e8a6SThomas Graf * @from: search offset 41713fc7e8a6SThomas Graf * @to: search limit 41723fc7e8a6SThomas Graf * @config: textsearch configuration 41733fc7e8a6SThomas Graf * 41743fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 41753fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 41763fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 41773fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 41783fc7e8a6SThomas Graf */ 41793fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4180059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 41813fc7e8a6SThomas Graf { 4182059a2440SBojan Prtvar struct ts_state state; 4183f72b948dSPhil Oester unsigned int ret; 4184f72b948dSPhil Oester 4185b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4186b228c9b0SWillem de Bruijn 41873fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 41883fc7e8a6SThomas Graf config->finish = skb_ts_finish; 41893fc7e8a6SThomas Graf 4190059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 41913fc7e8a6SThomas Graf 4192059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4193f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 41943fc7e8a6SThomas Graf } 4195b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 41963fc7e8a6SThomas Graf 4197be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4198be12a1feSHannes Frederic Sowa int offset, size_t size) 4199be12a1feSHannes Frederic Sowa { 4200be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4201be12a1feSHannes Frederic Sowa 4202be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4203be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4204be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 4205753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4206be12a1feSHannes Frederic Sowa get_page(page); 4207228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4208be12a1feSHannes Frederic Sowa } else { 4209be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4210be12a1feSHannes Frederic Sowa } 4211be12a1feSHannes Frederic Sowa 4212be12a1feSHannes Frederic Sowa return 0; 4213be12a1feSHannes Frederic Sowa } 4214be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4215be12a1feSHannes Frederic Sowa 4216cbb042f9SHerbert Xu /** 4217cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4218cbb042f9SHerbert Xu * @skb: buffer to update 4219cbb042f9SHerbert Xu * @len: length of data pulled 4220cbb042f9SHerbert Xu * 4221cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4222fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 422384fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 422484fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 422584fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4226cbb042f9SHerbert Xu */ 4227af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4228cbb042f9SHerbert Xu { 422931b33dfbSPravin B Shelar unsigned char *data = skb->data; 423031b33dfbSPravin B Shelar 4231cbb042f9SHerbert Xu BUG_ON(len > skb->len); 423231b33dfbSPravin B Shelar __skb_pull(skb, len); 423331b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 423431b33dfbSPravin B Shelar return skb->data; 4235cbb042f9SHerbert Xu } 4236f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4237f94691acSArnaldo Carvalho de Melo 423813acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 423913acc94eSYonghong Song { 424013acc94eSYonghong Song skb_frag_t head_frag; 424113acc94eSYonghong Song struct page *page; 424213acc94eSYonghong Song 424313acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4244d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 4245b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 4246b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 4247d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 424813acc94eSYonghong Song return head_frag; 424913acc94eSYonghong Song } 425013acc94eSYonghong Song 42513a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 42523a1296a3SSteffen Klassert netdev_features_t features, 42533a1296a3SSteffen Klassert unsigned int offset) 42543a1296a3SSteffen Klassert { 42553a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 42563a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 42573a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 42583a1296a3SSteffen Klassert unsigned int delta_len = 0; 42593a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 426053475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4261cf3ab8d4SLina Wang int len_diff, err; 42623a1296a3SSteffen Klassert 42633a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 42643a1296a3SSteffen Klassert 42653a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 42663a1296a3SSteffen Klassert 4267876e8ca8SYan Zhai while (list_skb) { 42683a1296a3SSteffen Klassert nskb = list_skb; 42693a1296a3SSteffen Klassert list_skb = list_skb->next; 42703a1296a3SSteffen Klassert 427153475c5dSDongseok Yi err = 0; 4272224102deSlena wang delta_truesize += nskb->truesize; 427353475c5dSDongseok Yi if (skb_shared(nskb)) { 427453475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 427553475c5dSDongseok Yi if (tmp) { 427653475c5dSDongseok Yi consume_skb(nskb); 427753475c5dSDongseok Yi nskb = tmp; 427853475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 427953475c5dSDongseok Yi } else { 428053475c5dSDongseok Yi err = -ENOMEM; 428153475c5dSDongseok Yi } 428253475c5dSDongseok Yi } 428353475c5dSDongseok Yi 42843a1296a3SSteffen Klassert if (!tail) 42853a1296a3SSteffen Klassert skb->next = nskb; 42863a1296a3SSteffen Klassert else 42873a1296a3SSteffen Klassert tail->next = nskb; 42883a1296a3SSteffen Klassert 428953475c5dSDongseok Yi if (unlikely(err)) { 429053475c5dSDongseok Yi nskb->next = list_skb; 429153475c5dSDongseok Yi goto err_linearize; 429253475c5dSDongseok Yi } 429353475c5dSDongseok Yi 42943a1296a3SSteffen Klassert tail = nskb; 42953a1296a3SSteffen Klassert 42963a1296a3SSteffen Klassert delta_len += nskb->len; 42973a1296a3SSteffen Klassert 42983a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 42993a1296a3SSteffen Klassert 4300cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4301cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 43023a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 43033a1296a3SSteffen Klassert 43043a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4305cf3ab8d4SLina Wang nskb->transport_header += len_diff; 43063a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 43073a1296a3SSteffen Klassert nskb->data - tnl_hlen, 43083a1296a3SSteffen Klassert offset + tnl_hlen); 43093a1296a3SSteffen Klassert 43103a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 43113a1296a3SSteffen Klassert __skb_linearize(nskb)) 43123a1296a3SSteffen Klassert goto err_linearize; 4313876e8ca8SYan Zhai } 43143a1296a3SSteffen Klassert 43153a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 43163a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 43173a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 43183a1296a3SSteffen Klassert 43193a1296a3SSteffen Klassert skb_gso_reset(skb); 43203a1296a3SSteffen Klassert 43213a1296a3SSteffen Klassert skb->prev = tail; 43223a1296a3SSteffen Klassert 43233a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 43243a1296a3SSteffen Klassert __skb_linearize(skb)) 43253a1296a3SSteffen Klassert goto err_linearize; 43263a1296a3SSteffen Klassert 43273a1296a3SSteffen Klassert skb_get(skb); 43283a1296a3SSteffen Klassert 43293a1296a3SSteffen Klassert return skb; 43303a1296a3SSteffen Klassert 43313a1296a3SSteffen Klassert err_linearize: 43323a1296a3SSteffen Klassert kfree_skb_list(skb->next); 43333a1296a3SSteffen Klassert skb->next = NULL; 43343a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 43353a1296a3SSteffen Klassert } 43363a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 43373a1296a3SSteffen Klassert 4338f4c50d99SHerbert Xu /** 4339f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4340df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4341576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4342f4c50d99SHerbert Xu * 4343f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 43444c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 43454c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4346f4c50d99SHerbert Xu */ 4347df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4348df5771ffSMichael S. Tsirkin netdev_features_t features) 4349f4c50d99SHerbert Xu { 4350f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4351f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 43521a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4353df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4354df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4355df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 43561fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4357f4c50d99SHerbert Xu unsigned int offset = doffset; 4358df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4359802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4360f4c50d99SHerbert Xu unsigned int headroom; 4361802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4362ec5f0615SPravin B Shelar __be16 proto; 436336c98382SAlexander Duyck bool csum, sg; 4364df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4365f4c50d99SHerbert Xu int err = -ENOMEM; 4366f4c50d99SHerbert Xu int i = 0; 4367f4c50d99SHerbert Xu int pos; 4368f4c50d99SHerbert Xu 43699e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 43709e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 43719e4b7a99SJiri Benc struct sk_buff *check_skb; 43729e4b7a99SJiri Benc 43739e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 43749e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 43759e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 43769e4b7a99SJiri Benc * a linear non head_frag item. 43773dcbdb13SShmulik Ladkani * 43789e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 43799e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 43809e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 43819e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 43829e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 43833dcbdb13SShmulik Ladkani */ 43843dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 43859e4b7a99SJiri Benc break; 43869e4b7a99SJiri Benc } 43879e4b7a99SJiri Benc } 43883dcbdb13SShmulik Ladkani } 43893dcbdb13SShmulik Ladkani 43905882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 43912f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4392ec5f0615SPravin B Shelar if (unlikely(!proto)) 4393ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4394ec5f0615SPravin B Shelar 439536c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4396f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 43977e2b10c1STom Herbert 439807b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 439907b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 440007b26c94SSteffen Klassert struct sk_buff *iter; 440143170c4eSIlan Tayari unsigned int frag_len; 440207b26c94SSteffen Klassert 440307b26c94SSteffen Klassert if (!list_skb || 440407b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 440507b26c94SSteffen Klassert goto normal; 440607b26c94SSteffen Klassert 440743170c4eSIlan Tayari /* If we get here then all the required 440843170c4eSIlan Tayari * GSO features except frag_list are supported. 440943170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 441043170c4eSIlan Tayari * with no frag_list. 441143170c4eSIlan Tayari * Currently we can do that only when the buffers don't 441243170c4eSIlan Tayari * have a linear part and all the buffers except 441343170c4eSIlan Tayari * the last are of the same length. 441407b26c94SSteffen Klassert */ 441543170c4eSIlan Tayari frag_len = list_skb->len; 441607b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 441743170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 441843170c4eSIlan Tayari goto normal; 4419eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 442007b26c94SSteffen Klassert goto normal; 442107b26c94SSteffen Klassert 442207b26c94SSteffen Klassert len -= iter->len; 442307b26c94SSteffen Klassert } 442443170c4eSIlan Tayari 442543170c4eSIlan Tayari if (len != frag_len) 442643170c4eSIlan Tayari goto normal; 442707b26c94SSteffen Klassert } 442807b26c94SSteffen Klassert 4429802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4430802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4431802ab55aSAlexander Duyck * now. 4432802ab55aSAlexander Duyck */ 4433802ab55aSAlexander Duyck partial_segs = len / mss; 4434d7fb5a80SAlexander Duyck if (partial_segs > 1) 4435802ab55aSAlexander Duyck mss *= partial_segs; 4436d7fb5a80SAlexander Duyck else 4437d7fb5a80SAlexander Duyck partial_segs = 0; 4438802ab55aSAlexander Duyck } 4439802ab55aSAlexander Duyck 444007b26c94SSteffen Klassert normal: 4441df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4442df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4443f4c50d99SHerbert Xu 4444f4c50d99SHerbert Xu do { 4445f4c50d99SHerbert Xu struct sk_buff *nskb; 44468cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4447c8884eddSHerbert Xu int hsize; 4448f4c50d99SHerbert Xu int size; 4449f4c50d99SHerbert Xu 44503953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 44513953c46cSMarcelo Ricardo Leitner len = list_skb->len; 44523953c46cSMarcelo Ricardo Leitner } else { 4453df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4454f4c50d99SHerbert Xu if (len > mss) 4455f4c50d99SHerbert Xu len = mss; 44563953c46cSMarcelo Ricardo Leitner } 4457f4c50d99SHerbert Xu 4458df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4459f4c50d99SHerbert Xu 4460dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 44611a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 44621a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 446389319d38SHerbert Xu 44649d8506ccSHerbert Xu i = 0; 44651a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44661a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44671fd819ecSMichael S. Tsirkin frag_skb = list_skb; 44681a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 44699d8506ccSHerbert Xu 44709d8506ccSHerbert Xu while (pos < offset + len) { 44719d8506ccSHerbert Xu BUG_ON(i >= nfrags); 44729d8506ccSHerbert Xu 44734e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 44749d8506ccSHerbert Xu if (pos + size > offset + len) 44759d8506ccSHerbert Xu break; 44769d8506ccSHerbert Xu 44779d8506ccSHerbert Xu i++; 44789d8506ccSHerbert Xu pos += size; 44794e1beba1SMichael S. Tsirkin frag++; 44809d8506ccSHerbert Xu } 44819d8506ccSHerbert Xu 44821a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 44831a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 448489319d38SHerbert Xu 4485f4c50d99SHerbert Xu if (unlikely(!nskb)) 4486f4c50d99SHerbert Xu goto err; 4487f4c50d99SHerbert Xu 44889d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 44899d8506ccSHerbert Xu kfree_skb(nskb); 44909d8506ccSHerbert Xu goto err; 44919d8506ccSHerbert Xu } 44929d8506ccSHerbert Xu 4493ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 449489319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 449589319d38SHerbert Xu kfree_skb(nskb); 449689319d38SHerbert Xu goto err; 449789319d38SHerbert Xu } 449889319d38SHerbert Xu 4499ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 450089319d38SHerbert Xu skb_release_head_state(nskb); 450189319d38SHerbert Xu __skb_push(nskb, doffset); 450289319d38SHerbert Xu } else { 450300b229f7SPaolo Abeni if (hsize < 0) 450400b229f7SPaolo Abeni hsize = 0; 4505dbd50f23SXin Long if (hsize > len || !sg) 4506dbd50f23SXin Long hsize = len; 4507dbd50f23SXin Long 4508c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4509df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4510c93bdd0eSMel Gorman NUMA_NO_NODE); 451189319d38SHerbert Xu 451289319d38SHerbert Xu if (unlikely(!nskb)) 451389319d38SHerbert Xu goto err; 451489319d38SHerbert Xu 451589319d38SHerbert Xu skb_reserve(nskb, headroom); 451689319d38SHerbert Xu __skb_put(nskb, doffset); 451789319d38SHerbert Xu } 451889319d38SHerbert Xu 4519f4c50d99SHerbert Xu if (segs) 4520f4c50d99SHerbert Xu tail->next = nskb; 4521f4c50d99SHerbert Xu else 4522f4c50d99SHerbert Xu segs = nskb; 4523f4c50d99SHerbert Xu tail = nskb; 4524f4c50d99SHerbert Xu 4525df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4526f4c50d99SHerbert Xu 4527030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4528fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 452968c33163SPravin B Shelar 4530df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 453168c33163SPravin B Shelar nskb->data - tnl_hlen, 453268c33163SPravin B Shelar doffset + tnl_hlen); 453389319d38SHerbert Xu 45349d8506ccSHerbert Xu if (nskb->len == len + doffset) 45351cdbcb79SSimon Horman goto perform_csum_check; 453689319d38SHerbert Xu 45377fbeffedSAlexander Duyck if (!sg) { 45381454c9faSYadu Kishore if (!csum) { 45397fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 45406f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 454176443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 454276443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 45431454c9faSYadu Kishore skb_put(nskb, 45441454c9faSYadu Kishore len), 45458d5930dfSAl Viro len); 45467e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4547de843723STom Herbert skb_headroom(nskb) + doffset; 45481454c9faSYadu Kishore } else { 4549c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4550c624c58eSlily goto err; 45511454c9faSYadu Kishore } 4552f4c50d99SHerbert Xu continue; 4553f4c50d99SHerbert Xu } 4554f4c50d99SHerbert Xu 45558cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4556f4c50d99SHerbert Xu 4557df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4558d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4559f4c50d99SHerbert Xu 456006b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 456106b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4562cef401deSEric Dumazet 4563bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4564bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4565bf5c25d6SWillem de Bruijn goto err; 4566bf5c25d6SWillem de Bruijn 45679d8506ccSHerbert Xu while (pos < offset + len) { 45689d8506ccSHerbert Xu if (i >= nfrags) { 45699d8506ccSHerbert Xu i = 0; 45701a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 45711a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 45721fd819ecSMichael S. Tsirkin frag_skb = list_skb; 457313acc94eSYonghong Song if (!skb_headlen(list_skb)) { 45749d8506ccSHerbert Xu BUG_ON(!nfrags); 457513acc94eSYonghong Song } else { 457613acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 45779d8506ccSHerbert Xu 457813acc94eSYonghong Song /* to make room for head_frag. */ 457913acc94eSYonghong Song i--; 458013acc94eSYonghong Song frag--; 458113acc94eSYonghong Song } 4582bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4583bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4584bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4585bf5c25d6SWillem de Bruijn goto err; 4586bf5c25d6SWillem de Bruijn 45871a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 45889d8506ccSHerbert Xu } 45899d8506ccSHerbert Xu 45909d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 45919d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 45929d8506ccSHerbert Xu net_warn_ratelimited( 45939d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 45949d8506ccSHerbert Xu pos, mss); 4595ff907a11SEric Dumazet err = -EINVAL; 45969d8506ccSHerbert Xu goto err; 45979d8506ccSHerbert Xu } 45989d8506ccSHerbert Xu 459913acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 46008cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 46018cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4602f4c50d99SHerbert Xu 4603f4c50d99SHerbert Xu if (pos < offset) { 4604b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 46058cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4606f4c50d99SHerbert Xu } 4607f4c50d99SHerbert Xu 460889319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4609f4c50d99SHerbert Xu 4610f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4611f4c50d99SHerbert Xu i++; 46124e1beba1SMichael S. Tsirkin frag++; 4613f4c50d99SHerbert Xu pos += size; 4614f4c50d99SHerbert Xu } else { 46158cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 461689319d38SHerbert Xu goto skip_fraglist; 4617f4c50d99SHerbert Xu } 4618f4c50d99SHerbert Xu 46198cb19905SMichael S. Tsirkin nskb_frag++; 4620f4c50d99SHerbert Xu } 4621f4c50d99SHerbert Xu 462289319d38SHerbert Xu skip_fraglist: 4623f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4624f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4625f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4626ec5f0615SPravin B Shelar 46271cdbcb79SSimon Horman perform_csum_check: 46287fbeffedSAlexander Duyck if (!csum) { 4629ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4630ff907a11SEric Dumazet __skb_linearize(nskb)) 4631ddff00d4SAlexander Duyck goto err; 4632ff907a11SEric Dumazet 46337fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4634ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 463576443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 463676443456SAlexander Duyck skb_checksum(nskb, doffset, 463776443456SAlexander Duyck nskb->len - doffset, 0); 46387e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 46397e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4640ec5f0615SPravin B Shelar } 4641df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4642f4c50d99SHerbert Xu 4643bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4644bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4645bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4646bec3cfdcSEric Dumazet */ 4647bec3cfdcSEric Dumazet segs->prev = tail; 4648432c856fSToshiaki Makita 4649802ab55aSAlexander Duyck if (partial_segs) { 465007b26c94SSteffen Klassert struct sk_buff *iter; 4651802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 465207b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4653802ab55aSAlexander Duyck 4654802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 465507b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4656802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4657802ab55aSAlexander Duyck 4658802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4659802ab55aSAlexander Duyck * our way back down the stack of protocols. 4660802ab55aSAlexander Duyck */ 466107b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 466207b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 466307b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 466407b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 466507b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 466607b26c94SSteffen Klassert } 466707b26c94SSteffen Klassert 466807b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 466907b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 467007b26c94SSteffen Klassert else if (tail != segs) 467107b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4672802ab55aSAlexander Duyck } 4673802ab55aSAlexander Duyck 4674432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4675432c856fSToshiaki Makita * using skb_set_owner_w(). 4676432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4677432c856fSToshiaki Makita */ 4678432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4679432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4680432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4681432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4682432c856fSToshiaki Makita } 4683f4c50d99SHerbert Xu return segs; 4684f4c50d99SHerbert Xu 4685f4c50d99SHerbert Xu err: 4686289dccbeSEric Dumazet kfree_skb_list(segs); 4687f4c50d99SHerbert Xu return ERR_PTR(err); 4688f4c50d99SHerbert Xu } 4689f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4690f4c50d99SHerbert Xu 4691df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4692df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4693df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4694df5042f4SFlorian Westphal 4695df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4696df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4697df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4698df5042f4SFlorian Westphal #endif 46994165079bSFlorian Westphal #ifdef CONFIG_XFRM 47004165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 47014165079bSFlorian Westphal #endif 470295a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 470395a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 470495a7233cSPaul Blakey #endif 47053ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47063ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 47073ee17bc7SMat Martineau #endif 470878476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 470978476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 471078476d31SJeremy Kerr #endif 4711df5042f4SFlorian Westphal }; 4712df5042f4SFlorian Westphal 4713df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4714df5042f4SFlorian Westphal { 4715df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4716df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4717df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4718df5042f4SFlorian Westphal #endif 47194165079bSFlorian Westphal #ifdef CONFIG_XFRM 47204165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 47214165079bSFlorian Westphal #endif 472295a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 472395a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 472495a7233cSPaul Blakey #endif 47253ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47263ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 47273ee17bc7SMat Martineau #endif 472878476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 472978476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 473078476d31SJeremy Kerr #endif 4731df5042f4SFlorian Westphal 0; 4732df5042f4SFlorian Westphal } 4733df5042f4SFlorian Westphal 4734df5042f4SFlorian Westphal static void skb_extensions_init(void) 4735df5042f4SFlorian Westphal { 4736df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4737df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4738df5042f4SFlorian Westphal 4739df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4740df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4741df5042f4SFlorian Westphal 0, 4742df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4743df5042f4SFlorian Westphal NULL); 4744df5042f4SFlorian Westphal } 4745df5042f4SFlorian Westphal #else 4746df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4747df5042f4SFlorian Westphal #endif 4748df5042f4SFlorian Westphal 47491da177e4SLinus Torvalds void __init skb_init(void) 47501da177e4SLinus Torvalds { 4751025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", 47521da177e4SLinus Torvalds sizeof(struct sk_buff), 47531da177e4SLinus Torvalds 0, 4754e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 475579a8a642SKees Cook offsetof(struct sk_buff, cb), 475679a8a642SKees Cook sizeof_field(struct sk_buff, cb), 475720c2df83SPaul Mundt NULL); 4758d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4759d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4760d179cd12SDavid S. Miller 0, 4761e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 476220c2df83SPaul Mundt NULL); 4763bf9f1baaSEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 47640b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. 47650b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head, 47660b34d680SEric Dumazet * and should not be copied to/from user. 47670b34d680SEric Dumazet */ 47680b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", 4769bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE, 4770bf9f1baaSEric Dumazet 0, 4771bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC, 47720b34d680SEric Dumazet 0, 47730b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM, 4774bf9f1baaSEric Dumazet NULL); 4775bf9f1baaSEric Dumazet #endif 4776df5042f4SFlorian Westphal skb_extensions_init(); 47771da177e4SLinus Torvalds } 47781da177e4SLinus Torvalds 477951c739d1SDavid S. Miller static int 478048a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 478148a1df65SJason A. Donenfeld unsigned int recursion_level) 4782716ea3a7SDavid Howells { 47831a028e50SDavid S. Miller int start = skb_headlen(skb); 47841a028e50SDavid S. Miller int i, copy = start - offset; 4785fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4786716ea3a7SDavid Howells int elt = 0; 4787716ea3a7SDavid Howells 478848a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 478948a1df65SJason A. Donenfeld return -EMSGSIZE; 479048a1df65SJason A. Donenfeld 4791716ea3a7SDavid Howells if (copy > 0) { 4792716ea3a7SDavid Howells if (copy > len) 4793716ea3a7SDavid Howells copy = len; 4794642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4795716ea3a7SDavid Howells elt++; 4796716ea3a7SDavid Howells if ((len -= copy) == 0) 4797716ea3a7SDavid Howells return elt; 4798716ea3a7SDavid Howells offset += copy; 4799716ea3a7SDavid Howells } 4800716ea3a7SDavid Howells 4801716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 48021a028e50SDavid S. Miller int end; 4803716ea3a7SDavid Howells 4804547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48051a028e50SDavid S. Miller 48069e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4807716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4808716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 480948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 481048a1df65SJason A. Donenfeld return -EMSGSIZE; 4811716ea3a7SDavid Howells 4812716ea3a7SDavid Howells if (copy > len) 4813716ea3a7SDavid Howells copy = len; 4814ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4815b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4816716ea3a7SDavid Howells elt++; 4817716ea3a7SDavid Howells if (!(len -= copy)) 4818716ea3a7SDavid Howells return elt; 4819716ea3a7SDavid Howells offset += copy; 4820716ea3a7SDavid Howells } 48211a028e50SDavid S. Miller start = end; 4822716ea3a7SDavid Howells } 4823716ea3a7SDavid Howells 4824fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 482548a1df65SJason A. Donenfeld int end, ret; 4826716ea3a7SDavid Howells 4827547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48281a028e50SDavid S. Miller 4829fbb398a8SDavid S. Miller end = start + frag_iter->len; 4830716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 483148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 483248a1df65SJason A. Donenfeld return -EMSGSIZE; 483348a1df65SJason A. Donenfeld 4834716ea3a7SDavid Howells if (copy > len) 4835716ea3a7SDavid Howells copy = len; 483648a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 483748a1df65SJason A. Donenfeld copy, recursion_level + 1); 483848a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 483948a1df65SJason A. Donenfeld return ret; 484048a1df65SJason A. Donenfeld elt += ret; 4841716ea3a7SDavid Howells if ((len -= copy) == 0) 4842716ea3a7SDavid Howells return elt; 4843716ea3a7SDavid Howells offset += copy; 4844716ea3a7SDavid Howells } 48451a028e50SDavid S. Miller start = end; 4846716ea3a7SDavid Howells } 4847716ea3a7SDavid Howells BUG_ON(len); 4848716ea3a7SDavid Howells return elt; 4849716ea3a7SDavid Howells } 4850716ea3a7SDavid Howells 485148a1df65SJason A. Donenfeld /** 485248a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 485348a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 485448a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 485548a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 485648a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 485748a1df65SJason A. Donenfeld * 485848a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 485948a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 486048a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 486148a1df65SJason A. Donenfeld * could not fit. 486248a1df65SJason A. Donenfeld */ 486348a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 486448a1df65SJason A. Donenfeld { 486548a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 486648a1df65SJason A. Donenfeld 486748a1df65SJason A. Donenfeld if (nsg <= 0) 486848a1df65SJason A. Donenfeld return nsg; 486948a1df65SJason A. Donenfeld 487048a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 487148a1df65SJason A. Donenfeld 487248a1df65SJason A. Donenfeld return nsg; 487348a1df65SJason A. Donenfeld } 487448a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 487548a1df65SJason A. Donenfeld 487625a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 487725a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 487825a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 487925a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 488025a91d8dSFan Du * 488125a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 488225a91d8dSFan Du * 1. sg_init_table 488325a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 488425a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 488525a91d8dSFan Du * 488625a91d8dSFan Du * This is equivalent to: 488725a91d8dSFan Du * 1. sg_init_table 488825a91d8dSFan Du * 2. skb_to_sgvec(payload1) 488925a91d8dSFan Du * 3. sg_unmark_end 489025a91d8dSFan Du * 4. skb_to_sgvec(payload2) 489125a91d8dSFan Du * 489225a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 489325a91d8dSFan Du * is more preferable. 489425a91d8dSFan Du */ 489525a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 489625a91d8dSFan Du int offset, int len) 489725a91d8dSFan Du { 489848a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 489925a91d8dSFan Du } 490025a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 490125a91d8dSFan Du 490251c739d1SDavid S. Miller 490351c739d1SDavid S. Miller 4904716ea3a7SDavid Howells /** 4905716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4906716ea3a7SDavid Howells * @skb: The socket buffer to check. 4907716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4908716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4909716ea3a7SDavid Howells * 4910716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4911716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4912716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4913716ea3a7SDavid Howells * 4914716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4915716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4916716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4917716ea3a7SDavid Howells * 4918716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4919716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4920716ea3a7SDavid Howells */ 4921716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4922716ea3a7SDavid Howells { 4923716ea3a7SDavid Howells int copyflag; 4924716ea3a7SDavid Howells int elt; 4925716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4926716ea3a7SDavid Howells 4927716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4928716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4929716ea3a7SDavid Howells * at the moment even if they are anonymous). 4930716ea3a7SDavid Howells */ 4931716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4932c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4933716ea3a7SDavid Howells return -ENOMEM; 4934716ea3a7SDavid Howells 4935716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 493621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4937716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4938716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4939716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4940716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4941716ea3a7SDavid Howells 4942716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4943716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4944716ea3a7SDavid Howells return -ENOMEM; 4945716ea3a7SDavid Howells 4946716ea3a7SDavid Howells /* Voila! */ 4947716ea3a7SDavid Howells *trailer = skb; 4948716ea3a7SDavid Howells return 1; 4949716ea3a7SDavid Howells } 4950716ea3a7SDavid Howells 4951716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4952716ea3a7SDavid Howells 4953716ea3a7SDavid Howells elt = 1; 4954716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4955716ea3a7SDavid Howells copyflag = 0; 4956716ea3a7SDavid Howells 4957716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4958716ea3a7SDavid Howells int ntail = 0; 4959716ea3a7SDavid Howells 4960716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4961716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4962716ea3a7SDavid Howells * after it. */ 4963716ea3a7SDavid Howells 4964716ea3a7SDavid Howells if (skb_shared(skb1)) 4965716ea3a7SDavid Howells copyflag = 1; 4966716ea3a7SDavid Howells 4967716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4968716ea3a7SDavid Howells 4969716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4970716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 497121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4972716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4973716ea3a7SDavid Howells ntail = tailbits + 128; 4974716ea3a7SDavid Howells } 4975716ea3a7SDavid Howells 4976716ea3a7SDavid Howells if (copyflag || 4977716ea3a7SDavid Howells skb_cloned(skb1) || 4978716ea3a7SDavid Howells ntail || 4979716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 498021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4981716ea3a7SDavid Howells struct sk_buff *skb2; 4982716ea3a7SDavid Howells 4983716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4984716ea3a7SDavid Howells if (ntail == 0) 4985716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4986716ea3a7SDavid Howells else 4987716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4988716ea3a7SDavid Howells skb_headroom(skb1), 4989716ea3a7SDavid Howells ntail, 4990716ea3a7SDavid Howells GFP_ATOMIC); 4991716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4992716ea3a7SDavid Howells return -ENOMEM; 4993716ea3a7SDavid Howells 4994716ea3a7SDavid Howells if (skb1->sk) 4995716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4996716ea3a7SDavid Howells 4997716ea3a7SDavid Howells /* Looking around. Are we still alive? 4998716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4999716ea3a7SDavid Howells 5000716ea3a7SDavid Howells skb2->next = skb1->next; 5001716ea3a7SDavid Howells *skb_p = skb2; 5002716ea3a7SDavid Howells kfree_skb(skb1); 5003716ea3a7SDavid Howells skb1 = skb2; 5004716ea3a7SDavid Howells } 5005716ea3a7SDavid Howells elt++; 5006716ea3a7SDavid Howells *trailer = skb1; 5007716ea3a7SDavid Howells skb_p = &skb1->next; 5008716ea3a7SDavid Howells } 5009716ea3a7SDavid Howells 5010716ea3a7SDavid Howells return elt; 5011716ea3a7SDavid Howells } 5012b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 5013716ea3a7SDavid Howells 5014b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 5015b1faf566SEric Dumazet { 5016b1faf566SEric Dumazet struct sock *sk = skb->sk; 5017b1faf566SEric Dumazet 5018b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 5019b1faf566SEric Dumazet } 5020b1faf566SEric Dumazet 50218605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 50228605330aSSoheil Hassas Yeganeh { 50238605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 50248605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 50258605330aSSoheil Hassas Yeganeh */ 50268605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 50278605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 50288605330aSSoheil Hassas Yeganeh } 50298605330aSSoheil Hassas Yeganeh 5030b1faf566SEric Dumazet /* 5031b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 5032b1faf566SEric Dumazet */ 5033b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 5034b1faf566SEric Dumazet { 5035b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 5036ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 5037b1faf566SEric Dumazet return -ENOMEM; 5038b1faf566SEric Dumazet 5039b1faf566SEric Dumazet skb_orphan(skb); 5040b1faf566SEric Dumazet skb->sk = sk; 5041b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 5042b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 50438605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 5044b1faf566SEric Dumazet 5045abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 5046abb57ea4SEric Dumazet skb_dst_force(skb); 5047abb57ea4SEric Dumazet 5048b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 5049b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 5050e3ae2365SAlexander Aring sk_error_report(sk); 5051b1faf566SEric Dumazet return 0; 5052b1faf566SEric Dumazet } 5053b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 5054b1faf566SEric Dumazet 505583a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 505683a1a1a7SSoheil Hassas Yeganeh { 505783a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 505883a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 505983a1a1a7SSoheil Hassas Yeganeh } 506083a1a1a7SSoheil Hassas Yeganeh 5061364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 5062364a9e93SWillem de Bruijn { 5063364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 506483a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 506583a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 5066997d5c3fSEric Dumazet unsigned long flags; 5067364a9e93SWillem de Bruijn 5068997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 5069364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 507038b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 507183a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 507238b25793SSoheil Hassas Yeganeh if (icmp_next) 5073985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 507438b25793SSoheil Hassas Yeganeh } 5075997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 5076364a9e93SWillem de Bruijn 507783a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 507883a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 507983a1a1a7SSoheil Hassas Yeganeh 508083a1a1a7SSoheil Hassas Yeganeh if (skb_next) 5081e3ae2365SAlexander Aring sk_error_report(sk); 5082364a9e93SWillem de Bruijn 5083364a9e93SWillem de Bruijn return skb; 5084364a9e93SWillem de Bruijn } 5085364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 5086364a9e93SWillem de Bruijn 5087cab41c47SAlexander Duyck /** 5088cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 5089cab41c47SAlexander Duyck * @skb: the skb to clone 5090cab41c47SAlexander Duyck * 5091cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 5092cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 5093cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 5094cab41c47SAlexander Duyck * 5095cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 5096cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 5097cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 5098cab41c47SAlexander Duyck * the sk_error_queue. 5099cab41c47SAlexander Duyck */ 510062bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 510162bccb8cSAlexander Duyck { 510262bccb8cSAlexander Duyck struct sock *sk = skb->sk; 510362bccb8cSAlexander Duyck struct sk_buff *clone; 510462bccb8cSAlexander Duyck 510541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 510662bccb8cSAlexander Duyck return NULL; 510762bccb8cSAlexander Duyck 510862bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 510962bccb8cSAlexander Duyck if (!clone) { 511062bccb8cSAlexander Duyck sock_put(sk); 511162bccb8cSAlexander Duyck return NULL; 511262bccb8cSAlexander Duyck } 511362bccb8cSAlexander Duyck 511462bccb8cSAlexander Duyck clone->sk = sk; 511562bccb8cSAlexander Duyck clone->destructor = sock_efree; 511662bccb8cSAlexander Duyck 511762bccb8cSAlexander Duyck return clone; 511862bccb8cSAlexander Duyck } 511962bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 512062bccb8cSAlexander Duyck 512137846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 512237846ef0SAlexander Duyck struct sock *sk, 51234ef1b286SSoheil Hassas Yeganeh int tstype, 51244ef1b286SSoheil Hassas Yeganeh bool opt_stats) 5125ac45f602SPatrick Ohly { 5126ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 5127ac45f602SPatrick Ohly int err; 5128ac45f602SPatrick Ohly 51294ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 51304ef1b286SSoheil Hassas Yeganeh 5131ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5132ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5133ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5134ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5135e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 51364ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 51371862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 51384ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 513909c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 514042f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5141a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 51424ed2d765SWillem de Bruijn } 514329030374SEric Dumazet 5144ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 514529030374SEric Dumazet 5146ac45f602SPatrick Ohly if (err) 5147ac45f602SPatrick Ohly kfree_skb(skb); 5148ac45f602SPatrick Ohly } 514937846ef0SAlexander Duyck 5150b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5151b245be1fSWillem de Bruijn { 5152b245be1fSWillem de Bruijn bool ret; 5153b245be1fSWillem de Bruijn 5154d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5155b245be1fSWillem de Bruijn return true; 5156b245be1fSWillem de Bruijn 5157b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5158b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5159b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5160b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5161b245be1fSWillem de Bruijn return ret; 5162b245be1fSWillem de Bruijn } 5163b245be1fSWillem de Bruijn 516437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 516537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 516637846ef0SAlexander Duyck { 516737846ef0SAlexander Duyck struct sock *sk = skb->sk; 516837846ef0SAlexander Duyck 5169b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 517035b99dffSWillem de Bruijn goto err; 5171b245be1fSWillem de Bruijn 51729ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 51739ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 51749ac25fc0SEric Dumazet */ 517541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 517637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 51774ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 517837846ef0SAlexander Duyck sock_put(sk); 517935b99dffSWillem de Bruijn return; 518037846ef0SAlexander Duyck } 518135b99dffSWillem de Bruijn 518235b99dffSWillem de Bruijn err: 518335b99dffSWillem de Bruijn kfree_skb(skb); 51849ac25fc0SEric Dumazet } 518537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 518637846ef0SAlexander Duyck 518737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5188e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 518937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 519037846ef0SAlexander Duyck struct sock *sk, int tstype) 519137846ef0SAlexander Duyck { 519237846ef0SAlexander Duyck struct sk_buff *skb; 51934ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 519437846ef0SAlexander Duyck 51953a8dd971SWillem de Bruijn if (!sk) 51963a8dd971SWillem de Bruijn return; 51973a8dd971SWillem de Bruijn 5198b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5199b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5200b50a5c70SMiroslav Lichvar return; 5201b50a5c70SMiroslav Lichvar 52023a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 52033a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 520437846ef0SAlexander Duyck return; 520537846ef0SAlexander Duyck 52061c885808SFrancis Yan if (tsonly) { 52071c885808SFrancis Yan #ifdef CONFIG_INET 52081c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 520942f67eeaSEric Dumazet sk_is_tcp(sk)) { 5210e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5211e7ed11eeSYousuk Seung ack_skb); 52124ef1b286SSoheil Hassas Yeganeh opt_stats = true; 52134ef1b286SSoheil Hassas Yeganeh } else 52141c885808SFrancis Yan #endif 52151c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 52161c885808SFrancis Yan } else { 521737846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 52181c885808SFrancis Yan } 521937846ef0SAlexander Duyck if (!skb) 522037846ef0SAlexander Duyck return; 522137846ef0SAlexander Duyck 522249ca0d8bSWillem de Bruijn if (tsonly) { 5223fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5224fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 522549ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 522649ca0d8bSWillem de Bruijn } 522749ca0d8bSWillem de Bruijn 522849ca0d8bSWillem de Bruijn if (hwtstamps) 522949ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 523049ca0d8bSWillem de Bruijn else 5231d93376f5SMartin KaFai Lau __net_timestamp(skb); 523249ca0d8bSWillem de Bruijn 52334ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 523437846ef0SAlexander Duyck } 5235e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5236e7fd2885SWillem de Bruijn 5237e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5238e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5239e7fd2885SWillem de Bruijn { 5240e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5241e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5242e7fd2885SWillem de Bruijn } 5243ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5244ac45f602SPatrick Ohly 5245eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS 52466e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 52476e3e939fSJohannes Berg { 52486e3e939fSJohannes Berg struct sock *sk = skb->sk; 52496e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5250dd4f1072SEric Dumazet int err = 1; 52516e3e939fSJohannes Berg 52526e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 52536e3e939fSJohannes Berg skb->wifi_acked = acked; 52546e3e939fSJohannes Berg 52556e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 52566e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 52576e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 52586e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 52596e3e939fSJohannes Berg 5260dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5261dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5262dd4f1072SEric Dumazet */ 526341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 52646e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5265dd4f1072SEric Dumazet sock_put(sk); 5266dd4f1072SEric Dumazet } 52676e3e939fSJohannes Berg if (err) 52686e3e939fSJohannes Berg kfree_skb(skb); 52696e3e939fSJohannes Berg } 52706e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 5271eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */ 52726e3e939fSJohannes Berg 5273f35d9d8aSRusty Russell /** 5274f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5275f35d9d8aSRusty Russell * @skb: the skb to set 5276f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5277f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5278f35d9d8aSRusty Russell * 5279f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5280f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5281f35d9d8aSRusty Russell * 5282f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5283f35d9d8aSRusty Russell * returns false you should drop the packet. 5284f35d9d8aSRusty Russell */ 5285f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5286f35d9d8aSRusty Russell { 528752b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 528852b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 528952b5d6f5SEric Dumazet 529052b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 529152b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 529252b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5293f35d9d8aSRusty Russell return false; 5294f35d9d8aSRusty Russell } 5295f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 529652b5d6f5SEric Dumazet skb->csum_start = csum_start; 5297f35d9d8aSRusty Russell skb->csum_offset = off; 5298e5d5decaSJason Wang skb_set_transport_header(skb, start); 5299f35d9d8aSRusty Russell return true; 5300f35d9d8aSRusty Russell } 5301b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5302f35d9d8aSRusty Russell 5303ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5304ed1f50c3SPaul Durrant unsigned int max) 5305ed1f50c3SPaul Durrant { 5306ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5307ed1f50c3SPaul Durrant return 0; 5308ed1f50c3SPaul Durrant 5309ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5310ed1f50c3SPaul Durrant * won't need to do it again. 5311ed1f50c3SPaul Durrant */ 5312ed1f50c3SPaul Durrant if (max > skb->len) 5313ed1f50c3SPaul Durrant max = skb->len; 5314ed1f50c3SPaul Durrant 5315ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5316ed1f50c3SPaul Durrant return -ENOMEM; 5317ed1f50c3SPaul Durrant 5318ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5319ed1f50c3SPaul Durrant return -EPROTO; 5320ed1f50c3SPaul Durrant 5321ed1f50c3SPaul Durrant return 0; 5322ed1f50c3SPaul Durrant } 5323ed1f50c3SPaul Durrant 5324f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5325f9708b43SJan Beulich 5326f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5327f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5328f9708b43SJan Beulich unsigned int off) 5329f9708b43SJan Beulich { 5330f9708b43SJan Beulich int err; 5331f9708b43SJan Beulich 5332161d1792SKees Cook switch (proto) { 5333f9708b43SJan Beulich case IPPROTO_TCP: 5334f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5335f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5336f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5337f9708b43SJan Beulich offsetof(struct tcphdr, 5338f9708b43SJan Beulich check))) 5339f9708b43SJan Beulich err = -EPROTO; 5340f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5341f9708b43SJan Beulich 5342f9708b43SJan Beulich case IPPROTO_UDP: 5343f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5344f9708b43SJan Beulich off + sizeof(struct udphdr)); 5345f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5346f9708b43SJan Beulich offsetof(struct udphdr, 5347f9708b43SJan Beulich check))) 5348f9708b43SJan Beulich err = -EPROTO; 5349f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5350f9708b43SJan Beulich } 5351f9708b43SJan Beulich 5352f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5353f9708b43SJan Beulich } 5354f9708b43SJan Beulich 5355ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5356ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5357ed1f50c3SPaul Durrant */ 5358ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5359ed1f50c3SPaul Durrant 5360f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5361ed1f50c3SPaul Durrant { 5362ed1f50c3SPaul Durrant unsigned int off; 5363ed1f50c3SPaul Durrant bool fragment; 5364f9708b43SJan Beulich __sum16 *csum; 5365ed1f50c3SPaul Durrant int err; 5366ed1f50c3SPaul Durrant 5367ed1f50c3SPaul Durrant fragment = false; 5368ed1f50c3SPaul Durrant 5369ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5370ed1f50c3SPaul Durrant sizeof(struct iphdr), 5371ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5372ed1f50c3SPaul Durrant if (err < 0) 5373ed1f50c3SPaul Durrant goto out; 5374ed1f50c3SPaul Durrant 537511f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5376ed1f50c3SPaul Durrant fragment = true; 5377ed1f50c3SPaul Durrant 5378ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5379ed1f50c3SPaul Durrant 5380ed1f50c3SPaul Durrant err = -EPROTO; 5381ed1f50c3SPaul Durrant 5382ed1f50c3SPaul Durrant if (fragment) 5383ed1f50c3SPaul Durrant goto out; 5384ed1f50c3SPaul Durrant 5385f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5386f9708b43SJan Beulich if (IS_ERR(csum)) 5387f9708b43SJan Beulich return PTR_ERR(csum); 5388ed1f50c3SPaul Durrant 5389ed1f50c3SPaul Durrant if (recalculate) 5390f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5391ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5392ed1f50c3SPaul Durrant skb->len - off, 5393f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5394ed1f50c3SPaul Durrant err = 0; 5395ed1f50c3SPaul Durrant 5396ed1f50c3SPaul Durrant out: 5397ed1f50c3SPaul Durrant return err; 5398ed1f50c3SPaul Durrant } 5399ed1f50c3SPaul Durrant 5400ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5401ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5402ed1f50c3SPaul Durrant */ 5403ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5404ed1f50c3SPaul Durrant 5405ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5406ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5407ed1f50c3SPaul Durrant 5408ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5409ed1f50c3SPaul Durrant { 5410ed1f50c3SPaul Durrant int err; 5411ed1f50c3SPaul Durrant u8 nexthdr; 5412ed1f50c3SPaul Durrant unsigned int off; 5413ed1f50c3SPaul Durrant unsigned int len; 5414ed1f50c3SPaul Durrant bool fragment; 5415ed1f50c3SPaul Durrant bool done; 5416f9708b43SJan Beulich __sum16 *csum; 5417ed1f50c3SPaul Durrant 5418ed1f50c3SPaul Durrant fragment = false; 5419ed1f50c3SPaul Durrant done = false; 5420ed1f50c3SPaul Durrant 5421ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5422ed1f50c3SPaul Durrant 5423ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5424ed1f50c3SPaul Durrant if (err < 0) 5425ed1f50c3SPaul Durrant goto out; 5426ed1f50c3SPaul Durrant 5427ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5428ed1f50c3SPaul Durrant 5429ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5430ed1f50c3SPaul Durrant while (off <= len && !done) { 5431ed1f50c3SPaul Durrant switch (nexthdr) { 5432ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5433ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5434ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5435ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5436ed1f50c3SPaul Durrant 5437ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5438ed1f50c3SPaul Durrant off + 5439ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5440ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5441ed1f50c3SPaul Durrant if (err < 0) 5442ed1f50c3SPaul Durrant goto out; 5443ed1f50c3SPaul Durrant 5444ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5445ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5446ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5447ed1f50c3SPaul Durrant break; 5448ed1f50c3SPaul Durrant } 5449ed1f50c3SPaul Durrant case IPPROTO_AH: { 5450ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5451ed1f50c3SPaul Durrant 5452ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5453ed1f50c3SPaul Durrant off + 5454ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5455ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5456ed1f50c3SPaul Durrant if (err < 0) 5457ed1f50c3SPaul Durrant goto out; 5458ed1f50c3SPaul Durrant 5459ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5460ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5461ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5462ed1f50c3SPaul Durrant break; 5463ed1f50c3SPaul Durrant } 5464ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5465ed1f50c3SPaul Durrant struct frag_hdr *hp; 5466ed1f50c3SPaul Durrant 5467ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5468ed1f50c3SPaul Durrant off + 5469ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5470ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5471ed1f50c3SPaul Durrant if (err < 0) 5472ed1f50c3SPaul Durrant goto out; 5473ed1f50c3SPaul Durrant 5474ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5475ed1f50c3SPaul Durrant 5476ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5477ed1f50c3SPaul Durrant fragment = true; 5478ed1f50c3SPaul Durrant 5479ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5480ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5481ed1f50c3SPaul Durrant break; 5482ed1f50c3SPaul Durrant } 5483ed1f50c3SPaul Durrant default: 5484ed1f50c3SPaul Durrant done = true; 5485ed1f50c3SPaul Durrant break; 5486ed1f50c3SPaul Durrant } 5487ed1f50c3SPaul Durrant } 5488ed1f50c3SPaul Durrant 5489ed1f50c3SPaul Durrant err = -EPROTO; 5490ed1f50c3SPaul Durrant 5491ed1f50c3SPaul Durrant if (!done || fragment) 5492ed1f50c3SPaul Durrant goto out; 5493ed1f50c3SPaul Durrant 5494f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5495f9708b43SJan Beulich if (IS_ERR(csum)) 5496f9708b43SJan Beulich return PTR_ERR(csum); 5497ed1f50c3SPaul Durrant 5498ed1f50c3SPaul Durrant if (recalculate) 5499f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5500ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5501f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5502ed1f50c3SPaul Durrant err = 0; 5503ed1f50c3SPaul Durrant 5504ed1f50c3SPaul Durrant out: 5505ed1f50c3SPaul Durrant return err; 5506ed1f50c3SPaul Durrant } 5507ed1f50c3SPaul Durrant 5508ed1f50c3SPaul Durrant /** 5509ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5510ed1f50c3SPaul Durrant * @skb: the skb to set up 5511ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5512ed1f50c3SPaul Durrant */ 5513ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5514ed1f50c3SPaul Durrant { 5515ed1f50c3SPaul Durrant int err; 5516ed1f50c3SPaul Durrant 5517ed1f50c3SPaul Durrant switch (skb->protocol) { 5518ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5519f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5520ed1f50c3SPaul Durrant break; 5521ed1f50c3SPaul Durrant 5522ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5523ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5524ed1f50c3SPaul Durrant break; 5525ed1f50c3SPaul Durrant 5526ed1f50c3SPaul Durrant default: 5527ed1f50c3SPaul Durrant err = -EPROTO; 5528ed1f50c3SPaul Durrant break; 5529ed1f50c3SPaul Durrant } 5530ed1f50c3SPaul Durrant 5531ed1f50c3SPaul Durrant return err; 5532ed1f50c3SPaul Durrant } 5533ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5534ed1f50c3SPaul Durrant 55359afd85c9SLinus Lüssing /** 55369afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 55379afd85c9SLinus Lüssing * @skb: the skb to check 55389afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55399afd85c9SLinus Lüssing * 55409afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 55419afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 55429afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 55439afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 55449afd85c9SLinus Lüssing * 5545a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5546a516993fSLinus Lüssing * differs from the provided skb. 55479afd85c9SLinus Lüssing */ 55489afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 55499afd85c9SLinus Lüssing unsigned int transport_len) 55509afd85c9SLinus Lüssing { 55519afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55529afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 55539afd85c9SLinus Lüssing int ret; 55549afd85c9SLinus Lüssing 5555a516993fSLinus Lüssing if (skb->len < len) 55569afd85c9SLinus Lüssing return NULL; 5557a516993fSLinus Lüssing else if (skb->len == len) 55589afd85c9SLinus Lüssing return skb; 55599afd85c9SLinus Lüssing 55609afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 55619afd85c9SLinus Lüssing if (!skb_chk) 55629afd85c9SLinus Lüssing return NULL; 55639afd85c9SLinus Lüssing 55649afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 55659afd85c9SLinus Lüssing if (ret) { 55669afd85c9SLinus Lüssing kfree_skb(skb_chk); 55679afd85c9SLinus Lüssing return NULL; 55689afd85c9SLinus Lüssing } 55699afd85c9SLinus Lüssing 55709afd85c9SLinus Lüssing return skb_chk; 55719afd85c9SLinus Lüssing } 55729afd85c9SLinus Lüssing 55739afd85c9SLinus Lüssing /** 55749afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 55759afd85c9SLinus Lüssing * @skb: the skb to check 55769afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55779afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 55789afd85c9SLinus Lüssing * 55799afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 55809afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 55819afd85c9SLinus Lüssing * 55829afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 55839afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 55849afd85c9SLinus Lüssing * 5585a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5586a516993fSLinus Lüssing * differs from the provided skb. 55879afd85c9SLinus Lüssing */ 55889afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 55899afd85c9SLinus Lüssing unsigned int transport_len, 55909afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 55919afd85c9SLinus Lüssing { 55929afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55939afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5594fcba67c9SLinus Lüssing __sum16 ret; 55959afd85c9SLinus Lüssing 55969afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 55979afd85c9SLinus Lüssing if (!skb_chk) 5598a516993fSLinus Lüssing goto err; 55999afd85c9SLinus Lüssing 5600a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5601a516993fSLinus Lüssing goto err; 56029afd85c9SLinus Lüssing 56039b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 56049afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 56059b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 56069afd85c9SLinus Lüssing 5607a516993fSLinus Lüssing if (ret) 5608a516993fSLinus Lüssing goto err; 56099afd85c9SLinus Lüssing 56109afd85c9SLinus Lüssing return skb_chk; 5611a516993fSLinus Lüssing 5612a516993fSLinus Lüssing err: 5613a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5614a516993fSLinus Lüssing kfree_skb(skb_chk); 5615a516993fSLinus Lüssing 5616a516993fSLinus Lüssing return NULL; 5617a516993fSLinus Lüssing 56189afd85c9SLinus Lüssing } 56199afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 56209afd85c9SLinus Lüssing 56214497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 56224497b076SBen Hutchings { 5623e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5624e87cc472SJoe Perches skb->dev->name); 56254497b076SBen Hutchings } 56264497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5627bad43ca8SEric Dumazet 5628bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5629bad43ca8SEric Dumazet { 56303d861f66SEric Dumazet if (head_stolen) { 56313d861f66SEric Dumazet skb_release_head_state(skb); 5632025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 56333d861f66SEric Dumazet } else { 5634bad43ca8SEric Dumazet __kfree_skb(skb); 5635bad43ca8SEric Dumazet } 56363d861f66SEric Dumazet } 5637bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5638bad43ca8SEric Dumazet 5639bad43ca8SEric Dumazet /** 5640bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5641bad43ca8SEric Dumazet * @to: prior buffer 5642bad43ca8SEric Dumazet * @from: buffer to add 5643bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5644c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5645bad43ca8SEric Dumazet */ 5646bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5647bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5648bad43ca8SEric Dumazet { 5649c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5650bad43ca8SEric Dumazet int i, delta, len = from->len; 5651bad43ca8SEric Dumazet 5652bad43ca8SEric Dumazet *fragstolen = false; 5653bad43ca8SEric Dumazet 5654bad43ca8SEric Dumazet if (skb_cloned(to)) 5655bad43ca8SEric Dumazet return false; 5656bad43ca8SEric Dumazet 56570646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated 56580646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones 56590646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment 56601effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 56611effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 56621effe8caSJean-Philippe Brucker * inconsistent reference counts. 56630646dc31SLiang Chen * In theory we could take full references if @from is cloned and 56640646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with 56650646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with. 56666a5bcd84SIlias Apalodimas */ 56670646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle || 56680646dc31SLiang Chen (from->pp_recycle && skb_cloned(from))) 56696a5bcd84SIlias Apalodimas return false; 56706a5bcd84SIlias Apalodimas 5671bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5672e93a0435SEric Dumazet if (len) 5673bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5674bad43ca8SEric Dumazet *delta_truesize = 0; 5675bad43ca8SEric Dumazet return true; 5676bad43ca8SEric Dumazet } 5677bad43ca8SEric Dumazet 5678c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5679c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5680c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5681bad43ca8SEric Dumazet return false; 56821f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 56831f8b977aSWillem de Bruijn return false; 5684bad43ca8SEric Dumazet 5685bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5686bad43ca8SEric Dumazet struct page *page; 5687bad43ca8SEric Dumazet unsigned int offset; 5688bad43ca8SEric Dumazet 5689c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5690c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5691bad43ca8SEric Dumazet return false; 5692bad43ca8SEric Dumazet 5693bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5694bad43ca8SEric Dumazet return false; 5695bad43ca8SEric Dumazet 5696bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5697bad43ca8SEric Dumazet 5698bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5699bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5700bad43ca8SEric Dumazet 5701c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5702bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5703bad43ca8SEric Dumazet *fragstolen = true; 5704bad43ca8SEric Dumazet } else { 5705c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5706c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5707bad43ca8SEric Dumazet return false; 5708bad43ca8SEric Dumazet 5709f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5710bad43ca8SEric Dumazet } 5711bad43ca8SEric Dumazet 5712bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5713bad43ca8SEric Dumazet 5714c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5715c818fa9eSEric Dumazet from_shinfo->frags, 5716c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5717c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5718bad43ca8SEric Dumazet 5719bad43ca8SEric Dumazet if (!skb_cloned(from)) 5720c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5721bad43ca8SEric Dumazet 57228ea853fdSLi RongQing /* if the skb is not cloned this does nothing 57238ea853fdSLi RongQing * since we set nr_frags to 0. 57248ea853fdSLi RongQing */ 5725c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5726c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5727bad43ca8SEric Dumazet 5728bad43ca8SEric Dumazet to->truesize += delta; 5729bad43ca8SEric Dumazet to->len += len; 5730bad43ca8SEric Dumazet to->data_len += len; 5731bad43ca8SEric Dumazet 5732bad43ca8SEric Dumazet *delta_truesize = delta; 5733bad43ca8SEric Dumazet return true; 5734bad43ca8SEric Dumazet } 5735bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5736621e84d6SNicolas Dichtel 5737621e84d6SNicolas Dichtel /** 57388b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5739621e84d6SNicolas Dichtel * 5740621e84d6SNicolas Dichtel * @skb: buffer to clean 57418b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5742621e84d6SNicolas Dichtel * 57438b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 57448b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 57458b27f277SNicolas Dichtel * operations. 57468b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 57478b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 57488b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5749621e84d6SNicolas Dichtel */ 57508b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5751621e84d6SNicolas Dichtel { 5752621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5753621e84d6SNicolas Dichtel skb->skb_iif = 0; 575460ff7467SWANG Cong skb->ignore_df = 0; 5755621e84d6SNicolas Dichtel skb_dst_drop(skb); 5756174e2381SFlorian Westphal skb_ext_reset(skb); 5757895b5c9fSFlorian Westphal nf_reset_ct(skb); 5758621e84d6SNicolas Dichtel nf_reset_trace(skb); 5759213dd74aSHerbert Xu 57606f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 57616f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5762875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 57636f9a5069SPetr Machata #endif 57646f9a5069SPetr Machata 5765213dd74aSHerbert Xu if (!xnet) 5766213dd74aSHerbert Xu return; 5767213dd74aSHerbert Xu 57682b5ec1a5SYe Yin ipvs_reset(skb); 5769213dd74aSHerbert Xu skb->mark = 0; 5770de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5771621e84d6SNicolas Dichtel } 5772621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5773de960aa9SFlorian Westphal 5774de960aa9SFlorian Westphal /** 5775de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5776de960aa9SFlorian Westphal * 5777de960aa9SFlorian Westphal * @skb: GSO skb 5778de960aa9SFlorian Westphal * 5779de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5780de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5781de960aa9SFlorian Westphal * 5782de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5783de960aa9SFlorian Westphal */ 5784a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5785de960aa9SFlorian Westphal { 5786de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5787f993bc25SFlorian Westphal unsigned int thlen = 0; 5788f993bc25SFlorian Westphal 5789f993bc25SFlorian Westphal if (skb->encapsulation) { 5790f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5791f993bc25SFlorian Westphal skb_transport_header(skb); 5792de960aa9SFlorian Westphal 5793de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5794f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5795f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5796f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 57971dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 579890017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5799ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5800ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5801f993bc25SFlorian Westphal } 58026d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 58036d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 58046d39d589SFlorian Westphal * accounted for. 58056d39d589SFlorian Westphal */ 5806f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5807de960aa9SFlorian Westphal } 5808a4a77718SDaniel Axtens 5809a4a77718SDaniel Axtens /** 5810a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5811a4a77718SDaniel Axtens * 5812a4a77718SDaniel Axtens * @skb: GSO skb 5813a4a77718SDaniel Axtens * 5814a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5815a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5816a4a77718SDaniel Axtens * 5817a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5818a4a77718SDaniel Axtens */ 5819a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5820a4a77718SDaniel Axtens { 5821a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5822a4a77718SDaniel Axtens skb_network_header(skb); 5823a4a77718SDaniel Axtens 5824a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5825a4a77718SDaniel Axtens } 5826a4a77718SDaniel Axtens 5827a4a77718SDaniel Axtens /** 5828a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5829a4a77718SDaniel Axtens * 5830a4a77718SDaniel Axtens * @skb: GSO skb 5831a4a77718SDaniel Axtens * 5832a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5833a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5834a4a77718SDaniel Axtens * headers (TCP/UDP). 5835a4a77718SDaniel Axtens */ 5836a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5837a4a77718SDaniel Axtens { 5838a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5839a4a77718SDaniel Axtens 5840a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5841a4a77718SDaniel Axtens } 58420d5501c1SVlad Yasevich 5843ae7ef81eSMarcelo Ricardo Leitner /** 58442b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 58452b16f048SDaniel Axtens * 58462b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 58472b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 58482b16f048SDaniel Axtens * 58492b16f048SDaniel Axtens * We might want to check: 58502b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 58512b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 58522b16f048SDaniel Axtens * 58532b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 58542b16f048SDaniel Axtens * 585549682bfaSMathieu Malaterre * @skb: GSO skb 585649682bfaSMathieu Malaterre * 58572b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 58582b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 58592b16f048SDaniel Axtens * 58602b16f048SDaniel Axtens * @max_len: The maximum permissible length. 58612b16f048SDaniel Axtens * 58622b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 58632b16f048SDaniel Axtens */ 58642b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 58652b16f048SDaniel Axtens unsigned int seg_len, 58662b16f048SDaniel Axtens unsigned int max_len) { 58672b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 58682b16f048SDaniel Axtens const struct sk_buff *iter; 58692b16f048SDaniel Axtens 58702b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 58712b16f048SDaniel Axtens return seg_len <= max_len; 58722b16f048SDaniel Axtens 58732b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 58742b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 58752b16f048SDaniel Axtens 58762b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 58772b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 58782b16f048SDaniel Axtens return false; 58792b16f048SDaniel Axtens } 58802b16f048SDaniel Axtens 58812b16f048SDaniel Axtens return true; 58822b16f048SDaniel Axtens } 58832b16f048SDaniel Axtens 58842b16f048SDaniel Axtens /** 5885779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5886ae7ef81eSMarcelo Ricardo Leitner * 5887ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 588876f21b99SDavid S. Miller * @mtu: MTU to validate against 5889ae7ef81eSMarcelo Ricardo Leitner * 5890779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5891779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5892779b7931SDaniel Axtens * payload. 5893ae7ef81eSMarcelo Ricardo Leitner */ 5894779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5895ae7ef81eSMarcelo Ricardo Leitner { 58962b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5897ae7ef81eSMarcelo Ricardo Leitner } 5898779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5899ae7ef81eSMarcelo Ricardo Leitner 59002b16f048SDaniel Axtens /** 59012b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 59022b16f048SDaniel Axtens * 59032b16f048SDaniel Axtens * @skb: GSO skb 59042b16f048SDaniel Axtens * @len: length to validate against 59052b16f048SDaniel Axtens * 59062b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 59072b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 59082b16f048SDaniel Axtens */ 59092b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 59102b16f048SDaniel Axtens { 59112b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 59122b16f048SDaniel Axtens } 59132b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 59142b16f048SDaniel Axtens 59150d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 59160d5501c1SVlad Yasevich { 5917d85e8be2SYuya Kusakabe int mac_len, meta_len; 5918d85e8be2SYuya Kusakabe void *meta; 59194bbb3e0eSToshiaki Makita 59200d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 59210d5501c1SVlad Yasevich kfree_skb(skb); 59220d5501c1SVlad Yasevich return NULL; 59230d5501c1SVlad Yasevich } 59240d5501c1SVlad Yasevich 59254bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5926ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 59274bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 59284bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5929ae474573SToshiaki Makita } 5930d85e8be2SYuya Kusakabe 5931d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5932d85e8be2SYuya Kusakabe if (meta_len) { 5933d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5934d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5935d85e8be2SYuya Kusakabe } 5936d85e8be2SYuya Kusakabe 59370d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 59380d5501c1SVlad Yasevich return skb; 59390d5501c1SVlad Yasevich } 59400d5501c1SVlad Yasevich 59410d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 59420d5501c1SVlad Yasevich { 59430d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 59440d5501c1SVlad Yasevich u16 vlan_tci; 59450d5501c1SVlad Yasevich 5946df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 59470d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 59480d5501c1SVlad Yasevich return skb; 59490d5501c1SVlad Yasevich } 59500d5501c1SVlad Yasevich 59510d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 59520d5501c1SVlad Yasevich if (unlikely(!skb)) 59530d5501c1SVlad Yasevich goto err_free; 595455eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 595555eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 59560d5501c1SVlad Yasevich goto err_free; 59570d5501c1SVlad Yasevich 59580d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 59590d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 59600d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 59610d5501c1SVlad Yasevich 59620d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 59630d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 59640d5501c1SVlad Yasevich 59650d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 59660d5501c1SVlad Yasevich if (unlikely(!skb)) 59670d5501c1SVlad Yasevich goto err_free; 59680d5501c1SVlad Yasevich 59690d5501c1SVlad Yasevich skb_reset_network_header(skb); 59708be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 59710d5501c1SVlad Yasevich skb_reset_transport_header(skb); 59720d5501c1SVlad Yasevich skb_reset_mac_len(skb); 59730d5501c1SVlad Yasevich 59740d5501c1SVlad Yasevich return skb; 59750d5501c1SVlad Yasevich 59760d5501c1SVlad Yasevich err_free: 59770d5501c1SVlad Yasevich kfree_skb(skb); 59780d5501c1SVlad Yasevich return NULL; 59790d5501c1SVlad Yasevich } 59800d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 59812e4e4410SEric Dumazet 598292ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5983e2195121SJiri Pirko { 5984e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5985e2195121SJiri Pirko return -ENOMEM; 5986e2195121SJiri Pirko 5987e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5988e2195121SJiri Pirko return 0; 5989e2195121SJiri Pirko 5990e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5991e2195121SJiri Pirko } 5992e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5993e2195121SJiri Pirko 5994bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5995bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5996bfca4c52SShmulik Ladkani */ 5997bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 599893515d53SJiri Pirko { 599993515d53SJiri Pirko struct vlan_hdr *vhdr; 6000b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 600193515d53SJiri Pirko int err; 600293515d53SJiri Pirko 6003b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6004b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 6005b6a79208SShmulik Ladkani offset)) { 6006b6a79208SShmulik Ladkani return -EINVAL; 6007b6a79208SShmulik Ladkani } 6008b6a79208SShmulik Ladkani 600993515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 601093515d53SJiri Pirko if (unlikely(err)) 6011b6a79208SShmulik Ladkani return err; 601293515d53SJiri Pirko 601393515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 601493515d53SJiri Pirko 601593515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 601693515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 601793515d53SJiri Pirko 601893515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 601993515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 602093515d53SJiri Pirko 602193515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 602293515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 602393515d53SJiri Pirko 602493515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 602593515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 602693515d53SJiri Pirko 602793515d53SJiri Pirko skb_reset_mac_len(skb); 602893515d53SJiri Pirko 602993515d53SJiri Pirko return err; 603093515d53SJiri Pirko } 6031bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 603293515d53SJiri Pirko 6033b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 6034b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6035b6a79208SShmulik Ladkani */ 603693515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 603793515d53SJiri Pirko { 603893515d53SJiri Pirko u16 vlan_tci; 603993515d53SJiri Pirko __be16 vlan_proto; 604093515d53SJiri Pirko int err; 604193515d53SJiri Pirko 6042df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 6043b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 604493515d53SJiri Pirko } else { 6045ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 604693515d53SJiri Pirko return 0; 604793515d53SJiri Pirko 604893515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 604993515d53SJiri Pirko if (err) 605093515d53SJiri Pirko return err; 605193515d53SJiri Pirko } 605293515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 6053ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 605493515d53SJiri Pirko return 0; 605593515d53SJiri Pirko 605693515d53SJiri Pirko vlan_proto = skb->protocol; 605793515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 605893515d53SJiri Pirko if (unlikely(err)) 605993515d53SJiri Pirko return err; 606093515d53SJiri Pirko 606193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 606293515d53SJiri Pirko return 0; 606393515d53SJiri Pirko } 606493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 606593515d53SJiri Pirko 6066b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 6067b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6068b6a79208SShmulik Ladkani */ 606993515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 607093515d53SJiri Pirko { 6071df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 6072b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 607393515d53SJiri Pirko int err; 607493515d53SJiri Pirko 6075b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6076b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 6077b6a79208SShmulik Ladkani offset)) { 6078b6a79208SShmulik Ladkani return -EINVAL; 6079b6a79208SShmulik Ladkani } 6080b6a79208SShmulik Ladkani 608193515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 6082df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 6083b6a79208SShmulik Ladkani if (err) 608493515d53SJiri Pirko return err; 60859241e2dfSDaniel Borkmann 608693515d53SJiri Pirko skb->protocol = skb->vlan_proto; 608793515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 608893515d53SJiri Pirko 60896b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 609093515d53SJiri Pirko } 609193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 609293515d53SJiri Pirko return 0; 609393515d53SJiri Pirko } 609493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 609593515d53SJiri Pirko 609619fbcb36SGuillaume Nault /** 609719fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 609819fbcb36SGuillaume Nault * 609919fbcb36SGuillaume Nault * @skb: Socket buffer to modify 610019fbcb36SGuillaume Nault * 610119fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 610219fbcb36SGuillaume Nault * 610319fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 610419fbcb36SGuillaume Nault * present. 610519fbcb36SGuillaume Nault * 610619fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 610719fbcb36SGuillaume Nault */ 610819fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 610919fbcb36SGuillaume Nault { 611019fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 611119fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 611219fbcb36SGuillaume Nault return -EPROTO; 611319fbcb36SGuillaume Nault 611419fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 611519fbcb36SGuillaume Nault skb_reset_mac_header(skb); 611619fbcb36SGuillaume Nault skb_reset_mac_len(skb); 611719fbcb36SGuillaume Nault 611819fbcb36SGuillaume Nault return 0; 611919fbcb36SGuillaume Nault } 612019fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 612119fbcb36SGuillaume Nault 612219fbcb36SGuillaume Nault /** 612319fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 612419fbcb36SGuillaume Nault * 612519fbcb36SGuillaume Nault * @skb: Socket buffer to modify 612619fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 612719fbcb36SGuillaume Nault * @src: Source MAC address of the new header 612819fbcb36SGuillaume Nault * 612919fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 613019fbcb36SGuillaume Nault * 613119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 613219fbcb36SGuillaume Nault * 613319fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 613419fbcb36SGuillaume Nault */ 613519fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 613619fbcb36SGuillaume Nault const unsigned char *src) 613719fbcb36SGuillaume Nault { 613819fbcb36SGuillaume Nault struct ethhdr *eth; 613919fbcb36SGuillaume Nault int err; 614019fbcb36SGuillaume Nault 614119fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 614219fbcb36SGuillaume Nault return -EPROTO; 614319fbcb36SGuillaume Nault 614419fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 614519fbcb36SGuillaume Nault if (err < 0) 614619fbcb36SGuillaume Nault return err; 614719fbcb36SGuillaume Nault 614819fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 614919fbcb36SGuillaume Nault skb_reset_mac_header(skb); 615019fbcb36SGuillaume Nault skb_reset_mac_len(skb); 615119fbcb36SGuillaume Nault 615219fbcb36SGuillaume Nault eth = eth_hdr(skb); 615319fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 615419fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 615519fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 615619fbcb36SGuillaume Nault 615719fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 615819fbcb36SGuillaume Nault 615919fbcb36SGuillaume Nault return 0; 616019fbcb36SGuillaume Nault } 616119fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 616219fbcb36SGuillaume Nault 61638822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 61648822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 61658822e270SJohn Hurley __be16 ethertype) 61668822e270SJohn Hurley { 61678822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 61688822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 61698822e270SJohn Hurley 61708822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 61718822e270SJohn Hurley } 61728822e270SJohn Hurley 61738822e270SJohn Hurley hdr->h_proto = ethertype; 61748822e270SJohn Hurley } 61758822e270SJohn Hurley 61768822e270SJohn Hurley /** 6177e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6178e7dbfed1SMartin Varghese * the packet 61798822e270SJohn Hurley * 61808822e270SJohn Hurley * @skb: buffer 61818822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 61828822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6183fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6184e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6185e7dbfed1SMartin Varghese * ethernet 61868822e270SJohn Hurley * 61878822e270SJohn Hurley * Expects skb->data at mac header. 61888822e270SJohn Hurley * 61898822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 61908822e270SJohn Hurley */ 6191fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6192d04ac224SMartin Varghese int mac_len, bool ethernet) 61938822e270SJohn Hurley { 61948822e270SJohn Hurley struct mpls_shim_hdr *lse; 61958822e270SJohn Hurley int err; 61968822e270SJohn Hurley 61978822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 61988822e270SJohn Hurley return -EINVAL; 61998822e270SJohn Hurley 62008822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 62018822e270SJohn Hurley if (skb->encapsulation) 62028822e270SJohn Hurley return -EINVAL; 62038822e270SJohn Hurley 62048822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 62058822e270SJohn Hurley if (unlikely(err)) 62068822e270SJohn Hurley return err; 62078822e270SJohn Hurley 62088822e270SJohn Hurley if (!skb->inner_protocol) { 6209e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 62108822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 62118822e270SJohn Hurley } 62128822e270SJohn Hurley 62138822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 62148822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6215fa4e0f88SDavide Caratti mac_len); 62168822e270SJohn Hurley skb_reset_mac_header(skb); 6217fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6218e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 62198822e270SJohn Hurley 62208822e270SJohn Hurley lse = mpls_hdr(skb); 62218822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 62228822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 62238822e270SJohn Hurley 62244296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 62258822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 62268822e270SJohn Hurley skb->protocol = mpls_proto; 62278822e270SJohn Hurley 62288822e270SJohn Hurley return 0; 62298822e270SJohn Hurley } 62308822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 62318822e270SJohn Hurley 62322e4e4410SEric Dumazet /** 6233ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6234ed246ceeSJohn Hurley * 6235ed246ceeSJohn Hurley * @skb: buffer 6236ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6237fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 623876f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6239ed246ceeSJohn Hurley * 6240ed246ceeSJohn Hurley * Expects skb->data at mac header. 6241ed246ceeSJohn Hurley * 6242ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6243ed246ceeSJohn Hurley */ 6244040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6245040b5cfbSMartin Varghese bool ethernet) 6246ed246ceeSJohn Hurley { 6247ed246ceeSJohn Hurley int err; 6248ed246ceeSJohn Hurley 6249ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6250dedc5a08SDavide Caratti return 0; 6251ed246ceeSJohn Hurley 6252fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6253ed246ceeSJohn Hurley if (unlikely(err)) 6254ed246ceeSJohn Hurley return err; 6255ed246ceeSJohn Hurley 6256ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6257ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6258fa4e0f88SDavide Caratti mac_len); 6259ed246ceeSJohn Hurley 6260ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6261ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6262fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6263ed246ceeSJohn Hurley 62644296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6265ed246ceeSJohn Hurley struct ethhdr *hdr; 6266ed246ceeSJohn Hurley 6267ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6268ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6269ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6270ed246ceeSJohn Hurley } 6271ed246ceeSJohn Hurley skb->protocol = next_proto; 6272ed246ceeSJohn Hurley 6273ed246ceeSJohn Hurley return 0; 6274ed246ceeSJohn Hurley } 6275ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6276ed246ceeSJohn Hurley 6277ed246ceeSJohn Hurley /** 6278d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6279d27cf5c5SJohn Hurley * 6280d27cf5c5SJohn Hurley * @skb: buffer 6281d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6282d27cf5c5SJohn Hurley * 6283d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6284d27cf5c5SJohn Hurley * 6285d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6286d27cf5c5SJohn Hurley */ 6287d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6288d27cf5c5SJohn Hurley { 6289d27cf5c5SJohn Hurley int err; 6290d27cf5c5SJohn Hurley 6291d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6292d27cf5c5SJohn Hurley return -EINVAL; 6293d27cf5c5SJohn Hurley 6294d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6295d27cf5c5SJohn Hurley if (unlikely(err)) 6296d27cf5c5SJohn Hurley return err; 6297d27cf5c5SJohn Hurley 6298d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6299d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6300d27cf5c5SJohn Hurley 6301d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6302d27cf5c5SJohn Hurley } 6303d27cf5c5SJohn Hurley 6304d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6305d27cf5c5SJohn Hurley 6306d27cf5c5SJohn Hurley return 0; 6307d27cf5c5SJohn Hurley } 6308d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6309d27cf5c5SJohn Hurley 6310d27cf5c5SJohn Hurley /** 63112a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 63122a2ea508SJohn Hurley * 63132a2ea508SJohn Hurley * @skb: buffer 63142a2ea508SJohn Hurley * 63152a2ea508SJohn Hurley * Expects skb->data at mac header. 63162a2ea508SJohn Hurley * 63172a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 63182a2ea508SJohn Hurley */ 63192a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 63202a2ea508SJohn Hurley { 63212a2ea508SJohn Hurley u32 lse; 63222a2ea508SJohn Hurley u8 ttl; 63232a2ea508SJohn Hurley 63242a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 63252a2ea508SJohn Hurley return -EINVAL; 63262a2ea508SJohn Hurley 632713de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 632813de4ed9SDavide Caratti return -ENOMEM; 632913de4ed9SDavide Caratti 63302a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 63312a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 63322a2ea508SJohn Hurley if (!--ttl) 63332a2ea508SJohn Hurley return -EINVAL; 63342a2ea508SJohn Hurley 63352a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 63362a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 63372a2ea508SJohn Hurley 63382a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 63392a2ea508SJohn Hurley } 63402a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 63412a2ea508SJohn Hurley 63422a2ea508SJohn Hurley /** 63432e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 63442e4e4410SEric Dumazet * 6345de3f0d0eSMasanari Iida * @header_len: size of linear part 6346de3f0d0eSMasanari Iida * @data_len: needed length in frags 6347de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6348de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6349de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 63502e4e4410SEric Dumazet * 63512e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 63522e4e4410SEric Dumazet */ 63532e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 63542e4e4410SEric Dumazet unsigned long data_len, 63552e4e4410SEric Dumazet int max_page_order, 63562e4e4410SEric Dumazet int *errcode, 63572e4e4410SEric Dumazet gfp_t gfp_mask) 63582e4e4410SEric Dumazet { 63592e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 63602e4e4410SEric Dumazet unsigned long chunk; 63612e4e4410SEric Dumazet struct sk_buff *skb; 63622e4e4410SEric Dumazet struct page *page; 63632e4e4410SEric Dumazet int i; 63642e4e4410SEric Dumazet 63652e4e4410SEric Dumazet *errcode = -EMSGSIZE; 63662e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 63672e4e4410SEric Dumazet * high order pages... 63682e4e4410SEric Dumazet */ 63692e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 63702e4e4410SEric Dumazet return NULL; 63712e4e4410SEric Dumazet 63722e4e4410SEric Dumazet *errcode = -ENOBUFS; 6373f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 63742e4e4410SEric Dumazet if (!skb) 63752e4e4410SEric Dumazet return NULL; 63762e4e4410SEric Dumazet 63772e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 63782e4e4410SEric Dumazet 63792e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 63802e4e4410SEric Dumazet int order = max_page_order; 63812e4e4410SEric Dumazet 63822e4e4410SEric Dumazet while (order) { 63832e4e4410SEric Dumazet if (npages >= 1 << order) { 6384d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 63852e4e4410SEric Dumazet __GFP_COMP | 6386d14b56f5SMichal Hocko __GFP_NOWARN, 63872e4e4410SEric Dumazet order); 63882e4e4410SEric Dumazet if (page) 63892e4e4410SEric Dumazet goto fill_page; 63902e4e4410SEric Dumazet /* Do not retry other high order allocations */ 63912e4e4410SEric Dumazet order = 1; 63922e4e4410SEric Dumazet max_page_order = 0; 63932e4e4410SEric Dumazet } 63942e4e4410SEric Dumazet order--; 63952e4e4410SEric Dumazet } 63962e4e4410SEric Dumazet page = alloc_page(gfp_mask); 63972e4e4410SEric Dumazet if (!page) 63982e4e4410SEric Dumazet goto failure; 63992e4e4410SEric Dumazet fill_page: 64002e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 64012e4e4410SEric Dumazet PAGE_SIZE << order); 64022e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 64032e4e4410SEric Dumazet data_len -= chunk; 64042e4e4410SEric Dumazet npages -= 1 << order; 64052e4e4410SEric Dumazet } 64062e4e4410SEric Dumazet return skb; 64072e4e4410SEric Dumazet 64082e4e4410SEric Dumazet failure: 64092e4e4410SEric Dumazet kfree_skb(skb); 64102e4e4410SEric Dumazet return NULL; 64112e4e4410SEric Dumazet } 64122e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 64136fa01ccdSSowmini Varadhan 64146fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 64156fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 64166fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 64176fa01ccdSSowmini Varadhan { 64186fa01ccdSSowmini Varadhan int i; 641912d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 64206fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 64216fa01ccdSSowmini Varadhan u8 *data; 64226fa01ccdSSowmini Varadhan 64236fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64246fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 642512d6c1d3SKees Cook 64265c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 64276fa01ccdSSowmini Varadhan if (!data) 64286fa01ccdSSowmini Varadhan return -ENOMEM; 642912d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64306fa01ccdSSowmini Varadhan 64316fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 64326fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 64336fa01ccdSSowmini Varadhan skb->len -= off; 64346fa01ccdSSowmini Varadhan 64356fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 64366fa01ccdSSowmini Varadhan skb_shinfo(skb), 64376fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 64386fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 64396fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 64406fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 64416fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6442bf9f1baaSEric Dumazet skb_kfree_head(data, size); 64436fa01ccdSSowmini Varadhan return -ENOMEM; 64446fa01ccdSSowmini Varadhan } 64456fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 64466fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64476fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64486fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6449b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 64506fa01ccdSSowmini Varadhan } else { 64516fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 64526fa01ccdSSowmini Varadhan * relocate values 64536fa01ccdSSowmini Varadhan */ 6454b07a2d97SJakub Kicinski skb_free_head(skb, false); 64556fa01ccdSSowmini Varadhan } 64566fa01ccdSSowmini Varadhan 64576fa01ccdSSowmini Varadhan skb->head = data; 64586fa01ccdSSowmini Varadhan skb->data = data; 64596fa01ccdSSowmini Varadhan skb->head_frag = 0; 6460763087daSEric Dumazet skb_set_end_offset(skb, size); 64616fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 64626fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64636fa01ccdSSowmini Varadhan skb->cloned = 0; 64646fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64656fa01ccdSSowmini Varadhan skb->nohdr = 0; 64666fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64676fa01ccdSSowmini Varadhan 64686fa01ccdSSowmini Varadhan return 0; 64696fa01ccdSSowmini Varadhan } 64706fa01ccdSSowmini Varadhan 64716fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 64726fa01ccdSSowmini Varadhan 64736fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 64746fa01ccdSSowmini Varadhan * pskb_carve() 64756fa01ccdSSowmini Varadhan */ 64766fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 64776fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 64786fa01ccdSSowmini Varadhan gfp_t gfp_mask) 64796fa01ccdSSowmini Varadhan { 64806fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 64816fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 64826fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 64836fa01ccdSSowmini Varadhan 64846fa01ccdSSowmini Varadhan do { 64856fa01ccdSSowmini Varadhan if (!list) { 64866fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 64876fa01ccdSSowmini Varadhan return -EFAULT; 64886fa01ccdSSowmini Varadhan } 64896fa01ccdSSowmini Varadhan if (list->len <= eat) { 64906fa01ccdSSowmini Varadhan /* Eaten as whole. */ 64916fa01ccdSSowmini Varadhan eat -= list->len; 64926fa01ccdSSowmini Varadhan list = list->next; 64936fa01ccdSSowmini Varadhan insp = list; 64946fa01ccdSSowmini Varadhan } else { 64956fa01ccdSSowmini Varadhan /* Eaten partially. */ 64966fa01ccdSSowmini Varadhan if (skb_shared(list)) { 64976fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 64986fa01ccdSSowmini Varadhan if (!clone) 64996fa01ccdSSowmini Varadhan return -ENOMEM; 65006fa01ccdSSowmini Varadhan insp = list->next; 65016fa01ccdSSowmini Varadhan list = clone; 65026fa01ccdSSowmini Varadhan } else { 65036fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 65046fa01ccdSSowmini Varadhan insp = list; 65056fa01ccdSSowmini Varadhan } 65066fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 65076fa01ccdSSowmini Varadhan kfree_skb(clone); 65086fa01ccdSSowmini Varadhan return -ENOMEM; 65096fa01ccdSSowmini Varadhan } 65106fa01ccdSSowmini Varadhan break; 65116fa01ccdSSowmini Varadhan } 65126fa01ccdSSowmini Varadhan } while (eat); 65136fa01ccdSSowmini Varadhan 65146fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 65156fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 65166fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6517ef527f96SEric Dumazet consume_skb(list); 65186fa01ccdSSowmini Varadhan } 65196fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 65206fa01ccdSSowmini Varadhan if (clone) { 65216fa01ccdSSowmini Varadhan clone->next = list; 65226fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 65236fa01ccdSSowmini Varadhan } 65246fa01ccdSSowmini Varadhan return 0; 65256fa01ccdSSowmini Varadhan } 65266fa01ccdSSowmini Varadhan 65276fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 65286fa01ccdSSowmini Varadhan * non-linear part of skb 65296fa01ccdSSowmini Varadhan */ 65306fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 65316fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 65326fa01ccdSSowmini Varadhan { 65336fa01ccdSSowmini Varadhan int i, k = 0; 653412d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 65356fa01ccdSSowmini Varadhan u8 *data; 65366fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 65376fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 65386fa01ccdSSowmini Varadhan 65396fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 65406fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 654112d6c1d3SKees Cook 65425c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 65436fa01ccdSSowmini Varadhan if (!data) 65446fa01ccdSSowmini Varadhan return -ENOMEM; 654512d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 65466fa01ccdSSowmini Varadhan 65476fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6548e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 65496fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6550bf9f1baaSEric Dumazet skb_kfree_head(data, size); 65516fa01ccdSSowmini Varadhan return -ENOMEM; 65526fa01ccdSSowmini Varadhan } 65536fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 65546fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 65556fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 65566fa01ccdSSowmini Varadhan 65576fa01ccdSSowmini Varadhan if (pos + fsize > off) { 65586fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 65596fa01ccdSSowmini Varadhan 65606fa01ccdSSowmini Varadhan if (pos < off) { 65616fa01ccdSSowmini Varadhan /* Split frag. 65626fa01ccdSSowmini Varadhan * We have two variants in this case: 65636fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 65646fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 65656fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 65666fa01ccdSSowmini Varadhan * where splitting is expensive. 65676fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 65686fa01ccdSSowmini Varadhan */ 6569b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 65706fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 65716fa01ccdSSowmini Varadhan } 65726fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 65736fa01ccdSSowmini Varadhan k++; 65746fa01ccdSSowmini Varadhan } 65756fa01ccdSSowmini Varadhan pos += fsize; 65766fa01ccdSSowmini Varadhan } 65776fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 65786fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 65796fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 65806fa01ccdSSowmini Varadhan 65816fa01ccdSSowmini Varadhan /* split line is in frag list */ 6582eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6583eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6584eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6585eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6586bf9f1baaSEric Dumazet skb_kfree_head(data, size); 6587eabe8618SMiaohe Lin return -ENOMEM; 65886fa01ccdSSowmini Varadhan } 6589b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 65906fa01ccdSSowmini Varadhan 65916fa01ccdSSowmini Varadhan skb->head = data; 65926fa01ccdSSowmini Varadhan skb->head_frag = 0; 65936fa01ccdSSowmini Varadhan skb->data = data; 6594763087daSEric Dumazet skb_set_end_offset(skb, size); 65956fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 65966fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 65976fa01ccdSSowmini Varadhan skb->cloned = 0; 65986fa01ccdSSowmini Varadhan skb->hdr_len = 0; 65996fa01ccdSSowmini Varadhan skb->nohdr = 0; 66006fa01ccdSSowmini Varadhan skb->len -= off; 66016fa01ccdSSowmini Varadhan skb->data_len = skb->len; 66026fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 66036fa01ccdSSowmini Varadhan return 0; 66046fa01ccdSSowmini Varadhan } 66056fa01ccdSSowmini Varadhan 66066fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 66076fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 66086fa01ccdSSowmini Varadhan { 66096fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 66106fa01ccdSSowmini Varadhan 66116fa01ccdSSowmini Varadhan if (len < headlen) 66126fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 66136fa01ccdSSowmini Varadhan else 66146fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 66156fa01ccdSSowmini Varadhan } 66166fa01ccdSSowmini Varadhan 66176fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 66186fa01ccdSSowmini Varadhan * a new skb 66196fa01ccdSSowmini Varadhan */ 66206fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 66216fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 66226fa01ccdSSowmini Varadhan { 66236fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 66246fa01ccdSSowmini Varadhan 66256fa01ccdSSowmini Varadhan if (!clone) 66266fa01ccdSSowmini Varadhan return NULL; 66276fa01ccdSSowmini Varadhan 66286fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 66296fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 66306fa01ccdSSowmini Varadhan kfree_skb(clone); 66316fa01ccdSSowmini Varadhan return NULL; 66326fa01ccdSSowmini Varadhan } 66336fa01ccdSSowmini Varadhan return clone; 66346fa01ccdSSowmini Varadhan } 66356fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6636c8c8b127SEric Dumazet 6637c8c8b127SEric Dumazet /** 6638c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6639c8c8b127SEric Dumazet * @skb: buffer 6640c8c8b127SEric Dumazet * 6641c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6642c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6643c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6644c8c8b127SEric Dumazet * truesize. 6645c8c8b127SEric Dumazet * Notes: 6646c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6647c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6648c8c8b127SEric Dumazet */ 6649c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6650c8c8b127SEric Dumazet { 66513174fed9SEric Dumazet if (skb->data_len) { 66523174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6653c8c8b127SEric Dumazet skb_cloned(skb)) 6654c8c8b127SEric Dumazet return; 6655c8c8b127SEric Dumazet 6656c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6657c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 66583174fed9SEric Dumazet } 66593174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 66603174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 66613174fed9SEric Dumazet * their truesize. 66623174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 66633174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 66643174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6665c8c8b127SEric Dumazet */ 6666c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6667c8c8b127SEric Dumazet } 6668d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6669df5042f4SFlorian Westphal 6670df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6671df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6672df5042f4SFlorian Westphal { 6673df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6674df5042f4SFlorian Westphal } 6675df5042f4SFlorian Westphal 66768b69a803SPaolo Abeni /** 66778b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 66788b69a803SPaolo Abeni * 66794930f483SFlorian Westphal * @flags: See kmalloc(). 66804930f483SFlorian Westphal * 66818b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 66828b69a803SPaolo Abeni * skb via __skb_ext_set(). 66838b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 66848b69a803SPaolo Abeni */ 66854930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6686df5042f4SFlorian Westphal { 66874930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6688df5042f4SFlorian Westphal 6689df5042f4SFlorian Westphal if (new) { 6690df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6691df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6692df5042f4SFlorian Westphal } 6693df5042f4SFlorian Westphal 6694df5042f4SFlorian Westphal return new; 6695df5042f4SFlorian Westphal } 6696df5042f4SFlorian Westphal 66974165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 66984165079bSFlorian Westphal unsigned int old_active) 6699df5042f4SFlorian Westphal { 6700df5042f4SFlorian Westphal struct skb_ext *new; 6701df5042f4SFlorian Westphal 6702df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6703df5042f4SFlorian Westphal return old; 6704df5042f4SFlorian Westphal 6705df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6706df5042f4SFlorian Westphal if (!new) 6707df5042f4SFlorian Westphal return NULL; 6708df5042f4SFlorian Westphal 6709df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6710df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6711df5042f4SFlorian Westphal 67124165079bSFlorian Westphal #ifdef CONFIG_XFRM 67134165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 67144165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 67154165079bSFlorian Westphal unsigned int i; 67164165079bSFlorian Westphal 67174165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 67184165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 67194165079bSFlorian Westphal } 67204165079bSFlorian Westphal #endif 6721df5042f4SFlorian Westphal __skb_ext_put(old); 6722df5042f4SFlorian Westphal return new; 6723df5042f4SFlorian Westphal } 6724df5042f4SFlorian Westphal 6725df5042f4SFlorian Westphal /** 67268b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 67278b69a803SPaolo Abeni * @skb: buffer 67288b69a803SPaolo Abeni * @id: extension id 67298b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 67308b69a803SPaolo Abeni * 67318b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 67328b69a803SPaolo Abeni * 67338b69a803SPaolo Abeni * Returns the pointer to the extension. 67348b69a803SPaolo Abeni */ 67358b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 67368b69a803SPaolo Abeni struct skb_ext *ext) 67378b69a803SPaolo Abeni { 67388b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 67398b69a803SPaolo Abeni 67408b69a803SPaolo Abeni skb_ext_put(skb); 67418b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 67428b69a803SPaolo Abeni ext->chunks = newlen; 67438b69a803SPaolo Abeni ext->offset[id] = newoff; 67448b69a803SPaolo Abeni skb->extensions = ext; 67458b69a803SPaolo Abeni skb->active_extensions = 1 << id; 67468b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 67478b69a803SPaolo Abeni } 67488b69a803SPaolo Abeni 67498b69a803SPaolo Abeni /** 6750df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6751df5042f4SFlorian Westphal * @skb: buffer 6752df5042f4SFlorian Westphal * @id: extension to allocate space for 6753df5042f4SFlorian Westphal * 6754df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6755df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6756df5042f4SFlorian Westphal * is returned. 6757df5042f4SFlorian Westphal * 6758df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6759df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6760df5042f4SFlorian Westphal * 6761df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6762df5042f4SFlorian Westphal */ 6763df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6764df5042f4SFlorian Westphal { 6765df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6766df5042f4SFlorian Westphal unsigned int newlen, newoff; 6767df5042f4SFlorian Westphal 6768df5042f4SFlorian Westphal if (skb->active_extensions) { 6769df5042f4SFlorian Westphal old = skb->extensions; 6770df5042f4SFlorian Westphal 67714165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6772df5042f4SFlorian Westphal if (!new) 6773df5042f4SFlorian Westphal return NULL; 6774df5042f4SFlorian Westphal 6775682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6776df5042f4SFlorian Westphal goto set_active; 6777df5042f4SFlorian Westphal 6778e94e50bdSPaolo Abeni newoff = new->chunks; 6779df5042f4SFlorian Westphal } else { 6780df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6781df5042f4SFlorian Westphal 67824930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6783df5042f4SFlorian Westphal if (!new) 6784df5042f4SFlorian Westphal return NULL; 6785df5042f4SFlorian Westphal } 6786df5042f4SFlorian Westphal 6787df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6788df5042f4SFlorian Westphal new->chunks = newlen; 6789df5042f4SFlorian Westphal new->offset[id] = newoff; 6790df5042f4SFlorian Westphal set_active: 6791b0999f38SPaolo Abeni skb->slow_gro = 1; 6792682ec859SPaolo Abeni skb->extensions = new; 6793df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6794df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6795df5042f4SFlorian Westphal } 6796df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6797df5042f4SFlorian Westphal 67984165079bSFlorian Westphal #ifdef CONFIG_XFRM 67994165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 68004165079bSFlorian Westphal { 68014165079bSFlorian Westphal unsigned int i; 68024165079bSFlorian Westphal 68034165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 68044165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 68054165079bSFlorian Westphal } 68064165079bSFlorian Westphal #endif 68074165079bSFlorian Westphal 680878476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 680978476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 681078476d31SJeremy Kerr { 681178476d31SJeremy Kerr if (flow->key) 681278476d31SJeremy Kerr mctp_key_unref(flow->key); 681378476d31SJeremy Kerr } 681478476d31SJeremy Kerr #endif 681578476d31SJeremy Kerr 6816df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6817df5042f4SFlorian Westphal { 6818df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6819df5042f4SFlorian Westphal 6820df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6821df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6822df5042f4SFlorian Westphal skb->extensions = NULL; 6823df5042f4SFlorian Westphal __skb_ext_put(ext); 68244165079bSFlorian Westphal #ifdef CONFIG_XFRM 68254165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 68264165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 68274165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 68284165079bSFlorian Westphal 68294165079bSFlorian Westphal skb_ext_put_sp(sp); 68304165079bSFlorian Westphal sp->len = 0; 68314165079bSFlorian Westphal #endif 6832df5042f4SFlorian Westphal } 6833df5042f4SFlorian Westphal } 6834df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6835df5042f4SFlorian Westphal 6836df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6837df5042f4SFlorian Westphal { 6838df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6839df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6840df5042f4SFlorian Westphal */ 6841df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6842df5042f4SFlorian Westphal goto free_now; 6843df5042f4SFlorian Westphal 6844df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6845df5042f4SFlorian Westphal return; 6846df5042f4SFlorian Westphal free_now: 68474165079bSFlorian Westphal #ifdef CONFIG_XFRM 68484165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 68494165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 68504165079bSFlorian Westphal #endif 685178476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 685278476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 685378476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 685478476d31SJeremy Kerr #endif 68554165079bSFlorian Westphal 6856df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6857df5042f4SFlorian Westphal } 6858df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6859df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 686068822bdfSEric Dumazet 686168822bdfSEric Dumazet /** 686268822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 686368822bdfSEric Dumazet * @skb: buffer 686468822bdfSEric Dumazet * 686568822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 686668822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 686768822bdfSEric Dumazet * and memory zone spinlock contention. 686868822bdfSEric Dumazet */ 686968822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 687068822bdfSEric Dumazet { 687168822bdfSEric Dumazet int cpu = skb->alloc_cpu; 687268822bdfSEric Dumazet struct softnet_data *sd; 687368822bdfSEric Dumazet unsigned long flags; 687439564c3fSEric Dumazet unsigned int defer_max; 687568822bdfSEric Dumazet bool kick; 687668822bdfSEric Dumazet 687768822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 687868822bdfSEric Dumazet !cpu_online(cpu) || 687968822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 688039564c3fSEric Dumazet nodefer: __kfree_skb(skb); 688168822bdfSEric Dumazet return; 688268822bdfSEric Dumazet } 688368822bdfSEric Dumazet 688468822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 688539564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 688639564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 688739564c3fSEric Dumazet goto nodefer; 688839564c3fSEric Dumazet 688968822bdfSEric Dumazet spin_lock_irqsave(&sd->defer_lock, flags); 6890c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6891c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6892c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6893c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6894c09b0cd2SJakub Kicinski 689568822bdfSEric Dumazet skb->next = sd->defer_list; 689668822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 689768822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 689868822bdfSEric Dumazet spin_unlock_irqrestore(&sd->defer_lock, flags); 689968822bdfSEric Dumazet 690068822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 690168822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 690268822bdfSEric Dumazet */ 690397e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 690468822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 690568822bdfSEric Dumazet } 6906