12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 41da177e4SLinus Torvalds * 5113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 61da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Fixes: 91da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 101da177e4SLinus Torvalds * balancer bugs. 111da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 121da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 131da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 141da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 151da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 161da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 171da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 181da177e4SLinus Torvalds * only put in the headers 191da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 201da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 211da177e4SLinus Torvalds * Andi Kleen : slabified it. 221da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * NOTE: 251da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 261da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 271da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 281da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 35e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 36e005d193SJoe Perches 371da177e4SLinus Torvalds #include <linux/module.h> 381da177e4SLinus Torvalds #include <linux/types.h> 391da177e4SLinus Torvalds #include <linux/kernel.h> 401da177e4SLinus Torvalds #include <linux/mm.h> 411da177e4SLinus Torvalds #include <linux/interrupt.h> 421da177e4SLinus Torvalds #include <linux/in.h> 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/slab.h> 45de960aa9SFlorian Westphal #include <linux/tcp.h> 46de960aa9SFlorian Westphal #include <linux/udp.h> 4790017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 501da177e4SLinus Torvalds #include <net/pkt_sched.h> 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #include <linux/string.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 549c55e01cSJens Axboe #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/cache.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <linux/init.h> 58716ea3a7SDavid Howells #include <linux/scatterlist.h> 59ac45f602SPatrick Ohly #include <linux/errqueue.h> 60268bb0ceSLinus Torvalds #include <linux/prefetch.h> 610d5501c1SVlad Yasevich #include <linux/if_vlan.h> 622a2ea508SJohn Hurley #include <linux/mpls.h> 63183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <net/protocol.h> 661da177e4SLinus Torvalds #include <net/dst.h> 671da177e4SLinus Torvalds #include <net/sock.h> 681da177e4SLinus Torvalds #include <net/checksum.h> 69ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 701da177e4SLinus Torvalds #include <net/xfrm.h> 718822e270SJohn Hurley #include <net/mpls.h> 723ee17bc7SMat Martineau #include <net/mptcp.h> 7378476d31SJeremy Kerr #include <net/mctp.h> 746a5bcd84SIlias Apalodimas #include <net/page_pool.h> 751da177e4SLinus Torvalds 767c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 77ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7851c56b00SEric Dumazet #include <linux/highmem.h> 79b245be1fSWillem de Bruijn #include <linux/capability.h> 80b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 812544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 82a1f8e7f7SAl Viro 8339564c3fSEric Dumazet #include "dev.h" 847f678defSVasily Averin #include "sock_destructor.h" 857b7ed885SBart Van Assche 8608009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8708009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 88df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 89df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 90df5042f4SFlorian Westphal #endif 915f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 925f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 931da177e4SLinus Torvalds 949cb252c4SMenglong Dong #undef FN 959cb252c4SMenglong Dong #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 969cb252c4SMenglong Dong const char * const drop_reasons[] = { 979cb252c4SMenglong Dong DEFINE_DROP_REASON(FN, FN) 989cb252c4SMenglong Dong }; 99ec43908dSMenglong Dong EXPORT_SYMBOL(drop_reasons); 100ec43908dSMenglong Dong 1011da177e4SLinus Torvalds /** 102f05de73bSJean Sacren * skb_panic - private function for out-of-line support 1031da177e4SLinus Torvalds * @skb: buffer 1041da177e4SLinus Torvalds * @sz: size 105f05de73bSJean Sacren * @addr: address 10699d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 1071da177e4SLinus Torvalds * 108f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 109f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 110f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 111f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1121da177e4SLinus Torvalds */ 113f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 11499d5851eSJames Hogan const char msg[]) 1151da177e4SLinus Torvalds { 11641a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 11799d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1184305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 11926095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1201da177e4SLinus Torvalds BUG(); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1241da177e4SLinus Torvalds { 125f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 128f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 129f05de73bSJean Sacren { 130f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 131f05de73bSJean Sacren } 132c93bdd0eSMel Gorman 13350fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 134f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 135f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 13650fad4b5SAlexander Lobakin 137dbae2b06SPaolo Abeni #if PAGE_SIZE == SZ_4K 138dbae2b06SPaolo Abeni 139dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 1 140dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 141dbae2b06SPaolo Abeni 142dbae2b06SPaolo Abeni /* specialized page frag allocator using a single order 0 page 143dbae2b06SPaolo Abeni * and slicing it into 1K sized fragment. Constrained to systems 144dbae2b06SPaolo Abeni * with a very limited amount of 1K fragments fitting a single 145dbae2b06SPaolo Abeni * page - to avoid excessive truesize underestimation 146dbae2b06SPaolo Abeni */ 147dbae2b06SPaolo Abeni 148dbae2b06SPaolo Abeni struct page_frag_1k { 149dbae2b06SPaolo Abeni void *va; 150dbae2b06SPaolo Abeni u16 offset; 151dbae2b06SPaolo Abeni bool pfmemalloc; 152dbae2b06SPaolo Abeni }; 153dbae2b06SPaolo Abeni 154dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 155dbae2b06SPaolo Abeni { 156dbae2b06SPaolo Abeni struct page *page; 157dbae2b06SPaolo Abeni int offset; 158dbae2b06SPaolo Abeni 159dbae2b06SPaolo Abeni offset = nc->offset - SZ_1K; 160dbae2b06SPaolo Abeni if (likely(offset >= 0)) 161dbae2b06SPaolo Abeni goto use_frag; 162dbae2b06SPaolo Abeni 163dbae2b06SPaolo Abeni page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 164dbae2b06SPaolo Abeni if (!page) 165dbae2b06SPaolo Abeni return NULL; 166dbae2b06SPaolo Abeni 167dbae2b06SPaolo Abeni nc->va = page_address(page); 168dbae2b06SPaolo Abeni nc->pfmemalloc = page_is_pfmemalloc(page); 169dbae2b06SPaolo Abeni offset = PAGE_SIZE - SZ_1K; 170dbae2b06SPaolo Abeni page_ref_add(page, offset / SZ_1K); 171dbae2b06SPaolo Abeni 172dbae2b06SPaolo Abeni use_frag: 173dbae2b06SPaolo Abeni nc->offset = offset; 174dbae2b06SPaolo Abeni return nc->va + offset; 175dbae2b06SPaolo Abeni } 176dbae2b06SPaolo Abeni #else 177dbae2b06SPaolo Abeni 178dbae2b06SPaolo Abeni /* the small page is actually unused in this build; add dummy helpers 179dbae2b06SPaolo Abeni * to please the compiler and avoid later preprocessor's conditionals 180dbae2b06SPaolo Abeni */ 181dbae2b06SPaolo Abeni #define NAPI_HAS_SMALL_PAGE_FRAG 0 182dbae2b06SPaolo Abeni #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 183dbae2b06SPaolo Abeni 184dbae2b06SPaolo Abeni struct page_frag_1k { 185dbae2b06SPaolo Abeni }; 186dbae2b06SPaolo Abeni 187dbae2b06SPaolo Abeni static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 188dbae2b06SPaolo Abeni { 189dbae2b06SPaolo Abeni return NULL; 190dbae2b06SPaolo Abeni } 191dbae2b06SPaolo Abeni 192dbae2b06SPaolo Abeni #endif 193dbae2b06SPaolo Abeni 19450fad4b5SAlexander Lobakin struct napi_alloc_cache { 19550fad4b5SAlexander Lobakin struct page_frag_cache page; 196dbae2b06SPaolo Abeni struct page_frag_1k page_small; 19750fad4b5SAlexander Lobakin unsigned int skb_count; 19850fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 19950fad4b5SAlexander Lobakin }; 20050fad4b5SAlexander Lobakin 20150fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 20250fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 20350fad4b5SAlexander Lobakin 204dbae2b06SPaolo Abeni /* Double check that napi_get_frags() allocates skbs with 205dbae2b06SPaolo Abeni * skb->head being backed by slab, not a page fragment. 206dbae2b06SPaolo Abeni * This is to make sure bug fixed in 3226b158e67c 207dbae2b06SPaolo Abeni * ("net: avoid 32 x truesize under-estimation for tiny skbs") 208dbae2b06SPaolo Abeni * does not accidentally come back. 209dbae2b06SPaolo Abeni */ 210dbae2b06SPaolo Abeni void napi_get_frags_check(struct napi_struct *napi) 211dbae2b06SPaolo Abeni { 212dbae2b06SPaolo Abeni struct sk_buff *skb; 213dbae2b06SPaolo Abeni 214dbae2b06SPaolo Abeni local_bh_disable(); 215dbae2b06SPaolo Abeni skb = napi_get_frags(napi); 216dbae2b06SPaolo Abeni WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 217dbae2b06SPaolo Abeni napi_free_frags(napi); 218dbae2b06SPaolo Abeni local_bh_enable(); 219dbae2b06SPaolo Abeni } 220dbae2b06SPaolo Abeni 22132e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 22250fad4b5SAlexander Lobakin { 22350fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 22450fad4b5SAlexander Lobakin 22550fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 22650fad4b5SAlexander Lobakin 22732e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 22850fad4b5SAlexander Lobakin } 22950fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 23050fad4b5SAlexander Lobakin 23150fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 23250fad4b5SAlexander Lobakin { 23350fad4b5SAlexander Lobakin void *data; 23450fad4b5SAlexander Lobakin 23550fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 236afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 23732e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 23832e3573fSYajun Deng 23950fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 24050fad4b5SAlexander Lobakin } else { 24132e3573fSYajun Deng struct napi_alloc_cache *nc; 24232e3573fSYajun Deng 24350fad4b5SAlexander Lobakin local_bh_disable(); 24432e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 24532e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 24650fad4b5SAlexander Lobakin local_bh_enable(); 24750fad4b5SAlexander Lobakin } 24850fad4b5SAlexander Lobakin return data; 24950fad4b5SAlexander Lobakin } 25050fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 25150fad4b5SAlexander Lobakin 252f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 253f450d539SAlexander Lobakin { 254f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 255f450d539SAlexander Lobakin struct sk_buff *skb; 256f450d539SAlexander Lobakin 25749ae83fcSSieng Piaw Liew if (unlikely(!nc->skb_count)) { 258f450d539SAlexander Lobakin nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 259f450d539SAlexander Lobakin GFP_ATOMIC, 260f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 261f450d539SAlexander Lobakin nc->skb_cache); 262f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 263f450d539SAlexander Lobakin return NULL; 26449ae83fcSSieng Piaw Liew } 265f450d539SAlexander Lobakin 266f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 267f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, skb); 268f450d539SAlexander Lobakin 269f450d539SAlexander Lobakin return skb; 270f450d539SAlexander Lobakin } 271f450d539SAlexander Lobakin 272ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */ 273483126b3SAlexander Lobakin static void __build_skb_around(struct sk_buff *skb, void *data, 274483126b3SAlexander Lobakin unsigned int frag_size) 275ba0509b6SJesper Dangaard Brouer { 276ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 277ba0509b6SJesper Dangaard Brouer unsigned int size = frag_size ? : ksize(data); 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 2991da177e4SLinus Torvalds /** 3002ea2f62cSEric Dumazet * __build_skb - build a network buffer 301b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 3022ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 303b2b5ce9dSEric Dumazet * 304b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 305deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 3062ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 3072ea2f62cSEric Dumazet * or vmalloc() 308b2b5ce9dSEric Dumazet * The return is the new skb buffer. 309b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 310b2b5ce9dSEric Dumazet * Notes : 311b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 312b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 313b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 314b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 315b2b5ce9dSEric Dumazet * before giving packet to stack. 316b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 317b2b5ce9dSEric Dumazet */ 3182ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 319b2b5ce9dSEric Dumazet { 320b2b5ce9dSEric Dumazet struct sk_buff *skb; 321b2b5ce9dSEric Dumazet 322b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 323ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 324b2b5ce9dSEric Dumazet return NULL; 325b2b5ce9dSEric Dumazet 326b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 327483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 328b2b5ce9dSEric Dumazet 329483126b3SAlexander Lobakin return skb; 330b2b5ce9dSEric Dumazet } 3312ea2f62cSEric Dumazet 3322ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3332ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3342ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3352ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3362ea2f62cSEric Dumazet */ 3372ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3382ea2f62cSEric Dumazet { 3392ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3402ea2f62cSEric Dumazet 3412ea2f62cSEric Dumazet if (skb && frag_size) { 3422ea2f62cSEric Dumazet skb->head_frag = 1; 3432f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3442ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3452ea2f62cSEric Dumazet } 3462ea2f62cSEric Dumazet return skb; 3472ea2f62cSEric Dumazet } 348b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 349b2b5ce9dSEric Dumazet 350ba0509b6SJesper Dangaard Brouer /** 351ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 352ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 353ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 354ba0509b6SJesper Dangaard Brouer * @frag_size: size of data, or 0 if head was kmalloced 355ba0509b6SJesper Dangaard Brouer */ 356ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 357ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 358ba0509b6SJesper Dangaard Brouer { 359ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 360ba0509b6SJesper Dangaard Brouer return NULL; 361ba0509b6SJesper Dangaard Brouer 362483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 363ba0509b6SJesper Dangaard Brouer 364483126b3SAlexander Lobakin if (frag_size) { 365ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 366ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 367ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 368ba0509b6SJesper Dangaard Brouer } 369ba0509b6SJesper Dangaard Brouer return skb; 370ba0509b6SJesper Dangaard Brouer } 371ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 372ba0509b6SJesper Dangaard Brouer 373f450d539SAlexander Lobakin /** 374f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 375f450d539SAlexander Lobakin * @data: data buffer provided by caller 376f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 377f450d539SAlexander Lobakin * 378f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 379f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 380f450d539SAlexander Lobakin * 381f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 382f450d539SAlexander Lobakin */ 383f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 384f450d539SAlexander Lobakin { 385f450d539SAlexander Lobakin struct sk_buff *skb; 386f450d539SAlexander Lobakin 387f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 388f450d539SAlexander Lobakin if (unlikely(!skb)) 389f450d539SAlexander Lobakin return NULL; 390f450d539SAlexander Lobakin 391f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 392f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 393f450d539SAlexander Lobakin 394f450d539SAlexander Lobakin return skb; 395f450d539SAlexander Lobakin } 396f450d539SAlexander Lobakin 397f450d539SAlexander Lobakin /** 398f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 399f450d539SAlexander Lobakin * @data: data buffer provided by caller 400f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 401f450d539SAlexander Lobakin * 402f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 403f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 404f450d539SAlexander Lobakin * 405f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 406f450d539SAlexander Lobakin */ 407f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 408f450d539SAlexander Lobakin { 409f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 410f450d539SAlexander Lobakin 411f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 412f450d539SAlexander Lobakin skb->head_frag = 1; 413f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 414f450d539SAlexander Lobakin } 415f450d539SAlexander Lobakin 416f450d539SAlexander Lobakin return skb; 417f450d539SAlexander Lobakin } 418f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 419f450d539SAlexander Lobakin 4205381b23dSAlexander Lobakin /* 4215381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 4225381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 4235381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 4245381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 4255381b23dSAlexander Lobakin * memory is free 4265381b23dSAlexander Lobakin */ 427ef28095fSAlexander Lobakin static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 428ef28095fSAlexander Lobakin bool *pfmemalloc) 4295381b23dSAlexander Lobakin { 4305381b23dSAlexander Lobakin void *obj; 4315381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 4325381b23dSAlexander Lobakin 4335381b23dSAlexander Lobakin /* 4345381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 4355381b23dSAlexander Lobakin * to the reserves, fail. 4365381b23dSAlexander Lobakin */ 4375381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, 4385381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 4395381b23dSAlexander Lobakin node); 4405381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 4415381b23dSAlexander Lobakin goto out; 4425381b23dSAlexander Lobakin 4435381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 4445381b23dSAlexander Lobakin ret_pfmemalloc = true; 4455381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, flags, node); 4465381b23dSAlexander Lobakin 4475381b23dSAlexander Lobakin out: 4485381b23dSAlexander Lobakin if (pfmemalloc) 4495381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 4505381b23dSAlexander Lobakin 4515381b23dSAlexander Lobakin return obj; 4525381b23dSAlexander Lobakin } 4535381b23dSAlexander Lobakin 4545381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 4555381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 4565381b23dSAlexander Lobakin * [BEEP] leaks. 4575381b23dSAlexander Lobakin * 4585381b23dSAlexander Lobakin */ 4595381b23dSAlexander Lobakin 4605381b23dSAlexander Lobakin /** 4615381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 4625381b23dSAlexander Lobakin * @size: size to allocate 4635381b23dSAlexander Lobakin * @gfp_mask: allocation mask 4645381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 4655381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 4665381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 4675381b23dSAlexander Lobakin * allocations in case the data is required for writeback 4685381b23dSAlexander Lobakin * @node: numa node to allocate memory on 4695381b23dSAlexander Lobakin * 4705381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 4715381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 4725381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 4735381b23dSAlexander Lobakin * 4745381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 4755381b23dSAlexander Lobakin * %GFP_ATOMIC. 4765381b23dSAlexander Lobakin */ 4775381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 4785381b23dSAlexander Lobakin int flags, int node) 4795381b23dSAlexander Lobakin { 4805381b23dSAlexander Lobakin struct kmem_cache *cache; 4815381b23dSAlexander Lobakin struct sk_buff *skb; 482a5df6333SLi RongQing unsigned int osize; 4835381b23dSAlexander Lobakin bool pfmemalloc; 484a5df6333SLi RongQing u8 *data; 4855381b23dSAlexander Lobakin 4865381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 4875381b23dSAlexander Lobakin ? skbuff_fclone_cache : skbuff_head_cache; 4885381b23dSAlexander Lobakin 4895381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 4905381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 4915381b23dSAlexander Lobakin 4925381b23dSAlexander Lobakin /* Get the HEAD */ 493d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 494d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 495d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 496d13612b5SAlexander Lobakin else 497d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 498df1ae022SAlexander Lobakin if (unlikely(!skb)) 499df1ae022SAlexander Lobakin return NULL; 5005381b23dSAlexander Lobakin prefetchw(skb); 5015381b23dSAlexander Lobakin 5025381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 5035381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 5045381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 5055381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 5065381b23dSAlexander Lobakin */ 5075381b23dSAlexander Lobakin size = SKB_DATA_ALIGN(size); 5085381b23dSAlexander Lobakin size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5095381b23dSAlexander Lobakin data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 510df1ae022SAlexander Lobakin if (unlikely(!data)) 5115381b23dSAlexander Lobakin goto nodata; 5125381b23dSAlexander Lobakin /* kmalloc(size) might give us more room than requested. 5135381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 5145381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 5155381b23dSAlexander Lobakin */ 516a5df6333SLi RongQing osize = ksize(data); 517a5df6333SLi RongQing size = SKB_WITH_OVERHEAD(osize); 5185381b23dSAlexander Lobakin prefetchw(data + size); 5195381b23dSAlexander Lobakin 5205381b23dSAlexander Lobakin /* 5215381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 5225381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 5235381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 5245381b23dSAlexander Lobakin */ 5255381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 526a5df6333SLi RongQing __build_skb_around(skb, data, osize); 5275381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 5285381b23dSAlexander Lobakin 5295381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 5305381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 5315381b23dSAlexander Lobakin 5325381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 5335381b23dSAlexander Lobakin 5345381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 5355381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 5365381b23dSAlexander Lobakin } 5375381b23dSAlexander Lobakin 5385381b23dSAlexander Lobakin return skb; 539df1ae022SAlexander Lobakin 5405381b23dSAlexander Lobakin nodata: 5415381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 542df1ae022SAlexander Lobakin return NULL; 5435381b23dSAlexander Lobakin } 5445381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 5455381b23dSAlexander Lobakin 5467ba7aeabSSebastian Andrzej Siewior /** 547fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 548fd11a83dSAlexander Duyck * @dev: network device to receive on 549d7499160SMasanari Iida * @len: length to allocate 550fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 551fd11a83dSAlexander Duyck * 552fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 553fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 554fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 555fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 556fd11a83dSAlexander Duyck * 557fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 558fd11a83dSAlexander Duyck */ 5599451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 5609451980aSAlexander Duyck gfp_t gfp_mask) 561fd11a83dSAlexander Duyck { 562b63ae8caSAlexander Duyck struct page_frag_cache *nc; 563fd11a83dSAlexander Duyck struct sk_buff *skb; 5649451980aSAlexander Duyck bool pfmemalloc; 5659451980aSAlexander Duyck void *data; 566fd11a83dSAlexander Duyck 5679451980aSAlexander Duyck len += NET_SKB_PAD; 568fd11a83dSAlexander Duyck 56966c55602SAlexander Lobakin /* If requested length is either too small or too big, 57066c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 57166c55602SAlexander Lobakin */ 57266c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 57366c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 574d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 575a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 576a080e7bdSAlexander Duyck if (!skb) 577a080e7bdSAlexander Duyck goto skb_fail; 578a080e7bdSAlexander Duyck goto skb_success; 579a080e7bdSAlexander Duyck } 5809451980aSAlexander Duyck 5819451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5829451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5839451980aSAlexander Duyck 5849451980aSAlexander Duyck if (sk_memalloc_socks()) 5859451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5869451980aSAlexander Duyck 587afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 5889451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 5898c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 5909451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 59192dcabd7SSebastian Andrzej Siewior } else { 59292dcabd7SSebastian Andrzej Siewior local_bh_disable(); 59392dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 59492dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 59592dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 59692dcabd7SSebastian Andrzej Siewior local_bh_enable(); 59792dcabd7SSebastian Andrzej Siewior } 5989451980aSAlexander Duyck 5999451980aSAlexander Duyck if (unlikely(!data)) 6009451980aSAlexander Duyck return NULL; 6019451980aSAlexander Duyck 6029451980aSAlexander Duyck skb = __build_skb(data, len); 6039451980aSAlexander Duyck if (unlikely(!skb)) { 604181edb2bSAlexander Duyck skb_free_frag(data); 6059451980aSAlexander Duyck return NULL; 6069451980aSAlexander Duyck } 6079451980aSAlexander Duyck 6089451980aSAlexander Duyck if (pfmemalloc) 6099451980aSAlexander Duyck skb->pfmemalloc = 1; 6109451980aSAlexander Duyck skb->head_frag = 1; 6119451980aSAlexander Duyck 612a080e7bdSAlexander Duyck skb_success: 6138af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 6147b2e497aSChristoph Hellwig skb->dev = dev; 615fd11a83dSAlexander Duyck 616a080e7bdSAlexander Duyck skb_fail: 6178af27456SChristoph Hellwig return skb; 6188af27456SChristoph Hellwig } 619b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 6201da177e4SLinus Torvalds 621fd11a83dSAlexander Duyck /** 622fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 623fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 624d7499160SMasanari Iida * @len: length to allocate 625fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 626fd11a83dSAlexander Duyck * 627fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 628fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 629fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 630fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 631fd11a83dSAlexander Duyck * 632fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 633fd11a83dSAlexander Duyck */ 6349451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 6359451980aSAlexander Duyck gfp_t gfp_mask) 636fd11a83dSAlexander Duyck { 6373226b158SEric Dumazet struct napi_alloc_cache *nc; 638fd11a83dSAlexander Duyck struct sk_buff *skb; 639dbae2b06SPaolo Abeni bool pfmemalloc; 6409451980aSAlexander Duyck void *data; 641fd11a83dSAlexander Duyck 642ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 6439451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 644fd11a83dSAlexander Duyck 6453226b158SEric Dumazet /* If requested length is either too small or too big, 6463226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 647dbae2b06SPaolo Abeni * When the small frag allocator is available, prefer it over kmalloc 648dbae2b06SPaolo Abeni * for small fragments 6493226b158SEric Dumazet */ 650dbae2b06SPaolo Abeni if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 6513226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 652d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 653cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 654cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 655a080e7bdSAlexander Duyck if (!skb) 656a080e7bdSAlexander Duyck goto skb_fail; 657a080e7bdSAlexander Duyck goto skb_success; 658a080e7bdSAlexander Duyck } 6599451980aSAlexander Duyck 6603226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 6619451980aSAlexander Duyck 6629451980aSAlexander Duyck if (sk_memalloc_socks()) 6639451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 6649451980aSAlexander Duyck 665dbae2b06SPaolo Abeni if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 666dbae2b06SPaolo Abeni /* we are artificially inflating the allocation size, but 667dbae2b06SPaolo Abeni * that is not as bad as it may look like, as: 668dbae2b06SPaolo Abeni * - 'len' less than GRO_MAX_HEAD makes little sense 669dbae2b06SPaolo Abeni * - On most systems, larger 'len' values lead to fragment 670dbae2b06SPaolo Abeni * size above 512 bytes 671dbae2b06SPaolo Abeni * - kmalloc would use the kmalloc-1k slab for such values 672dbae2b06SPaolo Abeni * - Builds with smaller GRO_MAX_HEAD will very likely do 673dbae2b06SPaolo Abeni * little networking, as that implies no WiFi and no 674dbae2b06SPaolo Abeni * tunnels support, and 32 bits arches. 675dbae2b06SPaolo Abeni */ 676dbae2b06SPaolo Abeni len = SZ_1K; 677dbae2b06SPaolo Abeni 678dbae2b06SPaolo Abeni data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 679dbae2b06SPaolo Abeni pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 680dbae2b06SPaolo Abeni } else { 681dbae2b06SPaolo Abeni len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 682dbae2b06SPaolo Abeni len = SKB_DATA_ALIGN(len); 683dbae2b06SPaolo Abeni 6848c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 685dbae2b06SPaolo Abeni pfmemalloc = nc->page.pfmemalloc; 686dbae2b06SPaolo Abeni } 687dbae2b06SPaolo Abeni 6889451980aSAlexander Duyck if (unlikely(!data)) 6899451980aSAlexander Duyck return NULL; 6909451980aSAlexander Duyck 691cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 6929451980aSAlexander Duyck if (unlikely(!skb)) { 693181edb2bSAlexander Duyck skb_free_frag(data); 6949451980aSAlexander Duyck return NULL; 6959451980aSAlexander Duyck } 6969451980aSAlexander Duyck 697dbae2b06SPaolo Abeni if (pfmemalloc) 6989451980aSAlexander Duyck skb->pfmemalloc = 1; 6999451980aSAlexander Duyck skb->head_frag = 1; 7009451980aSAlexander Duyck 701a080e7bdSAlexander Duyck skb_success: 702fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 703fd11a83dSAlexander Duyck skb->dev = napi->dev; 704fd11a83dSAlexander Duyck 705a080e7bdSAlexander Duyck skb_fail: 706fd11a83dSAlexander Duyck return skb; 707fd11a83dSAlexander Duyck } 708fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 709fd11a83dSAlexander Duyck 710654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 71150269e19SEric Dumazet int size, unsigned int truesize) 712654bed16SPeter Zijlstra { 713654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 714654bed16SPeter Zijlstra skb->len += size; 715654bed16SPeter Zijlstra skb->data_len += size; 71650269e19SEric Dumazet skb->truesize += truesize; 717654bed16SPeter Zijlstra } 718654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 719654bed16SPeter Zijlstra 720f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 721f8e617e1SJason Wang unsigned int truesize) 722f8e617e1SJason Wang { 723f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 724f8e617e1SJason Wang 725f8e617e1SJason Wang skb_frag_size_add(frag, size); 726f8e617e1SJason Wang skb->len += size; 727f8e617e1SJason Wang skb->data_len += size; 728f8e617e1SJason Wang skb->truesize += truesize; 729f8e617e1SJason Wang } 730f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 731f8e617e1SJason Wang 73227b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 7331da177e4SLinus Torvalds { 734bd8a7036SEric Dumazet kfree_skb_list(*listp); 73527b437c8SHerbert Xu *listp = NULL; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 73827b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 73927b437c8SHerbert Xu { 74027b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 74127b437c8SHerbert Xu } 74227b437c8SHerbert Xu 7431da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds struct sk_buff *list; 7461da177e4SLinus Torvalds 747fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 7481da177e4SLinus Torvalds skb_get(list); 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 751*4727bab4SYunsheng Lin static bool skb_pp_recycle(struct sk_buff *skb, void *data) 752*4727bab4SYunsheng Lin { 753*4727bab4SYunsheng Lin if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 754*4727bab4SYunsheng Lin return false; 755*4727bab4SYunsheng Lin return page_pool_return_skb_page(virt_to_page(data)); 756*4727bab4SYunsheng Lin } 757*4727bab4SYunsheng Lin 758d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 759d3836f21SEric Dumazet { 760181edb2bSAlexander Duyck unsigned char *head = skb->head; 761181edb2bSAlexander Duyck 7626a5bcd84SIlias Apalodimas if (skb->head_frag) { 7636a5bcd84SIlias Apalodimas if (skb_pp_recycle(skb, head)) 7646a5bcd84SIlias Apalodimas return; 765181edb2bSAlexander Duyck skb_free_frag(head); 7666a5bcd84SIlias Apalodimas } else { 767181edb2bSAlexander Duyck kfree(head); 768d3836f21SEric Dumazet } 7696a5bcd84SIlias Apalodimas } 770d3836f21SEric Dumazet 7715bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 7721da177e4SLinus Torvalds { 773ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 7741da177e4SLinus Torvalds int i; 775ff04a771SEric Dumazet 776ff04a771SEric Dumazet if (skb->cloned && 777ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 778ff04a771SEric Dumazet &shinfo->dataref)) 7792cc3aeb5SIlias Apalodimas goto exit; 780ff04a771SEric Dumazet 781753f1ca4SPavel Begunkov if (skb_zcopy(skb)) { 782753f1ca4SPavel Begunkov bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 783753f1ca4SPavel Begunkov 78470c43167SJonathan Lemon skb_zcopy_clear(skb, true); 785753f1ca4SPavel Begunkov if (skip_unref) 786753f1ca4SPavel Begunkov goto free_head; 787753f1ca4SPavel Begunkov } 78870c43167SJonathan Lemon 789ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 7906a5bcd84SIlias Apalodimas __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 7911da177e4SLinus Torvalds 792753f1ca4SPavel Begunkov free_head: 793ff04a771SEric Dumazet if (shinfo->frag_list) 794ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 7951da177e4SLinus Torvalds 796d3836f21SEric Dumazet skb_free_head(skb); 7972cc3aeb5SIlias Apalodimas exit: 7982cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 7992cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 8002cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 8012cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 8022cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 80358e61e41STom Rix * additional references to the fragmented part of the SKB. 8042cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 8052cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 8062cc3aeb5SIlias Apalodimas */ 8072cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /* 8111da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 8121da177e4SLinus Torvalds */ 8132d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 8141da177e4SLinus Torvalds { 815d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 816d179cd12SDavid S. Miller 817d179cd12SDavid S. Miller switch (skb->fclone) { 818d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 8191da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 8206ffe75ebSEric Dumazet return; 821d179cd12SDavid S. Miller 822d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 823d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 8246ffe75ebSEric Dumazet 8256ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 8266ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 8276ffe75ebSEric Dumazet * while here, branch prediction will be good. 8286ffe75ebSEric Dumazet */ 8292638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 8306ffe75ebSEric Dumazet goto fastpath; 831d179cd12SDavid S. Miller break; 832d179cd12SDavid S. Miller 8336ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 834d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 835d179cd12SDavid S. Miller break; 8363ff50b79SStephen Hemminger } 8372638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 8386ffe75ebSEric Dumazet return; 8396ffe75ebSEric Dumazet fastpath: 8406ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8430a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 8441da177e4SLinus Torvalds { 845adf30907SEric Dumazet skb_dst_drop(skb); 8461da177e4SLinus Torvalds if (skb->destructor) { 8477890e2f0SEric Dumazet DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 8481da177e4SLinus Torvalds skb->destructor(skb); 8491da177e4SLinus Torvalds } 850a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 851cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 8522fc72c7bSKOVACS Krisztian #endif 853df5042f4SFlorian Westphal skb_ext_put(skb); 85404a4bb55SLennert Buytenhek } 85504a4bb55SLennert Buytenhek 85604a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 85704a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 85804a4bb55SLennert Buytenhek { 85904a4bb55SLennert Buytenhek skb_release_head_state(skb); 860a28b1b90SFlorian Westphal if (likely(skb->head)) 8612d4baff8SHerbert Xu skb_release_data(skb); 8622d4baff8SHerbert Xu } 8631da177e4SLinus Torvalds 8642d4baff8SHerbert Xu /** 8652d4baff8SHerbert Xu * __kfree_skb - private function 8662d4baff8SHerbert Xu * @skb: buffer 8672d4baff8SHerbert Xu * 8682d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 8692d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 8702d4baff8SHerbert Xu * always call kfree_skb 8712d4baff8SHerbert Xu */ 8722d4baff8SHerbert Xu 8732d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 8742d4baff8SHerbert Xu { 8752d4baff8SHerbert Xu skb_release_all(skb); 8761da177e4SLinus Torvalds kfree_skbmem(skb); 8771da177e4SLinus Torvalds } 878b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds /** 881c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 882231d06aeSJörn Engel * @skb: buffer to free 883c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 884231d06aeSJörn Engel * 885231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 886c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 887c504e5c2SMenglong Dong * tracepoint. 888231d06aeSJörn Engel */ 889c205cc75SMenglong Dong void __fix_address 890c205cc75SMenglong Dong kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 891231d06aeSJörn Engel { 892c205cc75SMenglong Dong if (unlikely(!skb_unref(skb))) 893231d06aeSJörn Engel return; 8943889a803SPaolo Abeni 89520bbcd0aSMenglong Dong DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX); 89620bbcd0aSMenglong Dong 897c504e5c2SMenglong Dong trace_kfree_skb(skb, __builtin_return_address(0), reason); 898231d06aeSJörn Engel __kfree_skb(skb); 899231d06aeSJörn Engel } 900c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 901231d06aeSJörn Engel 902215b0f19SMenglong Dong void kfree_skb_list_reason(struct sk_buff *segs, 903215b0f19SMenglong Dong enum skb_drop_reason reason) 904bd8a7036SEric Dumazet { 905bd8a7036SEric Dumazet while (segs) { 906bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 907bd8a7036SEric Dumazet 908215b0f19SMenglong Dong kfree_skb_reason(segs, reason); 909bd8a7036SEric Dumazet segs = next; 910bd8a7036SEric Dumazet } 911bd8a7036SEric Dumazet } 912215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 913bd8a7036SEric Dumazet 9146413139dSWillem de Bruijn /* Dump skb information and contents. 9156413139dSWillem de Bruijn * 9166413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 9176413139dSWillem de Bruijn * 918302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 9196413139dSWillem de Bruijn */ 9206413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 9216413139dSWillem de Bruijn { 9226413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 9236413139dSWillem de Bruijn struct net_device *dev = skb->dev; 9246413139dSWillem de Bruijn struct sock *sk = skb->sk; 9256413139dSWillem de Bruijn struct sk_buff *list_skb; 9266413139dSWillem de Bruijn bool has_mac, has_trans; 9276413139dSWillem de Bruijn int headroom, tailroom; 9286413139dSWillem de Bruijn int i, len, seg_len; 9296413139dSWillem de Bruijn 9306413139dSWillem de Bruijn if (full_pkt) 9316413139dSWillem de Bruijn len = skb->len; 9326413139dSWillem de Bruijn else 9336413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 9346413139dSWillem de Bruijn 9356413139dSWillem de Bruijn headroom = skb_headroom(skb); 9366413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 9376413139dSWillem de Bruijn 9386413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 9396413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 9406413139dSWillem de Bruijn 9416413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 9426413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 9436413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 9446413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 9456413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 9466413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 9476413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 9486413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 9496413139dSWillem de Bruijn skb->network_header, 9506413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 9516413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 9526413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 9536413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 9546413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 9556413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 9566413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 9576413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 9586413139dSWillem de Bruijn 9596413139dSWillem de Bruijn if (dev) 9608a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 9616413139dSWillem de Bruijn level, dev->name, &dev->features); 9626413139dSWillem de Bruijn if (sk) 963db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 9646413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 9656413139dSWillem de Bruijn 9666413139dSWillem de Bruijn if (full_pkt && headroom) 9676413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 9686413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 9696413139dSWillem de Bruijn 9706413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 9716413139dSWillem de Bruijn if (seg_len) 9726413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 9736413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 9746413139dSWillem de Bruijn len -= seg_len; 9756413139dSWillem de Bruijn 9766413139dSWillem de Bruijn if (full_pkt && tailroom) 9776413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 9786413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 9796413139dSWillem de Bruijn 9806413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 9816413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 9826413139dSWillem de Bruijn u32 p_off, p_len, copied; 9836413139dSWillem de Bruijn struct page *p; 9846413139dSWillem de Bruijn u8 *vaddr; 9856413139dSWillem de Bruijn 986b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 9876413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 9886413139dSWillem de Bruijn copied) { 9896413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 9906413139dSWillem de Bruijn vaddr = kmap_atomic(p); 9916413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 9926413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 9936413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 9946413139dSWillem de Bruijn kunmap_atomic(vaddr); 9956413139dSWillem de Bruijn len -= seg_len; 9966413139dSWillem de Bruijn if (!len) 9976413139dSWillem de Bruijn break; 9986413139dSWillem de Bruijn } 9996413139dSWillem de Bruijn } 10006413139dSWillem de Bruijn 10016413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 10026413139dSWillem de Bruijn printk("skb fraglist:\n"); 10036413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 10046413139dSWillem de Bruijn skb_dump(level, list_skb, true); 10056413139dSWillem de Bruijn } 10066413139dSWillem de Bruijn } 10076413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 10086413139dSWillem de Bruijn 1009d1a203eaSStephen Hemminger /** 101025121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 101125121173SMichael S. Tsirkin * @skb: buffer that triggered an error 101225121173SMichael S. Tsirkin * 101325121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 101425121173SMichael S. Tsirkin * skb must be freed afterwards. 101525121173SMichael S. Tsirkin */ 101625121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 101725121173SMichael S. Tsirkin { 1018753f1ca4SPavel Begunkov if (skb) { 1019753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 10201f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 102125121173SMichael S. Tsirkin } 1022753f1ca4SPavel Begunkov } 102325121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 102425121173SMichael S. Tsirkin 1025be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 102625121173SMichael S. Tsirkin /** 1027ead2ceb0SNeil Horman * consume_skb - free an skbuff 1028ead2ceb0SNeil Horman * @skb: buffer to free 1029ead2ceb0SNeil Horman * 1030ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 1031ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1032ead2ceb0SNeil Horman * is being dropped after a failure and notes that 1033ead2ceb0SNeil Horman */ 1034ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 1035ead2ceb0SNeil Horman { 10363889a803SPaolo Abeni if (!skb_unref(skb)) 1037ead2ceb0SNeil Horman return; 10383889a803SPaolo Abeni 103907dc22e7SKoki Sanagi trace_consume_skb(skb); 1040ead2ceb0SNeil Horman __kfree_skb(skb); 1041ead2ceb0SNeil Horman } 1042ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 1043be769db2SHerbert Xu #endif 1044ead2ceb0SNeil Horman 10450a463c78SPaolo Abeni /** 1046c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 10470a463c78SPaolo Abeni * @skb: buffer to free 10480a463c78SPaolo Abeni * 1049ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 1050ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 10510a463c78SPaolo Abeni */ 1052ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 10530a463c78SPaolo Abeni { 10540a463c78SPaolo Abeni trace_consume_skb(skb); 10550a463c78SPaolo Abeni skb_release_data(skb); 10560a463c78SPaolo Abeni kfree_skbmem(skb); 10570a463c78SPaolo Abeni } 10580a463c78SPaolo Abeni 1059f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 1060795bb1c0SJesper Dangaard Brouer { 1061795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1062f450d539SAlexander Lobakin u32 i; 1063795bb1c0SJesper Dangaard Brouer 1064f450d539SAlexander Lobakin kasan_poison_object_data(skbuff_head_cache, skb); 1065795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 1066795bb1c0SJesper Dangaard Brouer 1067795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1068f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1069f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, 1070f450d539SAlexander Lobakin nc->skb_cache[i]); 1071f450d539SAlexander Lobakin 1072f450d539SAlexander Lobakin kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 1073f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 1074f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 1075795bb1c0SJesper Dangaard Brouer } 1076795bb1c0SJesper Dangaard Brouer } 1077f450d539SAlexander Lobakin 107815fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 107915fad714SJesper Dangaard Brouer { 10809243adfcSAlexander Lobakin skb_release_all(skb); 10819243adfcSAlexander Lobakin napi_skb_cache_put(skb); 10829243adfcSAlexander Lobakin } 10839243adfcSAlexander Lobakin 10849243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 10859243adfcSAlexander Lobakin { 10869efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 10878550ff8dSPaul Blakey nf_reset_ct(skb); 10889243adfcSAlexander Lobakin skb_dst_drop(skb); 10899243adfcSAlexander Lobakin skb_ext_put(skb); 10905e10da53SPaolo Abeni skb_orphan(skb); 10919efb4b5bSPaolo Abeni skb->slow_gro = 0; 10929efb4b5bSPaolo Abeni } 1093f450d539SAlexander Lobakin napi_skb_cache_put(skb); 109415fad714SJesper Dangaard Brouer } 1095795bb1c0SJesper Dangaard Brouer 1096795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 1097795bb1c0SJesper Dangaard Brouer { 1098885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 1099795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 1100885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 1101795bb1c0SJesper Dangaard Brouer return; 1102795bb1c0SJesper Dangaard Brouer } 1103795bb1c0SJesper Dangaard Brouer 1104ee2640dfSEric Dumazet DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 11056454eca8SYunsheng Lin 11067608894eSPaolo Abeni if (!skb_unref(skb)) 1107795bb1c0SJesper Dangaard Brouer return; 11087608894eSPaolo Abeni 1109795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 1110795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 1111795bb1c0SJesper Dangaard Brouer 1112795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 1113abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1114795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 1115795bb1c0SJesper Dangaard Brouer return; 1116795bb1c0SJesper Dangaard Brouer } 1117795bb1c0SJesper Dangaard Brouer 11189243adfcSAlexander Lobakin skb_release_all(skb); 1119f450d539SAlexander Lobakin napi_skb_cache_put(skb); 1120795bb1c0SJesper Dangaard Brouer } 1121795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 1122795bb1c0SJesper Dangaard Brouer 112303f61041SKees Cook /* Make sure a field is contained by headers group */ 1124b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 112503f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 112603f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1127b1937227SEric Dumazet 1128dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1129dec18810SHerbert Xu { 1130dec18810SHerbert Xu new->tstamp = old->tstamp; 1131b1937227SEric Dumazet /* We do not copy old->sk */ 1132dec18810SHerbert Xu new->dev = old->dev; 1133b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 11347fee226aSEric Dumazet skb_dst_copy(new, old); 1135df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1136b1937227SEric Dumazet __nf_copy(new, old, false); 11376aa895b0SPatrick McHardy 113803f61041SKees Cook /* Note : this field could be in the headers group. 1139b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1140b1937227SEric Dumazet */ 1141b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 114206021292SEliezer Tamir 114303f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1144b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1145b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1146b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1147b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1148b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1149b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1150b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1151b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1152b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1153b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1154b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1155b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1156b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1157b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1158b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1159b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1160b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1161b1937227SEric Dumazet #endif 1162e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1163b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 116406021292SEliezer Tamir #endif 116568822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 11662bd82484SEric Dumazet #ifdef CONFIG_XPS 11672bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 11682bd82484SEric Dumazet #endif 1169b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1170b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1171b1937227SEric Dumazet #endif 1172b1937227SEric Dumazet 1173dec18810SHerbert Xu } 1174dec18810SHerbert Xu 117582c49a35SHerbert Xu /* 117682c49a35SHerbert Xu * You should not add any new code to this function. Add it to 117782c49a35SHerbert Xu * __copy_skb_header above instead. 117882c49a35SHerbert Xu */ 1179e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds #define C(x) n->x = skb->x 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds n->next = n->prev = NULL; 11841da177e4SLinus Torvalds n->sk = NULL; 1185dec18810SHerbert Xu __copy_skb_header(n, skb); 1186dec18810SHerbert Xu 11871da177e4SLinus Torvalds C(len); 11881da177e4SLinus Torvalds C(data_len); 11893e6b3b2eSAlexey Dobriyan C(mac_len); 1190334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 119102f1c89dSPaul Moore n->cloned = 1; 11921da177e4SLinus Torvalds n->nohdr = 0; 1193b13dda9fSEric Dumazet n->peeked = 0; 1194e78bfb07SStefano Brivio C(pfmemalloc); 11956a5bcd84SIlias Apalodimas C(pp_recycle); 11961da177e4SLinus Torvalds n->destructor = NULL; 11971da177e4SLinus Torvalds C(tail); 11981da177e4SLinus Torvalds C(end); 119902f1c89dSPaul Moore C(head); 1200d3836f21SEric Dumazet C(head_frag); 120102f1c89dSPaul Moore C(data); 120202f1c89dSPaul Moore C(truesize); 120363354797SReshetova, Elena refcount_set(&n->users, 1); 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 12061da177e4SLinus Torvalds skb->cloned = 1; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds return n; 1209e0053ec0SHerbert Xu #undef C 1210e0053ec0SHerbert Xu } 1211e0053ec0SHerbert Xu 1212e0053ec0SHerbert Xu /** 1213da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1214da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1215da29e4b4SJakub Kicinski */ 1216da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1217da29e4b4SJakub Kicinski { 1218da29e4b4SJakub Kicinski struct sk_buff *n; 1219da29e4b4SJakub Kicinski 1220da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1221da29e4b4SJakub Kicinski if (!n) 1222da29e4b4SJakub Kicinski return NULL; 1223da29e4b4SJakub Kicinski 1224da29e4b4SJakub Kicinski n->len = first->len; 1225da29e4b4SJakub Kicinski n->data_len = first->len; 1226da29e4b4SJakub Kicinski n->truesize = first->truesize; 1227da29e4b4SJakub Kicinski 1228da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1229da29e4b4SJakub Kicinski 1230da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1231da29e4b4SJakub Kicinski n->destructor = NULL; 1232da29e4b4SJakub Kicinski 1233da29e4b4SJakub Kicinski return n; 1234da29e4b4SJakub Kicinski } 1235da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1236da29e4b4SJakub Kicinski 1237da29e4b4SJakub Kicinski /** 1238e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1239e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1240e0053ec0SHerbert Xu * @src: the skb to supply the contents 1241e0053ec0SHerbert Xu * 1242e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1243e0053ec0SHerbert Xu * supplied by the user. 1244e0053ec0SHerbert Xu * 1245e0053ec0SHerbert Xu * The target skb is returned upon exit. 1246e0053ec0SHerbert Xu */ 1247e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1248e0053ec0SHerbert Xu { 12492d4baff8SHerbert Xu skb_release_all(dst); 1250e0053ec0SHerbert Xu return __skb_clone(dst, src); 1251e0053ec0SHerbert Xu } 1252e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1253e0053ec0SHerbert Xu 12546f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1255a91dbff5SWillem de Bruijn { 1256a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1257a91dbff5SWillem de Bruijn struct user_struct *user; 1258a91dbff5SWillem de Bruijn 1259a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1260a91dbff5SWillem de Bruijn return 0; 1261a91dbff5SWillem de Bruijn 1262a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1263a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1264a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1265a91dbff5SWillem de Bruijn 1266a91dbff5SWillem de Bruijn do { 1267a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 1268a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1269a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1270a91dbff5SWillem de Bruijn return -ENOBUFS; 1271a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 1272a91dbff5SWillem de Bruijn old_pg); 1273a91dbff5SWillem de Bruijn 1274a91dbff5SWillem de Bruijn if (!mmp->user) { 1275a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1276a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1277a91dbff5SWillem de Bruijn } else { 1278a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1279a91dbff5SWillem de Bruijn } 1280a91dbff5SWillem de Bruijn 1281a91dbff5SWillem de Bruijn return 0; 1282a91dbff5SWillem de Bruijn } 12836f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1284a91dbff5SWillem de Bruijn 12856f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1286a91dbff5SWillem de Bruijn { 1287a91dbff5SWillem de Bruijn if (mmp->user) { 1288a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1289a91dbff5SWillem de Bruijn free_uid(mmp->user); 1290a91dbff5SWillem de Bruijn } 1291a91dbff5SWillem de Bruijn } 12926f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1293a91dbff5SWillem de Bruijn 1294c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 129552267790SWillem de Bruijn { 1296e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg; 129752267790SWillem de Bruijn struct sk_buff *skb; 129852267790SWillem de Bruijn 129952267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 130052267790SWillem de Bruijn 130152267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 130252267790SWillem de Bruijn if (!skb) 130352267790SWillem de Bruijn return NULL; 130452267790SWillem de Bruijn 130552267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 130652267790SWillem de Bruijn uarg = (void *)skb->cb; 1307a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1308a91dbff5SWillem de Bruijn 1309a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1310a91dbff5SWillem de Bruijn kfree_skb(skb); 1311a91dbff5SWillem de Bruijn return NULL; 1312a91dbff5SWillem de Bruijn } 131352267790SWillem de Bruijn 1314e7d2b510SPavel Begunkov uarg->ubuf.callback = msg_zerocopy_callback; 13154ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 13164ab6c99dSWillem de Bruijn uarg->len = 1; 13174ab6c99dSWillem de Bruijn uarg->bytelen = size; 131852267790SWillem de Bruijn uarg->zerocopy = 1; 1319e7d2b510SPavel Begunkov uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1320e7d2b510SPavel Begunkov refcount_set(&uarg->ubuf.refcnt, 1); 132152267790SWillem de Bruijn sock_hold(sk); 132252267790SWillem de Bruijn 1323e7d2b510SPavel Begunkov return &uarg->ubuf; 132452267790SWillem de Bruijn } 132552267790SWillem de Bruijn 1326e7d2b510SPavel Begunkov static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 132752267790SWillem de Bruijn { 132852267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 132952267790SWillem de Bruijn } 133052267790SWillem de Bruijn 13318c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 13324ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 13334ab6c99dSWillem de Bruijn { 13344ab6c99dSWillem de Bruijn if (uarg) { 1335e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc; 13364ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 13374ab6c99dSWillem de Bruijn u32 bytelen, next; 13384ab6c99dSWillem de Bruijn 13391b4b2b09SPavel Begunkov /* there might be non MSG_ZEROCOPY users */ 13401b4b2b09SPavel Begunkov if (uarg->callback != msg_zerocopy_callback) 13411b4b2b09SPavel Begunkov return NULL; 13421b4b2b09SPavel Begunkov 13434ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 13444ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 13454ab6c99dSWillem de Bruijn */ 13464ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 13474ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 13484ab6c99dSWillem de Bruijn return NULL; 13494ab6c99dSWillem de Bruijn } 13504ab6c99dSWillem de Bruijn 1351e7d2b510SPavel Begunkov uarg_zc = uarg_to_msgzc(uarg); 1352e7d2b510SPavel Begunkov bytelen = uarg_zc->bytelen + size; 1353e7d2b510SPavel Begunkov if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 13544ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 13554ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 13564ab6c99dSWillem de Bruijn goto new_alloc; 13574ab6c99dSWillem de Bruijn return NULL; 13584ab6c99dSWillem de Bruijn } 13594ab6c99dSWillem de Bruijn 13604ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 1361e7d2b510SPavel Begunkov if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1362e7d2b510SPavel Begunkov if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1363a91dbff5SWillem de Bruijn return NULL; 1364e7d2b510SPavel Begunkov uarg_zc->len++; 1365e7d2b510SPavel Begunkov uarg_zc->bytelen = bytelen; 13664ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1367100f6d8eSWillem de Bruijn 1368100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1369100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 13708e044917SJonathan Lemon net_zcopy_get(uarg); 1371100f6d8eSWillem de Bruijn 13724ab6c99dSWillem de Bruijn return uarg; 13734ab6c99dSWillem de Bruijn } 13744ab6c99dSWillem de Bruijn } 13754ab6c99dSWillem de Bruijn 13764ab6c99dSWillem de Bruijn new_alloc: 13778c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 13784ab6c99dSWillem de Bruijn } 13798c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 13804ab6c99dSWillem de Bruijn 13814ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 13824ab6c99dSWillem de Bruijn { 13834ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 13844ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 13854ab6c99dSWillem de Bruijn u64 sum_len; 13864ab6c99dSWillem de Bruijn 13874ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 13884ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 13894ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 13904ab6c99dSWillem de Bruijn 13914ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 13924ab6c99dSWillem de Bruijn return false; 13934ab6c99dSWillem de Bruijn 13944ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 13954ab6c99dSWillem de Bruijn return false; 13964ab6c99dSWillem de Bruijn 13974ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 13984ab6c99dSWillem de Bruijn return true; 13994ab6c99dSWillem de Bruijn } 14004ab6c99dSWillem de Bruijn 1401e7d2b510SPavel Begunkov static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 140252267790SWillem de Bruijn { 14034ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 140452267790SWillem de Bruijn struct sock_exterr_skb *serr; 140552267790SWillem de Bruijn struct sock *sk = skb->sk; 14064ab6c99dSWillem de Bruijn struct sk_buff_head *q; 14074ab6c99dSWillem de Bruijn unsigned long flags; 14083bdd5ee0SWillem de Bruijn bool is_zerocopy; 14094ab6c99dSWillem de Bruijn u32 lo, hi; 14104ab6c99dSWillem de Bruijn u16 len; 141152267790SWillem de Bruijn 1412ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1413ccaffff1SWillem de Bruijn 14144ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 14154ab6c99dSWillem de Bruijn * so do not queue a completion notification 14164ab6c99dSWillem de Bruijn */ 14174ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 141852267790SWillem de Bruijn goto release; 141952267790SWillem de Bruijn 14204ab6c99dSWillem de Bruijn len = uarg->len; 14214ab6c99dSWillem de Bruijn lo = uarg->id; 14224ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 14233bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 14244ab6c99dSWillem de Bruijn 142552267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 142652267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 142752267790SWillem de Bruijn serr->ee.ee_errno = 0; 142852267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 14294ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 14304ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 14313bdd5ee0SWillem de Bruijn if (!is_zerocopy) 143252267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 143352267790SWillem de Bruijn 14344ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 14354ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 14364ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 14374ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 14384ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 14394ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 144052267790SWillem de Bruijn skb = NULL; 14414ab6c99dSWillem de Bruijn } 14424ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 144352267790SWillem de Bruijn 1444e3ae2365SAlexander Aring sk_error_report(sk); 144552267790SWillem de Bruijn 144652267790SWillem de Bruijn release: 144752267790SWillem de Bruijn consume_skb(skb); 144852267790SWillem de Bruijn sock_put(sk); 144952267790SWillem de Bruijn } 145075518851SJonathan Lemon 14518c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 145236177832SJonathan Lemon bool success) 145375518851SJonathan Lemon { 1454e7d2b510SPavel Begunkov struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1455e7d2b510SPavel Begunkov 1456e7d2b510SPavel Begunkov uarg_zc->zerocopy = uarg_zc->zerocopy & success; 145775518851SJonathan Lemon 145875518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 1459e7d2b510SPavel Begunkov __msg_zerocopy_callback(uarg_zc); 146075518851SJonathan Lemon } 14618c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 146252267790SWillem de Bruijn 14638c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 146452267790SWillem de Bruijn { 1465e7d2b510SPavel Begunkov struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 146652267790SWillem de Bruijn 146752267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 1468e7d2b510SPavel Begunkov uarg_to_msgzc(uarg)->len--; 146952267790SWillem de Bruijn 147052900d22SWillem de Bruijn if (have_uref) 14718c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 147252267790SWillem de Bruijn } 14738c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 147452267790SWillem de Bruijn 147552267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 147652267790SWillem de Bruijn struct msghdr *msg, int len, 147752267790SWillem de Bruijn struct ubuf_info *uarg) 147852267790SWillem de Bruijn { 14794ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 148052267790SWillem de Bruijn int err, orig_len = skb->len; 148152267790SWillem de Bruijn 14824ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 14834ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 14844ab6c99dSWillem de Bruijn */ 14854ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 14864ab6c99dSWillem de Bruijn return -EEXIST; 14874ab6c99dSWillem de Bruijn 1488ebe73a28SDavid Ahern err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 148952267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 149054d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 149154d43117SWillem de Bruijn 149252267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1493829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 149454d43117SWillem de Bruijn skb->sk = sk; 149552267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 149654d43117SWillem de Bruijn skb->sk = save_sk; 149752267790SWillem de Bruijn return err; 149852267790SWillem de Bruijn } 149952267790SWillem de Bruijn 150052900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 150152267790SWillem de Bruijn return skb->len - orig_len; 150252267790SWillem de Bruijn } 150352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 150452267790SWillem de Bruijn 1505753f1ca4SPavel Begunkov void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1506753f1ca4SPavel Begunkov { 1507753f1ca4SPavel Begunkov int i; 1508753f1ca4SPavel Begunkov 1509753f1ca4SPavel Begunkov skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1510753f1ca4SPavel Begunkov for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1511753f1ca4SPavel Begunkov skb_frag_ref(skb, i); 1512753f1ca4SPavel Begunkov } 1513753f1ca4SPavel Begunkov EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1514753f1ca4SPavel Begunkov 15151f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 151652267790SWillem de Bruijn gfp_t gfp_mask) 151752267790SWillem de Bruijn { 151852267790SWillem de Bruijn if (skb_zcopy(orig)) { 151952267790SWillem de Bruijn if (skb_zcopy(nskb)) { 152052267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 152152267790SWillem de Bruijn if (!gfp_mask) { 152252267790SWillem de Bruijn WARN_ON_ONCE(1); 152352267790SWillem de Bruijn return -ENOMEM; 152452267790SWillem de Bruijn } 152552267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 152652267790SWillem de Bruijn return 0; 152752267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 152852267790SWillem de Bruijn return -EIO; 152952267790SWillem de Bruijn } 153052900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 153152267790SWillem de Bruijn } 153252267790SWillem de Bruijn return 0; 153352267790SWillem de Bruijn } 153452267790SWillem de Bruijn 15352c53040fSBen Hutchings /** 15362c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 153748c83012SMichael S. Tsirkin * @skb: the skb to modify 153848c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 153948c83012SMichael S. Tsirkin * 154006b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 154148c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 154248c83012SMichael S. Tsirkin * to userspace pages. 154348c83012SMichael S. Tsirkin * 154448c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 154548c83012SMichael S. Tsirkin * %GFP_ATOMIC. 154648c83012SMichael S. Tsirkin * 154748c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 154848c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 154948c83012SMichael S. Tsirkin */ 155048c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1551a6686f2fSShirley Ma { 1552a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1553a6686f2fSShirley Ma struct page *page, *head = NULL; 15543ece7826SWillem de Bruijn int i, new_frags; 15553ece7826SWillem de Bruijn u32 d_off; 1556a6686f2fSShirley Ma 15573ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 15583ece7826SWillem de Bruijn return -EINVAL; 15593ece7826SWillem de Bruijn 1560f72c4ac6SWillem de Bruijn if (!num_frags) 1561f72c4ac6SWillem de Bruijn goto release; 1562f72c4ac6SWillem de Bruijn 15633ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 15643ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 156502756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1566a6686f2fSShirley Ma if (!page) { 1567a6686f2fSShirley Ma while (head) { 156840dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1569a6686f2fSShirley Ma put_page(head); 1570a6686f2fSShirley Ma head = next; 1571a6686f2fSShirley Ma } 1572a6686f2fSShirley Ma return -ENOMEM; 1573a6686f2fSShirley Ma } 15743ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 15753ece7826SWillem de Bruijn head = page; 15763ece7826SWillem de Bruijn } 15773ece7826SWillem de Bruijn 15783ece7826SWillem de Bruijn page = head; 15793ece7826SWillem de Bruijn d_off = 0; 15803ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 15813ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 15823ece7826SWillem de Bruijn u32 p_off, p_len, copied; 15833ece7826SWillem de Bruijn struct page *p; 15843ece7826SWillem de Bruijn u8 *vaddr; 1585c613c209SWillem de Bruijn 1586b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1587c613c209SWillem de Bruijn p, p_off, p_len, copied) { 15883ece7826SWillem de Bruijn u32 copy, done = 0; 1589c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 15903ece7826SWillem de Bruijn 15913ece7826SWillem de Bruijn while (done < p_len) { 15923ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 15933ece7826SWillem de Bruijn d_off = 0; 15943ece7826SWillem de Bruijn page = (struct page *)page_private(page); 15953ece7826SWillem de Bruijn } 15963ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 15973ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 15983ece7826SWillem de Bruijn vaddr + p_off + done, copy); 15993ece7826SWillem de Bruijn done += copy; 16003ece7826SWillem de Bruijn d_off += copy; 16013ece7826SWillem de Bruijn } 160251c56b00SEric Dumazet kunmap_atomic(vaddr); 1603c613c209SWillem de Bruijn } 1604a6686f2fSShirley Ma } 1605a6686f2fSShirley Ma 1606a6686f2fSShirley Ma /* skb frags release userspace buffers */ 160702756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1608a8605c60SIan Campbell skb_frag_unref(skb, i); 1609a6686f2fSShirley Ma 1610a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 16113ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 16123ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 161340dadff2SSunghan Suh head = (struct page *)page_private(head); 1614a6686f2fSShirley Ma } 16153ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 16163ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 161748c83012SMichael S. Tsirkin 1618b90ddd56SWillem de Bruijn release: 16191f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1620a6686f2fSShirley Ma return 0; 1621a6686f2fSShirley Ma } 1622dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1623a6686f2fSShirley Ma 1624e0053ec0SHerbert Xu /** 1625e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1626e0053ec0SHerbert Xu * @skb: buffer to clone 1627e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1628e0053ec0SHerbert Xu * 1629e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1630e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1631e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1632e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1633e0053ec0SHerbert Xu * 1634e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1635e0053ec0SHerbert Xu * %GFP_ATOMIC. 1636e0053ec0SHerbert Xu */ 1637e0053ec0SHerbert Xu 1638e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1639e0053ec0SHerbert Xu { 1640d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1641d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1642d0bf4a9eSEric Dumazet skb1); 16436ffe75ebSEric Dumazet struct sk_buff *n; 1644e0053ec0SHerbert Xu 164570008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1646a6686f2fSShirley Ma return NULL; 1647a6686f2fSShirley Ma 1648e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 16492638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 16506ffe75ebSEric Dumazet n = &fclones->skb2; 16512638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1652c2dd4059SEric Dumazet n->fclone = SKB_FCLONE_CLONE; 1653e0053ec0SHerbert Xu } else { 1654c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1655c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1656c93bdd0eSMel Gorman 1657e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1658e0053ec0SHerbert Xu if (!n) 1659e0053ec0SHerbert Xu return NULL; 1660fe55f6d5SVegard Nossum 1661e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1662e0053ec0SHerbert Xu } 1663e0053ec0SHerbert Xu 1664e0053ec0SHerbert Xu return __skb_clone(n, skb); 16651da177e4SLinus Torvalds } 1666b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 16671da177e4SLinus Torvalds 1668b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1669f5b17294SPravin B Shelar { 1670030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1671030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1672030737bcSEric Dumazet skb->csum_start += off; 1673f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1674f5b17294SPravin B Shelar skb->transport_header += off; 1675f5b17294SPravin B Shelar skb->network_header += off; 1676f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1677f5b17294SPravin B Shelar skb->mac_header += off; 1678f5b17294SPravin B Shelar skb->inner_transport_header += off; 1679f5b17294SPravin B Shelar skb->inner_network_header += off; 1680aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1681f5b17294SPravin B Shelar } 1682b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1683f5b17294SPravin B Shelar 168408303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 16851da177e4SLinus Torvalds { 1686dec18810SHerbert Xu __copy_skb_header(new, old); 1687dec18810SHerbert Xu 16887967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 16897967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 16907967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 16911da177e4SLinus Torvalds } 169208303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 16931da177e4SLinus Torvalds 1694c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1695c93bdd0eSMel Gorman { 1696c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1697c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1698c93bdd0eSMel Gorman return 0; 1699c93bdd0eSMel Gorman } 1700c93bdd0eSMel Gorman 17011da177e4SLinus Torvalds /** 17021da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 17031da177e4SLinus Torvalds * @skb: buffer to copy 17041da177e4SLinus Torvalds * @gfp_mask: allocation priority 17051da177e4SLinus Torvalds * 17061da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 17071da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 17081da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 17091da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 17101da177e4SLinus Torvalds * 17111da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 17121da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 17131da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 17141da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 17151da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 17161da177e4SLinus Torvalds */ 17171da177e4SLinus Torvalds 1718dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 17191da177e4SLinus Torvalds { 17206602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1721ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1722c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1723c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 17246602cebbSEric Dumazet 17251da177e4SLinus Torvalds if (!n) 17261da177e4SLinus Torvalds return NULL; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds /* Set the data pointer */ 17291da177e4SLinus Torvalds skb_reserve(n, headerlen); 17301da177e4SLinus Torvalds /* Set the tail pointer and length */ 17311da177e4SLinus Torvalds skb_put(n, skb->len); 17321da177e4SLinus Torvalds 17339f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 17341da177e4SLinus Torvalds 173508303c18SIlya Lesokhin skb_copy_header(n, skb); 17361da177e4SLinus Torvalds return n; 17371da177e4SLinus Torvalds } 1738b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds /** 1741bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 17421da177e4SLinus Torvalds * @skb: buffer to copy 1743117632e6SEric Dumazet * @headroom: headroom of new skb 17441da177e4SLinus Torvalds * @gfp_mask: allocation priority 1745bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1746bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1747bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 17481da177e4SLinus Torvalds * 17491da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 17501da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 17511da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 17521da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 17531da177e4SLinus Torvalds * or the pointer to the buffer on success. 17541da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 17551da177e4SLinus Torvalds */ 17561da177e4SLinus Torvalds 1757bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1758bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 17591da177e4SLinus Torvalds { 1760117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1761bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1762bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 17636602cebbSEric Dumazet 17641da177e4SLinus Torvalds if (!n) 17651da177e4SLinus Torvalds goto out; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds /* Set the data pointer */ 1768117632e6SEric Dumazet skb_reserve(n, headroom); 17691da177e4SLinus Torvalds /* Set the tail pointer and length */ 17701da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 17711da177e4SLinus Torvalds /* Copy the bytes */ 1772d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 17731da177e4SLinus Torvalds 177425f484a6SHerbert Xu n->truesize += skb->data_len; 17751da177e4SLinus Torvalds n->data_len = skb->data_len; 17761da177e4SLinus Torvalds n->len = skb->len; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 17791da177e4SLinus Torvalds int i; 17801da177e4SLinus Torvalds 17811f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 17821f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 17831511022cSDan Carpenter kfree_skb(n); 17841511022cSDan Carpenter n = NULL; 1785a6686f2fSShirley Ma goto out; 1786a6686f2fSShirley Ma } 17871da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17881da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1789ea2ab693SIan Campbell skb_frag_ref(skb, i); 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 179421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 17951da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 17961da177e4SLinus Torvalds skb_clone_fraglist(n); 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds 179908303c18SIlya Lesokhin skb_copy_header(n, skb); 18001da177e4SLinus Torvalds out: 18011da177e4SLinus Torvalds return n; 18021da177e4SLinus Torvalds } 1803bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds /** 18061da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 18071da177e4SLinus Torvalds * @skb: buffer to reallocate 18081da177e4SLinus Torvalds * @nhead: room to add at head 18091da177e4SLinus Torvalds * @ntail: room to add at tail 18101da177e4SLinus Torvalds * @gfp_mask: allocation priority 18111da177e4SLinus Torvalds * 1812bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1813bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 18141da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 18151da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 18161da177e4SLinus Torvalds * 18171da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 18181da177e4SLinus Torvalds * reloaded after call to this function. 18191da177e4SLinus Torvalds */ 18201da177e4SLinus Torvalds 182186a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1822dd0fc66fSAl Viro gfp_t gfp_mask) 18231da177e4SLinus Torvalds { 1824158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1825158f323bSEric Dumazet int size = osize + nhead + ntail; 18261da177e4SLinus Torvalds long off; 1827158f323bSEric Dumazet u8 *data; 18281da177e4SLinus Torvalds 18294edd87adSHerbert Xu BUG_ON(nhead < 0); 18304edd87adSHerbert Xu 18319f77fad3STim Hansen BUG_ON(skb_shared(skb)); 18321da177e4SLinus Torvalds 1833753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 1834753f1ca4SPavel Begunkov 18351da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 18361da177e4SLinus Torvalds 1837c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1838c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1839c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1840c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 18411da177e4SLinus Torvalds if (!data) 18421da177e4SLinus Torvalds goto nodata; 184387151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 18466602cebbSEric Dumazet * optimized for the cases when header is void. 18476602cebbSEric Dumazet */ 18486602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 18496602cebbSEric Dumazet 18506602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 18516602cebbSEric Dumazet skb_shinfo(skb), 1852fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 18531da177e4SLinus Torvalds 18543e24591aSAlexander Duyck /* 18553e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 18563e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 18573e24591aSAlexander Duyck * be since all we did is relocate the values 18583e24591aSAlexander Duyck */ 18593e24591aSAlexander Duyck if (skb_cloned(skb)) { 186070008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1861a6686f2fSShirley Ma goto nofrags; 18621f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1863c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 18641da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1865ea2ab693SIan Campbell skb_frag_ref(skb, i); 18661da177e4SLinus Torvalds 186721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 18681da177e4SLinus Torvalds skb_clone_fraglist(skb); 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds skb_release_data(skb); 18713e24591aSAlexander Duyck } else { 18723e24591aSAlexander Duyck skb_free_head(skb); 18731fd63041SEric Dumazet } 18741da177e4SLinus Torvalds off = (data + nhead) - skb->head; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds skb->head = data; 1877d3836f21SEric Dumazet skb->head_frag = 0; 18781da177e4SLinus Torvalds skb->data += off; 1879763087daSEric Dumazet 1880763087daSEric Dumazet skb_set_end_offset(skb, size); 18814305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 188256eb8882SPatrick McHardy off = nhead; 188356eb8882SPatrick McHardy #endif 188427a884dcSArnaldo Carvalho de Melo skb->tail += off; 1885b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 18861da177e4SLinus Torvalds skb->cloned = 0; 1887334a8132SPatrick McHardy skb->hdr_len = 0; 18881da177e4SLinus Torvalds skb->nohdr = 0; 18891da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1890158f323bSEric Dumazet 1891de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1892de8f3a83SDaniel Borkmann 1893158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1894158f323bSEric Dumazet * For the moment, we really care of rx path, or 1895158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1896158f323bSEric Dumazet */ 1897158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1898158f323bSEric Dumazet skb->truesize += size - osize; 1899158f323bSEric Dumazet 19001da177e4SLinus Torvalds return 0; 19011da177e4SLinus Torvalds 1902a6686f2fSShirley Ma nofrags: 1903a6686f2fSShirley Ma kfree(data); 19041da177e4SLinus Torvalds nodata: 19051da177e4SLinus Torvalds return -ENOMEM; 19061da177e4SLinus Torvalds } 1907b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 19121da177e4SLinus Torvalds { 19131da177e4SLinus Torvalds struct sk_buff *skb2; 19141da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds if (delta <= 0) 19171da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 19181da177e4SLinus Torvalds else { 19191da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 19201da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 19211da177e4SLinus Torvalds GFP_ATOMIC)) { 19221da177e4SLinus Torvalds kfree_skb(skb2); 19231da177e4SLinus Torvalds skb2 = NULL; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds return skb2; 19271da177e4SLinus Torvalds } 1928b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 19291da177e4SLinus Torvalds 19302b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 19312b88cba5SEric Dumazet { 19322b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 19332b88cba5SEric Dumazet struct skb_shared_info *shinfo; 19342b88cba5SEric Dumazet int res; 19352b88cba5SEric Dumazet 19362b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 19372b88cba5SEric Dumazet saved_truesize = skb->truesize; 19382b88cba5SEric Dumazet 19392b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 19402b88cba5SEric Dumazet if (res) 19412b88cba5SEric Dumazet return res; 19422b88cba5SEric Dumazet 19432b88cba5SEric Dumazet skb->truesize = saved_truesize; 19442b88cba5SEric Dumazet 19452b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 19462b88cba5SEric Dumazet return 0; 19472b88cba5SEric Dumazet 19482b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 19492b88cba5SEric Dumazet 19502b88cba5SEric Dumazet /* We are about to change back skb->end, 19512b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 19522b88cba5SEric Dumazet */ 19532b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 19542b88cba5SEric Dumazet shinfo, 19552b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 19562b88cba5SEric Dumazet 19572b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 19582b88cba5SEric Dumazet 19592b88cba5SEric Dumazet return 0; 19602b88cba5SEric Dumazet } 19612b88cba5SEric Dumazet 19621da177e4SLinus Torvalds /** 1963f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 1964f1260ff1SVasily Averin * @skb: buffer to reallocate 1965f1260ff1SVasily Averin * @headroom: needed headroom 1966f1260ff1SVasily Averin * 1967f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 1968f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 1969f1260ff1SVasily Averin * and frees original skb in case of failures. 1970f1260ff1SVasily Averin * 1971f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 1972f1260ff1SVasily Averin */ 1973f1260ff1SVasily Averin 1974f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 1975f1260ff1SVasily Averin { 1976f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 19777f678defSVasily Averin int osize = skb_end_offset(skb); 19787f678defSVasily Averin struct sock *sk = skb->sk; 1979f1260ff1SVasily Averin 1980f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 1981f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 1982f1260ff1SVasily Averin return skb; 1983f1260ff1SVasily Averin 19847f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 19857f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 19867f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 1987f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 1988f1260ff1SVasily Averin 19897f678defSVasily Averin if (unlikely(!nskb)) 19907f678defSVasily Averin goto fail; 19917f678defSVasily Averin 19927f678defSVasily Averin if (sk) 19937f678defSVasily Averin skb_set_owner_w(nskb, sk); 1994f1260ff1SVasily Averin consume_skb(skb); 1995f1260ff1SVasily Averin skb = nskb; 1996f1260ff1SVasily Averin } 19977f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 19987f678defSVasily Averin goto fail; 19997f678defSVasily Averin 20007f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 20017f678defSVasily Averin delta = skb_end_offset(skb) - osize; 20027f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 20037f678defSVasily Averin skb->truesize += delta; 2004f1260ff1SVasily Averin } 2005f1260ff1SVasily Averin return skb; 20067f678defSVasily Averin 20077f678defSVasily Averin fail: 20087f678defSVasily Averin kfree_skb(skb); 20097f678defSVasily Averin return NULL; 2010f1260ff1SVasily Averin } 2011f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 2012f1260ff1SVasily Averin 2013f1260ff1SVasily Averin /** 20141da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 20151da177e4SLinus Torvalds * @skb: buffer to copy 20161da177e4SLinus Torvalds * @newheadroom: new free bytes at head 20171da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 20181da177e4SLinus Torvalds * @gfp_mask: allocation priority 20191da177e4SLinus Torvalds * 20201da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 20211da177e4SLinus Torvalds * allocate additional space. 20221da177e4SLinus Torvalds * 20231da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 20241da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 20251da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 20261da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 20271da177e4SLinus Torvalds * 20281da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 20291da177e4SLinus Torvalds * is called from an interrupt. 20301da177e4SLinus Torvalds */ 20311da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 203286a76cafSVictor Fusco int newheadroom, int newtailroom, 2033dd0fc66fSAl Viro gfp_t gfp_mask) 20341da177e4SLinus Torvalds { 20351da177e4SLinus Torvalds /* 20361da177e4SLinus Torvalds * Allocate the copy buffer 20371da177e4SLinus Torvalds */ 2038c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2039c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 2040c93bdd0eSMel Gorman NUMA_NO_NODE); 2041efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 20421da177e4SLinus Torvalds int head_copy_len, head_copy_off; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds if (!n) 20451da177e4SLinus Torvalds return NULL; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds skb_reserve(n, newheadroom); 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds /* Set the tail pointer and length */ 20501da177e4SLinus Torvalds skb_put(n, skb->len); 20511da177e4SLinus Torvalds 2052efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 20531da177e4SLinus Torvalds head_copy_off = 0; 20541da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 20551da177e4SLinus Torvalds head_copy_len = newheadroom; 20561da177e4SLinus Torvalds else 20571da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /* Copy the linear header and data. */ 20609f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 20619f77fad3STim Hansen skb->len + head_copy_len)); 20621da177e4SLinus Torvalds 206308303c18SIlya Lesokhin skb_copy_header(n, skb); 20641da177e4SLinus Torvalds 2065030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 2066efd1e8d5SPatrick McHardy 20671da177e4SLinus Torvalds return n; 20681da177e4SLinus Torvalds } 2069b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /** 2072cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 20731da177e4SLinus Torvalds * @skb: buffer to pad 20741da177e4SLinus Torvalds * @pad: space to pad 2075cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 20761da177e4SLinus Torvalds * 20771da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 20781da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 20791da177e4SLinus Torvalds * beyond the buffer end onto the wire. 20801da177e4SLinus Torvalds * 2081cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 2082cd0a137aSFlorian Fainelli * if @free_on_error is true. 20831da177e4SLinus Torvalds */ 20841da177e4SLinus Torvalds 2085cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 20861da177e4SLinus Torvalds { 20875b057c6bSHerbert Xu int err; 20885b057c6bSHerbert Xu int ntail; 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 20915b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 20921da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 20935b057c6bSHerbert Xu return 0; 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds 20964305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 20975b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 20985b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 20995b057c6bSHerbert Xu if (unlikely(err)) 21005b057c6bSHerbert Xu goto free_skb; 21015b057c6bSHerbert Xu } 21025b057c6bSHerbert Xu 21035b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 21045b057c6bSHerbert Xu * to be audited. 21055b057c6bSHerbert Xu */ 21065b057c6bSHerbert Xu err = skb_linearize(skb); 21075b057c6bSHerbert Xu if (unlikely(err)) 21085b057c6bSHerbert Xu goto free_skb; 21095b057c6bSHerbert Xu 21105b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 21115b057c6bSHerbert Xu return 0; 21125b057c6bSHerbert Xu 21135b057c6bSHerbert Xu free_skb: 2114cd0a137aSFlorian Fainelli if (free_on_error) 21151da177e4SLinus Torvalds kfree_skb(skb); 21165b057c6bSHerbert Xu return err; 21171da177e4SLinus Torvalds } 2118cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 21191da177e4SLinus Torvalds 21200dde3e16SIlpo Järvinen /** 21210c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 21220c7ddf36SMathias Krause * @skb: start of the buffer to use 21230c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 21240c7ddf36SMathias Krause * @len: amount of data to add 21250c7ddf36SMathias Krause * 21260c7ddf36SMathias Krause * This function extends the used data area of the potentially 21270c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 21280c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 21290c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 21300c7ddf36SMathias Krause * returned. 21310c7ddf36SMathias Krause */ 21320c7ddf36SMathias Krause 21334df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 21340c7ddf36SMathias Krause { 21350c7ddf36SMathias Krause if (tail != skb) { 21360c7ddf36SMathias Krause skb->data_len += len; 21370c7ddf36SMathias Krause skb->len += len; 21380c7ddf36SMathias Krause } 21390c7ddf36SMathias Krause return skb_put(tail, len); 21400c7ddf36SMathias Krause } 21410c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 21420c7ddf36SMathias Krause 21430c7ddf36SMathias Krause /** 21440dde3e16SIlpo Järvinen * skb_put - add data to a buffer 21450dde3e16SIlpo Järvinen * @skb: buffer to use 21460dde3e16SIlpo Järvinen * @len: amount of data to add 21470dde3e16SIlpo Järvinen * 21480dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 21490dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 21500dde3e16SIlpo Järvinen * first byte of the extra data is returned. 21510dde3e16SIlpo Järvinen */ 21524df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 21530dde3e16SIlpo Järvinen { 21544df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 21550dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 21560dde3e16SIlpo Järvinen skb->tail += len; 21570dde3e16SIlpo Järvinen skb->len += len; 21580dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 21590dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 21600dde3e16SIlpo Järvinen return tmp; 21610dde3e16SIlpo Järvinen } 21620dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 21630dde3e16SIlpo Järvinen 21646be8ac2fSIlpo Järvinen /** 2165c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2166c2aa270aSIlpo Järvinen * @skb: buffer to use 2167c2aa270aSIlpo Järvinen * @len: amount of data to add 2168c2aa270aSIlpo Järvinen * 2169c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2170c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2171c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2172c2aa270aSIlpo Järvinen */ 2173d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2174c2aa270aSIlpo Järvinen { 2175c2aa270aSIlpo Järvinen skb->data -= len; 2176c2aa270aSIlpo Järvinen skb->len += len; 2177c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2178c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2179c2aa270aSIlpo Järvinen return skb->data; 2180c2aa270aSIlpo Järvinen } 2181c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2182c2aa270aSIlpo Järvinen 2183c2aa270aSIlpo Järvinen /** 21846be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 21856be8ac2fSIlpo Järvinen * @skb: buffer to use 21866be8ac2fSIlpo Järvinen * @len: amount of data to remove 21876be8ac2fSIlpo Järvinen * 21886be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 21896be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 21906be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 21916be8ac2fSIlpo Järvinen * the old data. 21926be8ac2fSIlpo Järvinen */ 2193af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 21946be8ac2fSIlpo Järvinen { 219547d29646SDavid S. Miller return skb_pull_inline(skb, len); 21966be8ac2fSIlpo Järvinen } 21976be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 21986be8ac2fSIlpo Järvinen 2199419ae74eSIlpo Järvinen /** 220013244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 220113244cccSLuiz Augusto von Dentz * original position. 220213244cccSLuiz Augusto von Dentz * @skb: buffer to use 220313244cccSLuiz Augusto von Dentz * @len: amount of data to remove 220413244cccSLuiz Augusto von Dentz * 220513244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 220613244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 220713244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 220813244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 220913244cccSLuiz Augusto von Dentz */ 221013244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 221113244cccSLuiz Augusto von Dentz { 221213244cccSLuiz Augusto von Dentz void *data = skb->data; 221313244cccSLuiz Augusto von Dentz 221413244cccSLuiz Augusto von Dentz if (skb->len < len) 221513244cccSLuiz Augusto von Dentz return NULL; 221613244cccSLuiz Augusto von Dentz 221713244cccSLuiz Augusto von Dentz skb_pull(skb, len); 221813244cccSLuiz Augusto von Dentz 221913244cccSLuiz Augusto von Dentz return data; 222013244cccSLuiz Augusto von Dentz } 222113244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 222213244cccSLuiz Augusto von Dentz 222313244cccSLuiz Augusto von Dentz /** 2224419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2225419ae74eSIlpo Järvinen * @skb: buffer to alter 2226419ae74eSIlpo Järvinen * @len: new length 2227419ae74eSIlpo Järvinen * 2228419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2229419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2230419ae74eSIlpo Järvinen * The skb must be linear. 2231419ae74eSIlpo Järvinen */ 2232419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2233419ae74eSIlpo Järvinen { 2234419ae74eSIlpo Järvinen if (skb->len > len) 2235419ae74eSIlpo Järvinen __skb_trim(skb, len); 2236419ae74eSIlpo Järvinen } 2237419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2238419ae74eSIlpo Järvinen 22393cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 22401da177e4SLinus Torvalds */ 22411da177e4SLinus Torvalds 22423cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 22431da177e4SLinus Torvalds { 224427b437c8SHerbert Xu struct sk_buff **fragp; 224527b437c8SHerbert Xu struct sk_buff *frag; 22461da177e4SLinus Torvalds int offset = skb_headlen(skb); 22471da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 22481da177e4SLinus Torvalds int i; 224927b437c8SHerbert Xu int err; 225027b437c8SHerbert Xu 225127b437c8SHerbert Xu if (skb_cloned(skb) && 225227b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 225327b437c8SHerbert Xu return err; 22541da177e4SLinus Torvalds 2255f4d26fb3SHerbert Xu i = 0; 2256f4d26fb3SHerbert Xu if (offset >= len) 2257f4d26fb3SHerbert Xu goto drop_pages; 2258f4d26fb3SHerbert Xu 2259f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 22609e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 226127b437c8SHerbert Xu 226227b437c8SHerbert Xu if (end < len) { 22631da177e4SLinus Torvalds offset = end; 226427b437c8SHerbert Xu continue; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22679e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 226827b437c8SHerbert Xu 2269f4d26fb3SHerbert Xu drop_pages: 227027b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 227127b437c8SHerbert Xu 227227b437c8SHerbert Xu for (; i < nfrags; i++) 2273ea2ab693SIan Campbell skb_frag_unref(skb, i); 227427b437c8SHerbert Xu 227521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 227627b437c8SHerbert Xu skb_drop_fraglist(skb); 2277f4d26fb3SHerbert Xu goto done; 227827b437c8SHerbert Xu } 227927b437c8SHerbert Xu 228027b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 228127b437c8SHerbert Xu fragp = &frag->next) { 228227b437c8SHerbert Xu int end = offset + frag->len; 228327b437c8SHerbert Xu 228427b437c8SHerbert Xu if (skb_shared(frag)) { 228527b437c8SHerbert Xu struct sk_buff *nfrag; 228627b437c8SHerbert Xu 228727b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 228827b437c8SHerbert Xu if (unlikely(!nfrag)) 228927b437c8SHerbert Xu return -ENOMEM; 229027b437c8SHerbert Xu 229127b437c8SHerbert Xu nfrag->next = frag->next; 229285bb2a60SEric Dumazet consume_skb(frag); 229327b437c8SHerbert Xu frag = nfrag; 229427b437c8SHerbert Xu *fragp = frag; 229527b437c8SHerbert Xu } 229627b437c8SHerbert Xu 229727b437c8SHerbert Xu if (end < len) { 229827b437c8SHerbert Xu offset = end; 229927b437c8SHerbert Xu continue; 230027b437c8SHerbert Xu } 230127b437c8SHerbert Xu 230227b437c8SHerbert Xu if (end > len && 230327b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 230427b437c8SHerbert Xu return err; 230527b437c8SHerbert Xu 230627b437c8SHerbert Xu if (frag->next) 230727b437c8SHerbert Xu skb_drop_list(&frag->next); 230827b437c8SHerbert Xu break; 230927b437c8SHerbert Xu } 231027b437c8SHerbert Xu 2311f4d26fb3SHerbert Xu done: 231227b437c8SHerbert Xu if (len > skb_headlen(skb)) { 23131da177e4SLinus Torvalds skb->data_len -= skb->len - len; 23141da177e4SLinus Torvalds skb->len = len; 23151da177e4SLinus Torvalds } else { 23161da177e4SLinus Torvalds skb->len = len; 23171da177e4SLinus Torvalds skb->data_len = 0; 231827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds 2321c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2322c21b48ccSEric Dumazet skb_condense(skb); 23231da177e4SLinus Torvalds return 0; 23241da177e4SLinus Torvalds } 2325b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 23261da177e4SLinus Torvalds 232788078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 232888078d98SEric Dumazet */ 232988078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 233088078d98SEric Dumazet { 233188078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 233288078d98SEric Dumazet int delta = skb->len - len; 233388078d98SEric Dumazet 2334d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2335d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2336d55bef50SDimitris Michailidis len); 233754970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 233854970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 233954970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 234054970a2fSVasily Averin 234154970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 234254970a2fSVasily Averin return -EINVAL; 234388078d98SEric Dumazet } 234488078d98SEric Dumazet return __pskb_trim(skb, len); 234588078d98SEric Dumazet } 234688078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 234788078d98SEric Dumazet 23481da177e4SLinus Torvalds /** 23491da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 23501da177e4SLinus Torvalds * @skb: buffer to reallocate 23511da177e4SLinus Torvalds * @delta: number of bytes to advance tail 23521da177e4SLinus Torvalds * 23531da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 23541da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 23551da177e4SLinus Torvalds * data from fragmented part. 23561da177e4SLinus Torvalds * 23571da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 23581da177e4SLinus Torvalds * 23591da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 23601da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 23611da177e4SLinus Torvalds * 23621da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 23631da177e4SLinus Torvalds * reloaded after call to this function. 23641da177e4SLinus Torvalds */ 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 23671da177e4SLinus Torvalds * when it is necessary. 23681da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 23691da177e4SLinus Torvalds * 2. It may change skb pointers. 23701da177e4SLinus Torvalds * 23711da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 23721da177e4SLinus Torvalds */ 2373af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 23741da177e4SLinus Torvalds { 23751da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 23761da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 23771da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 23781da177e4SLinus Torvalds */ 23794305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 23821da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 23831da177e4SLinus Torvalds GFP_ATOMIC)) 23841da177e4SLinus Torvalds return NULL; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23879f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 23889f77fad3STim Hansen skb_tail_pointer(skb), delta)); 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 23911da177e4SLinus Torvalds * size of pulled pages. Superb. 23921da177e4SLinus Torvalds */ 239321dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 23941da177e4SLinus Torvalds goto pull_pages; 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 23971da177e4SLinus Torvalds eat = delta; 23981da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 23999e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 24009e903e08SEric Dumazet 24019e903e08SEric Dumazet if (size >= eat) 24021da177e4SLinus Torvalds goto pull_pages; 24039e903e08SEric Dumazet eat -= size; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 240709001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 24081da177e4SLinus Torvalds * but taking into account that pulling is expected to 24091da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 24101da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 24111da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 24121da177e4SLinus Torvalds */ 24131da177e4SLinus Torvalds if (eat) { 24141da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 24151da177e4SLinus Torvalds struct sk_buff *clone = NULL; 24161da177e4SLinus Torvalds struct sk_buff *insp = NULL; 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds do { 24191da177e4SLinus Torvalds if (list->len <= eat) { 24201da177e4SLinus Torvalds /* Eaten as whole. */ 24211da177e4SLinus Torvalds eat -= list->len; 24221da177e4SLinus Torvalds list = list->next; 24231da177e4SLinus Torvalds insp = list; 24241da177e4SLinus Torvalds } else { 24251da177e4SLinus Torvalds /* Eaten partially. */ 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds if (skb_shared(list)) { 24281da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 24291da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 24301da177e4SLinus Torvalds if (!clone) 24311da177e4SLinus Torvalds return NULL; 24321da177e4SLinus Torvalds insp = list->next; 24331da177e4SLinus Torvalds list = clone; 24341da177e4SLinus Torvalds } else { 24351da177e4SLinus Torvalds /* This may be pulled without 24361da177e4SLinus Torvalds * problems. */ 24371da177e4SLinus Torvalds insp = list; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 24401da177e4SLinus Torvalds kfree_skb(clone); 24411da177e4SLinus Torvalds return NULL; 24421da177e4SLinus Torvalds } 24431da177e4SLinus Torvalds break; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds } while (eat); 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds /* Free pulled out fragments. */ 24481da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 24491da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2450ef527f96SEric Dumazet consume_skb(list); 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds /* And insert new clone at head. */ 24531da177e4SLinus Torvalds if (clone) { 24541da177e4SLinus Torvalds clone->next = list; 24551da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 24561da177e4SLinus Torvalds } 24571da177e4SLinus Torvalds } 24581da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds pull_pages: 24611da177e4SLinus Torvalds eat = delta; 24621da177e4SLinus Torvalds k = 0; 24631da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24649e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 24659e903e08SEric Dumazet 24669e903e08SEric Dumazet if (size <= eat) { 2467ea2ab693SIan Campbell skb_frag_unref(skb, i); 24689e903e08SEric Dumazet eat -= size; 24691da177e4SLinus Torvalds } else { 2470b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2471b54c9d5bSJonathan Lemon 2472b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 24731da177e4SLinus Torvalds if (eat) { 2474b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2475b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 24763ccc6c6fSlinzhang if (!i) 24773ccc6c6fSlinzhang goto end; 24781da177e4SLinus Torvalds eat = 0; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds k++; 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 24841da177e4SLinus Torvalds 24853ccc6c6fSlinzhang end: 24861da177e4SLinus Torvalds skb->tail += delta; 24871da177e4SLinus Torvalds skb->data_len -= delta; 24881da177e4SLinus Torvalds 24891f8b977aSWillem de Bruijn if (!skb->data_len) 24901f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 24911f8b977aSWillem de Bruijn 249227a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 24931da177e4SLinus Torvalds } 2494b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 24951da177e4SLinus Torvalds 249622019b17SEric Dumazet /** 249722019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 249822019b17SEric Dumazet * @skb: source skb 249922019b17SEric Dumazet * @offset: offset in source 250022019b17SEric Dumazet * @to: destination buffer 250122019b17SEric Dumazet * @len: number of bytes to copy 250222019b17SEric Dumazet * 250322019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 250422019b17SEric Dumazet * destination buffer. 250522019b17SEric Dumazet * 250622019b17SEric Dumazet * CAUTION ! : 250722019b17SEric Dumazet * If its prototype is ever changed, 250822019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 250922019b17SEric Dumazet * since it is called from BPF assembly code. 251022019b17SEric Dumazet */ 25111da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 25121da177e4SLinus Torvalds { 25131a028e50SDavid S. Miller int start = skb_headlen(skb); 2514fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2515fbb398a8SDavid S. Miller int i, copy; 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds if (offset > (int)skb->len - len) 25181da177e4SLinus Torvalds goto fault; 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds /* Copy header. */ 25211a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 25221da177e4SLinus Torvalds if (copy > len) 25231da177e4SLinus Torvalds copy = len; 2524d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 25251da177e4SLinus Torvalds if ((len -= copy) == 0) 25261da177e4SLinus Torvalds return 0; 25271da177e4SLinus Torvalds offset += copy; 25281da177e4SLinus Torvalds to += copy; 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25321a028e50SDavid S. Miller int end; 253351c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 25341da177e4SLinus Torvalds 2535547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25361a028e50SDavid S. Miller 253751c56b00SEric Dumazet end = start + skb_frag_size(f); 25381da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2539c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2540c613c209SWillem de Bruijn struct page *p; 25411da177e4SLinus Torvalds u8 *vaddr; 25421da177e4SLinus Torvalds 25431da177e4SLinus Torvalds if (copy > len) 25441da177e4SLinus Torvalds copy = len; 25451da177e4SLinus Torvalds 2546c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2547b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2548c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2549c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2550c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 255151c56b00SEric Dumazet kunmap_atomic(vaddr); 2552c613c209SWillem de Bruijn } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds if ((len -= copy) == 0) 25551da177e4SLinus Torvalds return 0; 25561da177e4SLinus Torvalds offset += copy; 25571da177e4SLinus Torvalds to += copy; 25581da177e4SLinus Torvalds } 25591a028e50SDavid S. Miller start = end; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 2562fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 25631a028e50SDavid S. Miller int end; 25641da177e4SLinus Torvalds 2565547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25661a028e50SDavid S. Miller 2567fbb398a8SDavid S. Miller end = start + frag_iter->len; 25681da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 25691da177e4SLinus Torvalds if (copy > len) 25701da177e4SLinus Torvalds copy = len; 2571fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 25721da177e4SLinus Torvalds goto fault; 25731da177e4SLinus Torvalds if ((len -= copy) == 0) 25741da177e4SLinus Torvalds return 0; 25751da177e4SLinus Torvalds offset += copy; 25761da177e4SLinus Torvalds to += copy; 25771da177e4SLinus Torvalds } 25781a028e50SDavid S. Miller start = end; 25791da177e4SLinus Torvalds } 2580a6686f2fSShirley Ma 25811da177e4SLinus Torvalds if (!len) 25821da177e4SLinus Torvalds return 0; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds fault: 25851da177e4SLinus Torvalds return -EFAULT; 25861da177e4SLinus Torvalds } 2587b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 25881da177e4SLinus Torvalds 25899c55e01cSJens Axboe /* 25909c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 25919c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 25929c55e01cSJens Axboe */ 25939c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 25949c55e01cSJens Axboe { 25958b9d3728SJarek Poplawski put_page(spd->pages[i]); 25968b9d3728SJarek Poplawski } 25979c55e01cSJens Axboe 2598a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 25994fb66994SJarek Poplawski unsigned int *offset, 260018aafc62SEric Dumazet struct sock *sk) 26018b9d3728SJarek Poplawski { 26025640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 26038b9d3728SJarek Poplawski 26045640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 26058b9d3728SJarek Poplawski return NULL; 26064fb66994SJarek Poplawski 26075640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 26084fb66994SJarek Poplawski 26095640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 26105640f768SEric Dumazet page_address(page) + *offset, *len); 26115640f768SEric Dumazet *offset = pfrag->offset; 26125640f768SEric Dumazet pfrag->offset += *len; 26134fb66994SJarek Poplawski 26145640f768SEric Dumazet return pfrag->page; 26159c55e01cSJens Axboe } 26169c55e01cSJens Axboe 261741c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 261841c73a0dSEric Dumazet struct page *page, 261941c73a0dSEric Dumazet unsigned int offset) 262041c73a0dSEric Dumazet { 262141c73a0dSEric Dumazet return spd->nr_pages && 262241c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 262341c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 262441c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 262541c73a0dSEric Dumazet } 262641c73a0dSEric Dumazet 26279c55e01cSJens Axboe /* 26289c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 26299c55e01cSJens Axboe */ 2630a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 263135f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 26324fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 263318aafc62SEric Dumazet bool linear, 26347a67e56fSJarek Poplawski struct sock *sk) 26359c55e01cSJens Axboe { 263641c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2637a108d5f3SDavid S. Miller return true; 26389c55e01cSJens Axboe 26398b9d3728SJarek Poplawski if (linear) { 264018aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 26418b9d3728SJarek Poplawski if (!page) 2642a108d5f3SDavid S. Miller return true; 264341c73a0dSEric Dumazet } 264441c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 264541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2646a108d5f3SDavid S. Miller return false; 264741c73a0dSEric Dumazet } 26488b9d3728SJarek Poplawski get_page(page); 26499c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 26504fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 26519c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 26529c55e01cSJens Axboe spd->nr_pages++; 26538b9d3728SJarek Poplawski 2654a108d5f3SDavid S. Miller return false; 26559c55e01cSJens Axboe } 26569c55e01cSJens Axboe 2657a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 26582870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 265918aafc62SEric Dumazet unsigned int *len, 2660d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 266135f3d14dSJens Axboe struct sock *sk, 266235f3d14dSJens Axboe struct pipe_inode_info *pipe) 26639c55e01cSJens Axboe { 26642870c43dSOctavian Purdila if (!*len) 2665a108d5f3SDavid S. Miller return true; 26669c55e01cSJens Axboe 26672870c43dSOctavian Purdila /* skip this segment if already processed */ 26682870c43dSOctavian Purdila if (*off >= plen) { 26692870c43dSOctavian Purdila *off -= plen; 2670a108d5f3SDavid S. Miller return false; 26712870c43dSOctavian Purdila } 26722870c43dSOctavian Purdila 26732870c43dSOctavian Purdila /* ignore any bits we already processed */ 26749ca1b22dSEric Dumazet poff += *off; 26759ca1b22dSEric Dumazet plen -= *off; 26762870c43dSOctavian Purdila *off = 0; 26772870c43dSOctavian Purdila 267818aafc62SEric Dumazet do { 267918aafc62SEric Dumazet unsigned int flen = min(*len, plen); 26802870c43dSOctavian Purdila 268118aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 268218aafc62SEric Dumazet linear, sk)) 2683a108d5f3SDavid S. Miller return true; 268418aafc62SEric Dumazet poff += flen; 268518aafc62SEric Dumazet plen -= flen; 26862870c43dSOctavian Purdila *len -= flen; 268718aafc62SEric Dumazet } while (*len && plen); 26882870c43dSOctavian Purdila 2689a108d5f3SDavid S. Miller return false; 2690db43a282SOctavian Purdila } 26919c55e01cSJens Axboe 26929c55e01cSJens Axboe /* 2693a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 26942870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 26959c55e01cSJens Axboe */ 2696a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 269735f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 269835f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 26992870c43dSOctavian Purdila { 27002870c43dSOctavian Purdila int seg; 2701fa9835e5STom Herbert struct sk_buff *iter; 27029c55e01cSJens Axboe 27031d0c0b32SEric Dumazet /* map the linear part : 27042996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 27052996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 27062996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 27079c55e01cSJens Axboe */ 27082870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 27092870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 27102870c43dSOctavian Purdila skb_headlen(skb), 271118aafc62SEric Dumazet offset, len, spd, 27123a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 27131d0c0b32SEric Dumazet sk, pipe)) 2714a108d5f3SDavid S. Miller return true; 27159c55e01cSJens Axboe 27169c55e01cSJens Axboe /* 27179c55e01cSJens Axboe * then map the fragments 27189c55e01cSJens Axboe */ 27199c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 27209c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 27219c55e01cSJens Axboe 2722ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2723b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 272418aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2725a108d5f3SDavid S. Miller return true; 27269c55e01cSJens Axboe } 27279c55e01cSJens Axboe 2728fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2729fa9835e5STom Herbert if (*offset >= iter->len) { 2730fa9835e5STom Herbert *offset -= iter->len; 2731fa9835e5STom Herbert continue; 2732fa9835e5STom Herbert } 2733fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2734fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2735fa9835e5STom Herbert * case. 2736fa9835e5STom Herbert */ 2737fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2738fa9835e5STom Herbert return true; 2739fa9835e5STom Herbert } 2740fa9835e5STom Herbert 2741a108d5f3SDavid S. Miller return false; 27429c55e01cSJens Axboe } 27439c55e01cSJens Axboe 27449c55e01cSJens Axboe /* 27459c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2746fa9835e5STom Herbert * the fragments, and the frag list. 27479c55e01cSJens Axboe */ 2748a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 27499c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 275025869262SAl Viro unsigned int flags) 27519c55e01cSJens Axboe { 275241c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 275341c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 27549c55e01cSJens Axboe struct splice_pipe_desc spd = { 27559c55e01cSJens Axboe .pages = pages, 27569c55e01cSJens Axboe .partial = partial, 2757047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 275828a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 27599c55e01cSJens Axboe .spd_release = sock_spd_release, 27609c55e01cSJens Axboe }; 276135f3d14dSJens Axboe int ret = 0; 276235f3d14dSJens Axboe 2763fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 27649c55e01cSJens Axboe 2765a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 276625869262SAl Viro ret = splice_to_pipe(pipe, &spd); 27679c55e01cSJens Axboe 276835f3d14dSJens Axboe return ret; 27699c55e01cSJens Axboe } 27702b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 27719c55e01cSJens Axboe 27720739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 27730739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 27740739cd28SCong Wang { 27750739cd28SCong Wang struct socket *sock = sk->sk_socket; 27760739cd28SCong Wang 27770739cd28SCong Wang if (!sock) 27780739cd28SCong Wang return -EINVAL; 27790739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 27800739cd28SCong Wang } 27810739cd28SCong Wang 27820739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 27830739cd28SCong Wang size_t size, int flags) 27840739cd28SCong Wang { 27850739cd28SCong Wang struct socket *sock = sk->sk_socket; 27860739cd28SCong Wang 27870739cd28SCong Wang if (!sock) 27880739cd28SCong Wang return -EINVAL; 27890739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 27900739cd28SCong Wang } 27910739cd28SCong Wang 27920739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 27930739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 27940739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 27950739cd28SCong Wang size_t size, int flags); 27960739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 27970739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 279820bf50deSTom Herbert { 279920bf50deSTom Herbert unsigned int orig_len = len; 280020bf50deSTom Herbert struct sk_buff *head = skb; 280120bf50deSTom Herbert unsigned short fragidx; 280220bf50deSTom Herbert int slen, ret; 280320bf50deSTom Herbert 280420bf50deSTom Herbert do_frag_list: 280520bf50deSTom Herbert 280620bf50deSTom Herbert /* Deal with head data */ 280720bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 280820bf50deSTom Herbert struct kvec kv; 280920bf50deSTom Herbert struct msghdr msg; 281020bf50deSTom Herbert 281120bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 281220bf50deSTom Herbert kv.iov_base = skb->data + offset; 2813db5980d8SJohn Fastabend kv.iov_len = slen; 281420bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2815bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 281620bf50deSTom Herbert 28170739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 28180739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 281920bf50deSTom Herbert if (ret <= 0) 282020bf50deSTom Herbert goto error; 282120bf50deSTom Herbert 282220bf50deSTom Herbert offset += ret; 282320bf50deSTom Herbert len -= ret; 282420bf50deSTom Herbert } 282520bf50deSTom Herbert 282620bf50deSTom Herbert /* All the data was skb head? */ 282720bf50deSTom Herbert if (!len) 282820bf50deSTom Herbert goto out; 282920bf50deSTom Herbert 283020bf50deSTom Herbert /* Make offset relative to start of frags */ 283120bf50deSTom Herbert offset -= skb_headlen(skb); 283220bf50deSTom Herbert 283320bf50deSTom Herbert /* Find where we are in frag list */ 283420bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 283520bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 283620bf50deSTom Herbert 2837d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 283820bf50deSTom Herbert break; 283920bf50deSTom Herbert 2840d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 284120bf50deSTom Herbert } 284220bf50deSTom Herbert 284320bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 284420bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 284520bf50deSTom Herbert 2846d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 284720bf50deSTom Herbert 284820bf50deSTom Herbert while (slen) { 28490739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 28500739cd28SCong Wang sendpage_unlocked, sk, 28510739cd28SCong Wang skb_frag_page(frag), 2852b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 285320bf50deSTom Herbert slen, MSG_DONTWAIT); 285420bf50deSTom Herbert if (ret <= 0) 285520bf50deSTom Herbert goto error; 285620bf50deSTom Herbert 285720bf50deSTom Herbert len -= ret; 285820bf50deSTom Herbert offset += ret; 285920bf50deSTom Herbert slen -= ret; 286020bf50deSTom Herbert } 286120bf50deSTom Herbert 286220bf50deSTom Herbert offset = 0; 286320bf50deSTom Herbert } 286420bf50deSTom Herbert 286520bf50deSTom Herbert if (len) { 286620bf50deSTom Herbert /* Process any frag lists */ 286720bf50deSTom Herbert 286820bf50deSTom Herbert if (skb == head) { 286920bf50deSTom Herbert if (skb_has_frag_list(skb)) { 287020bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 287120bf50deSTom Herbert goto do_frag_list; 287220bf50deSTom Herbert } 287320bf50deSTom Herbert } else if (skb->next) { 287420bf50deSTom Herbert skb = skb->next; 287520bf50deSTom Herbert goto do_frag_list; 287620bf50deSTom Herbert } 287720bf50deSTom Herbert } 287820bf50deSTom Herbert 287920bf50deSTom Herbert out: 288020bf50deSTom Herbert return orig_len - len; 288120bf50deSTom Herbert 288220bf50deSTom Herbert error: 288320bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 288420bf50deSTom Herbert } 28850739cd28SCong Wang 28860739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 28870739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 28880739cd28SCong Wang int len) 28890739cd28SCong Wang { 28900739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 28910739cd28SCong Wang kernel_sendpage_locked); 28920739cd28SCong Wang } 289320bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 289420bf50deSTom Herbert 28950739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 28960739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 28970739cd28SCong Wang { 28980739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 28990739cd28SCong Wang sendpage_unlocked); 29000739cd28SCong Wang } 29010739cd28SCong Wang 2902357b40a1SHerbert Xu /** 2903357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2904357b40a1SHerbert Xu * @skb: destination buffer 2905357b40a1SHerbert Xu * @offset: offset in destination 2906357b40a1SHerbert Xu * @from: source buffer 2907357b40a1SHerbert Xu * @len: number of bytes to copy 2908357b40a1SHerbert Xu * 2909357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2910357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2911357b40a1SHerbert Xu * traversing fragment lists and such. 2912357b40a1SHerbert Xu */ 2913357b40a1SHerbert Xu 29140c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2915357b40a1SHerbert Xu { 29161a028e50SDavid S. Miller int start = skb_headlen(skb); 2917fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2918fbb398a8SDavid S. Miller int i, copy; 2919357b40a1SHerbert Xu 2920357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2921357b40a1SHerbert Xu goto fault; 2922357b40a1SHerbert Xu 29231a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2924357b40a1SHerbert Xu if (copy > len) 2925357b40a1SHerbert Xu copy = len; 292627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2927357b40a1SHerbert Xu if ((len -= copy) == 0) 2928357b40a1SHerbert Xu return 0; 2929357b40a1SHerbert Xu offset += copy; 2930357b40a1SHerbert Xu from += copy; 2931357b40a1SHerbert Xu } 2932357b40a1SHerbert Xu 2933357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2934357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 29351a028e50SDavid S. Miller int end; 2936357b40a1SHerbert Xu 2937547b792cSIlpo Järvinen WARN_ON(start > offset + len); 29381a028e50SDavid S. Miller 29399e903e08SEric Dumazet end = start + skb_frag_size(frag); 2940357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2941c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2942c613c209SWillem de Bruijn struct page *p; 2943357b40a1SHerbert Xu u8 *vaddr; 2944357b40a1SHerbert Xu 2945357b40a1SHerbert Xu if (copy > len) 2946357b40a1SHerbert Xu copy = len; 2947357b40a1SHerbert Xu 2948c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2949b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2950c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2951c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2952c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 295351c56b00SEric Dumazet kunmap_atomic(vaddr); 2954c613c209SWillem de Bruijn } 2955357b40a1SHerbert Xu 2956357b40a1SHerbert Xu if ((len -= copy) == 0) 2957357b40a1SHerbert Xu return 0; 2958357b40a1SHerbert Xu offset += copy; 2959357b40a1SHerbert Xu from += copy; 2960357b40a1SHerbert Xu } 29611a028e50SDavid S. Miller start = end; 2962357b40a1SHerbert Xu } 2963357b40a1SHerbert Xu 2964fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 29651a028e50SDavid S. Miller int end; 2966357b40a1SHerbert Xu 2967547b792cSIlpo Järvinen WARN_ON(start > offset + len); 29681a028e50SDavid S. Miller 2969fbb398a8SDavid S. Miller end = start + frag_iter->len; 2970357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2971357b40a1SHerbert Xu if (copy > len) 2972357b40a1SHerbert Xu copy = len; 2973fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 29741a028e50SDavid S. Miller from, copy)) 2975357b40a1SHerbert Xu goto fault; 2976357b40a1SHerbert Xu if ((len -= copy) == 0) 2977357b40a1SHerbert Xu return 0; 2978357b40a1SHerbert Xu offset += copy; 2979357b40a1SHerbert Xu from += copy; 2980357b40a1SHerbert Xu } 29811a028e50SDavid S. Miller start = end; 2982357b40a1SHerbert Xu } 2983357b40a1SHerbert Xu if (!len) 2984357b40a1SHerbert Xu return 0; 2985357b40a1SHerbert Xu 2986357b40a1SHerbert Xu fault: 2987357b40a1SHerbert Xu return -EFAULT; 2988357b40a1SHerbert Xu } 2989357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2990357b40a1SHerbert Xu 29911da177e4SLinus Torvalds /* Checksum skb data. */ 29922817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 29932817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 29941da177e4SLinus Torvalds { 29951a028e50SDavid S. Miller int start = skb_headlen(skb); 29961a028e50SDavid S. Miller int i, copy = start - offset; 2997fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 29981da177e4SLinus Torvalds int pos = 0; 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds /* Checksum header. */ 30011da177e4SLinus Torvalds if (copy > 0) { 30021da177e4SLinus Torvalds if (copy > len) 30031da177e4SLinus Torvalds copy = len; 30042544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 30052544af03SMatteo Croce skb->data + offset, copy, csum); 30061da177e4SLinus Torvalds if ((len -= copy) == 0) 30071da177e4SLinus Torvalds return csum; 30081da177e4SLinus Torvalds offset += copy; 30091da177e4SLinus Torvalds pos = copy; 30101da177e4SLinus Torvalds } 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 30131a028e50SDavid S. Miller int end; 301451c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 30151da177e4SLinus Torvalds 3016547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30171a028e50SDavid S. Miller 301851c56b00SEric Dumazet end = start + skb_frag_size(frag); 30191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3020c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3021c613c209SWillem de Bruijn struct page *p; 302244bb9363SAl Viro __wsum csum2; 30231da177e4SLinus Torvalds u8 *vaddr; 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds if (copy > len) 30261da177e4SLinus Torvalds copy = len; 3027c613c209SWillem de Bruijn 3028c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3029b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3030c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3031c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 30322544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 30332544af03SMatteo Croce csum_partial_ext, 30342544af03SMatteo Croce vaddr + p_off, p_len, 0); 303551c56b00SEric Dumazet kunmap_atomic(vaddr); 30362544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 30372544af03SMatteo Croce csum_block_add_ext, csum, 30382544af03SMatteo Croce csum2, pos, p_len); 3039c613c209SWillem de Bruijn pos += p_len; 3040c613c209SWillem de Bruijn } 3041c613c209SWillem de Bruijn 30421da177e4SLinus Torvalds if (!(len -= copy)) 30431da177e4SLinus Torvalds return csum; 30441da177e4SLinus Torvalds offset += copy; 30451da177e4SLinus Torvalds } 30461a028e50SDavid S. Miller start = end; 30471da177e4SLinus Torvalds } 30481da177e4SLinus Torvalds 3049fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 30501a028e50SDavid S. Miller int end; 30511da177e4SLinus Torvalds 3052547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30531a028e50SDavid S. Miller 3054fbb398a8SDavid S. Miller end = start + frag_iter->len; 30551da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 30565f92a738SAl Viro __wsum csum2; 30571da177e4SLinus Torvalds if (copy > len) 30581da177e4SLinus Torvalds copy = len; 30592817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 30602817a336SDaniel Borkmann copy, 0, ops); 30612544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 30622544af03SMatteo Croce csum, csum2, pos, copy); 30631da177e4SLinus Torvalds if ((len -= copy) == 0) 30641da177e4SLinus Torvalds return csum; 30651da177e4SLinus Torvalds offset += copy; 30661da177e4SLinus Torvalds pos += copy; 30671da177e4SLinus Torvalds } 30681a028e50SDavid S. Miller start = end; 30691da177e4SLinus Torvalds } 307009a62660SKris Katterjohn BUG_ON(len); 30711da177e4SLinus Torvalds 30721da177e4SLinus Torvalds return csum; 30731da177e4SLinus Torvalds } 30742817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 30752817a336SDaniel Borkmann 30762817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 30772817a336SDaniel Borkmann int len, __wsum csum) 30782817a336SDaniel Borkmann { 30792817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 3080cea80ea8SDaniel Borkmann .update = csum_partial_ext, 30812817a336SDaniel Borkmann .combine = csum_block_add_ext, 30822817a336SDaniel Borkmann }; 30832817a336SDaniel Borkmann 30842817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 30852817a336SDaniel Borkmann } 3086b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds /* Both of above in one bottle. */ 30891da177e4SLinus Torvalds 309081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 30918d5930dfSAl Viro u8 *to, int len) 30921da177e4SLinus Torvalds { 30931a028e50SDavid S. Miller int start = skb_headlen(skb); 30941a028e50SDavid S. Miller int i, copy = start - offset; 3095fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 30961da177e4SLinus Torvalds int pos = 0; 30978d5930dfSAl Viro __wsum csum = 0; 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds /* Copy header. */ 31001da177e4SLinus Torvalds if (copy > 0) { 31011da177e4SLinus Torvalds if (copy > len) 31021da177e4SLinus Torvalds copy = len; 31031da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 3104cc44c17bSAl Viro copy); 31051da177e4SLinus Torvalds if ((len -= copy) == 0) 31061da177e4SLinus Torvalds return csum; 31071da177e4SLinus Torvalds offset += copy; 31081da177e4SLinus Torvalds to += copy; 31091da177e4SLinus Torvalds pos = copy; 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 31131a028e50SDavid S. Miller int end; 31141da177e4SLinus Torvalds 3115547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31161a028e50SDavid S. Miller 31179e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 31181da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 3119c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3120c613c209SWillem de Bruijn u32 p_off, p_len, copied; 3121c613c209SWillem de Bruijn struct page *p; 31225084205fSAl Viro __wsum csum2; 31231da177e4SLinus Torvalds u8 *vaddr; 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds if (copy > len) 31261da177e4SLinus Torvalds copy = len; 3127c613c209SWillem de Bruijn 3128c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 3129b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 3130c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 3131c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 3132c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3133c613c209SWillem de Bruijn to + copied, 3134cc44c17bSAl Viro p_len); 313551c56b00SEric Dumazet kunmap_atomic(vaddr); 31361da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 3137c613c209SWillem de Bruijn pos += p_len; 3138c613c209SWillem de Bruijn } 3139c613c209SWillem de Bruijn 31401da177e4SLinus Torvalds if (!(len -= copy)) 31411da177e4SLinus Torvalds return csum; 31421da177e4SLinus Torvalds offset += copy; 31431da177e4SLinus Torvalds to += copy; 31441da177e4SLinus Torvalds } 31451a028e50SDavid S. Miller start = end; 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds 3148fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 314981d77662SAl Viro __wsum csum2; 31501a028e50SDavid S. Miller int end; 31511da177e4SLinus Torvalds 3152547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31531a028e50SDavid S. Miller 3154fbb398a8SDavid S. Miller end = start + frag_iter->len; 31551da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 31561da177e4SLinus Torvalds if (copy > len) 31571da177e4SLinus Torvalds copy = len; 3158fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 31591a028e50SDavid S. Miller offset - start, 31608d5930dfSAl Viro to, copy); 31611da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 31621da177e4SLinus Torvalds if ((len -= copy) == 0) 31631da177e4SLinus Torvalds return csum; 31641da177e4SLinus Torvalds offset += copy; 31651da177e4SLinus Torvalds to += copy; 31661da177e4SLinus Torvalds pos += copy; 31671da177e4SLinus Torvalds } 31681a028e50SDavid S. Miller start = end; 31691da177e4SLinus Torvalds } 317009a62660SKris Katterjohn BUG_ON(len); 31711da177e4SLinus Torvalds return csum; 31721da177e4SLinus Torvalds } 3173b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 31741da177e4SLinus Torvalds 317549f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 317649f8e832SCong Wang { 317749f8e832SCong Wang __sum16 sum; 317849f8e832SCong Wang 317949f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 318014641931SCong Wang /* See comments in __skb_checksum_complete(). */ 318149f8e832SCong Wang if (likely(!sum)) { 318249f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 318349f8e832SCong Wang !skb->csum_complete_sw) 31847fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 318549f8e832SCong Wang } 318649f8e832SCong Wang if (!skb_shared(skb)) 318749f8e832SCong Wang skb->csum_valid = !sum; 318849f8e832SCong Wang return sum; 318949f8e832SCong Wang } 319049f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 319149f8e832SCong Wang 319214641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 319314641931SCong Wang * which has been changed from the hardware checksum, for example, by 319414641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 319514641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 319614641931SCong Wang * 319714641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 319814641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 319914641931SCong Wang * shared. 320014641931SCong Wang */ 320149f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 320249f8e832SCong Wang { 320349f8e832SCong Wang __wsum csum; 320449f8e832SCong Wang __sum16 sum; 320549f8e832SCong Wang 320649f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 320749f8e832SCong Wang 320849f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 320914641931SCong Wang /* This check is inverted, because we already knew the hardware 321014641931SCong Wang * checksum is invalid before calling this function. So, if the 321114641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 321214641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 321314641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 321414641931SCong Wang * when moving skb->data around. 321514641931SCong Wang */ 321649f8e832SCong Wang if (likely(!sum)) { 321749f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 321849f8e832SCong Wang !skb->csum_complete_sw) 32197fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 322049f8e832SCong Wang } 322149f8e832SCong Wang 322249f8e832SCong Wang if (!skb_shared(skb)) { 322349f8e832SCong Wang /* Save full packet checksum */ 322449f8e832SCong Wang skb->csum = csum; 322549f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 322649f8e832SCong Wang skb->csum_complete_sw = 1; 322749f8e832SCong Wang skb->csum_valid = !sum; 322849f8e832SCong Wang } 322949f8e832SCong Wang 323049f8e832SCong Wang return sum; 323149f8e832SCong Wang } 323249f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 323349f8e832SCong Wang 32349617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 32359617813dSDavide Caratti { 32369617813dSDavide Caratti net_warn_ratelimited( 32379617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 32389617813dSDavide Caratti __func__); 32399617813dSDavide Caratti return 0; 32409617813dSDavide Caratti } 32419617813dSDavide Caratti 32429617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 32439617813dSDavide Caratti int offset, int len) 32449617813dSDavide Caratti { 32459617813dSDavide Caratti net_warn_ratelimited( 32469617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 32479617813dSDavide Caratti __func__); 32489617813dSDavide Caratti return 0; 32499617813dSDavide Caratti } 32509617813dSDavide Caratti 32519617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 32529617813dSDavide Caratti .update = warn_crc32c_csum_update, 32539617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 32549617813dSDavide Caratti }; 32559617813dSDavide Caratti 32569617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 32579617813dSDavide Caratti &default_crc32c_ops; 32589617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 32599617813dSDavide Caratti 3260af2806f8SThomas Graf /** 3261af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3262af2806f8SThomas Graf * @from: source buffer 3263af2806f8SThomas Graf * 3264af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3265af2806f8SThomas Graf * into skb_zerocopy(). 3266af2806f8SThomas Graf */ 3267af2806f8SThomas Graf unsigned int 3268af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3269af2806f8SThomas Graf { 3270af2806f8SThomas Graf unsigned int hlen = 0; 3271af2806f8SThomas Graf 3272af2806f8SThomas Graf if (!from->head_frag || 3273af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3274a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3275af2806f8SThomas Graf hlen = skb_headlen(from); 3276a17ad096SPravin B Shelar if (!hlen) 3277a17ad096SPravin B Shelar hlen = from->len; 3278a17ad096SPravin B Shelar } 3279af2806f8SThomas Graf 3280af2806f8SThomas Graf if (skb_has_frag_list(from)) 3281af2806f8SThomas Graf hlen = from->len; 3282af2806f8SThomas Graf 3283af2806f8SThomas Graf return hlen; 3284af2806f8SThomas Graf } 3285af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3286af2806f8SThomas Graf 3287af2806f8SThomas Graf /** 3288af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3289af2806f8SThomas Graf * @to: destination buffer 32907fceb4deSMasanari Iida * @from: source buffer 3291af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3292af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3293af2806f8SThomas Graf * 3294af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3295af2806f8SThomas Graf * to the frags in the source buffer. 3296af2806f8SThomas Graf * 3297af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3298af2806f8SThomas Graf * headroom in the `to` buffer. 329936d5fe6aSZoltan Kiss * 330036d5fe6aSZoltan Kiss * Return value: 330136d5fe6aSZoltan Kiss * 0: everything is OK 330236d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 330336d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3304af2806f8SThomas Graf */ 330536d5fe6aSZoltan Kiss int 330636d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3307af2806f8SThomas Graf { 3308af2806f8SThomas Graf int i, j = 0; 3309af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 331036d5fe6aSZoltan Kiss int ret; 3311af2806f8SThomas Graf struct page *page; 3312af2806f8SThomas Graf unsigned int offset; 3313af2806f8SThomas Graf 3314af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3315af2806f8SThomas Graf 3316af2806f8SThomas Graf /* dont bother with small payloads */ 331736d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 331836d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3319af2806f8SThomas Graf 3320af2806f8SThomas Graf if (hlen) { 332136d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 332236d5fe6aSZoltan Kiss if (unlikely(ret)) 332336d5fe6aSZoltan Kiss return ret; 3324af2806f8SThomas Graf len -= hlen; 3325af2806f8SThomas Graf } else { 3326af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3327af2806f8SThomas Graf if (plen) { 3328af2806f8SThomas Graf page = virt_to_head_page(from->head); 3329af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3330af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3331af2806f8SThomas Graf get_page(page); 3332af2806f8SThomas Graf j = 1; 3333af2806f8SThomas Graf len -= plen; 3334af2806f8SThomas Graf } 3335af2806f8SThomas Graf } 3336af2806f8SThomas Graf 3337ede57d58SRichard Gobert skb_len_add(to, len + plen); 3338af2806f8SThomas Graf 333936d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 334036d5fe6aSZoltan Kiss skb_tx_error(from); 334136d5fe6aSZoltan Kiss return -ENOMEM; 334236d5fe6aSZoltan Kiss } 33431f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 334436d5fe6aSZoltan Kiss 3345af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3346d8e18a51SMatthew Wilcox (Oracle) int size; 3347d8e18a51SMatthew Wilcox (Oracle) 3348af2806f8SThomas Graf if (!len) 3349af2806f8SThomas Graf break; 3350af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3351d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3352d8e18a51SMatthew Wilcox (Oracle) len); 3353d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3354d8e18a51SMatthew Wilcox (Oracle) len -= size; 3355af2806f8SThomas Graf skb_frag_ref(to, j); 3356af2806f8SThomas Graf j++; 3357af2806f8SThomas Graf } 3358af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 335936d5fe6aSZoltan Kiss 336036d5fe6aSZoltan Kiss return 0; 3361af2806f8SThomas Graf } 3362af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3363af2806f8SThomas Graf 33641da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 33651da177e4SLinus Torvalds { 3366d3bc23e7SAl Viro __wsum csum; 33671da177e4SLinus Torvalds long csstart; 33681da177e4SLinus Torvalds 336984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 337055508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 33711da177e4SLinus Torvalds else 33721da177e4SLinus Torvalds csstart = skb_headlen(skb); 33731da177e4SLinus Torvalds 337409a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 33751da177e4SLinus Torvalds 3376d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds csum = 0; 33791da177e4SLinus Torvalds if (csstart != skb->len) 33801da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 33818d5930dfSAl Viro skb->len - csstart); 33821da177e4SLinus Torvalds 338384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3384ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 33851da177e4SLinus Torvalds 3386d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds } 3389b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds /** 33921da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 33931da177e4SLinus Torvalds * @list: list to dequeue from 33941da177e4SLinus Torvalds * 33951da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 33961da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 33971da177e4SLinus Torvalds * returned or %NULL if the list is empty. 33981da177e4SLinus Torvalds */ 33991da177e4SLinus Torvalds 34001da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 34011da177e4SLinus Torvalds { 34021da177e4SLinus Torvalds unsigned long flags; 34031da177e4SLinus Torvalds struct sk_buff *result; 34041da177e4SLinus Torvalds 34051da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 34061da177e4SLinus Torvalds result = __skb_dequeue(list); 34071da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 34081da177e4SLinus Torvalds return result; 34091da177e4SLinus Torvalds } 3410b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds /** 34131da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 34141da177e4SLinus Torvalds * @list: list to dequeue from 34151da177e4SLinus Torvalds * 34161da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 34171da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 34181da177e4SLinus Torvalds * returned or %NULL if the list is empty. 34191da177e4SLinus Torvalds */ 34201da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 34211da177e4SLinus Torvalds { 34221da177e4SLinus Torvalds unsigned long flags; 34231da177e4SLinus Torvalds struct sk_buff *result; 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 34261da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 34271da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 34281da177e4SLinus Torvalds return result; 34291da177e4SLinus Torvalds } 3430b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 34311da177e4SLinus Torvalds 34321da177e4SLinus Torvalds /** 34331da177e4SLinus Torvalds * skb_queue_purge - empty a list 34341da177e4SLinus Torvalds * @list: list to empty 34351da177e4SLinus Torvalds * 34361da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 34371da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 34381da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 34391da177e4SLinus Torvalds */ 34401da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 34411da177e4SLinus Torvalds { 34421da177e4SLinus Torvalds struct sk_buff *skb; 34431da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 34441da177e4SLinus Torvalds kfree_skb(skb); 34451da177e4SLinus Torvalds } 3446b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 34471da177e4SLinus Torvalds 34481da177e4SLinus Torvalds /** 34499f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 34509f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3451385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 34529f5afeaeSYaogong Wang * 34539f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 34549f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 34559f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 34569f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 34579f5afeaeSYaogong Wang */ 3458385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 34599f5afeaeSYaogong Wang { 34607c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3461385114deSPeter Oskolkov unsigned int sum = 0; 34629f5afeaeSYaogong Wang 34637c90584cSEric Dumazet while (p) { 34647c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 34657c90584cSEric Dumazet 34667c90584cSEric Dumazet p = rb_next(p); 34677c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3468385114deSPeter Oskolkov sum += skb->truesize; 34699f5afeaeSYaogong Wang kfree_skb(skb); 34707c90584cSEric Dumazet } 3471385114deSPeter Oskolkov return sum; 34729f5afeaeSYaogong Wang } 34739f5afeaeSYaogong Wang 34749f5afeaeSYaogong Wang /** 34751da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 34761da177e4SLinus Torvalds * @list: list to use 34771da177e4SLinus Torvalds * @newsk: buffer to queue 34781da177e4SLinus Torvalds * 34791da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 34801da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 34811da177e4SLinus Torvalds * safely. 34821da177e4SLinus Torvalds * 34831da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 34841da177e4SLinus Torvalds */ 34851da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 34861da177e4SLinus Torvalds { 34871da177e4SLinus Torvalds unsigned long flags; 34881da177e4SLinus Torvalds 34891da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 34901da177e4SLinus Torvalds __skb_queue_head(list, newsk); 34911da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 34921da177e4SLinus Torvalds } 3493b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds /** 34961da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 34971da177e4SLinus Torvalds * @list: list to use 34981da177e4SLinus Torvalds * @newsk: buffer to queue 34991da177e4SLinus Torvalds * 35001da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 35011da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 35021da177e4SLinus Torvalds * safely. 35031da177e4SLinus Torvalds * 35041da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 35051da177e4SLinus Torvalds */ 35061da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 35071da177e4SLinus Torvalds { 35081da177e4SLinus Torvalds unsigned long flags; 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35111da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 35121da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35131da177e4SLinus Torvalds } 3514b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 35158728b834SDavid S. Miller 35161da177e4SLinus Torvalds /** 35171da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 35181da177e4SLinus Torvalds * @skb: buffer to remove 35198728b834SDavid S. Miller * @list: list to use 35201da177e4SLinus Torvalds * 35218728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 35228728b834SDavid S. Miller * function is atomic with respect to other list locked calls 35231da177e4SLinus Torvalds * 35248728b834SDavid S. Miller * You must know what list the SKB is on. 35251da177e4SLinus Torvalds */ 35268728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 35271da177e4SLinus Torvalds { 35281da177e4SLinus Torvalds unsigned long flags; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 35318728b834SDavid S. Miller __skb_unlink(skb, list); 35321da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 35331da177e4SLinus Torvalds } 3534b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds /** 35371da177e4SLinus Torvalds * skb_append - append a buffer 35381da177e4SLinus Torvalds * @old: buffer to insert after 35391da177e4SLinus Torvalds * @newsk: buffer to insert 35408728b834SDavid S. Miller * @list: list to use 35411da177e4SLinus Torvalds * 35421da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 35431da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 35441da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 35451da177e4SLinus Torvalds */ 35468728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 35471da177e4SLinus Torvalds { 35481da177e4SLinus Torvalds unsigned long flags; 35491da177e4SLinus Torvalds 35508728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 35517de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 35528728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 35531da177e4SLinus Torvalds } 3554b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 35551da177e4SLinus Torvalds 35561da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 35571da177e4SLinus Torvalds struct sk_buff* skb1, 35581da177e4SLinus Torvalds const u32 len, const int pos) 35591da177e4SLinus Torvalds { 35601da177e4SLinus Torvalds int i; 35611da177e4SLinus Torvalds 3562d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3563d626f62bSArnaldo Carvalho de Melo pos - len); 35641da177e4SLinus Torvalds /* And move data appendix as is. */ 35651da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 35661da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 35671da177e4SLinus Torvalds 35681da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 35691da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 35701da177e4SLinus Torvalds skb1->data_len = skb->data_len; 35711da177e4SLinus Torvalds skb1->len += skb1->data_len; 35721da177e4SLinus Torvalds skb->data_len = 0; 35731da177e4SLinus Torvalds skb->len = len; 357427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 35751da177e4SLinus Torvalds } 35761da177e4SLinus Torvalds 35771da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 35781da177e4SLinus Torvalds struct sk_buff* skb1, 35791da177e4SLinus Torvalds const u32 len, int pos) 35801da177e4SLinus Torvalds { 35811da177e4SLinus Torvalds int i, k = 0; 35821da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 35831da177e4SLinus Torvalds 35841da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 35851da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 35861da177e4SLinus Torvalds skb->len = len; 35871da177e4SLinus Torvalds skb->data_len = len - pos; 35881da177e4SLinus Torvalds 35891da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 35909e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 35911da177e4SLinus Torvalds 35921da177e4SLinus Torvalds if (pos + size > len) { 35931da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 35941da177e4SLinus Torvalds 35951da177e4SLinus Torvalds if (pos < len) { 35961da177e4SLinus Torvalds /* Split frag. 35971da177e4SLinus Torvalds * We have two variants in this case: 35981da177e4SLinus Torvalds * 1. Move all the frag to the second 35991da177e4SLinus Torvalds * part, if it is possible. F.e. 36001da177e4SLinus Torvalds * this approach is mandatory for TUX, 36011da177e4SLinus Torvalds * where splitting is expensive. 36021da177e4SLinus Torvalds * 2. Split is accurately. We make this. 36031da177e4SLinus Torvalds */ 3604ea2ab693SIan Campbell skb_frag_ref(skb, i); 3605b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 36069e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 36079e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 36081da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds k++; 36111da177e4SLinus Torvalds } else 36121da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 36131da177e4SLinus Torvalds pos += size; 36141da177e4SLinus Torvalds } 36151da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 36161da177e4SLinus Torvalds } 36171da177e4SLinus Torvalds 36181da177e4SLinus Torvalds /** 36191da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 36201da177e4SLinus Torvalds * @skb: the buffer to split 36211da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 36221da177e4SLinus Torvalds * @len: new length for skb 36231da177e4SLinus Torvalds */ 36241da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 36251da177e4SLinus Torvalds { 36261da177e4SLinus Torvalds int pos = skb_headlen(skb); 36279b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 36281da177e4SLinus Torvalds 3629753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3630753f1ca4SPavel Begunkov 36319b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 36321f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 36331da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 36341da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 36351da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 36361da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 36371da177e4SLinus Torvalds } 3638b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 36391da177e4SLinus Torvalds 36409f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 36419f782db3SIlpo Järvinen * 36429f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 36439f782db3SIlpo Järvinen */ 3644832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3645832d11c5SIlpo Järvinen { 3646c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3647832d11c5SIlpo Järvinen } 3648832d11c5SIlpo Järvinen 3649832d11c5SIlpo Järvinen /** 3650832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3651832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3652832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3653832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3654832d11c5SIlpo Järvinen * 3655832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 365620e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3657832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3658832d11c5SIlpo Järvinen * 3659832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3660832d11c5SIlpo Järvinen * 3661832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3662832d11c5SIlpo Järvinen * to have non-paged data as well. 3663832d11c5SIlpo Järvinen * 3664832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3665832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3666832d11c5SIlpo Järvinen */ 3667832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3668832d11c5SIlpo Järvinen { 3669832d11c5SIlpo Järvinen int from, to, merge, todo; 3670d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3671832d11c5SIlpo Järvinen 3672832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3673f8071cdeSEric Dumazet 3674f8071cdeSEric Dumazet if (skb_headlen(skb)) 3675f8071cdeSEric Dumazet return 0; 36761f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 36771f8b977aSWillem de Bruijn return 0; 3678832d11c5SIlpo Järvinen 3679832d11c5SIlpo Järvinen todo = shiftlen; 3680832d11c5SIlpo Järvinen from = 0; 3681832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3682832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3683832d11c5SIlpo Järvinen 3684832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3685832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3686832d11c5SIlpo Järvinen */ 3687832d11c5SIlpo Järvinen if (!to || 3688ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3689b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3690832d11c5SIlpo Järvinen merge = -1; 3691832d11c5SIlpo Järvinen } else { 3692832d11c5SIlpo Järvinen merge = to - 1; 3693832d11c5SIlpo Järvinen 36949e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3695832d11c5SIlpo Järvinen if (todo < 0) { 3696832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3697832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3698832d11c5SIlpo Järvinen return 0; 3699832d11c5SIlpo Järvinen 37009f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 37019f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3702832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3703832d11c5SIlpo Järvinen 37049e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 37059e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3706b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3707832d11c5SIlpo Järvinen 3708832d11c5SIlpo Järvinen goto onlymerged; 3709832d11c5SIlpo Järvinen } 3710832d11c5SIlpo Järvinen 3711832d11c5SIlpo Järvinen from++; 3712832d11c5SIlpo Järvinen } 3713832d11c5SIlpo Järvinen 3714832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3715832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3716832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3717832d11c5SIlpo Järvinen return 0; 3718832d11c5SIlpo Järvinen 3719832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3720832d11c5SIlpo Järvinen return 0; 3721832d11c5SIlpo Järvinen 3722832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3723832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3724832d11c5SIlpo Järvinen return 0; 3725832d11c5SIlpo Järvinen 3726832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3727832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3728832d11c5SIlpo Järvinen 37299e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3730832d11c5SIlpo Järvinen *fragto = *fragfrom; 37319e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3732832d11c5SIlpo Järvinen from++; 3733832d11c5SIlpo Järvinen to++; 3734832d11c5SIlpo Järvinen 3735832d11c5SIlpo Järvinen } else { 3736ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3737b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3738b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 37399e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3740832d11c5SIlpo Järvinen 3741b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 37429e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3743832d11c5SIlpo Järvinen todo = 0; 3744832d11c5SIlpo Järvinen 3745832d11c5SIlpo Järvinen to++; 3746832d11c5SIlpo Järvinen break; 3747832d11c5SIlpo Järvinen } 3748832d11c5SIlpo Järvinen } 3749832d11c5SIlpo Järvinen 3750832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3751832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3752832d11c5SIlpo Järvinen 3753832d11c5SIlpo Järvinen if (merge >= 0) { 3754832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3755832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3756832d11c5SIlpo Järvinen 37579e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 37586a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3759832d11c5SIlpo Järvinen } 3760832d11c5SIlpo Järvinen 3761832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3762832d11c5SIlpo Järvinen to = 0; 3763832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3764832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3765832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3766832d11c5SIlpo Järvinen 3767832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3768832d11c5SIlpo Järvinen 3769832d11c5SIlpo Järvinen onlymerged: 3770832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3771832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3772832d11c5SIlpo Järvinen */ 3773832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3774832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3775832d11c5SIlpo Järvinen 3776ede57d58SRichard Gobert skb_len_add(skb, -shiftlen); 3777ede57d58SRichard Gobert skb_len_add(tgt, shiftlen); 3778832d11c5SIlpo Järvinen 3779832d11c5SIlpo Järvinen return shiftlen; 3780832d11c5SIlpo Järvinen } 3781832d11c5SIlpo Järvinen 3782677e90edSThomas Graf /** 3783677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3784677e90edSThomas Graf * @skb: the buffer to read 3785677e90edSThomas Graf * @from: lower offset of data to be read 3786677e90edSThomas Graf * @to: upper offset of data to be read 3787677e90edSThomas Graf * @st: state variable 3788677e90edSThomas Graf * 3789677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3790677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3791677e90edSThomas Graf */ 3792677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3793677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3794677e90edSThomas Graf { 3795677e90edSThomas Graf st->lower_offset = from; 3796677e90edSThomas Graf st->upper_offset = to; 3797677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3798677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3799677e90edSThomas Graf st->frag_data = NULL; 380097550f6fSWillem de Bruijn st->frag_off = 0; 3801677e90edSThomas Graf } 3802b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3803677e90edSThomas Graf 3804677e90edSThomas Graf /** 3805677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3806677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3807677e90edSThomas Graf * @data: destination pointer for data to be returned 3808677e90edSThomas Graf * @st: state variable 3809677e90edSThomas Graf * 3810bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3811677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3812bc32383cSMathias Krause * the head of the data block to @data and returns the length 3813677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3814677e90edSThomas Graf * offset has been reached. 3815677e90edSThomas Graf * 3816677e90edSThomas Graf * The caller is not required to consume all of the data 3817bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3818677e90edSThomas Graf * of bytes already consumed and the next call to 3819677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3820677e90edSThomas Graf * 382125985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3822e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3823677e90edSThomas Graf * reads of potentially non linear data. 3824677e90edSThomas Graf * 3825bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3826677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3827677e90edSThomas Graf * a stack for this purpose. 3828677e90edSThomas Graf */ 3829677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3830677e90edSThomas Graf struct skb_seq_state *st) 3831677e90edSThomas Graf { 3832677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3833677e90edSThomas Graf skb_frag_t *frag; 3834677e90edSThomas Graf 3835aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3836aeb193eaSWedson Almeida Filho if (st->frag_data) { 3837aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3838aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3839aeb193eaSWedson Almeida Filho } 3840677e90edSThomas Graf return 0; 3841aeb193eaSWedson Almeida Filho } 3842677e90edSThomas Graf 3843677e90edSThomas Graf next_skb: 384495e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3845677e90edSThomas Graf 3846995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 384795e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3848677e90edSThomas Graf return block_limit - abs_offset; 3849677e90edSThomas Graf } 3850677e90edSThomas Graf 3851677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3852677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3853677e90edSThomas Graf 3854677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 385597550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 3856677e90edSThomas Graf 385797550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 385897550f6fSWillem de Bruijn 385997550f6fSWillem de Bruijn pg_idx = 0; 386097550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 386197550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 386297550f6fSWillem de Bruijn 386397550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 386497550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 386597550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 386697550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 386797550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 386897550f6fSWillem de Bruijn } 386997550f6fSWillem de Bruijn 387097550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 3871677e90edSThomas Graf if (abs_offset < block_limit) { 3872677e90edSThomas Graf if (!st->frag_data) 387397550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3874677e90edSThomas Graf 387597550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 3876677e90edSThomas Graf (abs_offset - st->stepped_offset); 3877677e90edSThomas Graf 3878677e90edSThomas Graf return block_limit - abs_offset; 3879677e90edSThomas Graf } 3880677e90edSThomas Graf 3881677e90edSThomas Graf if (st->frag_data) { 388251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3883677e90edSThomas Graf st->frag_data = NULL; 3884677e90edSThomas Graf } 3885677e90edSThomas Graf 388697550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 388797550f6fSWillem de Bruijn st->frag_off += pg_sz; 388897550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 388997550f6fSWillem de Bruijn st->frag_off = 0; 3890677e90edSThomas Graf st->frag_idx++; 389197550f6fSWillem de Bruijn } 3892677e90edSThomas Graf } 3893677e90edSThomas Graf 38945b5a60daSOlaf Kirch if (st->frag_data) { 389551c56b00SEric Dumazet kunmap_atomic(st->frag_data); 38965b5a60daSOlaf Kirch st->frag_data = NULL; 38975b5a60daSOlaf Kirch } 38985b5a60daSOlaf Kirch 389921dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3900677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 390195e3b24cSHerbert Xu st->frag_idx = 0; 3902677e90edSThomas Graf goto next_skb; 390371b3346dSShyam Iyer } else if (st->cur_skb->next) { 390471b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 390571b3346dSShyam Iyer st->frag_idx = 0; 3906677e90edSThomas Graf goto next_skb; 3907677e90edSThomas Graf } 3908677e90edSThomas Graf 3909677e90edSThomas Graf return 0; 3910677e90edSThomas Graf } 3911b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3912677e90edSThomas Graf 3913677e90edSThomas Graf /** 3914677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3915677e90edSThomas Graf * @st: state variable 3916677e90edSThomas Graf * 3917677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3918677e90edSThomas Graf * returned 0. 3919677e90edSThomas Graf */ 3920677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3921677e90edSThomas Graf { 3922677e90edSThomas Graf if (st->frag_data) 392351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3924677e90edSThomas Graf } 3925b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3926677e90edSThomas Graf 39273fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 39283fc7e8a6SThomas Graf 39293fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 39303fc7e8a6SThomas Graf struct ts_config *conf, 39313fc7e8a6SThomas Graf struct ts_state *state) 39323fc7e8a6SThomas Graf { 39333fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 39343fc7e8a6SThomas Graf } 39353fc7e8a6SThomas Graf 39363fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 39373fc7e8a6SThomas Graf { 39383fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 39393fc7e8a6SThomas Graf } 39403fc7e8a6SThomas Graf 39413fc7e8a6SThomas Graf /** 39423fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 39433fc7e8a6SThomas Graf * @skb: the buffer to look in 39443fc7e8a6SThomas Graf * @from: search offset 39453fc7e8a6SThomas Graf * @to: search limit 39463fc7e8a6SThomas Graf * @config: textsearch configuration 39473fc7e8a6SThomas Graf * 39483fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 39493fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 39503fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 39513fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 39523fc7e8a6SThomas Graf */ 39533fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3954059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 39553fc7e8a6SThomas Graf { 3956059a2440SBojan Prtvar struct ts_state state; 3957f72b948dSPhil Oester unsigned int ret; 3958f72b948dSPhil Oester 3959b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 3960b228c9b0SWillem de Bruijn 39613fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 39623fc7e8a6SThomas Graf config->finish = skb_ts_finish; 39633fc7e8a6SThomas Graf 3964059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 39653fc7e8a6SThomas Graf 3966059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3967f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 39683fc7e8a6SThomas Graf } 3969b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 39703fc7e8a6SThomas Graf 3971be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3972be12a1feSHannes Frederic Sowa int offset, size_t size) 3973be12a1feSHannes Frederic Sowa { 3974be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3975be12a1feSHannes Frederic Sowa 3976be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3977be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3978be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3979753f1ca4SPavel Begunkov skb_zcopy_downgrade_managed(skb); 3980be12a1feSHannes Frederic Sowa get_page(page); 3981be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3982be12a1feSHannes Frederic Sowa } else { 3983be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3984be12a1feSHannes Frederic Sowa } 3985be12a1feSHannes Frederic Sowa 3986be12a1feSHannes Frederic Sowa return 0; 3987be12a1feSHannes Frederic Sowa } 3988be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3989be12a1feSHannes Frederic Sowa 3990cbb042f9SHerbert Xu /** 3991cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3992cbb042f9SHerbert Xu * @skb: buffer to update 3993cbb042f9SHerbert Xu * @len: length of data pulled 3994cbb042f9SHerbert Xu * 3995cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3996fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 399784fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 399884fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 399984fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 4000cbb042f9SHerbert Xu */ 4001af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4002cbb042f9SHerbert Xu { 400331b33dfbSPravin B Shelar unsigned char *data = skb->data; 400431b33dfbSPravin B Shelar 4005cbb042f9SHerbert Xu BUG_ON(len > skb->len); 400631b33dfbSPravin B Shelar __skb_pull(skb, len); 400731b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 400831b33dfbSPravin B Shelar return skb->data; 4009cbb042f9SHerbert Xu } 4010f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4011f94691acSArnaldo Carvalho de Melo 401213acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 401313acc94eSYonghong Song { 401413acc94eSYonghong Song skb_frag_t head_frag; 401513acc94eSYonghong Song struct page *page; 401613acc94eSYonghong Song 401713acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 4018d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 4019b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 4020b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 4021d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 402213acc94eSYonghong Song return head_frag; 402313acc94eSYonghong Song } 402413acc94eSYonghong Song 40253a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 40263a1296a3SSteffen Klassert netdev_features_t features, 40273a1296a3SSteffen Klassert unsigned int offset) 40283a1296a3SSteffen Klassert { 40293a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 40303a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 40313a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 40323a1296a3SSteffen Klassert unsigned int delta_len = 0; 40333a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 403453475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 4035cf3ab8d4SLina Wang int len_diff, err; 40363a1296a3SSteffen Klassert 40373a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 40383a1296a3SSteffen Klassert 40393a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 40403a1296a3SSteffen Klassert 40413a1296a3SSteffen Klassert do { 40423a1296a3SSteffen Klassert nskb = list_skb; 40433a1296a3SSteffen Klassert list_skb = list_skb->next; 40443a1296a3SSteffen Klassert 404553475c5dSDongseok Yi err = 0; 4046224102deSlena wang delta_truesize += nskb->truesize; 404753475c5dSDongseok Yi if (skb_shared(nskb)) { 404853475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 404953475c5dSDongseok Yi if (tmp) { 405053475c5dSDongseok Yi consume_skb(nskb); 405153475c5dSDongseok Yi nskb = tmp; 405253475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 405353475c5dSDongseok Yi } else { 405453475c5dSDongseok Yi err = -ENOMEM; 405553475c5dSDongseok Yi } 405653475c5dSDongseok Yi } 405753475c5dSDongseok Yi 40583a1296a3SSteffen Klassert if (!tail) 40593a1296a3SSteffen Klassert skb->next = nskb; 40603a1296a3SSteffen Klassert else 40613a1296a3SSteffen Klassert tail->next = nskb; 40623a1296a3SSteffen Klassert 406353475c5dSDongseok Yi if (unlikely(err)) { 406453475c5dSDongseok Yi nskb->next = list_skb; 406553475c5dSDongseok Yi goto err_linearize; 406653475c5dSDongseok Yi } 406753475c5dSDongseok Yi 40683a1296a3SSteffen Klassert tail = nskb; 40693a1296a3SSteffen Klassert 40703a1296a3SSteffen Klassert delta_len += nskb->len; 40713a1296a3SSteffen Klassert 40723a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 40733a1296a3SSteffen Klassert 4074cf673ed0SFlorian Westphal skb_release_head_state(nskb); 4075cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 40763a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 40773a1296a3SSteffen Klassert 40783a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4079cf3ab8d4SLina Wang nskb->transport_header += len_diff; 40803a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 40813a1296a3SSteffen Klassert nskb->data - tnl_hlen, 40823a1296a3SSteffen Klassert offset + tnl_hlen); 40833a1296a3SSteffen Klassert 40843a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 40853a1296a3SSteffen Klassert __skb_linearize(nskb)) 40863a1296a3SSteffen Klassert goto err_linearize; 40873a1296a3SSteffen Klassert 40883a1296a3SSteffen Klassert } while (list_skb); 40893a1296a3SSteffen Klassert 40903a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 40913a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 40923a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 40933a1296a3SSteffen Klassert 40943a1296a3SSteffen Klassert skb_gso_reset(skb); 40953a1296a3SSteffen Klassert 40963a1296a3SSteffen Klassert skb->prev = tail; 40973a1296a3SSteffen Klassert 40983a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 40993a1296a3SSteffen Klassert __skb_linearize(skb)) 41003a1296a3SSteffen Klassert goto err_linearize; 41013a1296a3SSteffen Klassert 41023a1296a3SSteffen Klassert skb_get(skb); 41033a1296a3SSteffen Klassert 41043a1296a3SSteffen Klassert return skb; 41053a1296a3SSteffen Klassert 41063a1296a3SSteffen Klassert err_linearize: 41073a1296a3SSteffen Klassert kfree_skb_list(skb->next); 41083a1296a3SSteffen Klassert skb->next = NULL; 41093a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 41103a1296a3SSteffen Klassert } 41113a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 41123a1296a3SSteffen Klassert 4113f4c50d99SHerbert Xu /** 4114f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 4115df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 4116576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 4117f4c50d99SHerbert Xu * 4118f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 41194c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 41204c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 4121f4c50d99SHerbert Xu */ 4122df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 4123df5771ffSMichael S. Tsirkin netdev_features_t features) 4124f4c50d99SHerbert Xu { 4125f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 4126f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 41271a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 4128df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 4129df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 4130df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 41311fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 4132f4c50d99SHerbert Xu unsigned int offset = doffset; 4133df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 4134802ab55aSAlexander Duyck unsigned int partial_segs = 0; 4135f4c50d99SHerbert Xu unsigned int headroom; 4136802ab55aSAlexander Duyck unsigned int len = head_skb->len; 4137ec5f0615SPravin B Shelar __be16 proto; 413836c98382SAlexander Duyck bool csum, sg; 4139df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 4140f4c50d99SHerbert Xu int err = -ENOMEM; 4141f4c50d99SHerbert Xu int i = 0; 4142f4c50d99SHerbert Xu int pos; 4143f4c50d99SHerbert Xu 41443dcbdb13SShmulik Ladkani if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 41453dcbdb13SShmulik Ladkani (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 41463dcbdb13SShmulik Ladkani /* gso_size is untrusted, and we have a frag_list with a linear 41473dcbdb13SShmulik Ladkani * non head_frag head. 41483dcbdb13SShmulik Ladkani * 41493dcbdb13SShmulik Ladkani * (we assume checking the first list_skb member suffices; 41503dcbdb13SShmulik Ladkani * i.e if either of the list_skb members have non head_frag 41513dcbdb13SShmulik Ladkani * head, then the first one has too). 41523dcbdb13SShmulik Ladkani * 41533dcbdb13SShmulik Ladkani * If head_skb's headlen does not fit requested gso_size, it 41543dcbdb13SShmulik Ladkani * means that the frag_list members do NOT terminate on exact 41553dcbdb13SShmulik Ladkani * gso_size boundaries. Hence we cannot perform skb_frag_t page 41563dcbdb13SShmulik Ladkani * sharing. Therefore we must fallback to copying the frag_list 41573dcbdb13SShmulik Ladkani * skbs; we do so by disabling SG. 41583dcbdb13SShmulik Ladkani */ 41593dcbdb13SShmulik Ladkani if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 41603dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 41613dcbdb13SShmulik Ladkani } 41623dcbdb13SShmulik Ladkani 41635882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 41642f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4165ec5f0615SPravin B Shelar if (unlikely(!proto)) 4166ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4167ec5f0615SPravin B Shelar 416836c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4169f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 41707e2b10c1STom Herbert 417107b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 417207b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 417307b26c94SSteffen Klassert struct sk_buff *iter; 417443170c4eSIlan Tayari unsigned int frag_len; 417507b26c94SSteffen Klassert 417607b26c94SSteffen Klassert if (!list_skb || 417707b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 417807b26c94SSteffen Klassert goto normal; 417907b26c94SSteffen Klassert 418043170c4eSIlan Tayari /* If we get here then all the required 418143170c4eSIlan Tayari * GSO features except frag_list are supported. 418243170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 418343170c4eSIlan Tayari * with no frag_list. 418443170c4eSIlan Tayari * Currently we can do that only when the buffers don't 418543170c4eSIlan Tayari * have a linear part and all the buffers except 418643170c4eSIlan Tayari * the last are of the same length. 418707b26c94SSteffen Klassert */ 418843170c4eSIlan Tayari frag_len = list_skb->len; 418907b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 419043170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 419143170c4eSIlan Tayari goto normal; 4192eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 419307b26c94SSteffen Klassert goto normal; 419407b26c94SSteffen Klassert 419507b26c94SSteffen Klassert len -= iter->len; 419607b26c94SSteffen Klassert } 419743170c4eSIlan Tayari 419843170c4eSIlan Tayari if (len != frag_len) 419943170c4eSIlan Tayari goto normal; 420007b26c94SSteffen Klassert } 420107b26c94SSteffen Klassert 4202802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4203802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4204802ab55aSAlexander Duyck * now. 4205802ab55aSAlexander Duyck */ 4206802ab55aSAlexander Duyck partial_segs = len / mss; 4207d7fb5a80SAlexander Duyck if (partial_segs > 1) 4208802ab55aSAlexander Duyck mss *= partial_segs; 4209d7fb5a80SAlexander Duyck else 4210d7fb5a80SAlexander Duyck partial_segs = 0; 4211802ab55aSAlexander Duyck } 4212802ab55aSAlexander Duyck 421307b26c94SSteffen Klassert normal: 4214df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4215df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4216f4c50d99SHerbert Xu 4217f4c50d99SHerbert Xu do { 4218f4c50d99SHerbert Xu struct sk_buff *nskb; 42198cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4220c8884eddSHerbert Xu int hsize; 4221f4c50d99SHerbert Xu int size; 4222f4c50d99SHerbert Xu 42233953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 42243953c46cSMarcelo Ricardo Leitner len = list_skb->len; 42253953c46cSMarcelo Ricardo Leitner } else { 4226df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4227f4c50d99SHerbert Xu if (len > mss) 4228f4c50d99SHerbert Xu len = mss; 42293953c46cSMarcelo Ricardo Leitner } 4230f4c50d99SHerbert Xu 4231df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4232f4c50d99SHerbert Xu 4233dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 42341a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 42351a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 423689319d38SHerbert Xu 42379d8506ccSHerbert Xu i = 0; 42381a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 42391a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 42401fd819ecSMichael S. Tsirkin frag_skb = list_skb; 42411a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 42429d8506ccSHerbert Xu 42439d8506ccSHerbert Xu while (pos < offset + len) { 42449d8506ccSHerbert Xu BUG_ON(i >= nfrags); 42459d8506ccSHerbert Xu 42464e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 42479d8506ccSHerbert Xu if (pos + size > offset + len) 42489d8506ccSHerbert Xu break; 42499d8506ccSHerbert Xu 42509d8506ccSHerbert Xu i++; 42519d8506ccSHerbert Xu pos += size; 42524e1beba1SMichael S. Tsirkin frag++; 42539d8506ccSHerbert Xu } 42549d8506ccSHerbert Xu 42551a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 42561a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 425789319d38SHerbert Xu 4258f4c50d99SHerbert Xu if (unlikely(!nskb)) 4259f4c50d99SHerbert Xu goto err; 4260f4c50d99SHerbert Xu 42619d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 42629d8506ccSHerbert Xu kfree_skb(nskb); 42639d8506ccSHerbert Xu goto err; 42649d8506ccSHerbert Xu } 42659d8506ccSHerbert Xu 4266ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 426789319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 426889319d38SHerbert Xu kfree_skb(nskb); 426989319d38SHerbert Xu goto err; 427089319d38SHerbert Xu } 427189319d38SHerbert Xu 4272ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 427389319d38SHerbert Xu skb_release_head_state(nskb); 427489319d38SHerbert Xu __skb_push(nskb, doffset); 427589319d38SHerbert Xu } else { 427600b229f7SPaolo Abeni if (hsize < 0) 427700b229f7SPaolo Abeni hsize = 0; 4278dbd50f23SXin Long if (hsize > len || !sg) 4279dbd50f23SXin Long hsize = len; 4280dbd50f23SXin Long 4281c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4282df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4283c93bdd0eSMel Gorman NUMA_NO_NODE); 428489319d38SHerbert Xu 428589319d38SHerbert Xu if (unlikely(!nskb)) 428689319d38SHerbert Xu goto err; 428789319d38SHerbert Xu 428889319d38SHerbert Xu skb_reserve(nskb, headroom); 428989319d38SHerbert Xu __skb_put(nskb, doffset); 429089319d38SHerbert Xu } 429189319d38SHerbert Xu 4292f4c50d99SHerbert Xu if (segs) 4293f4c50d99SHerbert Xu tail->next = nskb; 4294f4c50d99SHerbert Xu else 4295f4c50d99SHerbert Xu segs = nskb; 4296f4c50d99SHerbert Xu tail = nskb; 4297f4c50d99SHerbert Xu 4298df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4299f4c50d99SHerbert Xu 4300030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4301fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 430268c33163SPravin B Shelar 4303df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 430468c33163SPravin B Shelar nskb->data - tnl_hlen, 430568c33163SPravin B Shelar doffset + tnl_hlen); 430689319d38SHerbert Xu 43079d8506ccSHerbert Xu if (nskb->len == len + doffset) 43081cdbcb79SSimon Horman goto perform_csum_check; 430989319d38SHerbert Xu 43107fbeffedSAlexander Duyck if (!sg) { 43111454c9faSYadu Kishore if (!csum) { 43127fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 43136f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 431476443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 431576443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 43161454c9faSYadu Kishore skb_put(nskb, 43171454c9faSYadu Kishore len), 43188d5930dfSAl Viro len); 43197e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4320de843723STom Herbert skb_headroom(nskb) + doffset; 43211454c9faSYadu Kishore } else { 4322c624c58eSlily if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len)) 4323c624c58eSlily goto err; 43241454c9faSYadu Kishore } 4325f4c50d99SHerbert Xu continue; 4326f4c50d99SHerbert Xu } 4327f4c50d99SHerbert Xu 43288cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4329f4c50d99SHerbert Xu 4330df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4331d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4332f4c50d99SHerbert Xu 433306b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 433406b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4335cef401deSEric Dumazet 4336bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4337bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4338bf5c25d6SWillem de Bruijn goto err; 4339bf5c25d6SWillem de Bruijn 43409d8506ccSHerbert Xu while (pos < offset + len) { 43419d8506ccSHerbert Xu if (i >= nfrags) { 43429d8506ccSHerbert Xu i = 0; 43431a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 43441a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 43451fd819ecSMichael S. Tsirkin frag_skb = list_skb; 434613acc94eSYonghong Song if (!skb_headlen(list_skb)) { 43479d8506ccSHerbert Xu BUG_ON(!nfrags); 434813acc94eSYonghong Song } else { 434913acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 43509d8506ccSHerbert Xu 435113acc94eSYonghong Song /* to make room for head_frag. */ 435213acc94eSYonghong Song i--; 435313acc94eSYonghong Song frag--; 435413acc94eSYonghong Song } 4355bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4356bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4357bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4358bf5c25d6SWillem de Bruijn goto err; 4359bf5c25d6SWillem de Bruijn 43601a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 43619d8506ccSHerbert Xu } 43629d8506ccSHerbert Xu 43639d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 43649d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 43659d8506ccSHerbert Xu net_warn_ratelimited( 43669d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 43679d8506ccSHerbert Xu pos, mss); 4368ff907a11SEric Dumazet err = -EINVAL; 43699d8506ccSHerbert Xu goto err; 43709d8506ccSHerbert Xu } 43719d8506ccSHerbert Xu 437213acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 43738cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 43748cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4375f4c50d99SHerbert Xu 4376f4c50d99SHerbert Xu if (pos < offset) { 4377b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 43788cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4379f4c50d99SHerbert Xu } 4380f4c50d99SHerbert Xu 438189319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4382f4c50d99SHerbert Xu 4383f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4384f4c50d99SHerbert Xu i++; 43854e1beba1SMichael S. Tsirkin frag++; 4386f4c50d99SHerbert Xu pos += size; 4387f4c50d99SHerbert Xu } else { 43888cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 438989319d38SHerbert Xu goto skip_fraglist; 4390f4c50d99SHerbert Xu } 4391f4c50d99SHerbert Xu 43928cb19905SMichael S. Tsirkin nskb_frag++; 4393f4c50d99SHerbert Xu } 4394f4c50d99SHerbert Xu 439589319d38SHerbert Xu skip_fraglist: 4396f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4397f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4398f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4399ec5f0615SPravin B Shelar 44001cdbcb79SSimon Horman perform_csum_check: 44017fbeffedSAlexander Duyck if (!csum) { 4402ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4403ff907a11SEric Dumazet __skb_linearize(nskb)) 4404ddff00d4SAlexander Duyck goto err; 4405ff907a11SEric Dumazet 44067fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4407ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 440876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 440976443456SAlexander Duyck skb_checksum(nskb, doffset, 441076443456SAlexander Duyck nskb->len - doffset, 0); 44117e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 44127e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4413ec5f0615SPravin B Shelar } 4414df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4415f4c50d99SHerbert Xu 4416bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4417bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4418bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4419bec3cfdcSEric Dumazet */ 4420bec3cfdcSEric Dumazet segs->prev = tail; 4421432c856fSToshiaki Makita 4422802ab55aSAlexander Duyck if (partial_segs) { 442307b26c94SSteffen Klassert struct sk_buff *iter; 4424802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 442507b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4426802ab55aSAlexander Duyck 4427802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 442807b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4429802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4430802ab55aSAlexander Duyck 4431802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4432802ab55aSAlexander Duyck * our way back down the stack of protocols. 4433802ab55aSAlexander Duyck */ 443407b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 443507b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 443607b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 443707b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 443807b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 443907b26c94SSteffen Klassert } 444007b26c94SSteffen Klassert 444107b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 444207b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 444307b26c94SSteffen Klassert else if (tail != segs) 444407b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4445802ab55aSAlexander Duyck } 4446802ab55aSAlexander Duyck 4447432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4448432c856fSToshiaki Makita * using skb_set_owner_w(). 4449432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4450432c856fSToshiaki Makita */ 4451432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4452432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4453432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4454432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4455432c856fSToshiaki Makita } 4456f4c50d99SHerbert Xu return segs; 4457f4c50d99SHerbert Xu 4458f4c50d99SHerbert Xu err: 4459289dccbeSEric Dumazet kfree_skb_list(segs); 4460f4c50d99SHerbert Xu return ERR_PTR(err); 4461f4c50d99SHerbert Xu } 4462f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4463f4c50d99SHerbert Xu 4464df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4465df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4466df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4467df5042f4SFlorian Westphal 4468df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4469df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4470df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4471df5042f4SFlorian Westphal #endif 44724165079bSFlorian Westphal #ifdef CONFIG_XFRM 44734165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 44744165079bSFlorian Westphal #endif 447595a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 447695a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 447795a7233cSPaul Blakey #endif 44783ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 44793ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 44803ee17bc7SMat Martineau #endif 448178476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 448278476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 448378476d31SJeremy Kerr #endif 4484df5042f4SFlorian Westphal }; 4485df5042f4SFlorian Westphal 4486df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4487df5042f4SFlorian Westphal { 4488df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4489df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4490df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4491df5042f4SFlorian Westphal #endif 44924165079bSFlorian Westphal #ifdef CONFIG_XFRM 44934165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 44944165079bSFlorian Westphal #endif 449595a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 449695a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 449795a7233cSPaul Blakey #endif 44983ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 44993ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 45003ee17bc7SMat Martineau #endif 450178476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 450278476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 450378476d31SJeremy Kerr #endif 4504df5042f4SFlorian Westphal 0; 4505df5042f4SFlorian Westphal } 4506df5042f4SFlorian Westphal 4507df5042f4SFlorian Westphal static void skb_extensions_init(void) 4508df5042f4SFlorian Westphal { 4509df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4510df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4511df5042f4SFlorian Westphal 4512df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4513df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4514df5042f4SFlorian Westphal 0, 4515df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4516df5042f4SFlorian Westphal NULL); 4517df5042f4SFlorian Westphal } 4518df5042f4SFlorian Westphal #else 4519df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4520df5042f4SFlorian Westphal #endif 4521df5042f4SFlorian Westphal 45221da177e4SLinus Torvalds void __init skb_init(void) 45231da177e4SLinus Torvalds { 452479a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 45251da177e4SLinus Torvalds sizeof(struct sk_buff), 45261da177e4SLinus Torvalds 0, 4527e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 452879a8a642SKees Cook offsetof(struct sk_buff, cb), 452979a8a642SKees Cook sizeof_field(struct sk_buff, cb), 453020c2df83SPaul Mundt NULL); 4531d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4532d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4533d179cd12SDavid S. Miller 0, 4534e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 453520c2df83SPaul Mundt NULL); 4536df5042f4SFlorian Westphal skb_extensions_init(); 45371da177e4SLinus Torvalds } 45381da177e4SLinus Torvalds 453951c739d1SDavid S. Miller static int 454048a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 454148a1df65SJason A. Donenfeld unsigned int recursion_level) 4542716ea3a7SDavid Howells { 45431a028e50SDavid S. Miller int start = skb_headlen(skb); 45441a028e50SDavid S. Miller int i, copy = start - offset; 4545fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4546716ea3a7SDavid Howells int elt = 0; 4547716ea3a7SDavid Howells 454848a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 454948a1df65SJason A. Donenfeld return -EMSGSIZE; 455048a1df65SJason A. Donenfeld 4551716ea3a7SDavid Howells if (copy > 0) { 4552716ea3a7SDavid Howells if (copy > len) 4553716ea3a7SDavid Howells copy = len; 4554642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4555716ea3a7SDavid Howells elt++; 4556716ea3a7SDavid Howells if ((len -= copy) == 0) 4557716ea3a7SDavid Howells return elt; 4558716ea3a7SDavid Howells offset += copy; 4559716ea3a7SDavid Howells } 4560716ea3a7SDavid Howells 4561716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 45621a028e50SDavid S. Miller int end; 4563716ea3a7SDavid Howells 4564547b792cSIlpo Järvinen WARN_ON(start > offset + len); 45651a028e50SDavid S. Miller 45669e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4567716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4568716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 456948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 457048a1df65SJason A. Donenfeld return -EMSGSIZE; 4571716ea3a7SDavid Howells 4572716ea3a7SDavid Howells if (copy > len) 4573716ea3a7SDavid Howells copy = len; 4574ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4575b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4576716ea3a7SDavid Howells elt++; 4577716ea3a7SDavid Howells if (!(len -= copy)) 4578716ea3a7SDavid Howells return elt; 4579716ea3a7SDavid Howells offset += copy; 4580716ea3a7SDavid Howells } 45811a028e50SDavid S. Miller start = end; 4582716ea3a7SDavid Howells } 4583716ea3a7SDavid Howells 4584fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 458548a1df65SJason A. Donenfeld int end, ret; 4586716ea3a7SDavid Howells 4587547b792cSIlpo Järvinen WARN_ON(start > offset + len); 45881a028e50SDavid S. Miller 4589fbb398a8SDavid S. Miller end = start + frag_iter->len; 4590716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 459148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 459248a1df65SJason A. Donenfeld return -EMSGSIZE; 459348a1df65SJason A. Donenfeld 4594716ea3a7SDavid Howells if (copy > len) 4595716ea3a7SDavid Howells copy = len; 459648a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 459748a1df65SJason A. Donenfeld copy, recursion_level + 1); 459848a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 459948a1df65SJason A. Donenfeld return ret; 460048a1df65SJason A. Donenfeld elt += ret; 4601716ea3a7SDavid Howells if ((len -= copy) == 0) 4602716ea3a7SDavid Howells return elt; 4603716ea3a7SDavid Howells offset += copy; 4604716ea3a7SDavid Howells } 46051a028e50SDavid S. Miller start = end; 4606716ea3a7SDavid Howells } 4607716ea3a7SDavid Howells BUG_ON(len); 4608716ea3a7SDavid Howells return elt; 4609716ea3a7SDavid Howells } 4610716ea3a7SDavid Howells 461148a1df65SJason A. Donenfeld /** 461248a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 461348a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 461448a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 461548a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 461648a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 461748a1df65SJason A. Donenfeld * 461848a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 461948a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 462048a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 462148a1df65SJason A. Donenfeld * could not fit. 462248a1df65SJason A. Donenfeld */ 462348a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 462448a1df65SJason A. Donenfeld { 462548a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 462648a1df65SJason A. Donenfeld 462748a1df65SJason A. Donenfeld if (nsg <= 0) 462848a1df65SJason A. Donenfeld return nsg; 462948a1df65SJason A. Donenfeld 463048a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 463148a1df65SJason A. Donenfeld 463248a1df65SJason A. Donenfeld return nsg; 463348a1df65SJason A. Donenfeld } 463448a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 463548a1df65SJason A. Donenfeld 463625a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 463725a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 463825a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 463925a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 464025a91d8dSFan Du * 464125a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 464225a91d8dSFan Du * 1. sg_init_table 464325a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 464425a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 464525a91d8dSFan Du * 464625a91d8dSFan Du * This is equivalent to: 464725a91d8dSFan Du * 1. sg_init_table 464825a91d8dSFan Du * 2. skb_to_sgvec(payload1) 464925a91d8dSFan Du * 3. sg_unmark_end 465025a91d8dSFan Du * 4. skb_to_sgvec(payload2) 465125a91d8dSFan Du * 465225a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 465325a91d8dSFan Du * is more preferable. 465425a91d8dSFan Du */ 465525a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 465625a91d8dSFan Du int offset, int len) 465725a91d8dSFan Du { 465848a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 465925a91d8dSFan Du } 466025a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 466125a91d8dSFan Du 466251c739d1SDavid S. Miller 466351c739d1SDavid S. Miller 4664716ea3a7SDavid Howells /** 4665716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4666716ea3a7SDavid Howells * @skb: The socket buffer to check. 4667716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4668716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4669716ea3a7SDavid Howells * 4670716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4671716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4672716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4673716ea3a7SDavid Howells * 4674716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4675716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4676716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4677716ea3a7SDavid Howells * 4678716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4679716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4680716ea3a7SDavid Howells */ 4681716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4682716ea3a7SDavid Howells { 4683716ea3a7SDavid Howells int copyflag; 4684716ea3a7SDavid Howells int elt; 4685716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4686716ea3a7SDavid Howells 4687716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4688716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4689716ea3a7SDavid Howells * at the moment even if they are anonymous). 4690716ea3a7SDavid Howells */ 4691716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4692c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4693716ea3a7SDavid Howells return -ENOMEM; 4694716ea3a7SDavid Howells 4695716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 469621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4697716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4698716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4699716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4700716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4701716ea3a7SDavid Howells 4702716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4703716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4704716ea3a7SDavid Howells return -ENOMEM; 4705716ea3a7SDavid Howells 4706716ea3a7SDavid Howells /* Voila! */ 4707716ea3a7SDavid Howells *trailer = skb; 4708716ea3a7SDavid Howells return 1; 4709716ea3a7SDavid Howells } 4710716ea3a7SDavid Howells 4711716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4712716ea3a7SDavid Howells 4713716ea3a7SDavid Howells elt = 1; 4714716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4715716ea3a7SDavid Howells copyflag = 0; 4716716ea3a7SDavid Howells 4717716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4718716ea3a7SDavid Howells int ntail = 0; 4719716ea3a7SDavid Howells 4720716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4721716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4722716ea3a7SDavid Howells * after it. */ 4723716ea3a7SDavid Howells 4724716ea3a7SDavid Howells if (skb_shared(skb1)) 4725716ea3a7SDavid Howells copyflag = 1; 4726716ea3a7SDavid Howells 4727716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4728716ea3a7SDavid Howells 4729716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4730716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 473121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4732716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4733716ea3a7SDavid Howells ntail = tailbits + 128; 4734716ea3a7SDavid Howells } 4735716ea3a7SDavid Howells 4736716ea3a7SDavid Howells if (copyflag || 4737716ea3a7SDavid Howells skb_cloned(skb1) || 4738716ea3a7SDavid Howells ntail || 4739716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 474021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4741716ea3a7SDavid Howells struct sk_buff *skb2; 4742716ea3a7SDavid Howells 4743716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4744716ea3a7SDavid Howells if (ntail == 0) 4745716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4746716ea3a7SDavid Howells else 4747716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4748716ea3a7SDavid Howells skb_headroom(skb1), 4749716ea3a7SDavid Howells ntail, 4750716ea3a7SDavid Howells GFP_ATOMIC); 4751716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4752716ea3a7SDavid Howells return -ENOMEM; 4753716ea3a7SDavid Howells 4754716ea3a7SDavid Howells if (skb1->sk) 4755716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4756716ea3a7SDavid Howells 4757716ea3a7SDavid Howells /* Looking around. Are we still alive? 4758716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4759716ea3a7SDavid Howells 4760716ea3a7SDavid Howells skb2->next = skb1->next; 4761716ea3a7SDavid Howells *skb_p = skb2; 4762716ea3a7SDavid Howells kfree_skb(skb1); 4763716ea3a7SDavid Howells skb1 = skb2; 4764716ea3a7SDavid Howells } 4765716ea3a7SDavid Howells elt++; 4766716ea3a7SDavid Howells *trailer = skb1; 4767716ea3a7SDavid Howells skb_p = &skb1->next; 4768716ea3a7SDavid Howells } 4769716ea3a7SDavid Howells 4770716ea3a7SDavid Howells return elt; 4771716ea3a7SDavid Howells } 4772b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4773716ea3a7SDavid Howells 4774b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4775b1faf566SEric Dumazet { 4776b1faf566SEric Dumazet struct sock *sk = skb->sk; 4777b1faf566SEric Dumazet 4778b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4779b1faf566SEric Dumazet } 4780b1faf566SEric Dumazet 47818605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 47828605330aSSoheil Hassas Yeganeh { 47838605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 47848605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 47858605330aSSoheil Hassas Yeganeh */ 47868605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 47878605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 47888605330aSSoheil Hassas Yeganeh } 47898605330aSSoheil Hassas Yeganeh 4790b1faf566SEric Dumazet /* 4791b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4792b1faf566SEric Dumazet */ 4793b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4794b1faf566SEric Dumazet { 4795b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4796ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4797b1faf566SEric Dumazet return -ENOMEM; 4798b1faf566SEric Dumazet 4799b1faf566SEric Dumazet skb_orphan(skb); 4800b1faf566SEric Dumazet skb->sk = sk; 4801b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4802b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 48038605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4804b1faf566SEric Dumazet 4805abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4806abb57ea4SEric Dumazet skb_dst_force(skb); 4807abb57ea4SEric Dumazet 4808b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4809b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4810e3ae2365SAlexander Aring sk_error_report(sk); 4811b1faf566SEric Dumazet return 0; 4812b1faf566SEric Dumazet } 4813b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4814b1faf566SEric Dumazet 481583a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 481683a1a1a7SSoheil Hassas Yeganeh { 481783a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 481883a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 481983a1a1a7SSoheil Hassas Yeganeh } 482083a1a1a7SSoheil Hassas Yeganeh 4821364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4822364a9e93SWillem de Bruijn { 4823364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 482483a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 482583a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4826997d5c3fSEric Dumazet unsigned long flags; 4827364a9e93SWillem de Bruijn 4828997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4829364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 483038b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 483183a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 483238b25793SSoheil Hassas Yeganeh if (icmp_next) 4833985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 483438b25793SSoheil Hassas Yeganeh } 4835997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4836364a9e93SWillem de Bruijn 483783a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 483883a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 483983a1a1a7SSoheil Hassas Yeganeh 484083a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4841e3ae2365SAlexander Aring sk_error_report(sk); 4842364a9e93SWillem de Bruijn 4843364a9e93SWillem de Bruijn return skb; 4844364a9e93SWillem de Bruijn } 4845364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4846364a9e93SWillem de Bruijn 4847cab41c47SAlexander Duyck /** 4848cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4849cab41c47SAlexander Duyck * @skb: the skb to clone 4850cab41c47SAlexander Duyck * 4851cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4852cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4853cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4854cab41c47SAlexander Duyck * 4855cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4856cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4857cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4858cab41c47SAlexander Duyck * the sk_error_queue. 4859cab41c47SAlexander Duyck */ 486062bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 486162bccb8cSAlexander Duyck { 486262bccb8cSAlexander Duyck struct sock *sk = skb->sk; 486362bccb8cSAlexander Duyck struct sk_buff *clone; 486462bccb8cSAlexander Duyck 486541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 486662bccb8cSAlexander Duyck return NULL; 486762bccb8cSAlexander Duyck 486862bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 486962bccb8cSAlexander Duyck if (!clone) { 487062bccb8cSAlexander Duyck sock_put(sk); 487162bccb8cSAlexander Duyck return NULL; 487262bccb8cSAlexander Duyck } 487362bccb8cSAlexander Duyck 487462bccb8cSAlexander Duyck clone->sk = sk; 487562bccb8cSAlexander Duyck clone->destructor = sock_efree; 487662bccb8cSAlexander Duyck 487762bccb8cSAlexander Duyck return clone; 487862bccb8cSAlexander Duyck } 487962bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 488062bccb8cSAlexander Duyck 488137846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 488237846ef0SAlexander Duyck struct sock *sk, 48834ef1b286SSoheil Hassas Yeganeh int tstype, 48844ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4885ac45f602SPatrick Ohly { 4886ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4887ac45f602SPatrick Ohly int err; 4888ac45f602SPatrick Ohly 48894ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 48904ef1b286SSoheil Hassas Yeganeh 4891ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4892ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4893ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4894ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4895e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 48964ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 48971862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 48984ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 489909c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 490042f67eeaSEric Dumazet if (sk_is_tcp(sk)) 4901a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 49024ed2d765SWillem de Bruijn } 490329030374SEric Dumazet 4904ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 490529030374SEric Dumazet 4906ac45f602SPatrick Ohly if (err) 4907ac45f602SPatrick Ohly kfree_skb(skb); 4908ac45f602SPatrick Ohly } 490937846ef0SAlexander Duyck 4910b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4911b245be1fSWillem de Bruijn { 4912b245be1fSWillem de Bruijn bool ret; 4913b245be1fSWillem de Bruijn 4914d2154b0aSKuniyuki Iwashima if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly)) 4915b245be1fSWillem de Bruijn return true; 4916b245be1fSWillem de Bruijn 4917b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4918b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4919b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4920b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4921b245be1fSWillem de Bruijn return ret; 4922b245be1fSWillem de Bruijn } 4923b245be1fSWillem de Bruijn 492437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 492537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 492637846ef0SAlexander Duyck { 492737846ef0SAlexander Duyck struct sock *sk = skb->sk; 492837846ef0SAlexander Duyck 4929b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 493035b99dffSWillem de Bruijn goto err; 4931b245be1fSWillem de Bruijn 49329ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 49339ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 49349ac25fc0SEric Dumazet */ 493541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 493637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 49374ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 493837846ef0SAlexander Duyck sock_put(sk); 493935b99dffSWillem de Bruijn return; 494037846ef0SAlexander Duyck } 494135b99dffSWillem de Bruijn 494235b99dffSWillem de Bruijn err: 494335b99dffSWillem de Bruijn kfree_skb(skb); 49449ac25fc0SEric Dumazet } 494537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 494637846ef0SAlexander Duyck 494737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 4948e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 494937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 495037846ef0SAlexander Duyck struct sock *sk, int tstype) 495137846ef0SAlexander Duyck { 495237846ef0SAlexander Duyck struct sk_buff *skb; 49534ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 495437846ef0SAlexander Duyck 49553a8dd971SWillem de Bruijn if (!sk) 49563a8dd971SWillem de Bruijn return; 49573a8dd971SWillem de Bruijn 4958b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4959b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4960b50a5c70SMiroslav Lichvar return; 4961b50a5c70SMiroslav Lichvar 49623a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 49633a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 496437846ef0SAlexander Duyck return; 496537846ef0SAlexander Duyck 49661c885808SFrancis Yan if (tsonly) { 49671c885808SFrancis Yan #ifdef CONFIG_INET 49681c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 496942f67eeaSEric Dumazet sk_is_tcp(sk)) { 4970e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 4971e7ed11eeSYousuk Seung ack_skb); 49724ef1b286SSoheil Hassas Yeganeh opt_stats = true; 49734ef1b286SSoheil Hassas Yeganeh } else 49741c885808SFrancis Yan #endif 49751c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 49761c885808SFrancis Yan } else { 497737846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 49781c885808SFrancis Yan } 497937846ef0SAlexander Duyck if (!skb) 498037846ef0SAlexander Duyck return; 498137846ef0SAlexander Duyck 498249ca0d8bSWillem de Bruijn if (tsonly) { 4983fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4984fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 498549ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 498649ca0d8bSWillem de Bruijn } 498749ca0d8bSWillem de Bruijn 498849ca0d8bSWillem de Bruijn if (hwtstamps) 498949ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 499049ca0d8bSWillem de Bruijn else 4991d93376f5SMartin KaFai Lau __net_timestamp(skb); 499249ca0d8bSWillem de Bruijn 49934ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 499437846ef0SAlexander Duyck } 4995e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4996e7fd2885SWillem de Bruijn 4997e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4998e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4999e7fd2885SWillem de Bruijn { 5000e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 5001e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 5002e7fd2885SWillem de Bruijn } 5003ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 5004ac45f602SPatrick Ohly 50056e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 50066e3e939fSJohannes Berg { 50076e3e939fSJohannes Berg struct sock *sk = skb->sk; 50086e3e939fSJohannes Berg struct sock_exterr_skb *serr; 5009dd4f1072SEric Dumazet int err = 1; 50106e3e939fSJohannes Berg 50116e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 50126e3e939fSJohannes Berg skb->wifi_acked = acked; 50136e3e939fSJohannes Berg 50146e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 50156e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 50166e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 50176e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 50186e3e939fSJohannes Berg 5019dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 5020dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 5021dd4f1072SEric Dumazet */ 502241c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 50236e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 5024dd4f1072SEric Dumazet sock_put(sk); 5025dd4f1072SEric Dumazet } 50266e3e939fSJohannes Berg if (err) 50276e3e939fSJohannes Berg kfree_skb(skb); 50286e3e939fSJohannes Berg } 50296e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 50306e3e939fSJohannes Berg 5031f35d9d8aSRusty Russell /** 5032f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 5033f35d9d8aSRusty Russell * @skb: the skb to set 5034f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 5035f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 5036f35d9d8aSRusty Russell * 5037f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 5038f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 5039f35d9d8aSRusty Russell * 5040f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 5041f35d9d8aSRusty Russell * returns false you should drop the packet. 5042f35d9d8aSRusty Russell */ 5043f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 5044f35d9d8aSRusty Russell { 504552b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 504652b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 504752b5d6f5SEric Dumazet 504852b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 504952b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 505052b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 5051f35d9d8aSRusty Russell return false; 5052f35d9d8aSRusty Russell } 5053f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 505452b5d6f5SEric Dumazet skb->csum_start = csum_start; 5055f35d9d8aSRusty Russell skb->csum_offset = off; 5056e5d5decaSJason Wang skb_set_transport_header(skb, start); 5057f35d9d8aSRusty Russell return true; 5058f35d9d8aSRusty Russell } 5059b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 5060f35d9d8aSRusty Russell 5061ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 5062ed1f50c3SPaul Durrant unsigned int max) 5063ed1f50c3SPaul Durrant { 5064ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 5065ed1f50c3SPaul Durrant return 0; 5066ed1f50c3SPaul Durrant 5067ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 5068ed1f50c3SPaul Durrant * won't need to do it again. 5069ed1f50c3SPaul Durrant */ 5070ed1f50c3SPaul Durrant if (max > skb->len) 5071ed1f50c3SPaul Durrant max = skb->len; 5072ed1f50c3SPaul Durrant 5073ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 5074ed1f50c3SPaul Durrant return -ENOMEM; 5075ed1f50c3SPaul Durrant 5076ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 5077ed1f50c3SPaul Durrant return -EPROTO; 5078ed1f50c3SPaul Durrant 5079ed1f50c3SPaul Durrant return 0; 5080ed1f50c3SPaul Durrant } 5081ed1f50c3SPaul Durrant 5082f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 5083f9708b43SJan Beulich 5084f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 5085f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 5086f9708b43SJan Beulich unsigned int off) 5087f9708b43SJan Beulich { 5088f9708b43SJan Beulich int err; 5089f9708b43SJan Beulich 5090161d1792SKees Cook switch (proto) { 5091f9708b43SJan Beulich case IPPROTO_TCP: 5092f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5093f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5094f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5095f9708b43SJan Beulich offsetof(struct tcphdr, 5096f9708b43SJan Beulich check))) 5097f9708b43SJan Beulich err = -EPROTO; 5098f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5099f9708b43SJan Beulich 5100f9708b43SJan Beulich case IPPROTO_UDP: 5101f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5102f9708b43SJan Beulich off + sizeof(struct udphdr)); 5103f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5104f9708b43SJan Beulich offsetof(struct udphdr, 5105f9708b43SJan Beulich check))) 5106f9708b43SJan Beulich err = -EPROTO; 5107f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5108f9708b43SJan Beulich } 5109f9708b43SJan Beulich 5110f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5111f9708b43SJan Beulich } 5112f9708b43SJan Beulich 5113ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5114ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5115ed1f50c3SPaul Durrant */ 5116ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5117ed1f50c3SPaul Durrant 5118f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5119ed1f50c3SPaul Durrant { 5120ed1f50c3SPaul Durrant unsigned int off; 5121ed1f50c3SPaul Durrant bool fragment; 5122f9708b43SJan Beulich __sum16 *csum; 5123ed1f50c3SPaul Durrant int err; 5124ed1f50c3SPaul Durrant 5125ed1f50c3SPaul Durrant fragment = false; 5126ed1f50c3SPaul Durrant 5127ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5128ed1f50c3SPaul Durrant sizeof(struct iphdr), 5129ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5130ed1f50c3SPaul Durrant if (err < 0) 5131ed1f50c3SPaul Durrant goto out; 5132ed1f50c3SPaul Durrant 513311f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5134ed1f50c3SPaul Durrant fragment = true; 5135ed1f50c3SPaul Durrant 5136ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5137ed1f50c3SPaul Durrant 5138ed1f50c3SPaul Durrant err = -EPROTO; 5139ed1f50c3SPaul Durrant 5140ed1f50c3SPaul Durrant if (fragment) 5141ed1f50c3SPaul Durrant goto out; 5142ed1f50c3SPaul Durrant 5143f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5144f9708b43SJan Beulich if (IS_ERR(csum)) 5145f9708b43SJan Beulich return PTR_ERR(csum); 5146ed1f50c3SPaul Durrant 5147ed1f50c3SPaul Durrant if (recalculate) 5148f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5149ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5150ed1f50c3SPaul Durrant skb->len - off, 5151f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5152ed1f50c3SPaul Durrant err = 0; 5153ed1f50c3SPaul Durrant 5154ed1f50c3SPaul Durrant out: 5155ed1f50c3SPaul Durrant return err; 5156ed1f50c3SPaul Durrant } 5157ed1f50c3SPaul Durrant 5158ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5159ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5160ed1f50c3SPaul Durrant */ 5161ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5162ed1f50c3SPaul Durrant 5163ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5164ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5165ed1f50c3SPaul Durrant 5166ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5167ed1f50c3SPaul Durrant { 5168ed1f50c3SPaul Durrant int err; 5169ed1f50c3SPaul Durrant u8 nexthdr; 5170ed1f50c3SPaul Durrant unsigned int off; 5171ed1f50c3SPaul Durrant unsigned int len; 5172ed1f50c3SPaul Durrant bool fragment; 5173ed1f50c3SPaul Durrant bool done; 5174f9708b43SJan Beulich __sum16 *csum; 5175ed1f50c3SPaul Durrant 5176ed1f50c3SPaul Durrant fragment = false; 5177ed1f50c3SPaul Durrant done = false; 5178ed1f50c3SPaul Durrant 5179ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5180ed1f50c3SPaul Durrant 5181ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5182ed1f50c3SPaul Durrant if (err < 0) 5183ed1f50c3SPaul Durrant goto out; 5184ed1f50c3SPaul Durrant 5185ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5186ed1f50c3SPaul Durrant 5187ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5188ed1f50c3SPaul Durrant while (off <= len && !done) { 5189ed1f50c3SPaul Durrant switch (nexthdr) { 5190ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5191ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5192ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5193ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5194ed1f50c3SPaul Durrant 5195ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5196ed1f50c3SPaul Durrant off + 5197ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5198ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5199ed1f50c3SPaul Durrant if (err < 0) 5200ed1f50c3SPaul Durrant goto out; 5201ed1f50c3SPaul Durrant 5202ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5203ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5204ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5205ed1f50c3SPaul Durrant break; 5206ed1f50c3SPaul Durrant } 5207ed1f50c3SPaul Durrant case IPPROTO_AH: { 5208ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5209ed1f50c3SPaul Durrant 5210ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5211ed1f50c3SPaul Durrant off + 5212ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5213ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5214ed1f50c3SPaul Durrant if (err < 0) 5215ed1f50c3SPaul Durrant goto out; 5216ed1f50c3SPaul Durrant 5217ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5218ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5219ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5220ed1f50c3SPaul Durrant break; 5221ed1f50c3SPaul Durrant } 5222ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5223ed1f50c3SPaul Durrant struct frag_hdr *hp; 5224ed1f50c3SPaul Durrant 5225ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5226ed1f50c3SPaul Durrant off + 5227ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5228ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5229ed1f50c3SPaul Durrant if (err < 0) 5230ed1f50c3SPaul Durrant goto out; 5231ed1f50c3SPaul Durrant 5232ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5233ed1f50c3SPaul Durrant 5234ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5235ed1f50c3SPaul Durrant fragment = true; 5236ed1f50c3SPaul Durrant 5237ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5238ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5239ed1f50c3SPaul Durrant break; 5240ed1f50c3SPaul Durrant } 5241ed1f50c3SPaul Durrant default: 5242ed1f50c3SPaul Durrant done = true; 5243ed1f50c3SPaul Durrant break; 5244ed1f50c3SPaul Durrant } 5245ed1f50c3SPaul Durrant } 5246ed1f50c3SPaul Durrant 5247ed1f50c3SPaul Durrant err = -EPROTO; 5248ed1f50c3SPaul Durrant 5249ed1f50c3SPaul Durrant if (!done || fragment) 5250ed1f50c3SPaul Durrant goto out; 5251ed1f50c3SPaul Durrant 5252f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5253f9708b43SJan Beulich if (IS_ERR(csum)) 5254f9708b43SJan Beulich return PTR_ERR(csum); 5255ed1f50c3SPaul Durrant 5256ed1f50c3SPaul Durrant if (recalculate) 5257f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5258ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5259f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5260ed1f50c3SPaul Durrant err = 0; 5261ed1f50c3SPaul Durrant 5262ed1f50c3SPaul Durrant out: 5263ed1f50c3SPaul Durrant return err; 5264ed1f50c3SPaul Durrant } 5265ed1f50c3SPaul Durrant 5266ed1f50c3SPaul Durrant /** 5267ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5268ed1f50c3SPaul Durrant * @skb: the skb to set up 5269ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5270ed1f50c3SPaul Durrant */ 5271ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5272ed1f50c3SPaul Durrant { 5273ed1f50c3SPaul Durrant int err; 5274ed1f50c3SPaul Durrant 5275ed1f50c3SPaul Durrant switch (skb->protocol) { 5276ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5277f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5278ed1f50c3SPaul Durrant break; 5279ed1f50c3SPaul Durrant 5280ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5281ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5282ed1f50c3SPaul Durrant break; 5283ed1f50c3SPaul Durrant 5284ed1f50c3SPaul Durrant default: 5285ed1f50c3SPaul Durrant err = -EPROTO; 5286ed1f50c3SPaul Durrant break; 5287ed1f50c3SPaul Durrant } 5288ed1f50c3SPaul Durrant 5289ed1f50c3SPaul Durrant return err; 5290ed1f50c3SPaul Durrant } 5291ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5292ed1f50c3SPaul Durrant 52939afd85c9SLinus Lüssing /** 52949afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 52959afd85c9SLinus Lüssing * @skb: the skb to check 52969afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 52979afd85c9SLinus Lüssing * 52989afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 52999afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 53009afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 53019afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 53029afd85c9SLinus Lüssing * 5303a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5304a516993fSLinus Lüssing * differs from the provided skb. 53059afd85c9SLinus Lüssing */ 53069afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 53079afd85c9SLinus Lüssing unsigned int transport_len) 53089afd85c9SLinus Lüssing { 53099afd85c9SLinus Lüssing struct sk_buff *skb_chk; 53109afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 53119afd85c9SLinus Lüssing int ret; 53129afd85c9SLinus Lüssing 5313a516993fSLinus Lüssing if (skb->len < len) 53149afd85c9SLinus Lüssing return NULL; 5315a516993fSLinus Lüssing else if (skb->len == len) 53169afd85c9SLinus Lüssing return skb; 53179afd85c9SLinus Lüssing 53189afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 53199afd85c9SLinus Lüssing if (!skb_chk) 53209afd85c9SLinus Lüssing return NULL; 53219afd85c9SLinus Lüssing 53229afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 53239afd85c9SLinus Lüssing if (ret) { 53249afd85c9SLinus Lüssing kfree_skb(skb_chk); 53259afd85c9SLinus Lüssing return NULL; 53269afd85c9SLinus Lüssing } 53279afd85c9SLinus Lüssing 53289afd85c9SLinus Lüssing return skb_chk; 53299afd85c9SLinus Lüssing } 53309afd85c9SLinus Lüssing 53319afd85c9SLinus Lüssing /** 53329afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 53339afd85c9SLinus Lüssing * @skb: the skb to check 53349afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 53359afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 53369afd85c9SLinus Lüssing * 53379afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 53389afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 53399afd85c9SLinus Lüssing * 53409afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 53419afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 53429afd85c9SLinus Lüssing * 5343a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5344a516993fSLinus Lüssing * differs from the provided skb. 53459afd85c9SLinus Lüssing */ 53469afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 53479afd85c9SLinus Lüssing unsigned int transport_len, 53489afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 53499afd85c9SLinus Lüssing { 53509afd85c9SLinus Lüssing struct sk_buff *skb_chk; 53519afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5352fcba67c9SLinus Lüssing __sum16 ret; 53539afd85c9SLinus Lüssing 53549afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 53559afd85c9SLinus Lüssing if (!skb_chk) 5356a516993fSLinus Lüssing goto err; 53579afd85c9SLinus Lüssing 5358a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5359a516993fSLinus Lüssing goto err; 53609afd85c9SLinus Lüssing 53619b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 53629afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 53639b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 53649afd85c9SLinus Lüssing 5365a516993fSLinus Lüssing if (ret) 5366a516993fSLinus Lüssing goto err; 53679afd85c9SLinus Lüssing 53689afd85c9SLinus Lüssing return skb_chk; 5369a516993fSLinus Lüssing 5370a516993fSLinus Lüssing err: 5371a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5372a516993fSLinus Lüssing kfree_skb(skb_chk); 5373a516993fSLinus Lüssing 5374a516993fSLinus Lüssing return NULL; 5375a516993fSLinus Lüssing 53769afd85c9SLinus Lüssing } 53779afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 53789afd85c9SLinus Lüssing 53794497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 53804497b076SBen Hutchings { 5381e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5382e87cc472SJoe Perches skb->dev->name); 53834497b076SBen Hutchings } 53844497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5385bad43ca8SEric Dumazet 5386bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5387bad43ca8SEric Dumazet { 53883d861f66SEric Dumazet if (head_stolen) { 53893d861f66SEric Dumazet skb_release_head_state(skb); 5390bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 53913d861f66SEric Dumazet } else { 5392bad43ca8SEric Dumazet __kfree_skb(skb); 5393bad43ca8SEric Dumazet } 53943d861f66SEric Dumazet } 5395bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5396bad43ca8SEric Dumazet 5397bad43ca8SEric Dumazet /** 5398bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5399bad43ca8SEric Dumazet * @to: prior buffer 5400bad43ca8SEric Dumazet * @from: buffer to add 5401bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5402c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5403bad43ca8SEric Dumazet */ 5404bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5405bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5406bad43ca8SEric Dumazet { 5407c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5408bad43ca8SEric Dumazet int i, delta, len = from->len; 5409bad43ca8SEric Dumazet 5410bad43ca8SEric Dumazet *fragstolen = false; 5411bad43ca8SEric Dumazet 5412bad43ca8SEric Dumazet if (skb_cloned(to)) 5413bad43ca8SEric Dumazet return false; 5414bad43ca8SEric Dumazet 54151effe8caSJean-Philippe Brucker /* In general, avoid mixing slab allocated and page_pool allocated 54161effe8caSJean-Philippe Brucker * pages within the same SKB. However when @to is not pp_recycle and 54171effe8caSJean-Philippe Brucker * @from is cloned, we can transition frag pages from page_pool to 54181effe8caSJean-Philippe Brucker * reference counted. 54191effe8caSJean-Philippe Brucker * 54201effe8caSJean-Philippe Brucker * On the other hand, don't allow coalescing two pp_recycle SKBs if 54211effe8caSJean-Philippe Brucker * @from is cloned, in case the SKB is using page_pool fragment 54221effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 54231effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 54241effe8caSJean-Philippe Brucker * inconsistent reference counts. 54256a5bcd84SIlias Apalodimas */ 54261effe8caSJean-Philippe Brucker if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from))) 54276a5bcd84SIlias Apalodimas return false; 54286a5bcd84SIlias Apalodimas 5429bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5430e93a0435SEric Dumazet if (len) 5431bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5432bad43ca8SEric Dumazet *delta_truesize = 0; 5433bad43ca8SEric Dumazet return true; 5434bad43ca8SEric Dumazet } 5435bad43ca8SEric Dumazet 5436c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5437c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5438c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5439bad43ca8SEric Dumazet return false; 54401f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 54411f8b977aSWillem de Bruijn return false; 5442bad43ca8SEric Dumazet 5443bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5444bad43ca8SEric Dumazet struct page *page; 5445bad43ca8SEric Dumazet unsigned int offset; 5446bad43ca8SEric Dumazet 5447c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5448c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5449bad43ca8SEric Dumazet return false; 5450bad43ca8SEric Dumazet 5451bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5452bad43ca8SEric Dumazet return false; 5453bad43ca8SEric Dumazet 5454bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5455bad43ca8SEric Dumazet 5456bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5457bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5458bad43ca8SEric Dumazet 5459c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5460bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5461bad43ca8SEric Dumazet *fragstolen = true; 5462bad43ca8SEric Dumazet } else { 5463c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5464c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5465bad43ca8SEric Dumazet return false; 5466bad43ca8SEric Dumazet 5467f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5468bad43ca8SEric Dumazet } 5469bad43ca8SEric Dumazet 5470bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5471bad43ca8SEric Dumazet 5472c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5473c818fa9eSEric Dumazet from_shinfo->frags, 5474c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5475c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5476bad43ca8SEric Dumazet 5477bad43ca8SEric Dumazet if (!skb_cloned(from)) 5478c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5479bad43ca8SEric Dumazet 54808ea853fdSLi RongQing /* if the skb is not cloned this does nothing 54818ea853fdSLi RongQing * since we set nr_frags to 0. 54828ea853fdSLi RongQing */ 5483c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5484c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5485bad43ca8SEric Dumazet 5486bad43ca8SEric Dumazet to->truesize += delta; 5487bad43ca8SEric Dumazet to->len += len; 5488bad43ca8SEric Dumazet to->data_len += len; 5489bad43ca8SEric Dumazet 5490bad43ca8SEric Dumazet *delta_truesize = delta; 5491bad43ca8SEric Dumazet return true; 5492bad43ca8SEric Dumazet } 5493bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5494621e84d6SNicolas Dichtel 5495621e84d6SNicolas Dichtel /** 54968b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5497621e84d6SNicolas Dichtel * 5498621e84d6SNicolas Dichtel * @skb: buffer to clean 54998b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5500621e84d6SNicolas Dichtel * 55018b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 55028b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 55038b27f277SNicolas Dichtel * operations. 55048b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 55058b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 55068b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5507621e84d6SNicolas Dichtel */ 55088b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5509621e84d6SNicolas Dichtel { 5510621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5511621e84d6SNicolas Dichtel skb->skb_iif = 0; 551260ff7467SWANG Cong skb->ignore_df = 0; 5513621e84d6SNicolas Dichtel skb_dst_drop(skb); 5514174e2381SFlorian Westphal skb_ext_reset(skb); 5515895b5c9fSFlorian Westphal nf_reset_ct(skb); 5516621e84d6SNicolas Dichtel nf_reset_trace(skb); 5517213dd74aSHerbert Xu 55186f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 55196f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5520875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 55216f9a5069SPetr Machata #endif 55226f9a5069SPetr Machata 5523213dd74aSHerbert Xu if (!xnet) 5524213dd74aSHerbert Xu return; 5525213dd74aSHerbert Xu 55262b5ec1a5SYe Yin ipvs_reset(skb); 5527213dd74aSHerbert Xu skb->mark = 0; 5528de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5529621e84d6SNicolas Dichtel } 5530621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5531de960aa9SFlorian Westphal 5532de960aa9SFlorian Westphal /** 5533de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5534de960aa9SFlorian Westphal * 5535de960aa9SFlorian Westphal * @skb: GSO skb 5536de960aa9SFlorian Westphal * 5537de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5538de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5539de960aa9SFlorian Westphal * 5540de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5541de960aa9SFlorian Westphal */ 5542a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5543de960aa9SFlorian Westphal { 5544de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5545f993bc25SFlorian Westphal unsigned int thlen = 0; 5546f993bc25SFlorian Westphal 5547f993bc25SFlorian Westphal if (skb->encapsulation) { 5548f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5549f993bc25SFlorian Westphal skb_transport_header(skb); 5550de960aa9SFlorian Westphal 5551de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5552f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5553f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5554f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 55551dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 555690017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5557ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5558ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5559f993bc25SFlorian Westphal } 55606d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 55616d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 55626d39d589SFlorian Westphal * accounted for. 55636d39d589SFlorian Westphal */ 5564f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5565de960aa9SFlorian Westphal } 5566a4a77718SDaniel Axtens 5567a4a77718SDaniel Axtens /** 5568a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5569a4a77718SDaniel Axtens * 5570a4a77718SDaniel Axtens * @skb: GSO skb 5571a4a77718SDaniel Axtens * 5572a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5573a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5574a4a77718SDaniel Axtens * 5575a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5576a4a77718SDaniel Axtens */ 5577a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5578a4a77718SDaniel Axtens { 5579a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5580a4a77718SDaniel Axtens skb_network_header(skb); 5581a4a77718SDaniel Axtens 5582a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5583a4a77718SDaniel Axtens } 5584a4a77718SDaniel Axtens 5585a4a77718SDaniel Axtens /** 5586a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5587a4a77718SDaniel Axtens * 5588a4a77718SDaniel Axtens * @skb: GSO skb 5589a4a77718SDaniel Axtens * 5590a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5591a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5592a4a77718SDaniel Axtens * headers (TCP/UDP). 5593a4a77718SDaniel Axtens */ 5594a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5595a4a77718SDaniel Axtens { 5596a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5597a4a77718SDaniel Axtens 5598a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5599a4a77718SDaniel Axtens } 56000d5501c1SVlad Yasevich 5601ae7ef81eSMarcelo Ricardo Leitner /** 56022b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 56032b16f048SDaniel Axtens * 56042b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 56052b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 56062b16f048SDaniel Axtens * 56072b16f048SDaniel Axtens * We might want to check: 56082b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 56092b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 56102b16f048SDaniel Axtens * 56112b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 56122b16f048SDaniel Axtens * 561349682bfaSMathieu Malaterre * @skb: GSO skb 561449682bfaSMathieu Malaterre * 56152b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 56162b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 56172b16f048SDaniel Axtens * 56182b16f048SDaniel Axtens * @max_len: The maximum permissible length. 56192b16f048SDaniel Axtens * 56202b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 56212b16f048SDaniel Axtens */ 56222b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 56232b16f048SDaniel Axtens unsigned int seg_len, 56242b16f048SDaniel Axtens unsigned int max_len) { 56252b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 56262b16f048SDaniel Axtens const struct sk_buff *iter; 56272b16f048SDaniel Axtens 56282b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 56292b16f048SDaniel Axtens return seg_len <= max_len; 56302b16f048SDaniel Axtens 56312b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 56322b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 56332b16f048SDaniel Axtens 56342b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 56352b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 56362b16f048SDaniel Axtens return false; 56372b16f048SDaniel Axtens } 56382b16f048SDaniel Axtens 56392b16f048SDaniel Axtens return true; 56402b16f048SDaniel Axtens } 56412b16f048SDaniel Axtens 56422b16f048SDaniel Axtens /** 5643779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5644ae7ef81eSMarcelo Ricardo Leitner * 5645ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 564676f21b99SDavid S. Miller * @mtu: MTU to validate against 5647ae7ef81eSMarcelo Ricardo Leitner * 5648779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5649779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5650779b7931SDaniel Axtens * payload. 5651ae7ef81eSMarcelo Ricardo Leitner */ 5652779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5653ae7ef81eSMarcelo Ricardo Leitner { 56542b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5655ae7ef81eSMarcelo Ricardo Leitner } 5656779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5657ae7ef81eSMarcelo Ricardo Leitner 56582b16f048SDaniel Axtens /** 56592b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 56602b16f048SDaniel Axtens * 56612b16f048SDaniel Axtens * @skb: GSO skb 56622b16f048SDaniel Axtens * @len: length to validate against 56632b16f048SDaniel Axtens * 56642b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 56652b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 56662b16f048SDaniel Axtens */ 56672b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 56682b16f048SDaniel Axtens { 56692b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 56702b16f048SDaniel Axtens } 56712b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 56722b16f048SDaniel Axtens 56730d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 56740d5501c1SVlad Yasevich { 5675d85e8be2SYuya Kusakabe int mac_len, meta_len; 5676d85e8be2SYuya Kusakabe void *meta; 56774bbb3e0eSToshiaki Makita 56780d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 56790d5501c1SVlad Yasevich kfree_skb(skb); 56800d5501c1SVlad Yasevich return NULL; 56810d5501c1SVlad Yasevich } 56820d5501c1SVlad Yasevich 56834bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5684ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 56854bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 56864bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5687ae474573SToshiaki Makita } 5688d85e8be2SYuya Kusakabe 5689d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5690d85e8be2SYuya Kusakabe if (meta_len) { 5691d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5692d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5693d85e8be2SYuya Kusakabe } 5694d85e8be2SYuya Kusakabe 56950d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 56960d5501c1SVlad Yasevich return skb; 56970d5501c1SVlad Yasevich } 56980d5501c1SVlad Yasevich 56990d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 57000d5501c1SVlad Yasevich { 57010d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 57020d5501c1SVlad Yasevich u16 vlan_tci; 57030d5501c1SVlad Yasevich 5704df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 57050d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 57060d5501c1SVlad Yasevich return skb; 57070d5501c1SVlad Yasevich } 57080d5501c1SVlad Yasevich 57090d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 57100d5501c1SVlad Yasevich if (unlikely(!skb)) 57110d5501c1SVlad Yasevich goto err_free; 571255eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 571355eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 57140d5501c1SVlad Yasevich goto err_free; 57150d5501c1SVlad Yasevich 57160d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 57170d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 57180d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 57190d5501c1SVlad Yasevich 57200d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 57210d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 57220d5501c1SVlad Yasevich 57230d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 57240d5501c1SVlad Yasevich if (unlikely(!skb)) 57250d5501c1SVlad Yasevich goto err_free; 57260d5501c1SVlad Yasevich 57270d5501c1SVlad Yasevich skb_reset_network_header(skb); 57288be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 57290d5501c1SVlad Yasevich skb_reset_transport_header(skb); 57300d5501c1SVlad Yasevich skb_reset_mac_len(skb); 57310d5501c1SVlad Yasevich 57320d5501c1SVlad Yasevich return skb; 57330d5501c1SVlad Yasevich 57340d5501c1SVlad Yasevich err_free: 57350d5501c1SVlad Yasevich kfree_skb(skb); 57360d5501c1SVlad Yasevich return NULL; 57370d5501c1SVlad Yasevich } 57380d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 57392e4e4410SEric Dumazet 574092ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5741e2195121SJiri Pirko { 5742e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5743e2195121SJiri Pirko return -ENOMEM; 5744e2195121SJiri Pirko 5745e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5746e2195121SJiri Pirko return 0; 5747e2195121SJiri Pirko 5748e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5749e2195121SJiri Pirko } 5750e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5751e2195121SJiri Pirko 5752bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5753bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5754bfca4c52SShmulik Ladkani */ 5755bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 575693515d53SJiri Pirko { 575793515d53SJiri Pirko struct vlan_hdr *vhdr; 5758b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 575993515d53SJiri Pirko int err; 576093515d53SJiri Pirko 5761b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5762b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5763b6a79208SShmulik Ladkani offset)) { 5764b6a79208SShmulik Ladkani return -EINVAL; 5765b6a79208SShmulik Ladkani } 5766b6a79208SShmulik Ladkani 576793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 576893515d53SJiri Pirko if (unlikely(err)) 5769b6a79208SShmulik Ladkani return err; 577093515d53SJiri Pirko 577193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 577293515d53SJiri Pirko 577393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 577493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 577593515d53SJiri Pirko 577693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 577793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 577893515d53SJiri Pirko 577993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 578093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 578193515d53SJiri Pirko 578293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 578393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 578493515d53SJiri Pirko 578593515d53SJiri Pirko skb_reset_mac_len(skb); 578693515d53SJiri Pirko 578793515d53SJiri Pirko return err; 578893515d53SJiri Pirko } 5789bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 579093515d53SJiri Pirko 5791b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5792b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5793b6a79208SShmulik Ladkani */ 579493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 579593515d53SJiri Pirko { 579693515d53SJiri Pirko u16 vlan_tci; 579793515d53SJiri Pirko __be16 vlan_proto; 579893515d53SJiri Pirko int err; 579993515d53SJiri Pirko 5800df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5801b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 580293515d53SJiri Pirko } else { 5803ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 580493515d53SJiri Pirko return 0; 580593515d53SJiri Pirko 580693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 580793515d53SJiri Pirko if (err) 580893515d53SJiri Pirko return err; 580993515d53SJiri Pirko } 581093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5811ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 581293515d53SJiri Pirko return 0; 581393515d53SJiri Pirko 581493515d53SJiri Pirko vlan_proto = skb->protocol; 581593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 581693515d53SJiri Pirko if (unlikely(err)) 581793515d53SJiri Pirko return err; 581893515d53SJiri Pirko 581993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 582093515d53SJiri Pirko return 0; 582193515d53SJiri Pirko } 582293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 582393515d53SJiri Pirko 5824b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5825b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5826b6a79208SShmulik Ladkani */ 582793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 582893515d53SJiri Pirko { 5829df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5830b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 583193515d53SJiri Pirko int err; 583293515d53SJiri Pirko 5833b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5834b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5835b6a79208SShmulik Ladkani offset)) { 5836b6a79208SShmulik Ladkani return -EINVAL; 5837b6a79208SShmulik Ladkani } 5838b6a79208SShmulik Ladkani 583993515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5840df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5841b6a79208SShmulik Ladkani if (err) 584293515d53SJiri Pirko return err; 58439241e2dfSDaniel Borkmann 584493515d53SJiri Pirko skb->protocol = skb->vlan_proto; 584593515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 584693515d53SJiri Pirko 58476b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 584893515d53SJiri Pirko } 584993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 585093515d53SJiri Pirko return 0; 585193515d53SJiri Pirko } 585293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 585393515d53SJiri Pirko 585419fbcb36SGuillaume Nault /** 585519fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 585619fbcb36SGuillaume Nault * 585719fbcb36SGuillaume Nault * @skb: Socket buffer to modify 585819fbcb36SGuillaume Nault * 585919fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 586019fbcb36SGuillaume Nault * 586119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 586219fbcb36SGuillaume Nault * present. 586319fbcb36SGuillaume Nault * 586419fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 586519fbcb36SGuillaume Nault */ 586619fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 586719fbcb36SGuillaume Nault { 586819fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 586919fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 587019fbcb36SGuillaume Nault return -EPROTO; 587119fbcb36SGuillaume Nault 587219fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 587319fbcb36SGuillaume Nault skb_reset_mac_header(skb); 587419fbcb36SGuillaume Nault skb_reset_mac_len(skb); 587519fbcb36SGuillaume Nault 587619fbcb36SGuillaume Nault return 0; 587719fbcb36SGuillaume Nault } 587819fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 587919fbcb36SGuillaume Nault 588019fbcb36SGuillaume Nault /** 588119fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 588219fbcb36SGuillaume Nault * 588319fbcb36SGuillaume Nault * @skb: Socket buffer to modify 588419fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 588519fbcb36SGuillaume Nault * @src: Source MAC address of the new header 588619fbcb36SGuillaume Nault * 588719fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 588819fbcb36SGuillaume Nault * 588919fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 589019fbcb36SGuillaume Nault * 589119fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 589219fbcb36SGuillaume Nault */ 589319fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 589419fbcb36SGuillaume Nault const unsigned char *src) 589519fbcb36SGuillaume Nault { 589619fbcb36SGuillaume Nault struct ethhdr *eth; 589719fbcb36SGuillaume Nault int err; 589819fbcb36SGuillaume Nault 589919fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 590019fbcb36SGuillaume Nault return -EPROTO; 590119fbcb36SGuillaume Nault 590219fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 590319fbcb36SGuillaume Nault if (err < 0) 590419fbcb36SGuillaume Nault return err; 590519fbcb36SGuillaume Nault 590619fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 590719fbcb36SGuillaume Nault skb_reset_mac_header(skb); 590819fbcb36SGuillaume Nault skb_reset_mac_len(skb); 590919fbcb36SGuillaume Nault 591019fbcb36SGuillaume Nault eth = eth_hdr(skb); 591119fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 591219fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 591319fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 591419fbcb36SGuillaume Nault 591519fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 591619fbcb36SGuillaume Nault 591719fbcb36SGuillaume Nault return 0; 591819fbcb36SGuillaume Nault } 591919fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 592019fbcb36SGuillaume Nault 59218822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 59228822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 59238822e270SJohn Hurley __be16 ethertype) 59248822e270SJohn Hurley { 59258822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 59268822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 59278822e270SJohn Hurley 59288822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 59298822e270SJohn Hurley } 59308822e270SJohn Hurley 59318822e270SJohn Hurley hdr->h_proto = ethertype; 59328822e270SJohn Hurley } 59338822e270SJohn Hurley 59348822e270SJohn Hurley /** 5935e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5936e7dbfed1SMartin Varghese * the packet 59378822e270SJohn Hurley * 59388822e270SJohn Hurley * @skb: buffer 59398822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 59408822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5941fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 5942e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5943e7dbfed1SMartin Varghese * ethernet 59448822e270SJohn Hurley * 59458822e270SJohn Hurley * Expects skb->data at mac header. 59468822e270SJohn Hurley * 59478822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 59488822e270SJohn Hurley */ 5949fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5950d04ac224SMartin Varghese int mac_len, bool ethernet) 59518822e270SJohn Hurley { 59528822e270SJohn Hurley struct mpls_shim_hdr *lse; 59538822e270SJohn Hurley int err; 59548822e270SJohn Hurley 59558822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 59568822e270SJohn Hurley return -EINVAL; 59578822e270SJohn Hurley 59588822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 59598822e270SJohn Hurley if (skb->encapsulation) 59608822e270SJohn Hurley return -EINVAL; 59618822e270SJohn Hurley 59628822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 59638822e270SJohn Hurley if (unlikely(err)) 59648822e270SJohn Hurley return err; 59658822e270SJohn Hurley 59668822e270SJohn Hurley if (!skb->inner_protocol) { 5967e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 59688822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 59698822e270SJohn Hurley } 59708822e270SJohn Hurley 59718822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 59728822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5973fa4e0f88SDavide Caratti mac_len); 59748822e270SJohn Hurley skb_reset_mac_header(skb); 5975fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5976e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 59778822e270SJohn Hurley 59788822e270SJohn Hurley lse = mpls_hdr(skb); 59798822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 59808822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 59818822e270SJohn Hurley 59824296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 59838822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 59848822e270SJohn Hurley skb->protocol = mpls_proto; 59858822e270SJohn Hurley 59868822e270SJohn Hurley return 0; 59878822e270SJohn Hurley } 59888822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 59898822e270SJohn Hurley 59902e4e4410SEric Dumazet /** 5991ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 5992ed246ceeSJohn Hurley * 5993ed246ceeSJohn Hurley * @skb: buffer 5994ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 5995fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 599676f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 5997ed246ceeSJohn Hurley * 5998ed246ceeSJohn Hurley * Expects skb->data at mac header. 5999ed246ceeSJohn Hurley * 6000ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 6001ed246ceeSJohn Hurley */ 6002040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 6003040b5cfbSMartin Varghese bool ethernet) 6004ed246ceeSJohn Hurley { 6005ed246ceeSJohn Hurley int err; 6006ed246ceeSJohn Hurley 6007ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6008dedc5a08SDavide Caratti return 0; 6009ed246ceeSJohn Hurley 6010fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 6011ed246ceeSJohn Hurley if (unlikely(err)) 6012ed246ceeSJohn Hurley return err; 6013ed246ceeSJohn Hurley 6014ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 6015ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 6016fa4e0f88SDavide Caratti mac_len); 6017ed246ceeSJohn Hurley 6018ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 6019ed246ceeSJohn Hurley skb_reset_mac_header(skb); 6020fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 6021ed246ceeSJohn Hurley 60224296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 6023ed246ceeSJohn Hurley struct ethhdr *hdr; 6024ed246ceeSJohn Hurley 6025ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 6026ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 6027ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 6028ed246ceeSJohn Hurley } 6029ed246ceeSJohn Hurley skb->protocol = next_proto; 6030ed246ceeSJohn Hurley 6031ed246ceeSJohn Hurley return 0; 6032ed246ceeSJohn Hurley } 6033ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 6034ed246ceeSJohn Hurley 6035ed246ceeSJohn Hurley /** 6036d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 6037d27cf5c5SJohn Hurley * 6038d27cf5c5SJohn Hurley * @skb: buffer 6039d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 6040d27cf5c5SJohn Hurley * 6041d27cf5c5SJohn Hurley * Expects skb->data at mac header. 6042d27cf5c5SJohn Hurley * 6043d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 6044d27cf5c5SJohn Hurley */ 6045d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 6046d27cf5c5SJohn Hurley { 6047d27cf5c5SJohn Hurley int err; 6048d27cf5c5SJohn Hurley 6049d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 6050d27cf5c5SJohn Hurley return -EINVAL; 6051d27cf5c5SJohn Hurley 6052d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 6053d27cf5c5SJohn Hurley if (unlikely(err)) 6054d27cf5c5SJohn Hurley return err; 6055d27cf5c5SJohn Hurley 6056d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 6057d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 6058d27cf5c5SJohn Hurley 6059d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 6060d27cf5c5SJohn Hurley } 6061d27cf5c5SJohn Hurley 6062d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 6063d27cf5c5SJohn Hurley 6064d27cf5c5SJohn Hurley return 0; 6065d27cf5c5SJohn Hurley } 6066d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 6067d27cf5c5SJohn Hurley 6068d27cf5c5SJohn Hurley /** 60692a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 60702a2ea508SJohn Hurley * 60712a2ea508SJohn Hurley * @skb: buffer 60722a2ea508SJohn Hurley * 60732a2ea508SJohn Hurley * Expects skb->data at mac header. 60742a2ea508SJohn Hurley * 60752a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 60762a2ea508SJohn Hurley */ 60772a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 60782a2ea508SJohn Hurley { 60792a2ea508SJohn Hurley u32 lse; 60802a2ea508SJohn Hurley u8 ttl; 60812a2ea508SJohn Hurley 60822a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 60832a2ea508SJohn Hurley return -EINVAL; 60842a2ea508SJohn Hurley 608513de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 608613de4ed9SDavide Caratti return -ENOMEM; 608713de4ed9SDavide Caratti 60882a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 60892a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 60902a2ea508SJohn Hurley if (!--ttl) 60912a2ea508SJohn Hurley return -EINVAL; 60922a2ea508SJohn Hurley 60932a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 60942a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 60952a2ea508SJohn Hurley 60962a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 60972a2ea508SJohn Hurley } 60982a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 60992a2ea508SJohn Hurley 61002a2ea508SJohn Hurley /** 61012e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 61022e4e4410SEric Dumazet * 6103de3f0d0eSMasanari Iida * @header_len: size of linear part 6104de3f0d0eSMasanari Iida * @data_len: needed length in frags 6105de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6106de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6107de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 61082e4e4410SEric Dumazet * 61092e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 61102e4e4410SEric Dumazet */ 61112e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 61122e4e4410SEric Dumazet unsigned long data_len, 61132e4e4410SEric Dumazet int max_page_order, 61142e4e4410SEric Dumazet int *errcode, 61152e4e4410SEric Dumazet gfp_t gfp_mask) 61162e4e4410SEric Dumazet { 61172e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 61182e4e4410SEric Dumazet unsigned long chunk; 61192e4e4410SEric Dumazet struct sk_buff *skb; 61202e4e4410SEric Dumazet struct page *page; 61212e4e4410SEric Dumazet int i; 61222e4e4410SEric Dumazet 61232e4e4410SEric Dumazet *errcode = -EMSGSIZE; 61242e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 61252e4e4410SEric Dumazet * high order pages... 61262e4e4410SEric Dumazet */ 61272e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 61282e4e4410SEric Dumazet return NULL; 61292e4e4410SEric Dumazet 61302e4e4410SEric Dumazet *errcode = -ENOBUFS; 6131f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 61322e4e4410SEric Dumazet if (!skb) 61332e4e4410SEric Dumazet return NULL; 61342e4e4410SEric Dumazet 61352e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 61362e4e4410SEric Dumazet 61372e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 61382e4e4410SEric Dumazet int order = max_page_order; 61392e4e4410SEric Dumazet 61402e4e4410SEric Dumazet while (order) { 61412e4e4410SEric Dumazet if (npages >= 1 << order) { 6142d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 61432e4e4410SEric Dumazet __GFP_COMP | 6144d14b56f5SMichal Hocko __GFP_NOWARN, 61452e4e4410SEric Dumazet order); 61462e4e4410SEric Dumazet if (page) 61472e4e4410SEric Dumazet goto fill_page; 61482e4e4410SEric Dumazet /* Do not retry other high order allocations */ 61492e4e4410SEric Dumazet order = 1; 61502e4e4410SEric Dumazet max_page_order = 0; 61512e4e4410SEric Dumazet } 61522e4e4410SEric Dumazet order--; 61532e4e4410SEric Dumazet } 61542e4e4410SEric Dumazet page = alloc_page(gfp_mask); 61552e4e4410SEric Dumazet if (!page) 61562e4e4410SEric Dumazet goto failure; 61572e4e4410SEric Dumazet fill_page: 61582e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 61592e4e4410SEric Dumazet PAGE_SIZE << order); 61602e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 61612e4e4410SEric Dumazet data_len -= chunk; 61622e4e4410SEric Dumazet npages -= 1 << order; 61632e4e4410SEric Dumazet } 61642e4e4410SEric Dumazet return skb; 61652e4e4410SEric Dumazet 61662e4e4410SEric Dumazet failure: 61672e4e4410SEric Dumazet kfree_skb(skb); 61682e4e4410SEric Dumazet return NULL; 61692e4e4410SEric Dumazet } 61702e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 61716fa01ccdSSowmini Varadhan 61726fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 61736fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 61746fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 61756fa01ccdSSowmini Varadhan { 61766fa01ccdSSowmini Varadhan int i; 61776fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 61786fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 61796fa01ccdSSowmini Varadhan u8 *data; 61806fa01ccdSSowmini Varadhan 61816fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 61826fa01ccdSSowmini Varadhan 61836fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 61846fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 61856fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 61866fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 61876fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 61886fa01ccdSSowmini Varadhan if (!data) 61896fa01ccdSSowmini Varadhan return -ENOMEM; 61906fa01ccdSSowmini Varadhan 61916fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 61926fa01ccdSSowmini Varadhan 61936fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 61946fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 61956fa01ccdSSowmini Varadhan skb->len -= off; 61966fa01ccdSSowmini Varadhan 61976fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 61986fa01ccdSSowmini Varadhan skb_shinfo(skb), 61996fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 62006fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 62016fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 62026fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 62036fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 62046fa01ccdSSowmini Varadhan kfree(data); 62056fa01ccdSSowmini Varadhan return -ENOMEM; 62066fa01ccdSSowmini Varadhan } 62076fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 62086fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 62096fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 62106fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 62116fa01ccdSSowmini Varadhan skb_release_data(skb); 62126fa01ccdSSowmini Varadhan } else { 62136fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 62146fa01ccdSSowmini Varadhan * relocate values 62156fa01ccdSSowmini Varadhan */ 62166fa01ccdSSowmini Varadhan skb_free_head(skb); 62176fa01ccdSSowmini Varadhan } 62186fa01ccdSSowmini Varadhan 62196fa01ccdSSowmini Varadhan skb->head = data; 62206fa01ccdSSowmini Varadhan skb->data = data; 62216fa01ccdSSowmini Varadhan skb->head_frag = 0; 6222763087daSEric Dumazet skb_set_end_offset(skb, size); 62236fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 62246fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 62256fa01ccdSSowmini Varadhan skb->cloned = 0; 62266fa01ccdSSowmini Varadhan skb->hdr_len = 0; 62276fa01ccdSSowmini Varadhan skb->nohdr = 0; 62286fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 62296fa01ccdSSowmini Varadhan 62306fa01ccdSSowmini Varadhan return 0; 62316fa01ccdSSowmini Varadhan } 62326fa01ccdSSowmini Varadhan 62336fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 62346fa01ccdSSowmini Varadhan 62356fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 62366fa01ccdSSowmini Varadhan * pskb_carve() 62376fa01ccdSSowmini Varadhan */ 62386fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 62396fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 62406fa01ccdSSowmini Varadhan gfp_t gfp_mask) 62416fa01ccdSSowmini Varadhan { 62426fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 62436fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 62446fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 62456fa01ccdSSowmini Varadhan 62466fa01ccdSSowmini Varadhan do { 62476fa01ccdSSowmini Varadhan if (!list) { 62486fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 62496fa01ccdSSowmini Varadhan return -EFAULT; 62506fa01ccdSSowmini Varadhan } 62516fa01ccdSSowmini Varadhan if (list->len <= eat) { 62526fa01ccdSSowmini Varadhan /* Eaten as whole. */ 62536fa01ccdSSowmini Varadhan eat -= list->len; 62546fa01ccdSSowmini Varadhan list = list->next; 62556fa01ccdSSowmini Varadhan insp = list; 62566fa01ccdSSowmini Varadhan } else { 62576fa01ccdSSowmini Varadhan /* Eaten partially. */ 62586fa01ccdSSowmini Varadhan if (skb_shared(list)) { 62596fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 62606fa01ccdSSowmini Varadhan if (!clone) 62616fa01ccdSSowmini Varadhan return -ENOMEM; 62626fa01ccdSSowmini Varadhan insp = list->next; 62636fa01ccdSSowmini Varadhan list = clone; 62646fa01ccdSSowmini Varadhan } else { 62656fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 62666fa01ccdSSowmini Varadhan insp = list; 62676fa01ccdSSowmini Varadhan } 62686fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 62696fa01ccdSSowmini Varadhan kfree_skb(clone); 62706fa01ccdSSowmini Varadhan return -ENOMEM; 62716fa01ccdSSowmini Varadhan } 62726fa01ccdSSowmini Varadhan break; 62736fa01ccdSSowmini Varadhan } 62746fa01ccdSSowmini Varadhan } while (eat); 62756fa01ccdSSowmini Varadhan 62766fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 62776fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 62786fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6279ef527f96SEric Dumazet consume_skb(list); 62806fa01ccdSSowmini Varadhan } 62816fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 62826fa01ccdSSowmini Varadhan if (clone) { 62836fa01ccdSSowmini Varadhan clone->next = list; 62846fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 62856fa01ccdSSowmini Varadhan } 62866fa01ccdSSowmini Varadhan return 0; 62876fa01ccdSSowmini Varadhan } 62886fa01ccdSSowmini Varadhan 62896fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 62906fa01ccdSSowmini Varadhan * non-linear part of skb 62916fa01ccdSSowmini Varadhan */ 62926fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 62936fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 62946fa01ccdSSowmini Varadhan { 62956fa01ccdSSowmini Varadhan int i, k = 0; 62966fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 62976fa01ccdSSowmini Varadhan u8 *data; 62986fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 62996fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 63006fa01ccdSSowmini Varadhan 63016fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 63026fa01ccdSSowmini Varadhan 63036fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 63046fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 63056fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 63066fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 63076fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 63086fa01ccdSSowmini Varadhan if (!data) 63096fa01ccdSSowmini Varadhan return -ENOMEM; 63106fa01ccdSSowmini Varadhan 63116fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 63126fa01ccdSSowmini Varadhan 63136fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6314e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 63156fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 63166fa01ccdSSowmini Varadhan kfree(data); 63176fa01ccdSSowmini Varadhan return -ENOMEM; 63186fa01ccdSSowmini Varadhan } 63196fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 63206fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 63216fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 63226fa01ccdSSowmini Varadhan 63236fa01ccdSSowmini Varadhan if (pos + fsize > off) { 63246fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 63256fa01ccdSSowmini Varadhan 63266fa01ccdSSowmini Varadhan if (pos < off) { 63276fa01ccdSSowmini Varadhan /* Split frag. 63286fa01ccdSSowmini Varadhan * We have two variants in this case: 63296fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 63306fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 63316fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 63326fa01ccdSSowmini Varadhan * where splitting is expensive. 63336fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 63346fa01ccdSSowmini Varadhan */ 6335b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 63366fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 63376fa01ccdSSowmini Varadhan } 63386fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 63396fa01ccdSSowmini Varadhan k++; 63406fa01ccdSSowmini Varadhan } 63416fa01ccdSSowmini Varadhan pos += fsize; 63426fa01ccdSSowmini Varadhan } 63436fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 63446fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 63456fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 63466fa01ccdSSowmini Varadhan 63476fa01ccdSSowmini Varadhan /* split line is in frag list */ 6348eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6349eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6350eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6351eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6352eabe8618SMiaohe Lin kfree(data); 6353eabe8618SMiaohe Lin return -ENOMEM; 63546fa01ccdSSowmini Varadhan } 63556fa01ccdSSowmini Varadhan skb_release_data(skb); 63566fa01ccdSSowmini Varadhan 63576fa01ccdSSowmini Varadhan skb->head = data; 63586fa01ccdSSowmini Varadhan skb->head_frag = 0; 63596fa01ccdSSowmini Varadhan skb->data = data; 6360763087daSEric Dumazet skb_set_end_offset(skb, size); 63616fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 63626fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 63636fa01ccdSSowmini Varadhan skb->cloned = 0; 63646fa01ccdSSowmini Varadhan skb->hdr_len = 0; 63656fa01ccdSSowmini Varadhan skb->nohdr = 0; 63666fa01ccdSSowmini Varadhan skb->len -= off; 63676fa01ccdSSowmini Varadhan skb->data_len = skb->len; 63686fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 63696fa01ccdSSowmini Varadhan return 0; 63706fa01ccdSSowmini Varadhan } 63716fa01ccdSSowmini Varadhan 63726fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 63736fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 63746fa01ccdSSowmini Varadhan { 63756fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 63766fa01ccdSSowmini Varadhan 63776fa01ccdSSowmini Varadhan if (len < headlen) 63786fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 63796fa01ccdSSowmini Varadhan else 63806fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 63816fa01ccdSSowmini Varadhan } 63826fa01ccdSSowmini Varadhan 63836fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 63846fa01ccdSSowmini Varadhan * a new skb 63856fa01ccdSSowmini Varadhan */ 63866fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 63876fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 63886fa01ccdSSowmini Varadhan { 63896fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 63906fa01ccdSSowmini Varadhan 63916fa01ccdSSowmini Varadhan if (!clone) 63926fa01ccdSSowmini Varadhan return NULL; 63936fa01ccdSSowmini Varadhan 63946fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 63956fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 63966fa01ccdSSowmini Varadhan kfree_skb(clone); 63976fa01ccdSSowmini Varadhan return NULL; 63986fa01ccdSSowmini Varadhan } 63996fa01ccdSSowmini Varadhan return clone; 64006fa01ccdSSowmini Varadhan } 64016fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6402c8c8b127SEric Dumazet 6403c8c8b127SEric Dumazet /** 6404c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6405c8c8b127SEric Dumazet * @skb: buffer 6406c8c8b127SEric Dumazet * 6407c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6408c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6409c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6410c8c8b127SEric Dumazet * truesize. 6411c8c8b127SEric Dumazet * Notes: 6412c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6413c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6414c8c8b127SEric Dumazet */ 6415c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6416c8c8b127SEric Dumazet { 64173174fed9SEric Dumazet if (skb->data_len) { 64183174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6419c8c8b127SEric Dumazet skb_cloned(skb)) 6420c8c8b127SEric Dumazet return; 6421c8c8b127SEric Dumazet 6422c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6423c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 64243174fed9SEric Dumazet } 64253174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 64263174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 64273174fed9SEric Dumazet * their truesize. 64283174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 64293174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 64303174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6431c8c8b127SEric Dumazet */ 6432c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6433c8c8b127SEric Dumazet } 6434df5042f4SFlorian Westphal 6435df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6436df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6437df5042f4SFlorian Westphal { 6438df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6439df5042f4SFlorian Westphal } 6440df5042f4SFlorian Westphal 64418b69a803SPaolo Abeni /** 64428b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 64438b69a803SPaolo Abeni * 64444930f483SFlorian Westphal * @flags: See kmalloc(). 64454930f483SFlorian Westphal * 64468b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 64478b69a803SPaolo Abeni * skb via __skb_ext_set(). 64488b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 64498b69a803SPaolo Abeni */ 64504930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6451df5042f4SFlorian Westphal { 64524930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6453df5042f4SFlorian Westphal 6454df5042f4SFlorian Westphal if (new) { 6455df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6456df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6457df5042f4SFlorian Westphal } 6458df5042f4SFlorian Westphal 6459df5042f4SFlorian Westphal return new; 6460df5042f4SFlorian Westphal } 6461df5042f4SFlorian Westphal 64624165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 64634165079bSFlorian Westphal unsigned int old_active) 6464df5042f4SFlorian Westphal { 6465df5042f4SFlorian Westphal struct skb_ext *new; 6466df5042f4SFlorian Westphal 6467df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6468df5042f4SFlorian Westphal return old; 6469df5042f4SFlorian Westphal 6470df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6471df5042f4SFlorian Westphal if (!new) 6472df5042f4SFlorian Westphal return NULL; 6473df5042f4SFlorian Westphal 6474df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6475df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6476df5042f4SFlorian Westphal 64774165079bSFlorian Westphal #ifdef CONFIG_XFRM 64784165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 64794165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 64804165079bSFlorian Westphal unsigned int i; 64814165079bSFlorian Westphal 64824165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 64834165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 64844165079bSFlorian Westphal } 64854165079bSFlorian Westphal #endif 6486df5042f4SFlorian Westphal __skb_ext_put(old); 6487df5042f4SFlorian Westphal return new; 6488df5042f4SFlorian Westphal } 6489df5042f4SFlorian Westphal 6490df5042f4SFlorian Westphal /** 64918b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 64928b69a803SPaolo Abeni * @skb: buffer 64938b69a803SPaolo Abeni * @id: extension id 64948b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 64958b69a803SPaolo Abeni * 64968b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 64978b69a803SPaolo Abeni * 64988b69a803SPaolo Abeni * Returns the pointer to the extension. 64998b69a803SPaolo Abeni */ 65008b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 65018b69a803SPaolo Abeni struct skb_ext *ext) 65028b69a803SPaolo Abeni { 65038b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 65048b69a803SPaolo Abeni 65058b69a803SPaolo Abeni skb_ext_put(skb); 65068b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 65078b69a803SPaolo Abeni ext->chunks = newlen; 65088b69a803SPaolo Abeni ext->offset[id] = newoff; 65098b69a803SPaolo Abeni skb->extensions = ext; 65108b69a803SPaolo Abeni skb->active_extensions = 1 << id; 65118b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 65128b69a803SPaolo Abeni } 65138b69a803SPaolo Abeni 65148b69a803SPaolo Abeni /** 6515df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6516df5042f4SFlorian Westphal * @skb: buffer 6517df5042f4SFlorian Westphal * @id: extension to allocate space for 6518df5042f4SFlorian Westphal * 6519df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6520df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6521df5042f4SFlorian Westphal * is returned. 6522df5042f4SFlorian Westphal * 6523df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6524df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6525df5042f4SFlorian Westphal * 6526df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6527df5042f4SFlorian Westphal */ 6528df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6529df5042f4SFlorian Westphal { 6530df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6531df5042f4SFlorian Westphal unsigned int newlen, newoff; 6532df5042f4SFlorian Westphal 6533df5042f4SFlorian Westphal if (skb->active_extensions) { 6534df5042f4SFlorian Westphal old = skb->extensions; 6535df5042f4SFlorian Westphal 65364165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6537df5042f4SFlorian Westphal if (!new) 6538df5042f4SFlorian Westphal return NULL; 6539df5042f4SFlorian Westphal 6540682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6541df5042f4SFlorian Westphal goto set_active; 6542df5042f4SFlorian Westphal 6543e94e50bdSPaolo Abeni newoff = new->chunks; 6544df5042f4SFlorian Westphal } else { 6545df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6546df5042f4SFlorian Westphal 65474930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6548df5042f4SFlorian Westphal if (!new) 6549df5042f4SFlorian Westphal return NULL; 6550df5042f4SFlorian Westphal } 6551df5042f4SFlorian Westphal 6552df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6553df5042f4SFlorian Westphal new->chunks = newlen; 6554df5042f4SFlorian Westphal new->offset[id] = newoff; 6555df5042f4SFlorian Westphal set_active: 6556b0999f38SPaolo Abeni skb->slow_gro = 1; 6557682ec859SPaolo Abeni skb->extensions = new; 6558df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6559df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6560df5042f4SFlorian Westphal } 6561df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6562df5042f4SFlorian Westphal 65634165079bSFlorian Westphal #ifdef CONFIG_XFRM 65644165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 65654165079bSFlorian Westphal { 65664165079bSFlorian Westphal unsigned int i; 65674165079bSFlorian Westphal 65684165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 65694165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 65704165079bSFlorian Westphal } 65714165079bSFlorian Westphal #endif 65724165079bSFlorian Westphal 657378476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 657478476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 657578476d31SJeremy Kerr { 657678476d31SJeremy Kerr if (flow->key) 657778476d31SJeremy Kerr mctp_key_unref(flow->key); 657878476d31SJeremy Kerr } 657978476d31SJeremy Kerr #endif 658078476d31SJeremy Kerr 6581df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6582df5042f4SFlorian Westphal { 6583df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6584df5042f4SFlorian Westphal 6585df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6586df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6587df5042f4SFlorian Westphal skb->extensions = NULL; 6588df5042f4SFlorian Westphal __skb_ext_put(ext); 65894165079bSFlorian Westphal #ifdef CONFIG_XFRM 65904165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 65914165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 65924165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 65934165079bSFlorian Westphal 65944165079bSFlorian Westphal skb_ext_put_sp(sp); 65954165079bSFlorian Westphal sp->len = 0; 65964165079bSFlorian Westphal #endif 6597df5042f4SFlorian Westphal } 6598df5042f4SFlorian Westphal } 6599df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6600df5042f4SFlorian Westphal 6601df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6602df5042f4SFlorian Westphal { 6603df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6604df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6605df5042f4SFlorian Westphal */ 6606df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6607df5042f4SFlorian Westphal goto free_now; 6608df5042f4SFlorian Westphal 6609df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6610df5042f4SFlorian Westphal return; 6611df5042f4SFlorian Westphal free_now: 66124165079bSFlorian Westphal #ifdef CONFIG_XFRM 66134165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 66144165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 66154165079bSFlorian Westphal #endif 661678476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 661778476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 661878476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 661978476d31SJeremy Kerr #endif 66204165079bSFlorian Westphal 6621df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6622df5042f4SFlorian Westphal } 6623df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6624df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 662568822bdfSEric Dumazet 662668822bdfSEric Dumazet /** 662768822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 662868822bdfSEric Dumazet * @skb: buffer 662968822bdfSEric Dumazet * 663068822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 663168822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 663268822bdfSEric Dumazet * and memory zone spinlock contention. 663368822bdfSEric Dumazet */ 663468822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 663568822bdfSEric Dumazet { 663668822bdfSEric Dumazet int cpu = skb->alloc_cpu; 663768822bdfSEric Dumazet struct softnet_data *sd; 663868822bdfSEric Dumazet unsigned long flags; 663939564c3fSEric Dumazet unsigned int defer_max; 664068822bdfSEric Dumazet bool kick; 664168822bdfSEric Dumazet 664268822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 664368822bdfSEric Dumazet !cpu_online(cpu) || 664468822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 664539564c3fSEric Dumazet nodefer: __kfree_skb(skb); 664668822bdfSEric Dumazet return; 664768822bdfSEric Dumazet } 664868822bdfSEric Dumazet 664968822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 665039564c3fSEric Dumazet defer_max = READ_ONCE(sysctl_skb_defer_max); 665139564c3fSEric Dumazet if (READ_ONCE(sd->defer_count) >= defer_max) 665239564c3fSEric Dumazet goto nodefer; 665339564c3fSEric Dumazet 665468822bdfSEric Dumazet spin_lock_irqsave(&sd->defer_lock, flags); 6655c09b0cd2SJakub Kicinski /* Send an IPI every time queue reaches half capacity. */ 6656c09b0cd2SJakub Kicinski kick = sd->defer_count == (defer_max >> 1); 6657c09b0cd2SJakub Kicinski /* Paired with the READ_ONCE() few lines above */ 6658c09b0cd2SJakub Kicinski WRITE_ONCE(sd->defer_count, sd->defer_count + 1); 6659c09b0cd2SJakub Kicinski 666068822bdfSEric Dumazet skb->next = sd->defer_list; 666168822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 666268822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 666368822bdfSEric Dumazet spin_unlock_irqrestore(&sd->defer_lock, flags); 666468822bdfSEric Dumazet 666568822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 666668822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 666768822bdfSEric Dumazet */ 666897e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 666968822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 667068822bdfSEric Dumazet } 6671