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> 70d457a0e3SEric Dumazet #include <net/gso.h> 71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 721da177e4SLinus Torvalds #include <net/xfrm.h> 738822e270SJohn Hurley #include <net/mpls.h> 743ee17bc7SMat Martineau #include <net/mptcp.h> 7578476d31SJeremy Kerr #include <net/mctp.h> 7675eaf63eSAlexander Lobakin #include <net/page_pool/helpers.h> 77071c0fc6SJohannes Berg #include <net/dropreason.h> 781da177e4SLinus Torvalds 797c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 80ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 8151c56b00SEric Dumazet #include <linux/highmem.h> 82b245be1fSWillem de Bruijn #include <linux/capability.h> 83b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 842544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 852195e2a0SJakub Kicinski #include <linux/textsearch.h> 86a1f8e7f7SAl Viro 8739564c3fSEric Dumazet #include "dev.h" 887f678defSVasily Averin #include "sock_destructor.h" 897b7ed885SBart Van Assche 90025a785fSJakub Kicinski struct kmem_cache *skbuff_cache __ro_after_init; 9108009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 92df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 93df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 94df5042f4SFlorian Westphal #endif 95bf9f1baaSEric Dumazet 96bf9f1baaSEric Dumazet 97bf9f1baaSEric Dumazet static struct kmem_cache *skb_small_head_cache __ro_after_init; 98bf9f1baaSEric Dumazet 99bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(MAX_TCP_HEADER) 100bf9f1baaSEric Dumazet 101bf9f1baaSEric Dumazet /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two. 102bf9f1baaSEric Dumazet * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique 103bf9f1baaSEric Dumazet * size, and we can differentiate heads from skb_small_head_cache 104bf9f1baaSEric Dumazet * vs system slabs by looking at their size (skb_end_offset()). 105bf9f1baaSEric Dumazet */ 106bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_CACHE_SIZE \ 107bf9f1baaSEric Dumazet (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \ 108bf9f1baaSEric Dumazet (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \ 109bf9f1baaSEric Dumazet SKB_SMALL_HEAD_SIZE) 110bf9f1baaSEric Dumazet 111bf9f1baaSEric Dumazet #define SKB_SMALL_HEAD_HEADROOM \ 112bf9f1baaSEric Dumazet SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE) 113bf9f1baaSEric Dumazet 1145f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 1155f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 1161da177e4SLinus Torvalds 1179cb252c4SMenglong Dong #undef FN 1189cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 119071c0fc6SJohannes Berg static const char * const drop_reasons[] = { 1200e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 1219cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 1229cb252c4SMenglong Dong }; 123071c0fc6SJohannes Berg 124071c0fc6SJohannes Berg static const struct drop_reason_list drop_reasons_core = { 125071c0fc6SJohannes Berg .reasons = drop_reasons, 126071c0fc6SJohannes Berg .n_reasons = ARRAY_SIZE(drop_reasons), 127071c0fc6SJohannes Berg }; 128071c0fc6SJohannes Berg 129071c0fc6SJohannes Berg const struct drop_reason_list __rcu * 130071c0fc6SJohannes Berg drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = { 131071c0fc6SJohannes Berg [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core), 132071c0fc6SJohannes Berg }; 133071c0fc6SJohannes Berg EXPORT_SYMBOL(drop_reasons_by_subsys); 134071c0fc6SJohannes Berg 135071c0fc6SJohannes Berg /** 136071c0fc6SJohannes Berg * drop_reasons_register_subsys - register another drop reason subsystem 137071c0fc6SJohannes Berg * @subsys: the subsystem to register, must not be the core 138071c0fc6SJohannes Berg * @list: the list of drop reasons within the subsystem, must point to 139071c0fc6SJohannes Berg * a statically initialized list 140071c0fc6SJohannes Berg */ 141071c0fc6SJohannes Berg void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys, 142071c0fc6SJohannes Berg const struct drop_reason_list *list) 143071c0fc6SJohannes Berg { 144071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 145071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 146071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 147071c0fc6SJohannes Berg return; 148071c0fc6SJohannes Berg 149071c0fc6SJohannes Berg /* must point to statically allocated memory, so INIT is OK */ 150071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list); 151071c0fc6SJohannes Berg } 152071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_register_subsys); 153071c0fc6SJohannes Berg 154071c0fc6SJohannes Berg /** 155071c0fc6SJohannes Berg * drop_reasons_unregister_subsys - unregister a drop reason subsystem 156071c0fc6SJohannes Berg * @subsys: the subsystem to remove, must not be the core 157071c0fc6SJohannes Berg * 158071c0fc6SJohannes Berg * Note: This will synchronize_rcu() to ensure no users when it returns. 159071c0fc6SJohannes Berg */ 160071c0fc6SJohannes Berg void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys) 161071c0fc6SJohannes Berg { 162071c0fc6SJohannes Berg if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE || 163071c0fc6SJohannes Berg subsys >= ARRAY_SIZE(drop_reasons_by_subsys), 164071c0fc6SJohannes Berg "invalid subsystem %d\n", subsys)) 165071c0fc6SJohannes Berg return; 166071c0fc6SJohannes Berg 167071c0fc6SJohannes Berg RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL); 168071c0fc6SJohannes Berg 169071c0fc6SJohannes Berg synchronize_rcu(); 170071c0fc6SJohannes Berg } 171071c0fc6SJohannes Berg EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys); 172ec43908dSMenglong Dong 1731da177e4SLinus Torvalds /** 174f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1751da177e4SLinus Torvalds * @skb: buffer 1761da177e4SLinus Torvalds * @sz: size 177f05de73bSJean Sacren * @addr: address 17899d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1791da177e4SLinus Torvalds * 180f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 181f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 182f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 183f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1841da177e4SLinus Torvalds */ 185f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 18699d5851eSJames Hogan const char msg[]) 1871da177e4SLinus Torvalds { 18841a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 18999d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1904305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 19126095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1921da177e4SLinus Torvalds BUG(); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1961da177e4SLinus Torvalds { 197f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 200f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 201f05de73bSJean Sacren { 202f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 203f05de73bSJean Sacren } 204c93bdd0eSMel Gorman 20550fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 206f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 207f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 20850fad4b5SAlexander Lobakin 209dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 210dbae2b06SPaolo Abeni 211dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 212dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 213dbae2b06SPaolo Abeni 214dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 215dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 216dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 217dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 218dbae2b06SPaolo Abeni */ 219dbae2b06SPaolo Abeni 220dbae2b06SPaolo Abeni struct page_frag_1k { 221dbae2b06SPaolo Abeni void *va; 222dbae2b06SPaolo Abeni u16 offset; 223dbae2b06SPaolo Abeni bool pfmemalloc; 224dbae2b06SPaolo Abeni }; 225dbae2b06SPaolo Abeni 226dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 227dbae2b06SPaolo Abeni { 228dbae2b06SPaolo Abeni struct page *page; 229dbae2b06SPaolo Abeni int offset; 230dbae2b06SPaolo Abeni 231dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 232dbae2b06SPaolo Abeni if (likely(offset >= 0)) 233dbae2b06SPaolo Abeni goto use_frag; 234dbae2b06SPaolo Abeni 235dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 236dbae2b06SPaolo Abeni if (!page) 237dbae2b06SPaolo Abeni return NULL; 238dbae2b06SPaolo Abeni 239dbae2b06SPaolo Abeni nc->va = page_address(page); 240dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 241dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 242dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 243dbae2b06SPaolo Abeni 244dbae2b06SPaolo Abeni use_frag: 245dbae2b06SPaolo Abeni nc->offset = offset; 246dbae2b06SPaolo Abeni return nc->va + offset; 247dbae2b06SPaolo Abeni } 248dbae2b06SPaolo Abeni #else 249dbae2b06SPaolo Abeni 250dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 251dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 252dbae2b06SPaolo Abeni */ 253dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 254dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 255dbae2b06SPaolo Abeni 256dbae2b06SPaolo Abeni struct page_frag_1k { 257dbae2b06SPaolo Abeni }; 258dbae2b06SPaolo Abeni 259dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 260dbae2b06SPaolo Abeni { 261dbae2b06SPaolo Abeni return NULL; 262dbae2b06SPaolo Abeni } 263dbae2b06SPaolo Abeni 264dbae2b06SPaolo Abeni #endif 265dbae2b06SPaolo Abeni 26650fad4b5SAlexander Lobakin struct napi_alloc_cache { 26750fad4b5SAlexander Lobakin struct page_frag_cache page; 268dbae2b06SPaolo Abeni struct page_frag_1k page_small; 26950fad4b5SAlexander Lobakin unsigned int skb_count; 27050fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 27150fad4b5SAlexander Lobakin }; 27250fad4b5SAlexander Lobakin 27350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 27450fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 27550fad4b5SAlexander Lobakin 276dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 277dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 278dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 279dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 280dbae2b06SPaolo Abeni * does not accidentally come back. 281dbae2b06SPaolo Abeni */ 282dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 283dbae2b06SPaolo Abeni { 284dbae2b06SPaolo Abeni struct sk_buff *skb; 285dbae2b06SPaolo Abeni 286dbae2b06SPaolo Abeni local_bh_disable(); 287dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 288dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 289dbae2b06SPaolo Abeni napi_free_frags(napi); 290dbae2b06SPaolo Abeni local_bh_enable(); 291dbae2b06SPaolo Abeni } 292dbae2b06SPaolo Abeni 29332e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 29450fad4b5SAlexander Lobakin { 29550fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 29650fad4b5SAlexander Lobakin 29750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 29850fad4b5SAlexander Lobakin 29932e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 30050fad4b5SAlexander Lobakin } 30150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 30250fad4b5SAlexander Lobakin 30350fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 30450fad4b5SAlexander Lobakin { 30550fad4b5SAlexander Lobakin void *data; 30650fad4b5SAlexander Lobakin 30750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 308afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 30932e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 31032e3573fSYajun Deng 31150fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 31250fad4b5SAlexander Lobakin } else { 31332e3573fSYajun Deng struct napi_alloc_cache *nc; 31432e3573fSYajun Deng 31550fad4b5SAlexander Lobakin local_bh_disable(); 31632e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 31732e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 31850fad4b5SAlexander Lobakin local_bh_enable(); 31950fad4b5SAlexander Lobakin } 32050fad4b5SAlexander Lobakin return data; 32150fad4b5SAlexander Lobakin } 32250fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 32350fad4b5SAlexander Lobakin 324f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 325f450d539SAlexander Lobakin { 326f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 327f450d539SAlexander Lobakin struct sk_buff *skb; 328f450d539SAlexander Lobakin 32949ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 330025a785fSJakub Kicinski nc->skb_count = kmem_cache_alloc_bulk(skbuff_cache, 331f450d539SAlexander Lobakin GFP_ATOMIC, 332f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 333f450d539SAlexander Lobakin nc->skb_cache); 334f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 335f450d539SAlexander Lobakin return NULL; 33649ae83fcSSieng Piaw Liew } 337f450d539SAlexander Lobakin 338f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 339025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, skb); 340f450d539SAlexander Lobakin 341f450d539SAlexander Lobakin return skb; 342f450d539SAlexander Lobakin } 343f450d539SAlexander Lobakin 344ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 345ce098da1SKees Cook unsigned int size) 346ba0509b6SJesper Dangaard Brouer { 347ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 348ba0509b6SJesper Dangaard Brouer 349ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 350ba0509b6SJesper Dangaard Brouer 351ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 352ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 353ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 354ba0509b6SJesper Dangaard Brouer skb->head = data; 355ba0509b6SJesper Dangaard Brouer skb->data = data; 356ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 357763087daSEric Dumazet skb_set_end_offset(skb, size); 358ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 359ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 36068822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 361ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 362ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 363ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 364ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 365ba0509b6SJesper Dangaard Brouer 3666370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 367ba0509b6SJesper Dangaard Brouer } 368ba0509b6SJesper Dangaard Brouer 369ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 370ce098da1SKees Cook unsigned int *size) 371ce098da1SKees Cook { 372ce098da1SKees Cook void *resized; 373ce098da1SKees Cook 374ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */ 375ce098da1SKees Cook *size = ksize(data); 376ce098da1SKees Cook /* krealloc() will immediately return "data" when 377ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper 378ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note 379ce098da1SKees Cook * that this "new" pointer needs to be passed back to the 380ce098da1SKees Cook * caller for use so the __alloc_size hinting will be 381ce098da1SKees Cook * tracked correctly. 382ce098da1SKees Cook */ 383ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC); 384ce098da1SKees Cook WARN_ON_ONCE(resized != data); 385ce098da1SKees Cook return resized; 386ce098da1SKees Cook } 387ce098da1SKees Cook 388ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers. 389ce098da1SKees Cook * Note that this should be used sparingly as slab buffers 390ce098da1SKees Cook * cannot be combined efficiently by GRO! 391ce098da1SKees Cook */ 392ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data) 393ce098da1SKees Cook { 394ce098da1SKees Cook struct sk_buff *skb; 395ce098da1SKees Cook unsigned int size; 396ce098da1SKees Cook 397025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 398ce098da1SKees Cook if (unlikely(!skb)) 399ce098da1SKees Cook return NULL; 400ce098da1SKees Cook 401ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail)); 402ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 403ce098da1SKees Cook __finalize_skb_around(skb, data, size); 404ce098da1SKees Cook 405ce098da1SKees Cook return skb; 406ce098da1SKees Cook } 407ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb); 408ce098da1SKees Cook 409ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */ 410ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data, 411ce098da1SKees Cook unsigned int frag_size) 412ce098da1SKees Cook { 413ce098da1SKees Cook unsigned int size = frag_size; 414ce098da1SKees Cook 415ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers 416ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead. 417ce098da1SKees Cook */ 418ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 419ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 420ce098da1SKees Cook 421ce098da1SKees Cook __finalize_skb_around(skb, data, size); 422ce098da1SKees Cook } 423ce098da1SKees Cook 4241da177e4SLinus Torvalds /** 4252ea2f62cSEric Dumazet * __build_skb - build a network buffer 426b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 427ce098da1SKees Cook * @frag_size: size of data (must not be 0) 428b2b5ce9dSEric Dumazet * 429b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 430ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page 431ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 432ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb() 433ce098da1SKees Cook * instead.) 434b2b5ce9dSEric Dumazet * The return is the new skb buffer. 435b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 436b2b5ce9dSEric Dumazet * Notes : 437b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 438b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 439b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 440b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 441b2b5ce9dSEric Dumazet * before giving packet to stack. 442b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 443b2b5ce9dSEric Dumazet */ 4442ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 445b2b5ce9dSEric Dumazet { 446b2b5ce9dSEric Dumazet struct sk_buff *skb; 447b2b5ce9dSEric Dumazet 448025a785fSJakub Kicinski skb = kmem_cache_alloc(skbuff_cache, GFP_ATOMIC); 449ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 450b2b5ce9dSEric Dumazet return NULL; 451b2b5ce9dSEric Dumazet 452b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 453483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 454b2b5ce9dSEric Dumazet 455483126b3SAlexander Lobakin return skb; 456b2b5ce9dSEric Dumazet } 4572ea2f62cSEric Dumazet 4582ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 4592ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 4602ea2f62cSEric Dumazet */ 4612ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 4622ea2f62cSEric Dumazet { 4632ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 4642ea2f62cSEric Dumazet 4653c640126SGal Pressman if (likely(skb && frag_size)) { 4662ea2f62cSEric Dumazet skb->head_frag = 1; 467566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 4682ea2f62cSEric Dumazet } 4692ea2f62cSEric Dumazet return skb; 4702ea2f62cSEric Dumazet } 471b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 472b2b5ce9dSEric Dumazet 473ba0509b6SJesper Dangaard Brouer /** 474ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 475ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 476ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 47712c1604aSJakub Kicinski * @frag_size: size of data 478ba0509b6SJesper Dangaard Brouer */ 479ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 480ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 481ba0509b6SJesper Dangaard Brouer { 482ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 483ba0509b6SJesper Dangaard Brouer return NULL; 484ba0509b6SJesper Dangaard Brouer 485483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 486ba0509b6SJesper Dangaard Brouer 487483126b3SAlexander Lobakin if (frag_size) { 488ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 489566b6701SGal Pressman skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 490ba0509b6SJesper Dangaard Brouer } 491ba0509b6SJesper Dangaard Brouer return skb; 492ba0509b6SJesper Dangaard Brouer } 493ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 494ba0509b6SJesper Dangaard Brouer 495f450d539SAlexander Lobakin /** 496f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 497f450d539SAlexander Lobakin * @data: data buffer provided by caller 49812c1604aSJakub Kicinski * @frag_size: size of data 499f450d539SAlexander Lobakin * 500f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 501f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 502f450d539SAlexander Lobakin * 503f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 504f450d539SAlexander Lobakin */ 505f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 506f450d539SAlexander Lobakin { 507f450d539SAlexander Lobakin struct sk_buff *skb; 508f450d539SAlexander Lobakin 509f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 510f450d539SAlexander Lobakin if (unlikely(!skb)) 511f450d539SAlexander Lobakin return NULL; 512f450d539SAlexander Lobakin 513f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 514f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 515f450d539SAlexander Lobakin 516f450d539SAlexander Lobakin return skb; 517f450d539SAlexander Lobakin } 518f450d539SAlexander Lobakin 519f450d539SAlexander Lobakin /** 520f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 521f450d539SAlexander Lobakin * @data: data buffer provided by caller 52212c1604aSJakub Kicinski * @frag_size: size of data 523f450d539SAlexander Lobakin * 524f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 525f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 526f450d539SAlexander Lobakin * 527f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 528f450d539SAlexander Lobakin */ 529f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 530f450d539SAlexander Lobakin { 531f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 532f450d539SAlexander Lobakin 533f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 534f450d539SAlexander Lobakin skb->head_frag = 1; 535f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 536f450d539SAlexander Lobakin } 537f450d539SAlexander Lobakin 538f450d539SAlexander Lobakin return skb; 539f450d539SAlexander Lobakin } 540f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 541f450d539SAlexander Lobakin 5425381b23dSAlexander Lobakin /* 5435381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 5445381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 5455381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 5465381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 5475381b23dSAlexander Lobakin * memory is free 5485381b23dSAlexander Lobakin */ 5495c0e820cSEric Dumazet static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node, 550ef28095fSAlexander Lobakin bool *pfmemalloc) 5515381b23dSAlexander Lobakin { 5525381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 553915d975bSEric Dumazet size_t obj_size; 5545c0e820cSEric Dumazet void *obj; 5555381b23dSAlexander Lobakin 5565c0e820cSEric Dumazet obj_size = SKB_HEAD_ALIGN(*size); 557bf9f1baaSEric Dumazet if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE && 558bf9f1baaSEric Dumazet !(flags & KMALLOC_NOT_NORMAL_BITS)) { 559bf9f1baaSEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, 560bf9f1baaSEric Dumazet flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 561bf9f1baaSEric Dumazet node); 562bf9f1baaSEric Dumazet *size = SKB_SMALL_HEAD_CACHE_SIZE; 563880ce5f2SEric Dumazet if (obj || !(gfp_pfmemalloc_allowed(flags))) 564bf9f1baaSEric Dumazet goto out; 565880ce5f2SEric Dumazet /* Try again but now we are using pfmemalloc reserves */ 566880ce5f2SEric Dumazet ret_pfmemalloc = true; 567880ce5f2SEric Dumazet obj = kmem_cache_alloc_node(skb_small_head_cache, flags, node); 568880ce5f2SEric Dumazet goto out; 569bf9f1baaSEric Dumazet } 570915d975bSEric Dumazet 571915d975bSEric Dumazet obj_size = kmalloc_size_roundup(obj_size); 572915d975bSEric Dumazet /* The following cast might truncate high-order bits of obj_size, this 573915d975bSEric Dumazet * is harmless because kmalloc(obj_size >= 2^32) will fail anyway. 574915d975bSEric Dumazet */ 575915d975bSEric Dumazet *size = (unsigned int)obj_size; 576915d975bSEric Dumazet 5775381b23dSAlexander Lobakin /* 5785381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 5795381b23dSAlexander Lobakin * to the reserves, fail. 5805381b23dSAlexander Lobakin */ 5815c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, 5825381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 5835381b23dSAlexander Lobakin node); 5845381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 5855381b23dSAlexander Lobakin goto out; 5865381b23dSAlexander Lobakin 5875381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 5885381b23dSAlexander Lobakin ret_pfmemalloc = true; 5895c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, flags, node); 5905381b23dSAlexander Lobakin 5915381b23dSAlexander Lobakin out: 5925381b23dSAlexander Lobakin if (pfmemalloc) 5935381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5945381b23dSAlexander Lobakin 5955381b23dSAlexander Lobakin return obj; 5965381b23dSAlexander Lobakin } 5975381b23dSAlexander Lobakin 5985381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 5995381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 6005381b23dSAlexander Lobakin * [BEEP] leaks. 6015381b23dSAlexander Lobakin * 6025381b23dSAlexander Lobakin */ 6035381b23dSAlexander Lobakin 6045381b23dSAlexander Lobakin /** 6055381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 6065381b23dSAlexander Lobakin * @size: size to allocate 6075381b23dSAlexander Lobakin * @gfp_mask: allocation mask 6085381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 6095381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 6105381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 6115381b23dSAlexander Lobakin * allocations in case the data is required for writeback 6125381b23dSAlexander Lobakin * @node: numa node to allocate memory on 6135381b23dSAlexander Lobakin * 6145381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 6155381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 6165381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 6175381b23dSAlexander Lobakin * 6185381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 6195381b23dSAlexander Lobakin * %GFP_ATOMIC. 6205381b23dSAlexander Lobakin */ 6215381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 6225381b23dSAlexander Lobakin int flags, int node) 6235381b23dSAlexander Lobakin { 6245381b23dSAlexander Lobakin struct kmem_cache *cache; 6255381b23dSAlexander Lobakin struct sk_buff *skb; 6265381b23dSAlexander Lobakin bool pfmemalloc; 627a5df6333SLi RongQing u8 *data; 6285381b23dSAlexander Lobakin 6295381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 630025a785fSJakub Kicinski ? skbuff_fclone_cache : skbuff_cache; 6315381b23dSAlexander Lobakin 6325381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 6335381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 6345381b23dSAlexander Lobakin 6355381b23dSAlexander Lobakin /* Get the HEAD */ 636d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 637d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 638d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 639d13612b5SAlexander Lobakin else 640d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 641df1ae022SAlexander Lobakin if (unlikely(!skb)) 642df1ae022SAlexander Lobakin return NULL; 6435381b23dSAlexander Lobakin prefetchw(skb); 6445381b23dSAlexander Lobakin 6455381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 6465381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 6475381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 6485381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 6495381b23dSAlexander Lobakin */ 6505c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc); 651df1ae022SAlexander Lobakin if (unlikely(!data)) 6525381b23dSAlexander Lobakin goto nodata; 65312d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 6545381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 6555381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 6565381b23dSAlexander Lobakin */ 65765998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size)); 6585381b23dSAlexander Lobakin 6595381b23dSAlexander Lobakin /* 6605381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 6615381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 6625381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 6635381b23dSAlexander Lobakin */ 6645381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 66565998d2bSEric Dumazet __build_skb_around(skb, data, size); 6665381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 6675381b23dSAlexander Lobakin 6685381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 6695381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 6705381b23dSAlexander Lobakin 6715381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 6725381b23dSAlexander Lobakin 6735381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 6745381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 6755381b23dSAlexander Lobakin } 6765381b23dSAlexander Lobakin 6775381b23dSAlexander Lobakin return skb; 678df1ae022SAlexander Lobakin 6795381b23dSAlexander Lobakin nodata: 6805381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 681df1ae022SAlexander Lobakin return NULL; 6825381b23dSAlexander Lobakin } 6835381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 6845381b23dSAlexander Lobakin 6857ba7aeabSSebastian Andrzej Siewior /** 686fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 687fd11a83dSAlexander Duyck * @dev: network device to receive on 688d7499160SMasanari Iida * @len: length to allocate 689fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 690fd11a83dSAlexander Duyck * 691fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 692fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 693fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 694fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 695fd11a83dSAlexander Duyck * 696fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 697fd11a83dSAlexander Duyck */ 6989451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 6999451980aSAlexander Duyck gfp_t gfp_mask) 700fd11a83dSAlexander Duyck { 701b63ae8caSAlexander Duyck struct page_frag_cache *nc; 702fd11a83dSAlexander Duyck struct sk_buff *skb; 7039451980aSAlexander Duyck bool pfmemalloc; 7049451980aSAlexander Duyck void *data; 705fd11a83dSAlexander Duyck 7069451980aSAlexander Duyck len += NET_SKB_PAD; 707fd11a83dSAlexander Duyck 70866c55602SAlexander Lobakin /* If requested length is either too small or too big, 70966c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 71066c55602SAlexander Lobakin */ 71166c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 71266c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 713d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 714a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 715a080e7bdSAlexander Duyck if (!skb) 716a080e7bdSAlexander Duyck goto skb_fail; 717a080e7bdSAlexander Duyck goto skb_success; 718a080e7bdSAlexander Duyck } 7199451980aSAlexander Duyck 720115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 7219451980aSAlexander Duyck 7229451980aSAlexander Duyck if (sk_memalloc_socks()) 7239451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7249451980aSAlexander Duyck 725afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 7269451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 7278c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 7289451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 72992dcabd7SSebastian Andrzej Siewior } else { 73092dcabd7SSebastian Andrzej Siewior local_bh_disable(); 73192dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 73292dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 73392dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 73492dcabd7SSebastian Andrzej Siewior local_bh_enable(); 73592dcabd7SSebastian Andrzej Siewior } 7369451980aSAlexander Duyck 7379451980aSAlexander Duyck if (unlikely(!data)) 7389451980aSAlexander Duyck return NULL; 7399451980aSAlexander Duyck 7409451980aSAlexander Duyck skb = __build_skb(data, len); 7419451980aSAlexander Duyck if (unlikely(!skb)) { 742181edb2bSAlexander Duyck skb_free_frag(data); 7439451980aSAlexander Duyck return NULL; 7449451980aSAlexander Duyck } 7459451980aSAlexander Duyck 7469451980aSAlexander Duyck if (pfmemalloc) 7479451980aSAlexander Duyck skb->pfmemalloc = 1; 7489451980aSAlexander Duyck skb->head_frag = 1; 7499451980aSAlexander Duyck 750a080e7bdSAlexander Duyck skb_success: 7518af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 7527b2e497aSChristoph Hellwig skb->dev = dev; 753fd11a83dSAlexander Duyck 754a080e7bdSAlexander Duyck skb_fail: 7558af27456SChristoph Hellwig return skb; 7568af27456SChristoph Hellwig } 757b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 7581da177e4SLinus Torvalds 759fd11a83dSAlexander Duyck /** 760fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 761fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 762d7499160SMasanari Iida * @len: length to allocate 763fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 764fd11a83dSAlexander Duyck * 765fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 766fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 767fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 768fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 769fd11a83dSAlexander Duyck * 770fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 771fd11a83dSAlexander Duyck */ 7729451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 7739451980aSAlexander Duyck gfp_t gfp_mask) 774fd11a83dSAlexander Duyck { 7753226b158SEric Dumazet struct napi_alloc_cache *nc; 776fd11a83dSAlexander Duyck struct sk_buff *skb; 777dbae2b06SPaolo Abeni bool pfmemalloc; 7789451980aSAlexander Duyck void *data; 779fd11a83dSAlexander Duyck 780ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 7819451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 782fd11a83dSAlexander Duyck 7833226b158SEric Dumazet /* If requested length is either too small or too big, 7843226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 785dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 786dbae2b06SPaolo Abeni * for small fragments 7873226b158SEric Dumazet */ 788dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7893226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 790d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 791cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 792cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 793a080e7bdSAlexander Duyck if (!skb) 794a080e7bdSAlexander Duyck goto skb_fail; 795a080e7bdSAlexander Duyck goto skb_success; 796a080e7bdSAlexander Duyck } 7979451980aSAlexander Duyck 7983226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 7999451980aSAlexander Duyck 8009451980aSAlexander Duyck if (sk_memalloc_socks()) 8019451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 8029451980aSAlexander Duyck 803dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 804dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 805dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 806dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 807dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 808dbae2b06SPaolo Abeni * size above 512 bytes 809dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 810dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 811dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 812dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 813dbae2b06SPaolo Abeni */ 814dbae2b06SPaolo Abeni len = SZ_1K; 815dbae2b06SPaolo Abeni 816dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 817dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 818dbae2b06SPaolo Abeni } else { 819115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 820dbae2b06SPaolo Abeni 8218c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 822dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 823dbae2b06SPaolo Abeni } 824dbae2b06SPaolo Abeni 8259451980aSAlexander Duyck if (unlikely(!data)) 8269451980aSAlexander Duyck return NULL; 8279451980aSAlexander Duyck 828cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 8299451980aSAlexander Duyck if (unlikely(!skb)) { 830181edb2bSAlexander Duyck skb_free_frag(data); 8319451980aSAlexander Duyck return NULL; 8329451980aSAlexander Duyck } 8339451980aSAlexander Duyck 834dbae2b06SPaolo Abeni if (pfmemalloc) 8359451980aSAlexander Duyck skb->pfmemalloc = 1; 8369451980aSAlexander Duyck skb->head_frag = 1; 8379451980aSAlexander Duyck 838a080e7bdSAlexander Duyck skb_success: 839fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 840fd11a83dSAlexander Duyck skb->dev = napi->dev; 841fd11a83dSAlexander Duyck 842a080e7bdSAlexander Duyck skb_fail: 843fd11a83dSAlexander Duyck return skb; 844fd11a83dSAlexander Duyck } 845fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 846fd11a83dSAlexander Duyck 847654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 84850269e19SEric Dumazet int size, unsigned int truesize) 849654bed16SPeter Zijlstra { 850654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 851654bed16SPeter Zijlstra skb->len += size; 852654bed16SPeter Zijlstra skb->data_len += size; 85350269e19SEric Dumazet skb->truesize += truesize; 854654bed16SPeter Zijlstra } 855654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 856654bed16SPeter Zijlstra 857f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 858f8e617e1SJason Wang unsigned int truesize) 859f8e617e1SJason Wang { 860f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 861f8e617e1SJason Wang 862f8e617e1SJason Wang skb_frag_size_add(frag, size); 863f8e617e1SJason Wang skb->len += size; 864f8e617e1SJason Wang skb->data_len += size; 865f8e617e1SJason Wang skb->truesize += truesize; 866f8e617e1SJason Wang } 867f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 868f8e617e1SJason Wang 86927b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 8701da177e4SLinus Torvalds { 871bd8a7036SEric Dumazet kfree_skb_list(*listp); 87227b437c8SHerbert Xu *listp = NULL; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 87527b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 87627b437c8SHerbert Xu { 87727b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 87827b437c8SHerbert Xu } 87927b437c8SHerbert Xu 8801da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 8811da177e4SLinus Torvalds { 8821da177e4SLinus Torvalds struct sk_buff *list; 8831da177e4SLinus Torvalds 884fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 8851da177e4SLinus Torvalds skb_get(list); 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88875eaf63eSAlexander Lobakin #if IS_ENABLED(CONFIG_PAGE_POOL) 88975eaf63eSAlexander Lobakin bool napi_pp_put_page(struct page *page, bool napi_safe) 89075eaf63eSAlexander Lobakin { 8915b899c33SAlexander Lobakin bool allow_direct = false; 89275eaf63eSAlexander Lobakin struct page_pool *pp; 89375eaf63eSAlexander Lobakin 89475eaf63eSAlexander Lobakin page = compound_head(page); 89575eaf63eSAlexander Lobakin 89675eaf63eSAlexander Lobakin /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation 89775eaf63eSAlexander Lobakin * in order to preserve any existing bits, such as bit 0 for the 89875eaf63eSAlexander Lobakin * head page of compound page and bit 1 for pfmemalloc page, so 89975eaf63eSAlexander Lobakin * mask those bits for freeing side when doing below checking, 90075eaf63eSAlexander Lobakin * and page_is_pfmemalloc() is checked in __page_pool_put_page() 90175eaf63eSAlexander Lobakin * to avoid recycling the pfmemalloc page. 90275eaf63eSAlexander Lobakin */ 90375eaf63eSAlexander Lobakin if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE)) 90475eaf63eSAlexander Lobakin return false; 90575eaf63eSAlexander Lobakin 90675eaf63eSAlexander Lobakin pp = page->pp; 90775eaf63eSAlexander Lobakin 90875eaf63eSAlexander Lobakin /* Allow direct recycle if we have reasons to believe that we are 90975eaf63eSAlexander Lobakin * in the same context as the consumer would run, so there's 91075eaf63eSAlexander Lobakin * no possible race. 9114a36d018SAlexander Lobakin * __page_pool_put_page() makes sure we're not in hardirq context 9124a36d018SAlexander Lobakin * and interrupts are enabled prior to accessing the cache. 91375eaf63eSAlexander Lobakin */ 9144a36d018SAlexander Lobakin if (napi_safe || in_softirq()) { 9155b899c33SAlexander Lobakin const struct napi_struct *napi = READ_ONCE(pp->p.napi); 9165b899c33SAlexander Lobakin 9175b899c33SAlexander Lobakin allow_direct = napi && 91875eaf63eSAlexander Lobakin READ_ONCE(napi->list_owner) == smp_processor_id(); 9195b899c33SAlexander Lobakin } 92075eaf63eSAlexander Lobakin 92175eaf63eSAlexander Lobakin /* Driver set this to memory recycling info. Reset it on recycle. 92275eaf63eSAlexander Lobakin * This will *not* work for NIC using a split-page memory model. 92375eaf63eSAlexander Lobakin * The page will be returned to the pool here regardless of the 92475eaf63eSAlexander Lobakin * 'flipped' fragment being in use or not. 92575eaf63eSAlexander Lobakin */ 92675eaf63eSAlexander Lobakin page_pool_put_full_page(pp, page, allow_direct); 92775eaf63eSAlexander Lobakin 92875eaf63eSAlexander Lobakin return true; 92975eaf63eSAlexander Lobakin } 93075eaf63eSAlexander Lobakin EXPORT_SYMBOL(napi_pp_put_page); 93175eaf63eSAlexander Lobakin #endif 93275eaf63eSAlexander Lobakin 933b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) 9344727bab4SYunsheng Lin { 9354727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 9364727bab4SYunsheng Lin return false; 93775eaf63eSAlexander Lobakin return napi_pp_put_page(virt_to_page(data), napi_safe); 9384727bab4SYunsheng Lin } 9394727bab4SYunsheng Lin 940bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset) 941bf9f1baaSEric Dumazet { 942bf9f1baaSEric Dumazet if (end_offset == SKB_SMALL_HEAD_HEADROOM) 943bf9f1baaSEric Dumazet kmem_cache_free(skb_small_head_cache, head); 944bf9f1baaSEric Dumazet else 945bf9f1baaSEric Dumazet kfree(head); 946bf9f1baaSEric Dumazet } 947bf9f1baaSEric Dumazet 948b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe) 949d3836f21SEric Dumazet { 950181edb2bSAlexander Duyck unsigned char *head = skb->head; 951181edb2bSAlexander Duyck 9526a5bcd84SIlias Apalodimas if (skb->head_frag) { 953b07a2d97SJakub Kicinski if (skb_pp_recycle(skb, head, napi_safe)) 9546a5bcd84SIlias Apalodimas return; 955181edb2bSAlexander Duyck skb_free_frag(head); 9566a5bcd84SIlias Apalodimas } else { 957bf9f1baaSEric Dumazet skb_kfree_head(head, skb_end_offset(skb)); 958d3836f21SEric Dumazet } 9596a5bcd84SIlias Apalodimas } 960d3836f21SEric Dumazet 961b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason, 962b07a2d97SJakub Kicinski bool napi_safe) 9631da177e4SLinus Torvalds { 964ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 9651da177e4SLinus Torvalds int i; 966ff04a771SEric Dumazet 967ff04a771SEric Dumazet if (skb->cloned && 968ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 969ff04a771SEric Dumazet &shinfo->dataref)) 9702cc3aeb5SIlias Apalodimas goto exit; 971ff04a771SEric Dumazet 972753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 973753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 974753f1ca4SPavel Begunkov 97570c43167SJonathan Lemon skb_zcopy_clear(skb, true); 976753f1ca4SPavel Begunkov if (skip_unref) 977753f1ca4SPavel Begunkov goto free_head; 978753f1ca4SPavel Begunkov } 97970c43167SJonathan Lemon 980ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 9818c48eea3SJakub Kicinski napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe); 9821da177e4SLinus Torvalds 983753f1ca4SPavel Begunkov free_head: 984ff04a771SEric Dumazet if (shinfo->frag_list) 985511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 9861da177e4SLinus Torvalds 987b07a2d97SJakub Kicinski skb_free_head(skb, napi_safe); 9882cc3aeb5SIlias Apalodimas exit: 9892cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 9902cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 9912cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 9922cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 9932cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 99458e61e41STom Rix * additional references to the fragmented part of the SKB. 9952cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 9962cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 9972cc3aeb5SIlias Apalodimas */ 9982cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 10021da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 10031da177e4SLinus Torvalds */ 10042d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 10051da177e4SLinus Torvalds { 1006d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 1007d179cd12SDavid S. Miller 1008d179cd12SDavid S. Miller switch (skb->fclone) { 1009d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 1010025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 10116ffe75ebSEric Dumazet return; 1012d179cd12SDavid S. Miller 1013d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 1014d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 10156ffe75ebSEric Dumazet 10166ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 10176ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 10186ffe75ebSEric Dumazet * while here, branch prediction will be good. 10196ffe75ebSEric Dumazet */ 10202638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 10216ffe75ebSEric Dumazet goto fastpath; 1022d179cd12SDavid S. Miller break; 1023d179cd12SDavid S. Miller 10246ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 1025d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 1026d179cd12SDavid S. Miller break; 10273ff50b79SStephen Hemminger } 10282638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 10296ffe75ebSEric Dumazet return; 10306ffe75ebSEric Dumazet fastpath: 10316ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10340a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 10351da177e4SLinus Torvalds { 1036adf30907SEric Dumazet skb_dst_drop(skb); 10371da177e4SLinus Torvalds if (skb->destructor) { 10387890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 10391da177e4SLinus Torvalds skb->destructor(skb); 10401da177e4SLinus Torvalds } 1041a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 1042cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 10432fc72c7bSKOVACS Krisztian #endif 1044df5042f4SFlorian Westphal skb_ext_put(skb); 104504a4bb55SLennert Buytenhek } 104604a4bb55SLennert Buytenhek 104704a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 1048b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason, 1049b07a2d97SJakub Kicinski bool napi_safe) 105004a4bb55SLennert Buytenhek { 105104a4bb55SLennert Buytenhek skb_release_head_state(skb); 1052a28b1b90SFlorian Westphal if (likely(skb->head)) 1053b07a2d97SJakub Kicinski skb_release_data(skb, reason, napi_safe); 10542d4baff8SHerbert Xu } 10551da177e4SLinus Torvalds 10562d4baff8SHerbert Xu /** 10572d4baff8SHerbert Xu * __kfree_skb - private function 10582d4baff8SHerbert Xu * @skb: buffer 10592d4baff8SHerbert Xu * 10602d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 10612d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 10622d4baff8SHerbert Xu * always call kfree_skb 10632d4baff8SHerbert Xu */ 10642d4baff8SHerbert Xu 10652d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 10662d4baff8SHerbert Xu { 1067b07a2d97SJakub Kicinski skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false); 10681da177e4SLinus Torvalds kfree_skbmem(skb); 10691da177e4SLinus Torvalds } 1070b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 10711da177e4SLinus Torvalds 1072a4650da2SJesper Dangaard Brouer static __always_inline 1073a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1074a4650da2SJesper Dangaard Brouer { 1075a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 1076a4650da2SJesper Dangaard Brouer return false; 1077a4650da2SJesper Dangaard Brouer 1078071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || 1079071c0fc6SJohannes Berg u32_get_bits(reason, 1080071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >= 1081071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_NUM); 1082a4650da2SJesper Dangaard Brouer 1083a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 1084dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1085a4650da2SJesper Dangaard Brouer else 1086a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 1087a4650da2SJesper Dangaard Brouer return true; 1088a4650da2SJesper Dangaard Brouer } 1089a4650da2SJesper Dangaard Brouer 10901da177e4SLinus Torvalds /** 1091c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 1092231d06aeSJörn Engel * @skb: buffer to free 1093c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 1094231d06aeSJörn Engel * 1095231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 1096c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 1097c504e5c2SMenglong Dong * tracepoint. 1098231d06aeSJörn Engel */ 1099c205cc75SMenglong Dong void __fix_address 1100c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1101231d06aeSJörn Engel { 1102a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 1103231d06aeSJörn Engel __kfree_skb(skb); 1104231d06aeSJörn Engel } 1105c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 1106231d06aeSJörn Engel 1107eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 1108eedade12SJesper Dangaard Brouer 1109eedade12SJesper Dangaard Brouer struct skb_free_array { 1110eedade12SJesper Dangaard Brouer unsigned int skb_count; 1111eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 1112eedade12SJesper Dangaard Brouer }; 1113eedade12SJesper Dangaard Brouer 1114eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 1115eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 1116eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 1117eedade12SJesper Dangaard Brouer { 1118eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1119eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 1120eedade12SJesper Dangaard Brouer __kfree_skb(skb); 1121eedade12SJesper Dangaard Brouer return; 1122eedade12SJesper Dangaard Brouer } 1123eedade12SJesper Dangaard Brouer 1124b07a2d97SJakub Kicinski skb_release_all(skb, reason, false); 1125eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 1126eedade12SJesper Dangaard Brouer 1127eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 1128025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE, 1129eedade12SJesper Dangaard Brouer sa->skb_array); 1130eedade12SJesper Dangaard Brouer sa->skb_count = 0; 1131eedade12SJesper Dangaard Brouer } 1132eedade12SJesper Dangaard Brouer } 1133eedade12SJesper Dangaard Brouer 1134a4650da2SJesper Dangaard Brouer void __fix_address 1135a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 1136bd8a7036SEric Dumazet { 1137eedade12SJesper Dangaard Brouer struct skb_free_array sa; 1138eedade12SJesper Dangaard Brouer 1139eedade12SJesper Dangaard Brouer sa.skb_count = 0; 1140eedade12SJesper Dangaard Brouer 1141bd8a7036SEric Dumazet while (segs) { 1142bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 1143bd8a7036SEric Dumazet 11449dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) { 11459dde0cd3SJesper Dangaard Brouer skb_poison_list(segs); 1146eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 11479dde0cd3SJesper Dangaard Brouer } 1148eedade12SJesper Dangaard Brouer 1149bd8a7036SEric Dumazet segs = next; 1150bd8a7036SEric Dumazet } 1151eedade12SJesper Dangaard Brouer 1152eedade12SJesper Dangaard Brouer if (sa.skb_count) 1153025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array); 1154bd8a7036SEric Dumazet } 1155215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1156bd8a7036SEric Dumazet 11576413139dSWillem de Bruijn /* Dump skb information and contents. 11586413139dSWillem de Bruijn * 11596413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 11606413139dSWillem de Bruijn * 1161302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 11626413139dSWillem de Bruijn */ 11636413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 11646413139dSWillem de Bruijn { 11656413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 11666413139dSWillem de Bruijn struct net_device *dev = skb->dev; 11676413139dSWillem de Bruijn struct sock *sk = skb->sk; 11686413139dSWillem de Bruijn struct sk_buff *list_skb; 11696413139dSWillem de Bruijn bool has_mac, has_trans; 11706413139dSWillem de Bruijn int headroom, tailroom; 11716413139dSWillem de Bruijn int i, len, seg_len; 11726413139dSWillem de Bruijn 11736413139dSWillem de Bruijn if (full_pkt) 11746413139dSWillem de Bruijn len = skb->len; 11756413139dSWillem de Bruijn else 11766413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 11776413139dSWillem de Bruijn 11786413139dSWillem de Bruijn headroom = skb_headroom(skb); 11796413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 11806413139dSWillem de Bruijn 11816413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 11826413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 11836413139dSWillem de Bruijn 11846413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 11856413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 11866413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 11876413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 11886413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 11896413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 11906413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 11916413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 11926413139dSWillem de Bruijn skb->network_header, 11936413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 11946413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 11956413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 11966413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 11976413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 11986413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 11996413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 12006413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 12016413139dSWillem de Bruijn 12026413139dSWillem de Bruijn if (dev) 12038a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 12046413139dSWillem de Bruijn level, dev->name, &dev->features); 12056413139dSWillem de Bruijn if (sk) 1206db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 12076413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 12086413139dSWillem de Bruijn 12096413139dSWillem de Bruijn if (full_pkt && headroom) 12106413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 12116413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 12126413139dSWillem de Bruijn 12136413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 12146413139dSWillem de Bruijn if (seg_len) 12156413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 12166413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 12176413139dSWillem de Bruijn len -= seg_len; 12186413139dSWillem de Bruijn 12196413139dSWillem de Bruijn if (full_pkt && tailroom) 12206413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 12216413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 12226413139dSWillem de Bruijn 12236413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 12246413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 12256413139dSWillem de Bruijn u32 p_off, p_len, copied; 12266413139dSWillem de Bruijn struct page *p; 12276413139dSWillem de Bruijn u8 *vaddr; 12286413139dSWillem de Bruijn 1229b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 12306413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 12316413139dSWillem de Bruijn copied) { 12326413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 12336413139dSWillem de Bruijn vaddr = kmap_atomic(p); 12346413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 12356413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 12366413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 12376413139dSWillem de Bruijn kunmap_atomic(vaddr); 12386413139dSWillem de Bruijn len -= seg_len; 12396413139dSWillem de Bruijn if (!len) 12406413139dSWillem de Bruijn break; 12416413139dSWillem de Bruijn } 12426413139dSWillem de Bruijn } 12436413139dSWillem de Bruijn 12446413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 12456413139dSWillem de Bruijn printk("skb fraglist:\n"); 12466413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 12476413139dSWillem de Bruijn skb_dump(level, list_skb, true); 12486413139dSWillem de Bruijn } 12496413139dSWillem de Bruijn } 12506413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 12516413139dSWillem de Bruijn 1252d1a203eaSStephen Hemminger /** 125325121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 125425121173SMichael S. Tsirkin * @skb: buffer that triggered an error 125525121173SMichael S. Tsirkin * 125625121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 125725121173SMichael S. Tsirkin * skb must be freed afterwards. 125825121173SMichael S. Tsirkin */ 125925121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 126025121173SMichael S. Tsirkin { 1261753f1ca4SPavel Begunkov if (skb) { 1262753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 12631f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 126425121173SMichael S. Tsirkin } 1265753f1ca4SPavel Begunkov } 126625121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 126725121173SMichael S. Tsirkin 1268be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 126925121173SMichael S. Tsirkin /** 1270ead2ceb0SNeil Horman * consume_skb - free an skbuff 1271ead2ceb0SNeil Horman * @skb: buffer to free 1272ead2ceb0SNeil Horman * 1273ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1274ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1275ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1276ead2ceb0SNeil Horman */ 1277ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1278ead2ceb0SNeil Horman { 12793889a803SPaolo Abeni if (!skb_unref(skb)) 1280ead2ceb0SNeil Horman return; 12813889a803SPaolo Abeni 1282dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1283ead2ceb0SNeil Horman __kfree_skb(skb); 1284ead2ceb0SNeil Horman } 1285ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1286be769db2SHerbert Xu #endif 1287ead2ceb0SNeil Horman 12880a463c78SPaolo Abeni /** 1289c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 12900a463c78SPaolo Abeni * @skb: buffer to free 12910a463c78SPaolo Abeni * 1292ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1293ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 12940a463c78SPaolo Abeni */ 1295ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 12960a463c78SPaolo Abeni { 1297dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1298b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 12990a463c78SPaolo Abeni kfree_skbmem(skb); 13000a463c78SPaolo Abeni } 13010a463c78SPaolo Abeni 1302f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1303795bb1c0SJesper Dangaard Brouer { 1304795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1305f450d539SAlexander Lobakin u32 i; 1306795bb1c0SJesper Dangaard Brouer 1307025a785fSJakub Kicinski kasan_poison_object_data(skbuff_cache, skb); 1308795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1309795bb1c0SJesper Dangaard Brouer 1310795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1311f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1312025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, 1313f450d539SAlexander Lobakin nc->skb_cache[i]); 1314f450d539SAlexander Lobakin 1315025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF, 1316f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1317f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1318795bb1c0SJesper Dangaard Brouer } 1319795bb1c0SJesper Dangaard Brouer } 1320f450d539SAlexander Lobakin 13218fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason) 132215fad714SJesper Dangaard Brouer { 13238fa66e4aSJakub Kicinski skb_release_all(skb, reason, true); 13249243adfcSAlexander Lobakin napi_skb_cache_put(skb); 13259243adfcSAlexander Lobakin } 13269243adfcSAlexander Lobakin 13279243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 13289243adfcSAlexander Lobakin { 13299efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 13308550ff8dSPaul Blakey nf_reset_ct(skb); 13319243adfcSAlexander Lobakin skb_dst_drop(skb); 13329243adfcSAlexander Lobakin skb_ext_put(skb); 13335e10da53SPaolo Abeni skb_orphan(skb); 13349efb4b5bSPaolo Abeni skb->slow_gro = 0; 13359efb4b5bSPaolo Abeni } 1336f450d539SAlexander Lobakin napi_skb_cache_put(skb); 133715fad714SJesper Dangaard Brouer } 1338795bb1c0SJesper Dangaard Brouer 1339795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1340795bb1c0SJesper Dangaard Brouer { 1341885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1342795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1343885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1344795bb1c0SJesper Dangaard Brouer return; 1345795bb1c0SJesper Dangaard Brouer } 1346795bb1c0SJesper Dangaard Brouer 1347ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 13486454eca8SYunsheng Lin 13497608894eSPaolo Abeni if (!skb_unref(skb)) 1350795bb1c0SJesper Dangaard Brouer return; 13517608894eSPaolo Abeni 1352795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1353dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1354795bb1c0SJesper Dangaard Brouer 1355795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1356abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1357795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1358795bb1c0SJesper Dangaard Brouer return; 1359795bb1c0SJesper Dangaard Brouer } 1360795bb1c0SJesper Dangaard Brouer 1361b07a2d97SJakub Kicinski skb_release_all(skb, SKB_CONSUMED, !!budget); 1362f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1363795bb1c0SJesper Dangaard Brouer } 1364795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1365795bb1c0SJesper Dangaard Brouer 136603f61041SKees Cook /* Make sure a field is contained by headers group */ 1367b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 136803f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 136903f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1370b1937227SEric Dumazet 1371dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1372dec18810SHerbert Xu { 1373dec18810SHerbert Xu new->tstamp = old->tstamp; 1374b1937227SEric Dumazet /* We do not copy old->sk */ 1375dec18810SHerbert Xu new->dev = old->dev; 1376b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 13777fee226aSEric Dumazet skb_dst_copy(new, old); 1378df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1379b1937227SEric Dumazet __nf_copy(new, old, false); 13806aa895b0SPatrick McHardy 138103f61041SKees Cook /* Note : this field could be in the headers group. 1382b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1383b1937227SEric Dumazet */ 1384b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 138506021292SEliezer Tamir 138603f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1387b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1388b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1389b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1390b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1391b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1392b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1393b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1394b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1395b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1396b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1397b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1398b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1399b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1400b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1401b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1402b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1403b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1404b1937227SEric Dumazet #endif 1405e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1406b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 140706021292SEliezer Tamir #endif 140868822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 14092bd82484SEric Dumazet #ifdef CONFIG_XPS 14102bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 14112bd82484SEric Dumazet #endif 1412b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1413b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1414b1937227SEric Dumazet #endif 1415b1937227SEric Dumazet 1416dec18810SHerbert Xu } 1417dec18810SHerbert Xu 141882c49a35SHerbert Xu /* 141982c49a35SHerbert Xu * You should not add any new code to this function. Add it to 142082c49a35SHerbert Xu * __copy_skb_header above instead. 142182c49a35SHerbert Xu */ 1422e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 14231da177e4SLinus Torvalds { 14241da177e4SLinus Torvalds #define C(x) n->x = skb->x 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds n->next = n->prev = NULL; 14271da177e4SLinus Torvalds n->sk = NULL; 1428dec18810SHerbert Xu __copy_skb_header(n, skb); 1429dec18810SHerbert Xu 14301da177e4SLinus Torvalds C(len); 14311da177e4SLinus Torvalds C(data_len); 14323e6b3b2eSAlexey Dobriyan C(mac_len); 1433334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 143402f1c89dSPaul Moore n->cloned = 1; 14351da177e4SLinus Torvalds n->nohdr = 0; 1436b13dda9fSEric Dumazet n->peeked = 0; 1437e78bfb07SStefano Brivio C(pfmemalloc); 14386a5bcd84SIlias Apalodimas C(pp_recycle); 14391da177e4SLinus Torvalds n->destructor = NULL; 14401da177e4SLinus Torvalds C(tail); 14411da177e4SLinus Torvalds C(end); 144202f1c89dSPaul Moore C(head); 1443d3836f21SEric Dumazet C(head_frag); 144402f1c89dSPaul Moore C(data); 144502f1c89dSPaul Moore C(truesize); 144663354797SReshetova, Elena refcount_set(&n->users, 1); 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 14491da177e4SLinus Torvalds skb->cloned = 1; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds return n; 1452e0053ec0SHerbert Xu #undef C 1453e0053ec0SHerbert Xu } 1454e0053ec0SHerbert Xu 1455e0053ec0SHerbert Xu /** 1456da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1457da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1458da29e4b4SJakub Kicinski */ 1459da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1460da29e4b4SJakub Kicinski { 1461da29e4b4SJakub Kicinski struct sk_buff *n; 1462da29e4b4SJakub Kicinski 1463da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1464da29e4b4SJakub Kicinski if (!n) 1465da29e4b4SJakub Kicinski return NULL; 1466da29e4b4SJakub Kicinski 1467da29e4b4SJakub Kicinski n->len = first->len; 1468da29e4b4SJakub Kicinski n->data_len = first->len; 1469da29e4b4SJakub Kicinski n->truesize = first->truesize; 1470da29e4b4SJakub Kicinski 1471da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1472da29e4b4SJakub Kicinski 1473da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1474da29e4b4SJakub Kicinski n->destructor = NULL; 1475da29e4b4SJakub Kicinski 1476da29e4b4SJakub Kicinski return n; 1477da29e4b4SJakub Kicinski } 1478da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1479da29e4b4SJakub Kicinski 1480da29e4b4SJakub Kicinski /** 1481e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1482e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1483e0053ec0SHerbert Xu * @src: the skb to supply the contents 1484e0053ec0SHerbert Xu * 1485e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1486e0053ec0SHerbert Xu * supplied by the user. 1487e0053ec0SHerbert Xu * 1488e0053ec0SHerbert Xu * The target skb is returned upon exit. 1489e0053ec0SHerbert Xu */ 1490e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1491e0053ec0SHerbert Xu { 1492b07a2d97SJakub Kicinski skb_release_all(dst, SKB_CONSUMED, false); 1493e0053ec0SHerbert Xu return __skb_clone(dst, src); 1494e0053ec0SHerbert Xu } 1495e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1496e0053ec0SHerbert Xu 14976f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1498a91dbff5SWillem de Bruijn { 149914ade6baSWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg, rlim; 1500a91dbff5SWillem de Bruijn struct user_struct *user; 1501a91dbff5SWillem de Bruijn 1502a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1503a91dbff5SWillem de Bruijn return 0; 1504a91dbff5SWillem de Bruijn 150514ade6baSWillem de Bruijn rlim = rlimit(RLIMIT_MEMLOCK); 150614ade6baSWillem de Bruijn if (rlim == RLIM_INFINITY) 150714ade6baSWillem de Bruijn return 0; 150814ade6baSWillem de Bruijn 1509a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 151014ade6baSWillem de Bruijn max_pg = rlim >> PAGE_SHIFT; 1511a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1512a91dbff5SWillem de Bruijn 1513a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 151457fc05e8SEric Dumazet do { 1515a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1516a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1517a91dbff5SWillem de Bruijn return -ENOBUFS; 151857fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1519a91dbff5SWillem de Bruijn 1520a91dbff5SWillem de Bruijn if (!mmp->user) { 1521a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1522a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1523a91dbff5SWillem de Bruijn } else { 1524a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1525a91dbff5SWillem de Bruijn } 1526a91dbff5SWillem de Bruijn 1527a91dbff5SWillem de Bruijn return 0; 1528a91dbff5SWillem de Bruijn } 15296f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1530a91dbff5SWillem de Bruijn 15316f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1532a91dbff5SWillem de Bruijn { 1533a91dbff5SWillem de Bruijn if (mmp->user) { 1534a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1535a91dbff5SWillem de Bruijn free_uid(mmp->user); 1536a91dbff5SWillem de Bruijn } 1537a91dbff5SWillem de Bruijn } 15386f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1539a91dbff5SWillem de Bruijn 1540c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 154152267790SWillem de Bruijn { 1542e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 154352267790SWillem de Bruijn struct sk_buff *skb; 154452267790SWillem de Bruijn 154552267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 154652267790SWillem de Bruijn 154752267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 154852267790SWillem de Bruijn if (!skb) 154952267790SWillem de Bruijn return NULL; 155052267790SWillem de Bruijn 155152267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 155252267790SWillem de Bruijn uarg = (void *)skb->cb; 1553a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1554a91dbff5SWillem de Bruijn 1555a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1556a91dbff5SWillem de Bruijn kfree_skb(skb); 1557a91dbff5SWillem de Bruijn return NULL; 1558a91dbff5SWillem de Bruijn } 155952267790SWillem de Bruijn 1560e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 15614ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 15624ab6c99dSWillem de Bruijn uarg->len = 1; 15634ab6c99dSWillem de Bruijn uarg->bytelen = size; 156452267790SWillem de Bruijn uarg->zerocopy = 1; 1565e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1566e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 156752267790SWillem de Bruijn sock_hold(sk); 156852267790SWillem de Bruijn 1569e7d2b510SPavel Begunkov return &uarg->ubuf; 157052267790SWillem de Bruijn } 157152267790SWillem de Bruijn 1572e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 157352267790SWillem de Bruijn { 157452267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 157552267790SWillem de Bruijn } 157652267790SWillem de Bruijn 15778c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 15784ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 15794ab6c99dSWillem de Bruijn { 15804ab6c99dSWillem de Bruijn if (uarg) { 1581e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 15824ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 15834ab6c99dSWillem de Bruijn u32 bytelen, next; 15844ab6c99dSWillem de Bruijn 15851b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 15861b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 15871b4b2b09SPavel Begunkov return NULL; 15881b4b2b09SPavel Begunkov 15894ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 15904ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 15914ab6c99dSWillem de Bruijn */ 15924ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 15934ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 15944ab6c99dSWillem de Bruijn return NULL; 15954ab6c99dSWillem de Bruijn } 15964ab6c99dSWillem de Bruijn 1597e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1598e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1599e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 16004ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 16014ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 16024ab6c99dSWillem de Bruijn goto new_alloc; 16034ab6c99dSWillem de Bruijn return NULL; 16044ab6c99dSWillem de Bruijn } 16054ab6c99dSWillem de Bruijn 16064ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1607e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1608e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1609a91dbff5SWillem de Bruijn return NULL; 1610e7d2b510SPavel Begunkov uarg_zc->len++; 1611e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 16124ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1613100f6d8eSWillem de Bruijn 1614100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1615100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 16168e044917SJonathan Lemon net_zcopy_get(uarg); 1617100f6d8eSWillem de Bruijn 16184ab6c99dSWillem de Bruijn return uarg; 16194ab6c99dSWillem de Bruijn } 16204ab6c99dSWillem de Bruijn } 16214ab6c99dSWillem de Bruijn 16224ab6c99dSWillem de Bruijn new_alloc: 16238c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 16244ab6c99dSWillem de Bruijn } 16258c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 16264ab6c99dSWillem de Bruijn 16274ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 16284ab6c99dSWillem de Bruijn { 16294ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 16304ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 16314ab6c99dSWillem de Bruijn u64 sum_len; 16324ab6c99dSWillem de Bruijn 16334ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 16344ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 16354ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 16364ab6c99dSWillem de Bruijn 16374ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 16384ab6c99dSWillem de Bruijn return false; 16394ab6c99dSWillem de Bruijn 16404ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 16414ab6c99dSWillem de Bruijn return false; 16424ab6c99dSWillem de Bruijn 16434ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 16444ab6c99dSWillem de Bruijn return true; 16454ab6c99dSWillem de Bruijn } 16464ab6c99dSWillem de Bruijn 1647e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 164852267790SWillem de Bruijn { 16494ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 165052267790SWillem de Bruijn struct sock_exterr_skb *serr; 165152267790SWillem de Bruijn struct sock *sk = skb->sk; 16524ab6c99dSWillem de Bruijn struct sk_buff_head *q; 16534ab6c99dSWillem de Bruijn unsigned long flags; 16543bdd5ee0SWillem de Bruijn bool is_zerocopy; 16554ab6c99dSWillem de Bruijn u32 lo, hi; 16564ab6c99dSWillem de Bruijn u16 len; 165752267790SWillem de Bruijn 1658ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1659ccaffff1SWillem de Bruijn 16604ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 16614ab6c99dSWillem de Bruijn * so do not queue a completion notification 16624ab6c99dSWillem de Bruijn */ 16634ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 166452267790SWillem de Bruijn goto release; 166552267790SWillem de Bruijn 16664ab6c99dSWillem de Bruijn len = uarg->len; 16674ab6c99dSWillem de Bruijn lo = uarg->id; 16684ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 16693bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 16704ab6c99dSWillem de Bruijn 167152267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 167252267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 167352267790SWillem de Bruijn serr->ee.ee_errno = 0; 167452267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 16754ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 16764ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 16773bdd5ee0SWillem de Bruijn if (!is_zerocopy) 167852267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 167952267790SWillem de Bruijn 16804ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 16814ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 16824ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 16834ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 16844ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 16854ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 168652267790SWillem de Bruijn skb = NULL; 16874ab6c99dSWillem de Bruijn } 16884ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 168952267790SWillem de Bruijn 1690e3ae2365SAlexander Aring sk_error_report(sk); 169152267790SWillem de Bruijn 169252267790SWillem de Bruijn release: 169352267790SWillem de Bruijn consume_skb(skb); 169452267790SWillem de Bruijn sock_put(sk); 169552267790SWillem de Bruijn } 169675518851SJonathan Lemon 16978c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 169836177832SJonathan Lemon bool success) 169975518851SJonathan Lemon { 1700e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1701e7d2b510SPavel Begunkov 1702e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 170375518851SJonathan Lemon 170475518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1705e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 170675518851SJonathan Lemon } 17078c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 170852267790SWillem de Bruijn 17098c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 171052267790SWillem de Bruijn { 1711e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 171252267790SWillem de Bruijn 171352267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1714e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 171552267790SWillem de Bruijn 171652900d22SWillem de Bruijn if (have_uref) 17178c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 171852267790SWillem de Bruijn } 17198c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 172052267790SWillem de Bruijn 172152267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 172252267790SWillem de Bruijn struct msghdr *msg, int len, 172352267790SWillem de Bruijn struct ubuf_info *uarg) 172452267790SWillem de Bruijn { 17254ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 172652267790SWillem de Bruijn int err, orig_len = skb->len; 172752267790SWillem de Bruijn 17284ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 17294ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 17304ab6c99dSWillem de Bruijn */ 17314ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 17324ab6c99dSWillem de Bruijn return -EEXIST; 17334ab6c99dSWillem de Bruijn 1734ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 173552267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 173654d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 173754d43117SWillem de Bruijn 173852267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1739829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 174054d43117SWillem de Bruijn skb->sk = sk; 174152267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 174254d43117SWillem de Bruijn skb->sk = save_sk; 174352267790SWillem de Bruijn return err; 174452267790SWillem de Bruijn } 174552267790SWillem de Bruijn 174652900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 174752267790SWillem de Bruijn return skb->len - orig_len; 174852267790SWillem de Bruijn } 174952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 175052267790SWillem de Bruijn 1751753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1752753f1ca4SPavel Begunkov { 1753753f1ca4SPavel Begunkov int i; 1754753f1ca4SPavel Begunkov 1755753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1756753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1757753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1758753f1ca4SPavel Begunkov } 1759753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1760753f1ca4SPavel Begunkov 17611f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 176252267790SWillem de Bruijn gfp_t gfp_mask) 176352267790SWillem de Bruijn { 176452267790SWillem de Bruijn if (skb_zcopy(orig)) { 176552267790SWillem de Bruijn if (skb_zcopy(nskb)) { 176652267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 176752267790SWillem de Bruijn if (!gfp_mask) { 176852267790SWillem de Bruijn WARN_ON_ONCE(1); 176952267790SWillem de Bruijn return -ENOMEM; 177052267790SWillem de Bruijn } 177152267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 177252267790SWillem de Bruijn return 0; 177352267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 177452267790SWillem de Bruijn return -EIO; 177552267790SWillem de Bruijn } 177652900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 177752267790SWillem de Bruijn } 177852267790SWillem de Bruijn return 0; 177952267790SWillem de Bruijn } 178052267790SWillem de Bruijn 17812c53040fSBen Hutchings /** 17822c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 178348c83012SMichael S. Tsirkin * @skb: the skb to modify 178448c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 178548c83012SMichael S. Tsirkin * 178606b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 178748c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 178848c83012SMichael S. Tsirkin * to userspace pages. 178948c83012SMichael S. Tsirkin * 179048c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 179148c83012SMichael S. Tsirkin * %GFP_ATOMIC. 179248c83012SMichael S. Tsirkin * 179348c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 179448c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 179548c83012SMichael S. Tsirkin */ 179648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1797a6686f2fSShirley Ma { 1798a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1799a6686f2fSShirley Ma struct page *page, *head = NULL; 18007e692df3SEric Dumazet int i, order, psize, new_frags; 18013ece7826SWillem de Bruijn u32 d_off; 1802a6686f2fSShirley Ma 18033ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 18043ece7826SWillem de Bruijn return -EINVAL; 18053ece7826SWillem de Bruijn 1806f72c4ac6SWillem de Bruijn if (!num_frags) 1807f72c4ac6SWillem de Bruijn goto release; 1808f72c4ac6SWillem de Bruijn 18097e692df3SEric Dumazet /* We might have to allocate high order pages, so compute what minimum 18107e692df3SEric Dumazet * page order is needed. 18117e692df3SEric Dumazet */ 18127e692df3SEric Dumazet order = 0; 18137e692df3SEric Dumazet while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb)) 18147e692df3SEric Dumazet order++; 18157e692df3SEric Dumazet psize = (PAGE_SIZE << order); 18167e692df3SEric Dumazet 18177e692df3SEric Dumazet new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order); 18183ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 18197e692df3SEric Dumazet page = alloc_pages(gfp_mask | __GFP_COMP, order); 1820a6686f2fSShirley Ma if (!page) { 1821a6686f2fSShirley Ma while (head) { 182240dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1823a6686f2fSShirley Ma put_page(head); 1824a6686f2fSShirley Ma head = next; 1825a6686f2fSShirley Ma } 1826a6686f2fSShirley Ma return -ENOMEM; 1827a6686f2fSShirley Ma } 18283ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 18293ece7826SWillem de Bruijn head = page; 18303ece7826SWillem de Bruijn } 18313ece7826SWillem de Bruijn 18323ece7826SWillem de Bruijn page = head; 18333ece7826SWillem de Bruijn d_off = 0; 18343ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 18353ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 18363ece7826SWillem de Bruijn u32 p_off, p_len, copied; 18373ece7826SWillem de Bruijn struct page *p; 18383ece7826SWillem de Bruijn u8 *vaddr; 1839c613c209SWillem de Bruijn 1840b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1841c613c209SWillem de Bruijn p, p_off, p_len, copied) { 18423ece7826SWillem de Bruijn u32 copy, done = 0; 1843c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 18443ece7826SWillem de Bruijn 18453ece7826SWillem de Bruijn while (done < p_len) { 18467e692df3SEric Dumazet if (d_off == psize) { 18473ece7826SWillem de Bruijn d_off = 0; 18483ece7826SWillem de Bruijn page = (struct page *)page_private(page); 18493ece7826SWillem de Bruijn } 18507e692df3SEric Dumazet copy = min_t(u32, psize - d_off, p_len - done); 18513ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 18523ece7826SWillem de Bruijn vaddr + p_off + done, copy); 18533ece7826SWillem de Bruijn done += copy; 18543ece7826SWillem de Bruijn d_off += copy; 18553ece7826SWillem de Bruijn } 185651c56b00SEric Dumazet kunmap_atomic(vaddr); 1857c613c209SWillem de Bruijn } 1858a6686f2fSShirley Ma } 1859a6686f2fSShirley Ma 1860a6686f2fSShirley Ma /* skb frags release userspace buffers */ 186102756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1862a8605c60SIan Campbell skb_frag_unref(skb, i); 1863a6686f2fSShirley Ma 1864a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 18653ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 18667e692df3SEric Dumazet __skb_fill_page_desc(skb, i, head, 0, psize); 186740dadff2SSunghan Suh head = (struct page *)page_private(head); 1868a6686f2fSShirley Ma } 18693ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 18703ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 187148c83012SMichael S. Tsirkin 1872b90ddd56SWillem de Bruijn release: 18731f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1874a6686f2fSShirley Ma return 0; 1875a6686f2fSShirley Ma } 1876dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1877a6686f2fSShirley Ma 1878e0053ec0SHerbert Xu /** 1879e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1880e0053ec0SHerbert Xu * @skb: buffer to clone 1881e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1882e0053ec0SHerbert Xu * 1883e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1884e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1885e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1886e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1887e0053ec0SHerbert Xu * 1888e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1889e0053ec0SHerbert Xu * %GFP_ATOMIC. 1890e0053ec0SHerbert Xu */ 1891e0053ec0SHerbert Xu 1892e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1893e0053ec0SHerbert Xu { 1894d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1895d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1896d0bf4a9eSEric Dumazet skb1); 18976ffe75ebSEric Dumazet struct sk_buff *n; 1898e0053ec0SHerbert Xu 189970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1900a6686f2fSShirley Ma return NULL; 1901a6686f2fSShirley Ma 1902e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 19032638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 19046ffe75ebSEric Dumazet n = &fclones->skb2; 19052638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1906c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1907e0053ec0SHerbert Xu } else { 1908c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1909c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1910c93bdd0eSMel Gorman 1911025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask); 1912e0053ec0SHerbert Xu if (!n) 1913e0053ec0SHerbert Xu return NULL; 1914fe55f6d5SVegard Nossum 1915e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1916e0053ec0SHerbert Xu } 1917e0053ec0SHerbert Xu 1918e0053ec0SHerbert Xu return __skb_clone(n, skb); 19191da177e4SLinus Torvalds } 1920b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 19211da177e4SLinus Torvalds 1922b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1923f5b17294SPravin B Shelar { 1924030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1925030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1926030737bcSEric Dumazet skb->csum_start += off; 1927f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1928f5b17294SPravin B Shelar skb->transport_header += off; 1929f5b17294SPravin B Shelar skb->network_header += off; 1930f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1931f5b17294SPravin B Shelar skb->mac_header += off; 1932f5b17294SPravin B Shelar skb->inner_transport_header += off; 1933f5b17294SPravin B Shelar skb->inner_network_header += off; 1934aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1935f5b17294SPravin B Shelar } 1936b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1937f5b17294SPravin B Shelar 193808303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 19391da177e4SLinus Torvalds { 1940dec18810SHerbert Xu __copy_skb_header(new, old); 1941dec18810SHerbert Xu 19427967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 19437967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 19447967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 19451da177e4SLinus Torvalds } 194608303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 19471da177e4SLinus Torvalds 1948c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1949c93bdd0eSMel Gorman { 1950c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1951c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1952c93bdd0eSMel Gorman return 0; 1953c93bdd0eSMel Gorman } 1954c93bdd0eSMel Gorman 19551da177e4SLinus Torvalds /** 19561da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 19571da177e4SLinus Torvalds * @skb: buffer to copy 19581da177e4SLinus Torvalds * @gfp_mask: allocation priority 19591da177e4SLinus Torvalds * 19601da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 19611da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 19621da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 19631da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 19641da177e4SLinus Torvalds * 19651da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 19661da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 19671da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 19681da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 19691da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 19701da177e4SLinus Torvalds */ 19711da177e4SLinus Torvalds 1972dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 19731da177e4SLinus Torvalds { 19746602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1975ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1976c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1977c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 19786602cebbSEric Dumazet 19791da177e4SLinus Torvalds if (!n) 19801da177e4SLinus Torvalds return NULL; 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds /* Set the data pointer */ 19831da177e4SLinus Torvalds skb_reserve(n, headerlen); 19841da177e4SLinus Torvalds /* Set the tail pointer and length */ 19851da177e4SLinus Torvalds skb_put(n, skb->len); 19861da177e4SLinus Torvalds 19879f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 19881da177e4SLinus Torvalds 198908303c18SIlya Lesokhin skb_copy_header(n, skb); 19901da177e4SLinus Torvalds return n; 19911da177e4SLinus Torvalds } 1992b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds /** 1995bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 19961da177e4SLinus Torvalds * @skb: buffer to copy 1997117632e6SEric Dumazet * @headroom: headroom of new skb 19981da177e4SLinus Torvalds * @gfp_mask: allocation priority 1999bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 2000bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 2001bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 20021da177e4SLinus Torvalds * 20031da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 20041da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 20051da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 20061da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 20071da177e4SLinus Torvalds * or the pointer to the buffer on success. 20081da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 20091da177e4SLinus Torvalds */ 20101da177e4SLinus Torvalds 2011bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 2012bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 20131da177e4SLinus Torvalds { 2014117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 2015bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 2016bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 20176602cebbSEric Dumazet 20181da177e4SLinus Torvalds if (!n) 20191da177e4SLinus Torvalds goto out; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* Set the data pointer */ 2022117632e6SEric Dumazet skb_reserve(n, headroom); 20231da177e4SLinus Torvalds /* Set the tail pointer and length */ 20241da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 20251da177e4SLinus Torvalds /* Copy the bytes */ 2026d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 20271da177e4SLinus Torvalds 202825f484a6SHerbert Xu n->truesize += skb->data_len; 20291da177e4SLinus Torvalds n->data_len = skb->data_len; 20301da177e4SLinus Torvalds n->len = skb->len; 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 20331da177e4SLinus Torvalds int i; 20341da177e4SLinus Torvalds 20351f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 20361f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 20371511022cSDan Carpenter kfree_skb(n); 20381511022cSDan Carpenter n = NULL; 2039a6686f2fSShirley Ma goto out; 2040a6686f2fSShirley Ma } 20411da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20421da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 2043ea2ab693SIan Campbell skb_frag_ref(skb, i); 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 204821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 20491da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 20501da177e4SLinus Torvalds skb_clone_fraglist(n); 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds 205308303c18SIlya Lesokhin skb_copy_header(n, skb); 20541da177e4SLinus Torvalds out: 20551da177e4SLinus Torvalds return n; 20561da177e4SLinus Torvalds } 2057bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /** 20601da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 20611da177e4SLinus Torvalds * @skb: buffer to reallocate 20621da177e4SLinus Torvalds * @nhead: room to add at head 20631da177e4SLinus Torvalds * @ntail: room to add at tail 20641da177e4SLinus Torvalds * @gfp_mask: allocation priority 20651da177e4SLinus Torvalds * 2066bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 2067bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 20681da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 20691da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 20701da177e4SLinus Torvalds * 20711da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20721da177e4SLinus Torvalds * reloaded after call to this function. 20731da177e4SLinus Torvalds */ 20741da177e4SLinus Torvalds 207586a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 2076dd0fc66fSAl Viro gfp_t gfp_mask) 20771da177e4SLinus Torvalds { 207812d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 207912d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 20801da177e4SLinus Torvalds long off; 2081158f323bSEric Dumazet u8 *data; 208212d6c1d3SKees Cook int i; 20831da177e4SLinus Torvalds 20844edd87adSHerbert Xu BUG_ON(nhead < 0); 20854edd87adSHerbert Xu 20869f77fad3STim Hansen BUG_ON(skb_shared(skb)); 20871da177e4SLinus Torvalds 2088753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 2089753f1ca4SPavel Begunkov 2090c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 2091c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 209212d6c1d3SKees Cook 20935c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 20941da177e4SLinus Torvalds if (!data) 20951da177e4SLinus Torvalds goto nodata; 209612d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 20996602cebbSEric Dumazet * optimized for the cases when header is void. 21006602cebbSEric Dumazet */ 21016602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 21026602cebbSEric Dumazet 21036602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 21046602cebbSEric Dumazet skb_shinfo(skb), 2105fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 21061da177e4SLinus Torvalds 21073e24591aSAlexander Duyck /* 21083e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 21093e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 21103e24591aSAlexander Duyck * be since all we did is relocate the values 21113e24591aSAlexander Duyck */ 21123e24591aSAlexander Duyck if (skb_cloned(skb)) { 211370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 2114a6686f2fSShirley Ma goto nofrags; 21151f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 2116c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 21171da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2118ea2ab693SIan Campbell skb_frag_ref(skb, i); 21191da177e4SLinus Torvalds 212021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 21211da177e4SLinus Torvalds skb_clone_fraglist(skb); 21221da177e4SLinus Torvalds 2123b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 21243e24591aSAlexander Duyck } else { 2125b07a2d97SJakub Kicinski skb_free_head(skb, false); 21261fd63041SEric Dumazet } 21271da177e4SLinus Torvalds off = (data + nhead) - skb->head; 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds skb->head = data; 2130d3836f21SEric Dumazet skb->head_frag = 0; 21311da177e4SLinus Torvalds skb->data += off; 2132763087daSEric Dumazet 2133763087daSEric Dumazet skb_set_end_offset(skb, size); 21344305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 213556eb8882SPatrick McHardy off = nhead; 213656eb8882SPatrick McHardy #endif 213727a884dcSArnaldo Carvalho de Melo skb->tail += off; 2138b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 21391da177e4SLinus Torvalds skb->cloned = 0; 2140334a8132SPatrick McHardy skb->hdr_len = 0; 21411da177e4SLinus Torvalds skb->nohdr = 0; 21421da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 2143158f323bSEric Dumazet 2144de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 2145de8f3a83SDaniel Borkmann 2146158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 2147158f323bSEric Dumazet * For the moment, we really care of rx path, or 2148158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 2149158f323bSEric Dumazet */ 2150158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2151158f323bSEric Dumazet skb->truesize += size - osize; 2152158f323bSEric Dumazet 21531da177e4SLinus Torvalds return 0; 21541da177e4SLinus Torvalds 2155a6686f2fSShirley Ma nofrags: 2156bf9f1baaSEric Dumazet skb_kfree_head(data, size); 21571da177e4SLinus Torvalds nodata: 21581da177e4SLinus Torvalds return -ENOMEM; 21591da177e4SLinus Torvalds } 2160b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds struct sk_buff *skb2; 21671da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds if (delta <= 0) 21701da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 21711da177e4SLinus Torvalds else { 21721da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 21731da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 21741da177e4SLinus Torvalds GFP_ATOMIC)) { 21751da177e4SLinus Torvalds kfree_skb(skb2); 21761da177e4SLinus Torvalds skb2 = NULL; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds return skb2; 21801da177e4SLinus Torvalds } 2181b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 21821da177e4SLinus Torvalds 2183880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */ 21842b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 21852b88cba5SEric Dumazet { 21862b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 21872b88cba5SEric Dumazet struct skb_shared_info *shinfo; 21882b88cba5SEric Dumazet int res; 21892b88cba5SEric Dumazet 21902b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 21912b88cba5SEric Dumazet saved_truesize = skb->truesize; 21922b88cba5SEric Dumazet 21932b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 21942b88cba5SEric Dumazet if (res) 21952b88cba5SEric Dumazet return res; 21962b88cba5SEric Dumazet 21972b88cba5SEric Dumazet skb->truesize = saved_truesize; 21982b88cba5SEric Dumazet 21992b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 22002b88cba5SEric Dumazet return 0; 22012b88cba5SEric Dumazet 2202880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value 2203880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). 2204880ce5f2SEric Dumazet */ 2205880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || 2206880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { 2207880ce5f2SEric Dumazet /* We think this path should not be taken. 2208880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case. 2209880ce5f2SEric Dumazet */ 2210880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", 2211880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb)); 2212880ce5f2SEric Dumazet WARN_ON_ONCE(1); 2213880ce5f2SEric Dumazet return 0; 2214880ce5f2SEric Dumazet } 2215880ce5f2SEric Dumazet 22162b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 22172b88cba5SEric Dumazet 22182b88cba5SEric Dumazet /* We are about to change back skb->end, 22192b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 22202b88cba5SEric Dumazet */ 22212b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 22222b88cba5SEric Dumazet shinfo, 22232b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 22242b88cba5SEric Dumazet 22252b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 22262b88cba5SEric Dumazet 22272b88cba5SEric Dumazet return 0; 22282b88cba5SEric Dumazet } 22292b88cba5SEric Dumazet 22301da177e4SLinus Torvalds /** 2231f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2232f1260ff1SVasily Averin * @skb: buffer to reallocate 2233f1260ff1SVasily Averin * @headroom: needed headroom 2234f1260ff1SVasily Averin * 2235f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2236f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2237f1260ff1SVasily Averin * and frees original skb in case of failures. 2238f1260ff1SVasily Averin * 2239f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2240f1260ff1SVasily Averin */ 2241f1260ff1SVasily Averin 2242f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2243f1260ff1SVasily Averin { 2244f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 22457f678defSVasily Averin int osize = skb_end_offset(skb); 22467f678defSVasily Averin struct sock *sk = skb->sk; 2247f1260ff1SVasily Averin 2248f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2249f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2250f1260ff1SVasily Averin return skb; 2251f1260ff1SVasily Averin 22527f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 22537f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 22547f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2255f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2256f1260ff1SVasily Averin 22577f678defSVasily Averin if (unlikely(!nskb)) 22587f678defSVasily Averin goto fail; 22597f678defSVasily Averin 22607f678defSVasily Averin if (sk) 22617f678defSVasily Averin skb_set_owner_w(nskb, sk); 2262f1260ff1SVasily Averin consume_skb(skb); 2263f1260ff1SVasily Averin skb = nskb; 2264f1260ff1SVasily Averin } 22657f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 22667f678defSVasily Averin goto fail; 22677f678defSVasily Averin 22687f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 22697f678defSVasily Averin delta = skb_end_offset(skb) - osize; 22707f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 22717f678defSVasily Averin skb->truesize += delta; 2272f1260ff1SVasily Averin } 2273f1260ff1SVasily Averin return skb; 22747f678defSVasily Averin 22757f678defSVasily Averin fail: 22767f678defSVasily Averin kfree_skb(skb); 22777f678defSVasily Averin return NULL; 2278f1260ff1SVasily Averin } 2279f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2280f1260ff1SVasily Averin 2281f1260ff1SVasily Averin /** 22821da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 22831da177e4SLinus Torvalds * @skb: buffer to copy 22841da177e4SLinus Torvalds * @newheadroom: new free bytes at head 22851da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 22861da177e4SLinus Torvalds * @gfp_mask: allocation priority 22871da177e4SLinus Torvalds * 22881da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 22891da177e4SLinus Torvalds * allocate additional space. 22901da177e4SLinus Torvalds * 22911da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 22921da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 22931da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 22941da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 22951da177e4SLinus Torvalds * 22961da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 22971da177e4SLinus Torvalds * is called from an interrupt. 22981da177e4SLinus Torvalds */ 22991da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 230086a76cafSVictor Fusco int newheadroom, int newtailroom, 2301dd0fc66fSAl Viro gfp_t gfp_mask) 23021da177e4SLinus Torvalds { 23031da177e4SLinus Torvalds /* 23041da177e4SLinus Torvalds * Allocate the copy buffer 23051da177e4SLinus Torvalds */ 2306c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2307c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2308c93bdd0eSMel Gorman NUMA_NO_NODE); 2309efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 23101da177e4SLinus Torvalds int head_copy_len, head_copy_off; 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds if (!n) 23131da177e4SLinus Torvalds return NULL; 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds skb_reserve(n, newheadroom); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* Set the tail pointer and length */ 23181da177e4SLinus Torvalds skb_put(n, skb->len); 23191da177e4SLinus Torvalds 2320efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 23211da177e4SLinus Torvalds head_copy_off = 0; 23221da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 23231da177e4SLinus Torvalds head_copy_len = newheadroom; 23241da177e4SLinus Torvalds else 23251da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds /* Copy the linear header and data. */ 23289f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 23299f77fad3STim Hansen skb->len + head_copy_len)); 23301da177e4SLinus Torvalds 233108303c18SIlya Lesokhin skb_copy_header(n, skb); 23321da177e4SLinus Torvalds 2333030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2334efd1e8d5SPatrick McHardy 23351da177e4SLinus Torvalds return n; 23361da177e4SLinus Torvalds } 2337b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /** 2340cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 23411da177e4SLinus Torvalds * @skb: buffer to pad 23421da177e4SLinus Torvalds * @pad: space to pad 2343cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 23441da177e4SLinus Torvalds * 23451da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 23461da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 23471da177e4SLinus Torvalds * beyond the buffer end onto the wire. 23481da177e4SLinus Torvalds * 2349cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2350cd0a137aSFlorian Fainelli * if @free_on_error is true. 23511da177e4SLinus Torvalds */ 23521da177e4SLinus Torvalds 2353cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 23541da177e4SLinus Torvalds { 23555b057c6bSHerbert Xu int err; 23565b057c6bSHerbert Xu int ntail; 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 23595b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 23601da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 23615b057c6bSHerbert Xu return 0; 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23644305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 23655b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 23665b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 23675b057c6bSHerbert Xu if (unlikely(err)) 23685b057c6bSHerbert Xu goto free_skb; 23695b057c6bSHerbert Xu } 23705b057c6bSHerbert Xu 23715b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 23725b057c6bSHerbert Xu * to be audited. 23735b057c6bSHerbert Xu */ 23745b057c6bSHerbert Xu err = skb_linearize(skb); 23755b057c6bSHerbert Xu if (unlikely(err)) 23765b057c6bSHerbert Xu goto free_skb; 23775b057c6bSHerbert Xu 23785b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 23795b057c6bSHerbert Xu return 0; 23805b057c6bSHerbert Xu 23815b057c6bSHerbert Xu free_skb: 2382cd0a137aSFlorian Fainelli if (free_on_error) 23831da177e4SLinus Torvalds kfree_skb(skb); 23845b057c6bSHerbert Xu return err; 23851da177e4SLinus Torvalds } 2386cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 23871da177e4SLinus Torvalds 23880dde3e16SIlpo Järvinen /** 23890c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 23900c7ddf36SMathias Krause * @skb: start of the buffer to use 23910c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 23920c7ddf36SMathias Krause * @len: amount of data to add 23930c7ddf36SMathias Krause * 23940c7ddf36SMathias Krause * This function extends the used data area of the potentially 23950c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 23960c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 23970c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 23980c7ddf36SMathias Krause * returned. 23990c7ddf36SMathias Krause */ 24000c7ddf36SMathias Krause 24014df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 24020c7ddf36SMathias Krause { 24030c7ddf36SMathias Krause if (tail != skb) { 24040c7ddf36SMathias Krause skb->data_len += len; 24050c7ddf36SMathias Krause skb->len += len; 24060c7ddf36SMathias Krause } 24070c7ddf36SMathias Krause return skb_put(tail, len); 24080c7ddf36SMathias Krause } 24090c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 24100c7ddf36SMathias Krause 24110c7ddf36SMathias Krause /** 24120dde3e16SIlpo Järvinen * skb_put - add data to a buffer 24130dde3e16SIlpo Järvinen * @skb: buffer to use 24140dde3e16SIlpo Järvinen * @len: amount of data to add 24150dde3e16SIlpo Järvinen * 24160dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 24170dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 24180dde3e16SIlpo Järvinen * first byte of the extra data is returned. 24190dde3e16SIlpo Järvinen */ 24204df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 24210dde3e16SIlpo Järvinen { 24224df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 24230dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 24240dde3e16SIlpo Järvinen skb->tail += len; 24250dde3e16SIlpo Järvinen skb->len += len; 24260dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 24270dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 24280dde3e16SIlpo Järvinen return tmp; 24290dde3e16SIlpo Järvinen } 24300dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 24310dde3e16SIlpo Järvinen 24326be8ac2fSIlpo Järvinen /** 2433c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2434c2aa270aSIlpo Järvinen * @skb: buffer to use 2435c2aa270aSIlpo Järvinen * @len: amount of data to add 2436c2aa270aSIlpo Järvinen * 2437c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2438c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2439c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2440c2aa270aSIlpo Järvinen */ 2441d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2442c2aa270aSIlpo Järvinen { 2443c2aa270aSIlpo Järvinen skb->data -= len; 2444c2aa270aSIlpo Järvinen skb->len += len; 2445c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2446c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2447c2aa270aSIlpo Järvinen return skb->data; 2448c2aa270aSIlpo Järvinen } 2449c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2450c2aa270aSIlpo Järvinen 2451c2aa270aSIlpo Järvinen /** 24526be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 24536be8ac2fSIlpo Järvinen * @skb: buffer to use 24546be8ac2fSIlpo Järvinen * @len: amount of data to remove 24556be8ac2fSIlpo Järvinen * 24566be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 24576be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 24586be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 24596be8ac2fSIlpo Järvinen * the old data. 24606be8ac2fSIlpo Järvinen */ 2461af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 24626be8ac2fSIlpo Järvinen { 246347d29646SDavid S. Miller return skb_pull_inline(skb, len); 24646be8ac2fSIlpo Järvinen } 24656be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 24666be8ac2fSIlpo Järvinen 2467419ae74eSIlpo Järvinen /** 246813244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 246913244cccSLuiz Augusto von Dentz * original position. 247013244cccSLuiz Augusto von Dentz * @skb: buffer to use 247113244cccSLuiz Augusto von Dentz * @len: amount of data to remove 247213244cccSLuiz Augusto von Dentz * 247313244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 247413244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 247513244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 247613244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 247713244cccSLuiz Augusto von Dentz */ 247813244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 247913244cccSLuiz Augusto von Dentz { 248013244cccSLuiz Augusto von Dentz void *data = skb->data; 248113244cccSLuiz Augusto von Dentz 248213244cccSLuiz Augusto von Dentz if (skb->len < len) 248313244cccSLuiz Augusto von Dentz return NULL; 248413244cccSLuiz Augusto von Dentz 248513244cccSLuiz Augusto von Dentz skb_pull(skb, len); 248613244cccSLuiz Augusto von Dentz 248713244cccSLuiz Augusto von Dentz return data; 248813244cccSLuiz Augusto von Dentz } 248913244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 249013244cccSLuiz Augusto von Dentz 249113244cccSLuiz Augusto von Dentz /** 2492419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2493419ae74eSIlpo Järvinen * @skb: buffer to alter 2494419ae74eSIlpo Järvinen * @len: new length 2495419ae74eSIlpo Järvinen * 2496419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2497419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2498419ae74eSIlpo Järvinen * The skb must be linear. 2499419ae74eSIlpo Järvinen */ 2500419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2501419ae74eSIlpo Järvinen { 2502419ae74eSIlpo Järvinen if (skb->len > len) 2503419ae74eSIlpo Järvinen __skb_trim(skb, len); 2504419ae74eSIlpo Järvinen } 2505419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2506419ae74eSIlpo Järvinen 25073cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 25081da177e4SLinus Torvalds */ 25091da177e4SLinus Torvalds 25103cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 25111da177e4SLinus Torvalds { 251227b437c8SHerbert Xu struct sk_buff **fragp; 251327b437c8SHerbert Xu struct sk_buff *frag; 25141da177e4SLinus Torvalds int offset = skb_headlen(skb); 25151da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 25161da177e4SLinus Torvalds int i; 251727b437c8SHerbert Xu int err; 251827b437c8SHerbert Xu 251927b437c8SHerbert Xu if (skb_cloned(skb) && 252027b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 252127b437c8SHerbert Xu return err; 25221da177e4SLinus Torvalds 2523f4d26fb3SHerbert Xu i = 0; 2524f4d26fb3SHerbert Xu if (offset >= len) 2525f4d26fb3SHerbert Xu goto drop_pages; 2526f4d26fb3SHerbert Xu 2527f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 25289e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 252927b437c8SHerbert Xu 253027b437c8SHerbert Xu if (end < len) { 25311da177e4SLinus Torvalds offset = end; 253227b437c8SHerbert Xu continue; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 25359e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 253627b437c8SHerbert Xu 2537f4d26fb3SHerbert Xu drop_pages: 253827b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 253927b437c8SHerbert Xu 254027b437c8SHerbert Xu for (; i < nfrags; i++) 2541ea2ab693SIan Campbell skb_frag_unref(skb, i); 254227b437c8SHerbert Xu 254321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 254427b437c8SHerbert Xu skb_drop_fraglist(skb); 2545f4d26fb3SHerbert Xu goto done; 254627b437c8SHerbert Xu } 254727b437c8SHerbert Xu 254827b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 254927b437c8SHerbert Xu fragp = &frag->next) { 255027b437c8SHerbert Xu int end = offset + frag->len; 255127b437c8SHerbert Xu 255227b437c8SHerbert Xu if (skb_shared(frag)) { 255327b437c8SHerbert Xu struct sk_buff *nfrag; 255427b437c8SHerbert Xu 255527b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 255627b437c8SHerbert Xu if (unlikely(!nfrag)) 255727b437c8SHerbert Xu return -ENOMEM; 255827b437c8SHerbert Xu 255927b437c8SHerbert Xu nfrag->next = frag->next; 256085bb2a60SEric Dumazet consume_skb(frag); 256127b437c8SHerbert Xu frag = nfrag; 256227b437c8SHerbert Xu *fragp = frag; 256327b437c8SHerbert Xu } 256427b437c8SHerbert Xu 256527b437c8SHerbert Xu if (end < len) { 256627b437c8SHerbert Xu offset = end; 256727b437c8SHerbert Xu continue; 256827b437c8SHerbert Xu } 256927b437c8SHerbert Xu 257027b437c8SHerbert Xu if (end > len && 257127b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 257227b437c8SHerbert Xu return err; 257327b437c8SHerbert Xu 257427b437c8SHerbert Xu if (frag->next) 257527b437c8SHerbert Xu skb_drop_list(&frag->next); 257627b437c8SHerbert Xu break; 257727b437c8SHerbert Xu } 257827b437c8SHerbert Xu 2579f4d26fb3SHerbert Xu done: 258027b437c8SHerbert Xu if (len > skb_headlen(skb)) { 25811da177e4SLinus Torvalds skb->data_len -= skb->len - len; 25821da177e4SLinus Torvalds skb->len = len; 25831da177e4SLinus Torvalds } else { 25841da177e4SLinus Torvalds skb->len = len; 25851da177e4SLinus Torvalds skb->data_len = 0; 258627a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 2589c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2590c21b48ccSEric Dumazet skb_condense(skb); 25911da177e4SLinus Torvalds return 0; 25921da177e4SLinus Torvalds } 2593b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 25941da177e4SLinus Torvalds 259588078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 259688078d98SEric Dumazet */ 259788078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 259888078d98SEric Dumazet { 259988078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 260088078d98SEric Dumazet int delta = skb->len - len; 260188078d98SEric Dumazet 2602d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2603d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2604d55bef50SDimitris Michailidis len); 260554970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 260654970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 260754970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 260854970a2fSVasily Averin 260954970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 261054970a2fSVasily Averin return -EINVAL; 261188078d98SEric Dumazet } 261288078d98SEric Dumazet return __pskb_trim(skb, len); 261388078d98SEric Dumazet } 261488078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 261588078d98SEric Dumazet 26161da177e4SLinus Torvalds /** 26171da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 26181da177e4SLinus Torvalds * @skb: buffer to reallocate 26191da177e4SLinus Torvalds * @delta: number of bytes to advance tail 26201da177e4SLinus Torvalds * 26211da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 26221da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 26231da177e4SLinus Torvalds * data from fragmented part. 26241da177e4SLinus Torvalds * 26251da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 26261da177e4SLinus Torvalds * 26271da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 26281da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 26291da177e4SLinus Torvalds * 26301da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 26311da177e4SLinus Torvalds * reloaded after call to this function. 26321da177e4SLinus Torvalds */ 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 26351da177e4SLinus Torvalds * when it is necessary. 26361da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 26371da177e4SLinus Torvalds * 2. It may change skb pointers. 26381da177e4SLinus Torvalds * 26391da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 26401da177e4SLinus Torvalds */ 2641af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 26441da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 26451da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 26461da177e4SLinus Torvalds */ 26474305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 26501da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 26511da177e4SLinus Torvalds GFP_ATOMIC)) 26521da177e4SLinus Torvalds return NULL; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26559f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 26569f77fad3STim Hansen skb_tail_pointer(skb), delta)); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 26591da177e4SLinus Torvalds * size of pulled pages. Superb. 26601da177e4SLinus Torvalds */ 266121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 26621da177e4SLinus Torvalds goto pull_pages; 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 26651da177e4SLinus Torvalds eat = delta; 26661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26679e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26689e903e08SEric Dumazet 26699e903e08SEric Dumazet if (size >= eat) 26701da177e4SLinus Torvalds goto pull_pages; 26719e903e08SEric Dumazet eat -= size; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 267509001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 26761da177e4SLinus Torvalds * but taking into account that pulling is expected to 26771da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 26781da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 26791da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 26801da177e4SLinus Torvalds */ 26811da177e4SLinus Torvalds if (eat) { 26821da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 26831da177e4SLinus Torvalds struct sk_buff *clone = NULL; 26841da177e4SLinus Torvalds struct sk_buff *insp = NULL; 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds do { 26871da177e4SLinus Torvalds if (list->len <= eat) { 26881da177e4SLinus Torvalds /* Eaten as whole. */ 26891da177e4SLinus Torvalds eat -= list->len; 26901da177e4SLinus Torvalds list = list->next; 26911da177e4SLinus Torvalds insp = list; 26921da177e4SLinus Torvalds } else { 26931da177e4SLinus Torvalds /* Eaten partially. */ 26942d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 26952d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 26962d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds if (skb_shared(list)) { 26991da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 27001da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 27011da177e4SLinus Torvalds if (!clone) 27021da177e4SLinus Torvalds return NULL; 27031da177e4SLinus Torvalds insp = list->next; 27041da177e4SLinus Torvalds list = clone; 27051da177e4SLinus Torvalds } else { 27061da177e4SLinus Torvalds /* This may be pulled without 27071da177e4SLinus Torvalds * problems. */ 27081da177e4SLinus Torvalds insp = list; 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 27111da177e4SLinus Torvalds kfree_skb(clone); 27121da177e4SLinus Torvalds return NULL; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds break; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds } while (eat); 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds /* Free pulled out fragments. */ 27191da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 27201da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2721ef527f96SEric Dumazet consume_skb(list); 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds /* And insert new clone at head. */ 27241da177e4SLinus Torvalds if (clone) { 27251da177e4SLinus Torvalds clone->next = list; 27261da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds pull_pages: 27321da177e4SLinus Torvalds eat = delta; 27331da177e4SLinus Torvalds k = 0; 27341da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27359e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 27369e903e08SEric Dumazet 27379e903e08SEric Dumazet if (size <= eat) { 2738ea2ab693SIan Campbell skb_frag_unref(skb, i); 27399e903e08SEric Dumazet eat -= size; 27401da177e4SLinus Torvalds } else { 2741b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2742b54c9d5bSJonathan Lemon 2743b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 27441da177e4SLinus Torvalds if (eat) { 2745b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2746b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 27473ccc6c6fSlinzhang if (!i) 27483ccc6c6fSlinzhang goto end; 27491da177e4SLinus Torvalds eat = 0; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds k++; 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 27551da177e4SLinus Torvalds 27563ccc6c6fSlinzhang end: 27571da177e4SLinus Torvalds skb->tail += delta; 27581da177e4SLinus Torvalds skb->data_len -= delta; 27591da177e4SLinus Torvalds 27601f8b977aSWillem de Bruijn if (!skb->data_len) 27611f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 27621f8b977aSWillem de Bruijn 276327a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 27641da177e4SLinus Torvalds } 2765b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 27661da177e4SLinus Torvalds 276722019b17SEric Dumazet /** 276822019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 276922019b17SEric Dumazet * @skb: source skb 277022019b17SEric Dumazet * @offset: offset in source 277122019b17SEric Dumazet * @to: destination buffer 277222019b17SEric Dumazet * @len: number of bytes to copy 277322019b17SEric Dumazet * 277422019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 277522019b17SEric Dumazet * destination buffer. 277622019b17SEric Dumazet * 277722019b17SEric Dumazet * CAUTION ! : 277822019b17SEric Dumazet * If its prototype is ever changed, 277922019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 278022019b17SEric Dumazet * since it is called from BPF assembly code. 278122019b17SEric Dumazet */ 27821da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 27831da177e4SLinus Torvalds { 27841a028e50SDavid S. Miller int start = skb_headlen(skb); 2785fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2786fbb398a8SDavid S. Miller int i, copy; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds if (offset > (int)skb->len - len) 27891da177e4SLinus Torvalds goto fault; 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds /* Copy header. */ 27921a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 27931da177e4SLinus Torvalds if (copy > len) 27941da177e4SLinus Torvalds copy = len; 2795d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 27961da177e4SLinus Torvalds if ((len -= copy) == 0) 27971da177e4SLinus Torvalds return 0; 27981da177e4SLinus Torvalds offset += copy; 27991da177e4SLinus Torvalds to += copy; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 28031a028e50SDavid S. Miller int end; 280451c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 28051da177e4SLinus Torvalds 2806547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28071a028e50SDavid S. Miller 280851c56b00SEric Dumazet end = start + skb_frag_size(f); 28091da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2810c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2811c613c209SWillem de Bruijn struct page *p; 28121da177e4SLinus Torvalds u8 *vaddr; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds if (copy > len) 28151da177e4SLinus Torvalds copy = len; 28161da177e4SLinus Torvalds 2817c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2818b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2819c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2820c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2821c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 282251c56b00SEric Dumazet kunmap_atomic(vaddr); 2823c613c209SWillem de Bruijn } 28241da177e4SLinus Torvalds 28251da177e4SLinus Torvalds if ((len -= copy) == 0) 28261da177e4SLinus Torvalds return 0; 28271da177e4SLinus Torvalds offset += copy; 28281da177e4SLinus Torvalds to += copy; 28291da177e4SLinus Torvalds } 28301a028e50SDavid S. Miller start = end; 28311da177e4SLinus Torvalds } 28321da177e4SLinus Torvalds 2833fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 28341a028e50SDavid S. Miller int end; 28351da177e4SLinus Torvalds 2836547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28371a028e50SDavid S. Miller 2838fbb398a8SDavid S. Miller end = start + frag_iter->len; 28391da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 28401da177e4SLinus Torvalds if (copy > len) 28411da177e4SLinus Torvalds copy = len; 2842fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 28431da177e4SLinus Torvalds goto fault; 28441da177e4SLinus Torvalds if ((len -= copy) == 0) 28451da177e4SLinus Torvalds return 0; 28461da177e4SLinus Torvalds offset += copy; 28471da177e4SLinus Torvalds to += copy; 28481da177e4SLinus Torvalds } 28491a028e50SDavid S. Miller start = end; 28501da177e4SLinus Torvalds } 2851a6686f2fSShirley Ma 28521da177e4SLinus Torvalds if (!len) 28531da177e4SLinus Torvalds return 0; 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds fault: 28561da177e4SLinus Torvalds return -EFAULT; 28571da177e4SLinus Torvalds } 2858b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 28591da177e4SLinus Torvalds 28609c55e01cSJens Axboe /* 28619c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 28629c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 28639c55e01cSJens Axboe */ 28649c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 28659c55e01cSJens Axboe { 28668b9d3728SJarek Poplawski put_page(spd->pages[i]); 28678b9d3728SJarek Poplawski } 28689c55e01cSJens Axboe 2869a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 28704fb66994SJarek Poplawski unsigned int *offset, 287118aafc62SEric Dumazet struct sock *sk) 28728b9d3728SJarek Poplawski { 28735640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 28748b9d3728SJarek Poplawski 28755640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 28768b9d3728SJarek Poplawski return NULL; 28774fb66994SJarek Poplawski 28785640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 28794fb66994SJarek Poplawski 28805640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 28815640f768SEric Dumazet page_address(page) + *offset, *len); 28825640f768SEric Dumazet *offset = pfrag->offset; 28835640f768SEric Dumazet pfrag->offset += *len; 28844fb66994SJarek Poplawski 28855640f768SEric Dumazet return pfrag->page; 28869c55e01cSJens Axboe } 28879c55e01cSJens Axboe 288841c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 288941c73a0dSEric Dumazet struct page *page, 289041c73a0dSEric Dumazet unsigned int offset) 289141c73a0dSEric Dumazet { 289241c73a0dSEric Dumazet return spd->nr_pages && 289341c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 289441c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 289541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 289641c73a0dSEric Dumazet } 289741c73a0dSEric Dumazet 28989c55e01cSJens Axboe /* 28999c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 29009c55e01cSJens Axboe */ 2901a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 290235f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 29034fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 290418aafc62SEric Dumazet bool linear, 29057a67e56fSJarek Poplawski struct sock *sk) 29069c55e01cSJens Axboe { 290741c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2908a108d5f3SDavid S. Miller return true; 29099c55e01cSJens Axboe 29108b9d3728SJarek Poplawski if (linear) { 291118aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 29128b9d3728SJarek Poplawski if (!page) 2913a108d5f3SDavid S. Miller return true; 291441c73a0dSEric Dumazet } 291541c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 291641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2917a108d5f3SDavid S. Miller return false; 291841c73a0dSEric Dumazet } 29198b9d3728SJarek Poplawski get_page(page); 29209c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 29214fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 29229c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 29239c55e01cSJens Axboe spd->nr_pages++; 29248b9d3728SJarek Poplawski 2925a108d5f3SDavid S. Miller return false; 29269c55e01cSJens Axboe } 29279c55e01cSJens Axboe 2928a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 29292870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 293018aafc62SEric Dumazet unsigned int *len, 2931d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 293235f3d14dSJens Axboe struct sock *sk, 293335f3d14dSJens Axboe struct pipe_inode_info *pipe) 29349c55e01cSJens Axboe { 29352870c43dSOctavian Purdila if (!*len) 2936a108d5f3SDavid S. Miller return true; 29379c55e01cSJens Axboe 29382870c43dSOctavian Purdila /* skip this segment if already processed */ 29392870c43dSOctavian Purdila if (*off >= plen) { 29402870c43dSOctavian Purdila *off -= plen; 2941a108d5f3SDavid S. Miller return false; 29422870c43dSOctavian Purdila } 29432870c43dSOctavian Purdila 29442870c43dSOctavian Purdila /* ignore any bits we already processed */ 29459ca1b22dSEric Dumazet poff += *off; 29469ca1b22dSEric Dumazet plen -= *off; 29472870c43dSOctavian Purdila *off = 0; 29482870c43dSOctavian Purdila 294918aafc62SEric Dumazet do { 295018aafc62SEric Dumazet unsigned int flen = min(*len, plen); 29512870c43dSOctavian Purdila 295218aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 295318aafc62SEric Dumazet linear, sk)) 2954a108d5f3SDavid S. Miller return true; 295518aafc62SEric Dumazet poff += flen; 295618aafc62SEric Dumazet plen -= flen; 29572870c43dSOctavian Purdila *len -= flen; 295818aafc62SEric Dumazet } while (*len && plen); 29592870c43dSOctavian Purdila 2960a108d5f3SDavid S. Miller return false; 2961db43a282SOctavian Purdila } 29629c55e01cSJens Axboe 29639c55e01cSJens Axboe /* 2964a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 29652870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 29669c55e01cSJens Axboe */ 2967a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 296835f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 296935f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 29702870c43dSOctavian Purdila { 29712870c43dSOctavian Purdila int seg; 2972fa9835e5STom Herbert struct sk_buff *iter; 29739c55e01cSJens Axboe 29741d0c0b32SEric Dumazet /* map the linear part : 29752996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 29762996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 29772996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 29789c55e01cSJens Axboe */ 29792870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 29802870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 29812870c43dSOctavian Purdila skb_headlen(skb), 298218aafc62SEric Dumazet offset, len, spd, 29833a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 29841d0c0b32SEric Dumazet sk, pipe)) 2985a108d5f3SDavid S. Miller return true; 29869c55e01cSJens Axboe 29879c55e01cSJens Axboe /* 29889c55e01cSJens Axboe * then map the fragments 29899c55e01cSJens Axboe */ 29909c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 29919c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 29929c55e01cSJens Axboe 2993ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2994b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 299518aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2996a108d5f3SDavid S. Miller return true; 29979c55e01cSJens Axboe } 29989c55e01cSJens Axboe 2999fa9835e5STom Herbert skb_walk_frags(skb, iter) { 3000fa9835e5STom Herbert if (*offset >= iter->len) { 3001fa9835e5STom Herbert *offset -= iter->len; 3002fa9835e5STom Herbert continue; 3003fa9835e5STom Herbert } 3004fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 3005fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 3006fa9835e5STom Herbert * case. 3007fa9835e5STom Herbert */ 3008fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 3009fa9835e5STom Herbert return true; 3010fa9835e5STom Herbert } 3011fa9835e5STom Herbert 3012a108d5f3SDavid S. Miller return false; 30139c55e01cSJens Axboe } 30149c55e01cSJens Axboe 30159c55e01cSJens Axboe /* 30169c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 3017fa9835e5STom Herbert * the fragments, and the frag list. 30189c55e01cSJens Axboe */ 3019a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 30209c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 302125869262SAl Viro unsigned int flags) 30229c55e01cSJens Axboe { 302341c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 302441c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 30259c55e01cSJens Axboe struct splice_pipe_desc spd = { 30269c55e01cSJens Axboe .pages = pages, 30279c55e01cSJens Axboe .partial = partial, 3028047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 302928a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 30309c55e01cSJens Axboe .spd_release = sock_spd_release, 30319c55e01cSJens Axboe }; 303235f3d14dSJens Axboe int ret = 0; 303335f3d14dSJens Axboe 3034fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 30359c55e01cSJens Axboe 3036a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 303725869262SAl Viro ret = splice_to_pipe(pipe, &spd); 30389c55e01cSJens Axboe 303935f3d14dSJens Axboe return ret; 30409c55e01cSJens Axboe } 30412b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 30429c55e01cSJens Axboe 3043c729ed6fSDavid Howells static int sendmsg_locked(struct sock *sk, struct msghdr *msg) 3044c729ed6fSDavid Howells { 3045c729ed6fSDavid Howells struct socket *sock = sk->sk_socket; 3046c729ed6fSDavid Howells size_t size = msg_data_left(msg); 3047c729ed6fSDavid Howells 3048c729ed6fSDavid Howells if (!sock) 3049c729ed6fSDavid Howells return -EINVAL; 3050c729ed6fSDavid Howells 3051c729ed6fSDavid Howells if (!sock->ops->sendmsg_locked) 3052c729ed6fSDavid Howells return sock_no_sendmsg_locked(sk, msg, size); 3053c729ed6fSDavid Howells 3054c729ed6fSDavid Howells return sock->ops->sendmsg_locked(sk, msg, size); 3055c729ed6fSDavid Howells } 3056c729ed6fSDavid Howells 3057c729ed6fSDavid Howells static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg) 30580739cd28SCong Wang { 30590739cd28SCong Wang struct socket *sock = sk->sk_socket; 30600739cd28SCong Wang 30610739cd28SCong Wang if (!sock) 30620739cd28SCong Wang return -EINVAL; 3063c729ed6fSDavid Howells return sock_sendmsg(sock, msg); 30640739cd28SCong Wang } 30650739cd28SCong Wang 3066c729ed6fSDavid Howells typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg); 30670739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 3068c729ed6fSDavid Howells int len, sendmsg_func sendmsg) 306920bf50deSTom Herbert { 307020bf50deSTom Herbert unsigned int orig_len = len; 307120bf50deSTom Herbert struct sk_buff *head = skb; 307220bf50deSTom Herbert unsigned short fragidx; 307320bf50deSTom Herbert int slen, ret; 307420bf50deSTom Herbert 307520bf50deSTom Herbert do_frag_list: 307620bf50deSTom Herbert 307720bf50deSTom Herbert /* Deal with head data */ 307820bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 307920bf50deSTom Herbert struct kvec kv; 308020bf50deSTom Herbert struct msghdr msg; 308120bf50deSTom Herbert 308220bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 308320bf50deSTom Herbert kv.iov_base = skb->data + offset; 3084db5980d8SJohn Fastabend kv.iov_len = slen; 308520bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 3086bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 308720bf50deSTom Herbert 3088c729ed6fSDavid Howells iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen); 3089c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, 3090c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg); 309120bf50deSTom Herbert if (ret <= 0) 309220bf50deSTom Herbert goto error; 309320bf50deSTom Herbert 309420bf50deSTom Herbert offset += ret; 309520bf50deSTom Herbert len -= ret; 309620bf50deSTom Herbert } 309720bf50deSTom Herbert 309820bf50deSTom Herbert /* All the data was skb head? */ 309920bf50deSTom Herbert if (!len) 310020bf50deSTom Herbert goto out; 310120bf50deSTom Herbert 310220bf50deSTom Herbert /* Make offset relative to start of frags */ 310320bf50deSTom Herbert offset -= skb_headlen(skb); 310420bf50deSTom Herbert 310520bf50deSTom Herbert /* Find where we are in frag list */ 310620bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 310720bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 310820bf50deSTom Herbert 3109d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 311020bf50deSTom Herbert break; 311120bf50deSTom Herbert 3112d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 311320bf50deSTom Herbert } 311420bf50deSTom Herbert 311520bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 311620bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 311720bf50deSTom Herbert 3118d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 311920bf50deSTom Herbert 312020bf50deSTom Herbert while (slen) { 3121c729ed6fSDavid Howells struct bio_vec bvec; 3122c729ed6fSDavid Howells struct msghdr msg = { 3123c729ed6fSDavid Howells .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT, 3124c729ed6fSDavid Howells }; 3125c729ed6fSDavid Howells 3126c729ed6fSDavid Howells bvec_set_page(&bvec, skb_frag_page(frag), slen, 3127c729ed6fSDavid Howells skb_frag_off(frag) + offset); 3128c729ed6fSDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, 3129c729ed6fSDavid Howells slen); 3130c729ed6fSDavid Howells 3131c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, 3132c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg); 313320bf50deSTom Herbert if (ret <= 0) 313420bf50deSTom Herbert goto error; 313520bf50deSTom Herbert 313620bf50deSTom Herbert len -= ret; 313720bf50deSTom Herbert offset += ret; 313820bf50deSTom Herbert slen -= ret; 313920bf50deSTom Herbert } 314020bf50deSTom Herbert 314120bf50deSTom Herbert offset = 0; 314220bf50deSTom Herbert } 314320bf50deSTom Herbert 314420bf50deSTom Herbert if (len) { 314520bf50deSTom Herbert /* Process any frag lists */ 314620bf50deSTom Herbert 314720bf50deSTom Herbert if (skb == head) { 314820bf50deSTom Herbert if (skb_has_frag_list(skb)) { 314920bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 315020bf50deSTom Herbert goto do_frag_list; 315120bf50deSTom Herbert } 315220bf50deSTom Herbert } else if (skb->next) { 315320bf50deSTom Herbert skb = skb->next; 315420bf50deSTom Herbert goto do_frag_list; 315520bf50deSTom Herbert } 315620bf50deSTom Herbert } 315720bf50deSTom Herbert 315820bf50deSTom Herbert out: 315920bf50deSTom Herbert return orig_len - len; 316020bf50deSTom Herbert 316120bf50deSTom Herbert error: 316220bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 316320bf50deSTom Herbert } 31640739cd28SCong Wang 31650739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 31660739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 31670739cd28SCong Wang int len) 31680739cd28SCong Wang { 3169c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_locked); 31700739cd28SCong Wang } 317120bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 317220bf50deSTom Herbert 31730739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 31740739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 31750739cd28SCong Wang { 3176c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked); 31770739cd28SCong Wang } 31780739cd28SCong Wang 3179357b40a1SHerbert Xu /** 3180357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3181357b40a1SHerbert Xu * @skb: destination buffer 3182357b40a1SHerbert Xu * @offset: offset in destination 3183357b40a1SHerbert Xu * @from: source buffer 3184357b40a1SHerbert Xu * @len: number of bytes to copy 3185357b40a1SHerbert Xu * 3186357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3187357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3188357b40a1SHerbert Xu * traversing fragment lists and such. 3189357b40a1SHerbert Xu */ 3190357b40a1SHerbert Xu 31910c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3192357b40a1SHerbert Xu { 31931a028e50SDavid S. Miller int start = skb_headlen(skb); 3194fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3195fbb398a8SDavid S. Miller int i, copy; 3196357b40a1SHerbert Xu 3197357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3198357b40a1SHerbert Xu goto fault; 3199357b40a1SHerbert Xu 32001a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3201357b40a1SHerbert Xu if (copy > len) 3202357b40a1SHerbert Xu copy = len; 320327d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3204357b40a1SHerbert Xu if ((len -= copy) == 0) 3205357b40a1SHerbert Xu return 0; 3206357b40a1SHerbert Xu offset += copy; 3207357b40a1SHerbert Xu from += copy; 3208357b40a1SHerbert Xu } 3209357b40a1SHerbert Xu 3210357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3211357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32121a028e50SDavid S. Miller int end; 3213357b40a1SHerbert Xu 3214547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32151a028e50SDavid S. Miller 32169e903e08SEric Dumazet end = start + skb_frag_size(frag); 3217357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3218c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3219c613c209SWillem de Bruijn struct page *p; 3220357b40a1SHerbert Xu u8 *vaddr; 3221357b40a1SHerbert Xu 3222357b40a1SHerbert Xu if (copy > len) 3223357b40a1SHerbert Xu copy = len; 3224357b40a1SHerbert Xu 3225c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3226b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3227c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3228c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3229c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 323051c56b00SEric Dumazet kunmap_atomic(vaddr); 3231c613c209SWillem de Bruijn } 3232357b40a1SHerbert Xu 3233357b40a1SHerbert Xu if ((len -= copy) == 0) 3234357b40a1SHerbert Xu return 0; 3235357b40a1SHerbert Xu offset += copy; 3236357b40a1SHerbert Xu from += copy; 3237357b40a1SHerbert Xu } 32381a028e50SDavid S. Miller start = end; 3239357b40a1SHerbert Xu } 3240357b40a1SHerbert Xu 3241fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32421a028e50SDavid S. Miller int end; 3243357b40a1SHerbert Xu 3244547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32451a028e50SDavid S. Miller 3246fbb398a8SDavid S. Miller end = start + frag_iter->len; 3247357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3248357b40a1SHerbert Xu if (copy > len) 3249357b40a1SHerbert Xu copy = len; 3250fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 32511a028e50SDavid S. Miller from, copy)) 3252357b40a1SHerbert Xu goto fault; 3253357b40a1SHerbert Xu if ((len -= copy) == 0) 3254357b40a1SHerbert Xu return 0; 3255357b40a1SHerbert Xu offset += copy; 3256357b40a1SHerbert Xu from += copy; 3257357b40a1SHerbert Xu } 32581a028e50SDavid S. Miller start = end; 3259357b40a1SHerbert Xu } 3260357b40a1SHerbert Xu if (!len) 3261357b40a1SHerbert Xu return 0; 3262357b40a1SHerbert Xu 3263357b40a1SHerbert Xu fault: 3264357b40a1SHerbert Xu return -EFAULT; 3265357b40a1SHerbert Xu } 3266357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3267357b40a1SHerbert Xu 32681da177e4SLinus Torvalds /* Checksum skb data. */ 32692817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 32702817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 32711da177e4SLinus Torvalds { 32721a028e50SDavid S. Miller int start = skb_headlen(skb); 32731a028e50SDavid S. Miller int i, copy = start - offset; 3274fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32751da177e4SLinus Torvalds int pos = 0; 32761da177e4SLinus Torvalds 32771da177e4SLinus Torvalds /* Checksum header. */ 32781da177e4SLinus Torvalds if (copy > 0) { 32791da177e4SLinus Torvalds if (copy > len) 32801da177e4SLinus Torvalds copy = len; 32812544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 32822544af03SMatteo Croce skb->data + offset, copy, csum); 32831da177e4SLinus Torvalds if ((len -= copy) == 0) 32841da177e4SLinus Torvalds return csum; 32851da177e4SLinus Torvalds offset += copy; 32861da177e4SLinus Torvalds pos = copy; 32871da177e4SLinus Torvalds } 32881da177e4SLinus Torvalds 32891da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32901a028e50SDavid S. Miller int end; 329151c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32921da177e4SLinus Torvalds 3293547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32941a028e50SDavid S. Miller 329551c56b00SEric Dumazet end = start + skb_frag_size(frag); 32961da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3297c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3298c613c209SWillem de Bruijn struct page *p; 329944bb9363SAl Viro __wsum csum2; 33001da177e4SLinus Torvalds u8 *vaddr; 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds if (copy > len) 33031da177e4SLinus Torvalds copy = len; 3304c613c209SWillem de Bruijn 3305c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3306b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3307c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3308c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 33092544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 33102544af03SMatteo Croce csum_partial_ext, 33112544af03SMatteo Croce vaddr + p_off, p_len, 0); 331251c56b00SEric Dumazet kunmap_atomic(vaddr); 33132544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 33142544af03SMatteo Croce csum_block_add_ext, csum, 33152544af03SMatteo Croce csum2, pos, p_len); 3316c613c209SWillem de Bruijn pos += p_len; 3317c613c209SWillem de Bruijn } 3318c613c209SWillem de Bruijn 33191da177e4SLinus Torvalds if (!(len -= copy)) 33201da177e4SLinus Torvalds return csum; 33211da177e4SLinus Torvalds offset += copy; 33221da177e4SLinus Torvalds } 33231a028e50SDavid S. Miller start = end; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 3326fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 33271a028e50SDavid S. Miller int end; 33281da177e4SLinus Torvalds 3329547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33301a028e50SDavid S. Miller 3331fbb398a8SDavid S. Miller end = start + frag_iter->len; 33321da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 33335f92a738SAl Viro __wsum csum2; 33341da177e4SLinus Torvalds if (copy > len) 33351da177e4SLinus Torvalds copy = len; 33362817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 33372817a336SDaniel Borkmann copy, 0, ops); 33382544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 33392544af03SMatteo Croce csum, csum2, pos, copy); 33401da177e4SLinus Torvalds if ((len -= copy) == 0) 33411da177e4SLinus Torvalds return csum; 33421da177e4SLinus Torvalds offset += copy; 33431da177e4SLinus Torvalds pos += copy; 33441da177e4SLinus Torvalds } 33451a028e50SDavid S. Miller start = end; 33461da177e4SLinus Torvalds } 334709a62660SKris Katterjohn BUG_ON(len); 33481da177e4SLinus Torvalds 33491da177e4SLinus Torvalds return csum; 33501da177e4SLinus Torvalds } 33512817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 33522817a336SDaniel Borkmann 33532817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 33542817a336SDaniel Borkmann int len, __wsum csum) 33552817a336SDaniel Borkmann { 33562817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3357cea80ea8SDaniel Borkmann .update = csum_partial_ext, 33582817a336SDaniel Borkmann .combine = csum_block_add_ext, 33592817a336SDaniel Borkmann }; 33602817a336SDaniel Borkmann 33612817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 33622817a336SDaniel Borkmann } 3363b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 33641da177e4SLinus Torvalds 33651da177e4SLinus Torvalds /* Both of above in one bottle. */ 33661da177e4SLinus Torvalds 336781d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 33688d5930dfSAl Viro u8 *to, int len) 33691da177e4SLinus Torvalds { 33701a028e50SDavid S. Miller int start = skb_headlen(skb); 33711a028e50SDavid S. Miller int i, copy = start - offset; 3372fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 33731da177e4SLinus Torvalds int pos = 0; 33748d5930dfSAl Viro __wsum csum = 0; 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds /* Copy header. */ 33771da177e4SLinus Torvalds if (copy > 0) { 33781da177e4SLinus Torvalds if (copy > len) 33791da177e4SLinus Torvalds copy = len; 33801da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3381cc44c17bSAl Viro copy); 33821da177e4SLinus Torvalds if ((len -= copy) == 0) 33831da177e4SLinus Torvalds return csum; 33841da177e4SLinus Torvalds offset += copy; 33851da177e4SLinus Torvalds to += copy; 33861da177e4SLinus Torvalds pos = copy; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33901a028e50SDavid S. Miller int end; 33911da177e4SLinus Torvalds 3392547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33931a028e50SDavid S. Miller 33949e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 33951da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3396c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3397c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3398c613c209SWillem de Bruijn struct page *p; 33995084205fSAl Viro __wsum csum2; 34001da177e4SLinus Torvalds u8 *vaddr; 34011da177e4SLinus Torvalds 34021da177e4SLinus Torvalds if (copy > len) 34031da177e4SLinus Torvalds copy = len; 3404c613c209SWillem de Bruijn 3405c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3406b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3407c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3408c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3409c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3410c613c209SWillem de Bruijn to + copied, 3411cc44c17bSAl Viro p_len); 341251c56b00SEric Dumazet kunmap_atomic(vaddr); 34131da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3414c613c209SWillem de Bruijn pos += p_len; 3415c613c209SWillem de Bruijn } 3416c613c209SWillem de Bruijn 34171da177e4SLinus Torvalds if (!(len -= copy)) 34181da177e4SLinus Torvalds return csum; 34191da177e4SLinus Torvalds offset += copy; 34201da177e4SLinus Torvalds to += copy; 34211da177e4SLinus Torvalds } 34221a028e50SDavid S. Miller start = end; 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds 3425fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 342681d77662SAl Viro __wsum csum2; 34271a028e50SDavid S. Miller int end; 34281da177e4SLinus Torvalds 3429547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34301a028e50SDavid S. Miller 3431fbb398a8SDavid S. Miller end = start + frag_iter->len; 34321da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 34331da177e4SLinus Torvalds if (copy > len) 34341da177e4SLinus Torvalds copy = len; 3435fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 34361a028e50SDavid S. Miller offset - start, 34378d5930dfSAl Viro to, copy); 34381da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 34391da177e4SLinus Torvalds if ((len -= copy) == 0) 34401da177e4SLinus Torvalds return csum; 34411da177e4SLinus Torvalds offset += copy; 34421da177e4SLinus Torvalds to += copy; 34431da177e4SLinus Torvalds pos += copy; 34441da177e4SLinus Torvalds } 34451a028e50SDavid S. Miller start = end; 34461da177e4SLinus Torvalds } 344709a62660SKris Katterjohn BUG_ON(len); 34481da177e4SLinus Torvalds return csum; 34491da177e4SLinus Torvalds } 3450b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 34511da177e4SLinus Torvalds 345249f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 345349f8e832SCong Wang { 345449f8e832SCong Wang __sum16 sum; 345549f8e832SCong Wang 345649f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 345714641931SCong Wang /* See comments in __skb_checksum_complete(). */ 345849f8e832SCong Wang if (likely(!sum)) { 345949f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 346049f8e832SCong Wang !skb->csum_complete_sw) 34617fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 346249f8e832SCong Wang } 346349f8e832SCong Wang if (!skb_shared(skb)) 346449f8e832SCong Wang skb->csum_valid = !sum; 346549f8e832SCong Wang return sum; 346649f8e832SCong Wang } 346749f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 346849f8e832SCong Wang 346914641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 347014641931SCong Wang * which has been changed from the hardware checksum, for example, by 347114641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 347214641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 347314641931SCong Wang * 347414641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 347514641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 347614641931SCong Wang * shared. 347714641931SCong Wang */ 347849f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 347949f8e832SCong Wang { 348049f8e832SCong Wang __wsum csum; 348149f8e832SCong Wang __sum16 sum; 348249f8e832SCong Wang 348349f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 348449f8e832SCong Wang 348549f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 348614641931SCong Wang /* This check is inverted, because we already knew the hardware 348714641931SCong Wang * checksum is invalid before calling this function. So, if the 348814641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 348914641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 349014641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 349114641931SCong Wang * when moving skb->data around. 349214641931SCong Wang */ 349349f8e832SCong Wang if (likely(!sum)) { 349449f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 349549f8e832SCong Wang !skb->csum_complete_sw) 34967fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 349749f8e832SCong Wang } 349849f8e832SCong Wang 349949f8e832SCong Wang if (!skb_shared(skb)) { 350049f8e832SCong Wang /* Save full packet checksum */ 350149f8e832SCong Wang skb->csum = csum; 350249f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 350349f8e832SCong Wang skb->csum_complete_sw = 1; 350449f8e832SCong Wang skb->csum_valid = !sum; 350549f8e832SCong Wang } 350649f8e832SCong Wang 350749f8e832SCong Wang return sum; 350849f8e832SCong Wang } 350949f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 351049f8e832SCong Wang 35119617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 35129617813dSDavide Caratti { 35139617813dSDavide Caratti net_warn_ratelimited( 35149617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 35159617813dSDavide Caratti __func__); 35169617813dSDavide Caratti return 0; 35179617813dSDavide Caratti } 35189617813dSDavide Caratti 35199617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 35209617813dSDavide Caratti int offset, int len) 35219617813dSDavide Caratti { 35229617813dSDavide Caratti net_warn_ratelimited( 35239617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 35249617813dSDavide Caratti __func__); 35259617813dSDavide Caratti return 0; 35269617813dSDavide Caratti } 35279617813dSDavide Caratti 35289617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 35299617813dSDavide Caratti .update = warn_crc32c_csum_update, 35309617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 35319617813dSDavide Caratti }; 35329617813dSDavide Caratti 35339617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 35349617813dSDavide Caratti &default_crc32c_ops; 35359617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 35369617813dSDavide Caratti 3537af2806f8SThomas Graf /** 3538af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3539af2806f8SThomas Graf * @from: source buffer 3540af2806f8SThomas Graf * 3541af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3542af2806f8SThomas Graf * into skb_zerocopy(). 3543af2806f8SThomas Graf */ 3544af2806f8SThomas Graf unsigned int 3545af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3546af2806f8SThomas Graf { 3547af2806f8SThomas Graf unsigned int hlen = 0; 3548af2806f8SThomas Graf 3549af2806f8SThomas Graf if (!from->head_frag || 3550af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3551a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3552af2806f8SThomas Graf hlen = skb_headlen(from); 3553a17ad096SPravin B Shelar if (!hlen) 3554a17ad096SPravin B Shelar hlen = from->len; 3555a17ad096SPravin B Shelar } 3556af2806f8SThomas Graf 3557af2806f8SThomas Graf if (skb_has_frag_list(from)) 3558af2806f8SThomas Graf hlen = from->len; 3559af2806f8SThomas Graf 3560af2806f8SThomas Graf return hlen; 3561af2806f8SThomas Graf } 3562af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3563af2806f8SThomas Graf 3564af2806f8SThomas Graf /** 3565af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3566af2806f8SThomas Graf * @to: destination buffer 35677fceb4deSMasanari Iida * @from: source buffer 3568af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3569af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3570af2806f8SThomas Graf * 3571af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3572af2806f8SThomas Graf * to the frags in the source buffer. 3573af2806f8SThomas Graf * 3574af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3575af2806f8SThomas Graf * headroom in the `to` buffer. 357636d5fe6aSZoltan Kiss * 357736d5fe6aSZoltan Kiss * Return value: 357836d5fe6aSZoltan Kiss * 0: everything is OK 357936d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 358036d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3581af2806f8SThomas Graf */ 358236d5fe6aSZoltan Kiss int 358336d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3584af2806f8SThomas Graf { 3585af2806f8SThomas Graf int i, j = 0; 3586af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 358736d5fe6aSZoltan Kiss int ret; 3588af2806f8SThomas Graf struct page *page; 3589af2806f8SThomas Graf unsigned int offset; 3590af2806f8SThomas Graf 3591af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3592af2806f8SThomas Graf 3593af2806f8SThomas Graf /* dont bother with small payloads */ 359436d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 359536d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3596af2806f8SThomas Graf 3597af2806f8SThomas Graf if (hlen) { 359836d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 359936d5fe6aSZoltan Kiss if (unlikely(ret)) 360036d5fe6aSZoltan Kiss return ret; 3601af2806f8SThomas Graf len -= hlen; 3602af2806f8SThomas Graf } else { 3603af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3604af2806f8SThomas Graf if (plen) { 3605af2806f8SThomas Graf page = virt_to_head_page(from->head); 3606af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3607af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3608af2806f8SThomas Graf get_page(page); 3609af2806f8SThomas Graf j = 1; 3610af2806f8SThomas Graf len -= plen; 3611af2806f8SThomas Graf } 3612af2806f8SThomas Graf } 3613af2806f8SThomas Graf 3614ede57d58SRichard Gobert skb_len_add(to, len + plen); 3615af2806f8SThomas Graf 361636d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 361736d5fe6aSZoltan Kiss skb_tx_error(from); 361836d5fe6aSZoltan Kiss return -ENOMEM; 361936d5fe6aSZoltan Kiss } 36201f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 362136d5fe6aSZoltan Kiss 3622af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3623d8e18a51SMatthew Wilcox (Oracle) int size; 3624d8e18a51SMatthew Wilcox (Oracle) 3625af2806f8SThomas Graf if (!len) 3626af2806f8SThomas Graf break; 3627af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3628d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3629d8e18a51SMatthew Wilcox (Oracle) len); 3630d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3631d8e18a51SMatthew Wilcox (Oracle) len -= size; 3632af2806f8SThomas Graf skb_frag_ref(to, j); 3633af2806f8SThomas Graf j++; 3634af2806f8SThomas Graf } 3635af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 363636d5fe6aSZoltan Kiss 363736d5fe6aSZoltan Kiss return 0; 3638af2806f8SThomas Graf } 3639af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3640af2806f8SThomas Graf 36411da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 36421da177e4SLinus Torvalds { 3643d3bc23e7SAl Viro __wsum csum; 36441da177e4SLinus Torvalds long csstart; 36451da177e4SLinus Torvalds 364684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 364755508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 36481da177e4SLinus Torvalds else 36491da177e4SLinus Torvalds csstart = skb_headlen(skb); 36501da177e4SLinus Torvalds 365109a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 36521da177e4SLinus Torvalds 3653d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 36541da177e4SLinus Torvalds 36551da177e4SLinus Torvalds csum = 0; 36561da177e4SLinus Torvalds if (csstart != skb->len) 36571da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 36588d5930dfSAl Viro skb->len - csstart); 36591da177e4SLinus Torvalds 366084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3661ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 36621da177e4SLinus Torvalds 3663d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 36641da177e4SLinus Torvalds } 36651da177e4SLinus Torvalds } 3666b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds /** 36691da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 36701da177e4SLinus Torvalds * @list: list to dequeue from 36711da177e4SLinus Torvalds * 36721da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 36731da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 36741da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36751da177e4SLinus Torvalds */ 36761da177e4SLinus Torvalds 36771da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 36781da177e4SLinus Torvalds { 36791da177e4SLinus Torvalds unsigned long flags; 36801da177e4SLinus Torvalds struct sk_buff *result; 36811da177e4SLinus Torvalds 36821da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36831da177e4SLinus Torvalds result = __skb_dequeue(list); 36841da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36851da177e4SLinus Torvalds return result; 36861da177e4SLinus Torvalds } 3687b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 36881da177e4SLinus Torvalds 36891da177e4SLinus Torvalds /** 36901da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 36911da177e4SLinus Torvalds * @list: list to dequeue from 36921da177e4SLinus Torvalds * 36931da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 36941da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 36951da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36961da177e4SLinus Torvalds */ 36971da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 36981da177e4SLinus Torvalds { 36991da177e4SLinus Torvalds unsigned long flags; 37001da177e4SLinus Torvalds struct sk_buff *result; 37011da177e4SLinus Torvalds 37021da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37031da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 37041da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37051da177e4SLinus Torvalds return result; 37061da177e4SLinus Torvalds } 3707b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds /** 37104025d3e7SEric Dumazet * skb_queue_purge_reason - empty a list 37111da177e4SLinus Torvalds * @list: list to empty 37124025d3e7SEric Dumazet * @reason: drop reason 37131da177e4SLinus Torvalds * 37141da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 37151da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 37161da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 37171da177e4SLinus Torvalds */ 37184025d3e7SEric Dumazet void skb_queue_purge_reason(struct sk_buff_head *list, 37194025d3e7SEric Dumazet enum skb_drop_reason reason) 37201da177e4SLinus Torvalds { 37211da177e4SLinus Torvalds struct sk_buff *skb; 37224025d3e7SEric Dumazet 37231da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 37244025d3e7SEric Dumazet kfree_skb_reason(skb, reason); 37251da177e4SLinus Torvalds } 37264025d3e7SEric Dumazet EXPORT_SYMBOL(skb_queue_purge_reason); 37271da177e4SLinus Torvalds 37281da177e4SLinus Torvalds /** 37299f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 37309f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3731385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 37329f5afeaeSYaogong Wang * 37339f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 37349f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 37359f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 37369f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 37379f5afeaeSYaogong Wang */ 3738385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 37399f5afeaeSYaogong Wang { 37407c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3741385114deSPeter Oskolkov unsigned int sum = 0; 37429f5afeaeSYaogong Wang 37437c90584cSEric Dumazet while (p) { 37447c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 37457c90584cSEric Dumazet 37467c90584cSEric Dumazet p = rb_next(p); 37477c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3748385114deSPeter Oskolkov sum += skb->truesize; 37499f5afeaeSYaogong Wang kfree_skb(skb); 37507c90584cSEric Dumazet } 3751385114deSPeter Oskolkov return sum; 37529f5afeaeSYaogong Wang } 37539f5afeaeSYaogong Wang 37540f158b32SEric Dumazet void skb_errqueue_purge(struct sk_buff_head *list) 37550f158b32SEric Dumazet { 37560f158b32SEric Dumazet struct sk_buff *skb, *next; 37570f158b32SEric Dumazet struct sk_buff_head kill; 37580f158b32SEric Dumazet unsigned long flags; 37590f158b32SEric Dumazet 37600f158b32SEric Dumazet __skb_queue_head_init(&kill); 37610f158b32SEric Dumazet 37620f158b32SEric Dumazet spin_lock_irqsave(&list->lock, flags); 37630f158b32SEric Dumazet skb_queue_walk_safe(list, skb, next) { 37640f158b32SEric Dumazet if (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ZEROCOPY || 37650f158b32SEric Dumazet SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) 37660f158b32SEric Dumazet continue; 37670f158b32SEric Dumazet __skb_unlink(skb, list); 37680f158b32SEric Dumazet __skb_queue_tail(&kill, skb); 37690f158b32SEric Dumazet } 37700f158b32SEric Dumazet spin_unlock_irqrestore(&list->lock, flags); 37710f158b32SEric Dumazet __skb_queue_purge(&kill); 37720f158b32SEric Dumazet } 37730f158b32SEric Dumazet EXPORT_SYMBOL(skb_errqueue_purge); 37740f158b32SEric Dumazet 37759f5afeaeSYaogong Wang /** 37761da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 37771da177e4SLinus Torvalds * @list: list to use 37781da177e4SLinus Torvalds * @newsk: buffer to queue 37791da177e4SLinus Torvalds * 37801da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 37811da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37821da177e4SLinus Torvalds * safely. 37831da177e4SLinus Torvalds * 37841da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37851da177e4SLinus Torvalds */ 37861da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 37871da177e4SLinus Torvalds { 37881da177e4SLinus Torvalds unsigned long flags; 37891da177e4SLinus Torvalds 37901da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37911da177e4SLinus Torvalds __skb_queue_head(list, newsk); 37921da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37931da177e4SLinus Torvalds } 3794b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 37951da177e4SLinus Torvalds 37961da177e4SLinus Torvalds /** 37971da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 37981da177e4SLinus Torvalds * @list: list to use 37991da177e4SLinus Torvalds * @newsk: buffer to queue 38001da177e4SLinus Torvalds * 38011da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 38021da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 38031da177e4SLinus Torvalds * safely. 38041da177e4SLinus Torvalds * 38051da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 38061da177e4SLinus Torvalds */ 38071da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 38081da177e4SLinus Torvalds { 38091da177e4SLinus Torvalds unsigned long flags; 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 38121da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 38131da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 38141da177e4SLinus Torvalds } 3815b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 38168728b834SDavid S. Miller 38171da177e4SLinus Torvalds /** 38181da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 38191da177e4SLinus Torvalds * @skb: buffer to remove 38208728b834SDavid S. Miller * @list: list to use 38211da177e4SLinus Torvalds * 38228728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 38238728b834SDavid S. Miller * function is atomic with respect to other list locked calls 38241da177e4SLinus Torvalds * 38258728b834SDavid S. Miller * You must know what list the SKB is on. 38261da177e4SLinus Torvalds */ 38278728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 38281da177e4SLinus Torvalds { 38291da177e4SLinus Torvalds unsigned long flags; 38301da177e4SLinus Torvalds 38311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 38328728b834SDavid S. Miller __skb_unlink(skb, list); 38331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 38341da177e4SLinus Torvalds } 3835b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 38361da177e4SLinus Torvalds 38371da177e4SLinus Torvalds /** 38381da177e4SLinus Torvalds * skb_append - append a buffer 38391da177e4SLinus Torvalds * @old: buffer to insert after 38401da177e4SLinus Torvalds * @newsk: buffer to insert 38418728b834SDavid S. Miller * @list: list to use 38421da177e4SLinus Torvalds * 38431da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 38441da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 38451da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 38461da177e4SLinus Torvalds */ 38478728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 38481da177e4SLinus Torvalds { 38491da177e4SLinus Torvalds unsigned long flags; 38501da177e4SLinus Torvalds 38518728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 38527de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 38538728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 38541da177e4SLinus Torvalds } 3855b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 38561da177e4SLinus Torvalds 38571da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 38581da177e4SLinus Torvalds struct sk_buff* skb1, 38591da177e4SLinus Torvalds const u32 len, const int pos) 38601da177e4SLinus Torvalds { 38611da177e4SLinus Torvalds int i; 38621da177e4SLinus Torvalds 3863d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3864d626f62bSArnaldo Carvalho de Melo pos - len); 38651da177e4SLinus Torvalds /* And move data appendix as is. */ 38661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 38671da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 38681da177e4SLinus Torvalds 38691da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 38701da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38711da177e4SLinus Torvalds skb1->data_len = skb->data_len; 38721da177e4SLinus Torvalds skb1->len += skb1->data_len; 38731da177e4SLinus Torvalds skb->data_len = 0; 38741da177e4SLinus Torvalds skb->len = len; 387527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds 38781da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 38791da177e4SLinus Torvalds struct sk_buff* skb1, 38801da177e4SLinus Torvalds const u32 len, int pos) 38811da177e4SLinus Torvalds { 38821da177e4SLinus Torvalds int i, k = 0; 38831da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 38841da177e4SLinus Torvalds 38851da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38861da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 38871da177e4SLinus Torvalds skb->len = len; 38881da177e4SLinus Torvalds skb->data_len = len - pos; 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 38919e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds if (pos + size > len) { 38941da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 38951da177e4SLinus Torvalds 38961da177e4SLinus Torvalds if (pos < len) { 38971da177e4SLinus Torvalds /* Split frag. 38981da177e4SLinus Torvalds * We have two variants in this case: 38991da177e4SLinus Torvalds * 1. Move all the frag to the second 39001da177e4SLinus Torvalds * part, if it is possible. F.e. 39011da177e4SLinus Torvalds * this approach is mandatory for TUX, 39021da177e4SLinus Torvalds * where splitting is expensive. 39031da177e4SLinus Torvalds * 2. Split is accurately. We make this. 39041da177e4SLinus Torvalds */ 3905ea2ab693SIan Campbell skb_frag_ref(skb, i); 3906b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 39079e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 39089e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 39091da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 39101da177e4SLinus Torvalds } 39111da177e4SLinus Torvalds k++; 39121da177e4SLinus Torvalds } else 39131da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 39141da177e4SLinus Torvalds pos += size; 39151da177e4SLinus Torvalds } 39161da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 39171da177e4SLinus Torvalds } 39181da177e4SLinus Torvalds 39191da177e4SLinus Torvalds /** 39201da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 39211da177e4SLinus Torvalds * @skb: the buffer to split 39221da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 39231da177e4SLinus Torvalds * @len: new length for skb 39241da177e4SLinus Torvalds */ 39251da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 39261da177e4SLinus Torvalds { 39271da177e4SLinus Torvalds int pos = skb_headlen(skb); 39289b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 39291da177e4SLinus Torvalds 3930753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3931753f1ca4SPavel Begunkov 39329b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 39331f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 39341da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 39351da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 39361da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 39371da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 39381da177e4SLinus Torvalds } 3939b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 39401da177e4SLinus Torvalds 39419f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 39429f782db3SIlpo Järvinen * 39439f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 39449f782db3SIlpo Järvinen */ 3945832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3946832d11c5SIlpo Järvinen { 3947c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3948832d11c5SIlpo Järvinen } 3949832d11c5SIlpo Järvinen 3950832d11c5SIlpo Järvinen /** 3951832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3952832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3953832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3954832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3955832d11c5SIlpo Järvinen * 3956832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 395720e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3958832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3959832d11c5SIlpo Järvinen * 3960832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3961832d11c5SIlpo Järvinen * 3962832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3963832d11c5SIlpo Järvinen * to have non-paged data as well. 3964832d11c5SIlpo Järvinen * 3965832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3966832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3967832d11c5SIlpo Järvinen */ 3968832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3969832d11c5SIlpo Järvinen { 3970832d11c5SIlpo Järvinen int from, to, merge, todo; 3971d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3972832d11c5SIlpo Järvinen 3973832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3974f8071cdeSEric Dumazet 3975f8071cdeSEric Dumazet if (skb_headlen(skb)) 3976f8071cdeSEric Dumazet return 0; 39771f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 39781f8b977aSWillem de Bruijn return 0; 3979832d11c5SIlpo Järvinen 3980832d11c5SIlpo Järvinen todo = shiftlen; 3981832d11c5SIlpo Järvinen from = 0; 3982832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3983832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3984832d11c5SIlpo Järvinen 3985832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3986832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3987832d11c5SIlpo Järvinen */ 3988832d11c5SIlpo Järvinen if (!to || 3989ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3990b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3991832d11c5SIlpo Järvinen merge = -1; 3992832d11c5SIlpo Järvinen } else { 3993832d11c5SIlpo Järvinen merge = to - 1; 3994832d11c5SIlpo Järvinen 39959e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3996832d11c5SIlpo Järvinen if (todo < 0) { 3997832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3998832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3999832d11c5SIlpo Järvinen return 0; 4000832d11c5SIlpo Järvinen 40019f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 40029f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 4003832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 4004832d11c5SIlpo Järvinen 40059e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 40069e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 4007b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 4008832d11c5SIlpo Järvinen 4009832d11c5SIlpo Järvinen goto onlymerged; 4010832d11c5SIlpo Järvinen } 4011832d11c5SIlpo Järvinen 4012832d11c5SIlpo Järvinen from++; 4013832d11c5SIlpo Järvinen } 4014832d11c5SIlpo Järvinen 4015832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 4016832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 4017832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 4018832d11c5SIlpo Järvinen return 0; 4019832d11c5SIlpo Järvinen 4020832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 4021832d11c5SIlpo Järvinen return 0; 4022832d11c5SIlpo Järvinen 4023832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 4024832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 4025832d11c5SIlpo Järvinen return 0; 4026832d11c5SIlpo Järvinen 4027832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 4028832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 4029832d11c5SIlpo Järvinen 40309e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 4031832d11c5SIlpo Järvinen *fragto = *fragfrom; 40329e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 4033832d11c5SIlpo Järvinen from++; 4034832d11c5SIlpo Järvinen to++; 4035832d11c5SIlpo Järvinen 4036832d11c5SIlpo Järvinen } else { 4037ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 4038b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 4039b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 40409e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 4041832d11c5SIlpo Järvinen 4042b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 40439e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 4044832d11c5SIlpo Järvinen todo = 0; 4045832d11c5SIlpo Järvinen 4046832d11c5SIlpo Järvinen to++; 4047832d11c5SIlpo Järvinen break; 4048832d11c5SIlpo Järvinen } 4049832d11c5SIlpo Järvinen } 4050832d11c5SIlpo Järvinen 4051832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 4052832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 4053832d11c5SIlpo Järvinen 4054832d11c5SIlpo Järvinen if (merge >= 0) { 4055832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 4056832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 4057832d11c5SIlpo Järvinen 40589e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 40596a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 4060832d11c5SIlpo Järvinen } 4061832d11c5SIlpo Järvinen 4062832d11c5SIlpo Järvinen /* Reposition in the original skb */ 4063832d11c5SIlpo Järvinen to = 0; 4064832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 4065832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 4066832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 4067832d11c5SIlpo Järvinen 4068832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 4069832d11c5SIlpo Järvinen 4070832d11c5SIlpo Järvinen onlymerged: 4071832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 4072832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 4073832d11c5SIlpo Järvinen */ 4074832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 4075832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 4076832d11c5SIlpo Järvinen 4077ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 4078ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 4079832d11c5SIlpo Järvinen 4080832d11c5SIlpo Järvinen return shiftlen; 4081832d11c5SIlpo Järvinen } 4082832d11c5SIlpo Järvinen 4083677e90edSThomas Graf /** 4084677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 4085677e90edSThomas Graf * @skb: the buffer to read 4086677e90edSThomas Graf * @from: lower offset of data to be read 4087677e90edSThomas Graf * @to: upper offset of data to be read 4088677e90edSThomas Graf * @st: state variable 4089677e90edSThomas Graf * 4090677e90edSThomas Graf * Initializes the specified state variable. Must be called before 4091677e90edSThomas Graf * invoking skb_seq_read() for the first time. 4092677e90edSThomas Graf */ 4093677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 4094677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 4095677e90edSThomas Graf { 4096677e90edSThomas Graf st->lower_offset = from; 4097677e90edSThomas Graf st->upper_offset = to; 4098677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 4099677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 4100677e90edSThomas Graf st->frag_data = NULL; 410197550f6fSWillem de Bruijn st->frag_off = 0; 4102677e90edSThomas Graf } 4103b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 4104677e90edSThomas Graf 4105677e90edSThomas Graf /** 4106677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 4107677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 4108677e90edSThomas Graf * @data: destination pointer for data to be returned 4109677e90edSThomas Graf * @st: state variable 4110677e90edSThomas Graf * 4111bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 4112677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 4113bc32383cSMathias Krause * the head of the data block to @data and returns the length 4114677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 4115677e90edSThomas Graf * offset has been reached. 4116677e90edSThomas Graf * 4117677e90edSThomas Graf * The caller is not required to consume all of the data 4118bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 4119677e90edSThomas Graf * of bytes already consumed and the next call to 4120677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 4121677e90edSThomas Graf * 412225985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 4123e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 4124677e90edSThomas Graf * reads of potentially non linear data. 4125677e90edSThomas Graf * 4126bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 4127677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 4128677e90edSThomas Graf * a stack for this purpose. 4129677e90edSThomas Graf */ 4130677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 4131677e90edSThomas Graf struct skb_seq_state *st) 4132677e90edSThomas Graf { 4133677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 4134677e90edSThomas Graf skb_frag_t *frag; 4135677e90edSThomas Graf 4136aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 4137aeb193eaSWedson Almeida Filho if (st->frag_data) { 4138aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 4139aeb193eaSWedson Almeida Filho st->frag_data = NULL; 4140aeb193eaSWedson Almeida Filho } 4141677e90edSThomas Graf return 0; 4142aeb193eaSWedson Almeida Filho } 4143677e90edSThomas Graf 4144677e90edSThomas Graf next_skb: 414595e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 4146677e90edSThomas Graf 4147995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 414895e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 4149677e90edSThomas Graf return block_limit - abs_offset; 4150677e90edSThomas Graf } 4151677e90edSThomas Graf 4152677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 4153677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 4154677e90edSThomas Graf 4155677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 415697550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 4157677e90edSThomas Graf 415897550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 415997550f6fSWillem de Bruijn 416097550f6fSWillem de Bruijn pg_idx = 0; 416197550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 416297550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 416397550f6fSWillem de Bruijn 416497550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 416597550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 416697550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 416797550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 416897550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 416997550f6fSWillem de Bruijn } 417097550f6fSWillem de Bruijn 417197550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 4172677e90edSThomas Graf if (abs_offset < block_limit) { 4173677e90edSThomas Graf if (!st->frag_data) 417497550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 4175677e90edSThomas Graf 417697550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 4177677e90edSThomas Graf (abs_offset - st->stepped_offset); 4178677e90edSThomas Graf 4179677e90edSThomas Graf return block_limit - abs_offset; 4180677e90edSThomas Graf } 4181677e90edSThomas Graf 4182677e90edSThomas Graf if (st->frag_data) { 418351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4184677e90edSThomas Graf st->frag_data = NULL; 4185677e90edSThomas Graf } 4186677e90edSThomas Graf 418797550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 418897550f6fSWillem de Bruijn st->frag_off += pg_sz; 418997550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 419097550f6fSWillem de Bruijn st->frag_off = 0; 4191677e90edSThomas Graf st->frag_idx++; 419297550f6fSWillem de Bruijn } 4193677e90edSThomas Graf } 4194677e90edSThomas Graf 41955b5a60daSOlaf Kirch if (st->frag_data) { 419651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 41975b5a60daSOlaf Kirch st->frag_data = NULL; 41985b5a60daSOlaf Kirch } 41995b5a60daSOlaf Kirch 420021dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4201677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 420295e3b24cSHerbert Xu st->frag_idx = 0; 4203677e90edSThomas Graf goto next_skb; 420471b3346dSShyam Iyer } else if (st->cur_skb->next) { 420571b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 420671b3346dSShyam Iyer st->frag_idx = 0; 4207677e90edSThomas Graf goto next_skb; 4208677e90edSThomas Graf } 4209677e90edSThomas Graf 4210677e90edSThomas Graf return 0; 4211677e90edSThomas Graf } 4212b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4213677e90edSThomas Graf 4214677e90edSThomas Graf /** 4215677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4216677e90edSThomas Graf * @st: state variable 4217677e90edSThomas Graf * 4218677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4219677e90edSThomas Graf * returned 0. 4220677e90edSThomas Graf */ 4221677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4222677e90edSThomas Graf { 4223677e90edSThomas Graf if (st->frag_data) 422451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4225677e90edSThomas Graf } 4226b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4227677e90edSThomas Graf 42283fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 42293fc7e8a6SThomas Graf 42303fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 42313fc7e8a6SThomas Graf struct ts_config *conf, 42323fc7e8a6SThomas Graf struct ts_state *state) 42333fc7e8a6SThomas Graf { 42343fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 42353fc7e8a6SThomas Graf } 42363fc7e8a6SThomas Graf 42373fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 42383fc7e8a6SThomas Graf { 42393fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 42403fc7e8a6SThomas Graf } 42413fc7e8a6SThomas Graf 42423fc7e8a6SThomas Graf /** 42433fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 42443fc7e8a6SThomas Graf * @skb: the buffer to look in 42453fc7e8a6SThomas Graf * @from: search offset 42463fc7e8a6SThomas Graf * @to: search limit 42473fc7e8a6SThomas Graf * @config: textsearch configuration 42483fc7e8a6SThomas Graf * 42493fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 42503fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 42513fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 42523fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 42533fc7e8a6SThomas Graf */ 42543fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4255059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 42563fc7e8a6SThomas Graf { 4257*7802db1dSPhil Sutter unsigned int patlen = config->ops->get_pattern_len(config); 4258059a2440SBojan Prtvar struct ts_state state; 4259f72b948dSPhil Oester unsigned int ret; 4260f72b948dSPhil Oester 4261b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4262b228c9b0SWillem de Bruijn 42633fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 42643fc7e8a6SThomas Graf config->finish = skb_ts_finish; 42653fc7e8a6SThomas Graf 4266059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 42673fc7e8a6SThomas Graf 4268059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4269*7802db1dSPhil Sutter return (ret + patlen <= to - from ? ret : UINT_MAX); 42703fc7e8a6SThomas Graf } 4271b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 42723fc7e8a6SThomas Graf 4273be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 427496449f90SDavid Howells int offset, size_t size, size_t max_frags) 4275be12a1feSHannes Frederic Sowa { 4276be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4277be12a1feSHannes Frederic Sowa 4278be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4279be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 428096449f90SDavid Howells } else if (i < max_frags) { 4281753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4282be12a1feSHannes Frederic Sowa get_page(page); 4283228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4284be12a1feSHannes Frederic Sowa } else { 4285be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4286be12a1feSHannes Frederic Sowa } 4287be12a1feSHannes Frederic Sowa 4288be12a1feSHannes Frederic Sowa return 0; 4289be12a1feSHannes Frederic Sowa } 4290be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4291be12a1feSHannes Frederic Sowa 4292cbb042f9SHerbert Xu /** 4293cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4294cbb042f9SHerbert Xu * @skb: buffer to update 4295cbb042f9SHerbert Xu * @len: length of data pulled 4296cbb042f9SHerbert Xu * 4297cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4298fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 429984fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 430084fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 430184fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4302cbb042f9SHerbert Xu */ 4303af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4304cbb042f9SHerbert Xu { 430531b33dfbSPravin B Shelar unsigned char *data = skb->data; 430631b33dfbSPravin B Shelar 4307cbb042f9SHerbert Xu BUG_ON(len > skb->len); 430831b33dfbSPravin B Shelar __skb_pull(skb, len); 430931b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 431031b33dfbSPravin B Shelar return skb->data; 4311cbb042f9SHerbert Xu } 4312f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4313f94691acSArnaldo Carvalho de Melo 431413acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 431513acc94eSYonghong Song { 431613acc94eSYonghong Song skb_frag_t head_frag; 431713acc94eSYonghong Song struct page *page; 431813acc94eSYonghong Song 431913acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4320b51f4113SYunsheng Lin skb_frag_fill_page_desc(&head_frag, page, frag_skb->data - 4321b51f4113SYunsheng Lin (unsigned char *)page_address(page), 4322b51f4113SYunsheng Lin skb_headlen(frag_skb)); 432313acc94eSYonghong Song return head_frag; 432413acc94eSYonghong Song } 432513acc94eSYonghong Song 43263a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 43273a1296a3SSteffen Klassert netdev_features_t features, 43283a1296a3SSteffen Klassert unsigned int offset) 43293a1296a3SSteffen Klassert { 43303a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 43313a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 43323a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 43333a1296a3SSteffen Klassert unsigned int delta_len = 0; 43343a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 433553475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4336cf3ab8d4SLina Wang int len_diff, err; 43373a1296a3SSteffen Klassert 43383a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 43393a1296a3SSteffen Klassert 4340c329b261SPaolo Abeni /* Ensure the head is writeable before touching the shared info */ 4341c329b261SPaolo Abeni err = skb_unclone(skb, GFP_ATOMIC); 4342c329b261SPaolo Abeni if (err) 4343c329b261SPaolo Abeni goto err_linearize; 4344c329b261SPaolo Abeni 43453a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 43463a1296a3SSteffen Klassert 4347876e8ca8SYan Zhai while (list_skb) { 43483a1296a3SSteffen Klassert nskb = list_skb; 43493a1296a3SSteffen Klassert list_skb = list_skb->next; 43503a1296a3SSteffen Klassert 435153475c5dSDongseok Yi err = 0; 4352224102deSlena wang delta_truesize += nskb->truesize; 435353475c5dSDongseok Yi if (skb_shared(nskb)) { 435453475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 435553475c5dSDongseok Yi if (tmp) { 435653475c5dSDongseok Yi consume_skb(nskb); 435753475c5dSDongseok Yi nskb = tmp; 435853475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 435953475c5dSDongseok Yi } else { 436053475c5dSDongseok Yi err = -ENOMEM; 436153475c5dSDongseok Yi } 436253475c5dSDongseok Yi } 436353475c5dSDongseok Yi 43643a1296a3SSteffen Klassert if (!tail) 43653a1296a3SSteffen Klassert skb->next = nskb; 43663a1296a3SSteffen Klassert else 43673a1296a3SSteffen Klassert tail->next = nskb; 43683a1296a3SSteffen Klassert 436953475c5dSDongseok Yi if (unlikely(err)) { 437053475c5dSDongseok Yi nskb->next = list_skb; 437153475c5dSDongseok Yi goto err_linearize; 437253475c5dSDongseok Yi } 437353475c5dSDongseok Yi 43743a1296a3SSteffen Klassert tail = nskb; 43753a1296a3SSteffen Klassert 43763a1296a3SSteffen Klassert delta_len += nskb->len; 43773a1296a3SSteffen Klassert 43783a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 43793a1296a3SSteffen Klassert 4380cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4381cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 43823a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 43833a1296a3SSteffen Klassert 43843a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4385cf3ab8d4SLina Wang nskb->transport_header += len_diff; 43863a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 43873a1296a3SSteffen Klassert nskb->data - tnl_hlen, 43883a1296a3SSteffen Klassert offset + tnl_hlen); 43893a1296a3SSteffen Klassert 43903a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 43913a1296a3SSteffen Klassert __skb_linearize(nskb)) 43923a1296a3SSteffen Klassert goto err_linearize; 4393876e8ca8SYan Zhai } 43943a1296a3SSteffen Klassert 43953a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 43963a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 43973a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 43983a1296a3SSteffen Klassert 43993a1296a3SSteffen Klassert skb_gso_reset(skb); 44003a1296a3SSteffen Klassert 44013a1296a3SSteffen Klassert skb->prev = tail; 44023a1296a3SSteffen Klassert 44033a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 44043a1296a3SSteffen Klassert __skb_linearize(skb)) 44053a1296a3SSteffen Klassert goto err_linearize; 44063a1296a3SSteffen Klassert 44073a1296a3SSteffen Klassert skb_get(skb); 44083a1296a3SSteffen Klassert 44093a1296a3SSteffen Klassert return skb; 44103a1296a3SSteffen Klassert 44113a1296a3SSteffen Klassert err_linearize: 44123a1296a3SSteffen Klassert kfree_skb_list(skb->next); 44133a1296a3SSteffen Klassert skb->next = NULL; 44143a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 44153a1296a3SSteffen Klassert } 44163a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 44173a1296a3SSteffen Klassert 4418f4c50d99SHerbert Xu /** 4419f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4420df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4421576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4422f4c50d99SHerbert Xu * 4423f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 44244c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 44254c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4426f4c50d99SHerbert Xu */ 4427df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4428df5771ffSMichael S. Tsirkin netdev_features_t features) 4429f4c50d99SHerbert Xu { 4430f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4431f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 44321a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4433df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4434df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 4435f4c50d99SHerbert Xu unsigned int offset = doffset; 4436df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4437802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4438f4c50d99SHerbert Xu unsigned int headroom; 4439802ab55aSAlexander Duyck unsigned int len = head_skb->len; 44402ea35288SMohamed Khalfella struct sk_buff *frag_skb; 44412ea35288SMohamed Khalfella skb_frag_t *frag; 4442ec5f0615SPravin B Shelar __be16 proto; 444336c98382SAlexander Duyck bool csum, sg; 4444f4c50d99SHerbert Xu int err = -ENOMEM; 4445f4c50d99SHerbert Xu int i = 0; 44462ea35288SMohamed Khalfella int nfrags, pos; 4447f4c50d99SHerbert Xu 44489e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 44499e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 44509e4b7a99SJiri Benc struct sk_buff *check_skb; 44519e4b7a99SJiri Benc 44529e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 44539e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 44549e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 44559e4b7a99SJiri Benc * a linear non head_frag item. 44563dcbdb13SShmulik Ladkani * 44579e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 44589e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 44599e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 44609e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 44619e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 44623dcbdb13SShmulik Ladkani */ 44633dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 44649e4b7a99SJiri Benc break; 44659e4b7a99SJiri Benc } 44669e4b7a99SJiri Benc } 44673dcbdb13SShmulik Ladkani } 44683dcbdb13SShmulik Ladkani 44695882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 44702f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4471ec5f0615SPravin B Shelar if (unlikely(!proto)) 4472ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4473ec5f0615SPravin B Shelar 447436c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4475f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 44767e2b10c1STom Herbert 447707b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 447807b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 447907b26c94SSteffen Klassert struct sk_buff *iter; 448043170c4eSIlan Tayari unsigned int frag_len; 448107b26c94SSteffen Klassert 448207b26c94SSteffen Klassert if (!list_skb || 448307b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 448407b26c94SSteffen Klassert goto normal; 448507b26c94SSteffen Klassert 448643170c4eSIlan Tayari /* If we get here then all the required 448743170c4eSIlan Tayari * GSO features except frag_list are supported. 448843170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 448943170c4eSIlan Tayari * with no frag_list. 449043170c4eSIlan Tayari * Currently we can do that only when the buffers don't 449143170c4eSIlan Tayari * have a linear part and all the buffers except 449243170c4eSIlan Tayari * the last are of the same length. 449307b26c94SSteffen Klassert */ 449443170c4eSIlan Tayari frag_len = list_skb->len; 449507b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 449643170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 449743170c4eSIlan Tayari goto normal; 4498eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 449907b26c94SSteffen Klassert goto normal; 450007b26c94SSteffen Klassert 450107b26c94SSteffen Klassert len -= iter->len; 450207b26c94SSteffen Klassert } 450343170c4eSIlan Tayari 450443170c4eSIlan Tayari if (len != frag_len) 450543170c4eSIlan Tayari goto normal; 450607b26c94SSteffen Klassert } 450707b26c94SSteffen Klassert 4508802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4509802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4510802ab55aSAlexander Duyck * now. 4511802ab55aSAlexander Duyck */ 4512802ab55aSAlexander Duyck partial_segs = len / mss; 4513d7fb5a80SAlexander Duyck if (partial_segs > 1) 4514802ab55aSAlexander Duyck mss *= partial_segs; 4515d7fb5a80SAlexander Duyck else 4516d7fb5a80SAlexander Duyck partial_segs = 0; 4517802ab55aSAlexander Duyck } 4518802ab55aSAlexander Duyck 451907b26c94SSteffen Klassert normal: 4520df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4521df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4522f4c50d99SHerbert Xu 45232ea35288SMohamed Khalfella if (skb_orphan_frags(head_skb, GFP_ATOMIC)) 45242ea35288SMohamed Khalfella return ERR_PTR(-ENOMEM); 45252ea35288SMohamed Khalfella 45262ea35288SMohamed Khalfella nfrags = skb_shinfo(head_skb)->nr_frags; 45272ea35288SMohamed Khalfella frag = skb_shinfo(head_skb)->frags; 45282ea35288SMohamed Khalfella frag_skb = head_skb; 45292ea35288SMohamed Khalfella 4530f4c50d99SHerbert Xu do { 4531f4c50d99SHerbert Xu struct sk_buff *nskb; 45328cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4533c8884eddSHerbert Xu int hsize; 4534f4c50d99SHerbert Xu int size; 4535f4c50d99SHerbert Xu 45363953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 45373953c46cSMarcelo Ricardo Leitner len = list_skb->len; 45383953c46cSMarcelo Ricardo Leitner } else { 4539df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4540f4c50d99SHerbert Xu if (len > mss) 4541f4c50d99SHerbert Xu len = mss; 45423953c46cSMarcelo Ricardo Leitner } 4543f4c50d99SHerbert Xu 4544df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4545f4c50d99SHerbert Xu 4546dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 45471a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 45481a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 454989319d38SHerbert Xu 45502ea35288SMohamed Khalfella nskb = skb_clone(list_skb, GFP_ATOMIC); 45512ea35288SMohamed Khalfella if (unlikely(!nskb)) 45522ea35288SMohamed Khalfella goto err; 45532ea35288SMohamed Khalfella 45549d8506ccSHerbert Xu i = 0; 45551a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 45561a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 45571fd819ecSMichael S. Tsirkin frag_skb = list_skb; 45581a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 45599d8506ccSHerbert Xu 45609d8506ccSHerbert Xu while (pos < offset + len) { 45619d8506ccSHerbert Xu BUG_ON(i >= nfrags); 45629d8506ccSHerbert Xu 45634e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 45649d8506ccSHerbert Xu if (pos + size > offset + len) 45659d8506ccSHerbert Xu break; 45669d8506ccSHerbert Xu 45679d8506ccSHerbert Xu i++; 45689d8506ccSHerbert Xu pos += size; 45694e1beba1SMichael S. Tsirkin frag++; 45709d8506ccSHerbert Xu } 45719d8506ccSHerbert Xu 45721a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 457389319d38SHerbert Xu 45749d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 45759d8506ccSHerbert Xu kfree_skb(nskb); 45769d8506ccSHerbert Xu goto err; 45779d8506ccSHerbert Xu } 45789d8506ccSHerbert Xu 4579ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 458089319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 458189319d38SHerbert Xu kfree_skb(nskb); 458289319d38SHerbert Xu goto err; 458389319d38SHerbert Xu } 458489319d38SHerbert Xu 4585ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 458689319d38SHerbert Xu skb_release_head_state(nskb); 458789319d38SHerbert Xu __skb_push(nskb, doffset); 458889319d38SHerbert Xu } else { 458900b229f7SPaolo Abeni if (hsize < 0) 459000b229f7SPaolo Abeni hsize = 0; 4591dbd50f23SXin Long if (hsize > len || !sg) 4592dbd50f23SXin Long hsize = len; 4593dbd50f23SXin Long 4594c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4595df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4596c93bdd0eSMel Gorman NUMA_NO_NODE); 459789319d38SHerbert Xu 459889319d38SHerbert Xu if (unlikely(!nskb)) 459989319d38SHerbert Xu goto err; 460089319d38SHerbert Xu 460189319d38SHerbert Xu skb_reserve(nskb, headroom); 460289319d38SHerbert Xu __skb_put(nskb, doffset); 460389319d38SHerbert Xu } 460489319d38SHerbert Xu 4605f4c50d99SHerbert Xu if (segs) 4606f4c50d99SHerbert Xu tail->next = nskb; 4607f4c50d99SHerbert Xu else 4608f4c50d99SHerbert Xu segs = nskb; 4609f4c50d99SHerbert Xu tail = nskb; 4610f4c50d99SHerbert Xu 4611df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4612f4c50d99SHerbert Xu 4613030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4614fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 461568c33163SPravin B Shelar 4616df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 461768c33163SPravin B Shelar nskb->data - tnl_hlen, 461868c33163SPravin B Shelar doffset + tnl_hlen); 461989319d38SHerbert Xu 46209d8506ccSHerbert Xu if (nskb->len == len + doffset) 46211cdbcb79SSimon Horman goto perform_csum_check; 462289319d38SHerbert Xu 46237fbeffedSAlexander Duyck if (!sg) { 46241454c9faSYadu Kishore if (!csum) { 46257fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 46266f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 462776443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 462876443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 46291454c9faSYadu Kishore skb_put(nskb, 46301454c9faSYadu Kishore len), 46318d5930dfSAl Viro len); 46327e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4633de843723STom Herbert skb_headroom(nskb) + doffset; 46341454c9faSYadu Kishore } else { 4635c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4636c624c58eSlily goto err; 46371454c9faSYadu Kishore } 4638f4c50d99SHerbert Xu continue; 4639f4c50d99SHerbert Xu } 4640f4c50d99SHerbert Xu 46418cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4642f4c50d99SHerbert Xu 4643df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4644d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4645f4c50d99SHerbert Xu 464606b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 464706b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4648cef401deSEric Dumazet 46492ea35288SMohamed Khalfella if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4650bf5c25d6SWillem de Bruijn goto err; 4651bf5c25d6SWillem de Bruijn 46529d8506ccSHerbert Xu while (pos < offset + len) { 46539d8506ccSHerbert Xu if (i >= nfrags) { 46542ea35288SMohamed Khalfella if (skb_orphan_frags(list_skb, GFP_ATOMIC) || 46552ea35288SMohamed Khalfella skb_zerocopy_clone(nskb, list_skb, 46562ea35288SMohamed Khalfella GFP_ATOMIC)) 46572ea35288SMohamed Khalfella goto err; 46582ea35288SMohamed Khalfella 46599d8506ccSHerbert Xu i = 0; 46601a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 46611a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 46621fd819ecSMichael S. Tsirkin frag_skb = list_skb; 466313acc94eSYonghong Song if (!skb_headlen(list_skb)) { 46649d8506ccSHerbert Xu BUG_ON(!nfrags); 466513acc94eSYonghong Song } else { 466613acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 46679d8506ccSHerbert Xu 466813acc94eSYonghong Song /* to make room for head_frag. */ 466913acc94eSYonghong Song i--; 467013acc94eSYonghong Song frag--; 467113acc94eSYonghong Song } 4672bf5c25d6SWillem de Bruijn 46731a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 46749d8506ccSHerbert Xu } 46759d8506ccSHerbert Xu 46769d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 46779d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 46789d8506ccSHerbert Xu net_warn_ratelimited( 46799d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 46809d8506ccSHerbert Xu pos, mss); 4681ff907a11SEric Dumazet err = -EINVAL; 46829d8506ccSHerbert Xu goto err; 46839d8506ccSHerbert Xu } 46849d8506ccSHerbert Xu 468513acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 46868cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 46878cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4688f4c50d99SHerbert Xu 4689f4c50d99SHerbert Xu if (pos < offset) { 4690b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 46918cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4692f4c50d99SHerbert Xu } 4693f4c50d99SHerbert Xu 469489319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4695f4c50d99SHerbert Xu 4696f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4697f4c50d99SHerbert Xu i++; 46984e1beba1SMichael S. Tsirkin frag++; 4699f4c50d99SHerbert Xu pos += size; 4700f4c50d99SHerbert Xu } else { 47018cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 470289319d38SHerbert Xu goto skip_fraglist; 4703f4c50d99SHerbert Xu } 4704f4c50d99SHerbert Xu 47058cb19905SMichael S. Tsirkin nskb_frag++; 4706f4c50d99SHerbert Xu } 4707f4c50d99SHerbert Xu 470889319d38SHerbert Xu skip_fraglist: 4709f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4710f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4711f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4712ec5f0615SPravin B Shelar 47131cdbcb79SSimon Horman perform_csum_check: 47147fbeffedSAlexander Duyck if (!csum) { 4715ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4716ff907a11SEric Dumazet __skb_linearize(nskb)) 4717ddff00d4SAlexander Duyck goto err; 4718ff907a11SEric Dumazet 47197fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4720ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 472176443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 472276443456SAlexander Duyck skb_checksum(nskb, doffset, 472376443456SAlexander Duyck nskb->len - doffset, 0); 47247e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 47257e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4726ec5f0615SPravin B Shelar } 4727df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4728f4c50d99SHerbert Xu 4729bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4730bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4731bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4732bec3cfdcSEric Dumazet */ 4733bec3cfdcSEric Dumazet segs->prev = tail; 4734432c856fSToshiaki Makita 4735802ab55aSAlexander Duyck if (partial_segs) { 473607b26c94SSteffen Klassert struct sk_buff *iter; 4737802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 473807b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4739802ab55aSAlexander Duyck 4740802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 474107b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4742802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4743802ab55aSAlexander Duyck 4744802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4745802ab55aSAlexander Duyck * our way back down the stack of protocols. 4746802ab55aSAlexander Duyck */ 474707b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 474807b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 474907b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 475007b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 475107b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 475207b26c94SSteffen Klassert } 475307b26c94SSteffen Klassert 475407b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 475507b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 475607b26c94SSteffen Klassert else if (tail != segs) 475707b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4758802ab55aSAlexander Duyck } 4759802ab55aSAlexander Duyck 4760432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4761432c856fSToshiaki Makita * using skb_set_owner_w(). 4762432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4763432c856fSToshiaki Makita */ 4764432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4765432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4766432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4767432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4768432c856fSToshiaki Makita } 4769f4c50d99SHerbert Xu return segs; 4770f4c50d99SHerbert Xu 4771f4c50d99SHerbert Xu err: 4772289dccbeSEric Dumazet kfree_skb_list(segs); 4773f4c50d99SHerbert Xu return ERR_PTR(err); 4774f4c50d99SHerbert Xu } 4775f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4776f4c50d99SHerbert Xu 4777df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4778df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4779df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4780df5042f4SFlorian Westphal 4781df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4782df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4783df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4784df5042f4SFlorian Westphal #endif 47854165079bSFlorian Westphal #ifdef CONFIG_XFRM 47864165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 47874165079bSFlorian Westphal #endif 478895a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 478995a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 479095a7233cSPaul Blakey #endif 47913ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47923ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 47933ee17bc7SMat Martineau #endif 479478476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 479578476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 479678476d31SJeremy Kerr #endif 4797df5042f4SFlorian Westphal }; 4798df5042f4SFlorian Westphal 4799df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4800df5042f4SFlorian Westphal { 48015d21d0a6SThomas Weißschuh unsigned int l = SKB_EXT_CHUNKSIZEOF(struct skb_ext); 48025d21d0a6SThomas Weißschuh int i; 48035d21d0a6SThomas Weißschuh 48045d21d0a6SThomas Weißschuh for (i = 0; i < ARRAY_SIZE(skb_ext_type_len); i++) 48055d21d0a6SThomas Weißschuh l += skb_ext_type_len[i]; 48065d21d0a6SThomas Weißschuh 48075d21d0a6SThomas Weißschuh return l; 4808df5042f4SFlorian Westphal } 4809df5042f4SFlorian Westphal 4810df5042f4SFlorian Westphal static void skb_extensions_init(void) 4811df5042f4SFlorian Westphal { 4812df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4813df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4814df5042f4SFlorian Westphal 4815df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4816df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4817df5042f4SFlorian Westphal 0, 4818df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4819df5042f4SFlorian Westphal NULL); 4820df5042f4SFlorian Westphal } 4821df5042f4SFlorian Westphal #else 4822df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4823df5042f4SFlorian Westphal #endif 4824df5042f4SFlorian Westphal 48250a064316SJesper Dangaard Brouer /* The SKB kmem_cache slab is critical for network performance. Never 48260a064316SJesper Dangaard Brouer * merge/alias the slab with similar sized objects. This avoids fragmentation 48270a064316SJesper Dangaard Brouer * that hurts performance of kmem_cache_{alloc,free}_bulk APIs. 48280a064316SJesper Dangaard Brouer */ 48290a064316SJesper Dangaard Brouer #ifndef CONFIG_SLUB_TINY 48300a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE SLAB_NO_MERGE 48310a064316SJesper Dangaard Brouer #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */ 48320a064316SJesper Dangaard Brouer #define FLAG_SKB_NO_MERGE 0 48330a064316SJesper Dangaard Brouer #endif 48340a064316SJesper Dangaard Brouer 48351da177e4SLinus Torvalds void __init skb_init(void) 48361da177e4SLinus Torvalds { 4837025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", 48381da177e4SLinus Torvalds sizeof(struct sk_buff), 48391da177e4SLinus Torvalds 0, 48400a064316SJesper Dangaard Brouer SLAB_HWCACHE_ALIGN|SLAB_PANIC| 48410a064316SJesper Dangaard Brouer FLAG_SKB_NO_MERGE, 484279a8a642SKees Cook offsetof(struct sk_buff, cb), 484379a8a642SKees Cook sizeof_field(struct sk_buff, cb), 484420c2df83SPaul Mundt NULL); 4845d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4846d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4847d179cd12SDavid S. Miller 0, 4848e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 484920c2df83SPaul Mundt NULL); 48500b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. 48510b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head, 48520b34d680SEric Dumazet * and should not be copied to/from user. 48530b34d680SEric Dumazet */ 48540b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", 4855bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE, 4856bf9f1baaSEric Dumazet 0, 4857bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC, 48580b34d680SEric Dumazet 0, 48590b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM, 4860bf9f1baaSEric Dumazet NULL); 4861df5042f4SFlorian Westphal skb_extensions_init(); 48621da177e4SLinus Torvalds } 48631da177e4SLinus Torvalds 486451c739d1SDavid S. Miller static int 486548a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 486648a1df65SJason A. Donenfeld unsigned int recursion_level) 4867716ea3a7SDavid Howells { 48681a028e50SDavid S. Miller int start = skb_headlen(skb); 48691a028e50SDavid S. Miller int i, copy = start - offset; 4870fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4871716ea3a7SDavid Howells int elt = 0; 4872716ea3a7SDavid Howells 487348a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 487448a1df65SJason A. Donenfeld return -EMSGSIZE; 487548a1df65SJason A. Donenfeld 4876716ea3a7SDavid Howells if (copy > 0) { 4877716ea3a7SDavid Howells if (copy > len) 4878716ea3a7SDavid Howells copy = len; 4879642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4880716ea3a7SDavid Howells elt++; 4881716ea3a7SDavid Howells if ((len -= copy) == 0) 4882716ea3a7SDavid Howells return elt; 4883716ea3a7SDavid Howells offset += copy; 4884716ea3a7SDavid Howells } 4885716ea3a7SDavid Howells 4886716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 48871a028e50SDavid S. Miller int end; 4888716ea3a7SDavid Howells 4889547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48901a028e50SDavid S. Miller 48919e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4892716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4893716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 489448a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 489548a1df65SJason A. Donenfeld return -EMSGSIZE; 4896716ea3a7SDavid Howells 4897716ea3a7SDavid Howells if (copy > len) 4898716ea3a7SDavid Howells copy = len; 4899ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4900b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4901716ea3a7SDavid Howells elt++; 4902716ea3a7SDavid Howells if (!(len -= copy)) 4903716ea3a7SDavid Howells return elt; 4904716ea3a7SDavid Howells offset += copy; 4905716ea3a7SDavid Howells } 49061a028e50SDavid S. Miller start = end; 4907716ea3a7SDavid Howells } 4908716ea3a7SDavid Howells 4909fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 491048a1df65SJason A. Donenfeld int end, ret; 4911716ea3a7SDavid Howells 4912547b792cSIlpo Järvinen WARN_ON(start > offset + len); 49131a028e50SDavid S. Miller 4914fbb398a8SDavid S. Miller end = start + frag_iter->len; 4915716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 491648a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 491748a1df65SJason A. Donenfeld return -EMSGSIZE; 491848a1df65SJason A. Donenfeld 4919716ea3a7SDavid Howells if (copy > len) 4920716ea3a7SDavid Howells copy = len; 492148a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 492248a1df65SJason A. Donenfeld copy, recursion_level + 1); 492348a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 492448a1df65SJason A. Donenfeld return ret; 492548a1df65SJason A. Donenfeld elt += ret; 4926716ea3a7SDavid Howells if ((len -= copy) == 0) 4927716ea3a7SDavid Howells return elt; 4928716ea3a7SDavid Howells offset += copy; 4929716ea3a7SDavid Howells } 49301a028e50SDavid S. Miller start = end; 4931716ea3a7SDavid Howells } 4932716ea3a7SDavid Howells BUG_ON(len); 4933716ea3a7SDavid Howells return elt; 4934716ea3a7SDavid Howells } 4935716ea3a7SDavid Howells 493648a1df65SJason A. Donenfeld /** 493748a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 493848a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 493948a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 494048a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 494148a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 494248a1df65SJason A. Donenfeld * 494348a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 494448a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 494548a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 494648a1df65SJason A. Donenfeld * could not fit. 494748a1df65SJason A. Donenfeld */ 494848a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 494948a1df65SJason A. Donenfeld { 495048a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 495148a1df65SJason A. Donenfeld 495248a1df65SJason A. Donenfeld if (nsg <= 0) 495348a1df65SJason A. Donenfeld return nsg; 495448a1df65SJason A. Donenfeld 495548a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 495648a1df65SJason A. Donenfeld 495748a1df65SJason A. Donenfeld return nsg; 495848a1df65SJason A. Donenfeld } 495948a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 496048a1df65SJason A. Donenfeld 496125a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 496225a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 496325a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 496425a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 496525a91d8dSFan Du * 496625a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 496725a91d8dSFan Du * 1. sg_init_table 496825a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 496925a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 497025a91d8dSFan Du * 497125a91d8dSFan Du * This is equivalent to: 497225a91d8dSFan Du * 1. sg_init_table 497325a91d8dSFan Du * 2. skb_to_sgvec(payload1) 497425a91d8dSFan Du * 3. sg_unmark_end 497525a91d8dSFan Du * 4. skb_to_sgvec(payload2) 497625a91d8dSFan Du * 497725a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 497825a91d8dSFan Du * is more preferable. 497925a91d8dSFan Du */ 498025a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 498125a91d8dSFan Du int offset, int len) 498225a91d8dSFan Du { 498348a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 498425a91d8dSFan Du } 498525a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 498625a91d8dSFan Du 498751c739d1SDavid S. Miller 498851c739d1SDavid S. Miller 4989716ea3a7SDavid Howells /** 4990716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4991716ea3a7SDavid Howells * @skb: The socket buffer to check. 4992716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4993716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4994716ea3a7SDavid Howells * 4995716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4996716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4997716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4998716ea3a7SDavid Howells * 4999716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 5000716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 5001716ea3a7SDavid Howells * set to point to the skb in which this space begins. 5002716ea3a7SDavid Howells * 5003716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 5004716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 5005716ea3a7SDavid Howells */ 5006716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 5007716ea3a7SDavid Howells { 5008716ea3a7SDavid Howells int copyflag; 5009716ea3a7SDavid Howells int elt; 5010716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 5011716ea3a7SDavid Howells 5012716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 5013716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 5014716ea3a7SDavid Howells * at the moment even if they are anonymous). 5015716ea3a7SDavid Howells */ 5016716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 5017c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 5018716ea3a7SDavid Howells return -ENOMEM; 5019716ea3a7SDavid Howells 5020716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 502121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 5022716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 5023716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 5024716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 5025716ea3a7SDavid Howells * space, 128 bytes is fair. */ 5026716ea3a7SDavid Howells 5027716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 5028716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 5029716ea3a7SDavid Howells return -ENOMEM; 5030716ea3a7SDavid Howells 5031716ea3a7SDavid Howells /* Voila! */ 5032716ea3a7SDavid Howells *trailer = skb; 5033716ea3a7SDavid Howells return 1; 5034716ea3a7SDavid Howells } 5035716ea3a7SDavid Howells 5036716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 5037716ea3a7SDavid Howells 5038716ea3a7SDavid Howells elt = 1; 5039716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 5040716ea3a7SDavid Howells copyflag = 0; 5041716ea3a7SDavid Howells 5042716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 5043716ea3a7SDavid Howells int ntail = 0; 5044716ea3a7SDavid Howells 5045716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 5046716ea3a7SDavid Howells * this can happen on input. Copy it and everything 5047716ea3a7SDavid Howells * after it. */ 5048716ea3a7SDavid Howells 5049716ea3a7SDavid Howells if (skb_shared(skb1)) 5050716ea3a7SDavid Howells copyflag = 1; 5051716ea3a7SDavid Howells 5052716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 5053716ea3a7SDavid Howells 5054716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 5055716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 505621dc3301SDavid S. Miller skb_has_frag_list(skb1) || 5057716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 5058716ea3a7SDavid Howells ntail = tailbits + 128; 5059716ea3a7SDavid Howells } 5060716ea3a7SDavid Howells 5061716ea3a7SDavid Howells if (copyflag || 5062716ea3a7SDavid Howells skb_cloned(skb1) || 5063716ea3a7SDavid Howells ntail || 5064716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 506521dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 5066716ea3a7SDavid Howells struct sk_buff *skb2; 5067716ea3a7SDavid Howells 5068716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 5069716ea3a7SDavid Howells if (ntail == 0) 5070716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 5071716ea3a7SDavid Howells else 5072716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 5073716ea3a7SDavid Howells skb_headroom(skb1), 5074716ea3a7SDavid Howells ntail, 5075716ea3a7SDavid Howells GFP_ATOMIC); 5076716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 5077716ea3a7SDavid Howells return -ENOMEM; 5078716ea3a7SDavid Howells 5079716ea3a7SDavid Howells if (skb1->sk) 5080716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 5081716ea3a7SDavid Howells 5082716ea3a7SDavid Howells /* Looking around. Are we still alive? 5083716ea3a7SDavid Howells * OK, link new skb, drop old one */ 5084716ea3a7SDavid Howells 5085716ea3a7SDavid Howells skb2->next = skb1->next; 5086716ea3a7SDavid Howells *skb_p = skb2; 5087716ea3a7SDavid Howells kfree_skb(skb1); 5088716ea3a7SDavid Howells skb1 = skb2; 5089716ea3a7SDavid Howells } 5090716ea3a7SDavid Howells elt++; 5091716ea3a7SDavid Howells *trailer = skb1; 5092716ea3a7SDavid Howells skb_p = &skb1->next; 5093716ea3a7SDavid Howells } 5094716ea3a7SDavid Howells 5095716ea3a7SDavid Howells return elt; 5096716ea3a7SDavid Howells } 5097b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 5098716ea3a7SDavid Howells 5099b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 5100b1faf566SEric Dumazet { 5101b1faf566SEric Dumazet struct sock *sk = skb->sk; 5102b1faf566SEric Dumazet 5103b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 5104b1faf566SEric Dumazet } 5105b1faf566SEric Dumazet 51068605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 51078605330aSSoheil Hassas Yeganeh { 51088605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 51098605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 51108605330aSSoheil Hassas Yeganeh */ 51118605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 51128605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 51138605330aSSoheil Hassas Yeganeh } 51148605330aSSoheil Hassas Yeganeh 5115b1faf566SEric Dumazet /* 5116b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 5117b1faf566SEric Dumazet */ 5118b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 5119b1faf566SEric Dumazet { 5120b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 5121ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 5122b1faf566SEric Dumazet return -ENOMEM; 5123b1faf566SEric Dumazet 5124b1faf566SEric Dumazet skb_orphan(skb); 5125b1faf566SEric Dumazet skb->sk = sk; 5126b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 5127b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 51288605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 5129b1faf566SEric Dumazet 5130abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 5131abb57ea4SEric Dumazet skb_dst_force(skb); 5132abb57ea4SEric Dumazet 5133b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 5134b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 5135e3ae2365SAlexander Aring sk_error_report(sk); 5136b1faf566SEric Dumazet return 0; 5137b1faf566SEric Dumazet } 5138b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 5139b1faf566SEric Dumazet 514083a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 514183a1a1a7SSoheil Hassas Yeganeh { 514283a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 514383a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 514483a1a1a7SSoheil Hassas Yeganeh } 514583a1a1a7SSoheil Hassas Yeganeh 5146364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 5147364a9e93SWillem de Bruijn { 5148364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 514983a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 515083a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 5151997d5c3fSEric Dumazet unsigned long flags; 5152364a9e93SWillem de Bruijn 5153997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 5154364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 515538b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 515683a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 515738b25793SSoheil Hassas Yeganeh if (icmp_next) 5158985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 515938b25793SSoheil Hassas Yeganeh } 5160997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 5161364a9e93SWillem de Bruijn 516283a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 516383a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 516483a1a1a7SSoheil Hassas Yeganeh 516583a1a1a7SSoheil Hassas Yeganeh if (skb_next) 5166e3ae2365SAlexander Aring sk_error_report(sk); 5167364a9e93SWillem de Bruijn 5168364a9e93SWillem de Bruijn return skb; 5169364a9e93SWillem de Bruijn } 5170364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 5171364a9e93SWillem de Bruijn 5172cab41c47SAlexander Duyck /** 5173cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 5174cab41c47SAlexander Duyck * @skb: the skb to clone 5175cab41c47SAlexander Duyck * 5176cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 5177cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 5178cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 5179cab41c47SAlexander Duyck * 5180cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 5181cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 5182cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 5183cab41c47SAlexander Duyck * the sk_error_queue. 5184cab41c47SAlexander Duyck */ 518562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 518662bccb8cSAlexander Duyck { 518762bccb8cSAlexander Duyck struct sock *sk = skb->sk; 518862bccb8cSAlexander Duyck struct sk_buff *clone; 518962bccb8cSAlexander Duyck 519041c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 519162bccb8cSAlexander Duyck return NULL; 519262bccb8cSAlexander Duyck 519362bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 519462bccb8cSAlexander Duyck if (!clone) { 519562bccb8cSAlexander Duyck sock_put(sk); 519662bccb8cSAlexander Duyck return NULL; 519762bccb8cSAlexander Duyck } 519862bccb8cSAlexander Duyck 519962bccb8cSAlexander Duyck clone->sk = sk; 520062bccb8cSAlexander Duyck clone->destructor = sock_efree; 520162bccb8cSAlexander Duyck 520262bccb8cSAlexander Duyck return clone; 520362bccb8cSAlexander Duyck } 520462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 520562bccb8cSAlexander Duyck 520637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 520737846ef0SAlexander Duyck struct sock *sk, 52084ef1b286SSoheil Hassas Yeganeh int tstype, 52094ef1b286SSoheil Hassas Yeganeh bool opt_stats) 5210ac45f602SPatrick Ohly { 5211ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 5212ac45f602SPatrick Ohly int err; 5213ac45f602SPatrick Ohly 52144ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 52154ef1b286SSoheil Hassas Yeganeh 5216ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5217ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5218ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5219ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5220e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 52214ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 52221862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 5223e3390b30SEric Dumazet if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) { 522409c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 522542f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5226a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 52274ed2d765SWillem de Bruijn } 522829030374SEric Dumazet 5229ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 523029030374SEric Dumazet 5231ac45f602SPatrick Ohly if (err) 5232ac45f602SPatrick Ohly kfree_skb(skb); 5233ac45f602SPatrick Ohly } 523437846ef0SAlexander Duyck 5235b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5236b245be1fSWillem de Bruijn { 5237b245be1fSWillem de Bruijn bool ret; 5238b245be1fSWillem de Bruijn 5239d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5240b245be1fSWillem de Bruijn return true; 5241b245be1fSWillem de Bruijn 5242b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5243b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5244b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5245b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5246b245be1fSWillem de Bruijn return ret; 5247b245be1fSWillem de Bruijn } 5248b245be1fSWillem de Bruijn 524937846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 525037846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 525137846ef0SAlexander Duyck { 525237846ef0SAlexander Duyck struct sock *sk = skb->sk; 525337846ef0SAlexander Duyck 5254b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 525535b99dffSWillem de Bruijn goto err; 5256b245be1fSWillem de Bruijn 52579ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 52589ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 52599ac25fc0SEric Dumazet */ 526041c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 526137846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 52624ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 526337846ef0SAlexander Duyck sock_put(sk); 526435b99dffSWillem de Bruijn return; 526537846ef0SAlexander Duyck } 526635b99dffSWillem de Bruijn 526735b99dffSWillem de Bruijn err: 526835b99dffSWillem de Bruijn kfree_skb(skb); 52699ac25fc0SEric Dumazet } 527037846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 527137846ef0SAlexander Duyck 527237846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5273e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 527437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 527537846ef0SAlexander Duyck struct sock *sk, int tstype) 527637846ef0SAlexander Duyck { 527737846ef0SAlexander Duyck struct sk_buff *skb; 52784ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 5279e3390b30SEric Dumazet u32 tsflags; 528037846ef0SAlexander Duyck 52813a8dd971SWillem de Bruijn if (!sk) 52823a8dd971SWillem de Bruijn return; 52833a8dd971SWillem de Bruijn 5284e3390b30SEric Dumazet tsflags = READ_ONCE(sk->sk_tsflags); 5285e3390b30SEric Dumazet if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5286b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5287b50a5c70SMiroslav Lichvar return; 5288b50a5c70SMiroslav Lichvar 5289e3390b30SEric Dumazet tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 52903a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 529137846ef0SAlexander Duyck return; 529237846ef0SAlexander Duyck 52931c885808SFrancis Yan if (tsonly) { 52941c885808SFrancis Yan #ifdef CONFIG_INET 5295e3390b30SEric Dumazet if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) && 529642f67eeaSEric Dumazet sk_is_tcp(sk)) { 5297e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5298e7ed11eeSYousuk Seung ack_skb); 52994ef1b286SSoheil Hassas Yeganeh opt_stats = true; 53004ef1b286SSoheil Hassas Yeganeh } else 53011c885808SFrancis Yan #endif 53021c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 53031c885808SFrancis Yan } else { 530437846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 530550749f2dSKuniyuki Iwashima 53068a02fb71SPratyush Yadav if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) { 53078a02fb71SPratyush Yadav kfree_skb(skb); 530850749f2dSKuniyuki Iwashima return; 53091c885808SFrancis Yan } 53108a02fb71SPratyush Yadav } 531137846ef0SAlexander Duyck if (!skb) 531237846ef0SAlexander Duyck return; 531337846ef0SAlexander Duyck 531449ca0d8bSWillem de Bruijn if (tsonly) { 5315fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5316fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 531749ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 531849ca0d8bSWillem de Bruijn } 531949ca0d8bSWillem de Bruijn 532049ca0d8bSWillem de Bruijn if (hwtstamps) 532149ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 532249ca0d8bSWillem de Bruijn else 5323d93376f5SMartin KaFai Lau __net_timestamp(skb); 532449ca0d8bSWillem de Bruijn 53254ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 532637846ef0SAlexander Duyck } 5327e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5328e7fd2885SWillem de Bruijn 5329e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5330e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5331e7fd2885SWillem de Bruijn { 5332e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5333e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5334e7fd2885SWillem de Bruijn } 5335ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5336ac45f602SPatrick Ohly 5337eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS 53386e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 53396e3e939fSJohannes Berg { 53406e3e939fSJohannes Berg struct sock *sk = skb->sk; 53416e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5342dd4f1072SEric Dumazet int err = 1; 53436e3e939fSJohannes Berg 53446e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 53456e3e939fSJohannes Berg skb->wifi_acked = acked; 53466e3e939fSJohannes Berg 53476e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 53486e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 53496e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 53506e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 53516e3e939fSJohannes Berg 5352dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5353dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5354dd4f1072SEric Dumazet */ 535541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 53566e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5357dd4f1072SEric Dumazet sock_put(sk); 5358dd4f1072SEric Dumazet } 53596e3e939fSJohannes Berg if (err) 53606e3e939fSJohannes Berg kfree_skb(skb); 53616e3e939fSJohannes Berg } 53626e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 5363eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */ 53646e3e939fSJohannes Berg 5365f35d9d8aSRusty Russell /** 5366f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5367f35d9d8aSRusty Russell * @skb: the skb to set 5368f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5369f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5370f35d9d8aSRusty Russell * 5371f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5372f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5373f35d9d8aSRusty Russell * 5374f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5375f35d9d8aSRusty Russell * returns false you should drop the packet. 5376f35d9d8aSRusty Russell */ 5377f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5378f35d9d8aSRusty Russell { 537952b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 538052b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 538152b5d6f5SEric Dumazet 5382424f8416SEric Dumazet if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) { 538352b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 538452b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5385f35d9d8aSRusty Russell return false; 5386f35d9d8aSRusty Russell } 5387f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 538852b5d6f5SEric Dumazet skb->csum_start = csum_start; 5389f35d9d8aSRusty Russell skb->csum_offset = off; 5390424f8416SEric Dumazet skb->transport_header = csum_start; 5391f35d9d8aSRusty Russell return true; 5392f35d9d8aSRusty Russell } 5393b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5394f35d9d8aSRusty Russell 5395ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5396ed1f50c3SPaul Durrant unsigned int max) 5397ed1f50c3SPaul Durrant { 5398ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5399ed1f50c3SPaul Durrant return 0; 5400ed1f50c3SPaul Durrant 5401ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5402ed1f50c3SPaul Durrant * won't need to do it again. 5403ed1f50c3SPaul Durrant */ 5404ed1f50c3SPaul Durrant if (max > skb->len) 5405ed1f50c3SPaul Durrant max = skb->len; 5406ed1f50c3SPaul Durrant 5407ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5408ed1f50c3SPaul Durrant return -ENOMEM; 5409ed1f50c3SPaul Durrant 5410ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5411ed1f50c3SPaul Durrant return -EPROTO; 5412ed1f50c3SPaul Durrant 5413ed1f50c3SPaul Durrant return 0; 5414ed1f50c3SPaul Durrant } 5415ed1f50c3SPaul Durrant 5416f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5417f9708b43SJan Beulich 5418f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5419f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5420f9708b43SJan Beulich unsigned int off) 5421f9708b43SJan Beulich { 5422f9708b43SJan Beulich int err; 5423f9708b43SJan Beulich 5424161d1792SKees Cook switch (proto) { 5425f9708b43SJan Beulich case IPPROTO_TCP: 5426f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5427f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5428f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5429f9708b43SJan Beulich offsetof(struct tcphdr, 5430f9708b43SJan Beulich check))) 5431f9708b43SJan Beulich err = -EPROTO; 5432f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5433f9708b43SJan Beulich 5434f9708b43SJan Beulich case IPPROTO_UDP: 5435f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5436f9708b43SJan Beulich off + sizeof(struct udphdr)); 5437f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5438f9708b43SJan Beulich offsetof(struct udphdr, 5439f9708b43SJan Beulich check))) 5440f9708b43SJan Beulich err = -EPROTO; 5441f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5442f9708b43SJan Beulich } 5443f9708b43SJan Beulich 5444f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5445f9708b43SJan Beulich } 5446f9708b43SJan Beulich 5447ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5448ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5449ed1f50c3SPaul Durrant */ 5450ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5451ed1f50c3SPaul Durrant 5452f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5453ed1f50c3SPaul Durrant { 5454ed1f50c3SPaul Durrant unsigned int off; 5455ed1f50c3SPaul Durrant bool fragment; 5456f9708b43SJan Beulich __sum16 *csum; 5457ed1f50c3SPaul Durrant int err; 5458ed1f50c3SPaul Durrant 5459ed1f50c3SPaul Durrant fragment = false; 5460ed1f50c3SPaul Durrant 5461ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5462ed1f50c3SPaul Durrant sizeof(struct iphdr), 5463ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5464ed1f50c3SPaul Durrant if (err < 0) 5465ed1f50c3SPaul Durrant goto out; 5466ed1f50c3SPaul Durrant 546711f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5468ed1f50c3SPaul Durrant fragment = true; 5469ed1f50c3SPaul Durrant 5470ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5471ed1f50c3SPaul Durrant 5472ed1f50c3SPaul Durrant err = -EPROTO; 5473ed1f50c3SPaul Durrant 5474ed1f50c3SPaul Durrant if (fragment) 5475ed1f50c3SPaul Durrant goto out; 5476ed1f50c3SPaul Durrant 5477f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5478f9708b43SJan Beulich if (IS_ERR(csum)) 5479f9708b43SJan Beulich return PTR_ERR(csum); 5480ed1f50c3SPaul Durrant 5481ed1f50c3SPaul Durrant if (recalculate) 5482f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5483ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5484ed1f50c3SPaul Durrant skb->len - off, 5485f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5486ed1f50c3SPaul Durrant err = 0; 5487ed1f50c3SPaul Durrant 5488ed1f50c3SPaul Durrant out: 5489ed1f50c3SPaul Durrant return err; 5490ed1f50c3SPaul Durrant } 5491ed1f50c3SPaul Durrant 5492ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5493ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5494ed1f50c3SPaul Durrant */ 5495ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5496ed1f50c3SPaul Durrant 5497ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5498ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5499ed1f50c3SPaul Durrant 5500ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5501ed1f50c3SPaul Durrant { 5502ed1f50c3SPaul Durrant int err; 5503ed1f50c3SPaul Durrant u8 nexthdr; 5504ed1f50c3SPaul Durrant unsigned int off; 5505ed1f50c3SPaul Durrant unsigned int len; 5506ed1f50c3SPaul Durrant bool fragment; 5507ed1f50c3SPaul Durrant bool done; 5508f9708b43SJan Beulich __sum16 *csum; 5509ed1f50c3SPaul Durrant 5510ed1f50c3SPaul Durrant fragment = false; 5511ed1f50c3SPaul Durrant done = false; 5512ed1f50c3SPaul Durrant 5513ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5514ed1f50c3SPaul Durrant 5515ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5516ed1f50c3SPaul Durrant if (err < 0) 5517ed1f50c3SPaul Durrant goto out; 5518ed1f50c3SPaul Durrant 5519ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5520ed1f50c3SPaul Durrant 5521ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5522ed1f50c3SPaul Durrant while (off <= len && !done) { 5523ed1f50c3SPaul Durrant switch (nexthdr) { 5524ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5525ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5526ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5527ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5528ed1f50c3SPaul Durrant 5529ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5530ed1f50c3SPaul Durrant off + 5531ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5532ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5533ed1f50c3SPaul Durrant if (err < 0) 5534ed1f50c3SPaul Durrant goto out; 5535ed1f50c3SPaul Durrant 5536ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5537ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5538ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5539ed1f50c3SPaul Durrant break; 5540ed1f50c3SPaul Durrant } 5541ed1f50c3SPaul Durrant case IPPROTO_AH: { 5542ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5543ed1f50c3SPaul Durrant 5544ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5545ed1f50c3SPaul Durrant off + 5546ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5547ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5548ed1f50c3SPaul Durrant if (err < 0) 5549ed1f50c3SPaul Durrant goto out; 5550ed1f50c3SPaul Durrant 5551ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5552ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5553ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5554ed1f50c3SPaul Durrant break; 5555ed1f50c3SPaul Durrant } 5556ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5557ed1f50c3SPaul Durrant struct frag_hdr *hp; 5558ed1f50c3SPaul Durrant 5559ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5560ed1f50c3SPaul Durrant off + 5561ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5562ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5563ed1f50c3SPaul Durrant if (err < 0) 5564ed1f50c3SPaul Durrant goto out; 5565ed1f50c3SPaul Durrant 5566ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5567ed1f50c3SPaul Durrant 5568ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5569ed1f50c3SPaul Durrant fragment = true; 5570ed1f50c3SPaul Durrant 5571ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5572ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5573ed1f50c3SPaul Durrant break; 5574ed1f50c3SPaul Durrant } 5575ed1f50c3SPaul Durrant default: 5576ed1f50c3SPaul Durrant done = true; 5577ed1f50c3SPaul Durrant break; 5578ed1f50c3SPaul Durrant } 5579ed1f50c3SPaul Durrant } 5580ed1f50c3SPaul Durrant 5581ed1f50c3SPaul Durrant err = -EPROTO; 5582ed1f50c3SPaul Durrant 5583ed1f50c3SPaul Durrant if (!done || fragment) 5584ed1f50c3SPaul Durrant goto out; 5585ed1f50c3SPaul Durrant 5586f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5587f9708b43SJan Beulich if (IS_ERR(csum)) 5588f9708b43SJan Beulich return PTR_ERR(csum); 5589ed1f50c3SPaul Durrant 5590ed1f50c3SPaul Durrant if (recalculate) 5591f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5592ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5593f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5594ed1f50c3SPaul Durrant err = 0; 5595ed1f50c3SPaul Durrant 5596ed1f50c3SPaul Durrant out: 5597ed1f50c3SPaul Durrant return err; 5598ed1f50c3SPaul Durrant } 5599ed1f50c3SPaul Durrant 5600ed1f50c3SPaul Durrant /** 5601ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5602ed1f50c3SPaul Durrant * @skb: the skb to set up 5603ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5604ed1f50c3SPaul Durrant */ 5605ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5606ed1f50c3SPaul Durrant { 5607ed1f50c3SPaul Durrant int err; 5608ed1f50c3SPaul Durrant 5609ed1f50c3SPaul Durrant switch (skb->protocol) { 5610ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5611f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5612ed1f50c3SPaul Durrant break; 5613ed1f50c3SPaul Durrant 5614ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5615ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5616ed1f50c3SPaul Durrant break; 5617ed1f50c3SPaul Durrant 5618ed1f50c3SPaul Durrant default: 5619ed1f50c3SPaul Durrant err = -EPROTO; 5620ed1f50c3SPaul Durrant break; 5621ed1f50c3SPaul Durrant } 5622ed1f50c3SPaul Durrant 5623ed1f50c3SPaul Durrant return err; 5624ed1f50c3SPaul Durrant } 5625ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5626ed1f50c3SPaul Durrant 56279afd85c9SLinus Lüssing /** 56289afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 56299afd85c9SLinus Lüssing * @skb: the skb to check 56309afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 56319afd85c9SLinus Lüssing * 56329afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 56339afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 56349afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 56359afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 56369afd85c9SLinus Lüssing * 5637a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5638a516993fSLinus Lüssing * differs from the provided skb. 56399afd85c9SLinus Lüssing */ 56409afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 56419afd85c9SLinus Lüssing unsigned int transport_len) 56429afd85c9SLinus Lüssing { 56439afd85c9SLinus Lüssing struct sk_buff *skb_chk; 56449afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 56459afd85c9SLinus Lüssing int ret; 56469afd85c9SLinus Lüssing 5647a516993fSLinus Lüssing if (skb->len < len) 56489afd85c9SLinus Lüssing return NULL; 5649a516993fSLinus Lüssing else if (skb->len == len) 56509afd85c9SLinus Lüssing return skb; 56519afd85c9SLinus Lüssing 56529afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 56539afd85c9SLinus Lüssing if (!skb_chk) 56549afd85c9SLinus Lüssing return NULL; 56559afd85c9SLinus Lüssing 56569afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 56579afd85c9SLinus Lüssing if (ret) { 56589afd85c9SLinus Lüssing kfree_skb(skb_chk); 56599afd85c9SLinus Lüssing return NULL; 56609afd85c9SLinus Lüssing } 56619afd85c9SLinus Lüssing 56629afd85c9SLinus Lüssing return skb_chk; 56639afd85c9SLinus Lüssing } 56649afd85c9SLinus Lüssing 56659afd85c9SLinus Lüssing /** 56669afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 56679afd85c9SLinus Lüssing * @skb: the skb to check 56689afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 56699afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 56709afd85c9SLinus Lüssing * 56719afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 56729afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 56739afd85c9SLinus Lüssing * 56749afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 56759afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 56769afd85c9SLinus Lüssing * 5677a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5678a516993fSLinus Lüssing * differs from the provided skb. 56799afd85c9SLinus Lüssing */ 56809afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 56819afd85c9SLinus Lüssing unsigned int transport_len, 56829afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 56839afd85c9SLinus Lüssing { 56849afd85c9SLinus Lüssing struct sk_buff *skb_chk; 56859afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5686fcba67c9SLinus Lüssing __sum16 ret; 56879afd85c9SLinus Lüssing 56889afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 56899afd85c9SLinus Lüssing if (!skb_chk) 5690a516993fSLinus Lüssing goto err; 56919afd85c9SLinus Lüssing 5692a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5693a516993fSLinus Lüssing goto err; 56949afd85c9SLinus Lüssing 56959b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 56969afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 56979b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 56989afd85c9SLinus Lüssing 5699a516993fSLinus Lüssing if (ret) 5700a516993fSLinus Lüssing goto err; 57019afd85c9SLinus Lüssing 57029afd85c9SLinus Lüssing return skb_chk; 5703a516993fSLinus Lüssing 5704a516993fSLinus Lüssing err: 5705a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5706a516993fSLinus Lüssing kfree_skb(skb_chk); 5707a516993fSLinus Lüssing 5708a516993fSLinus Lüssing return NULL; 5709a516993fSLinus Lüssing 57109afd85c9SLinus Lüssing } 57119afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 57129afd85c9SLinus Lüssing 57134497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 57144497b076SBen Hutchings { 5715e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5716e87cc472SJoe Perches skb->dev->name); 57174497b076SBen Hutchings } 57184497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5719bad43ca8SEric Dumazet 5720bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5721bad43ca8SEric Dumazet { 57223d861f66SEric Dumazet if (head_stolen) { 57233d861f66SEric Dumazet skb_release_head_state(skb); 5724025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 57253d861f66SEric Dumazet } else { 5726bad43ca8SEric Dumazet __kfree_skb(skb); 5727bad43ca8SEric Dumazet } 57283d861f66SEric Dumazet } 5729bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5730bad43ca8SEric Dumazet 5731bad43ca8SEric Dumazet /** 5732bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5733bad43ca8SEric Dumazet * @to: prior buffer 5734bad43ca8SEric Dumazet * @from: buffer to add 5735bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5736c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5737bad43ca8SEric Dumazet */ 5738bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5739bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5740bad43ca8SEric Dumazet { 5741c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5742bad43ca8SEric Dumazet int i, delta, len = from->len; 5743bad43ca8SEric Dumazet 5744bad43ca8SEric Dumazet *fragstolen = false; 5745bad43ca8SEric Dumazet 5746bad43ca8SEric Dumazet if (skb_cloned(to)) 5747bad43ca8SEric Dumazet return false; 5748bad43ca8SEric Dumazet 57490646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated 57500646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones 57510646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment 57521effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 57531effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 57541effe8caSJean-Philippe Brucker * inconsistent reference counts. 57550646dc31SLiang Chen * In theory we could take full references if @from is cloned and 57560646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with 57570646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with. 57586a5bcd84SIlias Apalodimas */ 57590646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle || 57600646dc31SLiang Chen (from->pp_recycle && skb_cloned(from))) 57616a5bcd84SIlias Apalodimas return false; 57626a5bcd84SIlias Apalodimas 5763bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5764e93a0435SEric Dumazet if (len) 5765bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5766bad43ca8SEric Dumazet *delta_truesize = 0; 5767bad43ca8SEric Dumazet return true; 5768bad43ca8SEric Dumazet } 5769bad43ca8SEric Dumazet 5770c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5771c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5772c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5773bad43ca8SEric Dumazet return false; 57741f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 57751f8b977aSWillem de Bruijn return false; 5776bad43ca8SEric Dumazet 5777bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5778bad43ca8SEric Dumazet struct page *page; 5779bad43ca8SEric Dumazet unsigned int offset; 5780bad43ca8SEric Dumazet 5781c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5782c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5783bad43ca8SEric Dumazet return false; 5784bad43ca8SEric Dumazet 5785bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5786bad43ca8SEric Dumazet return false; 5787bad43ca8SEric Dumazet 5788bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5789bad43ca8SEric Dumazet 5790bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5791bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5792bad43ca8SEric Dumazet 5793c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5794bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5795bad43ca8SEric Dumazet *fragstolen = true; 5796bad43ca8SEric Dumazet } else { 5797c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5798c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5799bad43ca8SEric Dumazet return false; 5800bad43ca8SEric Dumazet 5801f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5802bad43ca8SEric Dumazet } 5803bad43ca8SEric Dumazet 5804bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5805bad43ca8SEric Dumazet 5806c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5807c818fa9eSEric Dumazet from_shinfo->frags, 5808c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5809c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5810bad43ca8SEric Dumazet 5811bad43ca8SEric Dumazet if (!skb_cloned(from)) 5812c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5813bad43ca8SEric Dumazet 58148ea853fdSLi RongQing /* if the skb is not cloned this does nothing 58158ea853fdSLi RongQing * since we set nr_frags to 0. 58168ea853fdSLi RongQing */ 5817c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5818c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5819bad43ca8SEric Dumazet 5820bad43ca8SEric Dumazet to->truesize += delta; 5821bad43ca8SEric Dumazet to->len += len; 5822bad43ca8SEric Dumazet to->data_len += len; 5823bad43ca8SEric Dumazet 5824bad43ca8SEric Dumazet *delta_truesize = delta; 5825bad43ca8SEric Dumazet return true; 5826bad43ca8SEric Dumazet } 5827bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5828621e84d6SNicolas Dichtel 5829621e84d6SNicolas Dichtel /** 58308b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5831621e84d6SNicolas Dichtel * 5832621e84d6SNicolas Dichtel * @skb: buffer to clean 58338b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5834621e84d6SNicolas Dichtel * 58358b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 58368b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 58378b27f277SNicolas Dichtel * operations. 58388b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 58398b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 58408b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5841621e84d6SNicolas Dichtel */ 58428b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5843621e84d6SNicolas Dichtel { 5844621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5845621e84d6SNicolas Dichtel skb->skb_iif = 0; 584660ff7467SWANG Cong skb->ignore_df = 0; 5847621e84d6SNicolas Dichtel skb_dst_drop(skb); 5848174e2381SFlorian Westphal skb_ext_reset(skb); 5849895b5c9fSFlorian Westphal nf_reset_ct(skb); 5850621e84d6SNicolas Dichtel nf_reset_trace(skb); 5851213dd74aSHerbert Xu 58526f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 58536f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5854875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 58556f9a5069SPetr Machata #endif 58566f9a5069SPetr Machata 5857213dd74aSHerbert Xu if (!xnet) 5858213dd74aSHerbert Xu return; 5859213dd74aSHerbert Xu 58602b5ec1a5SYe Yin ipvs_reset(skb); 5861213dd74aSHerbert Xu skb->mark = 0; 5862de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5863621e84d6SNicolas Dichtel } 5864621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5865de960aa9SFlorian Westphal 58660d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 58670d5501c1SVlad Yasevich { 5868d85e8be2SYuya Kusakabe int mac_len, meta_len; 5869d85e8be2SYuya Kusakabe void *meta; 58704bbb3e0eSToshiaki Makita 58710d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 58720d5501c1SVlad Yasevich kfree_skb(skb); 58730d5501c1SVlad Yasevich return NULL; 58740d5501c1SVlad Yasevich } 58750d5501c1SVlad Yasevich 58764bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5877ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 58784bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 58794bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5880ae474573SToshiaki Makita } 5881d85e8be2SYuya Kusakabe 5882d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5883d85e8be2SYuya Kusakabe if (meta_len) { 5884d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5885d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5886d85e8be2SYuya Kusakabe } 5887d85e8be2SYuya Kusakabe 58880d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 58890d5501c1SVlad Yasevich return skb; 58900d5501c1SVlad Yasevich } 58910d5501c1SVlad Yasevich 58920d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 58930d5501c1SVlad Yasevich { 58940d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 58950d5501c1SVlad Yasevich u16 vlan_tci; 58960d5501c1SVlad Yasevich 5897df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 58980d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 58990d5501c1SVlad Yasevich return skb; 59000d5501c1SVlad Yasevich } 59010d5501c1SVlad Yasevich 59020d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 59030d5501c1SVlad Yasevich if (unlikely(!skb)) 59040d5501c1SVlad Yasevich goto err_free; 590555eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 590655eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 59070d5501c1SVlad Yasevich goto err_free; 59080d5501c1SVlad Yasevich 59090d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 59100d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 59110d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 59120d5501c1SVlad Yasevich 59130d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 59140d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 59150d5501c1SVlad Yasevich 59160d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 59170d5501c1SVlad Yasevich if (unlikely(!skb)) 59180d5501c1SVlad Yasevich goto err_free; 59190d5501c1SVlad Yasevich 59200d5501c1SVlad Yasevich skb_reset_network_header(skb); 59218be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 59220d5501c1SVlad Yasevich skb_reset_transport_header(skb); 59230d5501c1SVlad Yasevich skb_reset_mac_len(skb); 59240d5501c1SVlad Yasevich 59250d5501c1SVlad Yasevich return skb; 59260d5501c1SVlad Yasevich 59270d5501c1SVlad Yasevich err_free: 59280d5501c1SVlad Yasevich kfree_skb(skb); 59290d5501c1SVlad Yasevich return NULL; 59300d5501c1SVlad Yasevich } 59310d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 59322e4e4410SEric Dumazet 593392ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5934e2195121SJiri Pirko { 5935e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5936e2195121SJiri Pirko return -ENOMEM; 5937e2195121SJiri Pirko 5938e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5939e2195121SJiri Pirko return 0; 5940e2195121SJiri Pirko 5941e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5942e2195121SJiri Pirko } 5943e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5944e2195121SJiri Pirko 5945bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5946bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5947bfca4c52SShmulik Ladkani */ 5948bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 594993515d53SJiri Pirko { 5950b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 595193515d53SJiri Pirko int err; 595293515d53SJiri Pirko 5953b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5954b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5955b6a79208SShmulik Ladkani offset)) { 5956b6a79208SShmulik Ladkani return -EINVAL; 5957b6a79208SShmulik Ladkani } 5958b6a79208SShmulik Ladkani 595993515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 596093515d53SJiri Pirko if (unlikely(err)) 5961b6a79208SShmulik Ladkani return err; 596293515d53SJiri Pirko 596393515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 596493515d53SJiri Pirko 59650bcf2e4aSVladimir Oltean vlan_remove_tag(skb, vlan_tci); 596693515d53SJiri Pirko 596793515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 596893515d53SJiri Pirko 596993515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 597093515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 597193515d53SJiri Pirko 597293515d53SJiri Pirko skb_reset_mac_len(skb); 597393515d53SJiri Pirko 597493515d53SJiri Pirko return err; 597593515d53SJiri Pirko } 5976bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 597793515d53SJiri Pirko 5978b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5979b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5980b6a79208SShmulik Ladkani */ 598193515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 598293515d53SJiri Pirko { 598393515d53SJiri Pirko u16 vlan_tci; 598493515d53SJiri Pirko __be16 vlan_proto; 598593515d53SJiri Pirko int err; 598693515d53SJiri Pirko 5987df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5988b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 598993515d53SJiri Pirko } else { 5990ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 599193515d53SJiri Pirko return 0; 599293515d53SJiri Pirko 599393515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 599493515d53SJiri Pirko if (err) 599593515d53SJiri Pirko return err; 599693515d53SJiri Pirko } 599793515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5998ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 599993515d53SJiri Pirko return 0; 600093515d53SJiri Pirko 600193515d53SJiri Pirko vlan_proto = skb->protocol; 600293515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 600393515d53SJiri Pirko if (unlikely(err)) 600493515d53SJiri Pirko return err; 600593515d53SJiri Pirko 600693515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 600793515d53SJiri Pirko return 0; 600893515d53SJiri Pirko } 600993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 601093515d53SJiri Pirko 6011b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 6012b6a79208SShmulik Ladkani * Expects skb->data at mac header. 6013b6a79208SShmulik Ladkani */ 601493515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 601593515d53SJiri Pirko { 6016df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 6017b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 601893515d53SJiri Pirko int err; 601993515d53SJiri Pirko 6020b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 6021b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 6022b6a79208SShmulik Ladkani offset)) { 6023b6a79208SShmulik Ladkani return -EINVAL; 6024b6a79208SShmulik Ladkani } 6025b6a79208SShmulik Ladkani 602693515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 6027df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 6028b6a79208SShmulik Ladkani if (err) 602993515d53SJiri Pirko return err; 60309241e2dfSDaniel Borkmann 603193515d53SJiri Pirko skb->protocol = skb->vlan_proto; 603293515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 603393515d53SJiri Pirko 60346b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 603593515d53SJiri Pirko } 603693515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 603793515d53SJiri Pirko return 0; 603893515d53SJiri Pirko } 603993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 604093515d53SJiri Pirko 604119fbcb36SGuillaume Nault /** 604219fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 604319fbcb36SGuillaume Nault * 604419fbcb36SGuillaume Nault * @skb: Socket buffer to modify 604519fbcb36SGuillaume Nault * 604619fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 604719fbcb36SGuillaume Nault * 604819fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 604919fbcb36SGuillaume Nault * present. 605019fbcb36SGuillaume Nault * 605119fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 605219fbcb36SGuillaume Nault */ 605319fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 605419fbcb36SGuillaume Nault { 605519fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 605619fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 605719fbcb36SGuillaume Nault return -EPROTO; 605819fbcb36SGuillaume Nault 605919fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 606019fbcb36SGuillaume Nault skb_reset_mac_header(skb); 606119fbcb36SGuillaume Nault skb_reset_mac_len(skb); 606219fbcb36SGuillaume Nault 606319fbcb36SGuillaume Nault return 0; 606419fbcb36SGuillaume Nault } 606519fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 606619fbcb36SGuillaume Nault 606719fbcb36SGuillaume Nault /** 606819fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 606919fbcb36SGuillaume Nault * 607019fbcb36SGuillaume Nault * @skb: Socket buffer to modify 607119fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 607219fbcb36SGuillaume Nault * @src: Source MAC address of the new header 607319fbcb36SGuillaume Nault * 607419fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 607519fbcb36SGuillaume Nault * 607619fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 607719fbcb36SGuillaume Nault * 607819fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 607919fbcb36SGuillaume Nault */ 608019fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 608119fbcb36SGuillaume Nault const unsigned char *src) 608219fbcb36SGuillaume Nault { 608319fbcb36SGuillaume Nault struct ethhdr *eth; 608419fbcb36SGuillaume Nault int err; 608519fbcb36SGuillaume Nault 608619fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 608719fbcb36SGuillaume Nault return -EPROTO; 608819fbcb36SGuillaume Nault 608919fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 609019fbcb36SGuillaume Nault if (err < 0) 609119fbcb36SGuillaume Nault return err; 609219fbcb36SGuillaume Nault 609319fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 609419fbcb36SGuillaume Nault skb_reset_mac_header(skb); 609519fbcb36SGuillaume Nault skb_reset_mac_len(skb); 609619fbcb36SGuillaume Nault 609719fbcb36SGuillaume Nault eth = eth_hdr(skb); 609819fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 609919fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 610019fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 610119fbcb36SGuillaume Nault 610219fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 610319fbcb36SGuillaume Nault 610419fbcb36SGuillaume Nault return 0; 610519fbcb36SGuillaume Nault } 610619fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 610719fbcb36SGuillaume Nault 61088822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 61098822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 61108822e270SJohn Hurley __be16 ethertype) 61118822e270SJohn Hurley { 61128822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 61138822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 61148822e270SJohn Hurley 61158822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 61168822e270SJohn Hurley } 61178822e270SJohn Hurley 61188822e270SJohn Hurley hdr->h_proto = ethertype; 61198822e270SJohn Hurley } 61208822e270SJohn Hurley 61218822e270SJohn Hurley /** 6122e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6123e7dbfed1SMartin Varghese * the packet 61248822e270SJohn Hurley * 61258822e270SJohn Hurley * @skb: buffer 61268822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 61278822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6128fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6129e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6130e7dbfed1SMartin Varghese * ethernet 61318822e270SJohn Hurley * 61328822e270SJohn Hurley * Expects skb->data at mac header. 61338822e270SJohn Hurley * 61348822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 61358822e270SJohn Hurley */ 6136fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6137d04ac224SMartin Varghese int mac_len, bool ethernet) 61388822e270SJohn Hurley { 61398822e270SJohn Hurley struct mpls_shim_hdr *lse; 61408822e270SJohn Hurley int err; 61418822e270SJohn Hurley 61428822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 61438822e270SJohn Hurley return -EINVAL; 61448822e270SJohn Hurley 61458822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 61468822e270SJohn Hurley if (skb->encapsulation) 61478822e270SJohn Hurley return -EINVAL; 61488822e270SJohn Hurley 61498822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 61508822e270SJohn Hurley if (unlikely(err)) 61518822e270SJohn Hurley return err; 61528822e270SJohn Hurley 61538822e270SJohn Hurley if (!skb->inner_protocol) { 6154e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 61558822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 61568822e270SJohn Hurley } 61578822e270SJohn Hurley 61588822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 61598822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6160fa4e0f88SDavide Caratti mac_len); 61618822e270SJohn Hurley skb_reset_mac_header(skb); 6162fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6163e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 61648822e270SJohn Hurley 61658822e270SJohn Hurley lse = mpls_hdr(skb); 61668822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 61678822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 61688822e270SJohn Hurley 61694296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 61708822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 61718822e270SJohn Hurley skb->protocol = mpls_proto; 61728822e270SJohn Hurley 61738822e270SJohn Hurley return 0; 61748822e270SJohn Hurley } 61758822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 61768822e270SJohn Hurley 61772e4e4410SEric Dumazet /** 6178ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6179ed246ceeSJohn Hurley * 6180ed246ceeSJohn Hurley * @skb: buffer 6181ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6182fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 618376f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6184ed246ceeSJohn Hurley * 6185ed246ceeSJohn Hurley * Expects skb->data at mac header. 6186ed246ceeSJohn Hurley * 6187ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6188ed246ceeSJohn Hurley */ 6189040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6190040b5cfbSMartin Varghese bool ethernet) 6191ed246ceeSJohn Hurley { 6192ed246ceeSJohn Hurley int err; 6193ed246ceeSJohn Hurley 6194ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6195dedc5a08SDavide Caratti return 0; 6196ed246ceeSJohn Hurley 6197fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6198ed246ceeSJohn Hurley if (unlikely(err)) 6199ed246ceeSJohn Hurley return err; 6200ed246ceeSJohn Hurley 6201ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6202ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6203fa4e0f88SDavide Caratti mac_len); 6204ed246ceeSJohn Hurley 6205ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6206ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6207fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6208ed246ceeSJohn Hurley 62094296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6210ed246ceeSJohn Hurley struct ethhdr *hdr; 6211ed246ceeSJohn Hurley 6212ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6213ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6214ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6215ed246ceeSJohn Hurley } 6216ed246ceeSJohn Hurley skb->protocol = next_proto; 6217ed246ceeSJohn Hurley 6218ed246ceeSJohn Hurley return 0; 6219ed246ceeSJohn Hurley } 6220ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6221ed246ceeSJohn Hurley 6222ed246ceeSJohn Hurley /** 6223d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6224d27cf5c5SJohn Hurley * 6225d27cf5c5SJohn Hurley * @skb: buffer 6226d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6227d27cf5c5SJohn Hurley * 6228d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6229d27cf5c5SJohn Hurley * 6230d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6231d27cf5c5SJohn Hurley */ 6232d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6233d27cf5c5SJohn Hurley { 6234d27cf5c5SJohn Hurley int err; 6235d27cf5c5SJohn Hurley 6236d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6237d27cf5c5SJohn Hurley return -EINVAL; 6238d27cf5c5SJohn Hurley 6239d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6240d27cf5c5SJohn Hurley if (unlikely(err)) 6241d27cf5c5SJohn Hurley return err; 6242d27cf5c5SJohn Hurley 6243d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6244d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6245d27cf5c5SJohn Hurley 6246d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6247d27cf5c5SJohn Hurley } 6248d27cf5c5SJohn Hurley 6249d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6250d27cf5c5SJohn Hurley 6251d27cf5c5SJohn Hurley return 0; 6252d27cf5c5SJohn Hurley } 6253d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6254d27cf5c5SJohn Hurley 6255d27cf5c5SJohn Hurley /** 62562a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 62572a2ea508SJohn Hurley * 62582a2ea508SJohn Hurley * @skb: buffer 62592a2ea508SJohn Hurley * 62602a2ea508SJohn Hurley * Expects skb->data at mac header. 62612a2ea508SJohn Hurley * 62622a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 62632a2ea508SJohn Hurley */ 62642a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 62652a2ea508SJohn Hurley { 62662a2ea508SJohn Hurley u32 lse; 62672a2ea508SJohn Hurley u8 ttl; 62682a2ea508SJohn Hurley 62692a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 62702a2ea508SJohn Hurley return -EINVAL; 62712a2ea508SJohn Hurley 627213de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 627313de4ed9SDavide Caratti return -ENOMEM; 627413de4ed9SDavide Caratti 62752a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 62762a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 62772a2ea508SJohn Hurley if (!--ttl) 62782a2ea508SJohn Hurley return -EINVAL; 62792a2ea508SJohn Hurley 62802a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 62812a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 62822a2ea508SJohn Hurley 62832a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 62842a2ea508SJohn Hurley } 62852a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 62862a2ea508SJohn Hurley 62872a2ea508SJohn Hurley /** 62882e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 62892e4e4410SEric Dumazet * 6290de3f0d0eSMasanari Iida * @header_len: size of linear part 6291de3f0d0eSMasanari Iida * @data_len: needed length in frags 629209c2c907SEric Dumazet * @order: max page order desired. 6293de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6294de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 62952e4e4410SEric Dumazet * 62962e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 62972e4e4410SEric Dumazet */ 62982e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 62992e4e4410SEric Dumazet unsigned long data_len, 630009c2c907SEric Dumazet int order, 63012e4e4410SEric Dumazet int *errcode, 63022e4e4410SEric Dumazet gfp_t gfp_mask) 63032e4e4410SEric Dumazet { 63042e4e4410SEric Dumazet unsigned long chunk; 63052e4e4410SEric Dumazet struct sk_buff *skb; 63062e4e4410SEric Dumazet struct page *page; 630709c2c907SEric Dumazet int nr_frags = 0; 63082e4e4410SEric Dumazet 63092e4e4410SEric Dumazet *errcode = -EMSGSIZE; 631009c2c907SEric Dumazet if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order))) 63112e4e4410SEric Dumazet return NULL; 63122e4e4410SEric Dumazet 63132e4e4410SEric Dumazet *errcode = -ENOBUFS; 6314f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 63152e4e4410SEric Dumazet if (!skb) 63162e4e4410SEric Dumazet return NULL; 63172e4e4410SEric Dumazet 631809c2c907SEric Dumazet while (data_len) { 631909c2c907SEric Dumazet if (nr_frags == MAX_SKB_FRAGS - 1) 632009c2c907SEric Dumazet goto failure; 632109c2c907SEric Dumazet while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order)) 632209c2c907SEric Dumazet order--; 63232e4e4410SEric Dumazet 632409c2c907SEric Dumazet if (order) { 6325d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 63262e4e4410SEric Dumazet __GFP_COMP | 6327d14b56f5SMichal Hocko __GFP_NOWARN, 63282e4e4410SEric Dumazet order); 632909c2c907SEric Dumazet if (!page) { 63302e4e4410SEric Dumazet order--; 633109c2c907SEric Dumazet continue; 63322e4e4410SEric Dumazet } 633309c2c907SEric Dumazet } else { 63342e4e4410SEric Dumazet page = alloc_page(gfp_mask); 63352e4e4410SEric Dumazet if (!page) 63362e4e4410SEric Dumazet goto failure; 633709c2c907SEric Dumazet } 63382e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 63392e4e4410SEric Dumazet PAGE_SIZE << order); 634009c2c907SEric Dumazet skb_fill_page_desc(skb, nr_frags, page, 0, chunk); 634109c2c907SEric Dumazet nr_frags++; 634209c2c907SEric Dumazet skb->truesize += (PAGE_SIZE << order); 63432e4e4410SEric Dumazet data_len -= chunk; 63442e4e4410SEric Dumazet } 63452e4e4410SEric Dumazet return skb; 63462e4e4410SEric Dumazet 63472e4e4410SEric Dumazet failure: 63482e4e4410SEric Dumazet kfree_skb(skb); 63492e4e4410SEric Dumazet return NULL; 63502e4e4410SEric Dumazet } 63512e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 63526fa01ccdSSowmini Varadhan 63536fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 63546fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 63556fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 63566fa01ccdSSowmini Varadhan { 63576fa01ccdSSowmini Varadhan int i; 635812d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 63596fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 63606fa01ccdSSowmini Varadhan u8 *data; 63616fa01ccdSSowmini Varadhan 63626fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 63636fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 636412d6c1d3SKees Cook 63655c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 63666fa01ccdSSowmini Varadhan if (!data) 63676fa01ccdSSowmini Varadhan return -ENOMEM; 636812d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 63696fa01ccdSSowmini Varadhan 63706fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 63716fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 63726fa01ccdSSowmini Varadhan skb->len -= off; 63736fa01ccdSSowmini Varadhan 63746fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 63756fa01ccdSSowmini Varadhan skb_shinfo(skb), 63766fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 63776fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 63786fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 63796fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 63806fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6381bf9f1baaSEric Dumazet skb_kfree_head(data, size); 63826fa01ccdSSowmini Varadhan return -ENOMEM; 63836fa01ccdSSowmini Varadhan } 63846fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 63856fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 63866fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 63876fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6388b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 63896fa01ccdSSowmini Varadhan } else { 63906fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 63916fa01ccdSSowmini Varadhan * relocate values 63926fa01ccdSSowmini Varadhan */ 6393b07a2d97SJakub Kicinski skb_free_head(skb, false); 63946fa01ccdSSowmini Varadhan } 63956fa01ccdSSowmini Varadhan 63966fa01ccdSSowmini Varadhan skb->head = data; 63976fa01ccdSSowmini Varadhan skb->data = data; 63986fa01ccdSSowmini Varadhan skb->head_frag = 0; 6399763087daSEric Dumazet skb_set_end_offset(skb, size); 64006fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 64016fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64026fa01ccdSSowmini Varadhan skb->cloned = 0; 64036fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64046fa01ccdSSowmini Varadhan skb->nohdr = 0; 64056fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64066fa01ccdSSowmini Varadhan 64076fa01ccdSSowmini Varadhan return 0; 64086fa01ccdSSowmini Varadhan } 64096fa01ccdSSowmini Varadhan 64106fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 64116fa01ccdSSowmini Varadhan 64126fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 64136fa01ccdSSowmini Varadhan * pskb_carve() 64146fa01ccdSSowmini Varadhan */ 64156fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 64166fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 64176fa01ccdSSowmini Varadhan gfp_t gfp_mask) 64186fa01ccdSSowmini Varadhan { 64196fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 64206fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 64216fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 64226fa01ccdSSowmini Varadhan 64236fa01ccdSSowmini Varadhan do { 64246fa01ccdSSowmini Varadhan if (!list) { 64256fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 64266fa01ccdSSowmini Varadhan return -EFAULT; 64276fa01ccdSSowmini Varadhan } 64286fa01ccdSSowmini Varadhan if (list->len <= eat) { 64296fa01ccdSSowmini Varadhan /* Eaten as whole. */ 64306fa01ccdSSowmini Varadhan eat -= list->len; 64316fa01ccdSSowmini Varadhan list = list->next; 64326fa01ccdSSowmini Varadhan insp = list; 64336fa01ccdSSowmini Varadhan } else { 64346fa01ccdSSowmini Varadhan /* Eaten partially. */ 64356fa01ccdSSowmini Varadhan if (skb_shared(list)) { 64366fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 64376fa01ccdSSowmini Varadhan if (!clone) 64386fa01ccdSSowmini Varadhan return -ENOMEM; 64396fa01ccdSSowmini Varadhan insp = list->next; 64406fa01ccdSSowmini Varadhan list = clone; 64416fa01ccdSSowmini Varadhan } else { 64426fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 64436fa01ccdSSowmini Varadhan insp = list; 64446fa01ccdSSowmini Varadhan } 64456fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 64466fa01ccdSSowmini Varadhan kfree_skb(clone); 64476fa01ccdSSowmini Varadhan return -ENOMEM; 64486fa01ccdSSowmini Varadhan } 64496fa01ccdSSowmini Varadhan break; 64506fa01ccdSSowmini Varadhan } 64516fa01ccdSSowmini Varadhan } while (eat); 64526fa01ccdSSowmini Varadhan 64536fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 64546fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 64556fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6456ef527f96SEric Dumazet consume_skb(list); 64576fa01ccdSSowmini Varadhan } 64586fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 64596fa01ccdSSowmini Varadhan if (clone) { 64606fa01ccdSSowmini Varadhan clone->next = list; 64616fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 64626fa01ccdSSowmini Varadhan } 64636fa01ccdSSowmini Varadhan return 0; 64646fa01ccdSSowmini Varadhan } 64656fa01ccdSSowmini Varadhan 64666fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 64676fa01ccdSSowmini Varadhan * non-linear part of skb 64686fa01ccdSSowmini Varadhan */ 64696fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 64706fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 64716fa01ccdSSowmini Varadhan { 64726fa01ccdSSowmini Varadhan int i, k = 0; 647312d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 64746fa01ccdSSowmini Varadhan u8 *data; 64756fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 64766fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 64776fa01ccdSSowmini Varadhan 64786fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64796fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 648012d6c1d3SKees Cook 64815c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 64826fa01ccdSSowmini Varadhan if (!data) 64836fa01ccdSSowmini Varadhan return -ENOMEM; 648412d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64856fa01ccdSSowmini Varadhan 64866fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6487e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 64886fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6489bf9f1baaSEric Dumazet skb_kfree_head(data, size); 64906fa01ccdSSowmini Varadhan return -ENOMEM; 64916fa01ccdSSowmini Varadhan } 64926fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 64936fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 64946fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 64956fa01ccdSSowmini Varadhan 64966fa01ccdSSowmini Varadhan if (pos + fsize > off) { 64976fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 64986fa01ccdSSowmini Varadhan 64996fa01ccdSSowmini Varadhan if (pos < off) { 65006fa01ccdSSowmini Varadhan /* Split frag. 65016fa01ccdSSowmini Varadhan * We have two variants in this case: 65026fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 65036fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 65046fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 65056fa01ccdSSowmini Varadhan * where splitting is expensive. 65066fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 65076fa01ccdSSowmini Varadhan */ 6508b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 65096fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 65106fa01ccdSSowmini Varadhan } 65116fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 65126fa01ccdSSowmini Varadhan k++; 65136fa01ccdSSowmini Varadhan } 65146fa01ccdSSowmini Varadhan pos += fsize; 65156fa01ccdSSowmini Varadhan } 65166fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 65176fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 65186fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 65196fa01ccdSSowmini Varadhan 65206fa01ccdSSowmini Varadhan /* split line is in frag list */ 6521eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6522eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6523eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6524eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6525bf9f1baaSEric Dumazet skb_kfree_head(data, size); 6526eabe8618SMiaohe Lin return -ENOMEM; 65276fa01ccdSSowmini Varadhan } 6528b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 65296fa01ccdSSowmini Varadhan 65306fa01ccdSSowmini Varadhan skb->head = data; 65316fa01ccdSSowmini Varadhan skb->head_frag = 0; 65326fa01ccdSSowmini Varadhan skb->data = data; 6533763087daSEric Dumazet skb_set_end_offset(skb, size); 65346fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 65356fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 65366fa01ccdSSowmini Varadhan skb->cloned = 0; 65376fa01ccdSSowmini Varadhan skb->hdr_len = 0; 65386fa01ccdSSowmini Varadhan skb->nohdr = 0; 65396fa01ccdSSowmini Varadhan skb->len -= off; 65406fa01ccdSSowmini Varadhan skb->data_len = skb->len; 65416fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 65426fa01ccdSSowmini Varadhan return 0; 65436fa01ccdSSowmini Varadhan } 65446fa01ccdSSowmini Varadhan 65456fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 65466fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 65476fa01ccdSSowmini Varadhan { 65486fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 65496fa01ccdSSowmini Varadhan 65506fa01ccdSSowmini Varadhan if (len < headlen) 65516fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 65526fa01ccdSSowmini Varadhan else 65536fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 65546fa01ccdSSowmini Varadhan } 65556fa01ccdSSowmini Varadhan 65566fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 65576fa01ccdSSowmini Varadhan * a new skb 65586fa01ccdSSowmini Varadhan */ 65596fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 65606fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 65616fa01ccdSSowmini Varadhan { 65626fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 65636fa01ccdSSowmini Varadhan 65646fa01ccdSSowmini Varadhan if (!clone) 65656fa01ccdSSowmini Varadhan return NULL; 65666fa01ccdSSowmini Varadhan 65676fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 65686fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 65696fa01ccdSSowmini Varadhan kfree_skb(clone); 65706fa01ccdSSowmini Varadhan return NULL; 65716fa01ccdSSowmini Varadhan } 65726fa01ccdSSowmini Varadhan return clone; 65736fa01ccdSSowmini Varadhan } 65746fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6575c8c8b127SEric Dumazet 6576c8c8b127SEric Dumazet /** 6577c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6578c8c8b127SEric Dumazet * @skb: buffer 6579c8c8b127SEric Dumazet * 6580c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6581c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6582c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6583c8c8b127SEric Dumazet * truesize. 6584c8c8b127SEric Dumazet * Notes: 6585c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6586c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6587c8c8b127SEric Dumazet */ 6588c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6589c8c8b127SEric Dumazet { 65903174fed9SEric Dumazet if (skb->data_len) { 65913174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6592c8c8b127SEric Dumazet skb_cloned(skb)) 6593c8c8b127SEric Dumazet return; 6594c8c8b127SEric Dumazet 6595c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6596c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 65973174fed9SEric Dumazet } 65983174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 65993174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 66003174fed9SEric Dumazet * their truesize. 66013174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 66023174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 66033174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6604c8c8b127SEric Dumazet */ 6605c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6606c8c8b127SEric Dumazet } 6607d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6608df5042f4SFlorian Westphal 6609df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6610df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6611df5042f4SFlorian Westphal { 6612df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6613df5042f4SFlorian Westphal } 6614df5042f4SFlorian Westphal 66158b69a803SPaolo Abeni /** 66168b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 66178b69a803SPaolo Abeni * 66184930f483SFlorian Westphal * @flags: See kmalloc(). 66194930f483SFlorian Westphal * 66208b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 66218b69a803SPaolo Abeni * skb via __skb_ext_set(). 66228b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 66238b69a803SPaolo Abeni */ 66244930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6625df5042f4SFlorian Westphal { 66264930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6627df5042f4SFlorian Westphal 6628df5042f4SFlorian Westphal if (new) { 6629df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6630df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6631df5042f4SFlorian Westphal } 6632df5042f4SFlorian Westphal 6633df5042f4SFlorian Westphal return new; 6634df5042f4SFlorian Westphal } 6635df5042f4SFlorian Westphal 66364165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 66374165079bSFlorian Westphal unsigned int old_active) 6638df5042f4SFlorian Westphal { 6639df5042f4SFlorian Westphal struct skb_ext *new; 6640df5042f4SFlorian Westphal 6641df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6642df5042f4SFlorian Westphal return old; 6643df5042f4SFlorian Westphal 6644df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6645df5042f4SFlorian Westphal if (!new) 6646df5042f4SFlorian Westphal return NULL; 6647df5042f4SFlorian Westphal 6648df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6649df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6650df5042f4SFlorian Westphal 66514165079bSFlorian Westphal #ifdef CONFIG_XFRM 66524165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 66534165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 66544165079bSFlorian Westphal unsigned int i; 66554165079bSFlorian Westphal 66564165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 66574165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 66584165079bSFlorian Westphal } 66594165079bSFlorian Westphal #endif 6660df5042f4SFlorian Westphal __skb_ext_put(old); 6661df5042f4SFlorian Westphal return new; 6662df5042f4SFlorian Westphal } 6663df5042f4SFlorian Westphal 6664df5042f4SFlorian Westphal /** 66658b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 66668b69a803SPaolo Abeni * @skb: buffer 66678b69a803SPaolo Abeni * @id: extension id 66688b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 66698b69a803SPaolo Abeni * 66708b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 66718b69a803SPaolo Abeni * 66728b69a803SPaolo Abeni * Returns the pointer to the extension. 66738b69a803SPaolo Abeni */ 66748b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 66758b69a803SPaolo Abeni struct skb_ext *ext) 66768b69a803SPaolo Abeni { 66778b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 66788b69a803SPaolo Abeni 66798b69a803SPaolo Abeni skb_ext_put(skb); 66808b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 66818b69a803SPaolo Abeni ext->chunks = newlen; 66828b69a803SPaolo Abeni ext->offset[id] = newoff; 66838b69a803SPaolo Abeni skb->extensions = ext; 66848b69a803SPaolo Abeni skb->active_extensions = 1 << id; 66858b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 66868b69a803SPaolo Abeni } 66878b69a803SPaolo Abeni 66888b69a803SPaolo Abeni /** 6689df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6690df5042f4SFlorian Westphal * @skb: buffer 6691df5042f4SFlorian Westphal * @id: extension to allocate space for 6692df5042f4SFlorian Westphal * 6693df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6694df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6695df5042f4SFlorian Westphal * is returned. 6696df5042f4SFlorian Westphal * 6697df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6698df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6699df5042f4SFlorian Westphal * 6700df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6701df5042f4SFlorian Westphal */ 6702df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6703df5042f4SFlorian Westphal { 6704df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6705df5042f4SFlorian Westphal unsigned int newlen, newoff; 6706df5042f4SFlorian Westphal 6707df5042f4SFlorian Westphal if (skb->active_extensions) { 6708df5042f4SFlorian Westphal old = skb->extensions; 6709df5042f4SFlorian Westphal 67104165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6711df5042f4SFlorian Westphal if (!new) 6712df5042f4SFlorian Westphal return NULL; 6713df5042f4SFlorian Westphal 6714682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6715df5042f4SFlorian Westphal goto set_active; 6716df5042f4SFlorian Westphal 6717e94e50bdSPaolo Abeni newoff = new->chunks; 6718df5042f4SFlorian Westphal } else { 6719df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6720df5042f4SFlorian Westphal 67214930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6722df5042f4SFlorian Westphal if (!new) 6723df5042f4SFlorian Westphal return NULL; 6724df5042f4SFlorian Westphal } 6725df5042f4SFlorian Westphal 6726df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6727df5042f4SFlorian Westphal new->chunks = newlen; 6728df5042f4SFlorian Westphal new->offset[id] = newoff; 6729df5042f4SFlorian Westphal set_active: 6730b0999f38SPaolo Abeni skb->slow_gro = 1; 6731682ec859SPaolo Abeni skb->extensions = new; 6732df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6733df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6734df5042f4SFlorian Westphal } 6735df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6736df5042f4SFlorian Westphal 67374165079bSFlorian Westphal #ifdef CONFIG_XFRM 67384165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 67394165079bSFlorian Westphal { 67404165079bSFlorian Westphal unsigned int i; 67414165079bSFlorian Westphal 67424165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 67434165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 67444165079bSFlorian Westphal } 67454165079bSFlorian Westphal #endif 67464165079bSFlorian Westphal 674778476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 674878476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 674978476d31SJeremy Kerr { 675078476d31SJeremy Kerr if (flow->key) 675178476d31SJeremy Kerr mctp_key_unref(flow->key); 675278476d31SJeremy Kerr } 675378476d31SJeremy Kerr #endif 675478476d31SJeremy Kerr 6755df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6756df5042f4SFlorian Westphal { 6757df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6758df5042f4SFlorian Westphal 6759df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6760df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6761df5042f4SFlorian Westphal skb->extensions = NULL; 6762df5042f4SFlorian Westphal __skb_ext_put(ext); 67634165079bSFlorian Westphal #ifdef CONFIG_XFRM 67644165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 67654165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 67664165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 67674165079bSFlorian Westphal 67684165079bSFlorian Westphal skb_ext_put_sp(sp); 67694165079bSFlorian Westphal sp->len = 0; 67704165079bSFlorian Westphal #endif 6771df5042f4SFlorian Westphal } 6772df5042f4SFlorian Westphal } 6773df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6774df5042f4SFlorian Westphal 6775df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6776df5042f4SFlorian Westphal { 6777df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6778df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6779df5042f4SFlorian Westphal */ 6780df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6781df5042f4SFlorian Westphal goto free_now; 6782df5042f4SFlorian Westphal 6783df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6784df5042f4SFlorian Westphal return; 6785df5042f4SFlorian Westphal free_now: 67864165079bSFlorian Westphal #ifdef CONFIG_XFRM 67874165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 67884165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 67894165079bSFlorian Westphal #endif 679078476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 679178476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 679278476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 679378476d31SJeremy Kerr #endif 67944165079bSFlorian Westphal 6795df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6796df5042f4SFlorian Westphal } 6797df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6798df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 679968822bdfSEric Dumazet 680068822bdfSEric Dumazet /** 680168822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 680268822bdfSEric Dumazet * @skb: buffer 680368822bdfSEric Dumazet * 680468822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 680568822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 680668822bdfSEric Dumazet * and memory zone spinlock contention. 680768822bdfSEric Dumazet */ 680868822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 680968822bdfSEric Dumazet { 681068822bdfSEric Dumazet int cpu = skb->alloc_cpu; 681168822bdfSEric Dumazet struct softnet_data *sd; 681239564c3fSEric Dumazet unsigned int defer_max; 681368822bdfSEric Dumazet bool kick; 681468822bdfSEric Dumazet 681568822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 681668822bdfSEric Dumazet !cpu_online(cpu) || 681768822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 681839564c3fSEric Dumazet nodefer: __kfree_skb(skb); 681968822bdfSEric Dumazet return; 682068822bdfSEric Dumazet } 682168822bdfSEric Dumazet 6822e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb_dst(skb)); 6823e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb->destructor); 6824e8e1ce84SEric Dumazet 682568822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 682639564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 682739564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 682839564c3fSEric Dumazet goto nodefer; 682939564c3fSEric Dumazet 6830931e93bdSEric Dumazet spin_lock_bh(&sd->defer_lock); 6831c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6832c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6833c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6834c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6835c09b0cd2SJakub Kicinski 683668822bdfSEric Dumazet skb->next = sd->defer_list; 683768822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 683868822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 6839931e93bdSEric Dumazet spin_unlock_bh(&sd->defer_lock); 684068822bdfSEric Dumazet 684168822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 684268822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 684368822bdfSEric Dumazet */ 684497e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 684568822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 684668822bdfSEric Dumazet } 68472e910b95SDavid Howells 68482e910b95SDavid Howells static void skb_splice_csum_page(struct sk_buff *skb, struct page *page, 68492e910b95SDavid Howells size_t offset, size_t len) 68502e910b95SDavid Howells { 68512e910b95SDavid Howells const char *kaddr; 68522e910b95SDavid Howells __wsum csum; 68532e910b95SDavid Howells 68542e910b95SDavid Howells kaddr = kmap_local_page(page); 68552e910b95SDavid Howells csum = csum_partial(kaddr + offset, len, 0); 68562e910b95SDavid Howells kunmap_local(kaddr); 68572e910b95SDavid Howells skb->csum = csum_block_add(skb->csum, csum, skb->len); 68582e910b95SDavid Howells } 68592e910b95SDavid Howells 68602e910b95SDavid Howells /** 68612e910b95SDavid Howells * skb_splice_from_iter - Splice (or copy) pages to skbuff 68622e910b95SDavid Howells * @skb: The buffer to add pages to 68632e910b95SDavid Howells * @iter: Iterator representing the pages to be added 68642e910b95SDavid Howells * @maxsize: Maximum amount of pages to be added 68652e910b95SDavid Howells * @gfp: Allocation flags 68662e910b95SDavid Howells * 68672e910b95SDavid Howells * This is a common helper function for supporting MSG_SPLICE_PAGES. It 68682e910b95SDavid Howells * extracts pages from an iterator and adds them to the socket buffer if 68692e910b95SDavid Howells * possible, copying them to fragments if not possible (such as if they're slab 68702e910b95SDavid Howells * pages). 68712e910b95SDavid Howells * 68722e910b95SDavid Howells * Returns the amount of data spliced/copied or -EMSGSIZE if there's 68732e910b95SDavid Howells * insufficient space in the buffer to transfer anything. 68742e910b95SDavid Howells */ 68752e910b95SDavid Howells ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter, 68762e910b95SDavid Howells ssize_t maxsize, gfp_t gfp) 68772e910b95SDavid Howells { 68782e910b95SDavid Howells size_t frag_limit = READ_ONCE(sysctl_max_skb_frags); 68792e910b95SDavid Howells struct page *pages[8], **ppages = pages; 68802e910b95SDavid Howells ssize_t spliced = 0, ret = 0; 68812e910b95SDavid Howells unsigned int i; 68822e910b95SDavid Howells 68832e910b95SDavid Howells while (iter->count > 0) { 6884ef1bc119SDan Carpenter ssize_t space, nr, len; 6885ef1bc119SDan Carpenter size_t off; 68862e910b95SDavid Howells 68872e910b95SDavid Howells ret = -EMSGSIZE; 68882e910b95SDavid Howells space = frag_limit - skb_shinfo(skb)->nr_frags; 68892e910b95SDavid Howells if (space < 0) 68902e910b95SDavid Howells break; 68912e910b95SDavid Howells 68922e910b95SDavid Howells /* We might be able to coalesce without increasing nr_frags */ 68932e910b95SDavid Howells nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages)); 68942e910b95SDavid Howells 68952e910b95SDavid Howells len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); 68962e910b95SDavid Howells if (len <= 0) { 68972e910b95SDavid Howells ret = len ?: -EIO; 68982e910b95SDavid Howells break; 68992e910b95SDavid Howells } 69002e910b95SDavid Howells 69012e910b95SDavid Howells i = 0; 69022e910b95SDavid Howells do { 69032e910b95SDavid Howells struct page *page = pages[i++]; 69042e910b95SDavid Howells size_t part = min_t(size_t, PAGE_SIZE - off, len); 69052e910b95SDavid Howells 69062e910b95SDavid Howells ret = -EIO; 69072e910b95SDavid Howells if (WARN_ON_ONCE(!sendpage_ok(page))) 69082e910b95SDavid Howells goto out; 69092e910b95SDavid Howells 69102e910b95SDavid Howells ret = skb_append_pagefrags(skb, page, off, part, 69112e910b95SDavid Howells frag_limit); 69122e910b95SDavid Howells if (ret < 0) { 69132e910b95SDavid Howells iov_iter_revert(iter, len); 69142e910b95SDavid Howells goto out; 69152e910b95SDavid Howells } 69162e910b95SDavid Howells 69172e910b95SDavid Howells if (skb->ip_summed == CHECKSUM_NONE) 69182e910b95SDavid Howells skb_splice_csum_page(skb, page, off, part); 69192e910b95SDavid Howells 69202e910b95SDavid Howells off = 0; 69212e910b95SDavid Howells spliced += part; 69222e910b95SDavid Howells maxsize -= part; 69232e910b95SDavid Howells len -= part; 69242e910b95SDavid Howells } while (len > 0); 69252e910b95SDavid Howells 69262e910b95SDavid Howells if (maxsize <= 0) 69272e910b95SDavid Howells break; 69282e910b95SDavid Howells } 69292e910b95SDavid Howells 69302e910b95SDavid Howells out: 69312e910b95SDavid Howells skb_len_add(skb, spliced); 69322e910b95SDavid Howells return spliced ?: ret; 69332e910b95SDavid Howells } 69342e910b95SDavid Howells EXPORT_SYMBOL(skb_splice_from_iter); 6935