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> 822195e2a0SJakub Kicinski #include <linux/textsearch.h> 83a1f8e7f7SAl Viro 8439564c3fSEric Dumazet #include "dev.h" 857f678defSVasily Averin #include "sock_destructor.h" 867b7ed885SBart Van Assche 8708009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8808009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 89df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 90df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 91df5042f4SFlorian Westphal #endif 925f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 935f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 941da177e4SLinus Torvalds 959cb252c4SMenglong Dong #undef FN 969cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 979cb252c4SMenglong Dong const char * const drop_reasons[] = { 980e84afe8SEric Dumazet [SKB_CONSUMED] = "CONSUMED", 999cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 1009cb252c4SMenglong Dong }; 101ec43908dSMenglong Dong EXPORT_SYMBOL(drop_reasons); 102ec43908dSMenglong Dong 1031da177e4SLinus Torvalds /** 104f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1051da177e4SLinus Torvalds * @skb: buffer 1061da177e4SLinus Torvalds * @sz: size 107f05de73bSJean Sacren * @addr: address 10899d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1091da177e4SLinus Torvalds * 110f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 111f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 112f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 113f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1141da177e4SLinus Torvalds */ 115f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 11699d5851eSJames Hogan const char msg[]) 1171da177e4SLinus Torvalds { 11841a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 11999d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1204305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12126095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1221da177e4SLinus Torvalds BUG(); 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 125f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1261da177e4SLinus Torvalds { 127f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 130f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 131f05de73bSJean Sacren { 132f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 133f05de73bSJean Sacren } 134c93bdd0eSMel Gorman 13550fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 136f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 137f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 13850fad4b5SAlexander Lobakin 139dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 140dbae2b06SPaolo Abeni 141dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 142dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 143dbae2b06SPaolo Abeni 144dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 145dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 146dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 147dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 148dbae2b06SPaolo Abeni */ 149dbae2b06SPaolo Abeni 150dbae2b06SPaolo Abeni struct page_frag_1k { 151dbae2b06SPaolo Abeni void *va; 152dbae2b06SPaolo Abeni u16 offset; 153dbae2b06SPaolo Abeni bool pfmemalloc; 154dbae2b06SPaolo Abeni }; 155dbae2b06SPaolo Abeni 156dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 157dbae2b06SPaolo Abeni { 158dbae2b06SPaolo Abeni struct page *page; 159dbae2b06SPaolo Abeni int offset; 160dbae2b06SPaolo Abeni 161dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 162dbae2b06SPaolo Abeni if (likely(offset >= 0)) 163dbae2b06SPaolo Abeni goto use_frag; 164dbae2b06SPaolo Abeni 165dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 166dbae2b06SPaolo Abeni if (!page) 167dbae2b06SPaolo Abeni return NULL; 168dbae2b06SPaolo Abeni 169dbae2b06SPaolo Abeni nc->va = page_address(page); 170dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 171dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 172dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 173dbae2b06SPaolo Abeni 174dbae2b06SPaolo Abeni use_frag: 175dbae2b06SPaolo Abeni nc->offset = offset; 176dbae2b06SPaolo Abeni return nc->va + offset; 177dbae2b06SPaolo Abeni } 178dbae2b06SPaolo Abeni #else 179dbae2b06SPaolo Abeni 180dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 181dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 182dbae2b06SPaolo Abeni */ 183dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 184dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 185dbae2b06SPaolo Abeni 186dbae2b06SPaolo Abeni struct page_frag_1k { 187dbae2b06SPaolo Abeni }; 188dbae2b06SPaolo Abeni 189dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 190dbae2b06SPaolo Abeni { 191dbae2b06SPaolo Abeni return NULL; 192dbae2b06SPaolo Abeni } 193dbae2b06SPaolo Abeni 194dbae2b06SPaolo Abeni #endif 195dbae2b06SPaolo Abeni 19650fad4b5SAlexander Lobakin struct napi_alloc_cache { 19750fad4b5SAlexander Lobakin struct page_frag_cache page; 198dbae2b06SPaolo Abeni struct page_frag_1k page_small; 19950fad4b5SAlexander Lobakin unsigned int skb_count; 20050fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 20150fad4b5SAlexander Lobakin }; 20250fad4b5SAlexander Lobakin 20350fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 20450fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 20550fad4b5SAlexander Lobakin 206dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 207dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 208dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 209dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 210dbae2b06SPaolo Abeni * does not accidentally come back. 211dbae2b06SPaolo Abeni */ 212dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 213dbae2b06SPaolo Abeni { 214dbae2b06SPaolo Abeni struct sk_buff *skb; 215dbae2b06SPaolo Abeni 216dbae2b06SPaolo Abeni local_bh_disable(); 217dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 218dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 219dbae2b06SPaolo Abeni napi_free_frags(napi); 220dbae2b06SPaolo Abeni local_bh_enable(); 221dbae2b06SPaolo Abeni } 222dbae2b06SPaolo Abeni 22332e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 22450fad4b5SAlexander Lobakin { 22550fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 22650fad4b5SAlexander Lobakin 22750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 22850fad4b5SAlexander Lobakin 22932e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 23050fad4b5SAlexander Lobakin } 23150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 23250fad4b5SAlexander Lobakin 23350fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 23450fad4b5SAlexander Lobakin { 23550fad4b5SAlexander Lobakin void *data; 23650fad4b5SAlexander Lobakin 23750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 238afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 23932e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 24032e3573fSYajun Deng 24150fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 24250fad4b5SAlexander Lobakin } else { 24332e3573fSYajun Deng struct napi_alloc_cache *nc; 24432e3573fSYajun Deng 24550fad4b5SAlexander Lobakin local_bh_disable(); 24632e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 24732e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 24850fad4b5SAlexander Lobakin local_bh_enable(); 24950fad4b5SAlexander Lobakin } 25050fad4b5SAlexander Lobakin return data; 25150fad4b5SAlexander Lobakin } 25250fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 25350fad4b5SAlexander Lobakin 254f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 255f450d539SAlexander Lobakin { 256f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 257f450d539SAlexander Lobakin struct sk_buff *skb; 258f450d539SAlexander Lobakin 25949ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 260f450d539SAlexander Lobakin nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 261f450d539SAlexander Lobakin GFP_ATOMIC, 262f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 263f450d539SAlexander Lobakin nc->skb_cache); 264f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 265f450d539SAlexander Lobakin return NULL; 26649ae83fcSSieng Piaw Liew } 267f450d539SAlexander Lobakin 268f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 269f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, skb); 270f450d539SAlexander Lobakin 271f450d539SAlexander Lobakin return skb; 272f450d539SAlexander Lobakin } 273f450d539SAlexander Lobakin 274ce098da1SKees Cook static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 275ce098da1SKees Cook unsigned int size) 276ba0509b6SJesper Dangaard Brouer { 277ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 278ba0509b6SJesper Dangaard Brouer 279ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 280ba0509b6SJesper Dangaard Brouer 281ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 282ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 283ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 284ba0509b6SJesper Dangaard Brouer skb->head = data; 285ba0509b6SJesper Dangaard Brouer skb->data = data; 286ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 287763087daSEric Dumazet skb_set_end_offset(skb, size); 288ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 289ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 29068822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 291ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 292ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 293ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 294ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 295ba0509b6SJesper Dangaard Brouer 2966370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 297ba0509b6SJesper Dangaard Brouer } 298ba0509b6SJesper Dangaard Brouer 299ce098da1SKees Cook static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 300ce098da1SKees Cook unsigned int *size) 301ce098da1SKees Cook { 302ce098da1SKees Cook void *resized; 303ce098da1SKees Cook 304ce098da1SKees Cook /* Must find the allocation size (and grow it to match). */ 305ce098da1SKees Cook *size = ksize(data); 306ce098da1SKees Cook /* krealloc() will immediately return "data" when 307ce098da1SKees Cook * "ksize(data)" is requested: it is the existing upper 308ce098da1SKees Cook * bounds. As a result, GFP_ATOMIC will be ignored. Note 309ce098da1SKees Cook * that this "new" pointer needs to be passed back to the 310ce098da1SKees Cook * caller for use so the __alloc_size hinting will be 311ce098da1SKees Cook * tracked correctly. 312ce098da1SKees Cook */ 313ce098da1SKees Cook resized = krealloc(data, *size, GFP_ATOMIC); 314ce098da1SKees Cook WARN_ON_ONCE(resized != data); 315ce098da1SKees Cook return resized; 316ce098da1SKees Cook } 317ce098da1SKees Cook 318ce098da1SKees Cook /* build_skb() variant which can operate on slab buffers. 319ce098da1SKees Cook * Note that this should be used sparingly as slab buffers 320ce098da1SKees Cook * cannot be combined efficiently by GRO! 321ce098da1SKees Cook */ 322ce098da1SKees Cook struct sk_buff *slab_build_skb(void *data) 323ce098da1SKees Cook { 324ce098da1SKees Cook struct sk_buff *skb; 325ce098da1SKees Cook unsigned int size; 326ce098da1SKees Cook 327ce098da1SKees Cook skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 328ce098da1SKees Cook if (unlikely(!skb)) 329ce098da1SKees Cook return NULL; 330ce098da1SKees Cook 331ce098da1SKees Cook memset(skb, 0, offsetof(struct sk_buff, tail)); 332ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 333ce098da1SKees Cook __finalize_skb_around(skb, data, size); 334ce098da1SKees Cook 335ce098da1SKees Cook return skb; 336ce098da1SKees Cook } 337ce098da1SKees Cook EXPORT_SYMBOL(slab_build_skb); 338ce098da1SKees Cook 339ce098da1SKees Cook /* Caller must provide SKB that is memset cleared */ 340ce098da1SKees Cook static void __build_skb_around(struct sk_buff *skb, void *data, 341ce098da1SKees Cook unsigned int frag_size) 342ce098da1SKees Cook { 343ce098da1SKees Cook unsigned int size = frag_size; 344ce098da1SKees Cook 345ce098da1SKees Cook /* frag_size == 0 is considered deprecated now. Callers 346ce098da1SKees Cook * using slab buffer should use slab_build_skb() instead. 347ce098da1SKees Cook */ 348ce098da1SKees Cook if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 349ce098da1SKees Cook data = __slab_build_skb(skb, data, &size); 350ce098da1SKees Cook 351ce098da1SKees Cook __finalize_skb_around(skb, data, size); 352ce098da1SKees Cook } 353ce098da1SKees Cook 3541da177e4SLinus Torvalds /** 3552ea2f62cSEric Dumazet * __build_skb - build a network buffer 356b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 357ce098da1SKees Cook * @frag_size: size of data (must not be 0) 358b2b5ce9dSEric Dumazet * 359b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 360ce098da1SKees Cook * skb_shared_info. @data must have been allocated from the page 361ce098da1SKees Cook * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 362ce098da1SKees Cook * allocation is deprecated, and callers should use slab_build_skb() 363ce098da1SKees Cook * instead.) 364b2b5ce9dSEric Dumazet * The return is the new skb buffer. 365b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 366b2b5ce9dSEric Dumazet * Notes : 367b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 368b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 369b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 370b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 371b2b5ce9dSEric Dumazet * before giving packet to stack. 372b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 373b2b5ce9dSEric Dumazet */ 3742ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 375b2b5ce9dSEric Dumazet { 376b2b5ce9dSEric Dumazet struct sk_buff *skb; 377b2b5ce9dSEric Dumazet 378b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 379ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 380b2b5ce9dSEric Dumazet return NULL; 381b2b5ce9dSEric Dumazet 382b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 383483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 384b2b5ce9dSEric Dumazet 385483126b3SAlexander Lobakin return skb; 386b2b5ce9dSEric Dumazet } 3872ea2f62cSEric Dumazet 3882ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3892ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3902ea2f62cSEric Dumazet */ 3912ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3922ea2f62cSEric Dumazet { 3932ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3942ea2f62cSEric Dumazet 3952ea2f62cSEric Dumazet if (skb && frag_size) { 3962ea2f62cSEric Dumazet skb->head_frag = 1; 3972f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3982ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3992ea2f62cSEric Dumazet } 4002ea2f62cSEric Dumazet return skb; 4012ea2f62cSEric Dumazet } 402b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 403b2b5ce9dSEric Dumazet 404ba0509b6SJesper Dangaard Brouer /** 405ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 406ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 407ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 40812c1604aSJakub Kicinski * @frag_size: size of data 409ba0509b6SJesper Dangaard Brouer */ 410ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 411ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 412ba0509b6SJesper Dangaard Brouer { 413ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 414ba0509b6SJesper Dangaard Brouer return NULL; 415ba0509b6SJesper Dangaard Brouer 416483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 417ba0509b6SJesper Dangaard Brouer 418483126b3SAlexander Lobakin if (frag_size) { 419ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 420ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 421ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 422ba0509b6SJesper Dangaard Brouer } 423ba0509b6SJesper Dangaard Brouer return skb; 424ba0509b6SJesper Dangaard Brouer } 425ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 426ba0509b6SJesper Dangaard Brouer 427f450d539SAlexander Lobakin /** 428f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 429f450d539SAlexander Lobakin * @data: data buffer provided by caller 43012c1604aSJakub Kicinski * @frag_size: size of data 431f450d539SAlexander Lobakin * 432f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 433f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 434f450d539SAlexander Lobakin * 435f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 436f450d539SAlexander Lobakin */ 437f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 438f450d539SAlexander Lobakin { 439f450d539SAlexander Lobakin struct sk_buff *skb; 440f450d539SAlexander Lobakin 441f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 442f450d539SAlexander Lobakin if (unlikely(!skb)) 443f450d539SAlexander Lobakin return NULL; 444f450d539SAlexander Lobakin 445f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 446f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 447f450d539SAlexander Lobakin 448f450d539SAlexander Lobakin return skb; 449f450d539SAlexander Lobakin } 450f450d539SAlexander Lobakin 451f450d539SAlexander Lobakin /** 452f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 453f450d539SAlexander Lobakin * @data: data buffer provided by caller 45412c1604aSJakub Kicinski * @frag_size: size of data 455f450d539SAlexander Lobakin * 456f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 457f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 458f450d539SAlexander Lobakin * 459f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 460f450d539SAlexander Lobakin */ 461f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 462f450d539SAlexander Lobakin { 463f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 464f450d539SAlexander Lobakin 465f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 466f450d539SAlexander Lobakin skb->head_frag = 1; 467f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 468f450d539SAlexander Lobakin } 469f450d539SAlexander Lobakin 470f450d539SAlexander Lobakin return skb; 471f450d539SAlexander Lobakin } 472f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 473f450d539SAlexander Lobakin 4745381b23dSAlexander Lobakin /* 4755381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 4765381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 4775381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 4785381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 4795381b23dSAlexander Lobakin * memory is free 4805381b23dSAlexander Lobakin */ 481ef28095fSAlexander Lobakin static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 482ef28095fSAlexander Lobakin bool *pfmemalloc) 4835381b23dSAlexander Lobakin { 4845381b23dSAlexander Lobakin void *obj; 4855381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 4865381b23dSAlexander Lobakin 4875381b23dSAlexander Lobakin /* 4885381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 4895381b23dSAlexander Lobakin * to the reserves, fail. 4905381b23dSAlexander Lobakin */ 4915381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, 4925381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 4935381b23dSAlexander Lobakin node); 4945381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 4955381b23dSAlexander Lobakin goto out; 4965381b23dSAlexander Lobakin 4975381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 4985381b23dSAlexander Lobakin ret_pfmemalloc = true; 4995381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, flags, node); 5005381b23dSAlexander Lobakin 5015381b23dSAlexander Lobakin out: 5025381b23dSAlexander Lobakin if (pfmemalloc) 5035381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 5045381b23dSAlexander Lobakin 5055381b23dSAlexander Lobakin return obj; 5065381b23dSAlexander Lobakin } 5075381b23dSAlexander Lobakin 5085381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 5095381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 5105381b23dSAlexander Lobakin * [BEEP] leaks. 5115381b23dSAlexander Lobakin * 5125381b23dSAlexander Lobakin */ 5135381b23dSAlexander Lobakin 5145381b23dSAlexander Lobakin /** 5155381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 5165381b23dSAlexander Lobakin * @size: size to allocate 5175381b23dSAlexander Lobakin * @gfp_mask: allocation mask 5185381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 5195381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 5205381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 5215381b23dSAlexander Lobakin * allocations in case the data is required for writeback 5225381b23dSAlexander Lobakin * @node: numa node to allocate memory on 5235381b23dSAlexander Lobakin * 5245381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 5255381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 5265381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 5275381b23dSAlexander Lobakin * 5285381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 5295381b23dSAlexander Lobakin * %GFP_ATOMIC. 5305381b23dSAlexander Lobakin */ 5315381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 5325381b23dSAlexander Lobakin int flags, int node) 5335381b23dSAlexander Lobakin { 5345381b23dSAlexander Lobakin struct kmem_cache *cache; 5355381b23dSAlexander Lobakin struct sk_buff *skb; 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 */ 560115f1a5cSEric Dumazet size = SKB_HEAD_ALIGN(size); 561*65998d2bSEric Dumazet size = kmalloc_size_roundup(size); 562*65998d2bSEric Dumazet data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 563df1ae022SAlexander Lobakin if (unlikely(!data)) 5645381b23dSAlexander Lobakin goto nodata; 56512d6c1d3SKees Cook /* kmalloc_size_roundup() might give us more room than requested. 5665381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 5675381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 5685381b23dSAlexander Lobakin */ 569*65998d2bSEric Dumazet prefetchw(data + SKB_WITH_OVERHEAD(size)); 5705381b23dSAlexander Lobakin 5715381b23dSAlexander Lobakin /* 5725381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 5735381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 5745381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 5755381b23dSAlexander Lobakin */ 5765381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 577*65998d2bSEric Dumazet __build_skb_around(skb, data, size); 5785381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 5795381b23dSAlexander Lobakin 5805381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 5815381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 5825381b23dSAlexander Lobakin 5835381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 5845381b23dSAlexander Lobakin 5855381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 5865381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 5875381b23dSAlexander Lobakin } 5885381b23dSAlexander Lobakin 5895381b23dSAlexander Lobakin return skb; 590df1ae022SAlexander Lobakin 5915381b23dSAlexander Lobakin nodata: 5925381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 593df1ae022SAlexander Lobakin return NULL; 5945381b23dSAlexander Lobakin } 5955381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 5965381b23dSAlexander Lobakin 5977ba7aeabSSebastian Andrzej Siewior /** 598fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 599fd11a83dSAlexander Duyck * @dev: network device to receive on 600d7499160SMasanari Iida * @len: length to allocate 601fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 602fd11a83dSAlexander Duyck * 603fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 604fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 605fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 606fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 607fd11a83dSAlexander Duyck * 608fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 609fd11a83dSAlexander Duyck */ 6109451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 6119451980aSAlexander Duyck gfp_t gfp_mask) 612fd11a83dSAlexander Duyck { 613b63ae8caSAlexander Duyck struct page_frag_cache *nc; 614fd11a83dSAlexander Duyck struct sk_buff *skb; 6159451980aSAlexander Duyck bool pfmemalloc; 6169451980aSAlexander Duyck void *data; 617fd11a83dSAlexander Duyck 6189451980aSAlexander Duyck len += NET_SKB_PAD; 619fd11a83dSAlexander Duyck 62066c55602SAlexander Lobakin /* If requested length is either too small or too big, 62166c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 62266c55602SAlexander Lobakin */ 62366c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 62466c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 625d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 626a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 627a080e7bdSAlexander Duyck if (!skb) 628a080e7bdSAlexander Duyck goto skb_fail; 629a080e7bdSAlexander Duyck goto skb_success; 630a080e7bdSAlexander Duyck } 6319451980aSAlexander Duyck 632115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 6339451980aSAlexander Duyck 6349451980aSAlexander Duyck if (sk_memalloc_socks()) 6359451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 6369451980aSAlexander Duyck 637afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 6389451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 6398c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 6409451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 64192dcabd7SSebastian Andrzej Siewior } else { 64292dcabd7SSebastian Andrzej Siewior local_bh_disable(); 64392dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 64492dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 64592dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 64692dcabd7SSebastian Andrzej Siewior local_bh_enable(); 64792dcabd7SSebastian Andrzej Siewior } 6489451980aSAlexander Duyck 6499451980aSAlexander Duyck if (unlikely(!data)) 6509451980aSAlexander Duyck return NULL; 6519451980aSAlexander Duyck 6529451980aSAlexander Duyck skb = __build_skb(data, len); 6539451980aSAlexander Duyck if (unlikely(!skb)) { 654181edb2bSAlexander Duyck skb_free_frag(data); 6559451980aSAlexander Duyck return NULL; 6569451980aSAlexander Duyck } 6579451980aSAlexander Duyck 6589451980aSAlexander Duyck if (pfmemalloc) 6599451980aSAlexander Duyck skb->pfmemalloc = 1; 6609451980aSAlexander Duyck skb->head_frag = 1; 6619451980aSAlexander Duyck 662a080e7bdSAlexander Duyck skb_success: 6638af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 6647b2e497aSChristoph Hellwig skb->dev = dev; 665fd11a83dSAlexander Duyck 666a080e7bdSAlexander Duyck skb_fail: 6678af27456SChristoph Hellwig return skb; 6688af27456SChristoph Hellwig } 669b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 6701da177e4SLinus Torvalds 671fd11a83dSAlexander Duyck /** 672fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 673fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 674d7499160SMasanari Iida * @len: length to allocate 675fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 676fd11a83dSAlexander Duyck * 677fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 678fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 679fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 680fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 681fd11a83dSAlexander Duyck * 682fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 683fd11a83dSAlexander Duyck */ 6849451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 6859451980aSAlexander Duyck gfp_t gfp_mask) 686fd11a83dSAlexander Duyck { 6873226b158SEric Dumazet struct napi_alloc_cache *nc; 688fd11a83dSAlexander Duyck struct sk_buff *skb; 689dbae2b06SPaolo Abeni bool pfmemalloc; 6909451980aSAlexander Duyck void *data; 691fd11a83dSAlexander Duyck 692ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 6939451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 694fd11a83dSAlexander Duyck 6953226b158SEric Dumazet /* If requested length is either too small or too big, 6963226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 697dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 698dbae2b06SPaolo Abeni * for small fragments 6993226b158SEric Dumazet */ 700dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 7013226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 702d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 703cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 704cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 705a080e7bdSAlexander Duyck if (!skb) 706a080e7bdSAlexander Duyck goto skb_fail; 707a080e7bdSAlexander Duyck goto skb_success; 708a080e7bdSAlexander Duyck } 7099451980aSAlexander Duyck 7103226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 7119451980aSAlexander Duyck 7129451980aSAlexander Duyck if (sk_memalloc_socks()) 7139451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 7149451980aSAlexander Duyck 715dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 716dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 717dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 718dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 719dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 720dbae2b06SPaolo Abeni * size above 512 bytes 721dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 722dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 723dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 724dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 725dbae2b06SPaolo Abeni */ 726dbae2b06SPaolo Abeni len = SZ_1K; 727dbae2b06SPaolo Abeni 728dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 729dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 730dbae2b06SPaolo Abeni } else { 731115f1a5cSEric Dumazet len = SKB_HEAD_ALIGN(len); 732dbae2b06SPaolo Abeni 7338c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 734dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 735dbae2b06SPaolo Abeni } 736dbae2b06SPaolo Abeni 7379451980aSAlexander Duyck if (unlikely(!data)) 7389451980aSAlexander Duyck return NULL; 7399451980aSAlexander Duyck 740cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 7419451980aSAlexander Duyck if (unlikely(!skb)) { 742181edb2bSAlexander Duyck skb_free_frag(data); 7439451980aSAlexander Duyck return NULL; 7449451980aSAlexander Duyck } 7459451980aSAlexander Duyck 746dbae2b06SPaolo Abeni if (pfmemalloc) 7479451980aSAlexander Duyck skb->pfmemalloc = 1; 7489451980aSAlexander Duyck skb->head_frag = 1; 7499451980aSAlexander Duyck 750a080e7bdSAlexander Duyck skb_success: 751fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 752fd11a83dSAlexander Duyck skb->dev = napi->dev; 753fd11a83dSAlexander Duyck 754a080e7bdSAlexander Duyck skb_fail: 755fd11a83dSAlexander Duyck return skb; 756fd11a83dSAlexander Duyck } 757fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 758fd11a83dSAlexander Duyck 759654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 76050269e19SEric Dumazet int size, unsigned int truesize) 761654bed16SPeter Zijlstra { 762654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 763654bed16SPeter Zijlstra skb->len += size; 764654bed16SPeter Zijlstra skb->data_len += size; 76550269e19SEric Dumazet skb->truesize += truesize; 766654bed16SPeter Zijlstra } 767654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 768654bed16SPeter Zijlstra 769f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 770f8e617e1SJason Wang unsigned int truesize) 771f8e617e1SJason Wang { 772f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 773f8e617e1SJason Wang 774f8e617e1SJason Wang skb_frag_size_add(frag, size); 775f8e617e1SJason Wang skb->len += size; 776f8e617e1SJason Wang skb->data_len += size; 777f8e617e1SJason Wang skb->truesize += truesize; 778f8e617e1SJason Wang } 779f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 780f8e617e1SJason Wang 78127b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 7821da177e4SLinus Torvalds { 783bd8a7036SEric Dumazet kfree_skb_list(*listp); 78427b437c8SHerbert Xu *listp = NULL; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 78727b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 78827b437c8SHerbert Xu { 78927b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 79027b437c8SHerbert Xu } 79127b437c8SHerbert Xu 7921da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 7931da177e4SLinus Torvalds { 7941da177e4SLinus Torvalds struct sk_buff *list; 7951da177e4SLinus Torvalds 796fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 7971da177e4SLinus Torvalds skb_get(list); 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8004727bab4SYunsheng Lin static bool skb_pp_recycle(struct sk_buff *skb, void *data) 8014727bab4SYunsheng Lin { 8024727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 8034727bab4SYunsheng Lin return false; 8044727bab4SYunsheng Lin return page_pool_return_skb_page(virt_to_page(data)); 8054727bab4SYunsheng Lin } 8064727bab4SYunsheng Lin 807d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 808d3836f21SEric Dumazet { 809181edb2bSAlexander Duyck unsigned char *head = skb->head; 810181edb2bSAlexander Duyck 8116a5bcd84SIlias Apalodimas if (skb->head_frag) { 8126a5bcd84SIlias Apalodimas if (skb_pp_recycle(skb, head)) 8136a5bcd84SIlias Apalodimas return; 814181edb2bSAlexander Duyck skb_free_frag(head); 8156a5bcd84SIlias Apalodimas } else { 816181edb2bSAlexander Duyck kfree(head); 817d3836f21SEric Dumazet } 8186a5bcd84SIlias Apalodimas } 819d3836f21SEric Dumazet 820511a3edaSEric Dumazet static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason) 8211da177e4SLinus Torvalds { 822ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 8231da177e4SLinus Torvalds int i; 824ff04a771SEric Dumazet 825ff04a771SEric Dumazet if (skb->cloned && 826ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 827ff04a771SEric Dumazet &shinfo->dataref)) 8282cc3aeb5SIlias Apalodimas goto exit; 829ff04a771SEric Dumazet 830753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 831753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 832753f1ca4SPavel Begunkov 83370c43167SJonathan Lemon skb_zcopy_clear(skb, true); 834753f1ca4SPavel Begunkov if (skip_unref) 835753f1ca4SPavel Begunkov goto free_head; 836753f1ca4SPavel Begunkov } 83770c43167SJonathan Lemon 838ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 8396a5bcd84SIlias Apalodimas __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 8401da177e4SLinus Torvalds 841753f1ca4SPavel Begunkov free_head: 842ff04a771SEric Dumazet if (shinfo->frag_list) 843511a3edaSEric Dumazet kfree_skb_list_reason(shinfo->frag_list, reason); 8441da177e4SLinus Torvalds 845d3836f21SEric Dumazet skb_free_head(skb); 8462cc3aeb5SIlias Apalodimas exit: 8472cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 8482cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 8492cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 8502cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 8512cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 85258e61e41STom Rix * additional references to the fragmented part of the SKB. 8532cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 8542cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 8552cc3aeb5SIlias Apalodimas */ 8562cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* 8601da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 8611da177e4SLinus Torvalds */ 8622d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 8631da177e4SLinus Torvalds { 864d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 865d179cd12SDavid S. Miller 866d179cd12SDavid S. Miller switch (skb->fclone) { 867d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 8681da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 8696ffe75ebSEric Dumazet return; 870d179cd12SDavid S. Miller 871d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 872d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 8736ffe75ebSEric Dumazet 8746ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 8756ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 8766ffe75ebSEric Dumazet * while here, branch prediction will be good. 8776ffe75ebSEric Dumazet */ 8782638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 8796ffe75ebSEric Dumazet goto fastpath; 880d179cd12SDavid S. Miller break; 881d179cd12SDavid S. Miller 8826ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 883d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 884d179cd12SDavid S. Miller break; 8853ff50b79SStephen Hemminger } 8862638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 8876ffe75ebSEric Dumazet return; 8886ffe75ebSEric Dumazet fastpath: 8896ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8920a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 8931da177e4SLinus Torvalds { 894adf30907SEric Dumazet skb_dst_drop(skb); 8951da177e4SLinus Torvalds if (skb->destructor) { 8967890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 8971da177e4SLinus Torvalds skb->destructor(skb); 8981da177e4SLinus Torvalds } 899a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 900cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 9012fc72c7bSKOVACS Krisztian #endif 902df5042f4SFlorian Westphal skb_ext_put(skb); 90304a4bb55SLennert Buytenhek } 90404a4bb55SLennert Buytenhek 90504a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 906511a3edaSEric Dumazet static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason) 90704a4bb55SLennert Buytenhek { 90804a4bb55SLennert Buytenhek skb_release_head_state(skb); 909a28b1b90SFlorian Westphal if (likely(skb->head)) 910511a3edaSEric Dumazet skb_release_data(skb, reason); 9112d4baff8SHerbert Xu } 9121da177e4SLinus Torvalds 9132d4baff8SHerbert Xu /** 9142d4baff8SHerbert Xu * __kfree_skb - private function 9152d4baff8SHerbert Xu * @skb: buffer 9162d4baff8SHerbert Xu * 9172d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 9182d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 9192d4baff8SHerbert Xu * always call kfree_skb 9202d4baff8SHerbert Xu */ 9212d4baff8SHerbert Xu 9222d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 9232d4baff8SHerbert Xu { 924511a3edaSEric Dumazet skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 9251da177e4SLinus Torvalds kfree_skbmem(skb); 9261da177e4SLinus Torvalds } 927b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 9281da177e4SLinus Torvalds 929a4650da2SJesper Dangaard Brouer static __always_inline 930a4650da2SJesper Dangaard Brouer bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 931a4650da2SJesper Dangaard Brouer { 932a4650da2SJesper Dangaard Brouer if (unlikely(!skb_unref(skb))) 933a4650da2SJesper Dangaard Brouer return false; 934a4650da2SJesper Dangaard Brouer 935a4650da2SJesper Dangaard Brouer DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX); 936a4650da2SJesper Dangaard Brouer 937a4650da2SJesper Dangaard Brouer if (reason == SKB_CONSUMED) 938a4650da2SJesper Dangaard Brouer trace_consume_skb(skb); 939a4650da2SJesper Dangaard Brouer else 940a4650da2SJesper Dangaard Brouer trace_kfree_skb(skb, __builtin_return_address(0), reason); 941a4650da2SJesper Dangaard Brouer return true; 942a4650da2SJesper Dangaard Brouer } 943a4650da2SJesper Dangaard Brouer 9441da177e4SLinus Torvalds /** 945c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 946231d06aeSJörn Engel * @skb: buffer to free 947c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 948231d06aeSJörn Engel * 949231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 950c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 951c504e5c2SMenglong Dong * tracepoint. 952231d06aeSJörn Engel */ 953c205cc75SMenglong Dong void __fix_address 954c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 955231d06aeSJörn Engel { 956a4650da2SJesper Dangaard Brouer if (__kfree_skb_reason(skb, reason)) 957231d06aeSJörn Engel __kfree_skb(skb); 958231d06aeSJörn Engel } 959c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 960231d06aeSJörn Engel 961eedade12SJesper Dangaard Brouer #define KFREE_SKB_BULK_SIZE 16 962eedade12SJesper Dangaard Brouer 963eedade12SJesper Dangaard Brouer struct skb_free_array { 964eedade12SJesper Dangaard Brouer unsigned int skb_count; 965eedade12SJesper Dangaard Brouer void *skb_array[KFREE_SKB_BULK_SIZE]; 966eedade12SJesper Dangaard Brouer }; 967eedade12SJesper Dangaard Brouer 968eedade12SJesper Dangaard Brouer static void kfree_skb_add_bulk(struct sk_buff *skb, 969eedade12SJesper Dangaard Brouer struct skb_free_array *sa, 970eedade12SJesper Dangaard Brouer enum skb_drop_reason reason) 971eedade12SJesper Dangaard Brouer { 972eedade12SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 973eedade12SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 974eedade12SJesper Dangaard Brouer __kfree_skb(skb); 975eedade12SJesper Dangaard Brouer return; 976eedade12SJesper Dangaard Brouer } 977eedade12SJesper Dangaard Brouer 978eedade12SJesper Dangaard Brouer skb_release_all(skb, reason); 979eedade12SJesper Dangaard Brouer sa->skb_array[sa->skb_count++] = skb; 980eedade12SJesper Dangaard Brouer 981eedade12SJesper Dangaard Brouer if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 982eedade12SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, KFREE_SKB_BULK_SIZE, 983eedade12SJesper Dangaard Brouer sa->skb_array); 984eedade12SJesper Dangaard Brouer sa->skb_count = 0; 985eedade12SJesper Dangaard Brouer } 986eedade12SJesper Dangaard Brouer } 987eedade12SJesper Dangaard Brouer 988a4650da2SJesper Dangaard Brouer void __fix_address 989a4650da2SJesper Dangaard Brouer kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 990bd8a7036SEric Dumazet { 991eedade12SJesper Dangaard Brouer struct skb_free_array sa; 992eedade12SJesper Dangaard Brouer 993eedade12SJesper Dangaard Brouer sa.skb_count = 0; 994eedade12SJesper Dangaard Brouer 995bd8a7036SEric Dumazet while (segs) { 996bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 997bd8a7036SEric Dumazet 9989dde0cd3SJesper Dangaard Brouer if (__kfree_skb_reason(segs, reason)) { 9999dde0cd3SJesper Dangaard Brouer skb_poison_list(segs); 1000eedade12SJesper Dangaard Brouer kfree_skb_add_bulk(segs, &sa, reason); 10019dde0cd3SJesper Dangaard Brouer } 1002eedade12SJesper Dangaard Brouer 1003bd8a7036SEric Dumazet segs = next; 1004bd8a7036SEric Dumazet } 1005eedade12SJesper Dangaard Brouer 1006eedade12SJesper Dangaard Brouer if (sa.skb_count) 1007eedade12SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, sa.skb_count, 1008eedade12SJesper Dangaard Brouer sa.skb_array); 1009bd8a7036SEric Dumazet } 1010215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 1011bd8a7036SEric Dumazet 10126413139dSWillem de Bruijn /* Dump skb information and contents. 10136413139dSWillem de Bruijn * 10146413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 10156413139dSWillem de Bruijn * 1016302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 10176413139dSWillem de Bruijn */ 10186413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 10196413139dSWillem de Bruijn { 10206413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 10216413139dSWillem de Bruijn struct net_device *dev = skb->dev; 10226413139dSWillem de Bruijn struct sock *sk = skb->sk; 10236413139dSWillem de Bruijn struct sk_buff *list_skb; 10246413139dSWillem de Bruijn bool has_mac, has_trans; 10256413139dSWillem de Bruijn int headroom, tailroom; 10266413139dSWillem de Bruijn int i, len, seg_len; 10276413139dSWillem de Bruijn 10286413139dSWillem de Bruijn if (full_pkt) 10296413139dSWillem de Bruijn len = skb->len; 10306413139dSWillem de Bruijn else 10316413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 10326413139dSWillem de Bruijn 10336413139dSWillem de Bruijn headroom = skb_headroom(skb); 10346413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 10356413139dSWillem de Bruijn 10366413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 10376413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 10386413139dSWillem de Bruijn 10396413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 10406413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 10416413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 10426413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 10436413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 10446413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 10456413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 10466413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 10476413139dSWillem de Bruijn skb->network_header, 10486413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 10496413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 10506413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 10516413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 10526413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 10536413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 10546413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 10556413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 10566413139dSWillem de Bruijn 10576413139dSWillem de Bruijn if (dev) 10588a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 10596413139dSWillem de Bruijn level, dev->name, &dev->features); 10606413139dSWillem de Bruijn if (sk) 1061db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 10626413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 10636413139dSWillem de Bruijn 10646413139dSWillem de Bruijn if (full_pkt && headroom) 10656413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 10666413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 10676413139dSWillem de Bruijn 10686413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 10696413139dSWillem de Bruijn if (seg_len) 10706413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 10716413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 10726413139dSWillem de Bruijn len -= seg_len; 10736413139dSWillem de Bruijn 10746413139dSWillem de Bruijn if (full_pkt && tailroom) 10756413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 10766413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 10776413139dSWillem de Bruijn 10786413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 10796413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 10806413139dSWillem de Bruijn u32 p_off, p_len, copied; 10816413139dSWillem de Bruijn struct page *p; 10826413139dSWillem de Bruijn u8 *vaddr; 10836413139dSWillem de Bruijn 1084b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 10856413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 10866413139dSWillem de Bruijn copied) { 10876413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 10886413139dSWillem de Bruijn vaddr = kmap_atomic(p); 10896413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 10906413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 10916413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 10926413139dSWillem de Bruijn kunmap_atomic(vaddr); 10936413139dSWillem de Bruijn len -= seg_len; 10946413139dSWillem de Bruijn if (!len) 10956413139dSWillem de Bruijn break; 10966413139dSWillem de Bruijn } 10976413139dSWillem de Bruijn } 10986413139dSWillem de Bruijn 10996413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 11006413139dSWillem de Bruijn printk("skb fraglist:\n"); 11016413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 11026413139dSWillem de Bruijn skb_dump(level, list_skb, true); 11036413139dSWillem de Bruijn } 11046413139dSWillem de Bruijn } 11056413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 11066413139dSWillem de Bruijn 1107d1a203eaSStephen Hemminger /** 110825121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 110925121173SMichael S. Tsirkin * @skb: buffer that triggered an error 111025121173SMichael S. Tsirkin * 111125121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 111225121173SMichael S. Tsirkin * skb must be freed afterwards. 111325121173SMichael S. Tsirkin */ 111425121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 111525121173SMichael S. Tsirkin { 1116753f1ca4SPavel Begunkov if (skb) { 1117753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 11181f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 111925121173SMichael S. Tsirkin } 1120753f1ca4SPavel Begunkov } 112125121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 112225121173SMichael S. Tsirkin 1123be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 112425121173SMichael S. Tsirkin /** 1125ead2ceb0SNeil Horman * consume_skb - free an skbuff 1126ead2ceb0SNeil Horman * @skb: buffer to free 1127ead2ceb0SNeil Horman * 1128ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1129ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1130ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1131ead2ceb0SNeil Horman */ 1132ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1133ead2ceb0SNeil Horman { 11343889a803SPaolo Abeni if (!skb_unref(skb)) 1135ead2ceb0SNeil Horman return; 11363889a803SPaolo Abeni 113707dc22e7SKoki Sanagi trace_consume_skb(skb); 1138ead2ceb0SNeil Horman __kfree_skb(skb); 1139ead2ceb0SNeil Horman } 1140ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1141be769db2SHerbert Xu #endif 1142ead2ceb0SNeil Horman 11430a463c78SPaolo Abeni /** 1144c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 11450a463c78SPaolo Abeni * @skb: buffer to free 11460a463c78SPaolo Abeni * 1147ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1148ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 11490a463c78SPaolo Abeni */ 1150ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 11510a463c78SPaolo Abeni { 11520a463c78SPaolo Abeni trace_consume_skb(skb); 1153511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 11540a463c78SPaolo Abeni kfree_skbmem(skb); 11550a463c78SPaolo Abeni } 11560a463c78SPaolo Abeni 1157f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1158795bb1c0SJesper Dangaard Brouer { 1159795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1160f450d539SAlexander Lobakin u32 i; 1161795bb1c0SJesper Dangaard Brouer 1162f450d539SAlexander Lobakin kasan_poison_object_data(skbuff_head_cache, skb); 1163795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1164795bb1c0SJesper Dangaard Brouer 1165795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1166f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1167f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, 1168f450d539SAlexander Lobakin nc->skb_cache[i]); 1169f450d539SAlexander Lobakin 1170f450d539SAlexander Lobakin kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 1171f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1172f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1173795bb1c0SJesper Dangaard Brouer } 1174795bb1c0SJesper Dangaard Brouer } 1175f450d539SAlexander Lobakin 117615fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 117715fad714SJesper Dangaard Brouer { 1178511a3edaSEric Dumazet skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 11799243adfcSAlexander Lobakin napi_skb_cache_put(skb); 11809243adfcSAlexander Lobakin } 11819243adfcSAlexander Lobakin 11829243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 11839243adfcSAlexander Lobakin { 11849efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 11858550ff8dSPaul Blakey nf_reset_ct(skb); 11869243adfcSAlexander Lobakin skb_dst_drop(skb); 11879243adfcSAlexander Lobakin skb_ext_put(skb); 11885e10da53SPaolo Abeni skb_orphan(skb); 11899efb4b5bSPaolo Abeni skb->slow_gro = 0; 11909efb4b5bSPaolo Abeni } 1191f450d539SAlexander Lobakin napi_skb_cache_put(skb); 119215fad714SJesper Dangaard Brouer } 1193795bb1c0SJesper Dangaard Brouer 1194795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1195795bb1c0SJesper Dangaard Brouer { 1196885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1197795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1198885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1199795bb1c0SJesper Dangaard Brouer return; 1200795bb1c0SJesper Dangaard Brouer } 1201795bb1c0SJesper Dangaard Brouer 1202ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 12036454eca8SYunsheng Lin 12047608894eSPaolo Abeni if (!skb_unref(skb)) 1205795bb1c0SJesper Dangaard Brouer return; 12067608894eSPaolo Abeni 1207795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1208795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 1209795bb1c0SJesper Dangaard Brouer 1210795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1211abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1212795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1213795bb1c0SJesper Dangaard Brouer return; 1214795bb1c0SJesper Dangaard Brouer } 1215795bb1c0SJesper Dangaard Brouer 1216511a3edaSEric Dumazet skb_release_all(skb, SKB_CONSUMED); 1217f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1218795bb1c0SJesper Dangaard Brouer } 1219795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1220795bb1c0SJesper Dangaard Brouer 122103f61041SKees Cook /* Make sure a field is contained by headers group */ 1222b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 122303f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 122403f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1225b1937227SEric Dumazet 1226dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1227dec18810SHerbert Xu { 1228dec18810SHerbert Xu new->tstamp = old->tstamp; 1229b1937227SEric Dumazet /* We do not copy old->sk */ 1230dec18810SHerbert Xu new->dev = old->dev; 1231b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 12327fee226aSEric Dumazet skb_dst_copy(new, old); 1233df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1234b1937227SEric Dumazet __nf_copy(new, old, false); 12356aa895b0SPatrick McHardy 123603f61041SKees Cook /* Note : this field could be in the headers group. 1237b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1238b1937227SEric Dumazet */ 1239b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 124006021292SEliezer Tamir 124103f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1242b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1243b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1244b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1245b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1246b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1247b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1248b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1249b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1250b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1251b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1252b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1253b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1254b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1255b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1256b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1257b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1258b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1259b1937227SEric Dumazet #endif 1260e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1261b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 126206021292SEliezer Tamir #endif 126368822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 12642bd82484SEric Dumazet #ifdef CONFIG_XPS 12652bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 12662bd82484SEric Dumazet #endif 1267b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1268b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1269b1937227SEric Dumazet #endif 1270b1937227SEric Dumazet 1271dec18810SHerbert Xu } 1272dec18810SHerbert Xu 127382c49a35SHerbert Xu /* 127482c49a35SHerbert Xu * You should not add any new code to this function. Add it to 127582c49a35SHerbert Xu * __copy_skb_header above instead. 127682c49a35SHerbert Xu */ 1277e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds #define C(x) n->x = skb->x 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds n->next = n->prev = NULL; 12821da177e4SLinus Torvalds n->sk = NULL; 1283dec18810SHerbert Xu __copy_skb_header(n, skb); 1284dec18810SHerbert Xu 12851da177e4SLinus Torvalds C(len); 12861da177e4SLinus Torvalds C(data_len); 12873e6b3b2eSAlexey Dobriyan C(mac_len); 1288334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 128902f1c89dSPaul Moore n->cloned = 1; 12901da177e4SLinus Torvalds n->nohdr = 0; 1291b13dda9fSEric Dumazet n->peeked = 0; 1292e78bfb07SStefano Brivio C(pfmemalloc); 12936a5bcd84SIlias Apalodimas C(pp_recycle); 12941da177e4SLinus Torvalds n->destructor = NULL; 12951da177e4SLinus Torvalds C(tail); 12961da177e4SLinus Torvalds C(end); 129702f1c89dSPaul Moore C(head); 1298d3836f21SEric Dumazet C(head_frag); 129902f1c89dSPaul Moore C(data); 130002f1c89dSPaul Moore C(truesize); 130163354797SReshetova, Elena refcount_set(&n->users, 1); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 13041da177e4SLinus Torvalds skb->cloned = 1; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds return n; 1307e0053ec0SHerbert Xu #undef C 1308e0053ec0SHerbert Xu } 1309e0053ec0SHerbert Xu 1310e0053ec0SHerbert Xu /** 1311da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1312da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1313da29e4b4SJakub Kicinski */ 1314da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1315da29e4b4SJakub Kicinski { 1316da29e4b4SJakub Kicinski struct sk_buff *n; 1317da29e4b4SJakub Kicinski 1318da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1319da29e4b4SJakub Kicinski if (!n) 1320da29e4b4SJakub Kicinski return NULL; 1321da29e4b4SJakub Kicinski 1322da29e4b4SJakub Kicinski n->len = first->len; 1323da29e4b4SJakub Kicinski n->data_len = first->len; 1324da29e4b4SJakub Kicinski n->truesize = first->truesize; 1325da29e4b4SJakub Kicinski 1326da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1327da29e4b4SJakub Kicinski 1328da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1329da29e4b4SJakub Kicinski n->destructor = NULL; 1330da29e4b4SJakub Kicinski 1331da29e4b4SJakub Kicinski return n; 1332da29e4b4SJakub Kicinski } 1333da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1334da29e4b4SJakub Kicinski 1335da29e4b4SJakub Kicinski /** 1336e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1337e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1338e0053ec0SHerbert Xu * @src: the skb to supply the contents 1339e0053ec0SHerbert Xu * 1340e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1341e0053ec0SHerbert Xu * supplied by the user. 1342e0053ec0SHerbert Xu * 1343e0053ec0SHerbert Xu * The target skb is returned upon exit. 1344e0053ec0SHerbert Xu */ 1345e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1346e0053ec0SHerbert Xu { 1347511a3edaSEric Dumazet skb_release_all(dst, SKB_CONSUMED); 1348e0053ec0SHerbert Xu return __skb_clone(dst, src); 1349e0053ec0SHerbert Xu } 1350e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1351e0053ec0SHerbert Xu 13526f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1353a91dbff5SWillem de Bruijn { 1354a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1355a91dbff5SWillem de Bruijn struct user_struct *user; 1356a91dbff5SWillem de Bruijn 1357a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1358a91dbff5SWillem de Bruijn return 0; 1359a91dbff5SWillem de Bruijn 1360a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1361a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1362a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1363a91dbff5SWillem de Bruijn 1364a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 136557fc05e8SEric Dumazet do { 1366a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1367a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1368a91dbff5SWillem de Bruijn return -ENOBUFS; 136957fc05e8SEric Dumazet } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1370a91dbff5SWillem de Bruijn 1371a91dbff5SWillem de Bruijn if (!mmp->user) { 1372a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1373a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1374a91dbff5SWillem de Bruijn } else { 1375a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1376a91dbff5SWillem de Bruijn } 1377a91dbff5SWillem de Bruijn 1378a91dbff5SWillem de Bruijn return 0; 1379a91dbff5SWillem de Bruijn } 13806f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1381a91dbff5SWillem de Bruijn 13826f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1383a91dbff5SWillem de Bruijn { 1384a91dbff5SWillem de Bruijn if (mmp->user) { 1385a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1386a91dbff5SWillem de Bruijn free_uid(mmp->user); 1387a91dbff5SWillem de Bruijn } 1388a91dbff5SWillem de Bruijn } 13896f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1390a91dbff5SWillem de Bruijn 1391c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 139252267790SWillem de Bruijn { 1393e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 139452267790SWillem de Bruijn struct sk_buff *skb; 139552267790SWillem de Bruijn 139652267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 139752267790SWillem de Bruijn 139852267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 139952267790SWillem de Bruijn if (!skb) 140052267790SWillem de Bruijn return NULL; 140152267790SWillem de Bruijn 140252267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 140352267790SWillem de Bruijn uarg = (void *)skb->cb; 1404a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1405a91dbff5SWillem de Bruijn 1406a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1407a91dbff5SWillem de Bruijn kfree_skb(skb); 1408a91dbff5SWillem de Bruijn return NULL; 1409a91dbff5SWillem de Bruijn } 141052267790SWillem de Bruijn 1411e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 14124ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 14134ab6c99dSWillem de Bruijn uarg->len = 1; 14144ab6c99dSWillem de Bruijn uarg->bytelen = size; 141552267790SWillem de Bruijn uarg->zerocopy = 1; 1416e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1417e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 141852267790SWillem de Bruijn sock_hold(sk); 141952267790SWillem de Bruijn 1420e7d2b510SPavel Begunkov return &uarg->ubuf; 142152267790SWillem de Bruijn } 142252267790SWillem de Bruijn 1423e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 142452267790SWillem de Bruijn { 142552267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 142652267790SWillem de Bruijn } 142752267790SWillem de Bruijn 14288c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 14294ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 14304ab6c99dSWillem de Bruijn { 14314ab6c99dSWillem de Bruijn if (uarg) { 1432e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 14334ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 14344ab6c99dSWillem de Bruijn u32 bytelen, next; 14354ab6c99dSWillem de Bruijn 14361b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 14371b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 14381b4b2b09SPavel Begunkov return NULL; 14391b4b2b09SPavel Begunkov 14404ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 14414ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 14424ab6c99dSWillem de Bruijn */ 14434ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 14444ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 14454ab6c99dSWillem de Bruijn return NULL; 14464ab6c99dSWillem de Bruijn } 14474ab6c99dSWillem de Bruijn 1448e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1449e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1450e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 14514ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 14524ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 14534ab6c99dSWillem de Bruijn goto new_alloc; 14544ab6c99dSWillem de Bruijn return NULL; 14554ab6c99dSWillem de Bruijn } 14564ab6c99dSWillem de Bruijn 14574ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1458e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1459e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1460a91dbff5SWillem de Bruijn return NULL; 1461e7d2b510SPavel Begunkov uarg_zc->len++; 1462e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 14634ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1464100f6d8eSWillem de Bruijn 1465100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1466100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 14678e044917SJonathan Lemon net_zcopy_get(uarg); 1468100f6d8eSWillem de Bruijn 14694ab6c99dSWillem de Bruijn return uarg; 14704ab6c99dSWillem de Bruijn } 14714ab6c99dSWillem de Bruijn } 14724ab6c99dSWillem de Bruijn 14734ab6c99dSWillem de Bruijn new_alloc: 14748c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 14754ab6c99dSWillem de Bruijn } 14768c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 14774ab6c99dSWillem de Bruijn 14784ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 14794ab6c99dSWillem de Bruijn { 14804ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 14814ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 14824ab6c99dSWillem de Bruijn u64 sum_len; 14834ab6c99dSWillem de Bruijn 14844ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 14854ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 14864ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 14874ab6c99dSWillem de Bruijn 14884ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 14894ab6c99dSWillem de Bruijn return false; 14904ab6c99dSWillem de Bruijn 14914ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 14924ab6c99dSWillem de Bruijn return false; 14934ab6c99dSWillem de Bruijn 14944ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 14954ab6c99dSWillem de Bruijn return true; 14964ab6c99dSWillem de Bruijn } 14974ab6c99dSWillem de Bruijn 1498e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 149952267790SWillem de Bruijn { 15004ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 150152267790SWillem de Bruijn struct sock_exterr_skb *serr; 150252267790SWillem de Bruijn struct sock *sk = skb->sk; 15034ab6c99dSWillem de Bruijn struct sk_buff_head *q; 15044ab6c99dSWillem de Bruijn unsigned long flags; 15053bdd5ee0SWillem de Bruijn bool is_zerocopy; 15064ab6c99dSWillem de Bruijn u32 lo, hi; 15074ab6c99dSWillem de Bruijn u16 len; 150852267790SWillem de Bruijn 1509ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1510ccaffff1SWillem de Bruijn 15114ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 15124ab6c99dSWillem de Bruijn * so do not queue a completion notification 15134ab6c99dSWillem de Bruijn */ 15144ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 151552267790SWillem de Bruijn goto release; 151652267790SWillem de Bruijn 15174ab6c99dSWillem de Bruijn len = uarg->len; 15184ab6c99dSWillem de Bruijn lo = uarg->id; 15194ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 15203bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 15214ab6c99dSWillem de Bruijn 152252267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 152352267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 152452267790SWillem de Bruijn serr->ee.ee_errno = 0; 152552267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 15264ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 15274ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 15283bdd5ee0SWillem de Bruijn if (!is_zerocopy) 152952267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 153052267790SWillem de Bruijn 15314ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 15324ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 15334ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 15344ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 15354ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 15364ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 153752267790SWillem de Bruijn skb = NULL; 15384ab6c99dSWillem de Bruijn } 15394ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 154052267790SWillem de Bruijn 1541e3ae2365SAlexander Aring sk_error_report(sk); 154252267790SWillem de Bruijn 154352267790SWillem de Bruijn release: 154452267790SWillem de Bruijn consume_skb(skb); 154552267790SWillem de Bruijn sock_put(sk); 154652267790SWillem de Bruijn } 154775518851SJonathan Lemon 15488c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 154936177832SJonathan Lemon bool success) 155075518851SJonathan Lemon { 1551e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1552e7d2b510SPavel Begunkov 1553e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 155475518851SJonathan Lemon 155575518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1556e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 155775518851SJonathan Lemon } 15588c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 155952267790SWillem de Bruijn 15608c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 156152267790SWillem de Bruijn { 1562e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 156352267790SWillem de Bruijn 156452267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1565e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 156652267790SWillem de Bruijn 156752900d22SWillem de Bruijn if (have_uref) 15688c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 156952267790SWillem de Bruijn } 15708c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 157152267790SWillem de Bruijn 157252267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 157352267790SWillem de Bruijn struct msghdr *msg, int len, 157452267790SWillem de Bruijn struct ubuf_info *uarg) 157552267790SWillem de Bruijn { 15764ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 157752267790SWillem de Bruijn int err, orig_len = skb->len; 157852267790SWillem de Bruijn 15794ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 15804ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 15814ab6c99dSWillem de Bruijn */ 15824ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 15834ab6c99dSWillem de Bruijn return -EEXIST; 15844ab6c99dSWillem de Bruijn 1585ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 158652267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 158754d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 158854d43117SWillem de Bruijn 158952267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1590829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 159154d43117SWillem de Bruijn skb->sk = sk; 159252267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 159354d43117SWillem de Bruijn skb->sk = save_sk; 159452267790SWillem de Bruijn return err; 159552267790SWillem de Bruijn } 159652267790SWillem de Bruijn 159752900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 159852267790SWillem de Bruijn return skb->len - orig_len; 159952267790SWillem de Bruijn } 160052267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 160152267790SWillem de Bruijn 1602753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1603753f1ca4SPavel Begunkov { 1604753f1ca4SPavel Begunkov int i; 1605753f1ca4SPavel Begunkov 1606753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1607753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1608753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1609753f1ca4SPavel Begunkov } 1610753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1611753f1ca4SPavel Begunkov 16121f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 161352267790SWillem de Bruijn gfp_t gfp_mask) 161452267790SWillem de Bruijn { 161552267790SWillem de Bruijn if (skb_zcopy(orig)) { 161652267790SWillem de Bruijn if (skb_zcopy(nskb)) { 161752267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 161852267790SWillem de Bruijn if (!gfp_mask) { 161952267790SWillem de Bruijn WARN_ON_ONCE(1); 162052267790SWillem de Bruijn return -ENOMEM; 162152267790SWillem de Bruijn } 162252267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 162352267790SWillem de Bruijn return 0; 162452267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 162552267790SWillem de Bruijn return -EIO; 162652267790SWillem de Bruijn } 162752900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 162852267790SWillem de Bruijn } 162952267790SWillem de Bruijn return 0; 163052267790SWillem de Bruijn } 163152267790SWillem de Bruijn 16322c53040fSBen Hutchings /** 16332c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 163448c83012SMichael S. Tsirkin * @skb: the skb to modify 163548c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 163648c83012SMichael S. Tsirkin * 163706b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 163848c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 163948c83012SMichael S. Tsirkin * to userspace pages. 164048c83012SMichael S. Tsirkin * 164148c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 164248c83012SMichael S. Tsirkin * %GFP_ATOMIC. 164348c83012SMichael S. Tsirkin * 164448c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 164548c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 164648c83012SMichael S. Tsirkin */ 164748c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1648a6686f2fSShirley Ma { 1649a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1650a6686f2fSShirley Ma struct page *page, *head = NULL; 16513ece7826SWillem de Bruijn int i, new_frags; 16523ece7826SWillem de Bruijn u32 d_off; 1653a6686f2fSShirley Ma 16543ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 16553ece7826SWillem de Bruijn return -EINVAL; 16563ece7826SWillem de Bruijn 1657f72c4ac6SWillem de Bruijn if (!num_frags) 1658f72c4ac6SWillem de Bruijn goto release; 1659f72c4ac6SWillem de Bruijn 16603ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 16613ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 166202756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1663a6686f2fSShirley Ma if (!page) { 1664a6686f2fSShirley Ma while (head) { 166540dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1666a6686f2fSShirley Ma put_page(head); 1667a6686f2fSShirley Ma head = next; 1668a6686f2fSShirley Ma } 1669a6686f2fSShirley Ma return -ENOMEM; 1670a6686f2fSShirley Ma } 16713ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 16723ece7826SWillem de Bruijn head = page; 16733ece7826SWillem de Bruijn } 16743ece7826SWillem de Bruijn 16753ece7826SWillem de Bruijn page = head; 16763ece7826SWillem de Bruijn d_off = 0; 16773ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 16783ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 16793ece7826SWillem de Bruijn u32 p_off, p_len, copied; 16803ece7826SWillem de Bruijn struct page *p; 16813ece7826SWillem de Bruijn u8 *vaddr; 1682c613c209SWillem de Bruijn 1683b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1684c613c209SWillem de Bruijn p, p_off, p_len, copied) { 16853ece7826SWillem de Bruijn u32 copy, done = 0; 1686c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 16873ece7826SWillem de Bruijn 16883ece7826SWillem de Bruijn while (done < p_len) { 16893ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 16903ece7826SWillem de Bruijn d_off = 0; 16913ece7826SWillem de Bruijn page = (struct page *)page_private(page); 16923ece7826SWillem de Bruijn } 16933ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 16943ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 16953ece7826SWillem de Bruijn vaddr + p_off + done, copy); 16963ece7826SWillem de Bruijn done += copy; 16973ece7826SWillem de Bruijn d_off += copy; 16983ece7826SWillem de Bruijn } 169951c56b00SEric Dumazet kunmap_atomic(vaddr); 1700c613c209SWillem de Bruijn } 1701a6686f2fSShirley Ma } 1702a6686f2fSShirley Ma 1703a6686f2fSShirley Ma /* skb frags release userspace buffers */ 170402756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1705a8605c60SIan Campbell skb_frag_unref(skb, i); 1706a6686f2fSShirley Ma 1707a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 17083ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 17093ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 171040dadff2SSunghan Suh head = (struct page *)page_private(head); 1711a6686f2fSShirley Ma } 17123ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 17133ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 171448c83012SMichael S. Tsirkin 1715b90ddd56SWillem de Bruijn release: 17161f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1717a6686f2fSShirley Ma return 0; 1718a6686f2fSShirley Ma } 1719dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1720a6686f2fSShirley Ma 1721e0053ec0SHerbert Xu /** 1722e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1723e0053ec0SHerbert Xu * @skb: buffer to clone 1724e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1725e0053ec0SHerbert Xu * 1726e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1727e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1728e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1729e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1730e0053ec0SHerbert Xu * 1731e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1732e0053ec0SHerbert Xu * %GFP_ATOMIC. 1733e0053ec0SHerbert Xu */ 1734e0053ec0SHerbert Xu 1735e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1736e0053ec0SHerbert Xu { 1737d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1738d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1739d0bf4a9eSEric Dumazet skb1); 17406ffe75ebSEric Dumazet struct sk_buff *n; 1741e0053ec0SHerbert Xu 174270008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1743a6686f2fSShirley Ma return NULL; 1744a6686f2fSShirley Ma 1745e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 17462638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 17476ffe75ebSEric Dumazet n = &fclones->skb2; 17482638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1749c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1750e0053ec0SHerbert Xu } else { 1751c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1752c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1753c93bdd0eSMel Gorman 1754e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1755e0053ec0SHerbert Xu if (!n) 1756e0053ec0SHerbert Xu return NULL; 1757fe55f6d5SVegard Nossum 1758e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1759e0053ec0SHerbert Xu } 1760e0053ec0SHerbert Xu 1761e0053ec0SHerbert Xu return __skb_clone(n, skb); 17621da177e4SLinus Torvalds } 1763b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 17641da177e4SLinus Torvalds 1765b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1766f5b17294SPravin B Shelar { 1767030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1768030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1769030737bcSEric Dumazet skb->csum_start += off; 1770f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1771f5b17294SPravin B Shelar skb->transport_header += off; 1772f5b17294SPravin B Shelar skb->network_header += off; 1773f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1774f5b17294SPravin B Shelar skb->mac_header += off; 1775f5b17294SPravin B Shelar skb->inner_transport_header += off; 1776f5b17294SPravin B Shelar skb->inner_network_header += off; 1777aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1778f5b17294SPravin B Shelar } 1779b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1780f5b17294SPravin B Shelar 178108303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 17821da177e4SLinus Torvalds { 1783dec18810SHerbert Xu __copy_skb_header(new, old); 1784dec18810SHerbert Xu 17857967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 17867967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 17877967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 17881da177e4SLinus Torvalds } 178908303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 17901da177e4SLinus Torvalds 1791c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1792c93bdd0eSMel Gorman { 1793c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1794c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1795c93bdd0eSMel Gorman return 0; 1796c93bdd0eSMel Gorman } 1797c93bdd0eSMel Gorman 17981da177e4SLinus Torvalds /** 17991da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 18001da177e4SLinus Torvalds * @skb: buffer to copy 18011da177e4SLinus Torvalds * @gfp_mask: allocation priority 18021da177e4SLinus Torvalds * 18031da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 18041da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 18051da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 18061da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 18071da177e4SLinus Torvalds * 18081da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 18091da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 18101da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 18111da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 18121da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 18131da177e4SLinus Torvalds */ 18141da177e4SLinus Torvalds 1815dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 18161da177e4SLinus Torvalds { 18176602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1818ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1819c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1820c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 18216602cebbSEric Dumazet 18221da177e4SLinus Torvalds if (!n) 18231da177e4SLinus Torvalds return NULL; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds /* Set the data pointer */ 18261da177e4SLinus Torvalds skb_reserve(n, headerlen); 18271da177e4SLinus Torvalds /* Set the tail pointer and length */ 18281da177e4SLinus Torvalds skb_put(n, skb->len); 18291da177e4SLinus Torvalds 18309f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 18311da177e4SLinus Torvalds 183208303c18SIlya Lesokhin skb_copy_header(n, skb); 18331da177e4SLinus Torvalds return n; 18341da177e4SLinus Torvalds } 1835b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds /** 1838bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 18391da177e4SLinus Torvalds * @skb: buffer to copy 1840117632e6SEric Dumazet * @headroom: headroom of new skb 18411da177e4SLinus Torvalds * @gfp_mask: allocation priority 1842bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1843bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1844bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 18451da177e4SLinus Torvalds * 18461da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 18471da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 18481da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 18491da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 18501da177e4SLinus Torvalds * or the pointer to the buffer on success. 18511da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 18521da177e4SLinus Torvalds */ 18531da177e4SLinus Torvalds 1854bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1855bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 18561da177e4SLinus Torvalds { 1857117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1858bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1859bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 18606602cebbSEric Dumazet 18611da177e4SLinus Torvalds if (!n) 18621da177e4SLinus Torvalds goto out; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds /* Set the data pointer */ 1865117632e6SEric Dumazet skb_reserve(n, headroom); 18661da177e4SLinus Torvalds /* Set the tail pointer and length */ 18671da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 18681da177e4SLinus Torvalds /* Copy the bytes */ 1869d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 18701da177e4SLinus Torvalds 187125f484a6SHerbert Xu n->truesize += skb->data_len; 18721da177e4SLinus Torvalds n->data_len = skb->data_len; 18731da177e4SLinus Torvalds n->len = skb->len; 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 18761da177e4SLinus Torvalds int i; 18771da177e4SLinus Torvalds 18781f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 18791f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 18801511022cSDan Carpenter kfree_skb(n); 18811511022cSDan Carpenter n = NULL; 1882a6686f2fSShirley Ma goto out; 1883a6686f2fSShirley Ma } 18841da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 18851da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1886ea2ab693SIan Campbell skb_frag_ref(skb, i); 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 189121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 18921da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 18931da177e4SLinus Torvalds skb_clone_fraglist(n); 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 189608303c18SIlya Lesokhin skb_copy_header(n, skb); 18971da177e4SLinus Torvalds out: 18981da177e4SLinus Torvalds return n; 18991da177e4SLinus Torvalds } 1900bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /** 19031da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 19041da177e4SLinus Torvalds * @skb: buffer to reallocate 19051da177e4SLinus Torvalds * @nhead: room to add at head 19061da177e4SLinus Torvalds * @ntail: room to add at tail 19071da177e4SLinus Torvalds * @gfp_mask: allocation priority 19081da177e4SLinus Torvalds * 1909bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1910bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 19111da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 19121da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 19131da177e4SLinus Torvalds * 19141da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 19151da177e4SLinus Torvalds * reloaded after call to this function. 19161da177e4SLinus Torvalds */ 19171da177e4SLinus Torvalds 191886a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1919dd0fc66fSAl Viro gfp_t gfp_mask) 19201da177e4SLinus Torvalds { 192112d6c1d3SKees Cook unsigned int osize = skb_end_offset(skb); 192212d6c1d3SKees Cook unsigned int size = osize + nhead + ntail; 19231da177e4SLinus Torvalds long off; 1924158f323bSEric Dumazet u8 *data; 192512d6c1d3SKees Cook int i; 19261da177e4SLinus Torvalds 19274edd87adSHerbert Xu BUG_ON(nhead < 0); 19284edd87adSHerbert Xu 19299f77fad3STim Hansen BUG_ON(skb_shared(skb)); 19301da177e4SLinus Torvalds 1931753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 1932753f1ca4SPavel Begunkov 1933c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1934c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 193512d6c1d3SKees Cook 1936115f1a5cSEric Dumazet size = SKB_HEAD_ALIGN(size); 193712d6c1d3SKees Cook size = kmalloc_size_roundup(size); 193812d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 19391da177e4SLinus Torvalds if (!data) 19401da177e4SLinus Torvalds goto nodata; 194112d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 19446602cebbSEric Dumazet * optimized for the cases when header is void. 19456602cebbSEric Dumazet */ 19466602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 19476602cebbSEric Dumazet 19486602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 19496602cebbSEric Dumazet skb_shinfo(skb), 1950fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 19511da177e4SLinus Torvalds 19523e24591aSAlexander Duyck /* 19533e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 19543e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 19553e24591aSAlexander Duyck * be since all we did is relocate the values 19563e24591aSAlexander Duyck */ 19573e24591aSAlexander Duyck if (skb_cloned(skb)) { 195870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1959a6686f2fSShirley Ma goto nofrags; 19601f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1961c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 19621da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1963ea2ab693SIan Campbell skb_frag_ref(skb, i); 19641da177e4SLinus Torvalds 196521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 19661da177e4SLinus Torvalds skb_clone_fraglist(skb); 19671da177e4SLinus Torvalds 1968511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 19693e24591aSAlexander Duyck } else { 19703e24591aSAlexander Duyck skb_free_head(skb); 19711fd63041SEric Dumazet } 19721da177e4SLinus Torvalds off = (data + nhead) - skb->head; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds skb->head = data; 1975d3836f21SEric Dumazet skb->head_frag = 0; 19761da177e4SLinus Torvalds skb->data += off; 1977763087daSEric Dumazet 1978763087daSEric Dumazet skb_set_end_offset(skb, size); 19794305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 198056eb8882SPatrick McHardy off = nhead; 198156eb8882SPatrick McHardy #endif 198227a884dcSArnaldo Carvalho de Melo skb->tail += off; 1983b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 19841da177e4SLinus Torvalds skb->cloned = 0; 1985334a8132SPatrick McHardy skb->hdr_len = 0; 19861da177e4SLinus Torvalds skb->nohdr = 0; 19871da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1988158f323bSEric Dumazet 1989de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1990de8f3a83SDaniel Borkmann 1991158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1992158f323bSEric Dumazet * For the moment, we really care of rx path, or 1993158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1994158f323bSEric Dumazet */ 1995158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1996158f323bSEric Dumazet skb->truesize += size - osize; 1997158f323bSEric Dumazet 19981da177e4SLinus Torvalds return 0; 19991da177e4SLinus Torvalds 2000a6686f2fSShirley Ma nofrags: 2001a6686f2fSShirley Ma kfree(data); 20021da177e4SLinus Torvalds nodata: 20031da177e4SLinus Torvalds return -ENOMEM; 20041da177e4SLinus Torvalds } 2005b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds struct sk_buff *skb2; 20121da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds if (delta <= 0) 20151da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 20161da177e4SLinus Torvalds else { 20171da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 20181da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 20191da177e4SLinus Torvalds GFP_ATOMIC)) { 20201da177e4SLinus Torvalds kfree_skb(skb2); 20211da177e4SLinus Torvalds skb2 = NULL; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds return skb2; 20251da177e4SLinus Torvalds } 2026b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 20271da177e4SLinus Torvalds 20282b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 20292b88cba5SEric Dumazet { 20302b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 20312b88cba5SEric Dumazet struct skb_shared_info *shinfo; 20322b88cba5SEric Dumazet int res; 20332b88cba5SEric Dumazet 20342b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 20352b88cba5SEric Dumazet saved_truesize = skb->truesize; 20362b88cba5SEric Dumazet 20372b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 20382b88cba5SEric Dumazet if (res) 20392b88cba5SEric Dumazet return res; 20402b88cba5SEric Dumazet 20412b88cba5SEric Dumazet skb->truesize = saved_truesize; 20422b88cba5SEric Dumazet 20432b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 20442b88cba5SEric Dumazet return 0; 20452b88cba5SEric Dumazet 20462b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 20472b88cba5SEric Dumazet 20482b88cba5SEric Dumazet /* We are about to change back skb->end, 20492b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 20502b88cba5SEric Dumazet */ 20512b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 20522b88cba5SEric Dumazet shinfo, 20532b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 20542b88cba5SEric Dumazet 20552b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 20562b88cba5SEric Dumazet 20572b88cba5SEric Dumazet return 0; 20582b88cba5SEric Dumazet } 20592b88cba5SEric Dumazet 20601da177e4SLinus Torvalds /** 2061f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 2062f1260ff1SVasily Averin * @skb: buffer to reallocate 2063f1260ff1SVasily Averin * @headroom: needed headroom 2064f1260ff1SVasily Averin * 2065f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 2066f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 2067f1260ff1SVasily Averin * and frees original skb in case of failures. 2068f1260ff1SVasily Averin * 2069f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 2070f1260ff1SVasily Averin */ 2071f1260ff1SVasily Averin 2072f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2073f1260ff1SVasily Averin { 2074f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 20757f678defSVasily Averin int osize = skb_end_offset(skb); 20767f678defSVasily Averin struct sock *sk = skb->sk; 2077f1260ff1SVasily Averin 2078f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 2079f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 2080f1260ff1SVasily Averin return skb; 2081f1260ff1SVasily Averin 20827f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 20837f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 20847f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 2085f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2086f1260ff1SVasily Averin 20877f678defSVasily Averin if (unlikely(!nskb)) 20887f678defSVasily Averin goto fail; 20897f678defSVasily Averin 20907f678defSVasily Averin if (sk) 20917f678defSVasily Averin skb_set_owner_w(nskb, sk); 2092f1260ff1SVasily Averin consume_skb(skb); 2093f1260ff1SVasily Averin skb = nskb; 2094f1260ff1SVasily Averin } 20957f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 20967f678defSVasily Averin goto fail; 20977f678defSVasily Averin 20987f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 20997f678defSVasily Averin delta = skb_end_offset(skb) - osize; 21007f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 21017f678defSVasily Averin skb->truesize += delta; 2102f1260ff1SVasily Averin } 2103f1260ff1SVasily Averin return skb; 21047f678defSVasily Averin 21057f678defSVasily Averin fail: 21067f678defSVasily Averin kfree_skb(skb); 21077f678defSVasily Averin return NULL; 2108f1260ff1SVasily Averin } 2109f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2110f1260ff1SVasily Averin 2111f1260ff1SVasily Averin /** 21121da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 21131da177e4SLinus Torvalds * @skb: buffer to copy 21141da177e4SLinus Torvalds * @newheadroom: new free bytes at head 21151da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 21161da177e4SLinus Torvalds * @gfp_mask: allocation priority 21171da177e4SLinus Torvalds * 21181da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 21191da177e4SLinus Torvalds * allocate additional space. 21201da177e4SLinus Torvalds * 21211da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 21221da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 21231da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 21241da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 21251da177e4SLinus Torvalds * 21261da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 21271da177e4SLinus Torvalds * is called from an interrupt. 21281da177e4SLinus Torvalds */ 21291da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 213086a76cafSVictor Fusco int newheadroom, int newtailroom, 2131dd0fc66fSAl Viro gfp_t gfp_mask) 21321da177e4SLinus Torvalds { 21331da177e4SLinus Torvalds /* 21341da177e4SLinus Torvalds * Allocate the copy buffer 21351da177e4SLinus Torvalds */ 2136c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2137c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2138c93bdd0eSMel Gorman NUMA_NO_NODE); 2139efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 21401da177e4SLinus Torvalds int head_copy_len, head_copy_off; 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds if (!n) 21431da177e4SLinus Torvalds return NULL; 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds skb_reserve(n, newheadroom); 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds /* Set the tail pointer and length */ 21481da177e4SLinus Torvalds skb_put(n, skb->len); 21491da177e4SLinus Torvalds 2150efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 21511da177e4SLinus Torvalds head_copy_off = 0; 21521da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 21531da177e4SLinus Torvalds head_copy_len = newheadroom; 21541da177e4SLinus Torvalds else 21551da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 21561da177e4SLinus Torvalds 21571da177e4SLinus Torvalds /* Copy the linear header and data. */ 21589f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 21599f77fad3STim Hansen skb->len + head_copy_len)); 21601da177e4SLinus Torvalds 216108303c18SIlya Lesokhin skb_copy_header(n, skb); 21621da177e4SLinus Torvalds 2163030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2164efd1e8d5SPatrick McHardy 21651da177e4SLinus Torvalds return n; 21661da177e4SLinus Torvalds } 2167b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds /** 2170cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 21711da177e4SLinus Torvalds * @skb: buffer to pad 21721da177e4SLinus Torvalds * @pad: space to pad 2173cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 21741da177e4SLinus Torvalds * 21751da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 21761da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 21771da177e4SLinus Torvalds * beyond the buffer end onto the wire. 21781da177e4SLinus Torvalds * 2179cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2180cd0a137aSFlorian Fainelli * if @free_on_error is true. 21811da177e4SLinus Torvalds */ 21821da177e4SLinus Torvalds 2183cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 21841da177e4SLinus Torvalds { 21855b057c6bSHerbert Xu int err; 21865b057c6bSHerbert Xu int ntail; 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 21895b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 21901da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 21915b057c6bSHerbert Xu return 0; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21944305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 21955b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 21965b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 21975b057c6bSHerbert Xu if (unlikely(err)) 21985b057c6bSHerbert Xu goto free_skb; 21995b057c6bSHerbert Xu } 22005b057c6bSHerbert Xu 22015b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 22025b057c6bSHerbert Xu * to be audited. 22035b057c6bSHerbert Xu */ 22045b057c6bSHerbert Xu err = skb_linearize(skb); 22055b057c6bSHerbert Xu if (unlikely(err)) 22065b057c6bSHerbert Xu goto free_skb; 22075b057c6bSHerbert Xu 22085b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 22095b057c6bSHerbert Xu return 0; 22105b057c6bSHerbert Xu 22115b057c6bSHerbert Xu free_skb: 2212cd0a137aSFlorian Fainelli if (free_on_error) 22131da177e4SLinus Torvalds kfree_skb(skb); 22145b057c6bSHerbert Xu return err; 22151da177e4SLinus Torvalds } 2216cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 22171da177e4SLinus Torvalds 22180dde3e16SIlpo Järvinen /** 22190c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 22200c7ddf36SMathias Krause * @skb: start of the buffer to use 22210c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 22220c7ddf36SMathias Krause * @len: amount of data to add 22230c7ddf36SMathias Krause * 22240c7ddf36SMathias Krause * This function extends the used data area of the potentially 22250c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 22260c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 22270c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 22280c7ddf36SMathias Krause * returned. 22290c7ddf36SMathias Krause */ 22300c7ddf36SMathias Krause 22314df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 22320c7ddf36SMathias Krause { 22330c7ddf36SMathias Krause if (tail != skb) { 22340c7ddf36SMathias Krause skb->data_len += len; 22350c7ddf36SMathias Krause skb->len += len; 22360c7ddf36SMathias Krause } 22370c7ddf36SMathias Krause return skb_put(tail, len); 22380c7ddf36SMathias Krause } 22390c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 22400c7ddf36SMathias Krause 22410c7ddf36SMathias Krause /** 22420dde3e16SIlpo Järvinen * skb_put - add data to a buffer 22430dde3e16SIlpo Järvinen * @skb: buffer to use 22440dde3e16SIlpo Järvinen * @len: amount of data to add 22450dde3e16SIlpo Järvinen * 22460dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 22470dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 22480dde3e16SIlpo Järvinen * first byte of the extra data is returned. 22490dde3e16SIlpo Järvinen */ 22504df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 22510dde3e16SIlpo Järvinen { 22524df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 22530dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 22540dde3e16SIlpo Järvinen skb->tail += len; 22550dde3e16SIlpo Järvinen skb->len += len; 22560dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 22570dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 22580dde3e16SIlpo Järvinen return tmp; 22590dde3e16SIlpo Järvinen } 22600dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 22610dde3e16SIlpo Järvinen 22626be8ac2fSIlpo Järvinen /** 2263c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2264c2aa270aSIlpo Järvinen * @skb: buffer to use 2265c2aa270aSIlpo Järvinen * @len: amount of data to add 2266c2aa270aSIlpo Järvinen * 2267c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2268c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2269c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2270c2aa270aSIlpo Järvinen */ 2271d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2272c2aa270aSIlpo Järvinen { 2273c2aa270aSIlpo Järvinen skb->data -= len; 2274c2aa270aSIlpo Järvinen skb->len += len; 2275c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2276c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2277c2aa270aSIlpo Järvinen return skb->data; 2278c2aa270aSIlpo Järvinen } 2279c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2280c2aa270aSIlpo Järvinen 2281c2aa270aSIlpo Järvinen /** 22826be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 22836be8ac2fSIlpo Järvinen * @skb: buffer to use 22846be8ac2fSIlpo Järvinen * @len: amount of data to remove 22856be8ac2fSIlpo Järvinen * 22866be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 22876be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 22886be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 22896be8ac2fSIlpo Järvinen * the old data. 22906be8ac2fSIlpo Järvinen */ 2291af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 22926be8ac2fSIlpo Järvinen { 229347d29646SDavid S. Miller return skb_pull_inline(skb, len); 22946be8ac2fSIlpo Järvinen } 22956be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 22966be8ac2fSIlpo Järvinen 2297419ae74eSIlpo Järvinen /** 229813244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 229913244cccSLuiz Augusto von Dentz * original position. 230013244cccSLuiz Augusto von Dentz * @skb: buffer to use 230113244cccSLuiz Augusto von Dentz * @len: amount of data to remove 230213244cccSLuiz Augusto von Dentz * 230313244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 230413244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 230513244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 230613244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 230713244cccSLuiz Augusto von Dentz */ 230813244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 230913244cccSLuiz Augusto von Dentz { 231013244cccSLuiz Augusto von Dentz void *data = skb->data; 231113244cccSLuiz Augusto von Dentz 231213244cccSLuiz Augusto von Dentz if (skb->len < len) 231313244cccSLuiz Augusto von Dentz return NULL; 231413244cccSLuiz Augusto von Dentz 231513244cccSLuiz Augusto von Dentz skb_pull(skb, len); 231613244cccSLuiz Augusto von Dentz 231713244cccSLuiz Augusto von Dentz return data; 231813244cccSLuiz Augusto von Dentz } 231913244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 232013244cccSLuiz Augusto von Dentz 232113244cccSLuiz Augusto von Dentz /** 2322419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2323419ae74eSIlpo Järvinen * @skb: buffer to alter 2324419ae74eSIlpo Järvinen * @len: new length 2325419ae74eSIlpo Järvinen * 2326419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2327419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2328419ae74eSIlpo Järvinen * The skb must be linear. 2329419ae74eSIlpo Järvinen */ 2330419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2331419ae74eSIlpo Järvinen { 2332419ae74eSIlpo Järvinen if (skb->len > len) 2333419ae74eSIlpo Järvinen __skb_trim(skb, len); 2334419ae74eSIlpo Järvinen } 2335419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2336419ae74eSIlpo Järvinen 23373cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 23381da177e4SLinus Torvalds */ 23391da177e4SLinus Torvalds 23403cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 23411da177e4SLinus Torvalds { 234227b437c8SHerbert Xu struct sk_buff **fragp; 234327b437c8SHerbert Xu struct sk_buff *frag; 23441da177e4SLinus Torvalds int offset = skb_headlen(skb); 23451da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 23461da177e4SLinus Torvalds int i; 234727b437c8SHerbert Xu int err; 234827b437c8SHerbert Xu 234927b437c8SHerbert Xu if (skb_cloned(skb) && 235027b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 235127b437c8SHerbert Xu return err; 23521da177e4SLinus Torvalds 2353f4d26fb3SHerbert Xu i = 0; 2354f4d26fb3SHerbert Xu if (offset >= len) 2355f4d26fb3SHerbert Xu goto drop_pages; 2356f4d26fb3SHerbert Xu 2357f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 23589e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 235927b437c8SHerbert Xu 236027b437c8SHerbert Xu if (end < len) { 23611da177e4SLinus Torvalds offset = end; 236227b437c8SHerbert Xu continue; 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 23659e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 236627b437c8SHerbert Xu 2367f4d26fb3SHerbert Xu drop_pages: 236827b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 236927b437c8SHerbert Xu 237027b437c8SHerbert Xu for (; i < nfrags; i++) 2371ea2ab693SIan Campbell skb_frag_unref(skb, i); 237227b437c8SHerbert Xu 237321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 237427b437c8SHerbert Xu skb_drop_fraglist(skb); 2375f4d26fb3SHerbert Xu goto done; 237627b437c8SHerbert Xu } 237727b437c8SHerbert Xu 237827b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 237927b437c8SHerbert Xu fragp = &frag->next) { 238027b437c8SHerbert Xu int end = offset + frag->len; 238127b437c8SHerbert Xu 238227b437c8SHerbert Xu if (skb_shared(frag)) { 238327b437c8SHerbert Xu struct sk_buff *nfrag; 238427b437c8SHerbert Xu 238527b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 238627b437c8SHerbert Xu if (unlikely(!nfrag)) 238727b437c8SHerbert Xu return -ENOMEM; 238827b437c8SHerbert Xu 238927b437c8SHerbert Xu nfrag->next = frag->next; 239085bb2a60SEric Dumazet consume_skb(frag); 239127b437c8SHerbert Xu frag = nfrag; 239227b437c8SHerbert Xu *fragp = frag; 239327b437c8SHerbert Xu } 239427b437c8SHerbert Xu 239527b437c8SHerbert Xu if (end < len) { 239627b437c8SHerbert Xu offset = end; 239727b437c8SHerbert Xu continue; 239827b437c8SHerbert Xu } 239927b437c8SHerbert Xu 240027b437c8SHerbert Xu if (end > len && 240127b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 240227b437c8SHerbert Xu return err; 240327b437c8SHerbert Xu 240427b437c8SHerbert Xu if (frag->next) 240527b437c8SHerbert Xu skb_drop_list(&frag->next); 240627b437c8SHerbert Xu break; 240727b437c8SHerbert Xu } 240827b437c8SHerbert Xu 2409f4d26fb3SHerbert Xu done: 241027b437c8SHerbert Xu if (len > skb_headlen(skb)) { 24111da177e4SLinus Torvalds skb->data_len -= skb->len - len; 24121da177e4SLinus Torvalds skb->len = len; 24131da177e4SLinus Torvalds } else { 24141da177e4SLinus Torvalds skb->len = len; 24151da177e4SLinus Torvalds skb->data_len = 0; 241627a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 2419c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2420c21b48ccSEric Dumazet skb_condense(skb); 24211da177e4SLinus Torvalds return 0; 24221da177e4SLinus Torvalds } 2423b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 24241da177e4SLinus Torvalds 242588078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 242688078d98SEric Dumazet */ 242788078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 242888078d98SEric Dumazet { 242988078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 243088078d98SEric Dumazet int delta = skb->len - len; 243188078d98SEric Dumazet 2432d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2433d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2434d55bef50SDimitris Michailidis len); 243554970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 243654970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 243754970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 243854970a2fSVasily Averin 243954970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 244054970a2fSVasily Averin return -EINVAL; 244188078d98SEric Dumazet } 244288078d98SEric Dumazet return __pskb_trim(skb, len); 244388078d98SEric Dumazet } 244488078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 244588078d98SEric Dumazet 24461da177e4SLinus Torvalds /** 24471da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 24481da177e4SLinus Torvalds * @skb: buffer to reallocate 24491da177e4SLinus Torvalds * @delta: number of bytes to advance tail 24501da177e4SLinus Torvalds * 24511da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 24521da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 24531da177e4SLinus Torvalds * data from fragmented part. 24541da177e4SLinus Torvalds * 24551da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 24561da177e4SLinus Torvalds * 24571da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 24581da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 24591da177e4SLinus Torvalds * 24601da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 24611da177e4SLinus Torvalds * reloaded after call to this function. 24621da177e4SLinus Torvalds */ 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 24651da177e4SLinus Torvalds * when it is necessary. 24661da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 24671da177e4SLinus Torvalds * 2. It may change skb pointers. 24681da177e4SLinus Torvalds * 24691da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 24701da177e4SLinus Torvalds */ 2471af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 24721da177e4SLinus Torvalds { 24731da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 24741da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 24751da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 24761da177e4SLinus Torvalds */ 24774305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 24801da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 24811da177e4SLinus Torvalds GFP_ATOMIC)) 24821da177e4SLinus Torvalds return NULL; 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 24859f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 24869f77fad3STim Hansen skb_tail_pointer(skb), delta)); 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 24891da177e4SLinus Torvalds * size of pulled pages. Superb. 24901da177e4SLinus Torvalds */ 249121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 24921da177e4SLinus Torvalds goto pull_pages; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 24951da177e4SLinus Torvalds eat = delta; 24961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24979e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 24989e903e08SEric Dumazet 24999e903e08SEric Dumazet if (size >= eat) 25001da177e4SLinus Torvalds goto pull_pages; 25019e903e08SEric Dumazet eat -= size; 25021da177e4SLinus Torvalds } 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 250509001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 25061da177e4SLinus Torvalds * but taking into account that pulling is expected to 25071da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 25081da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 25091da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 25101da177e4SLinus Torvalds */ 25111da177e4SLinus Torvalds if (eat) { 25121da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 25131da177e4SLinus Torvalds struct sk_buff *clone = NULL; 25141da177e4SLinus Torvalds struct sk_buff *insp = NULL; 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds do { 25171da177e4SLinus Torvalds if (list->len <= eat) { 25181da177e4SLinus Torvalds /* Eaten as whole. */ 25191da177e4SLinus Torvalds eat -= list->len; 25201da177e4SLinus Torvalds list = list->next; 25211da177e4SLinus Torvalds insp = list; 25221da177e4SLinus Torvalds } else { 25231da177e4SLinus Torvalds /* Eaten partially. */ 25242d7afdcbSSubash Abhinov Kasiviswanathan if (skb_is_gso(skb) && !list->head_frag && 25252d7afdcbSSubash Abhinov Kasiviswanathan skb_headlen(list)) 25262d7afdcbSSubash Abhinov Kasiviswanathan skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds if (skb_shared(list)) { 25291da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 25301da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 25311da177e4SLinus Torvalds if (!clone) 25321da177e4SLinus Torvalds return NULL; 25331da177e4SLinus Torvalds insp = list->next; 25341da177e4SLinus Torvalds list = clone; 25351da177e4SLinus Torvalds } else { 25361da177e4SLinus Torvalds /* This may be pulled without 25371da177e4SLinus Torvalds * problems. */ 25381da177e4SLinus Torvalds insp = list; 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 25411da177e4SLinus Torvalds kfree_skb(clone); 25421da177e4SLinus Torvalds return NULL; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds break; 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds } while (eat); 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds /* Free pulled out fragments. */ 25491da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 25501da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2551ef527f96SEric Dumazet consume_skb(list); 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds /* And insert new clone at head. */ 25541da177e4SLinus Torvalds if (clone) { 25551da177e4SLinus Torvalds clone->next = list; 25561da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds } 25591da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds pull_pages: 25621da177e4SLinus Torvalds eat = delta; 25631da177e4SLinus Torvalds k = 0; 25641da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25659e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25669e903e08SEric Dumazet 25679e903e08SEric Dumazet if (size <= eat) { 2568ea2ab693SIan Campbell skb_frag_unref(skb, i); 25699e903e08SEric Dumazet eat -= size; 25701da177e4SLinus Torvalds } else { 2571b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2572b54c9d5bSJonathan Lemon 2573b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 25741da177e4SLinus Torvalds if (eat) { 2575b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2576b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 25773ccc6c6fSlinzhang if (!i) 25783ccc6c6fSlinzhang goto end; 25791da177e4SLinus Torvalds eat = 0; 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds k++; 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 25851da177e4SLinus Torvalds 25863ccc6c6fSlinzhang end: 25871da177e4SLinus Torvalds skb->tail += delta; 25881da177e4SLinus Torvalds skb->data_len -= delta; 25891da177e4SLinus Torvalds 25901f8b977aSWillem de Bruijn if (!skb->data_len) 25911f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 25921f8b977aSWillem de Bruijn 259327a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 25941da177e4SLinus Torvalds } 2595b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 25961da177e4SLinus Torvalds 259722019b17SEric Dumazet /** 259822019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 259922019b17SEric Dumazet * @skb: source skb 260022019b17SEric Dumazet * @offset: offset in source 260122019b17SEric Dumazet * @to: destination buffer 260222019b17SEric Dumazet * @len: number of bytes to copy 260322019b17SEric Dumazet * 260422019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 260522019b17SEric Dumazet * destination buffer. 260622019b17SEric Dumazet * 260722019b17SEric Dumazet * CAUTION ! : 260822019b17SEric Dumazet * If its prototype is ever changed, 260922019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 261022019b17SEric Dumazet * since it is called from BPF assembly code. 261122019b17SEric Dumazet */ 26121da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 26131da177e4SLinus Torvalds { 26141a028e50SDavid S. Miller int start = skb_headlen(skb); 2615fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2616fbb398a8SDavid S. Miller int i, copy; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (offset > (int)skb->len - len) 26191da177e4SLinus Torvalds goto fault; 26201da177e4SLinus Torvalds 26211da177e4SLinus Torvalds /* Copy header. */ 26221a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 26231da177e4SLinus Torvalds if (copy > len) 26241da177e4SLinus Torvalds copy = len; 2625d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 26261da177e4SLinus Torvalds if ((len -= copy) == 0) 26271da177e4SLinus Torvalds return 0; 26281da177e4SLinus Torvalds offset += copy; 26291da177e4SLinus Torvalds to += copy; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 26331a028e50SDavid S. Miller int end; 263451c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 26351da177e4SLinus Torvalds 2636547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26371a028e50SDavid S. Miller 263851c56b00SEric Dumazet end = start + skb_frag_size(f); 26391da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2640c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2641c613c209SWillem de Bruijn struct page *p; 26421da177e4SLinus Torvalds u8 *vaddr; 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds if (copy > len) 26451da177e4SLinus Torvalds copy = len; 26461da177e4SLinus Torvalds 2647c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2648b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2649c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2650c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2651c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 265251c56b00SEric Dumazet kunmap_atomic(vaddr); 2653c613c209SWillem de Bruijn } 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds if ((len -= copy) == 0) 26561da177e4SLinus Torvalds return 0; 26571da177e4SLinus Torvalds offset += copy; 26581da177e4SLinus Torvalds to += copy; 26591da177e4SLinus Torvalds } 26601a028e50SDavid S. Miller start = end; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 2663fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 26641a028e50SDavid S. Miller int end; 26651da177e4SLinus Torvalds 2666547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26671a028e50SDavid S. Miller 2668fbb398a8SDavid S. Miller end = start + frag_iter->len; 26691da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26701da177e4SLinus Torvalds if (copy > len) 26711da177e4SLinus Torvalds copy = len; 2672fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 26731da177e4SLinus Torvalds goto fault; 26741da177e4SLinus Torvalds if ((len -= copy) == 0) 26751da177e4SLinus Torvalds return 0; 26761da177e4SLinus Torvalds offset += copy; 26771da177e4SLinus Torvalds to += copy; 26781da177e4SLinus Torvalds } 26791a028e50SDavid S. Miller start = end; 26801da177e4SLinus Torvalds } 2681a6686f2fSShirley Ma 26821da177e4SLinus Torvalds if (!len) 26831da177e4SLinus Torvalds return 0; 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds fault: 26861da177e4SLinus Torvalds return -EFAULT; 26871da177e4SLinus Torvalds } 2688b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 26891da177e4SLinus Torvalds 26909c55e01cSJens Axboe /* 26919c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 26929c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 26939c55e01cSJens Axboe */ 26949c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 26959c55e01cSJens Axboe { 26968b9d3728SJarek Poplawski put_page(spd->pages[i]); 26978b9d3728SJarek Poplawski } 26989c55e01cSJens Axboe 2699a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 27004fb66994SJarek Poplawski unsigned int *offset, 270118aafc62SEric Dumazet struct sock *sk) 27028b9d3728SJarek Poplawski { 27035640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 27048b9d3728SJarek Poplawski 27055640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 27068b9d3728SJarek Poplawski return NULL; 27074fb66994SJarek Poplawski 27085640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 27094fb66994SJarek Poplawski 27105640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 27115640f768SEric Dumazet page_address(page) + *offset, *len); 27125640f768SEric Dumazet *offset = pfrag->offset; 27135640f768SEric Dumazet pfrag->offset += *len; 27144fb66994SJarek Poplawski 27155640f768SEric Dumazet return pfrag->page; 27169c55e01cSJens Axboe } 27179c55e01cSJens Axboe 271841c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 271941c73a0dSEric Dumazet struct page *page, 272041c73a0dSEric Dumazet unsigned int offset) 272141c73a0dSEric Dumazet { 272241c73a0dSEric Dumazet return spd->nr_pages && 272341c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 272441c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 272541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 272641c73a0dSEric Dumazet } 272741c73a0dSEric Dumazet 27289c55e01cSJens Axboe /* 27299c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 27309c55e01cSJens Axboe */ 2731a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 273235f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 27334fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 273418aafc62SEric Dumazet bool linear, 27357a67e56fSJarek Poplawski struct sock *sk) 27369c55e01cSJens Axboe { 273741c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2738a108d5f3SDavid S. Miller return true; 27399c55e01cSJens Axboe 27408b9d3728SJarek Poplawski if (linear) { 274118aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 27428b9d3728SJarek Poplawski if (!page) 2743a108d5f3SDavid S. Miller return true; 274441c73a0dSEric Dumazet } 274541c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 274641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2747a108d5f3SDavid S. Miller return false; 274841c73a0dSEric Dumazet } 27498b9d3728SJarek Poplawski get_page(page); 27509c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 27514fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 27529c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 27539c55e01cSJens Axboe spd->nr_pages++; 27548b9d3728SJarek Poplawski 2755a108d5f3SDavid S. Miller return false; 27569c55e01cSJens Axboe } 27579c55e01cSJens Axboe 2758a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 27592870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 276018aafc62SEric Dumazet unsigned int *len, 2761d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 276235f3d14dSJens Axboe struct sock *sk, 276335f3d14dSJens Axboe struct pipe_inode_info *pipe) 27649c55e01cSJens Axboe { 27652870c43dSOctavian Purdila if (!*len) 2766a108d5f3SDavid S. Miller return true; 27679c55e01cSJens Axboe 27682870c43dSOctavian Purdila /* skip this segment if already processed */ 27692870c43dSOctavian Purdila if (*off >= plen) { 27702870c43dSOctavian Purdila *off -= plen; 2771a108d5f3SDavid S. Miller return false; 27722870c43dSOctavian Purdila } 27732870c43dSOctavian Purdila 27742870c43dSOctavian Purdila /* ignore any bits we already processed */ 27759ca1b22dSEric Dumazet poff += *off; 27769ca1b22dSEric Dumazet plen -= *off; 27772870c43dSOctavian Purdila *off = 0; 27782870c43dSOctavian Purdila 277918aafc62SEric Dumazet do { 278018aafc62SEric Dumazet unsigned int flen = min(*len, plen); 27812870c43dSOctavian Purdila 278218aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 278318aafc62SEric Dumazet linear, sk)) 2784a108d5f3SDavid S. Miller return true; 278518aafc62SEric Dumazet poff += flen; 278618aafc62SEric Dumazet plen -= flen; 27872870c43dSOctavian Purdila *len -= flen; 278818aafc62SEric Dumazet } while (*len && plen); 27892870c43dSOctavian Purdila 2790a108d5f3SDavid S. Miller return false; 2791db43a282SOctavian Purdila } 27929c55e01cSJens Axboe 27939c55e01cSJens Axboe /* 2794a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 27952870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 27969c55e01cSJens Axboe */ 2797a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 279835f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 279935f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 28002870c43dSOctavian Purdila { 28012870c43dSOctavian Purdila int seg; 2802fa9835e5STom Herbert struct sk_buff *iter; 28039c55e01cSJens Axboe 28041d0c0b32SEric Dumazet /* map the linear part : 28052996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 28062996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 28072996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 28089c55e01cSJens Axboe */ 28092870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 28102870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 28112870c43dSOctavian Purdila skb_headlen(skb), 281218aafc62SEric Dumazet offset, len, spd, 28133a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 28141d0c0b32SEric Dumazet sk, pipe)) 2815a108d5f3SDavid S. Miller return true; 28169c55e01cSJens Axboe 28179c55e01cSJens Axboe /* 28189c55e01cSJens Axboe * then map the fragments 28199c55e01cSJens Axboe */ 28209c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 28219c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 28229c55e01cSJens Axboe 2823ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2824b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 282518aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2826a108d5f3SDavid S. Miller return true; 28279c55e01cSJens Axboe } 28289c55e01cSJens Axboe 2829fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2830fa9835e5STom Herbert if (*offset >= iter->len) { 2831fa9835e5STom Herbert *offset -= iter->len; 2832fa9835e5STom Herbert continue; 2833fa9835e5STom Herbert } 2834fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2835fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2836fa9835e5STom Herbert * case. 2837fa9835e5STom Herbert */ 2838fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2839fa9835e5STom Herbert return true; 2840fa9835e5STom Herbert } 2841fa9835e5STom Herbert 2842a108d5f3SDavid S. Miller return false; 28439c55e01cSJens Axboe } 28449c55e01cSJens Axboe 28459c55e01cSJens Axboe /* 28469c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2847fa9835e5STom Herbert * the fragments, and the frag list. 28489c55e01cSJens Axboe */ 2849a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 28509c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 285125869262SAl Viro unsigned int flags) 28529c55e01cSJens Axboe { 285341c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 285441c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 28559c55e01cSJens Axboe struct splice_pipe_desc spd = { 28569c55e01cSJens Axboe .pages = pages, 28579c55e01cSJens Axboe .partial = partial, 2858047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 285928a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 28609c55e01cSJens Axboe .spd_release = sock_spd_release, 28619c55e01cSJens Axboe }; 286235f3d14dSJens Axboe int ret = 0; 286335f3d14dSJens Axboe 2864fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 28659c55e01cSJens Axboe 2866a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 286725869262SAl Viro ret = splice_to_pipe(pipe, &spd); 28689c55e01cSJens Axboe 286935f3d14dSJens Axboe return ret; 28709c55e01cSJens Axboe } 28712b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 28729c55e01cSJens Axboe 28730739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 28740739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 28750739cd28SCong Wang { 28760739cd28SCong Wang struct socket *sock = sk->sk_socket; 28770739cd28SCong Wang 28780739cd28SCong Wang if (!sock) 28790739cd28SCong Wang return -EINVAL; 28800739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 28810739cd28SCong Wang } 28820739cd28SCong Wang 28830739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 28840739cd28SCong Wang size_t size, int flags) 28850739cd28SCong Wang { 28860739cd28SCong Wang struct socket *sock = sk->sk_socket; 28870739cd28SCong Wang 28880739cd28SCong Wang if (!sock) 28890739cd28SCong Wang return -EINVAL; 28900739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 28910739cd28SCong Wang } 28920739cd28SCong Wang 28930739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 28940739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 28950739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 28960739cd28SCong Wang size_t size, int flags); 28970739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 28980739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 289920bf50deSTom Herbert { 290020bf50deSTom Herbert unsigned int orig_len = len; 290120bf50deSTom Herbert struct sk_buff *head = skb; 290220bf50deSTom Herbert unsigned short fragidx; 290320bf50deSTom Herbert int slen, ret; 290420bf50deSTom Herbert 290520bf50deSTom Herbert do_frag_list: 290620bf50deSTom Herbert 290720bf50deSTom Herbert /* Deal with head data */ 290820bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 290920bf50deSTom Herbert struct kvec kv; 291020bf50deSTom Herbert struct msghdr msg; 291120bf50deSTom Herbert 291220bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 291320bf50deSTom Herbert kv.iov_base = skb->data + offset; 2914db5980d8SJohn Fastabend kv.iov_len = slen; 291520bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2916bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 291720bf50deSTom Herbert 29180739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 29190739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 292020bf50deSTom Herbert if (ret <= 0) 292120bf50deSTom Herbert goto error; 292220bf50deSTom Herbert 292320bf50deSTom Herbert offset += ret; 292420bf50deSTom Herbert len -= ret; 292520bf50deSTom Herbert } 292620bf50deSTom Herbert 292720bf50deSTom Herbert /* All the data was skb head? */ 292820bf50deSTom Herbert if (!len) 292920bf50deSTom Herbert goto out; 293020bf50deSTom Herbert 293120bf50deSTom Herbert /* Make offset relative to start of frags */ 293220bf50deSTom Herbert offset -= skb_headlen(skb); 293320bf50deSTom Herbert 293420bf50deSTom Herbert /* Find where we are in frag list */ 293520bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 293620bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 293720bf50deSTom Herbert 2938d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 293920bf50deSTom Herbert break; 294020bf50deSTom Herbert 2941d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 294220bf50deSTom Herbert } 294320bf50deSTom Herbert 294420bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 294520bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 294620bf50deSTom Herbert 2947d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 294820bf50deSTom Herbert 294920bf50deSTom Herbert while (slen) { 29500739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 29510739cd28SCong Wang sendpage_unlocked, sk, 29520739cd28SCong Wang skb_frag_page(frag), 2953b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 295420bf50deSTom Herbert slen, MSG_DONTWAIT); 295520bf50deSTom Herbert if (ret <= 0) 295620bf50deSTom Herbert goto error; 295720bf50deSTom Herbert 295820bf50deSTom Herbert len -= ret; 295920bf50deSTom Herbert offset += ret; 296020bf50deSTom Herbert slen -= ret; 296120bf50deSTom Herbert } 296220bf50deSTom Herbert 296320bf50deSTom Herbert offset = 0; 296420bf50deSTom Herbert } 296520bf50deSTom Herbert 296620bf50deSTom Herbert if (len) { 296720bf50deSTom Herbert /* Process any frag lists */ 296820bf50deSTom Herbert 296920bf50deSTom Herbert if (skb == head) { 297020bf50deSTom Herbert if (skb_has_frag_list(skb)) { 297120bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 297220bf50deSTom Herbert goto do_frag_list; 297320bf50deSTom Herbert } 297420bf50deSTom Herbert } else if (skb->next) { 297520bf50deSTom Herbert skb = skb->next; 297620bf50deSTom Herbert goto do_frag_list; 297720bf50deSTom Herbert } 297820bf50deSTom Herbert } 297920bf50deSTom Herbert 298020bf50deSTom Herbert out: 298120bf50deSTom Herbert return orig_len - len; 298220bf50deSTom Herbert 298320bf50deSTom Herbert error: 298420bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 298520bf50deSTom Herbert } 29860739cd28SCong Wang 29870739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 29880739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 29890739cd28SCong Wang int len) 29900739cd28SCong Wang { 29910739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 29920739cd28SCong Wang kernel_sendpage_locked); 29930739cd28SCong Wang } 299420bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 299520bf50deSTom Herbert 29960739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 29970739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 29980739cd28SCong Wang { 29990739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 30000739cd28SCong Wang sendpage_unlocked); 30010739cd28SCong Wang } 30020739cd28SCong Wang 3003357b40a1SHerbert Xu /** 3004357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 3005357b40a1SHerbert Xu * @skb: destination buffer 3006357b40a1SHerbert Xu * @offset: offset in destination 3007357b40a1SHerbert Xu * @from: source buffer 3008357b40a1SHerbert Xu * @len: number of bytes to copy 3009357b40a1SHerbert Xu * 3010357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 3011357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 3012357b40a1SHerbert Xu * traversing fragment lists and such. 3013357b40a1SHerbert Xu */ 3014357b40a1SHerbert Xu 30150c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3016357b40a1SHerbert Xu { 30171a028e50SDavid S. Miller int start = skb_headlen(skb); 3018fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3019fbb398a8SDavid S. Miller int i, copy; 3020357b40a1SHerbert Xu 3021357b40a1SHerbert Xu if (offset > (int)skb->len - len) 3022357b40a1SHerbert Xu goto fault; 3023357b40a1SHerbert Xu 30241a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 3025357b40a1SHerbert Xu if (copy > len) 3026357b40a1SHerbert Xu copy = len; 302727d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 3028357b40a1SHerbert Xu if ((len -= copy) == 0) 3029357b40a1SHerbert Xu return 0; 3030357b40a1SHerbert Xu offset += copy; 3031357b40a1SHerbert Xu from += copy; 3032357b40a1SHerbert Xu } 3033357b40a1SHerbert Xu 3034357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3035357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 30361a028e50SDavid S. Miller int end; 3037357b40a1SHerbert Xu 3038547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30391a028e50SDavid S. Miller 30409e903e08SEric Dumazet end = start + skb_frag_size(frag); 3041357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3042c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3043c613c209SWillem de Bruijn struct page *p; 3044357b40a1SHerbert Xu u8 *vaddr; 3045357b40a1SHerbert Xu 3046357b40a1SHerbert Xu if (copy > len) 3047357b40a1SHerbert Xu copy = len; 3048357b40a1SHerbert Xu 3049c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3050b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3051c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3052c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3053c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 305451c56b00SEric Dumazet kunmap_atomic(vaddr); 3055c613c209SWillem de Bruijn } 3056357b40a1SHerbert Xu 3057357b40a1SHerbert Xu if ((len -= copy) == 0) 3058357b40a1SHerbert Xu return 0; 3059357b40a1SHerbert Xu offset += copy; 3060357b40a1SHerbert Xu from += copy; 3061357b40a1SHerbert Xu } 30621a028e50SDavid S. Miller start = end; 3063357b40a1SHerbert Xu } 3064357b40a1SHerbert Xu 3065fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 30661a028e50SDavid S. Miller int end; 3067357b40a1SHerbert Xu 3068547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30691a028e50SDavid S. Miller 3070fbb398a8SDavid S. Miller end = start + frag_iter->len; 3071357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 3072357b40a1SHerbert Xu if (copy > len) 3073357b40a1SHerbert Xu copy = len; 3074fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 30751a028e50SDavid S. Miller from, copy)) 3076357b40a1SHerbert Xu goto fault; 3077357b40a1SHerbert Xu if ((len -= copy) == 0) 3078357b40a1SHerbert Xu return 0; 3079357b40a1SHerbert Xu offset += copy; 3080357b40a1SHerbert Xu from += copy; 3081357b40a1SHerbert Xu } 30821a028e50SDavid S. Miller start = end; 3083357b40a1SHerbert Xu } 3084357b40a1SHerbert Xu if (!len) 3085357b40a1SHerbert Xu return 0; 3086357b40a1SHerbert Xu 3087357b40a1SHerbert Xu fault: 3088357b40a1SHerbert Xu return -EFAULT; 3089357b40a1SHerbert Xu } 3090357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 3091357b40a1SHerbert Xu 30921da177e4SLinus Torvalds /* Checksum skb data. */ 30932817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 30942817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 30951da177e4SLinus Torvalds { 30961a028e50SDavid S. Miller int start = skb_headlen(skb); 30971a028e50SDavid S. Miller int i, copy = start - offset; 3098fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 30991da177e4SLinus Torvalds int pos = 0; 31001da177e4SLinus Torvalds 31011da177e4SLinus Torvalds /* Checksum header. */ 31021da177e4SLinus Torvalds if (copy > 0) { 31031da177e4SLinus Torvalds if (copy > len) 31041da177e4SLinus Torvalds copy = len; 31052544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 31062544af03SMatteo Croce skb->data + offset, copy, csum); 31071da177e4SLinus Torvalds if ((len -= copy) == 0) 31081da177e4SLinus Torvalds return csum; 31091da177e4SLinus Torvalds offset += copy; 31101da177e4SLinus Torvalds pos = copy; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 31141a028e50SDavid S. Miller int end; 311551c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 31161da177e4SLinus Torvalds 3117547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31181a028e50SDavid S. Miller 311951c56b00SEric Dumazet end = start + skb_frag_size(frag); 31201da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3121c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3122c613c209SWillem de Bruijn struct page *p; 312344bb9363SAl Viro __wsum csum2; 31241da177e4SLinus Torvalds u8 *vaddr; 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds if (copy > len) 31271da177e4SLinus Torvalds copy = len; 3128c613c209SWillem de Bruijn 3129c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3130b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3131c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3132c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 31332544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 31342544af03SMatteo Croce csum_partial_ext, 31352544af03SMatteo Croce vaddr + p_off, p_len, 0); 313651c56b00SEric Dumazet kunmap_atomic(vaddr); 31372544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 31382544af03SMatteo Croce csum_block_add_ext, csum, 31392544af03SMatteo Croce csum2, pos, p_len); 3140c613c209SWillem de Bruijn pos += p_len; 3141c613c209SWillem de Bruijn } 3142c613c209SWillem de Bruijn 31431da177e4SLinus Torvalds if (!(len -= copy)) 31441da177e4SLinus Torvalds return csum; 31451da177e4SLinus Torvalds offset += copy; 31461da177e4SLinus Torvalds } 31471a028e50SDavid S. Miller start = end; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 3150fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31511a028e50SDavid S. Miller int end; 31521da177e4SLinus Torvalds 3153547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31541a028e50SDavid S. Miller 3155fbb398a8SDavid S. Miller end = start + frag_iter->len; 31561da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 31575f92a738SAl Viro __wsum csum2; 31581da177e4SLinus Torvalds if (copy > len) 31591da177e4SLinus Torvalds copy = len; 31602817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 31612817a336SDaniel Borkmann copy, 0, ops); 31622544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 31632544af03SMatteo Croce csum, csum2, pos, copy); 31641da177e4SLinus Torvalds if ((len -= copy) == 0) 31651da177e4SLinus Torvalds return csum; 31661da177e4SLinus Torvalds offset += copy; 31671da177e4SLinus Torvalds pos += copy; 31681da177e4SLinus Torvalds } 31691a028e50SDavid S. Miller start = end; 31701da177e4SLinus Torvalds } 317109a62660SKris Katterjohn BUG_ON(len); 31721da177e4SLinus Torvalds 31731da177e4SLinus Torvalds return csum; 31741da177e4SLinus Torvalds } 31752817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 31762817a336SDaniel Borkmann 31772817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 31782817a336SDaniel Borkmann int len, __wsum csum) 31792817a336SDaniel Borkmann { 31802817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3181cea80ea8SDaniel Borkmann .update = csum_partial_ext, 31822817a336SDaniel Borkmann .combine = csum_block_add_ext, 31832817a336SDaniel Borkmann }; 31842817a336SDaniel Borkmann 31852817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 31862817a336SDaniel Borkmann } 3187b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 31881da177e4SLinus Torvalds 31891da177e4SLinus Torvalds /* Both of above in one bottle. */ 31901da177e4SLinus Torvalds 319181d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 31928d5930dfSAl Viro u8 *to, int len) 31931da177e4SLinus Torvalds { 31941a028e50SDavid S. Miller int start = skb_headlen(skb); 31951a028e50SDavid S. Miller int i, copy = start - offset; 3196fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 31971da177e4SLinus Torvalds int pos = 0; 31988d5930dfSAl Viro __wsum csum = 0; 31991da177e4SLinus Torvalds 32001da177e4SLinus Torvalds /* Copy header. */ 32011da177e4SLinus Torvalds if (copy > 0) { 32021da177e4SLinus Torvalds if (copy > len) 32031da177e4SLinus Torvalds copy = len; 32041da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3205cc44c17bSAl Viro copy); 32061da177e4SLinus Torvalds if ((len -= copy) == 0) 32071da177e4SLinus Torvalds return csum; 32081da177e4SLinus Torvalds offset += copy; 32091da177e4SLinus Torvalds to += copy; 32101da177e4SLinus Torvalds pos = copy; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32141a028e50SDavid S. Miller int end; 32151da177e4SLinus Torvalds 3216547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32171a028e50SDavid S. Miller 32189e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 32191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3220c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3221c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3222c613c209SWillem de Bruijn struct page *p; 32235084205fSAl Viro __wsum csum2; 32241da177e4SLinus Torvalds u8 *vaddr; 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds if (copy > len) 32271da177e4SLinus Torvalds copy = len; 3228c613c209SWillem de Bruijn 3229c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3230b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3231c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3232c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3233c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3234c613c209SWillem de Bruijn to + copied, 3235cc44c17bSAl Viro p_len); 323651c56b00SEric Dumazet kunmap_atomic(vaddr); 32371da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3238c613c209SWillem de Bruijn pos += p_len; 3239c613c209SWillem de Bruijn } 3240c613c209SWillem de Bruijn 32411da177e4SLinus Torvalds if (!(len -= copy)) 32421da177e4SLinus Torvalds return csum; 32431da177e4SLinus Torvalds offset += copy; 32441da177e4SLinus Torvalds to += copy; 32451da177e4SLinus Torvalds } 32461a028e50SDavid S. Miller start = end; 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 3249fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 325081d77662SAl Viro __wsum csum2; 32511a028e50SDavid S. Miller int end; 32521da177e4SLinus Torvalds 3253547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32541a028e50SDavid S. Miller 3255fbb398a8SDavid S. Miller end = start + frag_iter->len; 32561da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 32571da177e4SLinus Torvalds if (copy > len) 32581da177e4SLinus Torvalds copy = len; 3259fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 32601a028e50SDavid S. Miller offset - start, 32618d5930dfSAl Viro to, copy); 32621da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 32631da177e4SLinus Torvalds if ((len -= copy) == 0) 32641da177e4SLinus Torvalds return csum; 32651da177e4SLinus Torvalds offset += copy; 32661da177e4SLinus Torvalds to += copy; 32671da177e4SLinus Torvalds pos += copy; 32681da177e4SLinus Torvalds } 32691a028e50SDavid S. Miller start = end; 32701da177e4SLinus Torvalds } 327109a62660SKris Katterjohn BUG_ON(len); 32721da177e4SLinus Torvalds return csum; 32731da177e4SLinus Torvalds } 3274b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 32751da177e4SLinus Torvalds 327649f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 327749f8e832SCong Wang { 327849f8e832SCong Wang __sum16 sum; 327949f8e832SCong Wang 328049f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 328114641931SCong Wang /* See comments in __skb_checksum_complete(). */ 328249f8e832SCong Wang if (likely(!sum)) { 328349f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 328449f8e832SCong Wang !skb->csum_complete_sw) 32857fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 328649f8e832SCong Wang } 328749f8e832SCong Wang if (!skb_shared(skb)) 328849f8e832SCong Wang skb->csum_valid = !sum; 328949f8e832SCong Wang return sum; 329049f8e832SCong Wang } 329149f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 329249f8e832SCong Wang 329314641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 329414641931SCong Wang * which has been changed from the hardware checksum, for example, by 329514641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 329614641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 329714641931SCong Wang * 329814641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 329914641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 330014641931SCong Wang * shared. 330114641931SCong Wang */ 330249f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 330349f8e832SCong Wang { 330449f8e832SCong Wang __wsum csum; 330549f8e832SCong Wang __sum16 sum; 330649f8e832SCong Wang 330749f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 330849f8e832SCong Wang 330949f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 331014641931SCong Wang /* This check is inverted, because we already knew the hardware 331114641931SCong Wang * checksum is invalid before calling this function. So, if the 331214641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 331314641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 331414641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 331514641931SCong Wang * when moving skb->data around. 331614641931SCong Wang */ 331749f8e832SCong Wang if (likely(!sum)) { 331849f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 331949f8e832SCong Wang !skb->csum_complete_sw) 33207fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 332149f8e832SCong Wang } 332249f8e832SCong Wang 332349f8e832SCong Wang if (!skb_shared(skb)) { 332449f8e832SCong Wang /* Save full packet checksum */ 332549f8e832SCong Wang skb->csum = csum; 332649f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 332749f8e832SCong Wang skb->csum_complete_sw = 1; 332849f8e832SCong Wang skb->csum_valid = !sum; 332949f8e832SCong Wang } 333049f8e832SCong Wang 333149f8e832SCong Wang return sum; 333249f8e832SCong Wang } 333349f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 333449f8e832SCong Wang 33359617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 33369617813dSDavide Caratti { 33379617813dSDavide Caratti net_warn_ratelimited( 33389617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 33399617813dSDavide Caratti __func__); 33409617813dSDavide Caratti return 0; 33419617813dSDavide Caratti } 33429617813dSDavide Caratti 33439617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 33449617813dSDavide Caratti int offset, int len) 33459617813dSDavide Caratti { 33469617813dSDavide Caratti net_warn_ratelimited( 33479617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 33489617813dSDavide Caratti __func__); 33499617813dSDavide Caratti return 0; 33509617813dSDavide Caratti } 33519617813dSDavide Caratti 33529617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 33539617813dSDavide Caratti .update = warn_crc32c_csum_update, 33549617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 33559617813dSDavide Caratti }; 33569617813dSDavide Caratti 33579617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 33589617813dSDavide Caratti &default_crc32c_ops; 33599617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 33609617813dSDavide Caratti 3361af2806f8SThomas Graf /** 3362af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3363af2806f8SThomas Graf * @from: source buffer 3364af2806f8SThomas Graf * 3365af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3366af2806f8SThomas Graf * into skb_zerocopy(). 3367af2806f8SThomas Graf */ 3368af2806f8SThomas Graf unsigned int 3369af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3370af2806f8SThomas Graf { 3371af2806f8SThomas Graf unsigned int hlen = 0; 3372af2806f8SThomas Graf 3373af2806f8SThomas Graf if (!from->head_frag || 3374af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3375a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3376af2806f8SThomas Graf hlen = skb_headlen(from); 3377a17ad096SPravin B Shelar if (!hlen) 3378a17ad096SPravin B Shelar hlen = from->len; 3379a17ad096SPravin B Shelar } 3380af2806f8SThomas Graf 3381af2806f8SThomas Graf if (skb_has_frag_list(from)) 3382af2806f8SThomas Graf hlen = from->len; 3383af2806f8SThomas Graf 3384af2806f8SThomas Graf return hlen; 3385af2806f8SThomas Graf } 3386af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3387af2806f8SThomas Graf 3388af2806f8SThomas Graf /** 3389af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3390af2806f8SThomas Graf * @to: destination buffer 33917fceb4deSMasanari Iida * @from: source buffer 3392af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3393af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3394af2806f8SThomas Graf * 3395af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3396af2806f8SThomas Graf * to the frags in the source buffer. 3397af2806f8SThomas Graf * 3398af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3399af2806f8SThomas Graf * headroom in the `to` buffer. 340036d5fe6aSZoltan Kiss * 340136d5fe6aSZoltan Kiss * Return value: 340236d5fe6aSZoltan Kiss * 0: everything is OK 340336d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 340436d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3405af2806f8SThomas Graf */ 340636d5fe6aSZoltan Kiss int 340736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3408af2806f8SThomas Graf { 3409af2806f8SThomas Graf int i, j = 0; 3410af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 341136d5fe6aSZoltan Kiss int ret; 3412af2806f8SThomas Graf struct page *page; 3413af2806f8SThomas Graf unsigned int offset; 3414af2806f8SThomas Graf 3415af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3416af2806f8SThomas Graf 3417af2806f8SThomas Graf /* dont bother with small payloads */ 341836d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 341936d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3420af2806f8SThomas Graf 3421af2806f8SThomas Graf if (hlen) { 342236d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 342336d5fe6aSZoltan Kiss if (unlikely(ret)) 342436d5fe6aSZoltan Kiss return ret; 3425af2806f8SThomas Graf len -= hlen; 3426af2806f8SThomas Graf } else { 3427af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3428af2806f8SThomas Graf if (plen) { 3429af2806f8SThomas Graf page = virt_to_head_page(from->head); 3430af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3431af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3432af2806f8SThomas Graf get_page(page); 3433af2806f8SThomas Graf j = 1; 3434af2806f8SThomas Graf len -= plen; 3435af2806f8SThomas Graf } 3436af2806f8SThomas Graf } 3437af2806f8SThomas Graf 3438ede57d58SRichard Gobert skb_len_add(to, len + plen); 3439af2806f8SThomas Graf 344036d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 344136d5fe6aSZoltan Kiss skb_tx_error(from); 344236d5fe6aSZoltan Kiss return -ENOMEM; 344336d5fe6aSZoltan Kiss } 34441f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 344536d5fe6aSZoltan Kiss 3446af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3447d8e18a51SMatthew Wilcox (Oracle) int size; 3448d8e18a51SMatthew Wilcox (Oracle) 3449af2806f8SThomas Graf if (!len) 3450af2806f8SThomas Graf break; 3451af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3452d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3453d8e18a51SMatthew Wilcox (Oracle) len); 3454d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3455d8e18a51SMatthew Wilcox (Oracle) len -= size; 3456af2806f8SThomas Graf skb_frag_ref(to, j); 3457af2806f8SThomas Graf j++; 3458af2806f8SThomas Graf } 3459af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 346036d5fe6aSZoltan Kiss 346136d5fe6aSZoltan Kiss return 0; 3462af2806f8SThomas Graf } 3463af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3464af2806f8SThomas Graf 34651da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 34661da177e4SLinus Torvalds { 3467d3bc23e7SAl Viro __wsum csum; 34681da177e4SLinus Torvalds long csstart; 34691da177e4SLinus Torvalds 347084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 347155508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 34721da177e4SLinus Torvalds else 34731da177e4SLinus Torvalds csstart = skb_headlen(skb); 34741da177e4SLinus Torvalds 347509a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 34761da177e4SLinus Torvalds 3477d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 34781da177e4SLinus Torvalds 34791da177e4SLinus Torvalds csum = 0; 34801da177e4SLinus Torvalds if (csstart != skb->len) 34811da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 34828d5930dfSAl Viro skb->len - csstart); 34831da177e4SLinus Torvalds 348484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3485ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 34861da177e4SLinus Torvalds 3487d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 34881da177e4SLinus Torvalds } 34891da177e4SLinus Torvalds } 3490b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds /** 34931da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 34941da177e4SLinus Torvalds * @list: list to dequeue from 34951da177e4SLinus Torvalds * 34961da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 34971da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 34981da177e4SLinus Torvalds * returned or %NULL if the list is empty. 34991da177e4SLinus Torvalds */ 35001da177e4SLinus Torvalds 35011da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 35021da177e4SLinus Torvalds { 35031da177e4SLinus Torvalds unsigned long flags; 35041da177e4SLinus Torvalds struct sk_buff *result; 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35071da177e4SLinus Torvalds result = __skb_dequeue(list); 35081da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35091da177e4SLinus Torvalds return result; 35101da177e4SLinus Torvalds } 3511b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 35121da177e4SLinus Torvalds 35131da177e4SLinus Torvalds /** 35141da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 35151da177e4SLinus Torvalds * @list: list to dequeue from 35161da177e4SLinus Torvalds * 35171da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 35181da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 35191da177e4SLinus Torvalds * returned or %NULL if the list is empty. 35201da177e4SLinus Torvalds */ 35211da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 35221da177e4SLinus Torvalds { 35231da177e4SLinus Torvalds unsigned long flags; 35241da177e4SLinus Torvalds struct sk_buff *result; 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35271da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 35281da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35291da177e4SLinus Torvalds return result; 35301da177e4SLinus Torvalds } 3531b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 35321da177e4SLinus Torvalds 35331da177e4SLinus Torvalds /** 35341da177e4SLinus Torvalds * skb_queue_purge - empty a list 35351da177e4SLinus Torvalds * @list: list to empty 35361da177e4SLinus Torvalds * 35371da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 35381da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 35391da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 35401da177e4SLinus Torvalds */ 35411da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 35421da177e4SLinus Torvalds { 35431da177e4SLinus Torvalds struct sk_buff *skb; 35441da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 35451da177e4SLinus Torvalds kfree_skb(skb); 35461da177e4SLinus Torvalds } 3547b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 35481da177e4SLinus Torvalds 35491da177e4SLinus Torvalds /** 35509f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 35519f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3552385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 35539f5afeaeSYaogong Wang * 35549f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 35559f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 35569f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 35579f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 35589f5afeaeSYaogong Wang */ 3559385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 35609f5afeaeSYaogong Wang { 35617c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3562385114deSPeter Oskolkov unsigned int sum = 0; 35639f5afeaeSYaogong Wang 35647c90584cSEric Dumazet while (p) { 35657c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 35667c90584cSEric Dumazet 35677c90584cSEric Dumazet p = rb_next(p); 35687c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3569385114deSPeter Oskolkov sum += skb->truesize; 35709f5afeaeSYaogong Wang kfree_skb(skb); 35717c90584cSEric Dumazet } 3572385114deSPeter Oskolkov return sum; 35739f5afeaeSYaogong Wang } 35749f5afeaeSYaogong Wang 35759f5afeaeSYaogong Wang /** 35761da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 35771da177e4SLinus Torvalds * @list: list to use 35781da177e4SLinus Torvalds * @newsk: buffer to queue 35791da177e4SLinus Torvalds * 35801da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 35811da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 35821da177e4SLinus Torvalds * safely. 35831da177e4SLinus Torvalds * 35841da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 35851da177e4SLinus Torvalds */ 35861da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 35871da177e4SLinus Torvalds { 35881da177e4SLinus Torvalds unsigned long flags; 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35911da177e4SLinus Torvalds __skb_queue_head(list, newsk); 35921da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35931da177e4SLinus Torvalds } 3594b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 35951da177e4SLinus Torvalds 35961da177e4SLinus Torvalds /** 35971da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 35981da177e4SLinus Torvalds * @list: list to use 35991da177e4SLinus Torvalds * @newsk: buffer to queue 36001da177e4SLinus Torvalds * 36011da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 36021da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 36031da177e4SLinus Torvalds * safely. 36041da177e4SLinus Torvalds * 36051da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 36061da177e4SLinus Torvalds */ 36071da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 36081da177e4SLinus Torvalds { 36091da177e4SLinus Torvalds unsigned long flags; 36101da177e4SLinus Torvalds 36111da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36121da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 36131da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36141da177e4SLinus Torvalds } 3615b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 36168728b834SDavid S. Miller 36171da177e4SLinus Torvalds /** 36181da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 36191da177e4SLinus Torvalds * @skb: buffer to remove 36208728b834SDavid S. Miller * @list: list to use 36211da177e4SLinus Torvalds * 36228728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 36238728b834SDavid S. Miller * function is atomic with respect to other list locked calls 36241da177e4SLinus Torvalds * 36258728b834SDavid S. Miller * You must know what list the SKB is on. 36261da177e4SLinus Torvalds */ 36278728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 36281da177e4SLinus Torvalds { 36291da177e4SLinus Torvalds unsigned long flags; 36301da177e4SLinus Torvalds 36311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 36328728b834SDavid S. Miller __skb_unlink(skb, list); 36331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 36341da177e4SLinus Torvalds } 3635b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 36361da177e4SLinus Torvalds 36371da177e4SLinus Torvalds /** 36381da177e4SLinus Torvalds * skb_append - append a buffer 36391da177e4SLinus Torvalds * @old: buffer to insert after 36401da177e4SLinus Torvalds * @newsk: buffer to insert 36418728b834SDavid S. Miller * @list: list to use 36421da177e4SLinus Torvalds * 36431da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 36441da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 36451da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 36461da177e4SLinus Torvalds */ 36478728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 36481da177e4SLinus Torvalds { 36491da177e4SLinus Torvalds unsigned long flags; 36501da177e4SLinus Torvalds 36518728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 36527de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 36538728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 36541da177e4SLinus Torvalds } 3655b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 36581da177e4SLinus Torvalds struct sk_buff* skb1, 36591da177e4SLinus Torvalds const u32 len, const int pos) 36601da177e4SLinus Torvalds { 36611da177e4SLinus Torvalds int i; 36621da177e4SLinus Torvalds 3663d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3664d626f62bSArnaldo Carvalho de Melo pos - len); 36651da177e4SLinus Torvalds /* And move data appendix as is. */ 36661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 36671da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 36681da177e4SLinus Torvalds 36691da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 36701da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 36711da177e4SLinus Torvalds skb1->data_len = skb->data_len; 36721da177e4SLinus Torvalds skb1->len += skb1->data_len; 36731da177e4SLinus Torvalds skb->data_len = 0; 36741da177e4SLinus Torvalds skb->len = len; 367527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 36791da177e4SLinus Torvalds struct sk_buff* skb1, 36801da177e4SLinus Torvalds const u32 len, int pos) 36811da177e4SLinus Torvalds { 36821da177e4SLinus Torvalds int i, k = 0; 36831da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 36841da177e4SLinus Torvalds 36851da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 36861da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 36871da177e4SLinus Torvalds skb->len = len; 36881da177e4SLinus Torvalds skb->data_len = len - pos; 36891da177e4SLinus Torvalds 36901da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 36919e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 36921da177e4SLinus Torvalds 36931da177e4SLinus Torvalds if (pos + size > len) { 36941da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 36951da177e4SLinus Torvalds 36961da177e4SLinus Torvalds if (pos < len) { 36971da177e4SLinus Torvalds /* Split frag. 36981da177e4SLinus Torvalds * We have two variants in this case: 36991da177e4SLinus Torvalds * 1. Move all the frag to the second 37001da177e4SLinus Torvalds * part, if it is possible. F.e. 37011da177e4SLinus Torvalds * this approach is mandatory for TUX, 37021da177e4SLinus Torvalds * where splitting is expensive. 37031da177e4SLinus Torvalds * 2. Split is accurately. We make this. 37041da177e4SLinus Torvalds */ 3705ea2ab693SIan Campbell skb_frag_ref(skb, i); 3706b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 37079e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 37089e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 37091da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 37101da177e4SLinus Torvalds } 37111da177e4SLinus Torvalds k++; 37121da177e4SLinus Torvalds } else 37131da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 37141da177e4SLinus Torvalds pos += size; 37151da177e4SLinus Torvalds } 37161da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 37171da177e4SLinus Torvalds } 37181da177e4SLinus Torvalds 37191da177e4SLinus Torvalds /** 37201da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 37211da177e4SLinus Torvalds * @skb: the buffer to split 37221da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 37231da177e4SLinus Torvalds * @len: new length for skb 37241da177e4SLinus Torvalds */ 37251da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 37261da177e4SLinus Torvalds { 37271da177e4SLinus Torvalds int pos = skb_headlen(skb); 37289b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 37291da177e4SLinus Torvalds 3730753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3731753f1ca4SPavel Begunkov 37329b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 37331f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 37341da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 37351da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 37361da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 37371da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 37381da177e4SLinus Torvalds } 3739b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 37401da177e4SLinus Torvalds 37419f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 37429f782db3SIlpo Järvinen * 37439f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 37449f782db3SIlpo Järvinen */ 3745832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3746832d11c5SIlpo Järvinen { 3747c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3748832d11c5SIlpo Järvinen } 3749832d11c5SIlpo Järvinen 3750832d11c5SIlpo Järvinen /** 3751832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3752832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3753832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3754832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3755832d11c5SIlpo Järvinen * 3756832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 375720e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3758832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3759832d11c5SIlpo Järvinen * 3760832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3761832d11c5SIlpo Järvinen * 3762832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3763832d11c5SIlpo Järvinen * to have non-paged data as well. 3764832d11c5SIlpo Järvinen * 3765832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3766832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3767832d11c5SIlpo Järvinen */ 3768832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3769832d11c5SIlpo Järvinen { 3770832d11c5SIlpo Järvinen int from, to, merge, todo; 3771d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3772832d11c5SIlpo Järvinen 3773832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3774f8071cdeSEric Dumazet 3775f8071cdeSEric Dumazet if (skb_headlen(skb)) 3776f8071cdeSEric Dumazet return 0; 37771f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 37781f8b977aSWillem de Bruijn return 0; 3779832d11c5SIlpo Järvinen 3780832d11c5SIlpo Järvinen todo = shiftlen; 3781832d11c5SIlpo Järvinen from = 0; 3782832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3783832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3784832d11c5SIlpo Järvinen 3785832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3786832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3787832d11c5SIlpo Järvinen */ 3788832d11c5SIlpo Järvinen if (!to || 3789ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3790b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3791832d11c5SIlpo Järvinen merge = -1; 3792832d11c5SIlpo Järvinen } else { 3793832d11c5SIlpo Järvinen merge = to - 1; 3794832d11c5SIlpo Järvinen 37959e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3796832d11c5SIlpo Järvinen if (todo < 0) { 3797832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3798832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3799832d11c5SIlpo Järvinen return 0; 3800832d11c5SIlpo Järvinen 38019f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 38029f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3803832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3804832d11c5SIlpo Järvinen 38059e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 38069e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3807b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3808832d11c5SIlpo Järvinen 3809832d11c5SIlpo Järvinen goto onlymerged; 3810832d11c5SIlpo Järvinen } 3811832d11c5SIlpo Järvinen 3812832d11c5SIlpo Järvinen from++; 3813832d11c5SIlpo Järvinen } 3814832d11c5SIlpo Järvinen 3815832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3816832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3817832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3818832d11c5SIlpo Järvinen return 0; 3819832d11c5SIlpo Järvinen 3820832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3821832d11c5SIlpo Järvinen return 0; 3822832d11c5SIlpo Järvinen 3823832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3824832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3825832d11c5SIlpo Järvinen return 0; 3826832d11c5SIlpo Järvinen 3827832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3828832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3829832d11c5SIlpo Järvinen 38309e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3831832d11c5SIlpo Järvinen *fragto = *fragfrom; 38329e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3833832d11c5SIlpo Järvinen from++; 3834832d11c5SIlpo Järvinen to++; 3835832d11c5SIlpo Järvinen 3836832d11c5SIlpo Järvinen } else { 3837ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3838b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3839b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 38409e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3841832d11c5SIlpo Järvinen 3842b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 38439e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3844832d11c5SIlpo Järvinen todo = 0; 3845832d11c5SIlpo Järvinen 3846832d11c5SIlpo Järvinen to++; 3847832d11c5SIlpo Järvinen break; 3848832d11c5SIlpo Järvinen } 3849832d11c5SIlpo Järvinen } 3850832d11c5SIlpo Järvinen 3851832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3852832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3853832d11c5SIlpo Järvinen 3854832d11c5SIlpo Järvinen if (merge >= 0) { 3855832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3856832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3857832d11c5SIlpo Järvinen 38589e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 38596a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3860832d11c5SIlpo Järvinen } 3861832d11c5SIlpo Järvinen 3862832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3863832d11c5SIlpo Järvinen to = 0; 3864832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3865832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3866832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3867832d11c5SIlpo Järvinen 3868832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3869832d11c5SIlpo Järvinen 3870832d11c5SIlpo Järvinen onlymerged: 3871832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3872832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3873832d11c5SIlpo Järvinen */ 3874832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3875832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3876832d11c5SIlpo Järvinen 3877ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 3878ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 3879832d11c5SIlpo Järvinen 3880832d11c5SIlpo Järvinen return shiftlen; 3881832d11c5SIlpo Järvinen } 3882832d11c5SIlpo Järvinen 3883677e90edSThomas Graf /** 3884677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3885677e90edSThomas Graf * @skb: the buffer to read 3886677e90edSThomas Graf * @from: lower offset of data to be read 3887677e90edSThomas Graf * @to: upper offset of data to be read 3888677e90edSThomas Graf * @st: state variable 3889677e90edSThomas Graf * 3890677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3891677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3892677e90edSThomas Graf */ 3893677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3894677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3895677e90edSThomas Graf { 3896677e90edSThomas Graf st->lower_offset = from; 3897677e90edSThomas Graf st->upper_offset = to; 3898677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3899677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3900677e90edSThomas Graf st->frag_data = NULL; 390197550f6fSWillem de Bruijn st->frag_off = 0; 3902677e90edSThomas Graf } 3903b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3904677e90edSThomas Graf 3905677e90edSThomas Graf /** 3906677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3907677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3908677e90edSThomas Graf * @data: destination pointer for data to be returned 3909677e90edSThomas Graf * @st: state variable 3910677e90edSThomas Graf * 3911bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3912677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3913bc32383cSMathias Krause * the head of the data block to @data and returns the length 3914677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3915677e90edSThomas Graf * offset has been reached. 3916677e90edSThomas Graf * 3917677e90edSThomas Graf * The caller is not required to consume all of the data 3918bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3919677e90edSThomas Graf * of bytes already consumed and the next call to 3920677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3921677e90edSThomas Graf * 392225985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3923e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3924677e90edSThomas Graf * reads of potentially non linear data. 3925677e90edSThomas Graf * 3926bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3927677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3928677e90edSThomas Graf * a stack for this purpose. 3929677e90edSThomas Graf */ 3930677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3931677e90edSThomas Graf struct skb_seq_state *st) 3932677e90edSThomas Graf { 3933677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3934677e90edSThomas Graf skb_frag_t *frag; 3935677e90edSThomas Graf 3936aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3937aeb193eaSWedson Almeida Filho if (st->frag_data) { 3938aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3939aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3940aeb193eaSWedson Almeida Filho } 3941677e90edSThomas Graf return 0; 3942aeb193eaSWedson Almeida Filho } 3943677e90edSThomas Graf 3944677e90edSThomas Graf next_skb: 394595e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3946677e90edSThomas Graf 3947995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 394895e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3949677e90edSThomas Graf return block_limit - abs_offset; 3950677e90edSThomas Graf } 3951677e90edSThomas Graf 3952677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3953677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3954677e90edSThomas Graf 3955677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 395697550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 3957677e90edSThomas Graf 395897550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 395997550f6fSWillem de Bruijn 396097550f6fSWillem de Bruijn pg_idx = 0; 396197550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 396297550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 396397550f6fSWillem de Bruijn 396497550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 396597550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 396697550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 396797550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 396897550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 396997550f6fSWillem de Bruijn } 397097550f6fSWillem de Bruijn 397197550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 3972677e90edSThomas Graf if (abs_offset < block_limit) { 3973677e90edSThomas Graf if (!st->frag_data) 397497550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3975677e90edSThomas Graf 397697550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 3977677e90edSThomas Graf (abs_offset - st->stepped_offset); 3978677e90edSThomas Graf 3979677e90edSThomas Graf return block_limit - abs_offset; 3980677e90edSThomas Graf } 3981677e90edSThomas Graf 3982677e90edSThomas Graf if (st->frag_data) { 398351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3984677e90edSThomas Graf st->frag_data = NULL; 3985677e90edSThomas Graf } 3986677e90edSThomas Graf 398797550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 398897550f6fSWillem de Bruijn st->frag_off += pg_sz; 398997550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 399097550f6fSWillem de Bruijn st->frag_off = 0; 3991677e90edSThomas Graf st->frag_idx++; 399297550f6fSWillem de Bruijn } 3993677e90edSThomas Graf } 3994677e90edSThomas Graf 39955b5a60daSOlaf Kirch if (st->frag_data) { 399651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 39975b5a60daSOlaf Kirch st->frag_data = NULL; 39985b5a60daSOlaf Kirch } 39995b5a60daSOlaf Kirch 400021dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4001677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 400295e3b24cSHerbert Xu st->frag_idx = 0; 4003677e90edSThomas Graf goto next_skb; 400471b3346dSShyam Iyer } else if (st->cur_skb->next) { 400571b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 400671b3346dSShyam Iyer st->frag_idx = 0; 4007677e90edSThomas Graf goto next_skb; 4008677e90edSThomas Graf } 4009677e90edSThomas Graf 4010677e90edSThomas Graf return 0; 4011677e90edSThomas Graf } 4012b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 4013677e90edSThomas Graf 4014677e90edSThomas Graf /** 4015677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 4016677e90edSThomas Graf * @st: state variable 4017677e90edSThomas Graf * 4018677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 4019677e90edSThomas Graf * returned 0. 4020677e90edSThomas Graf */ 4021677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 4022677e90edSThomas Graf { 4023677e90edSThomas Graf if (st->frag_data) 402451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 4025677e90edSThomas Graf } 4026b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 4027677e90edSThomas Graf 40283fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 40293fc7e8a6SThomas Graf 40303fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 40313fc7e8a6SThomas Graf struct ts_config *conf, 40323fc7e8a6SThomas Graf struct ts_state *state) 40333fc7e8a6SThomas Graf { 40343fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 40353fc7e8a6SThomas Graf } 40363fc7e8a6SThomas Graf 40373fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 40383fc7e8a6SThomas Graf { 40393fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 40403fc7e8a6SThomas Graf } 40413fc7e8a6SThomas Graf 40423fc7e8a6SThomas Graf /** 40433fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 40443fc7e8a6SThomas Graf * @skb: the buffer to look in 40453fc7e8a6SThomas Graf * @from: search offset 40463fc7e8a6SThomas Graf * @to: search limit 40473fc7e8a6SThomas Graf * @config: textsearch configuration 40483fc7e8a6SThomas Graf * 40493fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 40503fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 40513fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 40523fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 40533fc7e8a6SThomas Graf */ 40543fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4055059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 40563fc7e8a6SThomas Graf { 4057059a2440SBojan Prtvar struct ts_state state; 4058f72b948dSPhil Oester unsigned int ret; 4059f72b948dSPhil Oester 4060b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4061b228c9b0SWillem de Bruijn 40623fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 40633fc7e8a6SThomas Graf config->finish = skb_ts_finish; 40643fc7e8a6SThomas Graf 4065059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 40663fc7e8a6SThomas Graf 4067059a2440SBojan Prtvar ret = textsearch_find(config, &state); 4068f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 40693fc7e8a6SThomas Graf } 4070b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 40713fc7e8a6SThomas Graf 4072be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4073be12a1feSHannes Frederic Sowa int offset, size_t size) 4074be12a1feSHannes Frederic Sowa { 4075be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 4076be12a1feSHannes Frederic Sowa 4077be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 4078be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4079be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 4080753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 4081be12a1feSHannes Frederic Sowa get_page(page); 4082228ebc41SEric Dumazet skb_fill_page_desc_noacc(skb, i, page, offset, size); 4083be12a1feSHannes Frederic Sowa } else { 4084be12a1feSHannes Frederic Sowa return -EMSGSIZE; 4085be12a1feSHannes Frederic Sowa } 4086be12a1feSHannes Frederic Sowa 4087be12a1feSHannes Frederic Sowa return 0; 4088be12a1feSHannes Frederic Sowa } 4089be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4090be12a1feSHannes Frederic Sowa 4091cbb042f9SHerbert Xu /** 4092cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 4093cbb042f9SHerbert Xu * @skb: buffer to update 4094cbb042f9SHerbert Xu * @len: length of data pulled 4095cbb042f9SHerbert Xu * 4096cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 4097fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 409884fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 409984fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 410084fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4101cbb042f9SHerbert Xu */ 4102af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4103cbb042f9SHerbert Xu { 410431b33dfbSPravin B Shelar unsigned char *data = skb->data; 410531b33dfbSPravin B Shelar 4106cbb042f9SHerbert Xu BUG_ON(len > skb->len); 410731b33dfbSPravin B Shelar __skb_pull(skb, len); 410831b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 410931b33dfbSPravin B Shelar return skb->data; 4110cbb042f9SHerbert Xu } 4111f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4112f94691acSArnaldo Carvalho de Melo 411313acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 411413acc94eSYonghong Song { 411513acc94eSYonghong Song skb_frag_t head_frag; 411613acc94eSYonghong Song struct page *page; 411713acc94eSYonghong Song 411813acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4119d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 4120b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 4121b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 4122d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 412313acc94eSYonghong Song return head_frag; 412413acc94eSYonghong Song } 412513acc94eSYonghong Song 41263a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 41273a1296a3SSteffen Klassert netdev_features_t features, 41283a1296a3SSteffen Klassert unsigned int offset) 41293a1296a3SSteffen Klassert { 41303a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 41313a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 41323a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 41333a1296a3SSteffen Klassert unsigned int delta_len = 0; 41343a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 413553475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4136cf3ab8d4SLina Wang int len_diff, err; 41373a1296a3SSteffen Klassert 41383a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 41393a1296a3SSteffen Klassert 41403a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 41413a1296a3SSteffen Klassert 4142876e8ca8SYan Zhai while (list_skb) { 41433a1296a3SSteffen Klassert nskb = list_skb; 41443a1296a3SSteffen Klassert list_skb = list_skb->next; 41453a1296a3SSteffen Klassert 414653475c5dSDongseok Yi err = 0; 4147224102deSlena wang delta_truesize += nskb->truesize; 414853475c5dSDongseok Yi if (skb_shared(nskb)) { 414953475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 415053475c5dSDongseok Yi if (tmp) { 415153475c5dSDongseok Yi consume_skb(nskb); 415253475c5dSDongseok Yi nskb = tmp; 415353475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 415453475c5dSDongseok Yi } else { 415553475c5dSDongseok Yi err = -ENOMEM; 415653475c5dSDongseok Yi } 415753475c5dSDongseok Yi } 415853475c5dSDongseok Yi 41593a1296a3SSteffen Klassert if (!tail) 41603a1296a3SSteffen Klassert skb->next = nskb; 41613a1296a3SSteffen Klassert else 41623a1296a3SSteffen Klassert tail->next = nskb; 41633a1296a3SSteffen Klassert 416453475c5dSDongseok Yi if (unlikely(err)) { 416553475c5dSDongseok Yi nskb->next = list_skb; 416653475c5dSDongseok Yi goto err_linearize; 416753475c5dSDongseok Yi } 416853475c5dSDongseok Yi 41693a1296a3SSteffen Klassert tail = nskb; 41703a1296a3SSteffen Klassert 41713a1296a3SSteffen Klassert delta_len += nskb->len; 41723a1296a3SSteffen Klassert 41733a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 41743a1296a3SSteffen Klassert 4175cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4176cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 41773a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 41783a1296a3SSteffen Klassert 41793a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4180cf3ab8d4SLina Wang nskb->transport_header += len_diff; 41813a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 41823a1296a3SSteffen Klassert nskb->data - tnl_hlen, 41833a1296a3SSteffen Klassert offset + tnl_hlen); 41843a1296a3SSteffen Klassert 41853a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 41863a1296a3SSteffen Klassert __skb_linearize(nskb)) 41873a1296a3SSteffen Klassert goto err_linearize; 4188876e8ca8SYan Zhai } 41893a1296a3SSteffen Klassert 41903a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 41913a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 41923a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 41933a1296a3SSteffen Klassert 41943a1296a3SSteffen Klassert skb_gso_reset(skb); 41953a1296a3SSteffen Klassert 41963a1296a3SSteffen Klassert skb->prev = tail; 41973a1296a3SSteffen Klassert 41983a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 41993a1296a3SSteffen Klassert __skb_linearize(skb)) 42003a1296a3SSteffen Klassert goto err_linearize; 42013a1296a3SSteffen Klassert 42023a1296a3SSteffen Klassert skb_get(skb); 42033a1296a3SSteffen Klassert 42043a1296a3SSteffen Klassert return skb; 42053a1296a3SSteffen Klassert 42063a1296a3SSteffen Klassert err_linearize: 42073a1296a3SSteffen Klassert kfree_skb_list(skb->next); 42083a1296a3SSteffen Klassert skb->next = NULL; 42093a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 42103a1296a3SSteffen Klassert } 42113a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 42123a1296a3SSteffen Klassert 4213f4c50d99SHerbert Xu /** 4214f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4215df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4216576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4217f4c50d99SHerbert Xu * 4218f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 42194c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 42204c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4221f4c50d99SHerbert Xu */ 4222df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4223df5771ffSMichael S. Tsirkin netdev_features_t features) 4224f4c50d99SHerbert Xu { 4225f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4226f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 42271a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4228df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4229df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4230df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 42311fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4232f4c50d99SHerbert Xu unsigned int offset = doffset; 4233df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4234802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4235f4c50d99SHerbert Xu unsigned int headroom; 4236802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4237ec5f0615SPravin B Shelar __be16 proto; 423836c98382SAlexander Duyck bool csum, sg; 4239df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4240f4c50d99SHerbert Xu int err = -ENOMEM; 4241f4c50d99SHerbert Xu int i = 0; 4242f4c50d99SHerbert Xu int pos; 4243f4c50d99SHerbert Xu 42449e4b7a99SJiri Benc if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) && 42459e4b7a99SJiri Benc mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) { 42469e4b7a99SJiri Benc struct sk_buff *check_skb; 42479e4b7a99SJiri Benc 42489e4b7a99SJiri Benc for (check_skb = list_skb; check_skb; check_skb = check_skb->next) { 42499e4b7a99SJiri Benc if (skb_headlen(check_skb) && !check_skb->head_frag) { 42509e4b7a99SJiri Benc /* gso_size is untrusted, and we have a frag_list with 42519e4b7a99SJiri Benc * a linear non head_frag item. 42523dcbdb13SShmulik Ladkani * 42539e4b7a99SJiri Benc * If head_skb's headlen does not fit requested gso_size, 42549e4b7a99SJiri Benc * it means that the frag_list members do NOT terminate 42559e4b7a99SJiri Benc * on exact gso_size boundaries. Hence we cannot perform 42569e4b7a99SJiri Benc * skb_frag_t page sharing. Therefore we must fallback to 42579e4b7a99SJiri Benc * copying the frag_list skbs; we do so by disabling SG. 42583dcbdb13SShmulik Ladkani */ 42593dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 42609e4b7a99SJiri Benc break; 42619e4b7a99SJiri Benc } 42629e4b7a99SJiri Benc } 42633dcbdb13SShmulik Ladkani } 42643dcbdb13SShmulik Ladkani 42655882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 42662f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4267ec5f0615SPravin B Shelar if (unlikely(!proto)) 4268ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4269ec5f0615SPravin B Shelar 427036c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4271f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 42727e2b10c1STom Herbert 427307b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 427407b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 427507b26c94SSteffen Klassert struct sk_buff *iter; 427643170c4eSIlan Tayari unsigned int frag_len; 427707b26c94SSteffen Klassert 427807b26c94SSteffen Klassert if (!list_skb || 427907b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 428007b26c94SSteffen Klassert goto normal; 428107b26c94SSteffen Klassert 428243170c4eSIlan Tayari /* If we get here then all the required 428343170c4eSIlan Tayari * GSO features except frag_list are supported. 428443170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 428543170c4eSIlan Tayari * with no frag_list. 428643170c4eSIlan Tayari * Currently we can do that only when the buffers don't 428743170c4eSIlan Tayari * have a linear part and all the buffers except 428843170c4eSIlan Tayari * the last are of the same length. 428907b26c94SSteffen Klassert */ 429043170c4eSIlan Tayari frag_len = list_skb->len; 429107b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 429243170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 429343170c4eSIlan Tayari goto normal; 4294eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 429507b26c94SSteffen Klassert goto normal; 429607b26c94SSteffen Klassert 429707b26c94SSteffen Klassert len -= iter->len; 429807b26c94SSteffen Klassert } 429943170c4eSIlan Tayari 430043170c4eSIlan Tayari if (len != frag_len) 430143170c4eSIlan Tayari goto normal; 430207b26c94SSteffen Klassert } 430307b26c94SSteffen Klassert 4304802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4305802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4306802ab55aSAlexander Duyck * now. 4307802ab55aSAlexander Duyck */ 4308802ab55aSAlexander Duyck partial_segs = len / mss; 4309d7fb5a80SAlexander Duyck if (partial_segs > 1) 4310802ab55aSAlexander Duyck mss *= partial_segs; 4311d7fb5a80SAlexander Duyck else 4312d7fb5a80SAlexander Duyck partial_segs = 0; 4313802ab55aSAlexander Duyck } 4314802ab55aSAlexander Duyck 431507b26c94SSteffen Klassert normal: 4316df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4317df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4318f4c50d99SHerbert Xu 4319f4c50d99SHerbert Xu do { 4320f4c50d99SHerbert Xu struct sk_buff *nskb; 43218cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4322c8884eddSHerbert Xu int hsize; 4323f4c50d99SHerbert Xu int size; 4324f4c50d99SHerbert Xu 43253953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 43263953c46cSMarcelo Ricardo Leitner len = list_skb->len; 43273953c46cSMarcelo Ricardo Leitner } else { 4328df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4329f4c50d99SHerbert Xu if (len > mss) 4330f4c50d99SHerbert Xu len = mss; 43313953c46cSMarcelo Ricardo Leitner } 4332f4c50d99SHerbert Xu 4333df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4334f4c50d99SHerbert Xu 4335dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 43361a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 43371a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 433889319d38SHerbert Xu 43399d8506ccSHerbert Xu i = 0; 43401a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 43411a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 43421fd819ecSMichael S. Tsirkin frag_skb = list_skb; 43431a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 43449d8506ccSHerbert Xu 43459d8506ccSHerbert Xu while (pos < offset + len) { 43469d8506ccSHerbert Xu BUG_ON(i >= nfrags); 43479d8506ccSHerbert Xu 43484e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 43499d8506ccSHerbert Xu if (pos + size > offset + len) 43509d8506ccSHerbert Xu break; 43519d8506ccSHerbert Xu 43529d8506ccSHerbert Xu i++; 43539d8506ccSHerbert Xu pos += size; 43544e1beba1SMichael S. Tsirkin frag++; 43559d8506ccSHerbert Xu } 43569d8506ccSHerbert Xu 43571a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 43581a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 435989319d38SHerbert Xu 4360f4c50d99SHerbert Xu if (unlikely(!nskb)) 4361f4c50d99SHerbert Xu goto err; 4362f4c50d99SHerbert Xu 43639d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 43649d8506ccSHerbert Xu kfree_skb(nskb); 43659d8506ccSHerbert Xu goto err; 43669d8506ccSHerbert Xu } 43679d8506ccSHerbert Xu 4368ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 436989319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 437089319d38SHerbert Xu kfree_skb(nskb); 437189319d38SHerbert Xu goto err; 437289319d38SHerbert Xu } 437389319d38SHerbert Xu 4374ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 437589319d38SHerbert Xu skb_release_head_state(nskb); 437689319d38SHerbert Xu __skb_push(nskb, doffset); 437789319d38SHerbert Xu } else { 437800b229f7SPaolo Abeni if (hsize < 0) 437900b229f7SPaolo Abeni hsize = 0; 4380dbd50f23SXin Long if (hsize > len || !sg) 4381dbd50f23SXin Long hsize = len; 4382dbd50f23SXin Long 4383c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4384df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4385c93bdd0eSMel Gorman NUMA_NO_NODE); 438689319d38SHerbert Xu 438789319d38SHerbert Xu if (unlikely(!nskb)) 438889319d38SHerbert Xu goto err; 438989319d38SHerbert Xu 439089319d38SHerbert Xu skb_reserve(nskb, headroom); 439189319d38SHerbert Xu __skb_put(nskb, doffset); 439289319d38SHerbert Xu } 439389319d38SHerbert Xu 4394f4c50d99SHerbert Xu if (segs) 4395f4c50d99SHerbert Xu tail->next = nskb; 4396f4c50d99SHerbert Xu else 4397f4c50d99SHerbert Xu segs = nskb; 4398f4c50d99SHerbert Xu tail = nskb; 4399f4c50d99SHerbert Xu 4400df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4401f4c50d99SHerbert Xu 4402030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4403fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 440468c33163SPravin B Shelar 4405df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 440668c33163SPravin B Shelar nskb->data - tnl_hlen, 440768c33163SPravin B Shelar doffset + tnl_hlen); 440889319d38SHerbert Xu 44099d8506ccSHerbert Xu if (nskb->len == len + doffset) 44101cdbcb79SSimon Horman goto perform_csum_check; 441189319d38SHerbert Xu 44127fbeffedSAlexander Duyck if (!sg) { 44131454c9faSYadu Kishore if (!csum) { 44147fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 44156f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 441676443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 441776443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 44181454c9faSYadu Kishore skb_put(nskb, 44191454c9faSYadu Kishore len), 44208d5930dfSAl Viro len); 44217e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4422de843723STom Herbert skb_headroom(nskb) + doffset; 44231454c9faSYadu Kishore } else { 4424c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4425c624c58eSlily goto err; 44261454c9faSYadu Kishore } 4427f4c50d99SHerbert Xu continue; 4428f4c50d99SHerbert Xu } 4429f4c50d99SHerbert Xu 44308cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4431f4c50d99SHerbert Xu 4432df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4433d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4434f4c50d99SHerbert Xu 443506b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 443606b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4437cef401deSEric Dumazet 4438bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4439bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4440bf5c25d6SWillem de Bruijn goto err; 4441bf5c25d6SWillem de Bruijn 44429d8506ccSHerbert Xu while (pos < offset + len) { 44439d8506ccSHerbert Xu if (i >= nfrags) { 44449d8506ccSHerbert Xu i = 0; 44451a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 44461a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 44471fd819ecSMichael S. Tsirkin frag_skb = list_skb; 444813acc94eSYonghong Song if (!skb_headlen(list_skb)) { 44499d8506ccSHerbert Xu BUG_ON(!nfrags); 445013acc94eSYonghong Song } else { 445113acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 44529d8506ccSHerbert Xu 445313acc94eSYonghong Song /* to make room for head_frag. */ 445413acc94eSYonghong Song i--; 445513acc94eSYonghong Song frag--; 445613acc94eSYonghong Song } 4457bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4458bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4459bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4460bf5c25d6SWillem de Bruijn goto err; 4461bf5c25d6SWillem de Bruijn 44621a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 44639d8506ccSHerbert Xu } 44649d8506ccSHerbert Xu 44659d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 44669d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 44679d8506ccSHerbert Xu net_warn_ratelimited( 44689d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 44699d8506ccSHerbert Xu pos, mss); 4470ff907a11SEric Dumazet err = -EINVAL; 44719d8506ccSHerbert Xu goto err; 44729d8506ccSHerbert Xu } 44739d8506ccSHerbert Xu 447413acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 44758cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 44768cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4477f4c50d99SHerbert Xu 4478f4c50d99SHerbert Xu if (pos < offset) { 4479b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 44808cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4481f4c50d99SHerbert Xu } 4482f4c50d99SHerbert Xu 448389319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4484f4c50d99SHerbert Xu 4485f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4486f4c50d99SHerbert Xu i++; 44874e1beba1SMichael S. Tsirkin frag++; 4488f4c50d99SHerbert Xu pos += size; 4489f4c50d99SHerbert Xu } else { 44908cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 449189319d38SHerbert Xu goto skip_fraglist; 4492f4c50d99SHerbert Xu } 4493f4c50d99SHerbert Xu 44948cb19905SMichael S. Tsirkin nskb_frag++; 4495f4c50d99SHerbert Xu } 4496f4c50d99SHerbert Xu 449789319d38SHerbert Xu skip_fraglist: 4498f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4499f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4500f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4501ec5f0615SPravin B Shelar 45021cdbcb79SSimon Horman perform_csum_check: 45037fbeffedSAlexander Duyck if (!csum) { 4504ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4505ff907a11SEric Dumazet __skb_linearize(nskb)) 4506ddff00d4SAlexander Duyck goto err; 4507ff907a11SEric Dumazet 45087fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4509ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 451076443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 451176443456SAlexander Duyck skb_checksum(nskb, doffset, 451276443456SAlexander Duyck nskb->len - doffset, 0); 45137e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 45147e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4515ec5f0615SPravin B Shelar } 4516df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4517f4c50d99SHerbert Xu 4518bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4519bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4520bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4521bec3cfdcSEric Dumazet */ 4522bec3cfdcSEric Dumazet segs->prev = tail; 4523432c856fSToshiaki Makita 4524802ab55aSAlexander Duyck if (partial_segs) { 452507b26c94SSteffen Klassert struct sk_buff *iter; 4526802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 452707b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4528802ab55aSAlexander Duyck 4529802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 453007b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4531802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4532802ab55aSAlexander Duyck 4533802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4534802ab55aSAlexander Duyck * our way back down the stack of protocols. 4535802ab55aSAlexander Duyck */ 453607b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 453707b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 453807b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 453907b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 454007b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 454107b26c94SSteffen Klassert } 454207b26c94SSteffen Klassert 454307b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 454407b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 454507b26c94SSteffen Klassert else if (tail != segs) 454607b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4547802ab55aSAlexander Duyck } 4548802ab55aSAlexander Duyck 4549432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4550432c856fSToshiaki Makita * using skb_set_owner_w(). 4551432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4552432c856fSToshiaki Makita */ 4553432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4554432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4555432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4556432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4557432c856fSToshiaki Makita } 4558f4c50d99SHerbert Xu return segs; 4559f4c50d99SHerbert Xu 4560f4c50d99SHerbert Xu err: 4561289dccbeSEric Dumazet kfree_skb_list(segs); 4562f4c50d99SHerbert Xu return ERR_PTR(err); 4563f4c50d99SHerbert Xu } 4564f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4565f4c50d99SHerbert Xu 4566df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4567df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4568df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4569df5042f4SFlorian Westphal 4570df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4571df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4572df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4573df5042f4SFlorian Westphal #endif 45744165079bSFlorian Westphal #ifdef CONFIG_XFRM 45754165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 45764165079bSFlorian Westphal #endif 457795a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 457895a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 457995a7233cSPaul Blakey #endif 45803ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 45813ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 45823ee17bc7SMat Martineau #endif 458378476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 458478476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 458578476d31SJeremy Kerr #endif 4586df5042f4SFlorian Westphal }; 4587df5042f4SFlorian Westphal 4588df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4589df5042f4SFlorian Westphal { 4590df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4591df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4592df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4593df5042f4SFlorian Westphal #endif 45944165079bSFlorian Westphal #ifdef CONFIG_XFRM 45954165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 45964165079bSFlorian Westphal #endif 459795a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 459895a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 459995a7233cSPaul Blakey #endif 46003ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 46013ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 46023ee17bc7SMat Martineau #endif 460378476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 460478476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 460578476d31SJeremy Kerr #endif 4606df5042f4SFlorian Westphal 0; 4607df5042f4SFlorian Westphal } 4608df5042f4SFlorian Westphal 4609df5042f4SFlorian Westphal static void skb_extensions_init(void) 4610df5042f4SFlorian Westphal { 4611df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4612df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4613df5042f4SFlorian Westphal 4614df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4615df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4616df5042f4SFlorian Westphal 0, 4617df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4618df5042f4SFlorian Westphal NULL); 4619df5042f4SFlorian Westphal } 4620df5042f4SFlorian Westphal #else 4621df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4622df5042f4SFlorian Westphal #endif 4623df5042f4SFlorian Westphal 46241da177e4SLinus Torvalds void __init skb_init(void) 46251da177e4SLinus Torvalds { 462679a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 46271da177e4SLinus Torvalds sizeof(struct sk_buff), 46281da177e4SLinus Torvalds 0, 4629e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 463079a8a642SKees Cook offsetof(struct sk_buff, cb), 463179a8a642SKees Cook sizeof_field(struct sk_buff, cb), 463220c2df83SPaul Mundt NULL); 4633d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4634d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4635d179cd12SDavid S. Miller 0, 4636e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 463720c2df83SPaul Mundt NULL); 4638df5042f4SFlorian Westphal skb_extensions_init(); 46391da177e4SLinus Torvalds } 46401da177e4SLinus Torvalds 464151c739d1SDavid S. Miller static int 464248a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 464348a1df65SJason A. Donenfeld unsigned int recursion_level) 4644716ea3a7SDavid Howells { 46451a028e50SDavid S. Miller int start = skb_headlen(skb); 46461a028e50SDavid S. Miller int i, copy = start - offset; 4647fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4648716ea3a7SDavid Howells int elt = 0; 4649716ea3a7SDavid Howells 465048a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 465148a1df65SJason A. Donenfeld return -EMSGSIZE; 465248a1df65SJason A. Donenfeld 4653716ea3a7SDavid Howells if (copy > 0) { 4654716ea3a7SDavid Howells if (copy > len) 4655716ea3a7SDavid Howells copy = len; 4656642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4657716ea3a7SDavid Howells elt++; 4658716ea3a7SDavid Howells if ((len -= copy) == 0) 4659716ea3a7SDavid Howells return elt; 4660716ea3a7SDavid Howells offset += copy; 4661716ea3a7SDavid Howells } 4662716ea3a7SDavid Howells 4663716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 46641a028e50SDavid S. Miller int end; 4665716ea3a7SDavid Howells 4666547b792cSIlpo Järvinen WARN_ON(start > offset + len); 46671a028e50SDavid S. Miller 46689e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4669716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4670716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 467148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 467248a1df65SJason A. Donenfeld return -EMSGSIZE; 4673716ea3a7SDavid Howells 4674716ea3a7SDavid Howells if (copy > len) 4675716ea3a7SDavid Howells copy = len; 4676ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4677b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4678716ea3a7SDavid Howells elt++; 4679716ea3a7SDavid Howells if (!(len -= copy)) 4680716ea3a7SDavid Howells return elt; 4681716ea3a7SDavid Howells offset += copy; 4682716ea3a7SDavid Howells } 46831a028e50SDavid S. Miller start = end; 4684716ea3a7SDavid Howells } 4685716ea3a7SDavid Howells 4686fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 468748a1df65SJason A. Donenfeld int end, ret; 4688716ea3a7SDavid Howells 4689547b792cSIlpo Järvinen WARN_ON(start > offset + len); 46901a028e50SDavid S. Miller 4691fbb398a8SDavid S. Miller end = start + frag_iter->len; 4692716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 469348a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 469448a1df65SJason A. Donenfeld return -EMSGSIZE; 469548a1df65SJason A. Donenfeld 4696716ea3a7SDavid Howells if (copy > len) 4697716ea3a7SDavid Howells copy = len; 469848a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 469948a1df65SJason A. Donenfeld copy, recursion_level + 1); 470048a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 470148a1df65SJason A. Donenfeld return ret; 470248a1df65SJason A. Donenfeld elt += ret; 4703716ea3a7SDavid Howells if ((len -= copy) == 0) 4704716ea3a7SDavid Howells return elt; 4705716ea3a7SDavid Howells offset += copy; 4706716ea3a7SDavid Howells } 47071a028e50SDavid S. Miller start = end; 4708716ea3a7SDavid Howells } 4709716ea3a7SDavid Howells BUG_ON(len); 4710716ea3a7SDavid Howells return elt; 4711716ea3a7SDavid Howells } 4712716ea3a7SDavid Howells 471348a1df65SJason A. Donenfeld /** 471448a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 471548a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 471648a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 471748a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 471848a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 471948a1df65SJason A. Donenfeld * 472048a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 472148a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 472248a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 472348a1df65SJason A. Donenfeld * could not fit. 472448a1df65SJason A. Donenfeld */ 472548a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 472648a1df65SJason A. Donenfeld { 472748a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 472848a1df65SJason A. Donenfeld 472948a1df65SJason A. Donenfeld if (nsg <= 0) 473048a1df65SJason A. Donenfeld return nsg; 473148a1df65SJason A. Donenfeld 473248a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 473348a1df65SJason A. Donenfeld 473448a1df65SJason A. Donenfeld return nsg; 473548a1df65SJason A. Donenfeld } 473648a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 473748a1df65SJason A. Donenfeld 473825a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 473925a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 474025a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 474125a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 474225a91d8dSFan Du * 474325a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 474425a91d8dSFan Du * 1. sg_init_table 474525a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 474625a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 474725a91d8dSFan Du * 474825a91d8dSFan Du * This is equivalent to: 474925a91d8dSFan Du * 1. sg_init_table 475025a91d8dSFan Du * 2. skb_to_sgvec(payload1) 475125a91d8dSFan Du * 3. sg_unmark_end 475225a91d8dSFan Du * 4. skb_to_sgvec(payload2) 475325a91d8dSFan Du * 475425a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 475525a91d8dSFan Du * is more preferable. 475625a91d8dSFan Du */ 475725a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 475825a91d8dSFan Du int offset, int len) 475925a91d8dSFan Du { 476048a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 476125a91d8dSFan Du } 476225a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 476325a91d8dSFan Du 476451c739d1SDavid S. Miller 476551c739d1SDavid S. Miller 4766716ea3a7SDavid Howells /** 4767716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4768716ea3a7SDavid Howells * @skb: The socket buffer to check. 4769716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4770716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4771716ea3a7SDavid Howells * 4772716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4773716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4774716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4775716ea3a7SDavid Howells * 4776716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4777716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4778716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4779716ea3a7SDavid Howells * 4780716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4781716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4782716ea3a7SDavid Howells */ 4783716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4784716ea3a7SDavid Howells { 4785716ea3a7SDavid Howells int copyflag; 4786716ea3a7SDavid Howells int elt; 4787716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4788716ea3a7SDavid Howells 4789716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4790716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4791716ea3a7SDavid Howells * at the moment even if they are anonymous). 4792716ea3a7SDavid Howells */ 4793716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4794c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4795716ea3a7SDavid Howells return -ENOMEM; 4796716ea3a7SDavid Howells 4797716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 479821dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4799716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4800716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4801716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4802716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4803716ea3a7SDavid Howells 4804716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4805716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4806716ea3a7SDavid Howells return -ENOMEM; 4807716ea3a7SDavid Howells 4808716ea3a7SDavid Howells /* Voila! */ 4809716ea3a7SDavid Howells *trailer = skb; 4810716ea3a7SDavid Howells return 1; 4811716ea3a7SDavid Howells } 4812716ea3a7SDavid Howells 4813716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4814716ea3a7SDavid Howells 4815716ea3a7SDavid Howells elt = 1; 4816716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4817716ea3a7SDavid Howells copyflag = 0; 4818716ea3a7SDavid Howells 4819716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4820716ea3a7SDavid Howells int ntail = 0; 4821716ea3a7SDavid Howells 4822716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4823716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4824716ea3a7SDavid Howells * after it. */ 4825716ea3a7SDavid Howells 4826716ea3a7SDavid Howells if (skb_shared(skb1)) 4827716ea3a7SDavid Howells copyflag = 1; 4828716ea3a7SDavid Howells 4829716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4830716ea3a7SDavid Howells 4831716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4832716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 483321dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4834716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4835716ea3a7SDavid Howells ntail = tailbits + 128; 4836716ea3a7SDavid Howells } 4837716ea3a7SDavid Howells 4838716ea3a7SDavid Howells if (copyflag || 4839716ea3a7SDavid Howells skb_cloned(skb1) || 4840716ea3a7SDavid Howells ntail || 4841716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 484221dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4843716ea3a7SDavid Howells struct sk_buff *skb2; 4844716ea3a7SDavid Howells 4845716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4846716ea3a7SDavid Howells if (ntail == 0) 4847716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4848716ea3a7SDavid Howells else 4849716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4850716ea3a7SDavid Howells skb_headroom(skb1), 4851716ea3a7SDavid Howells ntail, 4852716ea3a7SDavid Howells GFP_ATOMIC); 4853716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4854716ea3a7SDavid Howells return -ENOMEM; 4855716ea3a7SDavid Howells 4856716ea3a7SDavid Howells if (skb1->sk) 4857716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4858716ea3a7SDavid Howells 4859716ea3a7SDavid Howells /* Looking around. Are we still alive? 4860716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4861716ea3a7SDavid Howells 4862716ea3a7SDavid Howells skb2->next = skb1->next; 4863716ea3a7SDavid Howells *skb_p = skb2; 4864716ea3a7SDavid Howells kfree_skb(skb1); 4865716ea3a7SDavid Howells skb1 = skb2; 4866716ea3a7SDavid Howells } 4867716ea3a7SDavid Howells elt++; 4868716ea3a7SDavid Howells *trailer = skb1; 4869716ea3a7SDavid Howells skb_p = &skb1->next; 4870716ea3a7SDavid Howells } 4871716ea3a7SDavid Howells 4872716ea3a7SDavid Howells return elt; 4873716ea3a7SDavid Howells } 4874b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4875716ea3a7SDavid Howells 4876b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4877b1faf566SEric Dumazet { 4878b1faf566SEric Dumazet struct sock *sk = skb->sk; 4879b1faf566SEric Dumazet 4880b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4881b1faf566SEric Dumazet } 4882b1faf566SEric Dumazet 48838605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 48848605330aSSoheil Hassas Yeganeh { 48858605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 48868605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 48878605330aSSoheil Hassas Yeganeh */ 48888605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 48898605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 48908605330aSSoheil Hassas Yeganeh } 48918605330aSSoheil Hassas Yeganeh 4892b1faf566SEric Dumazet /* 4893b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4894b1faf566SEric Dumazet */ 4895b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4896b1faf566SEric Dumazet { 4897b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4898ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4899b1faf566SEric Dumazet return -ENOMEM; 4900b1faf566SEric Dumazet 4901b1faf566SEric Dumazet skb_orphan(skb); 4902b1faf566SEric Dumazet skb->sk = sk; 4903b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4904b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 49058605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4906b1faf566SEric Dumazet 4907abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4908abb57ea4SEric Dumazet skb_dst_force(skb); 4909abb57ea4SEric Dumazet 4910b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4911b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4912e3ae2365SAlexander Aring sk_error_report(sk); 4913b1faf566SEric Dumazet return 0; 4914b1faf566SEric Dumazet } 4915b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4916b1faf566SEric Dumazet 491783a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 491883a1a1a7SSoheil Hassas Yeganeh { 491983a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 492083a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 492183a1a1a7SSoheil Hassas Yeganeh } 492283a1a1a7SSoheil Hassas Yeganeh 4923364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4924364a9e93SWillem de Bruijn { 4925364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 492683a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 492783a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4928997d5c3fSEric Dumazet unsigned long flags; 4929364a9e93SWillem de Bruijn 4930997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4931364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 493238b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 493383a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 493438b25793SSoheil Hassas Yeganeh if (icmp_next) 4935985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 493638b25793SSoheil Hassas Yeganeh } 4937997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4938364a9e93SWillem de Bruijn 493983a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 494083a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 494183a1a1a7SSoheil Hassas Yeganeh 494283a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4943e3ae2365SAlexander Aring sk_error_report(sk); 4944364a9e93SWillem de Bruijn 4945364a9e93SWillem de Bruijn return skb; 4946364a9e93SWillem de Bruijn } 4947364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4948364a9e93SWillem de Bruijn 4949cab41c47SAlexander Duyck /** 4950cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4951cab41c47SAlexander Duyck * @skb: the skb to clone 4952cab41c47SAlexander Duyck * 4953cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4954cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4955cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4956cab41c47SAlexander Duyck * 4957cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4958cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4959cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4960cab41c47SAlexander Duyck * the sk_error_queue. 4961cab41c47SAlexander Duyck */ 496262bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 496362bccb8cSAlexander Duyck { 496462bccb8cSAlexander Duyck struct sock *sk = skb->sk; 496562bccb8cSAlexander Duyck struct sk_buff *clone; 496662bccb8cSAlexander Duyck 496741c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 496862bccb8cSAlexander Duyck return NULL; 496962bccb8cSAlexander Duyck 497062bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 497162bccb8cSAlexander Duyck if (!clone) { 497262bccb8cSAlexander Duyck sock_put(sk); 497362bccb8cSAlexander Duyck return NULL; 497462bccb8cSAlexander Duyck } 497562bccb8cSAlexander Duyck 497662bccb8cSAlexander Duyck clone->sk = sk; 497762bccb8cSAlexander Duyck clone->destructor = sock_efree; 497862bccb8cSAlexander Duyck 497962bccb8cSAlexander Duyck return clone; 498062bccb8cSAlexander Duyck } 498162bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 498262bccb8cSAlexander Duyck 498337846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 498437846ef0SAlexander Duyck struct sock *sk, 49854ef1b286SSoheil Hassas Yeganeh int tstype, 49864ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4987ac45f602SPatrick Ohly { 4988ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4989ac45f602SPatrick Ohly int err; 4990ac45f602SPatrick Ohly 49914ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 49924ef1b286SSoheil Hassas Yeganeh 4993ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4994ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4995ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4996ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4997e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 49984ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 49991862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 50004ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 500109c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 500242f67eeaSEric Dumazet if (sk_is_tcp(sk)) 5003a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 50044ed2d765SWillem de Bruijn } 500529030374SEric Dumazet 5006ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 500729030374SEric Dumazet 5008ac45f602SPatrick Ohly if (err) 5009ac45f602SPatrick Ohly kfree_skb(skb); 5010ac45f602SPatrick Ohly } 501137846ef0SAlexander Duyck 5012b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 5013b245be1fSWillem de Bruijn { 5014b245be1fSWillem de Bruijn bool ret; 5015b245be1fSWillem de Bruijn 5016d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 5017b245be1fSWillem de Bruijn return true; 5018b245be1fSWillem de Bruijn 5019b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 5020b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 5021b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 5022b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 5023b245be1fSWillem de Bruijn return ret; 5024b245be1fSWillem de Bruijn } 5025b245be1fSWillem de Bruijn 502637846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 502737846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 502837846ef0SAlexander Duyck { 502937846ef0SAlexander Duyck struct sock *sk = skb->sk; 503037846ef0SAlexander Duyck 5031b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 503235b99dffSWillem de Bruijn goto err; 5033b245be1fSWillem de Bruijn 50349ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 50359ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 50369ac25fc0SEric Dumazet */ 503741c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 503837846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 50394ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 504037846ef0SAlexander Duyck sock_put(sk); 504135b99dffSWillem de Bruijn return; 504237846ef0SAlexander Duyck } 504335b99dffSWillem de Bruijn 504435b99dffSWillem de Bruijn err: 504535b99dffSWillem de Bruijn kfree_skb(skb); 50469ac25fc0SEric Dumazet } 504737846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 504837846ef0SAlexander Duyck 504937846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 5050e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 505137846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 505237846ef0SAlexander Duyck struct sock *sk, int tstype) 505337846ef0SAlexander Duyck { 505437846ef0SAlexander Duyck struct sk_buff *skb; 50554ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 505637846ef0SAlexander Duyck 50573a8dd971SWillem de Bruijn if (!sk) 50583a8dd971SWillem de Bruijn return; 50593a8dd971SWillem de Bruijn 5060b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 5061b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 5062b50a5c70SMiroslav Lichvar return; 5063b50a5c70SMiroslav Lichvar 50643a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 50653a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 506637846ef0SAlexander Duyck return; 506737846ef0SAlexander Duyck 50681c885808SFrancis Yan if (tsonly) { 50691c885808SFrancis Yan #ifdef CONFIG_INET 50701c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 507142f67eeaSEric Dumazet sk_is_tcp(sk)) { 5072e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 5073e7ed11eeSYousuk Seung ack_skb); 50744ef1b286SSoheil Hassas Yeganeh opt_stats = true; 50754ef1b286SSoheil Hassas Yeganeh } else 50761c885808SFrancis Yan #endif 50771c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 50781c885808SFrancis Yan } else { 507937846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 50801c885808SFrancis Yan } 508137846ef0SAlexander Duyck if (!skb) 508237846ef0SAlexander Duyck return; 508337846ef0SAlexander Duyck 508449ca0d8bSWillem de Bruijn if (tsonly) { 5085fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 5086fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 508749ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 508849ca0d8bSWillem de Bruijn } 508949ca0d8bSWillem de Bruijn 509049ca0d8bSWillem de Bruijn if (hwtstamps) 509149ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 509249ca0d8bSWillem de Bruijn else 5093d93376f5SMartin KaFai Lau __net_timestamp(skb); 509449ca0d8bSWillem de Bruijn 50954ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 509637846ef0SAlexander Duyck } 5097e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 5098e7fd2885SWillem de Bruijn 5099e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 5100e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 5101e7fd2885SWillem de Bruijn { 5102e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5103e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5104e7fd2885SWillem de Bruijn } 5105ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5106ac45f602SPatrick Ohly 51076e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 51086e3e939fSJohannes Berg { 51096e3e939fSJohannes Berg struct sock *sk = skb->sk; 51106e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5111dd4f1072SEric Dumazet int err = 1; 51126e3e939fSJohannes Berg 51136e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 51146e3e939fSJohannes Berg skb->wifi_acked = acked; 51156e3e939fSJohannes Berg 51166e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 51176e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 51186e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 51196e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 51206e3e939fSJohannes Berg 5121dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5122dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5123dd4f1072SEric Dumazet */ 512441c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 51256e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5126dd4f1072SEric Dumazet sock_put(sk); 5127dd4f1072SEric Dumazet } 51286e3e939fSJohannes Berg if (err) 51296e3e939fSJohannes Berg kfree_skb(skb); 51306e3e939fSJohannes Berg } 51316e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 51326e3e939fSJohannes Berg 5133f35d9d8aSRusty Russell /** 5134f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5135f35d9d8aSRusty Russell * @skb: the skb to set 5136f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5137f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5138f35d9d8aSRusty Russell * 5139f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5140f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5141f35d9d8aSRusty Russell * 5142f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5143f35d9d8aSRusty Russell * returns false you should drop the packet. 5144f35d9d8aSRusty Russell */ 5145f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5146f35d9d8aSRusty Russell { 514752b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 514852b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 514952b5d6f5SEric Dumazet 515052b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 515152b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 515252b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5153f35d9d8aSRusty Russell return false; 5154f35d9d8aSRusty Russell } 5155f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 515652b5d6f5SEric Dumazet skb->csum_start = csum_start; 5157f35d9d8aSRusty Russell skb->csum_offset = off; 5158e5d5decaSJason Wang skb_set_transport_header(skb, start); 5159f35d9d8aSRusty Russell return true; 5160f35d9d8aSRusty Russell } 5161b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5162f35d9d8aSRusty Russell 5163ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5164ed1f50c3SPaul Durrant unsigned int max) 5165ed1f50c3SPaul Durrant { 5166ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5167ed1f50c3SPaul Durrant return 0; 5168ed1f50c3SPaul Durrant 5169ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5170ed1f50c3SPaul Durrant * won't need to do it again. 5171ed1f50c3SPaul Durrant */ 5172ed1f50c3SPaul Durrant if (max > skb->len) 5173ed1f50c3SPaul Durrant max = skb->len; 5174ed1f50c3SPaul Durrant 5175ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5176ed1f50c3SPaul Durrant return -ENOMEM; 5177ed1f50c3SPaul Durrant 5178ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5179ed1f50c3SPaul Durrant return -EPROTO; 5180ed1f50c3SPaul Durrant 5181ed1f50c3SPaul Durrant return 0; 5182ed1f50c3SPaul Durrant } 5183ed1f50c3SPaul Durrant 5184f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5185f9708b43SJan Beulich 5186f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5187f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5188f9708b43SJan Beulich unsigned int off) 5189f9708b43SJan Beulich { 5190f9708b43SJan Beulich int err; 5191f9708b43SJan Beulich 5192161d1792SKees Cook switch (proto) { 5193f9708b43SJan Beulich case IPPROTO_TCP: 5194f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5195f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5196f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5197f9708b43SJan Beulich offsetof(struct tcphdr, 5198f9708b43SJan Beulich check))) 5199f9708b43SJan Beulich err = -EPROTO; 5200f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5201f9708b43SJan Beulich 5202f9708b43SJan Beulich case IPPROTO_UDP: 5203f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5204f9708b43SJan Beulich off + sizeof(struct udphdr)); 5205f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5206f9708b43SJan Beulich offsetof(struct udphdr, 5207f9708b43SJan Beulich check))) 5208f9708b43SJan Beulich err = -EPROTO; 5209f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5210f9708b43SJan Beulich } 5211f9708b43SJan Beulich 5212f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5213f9708b43SJan Beulich } 5214f9708b43SJan Beulich 5215ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5216ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5217ed1f50c3SPaul Durrant */ 5218ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5219ed1f50c3SPaul Durrant 5220f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5221ed1f50c3SPaul Durrant { 5222ed1f50c3SPaul Durrant unsigned int off; 5223ed1f50c3SPaul Durrant bool fragment; 5224f9708b43SJan Beulich __sum16 *csum; 5225ed1f50c3SPaul Durrant int err; 5226ed1f50c3SPaul Durrant 5227ed1f50c3SPaul Durrant fragment = false; 5228ed1f50c3SPaul Durrant 5229ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5230ed1f50c3SPaul Durrant sizeof(struct iphdr), 5231ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5232ed1f50c3SPaul Durrant if (err < 0) 5233ed1f50c3SPaul Durrant goto out; 5234ed1f50c3SPaul Durrant 523511f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5236ed1f50c3SPaul Durrant fragment = true; 5237ed1f50c3SPaul Durrant 5238ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5239ed1f50c3SPaul Durrant 5240ed1f50c3SPaul Durrant err = -EPROTO; 5241ed1f50c3SPaul Durrant 5242ed1f50c3SPaul Durrant if (fragment) 5243ed1f50c3SPaul Durrant goto out; 5244ed1f50c3SPaul Durrant 5245f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5246f9708b43SJan Beulich if (IS_ERR(csum)) 5247f9708b43SJan Beulich return PTR_ERR(csum); 5248ed1f50c3SPaul Durrant 5249ed1f50c3SPaul Durrant if (recalculate) 5250f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5251ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5252ed1f50c3SPaul Durrant skb->len - off, 5253f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5254ed1f50c3SPaul Durrant err = 0; 5255ed1f50c3SPaul Durrant 5256ed1f50c3SPaul Durrant out: 5257ed1f50c3SPaul Durrant return err; 5258ed1f50c3SPaul Durrant } 5259ed1f50c3SPaul Durrant 5260ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5261ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5262ed1f50c3SPaul Durrant */ 5263ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5264ed1f50c3SPaul Durrant 5265ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5266ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5267ed1f50c3SPaul Durrant 5268ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5269ed1f50c3SPaul Durrant { 5270ed1f50c3SPaul Durrant int err; 5271ed1f50c3SPaul Durrant u8 nexthdr; 5272ed1f50c3SPaul Durrant unsigned int off; 5273ed1f50c3SPaul Durrant unsigned int len; 5274ed1f50c3SPaul Durrant bool fragment; 5275ed1f50c3SPaul Durrant bool done; 5276f9708b43SJan Beulich __sum16 *csum; 5277ed1f50c3SPaul Durrant 5278ed1f50c3SPaul Durrant fragment = false; 5279ed1f50c3SPaul Durrant done = false; 5280ed1f50c3SPaul Durrant 5281ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5282ed1f50c3SPaul Durrant 5283ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5284ed1f50c3SPaul Durrant if (err < 0) 5285ed1f50c3SPaul Durrant goto out; 5286ed1f50c3SPaul Durrant 5287ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5288ed1f50c3SPaul Durrant 5289ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5290ed1f50c3SPaul Durrant while (off <= len && !done) { 5291ed1f50c3SPaul Durrant switch (nexthdr) { 5292ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5293ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5294ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5295ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5296ed1f50c3SPaul Durrant 5297ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5298ed1f50c3SPaul Durrant off + 5299ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5300ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5301ed1f50c3SPaul Durrant if (err < 0) 5302ed1f50c3SPaul Durrant goto out; 5303ed1f50c3SPaul Durrant 5304ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5305ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5306ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5307ed1f50c3SPaul Durrant break; 5308ed1f50c3SPaul Durrant } 5309ed1f50c3SPaul Durrant case IPPROTO_AH: { 5310ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5311ed1f50c3SPaul Durrant 5312ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5313ed1f50c3SPaul Durrant off + 5314ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5315ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5316ed1f50c3SPaul Durrant if (err < 0) 5317ed1f50c3SPaul Durrant goto out; 5318ed1f50c3SPaul Durrant 5319ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5320ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5321ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5322ed1f50c3SPaul Durrant break; 5323ed1f50c3SPaul Durrant } 5324ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5325ed1f50c3SPaul Durrant struct frag_hdr *hp; 5326ed1f50c3SPaul Durrant 5327ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5328ed1f50c3SPaul Durrant off + 5329ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5330ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5331ed1f50c3SPaul Durrant if (err < 0) 5332ed1f50c3SPaul Durrant goto out; 5333ed1f50c3SPaul Durrant 5334ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5335ed1f50c3SPaul Durrant 5336ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5337ed1f50c3SPaul Durrant fragment = true; 5338ed1f50c3SPaul Durrant 5339ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5340ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5341ed1f50c3SPaul Durrant break; 5342ed1f50c3SPaul Durrant } 5343ed1f50c3SPaul Durrant default: 5344ed1f50c3SPaul Durrant done = true; 5345ed1f50c3SPaul Durrant break; 5346ed1f50c3SPaul Durrant } 5347ed1f50c3SPaul Durrant } 5348ed1f50c3SPaul Durrant 5349ed1f50c3SPaul Durrant err = -EPROTO; 5350ed1f50c3SPaul Durrant 5351ed1f50c3SPaul Durrant if (!done || fragment) 5352ed1f50c3SPaul Durrant goto out; 5353ed1f50c3SPaul Durrant 5354f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5355f9708b43SJan Beulich if (IS_ERR(csum)) 5356f9708b43SJan Beulich return PTR_ERR(csum); 5357ed1f50c3SPaul Durrant 5358ed1f50c3SPaul Durrant if (recalculate) 5359f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5360ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5361f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5362ed1f50c3SPaul Durrant err = 0; 5363ed1f50c3SPaul Durrant 5364ed1f50c3SPaul Durrant out: 5365ed1f50c3SPaul Durrant return err; 5366ed1f50c3SPaul Durrant } 5367ed1f50c3SPaul Durrant 5368ed1f50c3SPaul Durrant /** 5369ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5370ed1f50c3SPaul Durrant * @skb: the skb to set up 5371ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5372ed1f50c3SPaul Durrant */ 5373ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5374ed1f50c3SPaul Durrant { 5375ed1f50c3SPaul Durrant int err; 5376ed1f50c3SPaul Durrant 5377ed1f50c3SPaul Durrant switch (skb->protocol) { 5378ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5379f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5380ed1f50c3SPaul Durrant break; 5381ed1f50c3SPaul Durrant 5382ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5383ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5384ed1f50c3SPaul Durrant break; 5385ed1f50c3SPaul Durrant 5386ed1f50c3SPaul Durrant default: 5387ed1f50c3SPaul Durrant err = -EPROTO; 5388ed1f50c3SPaul Durrant break; 5389ed1f50c3SPaul Durrant } 5390ed1f50c3SPaul Durrant 5391ed1f50c3SPaul Durrant return err; 5392ed1f50c3SPaul Durrant } 5393ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5394ed1f50c3SPaul Durrant 53959afd85c9SLinus Lüssing /** 53969afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 53979afd85c9SLinus Lüssing * @skb: the skb to check 53989afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 53999afd85c9SLinus Lüssing * 54009afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 54019afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 54029afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 54039afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 54049afd85c9SLinus Lüssing * 5405a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5406a516993fSLinus Lüssing * differs from the provided skb. 54079afd85c9SLinus Lüssing */ 54089afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 54099afd85c9SLinus Lüssing unsigned int transport_len) 54109afd85c9SLinus Lüssing { 54119afd85c9SLinus Lüssing struct sk_buff *skb_chk; 54129afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 54139afd85c9SLinus Lüssing int ret; 54149afd85c9SLinus Lüssing 5415a516993fSLinus Lüssing if (skb->len < len) 54169afd85c9SLinus Lüssing return NULL; 5417a516993fSLinus Lüssing else if (skb->len == len) 54189afd85c9SLinus Lüssing return skb; 54199afd85c9SLinus Lüssing 54209afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 54219afd85c9SLinus Lüssing if (!skb_chk) 54229afd85c9SLinus Lüssing return NULL; 54239afd85c9SLinus Lüssing 54249afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 54259afd85c9SLinus Lüssing if (ret) { 54269afd85c9SLinus Lüssing kfree_skb(skb_chk); 54279afd85c9SLinus Lüssing return NULL; 54289afd85c9SLinus Lüssing } 54299afd85c9SLinus Lüssing 54309afd85c9SLinus Lüssing return skb_chk; 54319afd85c9SLinus Lüssing } 54329afd85c9SLinus Lüssing 54339afd85c9SLinus Lüssing /** 54349afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 54359afd85c9SLinus Lüssing * @skb: the skb to check 54369afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 54379afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 54389afd85c9SLinus Lüssing * 54399afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 54409afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 54419afd85c9SLinus Lüssing * 54429afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 54439afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 54449afd85c9SLinus Lüssing * 5445a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5446a516993fSLinus Lüssing * differs from the provided skb. 54479afd85c9SLinus Lüssing */ 54489afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 54499afd85c9SLinus Lüssing unsigned int transport_len, 54509afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 54519afd85c9SLinus Lüssing { 54529afd85c9SLinus Lüssing struct sk_buff *skb_chk; 54539afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5454fcba67c9SLinus Lüssing __sum16 ret; 54559afd85c9SLinus Lüssing 54569afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 54579afd85c9SLinus Lüssing if (!skb_chk) 5458a516993fSLinus Lüssing goto err; 54599afd85c9SLinus Lüssing 5460a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5461a516993fSLinus Lüssing goto err; 54629afd85c9SLinus Lüssing 54639b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 54649afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 54659b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 54669afd85c9SLinus Lüssing 5467a516993fSLinus Lüssing if (ret) 5468a516993fSLinus Lüssing goto err; 54699afd85c9SLinus Lüssing 54709afd85c9SLinus Lüssing return skb_chk; 5471a516993fSLinus Lüssing 5472a516993fSLinus Lüssing err: 5473a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5474a516993fSLinus Lüssing kfree_skb(skb_chk); 5475a516993fSLinus Lüssing 5476a516993fSLinus Lüssing return NULL; 5477a516993fSLinus Lüssing 54789afd85c9SLinus Lüssing } 54799afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 54809afd85c9SLinus Lüssing 54814497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 54824497b076SBen Hutchings { 5483e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5484e87cc472SJoe Perches skb->dev->name); 54854497b076SBen Hutchings } 54864497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5487bad43ca8SEric Dumazet 5488bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5489bad43ca8SEric Dumazet { 54903d861f66SEric Dumazet if (head_stolen) { 54913d861f66SEric Dumazet skb_release_head_state(skb); 5492bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 54933d861f66SEric Dumazet } else { 5494bad43ca8SEric Dumazet __kfree_skb(skb); 5495bad43ca8SEric Dumazet } 54963d861f66SEric Dumazet } 5497bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5498bad43ca8SEric Dumazet 5499bad43ca8SEric Dumazet /** 5500bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5501bad43ca8SEric Dumazet * @to: prior buffer 5502bad43ca8SEric Dumazet * @from: buffer to add 5503bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5504c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5505bad43ca8SEric Dumazet */ 5506bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5507bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5508bad43ca8SEric Dumazet { 5509c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5510bad43ca8SEric Dumazet int i, delta, len = from->len; 5511bad43ca8SEric Dumazet 5512bad43ca8SEric Dumazet *fragstolen = false; 5513bad43ca8SEric Dumazet 5514bad43ca8SEric Dumazet if (skb_cloned(to)) 5515bad43ca8SEric Dumazet return false; 5516bad43ca8SEric Dumazet 55171effe8caSJean-Philippe Brucker /* In general, avoid mixing slab allocated and page_pool allocated 55181effe8caSJean-Philippe Brucker * pages within the same SKB. However when @to is not pp_recycle and 55191effe8caSJean-Philippe Brucker * @from is cloned, we can transition frag pages from page_pool to 55201effe8caSJean-Philippe Brucker * reference counted. 55211effe8caSJean-Philippe Brucker * 55221effe8caSJean-Philippe Brucker * On the other hand, don't allow coalescing two pp_recycle SKBs if 55231effe8caSJean-Philippe Brucker * @from is cloned, in case the SKB is using page_pool fragment 55241effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 55251effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 55261effe8caSJean-Philippe Brucker * inconsistent reference counts. 55276a5bcd84SIlias Apalodimas */ 55281effe8caSJean-Philippe Brucker if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from))) 55296a5bcd84SIlias Apalodimas return false; 55306a5bcd84SIlias Apalodimas 5531bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5532e93a0435SEric Dumazet if (len) 5533bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5534bad43ca8SEric Dumazet *delta_truesize = 0; 5535bad43ca8SEric Dumazet return true; 5536bad43ca8SEric Dumazet } 5537bad43ca8SEric Dumazet 5538c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5539c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5540c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5541bad43ca8SEric Dumazet return false; 55421f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 55431f8b977aSWillem de Bruijn return false; 5544bad43ca8SEric Dumazet 5545bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5546bad43ca8SEric Dumazet struct page *page; 5547bad43ca8SEric Dumazet unsigned int offset; 5548bad43ca8SEric Dumazet 5549c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5550c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5551bad43ca8SEric Dumazet return false; 5552bad43ca8SEric Dumazet 5553bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5554bad43ca8SEric Dumazet return false; 5555bad43ca8SEric Dumazet 5556bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5557bad43ca8SEric Dumazet 5558bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5559bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5560bad43ca8SEric Dumazet 5561c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5562bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5563bad43ca8SEric Dumazet *fragstolen = true; 5564bad43ca8SEric Dumazet } else { 5565c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5566c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5567bad43ca8SEric Dumazet return false; 5568bad43ca8SEric Dumazet 5569f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5570bad43ca8SEric Dumazet } 5571bad43ca8SEric Dumazet 5572bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5573bad43ca8SEric Dumazet 5574c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5575c818fa9eSEric Dumazet from_shinfo->frags, 5576c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5577c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5578bad43ca8SEric Dumazet 5579bad43ca8SEric Dumazet if (!skb_cloned(from)) 5580c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5581bad43ca8SEric Dumazet 55828ea853fdSLi RongQing /* if the skb is not cloned this does nothing 55838ea853fdSLi RongQing * since we set nr_frags to 0. 55848ea853fdSLi RongQing */ 5585c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5586c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5587bad43ca8SEric Dumazet 5588bad43ca8SEric Dumazet to->truesize += delta; 5589bad43ca8SEric Dumazet to->len += len; 5590bad43ca8SEric Dumazet to->data_len += len; 5591bad43ca8SEric Dumazet 5592bad43ca8SEric Dumazet *delta_truesize = delta; 5593bad43ca8SEric Dumazet return true; 5594bad43ca8SEric Dumazet } 5595bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5596621e84d6SNicolas Dichtel 5597621e84d6SNicolas Dichtel /** 55988b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5599621e84d6SNicolas Dichtel * 5600621e84d6SNicolas Dichtel * @skb: buffer to clean 56018b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5602621e84d6SNicolas Dichtel * 56038b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 56048b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 56058b27f277SNicolas Dichtel * operations. 56068b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 56078b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 56088b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5609621e84d6SNicolas Dichtel */ 56108b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5611621e84d6SNicolas Dichtel { 5612621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5613621e84d6SNicolas Dichtel skb->skb_iif = 0; 561460ff7467SWANG Cong skb->ignore_df = 0; 5615621e84d6SNicolas Dichtel skb_dst_drop(skb); 5616174e2381SFlorian Westphal skb_ext_reset(skb); 5617895b5c9fSFlorian Westphal nf_reset_ct(skb); 5618621e84d6SNicolas Dichtel nf_reset_trace(skb); 5619213dd74aSHerbert Xu 56206f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 56216f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5622875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 56236f9a5069SPetr Machata #endif 56246f9a5069SPetr Machata 5625213dd74aSHerbert Xu if (!xnet) 5626213dd74aSHerbert Xu return; 5627213dd74aSHerbert Xu 56282b5ec1a5SYe Yin ipvs_reset(skb); 5629213dd74aSHerbert Xu skb->mark = 0; 5630de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5631621e84d6SNicolas Dichtel } 5632621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5633de960aa9SFlorian Westphal 5634de960aa9SFlorian Westphal /** 5635de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5636de960aa9SFlorian Westphal * 5637de960aa9SFlorian Westphal * @skb: GSO skb 5638de960aa9SFlorian Westphal * 5639de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5640de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5641de960aa9SFlorian Westphal * 5642de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5643de960aa9SFlorian Westphal */ 5644a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5645de960aa9SFlorian Westphal { 5646de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5647f993bc25SFlorian Westphal unsigned int thlen = 0; 5648f993bc25SFlorian Westphal 5649f993bc25SFlorian Westphal if (skb->encapsulation) { 5650f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5651f993bc25SFlorian Westphal skb_transport_header(skb); 5652de960aa9SFlorian Westphal 5653de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5654f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5655f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5656f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 56571dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 565890017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5659ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5660ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5661f993bc25SFlorian Westphal } 56626d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 56636d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 56646d39d589SFlorian Westphal * accounted for. 56656d39d589SFlorian Westphal */ 5666f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5667de960aa9SFlorian Westphal } 5668a4a77718SDaniel Axtens 5669a4a77718SDaniel Axtens /** 5670a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5671a4a77718SDaniel Axtens * 5672a4a77718SDaniel Axtens * @skb: GSO skb 5673a4a77718SDaniel Axtens * 5674a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5675a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5676a4a77718SDaniel Axtens * 5677a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5678a4a77718SDaniel Axtens */ 5679a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5680a4a77718SDaniel Axtens { 5681a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5682a4a77718SDaniel Axtens skb_network_header(skb); 5683a4a77718SDaniel Axtens 5684a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5685a4a77718SDaniel Axtens } 5686a4a77718SDaniel Axtens 5687a4a77718SDaniel Axtens /** 5688a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5689a4a77718SDaniel Axtens * 5690a4a77718SDaniel Axtens * @skb: GSO skb 5691a4a77718SDaniel Axtens * 5692a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5693a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5694a4a77718SDaniel Axtens * headers (TCP/UDP). 5695a4a77718SDaniel Axtens */ 5696a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5697a4a77718SDaniel Axtens { 5698a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5699a4a77718SDaniel Axtens 5700a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5701a4a77718SDaniel Axtens } 57020d5501c1SVlad Yasevich 5703ae7ef81eSMarcelo Ricardo Leitner /** 57042b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 57052b16f048SDaniel Axtens * 57062b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 57072b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 57082b16f048SDaniel Axtens * 57092b16f048SDaniel Axtens * We might want to check: 57102b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 57112b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 57122b16f048SDaniel Axtens * 57132b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 57142b16f048SDaniel Axtens * 571549682bfaSMathieu Malaterre * @skb: GSO skb 571649682bfaSMathieu Malaterre * 57172b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 57182b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 57192b16f048SDaniel Axtens * 57202b16f048SDaniel Axtens * @max_len: The maximum permissible length. 57212b16f048SDaniel Axtens * 57222b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 57232b16f048SDaniel Axtens */ 57242b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 57252b16f048SDaniel Axtens unsigned int seg_len, 57262b16f048SDaniel Axtens unsigned int max_len) { 57272b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 57282b16f048SDaniel Axtens const struct sk_buff *iter; 57292b16f048SDaniel Axtens 57302b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 57312b16f048SDaniel Axtens return seg_len <= max_len; 57322b16f048SDaniel Axtens 57332b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 57342b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 57352b16f048SDaniel Axtens 57362b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 57372b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 57382b16f048SDaniel Axtens return false; 57392b16f048SDaniel Axtens } 57402b16f048SDaniel Axtens 57412b16f048SDaniel Axtens return true; 57422b16f048SDaniel Axtens } 57432b16f048SDaniel Axtens 57442b16f048SDaniel Axtens /** 5745779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5746ae7ef81eSMarcelo Ricardo Leitner * 5747ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 574876f21b99SDavid S. Miller * @mtu: MTU to validate against 5749ae7ef81eSMarcelo Ricardo Leitner * 5750779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5751779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5752779b7931SDaniel Axtens * payload. 5753ae7ef81eSMarcelo Ricardo Leitner */ 5754779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5755ae7ef81eSMarcelo Ricardo Leitner { 57562b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5757ae7ef81eSMarcelo Ricardo Leitner } 5758779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5759ae7ef81eSMarcelo Ricardo Leitner 57602b16f048SDaniel Axtens /** 57612b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 57622b16f048SDaniel Axtens * 57632b16f048SDaniel Axtens * @skb: GSO skb 57642b16f048SDaniel Axtens * @len: length to validate against 57652b16f048SDaniel Axtens * 57662b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 57672b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 57682b16f048SDaniel Axtens */ 57692b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 57702b16f048SDaniel Axtens { 57712b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 57722b16f048SDaniel Axtens } 57732b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 57742b16f048SDaniel Axtens 57750d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 57760d5501c1SVlad Yasevich { 5777d85e8be2SYuya Kusakabe int mac_len, meta_len; 5778d85e8be2SYuya Kusakabe void *meta; 57794bbb3e0eSToshiaki Makita 57800d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 57810d5501c1SVlad Yasevich kfree_skb(skb); 57820d5501c1SVlad Yasevich return NULL; 57830d5501c1SVlad Yasevich } 57840d5501c1SVlad Yasevich 57854bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5786ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 57874bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 57884bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5789ae474573SToshiaki Makita } 5790d85e8be2SYuya Kusakabe 5791d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5792d85e8be2SYuya Kusakabe if (meta_len) { 5793d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5794d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5795d85e8be2SYuya Kusakabe } 5796d85e8be2SYuya Kusakabe 57970d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 57980d5501c1SVlad Yasevich return skb; 57990d5501c1SVlad Yasevich } 58000d5501c1SVlad Yasevich 58010d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 58020d5501c1SVlad Yasevich { 58030d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 58040d5501c1SVlad Yasevich u16 vlan_tci; 58050d5501c1SVlad Yasevich 5806df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 58070d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 58080d5501c1SVlad Yasevich return skb; 58090d5501c1SVlad Yasevich } 58100d5501c1SVlad Yasevich 58110d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 58120d5501c1SVlad Yasevich if (unlikely(!skb)) 58130d5501c1SVlad Yasevich goto err_free; 581455eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 581555eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 58160d5501c1SVlad Yasevich goto err_free; 58170d5501c1SVlad Yasevich 58180d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 58190d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 58200d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 58210d5501c1SVlad Yasevich 58220d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 58230d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 58240d5501c1SVlad Yasevich 58250d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 58260d5501c1SVlad Yasevich if (unlikely(!skb)) 58270d5501c1SVlad Yasevich goto err_free; 58280d5501c1SVlad Yasevich 58290d5501c1SVlad Yasevich skb_reset_network_header(skb); 58308be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 58310d5501c1SVlad Yasevich skb_reset_transport_header(skb); 58320d5501c1SVlad Yasevich skb_reset_mac_len(skb); 58330d5501c1SVlad Yasevich 58340d5501c1SVlad Yasevich return skb; 58350d5501c1SVlad Yasevich 58360d5501c1SVlad Yasevich err_free: 58370d5501c1SVlad Yasevich kfree_skb(skb); 58380d5501c1SVlad Yasevich return NULL; 58390d5501c1SVlad Yasevich } 58400d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 58412e4e4410SEric Dumazet 584292ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5843e2195121SJiri Pirko { 5844e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5845e2195121SJiri Pirko return -ENOMEM; 5846e2195121SJiri Pirko 5847e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5848e2195121SJiri Pirko return 0; 5849e2195121SJiri Pirko 5850e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5851e2195121SJiri Pirko } 5852e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5853e2195121SJiri Pirko 5854bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5855bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5856bfca4c52SShmulik Ladkani */ 5857bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 585893515d53SJiri Pirko { 585993515d53SJiri Pirko struct vlan_hdr *vhdr; 5860b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 586193515d53SJiri Pirko int err; 586293515d53SJiri Pirko 5863b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5864b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5865b6a79208SShmulik Ladkani offset)) { 5866b6a79208SShmulik Ladkani return -EINVAL; 5867b6a79208SShmulik Ladkani } 5868b6a79208SShmulik Ladkani 586993515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 587093515d53SJiri Pirko if (unlikely(err)) 5871b6a79208SShmulik Ladkani return err; 587293515d53SJiri Pirko 587393515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 587493515d53SJiri Pirko 587593515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 587693515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 587793515d53SJiri Pirko 587893515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 587993515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 588093515d53SJiri Pirko 588193515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 588293515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 588393515d53SJiri Pirko 588493515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 588593515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 588693515d53SJiri Pirko 588793515d53SJiri Pirko skb_reset_mac_len(skb); 588893515d53SJiri Pirko 588993515d53SJiri Pirko return err; 589093515d53SJiri Pirko } 5891bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 589293515d53SJiri Pirko 5893b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5894b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5895b6a79208SShmulik Ladkani */ 589693515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 589793515d53SJiri Pirko { 589893515d53SJiri Pirko u16 vlan_tci; 589993515d53SJiri Pirko __be16 vlan_proto; 590093515d53SJiri Pirko int err; 590193515d53SJiri Pirko 5902df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5903b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 590493515d53SJiri Pirko } else { 5905ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 590693515d53SJiri Pirko return 0; 590793515d53SJiri Pirko 590893515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 590993515d53SJiri Pirko if (err) 591093515d53SJiri Pirko return err; 591193515d53SJiri Pirko } 591293515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5913ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 591493515d53SJiri Pirko return 0; 591593515d53SJiri Pirko 591693515d53SJiri Pirko vlan_proto = skb->protocol; 591793515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 591893515d53SJiri Pirko if (unlikely(err)) 591993515d53SJiri Pirko return err; 592093515d53SJiri Pirko 592193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 592293515d53SJiri Pirko return 0; 592393515d53SJiri Pirko } 592493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 592593515d53SJiri Pirko 5926b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5927b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5928b6a79208SShmulik Ladkani */ 592993515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 593093515d53SJiri Pirko { 5931df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5932b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 593393515d53SJiri Pirko int err; 593493515d53SJiri Pirko 5935b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5936b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5937b6a79208SShmulik Ladkani offset)) { 5938b6a79208SShmulik Ladkani return -EINVAL; 5939b6a79208SShmulik Ladkani } 5940b6a79208SShmulik Ladkani 594193515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5942df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5943b6a79208SShmulik Ladkani if (err) 594493515d53SJiri Pirko return err; 59459241e2dfSDaniel Borkmann 594693515d53SJiri Pirko skb->protocol = skb->vlan_proto; 594793515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 594893515d53SJiri Pirko 59496b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 595093515d53SJiri Pirko } 595193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 595293515d53SJiri Pirko return 0; 595393515d53SJiri Pirko } 595493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 595593515d53SJiri Pirko 595619fbcb36SGuillaume Nault /** 595719fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 595819fbcb36SGuillaume Nault * 595919fbcb36SGuillaume Nault * @skb: Socket buffer to modify 596019fbcb36SGuillaume Nault * 596119fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 596219fbcb36SGuillaume Nault * 596319fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 596419fbcb36SGuillaume Nault * present. 596519fbcb36SGuillaume Nault * 596619fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 596719fbcb36SGuillaume Nault */ 596819fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 596919fbcb36SGuillaume Nault { 597019fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 597119fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 597219fbcb36SGuillaume Nault return -EPROTO; 597319fbcb36SGuillaume Nault 597419fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 597519fbcb36SGuillaume Nault skb_reset_mac_header(skb); 597619fbcb36SGuillaume Nault skb_reset_mac_len(skb); 597719fbcb36SGuillaume Nault 597819fbcb36SGuillaume Nault return 0; 597919fbcb36SGuillaume Nault } 598019fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 598119fbcb36SGuillaume Nault 598219fbcb36SGuillaume Nault /** 598319fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 598419fbcb36SGuillaume Nault * 598519fbcb36SGuillaume Nault * @skb: Socket buffer to modify 598619fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 598719fbcb36SGuillaume Nault * @src: Source MAC address of the new header 598819fbcb36SGuillaume Nault * 598919fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 599019fbcb36SGuillaume Nault * 599119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 599219fbcb36SGuillaume Nault * 599319fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 599419fbcb36SGuillaume Nault */ 599519fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 599619fbcb36SGuillaume Nault const unsigned char *src) 599719fbcb36SGuillaume Nault { 599819fbcb36SGuillaume Nault struct ethhdr *eth; 599919fbcb36SGuillaume Nault int err; 600019fbcb36SGuillaume Nault 600119fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 600219fbcb36SGuillaume Nault return -EPROTO; 600319fbcb36SGuillaume Nault 600419fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 600519fbcb36SGuillaume Nault if (err < 0) 600619fbcb36SGuillaume Nault return err; 600719fbcb36SGuillaume Nault 600819fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 600919fbcb36SGuillaume Nault skb_reset_mac_header(skb); 601019fbcb36SGuillaume Nault skb_reset_mac_len(skb); 601119fbcb36SGuillaume Nault 601219fbcb36SGuillaume Nault eth = eth_hdr(skb); 601319fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 601419fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 601519fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 601619fbcb36SGuillaume Nault 601719fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 601819fbcb36SGuillaume Nault 601919fbcb36SGuillaume Nault return 0; 602019fbcb36SGuillaume Nault } 602119fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 602219fbcb36SGuillaume Nault 60238822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 60248822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 60258822e270SJohn Hurley __be16 ethertype) 60268822e270SJohn Hurley { 60278822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 60288822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 60298822e270SJohn Hurley 60308822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 60318822e270SJohn Hurley } 60328822e270SJohn Hurley 60338822e270SJohn Hurley hdr->h_proto = ethertype; 60348822e270SJohn Hurley } 60358822e270SJohn Hurley 60368822e270SJohn Hurley /** 6037e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 6038e7dbfed1SMartin Varghese * the packet 60398822e270SJohn Hurley * 60408822e270SJohn Hurley * @skb: buffer 60418822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 60428822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 6043fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 6044e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 6045e7dbfed1SMartin Varghese * ethernet 60468822e270SJohn Hurley * 60478822e270SJohn Hurley * Expects skb->data at mac header. 60488822e270SJohn Hurley * 60498822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 60508822e270SJohn Hurley */ 6051fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 6052d04ac224SMartin Varghese int mac_len, bool ethernet) 60538822e270SJohn Hurley { 60548822e270SJohn Hurley struct mpls_shim_hdr *lse; 60558822e270SJohn Hurley int err; 60568822e270SJohn Hurley 60578822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 60588822e270SJohn Hurley return -EINVAL; 60598822e270SJohn Hurley 60608822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 60618822e270SJohn Hurley if (skb->encapsulation) 60628822e270SJohn Hurley return -EINVAL; 60638822e270SJohn Hurley 60648822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 60658822e270SJohn Hurley if (unlikely(err)) 60668822e270SJohn Hurley return err; 60678822e270SJohn Hurley 60688822e270SJohn Hurley if (!skb->inner_protocol) { 6069e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 60708822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 60718822e270SJohn Hurley } 60728822e270SJohn Hurley 60738822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 60748822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 6075fa4e0f88SDavide Caratti mac_len); 60768822e270SJohn Hurley skb_reset_mac_header(skb); 6077fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6078e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 60798822e270SJohn Hurley 60808822e270SJohn Hurley lse = mpls_hdr(skb); 60818822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 60828822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 60838822e270SJohn Hurley 60844296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 60858822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 60868822e270SJohn Hurley skb->protocol = mpls_proto; 60878822e270SJohn Hurley 60888822e270SJohn Hurley return 0; 60898822e270SJohn Hurley } 60908822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 60918822e270SJohn Hurley 60922e4e4410SEric Dumazet /** 6093ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 6094ed246ceeSJohn Hurley * 6095ed246ceeSJohn Hurley * @skb: buffer 6096ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 6097fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 609876f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 6099ed246ceeSJohn Hurley * 6100ed246ceeSJohn Hurley * Expects skb->data at mac header. 6101ed246ceeSJohn Hurley * 6102ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6103ed246ceeSJohn Hurley */ 6104040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6105040b5cfbSMartin Varghese bool ethernet) 6106ed246ceeSJohn Hurley { 6107ed246ceeSJohn Hurley int err; 6108ed246ceeSJohn Hurley 6109ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6110dedc5a08SDavide Caratti return 0; 6111ed246ceeSJohn Hurley 6112fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6113ed246ceeSJohn Hurley if (unlikely(err)) 6114ed246ceeSJohn Hurley return err; 6115ed246ceeSJohn Hurley 6116ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6117ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6118fa4e0f88SDavide Caratti mac_len); 6119ed246ceeSJohn Hurley 6120ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6121ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6122fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6123ed246ceeSJohn Hurley 61244296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6125ed246ceeSJohn Hurley struct ethhdr *hdr; 6126ed246ceeSJohn Hurley 6127ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6128ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6129ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6130ed246ceeSJohn Hurley } 6131ed246ceeSJohn Hurley skb->protocol = next_proto; 6132ed246ceeSJohn Hurley 6133ed246ceeSJohn Hurley return 0; 6134ed246ceeSJohn Hurley } 6135ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6136ed246ceeSJohn Hurley 6137ed246ceeSJohn Hurley /** 6138d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6139d27cf5c5SJohn Hurley * 6140d27cf5c5SJohn Hurley * @skb: buffer 6141d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6142d27cf5c5SJohn Hurley * 6143d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6144d27cf5c5SJohn Hurley * 6145d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6146d27cf5c5SJohn Hurley */ 6147d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6148d27cf5c5SJohn Hurley { 6149d27cf5c5SJohn Hurley int err; 6150d27cf5c5SJohn Hurley 6151d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6152d27cf5c5SJohn Hurley return -EINVAL; 6153d27cf5c5SJohn Hurley 6154d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6155d27cf5c5SJohn Hurley if (unlikely(err)) 6156d27cf5c5SJohn Hurley return err; 6157d27cf5c5SJohn Hurley 6158d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6159d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6160d27cf5c5SJohn Hurley 6161d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6162d27cf5c5SJohn Hurley } 6163d27cf5c5SJohn Hurley 6164d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6165d27cf5c5SJohn Hurley 6166d27cf5c5SJohn Hurley return 0; 6167d27cf5c5SJohn Hurley } 6168d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6169d27cf5c5SJohn Hurley 6170d27cf5c5SJohn Hurley /** 61712a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 61722a2ea508SJohn Hurley * 61732a2ea508SJohn Hurley * @skb: buffer 61742a2ea508SJohn Hurley * 61752a2ea508SJohn Hurley * Expects skb->data at mac header. 61762a2ea508SJohn Hurley * 61772a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 61782a2ea508SJohn Hurley */ 61792a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 61802a2ea508SJohn Hurley { 61812a2ea508SJohn Hurley u32 lse; 61822a2ea508SJohn Hurley u8 ttl; 61832a2ea508SJohn Hurley 61842a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 61852a2ea508SJohn Hurley return -EINVAL; 61862a2ea508SJohn Hurley 618713de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 618813de4ed9SDavide Caratti return -ENOMEM; 618913de4ed9SDavide Caratti 61902a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 61912a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 61922a2ea508SJohn Hurley if (!--ttl) 61932a2ea508SJohn Hurley return -EINVAL; 61942a2ea508SJohn Hurley 61952a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 61962a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 61972a2ea508SJohn Hurley 61982a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 61992a2ea508SJohn Hurley } 62002a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 62012a2ea508SJohn Hurley 62022a2ea508SJohn Hurley /** 62032e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 62042e4e4410SEric Dumazet * 6205de3f0d0eSMasanari Iida * @header_len: size of linear part 6206de3f0d0eSMasanari Iida * @data_len: needed length in frags 6207de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6208de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6209de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 62102e4e4410SEric Dumazet * 62112e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 62122e4e4410SEric Dumazet */ 62132e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 62142e4e4410SEric Dumazet unsigned long data_len, 62152e4e4410SEric Dumazet int max_page_order, 62162e4e4410SEric Dumazet int *errcode, 62172e4e4410SEric Dumazet gfp_t gfp_mask) 62182e4e4410SEric Dumazet { 62192e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 62202e4e4410SEric Dumazet unsigned long chunk; 62212e4e4410SEric Dumazet struct sk_buff *skb; 62222e4e4410SEric Dumazet struct page *page; 62232e4e4410SEric Dumazet int i; 62242e4e4410SEric Dumazet 62252e4e4410SEric Dumazet *errcode = -EMSGSIZE; 62262e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 62272e4e4410SEric Dumazet * high order pages... 62282e4e4410SEric Dumazet */ 62292e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 62302e4e4410SEric Dumazet return NULL; 62312e4e4410SEric Dumazet 62322e4e4410SEric Dumazet *errcode = -ENOBUFS; 6233f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 62342e4e4410SEric Dumazet if (!skb) 62352e4e4410SEric Dumazet return NULL; 62362e4e4410SEric Dumazet 62372e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 62382e4e4410SEric Dumazet 62392e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 62402e4e4410SEric Dumazet int order = max_page_order; 62412e4e4410SEric Dumazet 62422e4e4410SEric Dumazet while (order) { 62432e4e4410SEric Dumazet if (npages >= 1 << order) { 6244d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 62452e4e4410SEric Dumazet __GFP_COMP | 6246d14b56f5SMichal Hocko __GFP_NOWARN, 62472e4e4410SEric Dumazet order); 62482e4e4410SEric Dumazet if (page) 62492e4e4410SEric Dumazet goto fill_page; 62502e4e4410SEric Dumazet /* Do not retry other high order allocations */ 62512e4e4410SEric Dumazet order = 1; 62522e4e4410SEric Dumazet max_page_order = 0; 62532e4e4410SEric Dumazet } 62542e4e4410SEric Dumazet order--; 62552e4e4410SEric Dumazet } 62562e4e4410SEric Dumazet page = alloc_page(gfp_mask); 62572e4e4410SEric Dumazet if (!page) 62582e4e4410SEric Dumazet goto failure; 62592e4e4410SEric Dumazet fill_page: 62602e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 62612e4e4410SEric Dumazet PAGE_SIZE << order); 62622e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 62632e4e4410SEric Dumazet data_len -= chunk; 62642e4e4410SEric Dumazet npages -= 1 << order; 62652e4e4410SEric Dumazet } 62662e4e4410SEric Dumazet return skb; 62672e4e4410SEric Dumazet 62682e4e4410SEric Dumazet failure: 62692e4e4410SEric Dumazet kfree_skb(skb); 62702e4e4410SEric Dumazet return NULL; 62712e4e4410SEric Dumazet } 62722e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 62736fa01ccdSSowmini Varadhan 62746fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 62756fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 62766fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 62776fa01ccdSSowmini Varadhan { 62786fa01ccdSSowmini Varadhan int i; 627912d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 62806fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 62816fa01ccdSSowmini Varadhan u8 *data; 62826fa01ccdSSowmini Varadhan 62836fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 62846fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 628512d6c1d3SKees Cook 6286115f1a5cSEric Dumazet size = SKB_HEAD_ALIGN(size); 628712d6c1d3SKees Cook size = kmalloc_size_roundup(size); 628812d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 62896fa01ccdSSowmini Varadhan if (!data) 62906fa01ccdSSowmini Varadhan return -ENOMEM; 629112d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 62926fa01ccdSSowmini Varadhan 62936fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 62946fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 62956fa01ccdSSowmini Varadhan skb->len -= off; 62966fa01ccdSSowmini Varadhan 62976fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 62986fa01ccdSSowmini Varadhan skb_shinfo(skb), 62996fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 63006fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 63016fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 63026fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 63036fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 63046fa01ccdSSowmini Varadhan kfree(data); 63056fa01ccdSSowmini Varadhan return -ENOMEM; 63066fa01ccdSSowmini Varadhan } 63076fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 63086fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 63096fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 63106fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 6311511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 63126fa01ccdSSowmini Varadhan } else { 63136fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 63146fa01ccdSSowmini Varadhan * relocate values 63156fa01ccdSSowmini Varadhan */ 63166fa01ccdSSowmini Varadhan skb_free_head(skb); 63176fa01ccdSSowmini Varadhan } 63186fa01ccdSSowmini Varadhan 63196fa01ccdSSowmini Varadhan skb->head = data; 63206fa01ccdSSowmini Varadhan skb->data = data; 63216fa01ccdSSowmini Varadhan skb->head_frag = 0; 6322763087daSEric Dumazet skb_set_end_offset(skb, size); 63236fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 63246fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 63256fa01ccdSSowmini Varadhan skb->cloned = 0; 63266fa01ccdSSowmini Varadhan skb->hdr_len = 0; 63276fa01ccdSSowmini Varadhan skb->nohdr = 0; 63286fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 63296fa01ccdSSowmini Varadhan 63306fa01ccdSSowmini Varadhan return 0; 63316fa01ccdSSowmini Varadhan } 63326fa01ccdSSowmini Varadhan 63336fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 63346fa01ccdSSowmini Varadhan 63356fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 63366fa01ccdSSowmini Varadhan * pskb_carve() 63376fa01ccdSSowmini Varadhan */ 63386fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 63396fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 63406fa01ccdSSowmini Varadhan gfp_t gfp_mask) 63416fa01ccdSSowmini Varadhan { 63426fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 63436fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 63446fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 63456fa01ccdSSowmini Varadhan 63466fa01ccdSSowmini Varadhan do { 63476fa01ccdSSowmini Varadhan if (!list) { 63486fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 63496fa01ccdSSowmini Varadhan return -EFAULT; 63506fa01ccdSSowmini Varadhan } 63516fa01ccdSSowmini Varadhan if (list->len <= eat) { 63526fa01ccdSSowmini Varadhan /* Eaten as whole. */ 63536fa01ccdSSowmini Varadhan eat -= list->len; 63546fa01ccdSSowmini Varadhan list = list->next; 63556fa01ccdSSowmini Varadhan insp = list; 63566fa01ccdSSowmini Varadhan } else { 63576fa01ccdSSowmini Varadhan /* Eaten partially. */ 63586fa01ccdSSowmini Varadhan if (skb_shared(list)) { 63596fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 63606fa01ccdSSowmini Varadhan if (!clone) 63616fa01ccdSSowmini Varadhan return -ENOMEM; 63626fa01ccdSSowmini Varadhan insp = list->next; 63636fa01ccdSSowmini Varadhan list = clone; 63646fa01ccdSSowmini Varadhan } else { 63656fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 63666fa01ccdSSowmini Varadhan insp = list; 63676fa01ccdSSowmini Varadhan } 63686fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 63696fa01ccdSSowmini Varadhan kfree_skb(clone); 63706fa01ccdSSowmini Varadhan return -ENOMEM; 63716fa01ccdSSowmini Varadhan } 63726fa01ccdSSowmini Varadhan break; 63736fa01ccdSSowmini Varadhan } 63746fa01ccdSSowmini Varadhan } while (eat); 63756fa01ccdSSowmini Varadhan 63766fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 63776fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 63786fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6379ef527f96SEric Dumazet consume_skb(list); 63806fa01ccdSSowmini Varadhan } 63816fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 63826fa01ccdSSowmini Varadhan if (clone) { 63836fa01ccdSSowmini Varadhan clone->next = list; 63846fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 63856fa01ccdSSowmini Varadhan } 63866fa01ccdSSowmini Varadhan return 0; 63876fa01ccdSSowmini Varadhan } 63886fa01ccdSSowmini Varadhan 63896fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 63906fa01ccdSSowmini Varadhan * non-linear part of skb 63916fa01ccdSSowmini Varadhan */ 63926fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 63936fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 63946fa01ccdSSowmini Varadhan { 63956fa01ccdSSowmini Varadhan int i, k = 0; 639612d6c1d3SKees Cook unsigned int size = skb_end_offset(skb); 63976fa01ccdSSowmini Varadhan u8 *data; 63986fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 63996fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 64006fa01ccdSSowmini Varadhan 64016fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 64026fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 640312d6c1d3SKees Cook 6404115f1a5cSEric Dumazet size = SKB_HEAD_ALIGN(size); 640512d6c1d3SKees Cook size = kmalloc_size_roundup(size); 640612d6c1d3SKees Cook data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 64076fa01ccdSSowmini Varadhan if (!data) 64086fa01ccdSSowmini Varadhan return -ENOMEM; 640912d6c1d3SKees Cook size = SKB_WITH_OVERHEAD(size); 64106fa01ccdSSowmini Varadhan 64116fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6412e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 64136fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 64146fa01ccdSSowmini Varadhan kfree(data); 64156fa01ccdSSowmini Varadhan return -ENOMEM; 64166fa01ccdSSowmini Varadhan } 64176fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 64186fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 64196fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 64206fa01ccdSSowmini Varadhan 64216fa01ccdSSowmini Varadhan if (pos + fsize > off) { 64226fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 64236fa01ccdSSowmini Varadhan 64246fa01ccdSSowmini Varadhan if (pos < off) { 64256fa01ccdSSowmini Varadhan /* Split frag. 64266fa01ccdSSowmini Varadhan * We have two variants in this case: 64276fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 64286fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 64296fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 64306fa01ccdSSowmini Varadhan * where splitting is expensive. 64316fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 64326fa01ccdSSowmini Varadhan */ 6433b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 64346fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 64356fa01ccdSSowmini Varadhan } 64366fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 64376fa01ccdSSowmini Varadhan k++; 64386fa01ccdSSowmini Varadhan } 64396fa01ccdSSowmini Varadhan pos += fsize; 64406fa01ccdSSowmini Varadhan } 64416fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 64426fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 64436fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 64446fa01ccdSSowmini Varadhan 64456fa01ccdSSowmini Varadhan /* split line is in frag list */ 6446eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6447eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6448eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6449eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6450eabe8618SMiaohe Lin kfree(data); 6451eabe8618SMiaohe Lin return -ENOMEM; 64526fa01ccdSSowmini Varadhan } 6453511a3edaSEric Dumazet skb_release_data(skb, SKB_CONSUMED); 64546fa01ccdSSowmini Varadhan 64556fa01ccdSSowmini Varadhan skb->head = data; 64566fa01ccdSSowmini Varadhan skb->head_frag = 0; 64576fa01ccdSSowmini Varadhan skb->data = data; 6458763087daSEric Dumazet skb_set_end_offset(skb, size); 64596fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 64606fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 64616fa01ccdSSowmini Varadhan skb->cloned = 0; 64626fa01ccdSSowmini Varadhan skb->hdr_len = 0; 64636fa01ccdSSowmini Varadhan skb->nohdr = 0; 64646fa01ccdSSowmini Varadhan skb->len -= off; 64656fa01ccdSSowmini Varadhan skb->data_len = skb->len; 64666fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 64676fa01ccdSSowmini Varadhan return 0; 64686fa01ccdSSowmini Varadhan } 64696fa01ccdSSowmini Varadhan 64706fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 64716fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 64726fa01ccdSSowmini Varadhan { 64736fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 64746fa01ccdSSowmini Varadhan 64756fa01ccdSSowmini Varadhan if (len < headlen) 64766fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 64776fa01ccdSSowmini Varadhan else 64786fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 64796fa01ccdSSowmini Varadhan } 64806fa01ccdSSowmini Varadhan 64816fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 64826fa01ccdSSowmini Varadhan * a new skb 64836fa01ccdSSowmini Varadhan */ 64846fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 64856fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 64866fa01ccdSSowmini Varadhan { 64876fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 64886fa01ccdSSowmini Varadhan 64896fa01ccdSSowmini Varadhan if (!clone) 64906fa01ccdSSowmini Varadhan return NULL; 64916fa01ccdSSowmini Varadhan 64926fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 64936fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 64946fa01ccdSSowmini Varadhan kfree_skb(clone); 64956fa01ccdSSowmini Varadhan return NULL; 64966fa01ccdSSowmini Varadhan } 64976fa01ccdSSowmini Varadhan return clone; 64986fa01ccdSSowmini Varadhan } 64996fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6500c8c8b127SEric Dumazet 6501c8c8b127SEric Dumazet /** 6502c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6503c8c8b127SEric Dumazet * @skb: buffer 6504c8c8b127SEric Dumazet * 6505c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6506c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6507c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6508c8c8b127SEric Dumazet * truesize. 6509c8c8b127SEric Dumazet * Notes: 6510c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6511c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6512c8c8b127SEric Dumazet */ 6513c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6514c8c8b127SEric Dumazet { 65153174fed9SEric Dumazet if (skb->data_len) { 65163174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6517c8c8b127SEric Dumazet skb_cloned(skb)) 6518c8c8b127SEric Dumazet return; 6519c8c8b127SEric Dumazet 6520c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6521c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 65223174fed9SEric Dumazet } 65233174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 65243174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 65253174fed9SEric Dumazet * their truesize. 65263174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 65273174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 65283174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6529c8c8b127SEric Dumazet */ 6530c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6531c8c8b127SEric Dumazet } 6532d57a3a15SDavid Howells EXPORT_SYMBOL(skb_condense); 6533df5042f4SFlorian Westphal 6534df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6535df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6536df5042f4SFlorian Westphal { 6537df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6538df5042f4SFlorian Westphal } 6539df5042f4SFlorian Westphal 65408b69a803SPaolo Abeni /** 65418b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 65428b69a803SPaolo Abeni * 65434930f483SFlorian Westphal * @flags: See kmalloc(). 65444930f483SFlorian Westphal * 65458b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 65468b69a803SPaolo Abeni * skb via __skb_ext_set(). 65478b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 65488b69a803SPaolo Abeni */ 65494930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6550df5042f4SFlorian Westphal { 65514930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6552df5042f4SFlorian Westphal 6553df5042f4SFlorian Westphal if (new) { 6554df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6555df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6556df5042f4SFlorian Westphal } 6557df5042f4SFlorian Westphal 6558df5042f4SFlorian Westphal return new; 6559df5042f4SFlorian Westphal } 6560df5042f4SFlorian Westphal 65614165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 65624165079bSFlorian Westphal unsigned int old_active) 6563df5042f4SFlorian Westphal { 6564df5042f4SFlorian Westphal struct skb_ext *new; 6565df5042f4SFlorian Westphal 6566df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6567df5042f4SFlorian Westphal return old; 6568df5042f4SFlorian Westphal 6569df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6570df5042f4SFlorian Westphal if (!new) 6571df5042f4SFlorian Westphal return NULL; 6572df5042f4SFlorian Westphal 6573df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6574df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6575df5042f4SFlorian Westphal 65764165079bSFlorian Westphal #ifdef CONFIG_XFRM 65774165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 65784165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 65794165079bSFlorian Westphal unsigned int i; 65804165079bSFlorian Westphal 65814165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 65824165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 65834165079bSFlorian Westphal } 65844165079bSFlorian Westphal #endif 6585df5042f4SFlorian Westphal __skb_ext_put(old); 6586df5042f4SFlorian Westphal return new; 6587df5042f4SFlorian Westphal } 6588df5042f4SFlorian Westphal 6589df5042f4SFlorian Westphal /** 65908b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 65918b69a803SPaolo Abeni * @skb: buffer 65928b69a803SPaolo Abeni * @id: extension id 65938b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 65948b69a803SPaolo Abeni * 65958b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 65968b69a803SPaolo Abeni * 65978b69a803SPaolo Abeni * Returns the pointer to the extension. 65988b69a803SPaolo Abeni */ 65998b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 66008b69a803SPaolo Abeni struct skb_ext *ext) 66018b69a803SPaolo Abeni { 66028b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 66038b69a803SPaolo Abeni 66048b69a803SPaolo Abeni skb_ext_put(skb); 66058b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 66068b69a803SPaolo Abeni ext->chunks = newlen; 66078b69a803SPaolo Abeni ext->offset[id] = newoff; 66088b69a803SPaolo Abeni skb->extensions = ext; 66098b69a803SPaolo Abeni skb->active_extensions = 1 << id; 66108b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 66118b69a803SPaolo Abeni } 66128b69a803SPaolo Abeni 66138b69a803SPaolo Abeni /** 6614df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6615df5042f4SFlorian Westphal * @skb: buffer 6616df5042f4SFlorian Westphal * @id: extension to allocate space for 6617df5042f4SFlorian Westphal * 6618df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6619df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6620df5042f4SFlorian Westphal * is returned. 6621df5042f4SFlorian Westphal * 6622df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6623df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6624df5042f4SFlorian Westphal * 6625df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6626df5042f4SFlorian Westphal */ 6627df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6628df5042f4SFlorian Westphal { 6629df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6630df5042f4SFlorian Westphal unsigned int newlen, newoff; 6631df5042f4SFlorian Westphal 6632df5042f4SFlorian Westphal if (skb->active_extensions) { 6633df5042f4SFlorian Westphal old = skb->extensions; 6634df5042f4SFlorian Westphal 66354165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6636df5042f4SFlorian Westphal if (!new) 6637df5042f4SFlorian Westphal return NULL; 6638df5042f4SFlorian Westphal 6639682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6640df5042f4SFlorian Westphal goto set_active; 6641df5042f4SFlorian Westphal 6642e94e50bdSPaolo Abeni newoff = new->chunks; 6643df5042f4SFlorian Westphal } else { 6644df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6645df5042f4SFlorian Westphal 66464930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6647df5042f4SFlorian Westphal if (!new) 6648df5042f4SFlorian Westphal return NULL; 6649df5042f4SFlorian Westphal } 6650df5042f4SFlorian Westphal 6651df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6652df5042f4SFlorian Westphal new->chunks = newlen; 6653df5042f4SFlorian Westphal new->offset[id] = newoff; 6654df5042f4SFlorian Westphal set_active: 6655b0999f38SPaolo Abeni skb->slow_gro = 1; 6656682ec859SPaolo Abeni skb->extensions = new; 6657df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6658df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6659df5042f4SFlorian Westphal } 6660df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6661df5042f4SFlorian Westphal 66624165079bSFlorian Westphal #ifdef CONFIG_XFRM 66634165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 66644165079bSFlorian Westphal { 66654165079bSFlorian Westphal unsigned int i; 66664165079bSFlorian Westphal 66674165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 66684165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 66694165079bSFlorian Westphal } 66704165079bSFlorian Westphal #endif 66714165079bSFlorian Westphal 667278476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 667378476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 667478476d31SJeremy Kerr { 667578476d31SJeremy Kerr if (flow->key) 667678476d31SJeremy Kerr mctp_key_unref(flow->key); 667778476d31SJeremy Kerr } 667878476d31SJeremy Kerr #endif 667978476d31SJeremy Kerr 6680df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6681df5042f4SFlorian Westphal { 6682df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6683df5042f4SFlorian Westphal 6684df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6685df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6686df5042f4SFlorian Westphal skb->extensions = NULL; 6687df5042f4SFlorian Westphal __skb_ext_put(ext); 66884165079bSFlorian Westphal #ifdef CONFIG_XFRM 66894165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 66904165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 66914165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 66924165079bSFlorian Westphal 66934165079bSFlorian Westphal skb_ext_put_sp(sp); 66944165079bSFlorian Westphal sp->len = 0; 66954165079bSFlorian Westphal #endif 6696df5042f4SFlorian Westphal } 6697df5042f4SFlorian Westphal } 6698df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6699df5042f4SFlorian Westphal 6700df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6701df5042f4SFlorian Westphal { 6702df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6703df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6704df5042f4SFlorian Westphal */ 6705df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6706df5042f4SFlorian Westphal goto free_now; 6707df5042f4SFlorian Westphal 6708df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6709df5042f4SFlorian Westphal return; 6710df5042f4SFlorian Westphal free_now: 67114165079bSFlorian Westphal #ifdef CONFIG_XFRM 67124165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 67134165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 67144165079bSFlorian Westphal #endif 671578476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 671678476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 671778476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 671878476d31SJeremy Kerr #endif 67194165079bSFlorian Westphal 6720df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6721df5042f4SFlorian Westphal } 6722df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6723df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 672468822bdfSEric Dumazet 672568822bdfSEric Dumazet /** 672668822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 672768822bdfSEric Dumazet * @skb: buffer 672868822bdfSEric Dumazet * 672968822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 673068822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 673168822bdfSEric Dumazet * and memory zone spinlock contention. 673268822bdfSEric Dumazet */ 673368822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 673468822bdfSEric Dumazet { 673568822bdfSEric Dumazet int cpu = skb->alloc_cpu; 673668822bdfSEric Dumazet struct softnet_data *sd; 673768822bdfSEric Dumazet unsigned long flags; 673839564c3fSEric Dumazet unsigned int defer_max; 673968822bdfSEric Dumazet bool kick; 674068822bdfSEric Dumazet 674168822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 674268822bdfSEric Dumazet !cpu_online(cpu) || 674368822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 674439564c3fSEric Dumazet nodefer: __kfree_skb(skb); 674568822bdfSEric Dumazet return; 674668822bdfSEric Dumazet } 674768822bdfSEric Dumazet 674868822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 674939564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 675039564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 675139564c3fSEric Dumazet goto nodefer; 675239564c3fSEric Dumazet 675368822bdfSEric Dumazet spin_lock_irqsave(&sd->defer_lock, flags); 6754c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6755c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6756c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6757c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6758c09b0cd2SJakub Kicinski 675968822bdfSEric Dumazet skb->next = sd->defer_list; 676068822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 676168822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 676268822bdfSEric Dumazet spin_unlock_irqrestore(&sd->defer_lock, flags); 676368822bdfSEric Dumazet 676468822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 676568822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 676668822bdfSEric Dumazet */ 676797e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 676868822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 676968822bdfSEric Dumazet } 6770