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> 766a5bcd84SIlias Apalodimas #include <net/page_pool.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; 5535c0e820cSEric Dumazet unsigned int 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 } 5705c0e820cSEric Dumazet *size = obj_size = kmalloc_size_roundup(obj_size); 5715381b23dSAlexander Lobakin /* 5725381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 5735381b23dSAlexander Lobakin * to the reserves, fail. 5745381b23dSAlexander Lobakin */ 5755c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, 5765381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 5775381b23dSAlexander Lobakin node); 5785381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 5795381b23dSAlexander Lobakin goto out; 5805381b23dSAlexander Lobakin 5815381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 5825381b23dSAlexander Lobakin ret_pfmemalloc = true; 5835c0e820cSEric Dumazet obj = kmalloc_node_track_caller(obj_size, flags, node); 5845381b23dSAlexander Lobakin 5855381b23dSAlexander Lobakin out: 5865381b23dSAlexander Lobakin if (pfmemalloc) 5875381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5885381b23dSAlexander Lobakin 5895381b23dSAlexander Lobakin return obj; 5905381b23dSAlexander Lobakin } 5915381b23dSAlexander Lobakin 5925381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 5935381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 5945381b23dSAlexander Lobakin * [BEEP] leaks. 5955381b23dSAlexander Lobakin * 5965381b23dSAlexander Lobakin */ 5975381b23dSAlexander Lobakin 5985381b23dSAlexander Lobakin /** 5995381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 6005381b23dSAlexander Lobakin * @size: size to allocate 6015381b23dSAlexander Lobakin * @gfp_mask: allocation mask 6025381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 6035381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 6045381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 6055381b23dSAlexander Lobakin * allocations in case the data is required for writeback 6065381b23dSAlexander Lobakin * @node: numa node to allocate memory on 6075381b23dSAlexander Lobakin * 6085381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 6095381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 6105381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 6115381b23dSAlexander Lobakin * 6125381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 6135381b23dSAlexander Lobakin * %GFP_ATOMIC. 6145381b23dSAlexander Lobakin */ 6155381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 6165381b23dSAlexander Lobakin int flags, int node) 6175381b23dSAlexander Lobakin { 6185381b23dSAlexander Lobakin struct kmem_cache *cache; 6195381b23dSAlexander Lobakin struct sk_buff *skb; 6205381b23dSAlexander Lobakin bool pfmemalloc; 621a5df6333SLi RongQing u8 *data; 6225381b23dSAlexander Lobakin 6235381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 624025a785fSJakub Kicinski ? skbuff_fclone_cache : skbuff_cache; 6255381b23dSAlexander Lobakin 6265381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 6275381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 6285381b23dSAlexander Lobakin 6295381b23dSAlexander Lobakin /* Get the HEAD */ 630d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 631d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 632d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 633d13612b5SAlexander Lobakin else 634d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 635df1ae022SAlexander Lobakin if (unlikely(!skb)) 636df1ae022SAlexander Lobakin return NULL; 6375381b23dSAlexander Lobakin prefetchw(skb); 6385381b23dSAlexander Lobakin 6395381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 6405381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 6415381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 6425381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 6435381b23dSAlexander Lobakin */ 6445c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc); 645df1ae022SAlexander Lobakin if (unlikely(!data)) 6465381b23dSAlexander Lobakin goto nodata; 64712d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 6485381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 6495381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 6505381b23dSAlexander Lobakin */ 65165998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size)); 6525381b23dSAlexander Lobakin 6535381b23dSAlexander Lobakin /* 6545381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 6555381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 6565381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 6575381b23dSAlexander Lobakin */ 6585381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 65965998d2bSEric Dumazet __build_skb_around(skb, data, size); 6605381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 6615381b23dSAlexander Lobakin 6625381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 6635381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 6645381b23dSAlexander Lobakin 6655381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 6665381b23dSAlexander Lobakin 6675381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 6685381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 6695381b23dSAlexander Lobakin } 6705381b23dSAlexander Lobakin 6715381b23dSAlexander Lobakin return skb; 672df1ae022SAlexander Lobakin 6735381b23dSAlexander Lobakin nodata: 6745381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 675df1ae022SAlexander Lobakin return NULL; 6765381b23dSAlexander Lobakin } 6775381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 6785381b23dSAlexander Lobakin 6797ba7aeabSSebastian Andrzej Siewior /** 680fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 681fd11a83dSAlexander Duyck * @dev: network device to receive on 682d7499160SMasanari Iida * @len: length to allocate 683fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 684fd11a83dSAlexander Duyck * 685fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 686fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 687fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 688fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 689fd11a83dSAlexander Duyck * 690fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 691fd11a83dSAlexander Duyck */ 6929451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 6939451980aSAlexander Duyck gfp_t gfp_mask) 694fd11a83dSAlexander Duyck { 695b63ae8caSAlexander Duyck struct page_frag_cache *nc; 696fd11a83dSAlexander Duyck struct sk_buff *skb; 6979451980aSAlexander Duyck bool pfmemalloc; 6989451980aSAlexander Duyck void *data; 699fd11a83dSAlexander Duyck 7009451980aSAlexander Duyck len += NET_SKB_PAD; 701fd11a83dSAlexander Duyck 70266c55602SAlexander Lobakin /* If requested length is either too small or too big, 70366c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 70466c55602SAlexander Lobakin */ 70566c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 70666c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 707d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 708a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 709a080e7bdSAlexander Duyck if (!skb) 710a080e7bdSAlexander Duyck goto skb_fail; 711a080e7bdSAlexander Duyck goto skb_success; 712a080e7bdSAlexander Duyck } 7139451980aSAlexander Duyck 714115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 7159451980aSAlexander Duyck 7169451980aSAlexander Duyck if (sk_memalloc_socks()) 7179451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7189451980aSAlexander Duyck 719afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 7209451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 7218c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 7229451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 72392dcabd7SSebastian Andrzej Siewior } else { 72492dcabd7SSebastian Andrzej Siewior local_bh_disable(); 72592dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 72692dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 72792dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 72892dcabd7SSebastian Andrzej Siewior local_bh_enable(); 72992dcabd7SSebastian Andrzej Siewior } 7309451980aSAlexander Duyck 7319451980aSAlexander Duyck if (unlikely(!data)) 7329451980aSAlexander Duyck return NULL; 7339451980aSAlexander Duyck 7349451980aSAlexander Duyck skb = __build_skb(data, len); 7359451980aSAlexander Duyck if (unlikely(!skb)) { 736181edb2bSAlexander Duyck skb_free_frag(data); 7379451980aSAlexander Duyck return NULL; 7389451980aSAlexander Duyck } 7399451980aSAlexander Duyck 7409451980aSAlexander Duyck if (pfmemalloc) 7419451980aSAlexander Duyck skb->pfmemalloc = 1; 7429451980aSAlexander Duyck skb->head_frag = 1; 7439451980aSAlexander Duyck 744a080e7bdSAlexander Duyck skb_success: 7458af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 7467b2e497aSChristoph Hellwig skb->dev = dev; 747fd11a83dSAlexander Duyck 748a080e7bdSAlexander Duyck skb_fail: 7498af27456SChristoph Hellwig return skb; 7508af27456SChristoph Hellwig } 751b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 7521da177e4SLinus Torvalds 753fd11a83dSAlexander Duyck /** 754fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 755fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 756d7499160SMasanari Iida * @len: length to allocate 757fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 758fd11a83dSAlexander Duyck * 759fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 760fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 761fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 762fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 763fd11a83dSAlexander Duyck * 764fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 765fd11a83dSAlexander Duyck */ 7669451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 7679451980aSAlexander Duyck gfp_t gfp_mask) 768fd11a83dSAlexander Duyck { 7693226b158SEric Dumazet struct napi_alloc_cache *nc; 770fd11a83dSAlexander Duyck struct sk_buff *skb; 771dbae2b06SPaolo Abeni bool pfmemalloc; 7729451980aSAlexander Duyck void *data; 773fd11a83dSAlexander Duyck 774ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 7759451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 776fd11a83dSAlexander Duyck 7773226b158SEric Dumazet /* If requested length is either too small or too big, 7783226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 779dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 780dbae2b06SPaolo Abeni * for small fragments 7813226b158SEric Dumazet */ 782dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7833226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 784d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 785cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 786cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 787a080e7bdSAlexander Duyck if (!skb) 788a080e7bdSAlexander Duyck goto skb_fail; 789a080e7bdSAlexander Duyck goto skb_success; 790a080e7bdSAlexander Duyck } 7919451980aSAlexander Duyck 7923226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 7939451980aSAlexander Duyck 7949451980aSAlexander Duyck if (sk_memalloc_socks()) 7959451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7969451980aSAlexander Duyck 797dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 798dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 799dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 800dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 801dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 802dbae2b06SPaolo Abeni * size above 512 bytes 803dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 804dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 805dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 806dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 807dbae2b06SPaolo Abeni */ 808dbae2b06SPaolo Abeni len = SZ_1K; 809dbae2b06SPaolo Abeni 810dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 811dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 812dbae2b06SPaolo Abeni } else { 813115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 814dbae2b06SPaolo Abeni 8158c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 816dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 817dbae2b06SPaolo Abeni } 818dbae2b06SPaolo Abeni 8199451980aSAlexander Duyck if (unlikely(!data)) 8209451980aSAlexander Duyck return NULL; 8219451980aSAlexander Duyck 822cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 8239451980aSAlexander Duyck if (unlikely(!skb)) { 824181edb2bSAlexander Duyck skb_free_frag(data); 8259451980aSAlexander Duyck return NULL; 8269451980aSAlexander Duyck } 8279451980aSAlexander Duyck 828dbae2b06SPaolo Abeni if (pfmemalloc) 8299451980aSAlexander Duyck skb->pfmemalloc = 1; 8309451980aSAlexander Duyck skb->head_frag = 1; 8319451980aSAlexander Duyck 832a080e7bdSAlexander Duyck skb_success: 833fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 834fd11a83dSAlexander Duyck skb->dev = napi->dev; 835fd11a83dSAlexander Duyck 836a080e7bdSAlexander Duyck skb_fail: 837fd11a83dSAlexander Duyck return skb; 838fd11a83dSAlexander Duyck } 839fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 840fd11a83dSAlexander Duyck 841654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 84250269e19SEric Dumazet int size, unsigned int truesize) 843654bed16SPeter Zijlstra { 844654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 845654bed16SPeter Zijlstra skb->len += size; 846654bed16SPeter Zijlstra skb->data_len += size; 84750269e19SEric Dumazet skb->truesize += truesize; 848654bed16SPeter Zijlstra } 849654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 850654bed16SPeter Zijlstra 851f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 852f8e617e1SJason Wang unsigned int truesize) 853f8e617e1SJason Wang { 854f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 855f8e617e1SJason Wang 856f8e617e1SJason Wang skb_frag_size_add(frag, size); 857f8e617e1SJason Wang skb->len += size; 858f8e617e1SJason Wang skb->data_len += size; 859f8e617e1SJason Wang skb->truesize += truesize; 860f8e617e1SJason Wang } 861f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 862f8e617e1SJason Wang 86327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 8641da177e4SLinus Torvalds { 865bd8a7036SEric Dumazet kfree_skb_list(*listp); 86627b437c8SHerbert Xu *listp = NULL; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 87027b437c8SHerbert Xu { 87127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 87227b437c8SHerbert Xu } 87327b437c8SHerbert Xu 8741da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds struct sk_buff *list; 8771da177e4SLinus Torvalds 878fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 8791da177e4SLinus Torvalds skb_get(list); 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 882b07a2d97SJakub Kicinski static bool skb_pp_recycle(struct sk_buff *skb, void *data, bool napi_safe) 8834727bab4SYunsheng Lin { 8844727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 8854727bab4SYunsheng Lin return false; 8868c48eea3SJakub Kicinski return page_pool_return_skb_page(virt_to_page(data), napi_safe); 8874727bab4SYunsheng Lin } 8884727bab4SYunsheng Lin 889bf9f1baaSEric Dumazet static void skb_kfree_head(void *head, unsigned int end_offset) 890bf9f1baaSEric Dumazet { 891bf9f1baaSEric Dumazet if (end_offset == SKB_SMALL_HEAD_HEADROOM) 892bf9f1baaSEric Dumazet kmem_cache_free(skb_small_head_cache, head); 893bf9f1baaSEric Dumazet else 894bf9f1baaSEric Dumazet kfree(head); 895bf9f1baaSEric Dumazet } 896bf9f1baaSEric Dumazet 897b07a2d97SJakub Kicinski static void skb_free_head(struct sk_buff *skb, bool napi_safe) 898d3836f21SEric Dumazet { 899181edb2bSAlexander Duyck unsigned char *head = skb->head; 900181edb2bSAlexander Duyck 9016a5bcd84SIlias Apalodimas if (skb->head_frag) { 902b07a2d97SJakub Kicinski if (skb_pp_recycle(skb, head, napi_safe)) 9036a5bcd84SIlias Apalodimas return; 904181edb2bSAlexander Duyck skb_free_frag(head); 9056a5bcd84SIlias Apalodimas } else { 906bf9f1baaSEric Dumazet skb_kfree_head(head, skb_end_offset(skb)); 907d3836f21SEric Dumazet } 9086a5bcd84SIlias Apalodimas } 909d3836f21SEric Dumazet 910b07a2d97SJakub Kicinski static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason, 911b07a2d97SJakub Kicinski bool napi_safe) 9121da177e4SLinus Torvalds { 913ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 9141da177e4SLinus Torvalds int i; 915ff04a771SEric Dumazet 916ff04a771SEric Dumazet if (skb->cloned && 917ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 918ff04a771SEric Dumazet &shinfo->dataref)) 9192cc3aeb5SIlias Apalodimas goto exit; 920ff04a771SEric Dumazet 921753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 922753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 923753f1ca4SPavel Begunkov 92470c43167SJonathan Lemon skb_zcopy_clear(skb, true); 925753f1ca4SPavel Begunkov if (skip_unref) 926753f1ca4SPavel Begunkov goto free_head; 927753f1ca4SPavel Begunkov } 92870c43167SJonathan Lemon 929ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 9308c48eea3SJakub Kicinski napi_frag_unref(&shinfo->frags[i], skb->pp_recycle, napi_safe); 9311da177e4SLinus Torvalds 932753f1ca4SPavel Begunkov free_head: 933ff04a771SEric Dumazet if (shinfo->frag_list) 934511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 9351da177e4SLinus Torvalds 936b07a2d97SJakub Kicinski skb_free_head(skb, napi_safe); 9372cc3aeb5SIlias Apalodimas exit: 9382cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 9392cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 9402cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 9412cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 9422cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 94358e61e41STom Rix * additional references to the fragmented part of the SKB. 9442cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 9452cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 9462cc3aeb5SIlias Apalodimas */ 9472cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* 9511da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 9521da177e4SLinus Torvalds */ 9532d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 9541da177e4SLinus Torvalds { 955d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 956d179cd12SDavid S. Miller 957d179cd12SDavid S. Miller switch (skb->fclone) { 958d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 959025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 9606ffe75ebSEric Dumazet return; 961d179cd12SDavid S. Miller 962d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 963d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 9646ffe75ebSEric Dumazet 9656ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 9666ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 9676ffe75ebSEric Dumazet * while here, branch prediction will be good. 9686ffe75ebSEric Dumazet */ 9692638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 9706ffe75ebSEric Dumazet goto fastpath; 971d179cd12SDavid S. Miller break; 972d179cd12SDavid S. Miller 9736ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 974d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 975d179cd12SDavid S. Miller break; 9763ff50b79SStephen Hemminger } 9772638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 9786ffe75ebSEric Dumazet return; 9796ffe75ebSEric Dumazet fastpath: 9806ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9830a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 9841da177e4SLinus Torvalds { 985adf30907SEric Dumazet skb_dst_drop(skb); 9861da177e4SLinus Torvalds if (skb->destructor) { 9877890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 9881da177e4SLinus Torvalds skb->destructor(skb); 9891da177e4SLinus Torvalds } 990a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 991cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 9922fc72c7bSKOVACS Krisztian #endif 993df5042f4SFlorian Westphal skb_ext_put(skb); 99404a4bb55SLennert Buytenhek } 99504a4bb55SLennert Buytenhek 99604a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 997b07a2d97SJakub Kicinski static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason, 998b07a2d97SJakub Kicinski bool napi_safe) 99904a4bb55SLennert Buytenhek { 100004a4bb55SLennert Buytenhek skb_release_head_state(skb); 1001a28b1b90SFlorian Westphal if (likely(skb->head)) 1002b07a2d97SJakub Kicinski skb_release_data(skb, reason, napi_safe); 10032d4baff8SHerbert Xu } 10041da177e4SLinus Torvalds 10052d4baff8SHerbert Xu /** 10062d4baff8SHerbert Xu * __kfree_skb - private function 10072d4baff8SHerbert Xu * @skb: buffer 10082d4baff8SHerbert Xu * 10092d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 10102d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 10112d4baff8SHerbert Xu * always call kfree_skb 10122d4baff8SHerbert Xu */ 10132d4baff8SHerbert Xu 10142d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 10152d4baff8SHerbert Xu { 1016b07a2d97SJakub Kicinski skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED, false); 10171da177e4SLinus Torvalds kfree_skbmem(skb); 10181da177e4SLinus Torvalds } 1019b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 10201da177e4SLinus Torvalds 1021a4650da2SJesper Dangaard Brouer static __always_inline 1022a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1023a4650da2SJesper Dangaard Brouer { 1024a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 1025a4650da2SJesper Dangaard Brouer return false; 1026a4650da2SJesper Dangaard Brouer 1027071c0fc6SJohannes Berg DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET || 1028071c0fc6SJohannes Berg u32_get_bits(reason, 1029071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_MASK) >= 1030071c0fc6SJohannes Berg SKB_DROP_REASON_SUBSYS_NUM); 1031a4650da2SJesper Dangaard Brouer 1032a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 1033dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1034a4650da2SJesper Dangaard Brouer else 1035a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 1036a4650da2SJesper Dangaard Brouer return true; 1037a4650da2SJesper Dangaard Brouer } 1038a4650da2SJesper Dangaard Brouer 10391da177e4SLinus Torvalds /** 1040c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 1041231d06aeSJörn Engel * @skb: buffer to free 1042c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 1043231d06aeSJörn Engel * 1044231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 1045c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 1046c504e5c2SMenglong Dong * tracepoint. 1047231d06aeSJörn Engel */ 1048c205cc75SMenglong Dong void __fix_address 1049c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 1050231d06aeSJörn Engel { 1051a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 1052231d06aeSJörn Engel __kfree_skb(skb); 1053231d06aeSJörn Engel } 1054c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 1055231d06aeSJörn Engel 1056eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 1057eedade12SJesper Dangaard Brouer 1058eedade12SJesper Dangaard Brouer struct skb_free_array { 1059eedade12SJesper Dangaard Brouer unsigned int skb_count; 1060eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 1061eedade12SJesper Dangaard Brouer }; 1062eedade12SJesper Dangaard Brouer 1063eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 1064eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 1065eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 1066eedade12SJesper Dangaard Brouer { 1067eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1068eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 1069eedade12SJesper Dangaard Brouer __kfree_skb(skb); 1070eedade12SJesper Dangaard Brouer return; 1071eedade12SJesper Dangaard Brouer } 1072eedade12SJesper Dangaard Brouer 1073b07a2d97SJakub Kicinski skb_release_all(skb, reason, false); 1074eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 1075eedade12SJesper Dangaard Brouer 1076eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 1077025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, KFREE_SKB_BULK_SIZE, 1078eedade12SJesper Dangaard Brouer sa->skb_array); 1079eedade12SJesper Dangaard Brouer sa->skb_count = 0; 1080eedade12SJesper Dangaard Brouer } 1081eedade12SJesper Dangaard Brouer } 1082eedade12SJesper Dangaard Brouer 1083a4650da2SJesper Dangaard Brouer void __fix_address 1084a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 1085bd8a7036SEric Dumazet { 1086eedade12SJesper Dangaard Brouer struct skb_free_array sa; 1087eedade12SJesper Dangaard Brouer 1088eedade12SJesper Dangaard Brouer sa.skb_count = 0; 1089eedade12SJesper Dangaard Brouer 1090bd8a7036SEric Dumazet while (segs) { 1091bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 1092bd8a7036SEric Dumazet 10939dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) { 10949dde0cd3SJesper Dangaard Brouer skb_poison_list(segs); 1095eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 10969dde0cd3SJesper Dangaard Brouer } 1097eedade12SJesper Dangaard Brouer 1098bd8a7036SEric Dumazet segs = next; 1099bd8a7036SEric Dumazet } 1100eedade12SJesper Dangaard Brouer 1101eedade12SJesper Dangaard Brouer if (sa.skb_count) 1102025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, sa.skb_count, sa.skb_array); 1103bd8a7036SEric Dumazet } 1104215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1105bd8a7036SEric Dumazet 11066413139dSWillem de Bruijn /* Dump skb information and contents. 11076413139dSWillem de Bruijn * 11086413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 11096413139dSWillem de Bruijn * 1110302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 11116413139dSWillem de Bruijn */ 11126413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 11136413139dSWillem de Bruijn { 11146413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 11156413139dSWillem de Bruijn struct net_device *dev = skb->dev; 11166413139dSWillem de Bruijn struct sock *sk = skb->sk; 11176413139dSWillem de Bruijn struct sk_buff *list_skb; 11186413139dSWillem de Bruijn bool has_mac, has_trans; 11196413139dSWillem de Bruijn int headroom, tailroom; 11206413139dSWillem de Bruijn int i, len, seg_len; 11216413139dSWillem de Bruijn 11226413139dSWillem de Bruijn if (full_pkt) 11236413139dSWillem de Bruijn len = skb->len; 11246413139dSWillem de Bruijn else 11256413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 11266413139dSWillem de Bruijn 11276413139dSWillem de Bruijn headroom = skb_headroom(skb); 11286413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 11296413139dSWillem de Bruijn 11306413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 11316413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 11326413139dSWillem de Bruijn 11336413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 11346413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 11356413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 11366413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 11376413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 11386413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 11396413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 11406413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 11416413139dSWillem de Bruijn skb->network_header, 11426413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 11436413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 11446413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 11456413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 11466413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 11476413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 11486413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 11496413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 11506413139dSWillem de Bruijn 11516413139dSWillem de Bruijn if (dev) 11528a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 11536413139dSWillem de Bruijn level, dev->name, &dev->features); 11546413139dSWillem de Bruijn if (sk) 1155db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 11566413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 11576413139dSWillem de Bruijn 11586413139dSWillem de Bruijn if (full_pkt && headroom) 11596413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 11606413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 11616413139dSWillem de Bruijn 11626413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 11636413139dSWillem de Bruijn if (seg_len) 11646413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 11656413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 11666413139dSWillem de Bruijn len -= seg_len; 11676413139dSWillem de Bruijn 11686413139dSWillem de Bruijn if (full_pkt && tailroom) 11696413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 11706413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 11716413139dSWillem de Bruijn 11726413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 11736413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 11746413139dSWillem de Bruijn u32 p_off, p_len, copied; 11756413139dSWillem de Bruijn struct page *p; 11766413139dSWillem de Bruijn u8 *vaddr; 11776413139dSWillem de Bruijn 1178b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 11796413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 11806413139dSWillem de Bruijn copied) { 11816413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 11826413139dSWillem de Bruijn vaddr = kmap_atomic(p); 11836413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 11846413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 11856413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 11866413139dSWillem de Bruijn kunmap_atomic(vaddr); 11876413139dSWillem de Bruijn len -= seg_len; 11886413139dSWillem de Bruijn if (!len) 11896413139dSWillem de Bruijn break; 11906413139dSWillem de Bruijn } 11916413139dSWillem de Bruijn } 11926413139dSWillem de Bruijn 11936413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 11946413139dSWillem de Bruijn printk("skb fraglist:\n"); 11956413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 11966413139dSWillem de Bruijn skb_dump(level, list_skb, true); 11976413139dSWillem de Bruijn } 11986413139dSWillem de Bruijn } 11996413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 12006413139dSWillem de Bruijn 1201d1a203eaSStephen Hemminger /** 120225121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 120325121173SMichael S. Tsirkin * @skb: buffer that triggered an error 120425121173SMichael S. Tsirkin * 120525121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 120625121173SMichael S. Tsirkin * skb must be freed afterwards. 120725121173SMichael S. Tsirkin */ 120825121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 120925121173SMichael S. Tsirkin { 1210753f1ca4SPavel Begunkov if (skb) { 1211753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 12121f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 121325121173SMichael S. Tsirkin } 1214753f1ca4SPavel Begunkov } 121525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 121625121173SMichael S. Tsirkin 1217be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 121825121173SMichael S. Tsirkin /** 1219ead2ceb0SNeil Horman * consume_skb - free an skbuff 1220ead2ceb0SNeil Horman * @skb: buffer to free 1221ead2ceb0SNeil Horman * 1222ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1223ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1224ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1225ead2ceb0SNeil Horman */ 1226ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1227ead2ceb0SNeil Horman { 12283889a803SPaolo Abeni if (!skb_unref(skb)) 1229ead2ceb0SNeil Horman return; 12303889a803SPaolo Abeni 1231dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1232ead2ceb0SNeil Horman __kfree_skb(skb); 1233ead2ceb0SNeil Horman } 1234ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1235be769db2SHerbert Xu #endif 1236ead2ceb0SNeil Horman 12370a463c78SPaolo Abeni /** 1238c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 12390a463c78SPaolo Abeni * @skb: buffer to free 12400a463c78SPaolo Abeni * 1241ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1242ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 12430a463c78SPaolo Abeni */ 1244ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 12450a463c78SPaolo Abeni { 1246dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1247b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 12480a463c78SPaolo Abeni kfree_skbmem(skb); 12490a463c78SPaolo Abeni } 12500a463c78SPaolo Abeni 1251f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1252795bb1c0SJesper Dangaard Brouer { 1253795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1254f450d539SAlexander Lobakin u32 i; 1255795bb1c0SJesper Dangaard Brouer 1256025a785fSJakub Kicinski kasan_poison_object_data(skbuff_cache, skb); 1257795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1258795bb1c0SJesper Dangaard Brouer 1259795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1260f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1261025a785fSJakub Kicinski kasan_unpoison_object_data(skbuff_cache, 1262f450d539SAlexander Lobakin nc->skb_cache[i]); 1263f450d539SAlexander Lobakin 1264025a785fSJakub Kicinski kmem_cache_free_bulk(skbuff_cache, NAPI_SKB_CACHE_HALF, 1265f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1266f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1267795bb1c0SJesper Dangaard Brouer } 1268795bb1c0SJesper Dangaard Brouer } 1269f450d539SAlexander Lobakin 12708fa66e4aSJakub Kicinski void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason) 127115fad714SJesper Dangaard Brouer { 12728fa66e4aSJakub Kicinski skb_release_all(skb, reason, true); 12739243adfcSAlexander Lobakin napi_skb_cache_put(skb); 12749243adfcSAlexander Lobakin } 12759243adfcSAlexander Lobakin 12769243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 12779243adfcSAlexander Lobakin { 12789efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 12798550ff8dSPaul Blakey nf_reset_ct(skb); 12809243adfcSAlexander Lobakin skb_dst_drop(skb); 12819243adfcSAlexander Lobakin skb_ext_put(skb); 12825e10da53SPaolo Abeni skb_orphan(skb); 12839efb4b5bSPaolo Abeni skb->slow_gro = 0; 12849efb4b5bSPaolo Abeni } 1285f450d539SAlexander Lobakin napi_skb_cache_put(skb); 128615fad714SJesper Dangaard Brouer } 1287795bb1c0SJesper Dangaard Brouer 1288795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1289795bb1c0SJesper Dangaard Brouer { 1290885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1291795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1292885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1293795bb1c0SJesper Dangaard Brouer return; 1294795bb1c0SJesper Dangaard Brouer } 1295795bb1c0SJesper Dangaard Brouer 1296ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 12976454eca8SYunsheng Lin 12987608894eSPaolo Abeni if (!skb_unref(skb)) 1299795bb1c0SJesper Dangaard Brouer return; 13007608894eSPaolo Abeni 1301795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1302dd1b5278SEric Dumazet trace_consume_skb(skb, __builtin_return_address(0)); 1303795bb1c0SJesper Dangaard Brouer 1304795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1305abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1306795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1307795bb1c0SJesper Dangaard Brouer return; 1308795bb1c0SJesper Dangaard Brouer } 1309795bb1c0SJesper Dangaard Brouer 1310b07a2d97SJakub Kicinski skb_release_all(skb, SKB_CONSUMED, !!budget); 1311f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1312795bb1c0SJesper Dangaard Brouer } 1313795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1314795bb1c0SJesper Dangaard Brouer 131503f61041SKees Cook /* Make sure a field is contained by headers group */ 1316b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 131703f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 131803f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1319b1937227SEric Dumazet 1320dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1321dec18810SHerbert Xu { 1322dec18810SHerbert Xu new->tstamp = old->tstamp; 1323b1937227SEric Dumazet /* We do not copy old->sk */ 1324dec18810SHerbert Xu new->dev = old->dev; 1325b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 13267fee226aSEric Dumazet skb_dst_copy(new, old); 1327df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1328b1937227SEric Dumazet __nf_copy(new, old, false); 13296aa895b0SPatrick McHardy 133003f61041SKees Cook /* Note : this field could be in the headers group. 1331b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1332b1937227SEric Dumazet */ 1333b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 133406021292SEliezer Tamir 133503f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1336b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1337b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1338b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1339b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1340b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1341b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1342b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1343b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1344b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1345b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1346b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1347b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1348b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1349b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1350b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1351b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1352b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1353b1937227SEric Dumazet #endif 1354e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1355b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 135606021292SEliezer Tamir #endif 135768822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 13582bd82484SEric Dumazet #ifdef CONFIG_XPS 13592bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 13602bd82484SEric Dumazet #endif 1361b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1362b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1363b1937227SEric Dumazet #endif 1364b1937227SEric Dumazet 1365dec18810SHerbert Xu } 1366dec18810SHerbert Xu 136782c49a35SHerbert Xu /* 136882c49a35SHerbert Xu * You should not add any new code to this function. Add it to 136982c49a35SHerbert Xu * __copy_skb_header above instead. 137082c49a35SHerbert Xu */ 1371e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds #define C(x) n->x = skb->x 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds n->next = n->prev = NULL; 13761da177e4SLinus Torvalds n->sk = NULL; 1377dec18810SHerbert Xu __copy_skb_header(n, skb); 1378dec18810SHerbert Xu 13791da177e4SLinus Torvalds C(len); 13801da177e4SLinus Torvalds C(data_len); 13813e6b3b2eSAlexey Dobriyan C(mac_len); 1382334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 138302f1c89dSPaul Moore n->cloned = 1; 13841da177e4SLinus Torvalds n->nohdr = 0; 1385b13dda9fSEric Dumazet n->peeked = 0; 1386e78bfb07SStefano Brivio C(pfmemalloc); 13876a5bcd84SIlias Apalodimas C(pp_recycle); 13881da177e4SLinus Torvalds n->destructor = NULL; 13891da177e4SLinus Torvalds C(tail); 13901da177e4SLinus Torvalds C(end); 139102f1c89dSPaul Moore C(head); 1392d3836f21SEric Dumazet C(head_frag); 139302f1c89dSPaul Moore C(data); 139402f1c89dSPaul Moore C(truesize); 139563354797SReshetova, Elena refcount_set(&n->users, 1); 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 13981da177e4SLinus Torvalds skb->cloned = 1; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds return n; 1401e0053ec0SHerbert Xu #undef C 1402e0053ec0SHerbert Xu } 1403e0053ec0SHerbert Xu 1404e0053ec0SHerbert Xu /** 1405da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1406da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1407da29e4b4SJakub Kicinski */ 1408da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1409da29e4b4SJakub Kicinski { 1410da29e4b4SJakub Kicinski struct sk_buff *n; 1411da29e4b4SJakub Kicinski 1412da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1413da29e4b4SJakub Kicinski if (!n) 1414da29e4b4SJakub Kicinski return NULL; 1415da29e4b4SJakub Kicinski 1416da29e4b4SJakub Kicinski n->len = first->len; 1417da29e4b4SJakub Kicinski n->data_len = first->len; 1418da29e4b4SJakub Kicinski n->truesize = first->truesize; 1419da29e4b4SJakub Kicinski 1420da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1421da29e4b4SJakub Kicinski 1422da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1423da29e4b4SJakub Kicinski n->destructor = NULL; 1424da29e4b4SJakub Kicinski 1425da29e4b4SJakub Kicinski return n; 1426da29e4b4SJakub Kicinski } 1427da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1428da29e4b4SJakub Kicinski 1429da29e4b4SJakub Kicinski /** 1430e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1431e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1432e0053ec0SHerbert Xu * @src: the skb to supply the contents 1433e0053ec0SHerbert Xu * 1434e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1435e0053ec0SHerbert Xu * supplied by the user. 1436e0053ec0SHerbert Xu * 1437e0053ec0SHerbert Xu * The target skb is returned upon exit. 1438e0053ec0SHerbert Xu */ 1439e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1440e0053ec0SHerbert Xu { 1441b07a2d97SJakub Kicinski skb_release_all(dst, SKB_CONSUMED, false); 1442e0053ec0SHerbert Xu return __skb_clone(dst, src); 1443e0053ec0SHerbert Xu } 1444e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1445e0053ec0SHerbert Xu 14466f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1447a91dbff5SWillem de Bruijn { 144814ade6baSWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg, rlim; 1449a91dbff5SWillem de Bruijn struct user_struct *user; 1450a91dbff5SWillem de Bruijn 1451a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1452a91dbff5SWillem de Bruijn return 0; 1453a91dbff5SWillem de Bruijn 145414ade6baSWillem de Bruijn rlim = rlimit(RLIMIT_MEMLOCK); 145514ade6baSWillem de Bruijn if (rlim == RLIM_INFINITY) 145614ade6baSWillem de Bruijn return 0; 145714ade6baSWillem de Bruijn 1458a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 145914ade6baSWillem de Bruijn max_pg = rlim >> PAGE_SHIFT; 1460a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1461a91dbff5SWillem de Bruijn 1462a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 146357fc05e8SEric Dumazet do { 1464a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1465a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1466a91dbff5SWillem de Bruijn return -ENOBUFS; 146757fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1468a91dbff5SWillem de Bruijn 1469a91dbff5SWillem de Bruijn if (!mmp->user) { 1470a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1471a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1472a91dbff5SWillem de Bruijn } else { 1473a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1474a91dbff5SWillem de Bruijn } 1475a91dbff5SWillem de Bruijn 1476a91dbff5SWillem de Bruijn return 0; 1477a91dbff5SWillem de Bruijn } 14786f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1479a91dbff5SWillem de Bruijn 14806f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1481a91dbff5SWillem de Bruijn { 1482a91dbff5SWillem de Bruijn if (mmp->user) { 1483a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1484a91dbff5SWillem de Bruijn free_uid(mmp->user); 1485a91dbff5SWillem de Bruijn } 1486a91dbff5SWillem de Bruijn } 14876f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1488a91dbff5SWillem de Bruijn 1489c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 149052267790SWillem de Bruijn { 1491e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 149252267790SWillem de Bruijn struct sk_buff *skb; 149352267790SWillem de Bruijn 149452267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 149552267790SWillem de Bruijn 149652267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 149752267790SWillem de Bruijn if (!skb) 149852267790SWillem de Bruijn return NULL; 149952267790SWillem de Bruijn 150052267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 150152267790SWillem de Bruijn uarg = (void *)skb->cb; 1502a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1503a91dbff5SWillem de Bruijn 1504a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1505a91dbff5SWillem de Bruijn kfree_skb(skb); 1506a91dbff5SWillem de Bruijn return NULL; 1507a91dbff5SWillem de Bruijn } 150852267790SWillem de Bruijn 1509e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 15104ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 15114ab6c99dSWillem de Bruijn uarg->len = 1; 15124ab6c99dSWillem de Bruijn uarg->bytelen = size; 151352267790SWillem de Bruijn uarg->zerocopy = 1; 1514e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1515e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 151652267790SWillem de Bruijn sock_hold(sk); 151752267790SWillem de Bruijn 1518e7d2b510SPavel Begunkov return &uarg->ubuf; 151952267790SWillem de Bruijn } 152052267790SWillem de Bruijn 1521e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 152252267790SWillem de Bruijn { 152352267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 152452267790SWillem de Bruijn } 152552267790SWillem de Bruijn 15268c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 15274ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 15284ab6c99dSWillem de Bruijn { 15294ab6c99dSWillem de Bruijn if (uarg) { 1530e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 15314ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 15324ab6c99dSWillem de Bruijn u32 bytelen, next; 15334ab6c99dSWillem de Bruijn 15341b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 15351b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 15361b4b2b09SPavel Begunkov return NULL; 15371b4b2b09SPavel Begunkov 15384ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 15394ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 15404ab6c99dSWillem de Bruijn */ 15414ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 15424ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 15434ab6c99dSWillem de Bruijn return NULL; 15444ab6c99dSWillem de Bruijn } 15454ab6c99dSWillem de Bruijn 1546e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1547e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1548e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 15494ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 15504ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15514ab6c99dSWillem de Bruijn goto new_alloc; 15524ab6c99dSWillem de Bruijn return NULL; 15534ab6c99dSWillem de Bruijn } 15544ab6c99dSWillem de Bruijn 15554ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1556e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1557e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1558a91dbff5SWillem de Bruijn return NULL; 1559e7d2b510SPavel Begunkov uarg_zc->len++; 1560e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 15614ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1562100f6d8eSWillem de Bruijn 1563100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1564100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 15658e044917SJonathan Lemon net_zcopy_get(uarg); 1566100f6d8eSWillem de Bruijn 15674ab6c99dSWillem de Bruijn return uarg; 15684ab6c99dSWillem de Bruijn } 15694ab6c99dSWillem de Bruijn } 15704ab6c99dSWillem de Bruijn 15714ab6c99dSWillem de Bruijn new_alloc: 15728c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 15734ab6c99dSWillem de Bruijn } 15748c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 15754ab6c99dSWillem de Bruijn 15764ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 15774ab6c99dSWillem de Bruijn { 15784ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 15794ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 15804ab6c99dSWillem de Bruijn u64 sum_len; 15814ab6c99dSWillem de Bruijn 15824ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 15834ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 15844ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 15854ab6c99dSWillem de Bruijn 15864ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 15874ab6c99dSWillem de Bruijn return false; 15884ab6c99dSWillem de Bruijn 15894ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 15904ab6c99dSWillem de Bruijn return false; 15914ab6c99dSWillem de Bruijn 15924ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 15934ab6c99dSWillem de Bruijn return true; 15944ab6c99dSWillem de Bruijn } 15954ab6c99dSWillem de Bruijn 1596e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 159752267790SWillem de Bruijn { 15984ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 159952267790SWillem de Bruijn struct sock_exterr_skb *serr; 160052267790SWillem de Bruijn struct sock *sk = skb->sk; 16014ab6c99dSWillem de Bruijn struct sk_buff_head *q; 16024ab6c99dSWillem de Bruijn unsigned long flags; 16033bdd5ee0SWillem de Bruijn bool is_zerocopy; 16044ab6c99dSWillem de Bruijn u32 lo, hi; 16054ab6c99dSWillem de Bruijn u16 len; 160652267790SWillem de Bruijn 1607ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1608ccaffff1SWillem de Bruijn 16094ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 16104ab6c99dSWillem de Bruijn * so do not queue a completion notification 16114ab6c99dSWillem de Bruijn */ 16124ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 161352267790SWillem de Bruijn goto release; 161452267790SWillem de Bruijn 16154ab6c99dSWillem de Bruijn len = uarg->len; 16164ab6c99dSWillem de Bruijn lo = uarg->id; 16174ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 16183bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 16194ab6c99dSWillem de Bruijn 162052267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 162152267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 162252267790SWillem de Bruijn serr->ee.ee_errno = 0; 162352267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 16244ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 16254ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 16263bdd5ee0SWillem de Bruijn if (!is_zerocopy) 162752267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 162852267790SWillem de Bruijn 16294ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 16304ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 16314ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 16324ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 16334ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 16344ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 163552267790SWillem de Bruijn skb = NULL; 16364ab6c99dSWillem de Bruijn } 16374ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 163852267790SWillem de Bruijn 1639e3ae2365SAlexander Aring sk_error_report(sk); 164052267790SWillem de Bruijn 164152267790SWillem de Bruijn release: 164252267790SWillem de Bruijn consume_skb(skb); 164352267790SWillem de Bruijn sock_put(sk); 164452267790SWillem de Bruijn } 164575518851SJonathan Lemon 16468c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 164736177832SJonathan Lemon bool success) 164875518851SJonathan Lemon { 1649e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1650e7d2b510SPavel Begunkov 1651e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 165275518851SJonathan Lemon 165375518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1654e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 165575518851SJonathan Lemon } 16568c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 165752267790SWillem de Bruijn 16588c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 165952267790SWillem de Bruijn { 1660e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 166152267790SWillem de Bruijn 166252267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1663e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 166452267790SWillem de Bruijn 166552900d22SWillem de Bruijn if (have_uref) 16668c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 166752267790SWillem de Bruijn } 16688c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 166952267790SWillem de Bruijn 167052267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 167152267790SWillem de Bruijn struct msghdr *msg, int len, 167252267790SWillem de Bruijn struct ubuf_info *uarg) 167352267790SWillem de Bruijn { 16744ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 167552267790SWillem de Bruijn int err, orig_len = skb->len; 167652267790SWillem de Bruijn 16774ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 16784ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 16794ab6c99dSWillem de Bruijn */ 16804ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 16814ab6c99dSWillem de Bruijn return -EEXIST; 16824ab6c99dSWillem de Bruijn 1683ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 168452267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 168554d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 168654d43117SWillem de Bruijn 168752267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1688829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 168954d43117SWillem de Bruijn skb->sk = sk; 169052267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 169154d43117SWillem de Bruijn skb->sk = save_sk; 169252267790SWillem de Bruijn return err; 169352267790SWillem de Bruijn } 169452267790SWillem de Bruijn 169552900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 169652267790SWillem de Bruijn return skb->len - orig_len; 169752267790SWillem de Bruijn } 169852267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 169952267790SWillem de Bruijn 1700753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1701753f1ca4SPavel Begunkov { 1702753f1ca4SPavel Begunkov int i; 1703753f1ca4SPavel Begunkov 1704753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1705753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1706753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1707753f1ca4SPavel Begunkov } 1708753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1709753f1ca4SPavel Begunkov 17101f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 171152267790SWillem de Bruijn gfp_t gfp_mask) 171252267790SWillem de Bruijn { 171352267790SWillem de Bruijn if (skb_zcopy(orig)) { 171452267790SWillem de Bruijn if (skb_zcopy(nskb)) { 171552267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 171652267790SWillem de Bruijn if (!gfp_mask) { 171752267790SWillem de Bruijn WARN_ON_ONCE(1); 171852267790SWillem de Bruijn return -ENOMEM; 171952267790SWillem de Bruijn } 172052267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 172152267790SWillem de Bruijn return 0; 172252267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 172352267790SWillem de Bruijn return -EIO; 172452267790SWillem de Bruijn } 172552900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 172652267790SWillem de Bruijn } 172752267790SWillem de Bruijn return 0; 172852267790SWillem de Bruijn } 172952267790SWillem de Bruijn 17302c53040fSBen Hutchings /** 17312c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 173248c83012SMichael S. Tsirkin * @skb: the skb to modify 173348c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 173448c83012SMichael S. Tsirkin * 173506b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 173648c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 173748c83012SMichael S. Tsirkin * to userspace pages. 173848c83012SMichael S. Tsirkin * 173948c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 174048c83012SMichael S. Tsirkin * %GFP_ATOMIC. 174148c83012SMichael S. Tsirkin * 174248c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 174348c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 174448c83012SMichael S. Tsirkin */ 174548c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1746a6686f2fSShirley Ma { 1747a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1748a6686f2fSShirley Ma struct page *page, *head = NULL; 17497e692df3SEric Dumazet int i, order, psize, new_frags; 17503ece7826SWillem de Bruijn u32 d_off; 1751a6686f2fSShirley Ma 17523ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 17533ece7826SWillem de Bruijn return -EINVAL; 17543ece7826SWillem de Bruijn 1755f72c4ac6SWillem de Bruijn if (!num_frags) 1756f72c4ac6SWillem de Bruijn goto release; 1757f72c4ac6SWillem de Bruijn 17587e692df3SEric Dumazet /* We might have to allocate high order pages, so compute what minimum 17597e692df3SEric Dumazet * page order is needed. 17607e692df3SEric Dumazet */ 17617e692df3SEric Dumazet order = 0; 17627e692df3SEric Dumazet while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb)) 17637e692df3SEric Dumazet order++; 17647e692df3SEric Dumazet psize = (PAGE_SIZE << order); 17657e692df3SEric Dumazet 17667e692df3SEric Dumazet new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order); 17673ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 17687e692df3SEric Dumazet page = alloc_pages(gfp_mask | __GFP_COMP, order); 1769a6686f2fSShirley Ma if (!page) { 1770a6686f2fSShirley Ma while (head) { 177140dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1772a6686f2fSShirley Ma put_page(head); 1773a6686f2fSShirley Ma head = next; 1774a6686f2fSShirley Ma } 1775a6686f2fSShirley Ma return -ENOMEM; 1776a6686f2fSShirley Ma } 17773ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 17783ece7826SWillem de Bruijn head = page; 17793ece7826SWillem de Bruijn } 17803ece7826SWillem de Bruijn 17813ece7826SWillem de Bruijn page = head; 17823ece7826SWillem de Bruijn d_off = 0; 17833ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 17843ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17853ece7826SWillem de Bruijn u32 p_off, p_len, copied; 17863ece7826SWillem de Bruijn struct page *p; 17873ece7826SWillem de Bruijn u8 *vaddr; 1788c613c209SWillem de Bruijn 1789b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1790c613c209SWillem de Bruijn p, p_off, p_len, copied) { 17913ece7826SWillem de Bruijn u32 copy, done = 0; 1792c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 17933ece7826SWillem de Bruijn 17943ece7826SWillem de Bruijn while (done < p_len) { 17957e692df3SEric Dumazet if (d_off == psize) { 17963ece7826SWillem de Bruijn d_off = 0; 17973ece7826SWillem de Bruijn page = (struct page *)page_private(page); 17983ece7826SWillem de Bruijn } 17997e692df3SEric Dumazet copy = min_t(u32, psize - d_off, p_len - done); 18003ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 18013ece7826SWillem de Bruijn vaddr + p_off + done, copy); 18023ece7826SWillem de Bruijn done += copy; 18033ece7826SWillem de Bruijn d_off += copy; 18043ece7826SWillem de Bruijn } 180551c56b00SEric Dumazet kunmap_atomic(vaddr); 1806c613c209SWillem de Bruijn } 1807a6686f2fSShirley Ma } 1808a6686f2fSShirley Ma 1809a6686f2fSShirley Ma /* skb frags release userspace buffers */ 181002756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1811a8605c60SIan Campbell skb_frag_unref(skb, i); 1812a6686f2fSShirley Ma 1813a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 18143ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 18157e692df3SEric Dumazet __skb_fill_page_desc(skb, i, head, 0, psize); 181640dadff2SSunghan Suh head = (struct page *)page_private(head); 1817a6686f2fSShirley Ma } 18183ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 18193ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 182048c83012SMichael S. Tsirkin 1821b90ddd56SWillem de Bruijn release: 18221f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1823a6686f2fSShirley Ma return 0; 1824a6686f2fSShirley Ma } 1825dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1826a6686f2fSShirley Ma 1827e0053ec0SHerbert Xu /** 1828e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1829e0053ec0SHerbert Xu * @skb: buffer to clone 1830e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1831e0053ec0SHerbert Xu * 1832e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1833e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1834e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1835e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1836e0053ec0SHerbert Xu * 1837e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1838e0053ec0SHerbert Xu * %GFP_ATOMIC. 1839e0053ec0SHerbert Xu */ 1840e0053ec0SHerbert Xu 1841e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1842e0053ec0SHerbert Xu { 1843d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1844d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1845d0bf4a9eSEric Dumazet skb1); 18466ffe75ebSEric Dumazet struct sk_buff *n; 1847e0053ec0SHerbert Xu 184870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1849a6686f2fSShirley Ma return NULL; 1850a6686f2fSShirley Ma 1851e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 18522638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 18536ffe75ebSEric Dumazet n = &fclones->skb2; 18542638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1855c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1856e0053ec0SHerbert Xu } else { 1857c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1858c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1859c93bdd0eSMel Gorman 1860025a785fSJakub Kicinski n = kmem_cache_alloc(skbuff_cache, gfp_mask); 1861e0053ec0SHerbert Xu if (!n) 1862e0053ec0SHerbert Xu return NULL; 1863fe55f6d5SVegard Nossum 1864e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1865e0053ec0SHerbert Xu } 1866e0053ec0SHerbert Xu 1867e0053ec0SHerbert Xu return __skb_clone(n, skb); 18681da177e4SLinus Torvalds } 1869b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 18701da177e4SLinus Torvalds 1871b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1872f5b17294SPravin B Shelar { 1873030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1874030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1875030737bcSEric Dumazet skb->csum_start += off; 1876f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1877f5b17294SPravin B Shelar skb->transport_header += off; 1878f5b17294SPravin B Shelar skb->network_header += off; 1879f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1880f5b17294SPravin B Shelar skb->mac_header += off; 1881f5b17294SPravin B Shelar skb->inner_transport_header += off; 1882f5b17294SPravin B Shelar skb->inner_network_header += off; 1883aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1884f5b17294SPravin B Shelar } 1885b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1886f5b17294SPravin B Shelar 188708303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 18881da177e4SLinus Torvalds { 1889dec18810SHerbert Xu __copy_skb_header(new, old); 1890dec18810SHerbert Xu 18917967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 18927967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 18937967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 18941da177e4SLinus Torvalds } 189508303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 18961da177e4SLinus Torvalds 1897c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1898c93bdd0eSMel Gorman { 1899c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1900c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1901c93bdd0eSMel Gorman return 0; 1902c93bdd0eSMel Gorman } 1903c93bdd0eSMel Gorman 19041da177e4SLinus Torvalds /** 19051da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 19061da177e4SLinus Torvalds * @skb: buffer to copy 19071da177e4SLinus Torvalds * @gfp_mask: allocation priority 19081da177e4SLinus Torvalds * 19091da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 19101da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 19111da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 19121da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 19131da177e4SLinus Torvalds * 19141da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 19151da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 19161da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 19171da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 19181da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 19191da177e4SLinus Torvalds */ 19201da177e4SLinus Torvalds 1921dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 19221da177e4SLinus Torvalds { 19236602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1924ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1925c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1926c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 19276602cebbSEric Dumazet 19281da177e4SLinus Torvalds if (!n) 19291da177e4SLinus Torvalds return NULL; 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds /* Set the data pointer */ 19321da177e4SLinus Torvalds skb_reserve(n, headerlen); 19331da177e4SLinus Torvalds /* Set the tail pointer and length */ 19341da177e4SLinus Torvalds skb_put(n, skb->len); 19351da177e4SLinus Torvalds 19369f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 19371da177e4SLinus Torvalds 193808303c18SIlya Lesokhin skb_copy_header(n, skb); 19391da177e4SLinus Torvalds return n; 19401da177e4SLinus Torvalds } 1941b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /** 1944bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 19451da177e4SLinus Torvalds * @skb: buffer to copy 1946117632e6SEric Dumazet * @headroom: headroom of new skb 19471da177e4SLinus Torvalds * @gfp_mask: allocation priority 1948bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1949bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1950bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 19511da177e4SLinus Torvalds * 19521da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 19531da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 19541da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 19551da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 19561da177e4SLinus Torvalds * or the pointer to the buffer on success. 19571da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 19581da177e4SLinus Torvalds */ 19591da177e4SLinus Torvalds 1960bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1961bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 19621da177e4SLinus Torvalds { 1963117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1964bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1965bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 19666602cebbSEric Dumazet 19671da177e4SLinus Torvalds if (!n) 19681da177e4SLinus Torvalds goto out; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* Set the data pointer */ 1971117632e6SEric Dumazet skb_reserve(n, headroom); 19721da177e4SLinus Torvalds /* Set the tail pointer and length */ 19731da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 19741da177e4SLinus Torvalds /* Copy the bytes */ 1975d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 19761da177e4SLinus Torvalds 197725f484a6SHerbert Xu n->truesize += skb->data_len; 19781da177e4SLinus Torvalds n->data_len = skb->data_len; 19791da177e4SLinus Torvalds n->len = skb->len; 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 19821da177e4SLinus Torvalds int i; 19831da177e4SLinus Torvalds 19841f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 19851f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 19861511022cSDan Carpenter kfree_skb(n); 19871511022cSDan Carpenter n = NULL; 1988a6686f2fSShirley Ma goto out; 1989a6686f2fSShirley Ma } 19901da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19911da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1992ea2ab693SIan Campbell skb_frag_ref(skb, i); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 199721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 19981da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 19991da177e4SLinus Torvalds skb_clone_fraglist(n); 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 200208303c18SIlya Lesokhin skb_copy_header(n, skb); 20031da177e4SLinus Torvalds out: 20041da177e4SLinus Torvalds return n; 20051da177e4SLinus Torvalds } 2006bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /** 20091da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 20101da177e4SLinus Torvalds * @skb: buffer to reallocate 20111da177e4SLinus Torvalds * @nhead: room to add at head 20121da177e4SLinus Torvalds * @ntail: room to add at tail 20131da177e4SLinus Torvalds * @gfp_mask: allocation priority 20141da177e4SLinus Torvalds * 2015bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 2016bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 20171da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 20181da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 20191da177e4SLinus Torvalds * 20201da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 20211da177e4SLinus Torvalds * reloaded after call to this function. 20221da177e4SLinus Torvalds */ 20231da177e4SLinus Torvalds 202486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 2025dd0fc66fSAl Viro gfp_t gfp_mask) 20261da177e4SLinus Torvalds { 202712d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 202812d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 20291da177e4SLinus Torvalds long off; 2030158f323bSEric Dumazet u8 *data; 203112d6c1d3SKees Cook int i; 20321da177e4SLinus Torvalds 20334edd87adSHerbert Xu BUG_ON(nhead < 0); 20344edd87adSHerbert Xu 20359f77fad3STim Hansen BUG_ON(skb_shared(skb)); 20361da177e4SLinus Torvalds 2037753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 2038753f1ca4SPavel Begunkov 2039c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 2040c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 204112d6c1d3SKees Cook 20425c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 20431da177e4SLinus Torvalds if (!data) 20441da177e4SLinus Torvalds goto nodata; 204512d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 20486602cebbSEric Dumazet * optimized for the cases when header is void. 20496602cebbSEric Dumazet */ 20506602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 20516602cebbSEric Dumazet 20526602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 20536602cebbSEric Dumazet skb_shinfo(skb), 2054fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 20551da177e4SLinus Torvalds 20563e24591aSAlexander Duyck /* 20573e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 20583e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 20593e24591aSAlexander Duyck * be since all we did is relocate the values 20603e24591aSAlexander Duyck */ 20613e24591aSAlexander Duyck if (skb_cloned(skb)) { 206270008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 2063a6686f2fSShirley Ma goto nofrags; 20641f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 2065c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 20661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2067ea2ab693SIan Campbell skb_frag_ref(skb, i); 20681da177e4SLinus Torvalds 206921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 20701da177e4SLinus Torvalds skb_clone_fraglist(skb); 20711da177e4SLinus Torvalds 2072b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 20733e24591aSAlexander Duyck } else { 2074b07a2d97SJakub Kicinski skb_free_head(skb, false); 20751fd63041SEric Dumazet } 20761da177e4SLinus Torvalds off = (data + nhead) - skb->head; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds skb->head = data; 2079d3836f21SEric Dumazet skb->head_frag = 0; 20801da177e4SLinus Torvalds skb->data += off; 2081763087daSEric Dumazet 2082763087daSEric Dumazet skb_set_end_offset(skb, size); 20834305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 208456eb8882SPatrick McHardy off = nhead; 208556eb8882SPatrick McHardy #endif 208627a884dcSArnaldo Carvalho de Melo skb->tail += off; 2087b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 20881da177e4SLinus Torvalds skb->cloned = 0; 2089334a8132SPatrick McHardy skb->hdr_len = 0; 20901da177e4SLinus Torvalds skb->nohdr = 0; 20911da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 2092158f323bSEric Dumazet 2093de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 2094de8f3a83SDaniel Borkmann 2095158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 2096158f323bSEric Dumazet * For the moment, we really care of rx path, or 2097158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 2098158f323bSEric Dumazet */ 2099158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2100158f323bSEric Dumazet skb->truesize += size - osize; 2101158f323bSEric Dumazet 21021da177e4SLinus Torvalds return 0; 21031da177e4SLinus Torvalds 2104a6686f2fSShirley Ma nofrags: 2105bf9f1baaSEric Dumazet skb_kfree_head(data, size); 21061da177e4SLinus Torvalds nodata: 21071da177e4SLinus Torvalds return -ENOMEM; 21081da177e4SLinus Torvalds } 2109b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 21141da177e4SLinus Torvalds { 21151da177e4SLinus Torvalds struct sk_buff *skb2; 21161da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds if (delta <= 0) 21191da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 21201da177e4SLinus Torvalds else { 21211da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 21221da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 21231da177e4SLinus Torvalds GFP_ATOMIC)) { 21241da177e4SLinus Torvalds kfree_skb(skb2); 21251da177e4SLinus Torvalds skb2 = NULL; 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds return skb2; 21291da177e4SLinus Torvalds } 2130b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 21311da177e4SLinus Torvalds 2132880ce5f2SEric Dumazet /* Note: We plan to rework this in linux-6.4 */ 21332b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 21342b88cba5SEric Dumazet { 21352b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 21362b88cba5SEric Dumazet struct skb_shared_info *shinfo; 21372b88cba5SEric Dumazet int res; 21382b88cba5SEric Dumazet 21392b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 21402b88cba5SEric Dumazet saved_truesize = skb->truesize; 21412b88cba5SEric Dumazet 21422b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 21432b88cba5SEric Dumazet if (res) 21442b88cba5SEric Dumazet return res; 21452b88cba5SEric Dumazet 21462b88cba5SEric Dumazet skb->truesize = saved_truesize; 21472b88cba5SEric Dumazet 21482b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 21492b88cba5SEric Dumazet return 0; 21502b88cba5SEric Dumazet 2151880ce5f2SEric Dumazet /* We can not change skb->end if the original or new value 2152880ce5f2SEric Dumazet * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head(). 2153880ce5f2SEric Dumazet */ 2154880ce5f2SEric Dumazet if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM || 2155880ce5f2SEric Dumazet skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) { 2156880ce5f2SEric Dumazet /* We think this path should not be taken. 2157880ce5f2SEric Dumazet * Add a temporary trace to warn us just in case. 2158880ce5f2SEric Dumazet */ 2159880ce5f2SEric Dumazet pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n", 2160880ce5f2SEric Dumazet saved_end_offset, skb_end_offset(skb)); 2161880ce5f2SEric Dumazet WARN_ON_ONCE(1); 2162880ce5f2SEric Dumazet return 0; 2163880ce5f2SEric Dumazet } 2164880ce5f2SEric Dumazet 21652b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 21662b88cba5SEric Dumazet 21672b88cba5SEric Dumazet /* We are about to change back skb->end, 21682b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 21692b88cba5SEric Dumazet */ 21702b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 21712b88cba5SEric Dumazet shinfo, 21722b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 21732b88cba5SEric Dumazet 21742b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 21752b88cba5SEric Dumazet 21762b88cba5SEric Dumazet return 0; 21772b88cba5SEric Dumazet } 21782b88cba5SEric Dumazet 21791da177e4SLinus Torvalds /** 2180f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2181f1260ff1SVasily Averin * @skb: buffer to reallocate 2182f1260ff1SVasily Averin * @headroom: needed headroom 2183f1260ff1SVasily Averin * 2184f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2185f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2186f1260ff1SVasily Averin * and frees original skb in case of failures. 2187f1260ff1SVasily Averin * 2188f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2189f1260ff1SVasily Averin */ 2190f1260ff1SVasily Averin 2191f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2192f1260ff1SVasily Averin { 2193f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 21947f678defSVasily Averin int osize = skb_end_offset(skb); 21957f678defSVasily Averin struct sock *sk = skb->sk; 2196f1260ff1SVasily Averin 2197f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2198f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2199f1260ff1SVasily Averin return skb; 2200f1260ff1SVasily Averin 22017f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 22027f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 22037f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2204f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2205f1260ff1SVasily Averin 22067f678defSVasily Averin if (unlikely(!nskb)) 22077f678defSVasily Averin goto fail; 22087f678defSVasily Averin 22097f678defSVasily Averin if (sk) 22107f678defSVasily Averin skb_set_owner_w(nskb, sk); 2211f1260ff1SVasily Averin consume_skb(skb); 2212f1260ff1SVasily Averin skb = nskb; 2213f1260ff1SVasily Averin } 22147f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 22157f678defSVasily Averin goto fail; 22167f678defSVasily Averin 22177f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 22187f678defSVasily Averin delta = skb_end_offset(skb) - osize; 22197f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 22207f678defSVasily Averin skb->truesize += delta; 2221f1260ff1SVasily Averin } 2222f1260ff1SVasily Averin return skb; 22237f678defSVasily Averin 22247f678defSVasily Averin fail: 22257f678defSVasily Averin kfree_skb(skb); 22267f678defSVasily Averin return NULL; 2227f1260ff1SVasily Averin } 2228f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2229f1260ff1SVasily Averin 2230f1260ff1SVasily Averin /** 22311da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 22321da177e4SLinus Torvalds * @skb: buffer to copy 22331da177e4SLinus Torvalds * @newheadroom: new free bytes at head 22341da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 22351da177e4SLinus Torvalds * @gfp_mask: allocation priority 22361da177e4SLinus Torvalds * 22371da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 22381da177e4SLinus Torvalds * allocate additional space. 22391da177e4SLinus Torvalds * 22401da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 22411da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 22421da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 22431da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 22441da177e4SLinus Torvalds * 22451da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 22461da177e4SLinus Torvalds * is called from an interrupt. 22471da177e4SLinus Torvalds */ 22481da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 224986a76cafSVictor Fusco int newheadroom, int newtailroom, 2250dd0fc66fSAl Viro gfp_t gfp_mask) 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds /* 22531da177e4SLinus Torvalds * Allocate the copy buffer 22541da177e4SLinus Torvalds */ 2255c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2256c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2257c93bdd0eSMel Gorman NUMA_NO_NODE); 2258efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 22591da177e4SLinus Torvalds int head_copy_len, head_copy_off; 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds if (!n) 22621da177e4SLinus Torvalds return NULL; 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds skb_reserve(n, newheadroom); 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds /* Set the tail pointer and length */ 22671da177e4SLinus Torvalds skb_put(n, skb->len); 22681da177e4SLinus Torvalds 2269efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 22701da177e4SLinus Torvalds head_copy_off = 0; 22711da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 22721da177e4SLinus Torvalds head_copy_len = newheadroom; 22731da177e4SLinus Torvalds else 22741da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds /* Copy the linear header and data. */ 22779f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 22789f77fad3STim Hansen skb->len + head_copy_len)); 22791da177e4SLinus Torvalds 228008303c18SIlya Lesokhin skb_copy_header(n, skb); 22811da177e4SLinus Torvalds 2282030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2283efd1e8d5SPatrick McHardy 22841da177e4SLinus Torvalds return n; 22851da177e4SLinus Torvalds } 2286b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds /** 2289cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 22901da177e4SLinus Torvalds * @skb: buffer to pad 22911da177e4SLinus Torvalds * @pad: space to pad 2292cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 22931da177e4SLinus Torvalds * 22941da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 22951da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 22961da177e4SLinus Torvalds * beyond the buffer end onto the wire. 22971da177e4SLinus Torvalds * 2298cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2299cd0a137aSFlorian Fainelli * if @free_on_error is true. 23001da177e4SLinus Torvalds */ 23011da177e4SLinus Torvalds 2302cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 23031da177e4SLinus Torvalds { 23045b057c6bSHerbert Xu int err; 23055b057c6bSHerbert Xu int ntail; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 23085b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 23091da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 23105b057c6bSHerbert Xu return 0; 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23134305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 23145b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 23155b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 23165b057c6bSHerbert Xu if (unlikely(err)) 23175b057c6bSHerbert Xu goto free_skb; 23185b057c6bSHerbert Xu } 23195b057c6bSHerbert Xu 23205b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 23215b057c6bSHerbert Xu * to be audited. 23225b057c6bSHerbert Xu */ 23235b057c6bSHerbert Xu err = skb_linearize(skb); 23245b057c6bSHerbert Xu if (unlikely(err)) 23255b057c6bSHerbert Xu goto free_skb; 23265b057c6bSHerbert Xu 23275b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 23285b057c6bSHerbert Xu return 0; 23295b057c6bSHerbert Xu 23305b057c6bSHerbert Xu free_skb: 2331cd0a137aSFlorian Fainelli if (free_on_error) 23321da177e4SLinus Torvalds kfree_skb(skb); 23335b057c6bSHerbert Xu return err; 23341da177e4SLinus Torvalds } 2335cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 23361da177e4SLinus Torvalds 23370dde3e16SIlpo Järvinen /** 23380c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 23390c7ddf36SMathias Krause * @skb: start of the buffer to use 23400c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 23410c7ddf36SMathias Krause * @len: amount of data to add 23420c7ddf36SMathias Krause * 23430c7ddf36SMathias Krause * This function extends the used data area of the potentially 23440c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 23450c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 23460c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 23470c7ddf36SMathias Krause * returned. 23480c7ddf36SMathias Krause */ 23490c7ddf36SMathias Krause 23504df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 23510c7ddf36SMathias Krause { 23520c7ddf36SMathias Krause if (tail != skb) { 23530c7ddf36SMathias Krause skb->data_len += len; 23540c7ddf36SMathias Krause skb->len += len; 23550c7ddf36SMathias Krause } 23560c7ddf36SMathias Krause return skb_put(tail, len); 23570c7ddf36SMathias Krause } 23580c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 23590c7ddf36SMathias Krause 23600c7ddf36SMathias Krause /** 23610dde3e16SIlpo Järvinen * skb_put - add data to a buffer 23620dde3e16SIlpo Järvinen * @skb: buffer to use 23630dde3e16SIlpo Järvinen * @len: amount of data to add 23640dde3e16SIlpo Järvinen * 23650dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 23660dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 23670dde3e16SIlpo Järvinen * first byte of the extra data is returned. 23680dde3e16SIlpo Järvinen */ 23694df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 23700dde3e16SIlpo Järvinen { 23714df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 23720dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 23730dde3e16SIlpo Järvinen skb->tail += len; 23740dde3e16SIlpo Järvinen skb->len += len; 23750dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 23760dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 23770dde3e16SIlpo Järvinen return tmp; 23780dde3e16SIlpo Järvinen } 23790dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 23800dde3e16SIlpo Järvinen 23816be8ac2fSIlpo Järvinen /** 2382c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2383c2aa270aSIlpo Järvinen * @skb: buffer to use 2384c2aa270aSIlpo Järvinen * @len: amount of data to add 2385c2aa270aSIlpo Järvinen * 2386c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2387c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2388c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2389c2aa270aSIlpo Järvinen */ 2390d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2391c2aa270aSIlpo Järvinen { 2392c2aa270aSIlpo Järvinen skb->data -= len; 2393c2aa270aSIlpo Järvinen skb->len += len; 2394c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2395c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2396c2aa270aSIlpo Järvinen return skb->data; 2397c2aa270aSIlpo Järvinen } 2398c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2399c2aa270aSIlpo Järvinen 2400c2aa270aSIlpo Järvinen /** 24016be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 24026be8ac2fSIlpo Järvinen * @skb: buffer to use 24036be8ac2fSIlpo Järvinen * @len: amount of data to remove 24046be8ac2fSIlpo Järvinen * 24056be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 24066be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 24076be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 24086be8ac2fSIlpo Järvinen * the old data. 24096be8ac2fSIlpo Järvinen */ 2410af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 24116be8ac2fSIlpo Järvinen { 241247d29646SDavid S. Miller return skb_pull_inline(skb, len); 24136be8ac2fSIlpo Järvinen } 24146be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 24156be8ac2fSIlpo Järvinen 2416419ae74eSIlpo Järvinen /** 241713244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 241813244cccSLuiz Augusto von Dentz * original position. 241913244cccSLuiz Augusto von Dentz * @skb: buffer to use 242013244cccSLuiz Augusto von Dentz * @len: amount of data to remove 242113244cccSLuiz Augusto von Dentz * 242213244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 242313244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 242413244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 242513244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 242613244cccSLuiz Augusto von Dentz */ 242713244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 242813244cccSLuiz Augusto von Dentz { 242913244cccSLuiz Augusto von Dentz void *data = skb->data; 243013244cccSLuiz Augusto von Dentz 243113244cccSLuiz Augusto von Dentz if (skb->len < len) 243213244cccSLuiz Augusto von Dentz return NULL; 243313244cccSLuiz Augusto von Dentz 243413244cccSLuiz Augusto von Dentz skb_pull(skb, len); 243513244cccSLuiz Augusto von Dentz 243613244cccSLuiz Augusto von Dentz return data; 243713244cccSLuiz Augusto von Dentz } 243813244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 243913244cccSLuiz Augusto von Dentz 244013244cccSLuiz Augusto von Dentz /** 2441419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2442419ae74eSIlpo Järvinen * @skb: buffer to alter 2443419ae74eSIlpo Järvinen * @len: new length 2444419ae74eSIlpo Järvinen * 2445419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2446419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2447419ae74eSIlpo Järvinen * The skb must be linear. 2448419ae74eSIlpo Järvinen */ 2449419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2450419ae74eSIlpo Järvinen { 2451419ae74eSIlpo Järvinen if (skb->len > len) 2452419ae74eSIlpo Järvinen __skb_trim(skb, len); 2453419ae74eSIlpo Järvinen } 2454419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2455419ae74eSIlpo Järvinen 24563cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 24571da177e4SLinus Torvalds */ 24581da177e4SLinus Torvalds 24593cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 24601da177e4SLinus Torvalds { 246127b437c8SHerbert Xu struct sk_buff **fragp; 246227b437c8SHerbert Xu struct sk_buff *frag; 24631da177e4SLinus Torvalds int offset = skb_headlen(skb); 24641da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 24651da177e4SLinus Torvalds int i; 246627b437c8SHerbert Xu int err; 246727b437c8SHerbert Xu 246827b437c8SHerbert Xu if (skb_cloned(skb) && 246927b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 247027b437c8SHerbert Xu return err; 24711da177e4SLinus Torvalds 2472f4d26fb3SHerbert Xu i = 0; 2473f4d26fb3SHerbert Xu if (offset >= len) 2474f4d26fb3SHerbert Xu goto drop_pages; 2475f4d26fb3SHerbert Xu 2476f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 24779e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 247827b437c8SHerbert Xu 247927b437c8SHerbert Xu if (end < len) { 24801da177e4SLinus Torvalds offset = end; 248127b437c8SHerbert Xu continue; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 24849e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 248527b437c8SHerbert Xu 2486f4d26fb3SHerbert Xu drop_pages: 248727b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 248827b437c8SHerbert Xu 248927b437c8SHerbert Xu for (; i < nfrags; i++) 2490ea2ab693SIan Campbell skb_frag_unref(skb, i); 249127b437c8SHerbert Xu 249221dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 249327b437c8SHerbert Xu skb_drop_fraglist(skb); 2494f4d26fb3SHerbert Xu goto done; 249527b437c8SHerbert Xu } 249627b437c8SHerbert Xu 249727b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 249827b437c8SHerbert Xu fragp = &frag->next) { 249927b437c8SHerbert Xu int end = offset + frag->len; 250027b437c8SHerbert Xu 250127b437c8SHerbert Xu if (skb_shared(frag)) { 250227b437c8SHerbert Xu struct sk_buff *nfrag; 250327b437c8SHerbert Xu 250427b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 250527b437c8SHerbert Xu if (unlikely(!nfrag)) 250627b437c8SHerbert Xu return -ENOMEM; 250727b437c8SHerbert Xu 250827b437c8SHerbert Xu nfrag->next = frag->next; 250985bb2a60SEric Dumazet consume_skb(frag); 251027b437c8SHerbert Xu frag = nfrag; 251127b437c8SHerbert Xu *fragp = frag; 251227b437c8SHerbert Xu } 251327b437c8SHerbert Xu 251427b437c8SHerbert Xu if (end < len) { 251527b437c8SHerbert Xu offset = end; 251627b437c8SHerbert Xu continue; 251727b437c8SHerbert Xu } 251827b437c8SHerbert Xu 251927b437c8SHerbert Xu if (end > len && 252027b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 252127b437c8SHerbert Xu return err; 252227b437c8SHerbert Xu 252327b437c8SHerbert Xu if (frag->next) 252427b437c8SHerbert Xu skb_drop_list(&frag->next); 252527b437c8SHerbert Xu break; 252627b437c8SHerbert Xu } 252727b437c8SHerbert Xu 2528f4d26fb3SHerbert Xu done: 252927b437c8SHerbert Xu if (len > skb_headlen(skb)) { 25301da177e4SLinus Torvalds skb->data_len -= skb->len - len; 25311da177e4SLinus Torvalds skb->len = len; 25321da177e4SLinus Torvalds } else { 25331da177e4SLinus Torvalds skb->len = len; 25341da177e4SLinus Torvalds skb->data_len = 0; 253527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 2538c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2539c21b48ccSEric Dumazet skb_condense(skb); 25401da177e4SLinus Torvalds return 0; 25411da177e4SLinus Torvalds } 2542b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 25431da177e4SLinus Torvalds 254488078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 254588078d98SEric Dumazet */ 254688078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 254788078d98SEric Dumazet { 254888078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 254988078d98SEric Dumazet int delta = skb->len - len; 255088078d98SEric Dumazet 2551d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2552d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2553d55bef50SDimitris Michailidis len); 255454970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 255554970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 255654970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 255754970a2fSVasily Averin 255854970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 255954970a2fSVasily Averin return -EINVAL; 256088078d98SEric Dumazet } 256188078d98SEric Dumazet return __pskb_trim(skb, len); 256288078d98SEric Dumazet } 256388078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 256488078d98SEric Dumazet 25651da177e4SLinus Torvalds /** 25661da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 25671da177e4SLinus Torvalds * @skb: buffer to reallocate 25681da177e4SLinus Torvalds * @delta: number of bytes to advance tail 25691da177e4SLinus Torvalds * 25701da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 25711da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 25721da177e4SLinus Torvalds * data from fragmented part. 25731da177e4SLinus Torvalds * 25741da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 25751da177e4SLinus Torvalds * 25761da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 25771da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 25781da177e4SLinus Torvalds * 25791da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 25801da177e4SLinus Torvalds * reloaded after call to this function. 25811da177e4SLinus Torvalds */ 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 25841da177e4SLinus Torvalds * when it is necessary. 25851da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 25861da177e4SLinus Torvalds * 2. It may change skb pointers. 25871da177e4SLinus Torvalds * 25881da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 25891da177e4SLinus Torvalds */ 2590af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 25911da177e4SLinus Torvalds { 25921da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 25931da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 25941da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 25951da177e4SLinus Torvalds */ 25964305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 25991da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 26001da177e4SLinus Torvalds GFP_ATOMIC)) 26011da177e4SLinus Torvalds return NULL; 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds 26049f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 26059f77fad3STim Hansen skb_tail_pointer(skb), delta)); 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 26081da177e4SLinus Torvalds * size of pulled pages. Superb. 26091da177e4SLinus Torvalds */ 261021dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 26111da177e4SLinus Torvalds goto pull_pages; 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 26141da177e4SLinus Torvalds eat = delta; 26151da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26169e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26179e903e08SEric Dumazet 26189e903e08SEric Dumazet if (size >= eat) 26191da177e4SLinus Torvalds goto pull_pages; 26209e903e08SEric Dumazet eat -= size; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 262409001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 26251da177e4SLinus Torvalds * but taking into account that pulling is expected to 26261da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 26271da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 26281da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 26291da177e4SLinus Torvalds */ 26301da177e4SLinus Torvalds if (eat) { 26311da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 26321da177e4SLinus Torvalds struct sk_buff *clone = NULL; 26331da177e4SLinus Torvalds struct sk_buff *insp = NULL; 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds do { 26361da177e4SLinus Torvalds if (list->len <= eat) { 26371da177e4SLinus Torvalds /* Eaten as whole. */ 26381da177e4SLinus Torvalds eat -= list->len; 26391da177e4SLinus Torvalds list = list->next; 26401da177e4SLinus Torvalds insp = list; 26411da177e4SLinus Torvalds } else { 26421da177e4SLinus Torvalds /* Eaten partially. */ 26432d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 26442d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 26452d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds if (skb_shared(list)) { 26481da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 26491da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 26501da177e4SLinus Torvalds if (!clone) 26511da177e4SLinus Torvalds return NULL; 26521da177e4SLinus Torvalds insp = list->next; 26531da177e4SLinus Torvalds list = clone; 26541da177e4SLinus Torvalds } else { 26551da177e4SLinus Torvalds /* This may be pulled without 26561da177e4SLinus Torvalds * problems. */ 26571da177e4SLinus Torvalds insp = list; 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 26601da177e4SLinus Torvalds kfree_skb(clone); 26611da177e4SLinus Torvalds return NULL; 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds break; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds } while (eat); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds /* Free pulled out fragments. */ 26681da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 26691da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2670ef527f96SEric Dumazet consume_skb(list); 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds /* And insert new clone at head. */ 26731da177e4SLinus Torvalds if (clone) { 26741da177e4SLinus Torvalds clone->next = list; 26751da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds pull_pages: 26811da177e4SLinus Torvalds eat = delta; 26821da177e4SLinus Torvalds k = 0; 26831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26849e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 26859e903e08SEric Dumazet 26869e903e08SEric Dumazet if (size <= eat) { 2687ea2ab693SIan Campbell skb_frag_unref(skb, i); 26889e903e08SEric Dumazet eat -= size; 26891da177e4SLinus Torvalds } else { 2690b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2691b54c9d5bSJonathan Lemon 2692b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 26931da177e4SLinus Torvalds if (eat) { 2694b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2695b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 26963ccc6c6fSlinzhang if (!i) 26973ccc6c6fSlinzhang goto end; 26981da177e4SLinus Torvalds eat = 0; 26991da177e4SLinus Torvalds } 27001da177e4SLinus Torvalds k++; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 27041da177e4SLinus Torvalds 27053ccc6c6fSlinzhang end: 27061da177e4SLinus Torvalds skb->tail += delta; 27071da177e4SLinus Torvalds skb->data_len -= delta; 27081da177e4SLinus Torvalds 27091f8b977aSWillem de Bruijn if (!skb->data_len) 27101f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 27111f8b977aSWillem de Bruijn 271227a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 27131da177e4SLinus Torvalds } 2714b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 27151da177e4SLinus Torvalds 271622019b17SEric Dumazet /** 271722019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 271822019b17SEric Dumazet * @skb: source skb 271922019b17SEric Dumazet * @offset: offset in source 272022019b17SEric Dumazet * @to: destination buffer 272122019b17SEric Dumazet * @len: number of bytes to copy 272222019b17SEric Dumazet * 272322019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 272422019b17SEric Dumazet * destination buffer. 272522019b17SEric Dumazet * 272622019b17SEric Dumazet * CAUTION ! : 272722019b17SEric Dumazet * If its prototype is ever changed, 272822019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 272922019b17SEric Dumazet * since it is called from BPF assembly code. 273022019b17SEric Dumazet */ 27311da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 27321da177e4SLinus Torvalds { 27331a028e50SDavid S. Miller int start = skb_headlen(skb); 2734fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2735fbb398a8SDavid S. Miller int i, copy; 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds if (offset > (int)skb->len - len) 27381da177e4SLinus Torvalds goto fault; 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds /* Copy header. */ 27411a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 27421da177e4SLinus Torvalds if (copy > len) 27431da177e4SLinus Torvalds copy = len; 2744d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 27451da177e4SLinus Torvalds if ((len -= copy) == 0) 27461da177e4SLinus Torvalds return 0; 27471da177e4SLinus Torvalds offset += copy; 27481da177e4SLinus Torvalds to += copy; 27491da177e4SLinus Torvalds } 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27521a028e50SDavid S. Miller int end; 275351c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 27541da177e4SLinus Torvalds 2755547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27561a028e50SDavid S. Miller 275751c56b00SEric Dumazet end = start + skb_frag_size(f); 27581da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2759c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2760c613c209SWillem de Bruijn struct page *p; 27611da177e4SLinus Torvalds u8 *vaddr; 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds if (copy > len) 27641da177e4SLinus Torvalds copy = len; 27651da177e4SLinus Torvalds 2766c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2767b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2768c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2769c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2770c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 277151c56b00SEric Dumazet kunmap_atomic(vaddr); 2772c613c209SWillem de Bruijn } 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds if ((len -= copy) == 0) 27751da177e4SLinus Torvalds return 0; 27761da177e4SLinus Torvalds offset += copy; 27771da177e4SLinus Torvalds to += copy; 27781da177e4SLinus Torvalds } 27791a028e50SDavid S. Miller start = end; 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds 2782fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 27831a028e50SDavid S. Miller int end; 27841da177e4SLinus Torvalds 2785547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27861a028e50SDavid S. Miller 2787fbb398a8SDavid S. Miller end = start + frag_iter->len; 27881da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 27891da177e4SLinus Torvalds if (copy > len) 27901da177e4SLinus Torvalds copy = len; 2791fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 27921da177e4SLinus Torvalds goto fault; 27931da177e4SLinus Torvalds if ((len -= copy) == 0) 27941da177e4SLinus Torvalds return 0; 27951da177e4SLinus Torvalds offset += copy; 27961da177e4SLinus Torvalds to += copy; 27971da177e4SLinus Torvalds } 27981a028e50SDavid S. Miller start = end; 27991da177e4SLinus Torvalds } 2800a6686f2fSShirley Ma 28011da177e4SLinus Torvalds if (!len) 28021da177e4SLinus Torvalds return 0; 28031da177e4SLinus Torvalds 28041da177e4SLinus Torvalds fault: 28051da177e4SLinus Torvalds return -EFAULT; 28061da177e4SLinus Torvalds } 2807b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 28081da177e4SLinus Torvalds 28099c55e01cSJens Axboe /* 28109c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 28119c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 28129c55e01cSJens Axboe */ 28139c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 28149c55e01cSJens Axboe { 28158b9d3728SJarek Poplawski put_page(spd->pages[i]); 28168b9d3728SJarek Poplawski } 28179c55e01cSJens Axboe 2818a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 28194fb66994SJarek Poplawski unsigned int *offset, 282018aafc62SEric Dumazet struct sock *sk) 28218b9d3728SJarek Poplawski { 28225640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 28238b9d3728SJarek Poplawski 28245640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 28258b9d3728SJarek Poplawski return NULL; 28264fb66994SJarek Poplawski 28275640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 28284fb66994SJarek Poplawski 28295640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 28305640f768SEric Dumazet page_address(page) + *offset, *len); 28315640f768SEric Dumazet *offset = pfrag->offset; 28325640f768SEric Dumazet pfrag->offset += *len; 28334fb66994SJarek Poplawski 28345640f768SEric Dumazet return pfrag->page; 28359c55e01cSJens Axboe } 28369c55e01cSJens Axboe 283741c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 283841c73a0dSEric Dumazet struct page *page, 283941c73a0dSEric Dumazet unsigned int offset) 284041c73a0dSEric Dumazet { 284141c73a0dSEric Dumazet return spd->nr_pages && 284241c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 284341c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 284441c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 284541c73a0dSEric Dumazet } 284641c73a0dSEric Dumazet 28479c55e01cSJens Axboe /* 28489c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 28499c55e01cSJens Axboe */ 2850a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 285135f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 28524fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 285318aafc62SEric Dumazet bool linear, 28547a67e56fSJarek Poplawski struct sock *sk) 28559c55e01cSJens Axboe { 285641c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2857a108d5f3SDavid S. Miller return true; 28589c55e01cSJens Axboe 28598b9d3728SJarek Poplawski if (linear) { 286018aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 28618b9d3728SJarek Poplawski if (!page) 2862a108d5f3SDavid S. Miller return true; 286341c73a0dSEric Dumazet } 286441c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 286541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2866a108d5f3SDavid S. Miller return false; 286741c73a0dSEric Dumazet } 28688b9d3728SJarek Poplawski get_page(page); 28699c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 28704fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 28719c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 28729c55e01cSJens Axboe spd->nr_pages++; 28738b9d3728SJarek Poplawski 2874a108d5f3SDavid S. Miller return false; 28759c55e01cSJens Axboe } 28769c55e01cSJens Axboe 2877a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 28782870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 287918aafc62SEric Dumazet unsigned int *len, 2880d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 288135f3d14dSJens Axboe struct sock *sk, 288235f3d14dSJens Axboe struct pipe_inode_info *pipe) 28839c55e01cSJens Axboe { 28842870c43dSOctavian Purdila if (!*len) 2885a108d5f3SDavid S. Miller return true; 28869c55e01cSJens Axboe 28872870c43dSOctavian Purdila /* skip this segment if already processed */ 28882870c43dSOctavian Purdila if (*off >= plen) { 28892870c43dSOctavian Purdila *off -= plen; 2890a108d5f3SDavid S. Miller return false; 28912870c43dSOctavian Purdila } 28922870c43dSOctavian Purdila 28932870c43dSOctavian Purdila /* ignore any bits we already processed */ 28949ca1b22dSEric Dumazet poff += *off; 28959ca1b22dSEric Dumazet plen -= *off; 28962870c43dSOctavian Purdila *off = 0; 28972870c43dSOctavian Purdila 289818aafc62SEric Dumazet do { 289918aafc62SEric Dumazet unsigned int flen = min(*len, plen); 29002870c43dSOctavian Purdila 290118aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 290218aafc62SEric Dumazet linear, sk)) 2903a108d5f3SDavid S. Miller return true; 290418aafc62SEric Dumazet poff += flen; 290518aafc62SEric Dumazet plen -= flen; 29062870c43dSOctavian Purdila *len -= flen; 290718aafc62SEric Dumazet } while (*len && plen); 29082870c43dSOctavian Purdila 2909a108d5f3SDavid S. Miller return false; 2910db43a282SOctavian Purdila } 29119c55e01cSJens Axboe 29129c55e01cSJens Axboe /* 2913a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 29142870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 29159c55e01cSJens Axboe */ 2916a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 291735f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 291835f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 29192870c43dSOctavian Purdila { 29202870c43dSOctavian Purdila int seg; 2921fa9835e5STom Herbert struct sk_buff *iter; 29229c55e01cSJens Axboe 29231d0c0b32SEric Dumazet /* map the linear part : 29242996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 29252996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 29262996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 29279c55e01cSJens Axboe */ 29282870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 29292870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 29302870c43dSOctavian Purdila skb_headlen(skb), 293118aafc62SEric Dumazet offset, len, spd, 29323a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 29331d0c0b32SEric Dumazet sk, pipe)) 2934a108d5f3SDavid S. Miller return true; 29359c55e01cSJens Axboe 29369c55e01cSJens Axboe /* 29379c55e01cSJens Axboe * then map the fragments 29389c55e01cSJens Axboe */ 29399c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 29409c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 29419c55e01cSJens Axboe 2942ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2943b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 294418aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2945a108d5f3SDavid S. Miller return true; 29469c55e01cSJens Axboe } 29479c55e01cSJens Axboe 2948fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2949fa9835e5STom Herbert if (*offset >= iter->len) { 2950fa9835e5STom Herbert *offset -= iter->len; 2951fa9835e5STom Herbert continue; 2952fa9835e5STom Herbert } 2953fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2954fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2955fa9835e5STom Herbert * case. 2956fa9835e5STom Herbert */ 2957fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2958fa9835e5STom Herbert return true; 2959fa9835e5STom Herbert } 2960fa9835e5STom Herbert 2961a108d5f3SDavid S. Miller return false; 29629c55e01cSJens Axboe } 29639c55e01cSJens Axboe 29649c55e01cSJens Axboe /* 29659c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2966fa9835e5STom Herbert * the fragments, and the frag list. 29679c55e01cSJens Axboe */ 2968a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 29699c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 297025869262SAl Viro unsigned int flags) 29719c55e01cSJens Axboe { 297241c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 297341c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 29749c55e01cSJens Axboe struct splice_pipe_desc spd = { 29759c55e01cSJens Axboe .pages = pages, 29769c55e01cSJens Axboe .partial = partial, 2977047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 297828a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 29799c55e01cSJens Axboe .spd_release = sock_spd_release, 29809c55e01cSJens Axboe }; 298135f3d14dSJens Axboe int ret = 0; 298235f3d14dSJens Axboe 2983fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 29849c55e01cSJens Axboe 2985a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 298625869262SAl Viro ret = splice_to_pipe(pipe, &spd); 29879c55e01cSJens Axboe 298835f3d14dSJens Axboe return ret; 29899c55e01cSJens Axboe } 29902b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 29919c55e01cSJens Axboe 2992c729ed6fSDavid Howells static int sendmsg_locked(struct sock *sk, struct msghdr *msg) 2993c729ed6fSDavid Howells { 2994c729ed6fSDavid Howells struct socket *sock = sk->sk_socket; 2995c729ed6fSDavid Howells size_t size = msg_data_left(msg); 2996c729ed6fSDavid Howells 2997c729ed6fSDavid Howells if (!sock) 2998c729ed6fSDavid Howells return -EINVAL; 2999c729ed6fSDavid Howells 3000c729ed6fSDavid Howells if (!sock->ops->sendmsg_locked) 3001c729ed6fSDavid Howells return sock_no_sendmsg_locked(sk, msg, size); 3002c729ed6fSDavid Howells 3003c729ed6fSDavid Howells return sock->ops->sendmsg_locked(sk, msg, size); 3004c729ed6fSDavid Howells } 3005c729ed6fSDavid Howells 3006c729ed6fSDavid Howells static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg) 30070739cd28SCong Wang { 30080739cd28SCong Wang struct socket *sock = sk->sk_socket; 30090739cd28SCong Wang 30100739cd28SCong Wang if (!sock) 30110739cd28SCong Wang return -EINVAL; 3012c729ed6fSDavid Howells return sock_sendmsg(sock, msg); 30130739cd28SCong Wang } 30140739cd28SCong Wang 3015c729ed6fSDavid Howells typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg); 30160739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 3017c729ed6fSDavid Howells int len, sendmsg_func sendmsg) 301820bf50deSTom Herbert { 301920bf50deSTom Herbert unsigned int orig_len = len; 302020bf50deSTom Herbert struct sk_buff *head = skb; 302120bf50deSTom Herbert unsigned short fragidx; 302220bf50deSTom Herbert int slen, ret; 302320bf50deSTom Herbert 302420bf50deSTom Herbert do_frag_list: 302520bf50deSTom Herbert 302620bf50deSTom Herbert /* Deal with head data */ 302720bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 302820bf50deSTom Herbert struct kvec kv; 302920bf50deSTom Herbert struct msghdr msg; 303020bf50deSTom Herbert 303120bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 303220bf50deSTom Herbert kv.iov_base = skb->data + offset; 3033db5980d8SJohn Fastabend kv.iov_len = slen; 303420bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 3035bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 303620bf50deSTom Herbert 3037c729ed6fSDavid Howells iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen); 3038c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, 3039c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg); 304020bf50deSTom Herbert if (ret <= 0) 304120bf50deSTom Herbert goto error; 304220bf50deSTom Herbert 304320bf50deSTom Herbert offset += ret; 304420bf50deSTom Herbert len -= ret; 304520bf50deSTom Herbert } 304620bf50deSTom Herbert 304720bf50deSTom Herbert /* All the data was skb head? */ 304820bf50deSTom Herbert if (!len) 304920bf50deSTom Herbert goto out; 305020bf50deSTom Herbert 305120bf50deSTom Herbert /* Make offset relative to start of frags */ 305220bf50deSTom Herbert offset -= skb_headlen(skb); 305320bf50deSTom Herbert 305420bf50deSTom Herbert /* Find where we are in frag list */ 305520bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 305620bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 305720bf50deSTom Herbert 3058d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 305920bf50deSTom Herbert break; 306020bf50deSTom Herbert 3061d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 306220bf50deSTom Herbert } 306320bf50deSTom Herbert 306420bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 306520bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 306620bf50deSTom Herbert 3067d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 306820bf50deSTom Herbert 306920bf50deSTom Herbert while (slen) { 3070c729ed6fSDavid Howells struct bio_vec bvec; 3071c729ed6fSDavid Howells struct msghdr msg = { 3072c729ed6fSDavid Howells .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT, 3073c729ed6fSDavid Howells }; 3074c729ed6fSDavid Howells 3075c729ed6fSDavid Howells bvec_set_page(&bvec, skb_frag_page(frag), slen, 3076c729ed6fSDavid Howells skb_frag_off(frag) + offset); 3077c729ed6fSDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, 3078c729ed6fSDavid Howells slen); 3079c729ed6fSDavid Howells 3080c729ed6fSDavid Howells ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked, 3081c729ed6fSDavid Howells sendmsg_unlocked, sk, &msg); 308220bf50deSTom Herbert if (ret <= 0) 308320bf50deSTom Herbert goto error; 308420bf50deSTom Herbert 308520bf50deSTom Herbert len -= ret; 308620bf50deSTom Herbert offset += ret; 308720bf50deSTom Herbert slen -= ret; 308820bf50deSTom Herbert } 308920bf50deSTom Herbert 309020bf50deSTom Herbert offset = 0; 309120bf50deSTom Herbert } 309220bf50deSTom Herbert 309320bf50deSTom Herbert if (len) { 309420bf50deSTom Herbert /* Process any frag lists */ 309520bf50deSTom Herbert 309620bf50deSTom Herbert if (skb == head) { 309720bf50deSTom Herbert if (skb_has_frag_list(skb)) { 309820bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 309920bf50deSTom Herbert goto do_frag_list; 310020bf50deSTom Herbert } 310120bf50deSTom Herbert } else if (skb->next) { 310220bf50deSTom Herbert skb = skb->next; 310320bf50deSTom Herbert goto do_frag_list; 310420bf50deSTom Herbert } 310520bf50deSTom Herbert } 310620bf50deSTom Herbert 310720bf50deSTom Herbert out: 310820bf50deSTom Herbert return orig_len - len; 310920bf50deSTom Herbert 311020bf50deSTom Herbert error: 311120bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 311220bf50deSTom Herbert } 31130739cd28SCong Wang 31140739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 31150739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 31160739cd28SCong Wang int len) 31170739cd28SCong Wang { 3118c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_locked); 31190739cd28SCong Wang } 312020bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 312120bf50deSTom Herbert 31220739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 31230739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 31240739cd28SCong Wang { 3125c729ed6fSDavid Howells return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked); 31260739cd28SCong Wang } 31270739cd28SCong Wang 3128357b40a1SHerbert Xu /** 3129357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3130357b40a1SHerbert Xu * @skb: destination buffer 3131357b40a1SHerbert Xu * @offset: offset in destination 3132357b40a1SHerbert Xu * @from: source buffer 3133357b40a1SHerbert Xu * @len: number of bytes to copy 3134357b40a1SHerbert Xu * 3135357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3136357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3137357b40a1SHerbert Xu * traversing fragment lists and such. 3138357b40a1SHerbert Xu */ 3139357b40a1SHerbert Xu 31400c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3141357b40a1SHerbert Xu { 31421a028e50SDavid S. Miller int start = skb_headlen(skb); 3143fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3144fbb398a8SDavid S. Miller int i, copy; 3145357b40a1SHerbert Xu 3146357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3147357b40a1SHerbert Xu goto fault; 3148357b40a1SHerbert Xu 31491a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3150357b40a1SHerbert Xu if (copy > len) 3151357b40a1SHerbert Xu copy = len; 315227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3153357b40a1SHerbert Xu if ((len -= copy) == 0) 3154357b40a1SHerbert Xu return 0; 3155357b40a1SHerbert Xu offset += copy; 3156357b40a1SHerbert Xu from += copy; 3157357b40a1SHerbert Xu } 3158357b40a1SHerbert Xu 3159357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3160357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31611a028e50SDavid S. Miller int end; 3162357b40a1SHerbert Xu 3163547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31641a028e50SDavid S. Miller 31659e903e08SEric Dumazet end = start + skb_frag_size(frag); 3166357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3167c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3168c613c209SWillem de Bruijn struct page *p; 3169357b40a1SHerbert Xu u8 *vaddr; 3170357b40a1SHerbert Xu 3171357b40a1SHerbert Xu if (copy > len) 3172357b40a1SHerbert Xu copy = len; 3173357b40a1SHerbert Xu 3174c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3175b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3176c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3177c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3178c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 317951c56b00SEric Dumazet kunmap_atomic(vaddr); 3180c613c209SWillem de Bruijn } 3181357b40a1SHerbert Xu 3182357b40a1SHerbert Xu if ((len -= copy) == 0) 3183357b40a1SHerbert Xu return 0; 3184357b40a1SHerbert Xu offset += copy; 3185357b40a1SHerbert Xu from += copy; 3186357b40a1SHerbert Xu } 31871a028e50SDavid S. Miller start = end; 3188357b40a1SHerbert Xu } 3189357b40a1SHerbert Xu 3190fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31911a028e50SDavid S. Miller int end; 3192357b40a1SHerbert Xu 3193547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31941a028e50SDavid S. Miller 3195fbb398a8SDavid S. Miller end = start + frag_iter->len; 3196357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3197357b40a1SHerbert Xu if (copy > len) 3198357b40a1SHerbert Xu copy = len; 3199fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 32001a028e50SDavid S. Miller from, copy)) 3201357b40a1SHerbert Xu goto fault; 3202357b40a1SHerbert Xu if ((len -= copy) == 0) 3203357b40a1SHerbert Xu return 0; 3204357b40a1SHerbert Xu offset += copy; 3205357b40a1SHerbert Xu from += copy; 3206357b40a1SHerbert Xu } 32071a028e50SDavid S. Miller start = end; 3208357b40a1SHerbert Xu } 3209357b40a1SHerbert Xu if (!len) 3210357b40a1SHerbert Xu return 0; 3211357b40a1SHerbert Xu 3212357b40a1SHerbert Xu fault: 3213357b40a1SHerbert Xu return -EFAULT; 3214357b40a1SHerbert Xu } 3215357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3216357b40a1SHerbert Xu 32171da177e4SLinus Torvalds /* Checksum skb data. */ 32182817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 32192817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 32201da177e4SLinus Torvalds { 32211a028e50SDavid S. Miller int start = skb_headlen(skb); 32221a028e50SDavid S. Miller int i, copy = start - offset; 3223fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32241da177e4SLinus Torvalds int pos = 0; 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds /* Checksum header. */ 32271da177e4SLinus Torvalds if (copy > 0) { 32281da177e4SLinus Torvalds if (copy > len) 32291da177e4SLinus Torvalds copy = len; 32302544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 32312544af03SMatteo Croce skb->data + offset, copy, csum); 32321da177e4SLinus Torvalds if ((len -= copy) == 0) 32331da177e4SLinus Torvalds return csum; 32341da177e4SLinus Torvalds offset += copy; 32351da177e4SLinus Torvalds pos = copy; 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32391a028e50SDavid S. Miller int end; 324051c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 32411da177e4SLinus Torvalds 3242547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32431a028e50SDavid S. Miller 324451c56b00SEric Dumazet end = start + skb_frag_size(frag); 32451da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3246c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3247c613c209SWillem de Bruijn struct page *p; 324844bb9363SAl Viro __wsum csum2; 32491da177e4SLinus Torvalds u8 *vaddr; 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds if (copy > len) 32521da177e4SLinus Torvalds copy = len; 3253c613c209SWillem de Bruijn 3254c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3255b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3256c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3257c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 32582544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 32592544af03SMatteo Croce csum_partial_ext, 32602544af03SMatteo Croce vaddr + p_off, p_len, 0); 326151c56b00SEric Dumazet kunmap_atomic(vaddr); 32622544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 32632544af03SMatteo Croce csum_block_add_ext, csum, 32642544af03SMatteo Croce csum2, pos, p_len); 3265c613c209SWillem de Bruijn pos += p_len; 3266c613c209SWillem de Bruijn } 3267c613c209SWillem de Bruijn 32681da177e4SLinus Torvalds if (!(len -= copy)) 32691da177e4SLinus Torvalds return csum; 32701da177e4SLinus Torvalds offset += copy; 32711da177e4SLinus Torvalds } 32721a028e50SDavid S. Miller start = end; 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 3275fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32761a028e50SDavid S. Miller int end; 32771da177e4SLinus Torvalds 3278547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32791a028e50SDavid S. Miller 3280fbb398a8SDavid S. Miller end = start + frag_iter->len; 32811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32825f92a738SAl Viro __wsum csum2; 32831da177e4SLinus Torvalds if (copy > len) 32841da177e4SLinus Torvalds copy = len; 32852817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 32862817a336SDaniel Borkmann copy, 0, ops); 32872544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 32882544af03SMatteo Croce csum, csum2, pos, copy); 32891da177e4SLinus Torvalds if ((len -= copy) == 0) 32901da177e4SLinus Torvalds return csum; 32911da177e4SLinus Torvalds offset += copy; 32921da177e4SLinus Torvalds pos += copy; 32931da177e4SLinus Torvalds } 32941a028e50SDavid S. Miller start = end; 32951da177e4SLinus Torvalds } 329609a62660SKris Katterjohn BUG_ON(len); 32971da177e4SLinus Torvalds 32981da177e4SLinus Torvalds return csum; 32991da177e4SLinus Torvalds } 33002817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 33012817a336SDaniel Borkmann 33022817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 33032817a336SDaniel Borkmann int len, __wsum csum) 33042817a336SDaniel Borkmann { 33052817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3306cea80ea8SDaniel Borkmann .update = csum_partial_ext, 33072817a336SDaniel Borkmann .combine = csum_block_add_ext, 33082817a336SDaniel Borkmann }; 33092817a336SDaniel Borkmann 33102817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 33112817a336SDaniel Borkmann } 3312b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 33131da177e4SLinus Torvalds 33141da177e4SLinus Torvalds /* Both of above in one bottle. */ 33151da177e4SLinus Torvalds 331681d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 33178d5930dfSAl Viro u8 *to, int len) 33181da177e4SLinus Torvalds { 33191a028e50SDavid S. Miller int start = skb_headlen(skb); 33201a028e50SDavid S. Miller int i, copy = start - offset; 3321fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 33221da177e4SLinus Torvalds int pos = 0; 33238d5930dfSAl Viro __wsum csum = 0; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds /* Copy header. */ 33261da177e4SLinus Torvalds if (copy > 0) { 33271da177e4SLinus Torvalds if (copy > len) 33281da177e4SLinus Torvalds copy = len; 33291da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3330cc44c17bSAl Viro copy); 33311da177e4SLinus Torvalds if ((len -= copy) == 0) 33321da177e4SLinus Torvalds return csum; 33331da177e4SLinus Torvalds offset += copy; 33341da177e4SLinus Torvalds to += copy; 33351da177e4SLinus Torvalds pos = copy; 33361da177e4SLinus Torvalds } 33371da177e4SLinus Torvalds 33381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33391a028e50SDavid S. Miller int end; 33401da177e4SLinus Torvalds 3341547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33421a028e50SDavid S. Miller 33439e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 33441da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3345c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3346c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3347c613c209SWillem de Bruijn struct page *p; 33485084205fSAl Viro __wsum csum2; 33491da177e4SLinus Torvalds u8 *vaddr; 33501da177e4SLinus Torvalds 33511da177e4SLinus Torvalds if (copy > len) 33521da177e4SLinus Torvalds copy = len; 3353c613c209SWillem de Bruijn 3354c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3355b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3356c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3357c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3358c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3359c613c209SWillem de Bruijn to + copied, 3360cc44c17bSAl Viro p_len); 336151c56b00SEric Dumazet kunmap_atomic(vaddr); 33621da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3363c613c209SWillem de Bruijn pos += p_len; 3364c613c209SWillem de Bruijn } 3365c613c209SWillem de Bruijn 33661da177e4SLinus Torvalds if (!(len -= copy)) 33671da177e4SLinus Torvalds return csum; 33681da177e4SLinus Torvalds offset += copy; 33691da177e4SLinus Torvalds to += copy; 33701da177e4SLinus Torvalds } 33711a028e50SDavid S. Miller start = end; 33721da177e4SLinus Torvalds } 33731da177e4SLinus Torvalds 3374fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 337581d77662SAl Viro __wsum csum2; 33761a028e50SDavid S. Miller int end; 33771da177e4SLinus Torvalds 3378547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33791a028e50SDavid S. Miller 3380fbb398a8SDavid S. Miller end = start + frag_iter->len; 33811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 33821da177e4SLinus Torvalds if (copy > len) 33831da177e4SLinus Torvalds copy = len; 3384fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 33851a028e50SDavid S. Miller offset - start, 33868d5930dfSAl Viro to, copy); 33871da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 33881da177e4SLinus Torvalds if ((len -= copy) == 0) 33891da177e4SLinus Torvalds return csum; 33901da177e4SLinus Torvalds offset += copy; 33911da177e4SLinus Torvalds to += copy; 33921da177e4SLinus Torvalds pos += copy; 33931da177e4SLinus Torvalds } 33941a028e50SDavid S. Miller start = end; 33951da177e4SLinus Torvalds } 339609a62660SKris Katterjohn BUG_ON(len); 33971da177e4SLinus Torvalds return csum; 33981da177e4SLinus Torvalds } 3399b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 34001da177e4SLinus Torvalds 340149f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 340249f8e832SCong Wang { 340349f8e832SCong Wang __sum16 sum; 340449f8e832SCong Wang 340549f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 340614641931SCong Wang /* See comments in __skb_checksum_complete(). */ 340749f8e832SCong Wang if (likely(!sum)) { 340849f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 340949f8e832SCong Wang !skb->csum_complete_sw) 34107fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 341149f8e832SCong Wang } 341249f8e832SCong Wang if (!skb_shared(skb)) 341349f8e832SCong Wang skb->csum_valid = !sum; 341449f8e832SCong Wang return sum; 341549f8e832SCong Wang } 341649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 341749f8e832SCong Wang 341814641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 341914641931SCong Wang * which has been changed from the hardware checksum, for example, by 342014641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 342114641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 342214641931SCong Wang * 342314641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 342414641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 342514641931SCong Wang * shared. 342614641931SCong Wang */ 342749f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 342849f8e832SCong Wang { 342949f8e832SCong Wang __wsum csum; 343049f8e832SCong Wang __sum16 sum; 343149f8e832SCong Wang 343249f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 343349f8e832SCong Wang 343449f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 343514641931SCong Wang /* This check is inverted, because we already knew the hardware 343614641931SCong Wang * checksum is invalid before calling this function. So, if the 343714641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 343814641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 343914641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 344014641931SCong Wang * when moving skb->data around. 344114641931SCong Wang */ 344249f8e832SCong Wang if (likely(!sum)) { 344349f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 344449f8e832SCong Wang !skb->csum_complete_sw) 34457fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 344649f8e832SCong Wang } 344749f8e832SCong Wang 344849f8e832SCong Wang if (!skb_shared(skb)) { 344949f8e832SCong Wang /* Save full packet checksum */ 345049f8e832SCong Wang skb->csum = csum; 345149f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 345249f8e832SCong Wang skb->csum_complete_sw = 1; 345349f8e832SCong Wang skb->csum_valid = !sum; 345449f8e832SCong Wang } 345549f8e832SCong Wang 345649f8e832SCong Wang return sum; 345749f8e832SCong Wang } 345849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 345949f8e832SCong Wang 34609617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 34619617813dSDavide Caratti { 34629617813dSDavide Caratti net_warn_ratelimited( 34639617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34649617813dSDavide Caratti __func__); 34659617813dSDavide Caratti return 0; 34669617813dSDavide Caratti } 34679617813dSDavide Caratti 34689617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 34699617813dSDavide Caratti int offset, int len) 34709617813dSDavide Caratti { 34719617813dSDavide Caratti net_warn_ratelimited( 34729617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 34739617813dSDavide Caratti __func__); 34749617813dSDavide Caratti return 0; 34759617813dSDavide Caratti } 34769617813dSDavide Caratti 34779617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 34789617813dSDavide Caratti .update = warn_crc32c_csum_update, 34799617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 34809617813dSDavide Caratti }; 34819617813dSDavide Caratti 34829617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 34839617813dSDavide Caratti &default_crc32c_ops; 34849617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 34859617813dSDavide Caratti 3486af2806f8SThomas Graf /** 3487af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3488af2806f8SThomas Graf * @from: source buffer 3489af2806f8SThomas Graf * 3490af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3491af2806f8SThomas Graf * into skb_zerocopy(). 3492af2806f8SThomas Graf */ 3493af2806f8SThomas Graf unsigned int 3494af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3495af2806f8SThomas Graf { 3496af2806f8SThomas Graf unsigned int hlen = 0; 3497af2806f8SThomas Graf 3498af2806f8SThomas Graf if (!from->head_frag || 3499af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3500a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3501af2806f8SThomas Graf hlen = skb_headlen(from); 3502a17ad096SPravin B Shelar if (!hlen) 3503a17ad096SPravin B Shelar hlen = from->len; 3504a17ad096SPravin B Shelar } 3505af2806f8SThomas Graf 3506af2806f8SThomas Graf if (skb_has_frag_list(from)) 3507af2806f8SThomas Graf hlen = from->len; 3508af2806f8SThomas Graf 3509af2806f8SThomas Graf return hlen; 3510af2806f8SThomas Graf } 3511af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3512af2806f8SThomas Graf 3513af2806f8SThomas Graf /** 3514af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3515af2806f8SThomas Graf * @to: destination buffer 35167fceb4deSMasanari Iida * @from: source buffer 3517af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3518af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3519af2806f8SThomas Graf * 3520af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3521af2806f8SThomas Graf * to the frags in the source buffer. 3522af2806f8SThomas Graf * 3523af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3524af2806f8SThomas Graf * headroom in the `to` buffer. 352536d5fe6aSZoltan Kiss * 352636d5fe6aSZoltan Kiss * Return value: 352736d5fe6aSZoltan Kiss * 0: everything is OK 352836d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 352936d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3530af2806f8SThomas Graf */ 353136d5fe6aSZoltan Kiss int 353236d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3533af2806f8SThomas Graf { 3534af2806f8SThomas Graf int i, j = 0; 3535af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 353636d5fe6aSZoltan Kiss int ret; 3537af2806f8SThomas Graf struct page *page; 3538af2806f8SThomas Graf unsigned int offset; 3539af2806f8SThomas Graf 3540af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3541af2806f8SThomas Graf 3542af2806f8SThomas Graf /* dont bother with small payloads */ 354336d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 354436d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3545af2806f8SThomas Graf 3546af2806f8SThomas Graf if (hlen) { 354736d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 354836d5fe6aSZoltan Kiss if (unlikely(ret)) 354936d5fe6aSZoltan Kiss return ret; 3550af2806f8SThomas Graf len -= hlen; 3551af2806f8SThomas Graf } else { 3552af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3553af2806f8SThomas Graf if (plen) { 3554af2806f8SThomas Graf page = virt_to_head_page(from->head); 3555af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3556af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3557af2806f8SThomas Graf get_page(page); 3558af2806f8SThomas Graf j = 1; 3559af2806f8SThomas Graf len -= plen; 3560af2806f8SThomas Graf } 3561af2806f8SThomas Graf } 3562af2806f8SThomas Graf 3563ede57d58SRichard Gobert skb_len_add(to, len + plen); 3564af2806f8SThomas Graf 356536d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 356636d5fe6aSZoltan Kiss skb_tx_error(from); 356736d5fe6aSZoltan Kiss return -ENOMEM; 356836d5fe6aSZoltan Kiss } 35691f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 357036d5fe6aSZoltan Kiss 3571af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3572d8e18a51SMatthew Wilcox (Oracle) int size; 3573d8e18a51SMatthew Wilcox (Oracle) 3574af2806f8SThomas Graf if (!len) 3575af2806f8SThomas Graf break; 3576af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3577d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3578d8e18a51SMatthew Wilcox (Oracle) len); 3579d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3580d8e18a51SMatthew Wilcox (Oracle) len -= size; 3581af2806f8SThomas Graf skb_frag_ref(to, j); 3582af2806f8SThomas Graf j++; 3583af2806f8SThomas Graf } 3584af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 358536d5fe6aSZoltan Kiss 358636d5fe6aSZoltan Kiss return 0; 3587af2806f8SThomas Graf } 3588af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3589af2806f8SThomas Graf 35901da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 35911da177e4SLinus Torvalds { 3592d3bc23e7SAl Viro __wsum csum; 35931da177e4SLinus Torvalds long csstart; 35941da177e4SLinus Torvalds 359584fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 359655508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 35971da177e4SLinus Torvalds else 35981da177e4SLinus Torvalds csstart = skb_headlen(skb); 35991da177e4SLinus Torvalds 360009a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 36011da177e4SLinus Torvalds 3602d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 36031da177e4SLinus Torvalds 36041da177e4SLinus Torvalds csum = 0; 36051da177e4SLinus Torvalds if (csstart != skb->len) 36061da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 36078d5930dfSAl Viro skb->len - csstart); 36081da177e4SLinus Torvalds 360984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3610ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 36111da177e4SLinus Torvalds 3612d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 36131da177e4SLinus Torvalds } 36141da177e4SLinus Torvalds } 3615b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds /** 36181da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 36191da177e4SLinus Torvalds * @list: list to dequeue from 36201da177e4SLinus Torvalds * 36211da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 36221da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 36231da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36241da177e4SLinus Torvalds */ 36251da177e4SLinus Torvalds 36261da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 36271da177e4SLinus Torvalds { 36281da177e4SLinus Torvalds unsigned long flags; 36291da177e4SLinus Torvalds struct sk_buff *result; 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36321da177e4SLinus Torvalds result = __skb_dequeue(list); 36331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36341da177e4SLinus Torvalds return result; 36351da177e4SLinus Torvalds } 3636b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 36371da177e4SLinus Torvalds 36381da177e4SLinus Torvalds /** 36391da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 36401da177e4SLinus Torvalds * @list: list to dequeue from 36411da177e4SLinus Torvalds * 36421da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 36431da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 36441da177e4SLinus Torvalds * returned or %NULL if the list is empty. 36451da177e4SLinus Torvalds */ 36461da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 36471da177e4SLinus Torvalds { 36481da177e4SLinus Torvalds unsigned long flags; 36491da177e4SLinus Torvalds struct sk_buff *result; 36501da177e4SLinus Torvalds 36511da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36521da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 36531da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36541da177e4SLinus Torvalds return result; 36551da177e4SLinus Torvalds } 3656b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds /** 36591da177e4SLinus Torvalds * skb_queue_purge - empty a list 36601da177e4SLinus Torvalds * @list: list to empty 36611da177e4SLinus Torvalds * 36621da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 36631da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 36641da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 36651da177e4SLinus Torvalds */ 36661da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 36671da177e4SLinus Torvalds { 36681da177e4SLinus Torvalds struct sk_buff *skb; 36691da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 36701da177e4SLinus Torvalds kfree_skb(skb); 36711da177e4SLinus Torvalds } 3672b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds /** 36759f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 36769f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3677385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 36789f5afeaeSYaogong Wang * 36799f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 36809f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 36819f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 36829f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 36839f5afeaeSYaogong Wang */ 3684385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 36859f5afeaeSYaogong Wang { 36867c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3687385114deSPeter Oskolkov unsigned int sum = 0; 36889f5afeaeSYaogong Wang 36897c90584cSEric Dumazet while (p) { 36907c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 36917c90584cSEric Dumazet 36927c90584cSEric Dumazet p = rb_next(p); 36937c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3694385114deSPeter Oskolkov sum += skb->truesize; 36959f5afeaeSYaogong Wang kfree_skb(skb); 36967c90584cSEric Dumazet } 3697385114deSPeter Oskolkov return sum; 36989f5afeaeSYaogong Wang } 36999f5afeaeSYaogong Wang 37009f5afeaeSYaogong Wang /** 37011da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 37021da177e4SLinus Torvalds * @list: list to use 37031da177e4SLinus Torvalds * @newsk: buffer to queue 37041da177e4SLinus Torvalds * 37051da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 37061da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37071da177e4SLinus Torvalds * safely. 37081da177e4SLinus Torvalds * 37091da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37101da177e4SLinus Torvalds */ 37111da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 37121da177e4SLinus Torvalds { 37131da177e4SLinus Torvalds unsigned long flags; 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37161da177e4SLinus Torvalds __skb_queue_head(list, newsk); 37171da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37181da177e4SLinus Torvalds } 3719b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 37201da177e4SLinus Torvalds 37211da177e4SLinus Torvalds /** 37221da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 37231da177e4SLinus Torvalds * @list: list to use 37241da177e4SLinus Torvalds * @newsk: buffer to queue 37251da177e4SLinus Torvalds * 37261da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 37271da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 37281da177e4SLinus Torvalds * safely. 37291da177e4SLinus Torvalds * 37301da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37311da177e4SLinus Torvalds */ 37321da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 37331da177e4SLinus Torvalds { 37341da177e4SLinus Torvalds unsigned long flags; 37351da177e4SLinus Torvalds 37361da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37371da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 37381da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37391da177e4SLinus Torvalds } 3740b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 37418728b834SDavid S. Miller 37421da177e4SLinus Torvalds /** 37431da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 37441da177e4SLinus Torvalds * @skb: buffer to remove 37458728b834SDavid S. Miller * @list: list to use 37461da177e4SLinus Torvalds * 37478728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 37488728b834SDavid S. Miller * function is atomic with respect to other list locked calls 37491da177e4SLinus Torvalds * 37508728b834SDavid S. Miller * You must know what list the SKB is on. 37511da177e4SLinus Torvalds */ 37528728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 37531da177e4SLinus Torvalds { 37541da177e4SLinus Torvalds unsigned long flags; 37551da177e4SLinus Torvalds 37561da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 37578728b834SDavid S. Miller __skb_unlink(skb, list); 37581da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 37591da177e4SLinus Torvalds } 3760b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds /** 37631da177e4SLinus Torvalds * skb_append - append a buffer 37641da177e4SLinus Torvalds * @old: buffer to insert after 37651da177e4SLinus Torvalds * @newsk: buffer to insert 37668728b834SDavid S. Miller * @list: list to use 37671da177e4SLinus Torvalds * 37681da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 37691da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 37701da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 37711da177e4SLinus Torvalds */ 37728728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 37731da177e4SLinus Torvalds { 37741da177e4SLinus Torvalds unsigned long flags; 37751da177e4SLinus Torvalds 37768728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 37777de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 37788728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 37791da177e4SLinus Torvalds } 3780b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 37811da177e4SLinus Torvalds 37821da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 37831da177e4SLinus Torvalds struct sk_buff* skb1, 37841da177e4SLinus Torvalds const u32 len, const int pos) 37851da177e4SLinus Torvalds { 37861da177e4SLinus Torvalds int i; 37871da177e4SLinus Torvalds 3788d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3789d626f62bSArnaldo Carvalho de Melo pos - len); 37901da177e4SLinus Torvalds /* And move data appendix as is. */ 37911da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 37921da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 37931da177e4SLinus Torvalds 37941da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 37951da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 37961da177e4SLinus Torvalds skb1->data_len = skb->data_len; 37971da177e4SLinus Torvalds skb1->len += skb1->data_len; 37981da177e4SLinus Torvalds skb->data_len = 0; 37991da177e4SLinus Torvalds skb->len = len; 380027a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 38031da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 38041da177e4SLinus Torvalds struct sk_buff* skb1, 38051da177e4SLinus Torvalds const u32 len, int pos) 38061da177e4SLinus Torvalds { 38071da177e4SLinus Torvalds int i, k = 0; 38081da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 38091da177e4SLinus Torvalds 38101da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 38111da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 38121da177e4SLinus Torvalds skb->len = len; 38131da177e4SLinus Torvalds skb->data_len = len - pos; 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 38169e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 38171da177e4SLinus Torvalds 38181da177e4SLinus Torvalds if (pos + size > len) { 38191da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 38201da177e4SLinus Torvalds 38211da177e4SLinus Torvalds if (pos < len) { 38221da177e4SLinus Torvalds /* Split frag. 38231da177e4SLinus Torvalds * We have two variants in this case: 38241da177e4SLinus Torvalds * 1. Move all the frag to the second 38251da177e4SLinus Torvalds * part, if it is possible. F.e. 38261da177e4SLinus Torvalds * this approach is mandatory for TUX, 38271da177e4SLinus Torvalds * where splitting is expensive. 38281da177e4SLinus Torvalds * 2. Split is accurately. We make this. 38291da177e4SLinus Torvalds */ 3830ea2ab693SIan Campbell skb_frag_ref(skb, i); 3831b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 38329e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 38339e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 38341da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38351da177e4SLinus Torvalds } 38361da177e4SLinus Torvalds k++; 38371da177e4SLinus Torvalds } else 38381da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 38391da177e4SLinus Torvalds pos += size; 38401da177e4SLinus Torvalds } 38411da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 38421da177e4SLinus Torvalds } 38431da177e4SLinus Torvalds 38441da177e4SLinus Torvalds /** 38451da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 38461da177e4SLinus Torvalds * @skb: the buffer to split 38471da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 38481da177e4SLinus Torvalds * @len: new length for skb 38491da177e4SLinus Torvalds */ 38501da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 38511da177e4SLinus Torvalds { 38521da177e4SLinus Torvalds int pos = skb_headlen(skb); 38539b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 38541da177e4SLinus Torvalds 3855753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3856753f1ca4SPavel Begunkov 38579b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 38581f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 38591da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 38601da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 38611da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 38621da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 38631da177e4SLinus Torvalds } 3864b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 38651da177e4SLinus Torvalds 38669f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 38679f782db3SIlpo Järvinen * 38689f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 38699f782db3SIlpo Järvinen */ 3870832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3871832d11c5SIlpo Järvinen { 3872c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3873832d11c5SIlpo Järvinen } 3874832d11c5SIlpo Järvinen 3875832d11c5SIlpo Järvinen /** 3876832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3877832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3878832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3879832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3880832d11c5SIlpo Järvinen * 3881832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 388220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3883832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3884832d11c5SIlpo Järvinen * 3885832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3886832d11c5SIlpo Järvinen * 3887832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3888832d11c5SIlpo Järvinen * to have non-paged data as well. 3889832d11c5SIlpo Järvinen * 3890832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3891832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3892832d11c5SIlpo Järvinen */ 3893832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3894832d11c5SIlpo Järvinen { 3895832d11c5SIlpo Järvinen int from, to, merge, todo; 3896d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3897832d11c5SIlpo Järvinen 3898832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3899f8071cdeSEric Dumazet 3900f8071cdeSEric Dumazet if (skb_headlen(skb)) 3901f8071cdeSEric Dumazet return 0; 39021f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 39031f8b977aSWillem de Bruijn return 0; 3904832d11c5SIlpo Järvinen 3905832d11c5SIlpo Järvinen todo = shiftlen; 3906832d11c5SIlpo Järvinen from = 0; 3907832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3908832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3909832d11c5SIlpo Järvinen 3910832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3911832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3912832d11c5SIlpo Järvinen */ 3913832d11c5SIlpo Järvinen if (!to || 3914ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3915b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3916832d11c5SIlpo Järvinen merge = -1; 3917832d11c5SIlpo Järvinen } else { 3918832d11c5SIlpo Järvinen merge = to - 1; 3919832d11c5SIlpo Järvinen 39209e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3921832d11c5SIlpo Järvinen if (todo < 0) { 3922832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3923832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3924832d11c5SIlpo Järvinen return 0; 3925832d11c5SIlpo Järvinen 39269f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 39279f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3928832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3929832d11c5SIlpo Järvinen 39309e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 39319e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3932b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3933832d11c5SIlpo Järvinen 3934832d11c5SIlpo Järvinen goto onlymerged; 3935832d11c5SIlpo Järvinen } 3936832d11c5SIlpo Järvinen 3937832d11c5SIlpo Järvinen from++; 3938832d11c5SIlpo Järvinen } 3939832d11c5SIlpo Järvinen 3940832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3941832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3942832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3943832d11c5SIlpo Järvinen return 0; 3944832d11c5SIlpo Järvinen 3945832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3946832d11c5SIlpo Järvinen return 0; 3947832d11c5SIlpo Järvinen 3948832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3949832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3950832d11c5SIlpo Järvinen return 0; 3951832d11c5SIlpo Järvinen 3952832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3953832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3954832d11c5SIlpo Järvinen 39559e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3956832d11c5SIlpo Järvinen *fragto = *fragfrom; 39579e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3958832d11c5SIlpo Järvinen from++; 3959832d11c5SIlpo Järvinen to++; 3960832d11c5SIlpo Järvinen 3961832d11c5SIlpo Järvinen } else { 3962ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3963b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3964b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 39659e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3966832d11c5SIlpo Järvinen 3967b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 39689e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3969832d11c5SIlpo Järvinen todo = 0; 3970832d11c5SIlpo Järvinen 3971832d11c5SIlpo Järvinen to++; 3972832d11c5SIlpo Järvinen break; 3973832d11c5SIlpo Järvinen } 3974832d11c5SIlpo Järvinen } 3975832d11c5SIlpo Järvinen 3976832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3977832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3978832d11c5SIlpo Järvinen 3979832d11c5SIlpo Järvinen if (merge >= 0) { 3980832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3981832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3982832d11c5SIlpo Järvinen 39839e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 39846a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3985832d11c5SIlpo Järvinen } 3986832d11c5SIlpo Järvinen 3987832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3988832d11c5SIlpo Järvinen to = 0; 3989832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3990832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3991832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3992832d11c5SIlpo Järvinen 3993832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3994832d11c5SIlpo Järvinen 3995832d11c5SIlpo Järvinen onlymerged: 3996832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3997832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3998832d11c5SIlpo Järvinen */ 3999832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 4000832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 4001832d11c5SIlpo Järvinen 4002ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 4003ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 4004832d11c5SIlpo Järvinen 4005832d11c5SIlpo Järvinen return shiftlen; 4006832d11c5SIlpo Järvinen } 4007832d11c5SIlpo Järvinen 4008677e90edSThomas Graf /** 4009677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 4010677e90edSThomas Graf * @skb: the buffer to read 4011677e90edSThomas Graf * @from: lower offset of data to be read 4012677e90edSThomas Graf * @to: upper offset of data to be read 4013677e90edSThomas Graf * @st: state variable 4014677e90edSThomas Graf * 4015677e90edSThomas Graf * Initializes the specified state variable. Must be called before 4016677e90edSThomas Graf * invoking skb_seq_read() for the first time. 4017677e90edSThomas Graf */ 4018677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 4019677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 4020677e90edSThomas Graf { 4021677e90edSThomas Graf st->lower_offset = from; 4022677e90edSThomas Graf st->upper_offset = to; 4023677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 4024677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 4025677e90edSThomas Graf st->frag_data = NULL; 402697550f6fSWillem de Bruijn st->frag_off = 0; 4027677e90edSThomas Graf } 4028b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 4029677e90edSThomas Graf 4030677e90edSThomas Graf /** 4031677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 4032677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 4033677e90edSThomas Graf * @data: destination pointer for data to be returned 4034677e90edSThomas Graf * @st: state variable 4035677e90edSThomas Graf * 4036bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 4037677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 4038bc32383cSMathias Krause * the head of the data block to @data and returns the length 4039677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 4040677e90edSThomas Graf * offset has been reached. 4041677e90edSThomas Graf * 4042677e90edSThomas Graf * The caller is not required to consume all of the data 4043bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 4044677e90edSThomas Graf * of bytes already consumed and the next call to 4045677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 4046677e90edSThomas Graf * 404725985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 4048e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 4049677e90edSThomas Graf * reads of potentially non linear data. 4050677e90edSThomas Graf * 4051bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 4052677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 4053677e90edSThomas Graf * a stack for this purpose. 4054677e90edSThomas Graf */ 4055677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 4056677e90edSThomas Graf struct skb_seq_state *st) 4057677e90edSThomas Graf { 4058677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 4059677e90edSThomas Graf skb_frag_t *frag; 4060677e90edSThomas Graf 4061aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 4062aeb193eaSWedson Almeida Filho if (st->frag_data) { 4063aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 4064aeb193eaSWedson Almeida Filho st->frag_data = NULL; 4065aeb193eaSWedson Almeida Filho } 4066677e90edSThomas Graf return 0; 4067aeb193eaSWedson Almeida Filho } 4068677e90edSThomas Graf 4069677e90edSThomas Graf next_skb: 407095e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 4071677e90edSThomas Graf 4072995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 407395e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 4074677e90edSThomas Graf return block_limit - abs_offset; 4075677e90edSThomas Graf } 4076677e90edSThomas Graf 4077677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 4078677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 4079677e90edSThomas Graf 4080677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 408197550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 4082677e90edSThomas Graf 408397550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 408497550f6fSWillem de Bruijn 408597550f6fSWillem de Bruijn pg_idx = 0; 408697550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 408797550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 408897550f6fSWillem de Bruijn 408997550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 409097550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 409197550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 409297550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 409397550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 409497550f6fSWillem de Bruijn } 409597550f6fSWillem de Bruijn 409697550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 4097677e90edSThomas Graf if (abs_offset < block_limit) { 4098677e90edSThomas Graf if (!st->frag_data) 409997550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 4100677e90edSThomas Graf 410197550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 4102677e90edSThomas Graf (abs_offset - st->stepped_offset); 4103677e90edSThomas Graf 4104677e90edSThomas Graf return block_limit - abs_offset; 4105677e90edSThomas Graf } 4106677e90edSThomas Graf 4107677e90edSThomas Graf if (st->frag_data) { 410851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4109677e90edSThomas Graf st->frag_data = NULL; 4110677e90edSThomas Graf } 4111677e90edSThomas Graf 411297550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 411397550f6fSWillem de Bruijn st->frag_off += pg_sz; 411497550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 411597550f6fSWillem de Bruijn st->frag_off = 0; 4116677e90edSThomas Graf st->frag_idx++; 411797550f6fSWillem de Bruijn } 4118677e90edSThomas Graf } 4119677e90edSThomas Graf 41205b5a60daSOlaf Kirch if (st->frag_data) { 412151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 41225b5a60daSOlaf Kirch st->frag_data = NULL; 41235b5a60daSOlaf Kirch } 41245b5a60daSOlaf Kirch 412521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4126677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 412795e3b24cSHerbert Xu st->frag_idx = 0; 4128677e90edSThomas Graf goto next_skb; 412971b3346dSShyam Iyer } else if (st->cur_skb->next) { 413071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 413171b3346dSShyam Iyer st->frag_idx = 0; 4132677e90edSThomas Graf goto next_skb; 4133677e90edSThomas Graf } 4134677e90edSThomas Graf 4135677e90edSThomas Graf return 0; 4136677e90edSThomas Graf } 4137b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4138677e90edSThomas Graf 4139677e90edSThomas Graf /** 4140677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4141677e90edSThomas Graf * @st: state variable 4142677e90edSThomas Graf * 4143677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4144677e90edSThomas Graf * returned 0. 4145677e90edSThomas Graf */ 4146677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4147677e90edSThomas Graf { 4148677e90edSThomas Graf if (st->frag_data) 414951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4150677e90edSThomas Graf } 4151b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4152677e90edSThomas Graf 41533fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 41543fc7e8a6SThomas Graf 41553fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 41563fc7e8a6SThomas Graf struct ts_config *conf, 41573fc7e8a6SThomas Graf struct ts_state *state) 41583fc7e8a6SThomas Graf { 41593fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 41603fc7e8a6SThomas Graf } 41613fc7e8a6SThomas Graf 41623fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 41633fc7e8a6SThomas Graf { 41643fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 41653fc7e8a6SThomas Graf } 41663fc7e8a6SThomas Graf 41673fc7e8a6SThomas Graf /** 41683fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 41693fc7e8a6SThomas Graf * @skb: the buffer to look in 41703fc7e8a6SThomas Graf * @from: search offset 41713fc7e8a6SThomas Graf * @to: search limit 41723fc7e8a6SThomas Graf * @config: textsearch configuration 41733fc7e8a6SThomas Graf * 41743fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 41753fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 41763fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 41773fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 41783fc7e8a6SThomas Graf */ 41793fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4180059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 41813fc7e8a6SThomas Graf { 4182059a2440SBojan Prtvar struct ts_state state; 4183f72b948dSPhil Oester unsigned int ret; 4184f72b948dSPhil Oester 4185b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4186b228c9b0SWillem de Bruijn 41873fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 41883fc7e8a6SThomas Graf config->finish = skb_ts_finish; 41893fc7e8a6SThomas Graf 4190059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 41913fc7e8a6SThomas Graf 4192059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4193f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 41943fc7e8a6SThomas Graf } 4195b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 41963fc7e8a6SThomas Graf 4197be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 419896449f90SDavid Howells int offset, size_t size, size_t max_frags) 4199be12a1feSHannes Frederic Sowa { 4200be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4201be12a1feSHannes Frederic Sowa 4202be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4203be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 420496449f90SDavid Howells } else if (i < max_frags) { 4205753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4206be12a1feSHannes Frederic Sowa get_page(page); 4207228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4208be12a1feSHannes Frederic Sowa } else { 4209be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4210be12a1feSHannes Frederic Sowa } 4211be12a1feSHannes Frederic Sowa 4212be12a1feSHannes Frederic Sowa return 0; 4213be12a1feSHannes Frederic Sowa } 4214be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4215be12a1feSHannes Frederic Sowa 4216cbb042f9SHerbert Xu /** 4217cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4218cbb042f9SHerbert Xu * @skb: buffer to update 4219cbb042f9SHerbert Xu * @len: length of data pulled 4220cbb042f9SHerbert Xu * 4221cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4222fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 422384fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 422484fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 422584fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4226cbb042f9SHerbert Xu */ 4227af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4228cbb042f9SHerbert Xu { 422931b33dfbSPravin B Shelar unsigned char *data = skb->data; 423031b33dfbSPravin B Shelar 4231cbb042f9SHerbert Xu BUG_ON(len > skb->len); 423231b33dfbSPravin B Shelar __skb_pull(skb, len); 423331b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 423431b33dfbSPravin B Shelar return skb->data; 4235cbb042f9SHerbert Xu } 4236f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4237f94691acSArnaldo Carvalho de Melo 423813acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 423913acc94eSYonghong Song { 424013acc94eSYonghong Song skb_frag_t head_frag; 424113acc94eSYonghong Song struct page *page; 424213acc94eSYonghong Song 424313acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4244b51f4113SYunsheng Lin skb_frag_fill_page_desc(&head_frag, page, frag_skb->data - 4245b51f4113SYunsheng Lin (unsigned char *)page_address(page), 4246b51f4113SYunsheng Lin skb_headlen(frag_skb)); 424713acc94eSYonghong Song return head_frag; 424813acc94eSYonghong Song } 424913acc94eSYonghong Song 42503a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 42513a1296a3SSteffen Klassert netdev_features_t features, 42523a1296a3SSteffen Klassert unsigned int offset) 42533a1296a3SSteffen Klassert { 42543a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 42553a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 42563a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 42573a1296a3SSteffen Klassert unsigned int delta_len = 0; 42583a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 425953475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4260cf3ab8d4SLina Wang int len_diff, err; 42613a1296a3SSteffen Klassert 42623a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 42633a1296a3SSteffen Klassert 4264*c329b261SPaolo Abeni /* Ensure the head is writeable before touching the shared info */ 4265*c329b261SPaolo Abeni err = skb_unclone(skb, GFP_ATOMIC); 4266*c329b261SPaolo Abeni if (err) 4267*c329b261SPaolo Abeni goto err_linearize; 4268*c329b261SPaolo Abeni 42693a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 42703a1296a3SSteffen Klassert 4271876e8ca8SYan Zhai while (list_skb) { 42723a1296a3SSteffen Klassert nskb = list_skb; 42733a1296a3SSteffen Klassert list_skb = list_skb->next; 42743a1296a3SSteffen Klassert 427553475c5dSDongseok Yi err = 0; 4276224102deSlena wang delta_truesize += nskb->truesize; 427753475c5dSDongseok Yi if (skb_shared(nskb)) { 427853475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 427953475c5dSDongseok Yi if (tmp) { 428053475c5dSDongseok Yi consume_skb(nskb); 428153475c5dSDongseok Yi nskb = tmp; 428253475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 428353475c5dSDongseok Yi } else { 428453475c5dSDongseok Yi err = -ENOMEM; 428553475c5dSDongseok Yi } 428653475c5dSDongseok Yi } 428753475c5dSDongseok Yi 42883a1296a3SSteffen Klassert if (!tail) 42893a1296a3SSteffen Klassert skb->next = nskb; 42903a1296a3SSteffen Klassert else 42913a1296a3SSteffen Klassert tail->next = nskb; 42923a1296a3SSteffen Klassert 429353475c5dSDongseok Yi if (unlikely(err)) { 429453475c5dSDongseok Yi nskb->next = list_skb; 429553475c5dSDongseok Yi goto err_linearize; 429653475c5dSDongseok Yi } 429753475c5dSDongseok Yi 42983a1296a3SSteffen Klassert tail = nskb; 42993a1296a3SSteffen Klassert 43003a1296a3SSteffen Klassert delta_len += nskb->len; 43013a1296a3SSteffen Klassert 43023a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 43033a1296a3SSteffen Klassert 4304cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4305cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 43063a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 43073a1296a3SSteffen Klassert 43083a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4309cf3ab8d4SLina Wang nskb->transport_header += len_diff; 43103a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 43113a1296a3SSteffen Klassert nskb->data - tnl_hlen, 43123a1296a3SSteffen Klassert offset + tnl_hlen); 43133a1296a3SSteffen Klassert 43143a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 43153a1296a3SSteffen Klassert __skb_linearize(nskb)) 43163a1296a3SSteffen Klassert goto err_linearize; 4317876e8ca8SYan Zhai } 43183a1296a3SSteffen Klassert 43193a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 43203a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 43213a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 43223a1296a3SSteffen Klassert 43233a1296a3SSteffen Klassert skb_gso_reset(skb); 43243a1296a3SSteffen Klassert 43253a1296a3SSteffen Klassert skb->prev = tail; 43263a1296a3SSteffen Klassert 43273a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 43283a1296a3SSteffen Klassert __skb_linearize(skb)) 43293a1296a3SSteffen Klassert goto err_linearize; 43303a1296a3SSteffen Klassert 43313a1296a3SSteffen Klassert skb_get(skb); 43323a1296a3SSteffen Klassert 43333a1296a3SSteffen Klassert return skb; 43343a1296a3SSteffen Klassert 43353a1296a3SSteffen Klassert err_linearize: 43363a1296a3SSteffen Klassert kfree_skb_list(skb->next); 43373a1296a3SSteffen Klassert skb->next = NULL; 43383a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 43393a1296a3SSteffen Klassert } 43403a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 43413a1296a3SSteffen Klassert 4342f4c50d99SHerbert Xu /** 4343f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4344df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4345576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4346f4c50d99SHerbert Xu * 4347f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 43484c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 43494c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4350f4c50d99SHerbert Xu */ 4351df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4352df5771ffSMichael S. Tsirkin netdev_features_t features) 4353f4c50d99SHerbert Xu { 4354f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4355f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 43561a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4357df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4358df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4359df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 43601fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4361f4c50d99SHerbert Xu unsigned int offset = doffset; 4362df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4363802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4364f4c50d99SHerbert Xu unsigned int headroom; 4365802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4366ec5f0615SPravin B Shelar __be16 proto; 436736c98382SAlexander Duyck bool csum, sg; 4368df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4369f4c50d99SHerbert Xu int err = -ENOMEM; 4370f4c50d99SHerbert Xu int i = 0; 4371f4c50d99SHerbert Xu int pos; 4372f4c50d99SHerbert Xu 43739e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 43749e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 43759e4b7a99SJiri Benc struct sk_buff *check_skb; 43769e4b7a99SJiri Benc 43779e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 43789e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 43799e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 43809e4b7a99SJiri Benc * a linear non head_frag item. 43813dcbdb13SShmulik Ladkani * 43829e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 43839e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 43849e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 43859e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 43869e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 43873dcbdb13SShmulik Ladkani */ 43883dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 43899e4b7a99SJiri Benc break; 43909e4b7a99SJiri Benc } 43919e4b7a99SJiri Benc } 43923dcbdb13SShmulik Ladkani } 43933dcbdb13SShmulik Ladkani 43945882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 43952f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4396ec5f0615SPravin B Shelar if (unlikely(!proto)) 4397ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4398ec5f0615SPravin B Shelar 439936c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4400f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 44017e2b10c1STom Herbert 440207b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 440307b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 440407b26c94SSteffen Klassert struct sk_buff *iter; 440543170c4eSIlan Tayari unsigned int frag_len; 440607b26c94SSteffen Klassert 440707b26c94SSteffen Klassert if (!list_skb || 440807b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 440907b26c94SSteffen Klassert goto normal; 441007b26c94SSteffen Klassert 441143170c4eSIlan Tayari /* If we get here then all the required 441243170c4eSIlan Tayari * GSO features except frag_list are supported. 441343170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 441443170c4eSIlan Tayari * with no frag_list. 441543170c4eSIlan Tayari * Currently we can do that only when the buffers don't 441643170c4eSIlan Tayari * have a linear part and all the buffers except 441743170c4eSIlan Tayari * the last are of the same length. 441807b26c94SSteffen Klassert */ 441943170c4eSIlan Tayari frag_len = list_skb->len; 442007b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 442143170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 442243170c4eSIlan Tayari goto normal; 4423eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 442407b26c94SSteffen Klassert goto normal; 442507b26c94SSteffen Klassert 442607b26c94SSteffen Klassert len -= iter->len; 442707b26c94SSteffen Klassert } 442843170c4eSIlan Tayari 442943170c4eSIlan Tayari if (len != frag_len) 443043170c4eSIlan Tayari goto normal; 443107b26c94SSteffen Klassert } 443207b26c94SSteffen Klassert 4433802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4434802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4435802ab55aSAlexander Duyck * now. 4436802ab55aSAlexander Duyck */ 4437802ab55aSAlexander Duyck partial_segs = len / mss; 4438d7fb5a80SAlexander Duyck if (partial_segs > 1) 4439802ab55aSAlexander Duyck mss *= partial_segs; 4440d7fb5a80SAlexander Duyck else 4441d7fb5a80SAlexander Duyck partial_segs = 0; 4442802ab55aSAlexander Duyck } 4443802ab55aSAlexander Duyck 444407b26c94SSteffen Klassert normal: 4445df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4446df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4447f4c50d99SHerbert Xu 4448f4c50d99SHerbert Xu do { 4449f4c50d99SHerbert Xu struct sk_buff *nskb; 44508cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4451c8884eddSHerbert Xu int hsize; 4452f4c50d99SHerbert Xu int size; 4453f4c50d99SHerbert Xu 44543953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 44553953c46cSMarcelo Ricardo Leitner len = list_skb->len; 44563953c46cSMarcelo Ricardo Leitner } else { 4457df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4458f4c50d99SHerbert Xu if (len > mss) 4459f4c50d99SHerbert Xu len = mss; 44603953c46cSMarcelo Ricardo Leitner } 4461f4c50d99SHerbert Xu 4462df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4463f4c50d99SHerbert Xu 4464dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 44651a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 44661a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 446789319d38SHerbert Xu 44689d8506ccSHerbert Xu i = 0; 44691a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44701a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44711fd819ecSMichael S. Tsirkin frag_skb = list_skb; 44721a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 44739d8506ccSHerbert Xu 44749d8506ccSHerbert Xu while (pos < offset + len) { 44759d8506ccSHerbert Xu BUG_ON(i >= nfrags); 44769d8506ccSHerbert Xu 44774e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 44789d8506ccSHerbert Xu if (pos + size > offset + len) 44799d8506ccSHerbert Xu break; 44809d8506ccSHerbert Xu 44819d8506ccSHerbert Xu i++; 44829d8506ccSHerbert Xu pos += size; 44834e1beba1SMichael S. Tsirkin frag++; 44849d8506ccSHerbert Xu } 44859d8506ccSHerbert Xu 44861a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 44871a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 448889319d38SHerbert Xu 4489f4c50d99SHerbert Xu if (unlikely(!nskb)) 4490f4c50d99SHerbert Xu goto err; 4491f4c50d99SHerbert Xu 44929d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 44939d8506ccSHerbert Xu kfree_skb(nskb); 44949d8506ccSHerbert Xu goto err; 44959d8506ccSHerbert Xu } 44969d8506ccSHerbert Xu 4497ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 449889319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 449989319d38SHerbert Xu kfree_skb(nskb); 450089319d38SHerbert Xu goto err; 450189319d38SHerbert Xu } 450289319d38SHerbert Xu 4503ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 450489319d38SHerbert Xu skb_release_head_state(nskb); 450589319d38SHerbert Xu __skb_push(nskb, doffset); 450689319d38SHerbert Xu } else { 450700b229f7SPaolo Abeni if (hsize < 0) 450800b229f7SPaolo Abeni hsize = 0; 4509dbd50f23SXin Long if (hsize > len || !sg) 4510dbd50f23SXin Long hsize = len; 4511dbd50f23SXin Long 4512c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4513df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4514c93bdd0eSMel Gorman NUMA_NO_NODE); 451589319d38SHerbert Xu 451689319d38SHerbert Xu if (unlikely(!nskb)) 451789319d38SHerbert Xu goto err; 451889319d38SHerbert Xu 451989319d38SHerbert Xu skb_reserve(nskb, headroom); 452089319d38SHerbert Xu __skb_put(nskb, doffset); 452189319d38SHerbert Xu } 452289319d38SHerbert Xu 4523f4c50d99SHerbert Xu if (segs) 4524f4c50d99SHerbert Xu tail->next = nskb; 4525f4c50d99SHerbert Xu else 4526f4c50d99SHerbert Xu segs = nskb; 4527f4c50d99SHerbert Xu tail = nskb; 4528f4c50d99SHerbert Xu 4529df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4530f4c50d99SHerbert Xu 4531030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4532fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 453368c33163SPravin B Shelar 4534df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 453568c33163SPravin B Shelar nskb->data - tnl_hlen, 453668c33163SPravin B Shelar doffset + tnl_hlen); 453789319d38SHerbert Xu 45389d8506ccSHerbert Xu if (nskb->len == len + doffset) 45391cdbcb79SSimon Horman goto perform_csum_check; 454089319d38SHerbert Xu 45417fbeffedSAlexander Duyck if (!sg) { 45421454c9faSYadu Kishore if (!csum) { 45437fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 45446f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 454576443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 454676443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 45471454c9faSYadu Kishore skb_put(nskb, 45481454c9faSYadu Kishore len), 45498d5930dfSAl Viro len); 45507e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4551de843723STom Herbert skb_headroom(nskb) + doffset; 45521454c9faSYadu Kishore } else { 4553c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4554c624c58eSlily goto err; 45551454c9faSYadu Kishore } 4556f4c50d99SHerbert Xu continue; 4557f4c50d99SHerbert Xu } 4558f4c50d99SHerbert Xu 45598cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4560f4c50d99SHerbert Xu 4561df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4562d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4563f4c50d99SHerbert Xu 456406b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 456506b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4566cef401deSEric Dumazet 4567bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4568bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4569bf5c25d6SWillem de Bruijn goto err; 4570bf5c25d6SWillem de Bruijn 45719d8506ccSHerbert Xu while (pos < offset + len) { 45729d8506ccSHerbert Xu if (i >= nfrags) { 45739d8506ccSHerbert Xu i = 0; 45741a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 45751a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 45761fd819ecSMichael S. Tsirkin frag_skb = list_skb; 457713acc94eSYonghong Song if (!skb_headlen(list_skb)) { 45789d8506ccSHerbert Xu BUG_ON(!nfrags); 457913acc94eSYonghong Song } else { 458013acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 45819d8506ccSHerbert Xu 458213acc94eSYonghong Song /* to make room for head_frag. */ 458313acc94eSYonghong Song i--; 458413acc94eSYonghong Song frag--; 458513acc94eSYonghong Song } 4586bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4587bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4588bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4589bf5c25d6SWillem de Bruijn goto err; 4590bf5c25d6SWillem de Bruijn 45911a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 45929d8506ccSHerbert Xu } 45939d8506ccSHerbert Xu 45949d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 45959d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 45969d8506ccSHerbert Xu net_warn_ratelimited( 45979d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 45989d8506ccSHerbert Xu pos, mss); 4599ff907a11SEric Dumazet err = -EINVAL; 46009d8506ccSHerbert Xu goto err; 46019d8506ccSHerbert Xu } 46029d8506ccSHerbert Xu 460313acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 46048cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 46058cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4606f4c50d99SHerbert Xu 4607f4c50d99SHerbert Xu if (pos < offset) { 4608b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 46098cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4610f4c50d99SHerbert Xu } 4611f4c50d99SHerbert Xu 461289319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4613f4c50d99SHerbert Xu 4614f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4615f4c50d99SHerbert Xu i++; 46164e1beba1SMichael S. Tsirkin frag++; 4617f4c50d99SHerbert Xu pos += size; 4618f4c50d99SHerbert Xu } else { 46198cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 462089319d38SHerbert Xu goto skip_fraglist; 4621f4c50d99SHerbert Xu } 4622f4c50d99SHerbert Xu 46238cb19905SMichael S. Tsirkin nskb_frag++; 4624f4c50d99SHerbert Xu } 4625f4c50d99SHerbert Xu 462689319d38SHerbert Xu skip_fraglist: 4627f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4628f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4629f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4630ec5f0615SPravin B Shelar 46311cdbcb79SSimon Horman perform_csum_check: 46327fbeffedSAlexander Duyck if (!csum) { 4633ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4634ff907a11SEric Dumazet __skb_linearize(nskb)) 4635ddff00d4SAlexander Duyck goto err; 4636ff907a11SEric Dumazet 46377fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4638ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 463976443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 464076443456SAlexander Duyck skb_checksum(nskb, doffset, 464176443456SAlexander Duyck nskb->len - doffset, 0); 46427e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 46437e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4644ec5f0615SPravin B Shelar } 4645df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4646f4c50d99SHerbert Xu 4647bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4648bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4649bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4650bec3cfdcSEric Dumazet */ 4651bec3cfdcSEric Dumazet segs->prev = tail; 4652432c856fSToshiaki Makita 4653802ab55aSAlexander Duyck if (partial_segs) { 465407b26c94SSteffen Klassert struct sk_buff *iter; 4655802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 465607b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4657802ab55aSAlexander Duyck 4658802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 465907b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4660802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4661802ab55aSAlexander Duyck 4662802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4663802ab55aSAlexander Duyck * our way back down the stack of protocols. 4664802ab55aSAlexander Duyck */ 466507b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 466607b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 466707b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 466807b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 466907b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 467007b26c94SSteffen Klassert } 467107b26c94SSteffen Klassert 467207b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 467307b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 467407b26c94SSteffen Klassert else if (tail != segs) 467507b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4676802ab55aSAlexander Duyck } 4677802ab55aSAlexander Duyck 4678432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4679432c856fSToshiaki Makita * using skb_set_owner_w(). 4680432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4681432c856fSToshiaki Makita */ 4682432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4683432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4684432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4685432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4686432c856fSToshiaki Makita } 4687f4c50d99SHerbert Xu return segs; 4688f4c50d99SHerbert Xu 4689f4c50d99SHerbert Xu err: 4690289dccbeSEric Dumazet kfree_skb_list(segs); 4691f4c50d99SHerbert Xu return ERR_PTR(err); 4692f4c50d99SHerbert Xu } 4693f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4694f4c50d99SHerbert Xu 4695df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4696df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4697df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4698df5042f4SFlorian Westphal 4699df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4700df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4701df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4702df5042f4SFlorian Westphal #endif 47034165079bSFlorian Westphal #ifdef CONFIG_XFRM 47044165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 47054165079bSFlorian Westphal #endif 470695a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 470795a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 470895a7233cSPaul Blakey #endif 47093ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47103ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 47113ee17bc7SMat Martineau #endif 471278476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 471378476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 471478476d31SJeremy Kerr #endif 4715df5042f4SFlorian Westphal }; 4716df5042f4SFlorian Westphal 4717df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4718df5042f4SFlorian Westphal { 4719df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4720df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4721df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4722df5042f4SFlorian Westphal #endif 47234165079bSFlorian Westphal #ifdef CONFIG_XFRM 47244165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 47254165079bSFlorian Westphal #endif 472695a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 472795a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 472895a7233cSPaul Blakey #endif 47293ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 47303ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 47313ee17bc7SMat Martineau #endif 473278476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 473378476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 473478476d31SJeremy Kerr #endif 4735df5042f4SFlorian Westphal 0; 4736df5042f4SFlorian Westphal } 4737df5042f4SFlorian Westphal 4738df5042f4SFlorian Westphal static void skb_extensions_init(void) 4739df5042f4SFlorian Westphal { 4740df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4741df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4742df5042f4SFlorian Westphal 4743df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4744df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4745df5042f4SFlorian Westphal 0, 4746df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4747df5042f4SFlorian Westphal NULL); 4748df5042f4SFlorian Westphal } 4749df5042f4SFlorian Westphal #else 4750df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4751df5042f4SFlorian Westphal #endif 4752df5042f4SFlorian Westphal 47531da177e4SLinus Torvalds void __init skb_init(void) 47541da177e4SLinus Torvalds { 4755025a785fSJakub Kicinski skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache", 47561da177e4SLinus Torvalds sizeof(struct sk_buff), 47571da177e4SLinus Torvalds 0, 4758e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 475979a8a642SKees Cook offsetof(struct sk_buff, cb), 476079a8a642SKees Cook sizeof_field(struct sk_buff, cb), 476120c2df83SPaul Mundt NULL); 4762d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4763d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4764d179cd12SDavid S. Miller 0, 4765e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 476620c2df83SPaul Mundt NULL); 47670b34d680SEric Dumazet /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes. 47680b34d680SEric Dumazet * struct skb_shared_info is located at the end of skb->head, 47690b34d680SEric Dumazet * and should not be copied to/from user. 47700b34d680SEric Dumazet */ 47710b34d680SEric Dumazet skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head", 4772bf9f1baaSEric Dumazet SKB_SMALL_HEAD_CACHE_SIZE, 4773bf9f1baaSEric Dumazet 0, 4774bf9f1baaSEric Dumazet SLAB_HWCACHE_ALIGN | SLAB_PANIC, 47750b34d680SEric Dumazet 0, 47760b34d680SEric Dumazet SKB_SMALL_HEAD_HEADROOM, 4777bf9f1baaSEric Dumazet NULL); 4778df5042f4SFlorian Westphal skb_extensions_init(); 47791da177e4SLinus Torvalds } 47801da177e4SLinus Torvalds 478151c739d1SDavid S. Miller static int 478248a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 478348a1df65SJason A. Donenfeld unsigned int recursion_level) 4784716ea3a7SDavid Howells { 47851a028e50SDavid S. Miller int start = skb_headlen(skb); 47861a028e50SDavid S. Miller int i, copy = start - offset; 4787fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4788716ea3a7SDavid Howells int elt = 0; 4789716ea3a7SDavid Howells 479048a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 479148a1df65SJason A. Donenfeld return -EMSGSIZE; 479248a1df65SJason A. Donenfeld 4793716ea3a7SDavid Howells if (copy > 0) { 4794716ea3a7SDavid Howells if (copy > len) 4795716ea3a7SDavid Howells copy = len; 4796642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4797716ea3a7SDavid Howells elt++; 4798716ea3a7SDavid Howells if ((len -= copy) == 0) 4799716ea3a7SDavid Howells return elt; 4800716ea3a7SDavid Howells offset += copy; 4801716ea3a7SDavid Howells } 4802716ea3a7SDavid Howells 4803716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 48041a028e50SDavid S. Miller int end; 4805716ea3a7SDavid Howells 4806547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48071a028e50SDavid S. Miller 48089e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4809716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4810716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 481148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 481248a1df65SJason A. Donenfeld return -EMSGSIZE; 4813716ea3a7SDavid Howells 4814716ea3a7SDavid Howells if (copy > len) 4815716ea3a7SDavid Howells copy = len; 4816ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4817b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4818716ea3a7SDavid Howells elt++; 4819716ea3a7SDavid Howells if (!(len -= copy)) 4820716ea3a7SDavid Howells return elt; 4821716ea3a7SDavid Howells offset += copy; 4822716ea3a7SDavid Howells } 48231a028e50SDavid S. Miller start = end; 4824716ea3a7SDavid Howells } 4825716ea3a7SDavid Howells 4826fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 482748a1df65SJason A. Donenfeld int end, ret; 4828716ea3a7SDavid Howells 4829547b792cSIlpo Järvinen WARN_ON(start > offset + len); 48301a028e50SDavid S. Miller 4831fbb398a8SDavid S. Miller end = start + frag_iter->len; 4832716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 483348a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 483448a1df65SJason A. Donenfeld return -EMSGSIZE; 483548a1df65SJason A. Donenfeld 4836716ea3a7SDavid Howells if (copy > len) 4837716ea3a7SDavid Howells copy = len; 483848a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 483948a1df65SJason A. Donenfeld copy, recursion_level + 1); 484048a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 484148a1df65SJason A. Donenfeld return ret; 484248a1df65SJason A. Donenfeld elt += ret; 4843716ea3a7SDavid Howells if ((len -= copy) == 0) 4844716ea3a7SDavid Howells return elt; 4845716ea3a7SDavid Howells offset += copy; 4846716ea3a7SDavid Howells } 48471a028e50SDavid S. Miller start = end; 4848716ea3a7SDavid Howells } 4849716ea3a7SDavid Howells BUG_ON(len); 4850716ea3a7SDavid Howells return elt; 4851716ea3a7SDavid Howells } 4852716ea3a7SDavid Howells 485348a1df65SJason A. Donenfeld /** 485448a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 485548a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 485648a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 485748a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 485848a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 485948a1df65SJason A. Donenfeld * 486048a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 486148a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 486248a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 486348a1df65SJason A. Donenfeld * could not fit. 486448a1df65SJason A. Donenfeld */ 486548a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 486648a1df65SJason A. Donenfeld { 486748a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 486848a1df65SJason A. Donenfeld 486948a1df65SJason A. Donenfeld if (nsg <= 0) 487048a1df65SJason A. Donenfeld return nsg; 487148a1df65SJason A. Donenfeld 487248a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 487348a1df65SJason A. Donenfeld 487448a1df65SJason A. Donenfeld return nsg; 487548a1df65SJason A. Donenfeld } 487648a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 487748a1df65SJason A. Donenfeld 487825a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 487925a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 488025a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 488125a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 488225a91d8dSFan Du * 488325a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 488425a91d8dSFan Du * 1. sg_init_table 488525a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 488625a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 488725a91d8dSFan Du * 488825a91d8dSFan Du * This is equivalent to: 488925a91d8dSFan Du * 1. sg_init_table 489025a91d8dSFan Du * 2. skb_to_sgvec(payload1) 489125a91d8dSFan Du * 3. sg_unmark_end 489225a91d8dSFan Du * 4. skb_to_sgvec(payload2) 489325a91d8dSFan Du * 489425a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 489525a91d8dSFan Du * is more preferable. 489625a91d8dSFan Du */ 489725a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 489825a91d8dSFan Du int offset, int len) 489925a91d8dSFan Du { 490048a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 490125a91d8dSFan Du } 490225a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 490325a91d8dSFan Du 490451c739d1SDavid S. Miller 490551c739d1SDavid S. Miller 4906716ea3a7SDavid Howells /** 4907716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4908716ea3a7SDavid Howells * @skb: The socket buffer to check. 4909716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4910716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4911716ea3a7SDavid Howells * 4912716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4913716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4914716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4915716ea3a7SDavid Howells * 4916716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4917716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4918716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4919716ea3a7SDavid Howells * 4920716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4921716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4922716ea3a7SDavid Howells */ 4923716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4924716ea3a7SDavid Howells { 4925716ea3a7SDavid Howells int copyflag; 4926716ea3a7SDavid Howells int elt; 4927716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4928716ea3a7SDavid Howells 4929716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4930716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4931716ea3a7SDavid Howells * at the moment even if they are anonymous). 4932716ea3a7SDavid Howells */ 4933716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4934c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4935716ea3a7SDavid Howells return -ENOMEM; 4936716ea3a7SDavid Howells 4937716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 493821dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4939716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4940716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4941716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4942716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4943716ea3a7SDavid Howells 4944716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4945716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4946716ea3a7SDavid Howells return -ENOMEM; 4947716ea3a7SDavid Howells 4948716ea3a7SDavid Howells /* Voila! */ 4949716ea3a7SDavid Howells *trailer = skb; 4950716ea3a7SDavid Howells return 1; 4951716ea3a7SDavid Howells } 4952716ea3a7SDavid Howells 4953716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4954716ea3a7SDavid Howells 4955716ea3a7SDavid Howells elt = 1; 4956716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4957716ea3a7SDavid Howells copyflag = 0; 4958716ea3a7SDavid Howells 4959716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4960716ea3a7SDavid Howells int ntail = 0; 4961716ea3a7SDavid Howells 4962716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4963716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4964716ea3a7SDavid Howells * after it. */ 4965716ea3a7SDavid Howells 4966716ea3a7SDavid Howells if (skb_shared(skb1)) 4967716ea3a7SDavid Howells copyflag = 1; 4968716ea3a7SDavid Howells 4969716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4970716ea3a7SDavid Howells 4971716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4972716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 497321dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4974716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4975716ea3a7SDavid Howells ntail = tailbits + 128; 4976716ea3a7SDavid Howells } 4977716ea3a7SDavid Howells 4978716ea3a7SDavid Howells if (copyflag || 4979716ea3a7SDavid Howells skb_cloned(skb1) || 4980716ea3a7SDavid Howells ntail || 4981716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 498221dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4983716ea3a7SDavid Howells struct sk_buff *skb2; 4984716ea3a7SDavid Howells 4985716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4986716ea3a7SDavid Howells if (ntail == 0) 4987716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4988716ea3a7SDavid Howells else 4989716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4990716ea3a7SDavid Howells skb_headroom(skb1), 4991716ea3a7SDavid Howells ntail, 4992716ea3a7SDavid Howells GFP_ATOMIC); 4993716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4994716ea3a7SDavid Howells return -ENOMEM; 4995716ea3a7SDavid Howells 4996716ea3a7SDavid Howells if (skb1->sk) 4997716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4998716ea3a7SDavid Howells 4999716ea3a7SDavid Howells /* Looking around. Are we still alive? 5000716ea3a7SDavid Howells * OK, link new skb, drop old one */ 5001716ea3a7SDavid Howells 5002716ea3a7SDavid Howells skb2->next = skb1->next; 5003716ea3a7SDavid Howells *skb_p = skb2; 5004716ea3a7SDavid Howells kfree_skb(skb1); 5005716ea3a7SDavid Howells skb1 = skb2; 5006716ea3a7SDavid Howells } 5007716ea3a7SDavid Howells elt++; 5008716ea3a7SDavid Howells *trailer = skb1; 5009716ea3a7SDavid Howells skb_p = &skb1->next; 5010716ea3a7SDavid Howells } 5011716ea3a7SDavid Howells 5012716ea3a7SDavid Howells return elt; 5013716ea3a7SDavid Howells } 5014b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 5015716ea3a7SDavid Howells 5016b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 5017b1faf566SEric Dumazet { 5018b1faf566SEric Dumazet struct sock *sk = skb->sk; 5019b1faf566SEric Dumazet 5020b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 5021b1faf566SEric Dumazet } 5022b1faf566SEric Dumazet 50238605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 50248605330aSSoheil Hassas Yeganeh { 50258605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 50268605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 50278605330aSSoheil Hassas Yeganeh */ 50288605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 50298605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 50308605330aSSoheil Hassas Yeganeh } 50318605330aSSoheil Hassas Yeganeh 5032b1faf566SEric Dumazet /* 5033b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 5034b1faf566SEric Dumazet */ 5035b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 5036b1faf566SEric Dumazet { 5037b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 5038ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 5039b1faf566SEric Dumazet return -ENOMEM; 5040b1faf566SEric Dumazet 5041b1faf566SEric Dumazet skb_orphan(skb); 5042b1faf566SEric Dumazet skb->sk = sk; 5043b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 5044b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 50458605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 5046b1faf566SEric Dumazet 5047abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 5048abb57ea4SEric Dumazet skb_dst_force(skb); 5049abb57ea4SEric Dumazet 5050b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 5051b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 5052e3ae2365SAlexander Aring sk_error_report(sk); 5053b1faf566SEric Dumazet return 0; 5054b1faf566SEric Dumazet } 5055b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 5056b1faf566SEric Dumazet 505783a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 505883a1a1a7SSoheil Hassas Yeganeh { 505983a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 506083a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 506183a1a1a7SSoheil Hassas Yeganeh } 506283a1a1a7SSoheil Hassas Yeganeh 5063364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 5064364a9e93SWillem de Bruijn { 5065364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 506683a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 506783a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 5068997d5c3fSEric Dumazet unsigned long flags; 5069364a9e93SWillem de Bruijn 5070997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 5071364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 507238b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 507383a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 507438b25793SSoheil Hassas Yeganeh if (icmp_next) 5075985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 507638b25793SSoheil Hassas Yeganeh } 5077997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 5078364a9e93SWillem de Bruijn 507983a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 508083a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 508183a1a1a7SSoheil Hassas Yeganeh 508283a1a1a7SSoheil Hassas Yeganeh if (skb_next) 5083e3ae2365SAlexander Aring sk_error_report(sk); 5084364a9e93SWillem de Bruijn 5085364a9e93SWillem de Bruijn return skb; 5086364a9e93SWillem de Bruijn } 5087364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 5088364a9e93SWillem de Bruijn 5089cab41c47SAlexander Duyck /** 5090cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 5091cab41c47SAlexander Duyck * @skb: the skb to clone 5092cab41c47SAlexander Duyck * 5093cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 5094cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 5095cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 5096cab41c47SAlexander Duyck * 5097cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 5098cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 5099cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 5100cab41c47SAlexander Duyck * the sk_error_queue. 5101cab41c47SAlexander Duyck */ 510262bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 510362bccb8cSAlexander Duyck { 510462bccb8cSAlexander Duyck struct sock *sk = skb->sk; 510562bccb8cSAlexander Duyck struct sk_buff *clone; 510662bccb8cSAlexander Duyck 510741c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 510862bccb8cSAlexander Duyck return NULL; 510962bccb8cSAlexander Duyck 511062bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 511162bccb8cSAlexander Duyck if (!clone) { 511262bccb8cSAlexander Duyck sock_put(sk); 511362bccb8cSAlexander Duyck return NULL; 511462bccb8cSAlexander Duyck } 511562bccb8cSAlexander Duyck 511662bccb8cSAlexander Duyck clone->sk = sk; 511762bccb8cSAlexander Duyck clone->destructor = sock_efree; 511862bccb8cSAlexander Duyck 511962bccb8cSAlexander Duyck return clone; 512062bccb8cSAlexander Duyck } 512162bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 512262bccb8cSAlexander Duyck 512337846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 512437846ef0SAlexander Duyck struct sock *sk, 51254ef1b286SSoheil Hassas Yeganeh int tstype, 51264ef1b286SSoheil Hassas Yeganeh bool opt_stats) 5127ac45f602SPatrick Ohly { 5128ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 5129ac45f602SPatrick Ohly int err; 5130ac45f602SPatrick Ohly 51314ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 51324ef1b286SSoheil Hassas Yeganeh 5133ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5134ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5135ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5136ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5137e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 51384ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 51391862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 51404ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 514109c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 514242f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5143a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 51444ed2d765SWillem de Bruijn } 514529030374SEric Dumazet 5146ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 514729030374SEric Dumazet 5148ac45f602SPatrick Ohly if (err) 5149ac45f602SPatrick Ohly kfree_skb(skb); 5150ac45f602SPatrick Ohly } 515137846ef0SAlexander Duyck 5152b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5153b245be1fSWillem de Bruijn { 5154b245be1fSWillem de Bruijn bool ret; 5155b245be1fSWillem de Bruijn 5156d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5157b245be1fSWillem de Bruijn return true; 5158b245be1fSWillem de Bruijn 5159b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5160b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5161b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5162b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5163b245be1fSWillem de Bruijn return ret; 5164b245be1fSWillem de Bruijn } 5165b245be1fSWillem de Bruijn 516637846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 516737846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 516837846ef0SAlexander Duyck { 516937846ef0SAlexander Duyck struct sock *sk = skb->sk; 517037846ef0SAlexander Duyck 5171b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 517235b99dffSWillem de Bruijn goto err; 5173b245be1fSWillem de Bruijn 51749ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 51759ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 51769ac25fc0SEric Dumazet */ 517741c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 517837846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 51794ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 518037846ef0SAlexander Duyck sock_put(sk); 518135b99dffSWillem de Bruijn return; 518237846ef0SAlexander Duyck } 518335b99dffSWillem de Bruijn 518435b99dffSWillem de Bruijn err: 518535b99dffSWillem de Bruijn kfree_skb(skb); 51869ac25fc0SEric Dumazet } 518737846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 518837846ef0SAlexander Duyck 518937846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5190e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 519137846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 519237846ef0SAlexander Duyck struct sock *sk, int tstype) 519337846ef0SAlexander Duyck { 519437846ef0SAlexander Duyck struct sk_buff *skb; 51954ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 519637846ef0SAlexander Duyck 51973a8dd971SWillem de Bruijn if (!sk) 51983a8dd971SWillem de Bruijn return; 51993a8dd971SWillem de Bruijn 5200b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5201b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5202b50a5c70SMiroslav Lichvar return; 5203b50a5c70SMiroslav Lichvar 52043a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 52053a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 520637846ef0SAlexander Duyck return; 520737846ef0SAlexander Duyck 52081c885808SFrancis Yan if (tsonly) { 52091c885808SFrancis Yan #ifdef CONFIG_INET 52101c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 521142f67eeaSEric Dumazet sk_is_tcp(sk)) { 5212e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5213e7ed11eeSYousuk Seung ack_skb); 52144ef1b286SSoheil Hassas Yeganeh opt_stats = true; 52154ef1b286SSoheil Hassas Yeganeh } else 52161c885808SFrancis Yan #endif 52171c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 52181c885808SFrancis Yan } else { 521937846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 522050749f2dSKuniyuki Iwashima 52218a02fb71SPratyush Yadav if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) { 52228a02fb71SPratyush Yadav kfree_skb(skb); 522350749f2dSKuniyuki Iwashima return; 52241c885808SFrancis Yan } 52258a02fb71SPratyush Yadav } 522637846ef0SAlexander Duyck if (!skb) 522737846ef0SAlexander Duyck return; 522837846ef0SAlexander Duyck 522949ca0d8bSWillem de Bruijn if (tsonly) { 5230fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5231fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 523249ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 523349ca0d8bSWillem de Bruijn } 523449ca0d8bSWillem de Bruijn 523549ca0d8bSWillem de Bruijn if (hwtstamps) 523649ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 523749ca0d8bSWillem de Bruijn else 5238d93376f5SMartin KaFai Lau __net_timestamp(skb); 523949ca0d8bSWillem de Bruijn 52404ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 524137846ef0SAlexander Duyck } 5242e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5243e7fd2885SWillem de Bruijn 5244e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5245e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5246e7fd2885SWillem de Bruijn { 5247e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5248e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5249e7fd2885SWillem de Bruijn } 5250ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5251ac45f602SPatrick Ohly 5252eb6fba75SJakub Kicinski #ifdef CONFIG_WIRELESS 52536e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 52546e3e939fSJohannes Berg { 52556e3e939fSJohannes Berg struct sock *sk = skb->sk; 52566e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5257dd4f1072SEric Dumazet int err = 1; 52586e3e939fSJohannes Berg 52596e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 52606e3e939fSJohannes Berg skb->wifi_acked = acked; 52616e3e939fSJohannes Berg 52626e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 52636e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 52646e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 52656e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 52666e3e939fSJohannes Berg 5267dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5268dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5269dd4f1072SEric Dumazet */ 527041c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 52716e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5272dd4f1072SEric Dumazet sock_put(sk); 5273dd4f1072SEric Dumazet } 52746e3e939fSJohannes Berg if (err) 52756e3e939fSJohannes Berg kfree_skb(skb); 52766e3e939fSJohannes Berg } 52776e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 5278eb6fba75SJakub Kicinski #endif /* CONFIG_WIRELESS */ 52796e3e939fSJohannes Berg 5280f35d9d8aSRusty Russell /** 5281f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5282f35d9d8aSRusty Russell * @skb: the skb to set 5283f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5284f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5285f35d9d8aSRusty Russell * 5286f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5287f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5288f35d9d8aSRusty Russell * 5289f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5290f35d9d8aSRusty Russell * returns false you should drop the packet. 5291f35d9d8aSRusty Russell */ 5292f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5293f35d9d8aSRusty Russell { 529452b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 529552b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 529652b5d6f5SEric Dumazet 5297424f8416SEric Dumazet if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) { 529852b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 529952b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5300f35d9d8aSRusty Russell return false; 5301f35d9d8aSRusty Russell } 5302f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 530352b5d6f5SEric Dumazet skb->csum_start = csum_start; 5304f35d9d8aSRusty Russell skb->csum_offset = off; 5305424f8416SEric Dumazet skb->transport_header = csum_start; 5306f35d9d8aSRusty Russell return true; 5307f35d9d8aSRusty Russell } 5308b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5309f35d9d8aSRusty Russell 5310ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5311ed1f50c3SPaul Durrant unsigned int max) 5312ed1f50c3SPaul Durrant { 5313ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5314ed1f50c3SPaul Durrant return 0; 5315ed1f50c3SPaul Durrant 5316ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5317ed1f50c3SPaul Durrant * won't need to do it again. 5318ed1f50c3SPaul Durrant */ 5319ed1f50c3SPaul Durrant if (max > skb->len) 5320ed1f50c3SPaul Durrant max = skb->len; 5321ed1f50c3SPaul Durrant 5322ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5323ed1f50c3SPaul Durrant return -ENOMEM; 5324ed1f50c3SPaul Durrant 5325ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5326ed1f50c3SPaul Durrant return -EPROTO; 5327ed1f50c3SPaul Durrant 5328ed1f50c3SPaul Durrant return 0; 5329ed1f50c3SPaul Durrant } 5330ed1f50c3SPaul Durrant 5331f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5332f9708b43SJan Beulich 5333f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5334f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5335f9708b43SJan Beulich unsigned int off) 5336f9708b43SJan Beulich { 5337f9708b43SJan Beulich int err; 5338f9708b43SJan Beulich 5339161d1792SKees Cook switch (proto) { 5340f9708b43SJan Beulich case IPPROTO_TCP: 5341f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5342f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5343f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5344f9708b43SJan Beulich offsetof(struct tcphdr, 5345f9708b43SJan Beulich check))) 5346f9708b43SJan Beulich err = -EPROTO; 5347f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5348f9708b43SJan Beulich 5349f9708b43SJan Beulich case IPPROTO_UDP: 5350f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5351f9708b43SJan Beulich off + sizeof(struct udphdr)); 5352f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5353f9708b43SJan Beulich offsetof(struct udphdr, 5354f9708b43SJan Beulich check))) 5355f9708b43SJan Beulich err = -EPROTO; 5356f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5357f9708b43SJan Beulich } 5358f9708b43SJan Beulich 5359f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5360f9708b43SJan Beulich } 5361f9708b43SJan Beulich 5362ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5363ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5364ed1f50c3SPaul Durrant */ 5365ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5366ed1f50c3SPaul Durrant 5367f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5368ed1f50c3SPaul Durrant { 5369ed1f50c3SPaul Durrant unsigned int off; 5370ed1f50c3SPaul Durrant bool fragment; 5371f9708b43SJan Beulich __sum16 *csum; 5372ed1f50c3SPaul Durrant int err; 5373ed1f50c3SPaul Durrant 5374ed1f50c3SPaul Durrant fragment = false; 5375ed1f50c3SPaul Durrant 5376ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5377ed1f50c3SPaul Durrant sizeof(struct iphdr), 5378ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5379ed1f50c3SPaul Durrant if (err < 0) 5380ed1f50c3SPaul Durrant goto out; 5381ed1f50c3SPaul Durrant 538211f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5383ed1f50c3SPaul Durrant fragment = true; 5384ed1f50c3SPaul Durrant 5385ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5386ed1f50c3SPaul Durrant 5387ed1f50c3SPaul Durrant err = -EPROTO; 5388ed1f50c3SPaul Durrant 5389ed1f50c3SPaul Durrant if (fragment) 5390ed1f50c3SPaul Durrant goto out; 5391ed1f50c3SPaul Durrant 5392f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5393f9708b43SJan Beulich if (IS_ERR(csum)) 5394f9708b43SJan Beulich return PTR_ERR(csum); 5395ed1f50c3SPaul Durrant 5396ed1f50c3SPaul Durrant if (recalculate) 5397f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5398ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5399ed1f50c3SPaul Durrant skb->len - off, 5400f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5401ed1f50c3SPaul Durrant err = 0; 5402ed1f50c3SPaul Durrant 5403ed1f50c3SPaul Durrant out: 5404ed1f50c3SPaul Durrant return err; 5405ed1f50c3SPaul Durrant } 5406ed1f50c3SPaul Durrant 5407ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5408ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5409ed1f50c3SPaul Durrant */ 5410ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5411ed1f50c3SPaul Durrant 5412ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5413ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5414ed1f50c3SPaul Durrant 5415ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5416ed1f50c3SPaul Durrant { 5417ed1f50c3SPaul Durrant int err; 5418ed1f50c3SPaul Durrant u8 nexthdr; 5419ed1f50c3SPaul Durrant unsigned int off; 5420ed1f50c3SPaul Durrant unsigned int len; 5421ed1f50c3SPaul Durrant bool fragment; 5422ed1f50c3SPaul Durrant bool done; 5423f9708b43SJan Beulich __sum16 *csum; 5424ed1f50c3SPaul Durrant 5425ed1f50c3SPaul Durrant fragment = false; 5426ed1f50c3SPaul Durrant done = false; 5427ed1f50c3SPaul Durrant 5428ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5429ed1f50c3SPaul Durrant 5430ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5431ed1f50c3SPaul Durrant if (err < 0) 5432ed1f50c3SPaul Durrant goto out; 5433ed1f50c3SPaul Durrant 5434ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5435ed1f50c3SPaul Durrant 5436ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5437ed1f50c3SPaul Durrant while (off <= len && !done) { 5438ed1f50c3SPaul Durrant switch (nexthdr) { 5439ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5440ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5441ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5442ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5443ed1f50c3SPaul Durrant 5444ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5445ed1f50c3SPaul Durrant off + 5446ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5447ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5448ed1f50c3SPaul Durrant if (err < 0) 5449ed1f50c3SPaul Durrant goto out; 5450ed1f50c3SPaul Durrant 5451ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5452ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5453ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5454ed1f50c3SPaul Durrant break; 5455ed1f50c3SPaul Durrant } 5456ed1f50c3SPaul Durrant case IPPROTO_AH: { 5457ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5458ed1f50c3SPaul Durrant 5459ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5460ed1f50c3SPaul Durrant off + 5461ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5462ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5463ed1f50c3SPaul Durrant if (err < 0) 5464ed1f50c3SPaul Durrant goto out; 5465ed1f50c3SPaul Durrant 5466ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5467ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5468ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5469ed1f50c3SPaul Durrant break; 5470ed1f50c3SPaul Durrant } 5471ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5472ed1f50c3SPaul Durrant struct frag_hdr *hp; 5473ed1f50c3SPaul Durrant 5474ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5475ed1f50c3SPaul Durrant off + 5476ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5477ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5478ed1f50c3SPaul Durrant if (err < 0) 5479ed1f50c3SPaul Durrant goto out; 5480ed1f50c3SPaul Durrant 5481ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5482ed1f50c3SPaul Durrant 5483ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5484ed1f50c3SPaul Durrant fragment = true; 5485ed1f50c3SPaul Durrant 5486ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5487ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5488ed1f50c3SPaul Durrant break; 5489ed1f50c3SPaul Durrant } 5490ed1f50c3SPaul Durrant default: 5491ed1f50c3SPaul Durrant done = true; 5492ed1f50c3SPaul Durrant break; 5493ed1f50c3SPaul Durrant } 5494ed1f50c3SPaul Durrant } 5495ed1f50c3SPaul Durrant 5496ed1f50c3SPaul Durrant err = -EPROTO; 5497ed1f50c3SPaul Durrant 5498ed1f50c3SPaul Durrant if (!done || fragment) 5499ed1f50c3SPaul Durrant goto out; 5500ed1f50c3SPaul Durrant 5501f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5502f9708b43SJan Beulich if (IS_ERR(csum)) 5503f9708b43SJan Beulich return PTR_ERR(csum); 5504ed1f50c3SPaul Durrant 5505ed1f50c3SPaul Durrant if (recalculate) 5506f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5507ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5508f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5509ed1f50c3SPaul Durrant err = 0; 5510ed1f50c3SPaul Durrant 5511ed1f50c3SPaul Durrant out: 5512ed1f50c3SPaul Durrant return err; 5513ed1f50c3SPaul Durrant } 5514ed1f50c3SPaul Durrant 5515ed1f50c3SPaul Durrant /** 5516ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5517ed1f50c3SPaul Durrant * @skb: the skb to set up 5518ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5519ed1f50c3SPaul Durrant */ 5520ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5521ed1f50c3SPaul Durrant { 5522ed1f50c3SPaul Durrant int err; 5523ed1f50c3SPaul Durrant 5524ed1f50c3SPaul Durrant switch (skb->protocol) { 5525ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5526f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5527ed1f50c3SPaul Durrant break; 5528ed1f50c3SPaul Durrant 5529ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5530ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5531ed1f50c3SPaul Durrant break; 5532ed1f50c3SPaul Durrant 5533ed1f50c3SPaul Durrant default: 5534ed1f50c3SPaul Durrant err = -EPROTO; 5535ed1f50c3SPaul Durrant break; 5536ed1f50c3SPaul Durrant } 5537ed1f50c3SPaul Durrant 5538ed1f50c3SPaul Durrant return err; 5539ed1f50c3SPaul Durrant } 5540ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5541ed1f50c3SPaul Durrant 55429afd85c9SLinus Lüssing /** 55439afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 55449afd85c9SLinus Lüssing * @skb: the skb to check 55459afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55469afd85c9SLinus Lüssing * 55479afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 55489afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 55499afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 55509afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 55519afd85c9SLinus Lüssing * 5552a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5553a516993fSLinus Lüssing * differs from the provided skb. 55549afd85c9SLinus Lüssing */ 55559afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 55569afd85c9SLinus Lüssing unsigned int transport_len) 55579afd85c9SLinus Lüssing { 55589afd85c9SLinus Lüssing struct sk_buff *skb_chk; 55599afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 55609afd85c9SLinus Lüssing int ret; 55619afd85c9SLinus Lüssing 5562a516993fSLinus Lüssing if (skb->len < len) 55639afd85c9SLinus Lüssing return NULL; 5564a516993fSLinus Lüssing else if (skb->len == len) 55659afd85c9SLinus Lüssing return skb; 55669afd85c9SLinus Lüssing 55679afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 55689afd85c9SLinus Lüssing if (!skb_chk) 55699afd85c9SLinus Lüssing return NULL; 55709afd85c9SLinus Lüssing 55719afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 55729afd85c9SLinus Lüssing if (ret) { 55739afd85c9SLinus Lüssing kfree_skb(skb_chk); 55749afd85c9SLinus Lüssing return NULL; 55759afd85c9SLinus Lüssing } 55769afd85c9SLinus Lüssing 55779afd85c9SLinus Lüssing return skb_chk; 55789afd85c9SLinus Lüssing } 55799afd85c9SLinus Lüssing 55809afd85c9SLinus Lüssing /** 55819afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 55829afd85c9SLinus Lüssing * @skb: the skb to check 55839afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 55849afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 55859afd85c9SLinus Lüssing * 55869afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 55879afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 55889afd85c9SLinus Lüssing * 55899afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 55909afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 55919afd85c9SLinus Lüssing * 5592a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5593a516993fSLinus Lüssing * differs from the provided skb. 55949afd85c9SLinus Lüssing */ 55959afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 55969afd85c9SLinus Lüssing unsigned int transport_len, 55979afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 55989afd85c9SLinus Lüssing { 55999afd85c9SLinus Lüssing struct sk_buff *skb_chk; 56009afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5601fcba67c9SLinus Lüssing __sum16 ret; 56029afd85c9SLinus Lüssing 56039afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 56049afd85c9SLinus Lüssing if (!skb_chk) 5605a516993fSLinus Lüssing goto err; 56069afd85c9SLinus Lüssing 5607a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5608a516993fSLinus Lüssing goto err; 56099afd85c9SLinus Lüssing 56109b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 56119afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 56129b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 56139afd85c9SLinus Lüssing 5614a516993fSLinus Lüssing if (ret) 5615a516993fSLinus Lüssing goto err; 56169afd85c9SLinus Lüssing 56179afd85c9SLinus Lüssing return skb_chk; 5618a516993fSLinus Lüssing 5619a516993fSLinus Lüssing err: 5620a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5621a516993fSLinus Lüssing kfree_skb(skb_chk); 5622a516993fSLinus Lüssing 5623a516993fSLinus Lüssing return NULL; 5624a516993fSLinus Lüssing 56259afd85c9SLinus Lüssing } 56269afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 56279afd85c9SLinus Lüssing 56284497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 56294497b076SBen Hutchings { 5630e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5631e87cc472SJoe Perches skb->dev->name); 56324497b076SBen Hutchings } 56334497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5634bad43ca8SEric Dumazet 5635bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5636bad43ca8SEric Dumazet { 56373d861f66SEric Dumazet if (head_stolen) { 56383d861f66SEric Dumazet skb_release_head_state(skb); 5639025a785fSJakub Kicinski kmem_cache_free(skbuff_cache, skb); 56403d861f66SEric Dumazet } else { 5641bad43ca8SEric Dumazet __kfree_skb(skb); 5642bad43ca8SEric Dumazet } 56433d861f66SEric Dumazet } 5644bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5645bad43ca8SEric Dumazet 5646bad43ca8SEric Dumazet /** 5647bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5648bad43ca8SEric Dumazet * @to: prior buffer 5649bad43ca8SEric Dumazet * @from: buffer to add 5650bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5651c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5652bad43ca8SEric Dumazet */ 5653bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5654bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5655bad43ca8SEric Dumazet { 5656c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5657bad43ca8SEric Dumazet int i, delta, len = from->len; 5658bad43ca8SEric Dumazet 5659bad43ca8SEric Dumazet *fragstolen = false; 5660bad43ca8SEric Dumazet 5661bad43ca8SEric Dumazet if (skb_cloned(to)) 5662bad43ca8SEric Dumazet return false; 5663bad43ca8SEric Dumazet 56640646dc31SLiang Chen /* In general, avoid mixing page_pool and non-page_pool allocated 56650646dc31SLiang Chen * pages within the same SKB. Additionally avoid dealing with clones 56660646dc31SLiang Chen * with page_pool pages, in case the SKB is using page_pool fragment 56671effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 56681effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 56691effe8caSJean-Philippe Brucker * inconsistent reference counts. 56700646dc31SLiang Chen * In theory we could take full references if @from is cloned and 56710646dc31SLiang Chen * !@to->pp_recycle but its tricky (due to potential race with 56720646dc31SLiang Chen * the clone disappearing) and rare, so not worth dealing with. 56736a5bcd84SIlias Apalodimas */ 56740646dc31SLiang Chen if (to->pp_recycle != from->pp_recycle || 56750646dc31SLiang Chen (from->pp_recycle && skb_cloned(from))) 56766a5bcd84SIlias Apalodimas return false; 56776a5bcd84SIlias Apalodimas 5678bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5679e93a0435SEric Dumazet if (len) 5680bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5681bad43ca8SEric Dumazet *delta_truesize = 0; 5682bad43ca8SEric Dumazet return true; 5683bad43ca8SEric Dumazet } 5684bad43ca8SEric Dumazet 5685c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5686c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5687c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5688bad43ca8SEric Dumazet return false; 56891f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 56901f8b977aSWillem de Bruijn return false; 5691bad43ca8SEric Dumazet 5692bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5693bad43ca8SEric Dumazet struct page *page; 5694bad43ca8SEric Dumazet unsigned int offset; 5695bad43ca8SEric Dumazet 5696c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5697c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5698bad43ca8SEric Dumazet return false; 5699bad43ca8SEric Dumazet 5700bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5701bad43ca8SEric Dumazet return false; 5702bad43ca8SEric Dumazet 5703bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5704bad43ca8SEric Dumazet 5705bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5706bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5707bad43ca8SEric Dumazet 5708c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5709bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5710bad43ca8SEric Dumazet *fragstolen = true; 5711bad43ca8SEric Dumazet } else { 5712c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5713c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5714bad43ca8SEric Dumazet return false; 5715bad43ca8SEric Dumazet 5716f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5717bad43ca8SEric Dumazet } 5718bad43ca8SEric Dumazet 5719bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5720bad43ca8SEric Dumazet 5721c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5722c818fa9eSEric Dumazet from_shinfo->frags, 5723c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5724c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5725bad43ca8SEric Dumazet 5726bad43ca8SEric Dumazet if (!skb_cloned(from)) 5727c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5728bad43ca8SEric Dumazet 57298ea853fdSLi RongQing /* if the skb is not cloned this does nothing 57308ea853fdSLi RongQing * since we set nr_frags to 0. 57318ea853fdSLi RongQing */ 5732c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5733c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5734bad43ca8SEric Dumazet 5735bad43ca8SEric Dumazet to->truesize += delta; 5736bad43ca8SEric Dumazet to->len += len; 5737bad43ca8SEric Dumazet to->data_len += len; 5738bad43ca8SEric Dumazet 5739bad43ca8SEric Dumazet *delta_truesize = delta; 5740bad43ca8SEric Dumazet return true; 5741bad43ca8SEric Dumazet } 5742bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5743621e84d6SNicolas Dichtel 5744621e84d6SNicolas Dichtel /** 57458b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5746621e84d6SNicolas Dichtel * 5747621e84d6SNicolas Dichtel * @skb: buffer to clean 57488b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5749621e84d6SNicolas Dichtel * 57508b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 57518b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 57528b27f277SNicolas Dichtel * operations. 57538b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 57548b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 57558b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5756621e84d6SNicolas Dichtel */ 57578b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5758621e84d6SNicolas Dichtel { 5759621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5760621e84d6SNicolas Dichtel skb->skb_iif = 0; 576160ff7467SWANG Cong skb->ignore_df = 0; 5762621e84d6SNicolas Dichtel skb_dst_drop(skb); 5763174e2381SFlorian Westphal skb_ext_reset(skb); 5764895b5c9fSFlorian Westphal nf_reset_ct(skb); 5765621e84d6SNicolas Dichtel nf_reset_trace(skb); 5766213dd74aSHerbert Xu 57676f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 57686f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5769875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 57706f9a5069SPetr Machata #endif 57716f9a5069SPetr Machata 5772213dd74aSHerbert Xu if (!xnet) 5773213dd74aSHerbert Xu return; 5774213dd74aSHerbert Xu 57752b5ec1a5SYe Yin ipvs_reset(skb); 5776213dd74aSHerbert Xu skb->mark = 0; 5777de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5778621e84d6SNicolas Dichtel } 5779621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5780de960aa9SFlorian Westphal 57810d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 57820d5501c1SVlad Yasevich { 5783d85e8be2SYuya Kusakabe int mac_len, meta_len; 5784d85e8be2SYuya Kusakabe void *meta; 57854bbb3e0eSToshiaki Makita 57860d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 57870d5501c1SVlad Yasevich kfree_skb(skb); 57880d5501c1SVlad Yasevich return NULL; 57890d5501c1SVlad Yasevich } 57900d5501c1SVlad Yasevich 57914bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5792ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 57934bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 57944bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5795ae474573SToshiaki Makita } 5796d85e8be2SYuya Kusakabe 5797d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5798d85e8be2SYuya Kusakabe if (meta_len) { 5799d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5800d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5801d85e8be2SYuya Kusakabe } 5802d85e8be2SYuya Kusakabe 58030d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 58040d5501c1SVlad Yasevich return skb; 58050d5501c1SVlad Yasevich } 58060d5501c1SVlad Yasevich 58070d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 58080d5501c1SVlad Yasevich { 58090d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 58100d5501c1SVlad Yasevich u16 vlan_tci; 58110d5501c1SVlad Yasevich 5812df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 58130d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 58140d5501c1SVlad Yasevich return skb; 58150d5501c1SVlad Yasevich } 58160d5501c1SVlad Yasevich 58170d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 58180d5501c1SVlad Yasevich if (unlikely(!skb)) 58190d5501c1SVlad Yasevich goto err_free; 582055eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 582155eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 58220d5501c1SVlad Yasevich goto err_free; 58230d5501c1SVlad Yasevich 58240d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 58250d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 58260d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 58270d5501c1SVlad Yasevich 58280d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 58290d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 58300d5501c1SVlad Yasevich 58310d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 58320d5501c1SVlad Yasevich if (unlikely(!skb)) 58330d5501c1SVlad Yasevich goto err_free; 58340d5501c1SVlad Yasevich 58350d5501c1SVlad Yasevich skb_reset_network_header(skb); 58368be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 58370d5501c1SVlad Yasevich skb_reset_transport_header(skb); 58380d5501c1SVlad Yasevich skb_reset_mac_len(skb); 58390d5501c1SVlad Yasevich 58400d5501c1SVlad Yasevich return skb; 58410d5501c1SVlad Yasevich 58420d5501c1SVlad Yasevich err_free: 58430d5501c1SVlad Yasevich kfree_skb(skb); 58440d5501c1SVlad Yasevich return NULL; 58450d5501c1SVlad Yasevich } 58460d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 58472e4e4410SEric Dumazet 584892ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5849e2195121SJiri Pirko { 5850e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5851e2195121SJiri Pirko return -ENOMEM; 5852e2195121SJiri Pirko 5853e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5854e2195121SJiri Pirko return 0; 5855e2195121SJiri Pirko 5856e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5857e2195121SJiri Pirko } 5858e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5859e2195121SJiri Pirko 5860bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5861bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5862bfca4c52SShmulik Ladkani */ 5863bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 586493515d53SJiri Pirko { 5865b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 586693515d53SJiri Pirko int err; 586793515d53SJiri Pirko 5868b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5869b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5870b6a79208SShmulik Ladkani offset)) { 5871b6a79208SShmulik Ladkani return -EINVAL; 5872b6a79208SShmulik Ladkani } 5873b6a79208SShmulik Ladkani 587493515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 587593515d53SJiri Pirko if (unlikely(err)) 5876b6a79208SShmulik Ladkani return err; 587793515d53SJiri Pirko 587893515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 587993515d53SJiri Pirko 58800bcf2e4aSVladimir Oltean vlan_remove_tag(skb, vlan_tci); 588193515d53SJiri Pirko 588293515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 588393515d53SJiri Pirko 588493515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 588593515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 588693515d53SJiri Pirko 588793515d53SJiri Pirko skb_reset_mac_len(skb); 588893515d53SJiri Pirko 588993515d53SJiri Pirko return err; 589093515d53SJiri Pirko } 5891bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 589293515d53SJiri Pirko 5893b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5894b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5895b6a79208SShmulik Ladkani */ 589693515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 589793515d53SJiri Pirko { 589893515d53SJiri Pirko u16 vlan_tci; 589993515d53SJiri Pirko __be16 vlan_proto; 590093515d53SJiri Pirko int err; 590193515d53SJiri Pirko 5902df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5903b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 590493515d53SJiri Pirko } else { 5905ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 590693515d53SJiri Pirko return 0; 590793515d53SJiri Pirko 590893515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 590993515d53SJiri Pirko if (err) 591093515d53SJiri Pirko return err; 591193515d53SJiri Pirko } 591293515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5913ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 591493515d53SJiri Pirko return 0; 591593515d53SJiri Pirko 591693515d53SJiri Pirko vlan_proto = skb->protocol; 591793515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 591893515d53SJiri Pirko if (unlikely(err)) 591993515d53SJiri Pirko return err; 592093515d53SJiri Pirko 592193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 592293515d53SJiri Pirko return 0; 592393515d53SJiri Pirko } 592493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 592593515d53SJiri Pirko 5926b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5927b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5928b6a79208SShmulik Ladkani */ 592993515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 593093515d53SJiri Pirko { 5931df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5932b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 593393515d53SJiri Pirko int err; 593493515d53SJiri Pirko 5935b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5936b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5937b6a79208SShmulik Ladkani offset)) { 5938b6a79208SShmulik Ladkani return -EINVAL; 5939b6a79208SShmulik Ladkani } 5940b6a79208SShmulik Ladkani 594193515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5942df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5943b6a79208SShmulik Ladkani if (err) 594493515d53SJiri Pirko return err; 59459241e2dfSDaniel Borkmann 594693515d53SJiri Pirko skb->protocol = skb->vlan_proto; 594793515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 594893515d53SJiri Pirko 59496b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 595093515d53SJiri Pirko } 595193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 595293515d53SJiri Pirko return 0; 595393515d53SJiri Pirko } 595493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 595593515d53SJiri Pirko 595619fbcb36SGuillaume Nault /** 595719fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 595819fbcb36SGuillaume Nault * 595919fbcb36SGuillaume Nault * @skb: Socket buffer to modify 596019fbcb36SGuillaume Nault * 596119fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 596219fbcb36SGuillaume Nault * 596319fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 596419fbcb36SGuillaume Nault * present. 596519fbcb36SGuillaume Nault * 596619fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 596719fbcb36SGuillaume Nault */ 596819fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 596919fbcb36SGuillaume Nault { 597019fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 597119fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 597219fbcb36SGuillaume Nault return -EPROTO; 597319fbcb36SGuillaume Nault 597419fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 597519fbcb36SGuillaume Nault skb_reset_mac_header(skb); 597619fbcb36SGuillaume Nault skb_reset_mac_len(skb); 597719fbcb36SGuillaume Nault 597819fbcb36SGuillaume Nault return 0; 597919fbcb36SGuillaume Nault } 598019fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 598119fbcb36SGuillaume Nault 598219fbcb36SGuillaume Nault /** 598319fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 598419fbcb36SGuillaume Nault * 598519fbcb36SGuillaume Nault * @skb: Socket buffer to modify 598619fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 598719fbcb36SGuillaume Nault * @src: Source MAC address of the new header 598819fbcb36SGuillaume Nault * 598919fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 599019fbcb36SGuillaume Nault * 599119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 599219fbcb36SGuillaume Nault * 599319fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 599419fbcb36SGuillaume Nault */ 599519fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 599619fbcb36SGuillaume Nault const unsigned char *src) 599719fbcb36SGuillaume Nault { 599819fbcb36SGuillaume Nault struct ethhdr *eth; 599919fbcb36SGuillaume Nault int err; 600019fbcb36SGuillaume Nault 600119fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 600219fbcb36SGuillaume Nault return -EPROTO; 600319fbcb36SGuillaume Nault 600419fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 600519fbcb36SGuillaume Nault if (err < 0) 600619fbcb36SGuillaume Nault return err; 600719fbcb36SGuillaume Nault 600819fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 600919fbcb36SGuillaume Nault skb_reset_mac_header(skb); 601019fbcb36SGuillaume Nault skb_reset_mac_len(skb); 601119fbcb36SGuillaume Nault 601219fbcb36SGuillaume Nault eth = eth_hdr(skb); 601319fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 601419fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 601519fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 601619fbcb36SGuillaume Nault 601719fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 601819fbcb36SGuillaume Nault 601919fbcb36SGuillaume Nault return 0; 602019fbcb36SGuillaume Nault } 602119fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 602219fbcb36SGuillaume Nault 60238822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 60248822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 60258822e270SJohn Hurley __be16 ethertype) 60268822e270SJohn Hurley { 60278822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 60288822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 60298822e270SJohn Hurley 60308822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 60318822e270SJohn Hurley } 60328822e270SJohn Hurley 60338822e270SJohn Hurley hdr->h_proto = ethertype; 60348822e270SJohn Hurley } 60358822e270SJohn Hurley 60368822e270SJohn Hurley /** 6037e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6038e7dbfed1SMartin Varghese * the packet 60398822e270SJohn Hurley * 60408822e270SJohn Hurley * @skb: buffer 60418822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 60428822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6043fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6044e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6045e7dbfed1SMartin Varghese * ethernet 60468822e270SJohn Hurley * 60478822e270SJohn Hurley * Expects skb->data at mac header. 60488822e270SJohn Hurley * 60498822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 60508822e270SJohn Hurley */ 6051fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6052d04ac224SMartin Varghese int mac_len, bool ethernet) 60538822e270SJohn Hurley { 60548822e270SJohn Hurley struct mpls_shim_hdr *lse; 60558822e270SJohn Hurley int err; 60568822e270SJohn Hurley 60578822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 60588822e270SJohn Hurley return -EINVAL; 60598822e270SJohn Hurley 60608822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 60618822e270SJohn Hurley if (skb->encapsulation) 60628822e270SJohn Hurley return -EINVAL; 60638822e270SJohn Hurley 60648822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 60658822e270SJohn Hurley if (unlikely(err)) 60668822e270SJohn Hurley return err; 60678822e270SJohn Hurley 60688822e270SJohn Hurley if (!skb->inner_protocol) { 6069e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 60708822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 60718822e270SJohn Hurley } 60728822e270SJohn Hurley 60738822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 60748822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6075fa4e0f88SDavide Caratti mac_len); 60768822e270SJohn Hurley skb_reset_mac_header(skb); 6077fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6078e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 60798822e270SJohn Hurley 60808822e270SJohn Hurley lse = mpls_hdr(skb); 60818822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 60828822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 60838822e270SJohn Hurley 60844296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 60858822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 60868822e270SJohn Hurley skb->protocol = mpls_proto; 60878822e270SJohn Hurley 60888822e270SJohn Hurley return 0; 60898822e270SJohn Hurley } 60908822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 60918822e270SJohn Hurley 60922e4e4410SEric Dumazet /** 6093ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6094ed246ceeSJohn Hurley * 6095ed246ceeSJohn Hurley * @skb: buffer 6096ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6097fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 609876f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6099ed246ceeSJohn Hurley * 6100ed246ceeSJohn Hurley * Expects skb->data at mac header. 6101ed246ceeSJohn Hurley * 6102ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6103ed246ceeSJohn Hurley */ 6104040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6105040b5cfbSMartin Varghese bool ethernet) 6106ed246ceeSJohn Hurley { 6107ed246ceeSJohn Hurley int err; 6108ed246ceeSJohn Hurley 6109ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6110dedc5a08SDavide Caratti return 0; 6111ed246ceeSJohn Hurley 6112fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6113ed246ceeSJohn Hurley if (unlikely(err)) 6114ed246ceeSJohn Hurley return err; 6115ed246ceeSJohn Hurley 6116ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6117ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6118fa4e0f88SDavide Caratti mac_len); 6119ed246ceeSJohn Hurley 6120ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6121ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6122fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6123ed246ceeSJohn Hurley 61244296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6125ed246ceeSJohn Hurley struct ethhdr *hdr; 6126ed246ceeSJohn Hurley 6127ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6128ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6129ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6130ed246ceeSJohn Hurley } 6131ed246ceeSJohn Hurley skb->protocol = next_proto; 6132ed246ceeSJohn Hurley 6133ed246ceeSJohn Hurley return 0; 6134ed246ceeSJohn Hurley } 6135ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6136ed246ceeSJohn Hurley 6137ed246ceeSJohn Hurley /** 6138d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6139d27cf5c5SJohn Hurley * 6140d27cf5c5SJohn Hurley * @skb: buffer 6141d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6142d27cf5c5SJohn Hurley * 6143d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6144d27cf5c5SJohn Hurley * 6145d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6146d27cf5c5SJohn Hurley */ 6147d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6148d27cf5c5SJohn Hurley { 6149d27cf5c5SJohn Hurley int err; 6150d27cf5c5SJohn Hurley 6151d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6152d27cf5c5SJohn Hurley return -EINVAL; 6153d27cf5c5SJohn Hurley 6154d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6155d27cf5c5SJohn Hurley if (unlikely(err)) 6156d27cf5c5SJohn Hurley return err; 6157d27cf5c5SJohn Hurley 6158d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6159d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6160d27cf5c5SJohn Hurley 6161d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6162d27cf5c5SJohn Hurley } 6163d27cf5c5SJohn Hurley 6164d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6165d27cf5c5SJohn Hurley 6166d27cf5c5SJohn Hurley return 0; 6167d27cf5c5SJohn Hurley } 6168d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6169d27cf5c5SJohn Hurley 6170d27cf5c5SJohn Hurley /** 61712a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 61722a2ea508SJohn Hurley * 61732a2ea508SJohn Hurley * @skb: buffer 61742a2ea508SJohn Hurley * 61752a2ea508SJohn Hurley * Expects skb->data at mac header. 61762a2ea508SJohn Hurley * 61772a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 61782a2ea508SJohn Hurley */ 61792a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 61802a2ea508SJohn Hurley { 61812a2ea508SJohn Hurley u32 lse; 61822a2ea508SJohn Hurley u8 ttl; 61832a2ea508SJohn Hurley 61842a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 61852a2ea508SJohn Hurley return -EINVAL; 61862a2ea508SJohn Hurley 618713de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 618813de4ed9SDavide Caratti return -ENOMEM; 618913de4ed9SDavide Caratti 61902a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 61912a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 61922a2ea508SJohn Hurley if (!--ttl) 61932a2ea508SJohn Hurley return -EINVAL; 61942a2ea508SJohn Hurley 61952a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 61962a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 61972a2ea508SJohn Hurley 61982a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 61992a2ea508SJohn Hurley } 62002a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 62012a2ea508SJohn Hurley 62022a2ea508SJohn Hurley /** 62032e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 62042e4e4410SEric Dumazet * 6205de3f0d0eSMasanari Iida * @header_len: size of linear part 6206de3f0d0eSMasanari Iida * @data_len: needed length in frags 6207de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6208de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6209de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 62102e4e4410SEric Dumazet * 62112e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 62122e4e4410SEric Dumazet */ 62132e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 62142e4e4410SEric Dumazet unsigned long data_len, 62152e4e4410SEric Dumazet int max_page_order, 62162e4e4410SEric Dumazet int *errcode, 62172e4e4410SEric Dumazet gfp_t gfp_mask) 62182e4e4410SEric Dumazet { 62192e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 62202e4e4410SEric Dumazet unsigned long chunk; 62212e4e4410SEric Dumazet struct sk_buff *skb; 62222e4e4410SEric Dumazet struct page *page; 62232e4e4410SEric Dumazet int i; 62242e4e4410SEric Dumazet 62252e4e4410SEric Dumazet *errcode = -EMSGSIZE; 62262e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 62272e4e4410SEric Dumazet * high order pages... 62282e4e4410SEric Dumazet */ 62292e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 62302e4e4410SEric Dumazet return NULL; 62312e4e4410SEric Dumazet 62322e4e4410SEric Dumazet *errcode = -ENOBUFS; 6233f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 62342e4e4410SEric Dumazet if (!skb) 62352e4e4410SEric Dumazet return NULL; 62362e4e4410SEric Dumazet 62372e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 62382e4e4410SEric Dumazet 62392e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 62402e4e4410SEric Dumazet int order = max_page_order; 62412e4e4410SEric Dumazet 62422e4e4410SEric Dumazet while (order) { 62432e4e4410SEric Dumazet if (npages >= 1 << order) { 6244d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 62452e4e4410SEric Dumazet __GFP_COMP | 6246d14b56f5SMichal Hocko __GFP_NOWARN, 62472e4e4410SEric Dumazet order); 62482e4e4410SEric Dumazet if (page) 62492e4e4410SEric Dumazet goto fill_page; 62502e4e4410SEric Dumazet /* Do not retry other high order allocations */ 62512e4e4410SEric Dumazet order = 1; 62522e4e4410SEric Dumazet max_page_order = 0; 62532e4e4410SEric Dumazet } 62542e4e4410SEric Dumazet order--; 62552e4e4410SEric Dumazet } 62562e4e4410SEric Dumazet page = alloc_page(gfp_mask); 62572e4e4410SEric Dumazet if (!page) 62582e4e4410SEric Dumazet goto failure; 62592e4e4410SEric Dumazet fill_page: 62602e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 62612e4e4410SEric Dumazet PAGE_SIZE << order); 62622e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 62632e4e4410SEric Dumazet data_len -= chunk; 62642e4e4410SEric Dumazet npages -= 1 << order; 62652e4e4410SEric Dumazet } 62662e4e4410SEric Dumazet return skb; 62672e4e4410SEric Dumazet 62682e4e4410SEric Dumazet failure: 62692e4e4410SEric Dumazet kfree_skb(skb); 62702e4e4410SEric Dumazet return NULL; 62712e4e4410SEric Dumazet } 62722e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 62736fa01ccdSSowmini Varadhan 62746fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 62756fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 62766fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 62776fa01ccdSSowmini Varadhan { 62786fa01ccdSSowmini Varadhan int i; 627912d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 62806fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 62816fa01ccdSSowmini Varadhan u8 *data; 62826fa01ccdSSowmini Varadhan 62836fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 62846fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 628512d6c1d3SKees Cook 62865c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 62876fa01ccdSSowmini Varadhan if (!data) 62886fa01ccdSSowmini Varadhan return -ENOMEM; 628912d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 62906fa01ccdSSowmini Varadhan 62916fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 62926fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 62936fa01ccdSSowmini Varadhan skb->len -= off; 62946fa01ccdSSowmini Varadhan 62956fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 62966fa01ccdSSowmini Varadhan skb_shinfo(skb), 62976fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 62986fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 62996fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 63006fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 63016fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6302bf9f1baaSEric Dumazet skb_kfree_head(data, size); 63036fa01ccdSSowmini Varadhan return -ENOMEM; 63046fa01ccdSSowmini Varadhan } 63056fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 63066fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 63076fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 63086fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6309b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 63106fa01ccdSSowmini Varadhan } else { 63116fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 63126fa01ccdSSowmini Varadhan * relocate values 63136fa01ccdSSowmini Varadhan */ 6314b07a2d97SJakub Kicinski skb_free_head(skb, false); 63156fa01ccdSSowmini Varadhan } 63166fa01ccdSSowmini Varadhan 63176fa01ccdSSowmini Varadhan skb->head = data; 63186fa01ccdSSowmini Varadhan skb->data = data; 63196fa01ccdSSowmini Varadhan skb->head_frag = 0; 6320763087daSEric Dumazet skb_set_end_offset(skb, size); 63216fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 63226fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 63236fa01ccdSSowmini Varadhan skb->cloned = 0; 63246fa01ccdSSowmini Varadhan skb->hdr_len = 0; 63256fa01ccdSSowmini Varadhan skb->nohdr = 0; 63266fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 63276fa01ccdSSowmini Varadhan 63286fa01ccdSSowmini Varadhan return 0; 63296fa01ccdSSowmini Varadhan } 63306fa01ccdSSowmini Varadhan 63316fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 63326fa01ccdSSowmini Varadhan 63336fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 63346fa01ccdSSowmini Varadhan * pskb_carve() 63356fa01ccdSSowmini Varadhan */ 63366fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 63376fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 63386fa01ccdSSowmini Varadhan gfp_t gfp_mask) 63396fa01ccdSSowmini Varadhan { 63406fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 63416fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 63426fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 63436fa01ccdSSowmini Varadhan 63446fa01ccdSSowmini Varadhan do { 63456fa01ccdSSowmini Varadhan if (!list) { 63466fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 63476fa01ccdSSowmini Varadhan return -EFAULT; 63486fa01ccdSSowmini Varadhan } 63496fa01ccdSSowmini Varadhan if (list->len <= eat) { 63506fa01ccdSSowmini Varadhan /* Eaten as whole. */ 63516fa01ccdSSowmini Varadhan eat -= list->len; 63526fa01ccdSSowmini Varadhan list = list->next; 63536fa01ccdSSowmini Varadhan insp = list; 63546fa01ccdSSowmini Varadhan } else { 63556fa01ccdSSowmini Varadhan /* Eaten partially. */ 63566fa01ccdSSowmini Varadhan if (skb_shared(list)) { 63576fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 63586fa01ccdSSowmini Varadhan if (!clone) 63596fa01ccdSSowmini Varadhan return -ENOMEM; 63606fa01ccdSSowmini Varadhan insp = list->next; 63616fa01ccdSSowmini Varadhan list = clone; 63626fa01ccdSSowmini Varadhan } else { 63636fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 63646fa01ccdSSowmini Varadhan insp = list; 63656fa01ccdSSowmini Varadhan } 63666fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 63676fa01ccdSSowmini Varadhan kfree_skb(clone); 63686fa01ccdSSowmini Varadhan return -ENOMEM; 63696fa01ccdSSowmini Varadhan } 63706fa01ccdSSowmini Varadhan break; 63716fa01ccdSSowmini Varadhan } 63726fa01ccdSSowmini Varadhan } while (eat); 63736fa01ccdSSowmini Varadhan 63746fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 63756fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 63766fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6377ef527f96SEric Dumazet consume_skb(list); 63786fa01ccdSSowmini Varadhan } 63796fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 63806fa01ccdSSowmini Varadhan if (clone) { 63816fa01ccdSSowmini Varadhan clone->next = list; 63826fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 63836fa01ccdSSowmini Varadhan } 63846fa01ccdSSowmini Varadhan return 0; 63856fa01ccdSSowmini Varadhan } 63866fa01ccdSSowmini Varadhan 63876fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 63886fa01ccdSSowmini Varadhan * non-linear part of skb 63896fa01ccdSSowmini Varadhan */ 63906fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 63916fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 63926fa01ccdSSowmini Varadhan { 63936fa01ccdSSowmini Varadhan int i, k = 0; 639412d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 63956fa01ccdSSowmini Varadhan u8 *data; 63966fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 63976fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 63986fa01ccdSSowmini Varadhan 63996fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64006fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 640112d6c1d3SKees Cook 64025c0e820cSEric Dumazet data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL); 64036fa01ccdSSowmini Varadhan if (!data) 64046fa01ccdSSowmini Varadhan return -ENOMEM; 640512d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64066fa01ccdSSowmini Varadhan 64076fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6408e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 64096fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 6410bf9f1baaSEric Dumazet skb_kfree_head(data, size); 64116fa01ccdSSowmini Varadhan return -ENOMEM; 64126fa01ccdSSowmini Varadhan } 64136fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 64146fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 64156fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 64166fa01ccdSSowmini Varadhan 64176fa01ccdSSowmini Varadhan if (pos + fsize > off) { 64186fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 64196fa01ccdSSowmini Varadhan 64206fa01ccdSSowmini Varadhan if (pos < off) { 64216fa01ccdSSowmini Varadhan /* Split frag. 64226fa01ccdSSowmini Varadhan * We have two variants in this case: 64236fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 64246fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 64256fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 64266fa01ccdSSowmini Varadhan * where splitting is expensive. 64276fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 64286fa01ccdSSowmini Varadhan */ 6429b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 64306fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 64316fa01ccdSSowmini Varadhan } 64326fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64336fa01ccdSSowmini Varadhan k++; 64346fa01ccdSSowmini Varadhan } 64356fa01ccdSSowmini Varadhan pos += fsize; 64366fa01ccdSSowmini Varadhan } 64376fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 64386fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64396fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 64406fa01ccdSSowmini Varadhan 64416fa01ccdSSowmini Varadhan /* split line is in frag list */ 6442eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6443eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6444eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6445eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6446bf9f1baaSEric Dumazet skb_kfree_head(data, size); 6447eabe8618SMiaohe Lin return -ENOMEM; 64486fa01ccdSSowmini Varadhan } 6449b07a2d97SJakub Kicinski skb_release_data(skb, SKB_CONSUMED, false); 64506fa01ccdSSowmini Varadhan 64516fa01ccdSSowmini Varadhan skb->head = data; 64526fa01ccdSSowmini Varadhan skb->head_frag = 0; 64536fa01ccdSSowmini Varadhan skb->data = data; 6454763087daSEric Dumazet skb_set_end_offset(skb, size); 64556fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 64566fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64576fa01ccdSSowmini Varadhan skb->cloned = 0; 64586fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64596fa01ccdSSowmini Varadhan skb->nohdr = 0; 64606fa01ccdSSowmini Varadhan skb->len -= off; 64616fa01ccdSSowmini Varadhan skb->data_len = skb->len; 64626fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64636fa01ccdSSowmini Varadhan return 0; 64646fa01ccdSSowmini Varadhan } 64656fa01ccdSSowmini Varadhan 64666fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 64676fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 64686fa01ccdSSowmini Varadhan { 64696fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 64706fa01ccdSSowmini Varadhan 64716fa01ccdSSowmini Varadhan if (len < headlen) 64726fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 64736fa01ccdSSowmini Varadhan else 64746fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 64756fa01ccdSSowmini Varadhan } 64766fa01ccdSSowmini Varadhan 64776fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 64786fa01ccdSSowmini Varadhan * a new skb 64796fa01ccdSSowmini Varadhan */ 64806fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 64816fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 64826fa01ccdSSowmini Varadhan { 64836fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 64846fa01ccdSSowmini Varadhan 64856fa01ccdSSowmini Varadhan if (!clone) 64866fa01ccdSSowmini Varadhan return NULL; 64876fa01ccdSSowmini Varadhan 64886fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 64896fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 64906fa01ccdSSowmini Varadhan kfree_skb(clone); 64916fa01ccdSSowmini Varadhan return NULL; 64926fa01ccdSSowmini Varadhan } 64936fa01ccdSSowmini Varadhan return clone; 64946fa01ccdSSowmini Varadhan } 64956fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6496c8c8b127SEric Dumazet 6497c8c8b127SEric Dumazet /** 6498c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6499c8c8b127SEric Dumazet * @skb: buffer 6500c8c8b127SEric Dumazet * 6501c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6502c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6503c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6504c8c8b127SEric Dumazet * truesize. 6505c8c8b127SEric Dumazet * Notes: 6506c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6507c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6508c8c8b127SEric Dumazet */ 6509c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6510c8c8b127SEric Dumazet { 65113174fed9SEric Dumazet if (skb->data_len) { 65123174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6513c8c8b127SEric Dumazet skb_cloned(skb)) 6514c8c8b127SEric Dumazet return; 6515c8c8b127SEric Dumazet 6516c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6517c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 65183174fed9SEric Dumazet } 65193174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 65203174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 65213174fed9SEric Dumazet * their truesize. 65223174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 65233174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 65243174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6525c8c8b127SEric Dumazet */ 6526c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6527c8c8b127SEric Dumazet } 6528d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6529df5042f4SFlorian Westphal 6530df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6531df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6532df5042f4SFlorian Westphal { 6533df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6534df5042f4SFlorian Westphal } 6535df5042f4SFlorian Westphal 65368b69a803SPaolo Abeni /** 65378b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 65388b69a803SPaolo Abeni * 65394930f483SFlorian Westphal * @flags: See kmalloc(). 65404930f483SFlorian Westphal * 65418b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 65428b69a803SPaolo Abeni * skb via __skb_ext_set(). 65438b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 65448b69a803SPaolo Abeni */ 65454930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6546df5042f4SFlorian Westphal { 65474930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6548df5042f4SFlorian Westphal 6549df5042f4SFlorian Westphal if (new) { 6550df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6551df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6552df5042f4SFlorian Westphal } 6553df5042f4SFlorian Westphal 6554df5042f4SFlorian Westphal return new; 6555df5042f4SFlorian Westphal } 6556df5042f4SFlorian Westphal 65574165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 65584165079bSFlorian Westphal unsigned int old_active) 6559df5042f4SFlorian Westphal { 6560df5042f4SFlorian Westphal struct skb_ext *new; 6561df5042f4SFlorian Westphal 6562df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6563df5042f4SFlorian Westphal return old; 6564df5042f4SFlorian Westphal 6565df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6566df5042f4SFlorian Westphal if (!new) 6567df5042f4SFlorian Westphal return NULL; 6568df5042f4SFlorian Westphal 6569df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6570df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6571df5042f4SFlorian Westphal 65724165079bSFlorian Westphal #ifdef CONFIG_XFRM 65734165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 65744165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 65754165079bSFlorian Westphal unsigned int i; 65764165079bSFlorian Westphal 65774165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 65784165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 65794165079bSFlorian Westphal } 65804165079bSFlorian Westphal #endif 6581df5042f4SFlorian Westphal __skb_ext_put(old); 6582df5042f4SFlorian Westphal return new; 6583df5042f4SFlorian Westphal } 6584df5042f4SFlorian Westphal 6585df5042f4SFlorian Westphal /** 65868b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 65878b69a803SPaolo Abeni * @skb: buffer 65888b69a803SPaolo Abeni * @id: extension id 65898b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 65908b69a803SPaolo Abeni * 65918b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 65928b69a803SPaolo Abeni * 65938b69a803SPaolo Abeni * Returns the pointer to the extension. 65948b69a803SPaolo Abeni */ 65958b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 65968b69a803SPaolo Abeni struct skb_ext *ext) 65978b69a803SPaolo Abeni { 65988b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 65998b69a803SPaolo Abeni 66008b69a803SPaolo Abeni skb_ext_put(skb); 66018b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 66028b69a803SPaolo Abeni ext->chunks = newlen; 66038b69a803SPaolo Abeni ext->offset[id] = newoff; 66048b69a803SPaolo Abeni skb->extensions = ext; 66058b69a803SPaolo Abeni skb->active_extensions = 1 << id; 66068b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 66078b69a803SPaolo Abeni } 66088b69a803SPaolo Abeni 66098b69a803SPaolo Abeni /** 6610df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6611df5042f4SFlorian Westphal * @skb: buffer 6612df5042f4SFlorian Westphal * @id: extension to allocate space for 6613df5042f4SFlorian Westphal * 6614df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6615df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6616df5042f4SFlorian Westphal * is returned. 6617df5042f4SFlorian Westphal * 6618df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6619df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6620df5042f4SFlorian Westphal * 6621df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6622df5042f4SFlorian Westphal */ 6623df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6624df5042f4SFlorian Westphal { 6625df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6626df5042f4SFlorian Westphal unsigned int newlen, newoff; 6627df5042f4SFlorian Westphal 6628df5042f4SFlorian Westphal if (skb->active_extensions) { 6629df5042f4SFlorian Westphal old = skb->extensions; 6630df5042f4SFlorian Westphal 66314165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6632df5042f4SFlorian Westphal if (!new) 6633df5042f4SFlorian Westphal return NULL; 6634df5042f4SFlorian Westphal 6635682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6636df5042f4SFlorian Westphal goto set_active; 6637df5042f4SFlorian Westphal 6638e94e50bdSPaolo Abeni newoff = new->chunks; 6639df5042f4SFlorian Westphal } else { 6640df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6641df5042f4SFlorian Westphal 66424930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6643df5042f4SFlorian Westphal if (!new) 6644df5042f4SFlorian Westphal return NULL; 6645df5042f4SFlorian Westphal } 6646df5042f4SFlorian Westphal 6647df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6648df5042f4SFlorian Westphal new->chunks = newlen; 6649df5042f4SFlorian Westphal new->offset[id] = newoff; 6650df5042f4SFlorian Westphal set_active: 6651b0999f38SPaolo Abeni skb->slow_gro = 1; 6652682ec859SPaolo Abeni skb->extensions = new; 6653df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6654df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6655df5042f4SFlorian Westphal } 6656df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6657df5042f4SFlorian Westphal 66584165079bSFlorian Westphal #ifdef CONFIG_XFRM 66594165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 66604165079bSFlorian Westphal { 66614165079bSFlorian Westphal unsigned int i; 66624165079bSFlorian Westphal 66634165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 66644165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 66654165079bSFlorian Westphal } 66664165079bSFlorian Westphal #endif 66674165079bSFlorian Westphal 666878476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 666978476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 667078476d31SJeremy Kerr { 667178476d31SJeremy Kerr if (flow->key) 667278476d31SJeremy Kerr mctp_key_unref(flow->key); 667378476d31SJeremy Kerr } 667478476d31SJeremy Kerr #endif 667578476d31SJeremy Kerr 6676df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6677df5042f4SFlorian Westphal { 6678df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6679df5042f4SFlorian Westphal 6680df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6681df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6682df5042f4SFlorian Westphal skb->extensions = NULL; 6683df5042f4SFlorian Westphal __skb_ext_put(ext); 66844165079bSFlorian Westphal #ifdef CONFIG_XFRM 66854165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 66864165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 66874165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 66884165079bSFlorian Westphal 66894165079bSFlorian Westphal skb_ext_put_sp(sp); 66904165079bSFlorian Westphal sp->len = 0; 66914165079bSFlorian Westphal #endif 6692df5042f4SFlorian Westphal } 6693df5042f4SFlorian Westphal } 6694df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6695df5042f4SFlorian Westphal 6696df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6697df5042f4SFlorian Westphal { 6698df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6699df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6700df5042f4SFlorian Westphal */ 6701df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6702df5042f4SFlorian Westphal goto free_now; 6703df5042f4SFlorian Westphal 6704df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6705df5042f4SFlorian Westphal return; 6706df5042f4SFlorian Westphal free_now: 67074165079bSFlorian Westphal #ifdef CONFIG_XFRM 67084165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 67094165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 67104165079bSFlorian Westphal #endif 671178476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 671278476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 671378476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 671478476d31SJeremy Kerr #endif 67154165079bSFlorian Westphal 6716df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6717df5042f4SFlorian Westphal } 6718df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6719df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 672068822bdfSEric Dumazet 672168822bdfSEric Dumazet /** 672268822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 672368822bdfSEric Dumazet * @skb: buffer 672468822bdfSEric Dumazet * 672568822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 672668822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 672768822bdfSEric Dumazet * and memory zone spinlock contention. 672868822bdfSEric Dumazet */ 672968822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 673068822bdfSEric Dumazet { 673168822bdfSEric Dumazet int cpu = skb->alloc_cpu; 673268822bdfSEric Dumazet struct softnet_data *sd; 673339564c3fSEric Dumazet unsigned int defer_max; 673468822bdfSEric Dumazet bool kick; 673568822bdfSEric Dumazet 673668822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 673768822bdfSEric Dumazet !cpu_online(cpu) || 673868822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 673939564c3fSEric Dumazet nodefer: __kfree_skb(skb); 674068822bdfSEric Dumazet return; 674168822bdfSEric Dumazet } 674268822bdfSEric Dumazet 6743e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb_dst(skb)); 6744e8e1ce84SEric Dumazet DEBUG_NET_WARN_ON_ONCE(skb->destructor); 6745e8e1ce84SEric Dumazet 674668822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 674739564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 674839564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 674939564c3fSEric Dumazet goto nodefer; 675039564c3fSEric Dumazet 6751931e93bdSEric Dumazet spin_lock_bh(&sd->defer_lock); 6752c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6753c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6754c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6755c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6756c09b0cd2SJakub Kicinski 675768822bdfSEric Dumazet skb->next = sd->defer_list; 675868822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 675968822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 6760931e93bdSEric Dumazet spin_unlock_bh(&sd->defer_lock); 676168822bdfSEric Dumazet 676268822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 676368822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 676468822bdfSEric Dumazet */ 676597e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 676668822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 676768822bdfSEric Dumazet } 67682e910b95SDavid Howells 67692e910b95SDavid Howells static void skb_splice_csum_page(struct sk_buff *skb, struct page *page, 67702e910b95SDavid Howells size_t offset, size_t len) 67712e910b95SDavid Howells { 67722e910b95SDavid Howells const char *kaddr; 67732e910b95SDavid Howells __wsum csum; 67742e910b95SDavid Howells 67752e910b95SDavid Howells kaddr = kmap_local_page(page); 67762e910b95SDavid Howells csum = csum_partial(kaddr + offset, len, 0); 67772e910b95SDavid Howells kunmap_local(kaddr); 67782e910b95SDavid Howells skb->csum = csum_block_add(skb->csum, csum, skb->len); 67792e910b95SDavid Howells } 67802e910b95SDavid Howells 67812e910b95SDavid Howells /** 67822e910b95SDavid Howells * skb_splice_from_iter - Splice (or copy) pages to skbuff 67832e910b95SDavid Howells * @skb: The buffer to add pages to 67842e910b95SDavid Howells * @iter: Iterator representing the pages to be added 67852e910b95SDavid Howells * @maxsize: Maximum amount of pages to be added 67862e910b95SDavid Howells * @gfp: Allocation flags 67872e910b95SDavid Howells * 67882e910b95SDavid Howells * This is a common helper function for supporting MSG_SPLICE_PAGES. It 67892e910b95SDavid Howells * extracts pages from an iterator and adds them to the socket buffer if 67902e910b95SDavid Howells * possible, copying them to fragments if not possible (such as if they're slab 67912e910b95SDavid Howells * pages). 67922e910b95SDavid Howells * 67932e910b95SDavid Howells * Returns the amount of data spliced/copied or -EMSGSIZE if there's 67942e910b95SDavid Howells * insufficient space in the buffer to transfer anything. 67952e910b95SDavid Howells */ 67962e910b95SDavid Howells ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter, 67972e910b95SDavid Howells ssize_t maxsize, gfp_t gfp) 67982e910b95SDavid Howells { 67992e910b95SDavid Howells size_t frag_limit = READ_ONCE(sysctl_max_skb_frags); 68002e910b95SDavid Howells struct page *pages[8], **ppages = pages; 68012e910b95SDavid Howells ssize_t spliced = 0, ret = 0; 68022e910b95SDavid Howells unsigned int i; 68032e910b95SDavid Howells 68042e910b95SDavid Howells while (iter->count > 0) { 6805ef1bc119SDan Carpenter ssize_t space, nr, len; 6806ef1bc119SDan Carpenter size_t off; 68072e910b95SDavid Howells 68082e910b95SDavid Howells ret = -EMSGSIZE; 68092e910b95SDavid Howells space = frag_limit - skb_shinfo(skb)->nr_frags; 68102e910b95SDavid Howells if (space < 0) 68112e910b95SDavid Howells break; 68122e910b95SDavid Howells 68132e910b95SDavid Howells /* We might be able to coalesce without increasing nr_frags */ 68142e910b95SDavid Howells nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages)); 68152e910b95SDavid Howells 68162e910b95SDavid Howells len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); 68172e910b95SDavid Howells if (len <= 0) { 68182e910b95SDavid Howells ret = len ?: -EIO; 68192e910b95SDavid Howells break; 68202e910b95SDavid Howells } 68212e910b95SDavid Howells 68222e910b95SDavid Howells i = 0; 68232e910b95SDavid Howells do { 68242e910b95SDavid Howells struct page *page = pages[i++]; 68252e910b95SDavid Howells size_t part = min_t(size_t, PAGE_SIZE - off, len); 68262e910b95SDavid Howells 68272e910b95SDavid Howells ret = -EIO; 68282e910b95SDavid Howells if (WARN_ON_ONCE(!sendpage_ok(page))) 68292e910b95SDavid Howells goto out; 68302e910b95SDavid Howells 68312e910b95SDavid Howells ret = skb_append_pagefrags(skb, page, off, part, 68322e910b95SDavid Howells frag_limit); 68332e910b95SDavid Howells if (ret < 0) { 68342e910b95SDavid Howells iov_iter_revert(iter, len); 68352e910b95SDavid Howells goto out; 68362e910b95SDavid Howells } 68372e910b95SDavid Howells 68382e910b95SDavid Howells if (skb->ip_summed == CHECKSUM_NONE) 68392e910b95SDavid Howells skb_splice_csum_page(skb, page, off, part); 68402e910b95SDavid Howells 68412e910b95SDavid Howells off = 0; 68422e910b95SDavid Howells spliced += part; 68432e910b95SDavid Howells maxsize -= part; 68442e910b95SDavid Howells len -= part; 68452e910b95SDavid Howells } while (len > 0); 68462e910b95SDavid Howells 68472e910b95SDavid Howells if (maxsize <= 0) 68482e910b95SDavid Howells break; 68492e910b95SDavid Howells } 68502e910b95SDavid Howells 68512e910b95SDavid Howells out: 68522e910b95SDavid Howells skb_len_add(skb, spliced); 68532e910b95SDavid Howells return spliced ?: ret; 68542e910b95SDavid Howells } 68552e910b95SDavid Howells EXPORT_SYMBOL(skb_splice_from_iter); 6856