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 96bf9f1baaSEric Dumazet static struct kmem_cache *skb_small_head_cache __ro_after_init; 97bf9f1baaSEric Dumazet 98bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER) 99bf9f1baaSEric Dumazet 100bf9f1baaSEric Dumazet /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two. 101bf9f1baaSEric Dumazet * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique 102bf9f1baaSEric Dumazet * size, and we can differentiate heads from skb_small_head_cache 103bf9f1baaSEric Dumazet * vs system slabs by looking at their size (skb_end_offset()). 104bf9f1baaSEric Dumazet */ 105bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_CACHE_SIZE \ 106bf9f1baaSEric Dumazet (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \ 107bf9f1baaSEric Dumazet (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \ 108bf9f1baaSEric Dumazet SKB_SMALL_HEAD_SIZE) 109bf9f1baaSEric Dumazet 110bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_HEADROOM \ 111bf9f1baaSEric Dumazet SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE) 112bf9f1baaSEric Dumazet 1135f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 1145f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 1151da177e4SLinus Torvalds 1169cb252c4SMenglong Dong #undef FN 1179cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 118071c0fc6SJohannes Berg static const char * const drop_reasons[] = { 1190e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 1209cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 1219cb252c4SMenglong Dong }; 122071c0fc6SJohannes Berg 123071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = { 124071c0fc6SJohannes Berg .reasons = drop_reasons, 125071c0fc6SJohannes Berg .n_reasons = ARRAY_SIZE(drop_reasons), 126071c0fc6SJohannes Berg }; 127071c0fc6SJohannes Berg 128071c0fc6SJohannes Berg const struct drop_reason_list __rcu * 129071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = { 130071c0fc6SJohannes Berg [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core), 131071c0fc6SJohannes Berg }; 132071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys); 133071c0fc6SJohannes Berg 134071c0fc6SJohannes Berg /** 135071c0fc6SJohannes Berg * drop_reasons_register_subsys - register another drop reason subsystem 136071c0fc6SJohannes Berg * @subsys: the subsystem to register, must not be the core 137071c0fc6SJohannes Berg * @list: the list of drop reasons within the subsystem, must point to 138071c0fc6SJohannes Berg * a statically initialized list 139071c0fc6SJohannes Berg */ 140071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys, 141071c0fc6SJohannes Berg const struct drop_reason_list *list) 142071c0fc6SJohannes Berg { 143071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 144071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 145071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 146071c0fc6SJohannes Berg return; 147071c0fc6SJohannes Berg 148071c0fc6SJohannes Berg /* must point to statically allocated memory, so INIT is OK */ 149071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list); 150071c0fc6SJohannes Berg } 151071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys); 152071c0fc6SJohannes Berg 153071c0fc6SJohannes Berg /** 154071c0fc6SJohannes Berg * drop_reasons_unregister_subsys - unregister a drop reason subsystem 155071c0fc6SJohannes Berg * @subsys: the subsystem to remove, must not be the core 156071c0fc6SJohannes Berg * 157071c0fc6SJohannes Berg * Note: This will synchronize_rcu() to ensure no users when it returns. 158071c0fc6SJohannes Berg */ 159071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys) 160071c0fc6SJohannes Berg { 161071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 162071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 163071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 164071c0fc6SJohannes Berg return; 165071c0fc6SJohannes Berg 166071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL); 167071c0fc6SJohannes Berg 168071c0fc6SJohannes Berg synchronize_rcu(); 169071c0fc6SJohannes Berg } 170071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys); 171ec43908dSMenglong Dong 1721da177e4SLinus Torvalds /** 173f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1741da177e4SLinus Torvalds * @skb: buffer 1751da177e4SLinus Torvalds * @sz: size 176f05de73bSJean Sacren * @addr: address 17799d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1781da177e4SLinus Torvalds * 179f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 180f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 181f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 182f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1831da177e4SLinus Torvalds */ 184f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 18599d5851eSJames Hogan const char msg[]) 1861da177e4SLinus Torvalds { 18741a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 18899d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1894305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 19026095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1911da177e4SLinus Torvalds BUG(); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1951da177e4SLinus Torvalds { 196f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 199f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 200f05de73bSJean Sacren { 201f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 202f05de73bSJean Sacren } 203c93bdd0eSMel Gorman 20450fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 205f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 206f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 20750fad4b5SAlexander Lobakin 208dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 209dbae2b06SPaolo Abeni 210dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 211dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 212dbae2b06SPaolo Abeni 213dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 214dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 215dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 216dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 217dbae2b06SPaolo Abeni */ 218dbae2b06SPaolo Abeni 219dbae2b06SPaolo Abeni struct page_frag_1k { 220dbae2b06SPaolo Abeni void *va; 221dbae2b06SPaolo Abeni u16 offset; 222dbae2b06SPaolo Abeni bool pfmemalloc; 223dbae2b06SPaolo Abeni }; 224dbae2b06SPaolo Abeni 225dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 226dbae2b06SPaolo Abeni { 227dbae2b06SPaolo Abeni struct page *page; 228dbae2b06SPaolo Abeni int offset; 229dbae2b06SPaolo Abeni 230dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 231dbae2b06SPaolo Abeni if (likely(offset >= 0)) 232dbae2b06SPaolo Abeni goto use_frag; 233dbae2b06SPaolo Abeni 234dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 235dbae2b06SPaolo Abeni if (!page) 236dbae2b06SPaolo Abeni return NULL; 237dbae2b06SPaolo Abeni 238dbae2b06SPaolo Abeni nc->va = page_address(page); 239dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 240dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 241dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 242dbae2b06SPaolo Abeni 243dbae2b06SPaolo Abeni use_frag: 244dbae2b06SPaolo Abeni nc->offset = offset; 245dbae2b06SPaolo Abeni return nc->va + offset; 246dbae2b06SPaolo Abeni } 247dbae2b06SPaolo Abeni #else 248dbae2b06SPaolo Abeni 249dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 250dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 251dbae2b06SPaolo Abeni */ 252dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 253dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 254dbae2b06SPaolo Abeni 255dbae2b06SPaolo Abeni struct page_frag_1k { 256dbae2b06SPaolo Abeni }; 257dbae2b06SPaolo Abeni 258dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 259dbae2b06SPaolo Abeni { 260dbae2b06SPaolo Abeni return NULL; 261dbae2b06SPaolo Abeni } 262dbae2b06SPaolo Abeni 263dbae2b06SPaolo Abeni #endif 264dbae2b06SPaolo Abeni 26550fad4b5SAlexander Lobakin struct napi_alloc_cache { 26650fad4b5SAlexander Lobakin struct page_frag_cache page; 267dbae2b06SPaolo Abeni struct page_frag_1k page_small; 26850fad4b5SAlexander Lobakin unsigned int skb_count; 26950fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 27050fad4b5SAlexander Lobakin }; 27150fad4b5SAlexander Lobakin 27250fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 27350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 27450fad4b5SAlexander Lobakin 275dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 276dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 277dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 278dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 279dbae2b06SPaolo Abeni * does not accidentally come back. 280dbae2b06SPaolo Abeni */ 281dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 282dbae2b06SPaolo Abeni { 283dbae2b06SPaolo Abeni struct sk_buff *skb; 284dbae2b06SPaolo Abeni 285dbae2b06SPaolo Abeni local_bh_disable(); 286dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 287dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 288dbae2b06SPaolo Abeni napi_free_frags(napi); 289dbae2b06SPaolo Abeni local_bh_enable(); 290dbae2b06SPaolo Abeni } 291dbae2b06SPaolo Abeni 29232e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 29350fad4b5SAlexander Lobakin { 29450fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 29550fad4b5SAlexander Lobakin 29650fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 29750fad4b5SAlexander Lobakin 29832e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 29950fad4b5SAlexander Lobakin } 30050fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 30150fad4b5SAlexander Lobakin 30250fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 30350fad4b5SAlexander Lobakin { 30450fad4b5SAlexander Lobakin void *data; 30550fad4b5SAlexander Lobakin 30650fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 307afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 30832e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 30932e3573fSYajun Deng 31050fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 31150fad4b5SAlexander Lobakin } else { 31232e3573fSYajun Deng struct napi_alloc_cache *nc; 31332e3573fSYajun Deng 31450fad4b5SAlexander Lobakin local_bh_disable(); 31532e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 31632e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 31750fad4b5SAlexander Lobakin local_bh_enable(); 31850fad4b5SAlexander Lobakin } 31950fad4b5SAlexander Lobakin return data; 32050fad4b5SAlexander Lobakin } 32150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 32250fad4b5SAlexander Lobakin 323f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 324f450d539SAlexander Lobakin { 325f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 326f450d539SAlexander Lobakin struct sk_buff *skb; 327f450d539SAlexander Lobakin 32849ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 329025a785fSJakub Kicinski nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache, 330f450d539SAlexander Lobakin GFP_ATOMIC, 331f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 332f450d539SAlexander Lobakin nc->skb_cache); 333f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 334f450d539SAlexander Lobakin return NULL; 33549ae83fcSSieng Piaw Liew } 336f450d539SAlexander Lobakin 337f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 338025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, skb); 339f450d539SAlexander Lobakin 340f450d539SAlexander Lobakin return skb; 341f450d539SAlexander Lobakin } 342f450d539SAlexander Lobakin 343ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 344ce098da1SKees Cook unsigned int size) 345ba0509b6SJesper Dangaard Brouer { 346ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 347ba0509b6SJesper Dangaard Brouer 348ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 349ba0509b6SJesper Dangaard Brouer 350ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 351ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 352ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 353ba0509b6SJesper Dangaard Brouer skb->head = data; 354ba0509b6SJesper Dangaard Brouer skb->data = data; 355ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 356763087daSEric Dumazet skb_set_end_offset(skb, size); 357ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 358ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 35968822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 360ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 361ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 362ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 363ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 364ba0509b6SJesper Dangaard Brouer 3656370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 366ba0509b6SJesper Dangaard Brouer } 367ba0509b6SJesper Dangaard Brouer 368ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 369ce098da1SKees Cook unsigned int *size) 370ce098da1SKees Cook { 371ce098da1SKees Cook void *resized; 372ce098da1SKees Cook 373ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */ 374ce098da1SKees Cook *size = ksize(data); 375ce098da1SKees Cook /* krealloc() will immediately return "data" when 376ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper 377ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note 378ce098da1SKees Cook * that this "new" pointer needs to be passed back to the 379ce098da1SKees Cook * caller for use so the __alloc_size hinting will be 380ce098da1SKees Cook * tracked correctly. 381ce098da1SKees Cook */ 382ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC); 383ce098da1SKees Cook WARN_ON_ONCE(resized != data); 384ce098da1SKees Cook return resized; 385ce098da1SKees Cook } 386ce098da1SKees Cook 387ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers. 388ce098da1SKees Cook * Note that this should be used sparingly as slab buffers 389ce098da1SKees Cook * cannot be combined efficiently by GRO! 390ce098da1SKees Cook */ 391ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data) 392ce098da1SKees Cook { 393ce098da1SKees Cook struct sk_buff *skb; 394ce098da1SKees Cook unsigned int size; 395ce098da1SKees Cook 396025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 397ce098da1SKees Cook if (unlikely(!skb)) 398ce098da1SKees Cook return NULL; 399ce098da1SKees Cook 400ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail)); 401ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 402ce098da1SKees Cook __finalize_skb_around(skb, data, size); 403ce098da1SKees Cook 404ce098da1SKees Cook return skb; 405ce098da1SKees Cook } 406ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb); 407ce098da1SKees Cook 408ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */ 409ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data, 410ce098da1SKees Cook unsigned int frag_size) 411ce098da1SKees Cook { 412ce098da1SKees Cook unsigned int size = frag_size; 413ce098da1SKees Cook 414ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers 415ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead. 416ce098da1SKees Cook */ 417ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 418ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 419ce098da1SKees Cook 420ce098da1SKees Cook __finalize_skb_around(skb, data, size); 421ce098da1SKees Cook } 422ce098da1SKees Cook 4231da177e4SLinus Torvalds /** 4242ea2f62cSEric Dumazet * __build_skb - build a network buffer 425b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 426ce098da1SKees Cook * @frag_size: size of data (must not be 0) 427b2b5ce9dSEric Dumazet * 428b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 429ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page 430ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 431ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb() 432ce098da1SKees Cook * instead.) 433b2b5ce9dSEric Dumazet * The return is the new skb buffer. 434b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 435b2b5ce9dSEric Dumazet * Notes : 436b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 437b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 438b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 439b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 440b2b5ce9dSEric Dumazet * before giving packet to stack. 441b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 442b2b5ce9dSEric Dumazet */ 4432ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 444b2b5ce9dSEric Dumazet { 445b2b5ce9dSEric Dumazet struct sk_buff *skb; 446b2b5ce9dSEric Dumazet 447025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 448ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 449b2b5ce9dSEric Dumazet return NULL; 450b2b5ce9dSEric Dumazet 451b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 452483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 453b2b5ce9dSEric Dumazet 454483126b3SAlexander Lobakin return skb; 455b2b5ce9dSEric Dumazet } 4562ea2f62cSEric Dumazet 4572ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 4582ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 4592ea2f62cSEric Dumazet */ 4602ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 4612ea2f62cSEric Dumazet { 4622ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 4632ea2f62cSEric Dumazet 4643c640126SGal Pressman if (likely(skb && frag_size)) { 4652ea2f62cSEric Dumazet skb->head_frag = 1; 466566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 4672ea2f62cSEric Dumazet } 4682ea2f62cSEric Dumazet return skb; 4692ea2f62cSEric Dumazet } 470b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 471b2b5ce9dSEric Dumazet 472ba0509b6SJesper Dangaard Brouer /** 473ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 474ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 475ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 47612c1604aSJakub Kicinski * @frag_size: size of data 477ba0509b6SJesper Dangaard Brouer */ 478ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 479ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 480ba0509b6SJesper Dangaard Brouer { 481ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 482ba0509b6SJesper Dangaard Brouer return NULL; 483ba0509b6SJesper Dangaard Brouer 484483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 485ba0509b6SJesper Dangaard Brouer 486483126b3SAlexander Lobakin if (frag_size) { 487ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 488566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 489ba0509b6SJesper Dangaard Brouer } 490ba0509b6SJesper Dangaard Brouer return skb; 491ba0509b6SJesper Dangaard Brouer } 492ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 493ba0509b6SJesper Dangaard Brouer 494f450d539SAlexander Lobakin /** 495f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 496f450d539SAlexander Lobakin * @data: data buffer provided by caller 49712c1604aSJakub Kicinski * @frag_size: size of data 498f450d539SAlexander Lobakin * 499f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 500f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 501f450d539SAlexander Lobakin * 502f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 503f450d539SAlexander Lobakin */ 504f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 505f450d539SAlexander Lobakin { 506f450d539SAlexander Lobakin struct sk_buff *skb; 507f450d539SAlexander Lobakin 508f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 509f450d539SAlexander Lobakin if (unlikely(!skb)) 510f450d539SAlexander Lobakin return NULL; 511f450d539SAlexander Lobakin 512f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 513f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 514f450d539SAlexander Lobakin 515f450d539SAlexander Lobakin return skb; 516f450d539SAlexander Lobakin } 517f450d539SAlexander Lobakin 518f450d539SAlexander Lobakin /** 519f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 520f450d539SAlexander Lobakin * @data: data buffer provided by caller 52112c1604aSJakub Kicinski * @frag_size: size of data 522f450d539SAlexander Lobakin * 523f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 524f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 525f450d539SAlexander Lobakin * 526f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 527f450d539SAlexander Lobakin */ 528f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 529f450d539SAlexander Lobakin { 530f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 531f450d539SAlexander Lobakin 532f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 533f450d539SAlexander Lobakin skb->head_frag = 1; 534f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 535f450d539SAlexander Lobakin } 536f450d539SAlexander Lobakin 537f450d539SAlexander Lobakin return skb; 538f450d539SAlexander Lobakin } 539f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 540f450d539SAlexander Lobakin 5415381b23dSAlexander Lobakin /* 5425381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 5435381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 5445381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 5455381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 5465381b23dSAlexander Lobakin * memory is free 5475381b23dSAlexander Lobakin */ 5485c0e820cSEric Dumazet static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, 549ef28095fSAlexander Lobakin bool *pfmemalloc) 5505381b23dSAlexander Lobakin { 5515381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 5525c0e820cSEric Dumazet unsigned int obj_size; 5535c0e820cSEric Dumazet void *obj; 5545381b23dSAlexander Lobakin 5555c0e820cSEric Dumazet obj_size = SKB_HEAD_ALIGN(*size); 556bf9f1baaSEric Dumazet if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE && 557bf9f1baaSEric Dumazet !(flags & KMALLOC_NOT_NORMAL_BITS)) { 558bf9f1baaSEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, 559bf9f1baaSEric Dumazet flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 560bf9f1baaSEric Dumazet node); 561bf9f1baaSEric Dumazet *size = SKB_SMALL_HEAD_CACHE_SIZE; 562880ce5f2SEric Dumazet if (obj || !(gfp_pfmemalloc_allowed(flags))) 563bf9f1baaSEric Dumazet goto out; 564880ce5f2SEric Dumazet /* Try again but now we are using pfmemalloc reserves */ 565880ce5f2SEric Dumazet ret_pfmemalloc = true; 566880ce5f2SEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node); 567880ce5f2SEric Dumazet goto out; 568bf9f1baaSEric Dumazet } 5695c0e820cSEric Dumazet *size = obj_size = kmalloc_size_roundup(obj_size); 5705381b23dSAlexander Lobakin /* 5715381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 5725381b23dSAlexander Lobakin * to the reserves, fail. 5735381b23dSAlexander Lobakin */ 5745c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, 5755381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 5765381b23dSAlexander Lobakin node); 5775381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 5785381b23dSAlexander Lobakin goto out; 5795381b23dSAlexander Lobakin 5805381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 5815381b23dSAlexander Lobakin ret_pfmemalloc = true; 5825c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, flags, node); 5835381b23dSAlexander Lobakin 5845381b23dSAlexander Lobakin out: 5855381b23dSAlexander Lobakin if (pfmemalloc) 5865381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5875381b23dSAlexander Lobakin 5885381b23dSAlexander Lobakin return obj; 5895381b23dSAlexander Lobakin } 5905381b23dSAlexander Lobakin 5915381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 5925381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 5935381b23dSAlexander Lobakin * [BEEP] leaks. 5945381b23dSAlexander Lobakin * 5955381b23dSAlexander Lobakin */ 5965381b23dSAlexander Lobakin 5975381b23dSAlexander Lobakin /** 5985381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 5995381b23dSAlexander Lobakin * @size: size to allocate 6005381b23dSAlexander Lobakin * @gfp_mask: allocation mask 6015381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 6025381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 6035381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 6045381b23dSAlexander Lobakin * allocations in case the data is required for writeback 6055381b23dSAlexander Lobakin * @node: numa node to allocate memory on 6065381b23dSAlexander Lobakin * 6075381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 6085381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 6095381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 6105381b23dSAlexander Lobakin * 6115381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 6125381b23dSAlexander Lobakin * %GFP_ATOMIC. 6135381b23dSAlexander Lobakin */ 6145381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 6155381b23dSAlexander Lobakin int flags, int node) 6165381b23dSAlexander Lobakin { 6175381b23dSAlexander Lobakin struct kmem_cache *cache; 6185381b23dSAlexander Lobakin struct sk_buff *skb; 6195381b23dSAlexander Lobakin bool pfmemalloc; 620a5df6333SLi RongQing u8 *data; 6215381b23dSAlexander Lobakin 6225381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 623025a785fSJakub Kicinski ? skbuff_fclone_cache : skbuff_cache; 6245381b23dSAlexander Lobakin 6255381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 6265381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 6275381b23dSAlexander Lobakin 6285381b23dSAlexander Lobakin /* Get the HEAD */ 629d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 630d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 631d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 632d13612b5SAlexander Lobakin else 633d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 634df1ae022SAlexander Lobakin if (unlikely(!skb)) 635df1ae022SAlexander Lobakin return NULL; 6365381b23dSAlexander Lobakin prefetchw(skb); 6375381b23dSAlexander Lobakin 6385381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 6395381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 6405381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 6415381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 6425381b23dSAlexander Lobakin */ 6435c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc); 644df1ae022SAlexander Lobakin if (unlikely(!data)) 6455381b23dSAlexander Lobakin goto nodata; 64612d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 6475381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 6485381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 6495381b23dSAlexander Lobakin */ 65065998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size)); 6515381b23dSAlexander Lobakin 6525381b23dSAlexander Lobakin /* 6535381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 6545381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 6555381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 6565381b23dSAlexander Lobakin */ 6575381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 65865998d2bSEric Dumazet __build_skb_around(skb, data, size); 6595381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 6605381b23dSAlexander Lobakin 6615381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 6625381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 6635381b23dSAlexander Lobakin 6645381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 6655381b23dSAlexander Lobakin 6665381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 6675381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 6685381b23dSAlexander Lobakin } 6695381b23dSAlexander Lobakin 6705381b23dSAlexander Lobakin return skb; 671df1ae022SAlexander Lobakin 6725381b23dSAlexander Lobakin nodata: 6735381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 674df1ae022SAlexander Lobakin return NULL; 6755381b23dSAlexander Lobakin } 6765381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 6775381b23dSAlexander Lobakin 6787ba7aeabSSebastian Andrzej Siewior /** 679fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 680fd11a83dSAlexander Duyck * @dev: network device to receive on 681d7499160SMasanari Iida * @len: length to allocate 682fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 683fd11a83dSAlexander Duyck * 684fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 685fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 686fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 687fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 688fd11a83dSAlexander Duyck * 689fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 690fd11a83dSAlexander Duyck */ 6919451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 6929451980aSAlexander Duyck gfp_t gfp_mask) 693fd11a83dSAlexander Duyck { 694b63ae8caSAlexander Duyck struct page_frag_cache *nc; 695fd11a83dSAlexander Duyck struct sk_buff *skb; 6969451980aSAlexander Duyck bool pfmemalloc; 6979451980aSAlexander Duyck void *data; 698fd11a83dSAlexander Duyck 6999451980aSAlexander Duyck len += NET_SKB_PAD; 700fd11a83dSAlexander Duyck 70166c55602SAlexander Lobakin /* If requested length is either too small or too big, 70266c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 70366c55602SAlexander Lobakin */ 70466c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 70566c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 706d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 707a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 708a080e7bdSAlexander Duyck if (!skb) 709a080e7bdSAlexander Duyck goto skb_fail; 710a080e7bdSAlexander Duyck goto skb_success; 711a080e7bdSAlexander Duyck } 7129451980aSAlexander Duyck 713115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 7149451980aSAlexander Duyck 7159451980aSAlexander Duyck if (sk_memalloc_socks()) 7169451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7179451980aSAlexander Duyck 718afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 7199451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 7208c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 7219451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 72292dcabd7SSebastian Andrzej Siewior } else { 72392dcabd7SSebastian Andrzej Siewior local_bh_disable(); 72492dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 72592dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 72692dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 72792dcabd7SSebastian Andrzej Siewior local_bh_enable(); 72892dcabd7SSebastian Andrzej Siewior } 7299451980aSAlexander Duyck 7309451980aSAlexander Duyck if (unlikely(!data)) 7319451980aSAlexander Duyck return NULL; 7329451980aSAlexander Duyck 7339451980aSAlexander Duyck skb = __build_skb(data, len); 7349451980aSAlexander Duyck if (unlikely(!skb)) { 735181edb2bSAlexander Duyck skb_free_frag(data); 7369451980aSAlexander Duyck return NULL; 7379451980aSAlexander Duyck } 7389451980aSAlexander Duyck 7399451980aSAlexander Duyck if (pfmemalloc) 7409451980aSAlexander Duyck skb->pfmemalloc = 1; 7419451980aSAlexander Duyck skb->head_frag = 1; 7429451980aSAlexander Duyck 743a080e7bdSAlexander Duyck skb_success: 7448af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 7457b2e497aSChristoph Hellwig skb->dev = dev; 746fd11a83dSAlexander Duyck 747a080e7bdSAlexander Duyck skb_fail: 7488af27456SChristoph Hellwig return skb; 7498af27456SChristoph Hellwig } 750b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 7511da177e4SLinus Torvalds 752fd11a83dSAlexander Duyck /** 753fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 754fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 755d7499160SMasanari Iida * @len: length to allocate 756fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 757fd11a83dSAlexander Duyck * 758fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 759fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 760fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 761fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 762fd11a83dSAlexander Duyck * 763fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 764fd11a83dSAlexander Duyck */ 7659451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 7669451980aSAlexander Duyck gfp_t gfp_mask) 767fd11a83dSAlexander Duyck { 7683226b158SEric Dumazet struct napi_alloc_cache *nc; 769fd11a83dSAlexander Duyck struct sk_buff *skb; 770dbae2b06SPaolo Abeni bool pfmemalloc; 7719451980aSAlexander Duyck void *data; 772fd11a83dSAlexander Duyck 773ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 7749451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 775fd11a83dSAlexander Duyck 7763226b158SEric Dumazet /* If requested length is either too small or too big, 7773226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 778dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 779dbae2b06SPaolo Abeni * for small fragments 7803226b158SEric Dumazet */ 781dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7823226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 783d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 784cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 785cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 786a080e7bdSAlexander Duyck if (!skb) 787a080e7bdSAlexander Duyck goto skb_fail; 788a080e7bdSAlexander Duyck goto skb_success; 789a080e7bdSAlexander Duyck } 7909451980aSAlexander Duyck 7913226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 7929451980aSAlexander Duyck 7939451980aSAlexander Duyck if (sk_memalloc_socks()) 7949451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7959451980aSAlexander Duyck 796dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 797dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 798dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 799dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 800dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 801dbae2b06SPaolo Abeni * size above 512 bytes 802dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 803dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 804dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 805dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 806dbae2b06SPaolo Abeni */ 807dbae2b06SPaolo Abeni len = SZ_1K; 808dbae2b06SPaolo Abeni 809dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 810dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 811dbae2b06SPaolo Abeni } else { 812115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 813dbae2b06SPaolo Abeni 8148c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 815dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 816dbae2b06SPaolo Abeni } 817dbae2b06SPaolo Abeni 8189451980aSAlexander Duyck if (unlikely(!data)) 8199451980aSAlexander Duyck return NULL; 8209451980aSAlexander Duyck 821cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 8229451980aSAlexander Duyck if (unlikely(!skb)) { 823181edb2bSAlexander Duyck skb_free_frag(data); 8249451980aSAlexander Duyck return NULL; 8259451980aSAlexander Duyck } 8269451980aSAlexander Duyck 827dbae2b06SPaolo Abeni if (pfmemalloc) 8289451980aSAlexander Duyck skb->pfmemalloc = 1; 8299451980aSAlexander Duyck skb->head_frag = 1; 8309451980aSAlexander Duyck 831a080e7bdSAlexander Duyck skb_success: 832fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 833fd11a83dSAlexander Duyck skb->dev = napi->dev; 834fd11a83dSAlexander Duyck 835a080e7bdSAlexander Duyck skb_fail: 836fd11a83dSAlexander Duyck return skb; 837fd11a83dSAlexander Duyck } 838fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 839fd11a83dSAlexander Duyck 840654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 84150269e19SEric Dumazet int size, unsigned int truesize) 842654bed16SPeter Zijlstra { 843654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 844654bed16SPeter Zijlstra skb->len += size; 845654bed16SPeter Zijlstra skb->data_len += size; 84650269e19SEric Dumazet skb->truesize += truesize; 847654bed16SPeter Zijlstra } 848654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 849654bed16SPeter Zijlstra 850f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 851f8e617e1SJason Wang unsigned int truesize) 852f8e617e1SJason Wang { 853f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 854f8e617e1SJason Wang 855f8e617e1SJason Wang skb_frag_size_add(frag, size); 856f8e617e1SJason Wang skb->len += size; 857f8e617e1SJason Wang skb->data_len += size; 858f8e617e1SJason Wang skb->truesize += truesize; 859f8e617e1SJason Wang } 860f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 861f8e617e1SJason Wang 86227b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 8631da177e4SLinus Torvalds { 864bd8a7036SEric Dumazet kfree_skb_list(*listp); 86527b437c8SHerbert Xu *listp = NULL; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds 86827b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 86927b437c8SHerbert Xu { 87027b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 87127b437c8SHerbert Xu } 87227b437c8SHerbert Xu 8731da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds struct sk_buff *list; 8761da177e4SLinus Torvalds 877fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 8781da177e4SLinus Torvalds skb_get(list); 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 881b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) 8824727bab4SYunsheng Lin { 8834727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 8844727bab4SYunsheng Lin return false; 8858c48eea3SJakub Kicinski return page_pool_return_skb_page(virt_to_page(data), napi_safe); 8864727bab4SYunsheng Lin } 8874727bab4SYunsheng Lin 888bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset) 889bf9f1baaSEric Dumazet { 890bf9f1baaSEric Dumazet if (end_offset == SKB_SMALL_HEAD_HEADROOM) 891bf9f1baaSEric Dumazet kmem_cache_free(skb_small_head_cache, head); 892bf9f1baaSEric Dumazet else 893bf9f1baaSEric Dumazet kfree(head); 894bf9f1baaSEric Dumazet } 895bf9f1baaSEric Dumazet 896b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe) 897d3836f21SEric Dumazet { 898181edb2bSAlexander Duyck unsigned char *head = skb->head; 899181edb2bSAlexander Duyck 9006a5bcd84SIlias Apalodimas if (skb->head_frag) { 901b07a2d97SJakub Kicinski if (skb_pp_recycle(skb, head, napi_safe)) 9026a5bcd84SIlias Apalodimas return; 903181edb2bSAlexander Duyck skb_free_frag(head); 9046a5bcd84SIlias Apalodimas } else { 905bf9f1baaSEric Dumazet skb_kfree_head(head, skb_end_offset(skb)); 906d3836f21SEric Dumazet } 9076a5bcd84SIlias Apalodimas } 908d3836f21SEric Dumazet 909b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason, 910b07a2d97SJakub Kicinski bool napi_safe) 9111da177e4SLinus Torvalds { 912ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 9131da177e4SLinus Torvalds int i; 914ff04a771SEric Dumazet 915ff04a771SEric Dumazet if (skb->cloned && 916ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 917ff04a771SEric Dumazet &shinfo->dataref)) 9182cc3aeb5SIlias Apalodimas goto exit; 919ff04a771SEric Dumazet 920753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 921753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 922753f1ca4SPavel Begunkov 92370c43167SJonathan Lemon skb_zcopy_clear(skb, true); 924753f1ca4SPavel Begunkov if (skip_unref) 925753f1ca4SPavel Begunkov goto free_head; 926753f1ca4SPavel Begunkov } 92770c43167SJonathan Lemon 928ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 9298c48eea3SJakub Kicinski napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe); 9301da177e4SLinus Torvalds 931753f1ca4SPavel Begunkov free_head: 932ff04a771SEric Dumazet if (shinfo->frag_list) 933511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 9341da177e4SLinus Torvalds 935b07a2d97SJakub Kicinski skb_free_head(skb, napi_safe); 9362cc3aeb5SIlias Apalodimas exit: 9372cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 9382cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 9392cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 9402cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 9412cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 94258e61e41STom Rix * additional references to the fragmented part of the SKB. 9432cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 9442cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 9452cc3aeb5SIlias Apalodimas */ 9462cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 9471da177e4SLinus Torvalds } 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 9511da177e4SLinus Torvalds */ 9522d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 9531da177e4SLinus Torvalds { 954d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 955d179cd12SDavid S. Miller 956d179cd12SDavid S. Miller switch (skb->fclone) { 957d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 958025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 9596ffe75ebSEric Dumazet return; 960d179cd12SDavid S. Miller 961d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 962d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 9636ffe75ebSEric Dumazet 9646ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 9656ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 9666ffe75ebSEric Dumazet * while here, branch prediction will be good. 9676ffe75ebSEric Dumazet */ 9682638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 9696ffe75ebSEric Dumazet goto fastpath; 970d179cd12SDavid S. Miller break; 971d179cd12SDavid S. Miller 9726ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 973d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 974d179cd12SDavid S. Miller break; 9753ff50b79SStephen Hemminger } 9762638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 9776ffe75ebSEric Dumazet return; 9786ffe75ebSEric Dumazet fastpath: 9796ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9820a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 9831da177e4SLinus Torvalds { 984adf30907SEric Dumazet skb_dst_drop(skb); 9851da177e4SLinus Torvalds if (skb->destructor) { 9867890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 9871da177e4SLinus Torvalds skb->destructor(skb); 9881da177e4SLinus Torvalds } 989a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 990cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 9912fc72c7bSKOVACS Krisztian #endif 992df5042f4SFlorian Westphal skb_ext_put(skb); 99304a4bb55SLennert Buytenhek } 99404a4bb55SLennert Buytenhek 99504a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 996b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason, 997b07a2d97SJakub Kicinski bool napi_safe) 99804a4bb55SLennert Buytenhek { 99904a4bb55SLennert Buytenhek skb_release_head_state(skb); 1000a28b1b90SFlorian Westphal if (likely(skb->head)) 1001b07a2d97SJakub Kicinski skb_release_data(skb, reason, napi_safe); 10022d4baff8SHerbert Xu } 10031da177e4SLinus Torvalds 10042d4baff8SHerbert Xu /** 10052d4baff8SHerbert Xu * __kfree_skb - private function 10062d4baff8SHerbert Xu * @skb: buffer 10072d4baff8SHerbert Xu * 10082d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 10092d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 10102d4baff8SHerbert Xu * always call kfree_skb 10112d4baff8SHerbert Xu */ 10122d4baff8SHerbert Xu 10132d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 10142d4baff8SHerbert Xu { 1015b07a2d97SJakub Kicinski skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false); 10161da177e4SLinus Torvalds kfree_skbmem(skb); 10171da177e4SLinus Torvalds } 1018b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 10191da177e4SLinus Torvalds 1020a4650da2SJesper Dangaard Brouer static __always_inline 1021a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1022a4650da2SJesper Dangaard Brouer { 1023a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 1024a4650da2SJesper Dangaard Brouer return false; 1025a4650da2SJesper Dangaard Brouer 1026071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || 1027071c0fc6SJohannes Berg u32_get_bits(reason, 1028071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >= 1029071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_NUM); 1030a4650da2SJesper Dangaard Brouer 1031a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 1032dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1033a4650da2SJesper Dangaard Brouer else 1034a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 1035a4650da2SJesper Dangaard Brouer return true; 1036a4650da2SJesper Dangaard Brouer } 1037a4650da2SJesper Dangaard Brouer 10381da177e4SLinus Torvalds /** 1039c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 1040231d06aeSJörn Engel * @skb: buffer to free 1041c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 1042231d06aeSJörn Engel * 1043231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 1044c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 1045c504e5c2SMenglong Dong * tracepoint. 1046231d06aeSJörn Engel */ 1047c205cc75SMenglong Dong void __fix_address 1048c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1049231d06aeSJörn Engel { 1050a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 1051231d06aeSJörn Engel __kfree_skb(skb); 1052231d06aeSJörn Engel } 1053c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 1054231d06aeSJörn Engel 1055eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 1056eedade12SJesper Dangaard Brouer 1057eedade12SJesper Dangaard Brouer struct skb_free_array { 1058eedade12SJesper Dangaard Brouer unsigned int skb_count; 1059eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 1060eedade12SJesper Dangaard Brouer }; 1061eedade12SJesper Dangaard Brouer 1062eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 1063eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 1064eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 1065eedade12SJesper Dangaard Brouer { 1066eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1067eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 1068eedade12SJesper Dangaard Brouer __kfree_skb(skb); 1069eedade12SJesper Dangaard Brouer return; 1070eedade12SJesper Dangaard Brouer } 1071eedade12SJesper Dangaard Brouer 1072b07a2d97SJakub Kicinski skb_release_all(skb, reason, false); 1073eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 1074eedade12SJesper Dangaard Brouer 1075eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 1076025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE, 1077eedade12SJesper Dangaard Brouer sa->skb_array); 1078eedade12SJesper Dangaard Brouer sa->skb_count = 0; 1079eedade12SJesper Dangaard Brouer } 1080eedade12SJesper Dangaard Brouer } 1081eedade12SJesper Dangaard Brouer 1082a4650da2SJesper Dangaard Brouer void __fix_address 1083a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 1084bd8a7036SEric Dumazet { 1085eedade12SJesper Dangaard Brouer struct skb_free_array sa; 1086eedade12SJesper Dangaard Brouer 1087eedade12SJesper Dangaard Brouer sa.skb_count = 0; 1088eedade12SJesper Dangaard Brouer 1089bd8a7036SEric Dumazet while (segs) { 1090bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 1091bd8a7036SEric Dumazet 10929dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) { 10939dde0cd3SJesper Dangaard Brouer skb_poison_list(segs); 1094eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 10959dde0cd3SJesper Dangaard Brouer } 1096eedade12SJesper Dangaard Brouer 1097bd8a7036SEric Dumazet segs = next; 1098bd8a7036SEric Dumazet } 1099eedade12SJesper Dangaard Brouer 1100eedade12SJesper Dangaard Brouer if (sa.skb_count) 1101025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array); 1102bd8a7036SEric Dumazet } 1103215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1104bd8a7036SEric Dumazet 11056413139dSWillem de Bruijn /* Dump skb information and contents. 11066413139dSWillem de Bruijn * 11076413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 11086413139dSWillem de Bruijn * 1109302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 11106413139dSWillem de Bruijn */ 11116413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 11126413139dSWillem de Bruijn { 11136413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 11146413139dSWillem de Bruijn struct net_device *dev = skb->dev; 11156413139dSWillem de Bruijn struct sock *sk = skb->sk; 11166413139dSWillem de Bruijn struct sk_buff *list_skb; 11176413139dSWillem de Bruijn bool has_mac, has_trans; 11186413139dSWillem de Bruijn int headroom, tailroom; 11196413139dSWillem de Bruijn int i, len, seg_len; 11206413139dSWillem de Bruijn 11216413139dSWillem de Bruijn if (full_pkt) 11226413139dSWillem de Bruijn len = skb->len; 11236413139dSWillem de Bruijn else 11246413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 11256413139dSWillem de Bruijn 11266413139dSWillem de Bruijn headroom = skb_headroom(skb); 11276413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 11286413139dSWillem de Bruijn 11296413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 11306413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 11316413139dSWillem de Bruijn 11326413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 11336413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 11346413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 11356413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 11366413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 11376413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 11386413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 11396413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 11406413139dSWillem de Bruijn skb->network_header, 11416413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 11426413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 11436413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 11446413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 11456413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 11466413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 11476413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 11486413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 11496413139dSWillem de Bruijn 11506413139dSWillem de Bruijn if (dev) 11518a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 11526413139dSWillem de Bruijn level, dev->name, &dev->features); 11536413139dSWillem de Bruijn if (sk) 1154db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 11556413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 11566413139dSWillem de Bruijn 11576413139dSWillem de Bruijn if (full_pkt && headroom) 11586413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 11596413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 11606413139dSWillem de Bruijn 11616413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 11626413139dSWillem de Bruijn if (seg_len) 11636413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 11646413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 11656413139dSWillem de Bruijn len -= seg_len; 11666413139dSWillem de Bruijn 11676413139dSWillem de Bruijn if (full_pkt && tailroom) 11686413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 11696413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 11706413139dSWillem de Bruijn 11716413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 11726413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 11736413139dSWillem de Bruijn u32 p_off, p_len, copied; 11746413139dSWillem de Bruijn struct page *p; 11756413139dSWillem de Bruijn u8 *vaddr; 11766413139dSWillem de Bruijn 1177b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 11786413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 11796413139dSWillem de Bruijn copied) { 11806413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 11816413139dSWillem de Bruijn vaddr = kmap_atomic(p); 11826413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 11836413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 11846413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 11856413139dSWillem de Bruijn kunmap_atomic(vaddr); 11866413139dSWillem de Bruijn len -= seg_len; 11876413139dSWillem de Bruijn if (!len) 11886413139dSWillem de Bruijn break; 11896413139dSWillem de Bruijn } 11906413139dSWillem de Bruijn } 11916413139dSWillem de Bruijn 11926413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 11936413139dSWillem de Bruijn printk("skb fraglist:\n"); 11946413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 11956413139dSWillem de Bruijn skb_dump(level, list_skb, true); 11966413139dSWillem de Bruijn } 11976413139dSWillem de Bruijn } 11986413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 11996413139dSWillem de Bruijn 1200d1a203eaSStephen Hemminger /** 120125121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 120225121173SMichael S. Tsirkin * @skb: buffer that triggered an error 120325121173SMichael S. Tsirkin * 120425121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 120525121173SMichael S. Tsirkin * skb must be freed afterwards. 120625121173SMichael S. Tsirkin */ 120725121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 120825121173SMichael S. Tsirkin { 1209753f1ca4SPavel Begunkov if (skb) { 1210753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 12111f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 121225121173SMichael S. Tsirkin } 1213753f1ca4SPavel Begunkov } 121425121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 121525121173SMichael S. Tsirkin 1216be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 121725121173SMichael S. Tsirkin /** 1218ead2ceb0SNeil Horman * consume_skb - free an skbuff 1219ead2ceb0SNeil Horman * @skb: buffer to free 1220ead2ceb0SNeil Horman * 1221ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1222ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1223ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1224ead2ceb0SNeil Horman */ 1225ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1226ead2ceb0SNeil Horman { 12273889a803SPaolo Abeni if (!skb_unref(skb)) 1228ead2ceb0SNeil Horman return; 12293889a803SPaolo Abeni 1230dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1231ead2ceb0SNeil Horman __kfree_skb(skb); 1232ead2ceb0SNeil Horman } 1233ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1234be769db2SHerbert Xu #endif 1235ead2ceb0SNeil Horman 12360a463c78SPaolo Abeni /** 1237c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 12380a463c78SPaolo Abeni * @skb: buffer to free 12390a463c78SPaolo Abeni * 1240ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1241ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 12420a463c78SPaolo Abeni */ 1243ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 12440a463c78SPaolo Abeni { 1245dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1246b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 12470a463c78SPaolo Abeni kfree_skbmem(skb); 12480a463c78SPaolo Abeni } 12490a463c78SPaolo Abeni 1250f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1251795bb1c0SJesper Dangaard Brouer { 1252795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1253f450d539SAlexander Lobakin u32 i; 1254795bb1c0SJesper Dangaard Brouer 1255025a785fSJakub Kicinski kasan_poison_object_data(skbuff_cache, skb); 1256795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1257795bb1c0SJesper Dangaard Brouer 1258795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1259f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1260025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, 1261f450d539SAlexander Lobakin nc->skb_cache[i]); 1262f450d539SAlexander Lobakin 1263025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF, 1264f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1265f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1266795bb1c0SJesper Dangaard Brouer } 1267795bb1c0SJesper Dangaard Brouer } 1268f450d539SAlexander Lobakin 12698fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason) 127015fad714SJesper Dangaard Brouer { 12718fa66e4aSJakub Kicinski skb_release_all(skb, reason, true); 12729243adfcSAlexander Lobakin napi_skb_cache_put(skb); 12739243adfcSAlexander Lobakin } 12749243adfcSAlexander Lobakin 12759243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 12769243adfcSAlexander Lobakin { 12779efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 12788550ff8dSPaul Blakey nf_reset_ct(skb); 12799243adfcSAlexander Lobakin skb_dst_drop(skb); 12809243adfcSAlexander Lobakin skb_ext_put(skb); 12815e10da53SPaolo Abeni skb_orphan(skb); 12829efb4b5bSPaolo Abeni skb->slow_gro = 0; 12839efb4b5bSPaolo Abeni } 1284f450d539SAlexander Lobakin napi_skb_cache_put(skb); 128515fad714SJesper Dangaard Brouer } 1286795bb1c0SJesper Dangaard Brouer 1287795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1288795bb1c0SJesper Dangaard Brouer { 1289885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1290795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1291885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1292795bb1c0SJesper Dangaard Brouer return; 1293795bb1c0SJesper Dangaard Brouer } 1294795bb1c0SJesper Dangaard Brouer 1295ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 12966454eca8SYunsheng Lin 12977608894eSPaolo Abeni if (!skb_unref(skb)) 1298795bb1c0SJesper Dangaard Brouer return; 12997608894eSPaolo Abeni 1300795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1301dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1302795bb1c0SJesper Dangaard Brouer 1303795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1304abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1305795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1306795bb1c0SJesper Dangaard Brouer return; 1307795bb1c0SJesper Dangaard Brouer } 1308795bb1c0SJesper Dangaard Brouer 1309b07a2d97SJakub Kicinski skb_release_all(skb, SKB_CONSUMED, !!budget); 1310f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1311795bb1c0SJesper Dangaard Brouer } 1312795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1313795bb1c0SJesper Dangaard Brouer 131403f61041SKees Cook /* Make sure a field is contained by headers group */ 1315b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 131603f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 131703f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1318b1937227SEric Dumazet 1319dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1320dec18810SHerbert Xu { 1321dec18810SHerbert Xu new->tstamp = old->tstamp; 1322b1937227SEric Dumazet /* We do not copy old->sk */ 1323dec18810SHerbert Xu new->dev = old->dev; 1324b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 13257fee226aSEric Dumazet skb_dst_copy(new, old); 1326df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1327b1937227SEric Dumazet __nf_copy(new, old, false); 13286aa895b0SPatrick McHardy 132903f61041SKees Cook /* Note : this field could be in the headers group. 1330b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1331b1937227SEric Dumazet */ 1332b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 133306021292SEliezer Tamir 133403f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1335b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1336b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1337b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1338b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1339b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1340b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1341b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1342b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1343b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1344b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1345b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1346b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1347b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1348b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1349b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1350b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1351b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1352b1937227SEric Dumazet #endif 1353e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1354b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 135506021292SEliezer Tamir #endif 135668822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 13572bd82484SEric Dumazet #ifdef CONFIG_XPS 13582bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 13592bd82484SEric Dumazet #endif 1360b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1361b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1362b1937227SEric Dumazet #endif 1363b1937227SEric Dumazet 1364dec18810SHerbert Xu } 1365dec18810SHerbert Xu 136682c49a35SHerbert Xu /* 136782c49a35SHerbert Xu * You should not add any new code to this function. Add it to 136882c49a35SHerbert Xu * __copy_skb_header above instead. 136982c49a35SHerbert Xu */ 1370e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 13711da177e4SLinus Torvalds { 13721da177e4SLinus Torvalds #define C(x) n->x = skb->x 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds n->next = n->prev = NULL; 13751da177e4SLinus Torvalds n->sk = NULL; 1376dec18810SHerbert Xu __copy_skb_header(n, skb); 1377dec18810SHerbert Xu 13781da177e4SLinus Torvalds C(len); 13791da177e4SLinus Torvalds C(data_len); 13803e6b3b2eSAlexey Dobriyan C(mac_len); 1381334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 138202f1c89dSPaul Moore n->cloned = 1; 13831da177e4SLinus Torvalds n->nohdr = 0; 1384b13dda9fSEric Dumazet n->peeked = 0; 1385e78bfb07SStefano Brivio C(pfmemalloc); 13866a5bcd84SIlias Apalodimas C(pp_recycle); 13871da177e4SLinus Torvalds n->destructor = NULL; 13881da177e4SLinus Torvalds C(tail); 13891da177e4SLinus Torvalds C(end); 139002f1c89dSPaul Moore C(head); 1391d3836f21SEric Dumazet C(head_frag); 139202f1c89dSPaul Moore C(data); 139302f1c89dSPaul Moore C(truesize); 139463354797SReshetova, Elena refcount_set(&n->users, 1); 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 13971da177e4SLinus Torvalds skb->cloned = 1; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds return n; 1400e0053ec0SHerbert Xu #undef C 1401e0053ec0SHerbert Xu } 1402e0053ec0SHerbert Xu 1403e0053ec0SHerbert Xu /** 1404da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1405da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1406da29e4b4SJakub Kicinski */ 1407da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1408da29e4b4SJakub Kicinski { 1409da29e4b4SJakub Kicinski struct sk_buff *n; 1410da29e4b4SJakub Kicinski 1411da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1412da29e4b4SJakub Kicinski if (!n) 1413da29e4b4SJakub Kicinski return NULL; 1414da29e4b4SJakub Kicinski 1415da29e4b4SJakub Kicinski n->len = first->len; 1416da29e4b4SJakub Kicinski n->data_len = first->len; 1417da29e4b4SJakub Kicinski n->truesize = first->truesize; 1418da29e4b4SJakub Kicinski 1419da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1420da29e4b4SJakub Kicinski 1421da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1422da29e4b4SJakub Kicinski n->destructor = NULL; 1423da29e4b4SJakub Kicinski 1424da29e4b4SJakub Kicinski return n; 1425da29e4b4SJakub Kicinski } 1426da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1427da29e4b4SJakub Kicinski 1428da29e4b4SJakub Kicinski /** 1429e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1430e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1431e0053ec0SHerbert Xu * @src: the skb to supply the contents 1432e0053ec0SHerbert Xu * 1433e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1434e0053ec0SHerbert Xu * supplied by the user. 1435e0053ec0SHerbert Xu * 1436e0053ec0SHerbert Xu * The target skb is returned upon exit. 1437e0053ec0SHerbert Xu */ 1438e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1439e0053ec0SHerbert Xu { 1440b07a2d97SJakub Kicinski skb_release_all(dst, SKB_CONSUMED, false); 1441e0053ec0SHerbert Xu return __skb_clone(dst, src); 1442e0053ec0SHerbert Xu } 1443e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1444e0053ec0SHerbert Xu 14456f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1446a91dbff5SWillem de Bruijn { 144714ade6baSWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg, rlim; 1448a91dbff5SWillem de Bruijn struct user_struct *user; 1449a91dbff5SWillem de Bruijn 1450a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1451a91dbff5SWillem de Bruijn return 0; 1452a91dbff5SWillem de Bruijn 145314ade6baSWillem de Bruijn rlim = rlimit(RLIMIT_MEMLOCK); 145414ade6baSWillem de Bruijn if (rlim == RLIM_INFINITY) 145514ade6baSWillem de Bruijn return 0; 145614ade6baSWillem de Bruijn 1457a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 145814ade6baSWillem de Bruijn max_pg = rlim >> PAGE_SHIFT; 1459a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1460a91dbff5SWillem de Bruijn 1461a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 146257fc05e8SEric Dumazet do { 1463a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1464a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1465a91dbff5SWillem de Bruijn return -ENOBUFS; 146657fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1467a91dbff5SWillem de Bruijn 1468a91dbff5SWillem de Bruijn if (!mmp->user) { 1469a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1470a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1471a91dbff5SWillem de Bruijn } else { 1472a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1473a91dbff5SWillem de Bruijn } 1474a91dbff5SWillem de Bruijn 1475a91dbff5SWillem de Bruijn return 0; 1476a91dbff5SWillem de Bruijn } 14776f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1478a91dbff5SWillem de Bruijn 14796f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1480a91dbff5SWillem de Bruijn { 1481a91dbff5SWillem de Bruijn if (mmp->user) { 1482a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1483a91dbff5SWillem de Bruijn free_uid(mmp->user); 1484a91dbff5SWillem de Bruijn } 1485a91dbff5SWillem de Bruijn } 14866f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1487a91dbff5SWillem de Bruijn 1488c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 148952267790SWillem de Bruijn { 1490e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 149152267790SWillem de Bruijn struct sk_buff *skb; 149252267790SWillem de Bruijn 149352267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 149452267790SWillem de Bruijn 149552267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 149652267790SWillem de Bruijn if (!skb) 149752267790SWillem de Bruijn return NULL; 149852267790SWillem de Bruijn 149952267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 150052267790SWillem de Bruijn uarg = (void *)skb->cb; 1501a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1502a91dbff5SWillem de Bruijn 1503a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1504a91dbff5SWillem de Bruijn kfree_skb(skb); 1505a91dbff5SWillem de Bruijn return NULL; 1506a91dbff5SWillem de Bruijn } 150752267790SWillem de Bruijn 1508e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 15094ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 15104ab6c99dSWillem de Bruijn uarg->len = 1; 15114ab6c99dSWillem de Bruijn uarg->bytelen = size; 151252267790SWillem de Bruijn uarg->zerocopy = 1; 1513e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1514e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 151552267790SWillem de Bruijn sock_hold(sk); 151652267790SWillem de Bruijn 1517e7d2b510SPavel Begunkov return &uarg->ubuf; 151852267790SWillem de Bruijn } 151952267790SWillem de Bruijn 1520e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 152152267790SWillem de Bruijn { 152252267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 152352267790SWillem de Bruijn } 152452267790SWillem de Bruijn 15258c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 15264ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 15274ab6c99dSWillem de Bruijn { 15284ab6c99dSWillem de Bruijn if (uarg) { 1529e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 15304ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 15314ab6c99dSWillem de Bruijn u32 bytelen, next; 15324ab6c99dSWillem de Bruijn 15331b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 15341b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 15351b4b2b09SPavel Begunkov return NULL; 15361b4b2b09SPavel Begunkov 15374ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 15384ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 15394ab6c99dSWillem de Bruijn */ 15404ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 15414ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 15424ab6c99dSWillem de Bruijn return NULL; 15434ab6c99dSWillem de Bruijn } 15444ab6c99dSWillem de Bruijn 1545e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1546e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1547e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 15484ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 15494ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15504ab6c99dSWillem de Bruijn goto new_alloc; 15514ab6c99dSWillem de Bruijn return NULL; 15524ab6c99dSWillem de Bruijn } 15534ab6c99dSWillem de Bruijn 15544ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1555e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1556e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1557a91dbff5SWillem de Bruijn return NULL; 1558e7d2b510SPavel Begunkov uarg_zc->len++; 1559e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 15604ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1561100f6d8eSWillem de Bruijn 1562100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1563100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15648e044917SJonathan Lemon net_zcopy_get(uarg); 1565100f6d8eSWillem de Bruijn 15664ab6c99dSWillem de Bruijn return uarg; 15674ab6c99dSWillem de Bruijn } 15684ab6c99dSWillem de Bruijn } 15694ab6c99dSWillem de Bruijn 15704ab6c99dSWillem de Bruijn new_alloc: 15718c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 15724ab6c99dSWillem de Bruijn } 15738c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 15744ab6c99dSWillem de Bruijn 15754ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 15764ab6c99dSWillem de Bruijn { 15774ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 15784ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 15794ab6c99dSWillem de Bruijn u64 sum_len; 15804ab6c99dSWillem de Bruijn 15814ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 15824ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 15834ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 15844ab6c99dSWillem de Bruijn 15854ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 15864ab6c99dSWillem de Bruijn return false; 15874ab6c99dSWillem de Bruijn 15884ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 15894ab6c99dSWillem de Bruijn return false; 15904ab6c99dSWillem de Bruijn 15914ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 15924ab6c99dSWillem de Bruijn return true; 15934ab6c99dSWillem de Bruijn } 15944ab6c99dSWillem de Bruijn 1595e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 159652267790SWillem de Bruijn { 15974ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 159852267790SWillem de Bruijn struct sock_exterr_skb *serr; 159952267790SWillem de Bruijn struct sock *sk = skb->sk; 16004ab6c99dSWillem de Bruijn struct sk_buff_head *q; 16014ab6c99dSWillem de Bruijn unsigned long flags; 16023bdd5ee0SWillem de Bruijn bool is_zerocopy; 16034ab6c99dSWillem de Bruijn u32 lo, hi; 16044ab6c99dSWillem de Bruijn u16 len; 160552267790SWillem de Bruijn 1606ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1607ccaffff1SWillem de Bruijn 16084ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 16094ab6c99dSWillem de Bruijn * so do not queue a completion notification 16104ab6c99dSWillem de Bruijn */ 16114ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 161252267790SWillem de Bruijn goto release; 161352267790SWillem de Bruijn 16144ab6c99dSWillem de Bruijn len = uarg->len; 16154ab6c99dSWillem de Bruijn lo = uarg->id; 16164ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 16173bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 16184ab6c99dSWillem de Bruijn 161952267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 162052267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 162152267790SWillem de Bruijn serr->ee.ee_errno = 0; 162252267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 16234ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 16244ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 16253bdd5ee0SWillem de Bruijn if (!is_zerocopy) 162652267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 162752267790SWillem de Bruijn 16284ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 16294ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 16304ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 16314ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 16324ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 16334ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 163452267790SWillem de Bruijn skb = NULL; 16354ab6c99dSWillem de Bruijn } 16364ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 163752267790SWillem de Bruijn 1638e3ae2365SAlexander Aring sk_error_report(sk); 163952267790SWillem de Bruijn 164052267790SWillem de Bruijn release: 164152267790SWillem de Bruijn consume_skb(skb); 164252267790SWillem de Bruijn sock_put(sk); 164352267790SWillem de Bruijn } 164475518851SJonathan Lemon 16458c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 164636177832SJonathan Lemon bool success) 164775518851SJonathan Lemon { 1648e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1649e7d2b510SPavel Begunkov 1650e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 165175518851SJonathan Lemon 165275518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1653e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 165475518851SJonathan Lemon } 16558c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 165652267790SWillem de Bruijn 16578c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 165852267790SWillem de Bruijn { 1659e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 166052267790SWillem de Bruijn 166152267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1662e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 166352267790SWillem de Bruijn 166452900d22SWillem de Bruijn if (have_uref) 16658c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 166652267790SWillem de Bruijn } 16678c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 166852267790SWillem de Bruijn 166952267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 167052267790SWillem de Bruijn struct msghdr *msg, int len, 167152267790SWillem de Bruijn struct ubuf_info *uarg) 167252267790SWillem de Bruijn { 16734ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 167452267790SWillem de Bruijn int err, orig_len = skb->len; 167552267790SWillem de Bruijn 16764ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 16774ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 16784ab6c99dSWillem de Bruijn */ 16794ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 16804ab6c99dSWillem de Bruijn return -EEXIST; 16814ab6c99dSWillem de Bruijn 1682ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 168352267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 168454d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 168554d43117SWillem de Bruijn 168652267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1687829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 168854d43117SWillem de Bruijn skb->sk = sk; 168952267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 169054d43117SWillem de Bruijn skb->sk = save_sk; 169152267790SWillem de Bruijn return err; 169252267790SWillem de Bruijn } 169352267790SWillem de Bruijn 169452900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 169552267790SWillem de Bruijn return skb->len - orig_len; 169652267790SWillem de Bruijn } 169752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 169852267790SWillem de Bruijn 1699753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1700753f1ca4SPavel Begunkov { 1701753f1ca4SPavel Begunkov int i; 1702753f1ca4SPavel Begunkov 1703753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1704753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1705753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1706753f1ca4SPavel Begunkov } 1707753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1708753f1ca4SPavel Begunkov 17091f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 171052267790SWillem de Bruijn gfp_t gfp_mask) 171152267790SWillem de Bruijn { 171252267790SWillem de Bruijn if (skb_zcopy(orig)) { 171352267790SWillem de Bruijn if (skb_zcopy(nskb)) { 171452267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 171552267790SWillem de Bruijn if (!gfp_mask) { 171652267790SWillem de Bruijn WARN_ON_ONCE(1); 171752267790SWillem de Bruijn return -ENOMEM; 171852267790SWillem de Bruijn } 171952267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 172052267790SWillem de Bruijn return 0; 172152267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 172252267790SWillem de Bruijn return -EIO; 172352267790SWillem de Bruijn } 172452900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 172552267790SWillem de Bruijn } 172652267790SWillem de Bruijn return 0; 172752267790SWillem de Bruijn } 172852267790SWillem de Bruijn 17292c53040fSBen Hutchings /** 17302c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 173148c83012SMichael S. Tsirkin * @skb: the skb to modify 173248c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 173348c83012SMichael S. Tsirkin * 173406b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 173548c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 173648c83012SMichael S. Tsirkin * to userspace pages. 173748c83012SMichael S. Tsirkin * 173848c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 173948c83012SMichael S. Tsirkin * %GFP_ATOMIC. 174048c83012SMichael S. Tsirkin * 174148c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 174248c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 174348c83012SMichael S. Tsirkin */ 174448c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1745a6686f2fSShirley Ma { 1746a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1747a6686f2fSShirley Ma struct page *page, *head = NULL; 17487e692df3SEric Dumazet int i, order, psize, new_frags; 17493ece7826SWillem de Bruijn u32 d_off; 1750a6686f2fSShirley Ma 17513ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 17523ece7826SWillem de Bruijn return -EINVAL; 17533ece7826SWillem de Bruijn 1754f72c4ac6SWillem de Bruijn if (!num_frags) 1755f72c4ac6SWillem de Bruijn goto release; 1756f72c4ac6SWillem de Bruijn 17577e692df3SEric Dumazet /* We might have to allocate high order pages, so compute what minimum 17587e692df3SEric Dumazet * page order is needed. 17597e692df3SEric Dumazet */ 17607e692df3SEric Dumazet order = 0; 17617e692df3SEric Dumazet while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb)) 17627e692df3SEric Dumazet order++; 17637e692df3SEric Dumazet psize = (PAGE_SIZE << order); 17647e692df3SEric Dumazet 17657e692df3SEric Dumazet new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order); 17663ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 17677e692df3SEric Dumazet page = alloc_pages(gfp_mask | __GFP_COMP, order); 1768a6686f2fSShirley Ma if (!page) { 1769a6686f2fSShirley Ma while (head) { 177040dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1771a6686f2fSShirley Ma put_page(head); 1772a6686f2fSShirley Ma head = next; 1773a6686f2fSShirley Ma } 1774a6686f2fSShirley Ma return -ENOMEM; 1775a6686f2fSShirley Ma } 17763ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 17773ece7826SWillem de Bruijn head = page; 17783ece7826SWillem de Bruijn } 17793ece7826SWillem de Bruijn 17803ece7826SWillem de Bruijn page = head; 17813ece7826SWillem de Bruijn d_off = 0; 17823ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 17833ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17843ece7826SWillem de Bruijn u32 p_off, p_len, copied; 17853ece7826SWillem de Bruijn struct page *p; 17863ece7826SWillem de Bruijn u8 *vaddr; 1787c613c209SWillem de Bruijn 1788b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1789c613c209SWillem de Bruijn p, p_off, p_len, copied) { 17903ece7826SWillem de Bruijn u32 copy, done = 0; 1791c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 17923ece7826SWillem de Bruijn 17933ece7826SWillem de Bruijn while (done < p_len) { 17947e692df3SEric Dumazet if (d_off == psize) { 17953ece7826SWillem de Bruijn d_off = 0; 17963ece7826SWillem de Bruijn page = (struct page *)page_private(page); 17973ece7826SWillem de Bruijn } 17987e692df3SEric Dumazet copy = min_t(u32, psize - d_off, p_len - done); 17993ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 18003ece7826SWillem de Bruijn vaddr + p_off + done, copy); 18013ece7826SWillem de Bruijn done += copy; 18023ece7826SWillem de Bruijn d_off += copy; 18033ece7826SWillem de Bruijn } 180451c56b00SEric Dumazet kunmap_atomic(vaddr); 1805c613c209SWillem de Bruijn } 1806a6686f2fSShirley Ma } 1807a6686f2fSShirley Ma 1808a6686f2fSShirley Ma /* skb frags release userspace buffers */ 180902756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1810a8605c60SIan Campbell skb_frag_unref(skb, i); 1811a6686f2fSShirley Ma 1812a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 18133ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 18147e692df3SEric Dumazet __skb_fill_page_desc(skb, i, head, 0, psize); 181540dadff2SSunghan Suh head = (struct page *)page_private(head); 1816a6686f2fSShirley Ma } 18173ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 18183ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 181948c83012SMichael S. Tsirkin 1820b90ddd56SWillem de Bruijn release: 18211f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1822a6686f2fSShirley Ma return 0; 1823a6686f2fSShirley Ma } 1824dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1825a6686f2fSShirley Ma 1826e0053ec0SHerbert Xu /** 1827e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1828e0053ec0SHerbert Xu * @skb: buffer to clone 1829e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1830e0053ec0SHerbert Xu * 1831e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1832e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1833e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1834e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1835e0053ec0SHerbert Xu * 1836e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1837e0053ec0SHerbert Xu * %GFP_ATOMIC. 1838e0053ec0SHerbert Xu */ 1839e0053ec0SHerbert Xu 1840e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1841e0053ec0SHerbert Xu { 1842d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1843d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1844d0bf4a9eSEric Dumazet skb1); 18456ffe75ebSEric Dumazet struct sk_buff *n; 1846e0053ec0SHerbert Xu 184770008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1848a6686f2fSShirley Ma return NULL; 1849a6686f2fSShirley Ma 1850e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 18512638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 18526ffe75ebSEric Dumazet n = &fclones->skb2; 18532638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1854c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1855e0053ec0SHerbert Xu } else { 1856c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1857c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1858c93bdd0eSMel Gorman 1859025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask); 1860e0053ec0SHerbert Xu if (!n) 1861e0053ec0SHerbert Xu return NULL; 1862fe55f6d5SVegard Nossum 1863e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1864e0053ec0SHerbert Xu } 1865e0053ec0SHerbert Xu 1866e0053ec0SHerbert Xu return __skb_clone(n, skb); 18671da177e4SLinus Torvalds } 1868b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 18691da177e4SLinus Torvalds 1870b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1871f5b17294SPravin B Shelar { 1872030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1873030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1874030737bcSEric Dumazet skb->csum_start += off; 1875f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1876f5b17294SPravin B Shelar skb->transport_header += off; 1877f5b17294SPravin B Shelar skb->network_header += off; 1878f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1879f5b17294SPravin B Shelar skb->mac_header += off; 1880f5b17294SPravin B Shelar skb->inner_transport_header += off; 1881f5b17294SPravin B Shelar skb->inner_network_header += off; 1882aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1883f5b17294SPravin B Shelar } 1884b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1885f5b17294SPravin B Shelar 188608303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 18871da177e4SLinus Torvalds { 1888dec18810SHerbert Xu __copy_skb_header(new, old); 1889dec18810SHerbert Xu 18907967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 18917967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 18927967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 18931da177e4SLinus Torvalds } 189408303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 18951da177e4SLinus Torvalds 1896c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1897c93bdd0eSMel Gorman { 1898c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1899c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1900c93bdd0eSMel Gorman return 0; 1901c93bdd0eSMel Gorman } 1902c93bdd0eSMel Gorman 19031da177e4SLinus Torvalds /** 19041da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 19051da177e4SLinus Torvalds * @skb: buffer to copy 19061da177e4SLinus Torvalds * @gfp_mask: allocation priority 19071da177e4SLinus Torvalds * 19081da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 19091da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 19101da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 19111da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 19121da177e4SLinus Torvalds * 19131da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 19141da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 19151da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 19161da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 19171da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 19181da177e4SLinus Torvalds */ 19191da177e4SLinus Torvalds 1920dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 19211da177e4SLinus Torvalds { 19226602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1923ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1924c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1925c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 19266602cebbSEric Dumazet 19271da177e4SLinus Torvalds if (!n) 19281da177e4SLinus Torvalds return NULL; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* Set the data pointer */ 19311da177e4SLinus Torvalds skb_reserve(n, headerlen); 19321da177e4SLinus Torvalds /* Set the tail pointer and length */ 19331da177e4SLinus Torvalds skb_put(n, skb->len); 19341da177e4SLinus Torvalds 19359f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 19361da177e4SLinus Torvalds 193708303c18SIlya Lesokhin skb_copy_header(n, skb); 19381da177e4SLinus Torvalds return n; 19391da177e4SLinus Torvalds } 1940b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds /** 1943bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 19441da177e4SLinus Torvalds * @skb: buffer to copy 1945117632e6SEric Dumazet * @headroom: headroom of new skb 19461da177e4SLinus Torvalds * @gfp_mask: allocation priority 1947bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1948bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1949bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 19501da177e4SLinus Torvalds * 19511da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 19521da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 19531da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 19541da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 19551da177e4SLinus Torvalds * or the pointer to the buffer on success. 19561da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 19571da177e4SLinus Torvalds */ 19581da177e4SLinus Torvalds 1959bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1960bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 19611da177e4SLinus Torvalds { 1962117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1963bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1964bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 19656602cebbSEric Dumazet 19661da177e4SLinus Torvalds if (!n) 19671da177e4SLinus Torvalds goto out; 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds /* Set the data pointer */ 1970117632e6SEric Dumazet skb_reserve(n, headroom); 19711da177e4SLinus Torvalds /* Set the tail pointer and length */ 19721da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 19731da177e4SLinus Torvalds /* Copy the bytes */ 1974d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 19751da177e4SLinus Torvalds 197625f484a6SHerbert Xu n->truesize += skb->data_len; 19771da177e4SLinus Torvalds n->data_len = skb->data_len; 19781da177e4SLinus Torvalds n->len = skb->len; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 19811da177e4SLinus Torvalds int i; 19821da177e4SLinus Torvalds 19831f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 19841f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 19851511022cSDan Carpenter kfree_skb(n); 19861511022cSDan Carpenter n = NULL; 1987a6686f2fSShirley Ma goto out; 1988a6686f2fSShirley Ma } 19891da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19901da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1991ea2ab693SIan Campbell skb_frag_ref(skb, i); 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 199621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 19971da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 19981da177e4SLinus Torvalds skb_clone_fraglist(n); 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 200108303c18SIlya Lesokhin skb_copy_header(n, skb); 20021da177e4SLinus Torvalds out: 20031da177e4SLinus Torvalds return n; 20041da177e4SLinus Torvalds } 2005bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds /** 20081da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 20091da177e4SLinus Torvalds * @skb: buffer to reallocate 20101da177e4SLinus Torvalds * @nhead: room to add at head 20111da177e4SLinus Torvalds * @ntail: room to add at tail 20121da177e4SLinus Torvalds * @gfp_mask: allocation priority 20131da177e4SLinus Torvalds * 2014bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 2015bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 20161da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 20171da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 20181da177e4SLinus Torvalds * 20191da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20201da177e4SLinus Torvalds * reloaded after call to this function. 20211da177e4SLinus Torvalds */ 20221da177e4SLinus Torvalds 202386a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 2024dd0fc66fSAl Viro gfp_t gfp_mask) 20251da177e4SLinus Torvalds { 202612d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 202712d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 20281da177e4SLinus Torvalds long off; 2029158f323bSEric Dumazet u8 *data; 203012d6c1d3SKees Cook int i; 20311da177e4SLinus Torvalds 20324edd87adSHerbert Xu BUG_ON(nhead < 0); 20334edd87adSHerbert Xu 20349f77fad3STim Hansen BUG_ON(skb_shared(skb)); 20351da177e4SLinus Torvalds 2036753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 2037753f1ca4SPavel Begunkov 2038c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 2039c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 204012d6c1d3SKees Cook 20415c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 20421da177e4SLinus Torvalds if (!data) 20431da177e4SLinus Torvalds goto nodata; 204412d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 20476602cebbSEric Dumazet * optimized for the cases when header is void. 20486602cebbSEric Dumazet */ 20496602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 20506602cebbSEric Dumazet 20516602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 20526602cebbSEric Dumazet skb_shinfo(skb), 2053fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 20541da177e4SLinus Torvalds 20553e24591aSAlexander Duyck /* 20563e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 20573e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 20583e24591aSAlexander Duyck * be since all we did is relocate the values 20593e24591aSAlexander Duyck */ 20603e24591aSAlexander Duyck if (skb_cloned(skb)) { 206170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 2062a6686f2fSShirley Ma goto nofrags; 20631f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 2064c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 20651da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2066ea2ab693SIan Campbell skb_frag_ref(skb, i); 20671da177e4SLinus Torvalds 206821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 20691da177e4SLinus Torvalds skb_clone_fraglist(skb); 20701da177e4SLinus Torvalds 2071b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 20723e24591aSAlexander Duyck } else { 2073b07a2d97SJakub Kicinski skb_free_head(skb, false); 20741fd63041SEric Dumazet } 20751da177e4SLinus Torvalds off = (data + nhead) - skb->head; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds skb->head = data; 2078d3836f21SEric Dumazet skb->head_frag = 0; 20791da177e4SLinus Torvalds skb->data += off; 2080763087daSEric Dumazet 2081763087daSEric Dumazet skb_set_end_offset(skb, size); 20824305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 208356eb8882SPatrick McHardy off = nhead; 208456eb8882SPatrick McHardy #endif 208527a884dcSArnaldo Carvalho de Melo skb->tail += off; 2086b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 20871da177e4SLinus Torvalds skb->cloned = 0; 2088334a8132SPatrick McHardy skb->hdr_len = 0; 20891da177e4SLinus Torvalds skb->nohdr = 0; 20901da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 2091158f323bSEric Dumazet 2092de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 2093de8f3a83SDaniel Borkmann 2094158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 2095158f323bSEric Dumazet * For the moment, we really care of rx path, or 2096158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 2097158f323bSEric Dumazet */ 2098158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2099158f323bSEric Dumazet skb->truesize += size - osize; 2100158f323bSEric Dumazet 21011da177e4SLinus Torvalds return 0; 21021da177e4SLinus Torvalds 2103a6686f2fSShirley Ma nofrags: 2104bf9f1baaSEric Dumazet skb_kfree_head(data, size); 21051da177e4SLinus Torvalds nodata: 21061da177e4SLinus Torvalds return -ENOMEM; 21071da177e4SLinus Torvalds } 2108b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 21131da177e4SLinus Torvalds { 21141da177e4SLinus Torvalds struct sk_buff *skb2; 21151da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds if (delta <= 0) 21181da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 21191da177e4SLinus Torvalds else { 21201da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 21211da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 21221da177e4SLinus Torvalds GFP_ATOMIC)) { 21231da177e4SLinus Torvalds kfree_skb(skb2); 21241da177e4SLinus Torvalds skb2 = NULL; 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds return skb2; 21281da177e4SLinus Torvalds } 2129b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 21301da177e4SLinus Torvalds 2131880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */ 21322b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 21332b88cba5SEric Dumazet { 21342b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 21352b88cba5SEric Dumazet struct skb_shared_info *shinfo; 21362b88cba5SEric Dumazet int res; 21372b88cba5SEric Dumazet 21382b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 21392b88cba5SEric Dumazet saved_truesize = skb->truesize; 21402b88cba5SEric Dumazet 21412b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 21422b88cba5SEric Dumazet if (res) 21432b88cba5SEric Dumazet return res; 21442b88cba5SEric Dumazet 21452b88cba5SEric Dumazet skb->truesize = saved_truesize; 21462b88cba5SEric Dumazet 21472b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 21482b88cba5SEric Dumazet return 0; 21492b88cba5SEric Dumazet 2150880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value 2151880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). 2152880ce5f2SEric Dumazet */ 2153880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || 2154880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { 2155880ce5f2SEric Dumazet /* We think this path should not be taken. 2156880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case. 2157880ce5f2SEric Dumazet */ 2158880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", 2159880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb)); 2160880ce5f2SEric Dumazet WARN_ON_ONCE(1); 2161880ce5f2SEric Dumazet return 0; 2162880ce5f2SEric Dumazet } 2163880ce5f2SEric Dumazet 21642b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 21652b88cba5SEric Dumazet 21662b88cba5SEric Dumazet /* We are about to change back skb->end, 21672b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 21682b88cba5SEric Dumazet */ 21692b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 21702b88cba5SEric Dumazet shinfo, 21712b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 21722b88cba5SEric Dumazet 21732b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 21742b88cba5SEric Dumazet 21752b88cba5SEric Dumazet return 0; 21762b88cba5SEric Dumazet } 21772b88cba5SEric Dumazet 21781da177e4SLinus Torvalds /** 2179f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2180f1260ff1SVasily Averin * @skb: buffer to reallocate 2181f1260ff1SVasily Averin * @headroom: needed headroom 2182f1260ff1SVasily Averin * 2183f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2184f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2185f1260ff1SVasily Averin * and frees original skb in case of failures. 2186f1260ff1SVasily Averin * 2187f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2188f1260ff1SVasily Averin */ 2189f1260ff1SVasily Averin 2190f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2191f1260ff1SVasily Averin { 2192f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 21937f678defSVasily Averin int osize = skb_end_offset(skb); 21947f678defSVasily Averin struct sock *sk = skb->sk; 2195f1260ff1SVasily Averin 2196f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2197f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2198f1260ff1SVasily Averin return skb; 2199f1260ff1SVasily Averin 22007f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 22017f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 22027f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2203f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2204f1260ff1SVasily Averin 22057f678defSVasily Averin if (unlikely(!nskb)) 22067f678defSVasily Averin goto fail; 22077f678defSVasily Averin 22087f678defSVasily Averin if (sk) 22097f678defSVasily Averin skb_set_owner_w(nskb, sk); 2210f1260ff1SVasily Averin consume_skb(skb); 2211f1260ff1SVasily Averin skb = nskb; 2212f1260ff1SVasily Averin } 22137f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 22147f678defSVasily Averin goto fail; 22157f678defSVasily Averin 22167f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 22177f678defSVasily Averin delta = skb_end_offset(skb) - osize; 22187f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 22197f678defSVasily Averin skb->truesize += delta; 2220f1260ff1SVasily Averin } 2221f1260ff1SVasily Averin return skb; 22227f678defSVasily Averin 22237f678defSVasily Averin fail: 22247f678defSVasily Averin kfree_skb(skb); 22257f678defSVasily Averin return NULL; 2226f1260ff1SVasily Averin } 2227f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2228f1260ff1SVasily Averin 2229f1260ff1SVasily Averin /** 22301da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 22311da177e4SLinus Torvalds * @skb: buffer to copy 22321da177e4SLinus Torvalds * @newheadroom: new free bytes at head 22331da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 22341da177e4SLinus Torvalds * @gfp_mask: allocation priority 22351da177e4SLinus Torvalds * 22361da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 22371da177e4SLinus Torvalds * allocate additional space. 22381da177e4SLinus Torvalds * 22391da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 22401da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 22411da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 22421da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 22431da177e4SLinus Torvalds * 22441da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 22451da177e4SLinus Torvalds * is called from an interrupt. 22461da177e4SLinus Torvalds */ 22471da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 224886a76cafSVictor Fusco int newheadroom, int newtailroom, 2249dd0fc66fSAl Viro gfp_t gfp_mask) 22501da177e4SLinus Torvalds { 22511da177e4SLinus Torvalds /* 22521da177e4SLinus Torvalds * Allocate the copy buffer 22531da177e4SLinus Torvalds */ 2254c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2255c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2256c93bdd0eSMel Gorman NUMA_NO_NODE); 2257efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 22581da177e4SLinus Torvalds int head_copy_len, head_copy_off; 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds if (!n) 22611da177e4SLinus Torvalds return NULL; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds skb_reserve(n, newheadroom); 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds /* Set the tail pointer and length */ 22661da177e4SLinus Torvalds skb_put(n, skb->len); 22671da177e4SLinus Torvalds 2268efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 22691da177e4SLinus Torvalds head_copy_off = 0; 22701da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 22711da177e4SLinus Torvalds head_copy_len = newheadroom; 22721da177e4SLinus Torvalds else 22731da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds /* Copy the linear header and data. */ 22769f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 22779f77fad3STim Hansen skb->len + head_copy_len)); 22781da177e4SLinus Torvalds 227908303c18SIlya Lesokhin skb_copy_header(n, skb); 22801da177e4SLinus Torvalds 2281030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2282efd1e8d5SPatrick McHardy 22831da177e4SLinus Torvalds return n; 22841da177e4SLinus Torvalds } 2285b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds /** 2288cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 22891da177e4SLinus Torvalds * @skb: buffer to pad 22901da177e4SLinus Torvalds * @pad: space to pad 2291cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 22921da177e4SLinus Torvalds * 22931da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 22941da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 22951da177e4SLinus Torvalds * beyond the buffer end onto the wire. 22961da177e4SLinus Torvalds * 2297cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2298cd0a137aSFlorian Fainelli * if @free_on_error is true. 22991da177e4SLinus Torvalds */ 23001da177e4SLinus Torvalds 2301cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 23021da177e4SLinus Torvalds { 23035b057c6bSHerbert Xu int err; 23045b057c6bSHerbert Xu int ntail; 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 23075b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 23081da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 23095b057c6bSHerbert Xu return 0; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 23124305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 23135b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 23145b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 23155b057c6bSHerbert Xu if (unlikely(err)) 23165b057c6bSHerbert Xu goto free_skb; 23175b057c6bSHerbert Xu } 23185b057c6bSHerbert Xu 23195b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 23205b057c6bSHerbert Xu * to be audited. 23215b057c6bSHerbert Xu */ 23225b057c6bSHerbert Xu err = skb_linearize(skb); 23235b057c6bSHerbert Xu if (unlikely(err)) 23245b057c6bSHerbert Xu goto free_skb; 23255b057c6bSHerbert Xu 23265b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 23275b057c6bSHerbert Xu return 0; 23285b057c6bSHerbert Xu 23295b057c6bSHerbert Xu free_skb: 2330cd0a137aSFlorian Fainelli if (free_on_error) 23311da177e4SLinus Torvalds kfree_skb(skb); 23325b057c6bSHerbert Xu return err; 23331da177e4SLinus Torvalds } 2334cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 23351da177e4SLinus Torvalds 23360dde3e16SIlpo Järvinen /** 23370c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 23380c7ddf36SMathias Krause * @skb: start of the buffer to use 23390c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 23400c7ddf36SMathias Krause * @len: amount of data to add 23410c7ddf36SMathias Krause * 23420c7ddf36SMathias Krause * This function extends the used data area of the potentially 23430c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 23440c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 23450c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 23460c7ddf36SMathias Krause * returned. 23470c7ddf36SMathias Krause */ 23480c7ddf36SMathias Krause 23494df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 23500c7ddf36SMathias Krause { 23510c7ddf36SMathias Krause if (tail != skb) { 23520c7ddf36SMathias Krause skb->data_len += len; 23530c7ddf36SMathias Krause skb->len += len; 23540c7ddf36SMathias Krause } 23550c7ddf36SMathias Krause return skb_put(tail, len); 23560c7ddf36SMathias Krause } 23570c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 23580c7ddf36SMathias Krause 23590c7ddf36SMathias Krause /** 23600dde3e16SIlpo Järvinen * skb_put - add data to a buffer 23610dde3e16SIlpo Järvinen * @skb: buffer to use 23620dde3e16SIlpo Järvinen * @len: amount of data to add 23630dde3e16SIlpo Järvinen * 23640dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 23650dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 23660dde3e16SIlpo Järvinen * first byte of the extra data is returned. 23670dde3e16SIlpo Järvinen */ 23684df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 23690dde3e16SIlpo Järvinen { 23704df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 23710dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 23720dde3e16SIlpo Järvinen skb->tail += len; 23730dde3e16SIlpo Järvinen skb->len += len; 23740dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 23750dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 23760dde3e16SIlpo Järvinen return tmp; 23770dde3e16SIlpo Järvinen } 23780dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 23790dde3e16SIlpo Järvinen 23806be8ac2fSIlpo Järvinen /** 2381c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2382c2aa270aSIlpo Järvinen * @skb: buffer to use 2383c2aa270aSIlpo Järvinen * @len: amount of data to add 2384c2aa270aSIlpo Järvinen * 2385c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2386c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2387c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2388c2aa270aSIlpo Järvinen */ 2389d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2390c2aa270aSIlpo Järvinen { 2391c2aa270aSIlpo Järvinen skb->data -= len; 2392c2aa270aSIlpo Järvinen skb->len += len; 2393c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2394c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2395c2aa270aSIlpo Järvinen return skb->data; 2396c2aa270aSIlpo Järvinen } 2397c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2398c2aa270aSIlpo Järvinen 2399c2aa270aSIlpo Järvinen /** 24006be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 24016be8ac2fSIlpo Järvinen * @skb: buffer to use 24026be8ac2fSIlpo Järvinen * @len: amount of data to remove 24036be8ac2fSIlpo Järvinen * 24046be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 24056be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 24066be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 24076be8ac2fSIlpo Järvinen * the old data. 24086be8ac2fSIlpo Järvinen */ 2409af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 24106be8ac2fSIlpo Järvinen { 241147d29646SDavid S. Miller return skb_pull_inline(skb, len); 24126be8ac2fSIlpo Järvinen } 24136be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 24146be8ac2fSIlpo Järvinen 2415419ae74eSIlpo Järvinen /** 241613244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 241713244cccSLuiz Augusto von Dentz * original position. 241813244cccSLuiz Augusto von Dentz * @skb: buffer to use 241913244cccSLuiz Augusto von Dentz * @len: amount of data to remove 242013244cccSLuiz Augusto von Dentz * 242113244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 242213244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 242313244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 242413244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 242513244cccSLuiz Augusto von Dentz */ 242613244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 242713244cccSLuiz Augusto von Dentz { 242813244cccSLuiz Augusto von Dentz void *data = skb->data; 242913244cccSLuiz Augusto von Dentz 243013244cccSLuiz Augusto von Dentz if (skb->len < len) 243113244cccSLuiz Augusto von Dentz return NULL; 243213244cccSLuiz Augusto von Dentz 243313244cccSLuiz Augusto von Dentz skb_pull(skb, len); 243413244cccSLuiz Augusto von Dentz 243513244cccSLuiz Augusto von Dentz return data; 243613244cccSLuiz Augusto von Dentz } 243713244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 243813244cccSLuiz Augusto von Dentz 243913244cccSLuiz Augusto von Dentz /** 2440419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2441419ae74eSIlpo Järvinen * @skb: buffer to alter 2442419ae74eSIlpo Järvinen * @len: new length 2443419ae74eSIlpo Järvinen * 2444419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2445419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2446419ae74eSIlpo Järvinen * The skb must be linear. 2447419ae74eSIlpo Järvinen */ 2448419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2449419ae74eSIlpo Järvinen { 2450419ae74eSIlpo Järvinen if (skb->len > len) 2451419ae74eSIlpo Järvinen __skb_trim(skb, len); 2452419ae74eSIlpo Järvinen } 2453419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2454419ae74eSIlpo Järvinen 24553cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 24561da177e4SLinus Torvalds */ 24571da177e4SLinus Torvalds 24583cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 24591da177e4SLinus Torvalds { 246027b437c8SHerbert Xu struct sk_buff **fragp; 246127b437c8SHerbert Xu struct sk_buff *frag; 24621da177e4SLinus Torvalds int offset = skb_headlen(skb); 24631da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 24641da177e4SLinus Torvalds int i; 246527b437c8SHerbert Xu int err; 246627b437c8SHerbert Xu 246727b437c8SHerbert Xu if (skb_cloned(skb) && 246827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 246927b437c8SHerbert Xu return err; 24701da177e4SLinus Torvalds 2471f4d26fb3SHerbert Xu i = 0; 2472f4d26fb3SHerbert Xu if (offset >= len) 2473f4d26fb3SHerbert Xu goto drop_pages; 2474f4d26fb3SHerbert Xu 2475f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 24769e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 247727b437c8SHerbert Xu 247827b437c8SHerbert Xu if (end < len) { 24791da177e4SLinus Torvalds offset = end; 248027b437c8SHerbert Xu continue; 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24839e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 248427b437c8SHerbert Xu 2485f4d26fb3SHerbert Xu drop_pages: 248627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 248727b437c8SHerbert Xu 248827b437c8SHerbert Xu for (; i < nfrags; i++) 2489ea2ab693SIan Campbell skb_frag_unref(skb, i); 249027b437c8SHerbert Xu 249121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 249227b437c8SHerbert Xu skb_drop_fraglist(skb); 2493f4d26fb3SHerbert Xu goto done; 249427b437c8SHerbert Xu } 249527b437c8SHerbert Xu 249627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 249727b437c8SHerbert Xu fragp = &frag->next) { 249827b437c8SHerbert Xu int end = offset + frag->len; 249927b437c8SHerbert Xu 250027b437c8SHerbert Xu if (skb_shared(frag)) { 250127b437c8SHerbert Xu struct sk_buff *nfrag; 250227b437c8SHerbert Xu 250327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 250427b437c8SHerbert Xu if (unlikely(!nfrag)) 250527b437c8SHerbert Xu return -ENOMEM; 250627b437c8SHerbert Xu 250727b437c8SHerbert Xu nfrag->next = frag->next; 250885bb2a60SEric Dumazet consume_skb(frag); 250927b437c8SHerbert Xu frag = nfrag; 251027b437c8SHerbert Xu *fragp = frag; 251127b437c8SHerbert Xu } 251227b437c8SHerbert Xu 251327b437c8SHerbert Xu if (end < len) { 251427b437c8SHerbert Xu offset = end; 251527b437c8SHerbert Xu continue; 251627b437c8SHerbert Xu } 251727b437c8SHerbert Xu 251827b437c8SHerbert Xu if (end > len && 251927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 252027b437c8SHerbert Xu return err; 252127b437c8SHerbert Xu 252227b437c8SHerbert Xu if (frag->next) 252327b437c8SHerbert Xu skb_drop_list(&frag->next); 252427b437c8SHerbert Xu break; 252527b437c8SHerbert Xu } 252627b437c8SHerbert Xu 2527f4d26fb3SHerbert Xu done: 252827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 25291da177e4SLinus Torvalds skb->data_len -= skb->len - len; 25301da177e4SLinus Torvalds skb->len = len; 25311da177e4SLinus Torvalds } else { 25321da177e4SLinus Torvalds skb->len = len; 25331da177e4SLinus Torvalds skb->data_len = 0; 253427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 2537c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2538c21b48ccSEric Dumazet skb_condense(skb); 25391da177e4SLinus Torvalds return 0; 25401da177e4SLinus Torvalds } 2541b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 25421da177e4SLinus Torvalds 254388078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 254488078d98SEric Dumazet */ 254588078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 254688078d98SEric Dumazet { 254788078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 254888078d98SEric Dumazet int delta = skb->len - len; 254988078d98SEric Dumazet 2550d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2551d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2552d55bef50SDimitris Michailidis len); 255354970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 255454970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 255554970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 255654970a2fSVasily Averin 255754970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 255854970a2fSVasily Averin return -EINVAL; 255988078d98SEric Dumazet } 256088078d98SEric Dumazet return __pskb_trim(skb, len); 256188078d98SEric Dumazet } 256288078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 256388078d98SEric Dumazet 25641da177e4SLinus Torvalds /** 25651da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 25661da177e4SLinus Torvalds * @skb: buffer to reallocate 25671da177e4SLinus Torvalds * @delta: number of bytes to advance tail 25681da177e4SLinus Torvalds * 25691da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 25701da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 25711da177e4SLinus Torvalds * data from fragmented part. 25721da177e4SLinus Torvalds * 25731da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 25741da177e4SLinus Torvalds * 25751da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 25761da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 25771da177e4SLinus Torvalds * 25781da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 25791da177e4SLinus Torvalds * reloaded after call to this function. 25801da177e4SLinus Torvalds */ 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 25831da177e4SLinus Torvalds * when it is necessary. 25841da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 25851da177e4SLinus Torvalds * 2. It may change skb pointers. 25861da177e4SLinus Torvalds * 25871da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 25881da177e4SLinus Torvalds */ 2589af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 25901da177e4SLinus Torvalds { 25911da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 25921da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 25931da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 25941da177e4SLinus Torvalds */ 25954305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 25981da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 25991da177e4SLinus Torvalds GFP_ATOMIC)) 26001da177e4SLinus Torvalds return NULL; 26011da177e4SLinus Torvalds } 26021da177e4SLinus Torvalds 26039f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 26049f77fad3STim Hansen skb_tail_pointer(skb), delta)); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 26071da177e4SLinus Torvalds * size of pulled pages. Superb. 26081da177e4SLinus Torvalds */ 260921dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 26101da177e4SLinus Torvalds goto pull_pages; 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 26131da177e4SLinus Torvalds eat = delta; 26141da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26159e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26169e903e08SEric Dumazet 26179e903e08SEric Dumazet if (size >= eat) 26181da177e4SLinus Torvalds goto pull_pages; 26199e903e08SEric Dumazet eat -= size; 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 262309001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 26241da177e4SLinus Torvalds * but taking into account that pulling is expected to 26251da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 26261da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 26271da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 26281da177e4SLinus Torvalds */ 26291da177e4SLinus Torvalds if (eat) { 26301da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 26311da177e4SLinus Torvalds struct sk_buff *clone = NULL; 26321da177e4SLinus Torvalds struct sk_buff *insp = NULL; 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds do { 26351da177e4SLinus Torvalds if (list->len <= eat) { 26361da177e4SLinus Torvalds /* Eaten as whole. */ 26371da177e4SLinus Torvalds eat -= list->len; 26381da177e4SLinus Torvalds list = list->next; 26391da177e4SLinus Torvalds insp = list; 26401da177e4SLinus Torvalds } else { 26411da177e4SLinus Torvalds /* Eaten partially. */ 26422d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 26432d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 26442d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds if (skb_shared(list)) { 26471da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 26481da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 26491da177e4SLinus Torvalds if (!clone) 26501da177e4SLinus Torvalds return NULL; 26511da177e4SLinus Torvalds insp = list->next; 26521da177e4SLinus Torvalds list = clone; 26531da177e4SLinus Torvalds } else { 26541da177e4SLinus Torvalds /* This may be pulled without 26551da177e4SLinus Torvalds * problems. */ 26561da177e4SLinus Torvalds insp = list; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 26591da177e4SLinus Torvalds kfree_skb(clone); 26601da177e4SLinus Torvalds return NULL; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds break; 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds } while (eat); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds /* Free pulled out fragments. */ 26671da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 26681da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2669ef527f96SEric Dumazet consume_skb(list); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds /* And insert new clone at head. */ 26721da177e4SLinus Torvalds if (clone) { 26731da177e4SLinus Torvalds clone->next = list; 26741da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 26781da177e4SLinus Torvalds 26791da177e4SLinus Torvalds pull_pages: 26801da177e4SLinus Torvalds eat = delta; 26811da177e4SLinus Torvalds k = 0; 26821da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26839e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26849e903e08SEric Dumazet 26859e903e08SEric Dumazet if (size <= eat) { 2686ea2ab693SIan Campbell skb_frag_unref(skb, i); 26879e903e08SEric Dumazet eat -= size; 26881da177e4SLinus Torvalds } else { 2689b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2690b54c9d5bSJonathan Lemon 2691b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 26921da177e4SLinus Torvalds if (eat) { 2693b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2694b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 26953ccc6c6fSlinzhang if (!i) 26963ccc6c6fSlinzhang goto end; 26971da177e4SLinus Torvalds eat = 0; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds k++; 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 27031da177e4SLinus Torvalds 27043ccc6c6fSlinzhang end: 27051da177e4SLinus Torvalds skb->tail += delta; 27061da177e4SLinus Torvalds skb->data_len -= delta; 27071da177e4SLinus Torvalds 27081f8b977aSWillem de Bruijn if (!skb->data_len) 27091f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 27101f8b977aSWillem de Bruijn 271127a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 27121da177e4SLinus Torvalds } 2713b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 27141da177e4SLinus Torvalds 271522019b17SEric Dumazet /** 271622019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 271722019b17SEric Dumazet * @skb: source skb 271822019b17SEric Dumazet * @offset: offset in source 271922019b17SEric Dumazet * @to: destination buffer 272022019b17SEric Dumazet * @len: number of bytes to copy 272122019b17SEric Dumazet * 272222019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 272322019b17SEric Dumazet * destination buffer. 272422019b17SEric Dumazet * 272522019b17SEric Dumazet * CAUTION ! : 272622019b17SEric Dumazet * If its prototype is ever changed, 272722019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 272822019b17SEric Dumazet * since it is called from BPF assembly code. 272922019b17SEric Dumazet */ 27301da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 27311da177e4SLinus Torvalds { 27321a028e50SDavid S. Miller int start = skb_headlen(skb); 2733fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2734fbb398a8SDavid S. Miller int i, copy; 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds if (offset > (int)skb->len - len) 27371da177e4SLinus Torvalds goto fault; 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds /* Copy header. */ 27401a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 27411da177e4SLinus Torvalds if (copy > len) 27421da177e4SLinus Torvalds copy = len; 2743d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 27441da177e4SLinus Torvalds if ((len -= copy) == 0) 27451da177e4SLinus Torvalds return 0; 27461da177e4SLinus Torvalds offset += copy; 27471da177e4SLinus Torvalds to += copy; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27511a028e50SDavid S. Miller int end; 275251c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 27531da177e4SLinus Torvalds 2754547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27551a028e50SDavid S. Miller 275651c56b00SEric Dumazet end = start + skb_frag_size(f); 27571da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2758c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2759c613c209SWillem de Bruijn struct page *p; 27601da177e4SLinus Torvalds u8 *vaddr; 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds if (copy > len) 27631da177e4SLinus Torvalds copy = len; 27641da177e4SLinus Torvalds 2765c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2766b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2767c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2768c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2769c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 277051c56b00SEric Dumazet kunmap_atomic(vaddr); 2771c613c209SWillem de Bruijn } 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds if ((len -= copy) == 0) 27741da177e4SLinus Torvalds return 0; 27751da177e4SLinus Torvalds offset += copy; 27761da177e4SLinus Torvalds to += copy; 27771da177e4SLinus Torvalds } 27781a028e50SDavid S. Miller start = end; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 2781fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 27821a028e50SDavid S. Miller int end; 27831da177e4SLinus Torvalds 2784547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27851a028e50SDavid S. Miller 2786fbb398a8SDavid S. Miller end = start + frag_iter->len; 27871da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 27881da177e4SLinus Torvalds if (copy > len) 27891da177e4SLinus Torvalds copy = len; 2790fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 27911da177e4SLinus Torvalds goto fault; 27921da177e4SLinus Torvalds if ((len -= copy) == 0) 27931da177e4SLinus Torvalds return 0; 27941da177e4SLinus Torvalds offset += copy; 27951da177e4SLinus Torvalds to += copy; 27961da177e4SLinus Torvalds } 27971a028e50SDavid S. Miller start = end; 27981da177e4SLinus Torvalds } 2799a6686f2fSShirley Ma 28001da177e4SLinus Torvalds if (!len) 28011da177e4SLinus Torvalds return 0; 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds fault: 28041da177e4SLinus Torvalds return -EFAULT; 28051da177e4SLinus Torvalds } 2806b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 28071da177e4SLinus Torvalds 28089c55e01cSJens Axboe /* 28099c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 28109c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 28119c55e01cSJens Axboe */ 28129c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 28139c55e01cSJens Axboe { 28148b9d3728SJarek Poplawski put_page(spd->pages[i]); 28158b9d3728SJarek Poplawski } 28169c55e01cSJens Axboe 2817a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 28184fb66994SJarek Poplawski unsigned int *offset, 281918aafc62SEric Dumazet struct sock *sk) 28208b9d3728SJarek Poplawski { 28215640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 28228b9d3728SJarek Poplawski 28235640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 28248b9d3728SJarek Poplawski return NULL; 28254fb66994SJarek Poplawski 28265640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 28274fb66994SJarek Poplawski 28285640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 28295640f768SEric Dumazet page_address(page) + *offset, *len); 28305640f768SEric Dumazet *offset = pfrag->offset; 28315640f768SEric Dumazet pfrag->offset += *len; 28324fb66994SJarek Poplawski 28335640f768SEric Dumazet return pfrag->page; 28349c55e01cSJens Axboe } 28359c55e01cSJens Axboe 283641c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 283741c73a0dSEric Dumazet struct page *page, 283841c73a0dSEric Dumazet unsigned int offset) 283941c73a0dSEric Dumazet { 284041c73a0dSEric Dumazet return spd->nr_pages && 284141c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 284241c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 284341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 284441c73a0dSEric Dumazet } 284541c73a0dSEric Dumazet 28469c55e01cSJens Axboe /* 28479c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 28489c55e01cSJens Axboe */ 2849a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 285035f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 28514fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 285218aafc62SEric Dumazet bool linear, 28537a67e56fSJarek Poplawski struct sock *sk) 28549c55e01cSJens Axboe { 285541c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2856a108d5f3SDavid S. Miller return true; 28579c55e01cSJens Axboe 28588b9d3728SJarek Poplawski if (linear) { 285918aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 28608b9d3728SJarek Poplawski if (!page) 2861a108d5f3SDavid S. Miller return true; 286241c73a0dSEric Dumazet } 286341c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 286441c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2865a108d5f3SDavid S. Miller return false; 286641c73a0dSEric Dumazet } 28678b9d3728SJarek Poplawski get_page(page); 28689c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 28694fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 28709c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 28719c55e01cSJens Axboe spd->nr_pages++; 28728b9d3728SJarek Poplawski 2873a108d5f3SDavid S. Miller return false; 28749c55e01cSJens Axboe } 28759c55e01cSJens Axboe 2876a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 28772870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 287818aafc62SEric Dumazet unsigned int *len, 2879d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 288035f3d14dSJens Axboe struct sock *sk, 288135f3d14dSJens Axboe struct pipe_inode_info *pipe) 28829c55e01cSJens Axboe { 28832870c43dSOctavian Purdila if (!*len) 2884a108d5f3SDavid S. Miller return true; 28859c55e01cSJens Axboe 28862870c43dSOctavian Purdila /* skip this segment if already processed */ 28872870c43dSOctavian Purdila if (*off >= plen) { 28882870c43dSOctavian Purdila *off -= plen; 2889a108d5f3SDavid S. Miller return false; 28902870c43dSOctavian Purdila } 28912870c43dSOctavian Purdila 28922870c43dSOctavian Purdila /* ignore any bits we already processed */ 28939ca1b22dSEric Dumazet poff += *off; 28949ca1b22dSEric Dumazet plen -= *off; 28952870c43dSOctavian Purdila *off = 0; 28962870c43dSOctavian Purdila 289718aafc62SEric Dumazet do { 289818aafc62SEric Dumazet unsigned int flen = min(*len, plen); 28992870c43dSOctavian Purdila 290018aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 290118aafc62SEric Dumazet linear, sk)) 2902a108d5f3SDavid S. Miller return true; 290318aafc62SEric Dumazet poff += flen; 290418aafc62SEric Dumazet plen -= flen; 29052870c43dSOctavian Purdila *len -= flen; 290618aafc62SEric Dumazet } while (*len && plen); 29072870c43dSOctavian Purdila 2908a108d5f3SDavid S. Miller return false; 2909db43a282SOctavian Purdila } 29109c55e01cSJens Axboe 29119c55e01cSJens Axboe /* 2912a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 29132870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 29149c55e01cSJens Axboe */ 2915a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 291635f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 291735f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 29182870c43dSOctavian Purdila { 29192870c43dSOctavian Purdila int seg; 2920fa9835e5STom Herbert struct sk_buff *iter; 29219c55e01cSJens Axboe 29221d0c0b32SEric Dumazet /* map the linear part : 29232996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 29242996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 29252996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 29269c55e01cSJens Axboe */ 29272870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 29282870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 29292870c43dSOctavian Purdila skb_headlen(skb), 293018aafc62SEric Dumazet offset, len, spd, 29313a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 29321d0c0b32SEric Dumazet sk, pipe)) 2933a108d5f3SDavid S. Miller return true; 29349c55e01cSJens Axboe 29359c55e01cSJens Axboe /* 29369c55e01cSJens Axboe * then map the fragments 29379c55e01cSJens Axboe */ 29389c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 29399c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 29409c55e01cSJens Axboe 2941ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2942b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 294318aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2944a108d5f3SDavid S. Miller return true; 29459c55e01cSJens Axboe } 29469c55e01cSJens Axboe 2947fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2948fa9835e5STom Herbert if (*offset >= iter->len) { 2949fa9835e5STom Herbert *offset -= iter->len; 2950fa9835e5STom Herbert continue; 2951fa9835e5STom Herbert } 2952fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2953fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2954fa9835e5STom Herbert * case. 2955fa9835e5STom Herbert */ 2956fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2957fa9835e5STom Herbert return true; 2958fa9835e5STom Herbert } 2959fa9835e5STom Herbert 2960a108d5f3SDavid S. Miller return false; 29619c55e01cSJens Axboe } 29629c55e01cSJens Axboe 29639c55e01cSJens Axboe /* 29649c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2965fa9835e5STom Herbert * the fragments, and the frag list. 29669c55e01cSJens Axboe */ 2967a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 29689c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 296925869262SAl Viro unsigned int flags) 29709c55e01cSJens Axboe { 297141c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 297241c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 29739c55e01cSJens Axboe struct splice_pipe_desc spd = { 29749c55e01cSJens Axboe .pages = pages, 29759c55e01cSJens Axboe .partial = partial, 2976047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 297728a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 29789c55e01cSJens Axboe .spd_release = sock_spd_release, 29799c55e01cSJens Axboe }; 298035f3d14dSJens Axboe int ret = 0; 298135f3d14dSJens Axboe 2982fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 29839c55e01cSJens Axboe 2984a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 298525869262SAl Viro ret = splice_to_pipe(pipe, &spd); 29869c55e01cSJens Axboe 298735f3d14dSJens Axboe return ret; 29889c55e01cSJens Axboe } 29892b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 29909c55e01cSJens Axboe 29910739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 29920739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 29930739cd28SCong Wang { 29940739cd28SCong Wang struct socket *sock = sk->sk_socket; 29950739cd28SCong Wang 29960739cd28SCong Wang if (!sock) 29970739cd28SCong Wang return -EINVAL; 29980739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 29990739cd28SCong Wang } 30000739cd28SCong Wang 30010739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 30020739cd28SCong Wang size_t size, int flags) 30030739cd28SCong Wang { 30040739cd28SCong Wang struct socket *sock = sk->sk_socket; 30050739cd28SCong Wang 30060739cd28SCong Wang if (!sock) 30070739cd28SCong Wang return -EINVAL; 30080739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 30090739cd28SCong Wang } 30100739cd28SCong Wang 30110739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 30120739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 30130739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 30140739cd28SCong Wang size_t size, int flags); 30150739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 30160739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 301720bf50deSTom Herbert { 301820bf50deSTom Herbert unsigned int orig_len = len; 301920bf50deSTom Herbert struct sk_buff *head = skb; 302020bf50deSTom Herbert unsigned short fragidx; 302120bf50deSTom Herbert int slen, ret; 302220bf50deSTom Herbert 302320bf50deSTom Herbert do_frag_list: 302420bf50deSTom Herbert 302520bf50deSTom Herbert /* Deal with head data */ 302620bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 302720bf50deSTom Herbert struct kvec kv; 302820bf50deSTom Herbert struct msghdr msg; 302920bf50deSTom Herbert 303020bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 303120bf50deSTom Herbert kv.iov_base = skb->data + offset; 3032db5980d8SJohn Fastabend kv.iov_len = slen; 303320bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 3034bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 303520bf50deSTom Herbert 30360739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 30370739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 303820bf50deSTom Herbert if (ret <= 0) 303920bf50deSTom Herbert goto error; 304020bf50deSTom Herbert 304120bf50deSTom Herbert offset += ret; 304220bf50deSTom Herbert len -= ret; 304320bf50deSTom Herbert } 304420bf50deSTom Herbert 304520bf50deSTom Herbert /* All the data was skb head? */ 304620bf50deSTom Herbert if (!len) 304720bf50deSTom Herbert goto out; 304820bf50deSTom Herbert 304920bf50deSTom Herbert /* Make offset relative to start of frags */ 305020bf50deSTom Herbert offset -= skb_headlen(skb); 305120bf50deSTom Herbert 305220bf50deSTom Herbert /* Find where we are in frag list */ 305320bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 305420bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 305520bf50deSTom Herbert 3056d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 305720bf50deSTom Herbert break; 305820bf50deSTom Herbert 3059d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 306020bf50deSTom Herbert } 306120bf50deSTom Herbert 306220bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 306320bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 306420bf50deSTom Herbert 3065d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 306620bf50deSTom Herbert 306720bf50deSTom Herbert while (slen) { 30680739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 30690739cd28SCong Wang sendpage_unlocked, sk, 30700739cd28SCong Wang skb_frag_page(frag), 3071b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 307220bf50deSTom Herbert slen, MSG_DONTWAIT); 307320bf50deSTom Herbert if (ret <= 0) 307420bf50deSTom Herbert goto error; 307520bf50deSTom Herbert 307620bf50deSTom Herbert len -= ret; 307720bf50deSTom Herbert offset += ret; 307820bf50deSTom Herbert slen -= ret; 307920bf50deSTom Herbert } 308020bf50deSTom Herbert 308120bf50deSTom Herbert offset = 0; 308220bf50deSTom Herbert } 308320bf50deSTom Herbert 308420bf50deSTom Herbert if (len) { 308520bf50deSTom Herbert /* Process any frag lists */ 308620bf50deSTom Herbert 308720bf50deSTom Herbert if (skb == head) { 308820bf50deSTom Herbert if (skb_has_frag_list(skb)) { 308920bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 309020bf50deSTom Herbert goto do_frag_list; 309120bf50deSTom Herbert } 309220bf50deSTom Herbert } else if (skb->next) { 309320bf50deSTom Herbert skb = skb->next; 309420bf50deSTom Herbert goto do_frag_list; 309520bf50deSTom Herbert } 309620bf50deSTom Herbert } 309720bf50deSTom Herbert 309820bf50deSTom Herbert out: 309920bf50deSTom Herbert return orig_len - len; 310020bf50deSTom Herbert 310120bf50deSTom Herbert error: 310220bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 310320bf50deSTom Herbert } 31040739cd28SCong Wang 31050739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 31060739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 31070739cd28SCong Wang int len) 31080739cd28SCong Wang { 31090739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 31100739cd28SCong Wang kernel_sendpage_locked); 31110739cd28SCong Wang } 311220bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 311320bf50deSTom Herbert 31140739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 31150739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 31160739cd28SCong Wang { 31170739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 31180739cd28SCong Wang sendpage_unlocked); 31190739cd28SCong Wang } 31200739cd28SCong Wang 3121357b40a1SHerbert Xu /** 3122357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3123357b40a1SHerbert Xu * @skb: destination buffer 3124357b40a1SHerbert Xu * @offset: offset in destination 3125357b40a1SHerbert Xu * @from: source buffer 3126357b40a1SHerbert Xu * @len: number of bytes to copy 3127357b40a1SHerbert Xu * 3128357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3129357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3130357b40a1SHerbert Xu * traversing fragment lists and such. 3131357b40a1SHerbert Xu */ 3132357b40a1SHerbert Xu 31330c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3134357b40a1SHerbert Xu { 31351a028e50SDavid S. Miller int start = skb_headlen(skb); 3136fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3137fbb398a8SDavid S. Miller int i, copy; 3138357b40a1SHerbert Xu 3139357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3140357b40a1SHerbert Xu goto fault; 3141357b40a1SHerbert Xu 31421a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3143357b40a1SHerbert Xu if (copy > len) 3144357b40a1SHerbert Xu copy = len; 314527d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3146357b40a1SHerbert Xu if ((len -= copy) == 0) 3147357b40a1SHerbert Xu return 0; 3148357b40a1SHerbert Xu offset += copy; 3149357b40a1SHerbert Xu from += copy; 3150357b40a1SHerbert Xu } 3151357b40a1SHerbert Xu 3152357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3153357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31541a028e50SDavid S. Miller int end; 3155357b40a1SHerbert Xu 3156547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31571a028e50SDavid S. Miller 31589e903e08SEric Dumazet end = start + skb_frag_size(frag); 3159357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3160c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3161c613c209SWillem de Bruijn struct page *p; 3162357b40a1SHerbert Xu u8 *vaddr; 3163357b40a1SHerbert Xu 3164357b40a1SHerbert Xu if (copy > len) 3165357b40a1SHerbert Xu copy = len; 3166357b40a1SHerbert Xu 3167c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3168b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3169c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3170c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3171c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 317251c56b00SEric Dumazet kunmap_atomic(vaddr); 3173c613c209SWillem de Bruijn } 3174357b40a1SHerbert Xu 3175357b40a1SHerbert Xu if ((len -= copy) == 0) 3176357b40a1SHerbert Xu return 0; 3177357b40a1SHerbert Xu offset += copy; 3178357b40a1SHerbert Xu from += copy; 3179357b40a1SHerbert Xu } 31801a028e50SDavid S. Miller start = end; 3181357b40a1SHerbert Xu } 3182357b40a1SHerbert Xu 3183fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31841a028e50SDavid S. Miller int end; 3185357b40a1SHerbert Xu 3186547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31871a028e50SDavid S. Miller 3188fbb398a8SDavid S. Miller end = start + frag_iter->len; 3189357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3190357b40a1SHerbert Xu if (copy > len) 3191357b40a1SHerbert Xu copy = len; 3192fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 31931a028e50SDavid S. Miller from, copy)) 3194357b40a1SHerbert Xu goto fault; 3195357b40a1SHerbert Xu if ((len -= copy) == 0) 3196357b40a1SHerbert Xu return 0; 3197357b40a1SHerbert Xu offset += copy; 3198357b40a1SHerbert Xu from += copy; 3199357b40a1SHerbert Xu } 32001a028e50SDavid S. Miller start = end; 3201357b40a1SHerbert Xu } 3202357b40a1SHerbert Xu if (!len) 3203357b40a1SHerbert Xu return 0; 3204357b40a1SHerbert Xu 3205357b40a1SHerbert Xu fault: 3206357b40a1SHerbert Xu return -EFAULT; 3207357b40a1SHerbert Xu } 3208357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3209357b40a1SHerbert Xu 32101da177e4SLinus Torvalds /* Checksum skb data. */ 32112817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 32122817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 32131da177e4SLinus Torvalds { 32141a028e50SDavid S. Miller int start = skb_headlen(skb); 32151a028e50SDavid S. Miller int i, copy = start - offset; 3216fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32171da177e4SLinus Torvalds int pos = 0; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds /* Checksum header. */ 32201da177e4SLinus Torvalds if (copy > 0) { 32211da177e4SLinus Torvalds if (copy > len) 32221da177e4SLinus Torvalds copy = len; 32232544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 32242544af03SMatteo Croce skb->data + offset, copy, csum); 32251da177e4SLinus Torvalds if ((len -= copy) == 0) 32261da177e4SLinus Torvalds return csum; 32271da177e4SLinus Torvalds offset += copy; 32281da177e4SLinus Torvalds pos = copy; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32321a028e50SDavid S. Miller int end; 323351c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32341da177e4SLinus Torvalds 3235547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32361a028e50SDavid S. Miller 323751c56b00SEric Dumazet end = start + skb_frag_size(frag); 32381da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3239c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3240c613c209SWillem de Bruijn struct page *p; 324144bb9363SAl Viro __wsum csum2; 32421da177e4SLinus Torvalds u8 *vaddr; 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds if (copy > len) 32451da177e4SLinus Torvalds copy = len; 3246c613c209SWillem de Bruijn 3247c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3248b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3249c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3250c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 32512544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 32522544af03SMatteo Croce csum_partial_ext, 32532544af03SMatteo Croce vaddr + p_off, p_len, 0); 325451c56b00SEric Dumazet kunmap_atomic(vaddr); 32552544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 32562544af03SMatteo Croce csum_block_add_ext, csum, 32572544af03SMatteo Croce csum2, pos, p_len); 3258c613c209SWillem de Bruijn pos += p_len; 3259c613c209SWillem de Bruijn } 3260c613c209SWillem de Bruijn 32611da177e4SLinus Torvalds if (!(len -= copy)) 32621da177e4SLinus Torvalds return csum; 32631da177e4SLinus Torvalds offset += copy; 32641da177e4SLinus Torvalds } 32651a028e50SDavid S. Miller start = end; 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds 3268fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32691a028e50SDavid S. Miller int end; 32701da177e4SLinus Torvalds 3271547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32721a028e50SDavid S. Miller 3273fbb398a8SDavid S. Miller end = start + frag_iter->len; 32741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32755f92a738SAl Viro __wsum csum2; 32761da177e4SLinus Torvalds if (copy > len) 32771da177e4SLinus Torvalds copy = len; 32782817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 32792817a336SDaniel Borkmann copy, 0, ops); 32802544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 32812544af03SMatteo Croce csum, csum2, pos, copy); 32821da177e4SLinus Torvalds if ((len -= copy) == 0) 32831da177e4SLinus Torvalds return csum; 32841da177e4SLinus Torvalds offset += copy; 32851da177e4SLinus Torvalds pos += copy; 32861da177e4SLinus Torvalds } 32871a028e50SDavid S. Miller start = end; 32881da177e4SLinus Torvalds } 328909a62660SKris Katterjohn BUG_ON(len); 32901da177e4SLinus Torvalds 32911da177e4SLinus Torvalds return csum; 32921da177e4SLinus Torvalds } 32932817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 32942817a336SDaniel Borkmann 32952817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 32962817a336SDaniel Borkmann int len, __wsum csum) 32972817a336SDaniel Borkmann { 32982817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3299cea80ea8SDaniel Borkmann .update = csum_partial_ext, 33002817a336SDaniel Borkmann .combine = csum_block_add_ext, 33012817a336SDaniel Borkmann }; 33022817a336SDaniel Borkmann 33032817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 33042817a336SDaniel Borkmann } 3305b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 33061da177e4SLinus Torvalds 33071da177e4SLinus Torvalds /* Both of above in one bottle. */ 33081da177e4SLinus Torvalds 330981d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 33108d5930dfSAl Viro u8 *to, int len) 33111da177e4SLinus Torvalds { 33121a028e50SDavid S. Miller int start = skb_headlen(skb); 33131a028e50SDavid S. Miller int i, copy = start - offset; 3314fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 33151da177e4SLinus Torvalds int pos = 0; 33168d5930dfSAl Viro __wsum csum = 0; 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds /* Copy header. */ 33191da177e4SLinus Torvalds if (copy > 0) { 33201da177e4SLinus Torvalds if (copy > len) 33211da177e4SLinus Torvalds copy = len; 33221da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3323cc44c17bSAl Viro copy); 33241da177e4SLinus Torvalds if ((len -= copy) == 0) 33251da177e4SLinus Torvalds return csum; 33261da177e4SLinus Torvalds offset += copy; 33271da177e4SLinus Torvalds to += copy; 33281da177e4SLinus Torvalds pos = copy; 33291da177e4SLinus Torvalds } 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33321a028e50SDavid S. Miller int end; 33331da177e4SLinus Torvalds 3334547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33351a028e50SDavid S. Miller 33369e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 33371da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3338c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3339c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3340c613c209SWillem de Bruijn struct page *p; 33415084205fSAl Viro __wsum csum2; 33421da177e4SLinus Torvalds u8 *vaddr; 33431da177e4SLinus Torvalds 33441da177e4SLinus Torvalds if (copy > len) 33451da177e4SLinus Torvalds copy = len; 3346c613c209SWillem de Bruijn 3347c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3348b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3349c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3350c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3351c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3352c613c209SWillem de Bruijn to + copied, 3353cc44c17bSAl Viro p_len); 335451c56b00SEric Dumazet kunmap_atomic(vaddr); 33551da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3356c613c209SWillem de Bruijn pos += p_len; 3357c613c209SWillem de Bruijn } 3358c613c209SWillem de Bruijn 33591da177e4SLinus Torvalds if (!(len -= copy)) 33601da177e4SLinus Torvalds return csum; 33611da177e4SLinus Torvalds offset += copy; 33621da177e4SLinus Torvalds to += copy; 33631da177e4SLinus Torvalds } 33641a028e50SDavid S. Miller start = end; 33651da177e4SLinus Torvalds } 33661da177e4SLinus Torvalds 3367fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 336881d77662SAl Viro __wsum csum2; 33691a028e50SDavid S. Miller int end; 33701da177e4SLinus Torvalds 3371547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33721a028e50SDavid S. Miller 3373fbb398a8SDavid S. Miller end = start + frag_iter->len; 33741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 33751da177e4SLinus Torvalds if (copy > len) 33761da177e4SLinus Torvalds copy = len; 3377fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 33781a028e50SDavid S. Miller offset - start, 33798d5930dfSAl Viro to, copy); 33801da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 33811da177e4SLinus Torvalds if ((len -= copy) == 0) 33821da177e4SLinus Torvalds return csum; 33831da177e4SLinus Torvalds offset += copy; 33841da177e4SLinus Torvalds to += copy; 33851da177e4SLinus Torvalds pos += copy; 33861da177e4SLinus Torvalds } 33871a028e50SDavid S. Miller start = end; 33881da177e4SLinus Torvalds } 338909a62660SKris Katterjohn BUG_ON(len); 33901da177e4SLinus Torvalds return csum; 33911da177e4SLinus Torvalds } 3392b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 33931da177e4SLinus Torvalds 339449f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 339549f8e832SCong Wang { 339649f8e832SCong Wang __sum16 sum; 339749f8e832SCong Wang 339849f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 339914641931SCong Wang /* See comments in __skb_checksum_complete(). */ 340049f8e832SCong Wang if (likely(!sum)) { 340149f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 340249f8e832SCong Wang !skb->csum_complete_sw) 34037fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 340449f8e832SCong Wang } 340549f8e832SCong Wang if (!skb_shared(skb)) 340649f8e832SCong Wang skb->csum_valid = !sum; 340749f8e832SCong Wang return sum; 340849f8e832SCong Wang } 340949f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 341049f8e832SCong Wang 341114641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 341214641931SCong Wang * which has been changed from the hardware checksum, for example, by 341314641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 341414641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 341514641931SCong Wang * 341614641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 341714641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 341814641931SCong Wang * shared. 341914641931SCong Wang */ 342049f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 342149f8e832SCong Wang { 342249f8e832SCong Wang __wsum csum; 342349f8e832SCong Wang __sum16 sum; 342449f8e832SCong Wang 342549f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 342649f8e832SCong Wang 342749f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 342814641931SCong Wang /* This check is inverted, because we already knew the hardware 342914641931SCong Wang * checksum is invalid before calling this function. So, if the 343014641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 343114641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 343214641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 343314641931SCong Wang * when moving skb->data around. 343414641931SCong Wang */ 343549f8e832SCong Wang if (likely(!sum)) { 343649f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 343749f8e832SCong Wang !skb->csum_complete_sw) 34387fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 343949f8e832SCong Wang } 344049f8e832SCong Wang 344149f8e832SCong Wang if (!skb_shared(skb)) { 344249f8e832SCong Wang /* Save full packet checksum */ 344349f8e832SCong Wang skb->csum = csum; 344449f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 344549f8e832SCong Wang skb->csum_complete_sw = 1; 344649f8e832SCong Wang skb->csum_valid = !sum; 344749f8e832SCong Wang } 344849f8e832SCong Wang 344949f8e832SCong Wang return sum; 345049f8e832SCong Wang } 345149f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 345249f8e832SCong Wang 34539617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 34549617813dSDavide Caratti { 34559617813dSDavide Caratti net_warn_ratelimited( 34569617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34579617813dSDavide Caratti __func__); 34589617813dSDavide Caratti return 0; 34599617813dSDavide Caratti } 34609617813dSDavide Caratti 34619617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 34629617813dSDavide Caratti int offset, int len) 34639617813dSDavide Caratti { 34649617813dSDavide Caratti net_warn_ratelimited( 34659617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34669617813dSDavide Caratti __func__); 34679617813dSDavide Caratti return 0; 34689617813dSDavide Caratti } 34699617813dSDavide Caratti 34709617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 34719617813dSDavide Caratti .update = warn_crc32c_csum_update, 34729617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 34739617813dSDavide Caratti }; 34749617813dSDavide Caratti 34759617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 34769617813dSDavide Caratti &default_crc32c_ops; 34779617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 34789617813dSDavide Caratti 3479af2806f8SThomas Graf /** 3480af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3481af2806f8SThomas Graf * @from: source buffer 3482af2806f8SThomas Graf * 3483af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3484af2806f8SThomas Graf * into skb_zerocopy(). 3485af2806f8SThomas Graf */ 3486af2806f8SThomas Graf unsigned int 3487af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3488af2806f8SThomas Graf { 3489af2806f8SThomas Graf unsigned int hlen = 0; 3490af2806f8SThomas Graf 3491af2806f8SThomas Graf if (!from->head_frag || 3492af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3493a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3494af2806f8SThomas Graf hlen = skb_headlen(from); 3495a17ad096SPravin B Shelar if (!hlen) 3496a17ad096SPravin B Shelar hlen = from->len; 3497a17ad096SPravin B Shelar } 3498af2806f8SThomas Graf 3499af2806f8SThomas Graf if (skb_has_frag_list(from)) 3500af2806f8SThomas Graf hlen = from->len; 3501af2806f8SThomas Graf 3502af2806f8SThomas Graf return hlen; 3503af2806f8SThomas Graf } 3504af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3505af2806f8SThomas Graf 3506af2806f8SThomas Graf /** 3507af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3508af2806f8SThomas Graf * @to: destination buffer 35097fceb4deSMasanari Iida * @from: source buffer 3510af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3511af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3512af2806f8SThomas Graf * 3513af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3514af2806f8SThomas Graf * to the frags in the source buffer. 3515af2806f8SThomas Graf * 3516af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3517af2806f8SThomas Graf * headroom in the `to` buffer. 351836d5fe6aSZoltan Kiss * 351936d5fe6aSZoltan Kiss * Return value: 352036d5fe6aSZoltan Kiss * 0: everything is OK 352136d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 352236d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3523af2806f8SThomas Graf */ 352436d5fe6aSZoltan Kiss int 352536d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3526af2806f8SThomas Graf { 3527af2806f8SThomas Graf int i, j = 0; 3528af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 352936d5fe6aSZoltan Kiss int ret; 3530af2806f8SThomas Graf struct page *page; 3531af2806f8SThomas Graf unsigned int offset; 3532af2806f8SThomas Graf 3533af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3534af2806f8SThomas Graf 3535af2806f8SThomas Graf /* dont bother with small payloads */ 353636d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 353736d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3538af2806f8SThomas Graf 3539af2806f8SThomas Graf if (hlen) { 354036d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 354136d5fe6aSZoltan Kiss if (unlikely(ret)) 354236d5fe6aSZoltan Kiss return ret; 3543af2806f8SThomas Graf len -= hlen; 3544af2806f8SThomas Graf } else { 3545af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3546af2806f8SThomas Graf if (plen) { 3547af2806f8SThomas Graf page = virt_to_head_page(from->head); 3548af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3549af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3550af2806f8SThomas Graf get_page(page); 3551af2806f8SThomas Graf j = 1; 3552af2806f8SThomas Graf len -= plen; 3553af2806f8SThomas Graf } 3554af2806f8SThomas Graf } 3555af2806f8SThomas Graf 3556ede57d58SRichard Gobert skb_len_add(to, len + plen); 3557af2806f8SThomas Graf 355836d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 355936d5fe6aSZoltan Kiss skb_tx_error(from); 356036d5fe6aSZoltan Kiss return -ENOMEM; 356136d5fe6aSZoltan Kiss } 35621f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 356336d5fe6aSZoltan Kiss 3564af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3565d8e18a51SMatthew Wilcox (Oracle) int size; 3566d8e18a51SMatthew Wilcox (Oracle) 3567af2806f8SThomas Graf if (!len) 3568af2806f8SThomas Graf break; 3569af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3570d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3571d8e18a51SMatthew Wilcox (Oracle) len); 3572d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3573d8e18a51SMatthew Wilcox (Oracle) len -= size; 3574af2806f8SThomas Graf skb_frag_ref(to, j); 3575af2806f8SThomas Graf j++; 3576af2806f8SThomas Graf } 3577af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 357836d5fe6aSZoltan Kiss 357936d5fe6aSZoltan Kiss return 0; 3580af2806f8SThomas Graf } 3581af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3582af2806f8SThomas Graf 35831da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 35841da177e4SLinus Torvalds { 3585d3bc23e7SAl Viro __wsum csum; 35861da177e4SLinus Torvalds long csstart; 35871da177e4SLinus Torvalds 358884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 358955508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 35901da177e4SLinus Torvalds else 35911da177e4SLinus Torvalds csstart = skb_headlen(skb); 35921da177e4SLinus Torvalds 359309a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 35941da177e4SLinus Torvalds 3595d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 35961da177e4SLinus Torvalds 35971da177e4SLinus Torvalds csum = 0; 35981da177e4SLinus Torvalds if (csstart != skb->len) 35991da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 36008d5930dfSAl Viro skb->len - csstart); 36011da177e4SLinus Torvalds 360284fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3603ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 36041da177e4SLinus Torvalds 3605d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds } 3608b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds /** 36111da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 36121da177e4SLinus Torvalds * @list: list to dequeue from 36131da177e4SLinus Torvalds * 36141da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 36151da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 36161da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36171da177e4SLinus Torvalds */ 36181da177e4SLinus Torvalds 36191da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 36201da177e4SLinus Torvalds { 36211da177e4SLinus Torvalds unsigned long flags; 36221da177e4SLinus Torvalds struct sk_buff *result; 36231da177e4SLinus Torvalds 36241da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36251da177e4SLinus Torvalds result = __skb_dequeue(list); 36261da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36271da177e4SLinus Torvalds return result; 36281da177e4SLinus Torvalds } 3629b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds /** 36321da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 36331da177e4SLinus Torvalds * @list: list to dequeue from 36341da177e4SLinus Torvalds * 36351da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 36361da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 36371da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36381da177e4SLinus Torvalds */ 36391da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 36401da177e4SLinus Torvalds { 36411da177e4SLinus Torvalds unsigned long flags; 36421da177e4SLinus Torvalds struct sk_buff *result; 36431da177e4SLinus Torvalds 36441da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36451da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 36461da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36471da177e4SLinus Torvalds return result; 36481da177e4SLinus Torvalds } 3649b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds /** 36521da177e4SLinus Torvalds * skb_queue_purge - empty a list 36531da177e4SLinus Torvalds * @list: list to empty 36541da177e4SLinus Torvalds * 36551da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 36561da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 36571da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 36581da177e4SLinus Torvalds */ 36591da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 36601da177e4SLinus Torvalds { 36611da177e4SLinus Torvalds struct sk_buff *skb; 36621da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 36631da177e4SLinus Torvalds kfree_skb(skb); 36641da177e4SLinus Torvalds } 3665b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds /** 36689f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 36699f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3670385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 36719f5afeaeSYaogong Wang * 36729f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 36739f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 36749f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 36759f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 36769f5afeaeSYaogong Wang */ 3677385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 36789f5afeaeSYaogong Wang { 36797c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3680385114deSPeter Oskolkov unsigned int sum = 0; 36819f5afeaeSYaogong Wang 36827c90584cSEric Dumazet while (p) { 36837c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 36847c90584cSEric Dumazet 36857c90584cSEric Dumazet p = rb_next(p); 36867c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3687385114deSPeter Oskolkov sum += skb->truesize; 36889f5afeaeSYaogong Wang kfree_skb(skb); 36897c90584cSEric Dumazet } 3690385114deSPeter Oskolkov return sum; 36919f5afeaeSYaogong Wang } 36929f5afeaeSYaogong Wang 36939f5afeaeSYaogong Wang /** 36941da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 36951da177e4SLinus Torvalds * @list: list to use 36961da177e4SLinus Torvalds * @newsk: buffer to queue 36971da177e4SLinus Torvalds * 36981da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 36991da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37001da177e4SLinus Torvalds * safely. 37011da177e4SLinus Torvalds * 37021da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37031da177e4SLinus Torvalds */ 37041da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 37051da177e4SLinus Torvalds { 37061da177e4SLinus Torvalds unsigned long flags; 37071da177e4SLinus Torvalds 37081da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37091da177e4SLinus Torvalds __skb_queue_head(list, newsk); 37101da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37111da177e4SLinus Torvalds } 3712b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 37131da177e4SLinus Torvalds 37141da177e4SLinus Torvalds /** 37151da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 37161da177e4SLinus Torvalds * @list: list to use 37171da177e4SLinus Torvalds * @newsk: buffer to queue 37181da177e4SLinus Torvalds * 37191da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 37201da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37211da177e4SLinus Torvalds * safely. 37221da177e4SLinus Torvalds * 37231da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37241da177e4SLinus Torvalds */ 37251da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 37261da177e4SLinus Torvalds { 37271da177e4SLinus Torvalds unsigned long flags; 37281da177e4SLinus Torvalds 37291da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37301da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 37311da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37321da177e4SLinus Torvalds } 3733b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 37348728b834SDavid S. Miller 37351da177e4SLinus Torvalds /** 37361da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 37371da177e4SLinus Torvalds * @skb: buffer to remove 37388728b834SDavid S. Miller * @list: list to use 37391da177e4SLinus Torvalds * 37408728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 37418728b834SDavid S. Miller * function is atomic with respect to other list locked calls 37421da177e4SLinus Torvalds * 37438728b834SDavid S. Miller * You must know what list the SKB is on. 37441da177e4SLinus Torvalds */ 37458728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 37461da177e4SLinus Torvalds { 37471da177e4SLinus Torvalds unsigned long flags; 37481da177e4SLinus Torvalds 37491da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37508728b834SDavid S. Miller __skb_unlink(skb, list); 37511da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37521da177e4SLinus Torvalds } 3753b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 37541da177e4SLinus Torvalds 37551da177e4SLinus Torvalds /** 37561da177e4SLinus Torvalds * skb_append - append a buffer 37571da177e4SLinus Torvalds * @old: buffer to insert after 37581da177e4SLinus Torvalds * @newsk: buffer to insert 37598728b834SDavid S. Miller * @list: list to use 37601da177e4SLinus Torvalds * 37611da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 37621da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 37631da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37641da177e4SLinus Torvalds */ 37658728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 37661da177e4SLinus Torvalds { 37671da177e4SLinus Torvalds unsigned long flags; 37681da177e4SLinus Torvalds 37698728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 37707de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 37718728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 37721da177e4SLinus Torvalds } 3773b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 37741da177e4SLinus Torvalds 37751da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 37761da177e4SLinus Torvalds struct sk_buff* skb1, 37771da177e4SLinus Torvalds const u32 len, const int pos) 37781da177e4SLinus Torvalds { 37791da177e4SLinus Torvalds int i; 37801da177e4SLinus Torvalds 3781d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3782d626f62bSArnaldo Carvalho de Melo pos - len); 37831da177e4SLinus Torvalds /* And move data appendix as is. */ 37841da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 37851da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 37861da177e4SLinus Torvalds 37871da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 37881da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 37891da177e4SLinus Torvalds skb1->data_len = skb->data_len; 37901da177e4SLinus Torvalds skb1->len += skb1->data_len; 37911da177e4SLinus Torvalds skb->data_len = 0; 37921da177e4SLinus Torvalds skb->len = len; 379327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 37941da177e4SLinus Torvalds } 37951da177e4SLinus Torvalds 37961da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 37971da177e4SLinus Torvalds struct sk_buff* skb1, 37981da177e4SLinus Torvalds const u32 len, int pos) 37991da177e4SLinus Torvalds { 38001da177e4SLinus Torvalds int i, k = 0; 38011da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 38021da177e4SLinus Torvalds 38031da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38041da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 38051da177e4SLinus Torvalds skb->len = len; 38061da177e4SLinus Torvalds skb->data_len = len - pos; 38071da177e4SLinus Torvalds 38081da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 38099e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds if (pos + size > len) { 38121da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds if (pos < len) { 38151da177e4SLinus Torvalds /* Split frag. 38161da177e4SLinus Torvalds * We have two variants in this case: 38171da177e4SLinus Torvalds * 1. Move all the frag to the second 38181da177e4SLinus Torvalds * part, if it is possible. F.e. 38191da177e4SLinus Torvalds * this approach is mandatory for TUX, 38201da177e4SLinus Torvalds * where splitting is expensive. 38211da177e4SLinus Torvalds * 2. Split is accurately. We make this. 38221da177e4SLinus Torvalds */ 3823ea2ab693SIan Campbell skb_frag_ref(skb, i); 3824b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 38259e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 38269e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 38271da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38281da177e4SLinus Torvalds } 38291da177e4SLinus Torvalds k++; 38301da177e4SLinus Torvalds } else 38311da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38321da177e4SLinus Torvalds pos += size; 38331da177e4SLinus Torvalds } 38341da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 38351da177e4SLinus Torvalds } 38361da177e4SLinus Torvalds 38371da177e4SLinus Torvalds /** 38381da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 38391da177e4SLinus Torvalds * @skb: the buffer to split 38401da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 38411da177e4SLinus Torvalds * @len: new length for skb 38421da177e4SLinus Torvalds */ 38431da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 38441da177e4SLinus Torvalds { 38451da177e4SLinus Torvalds int pos = skb_headlen(skb); 38469b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 38471da177e4SLinus Torvalds 3848753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3849753f1ca4SPavel Begunkov 38509b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 38511f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 38521da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 38531da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 38541da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 38551da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 38561da177e4SLinus Torvalds } 3857b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 38581da177e4SLinus Torvalds 38599f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 38609f782db3SIlpo Järvinen * 38619f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 38629f782db3SIlpo Järvinen */ 3863832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3864832d11c5SIlpo Järvinen { 3865c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3866832d11c5SIlpo Järvinen } 3867832d11c5SIlpo Järvinen 3868832d11c5SIlpo Järvinen /** 3869832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3870832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3871832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3872832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3873832d11c5SIlpo Järvinen * 3874832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 387520e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3876832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3877832d11c5SIlpo Järvinen * 3878832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3879832d11c5SIlpo Järvinen * 3880832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3881832d11c5SIlpo Järvinen * to have non-paged data as well. 3882832d11c5SIlpo Järvinen * 3883832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3884832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3885832d11c5SIlpo Järvinen */ 3886832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3887832d11c5SIlpo Järvinen { 3888832d11c5SIlpo Järvinen int from, to, merge, todo; 3889d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3890832d11c5SIlpo Järvinen 3891832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3892f8071cdeSEric Dumazet 3893f8071cdeSEric Dumazet if (skb_headlen(skb)) 3894f8071cdeSEric Dumazet return 0; 38951f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 38961f8b977aSWillem de Bruijn return 0; 3897832d11c5SIlpo Järvinen 3898832d11c5SIlpo Järvinen todo = shiftlen; 3899832d11c5SIlpo Järvinen from = 0; 3900832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3901832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3902832d11c5SIlpo Järvinen 3903832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3904832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3905832d11c5SIlpo Järvinen */ 3906832d11c5SIlpo Järvinen if (!to || 3907ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3908b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3909832d11c5SIlpo Järvinen merge = -1; 3910832d11c5SIlpo Järvinen } else { 3911832d11c5SIlpo Järvinen merge = to - 1; 3912832d11c5SIlpo Järvinen 39139e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3914832d11c5SIlpo Järvinen if (todo < 0) { 3915832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3916832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3917832d11c5SIlpo Järvinen return 0; 3918832d11c5SIlpo Järvinen 39199f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 39209f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3921832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3922832d11c5SIlpo Järvinen 39239e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 39249e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3925b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3926832d11c5SIlpo Järvinen 3927832d11c5SIlpo Järvinen goto onlymerged; 3928832d11c5SIlpo Järvinen } 3929832d11c5SIlpo Järvinen 3930832d11c5SIlpo Järvinen from++; 3931832d11c5SIlpo Järvinen } 3932832d11c5SIlpo Järvinen 3933832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3934832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3935832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3936832d11c5SIlpo Järvinen return 0; 3937832d11c5SIlpo Järvinen 3938832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3939832d11c5SIlpo Järvinen return 0; 3940832d11c5SIlpo Järvinen 3941832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3942832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3943832d11c5SIlpo Järvinen return 0; 3944832d11c5SIlpo Järvinen 3945832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3946832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3947832d11c5SIlpo Järvinen 39489e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3949832d11c5SIlpo Järvinen *fragto = *fragfrom; 39509e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3951832d11c5SIlpo Järvinen from++; 3952832d11c5SIlpo Järvinen to++; 3953832d11c5SIlpo Järvinen 3954832d11c5SIlpo Järvinen } else { 3955ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3956b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3957b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 39589e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3959832d11c5SIlpo Järvinen 3960b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 39619e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3962832d11c5SIlpo Järvinen todo = 0; 3963832d11c5SIlpo Järvinen 3964832d11c5SIlpo Järvinen to++; 3965832d11c5SIlpo Järvinen break; 3966832d11c5SIlpo Järvinen } 3967832d11c5SIlpo Järvinen } 3968832d11c5SIlpo Järvinen 3969832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3970832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3971832d11c5SIlpo Järvinen 3972832d11c5SIlpo Järvinen if (merge >= 0) { 3973832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3974832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3975832d11c5SIlpo Järvinen 39769e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 39776a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3978832d11c5SIlpo Järvinen } 3979832d11c5SIlpo Järvinen 3980832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3981832d11c5SIlpo Järvinen to = 0; 3982832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3983832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3984832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3985832d11c5SIlpo Järvinen 3986832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3987832d11c5SIlpo Järvinen 3988832d11c5SIlpo Järvinen onlymerged: 3989832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3990832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3991832d11c5SIlpo Järvinen */ 3992832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3993832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3994832d11c5SIlpo Järvinen 3995ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 3996ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 3997832d11c5SIlpo Järvinen 3998832d11c5SIlpo Järvinen return shiftlen; 3999832d11c5SIlpo Järvinen } 4000832d11c5SIlpo Järvinen 4001677e90edSThomas Graf /** 4002677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 4003677e90edSThomas Graf * @skb: the buffer to read 4004677e90edSThomas Graf * @from: lower offset of data to be read 4005677e90edSThomas Graf * @to: upper offset of data to be read 4006677e90edSThomas Graf * @st: state variable 4007677e90edSThomas Graf * 4008677e90edSThomas Graf * Initializes the specified state variable. Must be called before 4009677e90edSThomas Graf * invoking skb_seq_read() for the first time. 4010677e90edSThomas Graf */ 4011677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 4012677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 4013677e90edSThomas Graf { 4014677e90edSThomas Graf st->lower_offset = from; 4015677e90edSThomas Graf st->upper_offset = to; 4016677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 4017677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 4018677e90edSThomas Graf st->frag_data = NULL; 401997550f6fSWillem de Bruijn st->frag_off = 0; 4020677e90edSThomas Graf } 4021b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 4022677e90edSThomas Graf 4023677e90edSThomas Graf /** 4024677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 4025677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 4026677e90edSThomas Graf * @data: destination pointer for data to be returned 4027677e90edSThomas Graf * @st: state variable 4028677e90edSThomas Graf * 4029bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 4030677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 4031bc32383cSMathias Krause * the head of the data block to @data and returns the length 4032677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 4033677e90edSThomas Graf * offset has been reached. 4034677e90edSThomas Graf * 4035677e90edSThomas Graf * The caller is not required to consume all of the data 4036bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 4037677e90edSThomas Graf * of bytes already consumed and the next call to 4038677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 4039677e90edSThomas Graf * 404025985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 4041e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 4042677e90edSThomas Graf * reads of potentially non linear data. 4043677e90edSThomas Graf * 4044bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 4045677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 4046677e90edSThomas Graf * a stack for this purpose. 4047677e90edSThomas Graf */ 4048677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 4049677e90edSThomas Graf struct skb_seq_state *st) 4050677e90edSThomas Graf { 4051677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 4052677e90edSThomas Graf skb_frag_t *frag; 4053677e90edSThomas Graf 4054aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 4055aeb193eaSWedson Almeida Filho if (st->frag_data) { 4056aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 4057aeb193eaSWedson Almeida Filho st->frag_data = NULL; 4058aeb193eaSWedson Almeida Filho } 4059677e90edSThomas Graf return 0; 4060aeb193eaSWedson Almeida Filho } 4061677e90edSThomas Graf 4062677e90edSThomas Graf next_skb: 406395e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 4064677e90edSThomas Graf 4065995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 406695e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 4067677e90edSThomas Graf return block_limit - abs_offset; 4068677e90edSThomas Graf } 4069677e90edSThomas Graf 4070677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 4071677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 4072677e90edSThomas Graf 4073677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 407497550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 4075677e90edSThomas Graf 407697550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 407797550f6fSWillem de Bruijn 407897550f6fSWillem de Bruijn pg_idx = 0; 407997550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 408097550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 408197550f6fSWillem de Bruijn 408297550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 408397550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 408497550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 408597550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 408697550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 408797550f6fSWillem de Bruijn } 408897550f6fSWillem de Bruijn 408997550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 4090677e90edSThomas Graf if (abs_offset < block_limit) { 4091677e90edSThomas Graf if (!st->frag_data) 409297550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 4093677e90edSThomas Graf 409497550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 4095677e90edSThomas Graf (abs_offset - st->stepped_offset); 4096677e90edSThomas Graf 4097677e90edSThomas Graf return block_limit - abs_offset; 4098677e90edSThomas Graf } 4099677e90edSThomas Graf 4100677e90edSThomas Graf if (st->frag_data) { 410151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4102677e90edSThomas Graf st->frag_data = NULL; 4103677e90edSThomas Graf } 4104677e90edSThomas Graf 410597550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 410697550f6fSWillem de Bruijn st->frag_off += pg_sz; 410797550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 410897550f6fSWillem de Bruijn st->frag_off = 0; 4109677e90edSThomas Graf st->frag_idx++; 411097550f6fSWillem de Bruijn } 4111677e90edSThomas Graf } 4112677e90edSThomas Graf 41135b5a60daSOlaf Kirch if (st->frag_data) { 411451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 41155b5a60daSOlaf Kirch st->frag_data = NULL; 41165b5a60daSOlaf Kirch } 41175b5a60daSOlaf Kirch 411821dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4119677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 412095e3b24cSHerbert Xu st->frag_idx = 0; 4121677e90edSThomas Graf goto next_skb; 412271b3346dSShyam Iyer } else if (st->cur_skb->next) { 412371b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 412471b3346dSShyam Iyer st->frag_idx = 0; 4125677e90edSThomas Graf goto next_skb; 4126677e90edSThomas Graf } 4127677e90edSThomas Graf 4128677e90edSThomas Graf return 0; 4129677e90edSThomas Graf } 4130b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4131677e90edSThomas Graf 4132677e90edSThomas Graf /** 4133677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4134677e90edSThomas Graf * @st: state variable 4135677e90edSThomas Graf * 4136677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4137677e90edSThomas Graf * returned 0. 4138677e90edSThomas Graf */ 4139677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4140677e90edSThomas Graf { 4141677e90edSThomas Graf if (st->frag_data) 414251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4143677e90edSThomas Graf } 4144b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4145677e90edSThomas Graf 41463fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 41473fc7e8a6SThomas Graf 41483fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 41493fc7e8a6SThomas Graf struct ts_config *conf, 41503fc7e8a6SThomas Graf struct ts_state *state) 41513fc7e8a6SThomas Graf { 41523fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 41533fc7e8a6SThomas Graf } 41543fc7e8a6SThomas Graf 41553fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 41563fc7e8a6SThomas Graf { 41573fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 41583fc7e8a6SThomas Graf } 41593fc7e8a6SThomas Graf 41603fc7e8a6SThomas Graf /** 41613fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 41623fc7e8a6SThomas Graf * @skb: the buffer to look in 41633fc7e8a6SThomas Graf * @from: search offset 41643fc7e8a6SThomas Graf * @to: search limit 41653fc7e8a6SThomas Graf * @config: textsearch configuration 41663fc7e8a6SThomas Graf * 41673fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 41683fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 41693fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 41703fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 41713fc7e8a6SThomas Graf */ 41723fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4173059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 41743fc7e8a6SThomas Graf { 4175059a2440SBojan Prtvar struct ts_state state; 4176f72b948dSPhil Oester unsigned int ret; 4177f72b948dSPhil Oester 4178b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4179b228c9b0SWillem de Bruijn 41803fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 41813fc7e8a6SThomas Graf config->finish = skb_ts_finish; 41823fc7e8a6SThomas Graf 4183059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 41843fc7e8a6SThomas Graf 4185059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4186f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 41873fc7e8a6SThomas Graf } 4188b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 41893fc7e8a6SThomas Graf 4190be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4191*96449f90SDavid Howells int offset, size_t size, size_t max_frags) 4192be12a1feSHannes Frederic Sowa { 4193be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4194be12a1feSHannes Frederic Sowa 4195be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4196be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4197*96449f90SDavid Howells } else if (i < max_frags) { 4198753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4199be12a1feSHannes Frederic Sowa get_page(page); 4200228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4201be12a1feSHannes Frederic Sowa } else { 4202be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4203be12a1feSHannes Frederic Sowa } 4204be12a1feSHannes Frederic Sowa 4205be12a1feSHannes Frederic Sowa return 0; 4206be12a1feSHannes Frederic Sowa } 4207be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4208be12a1feSHannes Frederic Sowa 4209cbb042f9SHerbert Xu /** 4210cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4211cbb042f9SHerbert Xu * @skb: buffer to update 4212cbb042f9SHerbert Xu * @len: length of data pulled 4213cbb042f9SHerbert Xu * 4214cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4215fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 421684fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 421784fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 421884fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4219cbb042f9SHerbert Xu */ 4220af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4221cbb042f9SHerbert Xu { 422231b33dfbSPravin B Shelar unsigned char *data = skb->data; 422331b33dfbSPravin B Shelar 4224cbb042f9SHerbert Xu BUG_ON(len > skb->len); 422531b33dfbSPravin B Shelar __skb_pull(skb, len); 422631b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 422731b33dfbSPravin B Shelar return skb->data; 4228cbb042f9SHerbert Xu } 4229f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4230f94691acSArnaldo Carvalho de Melo 423113acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 423213acc94eSYonghong Song { 423313acc94eSYonghong Song skb_frag_t head_frag; 423413acc94eSYonghong Song struct page *page; 423513acc94eSYonghong Song 423613acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4237b51f4113SYunsheng Lin skb_frag_fill_page_desc(&head_frag, page, frag_skb->data - 4238b51f4113SYunsheng Lin (unsigned char *)page_address(page), 4239b51f4113SYunsheng Lin skb_headlen(frag_skb)); 424013acc94eSYonghong Song return head_frag; 424113acc94eSYonghong Song } 424213acc94eSYonghong Song 42433a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 42443a1296a3SSteffen Klassert netdev_features_t features, 42453a1296a3SSteffen Klassert unsigned int offset) 42463a1296a3SSteffen Klassert { 42473a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 42483a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 42493a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 42503a1296a3SSteffen Klassert unsigned int delta_len = 0; 42513a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 425253475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4253cf3ab8d4SLina Wang int len_diff, err; 42543a1296a3SSteffen Klassert 42553a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 42563a1296a3SSteffen Klassert 42573a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 42583a1296a3SSteffen Klassert 4259876e8ca8SYan Zhai while (list_skb) { 42603a1296a3SSteffen Klassert nskb = list_skb; 42613a1296a3SSteffen Klassert list_skb = list_skb->next; 42623a1296a3SSteffen Klassert 426353475c5dSDongseok Yi err = 0; 4264224102deSlena wang delta_truesize += nskb->truesize; 426553475c5dSDongseok Yi if (skb_shared(nskb)) { 426653475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 426753475c5dSDongseok Yi if (tmp) { 426853475c5dSDongseok Yi consume_skb(nskb); 426953475c5dSDongseok Yi nskb = tmp; 427053475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 427153475c5dSDongseok Yi } else { 427253475c5dSDongseok Yi err = -ENOMEM; 427353475c5dSDongseok Yi } 427453475c5dSDongseok Yi } 427553475c5dSDongseok Yi 42763a1296a3SSteffen Klassert if (!tail) 42773a1296a3SSteffen Klassert skb->next = nskb; 42783a1296a3SSteffen Klassert else 42793a1296a3SSteffen Klassert tail->next = nskb; 42803a1296a3SSteffen Klassert 428153475c5dSDongseok Yi if (unlikely(err)) { 428253475c5dSDongseok Yi nskb->next = list_skb; 428353475c5dSDongseok Yi goto err_linearize; 428453475c5dSDongseok Yi } 428553475c5dSDongseok Yi 42863a1296a3SSteffen Klassert tail = nskb; 42873a1296a3SSteffen Klassert 42883a1296a3SSteffen Klassert delta_len += nskb->len; 42893a1296a3SSteffen Klassert 42903a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 42913a1296a3SSteffen Klassert 4292cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4293cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 42943a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 42953a1296a3SSteffen Klassert 42963a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4297cf3ab8d4SLina Wang nskb->transport_header += len_diff; 42983a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 42993a1296a3SSteffen Klassert nskb->data - tnl_hlen, 43003a1296a3SSteffen Klassert offset + tnl_hlen); 43013a1296a3SSteffen Klassert 43023a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 43033a1296a3SSteffen Klassert __skb_linearize(nskb)) 43043a1296a3SSteffen Klassert goto err_linearize; 4305876e8ca8SYan Zhai } 43063a1296a3SSteffen Klassert 43073a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 43083a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 43093a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 43103a1296a3SSteffen Klassert 43113a1296a3SSteffen Klassert skb_gso_reset(skb); 43123a1296a3SSteffen Klassert 43133a1296a3SSteffen Klassert skb->prev = tail; 43143a1296a3SSteffen Klassert 43153a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 43163a1296a3SSteffen Klassert __skb_linearize(skb)) 43173a1296a3SSteffen Klassert goto err_linearize; 43183a1296a3SSteffen Klassert 43193a1296a3SSteffen Klassert skb_get(skb); 43203a1296a3SSteffen Klassert 43213a1296a3SSteffen Klassert return skb; 43223a1296a3SSteffen Klassert 43233a1296a3SSteffen Klassert err_linearize: 43243a1296a3SSteffen Klassert kfree_skb_list(skb->next); 43253a1296a3SSteffen Klassert skb->next = NULL; 43263a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 43273a1296a3SSteffen Klassert } 43283a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 43293a1296a3SSteffen Klassert 4330f4c50d99SHerbert Xu /** 4331f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4332df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4333576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4334f4c50d99SHerbert Xu * 4335f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 43364c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 43374c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4338f4c50d99SHerbert Xu */ 4339df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4340df5771ffSMichael S. Tsirkin netdev_features_t features) 4341f4c50d99SHerbert Xu { 4342f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4343f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 43441a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4345df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4346df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4347df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 43481fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4349f4c50d99SHerbert Xu unsigned int offset = doffset; 4350df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4351802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4352f4c50d99SHerbert Xu unsigned int headroom; 4353802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4354ec5f0615SPravin B Shelar __be16 proto; 435536c98382SAlexander Duyck bool csum, sg; 4356df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4357f4c50d99SHerbert Xu int err = -ENOMEM; 4358f4c50d99SHerbert Xu int i = 0; 4359f4c50d99SHerbert Xu int pos; 4360f4c50d99SHerbert Xu 43619e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 43629e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 43639e4b7a99SJiri Benc struct sk_buff *check_skb; 43649e4b7a99SJiri Benc 43659e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 43669e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 43679e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 43689e4b7a99SJiri Benc * a linear non head_frag item. 43693dcbdb13SShmulik Ladkani * 43709e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 43719e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 43729e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 43739e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 43749e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 43753dcbdb13SShmulik Ladkani */ 43763dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 43779e4b7a99SJiri Benc break; 43789e4b7a99SJiri Benc } 43799e4b7a99SJiri Benc } 43803dcbdb13SShmulik Ladkani } 43813dcbdb13SShmulik Ladkani 43825882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 43832f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4384ec5f0615SPravin B Shelar if (unlikely(!proto)) 4385ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4386ec5f0615SPravin B Shelar 438736c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4388f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 43897e2b10c1STom Herbert 439007b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 439107b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 439207b26c94SSteffen Klassert struct sk_buff *iter; 439343170c4eSIlan Tayari unsigned int frag_len; 439407b26c94SSteffen Klassert 439507b26c94SSteffen Klassert if (!list_skb || 439607b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 439707b26c94SSteffen Klassert goto normal; 439807b26c94SSteffen Klassert 439943170c4eSIlan Tayari /* If we get here then all the required 440043170c4eSIlan Tayari * GSO features except frag_list are supported. 440143170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 440243170c4eSIlan Tayari * with no frag_list. 440343170c4eSIlan Tayari * Currently we can do that only when the buffers don't 440443170c4eSIlan Tayari * have a linear part and all the buffers except 440543170c4eSIlan Tayari * the last are of the same length. 440607b26c94SSteffen Klassert */ 440743170c4eSIlan Tayari frag_len = list_skb->len; 440807b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 440943170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 441043170c4eSIlan Tayari goto normal; 4411eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 441207b26c94SSteffen Klassert goto normal; 441307b26c94SSteffen Klassert 441407b26c94SSteffen Klassert len -= iter->len; 441507b26c94SSteffen Klassert } 441643170c4eSIlan Tayari 441743170c4eSIlan Tayari if (len != frag_len) 441843170c4eSIlan Tayari goto normal; 441907b26c94SSteffen Klassert } 442007b26c94SSteffen Klassert 4421802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4422802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4423802ab55aSAlexander Duyck * now. 4424802ab55aSAlexander Duyck */ 4425802ab55aSAlexander Duyck partial_segs = len / mss; 4426d7fb5a80SAlexander Duyck if (partial_segs > 1) 4427802ab55aSAlexander Duyck mss *= partial_segs; 4428d7fb5a80SAlexander Duyck else 4429d7fb5a80SAlexander Duyck partial_segs = 0; 4430802ab55aSAlexander Duyck } 4431802ab55aSAlexander Duyck 443207b26c94SSteffen Klassert normal: 4433df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4434df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4435f4c50d99SHerbert Xu 4436f4c50d99SHerbert Xu do { 4437f4c50d99SHerbert Xu struct sk_buff *nskb; 44388cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4439c8884eddSHerbert Xu int hsize; 4440f4c50d99SHerbert Xu int size; 4441f4c50d99SHerbert Xu 44423953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 44433953c46cSMarcelo Ricardo Leitner len = list_skb->len; 44443953c46cSMarcelo Ricardo Leitner } else { 4445df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4446f4c50d99SHerbert Xu if (len > mss) 4447f4c50d99SHerbert Xu len = mss; 44483953c46cSMarcelo Ricardo Leitner } 4449f4c50d99SHerbert Xu 4450df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4451f4c50d99SHerbert Xu 4452dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 44531a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 44541a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 445589319d38SHerbert Xu 44569d8506ccSHerbert Xu i = 0; 44571a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44581a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44591fd819ecSMichael S. Tsirkin frag_skb = list_skb; 44601a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 44619d8506ccSHerbert Xu 44629d8506ccSHerbert Xu while (pos < offset + len) { 44639d8506ccSHerbert Xu BUG_ON(i >= nfrags); 44649d8506ccSHerbert Xu 44654e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 44669d8506ccSHerbert Xu if (pos + size > offset + len) 44679d8506ccSHerbert Xu break; 44689d8506ccSHerbert Xu 44699d8506ccSHerbert Xu i++; 44709d8506ccSHerbert Xu pos += size; 44714e1beba1SMichael S. Tsirkin frag++; 44729d8506ccSHerbert Xu } 44739d8506ccSHerbert Xu 44741a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 44751a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 447689319d38SHerbert Xu 4477f4c50d99SHerbert Xu if (unlikely(!nskb)) 4478f4c50d99SHerbert Xu goto err; 4479f4c50d99SHerbert Xu 44809d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 44819d8506ccSHerbert Xu kfree_skb(nskb); 44829d8506ccSHerbert Xu goto err; 44839d8506ccSHerbert Xu } 44849d8506ccSHerbert Xu 4485ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 448689319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 448789319d38SHerbert Xu kfree_skb(nskb); 448889319d38SHerbert Xu goto err; 448989319d38SHerbert Xu } 449089319d38SHerbert Xu 4491ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 449289319d38SHerbert Xu skb_release_head_state(nskb); 449389319d38SHerbert Xu __skb_push(nskb, doffset); 449489319d38SHerbert Xu } else { 449500b229f7SPaolo Abeni if (hsize < 0) 449600b229f7SPaolo Abeni hsize = 0; 4497dbd50f23SXin Long if (hsize > len || !sg) 4498dbd50f23SXin Long hsize = len; 4499dbd50f23SXin Long 4500c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4501df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4502c93bdd0eSMel Gorman NUMA_NO_NODE); 450389319d38SHerbert Xu 450489319d38SHerbert Xu if (unlikely(!nskb)) 450589319d38SHerbert Xu goto err; 450689319d38SHerbert Xu 450789319d38SHerbert Xu skb_reserve(nskb, headroom); 450889319d38SHerbert Xu __skb_put(nskb, doffset); 450989319d38SHerbert Xu } 451089319d38SHerbert Xu 4511f4c50d99SHerbert Xu if (segs) 4512f4c50d99SHerbert Xu tail->next = nskb; 4513f4c50d99SHerbert Xu else 4514f4c50d99SHerbert Xu segs = nskb; 4515f4c50d99SHerbert Xu tail = nskb; 4516f4c50d99SHerbert Xu 4517df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4518f4c50d99SHerbert Xu 4519030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4520fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 452168c33163SPravin B Shelar 4522df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 452368c33163SPravin B Shelar nskb->data - tnl_hlen, 452468c33163SPravin B Shelar doffset + tnl_hlen); 452589319d38SHerbert Xu 45269d8506ccSHerbert Xu if (nskb->len == len + doffset) 45271cdbcb79SSimon Horman goto perform_csum_check; 452889319d38SHerbert Xu 45297fbeffedSAlexander Duyck if (!sg) { 45301454c9faSYadu Kishore if (!csum) { 45317fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 45326f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 453376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 453476443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 45351454c9faSYadu Kishore skb_put(nskb, 45361454c9faSYadu Kishore len), 45378d5930dfSAl Viro len); 45387e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4539de843723STom Herbert skb_headroom(nskb) + doffset; 45401454c9faSYadu Kishore } else { 4541c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4542c624c58eSlily goto err; 45431454c9faSYadu Kishore } 4544f4c50d99SHerbert Xu continue; 4545f4c50d99SHerbert Xu } 4546f4c50d99SHerbert Xu 45478cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4548f4c50d99SHerbert Xu 4549df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4550d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4551f4c50d99SHerbert Xu 455206b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 455306b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4554cef401deSEric Dumazet 4555bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4556bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4557bf5c25d6SWillem de Bruijn goto err; 4558bf5c25d6SWillem de Bruijn 45599d8506ccSHerbert Xu while (pos < offset + len) { 45609d8506ccSHerbert Xu if (i >= nfrags) { 45619d8506ccSHerbert Xu i = 0; 45621a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 45631a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 45641fd819ecSMichael S. Tsirkin frag_skb = list_skb; 456513acc94eSYonghong Song if (!skb_headlen(list_skb)) { 45669d8506ccSHerbert Xu BUG_ON(!nfrags); 456713acc94eSYonghong Song } else { 456813acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 45699d8506ccSHerbert Xu 457013acc94eSYonghong Song /* to make room for head_frag. */ 457113acc94eSYonghong Song i--; 457213acc94eSYonghong Song frag--; 457313acc94eSYonghong Song } 4574bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4575bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4576bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4577bf5c25d6SWillem de Bruijn goto err; 4578bf5c25d6SWillem de Bruijn 45791a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 45809d8506ccSHerbert Xu } 45819d8506ccSHerbert Xu 45829d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 45839d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 45849d8506ccSHerbert Xu net_warn_ratelimited( 45859d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 45869d8506ccSHerbert Xu pos, mss); 4587ff907a11SEric Dumazet err = -EINVAL; 45889d8506ccSHerbert Xu goto err; 45899d8506ccSHerbert Xu } 45909d8506ccSHerbert Xu 459113acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 45928cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 45938cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4594f4c50d99SHerbert Xu 4595f4c50d99SHerbert Xu if (pos < offset) { 4596b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 45978cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4598f4c50d99SHerbert Xu } 4599f4c50d99SHerbert Xu 460089319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4601f4c50d99SHerbert Xu 4602f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4603f4c50d99SHerbert Xu i++; 46044e1beba1SMichael S. Tsirkin frag++; 4605f4c50d99SHerbert Xu pos += size; 4606f4c50d99SHerbert Xu } else { 46078cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 460889319d38SHerbert Xu goto skip_fraglist; 4609f4c50d99SHerbert Xu } 4610f4c50d99SHerbert Xu 46118cb19905SMichael S. Tsirkin nskb_frag++; 4612f4c50d99SHerbert Xu } 4613f4c50d99SHerbert Xu 461489319d38SHerbert Xu skip_fraglist: 4615f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4616f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4617f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4618ec5f0615SPravin B Shelar 46191cdbcb79SSimon Horman perform_csum_check: 46207fbeffedSAlexander Duyck if (!csum) { 4621ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4622ff907a11SEric Dumazet __skb_linearize(nskb)) 4623ddff00d4SAlexander Duyck goto err; 4624ff907a11SEric Dumazet 46257fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4626ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 462776443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 462876443456SAlexander Duyck skb_checksum(nskb, doffset, 462976443456SAlexander Duyck nskb->len - doffset, 0); 46307e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 46317e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4632ec5f0615SPravin B Shelar } 4633df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4634f4c50d99SHerbert Xu 4635bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4636bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4637bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4638bec3cfdcSEric Dumazet */ 4639bec3cfdcSEric Dumazet segs->prev = tail; 4640432c856fSToshiaki Makita 4641802ab55aSAlexander Duyck if (partial_segs) { 464207b26c94SSteffen Klassert struct sk_buff *iter; 4643802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 464407b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4645802ab55aSAlexander Duyck 4646802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 464707b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4648802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4649802ab55aSAlexander Duyck 4650802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4651802ab55aSAlexander Duyck * our way back down the stack of protocols. 4652802ab55aSAlexander Duyck */ 465307b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 465407b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 465507b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 465607b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 465707b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 465807b26c94SSteffen Klassert } 465907b26c94SSteffen Klassert 466007b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 466107b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 466207b26c94SSteffen Klassert else if (tail != segs) 466307b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4664802ab55aSAlexander Duyck } 4665802ab55aSAlexander Duyck 4666432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4667432c856fSToshiaki Makita * using skb_set_owner_w(). 4668432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4669432c856fSToshiaki Makita */ 4670432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4671432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4672432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4673432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4674432c856fSToshiaki Makita } 4675f4c50d99SHerbert Xu return segs; 4676f4c50d99SHerbert Xu 4677f4c50d99SHerbert Xu err: 4678289dccbeSEric Dumazet kfree_skb_list(segs); 4679f4c50d99SHerbert Xu return ERR_PTR(err); 4680f4c50d99SHerbert Xu } 4681f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4682f4c50d99SHerbert Xu 4683df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4684df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4685df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4686df5042f4SFlorian Westphal 4687df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4688df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4689df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4690df5042f4SFlorian Westphal #endif 46914165079bSFlorian Westphal #ifdef CONFIG_XFRM 46924165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 46934165079bSFlorian Westphal #endif 469495a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 469595a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 469695a7233cSPaul Blakey #endif 46973ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 46983ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 46993ee17bc7SMat Martineau #endif 470078476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 470178476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 470278476d31SJeremy Kerr #endif 4703df5042f4SFlorian Westphal }; 4704df5042f4SFlorian Westphal 4705df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4706df5042f4SFlorian Westphal { 4707df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4708df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4709df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4710df5042f4SFlorian Westphal #endif 47114165079bSFlorian Westphal #ifdef CONFIG_XFRM 47124165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 47134165079bSFlorian Westphal #endif 471495a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 471595a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 471695a7233cSPaul Blakey #endif 47173ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47183ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 47193ee17bc7SMat Martineau #endif 472078476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 472178476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 472278476d31SJeremy Kerr #endif 4723df5042f4SFlorian Westphal 0; 4724df5042f4SFlorian Westphal } 4725df5042f4SFlorian Westphal 4726df5042f4SFlorian Westphal static void skb_extensions_init(void) 4727df5042f4SFlorian Westphal { 4728df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4729df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4730df5042f4SFlorian Westphal 4731df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4732df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4733df5042f4SFlorian Westphal 0, 4734df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4735df5042f4SFlorian Westphal NULL); 4736df5042f4SFlorian Westphal } 4737df5042f4SFlorian Westphal #else 4738df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4739df5042f4SFlorian Westphal #endif 4740df5042f4SFlorian Westphal 47411da177e4SLinus Torvalds void __init skb_init(void) 47421da177e4SLinus Torvalds { 4743025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", 47441da177e4SLinus Torvalds sizeof(struct sk_buff), 47451da177e4SLinus Torvalds 0, 4746e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 474779a8a642SKees Cook offsetof(struct sk_buff, cb), 474879a8a642SKees Cook sizeof_field(struct sk_buff, cb), 474920c2df83SPaul Mundt NULL); 4750d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4751d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4752d179cd12SDavid S. Miller 0, 4753e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 475420c2df83SPaul Mundt NULL); 47550b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. 47560b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head, 47570b34d680SEric Dumazet * and should not be copied to/from user. 47580b34d680SEric Dumazet */ 47590b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", 4760bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE, 4761bf9f1baaSEric Dumazet 0, 4762bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC, 47630b34d680SEric Dumazet 0, 47640b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM, 4765bf9f1baaSEric Dumazet NULL); 4766df5042f4SFlorian Westphal skb_extensions_init(); 47671da177e4SLinus Torvalds } 47681da177e4SLinus Torvalds 476951c739d1SDavid S. Miller static int 477048a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 477148a1df65SJason A. Donenfeld unsigned int recursion_level) 4772716ea3a7SDavid Howells { 47731a028e50SDavid S. Miller int start = skb_headlen(skb); 47741a028e50SDavid S. Miller int i, copy = start - offset; 4775fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4776716ea3a7SDavid Howells int elt = 0; 4777716ea3a7SDavid Howells 477848a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 477948a1df65SJason A. Donenfeld return -EMSGSIZE; 478048a1df65SJason A. Donenfeld 4781716ea3a7SDavid Howells if (copy > 0) { 4782716ea3a7SDavid Howells if (copy > len) 4783716ea3a7SDavid Howells copy = len; 4784642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4785716ea3a7SDavid Howells elt++; 4786716ea3a7SDavid Howells if ((len -= copy) == 0) 4787716ea3a7SDavid Howells return elt; 4788716ea3a7SDavid Howells offset += copy; 4789716ea3a7SDavid Howells } 4790716ea3a7SDavid Howells 4791716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 47921a028e50SDavid S. Miller int end; 4793716ea3a7SDavid Howells 4794547b792cSIlpo Järvinen WARN_ON(start > offset + len); 47951a028e50SDavid S. Miller 47969e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4797716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4798716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 479948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 480048a1df65SJason A. Donenfeld return -EMSGSIZE; 4801716ea3a7SDavid Howells 4802716ea3a7SDavid Howells if (copy > len) 4803716ea3a7SDavid Howells copy = len; 4804ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4805b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4806716ea3a7SDavid Howells elt++; 4807716ea3a7SDavid Howells if (!(len -= copy)) 4808716ea3a7SDavid Howells return elt; 4809716ea3a7SDavid Howells offset += copy; 4810716ea3a7SDavid Howells } 48111a028e50SDavid S. Miller start = end; 4812716ea3a7SDavid Howells } 4813716ea3a7SDavid Howells 4814fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 481548a1df65SJason A. Donenfeld int end, ret; 4816716ea3a7SDavid Howells 4817547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48181a028e50SDavid S. Miller 4819fbb398a8SDavid S. Miller end = start + frag_iter->len; 4820716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 482148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 482248a1df65SJason A. Donenfeld return -EMSGSIZE; 482348a1df65SJason A. Donenfeld 4824716ea3a7SDavid Howells if (copy > len) 4825716ea3a7SDavid Howells copy = len; 482648a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 482748a1df65SJason A. Donenfeld copy, recursion_level + 1); 482848a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 482948a1df65SJason A. Donenfeld return ret; 483048a1df65SJason A. Donenfeld elt += ret; 4831716ea3a7SDavid Howells if ((len -= copy) == 0) 4832716ea3a7SDavid Howells return elt; 4833716ea3a7SDavid Howells offset += copy; 4834716ea3a7SDavid Howells } 48351a028e50SDavid S. Miller start = end; 4836716ea3a7SDavid Howells } 4837716ea3a7SDavid Howells BUG_ON(len); 4838716ea3a7SDavid Howells return elt; 4839716ea3a7SDavid Howells } 4840716ea3a7SDavid Howells 484148a1df65SJason A. Donenfeld /** 484248a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 484348a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 484448a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 484548a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 484648a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 484748a1df65SJason A. Donenfeld * 484848a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 484948a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 485048a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 485148a1df65SJason A. Donenfeld * could not fit. 485248a1df65SJason A. Donenfeld */ 485348a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 485448a1df65SJason A. Donenfeld { 485548a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 485648a1df65SJason A. Donenfeld 485748a1df65SJason A. Donenfeld if (nsg <= 0) 485848a1df65SJason A. Donenfeld return nsg; 485948a1df65SJason A. Donenfeld 486048a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 486148a1df65SJason A. Donenfeld 486248a1df65SJason A. Donenfeld return nsg; 486348a1df65SJason A. Donenfeld } 486448a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 486548a1df65SJason A. Donenfeld 486625a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 486725a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 486825a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 486925a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 487025a91d8dSFan Du * 487125a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 487225a91d8dSFan Du * 1. sg_init_table 487325a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 487425a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 487525a91d8dSFan Du * 487625a91d8dSFan Du * This is equivalent to: 487725a91d8dSFan Du * 1. sg_init_table 487825a91d8dSFan Du * 2. skb_to_sgvec(payload1) 487925a91d8dSFan Du * 3. sg_unmark_end 488025a91d8dSFan Du * 4. skb_to_sgvec(payload2) 488125a91d8dSFan Du * 488225a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 488325a91d8dSFan Du * is more preferable. 488425a91d8dSFan Du */ 488525a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 488625a91d8dSFan Du int offset, int len) 488725a91d8dSFan Du { 488848a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 488925a91d8dSFan Du } 489025a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 489125a91d8dSFan Du 489251c739d1SDavid S. Miller 489351c739d1SDavid S. Miller 4894716ea3a7SDavid Howells /** 4895716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4896716ea3a7SDavid Howells * @skb: The socket buffer to check. 4897716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4898716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4899716ea3a7SDavid Howells * 4900716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4901716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4902716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4903716ea3a7SDavid Howells * 4904716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4905716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4906716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4907716ea3a7SDavid Howells * 4908716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4909716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4910716ea3a7SDavid Howells */ 4911716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4912716ea3a7SDavid Howells { 4913716ea3a7SDavid Howells int copyflag; 4914716ea3a7SDavid Howells int elt; 4915716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4916716ea3a7SDavid Howells 4917716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4918716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4919716ea3a7SDavid Howells * at the moment even if they are anonymous). 4920716ea3a7SDavid Howells */ 4921716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4922c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4923716ea3a7SDavid Howells return -ENOMEM; 4924716ea3a7SDavid Howells 4925716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 492621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4927716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4928716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4929716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4930716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4931716ea3a7SDavid Howells 4932716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4933716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4934716ea3a7SDavid Howells return -ENOMEM; 4935716ea3a7SDavid Howells 4936716ea3a7SDavid Howells /* Voila! */ 4937716ea3a7SDavid Howells *trailer = skb; 4938716ea3a7SDavid Howells return 1; 4939716ea3a7SDavid Howells } 4940716ea3a7SDavid Howells 4941716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4942716ea3a7SDavid Howells 4943716ea3a7SDavid Howells elt = 1; 4944716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4945716ea3a7SDavid Howells copyflag = 0; 4946716ea3a7SDavid Howells 4947716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4948716ea3a7SDavid Howells int ntail = 0; 4949716ea3a7SDavid Howells 4950716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4951716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4952716ea3a7SDavid Howells * after it. */ 4953716ea3a7SDavid Howells 4954716ea3a7SDavid Howells if (skb_shared(skb1)) 4955716ea3a7SDavid Howells copyflag = 1; 4956716ea3a7SDavid Howells 4957716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4958716ea3a7SDavid Howells 4959716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4960716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 496121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4962716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4963716ea3a7SDavid Howells ntail = tailbits + 128; 4964716ea3a7SDavid Howells } 4965716ea3a7SDavid Howells 4966716ea3a7SDavid Howells if (copyflag || 4967716ea3a7SDavid Howells skb_cloned(skb1) || 4968716ea3a7SDavid Howells ntail || 4969716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 497021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4971716ea3a7SDavid Howells struct sk_buff *skb2; 4972716ea3a7SDavid Howells 4973716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4974716ea3a7SDavid Howells if (ntail == 0) 4975716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4976716ea3a7SDavid Howells else 4977716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4978716ea3a7SDavid Howells skb_headroom(skb1), 4979716ea3a7SDavid Howells ntail, 4980716ea3a7SDavid Howells GFP_ATOMIC); 4981716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4982716ea3a7SDavid Howells return -ENOMEM; 4983716ea3a7SDavid Howells 4984716ea3a7SDavid Howells if (skb1->sk) 4985716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4986716ea3a7SDavid Howells 4987716ea3a7SDavid Howells /* Looking around. Are we still alive? 4988716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4989716ea3a7SDavid Howells 4990716ea3a7SDavid Howells skb2->next = skb1->next; 4991716ea3a7SDavid Howells *skb_p = skb2; 4992716ea3a7SDavid Howells kfree_skb(skb1); 4993716ea3a7SDavid Howells skb1 = skb2; 4994716ea3a7SDavid Howells } 4995716ea3a7SDavid Howells elt++; 4996716ea3a7SDavid Howells *trailer = skb1; 4997716ea3a7SDavid Howells skb_p = &skb1->next; 4998716ea3a7SDavid Howells } 4999716ea3a7SDavid Howells 5000716ea3a7SDavid Howells return elt; 5001716ea3a7SDavid Howells } 5002b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 5003716ea3a7SDavid Howells 5004b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 5005b1faf566SEric Dumazet { 5006b1faf566SEric Dumazet struct sock *sk = skb->sk; 5007b1faf566SEric Dumazet 5008b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 5009b1faf566SEric Dumazet } 5010b1faf566SEric Dumazet 50118605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 50128605330aSSoheil Hassas Yeganeh { 50138605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 50148605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 50158605330aSSoheil Hassas Yeganeh */ 50168605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 50178605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 50188605330aSSoheil Hassas Yeganeh } 50198605330aSSoheil Hassas Yeganeh 5020b1faf566SEric Dumazet /* 5021b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 5022b1faf566SEric Dumazet */ 5023b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 5024b1faf566SEric Dumazet { 5025b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 5026ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 5027b1faf566SEric Dumazet return -ENOMEM; 5028b1faf566SEric Dumazet 5029b1faf566SEric Dumazet skb_orphan(skb); 5030b1faf566SEric Dumazet skb->sk = sk; 5031b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 5032b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 50338605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 5034b1faf566SEric Dumazet 5035abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 5036abb57ea4SEric Dumazet skb_dst_force(skb); 5037abb57ea4SEric Dumazet 5038b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 5039b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 5040e3ae2365SAlexander Aring sk_error_report(sk); 5041b1faf566SEric Dumazet return 0; 5042b1faf566SEric Dumazet } 5043b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 5044b1faf566SEric Dumazet 504583a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 504683a1a1a7SSoheil Hassas Yeganeh { 504783a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 504883a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 504983a1a1a7SSoheil Hassas Yeganeh } 505083a1a1a7SSoheil Hassas Yeganeh 5051364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 5052364a9e93SWillem de Bruijn { 5053364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 505483a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 505583a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 5056997d5c3fSEric Dumazet unsigned long flags; 5057364a9e93SWillem de Bruijn 5058997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 5059364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 506038b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 506183a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 506238b25793SSoheil Hassas Yeganeh if (icmp_next) 5063985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 506438b25793SSoheil Hassas Yeganeh } 5065997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 5066364a9e93SWillem de Bruijn 506783a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 506883a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 506983a1a1a7SSoheil Hassas Yeganeh 507083a1a1a7SSoheil Hassas Yeganeh if (skb_next) 5071e3ae2365SAlexander Aring sk_error_report(sk); 5072364a9e93SWillem de Bruijn 5073364a9e93SWillem de Bruijn return skb; 5074364a9e93SWillem de Bruijn } 5075364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 5076364a9e93SWillem de Bruijn 5077cab41c47SAlexander Duyck /** 5078cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 5079cab41c47SAlexander Duyck * @skb: the skb to clone 5080cab41c47SAlexander Duyck * 5081cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 5082cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 5083cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 5084cab41c47SAlexander Duyck * 5085cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 5086cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 5087cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 5088cab41c47SAlexander Duyck * the sk_error_queue. 5089cab41c47SAlexander Duyck */ 509062bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 509162bccb8cSAlexander Duyck { 509262bccb8cSAlexander Duyck struct sock *sk = skb->sk; 509362bccb8cSAlexander Duyck struct sk_buff *clone; 509462bccb8cSAlexander Duyck 509541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 509662bccb8cSAlexander Duyck return NULL; 509762bccb8cSAlexander Duyck 509862bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 509962bccb8cSAlexander Duyck if (!clone) { 510062bccb8cSAlexander Duyck sock_put(sk); 510162bccb8cSAlexander Duyck return NULL; 510262bccb8cSAlexander Duyck } 510362bccb8cSAlexander Duyck 510462bccb8cSAlexander Duyck clone->sk = sk; 510562bccb8cSAlexander Duyck clone->destructor = sock_efree; 510662bccb8cSAlexander Duyck 510762bccb8cSAlexander Duyck return clone; 510862bccb8cSAlexander Duyck } 510962bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 511062bccb8cSAlexander Duyck 511137846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 511237846ef0SAlexander Duyck struct sock *sk, 51134ef1b286SSoheil Hassas Yeganeh int tstype, 51144ef1b286SSoheil Hassas Yeganeh bool opt_stats) 5115ac45f602SPatrick Ohly { 5116ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 5117ac45f602SPatrick Ohly int err; 5118ac45f602SPatrick Ohly 51194ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 51204ef1b286SSoheil Hassas Yeganeh 5121ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5122ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5123ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5124ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5125e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 51264ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 51271862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 51284ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 512909c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 513042f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5131a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 51324ed2d765SWillem de Bruijn } 513329030374SEric Dumazet 5134ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 513529030374SEric Dumazet 5136ac45f602SPatrick Ohly if (err) 5137ac45f602SPatrick Ohly kfree_skb(skb); 5138ac45f602SPatrick Ohly } 513937846ef0SAlexander Duyck 5140b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5141b245be1fSWillem de Bruijn { 5142b245be1fSWillem de Bruijn bool ret; 5143b245be1fSWillem de Bruijn 5144d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5145b245be1fSWillem de Bruijn return true; 5146b245be1fSWillem de Bruijn 5147b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5148b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5149b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5150b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5151b245be1fSWillem de Bruijn return ret; 5152b245be1fSWillem de Bruijn } 5153b245be1fSWillem de Bruijn 515437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 515537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 515637846ef0SAlexander Duyck { 515737846ef0SAlexander Duyck struct sock *sk = skb->sk; 515837846ef0SAlexander Duyck 5159b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 516035b99dffSWillem de Bruijn goto err; 5161b245be1fSWillem de Bruijn 51629ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 51639ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 51649ac25fc0SEric Dumazet */ 516541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 516637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 51674ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 516837846ef0SAlexander Duyck sock_put(sk); 516935b99dffSWillem de Bruijn return; 517037846ef0SAlexander Duyck } 517135b99dffSWillem de Bruijn 517235b99dffSWillem de Bruijn err: 517335b99dffSWillem de Bruijn kfree_skb(skb); 51749ac25fc0SEric Dumazet } 517537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 517637846ef0SAlexander Duyck 517737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5178e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 517937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 518037846ef0SAlexander Duyck struct sock *sk, int tstype) 518137846ef0SAlexander Duyck { 518237846ef0SAlexander Duyck struct sk_buff *skb; 51834ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 518437846ef0SAlexander Duyck 51853a8dd971SWillem de Bruijn if (!sk) 51863a8dd971SWillem de Bruijn return; 51873a8dd971SWillem de Bruijn 5188b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5189b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5190b50a5c70SMiroslav Lichvar return; 5191b50a5c70SMiroslav Lichvar 51923a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 51933a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 519437846ef0SAlexander Duyck return; 519537846ef0SAlexander Duyck 51961c885808SFrancis Yan if (tsonly) { 51971c885808SFrancis Yan #ifdef CONFIG_INET 51981c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 519942f67eeaSEric Dumazet sk_is_tcp(sk)) { 5200e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5201e7ed11eeSYousuk Seung ack_skb); 52024ef1b286SSoheil Hassas Yeganeh opt_stats = true; 52034ef1b286SSoheil Hassas Yeganeh } else 52041c885808SFrancis Yan #endif 52051c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 52061c885808SFrancis Yan } else { 520737846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 520850749f2dSKuniyuki Iwashima 520950749f2dSKuniyuki Iwashima if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) 521050749f2dSKuniyuki Iwashima return; 52111c885808SFrancis Yan } 521237846ef0SAlexander Duyck if (!skb) 521337846ef0SAlexander Duyck return; 521437846ef0SAlexander Duyck 521549ca0d8bSWillem de Bruijn if (tsonly) { 5216fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5217fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 521849ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 521949ca0d8bSWillem de Bruijn } 522049ca0d8bSWillem de Bruijn 522149ca0d8bSWillem de Bruijn if (hwtstamps) 522249ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 522349ca0d8bSWillem de Bruijn else 5224d93376f5SMartin KaFai Lau __net_timestamp(skb); 522549ca0d8bSWillem de Bruijn 52264ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 522737846ef0SAlexander Duyck } 5228e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5229e7fd2885SWillem de Bruijn 5230e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5231e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5232e7fd2885SWillem de Bruijn { 5233e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5234e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5235e7fd2885SWillem de Bruijn } 5236ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5237ac45f602SPatrick Ohly 5238eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS 52396e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 52406e3e939fSJohannes Berg { 52416e3e939fSJohannes Berg struct sock *sk = skb->sk; 52426e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5243dd4f1072SEric Dumazet int err = 1; 52446e3e939fSJohannes Berg 52456e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 52466e3e939fSJohannes Berg skb->wifi_acked = acked; 52476e3e939fSJohannes Berg 52486e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 52496e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 52506e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 52516e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 52526e3e939fSJohannes Berg 5253dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5254dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5255dd4f1072SEric Dumazet */ 525641c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 52576e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5258dd4f1072SEric Dumazet sock_put(sk); 5259dd4f1072SEric Dumazet } 52606e3e939fSJohannes Berg if (err) 52616e3e939fSJohannes Berg kfree_skb(skb); 52626e3e939fSJohannes Berg } 52636e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 5264eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */ 52656e3e939fSJohannes Berg 5266f35d9d8aSRusty Russell /** 5267f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5268f35d9d8aSRusty Russell * @skb: the skb to set 5269f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5270f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5271f35d9d8aSRusty Russell * 5272f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5273f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5274f35d9d8aSRusty Russell * 5275f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5276f35d9d8aSRusty Russell * returns false you should drop the packet. 5277f35d9d8aSRusty Russell */ 5278f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5279f35d9d8aSRusty Russell { 528052b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 528152b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 528252b5d6f5SEric Dumazet 5283424f8416SEric Dumazet if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) { 528452b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 528552b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5286f35d9d8aSRusty Russell return false; 5287f35d9d8aSRusty Russell } 5288f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 528952b5d6f5SEric Dumazet skb->csum_start = csum_start; 5290f35d9d8aSRusty Russell skb->csum_offset = off; 5291424f8416SEric Dumazet skb->transport_header = csum_start; 5292f35d9d8aSRusty Russell return true; 5293f35d9d8aSRusty Russell } 5294b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5295f35d9d8aSRusty Russell 5296ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5297ed1f50c3SPaul Durrant unsigned int max) 5298ed1f50c3SPaul Durrant { 5299ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5300ed1f50c3SPaul Durrant return 0; 5301ed1f50c3SPaul Durrant 5302ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5303ed1f50c3SPaul Durrant * won't need to do it again. 5304ed1f50c3SPaul Durrant */ 5305ed1f50c3SPaul Durrant if (max > skb->len) 5306ed1f50c3SPaul Durrant max = skb->len; 5307ed1f50c3SPaul Durrant 5308ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5309ed1f50c3SPaul Durrant return -ENOMEM; 5310ed1f50c3SPaul Durrant 5311ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5312ed1f50c3SPaul Durrant return -EPROTO; 5313ed1f50c3SPaul Durrant 5314ed1f50c3SPaul Durrant return 0; 5315ed1f50c3SPaul Durrant } 5316ed1f50c3SPaul Durrant 5317f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5318f9708b43SJan Beulich 5319f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5320f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5321f9708b43SJan Beulich unsigned int off) 5322f9708b43SJan Beulich { 5323f9708b43SJan Beulich int err; 5324f9708b43SJan Beulich 5325161d1792SKees Cook switch (proto) { 5326f9708b43SJan Beulich case IPPROTO_TCP: 5327f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5328f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5329f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5330f9708b43SJan Beulich offsetof(struct tcphdr, 5331f9708b43SJan Beulich check))) 5332f9708b43SJan Beulich err = -EPROTO; 5333f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5334f9708b43SJan Beulich 5335f9708b43SJan Beulich case IPPROTO_UDP: 5336f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5337f9708b43SJan Beulich off + sizeof(struct udphdr)); 5338f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5339f9708b43SJan Beulich offsetof(struct udphdr, 5340f9708b43SJan Beulich check))) 5341f9708b43SJan Beulich err = -EPROTO; 5342f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5343f9708b43SJan Beulich } 5344f9708b43SJan Beulich 5345f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5346f9708b43SJan Beulich } 5347f9708b43SJan Beulich 5348ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5349ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5350ed1f50c3SPaul Durrant */ 5351ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5352ed1f50c3SPaul Durrant 5353f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5354ed1f50c3SPaul Durrant { 5355ed1f50c3SPaul Durrant unsigned int off; 5356ed1f50c3SPaul Durrant bool fragment; 5357f9708b43SJan Beulich __sum16 *csum; 5358ed1f50c3SPaul Durrant int err; 5359ed1f50c3SPaul Durrant 5360ed1f50c3SPaul Durrant fragment = false; 5361ed1f50c3SPaul Durrant 5362ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5363ed1f50c3SPaul Durrant sizeof(struct iphdr), 5364ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5365ed1f50c3SPaul Durrant if (err < 0) 5366ed1f50c3SPaul Durrant goto out; 5367ed1f50c3SPaul Durrant 536811f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5369ed1f50c3SPaul Durrant fragment = true; 5370ed1f50c3SPaul Durrant 5371ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5372ed1f50c3SPaul Durrant 5373ed1f50c3SPaul Durrant err = -EPROTO; 5374ed1f50c3SPaul Durrant 5375ed1f50c3SPaul Durrant if (fragment) 5376ed1f50c3SPaul Durrant goto out; 5377ed1f50c3SPaul Durrant 5378f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5379f9708b43SJan Beulich if (IS_ERR(csum)) 5380f9708b43SJan Beulich return PTR_ERR(csum); 5381ed1f50c3SPaul Durrant 5382ed1f50c3SPaul Durrant if (recalculate) 5383f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5384ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5385ed1f50c3SPaul Durrant skb->len - off, 5386f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5387ed1f50c3SPaul Durrant err = 0; 5388ed1f50c3SPaul Durrant 5389ed1f50c3SPaul Durrant out: 5390ed1f50c3SPaul Durrant return err; 5391ed1f50c3SPaul Durrant } 5392ed1f50c3SPaul Durrant 5393ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5394ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5395ed1f50c3SPaul Durrant */ 5396ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5397ed1f50c3SPaul Durrant 5398ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5399ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5400ed1f50c3SPaul Durrant 5401ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5402ed1f50c3SPaul Durrant { 5403ed1f50c3SPaul Durrant int err; 5404ed1f50c3SPaul Durrant u8 nexthdr; 5405ed1f50c3SPaul Durrant unsigned int off; 5406ed1f50c3SPaul Durrant unsigned int len; 5407ed1f50c3SPaul Durrant bool fragment; 5408ed1f50c3SPaul Durrant bool done; 5409f9708b43SJan Beulich __sum16 *csum; 5410ed1f50c3SPaul Durrant 5411ed1f50c3SPaul Durrant fragment = false; 5412ed1f50c3SPaul Durrant done = false; 5413ed1f50c3SPaul Durrant 5414ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5415ed1f50c3SPaul Durrant 5416ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5417ed1f50c3SPaul Durrant if (err < 0) 5418ed1f50c3SPaul Durrant goto out; 5419ed1f50c3SPaul Durrant 5420ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5421ed1f50c3SPaul Durrant 5422ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5423ed1f50c3SPaul Durrant while (off <= len && !done) { 5424ed1f50c3SPaul Durrant switch (nexthdr) { 5425ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5426ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5427ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5428ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5429ed1f50c3SPaul Durrant 5430ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5431ed1f50c3SPaul Durrant off + 5432ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5433ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5434ed1f50c3SPaul Durrant if (err < 0) 5435ed1f50c3SPaul Durrant goto out; 5436ed1f50c3SPaul Durrant 5437ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5438ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5439ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5440ed1f50c3SPaul Durrant break; 5441ed1f50c3SPaul Durrant } 5442ed1f50c3SPaul Durrant case IPPROTO_AH: { 5443ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5444ed1f50c3SPaul Durrant 5445ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5446ed1f50c3SPaul Durrant off + 5447ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5448ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5449ed1f50c3SPaul Durrant if (err < 0) 5450ed1f50c3SPaul Durrant goto out; 5451ed1f50c3SPaul Durrant 5452ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5453ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5454ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5455ed1f50c3SPaul Durrant break; 5456ed1f50c3SPaul Durrant } 5457ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5458ed1f50c3SPaul Durrant struct frag_hdr *hp; 5459ed1f50c3SPaul Durrant 5460ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5461ed1f50c3SPaul Durrant off + 5462ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5463ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5464ed1f50c3SPaul Durrant if (err < 0) 5465ed1f50c3SPaul Durrant goto out; 5466ed1f50c3SPaul Durrant 5467ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5468ed1f50c3SPaul Durrant 5469ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5470ed1f50c3SPaul Durrant fragment = true; 5471ed1f50c3SPaul Durrant 5472ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5473ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5474ed1f50c3SPaul Durrant break; 5475ed1f50c3SPaul Durrant } 5476ed1f50c3SPaul Durrant default: 5477ed1f50c3SPaul Durrant done = true; 5478ed1f50c3SPaul Durrant break; 5479ed1f50c3SPaul Durrant } 5480ed1f50c3SPaul Durrant } 5481ed1f50c3SPaul Durrant 5482ed1f50c3SPaul Durrant err = -EPROTO; 5483ed1f50c3SPaul Durrant 5484ed1f50c3SPaul Durrant if (!done || fragment) 5485ed1f50c3SPaul Durrant goto out; 5486ed1f50c3SPaul Durrant 5487f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5488f9708b43SJan Beulich if (IS_ERR(csum)) 5489f9708b43SJan Beulich return PTR_ERR(csum); 5490ed1f50c3SPaul Durrant 5491ed1f50c3SPaul Durrant if (recalculate) 5492f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5493ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5494f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5495ed1f50c3SPaul Durrant err = 0; 5496ed1f50c3SPaul Durrant 5497ed1f50c3SPaul Durrant out: 5498ed1f50c3SPaul Durrant return err; 5499ed1f50c3SPaul Durrant } 5500ed1f50c3SPaul Durrant 5501ed1f50c3SPaul Durrant /** 5502ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5503ed1f50c3SPaul Durrant * @skb: the skb to set up 5504ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5505ed1f50c3SPaul Durrant */ 5506ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5507ed1f50c3SPaul Durrant { 5508ed1f50c3SPaul Durrant int err; 5509ed1f50c3SPaul Durrant 5510ed1f50c3SPaul Durrant switch (skb->protocol) { 5511ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5512f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5513ed1f50c3SPaul Durrant break; 5514ed1f50c3SPaul Durrant 5515ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5516ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5517ed1f50c3SPaul Durrant break; 5518ed1f50c3SPaul Durrant 5519ed1f50c3SPaul Durrant default: 5520ed1f50c3SPaul Durrant err = -EPROTO; 5521ed1f50c3SPaul Durrant break; 5522ed1f50c3SPaul Durrant } 5523ed1f50c3SPaul Durrant 5524ed1f50c3SPaul Durrant return err; 5525ed1f50c3SPaul Durrant } 5526ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5527ed1f50c3SPaul Durrant 55289afd85c9SLinus Lüssing /** 55299afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 55309afd85c9SLinus Lüssing * @skb: the skb to check 55319afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55329afd85c9SLinus Lüssing * 55339afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 55349afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 55359afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 55369afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 55379afd85c9SLinus Lüssing * 5538a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5539a516993fSLinus Lüssing * differs from the provided skb. 55409afd85c9SLinus Lüssing */ 55419afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 55429afd85c9SLinus Lüssing unsigned int transport_len) 55439afd85c9SLinus Lüssing { 55449afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55459afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 55469afd85c9SLinus Lüssing int ret; 55479afd85c9SLinus Lüssing 5548a516993fSLinus Lüssing if (skb->len < len) 55499afd85c9SLinus Lüssing return NULL; 5550a516993fSLinus Lüssing else if (skb->len == len) 55519afd85c9SLinus Lüssing return skb; 55529afd85c9SLinus Lüssing 55539afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 55549afd85c9SLinus Lüssing if (!skb_chk) 55559afd85c9SLinus Lüssing return NULL; 55569afd85c9SLinus Lüssing 55579afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 55589afd85c9SLinus Lüssing if (ret) { 55599afd85c9SLinus Lüssing kfree_skb(skb_chk); 55609afd85c9SLinus Lüssing return NULL; 55619afd85c9SLinus Lüssing } 55629afd85c9SLinus Lüssing 55639afd85c9SLinus Lüssing return skb_chk; 55649afd85c9SLinus Lüssing } 55659afd85c9SLinus Lüssing 55669afd85c9SLinus Lüssing /** 55679afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 55689afd85c9SLinus Lüssing * @skb: the skb to check 55699afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55709afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 55719afd85c9SLinus Lüssing * 55729afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 55739afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 55749afd85c9SLinus Lüssing * 55759afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 55769afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 55779afd85c9SLinus Lüssing * 5578a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5579a516993fSLinus Lüssing * differs from the provided skb. 55809afd85c9SLinus Lüssing */ 55819afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 55829afd85c9SLinus Lüssing unsigned int transport_len, 55839afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 55849afd85c9SLinus Lüssing { 55859afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55869afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5587fcba67c9SLinus Lüssing __sum16 ret; 55889afd85c9SLinus Lüssing 55899afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 55909afd85c9SLinus Lüssing if (!skb_chk) 5591a516993fSLinus Lüssing goto err; 55929afd85c9SLinus Lüssing 5593a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5594a516993fSLinus Lüssing goto err; 55959afd85c9SLinus Lüssing 55969b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 55979afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 55989b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 55999afd85c9SLinus Lüssing 5600a516993fSLinus Lüssing if (ret) 5601a516993fSLinus Lüssing goto err; 56029afd85c9SLinus Lüssing 56039afd85c9SLinus Lüssing return skb_chk; 5604a516993fSLinus Lüssing 5605a516993fSLinus Lüssing err: 5606a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5607a516993fSLinus Lüssing kfree_skb(skb_chk); 5608a516993fSLinus Lüssing 5609a516993fSLinus Lüssing return NULL; 5610a516993fSLinus Lüssing 56119afd85c9SLinus Lüssing } 56129afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 56139afd85c9SLinus Lüssing 56144497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 56154497b076SBen Hutchings { 5616e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5617e87cc472SJoe Perches skb->dev->name); 56184497b076SBen Hutchings } 56194497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5620bad43ca8SEric Dumazet 5621bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5622bad43ca8SEric Dumazet { 56233d861f66SEric Dumazet if (head_stolen) { 56243d861f66SEric Dumazet skb_release_head_state(skb); 5625025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 56263d861f66SEric Dumazet } else { 5627bad43ca8SEric Dumazet __kfree_skb(skb); 5628bad43ca8SEric Dumazet } 56293d861f66SEric Dumazet } 5630bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5631bad43ca8SEric Dumazet 5632bad43ca8SEric Dumazet /** 5633bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5634bad43ca8SEric Dumazet * @to: prior buffer 5635bad43ca8SEric Dumazet * @from: buffer to add 5636bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5637c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5638bad43ca8SEric Dumazet */ 5639bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5640bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5641bad43ca8SEric Dumazet { 5642c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5643bad43ca8SEric Dumazet int i, delta, len = from->len; 5644bad43ca8SEric Dumazet 5645bad43ca8SEric Dumazet *fragstolen = false; 5646bad43ca8SEric Dumazet 5647bad43ca8SEric Dumazet if (skb_cloned(to)) 5648bad43ca8SEric Dumazet return false; 5649bad43ca8SEric Dumazet 56500646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated 56510646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones 56520646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment 56531effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 56541effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 56551effe8caSJean-Philippe Brucker * inconsistent reference counts. 56560646dc31SLiang Chen * In theory we could take full references if @from is cloned and 56570646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with 56580646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with. 56596a5bcd84SIlias Apalodimas */ 56600646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle || 56610646dc31SLiang Chen (from->pp_recycle && skb_cloned(from))) 56626a5bcd84SIlias Apalodimas return false; 56636a5bcd84SIlias Apalodimas 5664bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5665e93a0435SEric Dumazet if (len) 5666bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5667bad43ca8SEric Dumazet *delta_truesize = 0; 5668bad43ca8SEric Dumazet return true; 5669bad43ca8SEric Dumazet } 5670bad43ca8SEric Dumazet 5671c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5672c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5673c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5674bad43ca8SEric Dumazet return false; 56751f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 56761f8b977aSWillem de Bruijn return false; 5677bad43ca8SEric Dumazet 5678bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5679bad43ca8SEric Dumazet struct page *page; 5680bad43ca8SEric Dumazet unsigned int offset; 5681bad43ca8SEric Dumazet 5682c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5683c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5684bad43ca8SEric Dumazet return false; 5685bad43ca8SEric Dumazet 5686bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5687bad43ca8SEric Dumazet return false; 5688bad43ca8SEric Dumazet 5689bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5690bad43ca8SEric Dumazet 5691bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5692bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5693bad43ca8SEric Dumazet 5694c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5695bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5696bad43ca8SEric Dumazet *fragstolen = true; 5697bad43ca8SEric Dumazet } else { 5698c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5699c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5700bad43ca8SEric Dumazet return false; 5701bad43ca8SEric Dumazet 5702f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5703bad43ca8SEric Dumazet } 5704bad43ca8SEric Dumazet 5705bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5706bad43ca8SEric Dumazet 5707c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5708c818fa9eSEric Dumazet from_shinfo->frags, 5709c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5710c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5711bad43ca8SEric Dumazet 5712bad43ca8SEric Dumazet if (!skb_cloned(from)) 5713c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5714bad43ca8SEric Dumazet 57158ea853fdSLi RongQing /* if the skb is not cloned this does nothing 57168ea853fdSLi RongQing * since we set nr_frags to 0. 57178ea853fdSLi RongQing */ 5718c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5719c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5720bad43ca8SEric Dumazet 5721bad43ca8SEric Dumazet to->truesize += delta; 5722bad43ca8SEric Dumazet to->len += len; 5723bad43ca8SEric Dumazet to->data_len += len; 5724bad43ca8SEric Dumazet 5725bad43ca8SEric Dumazet *delta_truesize = delta; 5726bad43ca8SEric Dumazet return true; 5727bad43ca8SEric Dumazet } 5728bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5729621e84d6SNicolas Dichtel 5730621e84d6SNicolas Dichtel /** 57318b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5732621e84d6SNicolas Dichtel * 5733621e84d6SNicolas Dichtel * @skb: buffer to clean 57348b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5735621e84d6SNicolas Dichtel * 57368b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 57378b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 57388b27f277SNicolas Dichtel * operations. 57398b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 57408b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 57418b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5742621e84d6SNicolas Dichtel */ 57438b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5744621e84d6SNicolas Dichtel { 5745621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5746621e84d6SNicolas Dichtel skb->skb_iif = 0; 574760ff7467SWANG Cong skb->ignore_df = 0; 5748621e84d6SNicolas Dichtel skb_dst_drop(skb); 5749174e2381SFlorian Westphal skb_ext_reset(skb); 5750895b5c9fSFlorian Westphal nf_reset_ct(skb); 5751621e84d6SNicolas Dichtel nf_reset_trace(skb); 5752213dd74aSHerbert Xu 57536f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 57546f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5755875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 57566f9a5069SPetr Machata #endif 57576f9a5069SPetr Machata 5758213dd74aSHerbert Xu if (!xnet) 5759213dd74aSHerbert Xu return; 5760213dd74aSHerbert Xu 57612b5ec1a5SYe Yin ipvs_reset(skb); 5762213dd74aSHerbert Xu skb->mark = 0; 5763de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5764621e84d6SNicolas Dichtel } 5765621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5766de960aa9SFlorian Westphal 5767de960aa9SFlorian Westphal /** 5768de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5769de960aa9SFlorian Westphal * 5770de960aa9SFlorian Westphal * @skb: GSO skb 5771de960aa9SFlorian Westphal * 5772de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5773de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5774de960aa9SFlorian Westphal * 5775de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5776de960aa9SFlorian Westphal */ 5777a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5778de960aa9SFlorian Westphal { 5779de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5780f993bc25SFlorian Westphal unsigned int thlen = 0; 5781f993bc25SFlorian Westphal 5782f993bc25SFlorian Westphal if (skb->encapsulation) { 5783f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5784f993bc25SFlorian Westphal skb_transport_header(skb); 5785de960aa9SFlorian Westphal 5786de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5787f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5788f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5789f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 57901dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 579190017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5792ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5793ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5794f993bc25SFlorian Westphal } 57956d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 57966d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 57976d39d589SFlorian Westphal * accounted for. 57986d39d589SFlorian Westphal */ 5799f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5800de960aa9SFlorian Westphal } 5801a4a77718SDaniel Axtens 5802a4a77718SDaniel Axtens /** 5803a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5804a4a77718SDaniel Axtens * 5805a4a77718SDaniel Axtens * @skb: GSO skb 5806a4a77718SDaniel Axtens * 5807a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5808a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5809a4a77718SDaniel Axtens * 5810a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5811a4a77718SDaniel Axtens */ 5812a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5813a4a77718SDaniel Axtens { 5814a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5815a4a77718SDaniel Axtens skb_network_header(skb); 5816a4a77718SDaniel Axtens 5817a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5818a4a77718SDaniel Axtens } 5819a4a77718SDaniel Axtens 5820a4a77718SDaniel Axtens /** 5821a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5822a4a77718SDaniel Axtens * 5823a4a77718SDaniel Axtens * @skb: GSO skb 5824a4a77718SDaniel Axtens * 5825a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5826a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5827a4a77718SDaniel Axtens * headers (TCP/UDP). 5828a4a77718SDaniel Axtens */ 5829a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5830a4a77718SDaniel Axtens { 5831a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5832a4a77718SDaniel Axtens 5833a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5834a4a77718SDaniel Axtens } 58350d5501c1SVlad Yasevich 5836ae7ef81eSMarcelo Ricardo Leitner /** 58372b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 58382b16f048SDaniel Axtens * 58392b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 58402b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 58412b16f048SDaniel Axtens * 58422b16f048SDaniel Axtens * We might want to check: 58432b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 58442b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 58452b16f048SDaniel Axtens * 58462b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 58472b16f048SDaniel Axtens * 584849682bfaSMathieu Malaterre * @skb: GSO skb 584949682bfaSMathieu Malaterre * 58502b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 58512b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 58522b16f048SDaniel Axtens * 58532b16f048SDaniel Axtens * @max_len: The maximum permissible length. 58542b16f048SDaniel Axtens * 58552b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 58562b16f048SDaniel Axtens */ 58572b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 58582b16f048SDaniel Axtens unsigned int seg_len, 58592b16f048SDaniel Axtens unsigned int max_len) { 58602b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 58612b16f048SDaniel Axtens const struct sk_buff *iter; 58622b16f048SDaniel Axtens 58632b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 58642b16f048SDaniel Axtens return seg_len <= max_len; 58652b16f048SDaniel Axtens 58662b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 58672b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 58682b16f048SDaniel Axtens 58692b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 58702b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 58712b16f048SDaniel Axtens return false; 58722b16f048SDaniel Axtens } 58732b16f048SDaniel Axtens 58742b16f048SDaniel Axtens return true; 58752b16f048SDaniel Axtens } 58762b16f048SDaniel Axtens 58772b16f048SDaniel Axtens /** 5878779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5879ae7ef81eSMarcelo Ricardo Leitner * 5880ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 588176f21b99SDavid S. Miller * @mtu: MTU to validate against 5882ae7ef81eSMarcelo Ricardo Leitner * 5883779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5884779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5885779b7931SDaniel Axtens * payload. 5886ae7ef81eSMarcelo Ricardo Leitner */ 5887779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5888ae7ef81eSMarcelo Ricardo Leitner { 58892b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5890ae7ef81eSMarcelo Ricardo Leitner } 5891779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5892ae7ef81eSMarcelo Ricardo Leitner 58932b16f048SDaniel Axtens /** 58942b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 58952b16f048SDaniel Axtens * 58962b16f048SDaniel Axtens * @skb: GSO skb 58972b16f048SDaniel Axtens * @len: length to validate against 58982b16f048SDaniel Axtens * 58992b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 59002b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 59012b16f048SDaniel Axtens */ 59022b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 59032b16f048SDaniel Axtens { 59042b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 59052b16f048SDaniel Axtens } 59062b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 59072b16f048SDaniel Axtens 59080d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 59090d5501c1SVlad Yasevich { 5910d85e8be2SYuya Kusakabe int mac_len, meta_len; 5911d85e8be2SYuya Kusakabe void *meta; 59124bbb3e0eSToshiaki Makita 59130d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 59140d5501c1SVlad Yasevich kfree_skb(skb); 59150d5501c1SVlad Yasevich return NULL; 59160d5501c1SVlad Yasevich } 59170d5501c1SVlad Yasevich 59184bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5919ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 59204bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 59214bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5922ae474573SToshiaki Makita } 5923d85e8be2SYuya Kusakabe 5924d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5925d85e8be2SYuya Kusakabe if (meta_len) { 5926d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5927d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5928d85e8be2SYuya Kusakabe } 5929d85e8be2SYuya Kusakabe 59300d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 59310d5501c1SVlad Yasevich return skb; 59320d5501c1SVlad Yasevich } 59330d5501c1SVlad Yasevich 59340d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 59350d5501c1SVlad Yasevich { 59360d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 59370d5501c1SVlad Yasevich u16 vlan_tci; 59380d5501c1SVlad Yasevich 5939df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 59400d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 59410d5501c1SVlad Yasevich return skb; 59420d5501c1SVlad Yasevich } 59430d5501c1SVlad Yasevich 59440d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 59450d5501c1SVlad Yasevich if (unlikely(!skb)) 59460d5501c1SVlad Yasevich goto err_free; 594755eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 594855eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 59490d5501c1SVlad Yasevich goto err_free; 59500d5501c1SVlad Yasevich 59510d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 59520d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 59530d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 59540d5501c1SVlad Yasevich 59550d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 59560d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 59570d5501c1SVlad Yasevich 59580d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 59590d5501c1SVlad Yasevich if (unlikely(!skb)) 59600d5501c1SVlad Yasevich goto err_free; 59610d5501c1SVlad Yasevich 59620d5501c1SVlad Yasevich skb_reset_network_header(skb); 59638be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 59640d5501c1SVlad Yasevich skb_reset_transport_header(skb); 59650d5501c1SVlad Yasevich skb_reset_mac_len(skb); 59660d5501c1SVlad Yasevich 59670d5501c1SVlad Yasevich return skb; 59680d5501c1SVlad Yasevich 59690d5501c1SVlad Yasevich err_free: 59700d5501c1SVlad Yasevich kfree_skb(skb); 59710d5501c1SVlad Yasevich return NULL; 59720d5501c1SVlad Yasevich } 59730d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 59742e4e4410SEric Dumazet 597592ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5976e2195121SJiri Pirko { 5977e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5978e2195121SJiri Pirko return -ENOMEM; 5979e2195121SJiri Pirko 5980e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5981e2195121SJiri Pirko return 0; 5982e2195121SJiri Pirko 5983e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5984e2195121SJiri Pirko } 5985e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5986e2195121SJiri Pirko 5987bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5988bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5989bfca4c52SShmulik Ladkani */ 5990bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 599193515d53SJiri Pirko { 5992b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 599393515d53SJiri Pirko int err; 599493515d53SJiri Pirko 5995b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5996b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5997b6a79208SShmulik Ladkani offset)) { 5998b6a79208SShmulik Ladkani return -EINVAL; 5999b6a79208SShmulik Ladkani } 6000b6a79208SShmulik Ladkani 600193515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 600293515d53SJiri Pirko if (unlikely(err)) 6003b6a79208SShmulik Ladkani return err; 600493515d53SJiri Pirko 600593515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 600693515d53SJiri Pirko 60070bcf2e4aSVladimir Oltean vlan_remove_tag(skb, vlan_tci); 600893515d53SJiri Pirko 600993515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 601093515d53SJiri Pirko 601193515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 601293515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 601393515d53SJiri Pirko 601493515d53SJiri Pirko skb_reset_mac_len(skb); 601593515d53SJiri Pirko 601693515d53SJiri Pirko return err; 601793515d53SJiri Pirko } 6018bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 601993515d53SJiri Pirko 6020b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 6021b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6022b6a79208SShmulik Ladkani */ 602393515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 602493515d53SJiri Pirko { 602593515d53SJiri Pirko u16 vlan_tci; 602693515d53SJiri Pirko __be16 vlan_proto; 602793515d53SJiri Pirko int err; 602893515d53SJiri Pirko 6029df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 6030b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 603193515d53SJiri Pirko } else { 6032ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 603393515d53SJiri Pirko return 0; 603493515d53SJiri Pirko 603593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 603693515d53SJiri Pirko if (err) 603793515d53SJiri Pirko return err; 603893515d53SJiri Pirko } 603993515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 6040ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 604193515d53SJiri Pirko return 0; 604293515d53SJiri Pirko 604393515d53SJiri Pirko vlan_proto = skb->protocol; 604493515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 604593515d53SJiri Pirko if (unlikely(err)) 604693515d53SJiri Pirko return err; 604793515d53SJiri Pirko 604893515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 604993515d53SJiri Pirko return 0; 605093515d53SJiri Pirko } 605193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 605293515d53SJiri Pirko 6053b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 6054b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6055b6a79208SShmulik Ladkani */ 605693515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 605793515d53SJiri Pirko { 6058df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 6059b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 606093515d53SJiri Pirko int err; 606193515d53SJiri Pirko 6062b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6063b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 6064b6a79208SShmulik Ladkani offset)) { 6065b6a79208SShmulik Ladkani return -EINVAL; 6066b6a79208SShmulik Ladkani } 6067b6a79208SShmulik Ladkani 606893515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 6069df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 6070b6a79208SShmulik Ladkani if (err) 607193515d53SJiri Pirko return err; 60729241e2dfSDaniel Borkmann 607393515d53SJiri Pirko skb->protocol = skb->vlan_proto; 607493515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 607593515d53SJiri Pirko 60766b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 607793515d53SJiri Pirko } 607893515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 607993515d53SJiri Pirko return 0; 608093515d53SJiri Pirko } 608193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 608293515d53SJiri Pirko 608319fbcb36SGuillaume Nault /** 608419fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 608519fbcb36SGuillaume Nault * 608619fbcb36SGuillaume Nault * @skb: Socket buffer to modify 608719fbcb36SGuillaume Nault * 608819fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 608919fbcb36SGuillaume Nault * 609019fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 609119fbcb36SGuillaume Nault * present. 609219fbcb36SGuillaume Nault * 609319fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 609419fbcb36SGuillaume Nault */ 609519fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 609619fbcb36SGuillaume Nault { 609719fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 609819fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 609919fbcb36SGuillaume Nault return -EPROTO; 610019fbcb36SGuillaume Nault 610119fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 610219fbcb36SGuillaume Nault skb_reset_mac_header(skb); 610319fbcb36SGuillaume Nault skb_reset_mac_len(skb); 610419fbcb36SGuillaume Nault 610519fbcb36SGuillaume Nault return 0; 610619fbcb36SGuillaume Nault } 610719fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 610819fbcb36SGuillaume Nault 610919fbcb36SGuillaume Nault /** 611019fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 611119fbcb36SGuillaume Nault * 611219fbcb36SGuillaume Nault * @skb: Socket buffer to modify 611319fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 611419fbcb36SGuillaume Nault * @src: Source MAC address of the new header 611519fbcb36SGuillaume Nault * 611619fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 611719fbcb36SGuillaume Nault * 611819fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 611919fbcb36SGuillaume Nault * 612019fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 612119fbcb36SGuillaume Nault */ 612219fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 612319fbcb36SGuillaume Nault const unsigned char *src) 612419fbcb36SGuillaume Nault { 612519fbcb36SGuillaume Nault struct ethhdr *eth; 612619fbcb36SGuillaume Nault int err; 612719fbcb36SGuillaume Nault 612819fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 612919fbcb36SGuillaume Nault return -EPROTO; 613019fbcb36SGuillaume Nault 613119fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 613219fbcb36SGuillaume Nault if (err < 0) 613319fbcb36SGuillaume Nault return err; 613419fbcb36SGuillaume Nault 613519fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 613619fbcb36SGuillaume Nault skb_reset_mac_header(skb); 613719fbcb36SGuillaume Nault skb_reset_mac_len(skb); 613819fbcb36SGuillaume Nault 613919fbcb36SGuillaume Nault eth = eth_hdr(skb); 614019fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 614119fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 614219fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 614319fbcb36SGuillaume Nault 614419fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 614519fbcb36SGuillaume Nault 614619fbcb36SGuillaume Nault return 0; 614719fbcb36SGuillaume Nault } 614819fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 614919fbcb36SGuillaume Nault 61508822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 61518822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 61528822e270SJohn Hurley __be16 ethertype) 61538822e270SJohn Hurley { 61548822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 61558822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 61568822e270SJohn Hurley 61578822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 61588822e270SJohn Hurley } 61598822e270SJohn Hurley 61608822e270SJohn Hurley hdr->h_proto = ethertype; 61618822e270SJohn Hurley } 61628822e270SJohn Hurley 61638822e270SJohn Hurley /** 6164e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6165e7dbfed1SMartin Varghese * the packet 61668822e270SJohn Hurley * 61678822e270SJohn Hurley * @skb: buffer 61688822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 61698822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6170fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6171e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6172e7dbfed1SMartin Varghese * ethernet 61738822e270SJohn Hurley * 61748822e270SJohn Hurley * Expects skb->data at mac header. 61758822e270SJohn Hurley * 61768822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 61778822e270SJohn Hurley */ 6178fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6179d04ac224SMartin Varghese int mac_len, bool ethernet) 61808822e270SJohn Hurley { 61818822e270SJohn Hurley struct mpls_shim_hdr *lse; 61828822e270SJohn Hurley int err; 61838822e270SJohn Hurley 61848822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 61858822e270SJohn Hurley return -EINVAL; 61868822e270SJohn Hurley 61878822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 61888822e270SJohn Hurley if (skb->encapsulation) 61898822e270SJohn Hurley return -EINVAL; 61908822e270SJohn Hurley 61918822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 61928822e270SJohn Hurley if (unlikely(err)) 61938822e270SJohn Hurley return err; 61948822e270SJohn Hurley 61958822e270SJohn Hurley if (!skb->inner_protocol) { 6196e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 61978822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 61988822e270SJohn Hurley } 61998822e270SJohn Hurley 62008822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 62018822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6202fa4e0f88SDavide Caratti mac_len); 62038822e270SJohn Hurley skb_reset_mac_header(skb); 6204fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6205e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 62068822e270SJohn Hurley 62078822e270SJohn Hurley lse = mpls_hdr(skb); 62088822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 62098822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 62108822e270SJohn Hurley 62114296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 62128822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 62138822e270SJohn Hurley skb->protocol = mpls_proto; 62148822e270SJohn Hurley 62158822e270SJohn Hurley return 0; 62168822e270SJohn Hurley } 62178822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 62188822e270SJohn Hurley 62192e4e4410SEric Dumazet /** 6220ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6221ed246ceeSJohn Hurley * 6222ed246ceeSJohn Hurley * @skb: buffer 6223ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6224fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 622576f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6226ed246ceeSJohn Hurley * 6227ed246ceeSJohn Hurley * Expects skb->data at mac header. 6228ed246ceeSJohn Hurley * 6229ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6230ed246ceeSJohn Hurley */ 6231040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6232040b5cfbSMartin Varghese bool ethernet) 6233ed246ceeSJohn Hurley { 6234ed246ceeSJohn Hurley int err; 6235ed246ceeSJohn Hurley 6236ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6237dedc5a08SDavide Caratti return 0; 6238ed246ceeSJohn Hurley 6239fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6240ed246ceeSJohn Hurley if (unlikely(err)) 6241ed246ceeSJohn Hurley return err; 6242ed246ceeSJohn Hurley 6243ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6244ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6245fa4e0f88SDavide Caratti mac_len); 6246ed246ceeSJohn Hurley 6247ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6248ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6249fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6250ed246ceeSJohn Hurley 62514296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6252ed246ceeSJohn Hurley struct ethhdr *hdr; 6253ed246ceeSJohn Hurley 6254ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6255ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6256ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6257ed246ceeSJohn Hurley } 6258ed246ceeSJohn Hurley skb->protocol = next_proto; 6259ed246ceeSJohn Hurley 6260ed246ceeSJohn Hurley return 0; 6261ed246ceeSJohn Hurley } 6262ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6263ed246ceeSJohn Hurley 6264ed246ceeSJohn Hurley /** 6265d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6266d27cf5c5SJohn Hurley * 6267d27cf5c5SJohn Hurley * @skb: buffer 6268d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6269d27cf5c5SJohn Hurley * 6270d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6271d27cf5c5SJohn Hurley * 6272d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6273d27cf5c5SJohn Hurley */ 6274d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6275d27cf5c5SJohn Hurley { 6276d27cf5c5SJohn Hurley int err; 6277d27cf5c5SJohn Hurley 6278d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6279d27cf5c5SJohn Hurley return -EINVAL; 6280d27cf5c5SJohn Hurley 6281d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6282d27cf5c5SJohn Hurley if (unlikely(err)) 6283d27cf5c5SJohn Hurley return err; 6284d27cf5c5SJohn Hurley 6285d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6286d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6287d27cf5c5SJohn Hurley 6288d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6289d27cf5c5SJohn Hurley } 6290d27cf5c5SJohn Hurley 6291d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6292d27cf5c5SJohn Hurley 6293d27cf5c5SJohn Hurley return 0; 6294d27cf5c5SJohn Hurley } 6295d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6296d27cf5c5SJohn Hurley 6297d27cf5c5SJohn Hurley /** 62982a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 62992a2ea508SJohn Hurley * 63002a2ea508SJohn Hurley * @skb: buffer 63012a2ea508SJohn Hurley * 63022a2ea508SJohn Hurley * Expects skb->data at mac header. 63032a2ea508SJohn Hurley * 63042a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 63052a2ea508SJohn Hurley */ 63062a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 63072a2ea508SJohn Hurley { 63082a2ea508SJohn Hurley u32 lse; 63092a2ea508SJohn Hurley u8 ttl; 63102a2ea508SJohn Hurley 63112a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 63122a2ea508SJohn Hurley return -EINVAL; 63132a2ea508SJohn Hurley 631413de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 631513de4ed9SDavide Caratti return -ENOMEM; 631613de4ed9SDavide Caratti 63172a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 63182a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 63192a2ea508SJohn Hurley if (!--ttl) 63202a2ea508SJohn Hurley return -EINVAL; 63212a2ea508SJohn Hurley 63222a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 63232a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 63242a2ea508SJohn Hurley 63252a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 63262a2ea508SJohn Hurley } 63272a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 63282a2ea508SJohn Hurley 63292a2ea508SJohn Hurley /** 63302e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 63312e4e4410SEric Dumazet * 6332de3f0d0eSMasanari Iida * @header_len: size of linear part 6333de3f0d0eSMasanari Iida * @data_len: needed length in frags 6334de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6335de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6336de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 63372e4e4410SEric Dumazet * 63382e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 63392e4e4410SEric Dumazet */ 63402e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 63412e4e4410SEric Dumazet unsigned long data_len, 63422e4e4410SEric Dumazet int max_page_order, 63432e4e4410SEric Dumazet int *errcode, 63442e4e4410SEric Dumazet gfp_t gfp_mask) 63452e4e4410SEric Dumazet { 63462e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 63472e4e4410SEric Dumazet unsigned long chunk; 63482e4e4410SEric Dumazet struct sk_buff *skb; 63492e4e4410SEric Dumazet struct page *page; 63502e4e4410SEric Dumazet int i; 63512e4e4410SEric Dumazet 63522e4e4410SEric Dumazet *errcode = -EMSGSIZE; 63532e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 63542e4e4410SEric Dumazet * high order pages... 63552e4e4410SEric Dumazet */ 63562e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 63572e4e4410SEric Dumazet return NULL; 63582e4e4410SEric Dumazet 63592e4e4410SEric Dumazet *errcode = -ENOBUFS; 6360f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 63612e4e4410SEric Dumazet if (!skb) 63622e4e4410SEric Dumazet return NULL; 63632e4e4410SEric Dumazet 63642e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 63652e4e4410SEric Dumazet 63662e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 63672e4e4410SEric Dumazet int order = max_page_order; 63682e4e4410SEric Dumazet 63692e4e4410SEric Dumazet while (order) { 63702e4e4410SEric Dumazet if (npages >= 1 << order) { 6371d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 63722e4e4410SEric Dumazet __GFP_COMP | 6373d14b56f5SMichal Hocko __GFP_NOWARN, 63742e4e4410SEric Dumazet order); 63752e4e4410SEric Dumazet if (page) 63762e4e4410SEric Dumazet goto fill_page; 63772e4e4410SEric Dumazet /* Do not retry other high order allocations */ 63782e4e4410SEric Dumazet order = 1; 63792e4e4410SEric Dumazet max_page_order = 0; 63802e4e4410SEric Dumazet } 63812e4e4410SEric Dumazet order--; 63822e4e4410SEric Dumazet } 63832e4e4410SEric Dumazet page = alloc_page(gfp_mask); 63842e4e4410SEric Dumazet if (!page) 63852e4e4410SEric Dumazet goto failure; 63862e4e4410SEric Dumazet fill_page: 63872e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 63882e4e4410SEric Dumazet PAGE_SIZE << order); 63892e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 63902e4e4410SEric Dumazet data_len -= chunk; 63912e4e4410SEric Dumazet npages -= 1 << order; 63922e4e4410SEric Dumazet } 63932e4e4410SEric Dumazet return skb; 63942e4e4410SEric Dumazet 63952e4e4410SEric Dumazet failure: 63962e4e4410SEric Dumazet kfree_skb(skb); 63972e4e4410SEric Dumazet return NULL; 63982e4e4410SEric Dumazet } 63992e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 64006fa01ccdSSowmini Varadhan 64016fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 64026fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 64036fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 64046fa01ccdSSowmini Varadhan { 64056fa01ccdSSowmini Varadhan int i; 640612d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 64076fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 64086fa01ccdSSowmini Varadhan u8 *data; 64096fa01ccdSSowmini Varadhan 64106fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64116fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 641212d6c1d3SKees Cook 64135c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 64146fa01ccdSSowmini Varadhan if (!data) 64156fa01ccdSSowmini Varadhan return -ENOMEM; 641612d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64176fa01ccdSSowmini Varadhan 64186fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 64196fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 64206fa01ccdSSowmini Varadhan skb->len -= off; 64216fa01ccdSSowmini Varadhan 64226fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 64236fa01ccdSSowmini Varadhan skb_shinfo(skb), 64246fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 64256fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 64266fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 64276fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 64286fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6429bf9f1baaSEric Dumazet skb_kfree_head(data, size); 64306fa01ccdSSowmini Varadhan return -ENOMEM; 64316fa01ccdSSowmini Varadhan } 64326fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 64336fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64346fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64356fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6436b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 64376fa01ccdSSowmini Varadhan } else { 64386fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 64396fa01ccdSSowmini Varadhan * relocate values 64406fa01ccdSSowmini Varadhan */ 6441b07a2d97SJakub Kicinski skb_free_head(skb, false); 64426fa01ccdSSowmini Varadhan } 64436fa01ccdSSowmini Varadhan 64446fa01ccdSSowmini Varadhan skb->head = data; 64456fa01ccdSSowmini Varadhan skb->data = data; 64466fa01ccdSSowmini Varadhan skb->head_frag = 0; 6447763087daSEric Dumazet skb_set_end_offset(skb, size); 64486fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 64496fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64506fa01ccdSSowmini Varadhan skb->cloned = 0; 64516fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64526fa01ccdSSowmini Varadhan skb->nohdr = 0; 64536fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64546fa01ccdSSowmini Varadhan 64556fa01ccdSSowmini Varadhan return 0; 64566fa01ccdSSowmini Varadhan } 64576fa01ccdSSowmini Varadhan 64586fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 64596fa01ccdSSowmini Varadhan 64606fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 64616fa01ccdSSowmini Varadhan * pskb_carve() 64626fa01ccdSSowmini Varadhan */ 64636fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 64646fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 64656fa01ccdSSowmini Varadhan gfp_t gfp_mask) 64666fa01ccdSSowmini Varadhan { 64676fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 64686fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 64696fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 64706fa01ccdSSowmini Varadhan 64716fa01ccdSSowmini Varadhan do { 64726fa01ccdSSowmini Varadhan if (!list) { 64736fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 64746fa01ccdSSowmini Varadhan return -EFAULT; 64756fa01ccdSSowmini Varadhan } 64766fa01ccdSSowmini Varadhan if (list->len <= eat) { 64776fa01ccdSSowmini Varadhan /* Eaten as whole. */ 64786fa01ccdSSowmini Varadhan eat -= list->len; 64796fa01ccdSSowmini Varadhan list = list->next; 64806fa01ccdSSowmini Varadhan insp = list; 64816fa01ccdSSowmini Varadhan } else { 64826fa01ccdSSowmini Varadhan /* Eaten partially. */ 64836fa01ccdSSowmini Varadhan if (skb_shared(list)) { 64846fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 64856fa01ccdSSowmini Varadhan if (!clone) 64866fa01ccdSSowmini Varadhan return -ENOMEM; 64876fa01ccdSSowmini Varadhan insp = list->next; 64886fa01ccdSSowmini Varadhan list = clone; 64896fa01ccdSSowmini Varadhan } else { 64906fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 64916fa01ccdSSowmini Varadhan insp = list; 64926fa01ccdSSowmini Varadhan } 64936fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 64946fa01ccdSSowmini Varadhan kfree_skb(clone); 64956fa01ccdSSowmini Varadhan return -ENOMEM; 64966fa01ccdSSowmini Varadhan } 64976fa01ccdSSowmini Varadhan break; 64986fa01ccdSSowmini Varadhan } 64996fa01ccdSSowmini Varadhan } while (eat); 65006fa01ccdSSowmini Varadhan 65016fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 65026fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 65036fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6504ef527f96SEric Dumazet consume_skb(list); 65056fa01ccdSSowmini Varadhan } 65066fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 65076fa01ccdSSowmini Varadhan if (clone) { 65086fa01ccdSSowmini Varadhan clone->next = list; 65096fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 65106fa01ccdSSowmini Varadhan } 65116fa01ccdSSowmini Varadhan return 0; 65126fa01ccdSSowmini Varadhan } 65136fa01ccdSSowmini Varadhan 65146fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 65156fa01ccdSSowmini Varadhan * non-linear part of skb 65166fa01ccdSSowmini Varadhan */ 65176fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 65186fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 65196fa01ccdSSowmini Varadhan { 65206fa01ccdSSowmini Varadhan int i, k = 0; 652112d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 65226fa01ccdSSowmini Varadhan u8 *data; 65236fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 65246fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 65256fa01ccdSSowmini Varadhan 65266fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 65276fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 652812d6c1d3SKees Cook 65295c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 65306fa01ccdSSowmini Varadhan if (!data) 65316fa01ccdSSowmini Varadhan return -ENOMEM; 653212d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 65336fa01ccdSSowmini Varadhan 65346fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6535e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 65366fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6537bf9f1baaSEric Dumazet skb_kfree_head(data, size); 65386fa01ccdSSowmini Varadhan return -ENOMEM; 65396fa01ccdSSowmini Varadhan } 65406fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 65416fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 65426fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 65436fa01ccdSSowmini Varadhan 65446fa01ccdSSowmini Varadhan if (pos + fsize > off) { 65456fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 65466fa01ccdSSowmini Varadhan 65476fa01ccdSSowmini Varadhan if (pos < off) { 65486fa01ccdSSowmini Varadhan /* Split frag. 65496fa01ccdSSowmini Varadhan * We have two variants in this case: 65506fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 65516fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 65526fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 65536fa01ccdSSowmini Varadhan * where splitting is expensive. 65546fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 65556fa01ccdSSowmini Varadhan */ 6556b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 65576fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 65586fa01ccdSSowmini Varadhan } 65596fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 65606fa01ccdSSowmini Varadhan k++; 65616fa01ccdSSowmini Varadhan } 65626fa01ccdSSowmini Varadhan pos += fsize; 65636fa01ccdSSowmini Varadhan } 65646fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 65656fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 65666fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 65676fa01ccdSSowmini Varadhan 65686fa01ccdSSowmini Varadhan /* split line is in frag list */ 6569eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6570eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6571eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6572eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6573bf9f1baaSEric Dumazet skb_kfree_head(data, size); 6574eabe8618SMiaohe Lin return -ENOMEM; 65756fa01ccdSSowmini Varadhan } 6576b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 65776fa01ccdSSowmini Varadhan 65786fa01ccdSSowmini Varadhan skb->head = data; 65796fa01ccdSSowmini Varadhan skb->head_frag = 0; 65806fa01ccdSSowmini Varadhan skb->data = data; 6581763087daSEric Dumazet skb_set_end_offset(skb, size); 65826fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 65836fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 65846fa01ccdSSowmini Varadhan skb->cloned = 0; 65856fa01ccdSSowmini Varadhan skb->hdr_len = 0; 65866fa01ccdSSowmini Varadhan skb->nohdr = 0; 65876fa01ccdSSowmini Varadhan skb->len -= off; 65886fa01ccdSSowmini Varadhan skb->data_len = skb->len; 65896fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 65906fa01ccdSSowmini Varadhan return 0; 65916fa01ccdSSowmini Varadhan } 65926fa01ccdSSowmini Varadhan 65936fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 65946fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 65956fa01ccdSSowmini Varadhan { 65966fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 65976fa01ccdSSowmini Varadhan 65986fa01ccdSSowmini Varadhan if (len < headlen) 65996fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 66006fa01ccdSSowmini Varadhan else 66016fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 66026fa01ccdSSowmini Varadhan } 66036fa01ccdSSowmini Varadhan 66046fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 66056fa01ccdSSowmini Varadhan * a new skb 66066fa01ccdSSowmini Varadhan */ 66076fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 66086fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 66096fa01ccdSSowmini Varadhan { 66106fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 66116fa01ccdSSowmini Varadhan 66126fa01ccdSSowmini Varadhan if (!clone) 66136fa01ccdSSowmini Varadhan return NULL; 66146fa01ccdSSowmini Varadhan 66156fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 66166fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 66176fa01ccdSSowmini Varadhan kfree_skb(clone); 66186fa01ccdSSowmini Varadhan return NULL; 66196fa01ccdSSowmini Varadhan } 66206fa01ccdSSowmini Varadhan return clone; 66216fa01ccdSSowmini Varadhan } 66226fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6623c8c8b127SEric Dumazet 6624c8c8b127SEric Dumazet /** 6625c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6626c8c8b127SEric Dumazet * @skb: buffer 6627c8c8b127SEric Dumazet * 6628c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6629c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6630c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6631c8c8b127SEric Dumazet * truesize. 6632c8c8b127SEric Dumazet * Notes: 6633c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6634c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6635c8c8b127SEric Dumazet */ 6636c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6637c8c8b127SEric Dumazet { 66383174fed9SEric Dumazet if (skb->data_len) { 66393174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6640c8c8b127SEric Dumazet skb_cloned(skb)) 6641c8c8b127SEric Dumazet return; 6642c8c8b127SEric Dumazet 6643c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6644c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 66453174fed9SEric Dumazet } 66463174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 66473174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 66483174fed9SEric Dumazet * their truesize. 66493174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 66503174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 66513174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6652c8c8b127SEric Dumazet */ 6653c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6654c8c8b127SEric Dumazet } 6655d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6656df5042f4SFlorian Westphal 6657df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6658df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6659df5042f4SFlorian Westphal { 6660df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6661df5042f4SFlorian Westphal } 6662df5042f4SFlorian Westphal 66638b69a803SPaolo Abeni /** 66648b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 66658b69a803SPaolo Abeni * 66664930f483SFlorian Westphal * @flags: See kmalloc(). 66674930f483SFlorian Westphal * 66688b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 66698b69a803SPaolo Abeni * skb via __skb_ext_set(). 66708b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 66718b69a803SPaolo Abeni */ 66724930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6673df5042f4SFlorian Westphal { 66744930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6675df5042f4SFlorian Westphal 6676df5042f4SFlorian Westphal if (new) { 6677df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6678df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6679df5042f4SFlorian Westphal } 6680df5042f4SFlorian Westphal 6681df5042f4SFlorian Westphal return new; 6682df5042f4SFlorian Westphal } 6683df5042f4SFlorian Westphal 66844165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 66854165079bSFlorian Westphal unsigned int old_active) 6686df5042f4SFlorian Westphal { 6687df5042f4SFlorian Westphal struct skb_ext *new; 6688df5042f4SFlorian Westphal 6689df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6690df5042f4SFlorian Westphal return old; 6691df5042f4SFlorian Westphal 6692df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6693df5042f4SFlorian Westphal if (!new) 6694df5042f4SFlorian Westphal return NULL; 6695df5042f4SFlorian Westphal 6696df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6697df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6698df5042f4SFlorian Westphal 66994165079bSFlorian Westphal #ifdef CONFIG_XFRM 67004165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 67014165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 67024165079bSFlorian Westphal unsigned int i; 67034165079bSFlorian Westphal 67044165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 67054165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 67064165079bSFlorian Westphal } 67074165079bSFlorian Westphal #endif 6708df5042f4SFlorian Westphal __skb_ext_put(old); 6709df5042f4SFlorian Westphal return new; 6710df5042f4SFlorian Westphal } 6711df5042f4SFlorian Westphal 6712df5042f4SFlorian Westphal /** 67138b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 67148b69a803SPaolo Abeni * @skb: buffer 67158b69a803SPaolo Abeni * @id: extension id 67168b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 67178b69a803SPaolo Abeni * 67188b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 67198b69a803SPaolo Abeni * 67208b69a803SPaolo Abeni * Returns the pointer to the extension. 67218b69a803SPaolo Abeni */ 67228b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 67238b69a803SPaolo Abeni struct skb_ext *ext) 67248b69a803SPaolo Abeni { 67258b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 67268b69a803SPaolo Abeni 67278b69a803SPaolo Abeni skb_ext_put(skb); 67288b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 67298b69a803SPaolo Abeni ext->chunks = newlen; 67308b69a803SPaolo Abeni ext->offset[id] = newoff; 67318b69a803SPaolo Abeni skb->extensions = ext; 67328b69a803SPaolo Abeni skb->active_extensions = 1 << id; 67338b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 67348b69a803SPaolo Abeni } 67358b69a803SPaolo Abeni 67368b69a803SPaolo Abeni /** 6737df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6738df5042f4SFlorian Westphal * @skb: buffer 6739df5042f4SFlorian Westphal * @id: extension to allocate space for 6740df5042f4SFlorian Westphal * 6741df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6742df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6743df5042f4SFlorian Westphal * is returned. 6744df5042f4SFlorian Westphal * 6745df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6746df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6747df5042f4SFlorian Westphal * 6748df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6749df5042f4SFlorian Westphal */ 6750df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6751df5042f4SFlorian Westphal { 6752df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6753df5042f4SFlorian Westphal unsigned int newlen, newoff; 6754df5042f4SFlorian Westphal 6755df5042f4SFlorian Westphal if (skb->active_extensions) { 6756df5042f4SFlorian Westphal old = skb->extensions; 6757df5042f4SFlorian Westphal 67584165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6759df5042f4SFlorian Westphal if (!new) 6760df5042f4SFlorian Westphal return NULL; 6761df5042f4SFlorian Westphal 6762682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6763df5042f4SFlorian Westphal goto set_active; 6764df5042f4SFlorian Westphal 6765e94e50bdSPaolo Abeni newoff = new->chunks; 6766df5042f4SFlorian Westphal } else { 6767df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6768df5042f4SFlorian Westphal 67694930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6770df5042f4SFlorian Westphal if (!new) 6771df5042f4SFlorian Westphal return NULL; 6772df5042f4SFlorian Westphal } 6773df5042f4SFlorian Westphal 6774df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6775df5042f4SFlorian Westphal new->chunks = newlen; 6776df5042f4SFlorian Westphal new->offset[id] = newoff; 6777df5042f4SFlorian Westphal set_active: 6778b0999f38SPaolo Abeni skb->slow_gro = 1; 6779682ec859SPaolo Abeni skb->extensions = new; 6780df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6781df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6782df5042f4SFlorian Westphal } 6783df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6784df5042f4SFlorian Westphal 67854165079bSFlorian Westphal #ifdef CONFIG_XFRM 67864165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 67874165079bSFlorian Westphal { 67884165079bSFlorian Westphal unsigned int i; 67894165079bSFlorian Westphal 67904165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 67914165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 67924165079bSFlorian Westphal } 67934165079bSFlorian Westphal #endif 67944165079bSFlorian Westphal 679578476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 679678476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 679778476d31SJeremy Kerr { 679878476d31SJeremy Kerr if (flow->key) 679978476d31SJeremy Kerr mctp_key_unref(flow->key); 680078476d31SJeremy Kerr } 680178476d31SJeremy Kerr #endif 680278476d31SJeremy Kerr 6803df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6804df5042f4SFlorian Westphal { 6805df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6806df5042f4SFlorian Westphal 6807df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6808df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6809df5042f4SFlorian Westphal skb->extensions = NULL; 6810df5042f4SFlorian Westphal __skb_ext_put(ext); 68114165079bSFlorian Westphal #ifdef CONFIG_XFRM 68124165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 68134165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 68144165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 68154165079bSFlorian Westphal 68164165079bSFlorian Westphal skb_ext_put_sp(sp); 68174165079bSFlorian Westphal sp->len = 0; 68184165079bSFlorian Westphal #endif 6819df5042f4SFlorian Westphal } 6820df5042f4SFlorian Westphal } 6821df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6822df5042f4SFlorian Westphal 6823df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6824df5042f4SFlorian Westphal { 6825df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6826df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6827df5042f4SFlorian Westphal */ 6828df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6829df5042f4SFlorian Westphal goto free_now; 6830df5042f4SFlorian Westphal 6831df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6832df5042f4SFlorian Westphal return; 6833df5042f4SFlorian Westphal free_now: 68344165079bSFlorian Westphal #ifdef CONFIG_XFRM 68354165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 68364165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 68374165079bSFlorian Westphal #endif 683878476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 683978476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 684078476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 684178476d31SJeremy Kerr #endif 68424165079bSFlorian Westphal 6843df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6844df5042f4SFlorian Westphal } 6845df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6846df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 684768822bdfSEric Dumazet 684868822bdfSEric Dumazet /** 684968822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 685068822bdfSEric Dumazet * @skb: buffer 685168822bdfSEric Dumazet * 685268822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 685368822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 685468822bdfSEric Dumazet * and memory zone spinlock contention. 685568822bdfSEric Dumazet */ 685668822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 685768822bdfSEric Dumazet { 685868822bdfSEric Dumazet int cpu = skb->alloc_cpu; 685968822bdfSEric Dumazet struct softnet_data *sd; 686039564c3fSEric Dumazet unsigned int defer_max; 686168822bdfSEric Dumazet bool kick; 686268822bdfSEric Dumazet 686368822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 686468822bdfSEric Dumazet !cpu_online(cpu) || 686568822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 686639564c3fSEric Dumazet nodefer: __kfree_skb(skb); 686768822bdfSEric Dumazet return; 686868822bdfSEric Dumazet } 686968822bdfSEric Dumazet 6870e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb_dst(skb)); 6871e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb->destructor); 6872e8e1ce84SEric Dumazet 687368822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 687439564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 687539564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 687639564c3fSEric Dumazet goto nodefer; 687739564c3fSEric Dumazet 6878931e93bdSEric Dumazet spin_lock_bh(&sd->defer_lock); 6879c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6880c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6881c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6882c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6883c09b0cd2SJakub Kicinski 688468822bdfSEric Dumazet skb->next = sd->defer_list; 688568822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 688668822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 6887931e93bdSEric Dumazet spin_unlock_bh(&sd->defer_lock); 688868822bdfSEric Dumazet 688968822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 689068822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 689168822bdfSEric Dumazet */ 689297e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 689368822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 689468822bdfSEric Dumazet } 6895