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> 61071c0fc6SJohannes 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> 76071c0fc6SJohannes 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, 127071c0fc6SJohannes Berg static const char * const drop_reasons[] = { 1280e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 1299cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 1309cb252c4SMenglong Dong }; 131071c0fc6SJohannes Berg 132071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = { 133071c0fc6SJohannes Berg .reasons = drop_reasons, 134071c0fc6SJohannes Berg .n_reasons = ARRAY_SIZE(drop_reasons), 135071c0fc6SJohannes Berg }; 136071c0fc6SJohannes Berg 137071c0fc6SJohannes Berg const struct drop_reason_list __rcu * 138071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = { 139071c0fc6SJohannes Berg [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core), 140071c0fc6SJohannes Berg }; 141071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys); 142071c0fc6SJohannes Berg 143071c0fc6SJohannes Berg /** 144071c0fc6SJohannes Berg * drop_reasons_register_subsys - register another drop reason subsystem 145071c0fc6SJohannes Berg * @subsys: the subsystem to register, must not be the core 146071c0fc6SJohannes Berg * @list: the list of drop reasons within the subsystem, must point to 147071c0fc6SJohannes Berg * a statically initialized list 148071c0fc6SJohannes Berg */ 149071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys, 150071c0fc6SJohannes Berg const struct drop_reason_list *list) 151071c0fc6SJohannes Berg { 152071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 153071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 154071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 155071c0fc6SJohannes Berg return; 156071c0fc6SJohannes Berg 157071c0fc6SJohannes Berg /* must point to statically allocated memory, so INIT is OK */ 158071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list); 159071c0fc6SJohannes Berg } 160071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys); 161071c0fc6SJohannes Berg 162071c0fc6SJohannes Berg /** 163071c0fc6SJohannes Berg * drop_reasons_unregister_subsys - unregister a drop reason subsystem 164071c0fc6SJohannes Berg * @subsys: the subsystem to remove, must not be the core 165071c0fc6SJohannes Berg * 166071c0fc6SJohannes Berg * Note: This will synchronize_rcu() to ensure no users when it returns. 167071c0fc6SJohannes Berg */ 168071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys) 169071c0fc6SJohannes Berg { 170071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 171071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 172071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 173071c0fc6SJohannes Berg return; 174071c0fc6SJohannes Berg 175071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL); 176071c0fc6SJohannes Berg 177071c0fc6SJohannes Berg synchronize_rcu(); 178071c0fc6SJohannes Berg } 179071c0fc6SJohannes 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 1039071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || 1040071c0fc6SJohannes Berg u32_get_bits(reason, 1041071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >= 1042071c0fc6SJohannes 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; 1761*7e692df3SEric Dumazet int i, order, psize, 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 1770*7e692df3SEric Dumazet /* We might have to allocate high order pages, so compute what minimum 1771*7e692df3SEric Dumazet * page order is needed. 1772*7e692df3SEric Dumazet */ 1773*7e692df3SEric Dumazet order = 0; 1774*7e692df3SEric Dumazet while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb)) 1775*7e692df3SEric Dumazet order++; 1776*7e692df3SEric Dumazet psize = (PAGE_SIZE << order); 1777*7e692df3SEric Dumazet 1778*7e692df3SEric Dumazet new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order); 17793ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 1780*7e692df3SEric Dumazet page = alloc_pages(gfp_mask | __GFP_COMP, order); 1781a6686f2fSShirley Ma if (!page) { 1782a6686f2fSShirley Ma while (head) { 178340dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1784a6686f2fSShirley Ma put_page(head); 1785a6686f2fSShirley Ma head = next; 1786a6686f2fSShirley Ma } 1787a6686f2fSShirley Ma return -ENOMEM; 1788a6686f2fSShirley Ma } 17893ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 17903ece7826SWillem de Bruijn head = page; 17913ece7826SWillem de Bruijn } 17923ece7826SWillem de Bruijn 17933ece7826SWillem de Bruijn page = head; 17943ece7826SWillem de Bruijn d_off = 0; 17953ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 17963ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17973ece7826SWillem de Bruijn u32 p_off, p_len, copied; 17983ece7826SWillem de Bruijn struct page *p; 17993ece7826SWillem de Bruijn u8 *vaddr; 1800c613c209SWillem de Bruijn 1801b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1802c613c209SWillem de Bruijn p, p_off, p_len, copied) { 18033ece7826SWillem de Bruijn u32 copy, done = 0; 1804c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 18053ece7826SWillem de Bruijn 18063ece7826SWillem de Bruijn while (done < p_len) { 1807*7e692df3SEric Dumazet if (d_off == psize) { 18083ece7826SWillem de Bruijn d_off = 0; 18093ece7826SWillem de Bruijn page = (struct page *)page_private(page); 18103ece7826SWillem de Bruijn } 1811*7e692df3SEric Dumazet copy = min_t(u32, psize - d_off, p_len - done); 18123ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 18133ece7826SWillem de Bruijn vaddr + p_off + done, copy); 18143ece7826SWillem de Bruijn done += copy; 18153ece7826SWillem de Bruijn d_off += copy; 18163ece7826SWillem de Bruijn } 181751c56b00SEric Dumazet kunmap_atomic(vaddr); 1818c613c209SWillem de Bruijn } 1819a6686f2fSShirley Ma } 1820a6686f2fSShirley Ma 1821a6686f2fSShirley Ma /* skb frags release userspace buffers */ 182202756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1823a8605c60SIan Campbell skb_frag_unref(skb, i); 1824a6686f2fSShirley Ma 1825a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 18263ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 1827*7e692df3SEric Dumazet __skb_fill_page_desc(skb, i, head, 0, psize); 182840dadff2SSunghan Suh head = (struct page *)page_private(head); 1829a6686f2fSShirley Ma } 18303ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 18313ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 183248c83012SMichael S. Tsirkin 1833b90ddd56SWillem de Bruijn release: 18341f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1835a6686f2fSShirley Ma return 0; 1836a6686f2fSShirley Ma } 1837dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1838a6686f2fSShirley Ma 1839e0053ec0SHerbert Xu /** 1840e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1841e0053ec0SHerbert Xu * @skb: buffer to clone 1842e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1843e0053ec0SHerbert Xu * 1844e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1845e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1846e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1847e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1848e0053ec0SHerbert Xu * 1849e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1850e0053ec0SHerbert Xu * %GFP_ATOMIC. 1851e0053ec0SHerbert Xu */ 1852e0053ec0SHerbert Xu 1853e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1854e0053ec0SHerbert Xu { 1855d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1856d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1857d0bf4a9eSEric Dumazet skb1); 18586ffe75ebSEric Dumazet struct sk_buff *n; 1859e0053ec0SHerbert Xu 186070008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1861a6686f2fSShirley Ma return NULL; 1862a6686f2fSShirley Ma 1863e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 18642638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 18656ffe75ebSEric Dumazet n = &fclones->skb2; 18662638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1867c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1868e0053ec0SHerbert Xu } else { 1869c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1870c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1871c93bdd0eSMel Gorman 1872025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask); 1873e0053ec0SHerbert Xu if (!n) 1874e0053ec0SHerbert Xu return NULL; 1875fe55f6d5SVegard Nossum 1876e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1877e0053ec0SHerbert Xu } 1878e0053ec0SHerbert Xu 1879e0053ec0SHerbert Xu return __skb_clone(n, skb); 18801da177e4SLinus Torvalds } 1881b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 18821da177e4SLinus Torvalds 1883b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1884f5b17294SPravin B Shelar { 1885030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1886030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1887030737bcSEric Dumazet skb->csum_start += off; 1888f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1889f5b17294SPravin B Shelar skb->transport_header += off; 1890f5b17294SPravin B Shelar skb->network_header += off; 1891f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1892f5b17294SPravin B Shelar skb->mac_header += off; 1893f5b17294SPravin B Shelar skb->inner_transport_header += off; 1894f5b17294SPravin B Shelar skb->inner_network_header += off; 1895aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1896f5b17294SPravin B Shelar } 1897b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1898f5b17294SPravin B Shelar 189908303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 19001da177e4SLinus Torvalds { 1901dec18810SHerbert Xu __copy_skb_header(new, old); 1902dec18810SHerbert Xu 19037967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 19047967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 19057967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 19061da177e4SLinus Torvalds } 190708303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 19081da177e4SLinus Torvalds 1909c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1910c93bdd0eSMel Gorman { 1911c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1912c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1913c93bdd0eSMel Gorman return 0; 1914c93bdd0eSMel Gorman } 1915c93bdd0eSMel Gorman 19161da177e4SLinus Torvalds /** 19171da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 19181da177e4SLinus Torvalds * @skb: buffer to copy 19191da177e4SLinus Torvalds * @gfp_mask: allocation priority 19201da177e4SLinus Torvalds * 19211da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 19221da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 19231da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 19241da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 19251da177e4SLinus Torvalds * 19261da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 19271da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 19281da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 19291da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 19301da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 19311da177e4SLinus Torvalds */ 19321da177e4SLinus Torvalds 1933dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 19341da177e4SLinus Torvalds { 19356602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1936ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1937c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1938c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 19396602cebbSEric Dumazet 19401da177e4SLinus Torvalds if (!n) 19411da177e4SLinus Torvalds return NULL; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* Set the data pointer */ 19441da177e4SLinus Torvalds skb_reserve(n, headerlen); 19451da177e4SLinus Torvalds /* Set the tail pointer and length */ 19461da177e4SLinus Torvalds skb_put(n, skb->len); 19471da177e4SLinus Torvalds 19489f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 19491da177e4SLinus Torvalds 195008303c18SIlya Lesokhin skb_copy_header(n, skb); 19511da177e4SLinus Torvalds return n; 19521da177e4SLinus Torvalds } 1953b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds /** 1956bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 19571da177e4SLinus Torvalds * @skb: buffer to copy 1958117632e6SEric Dumazet * @headroom: headroom of new skb 19591da177e4SLinus Torvalds * @gfp_mask: allocation priority 1960bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1961bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1962bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 19631da177e4SLinus Torvalds * 19641da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 19651da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 19661da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 19671da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 19681da177e4SLinus Torvalds * or the pointer to the buffer on success. 19691da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 19701da177e4SLinus Torvalds */ 19711da177e4SLinus Torvalds 1972bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1973bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 19741da177e4SLinus Torvalds { 1975117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1976bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1977bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 19786602cebbSEric Dumazet 19791da177e4SLinus Torvalds if (!n) 19801da177e4SLinus Torvalds goto out; 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds /* Set the data pointer */ 1983117632e6SEric Dumazet skb_reserve(n, headroom); 19841da177e4SLinus Torvalds /* Set the tail pointer and length */ 19851da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 19861da177e4SLinus Torvalds /* Copy the bytes */ 1987d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 19881da177e4SLinus Torvalds 198925f484a6SHerbert Xu n->truesize += skb->data_len; 19901da177e4SLinus Torvalds n->data_len = skb->data_len; 19911da177e4SLinus Torvalds n->len = skb->len; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 19941da177e4SLinus Torvalds int i; 19951da177e4SLinus Torvalds 19961f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 19971f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 19981511022cSDan Carpenter kfree_skb(n); 19991511022cSDan Carpenter n = NULL; 2000a6686f2fSShirley Ma goto out; 2001a6686f2fSShirley Ma } 20021da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20031da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 2004ea2ab693SIan Campbell skb_frag_ref(skb, i); 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 200921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 20101da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 20111da177e4SLinus Torvalds skb_clone_fraglist(n); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 201408303c18SIlya Lesokhin skb_copy_header(n, skb); 20151da177e4SLinus Torvalds out: 20161da177e4SLinus Torvalds return n; 20171da177e4SLinus Torvalds } 2018bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds /** 20211da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 20221da177e4SLinus Torvalds * @skb: buffer to reallocate 20231da177e4SLinus Torvalds * @nhead: room to add at head 20241da177e4SLinus Torvalds * @ntail: room to add at tail 20251da177e4SLinus Torvalds * @gfp_mask: allocation priority 20261da177e4SLinus Torvalds * 2027bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 2028bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 20291da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 20301da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 20311da177e4SLinus Torvalds * 20321da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20331da177e4SLinus Torvalds * reloaded after call to this function. 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds 203686a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 2037dd0fc66fSAl Viro gfp_t gfp_mask) 20381da177e4SLinus Torvalds { 203912d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 204012d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 20411da177e4SLinus Torvalds long off; 2042158f323bSEric Dumazet u8 *data; 204312d6c1d3SKees Cook int i; 20441da177e4SLinus Torvalds 20454edd87adSHerbert Xu BUG_ON(nhead < 0); 20464edd87adSHerbert Xu 20479f77fad3STim Hansen BUG_ON(skb_shared(skb)); 20481da177e4SLinus Torvalds 2049753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 2050753f1ca4SPavel Begunkov 2051c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 2052c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 205312d6c1d3SKees Cook 20545c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 20551da177e4SLinus Torvalds if (!data) 20561da177e4SLinus Torvalds goto nodata; 205712d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 20606602cebbSEric Dumazet * optimized for the cases when header is void. 20616602cebbSEric Dumazet */ 20626602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 20636602cebbSEric Dumazet 20646602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 20656602cebbSEric Dumazet skb_shinfo(skb), 2066fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 20671da177e4SLinus Torvalds 20683e24591aSAlexander Duyck /* 20693e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 20703e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 20713e24591aSAlexander Duyck * be since all we did is relocate the values 20723e24591aSAlexander Duyck */ 20733e24591aSAlexander Duyck if (skb_cloned(skb)) { 207470008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 2075a6686f2fSShirley Ma goto nofrags; 20761f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 2077c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 20781da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2079ea2ab693SIan Campbell skb_frag_ref(skb, i); 20801da177e4SLinus Torvalds 208121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 20821da177e4SLinus Torvalds skb_clone_fraglist(skb); 20831da177e4SLinus Torvalds 2084b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 20853e24591aSAlexander Duyck } else { 2086b07a2d97SJakub Kicinski skb_free_head(skb, false); 20871fd63041SEric Dumazet } 20881da177e4SLinus Torvalds off = (data + nhead) - skb->head; 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds skb->head = data; 2091d3836f21SEric Dumazet skb->head_frag = 0; 20921da177e4SLinus Torvalds skb->data += off; 2093763087daSEric Dumazet 2094763087daSEric Dumazet skb_set_end_offset(skb, size); 20954305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 209656eb8882SPatrick McHardy off = nhead; 209756eb8882SPatrick McHardy #endif 209827a884dcSArnaldo Carvalho de Melo skb->tail += off; 2099b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 21001da177e4SLinus Torvalds skb->cloned = 0; 2101334a8132SPatrick McHardy skb->hdr_len = 0; 21021da177e4SLinus Torvalds skb->nohdr = 0; 21031da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 2104158f323bSEric Dumazet 2105de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 2106de8f3a83SDaniel Borkmann 2107158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 2108158f323bSEric Dumazet * For the moment, we really care of rx path, or 2109158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 2110158f323bSEric Dumazet */ 2111158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2112158f323bSEric Dumazet skb->truesize += size - osize; 2113158f323bSEric Dumazet 21141da177e4SLinus Torvalds return 0; 21151da177e4SLinus Torvalds 2116a6686f2fSShirley Ma nofrags: 2117bf9f1baaSEric Dumazet skb_kfree_head(data, size); 21181da177e4SLinus Torvalds nodata: 21191da177e4SLinus Torvalds return -ENOMEM; 21201da177e4SLinus Torvalds } 2121b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 21261da177e4SLinus Torvalds { 21271da177e4SLinus Torvalds struct sk_buff *skb2; 21281da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds if (delta <= 0) 21311da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 21321da177e4SLinus Torvalds else { 21331da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 21341da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 21351da177e4SLinus Torvalds GFP_ATOMIC)) { 21361da177e4SLinus Torvalds kfree_skb(skb2); 21371da177e4SLinus Torvalds skb2 = NULL; 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds return skb2; 21411da177e4SLinus Torvalds } 2142b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 21431da177e4SLinus Torvalds 2144880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */ 21452b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 21462b88cba5SEric Dumazet { 21472b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 21482b88cba5SEric Dumazet struct skb_shared_info *shinfo; 21492b88cba5SEric Dumazet int res; 21502b88cba5SEric Dumazet 21512b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 21522b88cba5SEric Dumazet saved_truesize = skb->truesize; 21532b88cba5SEric Dumazet 21542b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 21552b88cba5SEric Dumazet if (res) 21562b88cba5SEric Dumazet return res; 21572b88cba5SEric Dumazet 21582b88cba5SEric Dumazet skb->truesize = saved_truesize; 21592b88cba5SEric Dumazet 21602b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 21612b88cba5SEric Dumazet return 0; 21622b88cba5SEric Dumazet 2163880ce5f2SEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 2164880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value 2165880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). 2166880ce5f2SEric Dumazet */ 2167880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || 2168880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { 2169880ce5f2SEric Dumazet /* We think this path should not be taken. 2170880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case. 2171880ce5f2SEric Dumazet */ 2172880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", 2173880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb)); 2174880ce5f2SEric Dumazet WARN_ON_ONCE(1); 2175880ce5f2SEric Dumazet return 0; 2176880ce5f2SEric Dumazet } 2177880ce5f2SEric Dumazet #endif 2178880ce5f2SEric Dumazet 21792b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 21802b88cba5SEric Dumazet 21812b88cba5SEric Dumazet /* We are about to change back skb->end, 21822b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 21832b88cba5SEric Dumazet */ 21842b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 21852b88cba5SEric Dumazet shinfo, 21862b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 21872b88cba5SEric Dumazet 21882b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 21892b88cba5SEric Dumazet 21902b88cba5SEric Dumazet return 0; 21912b88cba5SEric Dumazet } 21922b88cba5SEric Dumazet 21931da177e4SLinus Torvalds /** 2194f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2195f1260ff1SVasily Averin * @skb: buffer to reallocate 2196f1260ff1SVasily Averin * @headroom: needed headroom 2197f1260ff1SVasily Averin * 2198f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2199f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2200f1260ff1SVasily Averin * and frees original skb in case of failures. 2201f1260ff1SVasily Averin * 2202f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2203f1260ff1SVasily Averin */ 2204f1260ff1SVasily Averin 2205f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2206f1260ff1SVasily Averin { 2207f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 22087f678defSVasily Averin int osize = skb_end_offset(skb); 22097f678defSVasily Averin struct sock *sk = skb->sk; 2210f1260ff1SVasily Averin 2211f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2212f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2213f1260ff1SVasily Averin return skb; 2214f1260ff1SVasily Averin 22157f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 22167f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 22177f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2218f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2219f1260ff1SVasily Averin 22207f678defSVasily Averin if (unlikely(!nskb)) 22217f678defSVasily Averin goto fail; 22227f678defSVasily Averin 22237f678defSVasily Averin if (sk) 22247f678defSVasily Averin skb_set_owner_w(nskb, sk); 2225f1260ff1SVasily Averin consume_skb(skb); 2226f1260ff1SVasily Averin skb = nskb; 2227f1260ff1SVasily Averin } 22287f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 22297f678defSVasily Averin goto fail; 22307f678defSVasily Averin 22317f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 22327f678defSVasily Averin delta = skb_end_offset(skb) - osize; 22337f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 22347f678defSVasily Averin skb->truesize += delta; 2235f1260ff1SVasily Averin } 2236f1260ff1SVasily Averin return skb; 22377f678defSVasily Averin 22387f678defSVasily Averin fail: 22397f678defSVasily Averin kfree_skb(skb); 22407f678defSVasily Averin return NULL; 2241f1260ff1SVasily Averin } 2242f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2243f1260ff1SVasily Averin 2244f1260ff1SVasily Averin /** 22451da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 22461da177e4SLinus Torvalds * @skb: buffer to copy 22471da177e4SLinus Torvalds * @newheadroom: new free bytes at head 22481da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 22491da177e4SLinus Torvalds * @gfp_mask: allocation priority 22501da177e4SLinus Torvalds * 22511da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 22521da177e4SLinus Torvalds * allocate additional space. 22531da177e4SLinus Torvalds * 22541da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 22551da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 22561da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 22571da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 22581da177e4SLinus Torvalds * 22591da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 22601da177e4SLinus Torvalds * is called from an interrupt. 22611da177e4SLinus Torvalds */ 22621da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 226386a76cafSVictor Fusco int newheadroom, int newtailroom, 2264dd0fc66fSAl Viro gfp_t gfp_mask) 22651da177e4SLinus Torvalds { 22661da177e4SLinus Torvalds /* 22671da177e4SLinus Torvalds * Allocate the copy buffer 22681da177e4SLinus Torvalds */ 2269c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2270c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2271c93bdd0eSMel Gorman NUMA_NO_NODE); 2272efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 22731da177e4SLinus Torvalds int head_copy_len, head_copy_off; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds if (!n) 22761da177e4SLinus Torvalds return NULL; 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds skb_reserve(n, newheadroom); 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds /* Set the tail pointer and length */ 22811da177e4SLinus Torvalds skb_put(n, skb->len); 22821da177e4SLinus Torvalds 2283efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 22841da177e4SLinus Torvalds head_copy_off = 0; 22851da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 22861da177e4SLinus Torvalds head_copy_len = newheadroom; 22871da177e4SLinus Torvalds else 22881da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds /* Copy the linear header and data. */ 22919f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 22929f77fad3STim Hansen skb->len + head_copy_len)); 22931da177e4SLinus Torvalds 229408303c18SIlya Lesokhin skb_copy_header(n, skb); 22951da177e4SLinus Torvalds 2296030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2297efd1e8d5SPatrick McHardy 22981da177e4SLinus Torvalds return n; 22991da177e4SLinus Torvalds } 2300b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds /** 2303cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 23041da177e4SLinus Torvalds * @skb: buffer to pad 23051da177e4SLinus Torvalds * @pad: space to pad 2306cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 23071da177e4SLinus Torvalds * 23081da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 23091da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 23101da177e4SLinus Torvalds * beyond the buffer end onto the wire. 23111da177e4SLinus Torvalds * 2312cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2313cd0a137aSFlorian Fainelli * if @free_on_error is true. 23141da177e4SLinus Torvalds */ 23151da177e4SLinus Torvalds 2316cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 23171da177e4SLinus Torvalds { 23185b057c6bSHerbert Xu int err; 23195b057c6bSHerbert Xu int ntail; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 23225b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 23231da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 23245b057c6bSHerbert Xu return 0; 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds 23274305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 23285b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 23295b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 23305b057c6bSHerbert Xu if (unlikely(err)) 23315b057c6bSHerbert Xu goto free_skb; 23325b057c6bSHerbert Xu } 23335b057c6bSHerbert Xu 23345b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 23355b057c6bSHerbert Xu * to be audited. 23365b057c6bSHerbert Xu */ 23375b057c6bSHerbert Xu err = skb_linearize(skb); 23385b057c6bSHerbert Xu if (unlikely(err)) 23395b057c6bSHerbert Xu goto free_skb; 23405b057c6bSHerbert Xu 23415b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 23425b057c6bSHerbert Xu return 0; 23435b057c6bSHerbert Xu 23445b057c6bSHerbert Xu free_skb: 2345cd0a137aSFlorian Fainelli if (free_on_error) 23461da177e4SLinus Torvalds kfree_skb(skb); 23475b057c6bSHerbert Xu return err; 23481da177e4SLinus Torvalds } 2349cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 23501da177e4SLinus Torvalds 23510dde3e16SIlpo Järvinen /** 23520c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 23530c7ddf36SMathias Krause * @skb: start of the buffer to use 23540c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 23550c7ddf36SMathias Krause * @len: amount of data to add 23560c7ddf36SMathias Krause * 23570c7ddf36SMathias Krause * This function extends the used data area of the potentially 23580c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 23590c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 23600c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 23610c7ddf36SMathias Krause * returned. 23620c7ddf36SMathias Krause */ 23630c7ddf36SMathias Krause 23644df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 23650c7ddf36SMathias Krause { 23660c7ddf36SMathias Krause if (tail != skb) { 23670c7ddf36SMathias Krause skb->data_len += len; 23680c7ddf36SMathias Krause skb->len += len; 23690c7ddf36SMathias Krause } 23700c7ddf36SMathias Krause return skb_put(tail, len); 23710c7ddf36SMathias Krause } 23720c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 23730c7ddf36SMathias Krause 23740c7ddf36SMathias Krause /** 23750dde3e16SIlpo Järvinen * skb_put - add data to a buffer 23760dde3e16SIlpo Järvinen * @skb: buffer to use 23770dde3e16SIlpo Järvinen * @len: amount of data to add 23780dde3e16SIlpo Järvinen * 23790dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 23800dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 23810dde3e16SIlpo Järvinen * first byte of the extra data is returned. 23820dde3e16SIlpo Järvinen */ 23834df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 23840dde3e16SIlpo Järvinen { 23854df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 23860dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 23870dde3e16SIlpo Järvinen skb->tail += len; 23880dde3e16SIlpo Järvinen skb->len += len; 23890dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 23900dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 23910dde3e16SIlpo Järvinen return tmp; 23920dde3e16SIlpo Järvinen } 23930dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 23940dde3e16SIlpo Järvinen 23956be8ac2fSIlpo Järvinen /** 2396c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2397c2aa270aSIlpo Järvinen * @skb: buffer to use 2398c2aa270aSIlpo Järvinen * @len: amount of data to add 2399c2aa270aSIlpo Järvinen * 2400c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2401c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2402c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2403c2aa270aSIlpo Järvinen */ 2404d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2405c2aa270aSIlpo Järvinen { 2406c2aa270aSIlpo Järvinen skb->data -= len; 2407c2aa270aSIlpo Järvinen skb->len += len; 2408c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2409c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2410c2aa270aSIlpo Järvinen return skb->data; 2411c2aa270aSIlpo Järvinen } 2412c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2413c2aa270aSIlpo Järvinen 2414c2aa270aSIlpo Järvinen /** 24156be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 24166be8ac2fSIlpo Järvinen * @skb: buffer to use 24176be8ac2fSIlpo Järvinen * @len: amount of data to remove 24186be8ac2fSIlpo Järvinen * 24196be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 24206be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 24216be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 24226be8ac2fSIlpo Järvinen * the old data. 24236be8ac2fSIlpo Järvinen */ 2424af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 24256be8ac2fSIlpo Järvinen { 242647d29646SDavid S. Miller return skb_pull_inline(skb, len); 24276be8ac2fSIlpo Järvinen } 24286be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 24296be8ac2fSIlpo Järvinen 2430419ae74eSIlpo Järvinen /** 243113244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 243213244cccSLuiz Augusto von Dentz * original position. 243313244cccSLuiz Augusto von Dentz * @skb: buffer to use 243413244cccSLuiz Augusto von Dentz * @len: amount of data to remove 243513244cccSLuiz Augusto von Dentz * 243613244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 243713244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 243813244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 243913244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 244013244cccSLuiz Augusto von Dentz */ 244113244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 244213244cccSLuiz Augusto von Dentz { 244313244cccSLuiz Augusto von Dentz void *data = skb->data; 244413244cccSLuiz Augusto von Dentz 244513244cccSLuiz Augusto von Dentz if (skb->len < len) 244613244cccSLuiz Augusto von Dentz return NULL; 244713244cccSLuiz Augusto von Dentz 244813244cccSLuiz Augusto von Dentz skb_pull(skb, len); 244913244cccSLuiz Augusto von Dentz 245013244cccSLuiz Augusto von Dentz return data; 245113244cccSLuiz Augusto von Dentz } 245213244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 245313244cccSLuiz Augusto von Dentz 245413244cccSLuiz Augusto von Dentz /** 2455419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2456419ae74eSIlpo Järvinen * @skb: buffer to alter 2457419ae74eSIlpo Järvinen * @len: new length 2458419ae74eSIlpo Järvinen * 2459419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2460419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2461419ae74eSIlpo Järvinen * The skb must be linear. 2462419ae74eSIlpo Järvinen */ 2463419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2464419ae74eSIlpo Järvinen { 2465419ae74eSIlpo Järvinen if (skb->len > len) 2466419ae74eSIlpo Järvinen __skb_trim(skb, len); 2467419ae74eSIlpo Järvinen } 2468419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2469419ae74eSIlpo Järvinen 24703cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 24711da177e4SLinus Torvalds */ 24721da177e4SLinus Torvalds 24733cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 24741da177e4SLinus Torvalds { 247527b437c8SHerbert Xu struct sk_buff **fragp; 247627b437c8SHerbert Xu struct sk_buff *frag; 24771da177e4SLinus Torvalds int offset = skb_headlen(skb); 24781da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 24791da177e4SLinus Torvalds int i; 248027b437c8SHerbert Xu int err; 248127b437c8SHerbert Xu 248227b437c8SHerbert Xu if (skb_cloned(skb) && 248327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 248427b437c8SHerbert Xu return err; 24851da177e4SLinus Torvalds 2486f4d26fb3SHerbert Xu i = 0; 2487f4d26fb3SHerbert Xu if (offset >= len) 2488f4d26fb3SHerbert Xu goto drop_pages; 2489f4d26fb3SHerbert Xu 2490f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 24919e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 249227b437c8SHerbert Xu 249327b437c8SHerbert Xu if (end < len) { 24941da177e4SLinus Torvalds offset = end; 249527b437c8SHerbert Xu continue; 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24989e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 249927b437c8SHerbert Xu 2500f4d26fb3SHerbert Xu drop_pages: 250127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 250227b437c8SHerbert Xu 250327b437c8SHerbert Xu for (; i < nfrags; i++) 2504ea2ab693SIan Campbell skb_frag_unref(skb, i); 250527b437c8SHerbert Xu 250621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 250727b437c8SHerbert Xu skb_drop_fraglist(skb); 2508f4d26fb3SHerbert Xu goto done; 250927b437c8SHerbert Xu } 251027b437c8SHerbert Xu 251127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 251227b437c8SHerbert Xu fragp = &frag->next) { 251327b437c8SHerbert Xu int end = offset + frag->len; 251427b437c8SHerbert Xu 251527b437c8SHerbert Xu if (skb_shared(frag)) { 251627b437c8SHerbert Xu struct sk_buff *nfrag; 251727b437c8SHerbert Xu 251827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 251927b437c8SHerbert Xu if (unlikely(!nfrag)) 252027b437c8SHerbert Xu return -ENOMEM; 252127b437c8SHerbert Xu 252227b437c8SHerbert Xu nfrag->next = frag->next; 252385bb2a60SEric Dumazet consume_skb(frag); 252427b437c8SHerbert Xu frag = nfrag; 252527b437c8SHerbert Xu *fragp = frag; 252627b437c8SHerbert Xu } 252727b437c8SHerbert Xu 252827b437c8SHerbert Xu if (end < len) { 252927b437c8SHerbert Xu offset = end; 253027b437c8SHerbert Xu continue; 253127b437c8SHerbert Xu } 253227b437c8SHerbert Xu 253327b437c8SHerbert Xu if (end > len && 253427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 253527b437c8SHerbert Xu return err; 253627b437c8SHerbert Xu 253727b437c8SHerbert Xu if (frag->next) 253827b437c8SHerbert Xu skb_drop_list(&frag->next); 253927b437c8SHerbert Xu break; 254027b437c8SHerbert Xu } 254127b437c8SHerbert Xu 2542f4d26fb3SHerbert Xu done: 254327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 25441da177e4SLinus Torvalds skb->data_len -= skb->len - len; 25451da177e4SLinus Torvalds skb->len = len; 25461da177e4SLinus Torvalds } else { 25471da177e4SLinus Torvalds skb->len = len; 25481da177e4SLinus Torvalds skb->data_len = 0; 254927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds 2552c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2553c21b48ccSEric Dumazet skb_condense(skb); 25541da177e4SLinus Torvalds return 0; 25551da177e4SLinus Torvalds } 2556b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 25571da177e4SLinus Torvalds 255888078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 255988078d98SEric Dumazet */ 256088078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 256188078d98SEric Dumazet { 256288078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 256388078d98SEric Dumazet int delta = skb->len - len; 256488078d98SEric Dumazet 2565d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2566d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2567d55bef50SDimitris Michailidis len); 256854970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 256954970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 257054970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 257154970a2fSVasily Averin 257254970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 257354970a2fSVasily Averin return -EINVAL; 257488078d98SEric Dumazet } 257588078d98SEric Dumazet return __pskb_trim(skb, len); 257688078d98SEric Dumazet } 257788078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 257888078d98SEric Dumazet 25791da177e4SLinus Torvalds /** 25801da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 25811da177e4SLinus Torvalds * @skb: buffer to reallocate 25821da177e4SLinus Torvalds * @delta: number of bytes to advance tail 25831da177e4SLinus Torvalds * 25841da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 25851da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 25861da177e4SLinus Torvalds * data from fragmented part. 25871da177e4SLinus Torvalds * 25881da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 25891da177e4SLinus Torvalds * 25901da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 25911da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 25921da177e4SLinus Torvalds * 25931da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 25941da177e4SLinus Torvalds * reloaded after call to this function. 25951da177e4SLinus Torvalds */ 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 25981da177e4SLinus Torvalds * when it is necessary. 25991da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 26001da177e4SLinus Torvalds * 2. It may change skb pointers. 26011da177e4SLinus Torvalds * 26021da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 26031da177e4SLinus Torvalds */ 2604af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 26051da177e4SLinus Torvalds { 26061da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 26071da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 26081da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 26091da177e4SLinus Torvalds */ 26104305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 26131da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 26141da177e4SLinus Torvalds GFP_ATOMIC)) 26151da177e4SLinus Torvalds return NULL; 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds 26189f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 26199f77fad3STim Hansen skb_tail_pointer(skb), delta)); 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 26221da177e4SLinus Torvalds * size of pulled pages. Superb. 26231da177e4SLinus Torvalds */ 262421dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 26251da177e4SLinus Torvalds goto pull_pages; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 26281da177e4SLinus Torvalds eat = delta; 26291da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26309e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26319e903e08SEric Dumazet 26329e903e08SEric Dumazet if (size >= eat) 26331da177e4SLinus Torvalds goto pull_pages; 26349e903e08SEric Dumazet eat -= size; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 263809001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 26391da177e4SLinus Torvalds * but taking into account that pulling is expected to 26401da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 26411da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 26421da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 26431da177e4SLinus Torvalds */ 26441da177e4SLinus Torvalds if (eat) { 26451da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 26461da177e4SLinus Torvalds struct sk_buff *clone = NULL; 26471da177e4SLinus Torvalds struct sk_buff *insp = NULL; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds do { 26501da177e4SLinus Torvalds if (list->len <= eat) { 26511da177e4SLinus Torvalds /* Eaten as whole. */ 26521da177e4SLinus Torvalds eat -= list->len; 26531da177e4SLinus Torvalds list = list->next; 26541da177e4SLinus Torvalds insp = list; 26551da177e4SLinus Torvalds } else { 26561da177e4SLinus Torvalds /* Eaten partially. */ 26572d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 26582d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 26592d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds if (skb_shared(list)) { 26621da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 26631da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 26641da177e4SLinus Torvalds if (!clone) 26651da177e4SLinus Torvalds return NULL; 26661da177e4SLinus Torvalds insp = list->next; 26671da177e4SLinus Torvalds list = clone; 26681da177e4SLinus Torvalds } else { 26691da177e4SLinus Torvalds /* This may be pulled without 26701da177e4SLinus Torvalds * problems. */ 26711da177e4SLinus Torvalds insp = list; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 26741da177e4SLinus Torvalds kfree_skb(clone); 26751da177e4SLinus Torvalds return NULL; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds break; 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds } while (eat); 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds /* Free pulled out fragments. */ 26821da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 26831da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2684ef527f96SEric Dumazet consume_skb(list); 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds /* And insert new clone at head. */ 26871da177e4SLinus Torvalds if (clone) { 26881da177e4SLinus Torvalds clone->next = list; 26891da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds pull_pages: 26951da177e4SLinus Torvalds eat = delta; 26961da177e4SLinus Torvalds k = 0; 26971da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26989e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26999e903e08SEric Dumazet 27009e903e08SEric Dumazet if (size <= eat) { 2701ea2ab693SIan Campbell skb_frag_unref(skb, i); 27029e903e08SEric Dumazet eat -= size; 27031da177e4SLinus Torvalds } else { 2704b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2705b54c9d5bSJonathan Lemon 2706b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 27071da177e4SLinus Torvalds if (eat) { 2708b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2709b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 27103ccc6c6fSlinzhang if (!i) 27113ccc6c6fSlinzhang goto end; 27121da177e4SLinus Torvalds eat = 0; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds k++; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 27181da177e4SLinus Torvalds 27193ccc6c6fSlinzhang end: 27201da177e4SLinus Torvalds skb->tail += delta; 27211da177e4SLinus Torvalds skb->data_len -= delta; 27221da177e4SLinus Torvalds 27231f8b977aSWillem de Bruijn if (!skb->data_len) 27241f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 27251f8b977aSWillem de Bruijn 272627a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 27271da177e4SLinus Torvalds } 2728b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 27291da177e4SLinus Torvalds 273022019b17SEric Dumazet /** 273122019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 273222019b17SEric Dumazet * @skb: source skb 273322019b17SEric Dumazet * @offset: offset in source 273422019b17SEric Dumazet * @to: destination buffer 273522019b17SEric Dumazet * @len: number of bytes to copy 273622019b17SEric Dumazet * 273722019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 273822019b17SEric Dumazet * destination buffer. 273922019b17SEric Dumazet * 274022019b17SEric Dumazet * CAUTION ! : 274122019b17SEric Dumazet * If its prototype is ever changed, 274222019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 274322019b17SEric Dumazet * since it is called from BPF assembly code. 274422019b17SEric Dumazet */ 27451da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 27461da177e4SLinus Torvalds { 27471a028e50SDavid S. Miller int start = skb_headlen(skb); 2748fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2749fbb398a8SDavid S. Miller int i, copy; 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds if (offset > (int)skb->len - len) 27521da177e4SLinus Torvalds goto fault; 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds /* Copy header. */ 27551a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 27561da177e4SLinus Torvalds if (copy > len) 27571da177e4SLinus Torvalds copy = len; 2758d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 27591da177e4SLinus Torvalds if ((len -= copy) == 0) 27601da177e4SLinus Torvalds return 0; 27611da177e4SLinus Torvalds offset += copy; 27621da177e4SLinus Torvalds to += copy; 27631da177e4SLinus Torvalds } 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27661a028e50SDavid S. Miller int end; 276751c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 27681da177e4SLinus Torvalds 2769547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27701a028e50SDavid S. Miller 277151c56b00SEric Dumazet end = start + skb_frag_size(f); 27721da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2773c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2774c613c209SWillem de Bruijn struct page *p; 27751da177e4SLinus Torvalds u8 *vaddr; 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds if (copy > len) 27781da177e4SLinus Torvalds copy = len; 27791da177e4SLinus Torvalds 2780c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2781b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2782c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2783c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2784c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 278551c56b00SEric Dumazet kunmap_atomic(vaddr); 2786c613c209SWillem de Bruijn } 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds if ((len -= copy) == 0) 27891da177e4SLinus Torvalds return 0; 27901da177e4SLinus Torvalds offset += copy; 27911da177e4SLinus Torvalds to += copy; 27921da177e4SLinus Torvalds } 27931a028e50SDavid S. Miller start = end; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 2796fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 27971a028e50SDavid S. Miller int end; 27981da177e4SLinus Torvalds 2799547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28001a028e50SDavid S. Miller 2801fbb398a8SDavid S. Miller end = start + frag_iter->len; 28021da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 28031da177e4SLinus Torvalds if (copy > len) 28041da177e4SLinus Torvalds copy = len; 2805fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 28061da177e4SLinus Torvalds goto fault; 28071da177e4SLinus Torvalds if ((len -= copy) == 0) 28081da177e4SLinus Torvalds return 0; 28091da177e4SLinus Torvalds offset += copy; 28101da177e4SLinus Torvalds to += copy; 28111da177e4SLinus Torvalds } 28121a028e50SDavid S. Miller start = end; 28131da177e4SLinus Torvalds } 2814a6686f2fSShirley Ma 28151da177e4SLinus Torvalds if (!len) 28161da177e4SLinus Torvalds return 0; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds fault: 28191da177e4SLinus Torvalds return -EFAULT; 28201da177e4SLinus Torvalds } 2821b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 28221da177e4SLinus Torvalds 28239c55e01cSJens Axboe /* 28249c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 28259c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 28269c55e01cSJens Axboe */ 28279c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 28289c55e01cSJens Axboe { 28298b9d3728SJarek Poplawski put_page(spd->pages[i]); 28308b9d3728SJarek Poplawski } 28319c55e01cSJens Axboe 2832a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 28334fb66994SJarek Poplawski unsigned int *offset, 283418aafc62SEric Dumazet struct sock *sk) 28358b9d3728SJarek Poplawski { 28365640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 28378b9d3728SJarek Poplawski 28385640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 28398b9d3728SJarek Poplawski return NULL; 28404fb66994SJarek Poplawski 28415640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 28424fb66994SJarek Poplawski 28435640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 28445640f768SEric Dumazet page_address(page) + *offset, *len); 28455640f768SEric Dumazet *offset = pfrag->offset; 28465640f768SEric Dumazet pfrag->offset += *len; 28474fb66994SJarek Poplawski 28485640f768SEric Dumazet return pfrag->page; 28499c55e01cSJens Axboe } 28509c55e01cSJens Axboe 285141c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 285241c73a0dSEric Dumazet struct page *page, 285341c73a0dSEric Dumazet unsigned int offset) 285441c73a0dSEric Dumazet { 285541c73a0dSEric Dumazet return spd->nr_pages && 285641c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 285741c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 285841c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 285941c73a0dSEric Dumazet } 286041c73a0dSEric Dumazet 28619c55e01cSJens Axboe /* 28629c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 28639c55e01cSJens Axboe */ 2864a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 286535f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 28664fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 286718aafc62SEric Dumazet bool linear, 28687a67e56fSJarek Poplawski struct sock *sk) 28699c55e01cSJens Axboe { 287041c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2871a108d5f3SDavid S. Miller return true; 28729c55e01cSJens Axboe 28738b9d3728SJarek Poplawski if (linear) { 287418aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 28758b9d3728SJarek Poplawski if (!page) 2876a108d5f3SDavid S. Miller return true; 287741c73a0dSEric Dumazet } 287841c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 287941c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2880a108d5f3SDavid S. Miller return false; 288141c73a0dSEric Dumazet } 28828b9d3728SJarek Poplawski get_page(page); 28839c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 28844fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 28859c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 28869c55e01cSJens Axboe spd->nr_pages++; 28878b9d3728SJarek Poplawski 2888a108d5f3SDavid S. Miller return false; 28899c55e01cSJens Axboe } 28909c55e01cSJens Axboe 2891a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 28922870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 289318aafc62SEric Dumazet unsigned int *len, 2894d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 289535f3d14dSJens Axboe struct sock *sk, 289635f3d14dSJens Axboe struct pipe_inode_info *pipe) 28979c55e01cSJens Axboe { 28982870c43dSOctavian Purdila if (!*len) 2899a108d5f3SDavid S. Miller return true; 29009c55e01cSJens Axboe 29012870c43dSOctavian Purdila /* skip this segment if already processed */ 29022870c43dSOctavian Purdila if (*off >= plen) { 29032870c43dSOctavian Purdila *off -= plen; 2904a108d5f3SDavid S. Miller return false; 29052870c43dSOctavian Purdila } 29062870c43dSOctavian Purdila 29072870c43dSOctavian Purdila /* ignore any bits we already processed */ 29089ca1b22dSEric Dumazet poff += *off; 29099ca1b22dSEric Dumazet plen -= *off; 29102870c43dSOctavian Purdila *off = 0; 29112870c43dSOctavian Purdila 291218aafc62SEric Dumazet do { 291318aafc62SEric Dumazet unsigned int flen = min(*len, plen); 29142870c43dSOctavian Purdila 291518aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 291618aafc62SEric Dumazet linear, sk)) 2917a108d5f3SDavid S. Miller return true; 291818aafc62SEric Dumazet poff += flen; 291918aafc62SEric Dumazet plen -= flen; 29202870c43dSOctavian Purdila *len -= flen; 292118aafc62SEric Dumazet } while (*len && plen); 29222870c43dSOctavian Purdila 2923a108d5f3SDavid S. Miller return false; 2924db43a282SOctavian Purdila } 29259c55e01cSJens Axboe 29269c55e01cSJens Axboe /* 2927a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 29282870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 29299c55e01cSJens Axboe */ 2930a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 293135f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 293235f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 29332870c43dSOctavian Purdila { 29342870c43dSOctavian Purdila int seg; 2935fa9835e5STom Herbert struct sk_buff *iter; 29369c55e01cSJens Axboe 29371d0c0b32SEric Dumazet /* map the linear part : 29382996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 29392996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 29402996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 29419c55e01cSJens Axboe */ 29422870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 29432870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 29442870c43dSOctavian Purdila skb_headlen(skb), 294518aafc62SEric Dumazet offset, len, spd, 29463a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 29471d0c0b32SEric Dumazet sk, pipe)) 2948a108d5f3SDavid S. Miller return true; 29499c55e01cSJens Axboe 29509c55e01cSJens Axboe /* 29519c55e01cSJens Axboe * then map the fragments 29529c55e01cSJens Axboe */ 29539c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 29549c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 29559c55e01cSJens Axboe 2956ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2957b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 295818aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2959a108d5f3SDavid S. Miller return true; 29609c55e01cSJens Axboe } 29619c55e01cSJens Axboe 2962fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2963fa9835e5STom Herbert if (*offset >= iter->len) { 2964fa9835e5STom Herbert *offset -= iter->len; 2965fa9835e5STom Herbert continue; 2966fa9835e5STom Herbert } 2967fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2968fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2969fa9835e5STom Herbert * case. 2970fa9835e5STom Herbert */ 2971fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2972fa9835e5STom Herbert return true; 2973fa9835e5STom Herbert } 2974fa9835e5STom Herbert 2975a108d5f3SDavid S. Miller return false; 29769c55e01cSJens Axboe } 29779c55e01cSJens Axboe 29789c55e01cSJens Axboe /* 29799c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2980fa9835e5STom Herbert * the fragments, and the frag list. 29819c55e01cSJens Axboe */ 2982a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 29839c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 298425869262SAl Viro unsigned int flags) 29859c55e01cSJens Axboe { 298641c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 298741c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 29889c55e01cSJens Axboe struct splice_pipe_desc spd = { 29899c55e01cSJens Axboe .pages = pages, 29909c55e01cSJens Axboe .partial = partial, 2991047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 299228a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 29939c55e01cSJens Axboe .spd_release = sock_spd_release, 29949c55e01cSJens Axboe }; 299535f3d14dSJens Axboe int ret = 0; 299635f3d14dSJens Axboe 2997fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 29989c55e01cSJens Axboe 2999a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 300025869262SAl Viro ret = splice_to_pipe(pipe, &spd); 30019c55e01cSJens Axboe 300235f3d14dSJens Axboe return ret; 30039c55e01cSJens Axboe } 30042b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 30059c55e01cSJens Axboe 30060739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 30070739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 30080739cd28SCong Wang { 30090739cd28SCong Wang struct socket *sock = sk->sk_socket; 30100739cd28SCong Wang 30110739cd28SCong Wang if (!sock) 30120739cd28SCong Wang return -EINVAL; 30130739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 30140739cd28SCong Wang } 30150739cd28SCong Wang 30160739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 30170739cd28SCong Wang size_t size, int flags) 30180739cd28SCong Wang { 30190739cd28SCong Wang struct socket *sock = sk->sk_socket; 30200739cd28SCong Wang 30210739cd28SCong Wang if (!sock) 30220739cd28SCong Wang return -EINVAL; 30230739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 30240739cd28SCong Wang } 30250739cd28SCong Wang 30260739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 30270739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 30280739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 30290739cd28SCong Wang size_t size, int flags); 30300739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 30310739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 303220bf50deSTom Herbert { 303320bf50deSTom Herbert unsigned int orig_len = len; 303420bf50deSTom Herbert struct sk_buff *head = skb; 303520bf50deSTom Herbert unsigned short fragidx; 303620bf50deSTom Herbert int slen, ret; 303720bf50deSTom Herbert 303820bf50deSTom Herbert do_frag_list: 303920bf50deSTom Herbert 304020bf50deSTom Herbert /* Deal with head data */ 304120bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 304220bf50deSTom Herbert struct kvec kv; 304320bf50deSTom Herbert struct msghdr msg; 304420bf50deSTom Herbert 304520bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 304620bf50deSTom Herbert kv.iov_base = skb->data + offset; 3047db5980d8SJohn Fastabend kv.iov_len = slen; 304820bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 3049bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 305020bf50deSTom Herbert 30510739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 30520739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 305320bf50deSTom Herbert if (ret <= 0) 305420bf50deSTom Herbert goto error; 305520bf50deSTom Herbert 305620bf50deSTom Herbert offset += ret; 305720bf50deSTom Herbert len -= ret; 305820bf50deSTom Herbert } 305920bf50deSTom Herbert 306020bf50deSTom Herbert /* All the data was skb head? */ 306120bf50deSTom Herbert if (!len) 306220bf50deSTom Herbert goto out; 306320bf50deSTom Herbert 306420bf50deSTom Herbert /* Make offset relative to start of frags */ 306520bf50deSTom Herbert offset -= skb_headlen(skb); 306620bf50deSTom Herbert 306720bf50deSTom Herbert /* Find where we are in frag list */ 306820bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 306920bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 307020bf50deSTom Herbert 3071d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 307220bf50deSTom Herbert break; 307320bf50deSTom Herbert 3074d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 307520bf50deSTom Herbert } 307620bf50deSTom Herbert 307720bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 307820bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 307920bf50deSTom Herbert 3080d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 308120bf50deSTom Herbert 308220bf50deSTom Herbert while (slen) { 30830739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 30840739cd28SCong Wang sendpage_unlocked, sk, 30850739cd28SCong Wang skb_frag_page(frag), 3086b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 308720bf50deSTom Herbert slen, MSG_DONTWAIT); 308820bf50deSTom Herbert if (ret <= 0) 308920bf50deSTom Herbert goto error; 309020bf50deSTom Herbert 309120bf50deSTom Herbert len -= ret; 309220bf50deSTom Herbert offset += ret; 309320bf50deSTom Herbert slen -= ret; 309420bf50deSTom Herbert } 309520bf50deSTom Herbert 309620bf50deSTom Herbert offset = 0; 309720bf50deSTom Herbert } 309820bf50deSTom Herbert 309920bf50deSTom Herbert if (len) { 310020bf50deSTom Herbert /* Process any frag lists */ 310120bf50deSTom Herbert 310220bf50deSTom Herbert if (skb == head) { 310320bf50deSTom Herbert if (skb_has_frag_list(skb)) { 310420bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 310520bf50deSTom Herbert goto do_frag_list; 310620bf50deSTom Herbert } 310720bf50deSTom Herbert } else if (skb->next) { 310820bf50deSTom Herbert skb = skb->next; 310920bf50deSTom Herbert goto do_frag_list; 311020bf50deSTom Herbert } 311120bf50deSTom Herbert } 311220bf50deSTom Herbert 311320bf50deSTom Herbert out: 311420bf50deSTom Herbert return orig_len - len; 311520bf50deSTom Herbert 311620bf50deSTom Herbert error: 311720bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 311820bf50deSTom Herbert } 31190739cd28SCong Wang 31200739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 31210739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 31220739cd28SCong Wang int len) 31230739cd28SCong Wang { 31240739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 31250739cd28SCong Wang kernel_sendpage_locked); 31260739cd28SCong Wang } 312720bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 312820bf50deSTom Herbert 31290739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 31300739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 31310739cd28SCong Wang { 31320739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 31330739cd28SCong Wang sendpage_unlocked); 31340739cd28SCong Wang } 31350739cd28SCong Wang 3136357b40a1SHerbert Xu /** 3137357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3138357b40a1SHerbert Xu * @skb: destination buffer 3139357b40a1SHerbert Xu * @offset: offset in destination 3140357b40a1SHerbert Xu * @from: source buffer 3141357b40a1SHerbert Xu * @len: number of bytes to copy 3142357b40a1SHerbert Xu * 3143357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3144357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3145357b40a1SHerbert Xu * traversing fragment lists and such. 3146357b40a1SHerbert Xu */ 3147357b40a1SHerbert Xu 31480c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3149357b40a1SHerbert Xu { 31501a028e50SDavid S. Miller int start = skb_headlen(skb); 3151fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3152fbb398a8SDavid S. Miller int i, copy; 3153357b40a1SHerbert Xu 3154357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3155357b40a1SHerbert Xu goto fault; 3156357b40a1SHerbert Xu 31571a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3158357b40a1SHerbert Xu if (copy > len) 3159357b40a1SHerbert Xu copy = len; 316027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3161357b40a1SHerbert Xu if ((len -= copy) == 0) 3162357b40a1SHerbert Xu return 0; 3163357b40a1SHerbert Xu offset += copy; 3164357b40a1SHerbert Xu from += copy; 3165357b40a1SHerbert Xu } 3166357b40a1SHerbert Xu 3167357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3168357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31691a028e50SDavid S. Miller int end; 3170357b40a1SHerbert Xu 3171547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31721a028e50SDavid S. Miller 31739e903e08SEric Dumazet end = start + skb_frag_size(frag); 3174357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3175c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3176c613c209SWillem de Bruijn struct page *p; 3177357b40a1SHerbert Xu u8 *vaddr; 3178357b40a1SHerbert Xu 3179357b40a1SHerbert Xu if (copy > len) 3180357b40a1SHerbert Xu copy = len; 3181357b40a1SHerbert Xu 3182c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3183b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3184c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3185c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3186c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 318751c56b00SEric Dumazet kunmap_atomic(vaddr); 3188c613c209SWillem de Bruijn } 3189357b40a1SHerbert Xu 3190357b40a1SHerbert Xu if ((len -= copy) == 0) 3191357b40a1SHerbert Xu return 0; 3192357b40a1SHerbert Xu offset += copy; 3193357b40a1SHerbert Xu from += copy; 3194357b40a1SHerbert Xu } 31951a028e50SDavid S. Miller start = end; 3196357b40a1SHerbert Xu } 3197357b40a1SHerbert Xu 3198fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31991a028e50SDavid S. Miller int end; 3200357b40a1SHerbert Xu 3201547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32021a028e50SDavid S. Miller 3203fbb398a8SDavid S. Miller end = start + frag_iter->len; 3204357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3205357b40a1SHerbert Xu if (copy > len) 3206357b40a1SHerbert Xu copy = len; 3207fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 32081a028e50SDavid S. Miller from, copy)) 3209357b40a1SHerbert Xu goto fault; 3210357b40a1SHerbert Xu if ((len -= copy) == 0) 3211357b40a1SHerbert Xu return 0; 3212357b40a1SHerbert Xu offset += copy; 3213357b40a1SHerbert Xu from += copy; 3214357b40a1SHerbert Xu } 32151a028e50SDavid S. Miller start = end; 3216357b40a1SHerbert Xu } 3217357b40a1SHerbert Xu if (!len) 3218357b40a1SHerbert Xu return 0; 3219357b40a1SHerbert Xu 3220357b40a1SHerbert Xu fault: 3221357b40a1SHerbert Xu return -EFAULT; 3222357b40a1SHerbert Xu } 3223357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3224357b40a1SHerbert Xu 32251da177e4SLinus Torvalds /* Checksum skb data. */ 32262817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 32272817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 32281da177e4SLinus Torvalds { 32291a028e50SDavid S. Miller int start = skb_headlen(skb); 32301a028e50SDavid S. Miller int i, copy = start - offset; 3231fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32321da177e4SLinus Torvalds int pos = 0; 32331da177e4SLinus Torvalds 32341da177e4SLinus Torvalds /* Checksum header. */ 32351da177e4SLinus Torvalds if (copy > 0) { 32361da177e4SLinus Torvalds if (copy > len) 32371da177e4SLinus Torvalds copy = len; 32382544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 32392544af03SMatteo Croce skb->data + offset, copy, csum); 32401da177e4SLinus Torvalds if ((len -= copy) == 0) 32411da177e4SLinus Torvalds return csum; 32421da177e4SLinus Torvalds offset += copy; 32431da177e4SLinus Torvalds pos = copy; 32441da177e4SLinus Torvalds } 32451da177e4SLinus Torvalds 32461da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32471a028e50SDavid S. Miller int end; 324851c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32491da177e4SLinus Torvalds 3250547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32511a028e50SDavid S. Miller 325251c56b00SEric Dumazet end = start + skb_frag_size(frag); 32531da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3254c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3255c613c209SWillem de Bruijn struct page *p; 325644bb9363SAl Viro __wsum csum2; 32571da177e4SLinus Torvalds u8 *vaddr; 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds if (copy > len) 32601da177e4SLinus Torvalds copy = len; 3261c613c209SWillem de Bruijn 3262c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3263b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3264c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3265c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 32662544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 32672544af03SMatteo Croce csum_partial_ext, 32682544af03SMatteo Croce vaddr + p_off, p_len, 0); 326951c56b00SEric Dumazet kunmap_atomic(vaddr); 32702544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 32712544af03SMatteo Croce csum_block_add_ext, csum, 32722544af03SMatteo Croce csum2, pos, p_len); 3273c613c209SWillem de Bruijn pos += p_len; 3274c613c209SWillem de Bruijn } 3275c613c209SWillem de Bruijn 32761da177e4SLinus Torvalds if (!(len -= copy)) 32771da177e4SLinus Torvalds return csum; 32781da177e4SLinus Torvalds offset += copy; 32791da177e4SLinus Torvalds } 32801a028e50SDavid S. Miller start = end; 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds 3283fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32841a028e50SDavid S. Miller int end; 32851da177e4SLinus Torvalds 3286547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32871a028e50SDavid S. Miller 3288fbb398a8SDavid S. Miller end = start + frag_iter->len; 32891da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32905f92a738SAl Viro __wsum csum2; 32911da177e4SLinus Torvalds if (copy > len) 32921da177e4SLinus Torvalds copy = len; 32932817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 32942817a336SDaniel Borkmann copy, 0, ops); 32952544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 32962544af03SMatteo Croce csum, csum2, pos, copy); 32971da177e4SLinus Torvalds if ((len -= copy) == 0) 32981da177e4SLinus Torvalds return csum; 32991da177e4SLinus Torvalds offset += copy; 33001da177e4SLinus Torvalds pos += copy; 33011da177e4SLinus Torvalds } 33021a028e50SDavid S. Miller start = end; 33031da177e4SLinus Torvalds } 330409a62660SKris Katterjohn BUG_ON(len); 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds return csum; 33071da177e4SLinus Torvalds } 33082817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 33092817a336SDaniel Borkmann 33102817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 33112817a336SDaniel Borkmann int len, __wsum csum) 33122817a336SDaniel Borkmann { 33132817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3314cea80ea8SDaniel Borkmann .update = csum_partial_ext, 33152817a336SDaniel Borkmann .combine = csum_block_add_ext, 33162817a336SDaniel Borkmann }; 33172817a336SDaniel Borkmann 33182817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 33192817a336SDaniel Borkmann } 3320b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 33211da177e4SLinus Torvalds 33221da177e4SLinus Torvalds /* Both of above in one bottle. */ 33231da177e4SLinus Torvalds 332481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 33258d5930dfSAl Viro u8 *to, int len) 33261da177e4SLinus Torvalds { 33271a028e50SDavid S. Miller int start = skb_headlen(skb); 33281a028e50SDavid S. Miller int i, copy = start - offset; 3329fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 33301da177e4SLinus Torvalds int pos = 0; 33318d5930dfSAl Viro __wsum csum = 0; 33321da177e4SLinus Torvalds 33331da177e4SLinus Torvalds /* Copy header. */ 33341da177e4SLinus Torvalds if (copy > 0) { 33351da177e4SLinus Torvalds if (copy > len) 33361da177e4SLinus Torvalds copy = len; 33371da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3338cc44c17bSAl Viro copy); 33391da177e4SLinus Torvalds if ((len -= copy) == 0) 33401da177e4SLinus Torvalds return csum; 33411da177e4SLinus Torvalds offset += copy; 33421da177e4SLinus Torvalds to += copy; 33431da177e4SLinus Torvalds pos = copy; 33441da177e4SLinus Torvalds } 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33471a028e50SDavid S. Miller int end; 33481da177e4SLinus Torvalds 3349547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33501a028e50SDavid S. Miller 33519e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 33521da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3353c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3354c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3355c613c209SWillem de Bruijn struct page *p; 33565084205fSAl Viro __wsum csum2; 33571da177e4SLinus Torvalds u8 *vaddr; 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds if (copy > len) 33601da177e4SLinus Torvalds copy = len; 3361c613c209SWillem de Bruijn 3362c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3363b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3364c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3365c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3366c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3367c613c209SWillem de Bruijn to + copied, 3368cc44c17bSAl Viro p_len); 336951c56b00SEric Dumazet kunmap_atomic(vaddr); 33701da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3371c613c209SWillem de Bruijn pos += p_len; 3372c613c209SWillem de Bruijn } 3373c613c209SWillem de Bruijn 33741da177e4SLinus Torvalds if (!(len -= copy)) 33751da177e4SLinus Torvalds return csum; 33761da177e4SLinus Torvalds offset += copy; 33771da177e4SLinus Torvalds to += copy; 33781da177e4SLinus Torvalds } 33791a028e50SDavid S. Miller start = end; 33801da177e4SLinus Torvalds } 33811da177e4SLinus Torvalds 3382fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 338381d77662SAl Viro __wsum csum2; 33841a028e50SDavid S. Miller int end; 33851da177e4SLinus Torvalds 3386547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33871a028e50SDavid S. Miller 3388fbb398a8SDavid S. Miller end = start + frag_iter->len; 33891da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 33901da177e4SLinus Torvalds if (copy > len) 33911da177e4SLinus Torvalds copy = len; 3392fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 33931a028e50SDavid S. Miller offset - start, 33948d5930dfSAl Viro to, copy); 33951da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 33961da177e4SLinus Torvalds if ((len -= copy) == 0) 33971da177e4SLinus Torvalds return csum; 33981da177e4SLinus Torvalds offset += copy; 33991da177e4SLinus Torvalds to += copy; 34001da177e4SLinus Torvalds pos += copy; 34011da177e4SLinus Torvalds } 34021a028e50SDavid S. Miller start = end; 34031da177e4SLinus Torvalds } 340409a62660SKris Katterjohn BUG_ON(len); 34051da177e4SLinus Torvalds return csum; 34061da177e4SLinus Torvalds } 3407b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 34081da177e4SLinus Torvalds 340949f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 341049f8e832SCong Wang { 341149f8e832SCong Wang __sum16 sum; 341249f8e832SCong Wang 341349f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 341414641931SCong Wang /* See comments in __skb_checksum_complete(). */ 341549f8e832SCong Wang if (likely(!sum)) { 341649f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 341749f8e832SCong Wang !skb->csum_complete_sw) 34187fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 341949f8e832SCong Wang } 342049f8e832SCong Wang if (!skb_shared(skb)) 342149f8e832SCong Wang skb->csum_valid = !sum; 342249f8e832SCong Wang return sum; 342349f8e832SCong Wang } 342449f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 342549f8e832SCong Wang 342614641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 342714641931SCong Wang * which has been changed from the hardware checksum, for example, by 342814641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 342914641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 343014641931SCong Wang * 343114641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 343214641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 343314641931SCong Wang * shared. 343414641931SCong Wang */ 343549f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 343649f8e832SCong Wang { 343749f8e832SCong Wang __wsum csum; 343849f8e832SCong Wang __sum16 sum; 343949f8e832SCong Wang 344049f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 344149f8e832SCong Wang 344249f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 344314641931SCong Wang /* This check is inverted, because we already knew the hardware 344414641931SCong Wang * checksum is invalid before calling this function. So, if the 344514641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 344614641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 344714641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 344814641931SCong Wang * when moving skb->data around. 344914641931SCong Wang */ 345049f8e832SCong Wang if (likely(!sum)) { 345149f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 345249f8e832SCong Wang !skb->csum_complete_sw) 34537fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 345449f8e832SCong Wang } 345549f8e832SCong Wang 345649f8e832SCong Wang if (!skb_shared(skb)) { 345749f8e832SCong Wang /* Save full packet checksum */ 345849f8e832SCong Wang skb->csum = csum; 345949f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 346049f8e832SCong Wang skb->csum_complete_sw = 1; 346149f8e832SCong Wang skb->csum_valid = !sum; 346249f8e832SCong Wang } 346349f8e832SCong Wang 346449f8e832SCong Wang return sum; 346549f8e832SCong Wang } 346649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 346749f8e832SCong Wang 34689617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 34699617813dSDavide Caratti { 34709617813dSDavide Caratti net_warn_ratelimited( 34719617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34729617813dSDavide Caratti __func__); 34739617813dSDavide Caratti return 0; 34749617813dSDavide Caratti } 34759617813dSDavide Caratti 34769617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 34779617813dSDavide Caratti int offset, int len) 34789617813dSDavide Caratti { 34799617813dSDavide Caratti net_warn_ratelimited( 34809617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34819617813dSDavide Caratti __func__); 34829617813dSDavide Caratti return 0; 34839617813dSDavide Caratti } 34849617813dSDavide Caratti 34859617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 34869617813dSDavide Caratti .update = warn_crc32c_csum_update, 34879617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 34889617813dSDavide Caratti }; 34899617813dSDavide Caratti 34909617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 34919617813dSDavide Caratti &default_crc32c_ops; 34929617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 34939617813dSDavide Caratti 3494af2806f8SThomas Graf /** 3495af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3496af2806f8SThomas Graf * @from: source buffer 3497af2806f8SThomas Graf * 3498af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3499af2806f8SThomas Graf * into skb_zerocopy(). 3500af2806f8SThomas Graf */ 3501af2806f8SThomas Graf unsigned int 3502af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3503af2806f8SThomas Graf { 3504af2806f8SThomas Graf unsigned int hlen = 0; 3505af2806f8SThomas Graf 3506af2806f8SThomas Graf if (!from->head_frag || 3507af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3508a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3509af2806f8SThomas Graf hlen = skb_headlen(from); 3510a17ad096SPravin B Shelar if (!hlen) 3511a17ad096SPravin B Shelar hlen = from->len; 3512a17ad096SPravin B Shelar } 3513af2806f8SThomas Graf 3514af2806f8SThomas Graf if (skb_has_frag_list(from)) 3515af2806f8SThomas Graf hlen = from->len; 3516af2806f8SThomas Graf 3517af2806f8SThomas Graf return hlen; 3518af2806f8SThomas Graf } 3519af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3520af2806f8SThomas Graf 3521af2806f8SThomas Graf /** 3522af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3523af2806f8SThomas Graf * @to: destination buffer 35247fceb4deSMasanari Iida * @from: source buffer 3525af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3526af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3527af2806f8SThomas Graf * 3528af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3529af2806f8SThomas Graf * to the frags in the source buffer. 3530af2806f8SThomas Graf * 3531af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3532af2806f8SThomas Graf * headroom in the `to` buffer. 353336d5fe6aSZoltan Kiss * 353436d5fe6aSZoltan Kiss * Return value: 353536d5fe6aSZoltan Kiss * 0: everything is OK 353636d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 353736d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3538af2806f8SThomas Graf */ 353936d5fe6aSZoltan Kiss int 354036d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3541af2806f8SThomas Graf { 3542af2806f8SThomas Graf int i, j = 0; 3543af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 354436d5fe6aSZoltan Kiss int ret; 3545af2806f8SThomas Graf struct page *page; 3546af2806f8SThomas Graf unsigned int offset; 3547af2806f8SThomas Graf 3548af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3549af2806f8SThomas Graf 3550af2806f8SThomas Graf /* dont bother with small payloads */ 355136d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 355236d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3553af2806f8SThomas Graf 3554af2806f8SThomas Graf if (hlen) { 355536d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 355636d5fe6aSZoltan Kiss if (unlikely(ret)) 355736d5fe6aSZoltan Kiss return ret; 3558af2806f8SThomas Graf len -= hlen; 3559af2806f8SThomas Graf } else { 3560af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3561af2806f8SThomas Graf if (plen) { 3562af2806f8SThomas Graf page = virt_to_head_page(from->head); 3563af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3564af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3565af2806f8SThomas Graf get_page(page); 3566af2806f8SThomas Graf j = 1; 3567af2806f8SThomas Graf len -= plen; 3568af2806f8SThomas Graf } 3569af2806f8SThomas Graf } 3570af2806f8SThomas Graf 3571ede57d58SRichard Gobert skb_len_add(to, len + plen); 3572af2806f8SThomas Graf 357336d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 357436d5fe6aSZoltan Kiss skb_tx_error(from); 357536d5fe6aSZoltan Kiss return -ENOMEM; 357636d5fe6aSZoltan Kiss } 35771f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 357836d5fe6aSZoltan Kiss 3579af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3580d8e18a51SMatthew Wilcox (Oracle) int size; 3581d8e18a51SMatthew Wilcox (Oracle) 3582af2806f8SThomas Graf if (!len) 3583af2806f8SThomas Graf break; 3584af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3585d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3586d8e18a51SMatthew Wilcox (Oracle) len); 3587d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3588d8e18a51SMatthew Wilcox (Oracle) len -= size; 3589af2806f8SThomas Graf skb_frag_ref(to, j); 3590af2806f8SThomas Graf j++; 3591af2806f8SThomas Graf } 3592af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 359336d5fe6aSZoltan Kiss 359436d5fe6aSZoltan Kiss return 0; 3595af2806f8SThomas Graf } 3596af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3597af2806f8SThomas Graf 35981da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 35991da177e4SLinus Torvalds { 3600d3bc23e7SAl Viro __wsum csum; 36011da177e4SLinus Torvalds long csstart; 36021da177e4SLinus Torvalds 360384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 360455508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 36051da177e4SLinus Torvalds else 36061da177e4SLinus Torvalds csstart = skb_headlen(skb); 36071da177e4SLinus Torvalds 360809a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 36091da177e4SLinus Torvalds 3610d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 36111da177e4SLinus Torvalds 36121da177e4SLinus Torvalds csum = 0; 36131da177e4SLinus Torvalds if (csstart != skb->len) 36141da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 36158d5930dfSAl Viro skb->len - csstart); 36161da177e4SLinus Torvalds 361784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3618ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 36191da177e4SLinus Torvalds 3620d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 36211da177e4SLinus Torvalds } 36221da177e4SLinus Torvalds } 3623b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 36241da177e4SLinus Torvalds 36251da177e4SLinus Torvalds /** 36261da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 36271da177e4SLinus Torvalds * @list: list to dequeue from 36281da177e4SLinus Torvalds * 36291da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 36301da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 36311da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36321da177e4SLinus Torvalds */ 36331da177e4SLinus Torvalds 36341da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 36351da177e4SLinus Torvalds { 36361da177e4SLinus Torvalds unsigned long flags; 36371da177e4SLinus Torvalds struct sk_buff *result; 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36401da177e4SLinus Torvalds result = __skb_dequeue(list); 36411da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36421da177e4SLinus Torvalds return result; 36431da177e4SLinus Torvalds } 3644b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 36451da177e4SLinus Torvalds 36461da177e4SLinus Torvalds /** 36471da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 36481da177e4SLinus Torvalds * @list: list to dequeue from 36491da177e4SLinus Torvalds * 36501da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 36511da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 36521da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36531da177e4SLinus Torvalds */ 36541da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 36551da177e4SLinus Torvalds { 36561da177e4SLinus Torvalds unsigned long flags; 36571da177e4SLinus Torvalds struct sk_buff *result; 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36601da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 36611da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36621da177e4SLinus Torvalds return result; 36631da177e4SLinus Torvalds } 3664b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 36651da177e4SLinus Torvalds 36661da177e4SLinus Torvalds /** 36671da177e4SLinus Torvalds * skb_queue_purge - empty a list 36681da177e4SLinus Torvalds * @list: list to empty 36691da177e4SLinus Torvalds * 36701da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 36711da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 36721da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 36731da177e4SLinus Torvalds */ 36741da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 36751da177e4SLinus Torvalds { 36761da177e4SLinus Torvalds struct sk_buff *skb; 36771da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 36781da177e4SLinus Torvalds kfree_skb(skb); 36791da177e4SLinus Torvalds } 3680b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 36811da177e4SLinus Torvalds 36821da177e4SLinus Torvalds /** 36839f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 36849f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3685385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 36869f5afeaeSYaogong Wang * 36879f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 36889f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 36899f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 36909f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 36919f5afeaeSYaogong Wang */ 3692385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 36939f5afeaeSYaogong Wang { 36947c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3695385114deSPeter Oskolkov unsigned int sum = 0; 36969f5afeaeSYaogong Wang 36977c90584cSEric Dumazet while (p) { 36987c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 36997c90584cSEric Dumazet 37007c90584cSEric Dumazet p = rb_next(p); 37017c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3702385114deSPeter Oskolkov sum += skb->truesize; 37039f5afeaeSYaogong Wang kfree_skb(skb); 37047c90584cSEric Dumazet } 3705385114deSPeter Oskolkov return sum; 37069f5afeaeSYaogong Wang } 37079f5afeaeSYaogong Wang 37089f5afeaeSYaogong Wang /** 37091da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 37101da177e4SLinus Torvalds * @list: list to use 37111da177e4SLinus Torvalds * @newsk: buffer to queue 37121da177e4SLinus Torvalds * 37131da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 37141da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37151da177e4SLinus Torvalds * safely. 37161da177e4SLinus Torvalds * 37171da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37181da177e4SLinus Torvalds */ 37191da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 37201da177e4SLinus Torvalds { 37211da177e4SLinus Torvalds unsigned long flags; 37221da177e4SLinus Torvalds 37231da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37241da177e4SLinus Torvalds __skb_queue_head(list, newsk); 37251da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37261da177e4SLinus Torvalds } 3727b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 37281da177e4SLinus Torvalds 37291da177e4SLinus Torvalds /** 37301da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 37311da177e4SLinus Torvalds * @list: list to use 37321da177e4SLinus Torvalds * @newsk: buffer to queue 37331da177e4SLinus Torvalds * 37341da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 37351da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37361da177e4SLinus Torvalds * safely. 37371da177e4SLinus Torvalds * 37381da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37391da177e4SLinus Torvalds */ 37401da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 37411da177e4SLinus Torvalds { 37421da177e4SLinus Torvalds unsigned long flags; 37431da177e4SLinus Torvalds 37441da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37451da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 37461da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37471da177e4SLinus Torvalds } 3748b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 37498728b834SDavid S. Miller 37501da177e4SLinus Torvalds /** 37511da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 37521da177e4SLinus Torvalds * @skb: buffer to remove 37538728b834SDavid S. Miller * @list: list to use 37541da177e4SLinus Torvalds * 37558728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 37568728b834SDavid S. Miller * function is atomic with respect to other list locked calls 37571da177e4SLinus Torvalds * 37588728b834SDavid S. Miller * You must know what list the SKB is on. 37591da177e4SLinus Torvalds */ 37608728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 37611da177e4SLinus Torvalds { 37621da177e4SLinus Torvalds unsigned long flags; 37631da177e4SLinus Torvalds 37641da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37658728b834SDavid S. Miller __skb_unlink(skb, list); 37661da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37671da177e4SLinus Torvalds } 3768b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 37691da177e4SLinus Torvalds 37701da177e4SLinus Torvalds /** 37711da177e4SLinus Torvalds * skb_append - append a buffer 37721da177e4SLinus Torvalds * @old: buffer to insert after 37731da177e4SLinus Torvalds * @newsk: buffer to insert 37748728b834SDavid S. Miller * @list: list to use 37751da177e4SLinus Torvalds * 37761da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 37771da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 37781da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37791da177e4SLinus Torvalds */ 37808728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 37811da177e4SLinus Torvalds { 37821da177e4SLinus Torvalds unsigned long flags; 37831da177e4SLinus Torvalds 37848728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 37857de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 37868728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 37871da177e4SLinus Torvalds } 3788b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 37891da177e4SLinus Torvalds 37901da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 37911da177e4SLinus Torvalds struct sk_buff* skb1, 37921da177e4SLinus Torvalds const u32 len, const int pos) 37931da177e4SLinus Torvalds { 37941da177e4SLinus Torvalds int i; 37951da177e4SLinus Torvalds 3796d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3797d626f62bSArnaldo Carvalho de Melo pos - len); 37981da177e4SLinus Torvalds /* And move data appendix as is. */ 37991da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 38001da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 38031da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38041da177e4SLinus Torvalds skb1->data_len = skb->data_len; 38051da177e4SLinus Torvalds skb1->len += skb1->data_len; 38061da177e4SLinus Torvalds skb->data_len = 0; 38071da177e4SLinus Torvalds skb->len = len; 380827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 38121da177e4SLinus Torvalds struct sk_buff* skb1, 38131da177e4SLinus Torvalds const u32 len, int pos) 38141da177e4SLinus Torvalds { 38151da177e4SLinus Torvalds int i, k = 0; 38161da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 38171da177e4SLinus Torvalds 38181da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38191da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 38201da177e4SLinus Torvalds skb->len = len; 38211da177e4SLinus Torvalds skb->data_len = len - pos; 38221da177e4SLinus Torvalds 38231da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 38249e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 38251da177e4SLinus Torvalds 38261da177e4SLinus Torvalds if (pos + size > len) { 38271da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 38281da177e4SLinus Torvalds 38291da177e4SLinus Torvalds if (pos < len) { 38301da177e4SLinus Torvalds /* Split frag. 38311da177e4SLinus Torvalds * We have two variants in this case: 38321da177e4SLinus Torvalds * 1. Move all the frag to the second 38331da177e4SLinus Torvalds * part, if it is possible. F.e. 38341da177e4SLinus Torvalds * this approach is mandatory for TUX, 38351da177e4SLinus Torvalds * where splitting is expensive. 38361da177e4SLinus Torvalds * 2. Split is accurately. We make this. 38371da177e4SLinus Torvalds */ 3838ea2ab693SIan Campbell skb_frag_ref(skb, i); 3839b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 38409e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 38419e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 38421da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds k++; 38451da177e4SLinus Torvalds } else 38461da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38471da177e4SLinus Torvalds pos += size; 38481da177e4SLinus Torvalds } 38491da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 38501da177e4SLinus Torvalds } 38511da177e4SLinus Torvalds 38521da177e4SLinus Torvalds /** 38531da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 38541da177e4SLinus Torvalds * @skb: the buffer to split 38551da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 38561da177e4SLinus Torvalds * @len: new length for skb 38571da177e4SLinus Torvalds */ 38581da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 38591da177e4SLinus Torvalds { 38601da177e4SLinus Torvalds int pos = skb_headlen(skb); 38619b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 38621da177e4SLinus Torvalds 3863753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3864753f1ca4SPavel Begunkov 38659b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 38661f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 38671da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 38681da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 38691da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 38701da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 38711da177e4SLinus Torvalds } 3872b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 38731da177e4SLinus Torvalds 38749f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 38759f782db3SIlpo Järvinen * 38769f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 38779f782db3SIlpo Järvinen */ 3878832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3879832d11c5SIlpo Järvinen { 3880c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3881832d11c5SIlpo Järvinen } 3882832d11c5SIlpo Järvinen 3883832d11c5SIlpo Järvinen /** 3884832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3885832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3886832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3887832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3888832d11c5SIlpo Järvinen * 3889832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 389020e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3891832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3892832d11c5SIlpo Järvinen * 3893832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3894832d11c5SIlpo Järvinen * 3895832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3896832d11c5SIlpo Järvinen * to have non-paged data as well. 3897832d11c5SIlpo Järvinen * 3898832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3899832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3900832d11c5SIlpo Järvinen */ 3901832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3902832d11c5SIlpo Järvinen { 3903832d11c5SIlpo Järvinen int from, to, merge, todo; 3904d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3905832d11c5SIlpo Järvinen 3906832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3907f8071cdeSEric Dumazet 3908f8071cdeSEric Dumazet if (skb_headlen(skb)) 3909f8071cdeSEric Dumazet return 0; 39101f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 39111f8b977aSWillem de Bruijn return 0; 3912832d11c5SIlpo Järvinen 3913832d11c5SIlpo Järvinen todo = shiftlen; 3914832d11c5SIlpo Järvinen from = 0; 3915832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3916832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3917832d11c5SIlpo Järvinen 3918832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3919832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3920832d11c5SIlpo Järvinen */ 3921832d11c5SIlpo Järvinen if (!to || 3922ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3923b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3924832d11c5SIlpo Järvinen merge = -1; 3925832d11c5SIlpo Järvinen } else { 3926832d11c5SIlpo Järvinen merge = to - 1; 3927832d11c5SIlpo Järvinen 39289e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3929832d11c5SIlpo Järvinen if (todo < 0) { 3930832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3931832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3932832d11c5SIlpo Järvinen return 0; 3933832d11c5SIlpo Järvinen 39349f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 39359f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3936832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3937832d11c5SIlpo Järvinen 39389e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 39399e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3940b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3941832d11c5SIlpo Järvinen 3942832d11c5SIlpo Järvinen goto onlymerged; 3943832d11c5SIlpo Järvinen } 3944832d11c5SIlpo Järvinen 3945832d11c5SIlpo Järvinen from++; 3946832d11c5SIlpo Järvinen } 3947832d11c5SIlpo Järvinen 3948832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3949832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3950832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3951832d11c5SIlpo Järvinen return 0; 3952832d11c5SIlpo Järvinen 3953832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3954832d11c5SIlpo Järvinen return 0; 3955832d11c5SIlpo Järvinen 3956832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3957832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3958832d11c5SIlpo Järvinen return 0; 3959832d11c5SIlpo Järvinen 3960832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3961832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3962832d11c5SIlpo Järvinen 39639e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3964832d11c5SIlpo Järvinen *fragto = *fragfrom; 39659e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3966832d11c5SIlpo Järvinen from++; 3967832d11c5SIlpo Järvinen to++; 3968832d11c5SIlpo Järvinen 3969832d11c5SIlpo Järvinen } else { 3970ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3971b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3972b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 39739e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3974832d11c5SIlpo Järvinen 3975b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 39769e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3977832d11c5SIlpo Järvinen todo = 0; 3978832d11c5SIlpo Järvinen 3979832d11c5SIlpo Järvinen to++; 3980832d11c5SIlpo Järvinen break; 3981832d11c5SIlpo Järvinen } 3982832d11c5SIlpo Järvinen } 3983832d11c5SIlpo Järvinen 3984832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3985832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3986832d11c5SIlpo Järvinen 3987832d11c5SIlpo Järvinen if (merge >= 0) { 3988832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3989832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3990832d11c5SIlpo Järvinen 39919e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 39926a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3993832d11c5SIlpo Järvinen } 3994832d11c5SIlpo Järvinen 3995832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3996832d11c5SIlpo Järvinen to = 0; 3997832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3998832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3999832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 4000832d11c5SIlpo Järvinen 4001832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 4002832d11c5SIlpo Järvinen 4003832d11c5SIlpo Järvinen onlymerged: 4004832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 4005832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 4006832d11c5SIlpo Järvinen */ 4007832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 4008832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 4009832d11c5SIlpo Järvinen 4010ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 4011ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 4012832d11c5SIlpo Järvinen 4013832d11c5SIlpo Järvinen return shiftlen; 4014832d11c5SIlpo Järvinen } 4015832d11c5SIlpo Järvinen 4016677e90edSThomas Graf /** 4017677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 4018677e90edSThomas Graf * @skb: the buffer to read 4019677e90edSThomas Graf * @from: lower offset of data to be read 4020677e90edSThomas Graf * @to: upper offset of data to be read 4021677e90edSThomas Graf * @st: state variable 4022677e90edSThomas Graf * 4023677e90edSThomas Graf * Initializes the specified state variable. Must be called before 4024677e90edSThomas Graf * invoking skb_seq_read() for the first time. 4025677e90edSThomas Graf */ 4026677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 4027677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 4028677e90edSThomas Graf { 4029677e90edSThomas Graf st->lower_offset = from; 4030677e90edSThomas Graf st->upper_offset = to; 4031677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 4032677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 4033677e90edSThomas Graf st->frag_data = NULL; 403497550f6fSWillem de Bruijn st->frag_off = 0; 4035677e90edSThomas Graf } 4036b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 4037677e90edSThomas Graf 4038677e90edSThomas Graf /** 4039677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 4040677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 4041677e90edSThomas Graf * @data: destination pointer for data to be returned 4042677e90edSThomas Graf * @st: state variable 4043677e90edSThomas Graf * 4044bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 4045677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 4046bc32383cSMathias Krause * the head of the data block to @data and returns the length 4047677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 4048677e90edSThomas Graf * offset has been reached. 4049677e90edSThomas Graf * 4050677e90edSThomas Graf * The caller is not required to consume all of the data 4051bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 4052677e90edSThomas Graf * of bytes already consumed and the next call to 4053677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 4054677e90edSThomas Graf * 405525985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 4056e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 4057677e90edSThomas Graf * reads of potentially non linear data. 4058677e90edSThomas Graf * 4059bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 4060677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 4061677e90edSThomas Graf * a stack for this purpose. 4062677e90edSThomas Graf */ 4063677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 4064677e90edSThomas Graf struct skb_seq_state *st) 4065677e90edSThomas Graf { 4066677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 4067677e90edSThomas Graf skb_frag_t *frag; 4068677e90edSThomas Graf 4069aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 4070aeb193eaSWedson Almeida Filho if (st->frag_data) { 4071aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 4072aeb193eaSWedson Almeida Filho st->frag_data = NULL; 4073aeb193eaSWedson Almeida Filho } 4074677e90edSThomas Graf return 0; 4075aeb193eaSWedson Almeida Filho } 4076677e90edSThomas Graf 4077677e90edSThomas Graf next_skb: 407895e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 4079677e90edSThomas Graf 4080995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 408195e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 4082677e90edSThomas Graf return block_limit - abs_offset; 4083677e90edSThomas Graf } 4084677e90edSThomas Graf 4085677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 4086677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 4087677e90edSThomas Graf 4088677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 408997550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 4090677e90edSThomas Graf 409197550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 409297550f6fSWillem de Bruijn 409397550f6fSWillem de Bruijn pg_idx = 0; 409497550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 409597550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 409697550f6fSWillem de Bruijn 409797550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 409897550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 409997550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 410097550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 410197550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 410297550f6fSWillem de Bruijn } 410397550f6fSWillem de Bruijn 410497550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 4105677e90edSThomas Graf if (abs_offset < block_limit) { 4106677e90edSThomas Graf if (!st->frag_data) 410797550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 4108677e90edSThomas Graf 410997550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 4110677e90edSThomas Graf (abs_offset - st->stepped_offset); 4111677e90edSThomas Graf 4112677e90edSThomas Graf return block_limit - abs_offset; 4113677e90edSThomas Graf } 4114677e90edSThomas Graf 4115677e90edSThomas Graf if (st->frag_data) { 411651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4117677e90edSThomas Graf st->frag_data = NULL; 4118677e90edSThomas Graf } 4119677e90edSThomas Graf 412097550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 412197550f6fSWillem de Bruijn st->frag_off += pg_sz; 412297550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 412397550f6fSWillem de Bruijn st->frag_off = 0; 4124677e90edSThomas Graf st->frag_idx++; 412597550f6fSWillem de Bruijn } 4126677e90edSThomas Graf } 4127677e90edSThomas Graf 41285b5a60daSOlaf Kirch if (st->frag_data) { 412951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 41305b5a60daSOlaf Kirch st->frag_data = NULL; 41315b5a60daSOlaf Kirch } 41325b5a60daSOlaf Kirch 413321dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4134677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 413595e3b24cSHerbert Xu st->frag_idx = 0; 4136677e90edSThomas Graf goto next_skb; 413771b3346dSShyam Iyer } else if (st->cur_skb->next) { 413871b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 413971b3346dSShyam Iyer st->frag_idx = 0; 4140677e90edSThomas Graf goto next_skb; 4141677e90edSThomas Graf } 4142677e90edSThomas Graf 4143677e90edSThomas Graf return 0; 4144677e90edSThomas Graf } 4145b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4146677e90edSThomas Graf 4147677e90edSThomas Graf /** 4148677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4149677e90edSThomas Graf * @st: state variable 4150677e90edSThomas Graf * 4151677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4152677e90edSThomas Graf * returned 0. 4153677e90edSThomas Graf */ 4154677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4155677e90edSThomas Graf { 4156677e90edSThomas Graf if (st->frag_data) 415751c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4158677e90edSThomas Graf } 4159b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4160677e90edSThomas Graf 41613fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 41623fc7e8a6SThomas Graf 41633fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 41643fc7e8a6SThomas Graf struct ts_config *conf, 41653fc7e8a6SThomas Graf struct ts_state *state) 41663fc7e8a6SThomas Graf { 41673fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 41683fc7e8a6SThomas Graf } 41693fc7e8a6SThomas Graf 41703fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 41713fc7e8a6SThomas Graf { 41723fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 41733fc7e8a6SThomas Graf } 41743fc7e8a6SThomas Graf 41753fc7e8a6SThomas Graf /** 41763fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 41773fc7e8a6SThomas Graf * @skb: the buffer to look in 41783fc7e8a6SThomas Graf * @from: search offset 41793fc7e8a6SThomas Graf * @to: search limit 41803fc7e8a6SThomas Graf * @config: textsearch configuration 41813fc7e8a6SThomas Graf * 41823fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 41833fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 41843fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 41853fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 41863fc7e8a6SThomas Graf */ 41873fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4188059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 41893fc7e8a6SThomas Graf { 4190059a2440SBojan Prtvar struct ts_state state; 4191f72b948dSPhil Oester unsigned int ret; 4192f72b948dSPhil Oester 4193b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4194b228c9b0SWillem de Bruijn 41953fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 41963fc7e8a6SThomas Graf config->finish = skb_ts_finish; 41973fc7e8a6SThomas Graf 4198059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 41993fc7e8a6SThomas Graf 4200059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4201f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 42023fc7e8a6SThomas Graf } 4203b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 42043fc7e8a6SThomas Graf 4205be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4206be12a1feSHannes Frederic Sowa int offset, size_t size) 4207be12a1feSHannes Frederic Sowa { 4208be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4209be12a1feSHannes Frederic Sowa 4210be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4211be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4212be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 4213753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4214be12a1feSHannes Frederic Sowa get_page(page); 4215228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4216be12a1feSHannes Frederic Sowa } else { 4217be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4218be12a1feSHannes Frederic Sowa } 4219be12a1feSHannes Frederic Sowa 4220be12a1feSHannes Frederic Sowa return 0; 4221be12a1feSHannes Frederic Sowa } 4222be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4223be12a1feSHannes Frederic Sowa 4224cbb042f9SHerbert Xu /** 4225cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4226cbb042f9SHerbert Xu * @skb: buffer to update 4227cbb042f9SHerbert Xu * @len: length of data pulled 4228cbb042f9SHerbert Xu * 4229cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4230fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 423184fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 423284fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 423384fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4234cbb042f9SHerbert Xu */ 4235af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4236cbb042f9SHerbert Xu { 423731b33dfbSPravin B Shelar unsigned char *data = skb->data; 423831b33dfbSPravin B Shelar 4239cbb042f9SHerbert Xu BUG_ON(len > skb->len); 424031b33dfbSPravin B Shelar __skb_pull(skb, len); 424131b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 424231b33dfbSPravin B Shelar return skb->data; 4243cbb042f9SHerbert Xu } 4244f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4245f94691acSArnaldo Carvalho de Melo 424613acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 424713acc94eSYonghong Song { 424813acc94eSYonghong Song skb_frag_t head_frag; 424913acc94eSYonghong Song struct page *page; 425013acc94eSYonghong Song 425113acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4252d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 4253b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 4254b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 4255d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 425613acc94eSYonghong Song return head_frag; 425713acc94eSYonghong Song } 425813acc94eSYonghong Song 42593a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 42603a1296a3SSteffen Klassert netdev_features_t features, 42613a1296a3SSteffen Klassert unsigned int offset) 42623a1296a3SSteffen Klassert { 42633a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 42643a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 42653a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 42663a1296a3SSteffen Klassert unsigned int delta_len = 0; 42673a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 426853475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4269cf3ab8d4SLina Wang int len_diff, err; 42703a1296a3SSteffen Klassert 42713a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 42723a1296a3SSteffen Klassert 42733a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 42743a1296a3SSteffen Klassert 4275876e8ca8SYan Zhai while (list_skb) { 42763a1296a3SSteffen Klassert nskb = list_skb; 42773a1296a3SSteffen Klassert list_skb = list_skb->next; 42783a1296a3SSteffen Klassert 427953475c5dSDongseok Yi err = 0; 4280224102deSlena wang delta_truesize += nskb->truesize; 428153475c5dSDongseok Yi if (skb_shared(nskb)) { 428253475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 428353475c5dSDongseok Yi if (tmp) { 428453475c5dSDongseok Yi consume_skb(nskb); 428553475c5dSDongseok Yi nskb = tmp; 428653475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 428753475c5dSDongseok Yi } else { 428853475c5dSDongseok Yi err = -ENOMEM; 428953475c5dSDongseok Yi } 429053475c5dSDongseok Yi } 429153475c5dSDongseok Yi 42923a1296a3SSteffen Klassert if (!tail) 42933a1296a3SSteffen Klassert skb->next = nskb; 42943a1296a3SSteffen Klassert else 42953a1296a3SSteffen Klassert tail->next = nskb; 42963a1296a3SSteffen Klassert 429753475c5dSDongseok Yi if (unlikely(err)) { 429853475c5dSDongseok Yi nskb->next = list_skb; 429953475c5dSDongseok Yi goto err_linearize; 430053475c5dSDongseok Yi } 430153475c5dSDongseok Yi 43023a1296a3SSteffen Klassert tail = nskb; 43033a1296a3SSteffen Klassert 43043a1296a3SSteffen Klassert delta_len += nskb->len; 43053a1296a3SSteffen Klassert 43063a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 43073a1296a3SSteffen Klassert 4308cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4309cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 43103a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 43113a1296a3SSteffen Klassert 43123a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4313cf3ab8d4SLina Wang nskb->transport_header += len_diff; 43143a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 43153a1296a3SSteffen Klassert nskb->data - tnl_hlen, 43163a1296a3SSteffen Klassert offset + tnl_hlen); 43173a1296a3SSteffen Klassert 43183a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 43193a1296a3SSteffen Klassert __skb_linearize(nskb)) 43203a1296a3SSteffen Klassert goto err_linearize; 4321876e8ca8SYan Zhai } 43223a1296a3SSteffen Klassert 43233a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 43243a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 43253a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 43263a1296a3SSteffen Klassert 43273a1296a3SSteffen Klassert skb_gso_reset(skb); 43283a1296a3SSteffen Klassert 43293a1296a3SSteffen Klassert skb->prev = tail; 43303a1296a3SSteffen Klassert 43313a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 43323a1296a3SSteffen Klassert __skb_linearize(skb)) 43333a1296a3SSteffen Klassert goto err_linearize; 43343a1296a3SSteffen Klassert 43353a1296a3SSteffen Klassert skb_get(skb); 43363a1296a3SSteffen Klassert 43373a1296a3SSteffen Klassert return skb; 43383a1296a3SSteffen Klassert 43393a1296a3SSteffen Klassert err_linearize: 43403a1296a3SSteffen Klassert kfree_skb_list(skb->next); 43413a1296a3SSteffen Klassert skb->next = NULL; 43423a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 43433a1296a3SSteffen Klassert } 43443a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 43453a1296a3SSteffen Klassert 4346f4c50d99SHerbert Xu /** 4347f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4348df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4349576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4350f4c50d99SHerbert Xu * 4351f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 43524c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 43534c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4354f4c50d99SHerbert Xu */ 4355df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4356df5771ffSMichael S. Tsirkin netdev_features_t features) 4357f4c50d99SHerbert Xu { 4358f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4359f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 43601a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4361df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4362df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4363df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 43641fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4365f4c50d99SHerbert Xu unsigned int offset = doffset; 4366df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4367802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4368f4c50d99SHerbert Xu unsigned int headroom; 4369802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4370ec5f0615SPravin B Shelar __be16 proto; 437136c98382SAlexander Duyck bool csum, sg; 4372df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4373f4c50d99SHerbert Xu int err = -ENOMEM; 4374f4c50d99SHerbert Xu int i = 0; 4375f4c50d99SHerbert Xu int pos; 4376f4c50d99SHerbert Xu 43779e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 43789e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 43799e4b7a99SJiri Benc struct sk_buff *check_skb; 43809e4b7a99SJiri Benc 43819e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 43829e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 43839e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 43849e4b7a99SJiri Benc * a linear non head_frag item. 43853dcbdb13SShmulik Ladkani * 43869e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 43879e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 43889e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 43899e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 43909e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 43913dcbdb13SShmulik Ladkani */ 43923dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 43939e4b7a99SJiri Benc break; 43949e4b7a99SJiri Benc } 43959e4b7a99SJiri Benc } 43963dcbdb13SShmulik Ladkani } 43973dcbdb13SShmulik Ladkani 43985882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 43992f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4400ec5f0615SPravin B Shelar if (unlikely(!proto)) 4401ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4402ec5f0615SPravin B Shelar 440336c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4404f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 44057e2b10c1STom Herbert 440607b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 440707b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 440807b26c94SSteffen Klassert struct sk_buff *iter; 440943170c4eSIlan Tayari unsigned int frag_len; 441007b26c94SSteffen Klassert 441107b26c94SSteffen Klassert if (!list_skb || 441207b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 441307b26c94SSteffen Klassert goto normal; 441407b26c94SSteffen Klassert 441543170c4eSIlan Tayari /* If we get here then all the required 441643170c4eSIlan Tayari * GSO features except frag_list are supported. 441743170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 441843170c4eSIlan Tayari * with no frag_list. 441943170c4eSIlan Tayari * Currently we can do that only when the buffers don't 442043170c4eSIlan Tayari * have a linear part and all the buffers except 442143170c4eSIlan Tayari * the last are of the same length. 442207b26c94SSteffen Klassert */ 442343170c4eSIlan Tayari frag_len = list_skb->len; 442407b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 442543170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 442643170c4eSIlan Tayari goto normal; 4427eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 442807b26c94SSteffen Klassert goto normal; 442907b26c94SSteffen Klassert 443007b26c94SSteffen Klassert len -= iter->len; 443107b26c94SSteffen Klassert } 443243170c4eSIlan Tayari 443343170c4eSIlan Tayari if (len != frag_len) 443443170c4eSIlan Tayari goto normal; 443507b26c94SSteffen Klassert } 443607b26c94SSteffen Klassert 4437802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4438802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4439802ab55aSAlexander Duyck * now. 4440802ab55aSAlexander Duyck */ 4441802ab55aSAlexander Duyck partial_segs = len / mss; 4442d7fb5a80SAlexander Duyck if (partial_segs > 1) 4443802ab55aSAlexander Duyck mss *= partial_segs; 4444d7fb5a80SAlexander Duyck else 4445d7fb5a80SAlexander Duyck partial_segs = 0; 4446802ab55aSAlexander Duyck } 4447802ab55aSAlexander Duyck 444807b26c94SSteffen Klassert normal: 4449df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4450df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4451f4c50d99SHerbert Xu 4452f4c50d99SHerbert Xu do { 4453f4c50d99SHerbert Xu struct sk_buff *nskb; 44548cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4455c8884eddSHerbert Xu int hsize; 4456f4c50d99SHerbert Xu int size; 4457f4c50d99SHerbert Xu 44583953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 44593953c46cSMarcelo Ricardo Leitner len = list_skb->len; 44603953c46cSMarcelo Ricardo Leitner } else { 4461df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4462f4c50d99SHerbert Xu if (len > mss) 4463f4c50d99SHerbert Xu len = mss; 44643953c46cSMarcelo Ricardo Leitner } 4465f4c50d99SHerbert Xu 4466df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4467f4c50d99SHerbert Xu 4468dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 44691a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 44701a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 447189319d38SHerbert Xu 44729d8506ccSHerbert Xu i = 0; 44731a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44741a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44751fd819ecSMichael S. Tsirkin frag_skb = list_skb; 44761a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 44779d8506ccSHerbert Xu 44789d8506ccSHerbert Xu while (pos < offset + len) { 44799d8506ccSHerbert Xu BUG_ON(i >= nfrags); 44809d8506ccSHerbert Xu 44814e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 44829d8506ccSHerbert Xu if (pos + size > offset + len) 44839d8506ccSHerbert Xu break; 44849d8506ccSHerbert Xu 44859d8506ccSHerbert Xu i++; 44869d8506ccSHerbert Xu pos += size; 44874e1beba1SMichael S. Tsirkin frag++; 44889d8506ccSHerbert Xu } 44899d8506ccSHerbert Xu 44901a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 44911a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 449289319d38SHerbert Xu 4493f4c50d99SHerbert Xu if (unlikely(!nskb)) 4494f4c50d99SHerbert Xu goto err; 4495f4c50d99SHerbert Xu 44969d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 44979d8506ccSHerbert Xu kfree_skb(nskb); 44989d8506ccSHerbert Xu goto err; 44999d8506ccSHerbert Xu } 45009d8506ccSHerbert Xu 4501ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 450289319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 450389319d38SHerbert Xu kfree_skb(nskb); 450489319d38SHerbert Xu goto err; 450589319d38SHerbert Xu } 450689319d38SHerbert Xu 4507ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 450889319d38SHerbert Xu skb_release_head_state(nskb); 450989319d38SHerbert Xu __skb_push(nskb, doffset); 451089319d38SHerbert Xu } else { 451100b229f7SPaolo Abeni if (hsize < 0) 451200b229f7SPaolo Abeni hsize = 0; 4513dbd50f23SXin Long if (hsize > len || !sg) 4514dbd50f23SXin Long hsize = len; 4515dbd50f23SXin Long 4516c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4517df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4518c93bdd0eSMel Gorman NUMA_NO_NODE); 451989319d38SHerbert Xu 452089319d38SHerbert Xu if (unlikely(!nskb)) 452189319d38SHerbert Xu goto err; 452289319d38SHerbert Xu 452389319d38SHerbert Xu skb_reserve(nskb, headroom); 452489319d38SHerbert Xu __skb_put(nskb, doffset); 452589319d38SHerbert Xu } 452689319d38SHerbert Xu 4527f4c50d99SHerbert Xu if (segs) 4528f4c50d99SHerbert Xu tail->next = nskb; 4529f4c50d99SHerbert Xu else 4530f4c50d99SHerbert Xu segs = nskb; 4531f4c50d99SHerbert Xu tail = nskb; 4532f4c50d99SHerbert Xu 4533df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4534f4c50d99SHerbert Xu 4535030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4536fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 453768c33163SPravin B Shelar 4538df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 453968c33163SPravin B Shelar nskb->data - tnl_hlen, 454068c33163SPravin B Shelar doffset + tnl_hlen); 454189319d38SHerbert Xu 45429d8506ccSHerbert Xu if (nskb->len == len + doffset) 45431cdbcb79SSimon Horman goto perform_csum_check; 454489319d38SHerbert Xu 45457fbeffedSAlexander Duyck if (!sg) { 45461454c9faSYadu Kishore if (!csum) { 45477fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 45486f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 454976443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 455076443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 45511454c9faSYadu Kishore skb_put(nskb, 45521454c9faSYadu Kishore len), 45538d5930dfSAl Viro len); 45547e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4555de843723STom Herbert skb_headroom(nskb) + doffset; 45561454c9faSYadu Kishore } else { 4557c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4558c624c58eSlily goto err; 45591454c9faSYadu Kishore } 4560f4c50d99SHerbert Xu continue; 4561f4c50d99SHerbert Xu } 4562f4c50d99SHerbert Xu 45638cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4564f4c50d99SHerbert Xu 4565df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4566d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4567f4c50d99SHerbert Xu 456806b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 456906b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4570cef401deSEric Dumazet 4571bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4572bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4573bf5c25d6SWillem de Bruijn goto err; 4574bf5c25d6SWillem de Bruijn 45759d8506ccSHerbert Xu while (pos < offset + len) { 45769d8506ccSHerbert Xu if (i >= nfrags) { 45779d8506ccSHerbert Xu i = 0; 45781a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 45791a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 45801fd819ecSMichael S. Tsirkin frag_skb = list_skb; 458113acc94eSYonghong Song if (!skb_headlen(list_skb)) { 45829d8506ccSHerbert Xu BUG_ON(!nfrags); 458313acc94eSYonghong Song } else { 458413acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 45859d8506ccSHerbert Xu 458613acc94eSYonghong Song /* to make room for head_frag. */ 458713acc94eSYonghong Song i--; 458813acc94eSYonghong Song frag--; 458913acc94eSYonghong Song } 4590bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4591bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4592bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4593bf5c25d6SWillem de Bruijn goto err; 4594bf5c25d6SWillem de Bruijn 45951a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 45969d8506ccSHerbert Xu } 45979d8506ccSHerbert Xu 45989d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 45999d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 46009d8506ccSHerbert Xu net_warn_ratelimited( 46019d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 46029d8506ccSHerbert Xu pos, mss); 4603ff907a11SEric Dumazet err = -EINVAL; 46049d8506ccSHerbert Xu goto err; 46059d8506ccSHerbert Xu } 46069d8506ccSHerbert Xu 460713acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 46088cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 46098cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4610f4c50d99SHerbert Xu 4611f4c50d99SHerbert Xu if (pos < offset) { 4612b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 46138cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4614f4c50d99SHerbert Xu } 4615f4c50d99SHerbert Xu 461689319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4617f4c50d99SHerbert Xu 4618f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4619f4c50d99SHerbert Xu i++; 46204e1beba1SMichael S. Tsirkin frag++; 4621f4c50d99SHerbert Xu pos += size; 4622f4c50d99SHerbert Xu } else { 46238cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 462489319d38SHerbert Xu goto skip_fraglist; 4625f4c50d99SHerbert Xu } 4626f4c50d99SHerbert Xu 46278cb19905SMichael S. Tsirkin nskb_frag++; 4628f4c50d99SHerbert Xu } 4629f4c50d99SHerbert Xu 463089319d38SHerbert Xu skip_fraglist: 4631f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4632f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4633f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4634ec5f0615SPravin B Shelar 46351cdbcb79SSimon Horman perform_csum_check: 46367fbeffedSAlexander Duyck if (!csum) { 4637ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4638ff907a11SEric Dumazet __skb_linearize(nskb)) 4639ddff00d4SAlexander Duyck goto err; 4640ff907a11SEric Dumazet 46417fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4642ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 464376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 464476443456SAlexander Duyck skb_checksum(nskb, doffset, 464576443456SAlexander Duyck nskb->len - doffset, 0); 46467e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 46477e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4648ec5f0615SPravin B Shelar } 4649df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4650f4c50d99SHerbert Xu 4651bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4652bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4653bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4654bec3cfdcSEric Dumazet */ 4655bec3cfdcSEric Dumazet segs->prev = tail; 4656432c856fSToshiaki Makita 4657802ab55aSAlexander Duyck if (partial_segs) { 465807b26c94SSteffen Klassert struct sk_buff *iter; 4659802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 466007b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4661802ab55aSAlexander Duyck 4662802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 466307b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4664802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4665802ab55aSAlexander Duyck 4666802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4667802ab55aSAlexander Duyck * our way back down the stack of protocols. 4668802ab55aSAlexander Duyck */ 466907b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 467007b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 467107b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 467207b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 467307b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 467407b26c94SSteffen Klassert } 467507b26c94SSteffen Klassert 467607b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 467707b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 467807b26c94SSteffen Klassert else if (tail != segs) 467907b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4680802ab55aSAlexander Duyck } 4681802ab55aSAlexander Duyck 4682432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4683432c856fSToshiaki Makita * using skb_set_owner_w(). 4684432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4685432c856fSToshiaki Makita */ 4686432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4687432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4688432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4689432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4690432c856fSToshiaki Makita } 4691f4c50d99SHerbert Xu return segs; 4692f4c50d99SHerbert Xu 4693f4c50d99SHerbert Xu err: 4694289dccbeSEric Dumazet kfree_skb_list(segs); 4695f4c50d99SHerbert Xu return ERR_PTR(err); 4696f4c50d99SHerbert Xu } 4697f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4698f4c50d99SHerbert Xu 4699df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4700df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4701df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4702df5042f4SFlorian Westphal 4703df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4704df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4705df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4706df5042f4SFlorian Westphal #endif 47074165079bSFlorian Westphal #ifdef CONFIG_XFRM 47084165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 47094165079bSFlorian Westphal #endif 471095a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 471195a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 471295a7233cSPaul Blakey #endif 47133ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47143ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 47153ee17bc7SMat Martineau #endif 471678476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 471778476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 471878476d31SJeremy Kerr #endif 4719df5042f4SFlorian Westphal }; 4720df5042f4SFlorian Westphal 4721df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4722df5042f4SFlorian Westphal { 4723df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4724df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4725df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4726df5042f4SFlorian Westphal #endif 47274165079bSFlorian Westphal #ifdef CONFIG_XFRM 47284165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 47294165079bSFlorian Westphal #endif 473095a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 473195a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 473295a7233cSPaul Blakey #endif 47333ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47343ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 47353ee17bc7SMat Martineau #endif 473678476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 473778476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 473878476d31SJeremy Kerr #endif 4739df5042f4SFlorian Westphal 0; 4740df5042f4SFlorian Westphal } 4741df5042f4SFlorian Westphal 4742df5042f4SFlorian Westphal static void skb_extensions_init(void) 4743df5042f4SFlorian Westphal { 4744df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4745df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4746df5042f4SFlorian Westphal 4747df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4748df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4749df5042f4SFlorian Westphal 0, 4750df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4751df5042f4SFlorian Westphal NULL); 4752df5042f4SFlorian Westphal } 4753df5042f4SFlorian Westphal #else 4754df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4755df5042f4SFlorian Westphal #endif 4756df5042f4SFlorian Westphal 47571da177e4SLinus Torvalds void __init skb_init(void) 47581da177e4SLinus Torvalds { 4759025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", 47601da177e4SLinus Torvalds sizeof(struct sk_buff), 47611da177e4SLinus Torvalds 0, 4762e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 476379a8a642SKees Cook offsetof(struct sk_buff, cb), 476479a8a642SKees Cook sizeof_field(struct sk_buff, cb), 476520c2df83SPaul Mundt NULL); 4766d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4767d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4768d179cd12SDavid S. Miller 0, 4769e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 477020c2df83SPaul Mundt NULL); 4771bf9f1baaSEric Dumazet #ifdef HAVE_SKB_SMALL_HEAD_CACHE 47720b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. 47730b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head, 47740b34d680SEric Dumazet * and should not be copied to/from user. 47750b34d680SEric Dumazet */ 47760b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", 4777bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE, 4778bf9f1baaSEric Dumazet 0, 4779bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC, 47800b34d680SEric Dumazet 0, 47810b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM, 4782bf9f1baaSEric Dumazet NULL); 4783bf9f1baaSEric Dumazet #endif 4784df5042f4SFlorian Westphal skb_extensions_init(); 47851da177e4SLinus Torvalds } 47861da177e4SLinus Torvalds 478751c739d1SDavid S. Miller static int 478848a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 478948a1df65SJason A. Donenfeld unsigned int recursion_level) 4790716ea3a7SDavid Howells { 47911a028e50SDavid S. Miller int start = skb_headlen(skb); 47921a028e50SDavid S. Miller int i, copy = start - offset; 4793fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4794716ea3a7SDavid Howells int elt = 0; 4795716ea3a7SDavid Howells 479648a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 479748a1df65SJason A. Donenfeld return -EMSGSIZE; 479848a1df65SJason A. Donenfeld 4799716ea3a7SDavid Howells if (copy > 0) { 4800716ea3a7SDavid Howells if (copy > len) 4801716ea3a7SDavid Howells copy = len; 4802642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4803716ea3a7SDavid Howells elt++; 4804716ea3a7SDavid Howells if ((len -= copy) == 0) 4805716ea3a7SDavid Howells return elt; 4806716ea3a7SDavid Howells offset += copy; 4807716ea3a7SDavid Howells } 4808716ea3a7SDavid Howells 4809716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 48101a028e50SDavid S. Miller int end; 4811716ea3a7SDavid Howells 4812547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48131a028e50SDavid S. Miller 48149e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4815716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4816716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 481748a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 481848a1df65SJason A. Donenfeld return -EMSGSIZE; 4819716ea3a7SDavid Howells 4820716ea3a7SDavid Howells if (copy > len) 4821716ea3a7SDavid Howells copy = len; 4822ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4823b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4824716ea3a7SDavid Howells elt++; 4825716ea3a7SDavid Howells if (!(len -= copy)) 4826716ea3a7SDavid Howells return elt; 4827716ea3a7SDavid Howells offset += copy; 4828716ea3a7SDavid Howells } 48291a028e50SDavid S. Miller start = end; 4830716ea3a7SDavid Howells } 4831716ea3a7SDavid Howells 4832fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 483348a1df65SJason A. Donenfeld int end, ret; 4834716ea3a7SDavid Howells 4835547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48361a028e50SDavid S. Miller 4837fbb398a8SDavid S. Miller end = start + frag_iter->len; 4838716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 483948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 484048a1df65SJason A. Donenfeld return -EMSGSIZE; 484148a1df65SJason A. Donenfeld 4842716ea3a7SDavid Howells if (copy > len) 4843716ea3a7SDavid Howells copy = len; 484448a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 484548a1df65SJason A. Donenfeld copy, recursion_level + 1); 484648a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 484748a1df65SJason A. Donenfeld return ret; 484848a1df65SJason A. Donenfeld elt += ret; 4849716ea3a7SDavid Howells if ((len -= copy) == 0) 4850716ea3a7SDavid Howells return elt; 4851716ea3a7SDavid Howells offset += copy; 4852716ea3a7SDavid Howells } 48531a028e50SDavid S. Miller start = end; 4854716ea3a7SDavid Howells } 4855716ea3a7SDavid Howells BUG_ON(len); 4856716ea3a7SDavid Howells return elt; 4857716ea3a7SDavid Howells } 4858716ea3a7SDavid Howells 485948a1df65SJason A. Donenfeld /** 486048a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 486148a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 486248a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 486348a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 486448a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 486548a1df65SJason A. Donenfeld * 486648a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 486748a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 486848a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 486948a1df65SJason A. Donenfeld * could not fit. 487048a1df65SJason A. Donenfeld */ 487148a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 487248a1df65SJason A. Donenfeld { 487348a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 487448a1df65SJason A. Donenfeld 487548a1df65SJason A. Donenfeld if (nsg <= 0) 487648a1df65SJason A. Donenfeld return nsg; 487748a1df65SJason A. Donenfeld 487848a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 487948a1df65SJason A. Donenfeld 488048a1df65SJason A. Donenfeld return nsg; 488148a1df65SJason A. Donenfeld } 488248a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 488348a1df65SJason A. Donenfeld 488425a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 488525a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 488625a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 488725a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 488825a91d8dSFan Du * 488925a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 489025a91d8dSFan Du * 1. sg_init_table 489125a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 489225a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 489325a91d8dSFan Du * 489425a91d8dSFan Du * This is equivalent to: 489525a91d8dSFan Du * 1. sg_init_table 489625a91d8dSFan Du * 2. skb_to_sgvec(payload1) 489725a91d8dSFan Du * 3. sg_unmark_end 489825a91d8dSFan Du * 4. skb_to_sgvec(payload2) 489925a91d8dSFan Du * 490025a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 490125a91d8dSFan Du * is more preferable. 490225a91d8dSFan Du */ 490325a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 490425a91d8dSFan Du int offset, int len) 490525a91d8dSFan Du { 490648a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 490725a91d8dSFan Du } 490825a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 490925a91d8dSFan Du 491051c739d1SDavid S. Miller 491151c739d1SDavid S. Miller 4912716ea3a7SDavid Howells /** 4913716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4914716ea3a7SDavid Howells * @skb: The socket buffer to check. 4915716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4916716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4917716ea3a7SDavid Howells * 4918716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4919716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4920716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4921716ea3a7SDavid Howells * 4922716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4923716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4924716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4925716ea3a7SDavid Howells * 4926716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4927716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4928716ea3a7SDavid Howells */ 4929716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4930716ea3a7SDavid Howells { 4931716ea3a7SDavid Howells int copyflag; 4932716ea3a7SDavid Howells int elt; 4933716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4934716ea3a7SDavid Howells 4935716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4936716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4937716ea3a7SDavid Howells * at the moment even if they are anonymous). 4938716ea3a7SDavid Howells */ 4939716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4940c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4941716ea3a7SDavid Howells return -ENOMEM; 4942716ea3a7SDavid Howells 4943716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 494421dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4945716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4946716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4947716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4948716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4949716ea3a7SDavid Howells 4950716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4951716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4952716ea3a7SDavid Howells return -ENOMEM; 4953716ea3a7SDavid Howells 4954716ea3a7SDavid Howells /* Voila! */ 4955716ea3a7SDavid Howells *trailer = skb; 4956716ea3a7SDavid Howells return 1; 4957716ea3a7SDavid Howells } 4958716ea3a7SDavid Howells 4959716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4960716ea3a7SDavid Howells 4961716ea3a7SDavid Howells elt = 1; 4962716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4963716ea3a7SDavid Howells copyflag = 0; 4964716ea3a7SDavid Howells 4965716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4966716ea3a7SDavid Howells int ntail = 0; 4967716ea3a7SDavid Howells 4968716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4969716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4970716ea3a7SDavid Howells * after it. */ 4971716ea3a7SDavid Howells 4972716ea3a7SDavid Howells if (skb_shared(skb1)) 4973716ea3a7SDavid Howells copyflag = 1; 4974716ea3a7SDavid Howells 4975716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4976716ea3a7SDavid Howells 4977716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4978716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 497921dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4980716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4981716ea3a7SDavid Howells ntail = tailbits + 128; 4982716ea3a7SDavid Howells } 4983716ea3a7SDavid Howells 4984716ea3a7SDavid Howells if (copyflag || 4985716ea3a7SDavid Howells skb_cloned(skb1) || 4986716ea3a7SDavid Howells ntail || 4987716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 498821dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4989716ea3a7SDavid Howells struct sk_buff *skb2; 4990716ea3a7SDavid Howells 4991716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4992716ea3a7SDavid Howells if (ntail == 0) 4993716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4994716ea3a7SDavid Howells else 4995716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4996716ea3a7SDavid Howells skb_headroom(skb1), 4997716ea3a7SDavid Howells ntail, 4998716ea3a7SDavid Howells GFP_ATOMIC); 4999716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 5000716ea3a7SDavid Howells return -ENOMEM; 5001716ea3a7SDavid Howells 5002716ea3a7SDavid Howells if (skb1->sk) 5003716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 5004716ea3a7SDavid Howells 5005716ea3a7SDavid Howells /* Looking around. Are we still alive? 5006716ea3a7SDavid Howells * OK, link new skb, drop old one */ 5007716ea3a7SDavid Howells 5008716ea3a7SDavid Howells skb2->next = skb1->next; 5009716ea3a7SDavid Howells *skb_p = skb2; 5010716ea3a7SDavid Howells kfree_skb(skb1); 5011716ea3a7SDavid Howells skb1 = skb2; 5012716ea3a7SDavid Howells } 5013716ea3a7SDavid Howells elt++; 5014716ea3a7SDavid Howells *trailer = skb1; 5015716ea3a7SDavid Howells skb_p = &skb1->next; 5016716ea3a7SDavid Howells } 5017716ea3a7SDavid Howells 5018716ea3a7SDavid Howells return elt; 5019716ea3a7SDavid Howells } 5020b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 5021716ea3a7SDavid Howells 5022b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 5023b1faf566SEric Dumazet { 5024b1faf566SEric Dumazet struct sock *sk = skb->sk; 5025b1faf566SEric Dumazet 5026b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 5027b1faf566SEric Dumazet } 5028b1faf566SEric Dumazet 50298605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 50308605330aSSoheil Hassas Yeganeh { 50318605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 50328605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 50338605330aSSoheil Hassas Yeganeh */ 50348605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 50358605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 50368605330aSSoheil Hassas Yeganeh } 50378605330aSSoheil Hassas Yeganeh 5038b1faf566SEric Dumazet /* 5039b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 5040b1faf566SEric Dumazet */ 5041b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 5042b1faf566SEric Dumazet { 5043b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 5044ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 5045b1faf566SEric Dumazet return -ENOMEM; 5046b1faf566SEric Dumazet 5047b1faf566SEric Dumazet skb_orphan(skb); 5048b1faf566SEric Dumazet skb->sk = sk; 5049b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 5050b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 50518605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 5052b1faf566SEric Dumazet 5053abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 5054abb57ea4SEric Dumazet skb_dst_force(skb); 5055abb57ea4SEric Dumazet 5056b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 5057b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 5058e3ae2365SAlexander Aring sk_error_report(sk); 5059b1faf566SEric Dumazet return 0; 5060b1faf566SEric Dumazet } 5061b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 5062b1faf566SEric Dumazet 506383a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 506483a1a1a7SSoheil Hassas Yeganeh { 506583a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 506683a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 506783a1a1a7SSoheil Hassas Yeganeh } 506883a1a1a7SSoheil Hassas Yeganeh 5069364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 5070364a9e93SWillem de Bruijn { 5071364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 507283a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 507383a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 5074997d5c3fSEric Dumazet unsigned long flags; 5075364a9e93SWillem de Bruijn 5076997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 5077364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 507838b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 507983a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 508038b25793SSoheil Hassas Yeganeh if (icmp_next) 5081985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 508238b25793SSoheil Hassas Yeganeh } 5083997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 5084364a9e93SWillem de Bruijn 508583a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 508683a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 508783a1a1a7SSoheil Hassas Yeganeh 508883a1a1a7SSoheil Hassas Yeganeh if (skb_next) 5089e3ae2365SAlexander Aring sk_error_report(sk); 5090364a9e93SWillem de Bruijn 5091364a9e93SWillem de Bruijn return skb; 5092364a9e93SWillem de Bruijn } 5093364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 5094364a9e93SWillem de Bruijn 5095cab41c47SAlexander Duyck /** 5096cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 5097cab41c47SAlexander Duyck * @skb: the skb to clone 5098cab41c47SAlexander Duyck * 5099cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 5100cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 5101cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 5102cab41c47SAlexander Duyck * 5103cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 5104cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 5105cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 5106cab41c47SAlexander Duyck * the sk_error_queue. 5107cab41c47SAlexander Duyck */ 510862bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 510962bccb8cSAlexander Duyck { 511062bccb8cSAlexander Duyck struct sock *sk = skb->sk; 511162bccb8cSAlexander Duyck struct sk_buff *clone; 511262bccb8cSAlexander Duyck 511341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 511462bccb8cSAlexander Duyck return NULL; 511562bccb8cSAlexander Duyck 511662bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 511762bccb8cSAlexander Duyck if (!clone) { 511862bccb8cSAlexander Duyck sock_put(sk); 511962bccb8cSAlexander Duyck return NULL; 512062bccb8cSAlexander Duyck } 512162bccb8cSAlexander Duyck 512262bccb8cSAlexander Duyck clone->sk = sk; 512362bccb8cSAlexander Duyck clone->destructor = sock_efree; 512462bccb8cSAlexander Duyck 512562bccb8cSAlexander Duyck return clone; 512662bccb8cSAlexander Duyck } 512762bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 512862bccb8cSAlexander Duyck 512937846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 513037846ef0SAlexander Duyck struct sock *sk, 51314ef1b286SSoheil Hassas Yeganeh int tstype, 51324ef1b286SSoheil Hassas Yeganeh bool opt_stats) 5133ac45f602SPatrick Ohly { 5134ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 5135ac45f602SPatrick Ohly int err; 5136ac45f602SPatrick Ohly 51374ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 51384ef1b286SSoheil Hassas Yeganeh 5139ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5140ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5141ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5142ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5143e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 51444ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 51451862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 51464ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 514709c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 514842f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5149a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 51504ed2d765SWillem de Bruijn } 515129030374SEric Dumazet 5152ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 515329030374SEric Dumazet 5154ac45f602SPatrick Ohly if (err) 5155ac45f602SPatrick Ohly kfree_skb(skb); 5156ac45f602SPatrick Ohly } 515737846ef0SAlexander Duyck 5158b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5159b245be1fSWillem de Bruijn { 5160b245be1fSWillem de Bruijn bool ret; 5161b245be1fSWillem de Bruijn 5162d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5163b245be1fSWillem de Bruijn return true; 5164b245be1fSWillem de Bruijn 5165b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5166b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5167b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5168b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5169b245be1fSWillem de Bruijn return ret; 5170b245be1fSWillem de Bruijn } 5171b245be1fSWillem de Bruijn 517237846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 517337846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 517437846ef0SAlexander Duyck { 517537846ef0SAlexander Duyck struct sock *sk = skb->sk; 517637846ef0SAlexander Duyck 5177b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 517835b99dffSWillem de Bruijn goto err; 5179b245be1fSWillem de Bruijn 51809ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 51819ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 51829ac25fc0SEric Dumazet */ 518341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 518437846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 51854ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 518637846ef0SAlexander Duyck sock_put(sk); 518735b99dffSWillem de Bruijn return; 518837846ef0SAlexander Duyck } 518935b99dffSWillem de Bruijn 519035b99dffSWillem de Bruijn err: 519135b99dffSWillem de Bruijn kfree_skb(skb); 51929ac25fc0SEric Dumazet } 519337846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 519437846ef0SAlexander Duyck 519537846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5196e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 519737846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 519837846ef0SAlexander Duyck struct sock *sk, int tstype) 519937846ef0SAlexander Duyck { 520037846ef0SAlexander Duyck struct sk_buff *skb; 52014ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 520237846ef0SAlexander Duyck 52033a8dd971SWillem de Bruijn if (!sk) 52043a8dd971SWillem de Bruijn return; 52053a8dd971SWillem de Bruijn 5206b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5207b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5208b50a5c70SMiroslav Lichvar return; 5209b50a5c70SMiroslav Lichvar 52103a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 52113a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 521237846ef0SAlexander Duyck return; 521337846ef0SAlexander Duyck 52141c885808SFrancis Yan if (tsonly) { 52151c885808SFrancis Yan #ifdef CONFIG_INET 52161c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 521742f67eeaSEric Dumazet sk_is_tcp(sk)) { 5218e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5219e7ed11eeSYousuk Seung ack_skb); 52204ef1b286SSoheil Hassas Yeganeh opt_stats = true; 52214ef1b286SSoheil Hassas Yeganeh } else 52221c885808SFrancis Yan #endif 52231c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 52241c885808SFrancis Yan } else { 522537846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 522650749f2dSKuniyuki Iwashima 522750749f2dSKuniyuki Iwashima if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) 522850749f2dSKuniyuki Iwashima return; 52291c885808SFrancis Yan } 523037846ef0SAlexander Duyck if (!skb) 523137846ef0SAlexander Duyck return; 523237846ef0SAlexander Duyck 523349ca0d8bSWillem de Bruijn if (tsonly) { 5234fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5235fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 523649ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 523749ca0d8bSWillem de Bruijn } 523849ca0d8bSWillem de Bruijn 523949ca0d8bSWillem de Bruijn if (hwtstamps) 524049ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 524149ca0d8bSWillem de Bruijn else 5242d93376f5SMartin KaFai Lau __net_timestamp(skb); 524349ca0d8bSWillem de Bruijn 52444ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 524537846ef0SAlexander Duyck } 5246e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5247e7fd2885SWillem de Bruijn 5248e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5249e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5250e7fd2885SWillem de Bruijn { 5251e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5252e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5253e7fd2885SWillem de Bruijn } 5254ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5255ac45f602SPatrick Ohly 5256eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS 52576e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 52586e3e939fSJohannes Berg { 52596e3e939fSJohannes Berg struct sock *sk = skb->sk; 52606e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5261dd4f1072SEric Dumazet int err = 1; 52626e3e939fSJohannes Berg 52636e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 52646e3e939fSJohannes Berg skb->wifi_acked = acked; 52656e3e939fSJohannes Berg 52666e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 52676e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 52686e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 52696e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 52706e3e939fSJohannes Berg 5271dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5272dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5273dd4f1072SEric Dumazet */ 527441c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 52756e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5276dd4f1072SEric Dumazet sock_put(sk); 5277dd4f1072SEric Dumazet } 52786e3e939fSJohannes Berg if (err) 52796e3e939fSJohannes Berg kfree_skb(skb); 52806e3e939fSJohannes Berg } 52816e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 5282eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */ 52836e3e939fSJohannes Berg 5284f35d9d8aSRusty Russell /** 5285f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5286f35d9d8aSRusty Russell * @skb: the skb to set 5287f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5288f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5289f35d9d8aSRusty Russell * 5290f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5291f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5292f35d9d8aSRusty Russell * 5293f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5294f35d9d8aSRusty Russell * returns false you should drop the packet. 5295f35d9d8aSRusty Russell */ 5296f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5297f35d9d8aSRusty Russell { 529852b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 529952b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 530052b5d6f5SEric Dumazet 530152b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 530252b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 530352b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5304f35d9d8aSRusty Russell return false; 5305f35d9d8aSRusty Russell } 5306f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 530752b5d6f5SEric Dumazet skb->csum_start = csum_start; 5308f35d9d8aSRusty Russell skb->csum_offset = off; 5309e5d5decaSJason Wang skb_set_transport_header(skb, start); 5310f35d9d8aSRusty Russell return true; 5311f35d9d8aSRusty Russell } 5312b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5313f35d9d8aSRusty Russell 5314ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5315ed1f50c3SPaul Durrant unsigned int max) 5316ed1f50c3SPaul Durrant { 5317ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5318ed1f50c3SPaul Durrant return 0; 5319ed1f50c3SPaul Durrant 5320ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5321ed1f50c3SPaul Durrant * won't need to do it again. 5322ed1f50c3SPaul Durrant */ 5323ed1f50c3SPaul Durrant if (max > skb->len) 5324ed1f50c3SPaul Durrant max = skb->len; 5325ed1f50c3SPaul Durrant 5326ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5327ed1f50c3SPaul Durrant return -ENOMEM; 5328ed1f50c3SPaul Durrant 5329ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5330ed1f50c3SPaul Durrant return -EPROTO; 5331ed1f50c3SPaul Durrant 5332ed1f50c3SPaul Durrant return 0; 5333ed1f50c3SPaul Durrant } 5334ed1f50c3SPaul Durrant 5335f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5336f9708b43SJan Beulich 5337f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5338f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5339f9708b43SJan Beulich unsigned int off) 5340f9708b43SJan Beulich { 5341f9708b43SJan Beulich int err; 5342f9708b43SJan Beulich 5343161d1792SKees Cook switch (proto) { 5344f9708b43SJan Beulich case IPPROTO_TCP: 5345f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5346f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5347f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5348f9708b43SJan Beulich offsetof(struct tcphdr, 5349f9708b43SJan Beulich check))) 5350f9708b43SJan Beulich err = -EPROTO; 5351f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5352f9708b43SJan Beulich 5353f9708b43SJan Beulich case IPPROTO_UDP: 5354f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5355f9708b43SJan Beulich off + sizeof(struct udphdr)); 5356f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5357f9708b43SJan Beulich offsetof(struct udphdr, 5358f9708b43SJan Beulich check))) 5359f9708b43SJan Beulich err = -EPROTO; 5360f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5361f9708b43SJan Beulich } 5362f9708b43SJan Beulich 5363f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5364f9708b43SJan Beulich } 5365f9708b43SJan Beulich 5366ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5367ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5368ed1f50c3SPaul Durrant */ 5369ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5370ed1f50c3SPaul Durrant 5371f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5372ed1f50c3SPaul Durrant { 5373ed1f50c3SPaul Durrant unsigned int off; 5374ed1f50c3SPaul Durrant bool fragment; 5375f9708b43SJan Beulich __sum16 *csum; 5376ed1f50c3SPaul Durrant int err; 5377ed1f50c3SPaul Durrant 5378ed1f50c3SPaul Durrant fragment = false; 5379ed1f50c3SPaul Durrant 5380ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5381ed1f50c3SPaul Durrant sizeof(struct iphdr), 5382ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5383ed1f50c3SPaul Durrant if (err < 0) 5384ed1f50c3SPaul Durrant goto out; 5385ed1f50c3SPaul Durrant 538611f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5387ed1f50c3SPaul Durrant fragment = true; 5388ed1f50c3SPaul Durrant 5389ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5390ed1f50c3SPaul Durrant 5391ed1f50c3SPaul Durrant err = -EPROTO; 5392ed1f50c3SPaul Durrant 5393ed1f50c3SPaul Durrant if (fragment) 5394ed1f50c3SPaul Durrant goto out; 5395ed1f50c3SPaul Durrant 5396f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5397f9708b43SJan Beulich if (IS_ERR(csum)) 5398f9708b43SJan Beulich return PTR_ERR(csum); 5399ed1f50c3SPaul Durrant 5400ed1f50c3SPaul Durrant if (recalculate) 5401f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5402ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5403ed1f50c3SPaul Durrant skb->len - off, 5404f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5405ed1f50c3SPaul Durrant err = 0; 5406ed1f50c3SPaul Durrant 5407ed1f50c3SPaul Durrant out: 5408ed1f50c3SPaul Durrant return err; 5409ed1f50c3SPaul Durrant } 5410ed1f50c3SPaul Durrant 5411ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5412ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5413ed1f50c3SPaul Durrant */ 5414ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5415ed1f50c3SPaul Durrant 5416ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5417ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5418ed1f50c3SPaul Durrant 5419ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5420ed1f50c3SPaul Durrant { 5421ed1f50c3SPaul Durrant int err; 5422ed1f50c3SPaul Durrant u8 nexthdr; 5423ed1f50c3SPaul Durrant unsigned int off; 5424ed1f50c3SPaul Durrant unsigned int len; 5425ed1f50c3SPaul Durrant bool fragment; 5426ed1f50c3SPaul Durrant bool done; 5427f9708b43SJan Beulich __sum16 *csum; 5428ed1f50c3SPaul Durrant 5429ed1f50c3SPaul Durrant fragment = false; 5430ed1f50c3SPaul Durrant done = false; 5431ed1f50c3SPaul Durrant 5432ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5433ed1f50c3SPaul Durrant 5434ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5435ed1f50c3SPaul Durrant if (err < 0) 5436ed1f50c3SPaul Durrant goto out; 5437ed1f50c3SPaul Durrant 5438ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5439ed1f50c3SPaul Durrant 5440ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5441ed1f50c3SPaul Durrant while (off <= len && !done) { 5442ed1f50c3SPaul Durrant switch (nexthdr) { 5443ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5444ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5445ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5446ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5447ed1f50c3SPaul Durrant 5448ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5449ed1f50c3SPaul Durrant off + 5450ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5451ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5452ed1f50c3SPaul Durrant if (err < 0) 5453ed1f50c3SPaul Durrant goto out; 5454ed1f50c3SPaul Durrant 5455ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5456ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5457ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5458ed1f50c3SPaul Durrant break; 5459ed1f50c3SPaul Durrant } 5460ed1f50c3SPaul Durrant case IPPROTO_AH: { 5461ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5462ed1f50c3SPaul Durrant 5463ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5464ed1f50c3SPaul Durrant off + 5465ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5466ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5467ed1f50c3SPaul Durrant if (err < 0) 5468ed1f50c3SPaul Durrant goto out; 5469ed1f50c3SPaul Durrant 5470ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5471ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5472ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5473ed1f50c3SPaul Durrant break; 5474ed1f50c3SPaul Durrant } 5475ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5476ed1f50c3SPaul Durrant struct frag_hdr *hp; 5477ed1f50c3SPaul Durrant 5478ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5479ed1f50c3SPaul Durrant off + 5480ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5481ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5482ed1f50c3SPaul Durrant if (err < 0) 5483ed1f50c3SPaul Durrant goto out; 5484ed1f50c3SPaul Durrant 5485ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5486ed1f50c3SPaul Durrant 5487ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5488ed1f50c3SPaul Durrant fragment = true; 5489ed1f50c3SPaul Durrant 5490ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5491ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5492ed1f50c3SPaul Durrant break; 5493ed1f50c3SPaul Durrant } 5494ed1f50c3SPaul Durrant default: 5495ed1f50c3SPaul Durrant done = true; 5496ed1f50c3SPaul Durrant break; 5497ed1f50c3SPaul Durrant } 5498ed1f50c3SPaul Durrant } 5499ed1f50c3SPaul Durrant 5500ed1f50c3SPaul Durrant err = -EPROTO; 5501ed1f50c3SPaul Durrant 5502ed1f50c3SPaul Durrant if (!done || fragment) 5503ed1f50c3SPaul Durrant goto out; 5504ed1f50c3SPaul Durrant 5505f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5506f9708b43SJan Beulich if (IS_ERR(csum)) 5507f9708b43SJan Beulich return PTR_ERR(csum); 5508ed1f50c3SPaul Durrant 5509ed1f50c3SPaul Durrant if (recalculate) 5510f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5511ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5512f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5513ed1f50c3SPaul Durrant err = 0; 5514ed1f50c3SPaul Durrant 5515ed1f50c3SPaul Durrant out: 5516ed1f50c3SPaul Durrant return err; 5517ed1f50c3SPaul Durrant } 5518ed1f50c3SPaul Durrant 5519ed1f50c3SPaul Durrant /** 5520ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5521ed1f50c3SPaul Durrant * @skb: the skb to set up 5522ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5523ed1f50c3SPaul Durrant */ 5524ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5525ed1f50c3SPaul Durrant { 5526ed1f50c3SPaul Durrant int err; 5527ed1f50c3SPaul Durrant 5528ed1f50c3SPaul Durrant switch (skb->protocol) { 5529ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5530f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5531ed1f50c3SPaul Durrant break; 5532ed1f50c3SPaul Durrant 5533ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5534ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5535ed1f50c3SPaul Durrant break; 5536ed1f50c3SPaul Durrant 5537ed1f50c3SPaul Durrant default: 5538ed1f50c3SPaul Durrant err = -EPROTO; 5539ed1f50c3SPaul Durrant break; 5540ed1f50c3SPaul Durrant } 5541ed1f50c3SPaul Durrant 5542ed1f50c3SPaul Durrant return err; 5543ed1f50c3SPaul Durrant } 5544ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5545ed1f50c3SPaul Durrant 55469afd85c9SLinus Lüssing /** 55479afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 55489afd85c9SLinus Lüssing * @skb: the skb to check 55499afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55509afd85c9SLinus Lüssing * 55519afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 55529afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 55539afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 55549afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 55559afd85c9SLinus Lüssing * 5556a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5557a516993fSLinus Lüssing * differs from the provided skb. 55589afd85c9SLinus Lüssing */ 55599afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 55609afd85c9SLinus Lüssing unsigned int transport_len) 55619afd85c9SLinus Lüssing { 55629afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55639afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 55649afd85c9SLinus Lüssing int ret; 55659afd85c9SLinus Lüssing 5566a516993fSLinus Lüssing if (skb->len < len) 55679afd85c9SLinus Lüssing return NULL; 5568a516993fSLinus Lüssing else if (skb->len == len) 55699afd85c9SLinus Lüssing return skb; 55709afd85c9SLinus Lüssing 55719afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 55729afd85c9SLinus Lüssing if (!skb_chk) 55739afd85c9SLinus Lüssing return NULL; 55749afd85c9SLinus Lüssing 55759afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 55769afd85c9SLinus Lüssing if (ret) { 55779afd85c9SLinus Lüssing kfree_skb(skb_chk); 55789afd85c9SLinus Lüssing return NULL; 55799afd85c9SLinus Lüssing } 55809afd85c9SLinus Lüssing 55819afd85c9SLinus Lüssing return skb_chk; 55829afd85c9SLinus Lüssing } 55839afd85c9SLinus Lüssing 55849afd85c9SLinus Lüssing /** 55859afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 55869afd85c9SLinus Lüssing * @skb: the skb to check 55879afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55889afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 55899afd85c9SLinus Lüssing * 55909afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 55919afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 55929afd85c9SLinus Lüssing * 55939afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 55949afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 55959afd85c9SLinus Lüssing * 5596a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5597a516993fSLinus Lüssing * differs from the provided skb. 55989afd85c9SLinus Lüssing */ 55999afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 56009afd85c9SLinus Lüssing unsigned int transport_len, 56019afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 56029afd85c9SLinus Lüssing { 56039afd85c9SLinus Lüssing struct sk_buff *skb_chk; 56049afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5605fcba67c9SLinus Lüssing __sum16 ret; 56069afd85c9SLinus Lüssing 56079afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 56089afd85c9SLinus Lüssing if (!skb_chk) 5609a516993fSLinus Lüssing goto err; 56109afd85c9SLinus Lüssing 5611a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5612a516993fSLinus Lüssing goto err; 56139afd85c9SLinus Lüssing 56149b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 56159afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 56169b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 56179afd85c9SLinus Lüssing 5618a516993fSLinus Lüssing if (ret) 5619a516993fSLinus Lüssing goto err; 56209afd85c9SLinus Lüssing 56219afd85c9SLinus Lüssing return skb_chk; 5622a516993fSLinus Lüssing 5623a516993fSLinus Lüssing err: 5624a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5625a516993fSLinus Lüssing kfree_skb(skb_chk); 5626a516993fSLinus Lüssing 5627a516993fSLinus Lüssing return NULL; 5628a516993fSLinus Lüssing 56299afd85c9SLinus Lüssing } 56309afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 56319afd85c9SLinus Lüssing 56324497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 56334497b076SBen Hutchings { 5634e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5635e87cc472SJoe Perches skb->dev->name); 56364497b076SBen Hutchings } 56374497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5638bad43ca8SEric Dumazet 5639bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5640bad43ca8SEric Dumazet { 56413d861f66SEric Dumazet if (head_stolen) { 56423d861f66SEric Dumazet skb_release_head_state(skb); 5643025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 56443d861f66SEric Dumazet } else { 5645bad43ca8SEric Dumazet __kfree_skb(skb); 5646bad43ca8SEric Dumazet } 56473d861f66SEric Dumazet } 5648bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5649bad43ca8SEric Dumazet 5650bad43ca8SEric Dumazet /** 5651bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5652bad43ca8SEric Dumazet * @to: prior buffer 5653bad43ca8SEric Dumazet * @from: buffer to add 5654bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5655c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5656bad43ca8SEric Dumazet */ 5657bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5658bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5659bad43ca8SEric Dumazet { 5660c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5661bad43ca8SEric Dumazet int i, delta, len = from->len; 5662bad43ca8SEric Dumazet 5663bad43ca8SEric Dumazet *fragstolen = false; 5664bad43ca8SEric Dumazet 5665bad43ca8SEric Dumazet if (skb_cloned(to)) 5666bad43ca8SEric Dumazet return false; 5667bad43ca8SEric Dumazet 56680646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated 56690646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones 56700646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment 56711effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 56721effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 56731effe8caSJean-Philippe Brucker * inconsistent reference counts. 56740646dc31SLiang Chen * In theory we could take full references if @from is cloned and 56750646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with 56760646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with. 56776a5bcd84SIlias Apalodimas */ 56780646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle || 56790646dc31SLiang Chen (from->pp_recycle && skb_cloned(from))) 56806a5bcd84SIlias Apalodimas return false; 56816a5bcd84SIlias Apalodimas 5682bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5683e93a0435SEric Dumazet if (len) 5684bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5685bad43ca8SEric Dumazet *delta_truesize = 0; 5686bad43ca8SEric Dumazet return true; 5687bad43ca8SEric Dumazet } 5688bad43ca8SEric Dumazet 5689c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5690c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5691c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5692bad43ca8SEric Dumazet return false; 56931f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 56941f8b977aSWillem de Bruijn return false; 5695bad43ca8SEric Dumazet 5696bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5697bad43ca8SEric Dumazet struct page *page; 5698bad43ca8SEric Dumazet unsigned int offset; 5699bad43ca8SEric Dumazet 5700c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5701c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5702bad43ca8SEric Dumazet return false; 5703bad43ca8SEric Dumazet 5704bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5705bad43ca8SEric Dumazet return false; 5706bad43ca8SEric Dumazet 5707bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5708bad43ca8SEric Dumazet 5709bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5710bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5711bad43ca8SEric Dumazet 5712c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5713bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5714bad43ca8SEric Dumazet *fragstolen = true; 5715bad43ca8SEric Dumazet } else { 5716c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5717c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5718bad43ca8SEric Dumazet return false; 5719bad43ca8SEric Dumazet 5720f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5721bad43ca8SEric Dumazet } 5722bad43ca8SEric Dumazet 5723bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5724bad43ca8SEric Dumazet 5725c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5726c818fa9eSEric Dumazet from_shinfo->frags, 5727c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5728c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5729bad43ca8SEric Dumazet 5730bad43ca8SEric Dumazet if (!skb_cloned(from)) 5731c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5732bad43ca8SEric Dumazet 57338ea853fdSLi RongQing /* if the skb is not cloned this does nothing 57348ea853fdSLi RongQing * since we set nr_frags to 0. 57358ea853fdSLi RongQing */ 5736c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5737c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5738bad43ca8SEric Dumazet 5739bad43ca8SEric Dumazet to->truesize += delta; 5740bad43ca8SEric Dumazet to->len += len; 5741bad43ca8SEric Dumazet to->data_len += len; 5742bad43ca8SEric Dumazet 5743bad43ca8SEric Dumazet *delta_truesize = delta; 5744bad43ca8SEric Dumazet return true; 5745bad43ca8SEric Dumazet } 5746bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5747621e84d6SNicolas Dichtel 5748621e84d6SNicolas Dichtel /** 57498b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5750621e84d6SNicolas Dichtel * 5751621e84d6SNicolas Dichtel * @skb: buffer to clean 57528b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5753621e84d6SNicolas Dichtel * 57548b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 57558b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 57568b27f277SNicolas Dichtel * operations. 57578b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 57588b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 57598b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5760621e84d6SNicolas Dichtel */ 57618b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5762621e84d6SNicolas Dichtel { 5763621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5764621e84d6SNicolas Dichtel skb->skb_iif = 0; 576560ff7467SWANG Cong skb->ignore_df = 0; 5766621e84d6SNicolas Dichtel skb_dst_drop(skb); 5767174e2381SFlorian Westphal skb_ext_reset(skb); 5768895b5c9fSFlorian Westphal nf_reset_ct(skb); 5769621e84d6SNicolas Dichtel nf_reset_trace(skb); 5770213dd74aSHerbert Xu 57716f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 57726f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5773875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 57746f9a5069SPetr Machata #endif 57756f9a5069SPetr Machata 5776213dd74aSHerbert Xu if (!xnet) 5777213dd74aSHerbert Xu return; 5778213dd74aSHerbert Xu 57792b5ec1a5SYe Yin ipvs_reset(skb); 5780213dd74aSHerbert Xu skb->mark = 0; 5781de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5782621e84d6SNicolas Dichtel } 5783621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5784de960aa9SFlorian Westphal 5785de960aa9SFlorian Westphal /** 5786de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5787de960aa9SFlorian Westphal * 5788de960aa9SFlorian Westphal * @skb: GSO skb 5789de960aa9SFlorian Westphal * 5790de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5791de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5792de960aa9SFlorian Westphal * 5793de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5794de960aa9SFlorian Westphal */ 5795a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5796de960aa9SFlorian Westphal { 5797de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5798f993bc25SFlorian Westphal unsigned int thlen = 0; 5799f993bc25SFlorian Westphal 5800f993bc25SFlorian Westphal if (skb->encapsulation) { 5801f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5802f993bc25SFlorian Westphal skb_transport_header(skb); 5803de960aa9SFlorian Westphal 5804de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5805f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5806f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5807f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 58081dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 580990017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5810ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5811ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5812f993bc25SFlorian Westphal } 58136d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 58146d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 58156d39d589SFlorian Westphal * accounted for. 58166d39d589SFlorian Westphal */ 5817f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5818de960aa9SFlorian Westphal } 5819a4a77718SDaniel Axtens 5820a4a77718SDaniel Axtens /** 5821a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5822a4a77718SDaniel Axtens * 5823a4a77718SDaniel Axtens * @skb: GSO skb 5824a4a77718SDaniel Axtens * 5825a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5826a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5827a4a77718SDaniel Axtens * 5828a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5829a4a77718SDaniel Axtens */ 5830a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5831a4a77718SDaniel Axtens { 5832a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5833a4a77718SDaniel Axtens skb_network_header(skb); 5834a4a77718SDaniel Axtens 5835a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5836a4a77718SDaniel Axtens } 5837a4a77718SDaniel Axtens 5838a4a77718SDaniel Axtens /** 5839a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5840a4a77718SDaniel Axtens * 5841a4a77718SDaniel Axtens * @skb: GSO skb 5842a4a77718SDaniel Axtens * 5843a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5844a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5845a4a77718SDaniel Axtens * headers (TCP/UDP). 5846a4a77718SDaniel Axtens */ 5847a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5848a4a77718SDaniel Axtens { 5849a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5850a4a77718SDaniel Axtens 5851a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5852a4a77718SDaniel Axtens } 58530d5501c1SVlad Yasevich 5854ae7ef81eSMarcelo Ricardo Leitner /** 58552b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 58562b16f048SDaniel Axtens * 58572b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 58582b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 58592b16f048SDaniel Axtens * 58602b16f048SDaniel Axtens * We might want to check: 58612b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 58622b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 58632b16f048SDaniel Axtens * 58642b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 58652b16f048SDaniel Axtens * 586649682bfaSMathieu Malaterre * @skb: GSO skb 586749682bfaSMathieu Malaterre * 58682b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 58692b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 58702b16f048SDaniel Axtens * 58712b16f048SDaniel Axtens * @max_len: The maximum permissible length. 58722b16f048SDaniel Axtens * 58732b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 58742b16f048SDaniel Axtens */ 58752b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 58762b16f048SDaniel Axtens unsigned int seg_len, 58772b16f048SDaniel Axtens unsigned int max_len) { 58782b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 58792b16f048SDaniel Axtens const struct sk_buff *iter; 58802b16f048SDaniel Axtens 58812b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 58822b16f048SDaniel Axtens return seg_len <= max_len; 58832b16f048SDaniel Axtens 58842b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 58852b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 58862b16f048SDaniel Axtens 58872b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 58882b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 58892b16f048SDaniel Axtens return false; 58902b16f048SDaniel Axtens } 58912b16f048SDaniel Axtens 58922b16f048SDaniel Axtens return true; 58932b16f048SDaniel Axtens } 58942b16f048SDaniel Axtens 58952b16f048SDaniel Axtens /** 5896779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5897ae7ef81eSMarcelo Ricardo Leitner * 5898ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 589976f21b99SDavid S. Miller * @mtu: MTU to validate against 5900ae7ef81eSMarcelo Ricardo Leitner * 5901779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5902779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5903779b7931SDaniel Axtens * payload. 5904ae7ef81eSMarcelo Ricardo Leitner */ 5905779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5906ae7ef81eSMarcelo Ricardo Leitner { 59072b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5908ae7ef81eSMarcelo Ricardo Leitner } 5909779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5910ae7ef81eSMarcelo Ricardo Leitner 59112b16f048SDaniel Axtens /** 59122b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 59132b16f048SDaniel Axtens * 59142b16f048SDaniel Axtens * @skb: GSO skb 59152b16f048SDaniel Axtens * @len: length to validate against 59162b16f048SDaniel Axtens * 59172b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 59182b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 59192b16f048SDaniel Axtens */ 59202b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 59212b16f048SDaniel Axtens { 59222b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 59232b16f048SDaniel Axtens } 59242b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 59252b16f048SDaniel Axtens 59260d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 59270d5501c1SVlad Yasevich { 5928d85e8be2SYuya Kusakabe int mac_len, meta_len; 5929d85e8be2SYuya Kusakabe void *meta; 59304bbb3e0eSToshiaki Makita 59310d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 59320d5501c1SVlad Yasevich kfree_skb(skb); 59330d5501c1SVlad Yasevich return NULL; 59340d5501c1SVlad Yasevich } 59350d5501c1SVlad Yasevich 59364bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5937ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 59384bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 59394bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5940ae474573SToshiaki Makita } 5941d85e8be2SYuya Kusakabe 5942d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5943d85e8be2SYuya Kusakabe if (meta_len) { 5944d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5945d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5946d85e8be2SYuya Kusakabe } 5947d85e8be2SYuya Kusakabe 59480d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 59490d5501c1SVlad Yasevich return skb; 59500d5501c1SVlad Yasevich } 59510d5501c1SVlad Yasevich 59520d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 59530d5501c1SVlad Yasevich { 59540d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 59550d5501c1SVlad Yasevich u16 vlan_tci; 59560d5501c1SVlad Yasevich 5957df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 59580d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 59590d5501c1SVlad Yasevich return skb; 59600d5501c1SVlad Yasevich } 59610d5501c1SVlad Yasevich 59620d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 59630d5501c1SVlad Yasevich if (unlikely(!skb)) 59640d5501c1SVlad Yasevich goto err_free; 596555eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 596655eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 59670d5501c1SVlad Yasevich goto err_free; 59680d5501c1SVlad Yasevich 59690d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 59700d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 59710d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 59720d5501c1SVlad Yasevich 59730d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 59740d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 59750d5501c1SVlad Yasevich 59760d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 59770d5501c1SVlad Yasevich if (unlikely(!skb)) 59780d5501c1SVlad Yasevich goto err_free; 59790d5501c1SVlad Yasevich 59800d5501c1SVlad Yasevich skb_reset_network_header(skb); 59818be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 59820d5501c1SVlad Yasevich skb_reset_transport_header(skb); 59830d5501c1SVlad Yasevich skb_reset_mac_len(skb); 59840d5501c1SVlad Yasevich 59850d5501c1SVlad Yasevich return skb; 59860d5501c1SVlad Yasevich 59870d5501c1SVlad Yasevich err_free: 59880d5501c1SVlad Yasevich kfree_skb(skb); 59890d5501c1SVlad Yasevich return NULL; 59900d5501c1SVlad Yasevich } 59910d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 59922e4e4410SEric Dumazet 599392ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5994e2195121SJiri Pirko { 5995e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5996e2195121SJiri Pirko return -ENOMEM; 5997e2195121SJiri Pirko 5998e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5999e2195121SJiri Pirko return 0; 6000e2195121SJiri Pirko 6001e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 6002e2195121SJiri Pirko } 6003e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 6004e2195121SJiri Pirko 6005bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 6006bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 6007bfca4c52SShmulik Ladkani */ 6008bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 600993515d53SJiri Pirko { 6010b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 601193515d53SJiri Pirko int err; 601293515d53SJiri Pirko 6013b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6014b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 6015b6a79208SShmulik Ladkani offset)) { 6016b6a79208SShmulik Ladkani return -EINVAL; 6017b6a79208SShmulik Ladkani } 6018b6a79208SShmulik Ladkani 601993515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 602093515d53SJiri Pirko if (unlikely(err)) 6021b6a79208SShmulik Ladkani return err; 602293515d53SJiri Pirko 602393515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 602493515d53SJiri Pirko 60250bcf2e4aSVladimir Oltean vlan_remove_tag(skb, vlan_tci); 602693515d53SJiri Pirko 602793515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 602893515d53SJiri Pirko 602993515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 603093515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 603193515d53SJiri Pirko 603293515d53SJiri Pirko skb_reset_mac_len(skb); 603393515d53SJiri Pirko 603493515d53SJiri Pirko return err; 603593515d53SJiri Pirko } 6036bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 603793515d53SJiri Pirko 6038b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 6039b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6040b6a79208SShmulik Ladkani */ 604193515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 604293515d53SJiri Pirko { 604393515d53SJiri Pirko u16 vlan_tci; 604493515d53SJiri Pirko __be16 vlan_proto; 604593515d53SJiri Pirko int err; 604693515d53SJiri Pirko 6047df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 6048b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 604993515d53SJiri Pirko } else { 6050ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 605193515d53SJiri Pirko return 0; 605293515d53SJiri Pirko 605393515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 605493515d53SJiri Pirko if (err) 605593515d53SJiri Pirko return err; 605693515d53SJiri Pirko } 605793515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 6058ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 605993515d53SJiri Pirko return 0; 606093515d53SJiri Pirko 606193515d53SJiri Pirko vlan_proto = skb->protocol; 606293515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 606393515d53SJiri Pirko if (unlikely(err)) 606493515d53SJiri Pirko return err; 606593515d53SJiri Pirko 606693515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 606793515d53SJiri Pirko return 0; 606893515d53SJiri Pirko } 606993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 607093515d53SJiri Pirko 6071b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 6072b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6073b6a79208SShmulik Ladkani */ 607493515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 607593515d53SJiri Pirko { 6076df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 6077b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 607893515d53SJiri Pirko int err; 607993515d53SJiri Pirko 6080b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6081b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 6082b6a79208SShmulik Ladkani offset)) { 6083b6a79208SShmulik Ladkani return -EINVAL; 6084b6a79208SShmulik Ladkani } 6085b6a79208SShmulik Ladkani 608693515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 6087df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 6088b6a79208SShmulik Ladkani if (err) 608993515d53SJiri Pirko return err; 60909241e2dfSDaniel Borkmann 609193515d53SJiri Pirko skb->protocol = skb->vlan_proto; 609293515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 609393515d53SJiri Pirko 60946b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 609593515d53SJiri Pirko } 609693515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 609793515d53SJiri Pirko return 0; 609893515d53SJiri Pirko } 609993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 610093515d53SJiri Pirko 610119fbcb36SGuillaume Nault /** 610219fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 610319fbcb36SGuillaume Nault * 610419fbcb36SGuillaume Nault * @skb: Socket buffer to modify 610519fbcb36SGuillaume Nault * 610619fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 610719fbcb36SGuillaume Nault * 610819fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 610919fbcb36SGuillaume Nault * present. 611019fbcb36SGuillaume Nault * 611119fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 611219fbcb36SGuillaume Nault */ 611319fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 611419fbcb36SGuillaume Nault { 611519fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 611619fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 611719fbcb36SGuillaume Nault return -EPROTO; 611819fbcb36SGuillaume Nault 611919fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 612019fbcb36SGuillaume Nault skb_reset_mac_header(skb); 612119fbcb36SGuillaume Nault skb_reset_mac_len(skb); 612219fbcb36SGuillaume Nault 612319fbcb36SGuillaume Nault return 0; 612419fbcb36SGuillaume Nault } 612519fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 612619fbcb36SGuillaume Nault 612719fbcb36SGuillaume Nault /** 612819fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 612919fbcb36SGuillaume Nault * 613019fbcb36SGuillaume Nault * @skb: Socket buffer to modify 613119fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 613219fbcb36SGuillaume Nault * @src: Source MAC address of the new header 613319fbcb36SGuillaume Nault * 613419fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 613519fbcb36SGuillaume Nault * 613619fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 613719fbcb36SGuillaume Nault * 613819fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 613919fbcb36SGuillaume Nault */ 614019fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 614119fbcb36SGuillaume Nault const unsigned char *src) 614219fbcb36SGuillaume Nault { 614319fbcb36SGuillaume Nault struct ethhdr *eth; 614419fbcb36SGuillaume Nault int err; 614519fbcb36SGuillaume Nault 614619fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 614719fbcb36SGuillaume Nault return -EPROTO; 614819fbcb36SGuillaume Nault 614919fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 615019fbcb36SGuillaume Nault if (err < 0) 615119fbcb36SGuillaume Nault return err; 615219fbcb36SGuillaume Nault 615319fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 615419fbcb36SGuillaume Nault skb_reset_mac_header(skb); 615519fbcb36SGuillaume Nault skb_reset_mac_len(skb); 615619fbcb36SGuillaume Nault 615719fbcb36SGuillaume Nault eth = eth_hdr(skb); 615819fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 615919fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 616019fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 616119fbcb36SGuillaume Nault 616219fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 616319fbcb36SGuillaume Nault 616419fbcb36SGuillaume Nault return 0; 616519fbcb36SGuillaume Nault } 616619fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 616719fbcb36SGuillaume Nault 61688822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 61698822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 61708822e270SJohn Hurley __be16 ethertype) 61718822e270SJohn Hurley { 61728822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 61738822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 61748822e270SJohn Hurley 61758822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 61768822e270SJohn Hurley } 61778822e270SJohn Hurley 61788822e270SJohn Hurley hdr->h_proto = ethertype; 61798822e270SJohn Hurley } 61808822e270SJohn Hurley 61818822e270SJohn Hurley /** 6182e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6183e7dbfed1SMartin Varghese * the packet 61848822e270SJohn Hurley * 61858822e270SJohn Hurley * @skb: buffer 61868822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 61878822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6188fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6189e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6190e7dbfed1SMartin Varghese * ethernet 61918822e270SJohn Hurley * 61928822e270SJohn Hurley * Expects skb->data at mac header. 61938822e270SJohn Hurley * 61948822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 61958822e270SJohn Hurley */ 6196fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6197d04ac224SMartin Varghese int mac_len, bool ethernet) 61988822e270SJohn Hurley { 61998822e270SJohn Hurley struct mpls_shim_hdr *lse; 62008822e270SJohn Hurley int err; 62018822e270SJohn Hurley 62028822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 62038822e270SJohn Hurley return -EINVAL; 62048822e270SJohn Hurley 62058822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 62068822e270SJohn Hurley if (skb->encapsulation) 62078822e270SJohn Hurley return -EINVAL; 62088822e270SJohn Hurley 62098822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 62108822e270SJohn Hurley if (unlikely(err)) 62118822e270SJohn Hurley return err; 62128822e270SJohn Hurley 62138822e270SJohn Hurley if (!skb->inner_protocol) { 6214e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 62158822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 62168822e270SJohn Hurley } 62178822e270SJohn Hurley 62188822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 62198822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6220fa4e0f88SDavide Caratti mac_len); 62218822e270SJohn Hurley skb_reset_mac_header(skb); 6222fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6223e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 62248822e270SJohn Hurley 62258822e270SJohn Hurley lse = mpls_hdr(skb); 62268822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 62278822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 62288822e270SJohn Hurley 62294296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 62308822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 62318822e270SJohn Hurley skb->protocol = mpls_proto; 62328822e270SJohn Hurley 62338822e270SJohn Hurley return 0; 62348822e270SJohn Hurley } 62358822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 62368822e270SJohn Hurley 62372e4e4410SEric Dumazet /** 6238ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6239ed246ceeSJohn Hurley * 6240ed246ceeSJohn Hurley * @skb: buffer 6241ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6242fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 624376f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6244ed246ceeSJohn Hurley * 6245ed246ceeSJohn Hurley * Expects skb->data at mac header. 6246ed246ceeSJohn Hurley * 6247ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6248ed246ceeSJohn Hurley */ 6249040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6250040b5cfbSMartin Varghese bool ethernet) 6251ed246ceeSJohn Hurley { 6252ed246ceeSJohn Hurley int err; 6253ed246ceeSJohn Hurley 6254ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6255dedc5a08SDavide Caratti return 0; 6256ed246ceeSJohn Hurley 6257fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6258ed246ceeSJohn Hurley if (unlikely(err)) 6259ed246ceeSJohn Hurley return err; 6260ed246ceeSJohn Hurley 6261ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6262ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6263fa4e0f88SDavide Caratti mac_len); 6264ed246ceeSJohn Hurley 6265ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6266ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6267fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6268ed246ceeSJohn Hurley 62694296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6270ed246ceeSJohn Hurley struct ethhdr *hdr; 6271ed246ceeSJohn Hurley 6272ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6273ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6274ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6275ed246ceeSJohn Hurley } 6276ed246ceeSJohn Hurley skb->protocol = next_proto; 6277ed246ceeSJohn Hurley 6278ed246ceeSJohn Hurley return 0; 6279ed246ceeSJohn Hurley } 6280ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6281ed246ceeSJohn Hurley 6282ed246ceeSJohn Hurley /** 6283d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6284d27cf5c5SJohn Hurley * 6285d27cf5c5SJohn Hurley * @skb: buffer 6286d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6287d27cf5c5SJohn Hurley * 6288d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6289d27cf5c5SJohn Hurley * 6290d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6291d27cf5c5SJohn Hurley */ 6292d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6293d27cf5c5SJohn Hurley { 6294d27cf5c5SJohn Hurley int err; 6295d27cf5c5SJohn Hurley 6296d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6297d27cf5c5SJohn Hurley return -EINVAL; 6298d27cf5c5SJohn Hurley 6299d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6300d27cf5c5SJohn Hurley if (unlikely(err)) 6301d27cf5c5SJohn Hurley return err; 6302d27cf5c5SJohn Hurley 6303d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6304d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6305d27cf5c5SJohn Hurley 6306d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6307d27cf5c5SJohn Hurley } 6308d27cf5c5SJohn Hurley 6309d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6310d27cf5c5SJohn Hurley 6311d27cf5c5SJohn Hurley return 0; 6312d27cf5c5SJohn Hurley } 6313d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6314d27cf5c5SJohn Hurley 6315d27cf5c5SJohn Hurley /** 63162a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 63172a2ea508SJohn Hurley * 63182a2ea508SJohn Hurley * @skb: buffer 63192a2ea508SJohn Hurley * 63202a2ea508SJohn Hurley * Expects skb->data at mac header. 63212a2ea508SJohn Hurley * 63222a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 63232a2ea508SJohn Hurley */ 63242a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 63252a2ea508SJohn Hurley { 63262a2ea508SJohn Hurley u32 lse; 63272a2ea508SJohn Hurley u8 ttl; 63282a2ea508SJohn Hurley 63292a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 63302a2ea508SJohn Hurley return -EINVAL; 63312a2ea508SJohn Hurley 633213de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 633313de4ed9SDavide Caratti return -ENOMEM; 633413de4ed9SDavide Caratti 63352a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 63362a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 63372a2ea508SJohn Hurley if (!--ttl) 63382a2ea508SJohn Hurley return -EINVAL; 63392a2ea508SJohn Hurley 63402a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 63412a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 63422a2ea508SJohn Hurley 63432a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 63442a2ea508SJohn Hurley } 63452a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 63462a2ea508SJohn Hurley 63472a2ea508SJohn Hurley /** 63482e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 63492e4e4410SEric Dumazet * 6350de3f0d0eSMasanari Iida * @header_len: size of linear part 6351de3f0d0eSMasanari Iida * @data_len: needed length in frags 6352de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6353de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6354de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 63552e4e4410SEric Dumazet * 63562e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 63572e4e4410SEric Dumazet */ 63582e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 63592e4e4410SEric Dumazet unsigned long data_len, 63602e4e4410SEric Dumazet int max_page_order, 63612e4e4410SEric Dumazet int *errcode, 63622e4e4410SEric Dumazet gfp_t gfp_mask) 63632e4e4410SEric Dumazet { 63642e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 63652e4e4410SEric Dumazet unsigned long chunk; 63662e4e4410SEric Dumazet struct sk_buff *skb; 63672e4e4410SEric Dumazet struct page *page; 63682e4e4410SEric Dumazet int i; 63692e4e4410SEric Dumazet 63702e4e4410SEric Dumazet *errcode = -EMSGSIZE; 63712e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 63722e4e4410SEric Dumazet * high order pages... 63732e4e4410SEric Dumazet */ 63742e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 63752e4e4410SEric Dumazet return NULL; 63762e4e4410SEric Dumazet 63772e4e4410SEric Dumazet *errcode = -ENOBUFS; 6378f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 63792e4e4410SEric Dumazet if (!skb) 63802e4e4410SEric Dumazet return NULL; 63812e4e4410SEric Dumazet 63822e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 63832e4e4410SEric Dumazet 63842e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 63852e4e4410SEric Dumazet int order = max_page_order; 63862e4e4410SEric Dumazet 63872e4e4410SEric Dumazet while (order) { 63882e4e4410SEric Dumazet if (npages >= 1 << order) { 6389d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 63902e4e4410SEric Dumazet __GFP_COMP | 6391d14b56f5SMichal Hocko __GFP_NOWARN, 63922e4e4410SEric Dumazet order); 63932e4e4410SEric Dumazet if (page) 63942e4e4410SEric Dumazet goto fill_page; 63952e4e4410SEric Dumazet /* Do not retry other high order allocations */ 63962e4e4410SEric Dumazet order = 1; 63972e4e4410SEric Dumazet max_page_order = 0; 63982e4e4410SEric Dumazet } 63992e4e4410SEric Dumazet order--; 64002e4e4410SEric Dumazet } 64012e4e4410SEric Dumazet page = alloc_page(gfp_mask); 64022e4e4410SEric Dumazet if (!page) 64032e4e4410SEric Dumazet goto failure; 64042e4e4410SEric Dumazet fill_page: 64052e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 64062e4e4410SEric Dumazet PAGE_SIZE << order); 64072e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 64082e4e4410SEric Dumazet data_len -= chunk; 64092e4e4410SEric Dumazet npages -= 1 << order; 64102e4e4410SEric Dumazet } 64112e4e4410SEric Dumazet return skb; 64122e4e4410SEric Dumazet 64132e4e4410SEric Dumazet failure: 64142e4e4410SEric Dumazet kfree_skb(skb); 64152e4e4410SEric Dumazet return NULL; 64162e4e4410SEric Dumazet } 64172e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 64186fa01ccdSSowmini Varadhan 64196fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 64206fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 64216fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 64226fa01ccdSSowmini Varadhan { 64236fa01ccdSSowmini Varadhan int i; 642412d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 64256fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 64266fa01ccdSSowmini Varadhan u8 *data; 64276fa01ccdSSowmini Varadhan 64286fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64296fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 643012d6c1d3SKees Cook 64315c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 64326fa01ccdSSowmini Varadhan if (!data) 64336fa01ccdSSowmini Varadhan return -ENOMEM; 643412d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64356fa01ccdSSowmini Varadhan 64366fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 64376fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 64386fa01ccdSSowmini Varadhan skb->len -= off; 64396fa01ccdSSowmini Varadhan 64406fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 64416fa01ccdSSowmini Varadhan skb_shinfo(skb), 64426fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 64436fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 64446fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 64456fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 64466fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6447bf9f1baaSEric Dumazet skb_kfree_head(data, size); 64486fa01ccdSSowmini Varadhan return -ENOMEM; 64496fa01ccdSSowmini Varadhan } 64506fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 64516fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64526fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64536fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6454b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 64556fa01ccdSSowmini Varadhan } else { 64566fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 64576fa01ccdSSowmini Varadhan * relocate values 64586fa01ccdSSowmini Varadhan */ 6459b07a2d97SJakub Kicinski skb_free_head(skb, false); 64606fa01ccdSSowmini Varadhan } 64616fa01ccdSSowmini Varadhan 64626fa01ccdSSowmini Varadhan skb->head = data; 64636fa01ccdSSowmini Varadhan skb->data = data; 64646fa01ccdSSowmini Varadhan skb->head_frag = 0; 6465763087daSEric Dumazet skb_set_end_offset(skb, size); 64666fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 64676fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64686fa01ccdSSowmini Varadhan skb->cloned = 0; 64696fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64706fa01ccdSSowmini Varadhan skb->nohdr = 0; 64716fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64726fa01ccdSSowmini Varadhan 64736fa01ccdSSowmini Varadhan return 0; 64746fa01ccdSSowmini Varadhan } 64756fa01ccdSSowmini Varadhan 64766fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 64776fa01ccdSSowmini Varadhan 64786fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 64796fa01ccdSSowmini Varadhan * pskb_carve() 64806fa01ccdSSowmini Varadhan */ 64816fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 64826fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 64836fa01ccdSSowmini Varadhan gfp_t gfp_mask) 64846fa01ccdSSowmini Varadhan { 64856fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 64866fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 64876fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 64886fa01ccdSSowmini Varadhan 64896fa01ccdSSowmini Varadhan do { 64906fa01ccdSSowmini Varadhan if (!list) { 64916fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 64926fa01ccdSSowmini Varadhan return -EFAULT; 64936fa01ccdSSowmini Varadhan } 64946fa01ccdSSowmini Varadhan if (list->len <= eat) { 64956fa01ccdSSowmini Varadhan /* Eaten as whole. */ 64966fa01ccdSSowmini Varadhan eat -= list->len; 64976fa01ccdSSowmini Varadhan list = list->next; 64986fa01ccdSSowmini Varadhan insp = list; 64996fa01ccdSSowmini Varadhan } else { 65006fa01ccdSSowmini Varadhan /* Eaten partially. */ 65016fa01ccdSSowmini Varadhan if (skb_shared(list)) { 65026fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 65036fa01ccdSSowmini Varadhan if (!clone) 65046fa01ccdSSowmini Varadhan return -ENOMEM; 65056fa01ccdSSowmini Varadhan insp = list->next; 65066fa01ccdSSowmini Varadhan list = clone; 65076fa01ccdSSowmini Varadhan } else { 65086fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 65096fa01ccdSSowmini Varadhan insp = list; 65106fa01ccdSSowmini Varadhan } 65116fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 65126fa01ccdSSowmini Varadhan kfree_skb(clone); 65136fa01ccdSSowmini Varadhan return -ENOMEM; 65146fa01ccdSSowmini Varadhan } 65156fa01ccdSSowmini Varadhan break; 65166fa01ccdSSowmini Varadhan } 65176fa01ccdSSowmini Varadhan } while (eat); 65186fa01ccdSSowmini Varadhan 65196fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 65206fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 65216fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6522ef527f96SEric Dumazet consume_skb(list); 65236fa01ccdSSowmini Varadhan } 65246fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 65256fa01ccdSSowmini Varadhan if (clone) { 65266fa01ccdSSowmini Varadhan clone->next = list; 65276fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 65286fa01ccdSSowmini Varadhan } 65296fa01ccdSSowmini Varadhan return 0; 65306fa01ccdSSowmini Varadhan } 65316fa01ccdSSowmini Varadhan 65326fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 65336fa01ccdSSowmini Varadhan * non-linear part of skb 65346fa01ccdSSowmini Varadhan */ 65356fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 65366fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 65376fa01ccdSSowmini Varadhan { 65386fa01ccdSSowmini Varadhan int i, k = 0; 653912d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 65406fa01ccdSSowmini Varadhan u8 *data; 65416fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 65426fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 65436fa01ccdSSowmini Varadhan 65446fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 65456fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 654612d6c1d3SKees Cook 65475c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 65486fa01ccdSSowmini Varadhan if (!data) 65496fa01ccdSSowmini Varadhan return -ENOMEM; 655012d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 65516fa01ccdSSowmini Varadhan 65526fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6553e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 65546fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6555bf9f1baaSEric Dumazet skb_kfree_head(data, size); 65566fa01ccdSSowmini Varadhan return -ENOMEM; 65576fa01ccdSSowmini Varadhan } 65586fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 65596fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 65606fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 65616fa01ccdSSowmini Varadhan 65626fa01ccdSSowmini Varadhan if (pos + fsize > off) { 65636fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 65646fa01ccdSSowmini Varadhan 65656fa01ccdSSowmini Varadhan if (pos < off) { 65666fa01ccdSSowmini Varadhan /* Split frag. 65676fa01ccdSSowmini Varadhan * We have two variants in this case: 65686fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 65696fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 65706fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 65716fa01ccdSSowmini Varadhan * where splitting is expensive. 65726fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 65736fa01ccdSSowmini Varadhan */ 6574b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 65756fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 65766fa01ccdSSowmini Varadhan } 65776fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 65786fa01ccdSSowmini Varadhan k++; 65796fa01ccdSSowmini Varadhan } 65806fa01ccdSSowmini Varadhan pos += fsize; 65816fa01ccdSSowmini Varadhan } 65826fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 65836fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 65846fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 65856fa01ccdSSowmini Varadhan 65866fa01ccdSSowmini Varadhan /* split line is in frag list */ 6587eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6588eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6589eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6590eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6591bf9f1baaSEric Dumazet skb_kfree_head(data, size); 6592eabe8618SMiaohe Lin return -ENOMEM; 65936fa01ccdSSowmini Varadhan } 6594b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 65956fa01ccdSSowmini Varadhan 65966fa01ccdSSowmini Varadhan skb->head = data; 65976fa01ccdSSowmini Varadhan skb->head_frag = 0; 65986fa01ccdSSowmini Varadhan skb->data = data; 6599763087daSEric Dumazet skb_set_end_offset(skb, size); 66006fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 66016fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 66026fa01ccdSSowmini Varadhan skb->cloned = 0; 66036fa01ccdSSowmini Varadhan skb->hdr_len = 0; 66046fa01ccdSSowmini Varadhan skb->nohdr = 0; 66056fa01ccdSSowmini Varadhan skb->len -= off; 66066fa01ccdSSowmini Varadhan skb->data_len = skb->len; 66076fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 66086fa01ccdSSowmini Varadhan return 0; 66096fa01ccdSSowmini Varadhan } 66106fa01ccdSSowmini Varadhan 66116fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 66126fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 66136fa01ccdSSowmini Varadhan { 66146fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 66156fa01ccdSSowmini Varadhan 66166fa01ccdSSowmini Varadhan if (len < headlen) 66176fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 66186fa01ccdSSowmini Varadhan else 66196fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 66206fa01ccdSSowmini Varadhan } 66216fa01ccdSSowmini Varadhan 66226fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 66236fa01ccdSSowmini Varadhan * a new skb 66246fa01ccdSSowmini Varadhan */ 66256fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 66266fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 66276fa01ccdSSowmini Varadhan { 66286fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 66296fa01ccdSSowmini Varadhan 66306fa01ccdSSowmini Varadhan if (!clone) 66316fa01ccdSSowmini Varadhan return NULL; 66326fa01ccdSSowmini Varadhan 66336fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 66346fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 66356fa01ccdSSowmini Varadhan kfree_skb(clone); 66366fa01ccdSSowmini Varadhan return NULL; 66376fa01ccdSSowmini Varadhan } 66386fa01ccdSSowmini Varadhan return clone; 66396fa01ccdSSowmini Varadhan } 66406fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6641c8c8b127SEric Dumazet 6642c8c8b127SEric Dumazet /** 6643c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6644c8c8b127SEric Dumazet * @skb: buffer 6645c8c8b127SEric Dumazet * 6646c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6647c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6648c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6649c8c8b127SEric Dumazet * truesize. 6650c8c8b127SEric Dumazet * Notes: 6651c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6652c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6653c8c8b127SEric Dumazet */ 6654c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6655c8c8b127SEric Dumazet { 66563174fed9SEric Dumazet if (skb->data_len) { 66573174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6658c8c8b127SEric Dumazet skb_cloned(skb)) 6659c8c8b127SEric Dumazet return; 6660c8c8b127SEric Dumazet 6661c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6662c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 66633174fed9SEric Dumazet } 66643174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 66653174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 66663174fed9SEric Dumazet * their truesize. 66673174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 66683174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 66693174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6670c8c8b127SEric Dumazet */ 6671c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6672c8c8b127SEric Dumazet } 6673d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6674df5042f4SFlorian Westphal 6675df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6676df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6677df5042f4SFlorian Westphal { 6678df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6679df5042f4SFlorian Westphal } 6680df5042f4SFlorian Westphal 66818b69a803SPaolo Abeni /** 66828b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 66838b69a803SPaolo Abeni * 66844930f483SFlorian Westphal * @flags: See kmalloc(). 66854930f483SFlorian Westphal * 66868b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 66878b69a803SPaolo Abeni * skb via __skb_ext_set(). 66888b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 66898b69a803SPaolo Abeni */ 66904930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6691df5042f4SFlorian Westphal { 66924930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6693df5042f4SFlorian Westphal 6694df5042f4SFlorian Westphal if (new) { 6695df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6696df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6697df5042f4SFlorian Westphal } 6698df5042f4SFlorian Westphal 6699df5042f4SFlorian Westphal return new; 6700df5042f4SFlorian Westphal } 6701df5042f4SFlorian Westphal 67024165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 67034165079bSFlorian Westphal unsigned int old_active) 6704df5042f4SFlorian Westphal { 6705df5042f4SFlorian Westphal struct skb_ext *new; 6706df5042f4SFlorian Westphal 6707df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6708df5042f4SFlorian Westphal return old; 6709df5042f4SFlorian Westphal 6710df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6711df5042f4SFlorian Westphal if (!new) 6712df5042f4SFlorian Westphal return NULL; 6713df5042f4SFlorian Westphal 6714df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6715df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6716df5042f4SFlorian Westphal 67174165079bSFlorian Westphal #ifdef CONFIG_XFRM 67184165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 67194165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 67204165079bSFlorian Westphal unsigned int i; 67214165079bSFlorian Westphal 67224165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 67234165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 67244165079bSFlorian Westphal } 67254165079bSFlorian Westphal #endif 6726df5042f4SFlorian Westphal __skb_ext_put(old); 6727df5042f4SFlorian Westphal return new; 6728df5042f4SFlorian Westphal } 6729df5042f4SFlorian Westphal 6730df5042f4SFlorian Westphal /** 67318b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 67328b69a803SPaolo Abeni * @skb: buffer 67338b69a803SPaolo Abeni * @id: extension id 67348b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 67358b69a803SPaolo Abeni * 67368b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 67378b69a803SPaolo Abeni * 67388b69a803SPaolo Abeni * Returns the pointer to the extension. 67398b69a803SPaolo Abeni */ 67408b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 67418b69a803SPaolo Abeni struct skb_ext *ext) 67428b69a803SPaolo Abeni { 67438b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 67448b69a803SPaolo Abeni 67458b69a803SPaolo Abeni skb_ext_put(skb); 67468b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 67478b69a803SPaolo Abeni ext->chunks = newlen; 67488b69a803SPaolo Abeni ext->offset[id] = newoff; 67498b69a803SPaolo Abeni skb->extensions = ext; 67508b69a803SPaolo Abeni skb->active_extensions = 1 << id; 67518b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 67528b69a803SPaolo Abeni } 67538b69a803SPaolo Abeni 67548b69a803SPaolo Abeni /** 6755df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6756df5042f4SFlorian Westphal * @skb: buffer 6757df5042f4SFlorian Westphal * @id: extension to allocate space for 6758df5042f4SFlorian Westphal * 6759df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6760df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6761df5042f4SFlorian Westphal * is returned. 6762df5042f4SFlorian Westphal * 6763df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6764df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6765df5042f4SFlorian Westphal * 6766df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6767df5042f4SFlorian Westphal */ 6768df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6769df5042f4SFlorian Westphal { 6770df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6771df5042f4SFlorian Westphal unsigned int newlen, newoff; 6772df5042f4SFlorian Westphal 6773df5042f4SFlorian Westphal if (skb->active_extensions) { 6774df5042f4SFlorian Westphal old = skb->extensions; 6775df5042f4SFlorian Westphal 67764165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6777df5042f4SFlorian Westphal if (!new) 6778df5042f4SFlorian Westphal return NULL; 6779df5042f4SFlorian Westphal 6780682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6781df5042f4SFlorian Westphal goto set_active; 6782df5042f4SFlorian Westphal 6783e94e50bdSPaolo Abeni newoff = new->chunks; 6784df5042f4SFlorian Westphal } else { 6785df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6786df5042f4SFlorian Westphal 67874930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6788df5042f4SFlorian Westphal if (!new) 6789df5042f4SFlorian Westphal return NULL; 6790df5042f4SFlorian Westphal } 6791df5042f4SFlorian Westphal 6792df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6793df5042f4SFlorian Westphal new->chunks = newlen; 6794df5042f4SFlorian Westphal new->offset[id] = newoff; 6795df5042f4SFlorian Westphal set_active: 6796b0999f38SPaolo Abeni skb->slow_gro = 1; 6797682ec859SPaolo Abeni skb->extensions = new; 6798df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6799df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6800df5042f4SFlorian Westphal } 6801df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6802df5042f4SFlorian Westphal 68034165079bSFlorian Westphal #ifdef CONFIG_XFRM 68044165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 68054165079bSFlorian Westphal { 68064165079bSFlorian Westphal unsigned int i; 68074165079bSFlorian Westphal 68084165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 68094165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 68104165079bSFlorian Westphal } 68114165079bSFlorian Westphal #endif 68124165079bSFlorian Westphal 681378476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 681478476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 681578476d31SJeremy Kerr { 681678476d31SJeremy Kerr if (flow->key) 681778476d31SJeremy Kerr mctp_key_unref(flow->key); 681878476d31SJeremy Kerr } 681978476d31SJeremy Kerr #endif 682078476d31SJeremy Kerr 6821df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6822df5042f4SFlorian Westphal { 6823df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6824df5042f4SFlorian Westphal 6825df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6826df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6827df5042f4SFlorian Westphal skb->extensions = NULL; 6828df5042f4SFlorian Westphal __skb_ext_put(ext); 68294165079bSFlorian Westphal #ifdef CONFIG_XFRM 68304165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 68314165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 68324165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 68334165079bSFlorian Westphal 68344165079bSFlorian Westphal skb_ext_put_sp(sp); 68354165079bSFlorian Westphal sp->len = 0; 68364165079bSFlorian Westphal #endif 6837df5042f4SFlorian Westphal } 6838df5042f4SFlorian Westphal } 6839df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6840df5042f4SFlorian Westphal 6841df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6842df5042f4SFlorian Westphal { 6843df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6844df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6845df5042f4SFlorian Westphal */ 6846df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6847df5042f4SFlorian Westphal goto free_now; 6848df5042f4SFlorian Westphal 6849df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6850df5042f4SFlorian Westphal return; 6851df5042f4SFlorian Westphal free_now: 68524165079bSFlorian Westphal #ifdef CONFIG_XFRM 68534165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 68544165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 68554165079bSFlorian Westphal #endif 685678476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 685778476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 685878476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 685978476d31SJeremy Kerr #endif 68604165079bSFlorian Westphal 6861df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6862df5042f4SFlorian Westphal } 6863df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6864df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 686568822bdfSEric Dumazet 686668822bdfSEric Dumazet /** 686768822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 686868822bdfSEric Dumazet * @skb: buffer 686968822bdfSEric Dumazet * 687068822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 687168822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 687268822bdfSEric Dumazet * and memory zone spinlock contention. 687368822bdfSEric Dumazet */ 687468822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 687568822bdfSEric Dumazet { 687668822bdfSEric Dumazet int cpu = skb->alloc_cpu; 687768822bdfSEric Dumazet struct softnet_data *sd; 687839564c3fSEric Dumazet unsigned int defer_max; 687968822bdfSEric Dumazet bool kick; 688068822bdfSEric Dumazet 688168822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 688268822bdfSEric Dumazet !cpu_online(cpu) || 688368822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 688439564c3fSEric Dumazet nodefer: __kfree_skb(skb); 688568822bdfSEric Dumazet return; 688668822bdfSEric Dumazet } 688768822bdfSEric Dumazet 6888e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb_dst(skb)); 6889e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb->destructor); 6890e8e1ce84SEric Dumazet 689168822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 689239564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 689339564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 689439564c3fSEric Dumazet goto nodefer; 689539564c3fSEric Dumazet 6896931e93bdSEric Dumazet spin_lock_bh(&sd->defer_lock); 6897c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6898c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6899c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6900c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6901c09b0cd2SJakub Kicinski 690268822bdfSEric Dumazet skb->next = sd->defer_list; 690368822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 690468822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 6905931e93bdSEric Dumazet spin_unlock_bh(&sd->defer_lock); 690668822bdfSEric Dumazet 690768822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 690868822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 690968822bdfSEric Dumazet */ 691097e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 691168822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 691268822bdfSEric Dumazet } 6913