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> 610d5501c1SVlad Yasevich #include <linux/if_vlan.h> 622a2ea508SJohn Hurley #include <linux/mpls.h> 63183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <net/protocol.h> 661da177e4SLinus Torvalds #include <net/dst.h> 671da177e4SLinus Torvalds #include <net/sock.h> 681da177e4SLinus Torvalds #include <net/checksum.h> 69ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 701da177e4SLinus Torvalds #include <net/xfrm.h> 718822e270SJohn Hurley #include <net/mpls.h> 723ee17bc7SMat Martineau #include <net/mptcp.h> 7378476d31SJeremy Kerr #include <net/mctp.h> 746a5bcd84SIlias Apalodimas #include <net/page_pool.h> 751da177e4SLinus Torvalds 767c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 77ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7851c56b00SEric Dumazet #include <linux/highmem.h> 79b245be1fSWillem de Bruijn #include <linux/capability.h> 80b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 812544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 82a1f8e7f7SAl Viro 8339564c3fSEric Dumazet #include "dev.h" 847f678defSVasily Averin #include "sock_destructor.h" 857b7ed885SBart Van Assche 8608009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8708009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 88df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 89df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 90df5042f4SFlorian Westphal #endif 915f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 925f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 931da177e4SLinus Torvalds 949cb252c4SMenglong Dong #undef FN 959cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 969cb252c4SMenglong Dong const char * const drop_reasons[] = { 970e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 989cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 999cb252c4SMenglong Dong }; 100ec43908dSMenglong Dong EXPORT_SYMBOL(drop_reasons); 101ec43908dSMenglong Dong 1021da177e4SLinus Torvalds /** 103f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1041da177e4SLinus Torvalds * @skb: buffer 1051da177e4SLinus Torvalds * @sz: size 106f05de73bSJean Sacren * @addr: address 10799d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1081da177e4SLinus Torvalds * 109f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 110f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 111f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 112f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1131da177e4SLinus Torvalds */ 114f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 11599d5851eSJames Hogan const char msg[]) 1161da177e4SLinus Torvalds { 11741a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 11899d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1194305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12026095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1211da177e4SLinus Torvalds BUG(); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 124f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1251da177e4SLinus Torvalds { 126f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 129f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 130f05de73bSJean Sacren { 131f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 132f05de73bSJean Sacren } 133c93bdd0eSMel Gorman 13450fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 135f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 136f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 13750fad4b5SAlexander Lobakin 138dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 139dbae2b06SPaolo Abeni 140dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 141dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 142dbae2b06SPaolo Abeni 143dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 144dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 145dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 146dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 147dbae2b06SPaolo Abeni */ 148dbae2b06SPaolo Abeni 149dbae2b06SPaolo Abeni struct page_frag_1k { 150dbae2b06SPaolo Abeni void *va; 151dbae2b06SPaolo Abeni u16 offset; 152dbae2b06SPaolo Abeni bool pfmemalloc; 153dbae2b06SPaolo Abeni }; 154dbae2b06SPaolo Abeni 155dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 156dbae2b06SPaolo Abeni { 157dbae2b06SPaolo Abeni struct page *page; 158dbae2b06SPaolo Abeni int offset; 159dbae2b06SPaolo Abeni 160dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 161dbae2b06SPaolo Abeni if (likely(offset >= 0)) 162dbae2b06SPaolo Abeni goto use_frag; 163dbae2b06SPaolo Abeni 164dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 165dbae2b06SPaolo Abeni if (!page) 166dbae2b06SPaolo Abeni return NULL; 167dbae2b06SPaolo Abeni 168dbae2b06SPaolo Abeni nc->va = page_address(page); 169dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 170dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 171dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 172dbae2b06SPaolo Abeni 173dbae2b06SPaolo Abeni use_frag: 174dbae2b06SPaolo Abeni nc->offset = offset; 175dbae2b06SPaolo Abeni return nc->va + offset; 176dbae2b06SPaolo Abeni } 177dbae2b06SPaolo Abeni #else 178dbae2b06SPaolo Abeni 179dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 180dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 181dbae2b06SPaolo Abeni */ 182dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 183dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 184dbae2b06SPaolo Abeni 185dbae2b06SPaolo Abeni struct page_frag_1k { 186dbae2b06SPaolo Abeni }; 187dbae2b06SPaolo Abeni 188dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 189dbae2b06SPaolo Abeni { 190dbae2b06SPaolo Abeni return NULL; 191dbae2b06SPaolo Abeni } 192dbae2b06SPaolo Abeni 193dbae2b06SPaolo Abeni #endif 194dbae2b06SPaolo Abeni 19550fad4b5SAlexander Lobakin struct napi_alloc_cache { 19650fad4b5SAlexander Lobakin struct page_frag_cache page; 197dbae2b06SPaolo Abeni struct page_frag_1k page_small; 19850fad4b5SAlexander Lobakin unsigned int skb_count; 19950fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 20050fad4b5SAlexander Lobakin }; 20150fad4b5SAlexander Lobakin 20250fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 20350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 20450fad4b5SAlexander Lobakin 205dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 206dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 207dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 208dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 209dbae2b06SPaolo Abeni * does not accidentally come back. 210dbae2b06SPaolo Abeni */ 211dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 212dbae2b06SPaolo Abeni { 213dbae2b06SPaolo Abeni struct sk_buff *skb; 214dbae2b06SPaolo Abeni 215dbae2b06SPaolo Abeni local_bh_disable(); 216dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 217dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 218dbae2b06SPaolo Abeni napi_free_frags(napi); 219dbae2b06SPaolo Abeni local_bh_enable(); 220dbae2b06SPaolo Abeni } 221dbae2b06SPaolo Abeni 22232e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 22350fad4b5SAlexander Lobakin { 22450fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 22550fad4b5SAlexander Lobakin 22650fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 22750fad4b5SAlexander Lobakin 22832e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 22950fad4b5SAlexander Lobakin } 23050fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 23150fad4b5SAlexander Lobakin 23250fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 23350fad4b5SAlexander Lobakin { 23450fad4b5SAlexander Lobakin void *data; 23550fad4b5SAlexander Lobakin 23650fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 237afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 23832e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 23932e3573fSYajun Deng 24050fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 24150fad4b5SAlexander Lobakin } else { 24232e3573fSYajun Deng struct napi_alloc_cache *nc; 24332e3573fSYajun Deng 24450fad4b5SAlexander Lobakin local_bh_disable(); 24532e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 24632e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 24750fad4b5SAlexander Lobakin local_bh_enable(); 24850fad4b5SAlexander Lobakin } 24950fad4b5SAlexander Lobakin return data; 25050fad4b5SAlexander Lobakin } 25150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 25250fad4b5SAlexander Lobakin 253f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 254f450d539SAlexander Lobakin { 255f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 256f450d539SAlexander Lobakin struct sk_buff *skb; 257f450d539SAlexander Lobakin 25849ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 259f450d539SAlexander Lobakin nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 260f450d539SAlexander Lobakin GFP_ATOMIC, 261f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 262f450d539SAlexander Lobakin nc->skb_cache); 263f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 264f450d539SAlexander Lobakin return NULL; 26549ae83fcSSieng Piaw Liew } 266f450d539SAlexander Lobakin 267f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 268f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, skb); 269f450d539SAlexander Lobakin 270f450d539SAlexander Lobakin return skb; 271f450d539SAlexander Lobakin } 272f450d539SAlexander Lobakin 273ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 274ce098da1SKees Cook unsigned int size) 275ba0509b6SJesper Dangaard Brouer { 276ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 277ba0509b6SJesper Dangaard Brouer 278ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 279ba0509b6SJesper Dangaard Brouer 280ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 281ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 282ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 283ba0509b6SJesper Dangaard Brouer skb->head = data; 284ba0509b6SJesper Dangaard Brouer skb->data = data; 285ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 286763087daSEric Dumazet skb_set_end_offset(skb, size); 287ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 288ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 28968822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 290ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 291ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 292ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 293ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 294ba0509b6SJesper Dangaard Brouer 2956370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 296ba0509b6SJesper Dangaard Brouer } 297ba0509b6SJesper Dangaard Brouer 298ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 299ce098da1SKees Cook unsigned int *size) 300ce098da1SKees Cook { 301ce098da1SKees Cook void *resized; 302ce098da1SKees Cook 303ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */ 304ce098da1SKees Cook *size = ksize(data); 305ce098da1SKees Cook /* krealloc() will immediately return "data" when 306ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper 307ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note 308ce098da1SKees Cook * that this "new" pointer needs to be passed back to the 309ce098da1SKees Cook * caller for use so the __alloc_size hinting will be 310ce098da1SKees Cook * tracked correctly. 311ce098da1SKees Cook */ 312ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC); 313ce098da1SKees Cook WARN_ON_ONCE(resized != data); 314ce098da1SKees Cook return resized; 315ce098da1SKees Cook } 316ce098da1SKees Cook 317ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers. 318ce098da1SKees Cook * Note that this should be used sparingly as slab buffers 319ce098da1SKees Cook * cannot be combined efficiently by GRO! 320ce098da1SKees Cook */ 321ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data) 322ce098da1SKees Cook { 323ce098da1SKees Cook struct sk_buff *skb; 324ce098da1SKees Cook unsigned int size; 325ce098da1SKees Cook 326ce098da1SKees Cook skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 327ce098da1SKees Cook if (unlikely(!skb)) 328ce098da1SKees Cook return NULL; 329ce098da1SKees Cook 330ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail)); 331ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 332ce098da1SKees Cook __finalize_skb_around(skb, data, size); 333ce098da1SKees Cook 334ce098da1SKees Cook return skb; 335ce098da1SKees Cook } 336ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb); 337ce098da1SKees Cook 338ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */ 339ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data, 340ce098da1SKees Cook unsigned int frag_size) 341ce098da1SKees Cook { 342ce098da1SKees Cook unsigned int size = frag_size; 343ce098da1SKees Cook 344ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers 345ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead. 346ce098da1SKees Cook */ 347ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 348ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 349ce098da1SKees Cook 350ce098da1SKees Cook __finalize_skb_around(skb, data, size); 351ce098da1SKees Cook } 352ce098da1SKees Cook 3531da177e4SLinus Torvalds /** 3542ea2f62cSEric Dumazet * __build_skb - build a network buffer 355b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 356ce098da1SKees Cook * @frag_size: size of data (must not be 0) 357b2b5ce9dSEric Dumazet * 358b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 359ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page 360ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 361ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb() 362ce098da1SKees Cook * instead.) 363b2b5ce9dSEric Dumazet * The return is the new skb buffer. 364b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 365b2b5ce9dSEric Dumazet * Notes : 366b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 367b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 368b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 369b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 370b2b5ce9dSEric Dumazet * before giving packet to stack. 371b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 372b2b5ce9dSEric Dumazet */ 3732ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 374b2b5ce9dSEric Dumazet { 375b2b5ce9dSEric Dumazet struct sk_buff *skb; 376b2b5ce9dSEric Dumazet 377b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 378ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 379b2b5ce9dSEric Dumazet return NULL; 380b2b5ce9dSEric Dumazet 381b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 382483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 383b2b5ce9dSEric Dumazet 384483126b3SAlexander Lobakin return skb; 385b2b5ce9dSEric Dumazet } 3862ea2f62cSEric Dumazet 3872ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3882ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3892ea2f62cSEric Dumazet */ 3902ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3912ea2f62cSEric Dumazet { 3922ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3932ea2f62cSEric Dumazet 3942ea2f62cSEric Dumazet if (skb && frag_size) { 3952ea2f62cSEric Dumazet skb->head_frag = 1; 3962f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3972ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3982ea2f62cSEric Dumazet } 3992ea2f62cSEric Dumazet return skb; 4002ea2f62cSEric Dumazet } 401b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 402b2b5ce9dSEric Dumazet 403ba0509b6SJesper Dangaard Brouer /** 404ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 405ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 406ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 40712c1604aSJakub Kicinski * @frag_size: size of data 408ba0509b6SJesper Dangaard Brouer */ 409ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 410ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 411ba0509b6SJesper Dangaard Brouer { 412ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 413ba0509b6SJesper Dangaard Brouer return NULL; 414ba0509b6SJesper Dangaard Brouer 415483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 416ba0509b6SJesper Dangaard Brouer 417483126b3SAlexander Lobakin if (frag_size) { 418ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 419ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 420ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 421ba0509b6SJesper Dangaard Brouer } 422ba0509b6SJesper Dangaard Brouer return skb; 423ba0509b6SJesper Dangaard Brouer } 424ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 425ba0509b6SJesper Dangaard Brouer 426f450d539SAlexander Lobakin /** 427f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 428f450d539SAlexander Lobakin * @data: data buffer provided by caller 42912c1604aSJakub Kicinski * @frag_size: size of data 430f450d539SAlexander Lobakin * 431f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 432f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 433f450d539SAlexander Lobakin * 434f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 435f450d539SAlexander Lobakin */ 436f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 437f450d539SAlexander Lobakin { 438f450d539SAlexander Lobakin struct sk_buff *skb; 439f450d539SAlexander Lobakin 440f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 441f450d539SAlexander Lobakin if (unlikely(!skb)) 442f450d539SAlexander Lobakin return NULL; 443f450d539SAlexander Lobakin 444f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 445f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 446f450d539SAlexander Lobakin 447f450d539SAlexander Lobakin return skb; 448f450d539SAlexander Lobakin } 449f450d539SAlexander Lobakin 450f450d539SAlexander Lobakin /** 451f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 452f450d539SAlexander Lobakin * @data: data buffer provided by caller 45312c1604aSJakub Kicinski * @frag_size: size of data 454f450d539SAlexander Lobakin * 455f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 456f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 457f450d539SAlexander Lobakin * 458f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 459f450d539SAlexander Lobakin */ 460f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 461f450d539SAlexander Lobakin { 462f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 463f450d539SAlexander Lobakin 464f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 465f450d539SAlexander Lobakin skb->head_frag = 1; 466f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 467f450d539SAlexander Lobakin } 468f450d539SAlexander Lobakin 469f450d539SAlexander Lobakin return skb; 470f450d539SAlexander Lobakin } 471f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 472f450d539SAlexander Lobakin 4735381b23dSAlexander Lobakin /* 4745381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 4755381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 4765381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 4775381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 4785381b23dSAlexander Lobakin * memory is free 4795381b23dSAlexander Lobakin */ 480ef28095fSAlexander Lobakin static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 481ef28095fSAlexander Lobakin bool *pfmemalloc) 4825381b23dSAlexander Lobakin { 4835381b23dSAlexander Lobakin void *obj; 4845381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 4855381b23dSAlexander Lobakin 4865381b23dSAlexander Lobakin /* 4875381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 4885381b23dSAlexander Lobakin * to the reserves, fail. 4895381b23dSAlexander Lobakin */ 4905381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, 4915381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 4925381b23dSAlexander Lobakin node); 4935381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 4945381b23dSAlexander Lobakin goto out; 4955381b23dSAlexander Lobakin 4965381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 4975381b23dSAlexander Lobakin ret_pfmemalloc = true; 4985381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, flags, node); 4995381b23dSAlexander Lobakin 5005381b23dSAlexander Lobakin out: 5015381b23dSAlexander Lobakin if (pfmemalloc) 5025381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5035381b23dSAlexander Lobakin 5045381b23dSAlexander Lobakin return obj; 5055381b23dSAlexander Lobakin } 5065381b23dSAlexander Lobakin 5075381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 5085381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 5095381b23dSAlexander Lobakin * [BEEP] leaks. 5105381b23dSAlexander Lobakin * 5115381b23dSAlexander Lobakin */ 5125381b23dSAlexander Lobakin 5135381b23dSAlexander Lobakin /** 5145381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 5155381b23dSAlexander Lobakin * @size: size to allocate 5165381b23dSAlexander Lobakin * @gfp_mask: allocation mask 5175381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 5185381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 5195381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 5205381b23dSAlexander Lobakin * allocations in case the data is required for writeback 5215381b23dSAlexander Lobakin * @node: numa node to allocate memory on 5225381b23dSAlexander Lobakin * 5235381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 5245381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 5255381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 5265381b23dSAlexander Lobakin * 5275381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 5285381b23dSAlexander Lobakin * %GFP_ATOMIC. 5295381b23dSAlexander Lobakin */ 5305381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 5315381b23dSAlexander Lobakin int flags, int node) 5325381b23dSAlexander Lobakin { 5335381b23dSAlexander Lobakin struct kmem_cache *cache; 5345381b23dSAlexander Lobakin struct sk_buff *skb; 535a5df6333SLi RongQing unsigned int osize; 5365381b23dSAlexander Lobakin bool pfmemalloc; 537a5df6333SLi RongQing u8 *data; 5385381b23dSAlexander Lobakin 5395381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 5405381b23dSAlexander Lobakin ? skbuff_fclone_cache : skbuff_head_cache; 5415381b23dSAlexander Lobakin 5425381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 5435381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 5445381b23dSAlexander Lobakin 5455381b23dSAlexander Lobakin /* Get the HEAD */ 546d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 547d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 548d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 549d13612b5SAlexander Lobakin else 550d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 551df1ae022SAlexander Lobakin if (unlikely(!skb)) 552df1ae022SAlexander Lobakin return NULL; 5535381b23dSAlexander Lobakin prefetchw(skb); 5545381b23dSAlexander Lobakin 5555381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 5565381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 5575381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 5585381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 5595381b23dSAlexander Lobakin */ 5605381b23dSAlexander Lobakin size = SKB_DATA_ALIGN(size); 5615381b23dSAlexander Lobakin size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 56212d6c1d3SKees Cook osize = kmalloc_size_roundup(size); 56312d6c1d3SKees Cook data = kmalloc_reserve(osize, gfp_mask, node, &pfmemalloc); 564df1ae022SAlexander Lobakin if (unlikely(!data)) 5655381b23dSAlexander Lobakin goto nodata; 56612d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 5675381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 5685381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 5695381b23dSAlexander Lobakin */ 570a5df6333SLi RongQing size = SKB_WITH_OVERHEAD(osize); 5715381b23dSAlexander Lobakin prefetchw(data + size); 5725381b23dSAlexander Lobakin 5735381b23dSAlexander Lobakin /* 5745381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 5755381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 5765381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 5775381b23dSAlexander Lobakin */ 5785381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 579a5df6333SLi RongQing __build_skb_around(skb, data, osize); 5805381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 5815381b23dSAlexander Lobakin 5825381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 5835381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 5845381b23dSAlexander Lobakin 5855381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 5865381b23dSAlexander Lobakin 5875381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 5885381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 5895381b23dSAlexander Lobakin } 5905381b23dSAlexander Lobakin 5915381b23dSAlexander Lobakin return skb; 592df1ae022SAlexander Lobakin 5935381b23dSAlexander Lobakin nodata: 5945381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 595df1ae022SAlexander Lobakin return NULL; 5965381b23dSAlexander Lobakin } 5975381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 5985381b23dSAlexander Lobakin 5997ba7aeabSSebastian Andrzej Siewior /** 600fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 601fd11a83dSAlexander Duyck * @dev: network device to receive on 602d7499160SMasanari Iida * @len: length to allocate 603fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 604fd11a83dSAlexander Duyck * 605fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 606fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 607fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 608fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 609fd11a83dSAlexander Duyck * 610fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 611fd11a83dSAlexander Duyck */ 6129451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 6139451980aSAlexander Duyck gfp_t gfp_mask) 614fd11a83dSAlexander Duyck { 615b63ae8caSAlexander Duyck struct page_frag_cache *nc; 616fd11a83dSAlexander Duyck struct sk_buff *skb; 6179451980aSAlexander Duyck bool pfmemalloc; 6189451980aSAlexander Duyck void *data; 619fd11a83dSAlexander Duyck 6209451980aSAlexander Duyck len += NET_SKB_PAD; 621fd11a83dSAlexander Duyck 62266c55602SAlexander Lobakin /* If requested length is either too small or too big, 62366c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 62466c55602SAlexander Lobakin */ 62566c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 62666c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 627d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 628a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 629a080e7bdSAlexander Duyck if (!skb) 630a080e7bdSAlexander Duyck goto skb_fail; 631a080e7bdSAlexander Duyck goto skb_success; 632a080e7bdSAlexander Duyck } 6339451980aSAlexander Duyck 6349451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 6359451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 6369451980aSAlexander Duyck 6379451980aSAlexander Duyck if (sk_memalloc_socks()) 6389451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 6399451980aSAlexander Duyck 640afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 6419451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 6428c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 6439451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 64492dcabd7SSebastian Andrzej Siewior } else { 64592dcabd7SSebastian Andrzej Siewior local_bh_disable(); 64692dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 64792dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 64892dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 64992dcabd7SSebastian Andrzej Siewior local_bh_enable(); 65092dcabd7SSebastian Andrzej Siewior } 6519451980aSAlexander Duyck 6529451980aSAlexander Duyck if (unlikely(!data)) 6539451980aSAlexander Duyck return NULL; 6549451980aSAlexander Duyck 6559451980aSAlexander Duyck skb = __build_skb(data, len); 6569451980aSAlexander Duyck if (unlikely(!skb)) { 657181edb2bSAlexander Duyck skb_free_frag(data); 6589451980aSAlexander Duyck return NULL; 6599451980aSAlexander Duyck } 6609451980aSAlexander Duyck 6619451980aSAlexander Duyck if (pfmemalloc) 6629451980aSAlexander Duyck skb->pfmemalloc = 1; 6639451980aSAlexander Duyck skb->head_frag = 1; 6649451980aSAlexander Duyck 665a080e7bdSAlexander Duyck skb_success: 6668af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 6677b2e497aSChristoph Hellwig skb->dev = dev; 668fd11a83dSAlexander Duyck 669a080e7bdSAlexander Duyck skb_fail: 6708af27456SChristoph Hellwig return skb; 6718af27456SChristoph Hellwig } 672b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 6731da177e4SLinus Torvalds 674fd11a83dSAlexander Duyck /** 675fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 676fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 677d7499160SMasanari Iida * @len: length to allocate 678fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 679fd11a83dSAlexander Duyck * 680fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 681fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 682fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 683fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 684fd11a83dSAlexander Duyck * 685fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 686fd11a83dSAlexander Duyck */ 6879451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 6889451980aSAlexander Duyck gfp_t gfp_mask) 689fd11a83dSAlexander Duyck { 6903226b158SEric Dumazet struct napi_alloc_cache *nc; 691fd11a83dSAlexander Duyck struct sk_buff *skb; 692dbae2b06SPaolo Abeni bool pfmemalloc; 6939451980aSAlexander Duyck void *data; 694fd11a83dSAlexander Duyck 695ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 6969451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 697fd11a83dSAlexander Duyck 6983226b158SEric Dumazet /* If requested length is either too small or too big, 6993226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 700dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 701dbae2b06SPaolo Abeni * for small fragments 7023226b158SEric Dumazet */ 703dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7043226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 705d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 706cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 707cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 708a080e7bdSAlexander Duyck if (!skb) 709a080e7bdSAlexander Duyck goto skb_fail; 710a080e7bdSAlexander Duyck goto skb_success; 711a080e7bdSAlexander Duyck } 7129451980aSAlexander Duyck 7133226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 7149451980aSAlexander Duyck 7159451980aSAlexander Duyck if (sk_memalloc_socks()) 7169451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7179451980aSAlexander Duyck 718dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 719dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 720dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 721dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 722dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 723dbae2b06SPaolo Abeni * size above 512 bytes 724dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 725dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 726dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 727dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 728dbae2b06SPaolo Abeni */ 729dbae2b06SPaolo Abeni len = SZ_1K; 730dbae2b06SPaolo Abeni 731dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 732dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 733dbae2b06SPaolo Abeni } else { 734dbae2b06SPaolo Abeni len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 735dbae2b06SPaolo Abeni len = SKB_DATA_ALIGN(len); 736dbae2b06SPaolo Abeni 7378c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 738dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 739dbae2b06SPaolo Abeni } 740dbae2b06SPaolo Abeni 7419451980aSAlexander Duyck if (unlikely(!data)) 7429451980aSAlexander Duyck return NULL; 7439451980aSAlexander Duyck 744cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 7459451980aSAlexander Duyck if (unlikely(!skb)) { 746181edb2bSAlexander Duyck skb_free_frag(data); 7479451980aSAlexander Duyck return NULL; 7489451980aSAlexander Duyck } 7499451980aSAlexander Duyck 750dbae2b06SPaolo Abeni if (pfmemalloc) 7519451980aSAlexander Duyck skb->pfmemalloc = 1; 7529451980aSAlexander Duyck skb->head_frag = 1; 7539451980aSAlexander Duyck 754a080e7bdSAlexander Duyck skb_success: 755fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 756fd11a83dSAlexander Duyck skb->dev = napi->dev; 757fd11a83dSAlexander Duyck 758a080e7bdSAlexander Duyck skb_fail: 759fd11a83dSAlexander Duyck return skb; 760fd11a83dSAlexander Duyck } 761fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 762fd11a83dSAlexander Duyck 763654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 76450269e19SEric Dumazet int size, unsigned int truesize) 765654bed16SPeter Zijlstra { 766654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 767654bed16SPeter Zijlstra skb->len += size; 768654bed16SPeter Zijlstra skb->data_len += size; 76950269e19SEric Dumazet skb->truesize += truesize; 770654bed16SPeter Zijlstra } 771654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 772654bed16SPeter Zijlstra 773f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 774f8e617e1SJason Wang unsigned int truesize) 775f8e617e1SJason Wang { 776f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 777f8e617e1SJason Wang 778f8e617e1SJason Wang skb_frag_size_add(frag, size); 779f8e617e1SJason Wang skb->len += size; 780f8e617e1SJason Wang skb->data_len += size; 781f8e617e1SJason Wang skb->truesize += truesize; 782f8e617e1SJason Wang } 783f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 784f8e617e1SJason Wang 78527b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 7861da177e4SLinus Torvalds { 787bd8a7036SEric Dumazet kfree_skb_list(*listp); 78827b437c8SHerbert Xu *listp = NULL; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 79127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 79227b437c8SHerbert Xu { 79327b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 79427b437c8SHerbert Xu } 79527b437c8SHerbert Xu 7961da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 7971da177e4SLinus Torvalds { 7981da177e4SLinus Torvalds struct sk_buff *list; 7991da177e4SLinus Torvalds 800fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 8011da177e4SLinus Torvalds skb_get(list); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8044727bab4SYunsheng Lin static bool skb_pp_recycle(struct sk_buff *skb, void *data) 8054727bab4SYunsheng Lin { 8064727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 8074727bab4SYunsheng Lin return false; 8084727bab4SYunsheng Lin return page_pool_return_skb_page(virt_to_page(data)); 8094727bab4SYunsheng Lin } 8104727bab4SYunsheng Lin 811d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 812d3836f21SEric Dumazet { 813181edb2bSAlexander Duyck unsigned char *head = skb->head; 814181edb2bSAlexander Duyck 8156a5bcd84SIlias Apalodimas if (skb->head_frag) { 8166a5bcd84SIlias Apalodimas if (skb_pp_recycle(skb, head)) 8176a5bcd84SIlias Apalodimas return; 818181edb2bSAlexander Duyck skb_free_frag(head); 8196a5bcd84SIlias Apalodimas } else { 820181edb2bSAlexander Duyck kfree(head); 821d3836f21SEric Dumazet } 8226a5bcd84SIlias Apalodimas } 823d3836f21SEric Dumazet 824511a3edaSEric Dumazet static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason) 8251da177e4SLinus Torvalds { 826ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 8271da177e4SLinus Torvalds int i; 828ff04a771SEric Dumazet 829ff04a771SEric Dumazet if (skb->cloned && 830ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 831ff04a771SEric Dumazet &shinfo->dataref)) 8322cc3aeb5SIlias Apalodimas goto exit; 833ff04a771SEric Dumazet 834753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 835753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 836753f1ca4SPavel Begunkov 83770c43167SJonathan Lemon skb_zcopy_clear(skb, true); 838753f1ca4SPavel Begunkov if (skip_unref) 839753f1ca4SPavel Begunkov goto free_head; 840753f1ca4SPavel Begunkov } 84170c43167SJonathan Lemon 842ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 8436a5bcd84SIlias Apalodimas __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 8441da177e4SLinus Torvalds 845753f1ca4SPavel Begunkov free_head: 846ff04a771SEric Dumazet if (shinfo->frag_list) 847511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 8481da177e4SLinus Torvalds 849d3836f21SEric Dumazet skb_free_head(skb); 8502cc3aeb5SIlias Apalodimas exit: 8512cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 8522cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 8532cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 8542cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 8552cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 85658e61e41STom Rix * additional references to the fragmented part of the SKB. 8572cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 8582cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 8592cc3aeb5SIlias Apalodimas */ 8602cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* 8641da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 8651da177e4SLinus Torvalds */ 8662d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 8671da177e4SLinus Torvalds { 868d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 869d179cd12SDavid S. Miller 870d179cd12SDavid S. Miller switch (skb->fclone) { 871d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 8721da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 8736ffe75ebSEric Dumazet return; 874d179cd12SDavid S. Miller 875d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 876d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 8776ffe75ebSEric Dumazet 8786ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 8796ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 8806ffe75ebSEric Dumazet * while here, branch prediction will be good. 8816ffe75ebSEric Dumazet */ 8822638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 8836ffe75ebSEric Dumazet goto fastpath; 884d179cd12SDavid S. Miller break; 885d179cd12SDavid S. Miller 8866ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 887d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 888d179cd12SDavid S. Miller break; 8893ff50b79SStephen Hemminger } 8902638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 8916ffe75ebSEric Dumazet return; 8926ffe75ebSEric Dumazet fastpath: 8936ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8960a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 8971da177e4SLinus Torvalds { 898adf30907SEric Dumazet skb_dst_drop(skb); 8991da177e4SLinus Torvalds if (skb->destructor) { 9007890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 9011da177e4SLinus Torvalds skb->destructor(skb); 9021da177e4SLinus Torvalds } 903a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 904cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 9052fc72c7bSKOVACS Krisztian #endif 906df5042f4SFlorian Westphal skb_ext_put(skb); 90704a4bb55SLennert Buytenhek } 90804a4bb55SLennert Buytenhek 90904a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 910511a3edaSEric Dumazet static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason) 91104a4bb55SLennert Buytenhek { 91204a4bb55SLennert Buytenhek skb_release_head_state(skb); 913a28b1b90SFlorian Westphal if (likely(skb->head)) 914511a3edaSEric Dumazet skb_release_data(skb, reason); 9152d4baff8SHerbert Xu } 9161da177e4SLinus Torvalds 9172d4baff8SHerbert Xu /** 9182d4baff8SHerbert Xu * __kfree_skb - private function 9192d4baff8SHerbert Xu * @skb: buffer 9202d4baff8SHerbert Xu * 9212d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 9222d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 9232d4baff8SHerbert Xu * always call kfree_skb 9242d4baff8SHerbert Xu */ 9252d4baff8SHerbert Xu 9262d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 9272d4baff8SHerbert Xu { 928511a3edaSEric Dumazet skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 9291da177e4SLinus Torvalds kfree_skbmem(skb); 9301da177e4SLinus Torvalds } 931b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 9321da177e4SLinus Torvalds 933a4650da2SJesper Dangaard Brouer static __always_inline 934a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 935a4650da2SJesper Dangaard Brouer { 936a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 937a4650da2SJesper Dangaard Brouer return false; 938a4650da2SJesper Dangaard Brouer 939a4650da2SJesper Dangaard Brouer DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX); 940a4650da2SJesper Dangaard Brouer 941a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 942a4650da2SJesper Dangaard Brouer trace_consume_skb(skb); 943a4650da2SJesper Dangaard Brouer else 944a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 945a4650da2SJesper Dangaard Brouer return true; 946a4650da2SJesper Dangaard Brouer } 947a4650da2SJesper Dangaard Brouer 9481da177e4SLinus Torvalds /** 949c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 950231d06aeSJörn Engel * @skb: buffer to free 951c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 952231d06aeSJörn Engel * 953231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 954c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 955c504e5c2SMenglong Dong * tracepoint. 956231d06aeSJörn Engel */ 957c205cc75SMenglong Dong void __fix_address 958c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 959231d06aeSJörn Engel { 960a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 961231d06aeSJörn Engel __kfree_skb(skb); 962231d06aeSJörn Engel } 963c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 964231d06aeSJörn Engel 965*eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 966*eedade12SJesper Dangaard Brouer 967*eedade12SJesper Dangaard Brouer struct skb_free_array { 968*eedade12SJesper Dangaard Brouer unsigned int skb_count; 969*eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 970*eedade12SJesper Dangaard Brouer }; 971*eedade12SJesper Dangaard Brouer 972*eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 973*eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 974*eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 975*eedade12SJesper Dangaard Brouer { 976*eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 977*eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 978*eedade12SJesper Dangaard Brouer __kfree_skb(skb); 979*eedade12SJesper Dangaard Brouer return; 980*eedade12SJesper Dangaard Brouer } 981*eedade12SJesper Dangaard Brouer 982*eedade12SJesper Dangaard Brouer skb_release_all(skb, reason); 983*eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 984*eedade12SJesper Dangaard Brouer 985*eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 986*eedade12SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, KFREE_SKB_BULK_SIZE, 987*eedade12SJesper Dangaard Brouer sa->skb_array); 988*eedade12SJesper Dangaard Brouer sa->skb_count = 0; 989*eedade12SJesper Dangaard Brouer } 990*eedade12SJesper Dangaard Brouer } 991*eedade12SJesper Dangaard Brouer 992a4650da2SJesper Dangaard Brouer void __fix_address 993a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 994bd8a7036SEric Dumazet { 995*eedade12SJesper Dangaard Brouer struct skb_free_array sa; 996*eedade12SJesper Dangaard Brouer 997*eedade12SJesper Dangaard Brouer sa.skb_count = 0; 998*eedade12SJesper Dangaard Brouer 999bd8a7036SEric Dumazet while (segs) { 1000bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 1001bd8a7036SEric Dumazet 1002*eedade12SJesper Dangaard Brouer skb_mark_not_on_list(segs); 1003*eedade12SJesper Dangaard Brouer 1004a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) 1005*eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 1006*eedade12SJesper Dangaard Brouer 1007bd8a7036SEric Dumazet segs = next; 1008bd8a7036SEric Dumazet } 1009*eedade12SJesper Dangaard Brouer 1010*eedade12SJesper Dangaard Brouer if (sa.skb_count) 1011*eedade12SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, sa.skb_count, 1012*eedade12SJesper Dangaard Brouer sa.skb_array); 1013bd8a7036SEric Dumazet } 1014215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1015bd8a7036SEric Dumazet 10166413139dSWillem de Bruijn /* Dump skb information and contents. 10176413139dSWillem de Bruijn * 10186413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 10196413139dSWillem de Bruijn * 1020302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 10216413139dSWillem de Bruijn */ 10226413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 10236413139dSWillem de Bruijn { 10246413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 10256413139dSWillem de Bruijn struct net_device *dev = skb->dev; 10266413139dSWillem de Bruijn struct sock *sk = skb->sk; 10276413139dSWillem de Bruijn struct sk_buff *list_skb; 10286413139dSWillem de Bruijn bool has_mac, has_trans; 10296413139dSWillem de Bruijn int headroom, tailroom; 10306413139dSWillem de Bruijn int i, len, seg_len; 10316413139dSWillem de Bruijn 10326413139dSWillem de Bruijn if (full_pkt) 10336413139dSWillem de Bruijn len = skb->len; 10346413139dSWillem de Bruijn else 10356413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 10366413139dSWillem de Bruijn 10376413139dSWillem de Bruijn headroom = skb_headroom(skb); 10386413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 10396413139dSWillem de Bruijn 10406413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 10416413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 10426413139dSWillem de Bruijn 10436413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 10446413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 10456413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 10466413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 10476413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 10486413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 10496413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 10506413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 10516413139dSWillem de Bruijn skb->network_header, 10526413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 10536413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 10546413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 10556413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 10566413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 10576413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 10586413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 10596413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 10606413139dSWillem de Bruijn 10616413139dSWillem de Bruijn if (dev) 10628a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 10636413139dSWillem de Bruijn level, dev->name, &dev->features); 10646413139dSWillem de Bruijn if (sk) 1065db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 10666413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 10676413139dSWillem de Bruijn 10686413139dSWillem de Bruijn if (full_pkt && headroom) 10696413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 10706413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 10716413139dSWillem de Bruijn 10726413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 10736413139dSWillem de Bruijn if (seg_len) 10746413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 10756413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 10766413139dSWillem de Bruijn len -= seg_len; 10776413139dSWillem de Bruijn 10786413139dSWillem de Bruijn if (full_pkt && tailroom) 10796413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 10806413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 10816413139dSWillem de Bruijn 10826413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 10836413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 10846413139dSWillem de Bruijn u32 p_off, p_len, copied; 10856413139dSWillem de Bruijn struct page *p; 10866413139dSWillem de Bruijn u8 *vaddr; 10876413139dSWillem de Bruijn 1088b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 10896413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 10906413139dSWillem de Bruijn copied) { 10916413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 10926413139dSWillem de Bruijn vaddr = kmap_atomic(p); 10936413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 10946413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 10956413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 10966413139dSWillem de Bruijn kunmap_atomic(vaddr); 10976413139dSWillem de Bruijn len -= seg_len; 10986413139dSWillem de Bruijn if (!len) 10996413139dSWillem de Bruijn break; 11006413139dSWillem de Bruijn } 11016413139dSWillem de Bruijn } 11026413139dSWillem de Bruijn 11036413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 11046413139dSWillem de Bruijn printk("skb fraglist:\n"); 11056413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 11066413139dSWillem de Bruijn skb_dump(level, list_skb, true); 11076413139dSWillem de Bruijn } 11086413139dSWillem de Bruijn } 11096413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 11106413139dSWillem de Bruijn 1111d1a203eaSStephen Hemminger /** 111225121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 111325121173SMichael S. Tsirkin * @skb: buffer that triggered an error 111425121173SMichael S. Tsirkin * 111525121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 111625121173SMichael S. Tsirkin * skb must be freed afterwards. 111725121173SMichael S. Tsirkin */ 111825121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 111925121173SMichael S. Tsirkin { 1120753f1ca4SPavel Begunkov if (skb) { 1121753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 11221f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 112325121173SMichael S. Tsirkin } 1124753f1ca4SPavel Begunkov } 112525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 112625121173SMichael S. Tsirkin 1127be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 112825121173SMichael S. Tsirkin /** 1129ead2ceb0SNeil Horman * consume_skb - free an skbuff 1130ead2ceb0SNeil Horman * @skb: buffer to free 1131ead2ceb0SNeil Horman * 1132ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1133ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1134ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1135ead2ceb0SNeil Horman */ 1136ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1137ead2ceb0SNeil Horman { 11383889a803SPaolo Abeni if (!skb_unref(skb)) 1139ead2ceb0SNeil Horman return; 11403889a803SPaolo Abeni 114107dc22e7SKoki Sanagi trace_consume_skb(skb); 1142ead2ceb0SNeil Horman __kfree_skb(skb); 1143ead2ceb0SNeil Horman } 1144ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1145be769db2SHerbert Xu #endif 1146ead2ceb0SNeil Horman 11470a463c78SPaolo Abeni /** 1148c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 11490a463c78SPaolo Abeni * @skb: buffer to free 11500a463c78SPaolo Abeni * 1151ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1152ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 11530a463c78SPaolo Abeni */ 1154ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 11550a463c78SPaolo Abeni { 11560a463c78SPaolo Abeni trace_consume_skb(skb); 1157511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 11580a463c78SPaolo Abeni kfree_skbmem(skb); 11590a463c78SPaolo Abeni } 11600a463c78SPaolo Abeni 1161f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1162795bb1c0SJesper Dangaard Brouer { 1163795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1164f450d539SAlexander Lobakin u32 i; 1165795bb1c0SJesper Dangaard Brouer 1166f450d539SAlexander Lobakin kasan_poison_object_data(skbuff_head_cache, skb); 1167795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1168795bb1c0SJesper Dangaard Brouer 1169795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1170f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1171f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, 1172f450d539SAlexander Lobakin nc->skb_cache[i]); 1173f450d539SAlexander Lobakin 1174f450d539SAlexander Lobakin kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 1175f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1176f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1177795bb1c0SJesper Dangaard Brouer } 1178795bb1c0SJesper Dangaard Brouer } 1179f450d539SAlexander Lobakin 118015fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 118115fad714SJesper Dangaard Brouer { 1182511a3edaSEric Dumazet skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 11839243adfcSAlexander Lobakin napi_skb_cache_put(skb); 11849243adfcSAlexander Lobakin } 11859243adfcSAlexander Lobakin 11869243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 11879243adfcSAlexander Lobakin { 11889efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 11898550ff8dSPaul Blakey nf_reset_ct(skb); 11909243adfcSAlexander Lobakin skb_dst_drop(skb); 11919243adfcSAlexander Lobakin skb_ext_put(skb); 11925e10da53SPaolo Abeni skb_orphan(skb); 11939efb4b5bSPaolo Abeni skb->slow_gro = 0; 11949efb4b5bSPaolo Abeni } 1195f450d539SAlexander Lobakin napi_skb_cache_put(skb); 119615fad714SJesper Dangaard Brouer } 1197795bb1c0SJesper Dangaard Brouer 1198795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1199795bb1c0SJesper Dangaard Brouer { 1200885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1201795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1202885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1203795bb1c0SJesper Dangaard Brouer return; 1204795bb1c0SJesper Dangaard Brouer } 1205795bb1c0SJesper Dangaard Brouer 1206ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 12076454eca8SYunsheng Lin 12087608894eSPaolo Abeni if (!skb_unref(skb)) 1209795bb1c0SJesper Dangaard Brouer return; 12107608894eSPaolo Abeni 1211795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1212795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 1213795bb1c0SJesper Dangaard Brouer 1214795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1215abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1216795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1217795bb1c0SJesper Dangaard Brouer return; 1218795bb1c0SJesper Dangaard Brouer } 1219795bb1c0SJesper Dangaard Brouer 1220511a3edaSEric Dumazet skb_release_all(skb, SKB_CONSUMED); 1221f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1222795bb1c0SJesper Dangaard Brouer } 1223795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1224795bb1c0SJesper Dangaard Brouer 122503f61041SKees Cook /* Make sure a field is contained by headers group */ 1226b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 122703f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 122803f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1229b1937227SEric Dumazet 1230dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1231dec18810SHerbert Xu { 1232dec18810SHerbert Xu new->tstamp = old->tstamp; 1233b1937227SEric Dumazet /* We do not copy old->sk */ 1234dec18810SHerbert Xu new->dev = old->dev; 1235b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 12367fee226aSEric Dumazet skb_dst_copy(new, old); 1237df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1238b1937227SEric Dumazet __nf_copy(new, old, false); 12396aa895b0SPatrick McHardy 124003f61041SKees Cook /* Note : this field could be in the headers group. 1241b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1242b1937227SEric Dumazet */ 1243b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 124406021292SEliezer Tamir 124503f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1246b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1247b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1248b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1249b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1250b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1251b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1252b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1253b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1254b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1255b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1256b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1257b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1258b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1259b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1260b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1261b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1262b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1263b1937227SEric Dumazet #endif 1264e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1265b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 126606021292SEliezer Tamir #endif 126768822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 12682bd82484SEric Dumazet #ifdef CONFIG_XPS 12692bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 12702bd82484SEric Dumazet #endif 1271b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1272b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1273b1937227SEric Dumazet #endif 1274b1937227SEric Dumazet 1275dec18810SHerbert Xu } 1276dec18810SHerbert Xu 127782c49a35SHerbert Xu /* 127882c49a35SHerbert Xu * You should not add any new code to this function. Add it to 127982c49a35SHerbert Xu * __copy_skb_header above instead. 128082c49a35SHerbert Xu */ 1281e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds #define C(x) n->x = skb->x 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds n->next = n->prev = NULL; 12861da177e4SLinus Torvalds n->sk = NULL; 1287dec18810SHerbert Xu __copy_skb_header(n, skb); 1288dec18810SHerbert Xu 12891da177e4SLinus Torvalds C(len); 12901da177e4SLinus Torvalds C(data_len); 12913e6b3b2eSAlexey Dobriyan C(mac_len); 1292334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 129302f1c89dSPaul Moore n->cloned = 1; 12941da177e4SLinus Torvalds n->nohdr = 0; 1295b13dda9fSEric Dumazet n->peeked = 0; 1296e78bfb07SStefano Brivio C(pfmemalloc); 12976a5bcd84SIlias Apalodimas C(pp_recycle); 12981da177e4SLinus Torvalds n->destructor = NULL; 12991da177e4SLinus Torvalds C(tail); 13001da177e4SLinus Torvalds C(end); 130102f1c89dSPaul Moore C(head); 1302d3836f21SEric Dumazet C(head_frag); 130302f1c89dSPaul Moore C(data); 130402f1c89dSPaul Moore C(truesize); 130563354797SReshetova, Elena refcount_set(&n->users, 1); 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 13081da177e4SLinus Torvalds skb->cloned = 1; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds return n; 1311e0053ec0SHerbert Xu #undef C 1312e0053ec0SHerbert Xu } 1313e0053ec0SHerbert Xu 1314e0053ec0SHerbert Xu /** 1315da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1316da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1317da29e4b4SJakub Kicinski */ 1318da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1319da29e4b4SJakub Kicinski { 1320da29e4b4SJakub Kicinski struct sk_buff *n; 1321da29e4b4SJakub Kicinski 1322da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1323da29e4b4SJakub Kicinski if (!n) 1324da29e4b4SJakub Kicinski return NULL; 1325da29e4b4SJakub Kicinski 1326da29e4b4SJakub Kicinski n->len = first->len; 1327da29e4b4SJakub Kicinski n->data_len = first->len; 1328da29e4b4SJakub Kicinski n->truesize = first->truesize; 1329da29e4b4SJakub Kicinski 1330da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1331da29e4b4SJakub Kicinski 1332da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1333da29e4b4SJakub Kicinski n->destructor = NULL; 1334da29e4b4SJakub Kicinski 1335da29e4b4SJakub Kicinski return n; 1336da29e4b4SJakub Kicinski } 1337da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1338da29e4b4SJakub Kicinski 1339da29e4b4SJakub Kicinski /** 1340e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1341e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1342e0053ec0SHerbert Xu * @src: the skb to supply the contents 1343e0053ec0SHerbert Xu * 1344e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1345e0053ec0SHerbert Xu * supplied by the user. 1346e0053ec0SHerbert Xu * 1347e0053ec0SHerbert Xu * The target skb is returned upon exit. 1348e0053ec0SHerbert Xu */ 1349e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1350e0053ec0SHerbert Xu { 1351511a3edaSEric Dumazet skb_release_all(dst, SKB_CONSUMED); 1352e0053ec0SHerbert Xu return __skb_clone(dst, src); 1353e0053ec0SHerbert Xu } 1354e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1355e0053ec0SHerbert Xu 13566f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1357a91dbff5SWillem de Bruijn { 1358a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1359a91dbff5SWillem de Bruijn struct user_struct *user; 1360a91dbff5SWillem de Bruijn 1361a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1362a91dbff5SWillem de Bruijn return 0; 1363a91dbff5SWillem de Bruijn 1364a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1365a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1366a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1367a91dbff5SWillem de Bruijn 1368a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 136957fc05e8SEric Dumazet do { 1370a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1371a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1372a91dbff5SWillem de Bruijn return -ENOBUFS; 137357fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1374a91dbff5SWillem de Bruijn 1375a91dbff5SWillem de Bruijn if (!mmp->user) { 1376a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1377a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1378a91dbff5SWillem de Bruijn } else { 1379a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1380a91dbff5SWillem de Bruijn } 1381a91dbff5SWillem de Bruijn 1382a91dbff5SWillem de Bruijn return 0; 1383a91dbff5SWillem de Bruijn } 13846f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1385a91dbff5SWillem de Bruijn 13866f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1387a91dbff5SWillem de Bruijn { 1388a91dbff5SWillem de Bruijn if (mmp->user) { 1389a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1390a91dbff5SWillem de Bruijn free_uid(mmp->user); 1391a91dbff5SWillem de Bruijn } 1392a91dbff5SWillem de Bruijn } 13936f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1394a91dbff5SWillem de Bruijn 1395c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 139652267790SWillem de Bruijn { 1397e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 139852267790SWillem de Bruijn struct sk_buff *skb; 139952267790SWillem de Bruijn 140052267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 140152267790SWillem de Bruijn 140252267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 140352267790SWillem de Bruijn if (!skb) 140452267790SWillem de Bruijn return NULL; 140552267790SWillem de Bruijn 140652267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 140752267790SWillem de Bruijn uarg = (void *)skb->cb; 1408a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1409a91dbff5SWillem de Bruijn 1410a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1411a91dbff5SWillem de Bruijn kfree_skb(skb); 1412a91dbff5SWillem de Bruijn return NULL; 1413a91dbff5SWillem de Bruijn } 141452267790SWillem de Bruijn 1415e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 14164ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 14174ab6c99dSWillem de Bruijn uarg->len = 1; 14184ab6c99dSWillem de Bruijn uarg->bytelen = size; 141952267790SWillem de Bruijn uarg->zerocopy = 1; 1420e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1421e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 142252267790SWillem de Bruijn sock_hold(sk); 142352267790SWillem de Bruijn 1424e7d2b510SPavel Begunkov return &uarg->ubuf; 142552267790SWillem de Bruijn } 142652267790SWillem de Bruijn 1427e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 142852267790SWillem de Bruijn { 142952267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 143052267790SWillem de Bruijn } 143152267790SWillem de Bruijn 14328c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 14334ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 14344ab6c99dSWillem de Bruijn { 14354ab6c99dSWillem de Bruijn if (uarg) { 1436e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 14374ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 14384ab6c99dSWillem de Bruijn u32 bytelen, next; 14394ab6c99dSWillem de Bruijn 14401b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 14411b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 14421b4b2b09SPavel Begunkov return NULL; 14431b4b2b09SPavel Begunkov 14444ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 14454ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 14464ab6c99dSWillem de Bruijn */ 14474ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 14484ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 14494ab6c99dSWillem de Bruijn return NULL; 14504ab6c99dSWillem de Bruijn } 14514ab6c99dSWillem de Bruijn 1452e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1453e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1454e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 14554ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 14564ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 14574ab6c99dSWillem de Bruijn goto new_alloc; 14584ab6c99dSWillem de Bruijn return NULL; 14594ab6c99dSWillem de Bruijn } 14604ab6c99dSWillem de Bruijn 14614ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1462e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1463e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1464a91dbff5SWillem de Bruijn return NULL; 1465e7d2b510SPavel Begunkov uarg_zc->len++; 1466e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 14674ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1468100f6d8eSWillem de Bruijn 1469100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1470100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 14718e044917SJonathan Lemon net_zcopy_get(uarg); 1472100f6d8eSWillem de Bruijn 14734ab6c99dSWillem de Bruijn return uarg; 14744ab6c99dSWillem de Bruijn } 14754ab6c99dSWillem de Bruijn } 14764ab6c99dSWillem de Bruijn 14774ab6c99dSWillem de Bruijn new_alloc: 14788c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 14794ab6c99dSWillem de Bruijn } 14808c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 14814ab6c99dSWillem de Bruijn 14824ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 14834ab6c99dSWillem de Bruijn { 14844ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 14854ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 14864ab6c99dSWillem de Bruijn u64 sum_len; 14874ab6c99dSWillem de Bruijn 14884ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 14894ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 14904ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 14914ab6c99dSWillem de Bruijn 14924ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 14934ab6c99dSWillem de Bruijn return false; 14944ab6c99dSWillem de Bruijn 14954ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 14964ab6c99dSWillem de Bruijn return false; 14974ab6c99dSWillem de Bruijn 14984ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 14994ab6c99dSWillem de Bruijn return true; 15004ab6c99dSWillem de Bruijn } 15014ab6c99dSWillem de Bruijn 1502e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 150352267790SWillem de Bruijn { 15044ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 150552267790SWillem de Bruijn struct sock_exterr_skb *serr; 150652267790SWillem de Bruijn struct sock *sk = skb->sk; 15074ab6c99dSWillem de Bruijn struct sk_buff_head *q; 15084ab6c99dSWillem de Bruijn unsigned long flags; 15093bdd5ee0SWillem de Bruijn bool is_zerocopy; 15104ab6c99dSWillem de Bruijn u32 lo, hi; 15114ab6c99dSWillem de Bruijn u16 len; 151252267790SWillem de Bruijn 1513ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1514ccaffff1SWillem de Bruijn 15154ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 15164ab6c99dSWillem de Bruijn * so do not queue a completion notification 15174ab6c99dSWillem de Bruijn */ 15184ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 151952267790SWillem de Bruijn goto release; 152052267790SWillem de Bruijn 15214ab6c99dSWillem de Bruijn len = uarg->len; 15224ab6c99dSWillem de Bruijn lo = uarg->id; 15234ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 15243bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 15254ab6c99dSWillem de Bruijn 152652267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 152752267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 152852267790SWillem de Bruijn serr->ee.ee_errno = 0; 152952267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 15304ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 15314ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 15323bdd5ee0SWillem de Bruijn if (!is_zerocopy) 153352267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 153452267790SWillem de Bruijn 15354ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 15364ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 15374ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 15384ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 15394ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 15404ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 154152267790SWillem de Bruijn skb = NULL; 15424ab6c99dSWillem de Bruijn } 15434ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 154452267790SWillem de Bruijn 1545e3ae2365SAlexander Aring sk_error_report(sk); 154652267790SWillem de Bruijn 154752267790SWillem de Bruijn release: 154852267790SWillem de Bruijn consume_skb(skb); 154952267790SWillem de Bruijn sock_put(sk); 155052267790SWillem de Bruijn } 155175518851SJonathan Lemon 15528c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 155336177832SJonathan Lemon bool success) 155475518851SJonathan Lemon { 1555e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1556e7d2b510SPavel Begunkov 1557e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 155875518851SJonathan Lemon 155975518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1560e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 156175518851SJonathan Lemon } 15628c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 156352267790SWillem de Bruijn 15648c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 156552267790SWillem de Bruijn { 1566e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 156752267790SWillem de Bruijn 156852267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1569e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 157052267790SWillem de Bruijn 157152900d22SWillem de Bruijn if (have_uref) 15728c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 157352267790SWillem de Bruijn } 15748c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 157552267790SWillem de Bruijn 157652267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 157752267790SWillem de Bruijn struct msghdr *msg, int len, 157852267790SWillem de Bruijn struct ubuf_info *uarg) 157952267790SWillem de Bruijn { 15804ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 158152267790SWillem de Bruijn int err, orig_len = skb->len; 158252267790SWillem de Bruijn 15834ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 15844ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 15854ab6c99dSWillem de Bruijn */ 15864ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 15874ab6c99dSWillem de Bruijn return -EEXIST; 15884ab6c99dSWillem de Bruijn 1589ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 159052267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 159154d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 159254d43117SWillem de Bruijn 159352267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1594829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 159554d43117SWillem de Bruijn skb->sk = sk; 159652267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 159754d43117SWillem de Bruijn skb->sk = save_sk; 159852267790SWillem de Bruijn return err; 159952267790SWillem de Bruijn } 160052267790SWillem de Bruijn 160152900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 160252267790SWillem de Bruijn return skb->len - orig_len; 160352267790SWillem de Bruijn } 160452267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 160552267790SWillem de Bruijn 1606753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1607753f1ca4SPavel Begunkov { 1608753f1ca4SPavel Begunkov int i; 1609753f1ca4SPavel Begunkov 1610753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1611753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1612753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1613753f1ca4SPavel Begunkov } 1614753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1615753f1ca4SPavel Begunkov 16161f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 161752267790SWillem de Bruijn gfp_t gfp_mask) 161852267790SWillem de Bruijn { 161952267790SWillem de Bruijn if (skb_zcopy(orig)) { 162052267790SWillem de Bruijn if (skb_zcopy(nskb)) { 162152267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 162252267790SWillem de Bruijn if (!gfp_mask) { 162352267790SWillem de Bruijn WARN_ON_ONCE(1); 162452267790SWillem de Bruijn return -ENOMEM; 162552267790SWillem de Bruijn } 162652267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 162752267790SWillem de Bruijn return 0; 162852267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 162952267790SWillem de Bruijn return -EIO; 163052267790SWillem de Bruijn } 163152900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 163252267790SWillem de Bruijn } 163352267790SWillem de Bruijn return 0; 163452267790SWillem de Bruijn } 163552267790SWillem de Bruijn 16362c53040fSBen Hutchings /** 16372c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 163848c83012SMichael S. Tsirkin * @skb: the skb to modify 163948c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 164048c83012SMichael S. Tsirkin * 164106b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 164248c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 164348c83012SMichael S. Tsirkin * to userspace pages. 164448c83012SMichael S. Tsirkin * 164548c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 164648c83012SMichael S. Tsirkin * %GFP_ATOMIC. 164748c83012SMichael S. Tsirkin * 164848c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 164948c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 165048c83012SMichael S. Tsirkin */ 165148c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1652a6686f2fSShirley Ma { 1653a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1654a6686f2fSShirley Ma struct page *page, *head = NULL; 16553ece7826SWillem de Bruijn int i, new_frags; 16563ece7826SWillem de Bruijn u32 d_off; 1657a6686f2fSShirley Ma 16583ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 16593ece7826SWillem de Bruijn return -EINVAL; 16603ece7826SWillem de Bruijn 1661f72c4ac6SWillem de Bruijn if (!num_frags) 1662f72c4ac6SWillem de Bruijn goto release; 1663f72c4ac6SWillem de Bruijn 16643ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 16653ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 166602756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1667a6686f2fSShirley Ma if (!page) { 1668a6686f2fSShirley Ma while (head) { 166940dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1670a6686f2fSShirley Ma put_page(head); 1671a6686f2fSShirley Ma head = next; 1672a6686f2fSShirley Ma } 1673a6686f2fSShirley Ma return -ENOMEM; 1674a6686f2fSShirley Ma } 16753ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 16763ece7826SWillem de Bruijn head = page; 16773ece7826SWillem de Bruijn } 16783ece7826SWillem de Bruijn 16793ece7826SWillem de Bruijn page = head; 16803ece7826SWillem de Bruijn d_off = 0; 16813ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 16823ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 16833ece7826SWillem de Bruijn u32 p_off, p_len, copied; 16843ece7826SWillem de Bruijn struct page *p; 16853ece7826SWillem de Bruijn u8 *vaddr; 1686c613c209SWillem de Bruijn 1687b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1688c613c209SWillem de Bruijn p, p_off, p_len, copied) { 16893ece7826SWillem de Bruijn u32 copy, done = 0; 1690c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 16913ece7826SWillem de Bruijn 16923ece7826SWillem de Bruijn while (done < p_len) { 16933ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 16943ece7826SWillem de Bruijn d_off = 0; 16953ece7826SWillem de Bruijn page = (struct page *)page_private(page); 16963ece7826SWillem de Bruijn } 16973ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 16983ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 16993ece7826SWillem de Bruijn vaddr + p_off + done, copy); 17003ece7826SWillem de Bruijn done += copy; 17013ece7826SWillem de Bruijn d_off += copy; 17023ece7826SWillem de Bruijn } 170351c56b00SEric Dumazet kunmap_atomic(vaddr); 1704c613c209SWillem de Bruijn } 1705a6686f2fSShirley Ma } 1706a6686f2fSShirley Ma 1707a6686f2fSShirley Ma /* skb frags release userspace buffers */ 170802756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1709a8605c60SIan Campbell skb_frag_unref(skb, i); 1710a6686f2fSShirley Ma 1711a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 17123ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 17133ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 171440dadff2SSunghan Suh head = (struct page *)page_private(head); 1715a6686f2fSShirley Ma } 17163ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 17173ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 171848c83012SMichael S. Tsirkin 1719b90ddd56SWillem de Bruijn release: 17201f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1721a6686f2fSShirley Ma return 0; 1722a6686f2fSShirley Ma } 1723dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1724a6686f2fSShirley Ma 1725e0053ec0SHerbert Xu /** 1726e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1727e0053ec0SHerbert Xu * @skb: buffer to clone 1728e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1729e0053ec0SHerbert Xu * 1730e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1731e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1732e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1733e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1734e0053ec0SHerbert Xu * 1735e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1736e0053ec0SHerbert Xu * %GFP_ATOMIC. 1737e0053ec0SHerbert Xu */ 1738e0053ec0SHerbert Xu 1739e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1740e0053ec0SHerbert Xu { 1741d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1742d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1743d0bf4a9eSEric Dumazet skb1); 17446ffe75ebSEric Dumazet struct sk_buff *n; 1745e0053ec0SHerbert Xu 174670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1747a6686f2fSShirley Ma return NULL; 1748a6686f2fSShirley Ma 1749e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 17502638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 17516ffe75ebSEric Dumazet n = &fclones->skb2; 17522638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1753c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1754e0053ec0SHerbert Xu } else { 1755c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1756c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1757c93bdd0eSMel Gorman 1758e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1759e0053ec0SHerbert Xu if (!n) 1760e0053ec0SHerbert Xu return NULL; 1761fe55f6d5SVegard Nossum 1762e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1763e0053ec0SHerbert Xu } 1764e0053ec0SHerbert Xu 1765e0053ec0SHerbert Xu return __skb_clone(n, skb); 17661da177e4SLinus Torvalds } 1767b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 17681da177e4SLinus Torvalds 1769b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1770f5b17294SPravin B Shelar { 1771030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1772030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1773030737bcSEric Dumazet skb->csum_start += off; 1774f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1775f5b17294SPravin B Shelar skb->transport_header += off; 1776f5b17294SPravin B Shelar skb->network_header += off; 1777f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1778f5b17294SPravin B Shelar skb->mac_header += off; 1779f5b17294SPravin B Shelar skb->inner_transport_header += off; 1780f5b17294SPravin B Shelar skb->inner_network_header += off; 1781aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1782f5b17294SPravin B Shelar } 1783b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1784f5b17294SPravin B Shelar 178508303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 17861da177e4SLinus Torvalds { 1787dec18810SHerbert Xu __copy_skb_header(new, old); 1788dec18810SHerbert Xu 17897967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 17907967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 17917967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 17921da177e4SLinus Torvalds } 179308303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 17941da177e4SLinus Torvalds 1795c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1796c93bdd0eSMel Gorman { 1797c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1798c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1799c93bdd0eSMel Gorman return 0; 1800c93bdd0eSMel Gorman } 1801c93bdd0eSMel Gorman 18021da177e4SLinus Torvalds /** 18031da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 18041da177e4SLinus Torvalds * @skb: buffer to copy 18051da177e4SLinus Torvalds * @gfp_mask: allocation priority 18061da177e4SLinus Torvalds * 18071da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 18081da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 18091da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 18101da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 18111da177e4SLinus Torvalds * 18121da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 18131da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 18141da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 18151da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 18161da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 18171da177e4SLinus Torvalds */ 18181da177e4SLinus Torvalds 1819dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 18201da177e4SLinus Torvalds { 18216602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1822ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1823c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1824c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 18256602cebbSEric Dumazet 18261da177e4SLinus Torvalds if (!n) 18271da177e4SLinus Torvalds return NULL; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds /* Set the data pointer */ 18301da177e4SLinus Torvalds skb_reserve(n, headerlen); 18311da177e4SLinus Torvalds /* Set the tail pointer and length */ 18321da177e4SLinus Torvalds skb_put(n, skb->len); 18331da177e4SLinus Torvalds 18349f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 18351da177e4SLinus Torvalds 183608303c18SIlya Lesokhin skb_copy_header(n, skb); 18371da177e4SLinus Torvalds return n; 18381da177e4SLinus Torvalds } 1839b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds /** 1842bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 18431da177e4SLinus Torvalds * @skb: buffer to copy 1844117632e6SEric Dumazet * @headroom: headroom of new skb 18451da177e4SLinus Torvalds * @gfp_mask: allocation priority 1846bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1847bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1848bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 18491da177e4SLinus Torvalds * 18501da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 18511da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 18521da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 18531da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 18541da177e4SLinus Torvalds * or the pointer to the buffer on success. 18551da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 18561da177e4SLinus Torvalds */ 18571da177e4SLinus Torvalds 1858bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1859bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 18601da177e4SLinus Torvalds { 1861117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1862bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1863bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 18646602cebbSEric Dumazet 18651da177e4SLinus Torvalds if (!n) 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds /* Set the data pointer */ 1869117632e6SEric Dumazet skb_reserve(n, headroom); 18701da177e4SLinus Torvalds /* Set the tail pointer and length */ 18711da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 18721da177e4SLinus Torvalds /* Copy the bytes */ 1873d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 18741da177e4SLinus Torvalds 187525f484a6SHerbert Xu n->truesize += skb->data_len; 18761da177e4SLinus Torvalds n->data_len = skb->data_len; 18771da177e4SLinus Torvalds n->len = skb->len; 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 18801da177e4SLinus Torvalds int i; 18811da177e4SLinus Torvalds 18821f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 18831f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 18841511022cSDan Carpenter kfree_skb(n); 18851511022cSDan Carpenter n = NULL; 1886a6686f2fSShirley Ma goto out; 1887a6686f2fSShirley Ma } 18881da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 18891da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1890ea2ab693SIan Campbell skb_frag_ref(skb, i); 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 189521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 18961da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 18971da177e4SLinus Torvalds skb_clone_fraglist(n); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 190008303c18SIlya Lesokhin skb_copy_header(n, skb); 19011da177e4SLinus Torvalds out: 19021da177e4SLinus Torvalds return n; 19031da177e4SLinus Torvalds } 1904bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds /** 19071da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 19081da177e4SLinus Torvalds * @skb: buffer to reallocate 19091da177e4SLinus Torvalds * @nhead: room to add at head 19101da177e4SLinus Torvalds * @ntail: room to add at tail 19111da177e4SLinus Torvalds * @gfp_mask: allocation priority 19121da177e4SLinus Torvalds * 1913bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1914bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 19151da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 19161da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 19191da177e4SLinus Torvalds * reloaded after call to this function. 19201da177e4SLinus Torvalds */ 19211da177e4SLinus Torvalds 192286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1923dd0fc66fSAl Viro gfp_t gfp_mask) 19241da177e4SLinus Torvalds { 192512d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 192612d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 19271da177e4SLinus Torvalds long off; 1928158f323bSEric Dumazet u8 *data; 192912d6c1d3SKees Cook int i; 19301da177e4SLinus Torvalds 19314edd87adSHerbert Xu BUG_ON(nhead < 0); 19324edd87adSHerbert Xu 19339f77fad3STim Hansen BUG_ON(skb_shared(skb)); 19341da177e4SLinus Torvalds 1935753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 1936753f1ca4SPavel Begunkov 1937c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1938c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 193912d6c1d3SKees Cook 194012d6c1d3SKees Cook size = SKB_DATA_ALIGN(size); 194112d6c1d3SKees Cook size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 194212d6c1d3SKees Cook size = kmalloc_size_roundup(size); 194312d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 19441da177e4SLinus Torvalds if (!data) 19451da177e4SLinus Torvalds goto nodata; 194612d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 19496602cebbSEric Dumazet * optimized for the cases when header is void. 19506602cebbSEric Dumazet */ 19516602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 19526602cebbSEric Dumazet 19536602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 19546602cebbSEric Dumazet skb_shinfo(skb), 1955fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 19561da177e4SLinus Torvalds 19573e24591aSAlexander Duyck /* 19583e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 19593e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 19603e24591aSAlexander Duyck * be since all we did is relocate the values 19613e24591aSAlexander Duyck */ 19623e24591aSAlexander Duyck if (skb_cloned(skb)) { 196370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1964a6686f2fSShirley Ma goto nofrags; 19651f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1966c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 19671da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1968ea2ab693SIan Campbell skb_frag_ref(skb, i); 19691da177e4SLinus Torvalds 197021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 19711da177e4SLinus Torvalds skb_clone_fraglist(skb); 19721da177e4SLinus Torvalds 1973511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 19743e24591aSAlexander Duyck } else { 19753e24591aSAlexander Duyck skb_free_head(skb); 19761fd63041SEric Dumazet } 19771da177e4SLinus Torvalds off = (data + nhead) - skb->head; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds skb->head = data; 1980d3836f21SEric Dumazet skb->head_frag = 0; 19811da177e4SLinus Torvalds skb->data += off; 1982763087daSEric Dumazet 1983763087daSEric Dumazet skb_set_end_offset(skb, size); 19844305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 198556eb8882SPatrick McHardy off = nhead; 198656eb8882SPatrick McHardy #endif 198727a884dcSArnaldo Carvalho de Melo skb->tail += off; 1988b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 19891da177e4SLinus Torvalds skb->cloned = 0; 1990334a8132SPatrick McHardy skb->hdr_len = 0; 19911da177e4SLinus Torvalds skb->nohdr = 0; 19921da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1993158f323bSEric Dumazet 1994de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1995de8f3a83SDaniel Borkmann 1996158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1997158f323bSEric Dumazet * For the moment, we really care of rx path, or 1998158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1999158f323bSEric Dumazet */ 2000158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2001158f323bSEric Dumazet skb->truesize += size - osize; 2002158f323bSEric Dumazet 20031da177e4SLinus Torvalds return 0; 20041da177e4SLinus Torvalds 2005a6686f2fSShirley Ma nofrags: 2006a6686f2fSShirley Ma kfree(data); 20071da177e4SLinus Torvalds nodata: 20081da177e4SLinus Torvalds return -ENOMEM; 20091da177e4SLinus Torvalds } 2010b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds struct sk_buff *skb2; 20171da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds if (delta <= 0) 20201da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 20211da177e4SLinus Torvalds else { 20221da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 20231da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 20241da177e4SLinus Torvalds GFP_ATOMIC)) { 20251da177e4SLinus Torvalds kfree_skb(skb2); 20261da177e4SLinus Torvalds skb2 = NULL; 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds return skb2; 20301da177e4SLinus Torvalds } 2031b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 20321da177e4SLinus Torvalds 20332b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 20342b88cba5SEric Dumazet { 20352b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 20362b88cba5SEric Dumazet struct skb_shared_info *shinfo; 20372b88cba5SEric Dumazet int res; 20382b88cba5SEric Dumazet 20392b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 20402b88cba5SEric Dumazet saved_truesize = skb->truesize; 20412b88cba5SEric Dumazet 20422b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 20432b88cba5SEric Dumazet if (res) 20442b88cba5SEric Dumazet return res; 20452b88cba5SEric Dumazet 20462b88cba5SEric Dumazet skb->truesize = saved_truesize; 20472b88cba5SEric Dumazet 20482b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 20492b88cba5SEric Dumazet return 0; 20502b88cba5SEric Dumazet 20512b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 20522b88cba5SEric Dumazet 20532b88cba5SEric Dumazet /* We are about to change back skb->end, 20542b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 20552b88cba5SEric Dumazet */ 20562b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 20572b88cba5SEric Dumazet shinfo, 20582b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 20592b88cba5SEric Dumazet 20602b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 20612b88cba5SEric Dumazet 20622b88cba5SEric Dumazet return 0; 20632b88cba5SEric Dumazet } 20642b88cba5SEric Dumazet 20651da177e4SLinus Torvalds /** 2066f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2067f1260ff1SVasily Averin * @skb: buffer to reallocate 2068f1260ff1SVasily Averin * @headroom: needed headroom 2069f1260ff1SVasily Averin * 2070f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2071f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2072f1260ff1SVasily Averin * and frees original skb in case of failures. 2073f1260ff1SVasily Averin * 2074f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2075f1260ff1SVasily Averin */ 2076f1260ff1SVasily Averin 2077f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2078f1260ff1SVasily Averin { 2079f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 20807f678defSVasily Averin int osize = skb_end_offset(skb); 20817f678defSVasily Averin struct sock *sk = skb->sk; 2082f1260ff1SVasily Averin 2083f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2084f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2085f1260ff1SVasily Averin return skb; 2086f1260ff1SVasily Averin 20877f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 20887f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 20897f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2090f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2091f1260ff1SVasily Averin 20927f678defSVasily Averin if (unlikely(!nskb)) 20937f678defSVasily Averin goto fail; 20947f678defSVasily Averin 20957f678defSVasily Averin if (sk) 20967f678defSVasily Averin skb_set_owner_w(nskb, sk); 2097f1260ff1SVasily Averin consume_skb(skb); 2098f1260ff1SVasily Averin skb = nskb; 2099f1260ff1SVasily Averin } 21007f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 21017f678defSVasily Averin goto fail; 21027f678defSVasily Averin 21037f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 21047f678defSVasily Averin delta = skb_end_offset(skb) - osize; 21057f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 21067f678defSVasily Averin skb->truesize += delta; 2107f1260ff1SVasily Averin } 2108f1260ff1SVasily Averin return skb; 21097f678defSVasily Averin 21107f678defSVasily Averin fail: 21117f678defSVasily Averin kfree_skb(skb); 21127f678defSVasily Averin return NULL; 2113f1260ff1SVasily Averin } 2114f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2115f1260ff1SVasily Averin 2116f1260ff1SVasily Averin /** 21171da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 21181da177e4SLinus Torvalds * @skb: buffer to copy 21191da177e4SLinus Torvalds * @newheadroom: new free bytes at head 21201da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 21211da177e4SLinus Torvalds * @gfp_mask: allocation priority 21221da177e4SLinus Torvalds * 21231da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 21241da177e4SLinus Torvalds * allocate additional space. 21251da177e4SLinus Torvalds * 21261da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 21271da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 21281da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 21291da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 21301da177e4SLinus Torvalds * 21311da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 21321da177e4SLinus Torvalds * is called from an interrupt. 21331da177e4SLinus Torvalds */ 21341da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 213586a76cafSVictor Fusco int newheadroom, int newtailroom, 2136dd0fc66fSAl Viro gfp_t gfp_mask) 21371da177e4SLinus Torvalds { 21381da177e4SLinus Torvalds /* 21391da177e4SLinus Torvalds * Allocate the copy buffer 21401da177e4SLinus Torvalds */ 2141c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2142c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2143c93bdd0eSMel Gorman NUMA_NO_NODE); 2144efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 21451da177e4SLinus Torvalds int head_copy_len, head_copy_off; 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds if (!n) 21481da177e4SLinus Torvalds return NULL; 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds skb_reserve(n, newheadroom); 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds /* Set the tail pointer and length */ 21531da177e4SLinus Torvalds skb_put(n, skb->len); 21541da177e4SLinus Torvalds 2155efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 21561da177e4SLinus Torvalds head_copy_off = 0; 21571da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 21581da177e4SLinus Torvalds head_copy_len = newheadroom; 21591da177e4SLinus Torvalds else 21601da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /* Copy the linear header and data. */ 21639f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 21649f77fad3STim Hansen skb->len + head_copy_len)); 21651da177e4SLinus Torvalds 216608303c18SIlya Lesokhin skb_copy_header(n, skb); 21671da177e4SLinus Torvalds 2168030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2169efd1e8d5SPatrick McHardy 21701da177e4SLinus Torvalds return n; 21711da177e4SLinus Torvalds } 2172b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds /** 2175cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 21761da177e4SLinus Torvalds * @skb: buffer to pad 21771da177e4SLinus Torvalds * @pad: space to pad 2178cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 21791da177e4SLinus Torvalds * 21801da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 21811da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 21821da177e4SLinus Torvalds * beyond the buffer end onto the wire. 21831da177e4SLinus Torvalds * 2184cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2185cd0a137aSFlorian Fainelli * if @free_on_error is true. 21861da177e4SLinus Torvalds */ 21871da177e4SLinus Torvalds 2188cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 21891da177e4SLinus Torvalds { 21905b057c6bSHerbert Xu int err; 21915b057c6bSHerbert Xu int ntail; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 21945b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 21951da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 21965b057c6bSHerbert Xu return 0; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21994305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 22005b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 22015b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 22025b057c6bSHerbert Xu if (unlikely(err)) 22035b057c6bSHerbert Xu goto free_skb; 22045b057c6bSHerbert Xu } 22055b057c6bSHerbert Xu 22065b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 22075b057c6bSHerbert Xu * to be audited. 22085b057c6bSHerbert Xu */ 22095b057c6bSHerbert Xu err = skb_linearize(skb); 22105b057c6bSHerbert Xu if (unlikely(err)) 22115b057c6bSHerbert Xu goto free_skb; 22125b057c6bSHerbert Xu 22135b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 22145b057c6bSHerbert Xu return 0; 22155b057c6bSHerbert Xu 22165b057c6bSHerbert Xu free_skb: 2217cd0a137aSFlorian Fainelli if (free_on_error) 22181da177e4SLinus Torvalds kfree_skb(skb); 22195b057c6bSHerbert Xu return err; 22201da177e4SLinus Torvalds } 2221cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 22221da177e4SLinus Torvalds 22230dde3e16SIlpo Järvinen /** 22240c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 22250c7ddf36SMathias Krause * @skb: start of the buffer to use 22260c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 22270c7ddf36SMathias Krause * @len: amount of data to add 22280c7ddf36SMathias Krause * 22290c7ddf36SMathias Krause * This function extends the used data area of the potentially 22300c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 22310c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 22320c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 22330c7ddf36SMathias Krause * returned. 22340c7ddf36SMathias Krause */ 22350c7ddf36SMathias Krause 22364df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 22370c7ddf36SMathias Krause { 22380c7ddf36SMathias Krause if (tail != skb) { 22390c7ddf36SMathias Krause skb->data_len += len; 22400c7ddf36SMathias Krause skb->len += len; 22410c7ddf36SMathias Krause } 22420c7ddf36SMathias Krause return skb_put(tail, len); 22430c7ddf36SMathias Krause } 22440c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 22450c7ddf36SMathias Krause 22460c7ddf36SMathias Krause /** 22470dde3e16SIlpo Järvinen * skb_put - add data to a buffer 22480dde3e16SIlpo Järvinen * @skb: buffer to use 22490dde3e16SIlpo Järvinen * @len: amount of data to add 22500dde3e16SIlpo Järvinen * 22510dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 22520dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 22530dde3e16SIlpo Järvinen * first byte of the extra data is returned. 22540dde3e16SIlpo Järvinen */ 22554df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 22560dde3e16SIlpo Järvinen { 22574df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 22580dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 22590dde3e16SIlpo Järvinen skb->tail += len; 22600dde3e16SIlpo Järvinen skb->len += len; 22610dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 22620dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 22630dde3e16SIlpo Järvinen return tmp; 22640dde3e16SIlpo Järvinen } 22650dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 22660dde3e16SIlpo Järvinen 22676be8ac2fSIlpo Järvinen /** 2268c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2269c2aa270aSIlpo Järvinen * @skb: buffer to use 2270c2aa270aSIlpo Järvinen * @len: amount of data to add 2271c2aa270aSIlpo Järvinen * 2272c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2273c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2274c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2275c2aa270aSIlpo Järvinen */ 2276d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2277c2aa270aSIlpo Järvinen { 2278c2aa270aSIlpo Järvinen skb->data -= len; 2279c2aa270aSIlpo Järvinen skb->len += len; 2280c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2281c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2282c2aa270aSIlpo Järvinen return skb->data; 2283c2aa270aSIlpo Järvinen } 2284c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2285c2aa270aSIlpo Järvinen 2286c2aa270aSIlpo Järvinen /** 22876be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 22886be8ac2fSIlpo Järvinen * @skb: buffer to use 22896be8ac2fSIlpo Järvinen * @len: amount of data to remove 22906be8ac2fSIlpo Järvinen * 22916be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 22926be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 22936be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 22946be8ac2fSIlpo Järvinen * the old data. 22956be8ac2fSIlpo Järvinen */ 2296af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 22976be8ac2fSIlpo Järvinen { 229847d29646SDavid S. Miller return skb_pull_inline(skb, len); 22996be8ac2fSIlpo Järvinen } 23006be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 23016be8ac2fSIlpo Järvinen 2302419ae74eSIlpo Järvinen /** 230313244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 230413244cccSLuiz Augusto von Dentz * original position. 230513244cccSLuiz Augusto von Dentz * @skb: buffer to use 230613244cccSLuiz Augusto von Dentz * @len: amount of data to remove 230713244cccSLuiz Augusto von Dentz * 230813244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 230913244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 231013244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 231113244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 231213244cccSLuiz Augusto von Dentz */ 231313244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 231413244cccSLuiz Augusto von Dentz { 231513244cccSLuiz Augusto von Dentz void *data = skb->data; 231613244cccSLuiz Augusto von Dentz 231713244cccSLuiz Augusto von Dentz if (skb->len < len) 231813244cccSLuiz Augusto von Dentz return NULL; 231913244cccSLuiz Augusto von Dentz 232013244cccSLuiz Augusto von Dentz skb_pull(skb, len); 232113244cccSLuiz Augusto von Dentz 232213244cccSLuiz Augusto von Dentz return data; 232313244cccSLuiz Augusto von Dentz } 232413244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 232513244cccSLuiz Augusto von Dentz 232613244cccSLuiz Augusto von Dentz /** 2327419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2328419ae74eSIlpo Järvinen * @skb: buffer to alter 2329419ae74eSIlpo Järvinen * @len: new length 2330419ae74eSIlpo Järvinen * 2331419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2332419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2333419ae74eSIlpo Järvinen * The skb must be linear. 2334419ae74eSIlpo Järvinen */ 2335419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2336419ae74eSIlpo Järvinen { 2337419ae74eSIlpo Järvinen if (skb->len > len) 2338419ae74eSIlpo Järvinen __skb_trim(skb, len); 2339419ae74eSIlpo Järvinen } 2340419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2341419ae74eSIlpo Järvinen 23423cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 23431da177e4SLinus Torvalds */ 23441da177e4SLinus Torvalds 23453cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 23461da177e4SLinus Torvalds { 234727b437c8SHerbert Xu struct sk_buff **fragp; 234827b437c8SHerbert Xu struct sk_buff *frag; 23491da177e4SLinus Torvalds int offset = skb_headlen(skb); 23501da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 23511da177e4SLinus Torvalds int i; 235227b437c8SHerbert Xu int err; 235327b437c8SHerbert Xu 235427b437c8SHerbert Xu if (skb_cloned(skb) && 235527b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 235627b437c8SHerbert Xu return err; 23571da177e4SLinus Torvalds 2358f4d26fb3SHerbert Xu i = 0; 2359f4d26fb3SHerbert Xu if (offset >= len) 2360f4d26fb3SHerbert Xu goto drop_pages; 2361f4d26fb3SHerbert Xu 2362f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 23639e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 236427b437c8SHerbert Xu 236527b437c8SHerbert Xu if (end < len) { 23661da177e4SLinus Torvalds offset = end; 236727b437c8SHerbert Xu continue; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23709e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 237127b437c8SHerbert Xu 2372f4d26fb3SHerbert Xu drop_pages: 237327b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 237427b437c8SHerbert Xu 237527b437c8SHerbert Xu for (; i < nfrags; i++) 2376ea2ab693SIan Campbell skb_frag_unref(skb, i); 237727b437c8SHerbert Xu 237821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 237927b437c8SHerbert Xu skb_drop_fraglist(skb); 2380f4d26fb3SHerbert Xu goto done; 238127b437c8SHerbert Xu } 238227b437c8SHerbert Xu 238327b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 238427b437c8SHerbert Xu fragp = &frag->next) { 238527b437c8SHerbert Xu int end = offset + frag->len; 238627b437c8SHerbert Xu 238727b437c8SHerbert Xu if (skb_shared(frag)) { 238827b437c8SHerbert Xu struct sk_buff *nfrag; 238927b437c8SHerbert Xu 239027b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 239127b437c8SHerbert Xu if (unlikely(!nfrag)) 239227b437c8SHerbert Xu return -ENOMEM; 239327b437c8SHerbert Xu 239427b437c8SHerbert Xu nfrag->next = frag->next; 239585bb2a60SEric Dumazet consume_skb(frag); 239627b437c8SHerbert Xu frag = nfrag; 239727b437c8SHerbert Xu *fragp = frag; 239827b437c8SHerbert Xu } 239927b437c8SHerbert Xu 240027b437c8SHerbert Xu if (end < len) { 240127b437c8SHerbert Xu offset = end; 240227b437c8SHerbert Xu continue; 240327b437c8SHerbert Xu } 240427b437c8SHerbert Xu 240527b437c8SHerbert Xu if (end > len && 240627b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 240727b437c8SHerbert Xu return err; 240827b437c8SHerbert Xu 240927b437c8SHerbert Xu if (frag->next) 241027b437c8SHerbert Xu skb_drop_list(&frag->next); 241127b437c8SHerbert Xu break; 241227b437c8SHerbert Xu } 241327b437c8SHerbert Xu 2414f4d26fb3SHerbert Xu done: 241527b437c8SHerbert Xu if (len > skb_headlen(skb)) { 24161da177e4SLinus Torvalds skb->data_len -= skb->len - len; 24171da177e4SLinus Torvalds skb->len = len; 24181da177e4SLinus Torvalds } else { 24191da177e4SLinus Torvalds skb->len = len; 24201da177e4SLinus Torvalds skb->data_len = 0; 242127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 2424c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2425c21b48ccSEric Dumazet skb_condense(skb); 24261da177e4SLinus Torvalds return 0; 24271da177e4SLinus Torvalds } 2428b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 24291da177e4SLinus Torvalds 243088078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 243188078d98SEric Dumazet */ 243288078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 243388078d98SEric Dumazet { 243488078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 243588078d98SEric Dumazet int delta = skb->len - len; 243688078d98SEric Dumazet 2437d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2438d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2439d55bef50SDimitris Michailidis len); 244054970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 244154970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 244254970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 244354970a2fSVasily Averin 244454970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 244554970a2fSVasily Averin return -EINVAL; 244688078d98SEric Dumazet } 244788078d98SEric Dumazet return __pskb_trim(skb, len); 244888078d98SEric Dumazet } 244988078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 245088078d98SEric Dumazet 24511da177e4SLinus Torvalds /** 24521da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 24531da177e4SLinus Torvalds * @skb: buffer to reallocate 24541da177e4SLinus Torvalds * @delta: number of bytes to advance tail 24551da177e4SLinus Torvalds * 24561da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 24571da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 24581da177e4SLinus Torvalds * data from fragmented part. 24591da177e4SLinus Torvalds * 24601da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 24611da177e4SLinus Torvalds * 24621da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 24631da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 24641da177e4SLinus Torvalds * 24651da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 24661da177e4SLinus Torvalds * reloaded after call to this function. 24671da177e4SLinus Torvalds */ 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 24701da177e4SLinus Torvalds * when it is necessary. 24711da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 24721da177e4SLinus Torvalds * 2. It may change skb pointers. 24731da177e4SLinus Torvalds * 24741da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 24751da177e4SLinus Torvalds */ 2476af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 24771da177e4SLinus Torvalds { 24781da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 24791da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 24801da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 24811da177e4SLinus Torvalds */ 24824305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 24831da177e4SLinus Torvalds 24841da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 24851da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 24861da177e4SLinus Torvalds GFP_ATOMIC)) 24871da177e4SLinus Torvalds return NULL; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 24909f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 24919f77fad3STim Hansen skb_tail_pointer(skb), delta)); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 24941da177e4SLinus Torvalds * size of pulled pages. Superb. 24951da177e4SLinus Torvalds */ 249621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 24971da177e4SLinus Torvalds goto pull_pages; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 25001da177e4SLinus Torvalds eat = delta; 25011da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25029e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25039e903e08SEric Dumazet 25049e903e08SEric Dumazet if (size >= eat) 25051da177e4SLinus Torvalds goto pull_pages; 25069e903e08SEric Dumazet eat -= size; 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 251009001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 25111da177e4SLinus Torvalds * but taking into account that pulling is expected to 25121da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 25131da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 25141da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 25151da177e4SLinus Torvalds */ 25161da177e4SLinus Torvalds if (eat) { 25171da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 25181da177e4SLinus Torvalds struct sk_buff *clone = NULL; 25191da177e4SLinus Torvalds struct sk_buff *insp = NULL; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds do { 25221da177e4SLinus Torvalds if (list->len <= eat) { 25231da177e4SLinus Torvalds /* Eaten as whole. */ 25241da177e4SLinus Torvalds eat -= list->len; 25251da177e4SLinus Torvalds list = list->next; 25261da177e4SLinus Torvalds insp = list; 25271da177e4SLinus Torvalds } else { 25281da177e4SLinus Torvalds /* Eaten partially. */ 25292d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 25302d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 25312d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds if (skb_shared(list)) { 25341da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 25351da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 25361da177e4SLinus Torvalds if (!clone) 25371da177e4SLinus Torvalds return NULL; 25381da177e4SLinus Torvalds insp = list->next; 25391da177e4SLinus Torvalds list = clone; 25401da177e4SLinus Torvalds } else { 25411da177e4SLinus Torvalds /* This may be pulled without 25421da177e4SLinus Torvalds * problems. */ 25431da177e4SLinus Torvalds insp = list; 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 25461da177e4SLinus Torvalds kfree_skb(clone); 25471da177e4SLinus Torvalds return NULL; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds break; 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds } while (eat); 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds /* Free pulled out fragments. */ 25541da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 25551da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2556ef527f96SEric Dumazet consume_skb(list); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds /* And insert new clone at head. */ 25591da177e4SLinus Torvalds if (clone) { 25601da177e4SLinus Torvalds clone->next = list; 25611da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds } 25641da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds pull_pages: 25671da177e4SLinus Torvalds eat = delta; 25681da177e4SLinus Torvalds k = 0; 25691da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25709e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25719e903e08SEric Dumazet 25729e903e08SEric Dumazet if (size <= eat) { 2573ea2ab693SIan Campbell skb_frag_unref(skb, i); 25749e903e08SEric Dumazet eat -= size; 25751da177e4SLinus Torvalds } else { 2576b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2577b54c9d5bSJonathan Lemon 2578b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 25791da177e4SLinus Torvalds if (eat) { 2580b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2581b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 25823ccc6c6fSlinzhang if (!i) 25833ccc6c6fSlinzhang goto end; 25841da177e4SLinus Torvalds eat = 0; 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds k++; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 25901da177e4SLinus Torvalds 25913ccc6c6fSlinzhang end: 25921da177e4SLinus Torvalds skb->tail += delta; 25931da177e4SLinus Torvalds skb->data_len -= delta; 25941da177e4SLinus Torvalds 25951f8b977aSWillem de Bruijn if (!skb->data_len) 25961f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 25971f8b977aSWillem de Bruijn 259827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 25991da177e4SLinus Torvalds } 2600b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 26011da177e4SLinus Torvalds 260222019b17SEric Dumazet /** 260322019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 260422019b17SEric Dumazet * @skb: source skb 260522019b17SEric Dumazet * @offset: offset in source 260622019b17SEric Dumazet * @to: destination buffer 260722019b17SEric Dumazet * @len: number of bytes to copy 260822019b17SEric Dumazet * 260922019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 261022019b17SEric Dumazet * destination buffer. 261122019b17SEric Dumazet * 261222019b17SEric Dumazet * CAUTION ! : 261322019b17SEric Dumazet * If its prototype is ever changed, 261422019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 261522019b17SEric Dumazet * since it is called from BPF assembly code. 261622019b17SEric Dumazet */ 26171da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 26181da177e4SLinus Torvalds { 26191a028e50SDavid S. Miller int start = skb_headlen(skb); 2620fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2621fbb398a8SDavid S. Miller int i, copy; 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds if (offset > (int)skb->len - len) 26241da177e4SLinus Torvalds goto fault; 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds /* Copy header. */ 26271a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 26281da177e4SLinus Torvalds if (copy > len) 26291da177e4SLinus Torvalds copy = len; 2630d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 26311da177e4SLinus Torvalds if ((len -= copy) == 0) 26321da177e4SLinus Torvalds return 0; 26331da177e4SLinus Torvalds offset += copy; 26341da177e4SLinus Torvalds to += copy; 26351da177e4SLinus Torvalds } 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26381a028e50SDavid S. Miller int end; 263951c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 26401da177e4SLinus Torvalds 2641547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26421a028e50SDavid S. Miller 264351c56b00SEric Dumazet end = start + skb_frag_size(f); 26441da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2645c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2646c613c209SWillem de Bruijn struct page *p; 26471da177e4SLinus Torvalds u8 *vaddr; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds if (copy > len) 26501da177e4SLinus Torvalds copy = len; 26511da177e4SLinus Torvalds 2652c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2653b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2654c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2655c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2656c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 265751c56b00SEric Dumazet kunmap_atomic(vaddr); 2658c613c209SWillem de Bruijn } 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds if ((len -= copy) == 0) 26611da177e4SLinus Torvalds return 0; 26621da177e4SLinus Torvalds offset += copy; 26631da177e4SLinus Torvalds to += copy; 26641da177e4SLinus Torvalds } 26651a028e50SDavid S. Miller start = end; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 2668fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26691a028e50SDavid S. Miller int end; 26701da177e4SLinus Torvalds 2671547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26721a028e50SDavid S. Miller 2673fbb398a8SDavid S. Miller end = start + frag_iter->len; 26741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26751da177e4SLinus Torvalds if (copy > len) 26761da177e4SLinus Torvalds copy = len; 2677fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 26781da177e4SLinus Torvalds goto fault; 26791da177e4SLinus Torvalds if ((len -= copy) == 0) 26801da177e4SLinus Torvalds return 0; 26811da177e4SLinus Torvalds offset += copy; 26821da177e4SLinus Torvalds to += copy; 26831da177e4SLinus Torvalds } 26841a028e50SDavid S. Miller start = end; 26851da177e4SLinus Torvalds } 2686a6686f2fSShirley Ma 26871da177e4SLinus Torvalds if (!len) 26881da177e4SLinus Torvalds return 0; 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds fault: 26911da177e4SLinus Torvalds return -EFAULT; 26921da177e4SLinus Torvalds } 2693b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 26941da177e4SLinus Torvalds 26959c55e01cSJens Axboe /* 26969c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 26979c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 26989c55e01cSJens Axboe */ 26999c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 27009c55e01cSJens Axboe { 27018b9d3728SJarek Poplawski put_page(spd->pages[i]); 27028b9d3728SJarek Poplawski } 27039c55e01cSJens Axboe 2704a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 27054fb66994SJarek Poplawski unsigned int *offset, 270618aafc62SEric Dumazet struct sock *sk) 27078b9d3728SJarek Poplawski { 27085640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 27098b9d3728SJarek Poplawski 27105640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 27118b9d3728SJarek Poplawski return NULL; 27124fb66994SJarek Poplawski 27135640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 27144fb66994SJarek Poplawski 27155640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 27165640f768SEric Dumazet page_address(page) + *offset, *len); 27175640f768SEric Dumazet *offset = pfrag->offset; 27185640f768SEric Dumazet pfrag->offset += *len; 27194fb66994SJarek Poplawski 27205640f768SEric Dumazet return pfrag->page; 27219c55e01cSJens Axboe } 27229c55e01cSJens Axboe 272341c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 272441c73a0dSEric Dumazet struct page *page, 272541c73a0dSEric Dumazet unsigned int offset) 272641c73a0dSEric Dumazet { 272741c73a0dSEric Dumazet return spd->nr_pages && 272841c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 272941c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 273041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 273141c73a0dSEric Dumazet } 273241c73a0dSEric Dumazet 27339c55e01cSJens Axboe /* 27349c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 27359c55e01cSJens Axboe */ 2736a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 273735f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 27384fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 273918aafc62SEric Dumazet bool linear, 27407a67e56fSJarek Poplawski struct sock *sk) 27419c55e01cSJens Axboe { 274241c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2743a108d5f3SDavid S. Miller return true; 27449c55e01cSJens Axboe 27458b9d3728SJarek Poplawski if (linear) { 274618aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 27478b9d3728SJarek Poplawski if (!page) 2748a108d5f3SDavid S. Miller return true; 274941c73a0dSEric Dumazet } 275041c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 275141c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2752a108d5f3SDavid S. Miller return false; 275341c73a0dSEric Dumazet } 27548b9d3728SJarek Poplawski get_page(page); 27559c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 27564fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 27579c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 27589c55e01cSJens Axboe spd->nr_pages++; 27598b9d3728SJarek Poplawski 2760a108d5f3SDavid S. Miller return false; 27619c55e01cSJens Axboe } 27629c55e01cSJens Axboe 2763a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 27642870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 276518aafc62SEric Dumazet unsigned int *len, 2766d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 276735f3d14dSJens Axboe struct sock *sk, 276835f3d14dSJens Axboe struct pipe_inode_info *pipe) 27699c55e01cSJens Axboe { 27702870c43dSOctavian Purdila if (!*len) 2771a108d5f3SDavid S. Miller return true; 27729c55e01cSJens Axboe 27732870c43dSOctavian Purdila /* skip this segment if already processed */ 27742870c43dSOctavian Purdila if (*off >= plen) { 27752870c43dSOctavian Purdila *off -= plen; 2776a108d5f3SDavid S. Miller return false; 27772870c43dSOctavian Purdila } 27782870c43dSOctavian Purdila 27792870c43dSOctavian Purdila /* ignore any bits we already processed */ 27809ca1b22dSEric Dumazet poff += *off; 27819ca1b22dSEric Dumazet plen -= *off; 27822870c43dSOctavian Purdila *off = 0; 27832870c43dSOctavian Purdila 278418aafc62SEric Dumazet do { 278518aafc62SEric Dumazet unsigned int flen = min(*len, plen); 27862870c43dSOctavian Purdila 278718aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 278818aafc62SEric Dumazet linear, sk)) 2789a108d5f3SDavid S. Miller return true; 279018aafc62SEric Dumazet poff += flen; 279118aafc62SEric Dumazet plen -= flen; 27922870c43dSOctavian Purdila *len -= flen; 279318aafc62SEric Dumazet } while (*len && plen); 27942870c43dSOctavian Purdila 2795a108d5f3SDavid S. Miller return false; 2796db43a282SOctavian Purdila } 27979c55e01cSJens Axboe 27989c55e01cSJens Axboe /* 2799a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 28002870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 28019c55e01cSJens Axboe */ 2802a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 280335f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 280435f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 28052870c43dSOctavian Purdila { 28062870c43dSOctavian Purdila int seg; 2807fa9835e5STom Herbert struct sk_buff *iter; 28089c55e01cSJens Axboe 28091d0c0b32SEric Dumazet /* map the linear part : 28102996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 28112996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 28122996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 28139c55e01cSJens Axboe */ 28142870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 28152870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 28162870c43dSOctavian Purdila skb_headlen(skb), 281718aafc62SEric Dumazet offset, len, spd, 28183a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 28191d0c0b32SEric Dumazet sk, pipe)) 2820a108d5f3SDavid S. Miller return true; 28219c55e01cSJens Axboe 28229c55e01cSJens Axboe /* 28239c55e01cSJens Axboe * then map the fragments 28249c55e01cSJens Axboe */ 28259c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 28269c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 28279c55e01cSJens Axboe 2828ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2829b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 283018aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2831a108d5f3SDavid S. Miller return true; 28329c55e01cSJens Axboe } 28339c55e01cSJens Axboe 2834fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2835fa9835e5STom Herbert if (*offset >= iter->len) { 2836fa9835e5STom Herbert *offset -= iter->len; 2837fa9835e5STom Herbert continue; 2838fa9835e5STom Herbert } 2839fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2840fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2841fa9835e5STom Herbert * case. 2842fa9835e5STom Herbert */ 2843fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2844fa9835e5STom Herbert return true; 2845fa9835e5STom Herbert } 2846fa9835e5STom Herbert 2847a108d5f3SDavid S. Miller return false; 28489c55e01cSJens Axboe } 28499c55e01cSJens Axboe 28509c55e01cSJens Axboe /* 28519c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2852fa9835e5STom Herbert * the fragments, and the frag list. 28539c55e01cSJens Axboe */ 2854a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 28559c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 285625869262SAl Viro unsigned int flags) 28579c55e01cSJens Axboe { 285841c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 285941c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 28609c55e01cSJens Axboe struct splice_pipe_desc spd = { 28619c55e01cSJens Axboe .pages = pages, 28629c55e01cSJens Axboe .partial = partial, 2863047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 286428a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 28659c55e01cSJens Axboe .spd_release = sock_spd_release, 28669c55e01cSJens Axboe }; 286735f3d14dSJens Axboe int ret = 0; 286835f3d14dSJens Axboe 2869fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 28709c55e01cSJens Axboe 2871a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 287225869262SAl Viro ret = splice_to_pipe(pipe, &spd); 28739c55e01cSJens Axboe 287435f3d14dSJens Axboe return ret; 28759c55e01cSJens Axboe } 28762b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 28779c55e01cSJens Axboe 28780739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 28790739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 28800739cd28SCong Wang { 28810739cd28SCong Wang struct socket *sock = sk->sk_socket; 28820739cd28SCong Wang 28830739cd28SCong Wang if (!sock) 28840739cd28SCong Wang return -EINVAL; 28850739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 28860739cd28SCong Wang } 28870739cd28SCong Wang 28880739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 28890739cd28SCong Wang size_t size, int flags) 28900739cd28SCong Wang { 28910739cd28SCong Wang struct socket *sock = sk->sk_socket; 28920739cd28SCong Wang 28930739cd28SCong Wang if (!sock) 28940739cd28SCong Wang return -EINVAL; 28950739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 28960739cd28SCong Wang } 28970739cd28SCong Wang 28980739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 28990739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 29000739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 29010739cd28SCong Wang size_t size, int flags); 29020739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 29030739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 290420bf50deSTom Herbert { 290520bf50deSTom Herbert unsigned int orig_len = len; 290620bf50deSTom Herbert struct sk_buff *head = skb; 290720bf50deSTom Herbert unsigned short fragidx; 290820bf50deSTom Herbert int slen, ret; 290920bf50deSTom Herbert 291020bf50deSTom Herbert do_frag_list: 291120bf50deSTom Herbert 291220bf50deSTom Herbert /* Deal with head data */ 291320bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 291420bf50deSTom Herbert struct kvec kv; 291520bf50deSTom Herbert struct msghdr msg; 291620bf50deSTom Herbert 291720bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 291820bf50deSTom Herbert kv.iov_base = skb->data + offset; 2919db5980d8SJohn Fastabend kv.iov_len = slen; 292020bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2921bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 292220bf50deSTom Herbert 29230739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 29240739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 292520bf50deSTom Herbert if (ret <= 0) 292620bf50deSTom Herbert goto error; 292720bf50deSTom Herbert 292820bf50deSTom Herbert offset += ret; 292920bf50deSTom Herbert len -= ret; 293020bf50deSTom Herbert } 293120bf50deSTom Herbert 293220bf50deSTom Herbert /* All the data was skb head? */ 293320bf50deSTom Herbert if (!len) 293420bf50deSTom Herbert goto out; 293520bf50deSTom Herbert 293620bf50deSTom Herbert /* Make offset relative to start of frags */ 293720bf50deSTom Herbert offset -= skb_headlen(skb); 293820bf50deSTom Herbert 293920bf50deSTom Herbert /* Find where we are in frag list */ 294020bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 294120bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 294220bf50deSTom Herbert 2943d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 294420bf50deSTom Herbert break; 294520bf50deSTom Herbert 2946d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 294720bf50deSTom Herbert } 294820bf50deSTom Herbert 294920bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 295020bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 295120bf50deSTom Herbert 2952d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 295320bf50deSTom Herbert 295420bf50deSTom Herbert while (slen) { 29550739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 29560739cd28SCong Wang sendpage_unlocked, sk, 29570739cd28SCong Wang skb_frag_page(frag), 2958b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 295920bf50deSTom Herbert slen, MSG_DONTWAIT); 296020bf50deSTom Herbert if (ret <= 0) 296120bf50deSTom Herbert goto error; 296220bf50deSTom Herbert 296320bf50deSTom Herbert len -= ret; 296420bf50deSTom Herbert offset += ret; 296520bf50deSTom Herbert slen -= ret; 296620bf50deSTom Herbert } 296720bf50deSTom Herbert 296820bf50deSTom Herbert offset = 0; 296920bf50deSTom Herbert } 297020bf50deSTom Herbert 297120bf50deSTom Herbert if (len) { 297220bf50deSTom Herbert /* Process any frag lists */ 297320bf50deSTom Herbert 297420bf50deSTom Herbert if (skb == head) { 297520bf50deSTom Herbert if (skb_has_frag_list(skb)) { 297620bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 297720bf50deSTom Herbert goto do_frag_list; 297820bf50deSTom Herbert } 297920bf50deSTom Herbert } else if (skb->next) { 298020bf50deSTom Herbert skb = skb->next; 298120bf50deSTom Herbert goto do_frag_list; 298220bf50deSTom Herbert } 298320bf50deSTom Herbert } 298420bf50deSTom Herbert 298520bf50deSTom Herbert out: 298620bf50deSTom Herbert return orig_len - len; 298720bf50deSTom Herbert 298820bf50deSTom Herbert error: 298920bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 299020bf50deSTom Herbert } 29910739cd28SCong Wang 29920739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 29930739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 29940739cd28SCong Wang int len) 29950739cd28SCong Wang { 29960739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 29970739cd28SCong Wang kernel_sendpage_locked); 29980739cd28SCong Wang } 299920bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 300020bf50deSTom Herbert 30010739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 30020739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 30030739cd28SCong Wang { 30040739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 30050739cd28SCong Wang sendpage_unlocked); 30060739cd28SCong Wang } 30070739cd28SCong Wang 3008357b40a1SHerbert Xu /** 3009357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3010357b40a1SHerbert Xu * @skb: destination buffer 3011357b40a1SHerbert Xu * @offset: offset in destination 3012357b40a1SHerbert Xu * @from: source buffer 3013357b40a1SHerbert Xu * @len: number of bytes to copy 3014357b40a1SHerbert Xu * 3015357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3016357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3017357b40a1SHerbert Xu * traversing fragment lists and such. 3018357b40a1SHerbert Xu */ 3019357b40a1SHerbert Xu 30200c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3021357b40a1SHerbert Xu { 30221a028e50SDavid S. Miller int start = skb_headlen(skb); 3023fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3024fbb398a8SDavid S. Miller int i, copy; 3025357b40a1SHerbert Xu 3026357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3027357b40a1SHerbert Xu goto fault; 3028357b40a1SHerbert Xu 30291a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3030357b40a1SHerbert Xu if (copy > len) 3031357b40a1SHerbert Xu copy = len; 303227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3033357b40a1SHerbert Xu if ((len -= copy) == 0) 3034357b40a1SHerbert Xu return 0; 3035357b40a1SHerbert Xu offset += copy; 3036357b40a1SHerbert Xu from += copy; 3037357b40a1SHerbert Xu } 3038357b40a1SHerbert Xu 3039357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3040357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 30411a028e50SDavid S. Miller int end; 3042357b40a1SHerbert Xu 3043547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30441a028e50SDavid S. Miller 30459e903e08SEric Dumazet end = start + skb_frag_size(frag); 3046357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3047c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3048c613c209SWillem de Bruijn struct page *p; 3049357b40a1SHerbert Xu u8 *vaddr; 3050357b40a1SHerbert Xu 3051357b40a1SHerbert Xu if (copy > len) 3052357b40a1SHerbert Xu copy = len; 3053357b40a1SHerbert Xu 3054c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3055b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3056c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3057c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3058c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 305951c56b00SEric Dumazet kunmap_atomic(vaddr); 3060c613c209SWillem de Bruijn } 3061357b40a1SHerbert Xu 3062357b40a1SHerbert Xu if ((len -= copy) == 0) 3063357b40a1SHerbert Xu return 0; 3064357b40a1SHerbert Xu offset += copy; 3065357b40a1SHerbert Xu from += copy; 3066357b40a1SHerbert Xu } 30671a028e50SDavid S. Miller start = end; 3068357b40a1SHerbert Xu } 3069357b40a1SHerbert Xu 3070fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 30711a028e50SDavid S. Miller int end; 3072357b40a1SHerbert Xu 3073547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30741a028e50SDavid S. Miller 3075fbb398a8SDavid S. Miller end = start + frag_iter->len; 3076357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3077357b40a1SHerbert Xu if (copy > len) 3078357b40a1SHerbert Xu copy = len; 3079fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 30801a028e50SDavid S. Miller from, copy)) 3081357b40a1SHerbert Xu goto fault; 3082357b40a1SHerbert Xu if ((len -= copy) == 0) 3083357b40a1SHerbert Xu return 0; 3084357b40a1SHerbert Xu offset += copy; 3085357b40a1SHerbert Xu from += copy; 3086357b40a1SHerbert Xu } 30871a028e50SDavid S. Miller start = end; 3088357b40a1SHerbert Xu } 3089357b40a1SHerbert Xu if (!len) 3090357b40a1SHerbert Xu return 0; 3091357b40a1SHerbert Xu 3092357b40a1SHerbert Xu fault: 3093357b40a1SHerbert Xu return -EFAULT; 3094357b40a1SHerbert Xu } 3095357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3096357b40a1SHerbert Xu 30971da177e4SLinus Torvalds /* Checksum skb data. */ 30982817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 30992817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 31001da177e4SLinus Torvalds { 31011a028e50SDavid S. Miller int start = skb_headlen(skb); 31021a028e50SDavid S. Miller int i, copy = start - offset; 3103fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 31041da177e4SLinus Torvalds int pos = 0; 31051da177e4SLinus Torvalds 31061da177e4SLinus Torvalds /* Checksum header. */ 31071da177e4SLinus Torvalds if (copy > 0) { 31081da177e4SLinus Torvalds if (copy > len) 31091da177e4SLinus Torvalds copy = len; 31102544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 31112544af03SMatteo Croce skb->data + offset, copy, csum); 31121da177e4SLinus Torvalds if ((len -= copy) == 0) 31131da177e4SLinus Torvalds return csum; 31141da177e4SLinus Torvalds offset += copy; 31151da177e4SLinus Torvalds pos = copy; 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 31191a028e50SDavid S. Miller int end; 312051c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31211da177e4SLinus Torvalds 3122547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31231a028e50SDavid S. Miller 312451c56b00SEric Dumazet end = start + skb_frag_size(frag); 31251da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3126c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3127c613c209SWillem de Bruijn struct page *p; 312844bb9363SAl Viro __wsum csum2; 31291da177e4SLinus Torvalds u8 *vaddr; 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds if (copy > len) 31321da177e4SLinus Torvalds copy = len; 3133c613c209SWillem de Bruijn 3134c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3135b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3136c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3137c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 31382544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 31392544af03SMatteo Croce csum_partial_ext, 31402544af03SMatteo Croce vaddr + p_off, p_len, 0); 314151c56b00SEric Dumazet kunmap_atomic(vaddr); 31422544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 31432544af03SMatteo Croce csum_block_add_ext, csum, 31442544af03SMatteo Croce csum2, pos, p_len); 3145c613c209SWillem de Bruijn pos += p_len; 3146c613c209SWillem de Bruijn } 3147c613c209SWillem de Bruijn 31481da177e4SLinus Torvalds if (!(len -= copy)) 31491da177e4SLinus Torvalds return csum; 31501da177e4SLinus Torvalds offset += copy; 31511da177e4SLinus Torvalds } 31521a028e50SDavid S. Miller start = end; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 3155fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31561a028e50SDavid S. Miller int end; 31571da177e4SLinus Torvalds 3158547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31591a028e50SDavid S. Miller 3160fbb398a8SDavid S. Miller end = start + frag_iter->len; 31611da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 31625f92a738SAl Viro __wsum csum2; 31631da177e4SLinus Torvalds if (copy > len) 31641da177e4SLinus Torvalds copy = len; 31652817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 31662817a336SDaniel Borkmann copy, 0, ops); 31672544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 31682544af03SMatteo Croce csum, csum2, pos, copy); 31691da177e4SLinus Torvalds if ((len -= copy) == 0) 31701da177e4SLinus Torvalds return csum; 31711da177e4SLinus Torvalds offset += copy; 31721da177e4SLinus Torvalds pos += copy; 31731da177e4SLinus Torvalds } 31741a028e50SDavid S. Miller start = end; 31751da177e4SLinus Torvalds } 317609a62660SKris Katterjohn BUG_ON(len); 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds return csum; 31791da177e4SLinus Torvalds } 31802817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 31812817a336SDaniel Borkmann 31822817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 31832817a336SDaniel Borkmann int len, __wsum csum) 31842817a336SDaniel Borkmann { 31852817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3186cea80ea8SDaniel Borkmann .update = csum_partial_ext, 31872817a336SDaniel Borkmann .combine = csum_block_add_ext, 31882817a336SDaniel Borkmann }; 31892817a336SDaniel Borkmann 31902817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 31912817a336SDaniel Borkmann } 3192b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds /* Both of above in one bottle. */ 31951da177e4SLinus Torvalds 319681d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 31978d5930dfSAl Viro u8 *to, int len) 31981da177e4SLinus Torvalds { 31991a028e50SDavid S. Miller int start = skb_headlen(skb); 32001a028e50SDavid S. Miller int i, copy = start - offset; 3201fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 32021da177e4SLinus Torvalds int pos = 0; 32038d5930dfSAl Viro __wsum csum = 0; 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds /* Copy header. */ 32061da177e4SLinus Torvalds if (copy > 0) { 32071da177e4SLinus Torvalds if (copy > len) 32081da177e4SLinus Torvalds copy = len; 32091da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3210cc44c17bSAl Viro copy); 32111da177e4SLinus Torvalds if ((len -= copy) == 0) 32121da177e4SLinus Torvalds return csum; 32131da177e4SLinus Torvalds offset += copy; 32141da177e4SLinus Torvalds to += copy; 32151da177e4SLinus Torvalds pos = copy; 32161da177e4SLinus Torvalds } 32171da177e4SLinus Torvalds 32181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32191a028e50SDavid S. Miller int end; 32201da177e4SLinus Torvalds 3221547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32221a028e50SDavid S. Miller 32239e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 32241da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3225c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3226c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3227c613c209SWillem de Bruijn struct page *p; 32285084205fSAl Viro __wsum csum2; 32291da177e4SLinus Torvalds u8 *vaddr; 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds if (copy > len) 32321da177e4SLinus Torvalds copy = len; 3233c613c209SWillem de Bruijn 3234c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3235b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3236c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3237c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3238c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3239c613c209SWillem de Bruijn to + copied, 3240cc44c17bSAl Viro p_len); 324151c56b00SEric Dumazet kunmap_atomic(vaddr); 32421da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3243c613c209SWillem de Bruijn pos += p_len; 3244c613c209SWillem de Bruijn } 3245c613c209SWillem de Bruijn 32461da177e4SLinus Torvalds if (!(len -= copy)) 32471da177e4SLinus Torvalds return csum; 32481da177e4SLinus Torvalds offset += copy; 32491da177e4SLinus Torvalds to += copy; 32501da177e4SLinus Torvalds } 32511a028e50SDavid S. Miller start = end; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 3254fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 325581d77662SAl Viro __wsum csum2; 32561a028e50SDavid S. Miller int end; 32571da177e4SLinus Torvalds 3258547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32591a028e50SDavid S. Miller 3260fbb398a8SDavid S. Miller end = start + frag_iter->len; 32611da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32621da177e4SLinus Torvalds if (copy > len) 32631da177e4SLinus Torvalds copy = len; 3264fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 32651a028e50SDavid S. Miller offset - start, 32668d5930dfSAl Viro to, copy); 32671da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 32681da177e4SLinus Torvalds if ((len -= copy) == 0) 32691da177e4SLinus Torvalds return csum; 32701da177e4SLinus Torvalds offset += copy; 32711da177e4SLinus Torvalds to += copy; 32721da177e4SLinus Torvalds pos += copy; 32731da177e4SLinus Torvalds } 32741a028e50SDavid S. Miller start = end; 32751da177e4SLinus Torvalds } 327609a62660SKris Katterjohn BUG_ON(len); 32771da177e4SLinus Torvalds return csum; 32781da177e4SLinus Torvalds } 3279b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 32801da177e4SLinus Torvalds 328149f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 328249f8e832SCong Wang { 328349f8e832SCong Wang __sum16 sum; 328449f8e832SCong Wang 328549f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 328614641931SCong Wang /* See comments in __skb_checksum_complete(). */ 328749f8e832SCong Wang if (likely(!sum)) { 328849f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 328949f8e832SCong Wang !skb->csum_complete_sw) 32907fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 329149f8e832SCong Wang } 329249f8e832SCong Wang if (!skb_shared(skb)) 329349f8e832SCong Wang skb->csum_valid = !sum; 329449f8e832SCong Wang return sum; 329549f8e832SCong Wang } 329649f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 329749f8e832SCong Wang 329814641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 329914641931SCong Wang * which has been changed from the hardware checksum, for example, by 330014641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 330114641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 330214641931SCong Wang * 330314641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 330414641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 330514641931SCong Wang * shared. 330614641931SCong Wang */ 330749f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 330849f8e832SCong Wang { 330949f8e832SCong Wang __wsum csum; 331049f8e832SCong Wang __sum16 sum; 331149f8e832SCong Wang 331249f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 331349f8e832SCong Wang 331449f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 331514641931SCong Wang /* This check is inverted, because we already knew the hardware 331614641931SCong Wang * checksum is invalid before calling this function. So, if the 331714641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 331814641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 331914641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 332014641931SCong Wang * when moving skb->data around. 332114641931SCong Wang */ 332249f8e832SCong Wang if (likely(!sum)) { 332349f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 332449f8e832SCong Wang !skb->csum_complete_sw) 33257fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 332649f8e832SCong Wang } 332749f8e832SCong Wang 332849f8e832SCong Wang if (!skb_shared(skb)) { 332949f8e832SCong Wang /* Save full packet checksum */ 333049f8e832SCong Wang skb->csum = csum; 333149f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 333249f8e832SCong Wang skb->csum_complete_sw = 1; 333349f8e832SCong Wang skb->csum_valid = !sum; 333449f8e832SCong Wang } 333549f8e832SCong Wang 333649f8e832SCong Wang return sum; 333749f8e832SCong Wang } 333849f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 333949f8e832SCong Wang 33409617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 33419617813dSDavide Caratti { 33429617813dSDavide Caratti net_warn_ratelimited( 33439617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 33449617813dSDavide Caratti __func__); 33459617813dSDavide Caratti return 0; 33469617813dSDavide Caratti } 33479617813dSDavide Caratti 33489617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 33499617813dSDavide Caratti int offset, int len) 33509617813dSDavide Caratti { 33519617813dSDavide Caratti net_warn_ratelimited( 33529617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 33539617813dSDavide Caratti __func__); 33549617813dSDavide Caratti return 0; 33559617813dSDavide Caratti } 33569617813dSDavide Caratti 33579617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 33589617813dSDavide Caratti .update = warn_crc32c_csum_update, 33599617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 33609617813dSDavide Caratti }; 33619617813dSDavide Caratti 33629617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 33639617813dSDavide Caratti &default_crc32c_ops; 33649617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 33659617813dSDavide Caratti 3366af2806f8SThomas Graf /** 3367af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3368af2806f8SThomas Graf * @from: source buffer 3369af2806f8SThomas Graf * 3370af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3371af2806f8SThomas Graf * into skb_zerocopy(). 3372af2806f8SThomas Graf */ 3373af2806f8SThomas Graf unsigned int 3374af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3375af2806f8SThomas Graf { 3376af2806f8SThomas Graf unsigned int hlen = 0; 3377af2806f8SThomas Graf 3378af2806f8SThomas Graf if (!from->head_frag || 3379af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3380a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3381af2806f8SThomas Graf hlen = skb_headlen(from); 3382a17ad096SPravin B Shelar if (!hlen) 3383a17ad096SPravin B Shelar hlen = from->len; 3384a17ad096SPravin B Shelar } 3385af2806f8SThomas Graf 3386af2806f8SThomas Graf if (skb_has_frag_list(from)) 3387af2806f8SThomas Graf hlen = from->len; 3388af2806f8SThomas Graf 3389af2806f8SThomas Graf return hlen; 3390af2806f8SThomas Graf } 3391af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3392af2806f8SThomas Graf 3393af2806f8SThomas Graf /** 3394af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3395af2806f8SThomas Graf * @to: destination buffer 33967fceb4deSMasanari Iida * @from: source buffer 3397af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3398af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3399af2806f8SThomas Graf * 3400af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3401af2806f8SThomas Graf * to the frags in the source buffer. 3402af2806f8SThomas Graf * 3403af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3404af2806f8SThomas Graf * headroom in the `to` buffer. 340536d5fe6aSZoltan Kiss * 340636d5fe6aSZoltan Kiss * Return value: 340736d5fe6aSZoltan Kiss * 0: everything is OK 340836d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 340936d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3410af2806f8SThomas Graf */ 341136d5fe6aSZoltan Kiss int 341236d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3413af2806f8SThomas Graf { 3414af2806f8SThomas Graf int i, j = 0; 3415af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 341636d5fe6aSZoltan Kiss int ret; 3417af2806f8SThomas Graf struct page *page; 3418af2806f8SThomas Graf unsigned int offset; 3419af2806f8SThomas Graf 3420af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3421af2806f8SThomas Graf 3422af2806f8SThomas Graf /* dont bother with small payloads */ 342336d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 342436d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3425af2806f8SThomas Graf 3426af2806f8SThomas Graf if (hlen) { 342736d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 342836d5fe6aSZoltan Kiss if (unlikely(ret)) 342936d5fe6aSZoltan Kiss return ret; 3430af2806f8SThomas Graf len -= hlen; 3431af2806f8SThomas Graf } else { 3432af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3433af2806f8SThomas Graf if (plen) { 3434af2806f8SThomas Graf page = virt_to_head_page(from->head); 3435af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3436af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3437af2806f8SThomas Graf get_page(page); 3438af2806f8SThomas Graf j = 1; 3439af2806f8SThomas Graf len -= plen; 3440af2806f8SThomas Graf } 3441af2806f8SThomas Graf } 3442af2806f8SThomas Graf 3443ede57d58SRichard Gobert skb_len_add(to, len + plen); 3444af2806f8SThomas Graf 344536d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 344636d5fe6aSZoltan Kiss skb_tx_error(from); 344736d5fe6aSZoltan Kiss return -ENOMEM; 344836d5fe6aSZoltan Kiss } 34491f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 345036d5fe6aSZoltan Kiss 3451af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3452d8e18a51SMatthew Wilcox (Oracle) int size; 3453d8e18a51SMatthew Wilcox (Oracle) 3454af2806f8SThomas Graf if (!len) 3455af2806f8SThomas Graf break; 3456af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3457d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3458d8e18a51SMatthew Wilcox (Oracle) len); 3459d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3460d8e18a51SMatthew Wilcox (Oracle) len -= size; 3461af2806f8SThomas Graf skb_frag_ref(to, j); 3462af2806f8SThomas Graf j++; 3463af2806f8SThomas Graf } 3464af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 346536d5fe6aSZoltan Kiss 346636d5fe6aSZoltan Kiss return 0; 3467af2806f8SThomas Graf } 3468af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3469af2806f8SThomas Graf 34701da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 34711da177e4SLinus Torvalds { 3472d3bc23e7SAl Viro __wsum csum; 34731da177e4SLinus Torvalds long csstart; 34741da177e4SLinus Torvalds 347584fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 347655508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 34771da177e4SLinus Torvalds else 34781da177e4SLinus Torvalds csstart = skb_headlen(skb); 34791da177e4SLinus Torvalds 348009a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 34811da177e4SLinus Torvalds 3482d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds csum = 0; 34851da177e4SLinus Torvalds if (csstart != skb->len) 34861da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 34878d5930dfSAl Viro skb->len - csstart); 34881da177e4SLinus Torvalds 348984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3490ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 34911da177e4SLinus Torvalds 3492d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds } 3495b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 34961da177e4SLinus Torvalds 34971da177e4SLinus Torvalds /** 34981da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 34991da177e4SLinus Torvalds * @list: list to dequeue from 35001da177e4SLinus Torvalds * 35011da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 35021da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 35031da177e4SLinus Torvalds * returned or %NULL if the list is empty. 35041da177e4SLinus Torvalds */ 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 35071da177e4SLinus Torvalds { 35081da177e4SLinus Torvalds unsigned long flags; 35091da177e4SLinus Torvalds struct sk_buff *result; 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35121da177e4SLinus Torvalds result = __skb_dequeue(list); 35131da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35141da177e4SLinus Torvalds return result; 35151da177e4SLinus Torvalds } 3516b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds /** 35191da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 35201da177e4SLinus Torvalds * @list: list to dequeue from 35211da177e4SLinus Torvalds * 35221da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 35231da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 35241da177e4SLinus Torvalds * returned or %NULL if the list is empty. 35251da177e4SLinus Torvalds */ 35261da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 35271da177e4SLinus Torvalds { 35281da177e4SLinus Torvalds unsigned long flags; 35291da177e4SLinus Torvalds struct sk_buff *result; 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35321da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 35331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35341da177e4SLinus Torvalds return result; 35351da177e4SLinus Torvalds } 3536b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds /** 35391da177e4SLinus Torvalds * skb_queue_purge - empty a list 35401da177e4SLinus Torvalds * @list: list to empty 35411da177e4SLinus Torvalds * 35421da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 35431da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 35441da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 35451da177e4SLinus Torvalds */ 35461da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 35471da177e4SLinus Torvalds { 35481da177e4SLinus Torvalds struct sk_buff *skb; 35491da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 35501da177e4SLinus Torvalds kfree_skb(skb); 35511da177e4SLinus Torvalds } 3552b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 35531da177e4SLinus Torvalds 35541da177e4SLinus Torvalds /** 35559f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 35569f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3557385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 35589f5afeaeSYaogong Wang * 35599f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 35609f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 35619f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 35629f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 35639f5afeaeSYaogong Wang */ 3564385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 35659f5afeaeSYaogong Wang { 35667c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3567385114deSPeter Oskolkov unsigned int sum = 0; 35689f5afeaeSYaogong Wang 35697c90584cSEric Dumazet while (p) { 35707c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 35717c90584cSEric Dumazet 35727c90584cSEric Dumazet p = rb_next(p); 35737c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3574385114deSPeter Oskolkov sum += skb->truesize; 35759f5afeaeSYaogong Wang kfree_skb(skb); 35767c90584cSEric Dumazet } 3577385114deSPeter Oskolkov return sum; 35789f5afeaeSYaogong Wang } 35799f5afeaeSYaogong Wang 35809f5afeaeSYaogong Wang /** 35811da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 35821da177e4SLinus Torvalds * @list: list to use 35831da177e4SLinus Torvalds * @newsk: buffer to queue 35841da177e4SLinus Torvalds * 35851da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 35861da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 35871da177e4SLinus Torvalds * safely. 35881da177e4SLinus Torvalds * 35891da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 35901da177e4SLinus Torvalds */ 35911da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 35921da177e4SLinus Torvalds { 35931da177e4SLinus Torvalds unsigned long flags; 35941da177e4SLinus Torvalds 35951da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35961da177e4SLinus Torvalds __skb_queue_head(list, newsk); 35971da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35981da177e4SLinus Torvalds } 3599b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 36001da177e4SLinus Torvalds 36011da177e4SLinus Torvalds /** 36021da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 36031da177e4SLinus Torvalds * @list: list to use 36041da177e4SLinus Torvalds * @newsk: buffer to queue 36051da177e4SLinus Torvalds * 36061da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 36071da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 36081da177e4SLinus Torvalds * safely. 36091da177e4SLinus Torvalds * 36101da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 36111da177e4SLinus Torvalds */ 36121da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 36131da177e4SLinus Torvalds { 36141da177e4SLinus Torvalds unsigned long flags; 36151da177e4SLinus Torvalds 36161da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36171da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 36181da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36191da177e4SLinus Torvalds } 3620b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 36218728b834SDavid S. Miller 36221da177e4SLinus Torvalds /** 36231da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 36241da177e4SLinus Torvalds * @skb: buffer to remove 36258728b834SDavid S. Miller * @list: list to use 36261da177e4SLinus Torvalds * 36278728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 36288728b834SDavid S. Miller * function is atomic with respect to other list locked calls 36291da177e4SLinus Torvalds * 36308728b834SDavid S. Miller * You must know what list the SKB is on. 36311da177e4SLinus Torvalds */ 36328728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 36331da177e4SLinus Torvalds { 36341da177e4SLinus Torvalds unsigned long flags; 36351da177e4SLinus Torvalds 36361da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36378728b834SDavid S. Miller __skb_unlink(skb, list); 36381da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36391da177e4SLinus Torvalds } 3640b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 36411da177e4SLinus Torvalds 36421da177e4SLinus Torvalds /** 36431da177e4SLinus Torvalds * skb_append - append a buffer 36441da177e4SLinus Torvalds * @old: buffer to insert after 36451da177e4SLinus Torvalds * @newsk: buffer to insert 36468728b834SDavid S. Miller * @list: list to use 36471da177e4SLinus Torvalds * 36481da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 36491da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 36501da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 36511da177e4SLinus Torvalds */ 36528728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 36531da177e4SLinus Torvalds { 36541da177e4SLinus Torvalds unsigned long flags; 36551da177e4SLinus Torvalds 36568728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 36577de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 36588728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 36591da177e4SLinus Torvalds } 3660b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 36631da177e4SLinus Torvalds struct sk_buff* skb1, 36641da177e4SLinus Torvalds const u32 len, const int pos) 36651da177e4SLinus Torvalds { 36661da177e4SLinus Torvalds int i; 36671da177e4SLinus Torvalds 3668d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3669d626f62bSArnaldo Carvalho de Melo pos - len); 36701da177e4SLinus Torvalds /* And move data appendix as is. */ 36711da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 36721da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 36751da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 36761da177e4SLinus Torvalds skb1->data_len = skb->data_len; 36771da177e4SLinus Torvalds skb1->len += skb1->data_len; 36781da177e4SLinus Torvalds skb->data_len = 0; 36791da177e4SLinus Torvalds skb->len = len; 368027a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 36811da177e4SLinus Torvalds } 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 36841da177e4SLinus Torvalds struct sk_buff* skb1, 36851da177e4SLinus Torvalds const u32 len, int pos) 36861da177e4SLinus Torvalds { 36871da177e4SLinus Torvalds int i, k = 0; 36881da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 36891da177e4SLinus Torvalds 36901da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 36911da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 36921da177e4SLinus Torvalds skb->len = len; 36931da177e4SLinus Torvalds skb->data_len = len - pos; 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 36969e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 36971da177e4SLinus Torvalds 36981da177e4SLinus Torvalds if (pos + size > len) { 36991da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 37001da177e4SLinus Torvalds 37011da177e4SLinus Torvalds if (pos < len) { 37021da177e4SLinus Torvalds /* Split frag. 37031da177e4SLinus Torvalds * We have two variants in this case: 37041da177e4SLinus Torvalds * 1. Move all the frag to the second 37051da177e4SLinus Torvalds * part, if it is possible. F.e. 37061da177e4SLinus Torvalds * this approach is mandatory for TUX, 37071da177e4SLinus Torvalds * where splitting is expensive. 37081da177e4SLinus Torvalds * 2. Split is accurately. We make this. 37091da177e4SLinus Torvalds */ 3710ea2ab693SIan Campbell skb_frag_ref(skb, i); 3711b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 37129e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 37139e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 37141da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 37151da177e4SLinus Torvalds } 37161da177e4SLinus Torvalds k++; 37171da177e4SLinus Torvalds } else 37181da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 37191da177e4SLinus Torvalds pos += size; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 37221da177e4SLinus Torvalds } 37231da177e4SLinus Torvalds 37241da177e4SLinus Torvalds /** 37251da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 37261da177e4SLinus Torvalds * @skb: the buffer to split 37271da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 37281da177e4SLinus Torvalds * @len: new length for skb 37291da177e4SLinus Torvalds */ 37301da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 37311da177e4SLinus Torvalds { 37321da177e4SLinus Torvalds int pos = skb_headlen(skb); 37339b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 37341da177e4SLinus Torvalds 3735753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3736753f1ca4SPavel Begunkov 37379b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 37381f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 37391da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 37401da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 37411da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 37421da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 37431da177e4SLinus Torvalds } 3744b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 37451da177e4SLinus Torvalds 37469f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 37479f782db3SIlpo Järvinen * 37489f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 37499f782db3SIlpo Järvinen */ 3750832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3751832d11c5SIlpo Järvinen { 3752c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3753832d11c5SIlpo Järvinen } 3754832d11c5SIlpo Järvinen 3755832d11c5SIlpo Järvinen /** 3756832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3757832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3758832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3759832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3760832d11c5SIlpo Järvinen * 3761832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 376220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3763832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3764832d11c5SIlpo Järvinen * 3765832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3766832d11c5SIlpo Järvinen * 3767832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3768832d11c5SIlpo Järvinen * to have non-paged data as well. 3769832d11c5SIlpo Järvinen * 3770832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3771832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3772832d11c5SIlpo Järvinen */ 3773832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3774832d11c5SIlpo Järvinen { 3775832d11c5SIlpo Järvinen int from, to, merge, todo; 3776d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3777832d11c5SIlpo Järvinen 3778832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3779f8071cdeSEric Dumazet 3780f8071cdeSEric Dumazet if (skb_headlen(skb)) 3781f8071cdeSEric Dumazet return 0; 37821f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 37831f8b977aSWillem de Bruijn return 0; 3784832d11c5SIlpo Järvinen 3785832d11c5SIlpo Järvinen todo = shiftlen; 3786832d11c5SIlpo Järvinen from = 0; 3787832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3788832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3789832d11c5SIlpo Järvinen 3790832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3791832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3792832d11c5SIlpo Järvinen */ 3793832d11c5SIlpo Järvinen if (!to || 3794ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3795b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3796832d11c5SIlpo Järvinen merge = -1; 3797832d11c5SIlpo Järvinen } else { 3798832d11c5SIlpo Järvinen merge = to - 1; 3799832d11c5SIlpo Järvinen 38009e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3801832d11c5SIlpo Järvinen if (todo < 0) { 3802832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3803832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3804832d11c5SIlpo Järvinen return 0; 3805832d11c5SIlpo Järvinen 38069f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 38079f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3808832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3809832d11c5SIlpo Järvinen 38109e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 38119e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3812b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3813832d11c5SIlpo Järvinen 3814832d11c5SIlpo Järvinen goto onlymerged; 3815832d11c5SIlpo Järvinen } 3816832d11c5SIlpo Järvinen 3817832d11c5SIlpo Järvinen from++; 3818832d11c5SIlpo Järvinen } 3819832d11c5SIlpo Järvinen 3820832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3821832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3822832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3823832d11c5SIlpo Järvinen return 0; 3824832d11c5SIlpo Järvinen 3825832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3826832d11c5SIlpo Järvinen return 0; 3827832d11c5SIlpo Järvinen 3828832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3829832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3830832d11c5SIlpo Järvinen return 0; 3831832d11c5SIlpo Järvinen 3832832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3833832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3834832d11c5SIlpo Järvinen 38359e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3836832d11c5SIlpo Järvinen *fragto = *fragfrom; 38379e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3838832d11c5SIlpo Järvinen from++; 3839832d11c5SIlpo Järvinen to++; 3840832d11c5SIlpo Järvinen 3841832d11c5SIlpo Järvinen } else { 3842ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3843b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3844b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 38459e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3846832d11c5SIlpo Järvinen 3847b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 38489e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3849832d11c5SIlpo Järvinen todo = 0; 3850832d11c5SIlpo Järvinen 3851832d11c5SIlpo Järvinen to++; 3852832d11c5SIlpo Järvinen break; 3853832d11c5SIlpo Järvinen } 3854832d11c5SIlpo Järvinen } 3855832d11c5SIlpo Järvinen 3856832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3857832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3858832d11c5SIlpo Järvinen 3859832d11c5SIlpo Järvinen if (merge >= 0) { 3860832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3861832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3862832d11c5SIlpo Järvinen 38639e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 38646a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3865832d11c5SIlpo Järvinen } 3866832d11c5SIlpo Järvinen 3867832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3868832d11c5SIlpo Järvinen to = 0; 3869832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3870832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3871832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3872832d11c5SIlpo Järvinen 3873832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3874832d11c5SIlpo Järvinen 3875832d11c5SIlpo Järvinen onlymerged: 3876832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3877832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3878832d11c5SIlpo Järvinen */ 3879832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3880832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3881832d11c5SIlpo Järvinen 3882ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 3883ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 3884832d11c5SIlpo Järvinen 3885832d11c5SIlpo Järvinen return shiftlen; 3886832d11c5SIlpo Järvinen } 3887832d11c5SIlpo Järvinen 3888677e90edSThomas Graf /** 3889677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3890677e90edSThomas Graf * @skb: the buffer to read 3891677e90edSThomas Graf * @from: lower offset of data to be read 3892677e90edSThomas Graf * @to: upper offset of data to be read 3893677e90edSThomas Graf * @st: state variable 3894677e90edSThomas Graf * 3895677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3896677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3897677e90edSThomas Graf */ 3898677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3899677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3900677e90edSThomas Graf { 3901677e90edSThomas Graf st->lower_offset = from; 3902677e90edSThomas Graf st->upper_offset = to; 3903677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3904677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3905677e90edSThomas Graf st->frag_data = NULL; 390697550f6fSWillem de Bruijn st->frag_off = 0; 3907677e90edSThomas Graf } 3908b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3909677e90edSThomas Graf 3910677e90edSThomas Graf /** 3911677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3912677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3913677e90edSThomas Graf * @data: destination pointer for data to be returned 3914677e90edSThomas Graf * @st: state variable 3915677e90edSThomas Graf * 3916bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3917677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3918bc32383cSMathias Krause * the head of the data block to @data and returns the length 3919677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3920677e90edSThomas Graf * offset has been reached. 3921677e90edSThomas Graf * 3922677e90edSThomas Graf * The caller is not required to consume all of the data 3923bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3924677e90edSThomas Graf * of bytes already consumed and the next call to 3925677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3926677e90edSThomas Graf * 392725985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3928e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3929677e90edSThomas Graf * reads of potentially non linear data. 3930677e90edSThomas Graf * 3931bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3932677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3933677e90edSThomas Graf * a stack for this purpose. 3934677e90edSThomas Graf */ 3935677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3936677e90edSThomas Graf struct skb_seq_state *st) 3937677e90edSThomas Graf { 3938677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3939677e90edSThomas Graf skb_frag_t *frag; 3940677e90edSThomas Graf 3941aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3942aeb193eaSWedson Almeida Filho if (st->frag_data) { 3943aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3944aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3945aeb193eaSWedson Almeida Filho } 3946677e90edSThomas Graf return 0; 3947aeb193eaSWedson Almeida Filho } 3948677e90edSThomas Graf 3949677e90edSThomas Graf next_skb: 395095e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3951677e90edSThomas Graf 3952995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 395395e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3954677e90edSThomas Graf return block_limit - abs_offset; 3955677e90edSThomas Graf } 3956677e90edSThomas Graf 3957677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3958677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3959677e90edSThomas Graf 3960677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 396197550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 3962677e90edSThomas Graf 396397550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 396497550f6fSWillem de Bruijn 396597550f6fSWillem de Bruijn pg_idx = 0; 396697550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 396797550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 396897550f6fSWillem de Bruijn 396997550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 397097550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 397197550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 397297550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 397397550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 397497550f6fSWillem de Bruijn } 397597550f6fSWillem de Bruijn 397697550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 3977677e90edSThomas Graf if (abs_offset < block_limit) { 3978677e90edSThomas Graf if (!st->frag_data) 397997550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3980677e90edSThomas Graf 398197550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 3982677e90edSThomas Graf (abs_offset - st->stepped_offset); 3983677e90edSThomas Graf 3984677e90edSThomas Graf return block_limit - abs_offset; 3985677e90edSThomas Graf } 3986677e90edSThomas Graf 3987677e90edSThomas Graf if (st->frag_data) { 398851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3989677e90edSThomas Graf st->frag_data = NULL; 3990677e90edSThomas Graf } 3991677e90edSThomas Graf 399297550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 399397550f6fSWillem de Bruijn st->frag_off += pg_sz; 399497550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 399597550f6fSWillem de Bruijn st->frag_off = 0; 3996677e90edSThomas Graf st->frag_idx++; 399797550f6fSWillem de Bruijn } 3998677e90edSThomas Graf } 3999677e90edSThomas Graf 40005b5a60daSOlaf Kirch if (st->frag_data) { 400151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 40025b5a60daSOlaf Kirch st->frag_data = NULL; 40035b5a60daSOlaf Kirch } 40045b5a60daSOlaf Kirch 400521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4006677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 400795e3b24cSHerbert Xu st->frag_idx = 0; 4008677e90edSThomas Graf goto next_skb; 400971b3346dSShyam Iyer } else if (st->cur_skb->next) { 401071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 401171b3346dSShyam Iyer st->frag_idx = 0; 4012677e90edSThomas Graf goto next_skb; 4013677e90edSThomas Graf } 4014677e90edSThomas Graf 4015677e90edSThomas Graf return 0; 4016677e90edSThomas Graf } 4017b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4018677e90edSThomas Graf 4019677e90edSThomas Graf /** 4020677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4021677e90edSThomas Graf * @st: state variable 4022677e90edSThomas Graf * 4023677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4024677e90edSThomas Graf * returned 0. 4025677e90edSThomas Graf */ 4026677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4027677e90edSThomas Graf { 4028677e90edSThomas Graf if (st->frag_data) 402951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4030677e90edSThomas Graf } 4031b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4032677e90edSThomas Graf 40333fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 40343fc7e8a6SThomas Graf 40353fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 40363fc7e8a6SThomas Graf struct ts_config *conf, 40373fc7e8a6SThomas Graf struct ts_state *state) 40383fc7e8a6SThomas Graf { 40393fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 40403fc7e8a6SThomas Graf } 40413fc7e8a6SThomas Graf 40423fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 40433fc7e8a6SThomas Graf { 40443fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 40453fc7e8a6SThomas Graf } 40463fc7e8a6SThomas Graf 40473fc7e8a6SThomas Graf /** 40483fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 40493fc7e8a6SThomas Graf * @skb: the buffer to look in 40503fc7e8a6SThomas Graf * @from: search offset 40513fc7e8a6SThomas Graf * @to: search limit 40523fc7e8a6SThomas Graf * @config: textsearch configuration 40533fc7e8a6SThomas Graf * 40543fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 40553fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 40563fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 40573fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 40583fc7e8a6SThomas Graf */ 40593fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4060059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 40613fc7e8a6SThomas Graf { 4062059a2440SBojan Prtvar struct ts_state state; 4063f72b948dSPhil Oester unsigned int ret; 4064f72b948dSPhil Oester 4065b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4066b228c9b0SWillem de Bruijn 40673fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 40683fc7e8a6SThomas Graf config->finish = skb_ts_finish; 40693fc7e8a6SThomas Graf 4070059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 40713fc7e8a6SThomas Graf 4072059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4073f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 40743fc7e8a6SThomas Graf } 4075b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 40763fc7e8a6SThomas Graf 4077be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4078be12a1feSHannes Frederic Sowa int offset, size_t size) 4079be12a1feSHannes Frederic Sowa { 4080be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4081be12a1feSHannes Frederic Sowa 4082be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4083be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4084be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 4085753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4086be12a1feSHannes Frederic Sowa get_page(page); 4087228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4088be12a1feSHannes Frederic Sowa } else { 4089be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4090be12a1feSHannes Frederic Sowa } 4091be12a1feSHannes Frederic Sowa 4092be12a1feSHannes Frederic Sowa return 0; 4093be12a1feSHannes Frederic Sowa } 4094be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4095be12a1feSHannes Frederic Sowa 4096cbb042f9SHerbert Xu /** 4097cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4098cbb042f9SHerbert Xu * @skb: buffer to update 4099cbb042f9SHerbert Xu * @len: length of data pulled 4100cbb042f9SHerbert Xu * 4101cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4102fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 410384fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 410484fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 410584fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4106cbb042f9SHerbert Xu */ 4107af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4108cbb042f9SHerbert Xu { 410931b33dfbSPravin B Shelar unsigned char *data = skb->data; 411031b33dfbSPravin B Shelar 4111cbb042f9SHerbert Xu BUG_ON(len > skb->len); 411231b33dfbSPravin B Shelar __skb_pull(skb, len); 411331b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 411431b33dfbSPravin B Shelar return skb->data; 4115cbb042f9SHerbert Xu } 4116f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4117f94691acSArnaldo Carvalho de Melo 411813acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 411913acc94eSYonghong Song { 412013acc94eSYonghong Song skb_frag_t head_frag; 412113acc94eSYonghong Song struct page *page; 412213acc94eSYonghong Song 412313acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4124d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 4125b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 4126b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 4127d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 412813acc94eSYonghong Song return head_frag; 412913acc94eSYonghong Song } 413013acc94eSYonghong Song 41313a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 41323a1296a3SSteffen Klassert netdev_features_t features, 41333a1296a3SSteffen Klassert unsigned int offset) 41343a1296a3SSteffen Klassert { 41353a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 41363a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 41373a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 41383a1296a3SSteffen Klassert unsigned int delta_len = 0; 41393a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 414053475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4141cf3ab8d4SLina Wang int len_diff, err; 41423a1296a3SSteffen Klassert 41433a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 41443a1296a3SSteffen Klassert 41453a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 41463a1296a3SSteffen Klassert 41473a1296a3SSteffen Klassert do { 41483a1296a3SSteffen Klassert nskb = list_skb; 41493a1296a3SSteffen Klassert list_skb = list_skb->next; 41503a1296a3SSteffen Klassert 415153475c5dSDongseok Yi err = 0; 4152224102deSlena wang delta_truesize += nskb->truesize; 415353475c5dSDongseok Yi if (skb_shared(nskb)) { 415453475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 415553475c5dSDongseok Yi if (tmp) { 415653475c5dSDongseok Yi consume_skb(nskb); 415753475c5dSDongseok Yi nskb = tmp; 415853475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 415953475c5dSDongseok Yi } else { 416053475c5dSDongseok Yi err = -ENOMEM; 416153475c5dSDongseok Yi } 416253475c5dSDongseok Yi } 416353475c5dSDongseok Yi 41643a1296a3SSteffen Klassert if (!tail) 41653a1296a3SSteffen Klassert skb->next = nskb; 41663a1296a3SSteffen Klassert else 41673a1296a3SSteffen Klassert tail->next = nskb; 41683a1296a3SSteffen Klassert 416953475c5dSDongseok Yi if (unlikely(err)) { 417053475c5dSDongseok Yi nskb->next = list_skb; 417153475c5dSDongseok Yi goto err_linearize; 417253475c5dSDongseok Yi } 417353475c5dSDongseok Yi 41743a1296a3SSteffen Klassert tail = nskb; 41753a1296a3SSteffen Klassert 41763a1296a3SSteffen Klassert delta_len += nskb->len; 41773a1296a3SSteffen Klassert 41783a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 41793a1296a3SSteffen Klassert 4180cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4181cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 41823a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 41833a1296a3SSteffen Klassert 41843a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4185cf3ab8d4SLina Wang nskb->transport_header += len_diff; 41863a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 41873a1296a3SSteffen Klassert nskb->data - tnl_hlen, 41883a1296a3SSteffen Klassert offset + tnl_hlen); 41893a1296a3SSteffen Klassert 41903a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 41913a1296a3SSteffen Klassert __skb_linearize(nskb)) 41923a1296a3SSteffen Klassert goto err_linearize; 41933a1296a3SSteffen Klassert 41943a1296a3SSteffen Klassert } while (list_skb); 41953a1296a3SSteffen Klassert 41963a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 41973a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 41983a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 41993a1296a3SSteffen Klassert 42003a1296a3SSteffen Klassert skb_gso_reset(skb); 42013a1296a3SSteffen Klassert 42023a1296a3SSteffen Klassert skb->prev = tail; 42033a1296a3SSteffen Klassert 42043a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 42053a1296a3SSteffen Klassert __skb_linearize(skb)) 42063a1296a3SSteffen Klassert goto err_linearize; 42073a1296a3SSteffen Klassert 42083a1296a3SSteffen Klassert skb_get(skb); 42093a1296a3SSteffen Klassert 42103a1296a3SSteffen Klassert return skb; 42113a1296a3SSteffen Klassert 42123a1296a3SSteffen Klassert err_linearize: 42133a1296a3SSteffen Klassert kfree_skb_list(skb->next); 42143a1296a3SSteffen Klassert skb->next = NULL; 42153a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 42163a1296a3SSteffen Klassert } 42173a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 42183a1296a3SSteffen Klassert 4219f4c50d99SHerbert Xu /** 4220f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4221df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4222576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4223f4c50d99SHerbert Xu * 4224f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 42254c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 42264c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4227f4c50d99SHerbert Xu */ 4228df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4229df5771ffSMichael S. Tsirkin netdev_features_t features) 4230f4c50d99SHerbert Xu { 4231f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4232f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 42331a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4234df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4235df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4236df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 42371fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4238f4c50d99SHerbert Xu unsigned int offset = doffset; 4239df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4240802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4241f4c50d99SHerbert Xu unsigned int headroom; 4242802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4243ec5f0615SPravin B Shelar __be16 proto; 424436c98382SAlexander Duyck bool csum, sg; 4245df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4246f4c50d99SHerbert Xu int err = -ENOMEM; 4247f4c50d99SHerbert Xu int i = 0; 4248f4c50d99SHerbert Xu int pos; 4249f4c50d99SHerbert Xu 42509e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 42519e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 42529e4b7a99SJiri Benc struct sk_buff *check_skb; 42539e4b7a99SJiri Benc 42549e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 42559e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 42569e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 42579e4b7a99SJiri Benc * a linear non head_frag item. 42583dcbdb13SShmulik Ladkani * 42599e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 42609e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 42619e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 42629e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 42639e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 42643dcbdb13SShmulik Ladkani */ 42653dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 42669e4b7a99SJiri Benc break; 42679e4b7a99SJiri Benc } 42689e4b7a99SJiri Benc } 42693dcbdb13SShmulik Ladkani } 42703dcbdb13SShmulik Ladkani 42715882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 42722f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4273ec5f0615SPravin B Shelar if (unlikely(!proto)) 4274ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4275ec5f0615SPravin B Shelar 427636c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4277f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 42787e2b10c1STom Herbert 427907b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 428007b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 428107b26c94SSteffen Klassert struct sk_buff *iter; 428243170c4eSIlan Tayari unsigned int frag_len; 428307b26c94SSteffen Klassert 428407b26c94SSteffen Klassert if (!list_skb || 428507b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 428607b26c94SSteffen Klassert goto normal; 428707b26c94SSteffen Klassert 428843170c4eSIlan Tayari /* If we get here then all the required 428943170c4eSIlan Tayari * GSO features except frag_list are supported. 429043170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 429143170c4eSIlan Tayari * with no frag_list. 429243170c4eSIlan Tayari * Currently we can do that only when the buffers don't 429343170c4eSIlan Tayari * have a linear part and all the buffers except 429443170c4eSIlan Tayari * the last are of the same length. 429507b26c94SSteffen Klassert */ 429643170c4eSIlan Tayari frag_len = list_skb->len; 429707b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 429843170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 429943170c4eSIlan Tayari goto normal; 4300eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 430107b26c94SSteffen Klassert goto normal; 430207b26c94SSteffen Klassert 430307b26c94SSteffen Klassert len -= iter->len; 430407b26c94SSteffen Klassert } 430543170c4eSIlan Tayari 430643170c4eSIlan Tayari if (len != frag_len) 430743170c4eSIlan Tayari goto normal; 430807b26c94SSteffen Klassert } 430907b26c94SSteffen Klassert 4310802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4311802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4312802ab55aSAlexander Duyck * now. 4313802ab55aSAlexander Duyck */ 4314802ab55aSAlexander Duyck partial_segs = len / mss; 4315d7fb5a80SAlexander Duyck if (partial_segs > 1) 4316802ab55aSAlexander Duyck mss *= partial_segs; 4317d7fb5a80SAlexander Duyck else 4318d7fb5a80SAlexander Duyck partial_segs = 0; 4319802ab55aSAlexander Duyck } 4320802ab55aSAlexander Duyck 432107b26c94SSteffen Klassert normal: 4322df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4323df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4324f4c50d99SHerbert Xu 4325f4c50d99SHerbert Xu do { 4326f4c50d99SHerbert Xu struct sk_buff *nskb; 43278cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4328c8884eddSHerbert Xu int hsize; 4329f4c50d99SHerbert Xu int size; 4330f4c50d99SHerbert Xu 43313953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 43323953c46cSMarcelo Ricardo Leitner len = list_skb->len; 43333953c46cSMarcelo Ricardo Leitner } else { 4334df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4335f4c50d99SHerbert Xu if (len > mss) 4336f4c50d99SHerbert Xu len = mss; 43373953c46cSMarcelo Ricardo Leitner } 4338f4c50d99SHerbert Xu 4339df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4340f4c50d99SHerbert Xu 4341dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 43421a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 43431a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 434489319d38SHerbert Xu 43459d8506ccSHerbert Xu i = 0; 43461a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 43471a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 43481fd819ecSMichael S. Tsirkin frag_skb = list_skb; 43491a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 43509d8506ccSHerbert Xu 43519d8506ccSHerbert Xu while (pos < offset + len) { 43529d8506ccSHerbert Xu BUG_ON(i >= nfrags); 43539d8506ccSHerbert Xu 43544e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 43559d8506ccSHerbert Xu if (pos + size > offset + len) 43569d8506ccSHerbert Xu break; 43579d8506ccSHerbert Xu 43589d8506ccSHerbert Xu i++; 43599d8506ccSHerbert Xu pos += size; 43604e1beba1SMichael S. Tsirkin frag++; 43619d8506ccSHerbert Xu } 43629d8506ccSHerbert Xu 43631a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 43641a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 436589319d38SHerbert Xu 4366f4c50d99SHerbert Xu if (unlikely(!nskb)) 4367f4c50d99SHerbert Xu goto err; 4368f4c50d99SHerbert Xu 43699d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 43709d8506ccSHerbert Xu kfree_skb(nskb); 43719d8506ccSHerbert Xu goto err; 43729d8506ccSHerbert Xu } 43739d8506ccSHerbert Xu 4374ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 437589319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 437689319d38SHerbert Xu kfree_skb(nskb); 437789319d38SHerbert Xu goto err; 437889319d38SHerbert Xu } 437989319d38SHerbert Xu 4380ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 438189319d38SHerbert Xu skb_release_head_state(nskb); 438289319d38SHerbert Xu __skb_push(nskb, doffset); 438389319d38SHerbert Xu } else { 438400b229f7SPaolo Abeni if (hsize < 0) 438500b229f7SPaolo Abeni hsize = 0; 4386dbd50f23SXin Long if (hsize > len || !sg) 4387dbd50f23SXin Long hsize = len; 4388dbd50f23SXin Long 4389c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4390df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4391c93bdd0eSMel Gorman NUMA_NO_NODE); 439289319d38SHerbert Xu 439389319d38SHerbert Xu if (unlikely(!nskb)) 439489319d38SHerbert Xu goto err; 439589319d38SHerbert Xu 439689319d38SHerbert Xu skb_reserve(nskb, headroom); 439789319d38SHerbert Xu __skb_put(nskb, doffset); 439889319d38SHerbert Xu } 439989319d38SHerbert Xu 4400f4c50d99SHerbert Xu if (segs) 4401f4c50d99SHerbert Xu tail->next = nskb; 4402f4c50d99SHerbert Xu else 4403f4c50d99SHerbert Xu segs = nskb; 4404f4c50d99SHerbert Xu tail = nskb; 4405f4c50d99SHerbert Xu 4406df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4407f4c50d99SHerbert Xu 4408030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4409fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 441068c33163SPravin B Shelar 4411df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 441268c33163SPravin B Shelar nskb->data - tnl_hlen, 441368c33163SPravin B Shelar doffset + tnl_hlen); 441489319d38SHerbert Xu 44159d8506ccSHerbert Xu if (nskb->len == len + doffset) 44161cdbcb79SSimon Horman goto perform_csum_check; 441789319d38SHerbert Xu 44187fbeffedSAlexander Duyck if (!sg) { 44191454c9faSYadu Kishore if (!csum) { 44207fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 44216f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 442276443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 442376443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 44241454c9faSYadu Kishore skb_put(nskb, 44251454c9faSYadu Kishore len), 44268d5930dfSAl Viro len); 44277e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4428de843723STom Herbert skb_headroom(nskb) + doffset; 44291454c9faSYadu Kishore } else { 4430c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4431c624c58eSlily goto err; 44321454c9faSYadu Kishore } 4433f4c50d99SHerbert Xu continue; 4434f4c50d99SHerbert Xu } 4435f4c50d99SHerbert Xu 44368cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4437f4c50d99SHerbert Xu 4438df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4439d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4440f4c50d99SHerbert Xu 444106b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 444206b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4443cef401deSEric Dumazet 4444bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4445bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4446bf5c25d6SWillem de Bruijn goto err; 4447bf5c25d6SWillem de Bruijn 44489d8506ccSHerbert Xu while (pos < offset + len) { 44499d8506ccSHerbert Xu if (i >= nfrags) { 44509d8506ccSHerbert Xu i = 0; 44511a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44521a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44531fd819ecSMichael S. Tsirkin frag_skb = list_skb; 445413acc94eSYonghong Song if (!skb_headlen(list_skb)) { 44559d8506ccSHerbert Xu BUG_ON(!nfrags); 445613acc94eSYonghong Song } else { 445713acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 44589d8506ccSHerbert Xu 445913acc94eSYonghong Song /* to make room for head_frag. */ 446013acc94eSYonghong Song i--; 446113acc94eSYonghong Song frag--; 446213acc94eSYonghong Song } 4463bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4464bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4465bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4466bf5c25d6SWillem de Bruijn goto err; 4467bf5c25d6SWillem de Bruijn 44681a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 44699d8506ccSHerbert Xu } 44709d8506ccSHerbert Xu 44719d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 44729d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 44739d8506ccSHerbert Xu net_warn_ratelimited( 44749d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 44759d8506ccSHerbert Xu pos, mss); 4476ff907a11SEric Dumazet err = -EINVAL; 44779d8506ccSHerbert Xu goto err; 44789d8506ccSHerbert Xu } 44799d8506ccSHerbert Xu 448013acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 44818cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 44828cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4483f4c50d99SHerbert Xu 4484f4c50d99SHerbert Xu if (pos < offset) { 4485b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 44868cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4487f4c50d99SHerbert Xu } 4488f4c50d99SHerbert Xu 448989319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4490f4c50d99SHerbert Xu 4491f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4492f4c50d99SHerbert Xu i++; 44934e1beba1SMichael S. Tsirkin frag++; 4494f4c50d99SHerbert Xu pos += size; 4495f4c50d99SHerbert Xu } else { 44968cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 449789319d38SHerbert Xu goto skip_fraglist; 4498f4c50d99SHerbert Xu } 4499f4c50d99SHerbert Xu 45008cb19905SMichael S. Tsirkin nskb_frag++; 4501f4c50d99SHerbert Xu } 4502f4c50d99SHerbert Xu 450389319d38SHerbert Xu skip_fraglist: 4504f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4505f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4506f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4507ec5f0615SPravin B Shelar 45081cdbcb79SSimon Horman perform_csum_check: 45097fbeffedSAlexander Duyck if (!csum) { 4510ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4511ff907a11SEric Dumazet __skb_linearize(nskb)) 4512ddff00d4SAlexander Duyck goto err; 4513ff907a11SEric Dumazet 45147fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4515ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 451676443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 451776443456SAlexander Duyck skb_checksum(nskb, doffset, 451876443456SAlexander Duyck nskb->len - doffset, 0); 45197e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 45207e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4521ec5f0615SPravin B Shelar } 4522df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4523f4c50d99SHerbert Xu 4524bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4525bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4526bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4527bec3cfdcSEric Dumazet */ 4528bec3cfdcSEric Dumazet segs->prev = tail; 4529432c856fSToshiaki Makita 4530802ab55aSAlexander Duyck if (partial_segs) { 453107b26c94SSteffen Klassert struct sk_buff *iter; 4532802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 453307b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4534802ab55aSAlexander Duyck 4535802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 453607b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4537802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4538802ab55aSAlexander Duyck 4539802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4540802ab55aSAlexander Duyck * our way back down the stack of protocols. 4541802ab55aSAlexander Duyck */ 454207b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 454307b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 454407b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 454507b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 454607b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 454707b26c94SSteffen Klassert } 454807b26c94SSteffen Klassert 454907b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 455007b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 455107b26c94SSteffen Klassert else if (tail != segs) 455207b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4553802ab55aSAlexander Duyck } 4554802ab55aSAlexander Duyck 4555432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4556432c856fSToshiaki Makita * using skb_set_owner_w(). 4557432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4558432c856fSToshiaki Makita */ 4559432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4560432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4561432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4562432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4563432c856fSToshiaki Makita } 4564f4c50d99SHerbert Xu return segs; 4565f4c50d99SHerbert Xu 4566f4c50d99SHerbert Xu err: 4567289dccbeSEric Dumazet kfree_skb_list(segs); 4568f4c50d99SHerbert Xu return ERR_PTR(err); 4569f4c50d99SHerbert Xu } 4570f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4571f4c50d99SHerbert Xu 4572df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4573df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4574df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4575df5042f4SFlorian Westphal 4576df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4577df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4578df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4579df5042f4SFlorian Westphal #endif 45804165079bSFlorian Westphal #ifdef CONFIG_XFRM 45814165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 45824165079bSFlorian Westphal #endif 458395a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 458495a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 458595a7233cSPaul Blakey #endif 45863ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 45873ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 45883ee17bc7SMat Martineau #endif 458978476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 459078476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 459178476d31SJeremy Kerr #endif 4592df5042f4SFlorian Westphal }; 4593df5042f4SFlorian Westphal 4594df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4595df5042f4SFlorian Westphal { 4596df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4597df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4598df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4599df5042f4SFlorian Westphal #endif 46004165079bSFlorian Westphal #ifdef CONFIG_XFRM 46014165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 46024165079bSFlorian Westphal #endif 460395a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 460495a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 460595a7233cSPaul Blakey #endif 46063ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 46073ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 46083ee17bc7SMat Martineau #endif 460978476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 461078476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 461178476d31SJeremy Kerr #endif 4612df5042f4SFlorian Westphal 0; 4613df5042f4SFlorian Westphal } 4614df5042f4SFlorian Westphal 4615df5042f4SFlorian Westphal static void skb_extensions_init(void) 4616df5042f4SFlorian Westphal { 4617df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4618df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4619df5042f4SFlorian Westphal 4620df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4621df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4622df5042f4SFlorian Westphal 0, 4623df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4624df5042f4SFlorian Westphal NULL); 4625df5042f4SFlorian Westphal } 4626df5042f4SFlorian Westphal #else 4627df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4628df5042f4SFlorian Westphal #endif 4629df5042f4SFlorian Westphal 46301da177e4SLinus Torvalds void __init skb_init(void) 46311da177e4SLinus Torvalds { 463279a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 46331da177e4SLinus Torvalds sizeof(struct sk_buff), 46341da177e4SLinus Torvalds 0, 4635e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 463679a8a642SKees Cook offsetof(struct sk_buff, cb), 463779a8a642SKees Cook sizeof_field(struct sk_buff, cb), 463820c2df83SPaul Mundt NULL); 4639d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4640d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4641d179cd12SDavid S. Miller 0, 4642e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 464320c2df83SPaul Mundt NULL); 4644df5042f4SFlorian Westphal skb_extensions_init(); 46451da177e4SLinus Torvalds } 46461da177e4SLinus Torvalds 464751c739d1SDavid S. Miller static int 464848a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 464948a1df65SJason A. Donenfeld unsigned int recursion_level) 4650716ea3a7SDavid Howells { 46511a028e50SDavid S. Miller int start = skb_headlen(skb); 46521a028e50SDavid S. Miller int i, copy = start - offset; 4653fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4654716ea3a7SDavid Howells int elt = 0; 4655716ea3a7SDavid Howells 465648a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 465748a1df65SJason A. Donenfeld return -EMSGSIZE; 465848a1df65SJason A. Donenfeld 4659716ea3a7SDavid Howells if (copy > 0) { 4660716ea3a7SDavid Howells if (copy > len) 4661716ea3a7SDavid Howells copy = len; 4662642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4663716ea3a7SDavid Howells elt++; 4664716ea3a7SDavid Howells if ((len -= copy) == 0) 4665716ea3a7SDavid Howells return elt; 4666716ea3a7SDavid Howells offset += copy; 4667716ea3a7SDavid Howells } 4668716ea3a7SDavid Howells 4669716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 46701a028e50SDavid S. Miller int end; 4671716ea3a7SDavid Howells 4672547b792cSIlpo Järvinen WARN_ON(start > offset + len); 46731a028e50SDavid S. Miller 46749e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4675716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4676716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 467748a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 467848a1df65SJason A. Donenfeld return -EMSGSIZE; 4679716ea3a7SDavid Howells 4680716ea3a7SDavid Howells if (copy > len) 4681716ea3a7SDavid Howells copy = len; 4682ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4683b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4684716ea3a7SDavid Howells elt++; 4685716ea3a7SDavid Howells if (!(len -= copy)) 4686716ea3a7SDavid Howells return elt; 4687716ea3a7SDavid Howells offset += copy; 4688716ea3a7SDavid Howells } 46891a028e50SDavid S. Miller start = end; 4690716ea3a7SDavid Howells } 4691716ea3a7SDavid Howells 4692fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 469348a1df65SJason A. Donenfeld int end, ret; 4694716ea3a7SDavid Howells 4695547b792cSIlpo Järvinen WARN_ON(start > offset + len); 46961a028e50SDavid S. Miller 4697fbb398a8SDavid S. Miller end = start + frag_iter->len; 4698716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 469948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 470048a1df65SJason A. Donenfeld return -EMSGSIZE; 470148a1df65SJason A. Donenfeld 4702716ea3a7SDavid Howells if (copy > len) 4703716ea3a7SDavid Howells copy = len; 470448a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 470548a1df65SJason A. Donenfeld copy, recursion_level + 1); 470648a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 470748a1df65SJason A. Donenfeld return ret; 470848a1df65SJason A. Donenfeld elt += ret; 4709716ea3a7SDavid Howells if ((len -= copy) == 0) 4710716ea3a7SDavid Howells return elt; 4711716ea3a7SDavid Howells offset += copy; 4712716ea3a7SDavid Howells } 47131a028e50SDavid S. Miller start = end; 4714716ea3a7SDavid Howells } 4715716ea3a7SDavid Howells BUG_ON(len); 4716716ea3a7SDavid Howells return elt; 4717716ea3a7SDavid Howells } 4718716ea3a7SDavid Howells 471948a1df65SJason A. Donenfeld /** 472048a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 472148a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 472248a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 472348a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 472448a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 472548a1df65SJason A. Donenfeld * 472648a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 472748a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 472848a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 472948a1df65SJason A. Donenfeld * could not fit. 473048a1df65SJason A. Donenfeld */ 473148a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 473248a1df65SJason A. Donenfeld { 473348a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 473448a1df65SJason A. Donenfeld 473548a1df65SJason A. Donenfeld if (nsg <= 0) 473648a1df65SJason A. Donenfeld return nsg; 473748a1df65SJason A. Donenfeld 473848a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 473948a1df65SJason A. Donenfeld 474048a1df65SJason A. Donenfeld return nsg; 474148a1df65SJason A. Donenfeld } 474248a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 474348a1df65SJason A. Donenfeld 474425a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 474525a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 474625a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 474725a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 474825a91d8dSFan Du * 474925a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 475025a91d8dSFan Du * 1. sg_init_table 475125a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 475225a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 475325a91d8dSFan Du * 475425a91d8dSFan Du * This is equivalent to: 475525a91d8dSFan Du * 1. sg_init_table 475625a91d8dSFan Du * 2. skb_to_sgvec(payload1) 475725a91d8dSFan Du * 3. sg_unmark_end 475825a91d8dSFan Du * 4. skb_to_sgvec(payload2) 475925a91d8dSFan Du * 476025a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 476125a91d8dSFan Du * is more preferable. 476225a91d8dSFan Du */ 476325a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 476425a91d8dSFan Du int offset, int len) 476525a91d8dSFan Du { 476648a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 476725a91d8dSFan Du } 476825a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 476925a91d8dSFan Du 477051c739d1SDavid S. Miller 477151c739d1SDavid S. Miller 4772716ea3a7SDavid Howells /** 4773716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4774716ea3a7SDavid Howells * @skb: The socket buffer to check. 4775716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4776716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4777716ea3a7SDavid Howells * 4778716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4779716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4780716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4781716ea3a7SDavid Howells * 4782716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4783716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4784716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4785716ea3a7SDavid Howells * 4786716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4787716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4788716ea3a7SDavid Howells */ 4789716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4790716ea3a7SDavid Howells { 4791716ea3a7SDavid Howells int copyflag; 4792716ea3a7SDavid Howells int elt; 4793716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4794716ea3a7SDavid Howells 4795716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4796716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4797716ea3a7SDavid Howells * at the moment even if they are anonymous). 4798716ea3a7SDavid Howells */ 4799716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4800c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4801716ea3a7SDavid Howells return -ENOMEM; 4802716ea3a7SDavid Howells 4803716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 480421dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4805716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4806716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4807716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4808716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4809716ea3a7SDavid Howells 4810716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4811716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4812716ea3a7SDavid Howells return -ENOMEM; 4813716ea3a7SDavid Howells 4814716ea3a7SDavid Howells /* Voila! */ 4815716ea3a7SDavid Howells *trailer = skb; 4816716ea3a7SDavid Howells return 1; 4817716ea3a7SDavid Howells } 4818716ea3a7SDavid Howells 4819716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4820716ea3a7SDavid Howells 4821716ea3a7SDavid Howells elt = 1; 4822716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4823716ea3a7SDavid Howells copyflag = 0; 4824716ea3a7SDavid Howells 4825716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4826716ea3a7SDavid Howells int ntail = 0; 4827716ea3a7SDavid Howells 4828716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4829716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4830716ea3a7SDavid Howells * after it. */ 4831716ea3a7SDavid Howells 4832716ea3a7SDavid Howells if (skb_shared(skb1)) 4833716ea3a7SDavid Howells copyflag = 1; 4834716ea3a7SDavid Howells 4835716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4836716ea3a7SDavid Howells 4837716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4838716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 483921dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4840716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4841716ea3a7SDavid Howells ntail = tailbits + 128; 4842716ea3a7SDavid Howells } 4843716ea3a7SDavid Howells 4844716ea3a7SDavid Howells if (copyflag || 4845716ea3a7SDavid Howells skb_cloned(skb1) || 4846716ea3a7SDavid Howells ntail || 4847716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 484821dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4849716ea3a7SDavid Howells struct sk_buff *skb2; 4850716ea3a7SDavid Howells 4851716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4852716ea3a7SDavid Howells if (ntail == 0) 4853716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4854716ea3a7SDavid Howells else 4855716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4856716ea3a7SDavid Howells skb_headroom(skb1), 4857716ea3a7SDavid Howells ntail, 4858716ea3a7SDavid Howells GFP_ATOMIC); 4859716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4860716ea3a7SDavid Howells return -ENOMEM; 4861716ea3a7SDavid Howells 4862716ea3a7SDavid Howells if (skb1->sk) 4863716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4864716ea3a7SDavid Howells 4865716ea3a7SDavid Howells /* Looking around. Are we still alive? 4866716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4867716ea3a7SDavid Howells 4868716ea3a7SDavid Howells skb2->next = skb1->next; 4869716ea3a7SDavid Howells *skb_p = skb2; 4870716ea3a7SDavid Howells kfree_skb(skb1); 4871716ea3a7SDavid Howells skb1 = skb2; 4872716ea3a7SDavid Howells } 4873716ea3a7SDavid Howells elt++; 4874716ea3a7SDavid Howells *trailer = skb1; 4875716ea3a7SDavid Howells skb_p = &skb1->next; 4876716ea3a7SDavid Howells } 4877716ea3a7SDavid Howells 4878716ea3a7SDavid Howells return elt; 4879716ea3a7SDavid Howells } 4880b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4881716ea3a7SDavid Howells 4882b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4883b1faf566SEric Dumazet { 4884b1faf566SEric Dumazet struct sock *sk = skb->sk; 4885b1faf566SEric Dumazet 4886b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4887b1faf566SEric Dumazet } 4888b1faf566SEric Dumazet 48898605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 48908605330aSSoheil Hassas Yeganeh { 48918605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 48928605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 48938605330aSSoheil Hassas Yeganeh */ 48948605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 48958605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 48968605330aSSoheil Hassas Yeganeh } 48978605330aSSoheil Hassas Yeganeh 4898b1faf566SEric Dumazet /* 4899b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4900b1faf566SEric Dumazet */ 4901b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4902b1faf566SEric Dumazet { 4903b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4904ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4905b1faf566SEric Dumazet return -ENOMEM; 4906b1faf566SEric Dumazet 4907b1faf566SEric Dumazet skb_orphan(skb); 4908b1faf566SEric Dumazet skb->sk = sk; 4909b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4910b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 49118605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4912b1faf566SEric Dumazet 4913abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4914abb57ea4SEric Dumazet skb_dst_force(skb); 4915abb57ea4SEric Dumazet 4916b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4917b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4918e3ae2365SAlexander Aring sk_error_report(sk); 4919b1faf566SEric Dumazet return 0; 4920b1faf566SEric Dumazet } 4921b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4922b1faf566SEric Dumazet 492383a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 492483a1a1a7SSoheil Hassas Yeganeh { 492583a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 492683a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 492783a1a1a7SSoheil Hassas Yeganeh } 492883a1a1a7SSoheil Hassas Yeganeh 4929364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4930364a9e93SWillem de Bruijn { 4931364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 493283a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 493383a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4934997d5c3fSEric Dumazet unsigned long flags; 4935364a9e93SWillem de Bruijn 4936997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4937364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 493838b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 493983a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 494038b25793SSoheil Hassas Yeganeh if (icmp_next) 4941985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 494238b25793SSoheil Hassas Yeganeh } 4943997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4944364a9e93SWillem de Bruijn 494583a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 494683a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 494783a1a1a7SSoheil Hassas Yeganeh 494883a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4949e3ae2365SAlexander Aring sk_error_report(sk); 4950364a9e93SWillem de Bruijn 4951364a9e93SWillem de Bruijn return skb; 4952364a9e93SWillem de Bruijn } 4953364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4954364a9e93SWillem de Bruijn 4955cab41c47SAlexander Duyck /** 4956cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4957cab41c47SAlexander Duyck * @skb: the skb to clone 4958cab41c47SAlexander Duyck * 4959cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4960cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4961cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4962cab41c47SAlexander Duyck * 4963cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4964cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4965cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4966cab41c47SAlexander Duyck * the sk_error_queue. 4967cab41c47SAlexander Duyck */ 496862bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 496962bccb8cSAlexander Duyck { 497062bccb8cSAlexander Duyck struct sock *sk = skb->sk; 497162bccb8cSAlexander Duyck struct sk_buff *clone; 497262bccb8cSAlexander Duyck 497341c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 497462bccb8cSAlexander Duyck return NULL; 497562bccb8cSAlexander Duyck 497662bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 497762bccb8cSAlexander Duyck if (!clone) { 497862bccb8cSAlexander Duyck sock_put(sk); 497962bccb8cSAlexander Duyck return NULL; 498062bccb8cSAlexander Duyck } 498162bccb8cSAlexander Duyck 498262bccb8cSAlexander Duyck clone->sk = sk; 498362bccb8cSAlexander Duyck clone->destructor = sock_efree; 498462bccb8cSAlexander Duyck 498562bccb8cSAlexander Duyck return clone; 498662bccb8cSAlexander Duyck } 498762bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 498862bccb8cSAlexander Duyck 498937846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 499037846ef0SAlexander Duyck struct sock *sk, 49914ef1b286SSoheil Hassas Yeganeh int tstype, 49924ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4993ac45f602SPatrick Ohly { 4994ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4995ac45f602SPatrick Ohly int err; 4996ac45f602SPatrick Ohly 49974ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 49984ef1b286SSoheil Hassas Yeganeh 4999ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 5000ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 5001ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 5002ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 5003e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 50044ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 50051862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 50064ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 500709c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 500842f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5009a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 50104ed2d765SWillem de Bruijn } 501129030374SEric Dumazet 5012ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 501329030374SEric Dumazet 5014ac45f602SPatrick Ohly if (err) 5015ac45f602SPatrick Ohly kfree_skb(skb); 5016ac45f602SPatrick Ohly } 501737846ef0SAlexander Duyck 5018b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5019b245be1fSWillem de Bruijn { 5020b245be1fSWillem de Bruijn bool ret; 5021b245be1fSWillem de Bruijn 5022d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5023b245be1fSWillem de Bruijn return true; 5024b245be1fSWillem de Bruijn 5025b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5026b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5027b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5028b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5029b245be1fSWillem de Bruijn return ret; 5030b245be1fSWillem de Bruijn } 5031b245be1fSWillem de Bruijn 503237846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 503337846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 503437846ef0SAlexander Duyck { 503537846ef0SAlexander Duyck struct sock *sk = skb->sk; 503637846ef0SAlexander Duyck 5037b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 503835b99dffSWillem de Bruijn goto err; 5039b245be1fSWillem de Bruijn 50409ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 50419ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 50429ac25fc0SEric Dumazet */ 504341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 504437846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 50454ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 504637846ef0SAlexander Duyck sock_put(sk); 504735b99dffSWillem de Bruijn return; 504837846ef0SAlexander Duyck } 504935b99dffSWillem de Bruijn 505035b99dffSWillem de Bruijn err: 505135b99dffSWillem de Bruijn kfree_skb(skb); 50529ac25fc0SEric Dumazet } 505337846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 505437846ef0SAlexander Duyck 505537846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5056e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 505737846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 505837846ef0SAlexander Duyck struct sock *sk, int tstype) 505937846ef0SAlexander Duyck { 506037846ef0SAlexander Duyck struct sk_buff *skb; 50614ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 506237846ef0SAlexander Duyck 50633a8dd971SWillem de Bruijn if (!sk) 50643a8dd971SWillem de Bruijn return; 50653a8dd971SWillem de Bruijn 5066b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5067b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5068b50a5c70SMiroslav Lichvar return; 5069b50a5c70SMiroslav Lichvar 50703a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 50713a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 507237846ef0SAlexander Duyck return; 507337846ef0SAlexander Duyck 50741c885808SFrancis Yan if (tsonly) { 50751c885808SFrancis Yan #ifdef CONFIG_INET 50761c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 507742f67eeaSEric Dumazet sk_is_tcp(sk)) { 5078e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5079e7ed11eeSYousuk Seung ack_skb); 50804ef1b286SSoheil Hassas Yeganeh opt_stats = true; 50814ef1b286SSoheil Hassas Yeganeh } else 50821c885808SFrancis Yan #endif 50831c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 50841c885808SFrancis Yan } else { 508537846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 50861c885808SFrancis Yan } 508737846ef0SAlexander Duyck if (!skb) 508837846ef0SAlexander Duyck return; 508937846ef0SAlexander Duyck 509049ca0d8bSWillem de Bruijn if (tsonly) { 5091fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5092fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 509349ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 509449ca0d8bSWillem de Bruijn } 509549ca0d8bSWillem de Bruijn 509649ca0d8bSWillem de Bruijn if (hwtstamps) 509749ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 509849ca0d8bSWillem de Bruijn else 5099d93376f5SMartin KaFai Lau __net_timestamp(skb); 510049ca0d8bSWillem de Bruijn 51014ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 510237846ef0SAlexander Duyck } 5103e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5104e7fd2885SWillem de Bruijn 5105e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5106e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5107e7fd2885SWillem de Bruijn { 5108e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5109e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5110e7fd2885SWillem de Bruijn } 5111ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5112ac45f602SPatrick Ohly 51136e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 51146e3e939fSJohannes Berg { 51156e3e939fSJohannes Berg struct sock *sk = skb->sk; 51166e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5117dd4f1072SEric Dumazet int err = 1; 51186e3e939fSJohannes Berg 51196e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 51206e3e939fSJohannes Berg skb->wifi_acked = acked; 51216e3e939fSJohannes Berg 51226e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 51236e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 51246e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 51256e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 51266e3e939fSJohannes Berg 5127dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5128dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5129dd4f1072SEric Dumazet */ 513041c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 51316e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5132dd4f1072SEric Dumazet sock_put(sk); 5133dd4f1072SEric Dumazet } 51346e3e939fSJohannes Berg if (err) 51356e3e939fSJohannes Berg kfree_skb(skb); 51366e3e939fSJohannes Berg } 51376e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 51386e3e939fSJohannes Berg 5139f35d9d8aSRusty Russell /** 5140f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5141f35d9d8aSRusty Russell * @skb: the skb to set 5142f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5143f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5144f35d9d8aSRusty Russell * 5145f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5146f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5147f35d9d8aSRusty Russell * 5148f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5149f35d9d8aSRusty Russell * returns false you should drop the packet. 5150f35d9d8aSRusty Russell */ 5151f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5152f35d9d8aSRusty Russell { 515352b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 515452b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 515552b5d6f5SEric Dumazet 515652b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 515752b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 515852b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5159f35d9d8aSRusty Russell return false; 5160f35d9d8aSRusty Russell } 5161f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 516252b5d6f5SEric Dumazet skb->csum_start = csum_start; 5163f35d9d8aSRusty Russell skb->csum_offset = off; 5164e5d5decaSJason Wang skb_set_transport_header(skb, start); 5165f35d9d8aSRusty Russell return true; 5166f35d9d8aSRusty Russell } 5167b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5168f35d9d8aSRusty Russell 5169ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5170ed1f50c3SPaul Durrant unsigned int max) 5171ed1f50c3SPaul Durrant { 5172ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5173ed1f50c3SPaul Durrant return 0; 5174ed1f50c3SPaul Durrant 5175ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5176ed1f50c3SPaul Durrant * won't need to do it again. 5177ed1f50c3SPaul Durrant */ 5178ed1f50c3SPaul Durrant if (max > skb->len) 5179ed1f50c3SPaul Durrant max = skb->len; 5180ed1f50c3SPaul Durrant 5181ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5182ed1f50c3SPaul Durrant return -ENOMEM; 5183ed1f50c3SPaul Durrant 5184ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5185ed1f50c3SPaul Durrant return -EPROTO; 5186ed1f50c3SPaul Durrant 5187ed1f50c3SPaul Durrant return 0; 5188ed1f50c3SPaul Durrant } 5189ed1f50c3SPaul Durrant 5190f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5191f9708b43SJan Beulich 5192f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5193f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5194f9708b43SJan Beulich unsigned int off) 5195f9708b43SJan Beulich { 5196f9708b43SJan Beulich int err; 5197f9708b43SJan Beulich 5198161d1792SKees Cook switch (proto) { 5199f9708b43SJan Beulich case IPPROTO_TCP: 5200f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5201f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5202f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5203f9708b43SJan Beulich offsetof(struct tcphdr, 5204f9708b43SJan Beulich check))) 5205f9708b43SJan Beulich err = -EPROTO; 5206f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5207f9708b43SJan Beulich 5208f9708b43SJan Beulich case IPPROTO_UDP: 5209f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5210f9708b43SJan Beulich off + sizeof(struct udphdr)); 5211f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5212f9708b43SJan Beulich offsetof(struct udphdr, 5213f9708b43SJan Beulich check))) 5214f9708b43SJan Beulich err = -EPROTO; 5215f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5216f9708b43SJan Beulich } 5217f9708b43SJan Beulich 5218f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5219f9708b43SJan Beulich } 5220f9708b43SJan Beulich 5221ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5222ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5223ed1f50c3SPaul Durrant */ 5224ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5225ed1f50c3SPaul Durrant 5226f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5227ed1f50c3SPaul Durrant { 5228ed1f50c3SPaul Durrant unsigned int off; 5229ed1f50c3SPaul Durrant bool fragment; 5230f9708b43SJan Beulich __sum16 *csum; 5231ed1f50c3SPaul Durrant int err; 5232ed1f50c3SPaul Durrant 5233ed1f50c3SPaul Durrant fragment = false; 5234ed1f50c3SPaul Durrant 5235ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5236ed1f50c3SPaul Durrant sizeof(struct iphdr), 5237ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5238ed1f50c3SPaul Durrant if (err < 0) 5239ed1f50c3SPaul Durrant goto out; 5240ed1f50c3SPaul Durrant 524111f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5242ed1f50c3SPaul Durrant fragment = true; 5243ed1f50c3SPaul Durrant 5244ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5245ed1f50c3SPaul Durrant 5246ed1f50c3SPaul Durrant err = -EPROTO; 5247ed1f50c3SPaul Durrant 5248ed1f50c3SPaul Durrant if (fragment) 5249ed1f50c3SPaul Durrant goto out; 5250ed1f50c3SPaul Durrant 5251f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5252f9708b43SJan Beulich if (IS_ERR(csum)) 5253f9708b43SJan Beulich return PTR_ERR(csum); 5254ed1f50c3SPaul Durrant 5255ed1f50c3SPaul Durrant if (recalculate) 5256f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5257ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5258ed1f50c3SPaul Durrant skb->len - off, 5259f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5260ed1f50c3SPaul Durrant err = 0; 5261ed1f50c3SPaul Durrant 5262ed1f50c3SPaul Durrant out: 5263ed1f50c3SPaul Durrant return err; 5264ed1f50c3SPaul Durrant } 5265ed1f50c3SPaul Durrant 5266ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5267ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5268ed1f50c3SPaul Durrant */ 5269ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5270ed1f50c3SPaul Durrant 5271ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5272ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5273ed1f50c3SPaul Durrant 5274ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5275ed1f50c3SPaul Durrant { 5276ed1f50c3SPaul Durrant int err; 5277ed1f50c3SPaul Durrant u8 nexthdr; 5278ed1f50c3SPaul Durrant unsigned int off; 5279ed1f50c3SPaul Durrant unsigned int len; 5280ed1f50c3SPaul Durrant bool fragment; 5281ed1f50c3SPaul Durrant bool done; 5282f9708b43SJan Beulich __sum16 *csum; 5283ed1f50c3SPaul Durrant 5284ed1f50c3SPaul Durrant fragment = false; 5285ed1f50c3SPaul Durrant done = false; 5286ed1f50c3SPaul Durrant 5287ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5288ed1f50c3SPaul Durrant 5289ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5290ed1f50c3SPaul Durrant if (err < 0) 5291ed1f50c3SPaul Durrant goto out; 5292ed1f50c3SPaul Durrant 5293ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5294ed1f50c3SPaul Durrant 5295ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5296ed1f50c3SPaul Durrant while (off <= len && !done) { 5297ed1f50c3SPaul Durrant switch (nexthdr) { 5298ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5299ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5300ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5301ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5302ed1f50c3SPaul Durrant 5303ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5304ed1f50c3SPaul Durrant off + 5305ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5306ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5307ed1f50c3SPaul Durrant if (err < 0) 5308ed1f50c3SPaul Durrant goto out; 5309ed1f50c3SPaul Durrant 5310ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5311ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5312ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5313ed1f50c3SPaul Durrant break; 5314ed1f50c3SPaul Durrant } 5315ed1f50c3SPaul Durrant case IPPROTO_AH: { 5316ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5317ed1f50c3SPaul Durrant 5318ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5319ed1f50c3SPaul Durrant off + 5320ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5321ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5322ed1f50c3SPaul Durrant if (err < 0) 5323ed1f50c3SPaul Durrant goto out; 5324ed1f50c3SPaul Durrant 5325ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5326ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5327ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5328ed1f50c3SPaul Durrant break; 5329ed1f50c3SPaul Durrant } 5330ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5331ed1f50c3SPaul Durrant struct frag_hdr *hp; 5332ed1f50c3SPaul Durrant 5333ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5334ed1f50c3SPaul Durrant off + 5335ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5336ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5337ed1f50c3SPaul Durrant if (err < 0) 5338ed1f50c3SPaul Durrant goto out; 5339ed1f50c3SPaul Durrant 5340ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5341ed1f50c3SPaul Durrant 5342ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5343ed1f50c3SPaul Durrant fragment = true; 5344ed1f50c3SPaul Durrant 5345ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5346ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5347ed1f50c3SPaul Durrant break; 5348ed1f50c3SPaul Durrant } 5349ed1f50c3SPaul Durrant default: 5350ed1f50c3SPaul Durrant done = true; 5351ed1f50c3SPaul Durrant break; 5352ed1f50c3SPaul Durrant } 5353ed1f50c3SPaul Durrant } 5354ed1f50c3SPaul Durrant 5355ed1f50c3SPaul Durrant err = -EPROTO; 5356ed1f50c3SPaul Durrant 5357ed1f50c3SPaul Durrant if (!done || fragment) 5358ed1f50c3SPaul Durrant goto out; 5359ed1f50c3SPaul Durrant 5360f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5361f9708b43SJan Beulich if (IS_ERR(csum)) 5362f9708b43SJan Beulich return PTR_ERR(csum); 5363ed1f50c3SPaul Durrant 5364ed1f50c3SPaul Durrant if (recalculate) 5365f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5366ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5367f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5368ed1f50c3SPaul Durrant err = 0; 5369ed1f50c3SPaul Durrant 5370ed1f50c3SPaul Durrant out: 5371ed1f50c3SPaul Durrant return err; 5372ed1f50c3SPaul Durrant } 5373ed1f50c3SPaul Durrant 5374ed1f50c3SPaul Durrant /** 5375ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5376ed1f50c3SPaul Durrant * @skb: the skb to set up 5377ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5378ed1f50c3SPaul Durrant */ 5379ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5380ed1f50c3SPaul Durrant { 5381ed1f50c3SPaul Durrant int err; 5382ed1f50c3SPaul Durrant 5383ed1f50c3SPaul Durrant switch (skb->protocol) { 5384ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5385f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5386ed1f50c3SPaul Durrant break; 5387ed1f50c3SPaul Durrant 5388ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5389ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5390ed1f50c3SPaul Durrant break; 5391ed1f50c3SPaul Durrant 5392ed1f50c3SPaul Durrant default: 5393ed1f50c3SPaul Durrant err = -EPROTO; 5394ed1f50c3SPaul Durrant break; 5395ed1f50c3SPaul Durrant } 5396ed1f50c3SPaul Durrant 5397ed1f50c3SPaul Durrant return err; 5398ed1f50c3SPaul Durrant } 5399ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5400ed1f50c3SPaul Durrant 54019afd85c9SLinus Lüssing /** 54029afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 54039afd85c9SLinus Lüssing * @skb: the skb to check 54049afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 54059afd85c9SLinus Lüssing * 54069afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 54079afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 54089afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 54099afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 54109afd85c9SLinus Lüssing * 5411a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5412a516993fSLinus Lüssing * differs from the provided skb. 54139afd85c9SLinus Lüssing */ 54149afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 54159afd85c9SLinus Lüssing unsigned int transport_len) 54169afd85c9SLinus Lüssing { 54179afd85c9SLinus Lüssing struct sk_buff *skb_chk; 54189afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 54199afd85c9SLinus Lüssing int ret; 54209afd85c9SLinus Lüssing 5421a516993fSLinus Lüssing if (skb->len < len) 54229afd85c9SLinus Lüssing return NULL; 5423a516993fSLinus Lüssing else if (skb->len == len) 54249afd85c9SLinus Lüssing return skb; 54259afd85c9SLinus Lüssing 54269afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 54279afd85c9SLinus Lüssing if (!skb_chk) 54289afd85c9SLinus Lüssing return NULL; 54299afd85c9SLinus Lüssing 54309afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 54319afd85c9SLinus Lüssing if (ret) { 54329afd85c9SLinus Lüssing kfree_skb(skb_chk); 54339afd85c9SLinus Lüssing return NULL; 54349afd85c9SLinus Lüssing } 54359afd85c9SLinus Lüssing 54369afd85c9SLinus Lüssing return skb_chk; 54379afd85c9SLinus Lüssing } 54389afd85c9SLinus Lüssing 54399afd85c9SLinus Lüssing /** 54409afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 54419afd85c9SLinus Lüssing * @skb: the skb to check 54429afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 54439afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 54449afd85c9SLinus Lüssing * 54459afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 54469afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 54479afd85c9SLinus Lüssing * 54489afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 54499afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 54509afd85c9SLinus Lüssing * 5451a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5452a516993fSLinus Lüssing * differs from the provided skb. 54539afd85c9SLinus Lüssing */ 54549afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 54559afd85c9SLinus Lüssing unsigned int transport_len, 54569afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 54579afd85c9SLinus Lüssing { 54589afd85c9SLinus Lüssing struct sk_buff *skb_chk; 54599afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5460fcba67c9SLinus Lüssing __sum16 ret; 54619afd85c9SLinus Lüssing 54629afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 54639afd85c9SLinus Lüssing if (!skb_chk) 5464a516993fSLinus Lüssing goto err; 54659afd85c9SLinus Lüssing 5466a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5467a516993fSLinus Lüssing goto err; 54689afd85c9SLinus Lüssing 54699b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 54709afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 54719b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 54729afd85c9SLinus Lüssing 5473a516993fSLinus Lüssing if (ret) 5474a516993fSLinus Lüssing goto err; 54759afd85c9SLinus Lüssing 54769afd85c9SLinus Lüssing return skb_chk; 5477a516993fSLinus Lüssing 5478a516993fSLinus Lüssing err: 5479a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5480a516993fSLinus Lüssing kfree_skb(skb_chk); 5481a516993fSLinus Lüssing 5482a516993fSLinus Lüssing return NULL; 5483a516993fSLinus Lüssing 54849afd85c9SLinus Lüssing } 54859afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 54869afd85c9SLinus Lüssing 54874497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 54884497b076SBen Hutchings { 5489e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5490e87cc472SJoe Perches skb->dev->name); 54914497b076SBen Hutchings } 54924497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5493bad43ca8SEric Dumazet 5494bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5495bad43ca8SEric Dumazet { 54963d861f66SEric Dumazet if (head_stolen) { 54973d861f66SEric Dumazet skb_release_head_state(skb); 5498bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 54993d861f66SEric Dumazet } else { 5500bad43ca8SEric Dumazet __kfree_skb(skb); 5501bad43ca8SEric Dumazet } 55023d861f66SEric Dumazet } 5503bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5504bad43ca8SEric Dumazet 5505bad43ca8SEric Dumazet /** 5506bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5507bad43ca8SEric Dumazet * @to: prior buffer 5508bad43ca8SEric Dumazet * @from: buffer to add 5509bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5510c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5511bad43ca8SEric Dumazet */ 5512bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5513bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5514bad43ca8SEric Dumazet { 5515c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5516bad43ca8SEric Dumazet int i, delta, len = from->len; 5517bad43ca8SEric Dumazet 5518bad43ca8SEric Dumazet *fragstolen = false; 5519bad43ca8SEric Dumazet 5520bad43ca8SEric Dumazet if (skb_cloned(to)) 5521bad43ca8SEric Dumazet return false; 5522bad43ca8SEric Dumazet 55231effe8caSJean-Philippe Brucker /* In general, avoid mixing slab allocated and page_pool allocated 55241effe8caSJean-Philippe Brucker * pages within the same SKB. However when @to is not pp_recycle and 55251effe8caSJean-Philippe Brucker * @from is cloned, we can transition frag pages from page_pool to 55261effe8caSJean-Philippe Brucker * reference counted. 55271effe8caSJean-Philippe Brucker * 55281effe8caSJean-Philippe Brucker * On the other hand, don't allow coalescing two pp_recycle SKBs if 55291effe8caSJean-Philippe Brucker * @from is cloned, in case the SKB is using page_pool fragment 55301effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 55311effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 55321effe8caSJean-Philippe Brucker * inconsistent reference counts. 55336a5bcd84SIlias Apalodimas */ 55341effe8caSJean-Philippe Brucker if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from))) 55356a5bcd84SIlias Apalodimas return false; 55366a5bcd84SIlias Apalodimas 5537bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5538e93a0435SEric Dumazet if (len) 5539bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5540bad43ca8SEric Dumazet *delta_truesize = 0; 5541bad43ca8SEric Dumazet return true; 5542bad43ca8SEric Dumazet } 5543bad43ca8SEric Dumazet 5544c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5545c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5546c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5547bad43ca8SEric Dumazet return false; 55481f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 55491f8b977aSWillem de Bruijn return false; 5550bad43ca8SEric Dumazet 5551bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5552bad43ca8SEric Dumazet struct page *page; 5553bad43ca8SEric Dumazet unsigned int offset; 5554bad43ca8SEric Dumazet 5555c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5556c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5557bad43ca8SEric Dumazet return false; 5558bad43ca8SEric Dumazet 5559bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5560bad43ca8SEric Dumazet return false; 5561bad43ca8SEric Dumazet 5562bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5563bad43ca8SEric Dumazet 5564bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5565bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5566bad43ca8SEric Dumazet 5567c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5568bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5569bad43ca8SEric Dumazet *fragstolen = true; 5570bad43ca8SEric Dumazet } else { 5571c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5572c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5573bad43ca8SEric Dumazet return false; 5574bad43ca8SEric Dumazet 5575f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5576bad43ca8SEric Dumazet } 5577bad43ca8SEric Dumazet 5578bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5579bad43ca8SEric Dumazet 5580c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5581c818fa9eSEric Dumazet from_shinfo->frags, 5582c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5583c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5584bad43ca8SEric Dumazet 5585bad43ca8SEric Dumazet if (!skb_cloned(from)) 5586c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5587bad43ca8SEric Dumazet 55888ea853fdSLi RongQing /* if the skb is not cloned this does nothing 55898ea853fdSLi RongQing * since we set nr_frags to 0. 55908ea853fdSLi RongQing */ 5591c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5592c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5593bad43ca8SEric Dumazet 5594bad43ca8SEric Dumazet to->truesize += delta; 5595bad43ca8SEric Dumazet to->len += len; 5596bad43ca8SEric Dumazet to->data_len += len; 5597bad43ca8SEric Dumazet 5598bad43ca8SEric Dumazet *delta_truesize = delta; 5599bad43ca8SEric Dumazet return true; 5600bad43ca8SEric Dumazet } 5601bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5602621e84d6SNicolas Dichtel 5603621e84d6SNicolas Dichtel /** 56048b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5605621e84d6SNicolas Dichtel * 5606621e84d6SNicolas Dichtel * @skb: buffer to clean 56078b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5608621e84d6SNicolas Dichtel * 56098b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 56108b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 56118b27f277SNicolas Dichtel * operations. 56128b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 56138b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 56148b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5615621e84d6SNicolas Dichtel */ 56168b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5617621e84d6SNicolas Dichtel { 5618621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5619621e84d6SNicolas Dichtel skb->skb_iif = 0; 562060ff7467SWANG Cong skb->ignore_df = 0; 5621621e84d6SNicolas Dichtel skb_dst_drop(skb); 5622174e2381SFlorian Westphal skb_ext_reset(skb); 5623895b5c9fSFlorian Westphal nf_reset_ct(skb); 5624621e84d6SNicolas Dichtel nf_reset_trace(skb); 5625213dd74aSHerbert Xu 56266f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 56276f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5628875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 56296f9a5069SPetr Machata #endif 56306f9a5069SPetr Machata 5631213dd74aSHerbert Xu if (!xnet) 5632213dd74aSHerbert Xu return; 5633213dd74aSHerbert Xu 56342b5ec1a5SYe Yin ipvs_reset(skb); 5635213dd74aSHerbert Xu skb->mark = 0; 5636de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5637621e84d6SNicolas Dichtel } 5638621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5639de960aa9SFlorian Westphal 5640de960aa9SFlorian Westphal /** 5641de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5642de960aa9SFlorian Westphal * 5643de960aa9SFlorian Westphal * @skb: GSO skb 5644de960aa9SFlorian Westphal * 5645de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5646de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5647de960aa9SFlorian Westphal * 5648de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5649de960aa9SFlorian Westphal */ 5650a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5651de960aa9SFlorian Westphal { 5652de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5653f993bc25SFlorian Westphal unsigned int thlen = 0; 5654f993bc25SFlorian Westphal 5655f993bc25SFlorian Westphal if (skb->encapsulation) { 5656f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5657f993bc25SFlorian Westphal skb_transport_header(skb); 5658de960aa9SFlorian Westphal 5659de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5660f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5661f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5662f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 56631dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 566490017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5665ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5666ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5667f993bc25SFlorian Westphal } 56686d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 56696d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 56706d39d589SFlorian Westphal * accounted for. 56716d39d589SFlorian Westphal */ 5672f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5673de960aa9SFlorian Westphal } 5674a4a77718SDaniel Axtens 5675a4a77718SDaniel Axtens /** 5676a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5677a4a77718SDaniel Axtens * 5678a4a77718SDaniel Axtens * @skb: GSO skb 5679a4a77718SDaniel Axtens * 5680a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5681a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5682a4a77718SDaniel Axtens * 5683a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5684a4a77718SDaniel Axtens */ 5685a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5686a4a77718SDaniel Axtens { 5687a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5688a4a77718SDaniel Axtens skb_network_header(skb); 5689a4a77718SDaniel Axtens 5690a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5691a4a77718SDaniel Axtens } 5692a4a77718SDaniel Axtens 5693a4a77718SDaniel Axtens /** 5694a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5695a4a77718SDaniel Axtens * 5696a4a77718SDaniel Axtens * @skb: GSO skb 5697a4a77718SDaniel Axtens * 5698a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5699a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5700a4a77718SDaniel Axtens * headers (TCP/UDP). 5701a4a77718SDaniel Axtens */ 5702a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5703a4a77718SDaniel Axtens { 5704a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5705a4a77718SDaniel Axtens 5706a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5707a4a77718SDaniel Axtens } 57080d5501c1SVlad Yasevich 5709ae7ef81eSMarcelo Ricardo Leitner /** 57102b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 57112b16f048SDaniel Axtens * 57122b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 57132b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 57142b16f048SDaniel Axtens * 57152b16f048SDaniel Axtens * We might want to check: 57162b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 57172b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 57182b16f048SDaniel Axtens * 57192b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 57202b16f048SDaniel Axtens * 572149682bfaSMathieu Malaterre * @skb: GSO skb 572249682bfaSMathieu Malaterre * 57232b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 57242b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 57252b16f048SDaniel Axtens * 57262b16f048SDaniel Axtens * @max_len: The maximum permissible length. 57272b16f048SDaniel Axtens * 57282b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 57292b16f048SDaniel Axtens */ 57302b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 57312b16f048SDaniel Axtens unsigned int seg_len, 57322b16f048SDaniel Axtens unsigned int max_len) { 57332b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 57342b16f048SDaniel Axtens const struct sk_buff *iter; 57352b16f048SDaniel Axtens 57362b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 57372b16f048SDaniel Axtens return seg_len <= max_len; 57382b16f048SDaniel Axtens 57392b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 57402b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 57412b16f048SDaniel Axtens 57422b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 57432b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 57442b16f048SDaniel Axtens return false; 57452b16f048SDaniel Axtens } 57462b16f048SDaniel Axtens 57472b16f048SDaniel Axtens return true; 57482b16f048SDaniel Axtens } 57492b16f048SDaniel Axtens 57502b16f048SDaniel Axtens /** 5751779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5752ae7ef81eSMarcelo Ricardo Leitner * 5753ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 575476f21b99SDavid S. Miller * @mtu: MTU to validate against 5755ae7ef81eSMarcelo Ricardo Leitner * 5756779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5757779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5758779b7931SDaniel Axtens * payload. 5759ae7ef81eSMarcelo Ricardo Leitner */ 5760779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5761ae7ef81eSMarcelo Ricardo Leitner { 57622b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5763ae7ef81eSMarcelo Ricardo Leitner } 5764779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5765ae7ef81eSMarcelo Ricardo Leitner 57662b16f048SDaniel Axtens /** 57672b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 57682b16f048SDaniel Axtens * 57692b16f048SDaniel Axtens * @skb: GSO skb 57702b16f048SDaniel Axtens * @len: length to validate against 57712b16f048SDaniel Axtens * 57722b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 57732b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 57742b16f048SDaniel Axtens */ 57752b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 57762b16f048SDaniel Axtens { 57772b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 57782b16f048SDaniel Axtens } 57792b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 57802b16f048SDaniel Axtens 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 { 586593515d53SJiri Pirko struct vlan_hdr *vhdr; 5866b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 586793515d53SJiri Pirko int err; 586893515d53SJiri Pirko 5869b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5870b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5871b6a79208SShmulik Ladkani offset)) { 5872b6a79208SShmulik Ladkani return -EINVAL; 5873b6a79208SShmulik Ladkani } 5874b6a79208SShmulik Ladkani 587593515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 587693515d53SJiri Pirko if (unlikely(err)) 5877b6a79208SShmulik Ladkani return err; 587893515d53SJiri Pirko 587993515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 588093515d53SJiri Pirko 588193515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 588293515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 588393515d53SJiri Pirko 588493515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 588593515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 588693515d53SJiri Pirko 588793515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 588893515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 588993515d53SJiri Pirko 589093515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 589193515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 589293515d53SJiri Pirko 589393515d53SJiri Pirko skb_reset_mac_len(skb); 589493515d53SJiri Pirko 589593515d53SJiri Pirko return err; 589693515d53SJiri Pirko } 5897bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 589893515d53SJiri Pirko 5899b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5900b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5901b6a79208SShmulik Ladkani */ 590293515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 590393515d53SJiri Pirko { 590493515d53SJiri Pirko u16 vlan_tci; 590593515d53SJiri Pirko __be16 vlan_proto; 590693515d53SJiri Pirko int err; 590793515d53SJiri Pirko 5908df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5909b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 591093515d53SJiri Pirko } else { 5911ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 591293515d53SJiri Pirko return 0; 591393515d53SJiri Pirko 591493515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 591593515d53SJiri Pirko if (err) 591693515d53SJiri Pirko return err; 591793515d53SJiri Pirko } 591893515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5919ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 592093515d53SJiri Pirko return 0; 592193515d53SJiri Pirko 592293515d53SJiri Pirko vlan_proto = skb->protocol; 592393515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 592493515d53SJiri Pirko if (unlikely(err)) 592593515d53SJiri Pirko return err; 592693515d53SJiri Pirko 592793515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 592893515d53SJiri Pirko return 0; 592993515d53SJiri Pirko } 593093515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 593193515d53SJiri Pirko 5932b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5933b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5934b6a79208SShmulik Ladkani */ 593593515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 593693515d53SJiri Pirko { 5937df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5938b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 593993515d53SJiri Pirko int err; 594093515d53SJiri Pirko 5941b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5942b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5943b6a79208SShmulik Ladkani offset)) { 5944b6a79208SShmulik Ladkani return -EINVAL; 5945b6a79208SShmulik Ladkani } 5946b6a79208SShmulik Ladkani 594793515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5948df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5949b6a79208SShmulik Ladkani if (err) 595093515d53SJiri Pirko return err; 59519241e2dfSDaniel Borkmann 595293515d53SJiri Pirko skb->protocol = skb->vlan_proto; 595393515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 595493515d53SJiri Pirko 59556b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 595693515d53SJiri Pirko } 595793515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 595893515d53SJiri Pirko return 0; 595993515d53SJiri Pirko } 596093515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 596193515d53SJiri Pirko 596219fbcb36SGuillaume Nault /** 596319fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 596419fbcb36SGuillaume Nault * 596519fbcb36SGuillaume Nault * @skb: Socket buffer to modify 596619fbcb36SGuillaume Nault * 596719fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 596819fbcb36SGuillaume Nault * 596919fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 597019fbcb36SGuillaume Nault * present. 597119fbcb36SGuillaume Nault * 597219fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 597319fbcb36SGuillaume Nault */ 597419fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 597519fbcb36SGuillaume Nault { 597619fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 597719fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 597819fbcb36SGuillaume Nault return -EPROTO; 597919fbcb36SGuillaume Nault 598019fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 598119fbcb36SGuillaume Nault skb_reset_mac_header(skb); 598219fbcb36SGuillaume Nault skb_reset_mac_len(skb); 598319fbcb36SGuillaume Nault 598419fbcb36SGuillaume Nault return 0; 598519fbcb36SGuillaume Nault } 598619fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 598719fbcb36SGuillaume Nault 598819fbcb36SGuillaume Nault /** 598919fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 599019fbcb36SGuillaume Nault * 599119fbcb36SGuillaume Nault * @skb: Socket buffer to modify 599219fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 599319fbcb36SGuillaume Nault * @src: Source MAC address of the new header 599419fbcb36SGuillaume Nault * 599519fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 599619fbcb36SGuillaume Nault * 599719fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 599819fbcb36SGuillaume Nault * 599919fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 600019fbcb36SGuillaume Nault */ 600119fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 600219fbcb36SGuillaume Nault const unsigned char *src) 600319fbcb36SGuillaume Nault { 600419fbcb36SGuillaume Nault struct ethhdr *eth; 600519fbcb36SGuillaume Nault int err; 600619fbcb36SGuillaume Nault 600719fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 600819fbcb36SGuillaume Nault return -EPROTO; 600919fbcb36SGuillaume Nault 601019fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 601119fbcb36SGuillaume Nault if (err < 0) 601219fbcb36SGuillaume Nault return err; 601319fbcb36SGuillaume Nault 601419fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 601519fbcb36SGuillaume Nault skb_reset_mac_header(skb); 601619fbcb36SGuillaume Nault skb_reset_mac_len(skb); 601719fbcb36SGuillaume Nault 601819fbcb36SGuillaume Nault eth = eth_hdr(skb); 601919fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 602019fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 602119fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 602219fbcb36SGuillaume Nault 602319fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 602419fbcb36SGuillaume Nault 602519fbcb36SGuillaume Nault return 0; 602619fbcb36SGuillaume Nault } 602719fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 602819fbcb36SGuillaume Nault 60298822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 60308822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 60318822e270SJohn Hurley __be16 ethertype) 60328822e270SJohn Hurley { 60338822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 60348822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 60358822e270SJohn Hurley 60368822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 60378822e270SJohn Hurley } 60388822e270SJohn Hurley 60398822e270SJohn Hurley hdr->h_proto = ethertype; 60408822e270SJohn Hurley } 60418822e270SJohn Hurley 60428822e270SJohn Hurley /** 6043e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6044e7dbfed1SMartin Varghese * the packet 60458822e270SJohn Hurley * 60468822e270SJohn Hurley * @skb: buffer 60478822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 60488822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6049fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6050e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6051e7dbfed1SMartin Varghese * ethernet 60528822e270SJohn Hurley * 60538822e270SJohn Hurley * Expects skb->data at mac header. 60548822e270SJohn Hurley * 60558822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 60568822e270SJohn Hurley */ 6057fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6058d04ac224SMartin Varghese int mac_len, bool ethernet) 60598822e270SJohn Hurley { 60608822e270SJohn Hurley struct mpls_shim_hdr *lse; 60618822e270SJohn Hurley int err; 60628822e270SJohn Hurley 60638822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 60648822e270SJohn Hurley return -EINVAL; 60658822e270SJohn Hurley 60668822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 60678822e270SJohn Hurley if (skb->encapsulation) 60688822e270SJohn Hurley return -EINVAL; 60698822e270SJohn Hurley 60708822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 60718822e270SJohn Hurley if (unlikely(err)) 60728822e270SJohn Hurley return err; 60738822e270SJohn Hurley 60748822e270SJohn Hurley if (!skb->inner_protocol) { 6075e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 60768822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 60778822e270SJohn Hurley } 60788822e270SJohn Hurley 60798822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 60808822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6081fa4e0f88SDavide Caratti mac_len); 60828822e270SJohn Hurley skb_reset_mac_header(skb); 6083fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6084e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 60858822e270SJohn Hurley 60868822e270SJohn Hurley lse = mpls_hdr(skb); 60878822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 60888822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 60898822e270SJohn Hurley 60904296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 60918822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 60928822e270SJohn Hurley skb->protocol = mpls_proto; 60938822e270SJohn Hurley 60948822e270SJohn Hurley return 0; 60958822e270SJohn Hurley } 60968822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 60978822e270SJohn Hurley 60982e4e4410SEric Dumazet /** 6099ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6100ed246ceeSJohn Hurley * 6101ed246ceeSJohn Hurley * @skb: buffer 6102ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6103fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 610476f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6105ed246ceeSJohn Hurley * 6106ed246ceeSJohn Hurley * Expects skb->data at mac header. 6107ed246ceeSJohn Hurley * 6108ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6109ed246ceeSJohn Hurley */ 6110040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6111040b5cfbSMartin Varghese bool ethernet) 6112ed246ceeSJohn Hurley { 6113ed246ceeSJohn Hurley int err; 6114ed246ceeSJohn Hurley 6115ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6116dedc5a08SDavide Caratti return 0; 6117ed246ceeSJohn Hurley 6118fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6119ed246ceeSJohn Hurley if (unlikely(err)) 6120ed246ceeSJohn Hurley return err; 6121ed246ceeSJohn Hurley 6122ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6123ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6124fa4e0f88SDavide Caratti mac_len); 6125ed246ceeSJohn Hurley 6126ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6127ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6128fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6129ed246ceeSJohn Hurley 61304296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6131ed246ceeSJohn Hurley struct ethhdr *hdr; 6132ed246ceeSJohn Hurley 6133ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6134ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6135ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6136ed246ceeSJohn Hurley } 6137ed246ceeSJohn Hurley skb->protocol = next_proto; 6138ed246ceeSJohn Hurley 6139ed246ceeSJohn Hurley return 0; 6140ed246ceeSJohn Hurley } 6141ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6142ed246ceeSJohn Hurley 6143ed246ceeSJohn Hurley /** 6144d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6145d27cf5c5SJohn Hurley * 6146d27cf5c5SJohn Hurley * @skb: buffer 6147d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6148d27cf5c5SJohn Hurley * 6149d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6150d27cf5c5SJohn Hurley * 6151d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6152d27cf5c5SJohn Hurley */ 6153d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6154d27cf5c5SJohn Hurley { 6155d27cf5c5SJohn Hurley int err; 6156d27cf5c5SJohn Hurley 6157d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6158d27cf5c5SJohn Hurley return -EINVAL; 6159d27cf5c5SJohn Hurley 6160d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6161d27cf5c5SJohn Hurley if (unlikely(err)) 6162d27cf5c5SJohn Hurley return err; 6163d27cf5c5SJohn Hurley 6164d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6165d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6166d27cf5c5SJohn Hurley 6167d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6168d27cf5c5SJohn Hurley } 6169d27cf5c5SJohn Hurley 6170d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6171d27cf5c5SJohn Hurley 6172d27cf5c5SJohn Hurley return 0; 6173d27cf5c5SJohn Hurley } 6174d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6175d27cf5c5SJohn Hurley 6176d27cf5c5SJohn Hurley /** 61772a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 61782a2ea508SJohn Hurley * 61792a2ea508SJohn Hurley * @skb: buffer 61802a2ea508SJohn Hurley * 61812a2ea508SJohn Hurley * Expects skb->data at mac header. 61822a2ea508SJohn Hurley * 61832a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 61842a2ea508SJohn Hurley */ 61852a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 61862a2ea508SJohn Hurley { 61872a2ea508SJohn Hurley u32 lse; 61882a2ea508SJohn Hurley u8 ttl; 61892a2ea508SJohn Hurley 61902a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 61912a2ea508SJohn Hurley return -EINVAL; 61922a2ea508SJohn Hurley 619313de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 619413de4ed9SDavide Caratti return -ENOMEM; 619513de4ed9SDavide Caratti 61962a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 61972a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 61982a2ea508SJohn Hurley if (!--ttl) 61992a2ea508SJohn Hurley return -EINVAL; 62002a2ea508SJohn Hurley 62012a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 62022a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 62032a2ea508SJohn Hurley 62042a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 62052a2ea508SJohn Hurley } 62062a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 62072a2ea508SJohn Hurley 62082a2ea508SJohn Hurley /** 62092e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 62102e4e4410SEric Dumazet * 6211de3f0d0eSMasanari Iida * @header_len: size of linear part 6212de3f0d0eSMasanari Iida * @data_len: needed length in frags 6213de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6214de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6215de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 62162e4e4410SEric Dumazet * 62172e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 62182e4e4410SEric Dumazet */ 62192e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 62202e4e4410SEric Dumazet unsigned long data_len, 62212e4e4410SEric Dumazet int max_page_order, 62222e4e4410SEric Dumazet int *errcode, 62232e4e4410SEric Dumazet gfp_t gfp_mask) 62242e4e4410SEric Dumazet { 62252e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 62262e4e4410SEric Dumazet unsigned long chunk; 62272e4e4410SEric Dumazet struct sk_buff *skb; 62282e4e4410SEric Dumazet struct page *page; 62292e4e4410SEric Dumazet int i; 62302e4e4410SEric Dumazet 62312e4e4410SEric Dumazet *errcode = -EMSGSIZE; 62322e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 62332e4e4410SEric Dumazet * high order pages... 62342e4e4410SEric Dumazet */ 62352e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 62362e4e4410SEric Dumazet return NULL; 62372e4e4410SEric Dumazet 62382e4e4410SEric Dumazet *errcode = -ENOBUFS; 6239f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 62402e4e4410SEric Dumazet if (!skb) 62412e4e4410SEric Dumazet return NULL; 62422e4e4410SEric Dumazet 62432e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 62442e4e4410SEric Dumazet 62452e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 62462e4e4410SEric Dumazet int order = max_page_order; 62472e4e4410SEric Dumazet 62482e4e4410SEric Dumazet while (order) { 62492e4e4410SEric Dumazet if (npages >= 1 << order) { 6250d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 62512e4e4410SEric Dumazet __GFP_COMP | 6252d14b56f5SMichal Hocko __GFP_NOWARN, 62532e4e4410SEric Dumazet order); 62542e4e4410SEric Dumazet if (page) 62552e4e4410SEric Dumazet goto fill_page; 62562e4e4410SEric Dumazet /* Do not retry other high order allocations */ 62572e4e4410SEric Dumazet order = 1; 62582e4e4410SEric Dumazet max_page_order = 0; 62592e4e4410SEric Dumazet } 62602e4e4410SEric Dumazet order--; 62612e4e4410SEric Dumazet } 62622e4e4410SEric Dumazet page = alloc_page(gfp_mask); 62632e4e4410SEric Dumazet if (!page) 62642e4e4410SEric Dumazet goto failure; 62652e4e4410SEric Dumazet fill_page: 62662e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 62672e4e4410SEric Dumazet PAGE_SIZE << order); 62682e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 62692e4e4410SEric Dumazet data_len -= chunk; 62702e4e4410SEric Dumazet npages -= 1 << order; 62712e4e4410SEric Dumazet } 62722e4e4410SEric Dumazet return skb; 62732e4e4410SEric Dumazet 62742e4e4410SEric Dumazet failure: 62752e4e4410SEric Dumazet kfree_skb(skb); 62762e4e4410SEric Dumazet return NULL; 62772e4e4410SEric Dumazet } 62782e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 62796fa01ccdSSowmini Varadhan 62806fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 62816fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 62826fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 62836fa01ccdSSowmini Varadhan { 62846fa01ccdSSowmini Varadhan int i; 628512d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 62866fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 62876fa01ccdSSowmini Varadhan u8 *data; 62886fa01ccdSSowmini Varadhan 62896fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 62906fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 629112d6c1d3SKees Cook 629212d6c1d3SKees Cook size = SKB_DATA_ALIGN(size); 629312d6c1d3SKees Cook size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 629412d6c1d3SKees Cook size = kmalloc_size_roundup(size); 629512d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 62966fa01ccdSSowmini Varadhan if (!data) 62976fa01ccdSSowmini Varadhan return -ENOMEM; 629812d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 62996fa01ccdSSowmini Varadhan 63006fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 63016fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 63026fa01ccdSSowmini Varadhan skb->len -= off; 63036fa01ccdSSowmini Varadhan 63046fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 63056fa01ccdSSowmini Varadhan skb_shinfo(skb), 63066fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 63076fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 63086fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 63096fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 63106fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 63116fa01ccdSSowmini Varadhan kfree(data); 63126fa01ccdSSowmini Varadhan return -ENOMEM; 63136fa01ccdSSowmini Varadhan } 63146fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 63156fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 63166fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 63176fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6318511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 63196fa01ccdSSowmini Varadhan } else { 63206fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 63216fa01ccdSSowmini Varadhan * relocate values 63226fa01ccdSSowmini Varadhan */ 63236fa01ccdSSowmini Varadhan skb_free_head(skb); 63246fa01ccdSSowmini Varadhan } 63256fa01ccdSSowmini Varadhan 63266fa01ccdSSowmini Varadhan skb->head = data; 63276fa01ccdSSowmini Varadhan skb->data = data; 63286fa01ccdSSowmini Varadhan skb->head_frag = 0; 6329763087daSEric Dumazet skb_set_end_offset(skb, size); 63306fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 63316fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 63326fa01ccdSSowmini Varadhan skb->cloned = 0; 63336fa01ccdSSowmini Varadhan skb->hdr_len = 0; 63346fa01ccdSSowmini Varadhan skb->nohdr = 0; 63356fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 63366fa01ccdSSowmini Varadhan 63376fa01ccdSSowmini Varadhan return 0; 63386fa01ccdSSowmini Varadhan } 63396fa01ccdSSowmini Varadhan 63406fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 63416fa01ccdSSowmini Varadhan 63426fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 63436fa01ccdSSowmini Varadhan * pskb_carve() 63446fa01ccdSSowmini Varadhan */ 63456fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 63466fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 63476fa01ccdSSowmini Varadhan gfp_t gfp_mask) 63486fa01ccdSSowmini Varadhan { 63496fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 63506fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 63516fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 63526fa01ccdSSowmini Varadhan 63536fa01ccdSSowmini Varadhan do { 63546fa01ccdSSowmini Varadhan if (!list) { 63556fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 63566fa01ccdSSowmini Varadhan return -EFAULT; 63576fa01ccdSSowmini Varadhan } 63586fa01ccdSSowmini Varadhan if (list->len <= eat) { 63596fa01ccdSSowmini Varadhan /* Eaten as whole. */ 63606fa01ccdSSowmini Varadhan eat -= list->len; 63616fa01ccdSSowmini Varadhan list = list->next; 63626fa01ccdSSowmini Varadhan insp = list; 63636fa01ccdSSowmini Varadhan } else { 63646fa01ccdSSowmini Varadhan /* Eaten partially. */ 63656fa01ccdSSowmini Varadhan if (skb_shared(list)) { 63666fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 63676fa01ccdSSowmini Varadhan if (!clone) 63686fa01ccdSSowmini Varadhan return -ENOMEM; 63696fa01ccdSSowmini Varadhan insp = list->next; 63706fa01ccdSSowmini Varadhan list = clone; 63716fa01ccdSSowmini Varadhan } else { 63726fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 63736fa01ccdSSowmini Varadhan insp = list; 63746fa01ccdSSowmini Varadhan } 63756fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 63766fa01ccdSSowmini Varadhan kfree_skb(clone); 63776fa01ccdSSowmini Varadhan return -ENOMEM; 63786fa01ccdSSowmini Varadhan } 63796fa01ccdSSowmini Varadhan break; 63806fa01ccdSSowmini Varadhan } 63816fa01ccdSSowmini Varadhan } while (eat); 63826fa01ccdSSowmini Varadhan 63836fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 63846fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 63856fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6386ef527f96SEric Dumazet consume_skb(list); 63876fa01ccdSSowmini Varadhan } 63886fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 63896fa01ccdSSowmini Varadhan if (clone) { 63906fa01ccdSSowmini Varadhan clone->next = list; 63916fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 63926fa01ccdSSowmini Varadhan } 63936fa01ccdSSowmini Varadhan return 0; 63946fa01ccdSSowmini Varadhan } 63956fa01ccdSSowmini Varadhan 63966fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 63976fa01ccdSSowmini Varadhan * non-linear part of skb 63986fa01ccdSSowmini Varadhan */ 63996fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 64006fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 64016fa01ccdSSowmini Varadhan { 64026fa01ccdSSowmini Varadhan int i, k = 0; 640312d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 64046fa01ccdSSowmini Varadhan u8 *data; 64056fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 64066fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 64076fa01ccdSSowmini Varadhan 64086fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64096fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 641012d6c1d3SKees Cook 641112d6c1d3SKees Cook size = SKB_DATA_ALIGN(size); 641212d6c1d3SKees Cook size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 641312d6c1d3SKees Cook size = kmalloc_size_roundup(size); 641412d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 64156fa01ccdSSowmini Varadhan if (!data) 64166fa01ccdSSowmini Varadhan return -ENOMEM; 641712d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64186fa01ccdSSowmini Varadhan 64196fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6420e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 64216fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 64226fa01ccdSSowmini Varadhan kfree(data); 64236fa01ccdSSowmini Varadhan return -ENOMEM; 64246fa01ccdSSowmini Varadhan } 64256fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 64266fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 64276fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 64286fa01ccdSSowmini Varadhan 64296fa01ccdSSowmini Varadhan if (pos + fsize > off) { 64306fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 64316fa01ccdSSowmini Varadhan 64326fa01ccdSSowmini Varadhan if (pos < off) { 64336fa01ccdSSowmini Varadhan /* Split frag. 64346fa01ccdSSowmini Varadhan * We have two variants in this case: 64356fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 64366fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 64376fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 64386fa01ccdSSowmini Varadhan * where splitting is expensive. 64396fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 64406fa01ccdSSowmini Varadhan */ 6441b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 64426fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 64436fa01ccdSSowmini Varadhan } 64446fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64456fa01ccdSSowmini Varadhan k++; 64466fa01ccdSSowmini Varadhan } 64476fa01ccdSSowmini Varadhan pos += fsize; 64486fa01ccdSSowmini Varadhan } 64496fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 64506fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64516fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 64526fa01ccdSSowmini Varadhan 64536fa01ccdSSowmini Varadhan /* split line is in frag list */ 6454eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6455eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6456eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6457eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6458eabe8618SMiaohe Lin kfree(data); 6459eabe8618SMiaohe Lin return -ENOMEM; 64606fa01ccdSSowmini Varadhan } 6461511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 64626fa01ccdSSowmini Varadhan 64636fa01ccdSSowmini Varadhan skb->head = data; 64646fa01ccdSSowmini Varadhan skb->head_frag = 0; 64656fa01ccdSSowmini Varadhan skb->data = data; 6466763087daSEric Dumazet skb_set_end_offset(skb, size); 64676fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 64686fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64696fa01ccdSSowmini Varadhan skb->cloned = 0; 64706fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64716fa01ccdSSowmini Varadhan skb->nohdr = 0; 64726fa01ccdSSowmini Varadhan skb->len -= off; 64736fa01ccdSSowmini Varadhan skb->data_len = skb->len; 64746fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64756fa01ccdSSowmini Varadhan return 0; 64766fa01ccdSSowmini Varadhan } 64776fa01ccdSSowmini Varadhan 64786fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 64796fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 64806fa01ccdSSowmini Varadhan { 64816fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 64826fa01ccdSSowmini Varadhan 64836fa01ccdSSowmini Varadhan if (len < headlen) 64846fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 64856fa01ccdSSowmini Varadhan else 64866fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 64876fa01ccdSSowmini Varadhan } 64886fa01ccdSSowmini Varadhan 64896fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 64906fa01ccdSSowmini Varadhan * a new skb 64916fa01ccdSSowmini Varadhan */ 64926fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 64936fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 64946fa01ccdSSowmini Varadhan { 64956fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 64966fa01ccdSSowmini Varadhan 64976fa01ccdSSowmini Varadhan if (!clone) 64986fa01ccdSSowmini Varadhan return NULL; 64996fa01ccdSSowmini Varadhan 65006fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 65016fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 65026fa01ccdSSowmini Varadhan kfree_skb(clone); 65036fa01ccdSSowmini Varadhan return NULL; 65046fa01ccdSSowmini Varadhan } 65056fa01ccdSSowmini Varadhan return clone; 65066fa01ccdSSowmini Varadhan } 65076fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6508c8c8b127SEric Dumazet 6509c8c8b127SEric Dumazet /** 6510c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6511c8c8b127SEric Dumazet * @skb: buffer 6512c8c8b127SEric Dumazet * 6513c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6514c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6515c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6516c8c8b127SEric Dumazet * truesize. 6517c8c8b127SEric Dumazet * Notes: 6518c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6519c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6520c8c8b127SEric Dumazet */ 6521c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6522c8c8b127SEric Dumazet { 65233174fed9SEric Dumazet if (skb->data_len) { 65243174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6525c8c8b127SEric Dumazet skb_cloned(skb)) 6526c8c8b127SEric Dumazet return; 6527c8c8b127SEric Dumazet 6528c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6529c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 65303174fed9SEric Dumazet } 65313174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 65323174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 65333174fed9SEric Dumazet * their truesize. 65343174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 65353174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 65363174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6537c8c8b127SEric Dumazet */ 6538c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6539c8c8b127SEric Dumazet } 6540d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6541df5042f4SFlorian Westphal 6542df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6543df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6544df5042f4SFlorian Westphal { 6545df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6546df5042f4SFlorian Westphal } 6547df5042f4SFlorian Westphal 65488b69a803SPaolo Abeni /** 65498b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 65508b69a803SPaolo Abeni * 65514930f483SFlorian Westphal * @flags: See kmalloc(). 65524930f483SFlorian Westphal * 65538b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 65548b69a803SPaolo Abeni * skb via __skb_ext_set(). 65558b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 65568b69a803SPaolo Abeni */ 65574930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6558df5042f4SFlorian Westphal { 65594930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6560df5042f4SFlorian Westphal 6561df5042f4SFlorian Westphal if (new) { 6562df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6563df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6564df5042f4SFlorian Westphal } 6565df5042f4SFlorian Westphal 6566df5042f4SFlorian Westphal return new; 6567df5042f4SFlorian Westphal } 6568df5042f4SFlorian Westphal 65694165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 65704165079bSFlorian Westphal unsigned int old_active) 6571df5042f4SFlorian Westphal { 6572df5042f4SFlorian Westphal struct skb_ext *new; 6573df5042f4SFlorian Westphal 6574df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6575df5042f4SFlorian Westphal return old; 6576df5042f4SFlorian Westphal 6577df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6578df5042f4SFlorian Westphal if (!new) 6579df5042f4SFlorian Westphal return NULL; 6580df5042f4SFlorian Westphal 6581df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6582df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6583df5042f4SFlorian Westphal 65844165079bSFlorian Westphal #ifdef CONFIG_XFRM 65854165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 65864165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 65874165079bSFlorian Westphal unsigned int i; 65884165079bSFlorian Westphal 65894165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 65904165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 65914165079bSFlorian Westphal } 65924165079bSFlorian Westphal #endif 6593df5042f4SFlorian Westphal __skb_ext_put(old); 6594df5042f4SFlorian Westphal return new; 6595df5042f4SFlorian Westphal } 6596df5042f4SFlorian Westphal 6597df5042f4SFlorian Westphal /** 65988b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 65998b69a803SPaolo Abeni * @skb: buffer 66008b69a803SPaolo Abeni * @id: extension id 66018b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 66028b69a803SPaolo Abeni * 66038b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 66048b69a803SPaolo Abeni * 66058b69a803SPaolo Abeni * Returns the pointer to the extension. 66068b69a803SPaolo Abeni */ 66078b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 66088b69a803SPaolo Abeni struct skb_ext *ext) 66098b69a803SPaolo Abeni { 66108b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 66118b69a803SPaolo Abeni 66128b69a803SPaolo Abeni skb_ext_put(skb); 66138b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 66148b69a803SPaolo Abeni ext->chunks = newlen; 66158b69a803SPaolo Abeni ext->offset[id] = newoff; 66168b69a803SPaolo Abeni skb->extensions = ext; 66178b69a803SPaolo Abeni skb->active_extensions = 1 << id; 66188b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 66198b69a803SPaolo Abeni } 66208b69a803SPaolo Abeni 66218b69a803SPaolo Abeni /** 6622df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6623df5042f4SFlorian Westphal * @skb: buffer 6624df5042f4SFlorian Westphal * @id: extension to allocate space for 6625df5042f4SFlorian Westphal * 6626df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6627df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6628df5042f4SFlorian Westphal * is returned. 6629df5042f4SFlorian Westphal * 6630df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6631df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6632df5042f4SFlorian Westphal * 6633df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6634df5042f4SFlorian Westphal */ 6635df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6636df5042f4SFlorian Westphal { 6637df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6638df5042f4SFlorian Westphal unsigned int newlen, newoff; 6639df5042f4SFlorian Westphal 6640df5042f4SFlorian Westphal if (skb->active_extensions) { 6641df5042f4SFlorian Westphal old = skb->extensions; 6642df5042f4SFlorian Westphal 66434165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6644df5042f4SFlorian Westphal if (!new) 6645df5042f4SFlorian Westphal return NULL; 6646df5042f4SFlorian Westphal 6647682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6648df5042f4SFlorian Westphal goto set_active; 6649df5042f4SFlorian Westphal 6650e94e50bdSPaolo Abeni newoff = new->chunks; 6651df5042f4SFlorian Westphal } else { 6652df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6653df5042f4SFlorian Westphal 66544930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6655df5042f4SFlorian Westphal if (!new) 6656df5042f4SFlorian Westphal return NULL; 6657df5042f4SFlorian Westphal } 6658df5042f4SFlorian Westphal 6659df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6660df5042f4SFlorian Westphal new->chunks = newlen; 6661df5042f4SFlorian Westphal new->offset[id] = newoff; 6662df5042f4SFlorian Westphal set_active: 6663b0999f38SPaolo Abeni skb->slow_gro = 1; 6664682ec859SPaolo Abeni skb->extensions = new; 6665df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6666df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6667df5042f4SFlorian Westphal } 6668df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6669df5042f4SFlorian Westphal 66704165079bSFlorian Westphal #ifdef CONFIG_XFRM 66714165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 66724165079bSFlorian Westphal { 66734165079bSFlorian Westphal unsigned int i; 66744165079bSFlorian Westphal 66754165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 66764165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 66774165079bSFlorian Westphal } 66784165079bSFlorian Westphal #endif 66794165079bSFlorian Westphal 668078476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 668178476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 668278476d31SJeremy Kerr { 668378476d31SJeremy Kerr if (flow->key) 668478476d31SJeremy Kerr mctp_key_unref(flow->key); 668578476d31SJeremy Kerr } 668678476d31SJeremy Kerr #endif 668778476d31SJeremy Kerr 6688df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6689df5042f4SFlorian Westphal { 6690df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6691df5042f4SFlorian Westphal 6692df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6693df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6694df5042f4SFlorian Westphal skb->extensions = NULL; 6695df5042f4SFlorian Westphal __skb_ext_put(ext); 66964165079bSFlorian Westphal #ifdef CONFIG_XFRM 66974165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 66984165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 66994165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 67004165079bSFlorian Westphal 67014165079bSFlorian Westphal skb_ext_put_sp(sp); 67024165079bSFlorian Westphal sp->len = 0; 67034165079bSFlorian Westphal #endif 6704df5042f4SFlorian Westphal } 6705df5042f4SFlorian Westphal } 6706df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6707df5042f4SFlorian Westphal 6708df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6709df5042f4SFlorian Westphal { 6710df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6711df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6712df5042f4SFlorian Westphal */ 6713df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6714df5042f4SFlorian Westphal goto free_now; 6715df5042f4SFlorian Westphal 6716df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6717df5042f4SFlorian Westphal return; 6718df5042f4SFlorian Westphal free_now: 67194165079bSFlorian Westphal #ifdef CONFIG_XFRM 67204165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 67214165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 67224165079bSFlorian Westphal #endif 672378476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 672478476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 672578476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 672678476d31SJeremy Kerr #endif 67274165079bSFlorian Westphal 6728df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6729df5042f4SFlorian Westphal } 6730df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6731df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 673268822bdfSEric Dumazet 673368822bdfSEric Dumazet /** 673468822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 673568822bdfSEric Dumazet * @skb: buffer 673668822bdfSEric Dumazet * 673768822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 673868822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 673968822bdfSEric Dumazet * and memory zone spinlock contention. 674068822bdfSEric Dumazet */ 674168822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 674268822bdfSEric Dumazet { 674368822bdfSEric Dumazet int cpu = skb->alloc_cpu; 674468822bdfSEric Dumazet struct softnet_data *sd; 674568822bdfSEric Dumazet unsigned long flags; 674639564c3fSEric Dumazet unsigned int defer_max; 674768822bdfSEric Dumazet bool kick; 674868822bdfSEric Dumazet 674968822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 675068822bdfSEric Dumazet !cpu_online(cpu) || 675168822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 675239564c3fSEric Dumazet nodefer: __kfree_skb(skb); 675368822bdfSEric Dumazet return; 675468822bdfSEric Dumazet } 675568822bdfSEric Dumazet 675668822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 675739564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 675839564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 675939564c3fSEric Dumazet goto nodefer; 676039564c3fSEric Dumazet 676168822bdfSEric Dumazet spin_lock_irqsave(&sd->defer_lock, flags); 6762c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6763c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6764c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6765c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6766c09b0cd2SJakub Kicinski 676768822bdfSEric Dumazet skb->next = sd->defer_list; 676868822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 676968822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 677068822bdfSEric Dumazet spin_unlock_irqrestore(&sd->defer_lock, flags); 677168822bdfSEric Dumazet 677268822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 677368822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 677468822bdfSEric Dumazet */ 677597e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 677668822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 677768822bdfSEric Dumazet } 6778