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 837f678defSVasily Averin #include "sock_destructor.h" 847b7ed885SBart Van Assche 8508009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8608009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 87df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 88df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 89df5042f4SFlorian Westphal #endif 905f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 915f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /** 94f05de73bSJean Sacren * skb_panic - private function for out-of-line support 951da177e4SLinus Torvalds * @skb: buffer 961da177e4SLinus Torvalds * @sz: size 97f05de73bSJean Sacren * @addr: address 9899d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 991da177e4SLinus Torvalds * 100f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 101f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 102f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 103f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1041da177e4SLinus Torvalds */ 105f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 10699d5851eSJames Hogan const char msg[]) 1071da177e4SLinus Torvalds { 10841a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 10999d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1104305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 11126095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1121da177e4SLinus Torvalds BUG(); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1161da177e4SLinus Torvalds { 117f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 120f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 121f05de73bSJean Sacren { 122f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 123f05de73bSJean Sacren } 124c93bdd0eSMel Gorman 12550fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 126f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 127f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 12850fad4b5SAlexander Lobakin 12950fad4b5SAlexander Lobakin struct napi_alloc_cache { 13050fad4b5SAlexander Lobakin struct page_frag_cache page; 13150fad4b5SAlexander Lobakin unsigned int skb_count; 13250fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 13350fad4b5SAlexander Lobakin }; 13450fad4b5SAlexander Lobakin 13550fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 13650fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 13750fad4b5SAlexander Lobakin 13832e3573fSYajun Deng void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 13950fad4b5SAlexander Lobakin { 14050fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 14150fad4b5SAlexander Lobakin 14250fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 14350fad4b5SAlexander Lobakin 14432e3573fSYajun Deng return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 14550fad4b5SAlexander Lobakin } 14650fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 14750fad4b5SAlexander Lobakin 14850fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 14950fad4b5SAlexander Lobakin { 15050fad4b5SAlexander Lobakin void *data; 15150fad4b5SAlexander Lobakin 15250fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 153afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 15432e3573fSYajun Deng struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 15532e3573fSYajun Deng 15650fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 15750fad4b5SAlexander Lobakin } else { 15832e3573fSYajun Deng struct napi_alloc_cache *nc; 15932e3573fSYajun Deng 16050fad4b5SAlexander Lobakin local_bh_disable(); 16132e3573fSYajun Deng nc = this_cpu_ptr(&napi_alloc_cache); 16232e3573fSYajun Deng data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 16350fad4b5SAlexander Lobakin local_bh_enable(); 16450fad4b5SAlexander Lobakin } 16550fad4b5SAlexander Lobakin return data; 16650fad4b5SAlexander Lobakin } 16750fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 16850fad4b5SAlexander Lobakin 169f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 170f450d539SAlexander Lobakin { 171f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 172f450d539SAlexander Lobakin struct sk_buff *skb; 173f450d539SAlexander Lobakin 174f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 175f450d539SAlexander Lobakin nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 176f450d539SAlexander Lobakin GFP_ATOMIC, 177f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 178f450d539SAlexander Lobakin nc->skb_cache); 179f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 180f450d539SAlexander Lobakin return NULL; 181f450d539SAlexander Lobakin 182f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 183f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, skb); 184f450d539SAlexander Lobakin 185f450d539SAlexander Lobakin return skb; 186f450d539SAlexander Lobakin } 187f450d539SAlexander Lobakin 188ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */ 189483126b3SAlexander Lobakin static void __build_skb_around(struct sk_buff *skb, void *data, 190483126b3SAlexander Lobakin unsigned int frag_size) 191ba0509b6SJesper Dangaard Brouer { 192ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 193ba0509b6SJesper Dangaard Brouer unsigned int size = frag_size ? : ksize(data); 194ba0509b6SJesper Dangaard Brouer 195ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 196ba0509b6SJesper Dangaard Brouer 197ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 198ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 199ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 200ba0509b6SJesper Dangaard Brouer skb->head = data; 201ba0509b6SJesper Dangaard Brouer skb->data = data; 202ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 203763087daSEric Dumazet skb_set_end_offset(skb, size); 204ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 205ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 20668822bdfSEric Dumazet skb->alloc_cpu = raw_smp_processor_id(); 207ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 208ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 209ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 210ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 211ba0509b6SJesper Dangaard Brouer 2126370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 213ba0509b6SJesper Dangaard Brouer } 214ba0509b6SJesper Dangaard Brouer 2151da177e4SLinus Torvalds /** 2162ea2f62cSEric Dumazet * __build_skb - build a network buffer 217b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2182ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 219b2b5ce9dSEric Dumazet * 220b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 221deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2222ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2232ea2f62cSEric Dumazet * or vmalloc() 224b2b5ce9dSEric Dumazet * The return is the new skb buffer. 225b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 226b2b5ce9dSEric Dumazet * Notes : 227b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 228b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 229b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 230b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 231b2b5ce9dSEric Dumazet * before giving packet to stack. 232b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 233b2b5ce9dSEric Dumazet */ 2342ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 235b2b5ce9dSEric Dumazet { 236b2b5ce9dSEric Dumazet struct sk_buff *skb; 237b2b5ce9dSEric Dumazet 238b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 239ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 240b2b5ce9dSEric Dumazet return NULL; 241b2b5ce9dSEric Dumazet 242b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 243483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 244b2b5ce9dSEric Dumazet 245483126b3SAlexander Lobakin return skb; 246b2b5ce9dSEric Dumazet } 2472ea2f62cSEric Dumazet 2482ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 2492ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 2502ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 2512ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 2522ea2f62cSEric Dumazet */ 2532ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 2542ea2f62cSEric Dumazet { 2552ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 2562ea2f62cSEric Dumazet 2572ea2f62cSEric Dumazet if (skb && frag_size) { 2582ea2f62cSEric Dumazet skb->head_frag = 1; 2592f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 2602ea2f62cSEric Dumazet skb->pfmemalloc = 1; 2612ea2f62cSEric Dumazet } 2622ea2f62cSEric Dumazet return skb; 2632ea2f62cSEric Dumazet } 264b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 265b2b5ce9dSEric Dumazet 266ba0509b6SJesper Dangaard Brouer /** 267ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 268ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 269ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 270ba0509b6SJesper Dangaard Brouer * @frag_size: size of data, or 0 if head was kmalloced 271ba0509b6SJesper Dangaard Brouer */ 272ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 273ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 274ba0509b6SJesper Dangaard Brouer { 275ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 276ba0509b6SJesper Dangaard Brouer return NULL; 277ba0509b6SJesper Dangaard Brouer 278483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 279ba0509b6SJesper Dangaard Brouer 280483126b3SAlexander Lobakin if (frag_size) { 281ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 282ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 283ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 284ba0509b6SJesper Dangaard Brouer } 285ba0509b6SJesper Dangaard Brouer return skb; 286ba0509b6SJesper Dangaard Brouer } 287ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 288ba0509b6SJesper Dangaard Brouer 289f450d539SAlexander Lobakin /** 290f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 291f450d539SAlexander Lobakin * @data: data buffer provided by caller 292f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 293f450d539SAlexander Lobakin * 294f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 295f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 296f450d539SAlexander Lobakin * 297f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 298f450d539SAlexander Lobakin */ 299f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 300f450d539SAlexander Lobakin { 301f450d539SAlexander Lobakin struct sk_buff *skb; 302f450d539SAlexander Lobakin 303f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 304f450d539SAlexander Lobakin if (unlikely(!skb)) 305f450d539SAlexander Lobakin return NULL; 306f450d539SAlexander Lobakin 307f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 308f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 309f450d539SAlexander Lobakin 310f450d539SAlexander Lobakin return skb; 311f450d539SAlexander Lobakin } 312f450d539SAlexander Lobakin 313f450d539SAlexander Lobakin /** 314f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 315f450d539SAlexander Lobakin * @data: data buffer provided by caller 316f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 317f450d539SAlexander Lobakin * 318f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 319f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 320f450d539SAlexander Lobakin * 321f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 322f450d539SAlexander Lobakin */ 323f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 324f450d539SAlexander Lobakin { 325f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 326f450d539SAlexander Lobakin 327f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 328f450d539SAlexander Lobakin skb->head_frag = 1; 329f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 330f450d539SAlexander Lobakin } 331f450d539SAlexander Lobakin 332f450d539SAlexander Lobakin return skb; 333f450d539SAlexander Lobakin } 334f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 335f450d539SAlexander Lobakin 3365381b23dSAlexander Lobakin /* 3375381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 3385381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 3395381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 3405381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 3415381b23dSAlexander Lobakin * memory is free 3425381b23dSAlexander Lobakin */ 343ef28095fSAlexander Lobakin static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 344ef28095fSAlexander Lobakin bool *pfmemalloc) 3455381b23dSAlexander Lobakin { 3465381b23dSAlexander Lobakin void *obj; 3475381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 3485381b23dSAlexander Lobakin 3495381b23dSAlexander Lobakin /* 3505381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 3515381b23dSAlexander Lobakin * to the reserves, fail. 3525381b23dSAlexander Lobakin */ 3535381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, 3545381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 3555381b23dSAlexander Lobakin node); 3565381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 3575381b23dSAlexander Lobakin goto out; 3585381b23dSAlexander Lobakin 3595381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 3605381b23dSAlexander Lobakin ret_pfmemalloc = true; 3615381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, flags, node); 3625381b23dSAlexander Lobakin 3635381b23dSAlexander Lobakin out: 3645381b23dSAlexander Lobakin if (pfmemalloc) 3655381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 3665381b23dSAlexander Lobakin 3675381b23dSAlexander Lobakin return obj; 3685381b23dSAlexander Lobakin } 3695381b23dSAlexander Lobakin 3705381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 3715381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 3725381b23dSAlexander Lobakin * [BEEP] leaks. 3735381b23dSAlexander Lobakin * 3745381b23dSAlexander Lobakin */ 3755381b23dSAlexander Lobakin 3765381b23dSAlexander Lobakin /** 3775381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 3785381b23dSAlexander Lobakin * @size: size to allocate 3795381b23dSAlexander Lobakin * @gfp_mask: allocation mask 3805381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 3815381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 3825381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 3835381b23dSAlexander Lobakin * allocations in case the data is required for writeback 3845381b23dSAlexander Lobakin * @node: numa node to allocate memory on 3855381b23dSAlexander Lobakin * 3865381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 3875381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 3885381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 3895381b23dSAlexander Lobakin * 3905381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 3915381b23dSAlexander Lobakin * %GFP_ATOMIC. 3925381b23dSAlexander Lobakin */ 3935381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 3945381b23dSAlexander Lobakin int flags, int node) 3955381b23dSAlexander Lobakin { 3965381b23dSAlexander Lobakin struct kmem_cache *cache; 3975381b23dSAlexander Lobakin struct sk_buff *skb; 398a5df6333SLi RongQing unsigned int osize; 3995381b23dSAlexander Lobakin bool pfmemalloc; 400a5df6333SLi RongQing u8 *data; 4015381b23dSAlexander Lobakin 4025381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 4035381b23dSAlexander Lobakin ? skbuff_fclone_cache : skbuff_head_cache; 4045381b23dSAlexander Lobakin 4055381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 4065381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 4075381b23dSAlexander Lobakin 4085381b23dSAlexander Lobakin /* Get the HEAD */ 409d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 410d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 411d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 412d13612b5SAlexander Lobakin else 413d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 414df1ae022SAlexander Lobakin if (unlikely(!skb)) 415df1ae022SAlexander Lobakin return NULL; 4165381b23dSAlexander Lobakin prefetchw(skb); 4175381b23dSAlexander Lobakin 4185381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 4195381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 4205381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 4215381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 4225381b23dSAlexander Lobakin */ 4235381b23dSAlexander Lobakin size = SKB_DATA_ALIGN(size); 4245381b23dSAlexander Lobakin size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4255381b23dSAlexander Lobakin data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 426df1ae022SAlexander Lobakin if (unlikely(!data)) 4275381b23dSAlexander Lobakin goto nodata; 4285381b23dSAlexander Lobakin /* kmalloc(size) might give us more room than requested. 4295381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 4305381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 4315381b23dSAlexander Lobakin */ 432a5df6333SLi RongQing osize = ksize(data); 433a5df6333SLi RongQing size = SKB_WITH_OVERHEAD(osize); 4345381b23dSAlexander Lobakin prefetchw(data + size); 4355381b23dSAlexander Lobakin 4365381b23dSAlexander Lobakin /* 4375381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 4385381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 4395381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 4405381b23dSAlexander Lobakin */ 4415381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 442a5df6333SLi RongQing __build_skb_around(skb, data, osize); 4435381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 4445381b23dSAlexander Lobakin 4455381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 4465381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 4475381b23dSAlexander Lobakin 4485381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 4495381b23dSAlexander Lobakin 4505381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 4515381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 4525381b23dSAlexander Lobakin 4535381b23dSAlexander Lobakin fclones->skb2.fclone = SKB_FCLONE_CLONE; 4545381b23dSAlexander Lobakin } 4555381b23dSAlexander Lobakin 4565381b23dSAlexander Lobakin return skb; 457df1ae022SAlexander Lobakin 4585381b23dSAlexander Lobakin nodata: 4595381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 460df1ae022SAlexander Lobakin return NULL; 4615381b23dSAlexander Lobakin } 4625381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 4635381b23dSAlexander Lobakin 4647ba7aeabSSebastian Andrzej Siewior /** 465fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 466fd11a83dSAlexander Duyck * @dev: network device to receive on 467d7499160SMasanari Iida * @len: length to allocate 468fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 469fd11a83dSAlexander Duyck * 470fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 471fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 472fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 473fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 474fd11a83dSAlexander Duyck * 475fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 476fd11a83dSAlexander Duyck */ 4779451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4789451980aSAlexander Duyck gfp_t gfp_mask) 479fd11a83dSAlexander Duyck { 480b63ae8caSAlexander Duyck struct page_frag_cache *nc; 481fd11a83dSAlexander Duyck struct sk_buff *skb; 4829451980aSAlexander Duyck bool pfmemalloc; 4839451980aSAlexander Duyck void *data; 484fd11a83dSAlexander Duyck 4859451980aSAlexander Duyck len += NET_SKB_PAD; 486fd11a83dSAlexander Duyck 48766c55602SAlexander Lobakin /* If requested length is either too small or too big, 48866c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 48966c55602SAlexander Lobakin */ 49066c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 49166c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 492d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 493a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 494a080e7bdSAlexander Duyck if (!skb) 495a080e7bdSAlexander Duyck goto skb_fail; 496a080e7bdSAlexander Duyck goto skb_success; 497a080e7bdSAlexander Duyck } 4989451980aSAlexander Duyck 4999451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5009451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5019451980aSAlexander Duyck 5029451980aSAlexander Duyck if (sk_memalloc_socks()) 5039451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5049451980aSAlexander Duyck 505afa79d08SChangbin Du if (in_hardirq() || irqs_disabled()) { 5069451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 5078c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 5089451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 50992dcabd7SSebastian Andrzej Siewior } else { 51092dcabd7SSebastian Andrzej Siewior local_bh_disable(); 51192dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 51292dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 51392dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 51492dcabd7SSebastian Andrzej Siewior local_bh_enable(); 51592dcabd7SSebastian Andrzej Siewior } 5169451980aSAlexander Duyck 5179451980aSAlexander Duyck if (unlikely(!data)) 5189451980aSAlexander Duyck return NULL; 5199451980aSAlexander Duyck 5209451980aSAlexander Duyck skb = __build_skb(data, len); 5219451980aSAlexander Duyck if (unlikely(!skb)) { 522181edb2bSAlexander Duyck skb_free_frag(data); 5239451980aSAlexander Duyck return NULL; 5249451980aSAlexander Duyck } 5259451980aSAlexander Duyck 5269451980aSAlexander Duyck if (pfmemalloc) 5279451980aSAlexander Duyck skb->pfmemalloc = 1; 5289451980aSAlexander Duyck skb->head_frag = 1; 5299451980aSAlexander Duyck 530a080e7bdSAlexander Duyck skb_success: 5318af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 5327b2e497aSChristoph Hellwig skb->dev = dev; 533fd11a83dSAlexander Duyck 534a080e7bdSAlexander Duyck skb_fail: 5358af27456SChristoph Hellwig return skb; 5368af27456SChristoph Hellwig } 537b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 5381da177e4SLinus Torvalds 539fd11a83dSAlexander Duyck /** 540fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 541fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 542d7499160SMasanari Iida * @len: length to allocate 543fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 544fd11a83dSAlexander Duyck * 545fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 546fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 547fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 548fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 549fd11a83dSAlexander Duyck * 550fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 551fd11a83dSAlexander Duyck */ 5529451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 5539451980aSAlexander Duyck gfp_t gfp_mask) 554fd11a83dSAlexander Duyck { 5553226b158SEric Dumazet struct napi_alloc_cache *nc; 556fd11a83dSAlexander Duyck struct sk_buff *skb; 5579451980aSAlexander Duyck void *data; 558fd11a83dSAlexander Duyck 5599451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 560fd11a83dSAlexander Duyck 5613226b158SEric Dumazet /* If requested length is either too small or too big, 5623226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 5633226b158SEric Dumazet */ 5643226b158SEric Dumazet if (len <= SKB_WITH_OVERHEAD(1024) || 5653226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 566d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 567cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 568cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 569a080e7bdSAlexander Duyck if (!skb) 570a080e7bdSAlexander Duyck goto skb_fail; 571a080e7bdSAlexander Duyck goto skb_success; 572a080e7bdSAlexander Duyck } 5739451980aSAlexander Duyck 5743226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 5759451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5769451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5779451980aSAlexander Duyck 5789451980aSAlexander Duyck if (sk_memalloc_socks()) 5799451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5809451980aSAlexander Duyck 5818c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 5829451980aSAlexander Duyck if (unlikely(!data)) 5839451980aSAlexander Duyck return NULL; 5849451980aSAlexander Duyck 585cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 5869451980aSAlexander Duyck if (unlikely(!skb)) { 587181edb2bSAlexander Duyck skb_free_frag(data); 5889451980aSAlexander Duyck return NULL; 5899451980aSAlexander Duyck } 5909451980aSAlexander Duyck 591795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5929451980aSAlexander Duyck skb->pfmemalloc = 1; 5939451980aSAlexander Duyck skb->head_frag = 1; 5949451980aSAlexander Duyck 595a080e7bdSAlexander Duyck skb_success: 596fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 597fd11a83dSAlexander Duyck skb->dev = napi->dev; 598fd11a83dSAlexander Duyck 599a080e7bdSAlexander Duyck skb_fail: 600fd11a83dSAlexander Duyck return skb; 601fd11a83dSAlexander Duyck } 602fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 603fd11a83dSAlexander Duyck 604654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 60550269e19SEric Dumazet int size, unsigned int truesize) 606654bed16SPeter Zijlstra { 607654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 608654bed16SPeter Zijlstra skb->len += size; 609654bed16SPeter Zijlstra skb->data_len += size; 61050269e19SEric Dumazet skb->truesize += truesize; 611654bed16SPeter Zijlstra } 612654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 613654bed16SPeter Zijlstra 614f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 615f8e617e1SJason Wang unsigned int truesize) 616f8e617e1SJason Wang { 617f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 618f8e617e1SJason Wang 619f8e617e1SJason Wang skb_frag_size_add(frag, size); 620f8e617e1SJason Wang skb->len += size; 621f8e617e1SJason Wang skb->data_len += size; 622f8e617e1SJason Wang skb->truesize += truesize; 623f8e617e1SJason Wang } 624f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 625f8e617e1SJason Wang 62627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 6271da177e4SLinus Torvalds { 628bd8a7036SEric Dumazet kfree_skb_list(*listp); 62927b437c8SHerbert Xu *listp = NULL; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 63227b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 63327b437c8SHerbert Xu { 63427b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 63527b437c8SHerbert Xu } 63627b437c8SHerbert Xu 6371da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds struct sk_buff *list; 6401da177e4SLinus Torvalds 641fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 6421da177e4SLinus Torvalds skb_get(list); 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 645d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 646d3836f21SEric Dumazet { 647181edb2bSAlexander Duyck unsigned char *head = skb->head; 648181edb2bSAlexander Duyck 6496a5bcd84SIlias Apalodimas if (skb->head_frag) { 6506a5bcd84SIlias Apalodimas if (skb_pp_recycle(skb, head)) 6516a5bcd84SIlias Apalodimas return; 652181edb2bSAlexander Duyck skb_free_frag(head); 6536a5bcd84SIlias Apalodimas } else { 654181edb2bSAlexander Duyck kfree(head); 655d3836f21SEric Dumazet } 6566a5bcd84SIlias Apalodimas } 657d3836f21SEric Dumazet 6585bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 6591da177e4SLinus Torvalds { 660ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 6611da177e4SLinus Torvalds int i; 662ff04a771SEric Dumazet 663ff04a771SEric Dumazet if (skb->cloned && 664ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 665ff04a771SEric Dumazet &shinfo->dataref)) 6662cc3aeb5SIlias Apalodimas goto exit; 667ff04a771SEric Dumazet 66870c43167SJonathan Lemon skb_zcopy_clear(skb, true); 66970c43167SJonathan Lemon 670ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 6716a5bcd84SIlias Apalodimas __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 6721da177e4SLinus Torvalds 673ff04a771SEric Dumazet if (shinfo->frag_list) 674ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6751da177e4SLinus Torvalds 676d3836f21SEric Dumazet skb_free_head(skb); 6772cc3aeb5SIlias Apalodimas exit: 6782cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 6792cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 6802cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 6812cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 6822cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 68358e61e41STom Rix * additional references to the fragmented part of the SKB. 6842cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 6852cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 6862cc3aeb5SIlias Apalodimas */ 6872cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6921da177e4SLinus Torvalds */ 6932d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6941da177e4SLinus Torvalds { 695d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 696d179cd12SDavid S. Miller 697d179cd12SDavid S. Miller switch (skb->fclone) { 698d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6991da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 7006ffe75ebSEric Dumazet return; 701d179cd12SDavid S. Miller 702d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 703d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 7046ffe75ebSEric Dumazet 7056ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 7066ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 7076ffe75ebSEric Dumazet * while here, branch prediction will be good. 7086ffe75ebSEric Dumazet */ 7092638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 7106ffe75ebSEric Dumazet goto fastpath; 711d179cd12SDavid S. Miller break; 712d179cd12SDavid S. Miller 7136ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 714d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 715d179cd12SDavid S. Miller break; 7163ff50b79SStephen Hemminger } 7172638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 7186ffe75ebSEric Dumazet return; 7196ffe75ebSEric Dumazet fastpath: 7206ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 7230a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 7241da177e4SLinus Torvalds { 725adf30907SEric Dumazet skb_dst_drop(skb); 7261da177e4SLinus Torvalds if (skb->destructor) { 727afa79d08SChangbin Du WARN_ON(in_hardirq()); 7281da177e4SLinus Torvalds skb->destructor(skb); 7291da177e4SLinus Torvalds } 730a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 731cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 7322fc72c7bSKOVACS Krisztian #endif 733df5042f4SFlorian Westphal skb_ext_put(skb); 73404a4bb55SLennert Buytenhek } 73504a4bb55SLennert Buytenhek 73604a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 73704a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 73804a4bb55SLennert Buytenhek { 73904a4bb55SLennert Buytenhek skb_release_head_state(skb); 740a28b1b90SFlorian Westphal if (likely(skb->head)) 7412d4baff8SHerbert Xu skb_release_data(skb); 7422d4baff8SHerbert Xu } 7431da177e4SLinus Torvalds 7442d4baff8SHerbert Xu /** 7452d4baff8SHerbert Xu * __kfree_skb - private function 7462d4baff8SHerbert Xu * @skb: buffer 7472d4baff8SHerbert Xu * 7482d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 7492d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 7502d4baff8SHerbert Xu * always call kfree_skb 7512d4baff8SHerbert Xu */ 7522d4baff8SHerbert Xu 7532d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 7542d4baff8SHerbert Xu { 7552d4baff8SHerbert Xu skb_release_all(skb); 7561da177e4SLinus Torvalds kfree_skbmem(skb); 7571da177e4SLinus Torvalds } 758b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /** 761c504e5c2SMenglong Dong * kfree_skb_reason - free an sk_buff with special reason 762231d06aeSJörn Engel * @skb: buffer to free 763c504e5c2SMenglong Dong * @reason: reason why this skb is dropped 764231d06aeSJörn Engel * 765231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 766c504e5c2SMenglong Dong * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 767c504e5c2SMenglong Dong * tracepoint. 768231d06aeSJörn Engel */ 769c504e5c2SMenglong Dong void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 770231d06aeSJörn Engel { 7713889a803SPaolo Abeni if (!skb_unref(skb)) 772231d06aeSJörn Engel return; 7733889a803SPaolo Abeni 77420bbcd0aSMenglong Dong DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX); 77520bbcd0aSMenglong Dong 776c504e5c2SMenglong Dong trace_kfree_skb(skb, __builtin_return_address(0), reason); 777231d06aeSJörn Engel __kfree_skb(skb); 778231d06aeSJörn Engel } 779c504e5c2SMenglong Dong EXPORT_SYMBOL(kfree_skb_reason); 780231d06aeSJörn Engel 781215b0f19SMenglong Dong void kfree_skb_list_reason(struct sk_buff *segs, 782215b0f19SMenglong Dong enum skb_drop_reason reason) 783bd8a7036SEric Dumazet { 784bd8a7036SEric Dumazet while (segs) { 785bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 786bd8a7036SEric Dumazet 787215b0f19SMenglong Dong kfree_skb_reason(segs, reason); 788bd8a7036SEric Dumazet segs = next; 789bd8a7036SEric Dumazet } 790bd8a7036SEric Dumazet } 791215b0f19SMenglong Dong EXPORT_SYMBOL(kfree_skb_list_reason); 792bd8a7036SEric Dumazet 7936413139dSWillem de Bruijn /* Dump skb information and contents. 7946413139dSWillem de Bruijn * 7956413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 7966413139dSWillem de Bruijn * 797302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 7986413139dSWillem de Bruijn */ 7996413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 8006413139dSWillem de Bruijn { 8016413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 8026413139dSWillem de Bruijn struct net_device *dev = skb->dev; 8036413139dSWillem de Bruijn struct sock *sk = skb->sk; 8046413139dSWillem de Bruijn struct sk_buff *list_skb; 8056413139dSWillem de Bruijn bool has_mac, has_trans; 8066413139dSWillem de Bruijn int headroom, tailroom; 8076413139dSWillem de Bruijn int i, len, seg_len; 8086413139dSWillem de Bruijn 8096413139dSWillem de Bruijn if (full_pkt) 8106413139dSWillem de Bruijn len = skb->len; 8116413139dSWillem de Bruijn else 8126413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 8136413139dSWillem de Bruijn 8146413139dSWillem de Bruijn headroom = skb_headroom(skb); 8156413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 8166413139dSWillem de Bruijn 8176413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 8186413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 8196413139dSWillem de Bruijn 8206413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 8216413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 8226413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 8236413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 8246413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 8256413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 8266413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 8276413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 8286413139dSWillem de Bruijn skb->network_header, 8296413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 8306413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 8316413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 8326413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 8336413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 8346413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 8356413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 8366413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 8376413139dSWillem de Bruijn 8386413139dSWillem de Bruijn if (dev) 8398a03ef67SGal Pressman printk("%sdev name=%s feat=%pNF\n", 8406413139dSWillem de Bruijn level, dev->name, &dev->features); 8416413139dSWillem de Bruijn if (sk) 842db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 8436413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 8446413139dSWillem de Bruijn 8456413139dSWillem de Bruijn if (full_pkt && headroom) 8466413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 8476413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 8486413139dSWillem de Bruijn 8496413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 8506413139dSWillem de Bruijn if (seg_len) 8516413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 8526413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 8536413139dSWillem de Bruijn len -= seg_len; 8546413139dSWillem de Bruijn 8556413139dSWillem de Bruijn if (full_pkt && tailroom) 8566413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 8576413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 8586413139dSWillem de Bruijn 8596413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 8606413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 8616413139dSWillem de Bruijn u32 p_off, p_len, copied; 8626413139dSWillem de Bruijn struct page *p; 8636413139dSWillem de Bruijn u8 *vaddr; 8646413139dSWillem de Bruijn 865b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 8666413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 8676413139dSWillem de Bruijn copied) { 8686413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 8696413139dSWillem de Bruijn vaddr = kmap_atomic(p); 8706413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 8716413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 8726413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 8736413139dSWillem de Bruijn kunmap_atomic(vaddr); 8746413139dSWillem de Bruijn len -= seg_len; 8756413139dSWillem de Bruijn if (!len) 8766413139dSWillem de Bruijn break; 8776413139dSWillem de Bruijn } 8786413139dSWillem de Bruijn } 8796413139dSWillem de Bruijn 8806413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 8816413139dSWillem de Bruijn printk("skb fraglist:\n"); 8826413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 8836413139dSWillem de Bruijn skb_dump(level, list_skb, true); 8846413139dSWillem de Bruijn } 8856413139dSWillem de Bruijn } 8866413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 8876413139dSWillem de Bruijn 888d1a203eaSStephen Hemminger /** 88925121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 89025121173SMichael S. Tsirkin * @skb: buffer that triggered an error 89125121173SMichael S. Tsirkin * 89225121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 89325121173SMichael S. Tsirkin * skb must be freed afterwards. 89425121173SMichael S. Tsirkin */ 89525121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 89625121173SMichael S. Tsirkin { 8971f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 89825121173SMichael S. Tsirkin } 89925121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 90025121173SMichael S. Tsirkin 901be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 90225121173SMichael S. Tsirkin /** 903ead2ceb0SNeil Horman * consume_skb - free an skbuff 904ead2ceb0SNeil Horman * @skb: buffer to free 905ead2ceb0SNeil Horman * 906ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 907ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 908ead2ceb0SNeil Horman * is being dropped after a failure and notes that 909ead2ceb0SNeil Horman */ 910ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 911ead2ceb0SNeil Horman { 9123889a803SPaolo Abeni if (!skb_unref(skb)) 913ead2ceb0SNeil Horman return; 9143889a803SPaolo Abeni 91507dc22e7SKoki Sanagi trace_consume_skb(skb); 916ead2ceb0SNeil Horman __kfree_skb(skb); 917ead2ceb0SNeil Horman } 918ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 919be769db2SHerbert Xu #endif 920ead2ceb0SNeil Horman 9210a463c78SPaolo Abeni /** 922c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 9230a463c78SPaolo Abeni * @skb: buffer to free 9240a463c78SPaolo Abeni * 925ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 926ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 9270a463c78SPaolo Abeni */ 928ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 9290a463c78SPaolo Abeni { 9300a463c78SPaolo Abeni trace_consume_skb(skb); 9310a463c78SPaolo Abeni skb_release_data(skb); 9320a463c78SPaolo Abeni kfree_skbmem(skb); 9330a463c78SPaolo Abeni } 9340a463c78SPaolo Abeni 935f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 936795bb1c0SJesper Dangaard Brouer { 937795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 938f450d539SAlexander Lobakin u32 i; 939795bb1c0SJesper Dangaard Brouer 940f450d539SAlexander Lobakin kasan_poison_object_data(skbuff_head_cache, skb); 941795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 942795bb1c0SJesper Dangaard Brouer 943795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 944f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 945f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, 946f450d539SAlexander Lobakin nc->skb_cache[i]); 947f450d539SAlexander Lobakin 948f450d539SAlexander Lobakin kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 949f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 950f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 951795bb1c0SJesper Dangaard Brouer } 952795bb1c0SJesper Dangaard Brouer } 953f450d539SAlexander Lobakin 95415fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 95515fad714SJesper Dangaard Brouer { 9569243adfcSAlexander Lobakin skb_release_all(skb); 9579243adfcSAlexander Lobakin napi_skb_cache_put(skb); 9589243adfcSAlexander Lobakin } 9599243adfcSAlexander Lobakin 9609243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 9619243adfcSAlexander Lobakin { 9629efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 9638550ff8dSPaul Blakey nf_reset_ct(skb); 9649243adfcSAlexander Lobakin skb_dst_drop(skb); 9659243adfcSAlexander Lobakin skb_ext_put(skb); 9665e10da53SPaolo Abeni skb_orphan(skb); 9679efb4b5bSPaolo Abeni skb->slow_gro = 0; 9689efb4b5bSPaolo Abeni } 969f450d539SAlexander Lobakin napi_skb_cache_put(skb); 97015fad714SJesper Dangaard Brouer } 971795bb1c0SJesper Dangaard Brouer 972795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 973795bb1c0SJesper Dangaard Brouer { 974885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 975795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 976885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 977795bb1c0SJesper Dangaard Brouer return; 978795bb1c0SJesper Dangaard Brouer } 979795bb1c0SJesper Dangaard Brouer 9806454eca8SYunsheng Lin lockdep_assert_in_softirq(); 9816454eca8SYunsheng Lin 9827608894eSPaolo Abeni if (!skb_unref(skb)) 983795bb1c0SJesper Dangaard Brouer return; 9847608894eSPaolo Abeni 985795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 986795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 987795bb1c0SJesper Dangaard Brouer 988795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 989abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 990795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 991795bb1c0SJesper Dangaard Brouer return; 992795bb1c0SJesper Dangaard Brouer } 993795bb1c0SJesper Dangaard Brouer 9949243adfcSAlexander Lobakin skb_release_all(skb); 995f450d539SAlexander Lobakin napi_skb_cache_put(skb); 996795bb1c0SJesper Dangaard Brouer } 997795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 998795bb1c0SJesper Dangaard Brouer 99903f61041SKees Cook /* Make sure a field is contained by headers group */ 1000b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 100103f61041SKees Cook BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 100203f61041SKees Cook offsetof(struct sk_buff, headers.field)); \ 1003b1937227SEric Dumazet 1004dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1005dec18810SHerbert Xu { 1006dec18810SHerbert Xu new->tstamp = old->tstamp; 1007b1937227SEric Dumazet /* We do not copy old->sk */ 1008dec18810SHerbert Xu new->dev = old->dev; 1009b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 10107fee226aSEric Dumazet skb_dst_copy(new, old); 1011df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1012b1937227SEric Dumazet __nf_copy(new, old, false); 10136aa895b0SPatrick McHardy 101403f61041SKees Cook /* Note : this field could be in the headers group. 1015b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1016b1937227SEric Dumazet */ 1017b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 101806021292SEliezer Tamir 101903f61041SKees Cook memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1020b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1021b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1022b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1023b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1024b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1025b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1026b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1027b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1028b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1029b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1030b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1031b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1032b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1033b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1034b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1035b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1036b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1037b1937227SEric Dumazet #endif 1038e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1039b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 104006021292SEliezer Tamir #endif 104168822bdfSEric Dumazet CHECK_SKB_FIELD(alloc_cpu); 10422bd82484SEric Dumazet #ifdef CONFIG_XPS 10432bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 10442bd82484SEric Dumazet #endif 1045b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1046b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1047b1937227SEric Dumazet #endif 1048b1937227SEric Dumazet 1049dec18810SHerbert Xu } 1050dec18810SHerbert Xu 105182c49a35SHerbert Xu /* 105282c49a35SHerbert Xu * You should not add any new code to this function. Add it to 105382c49a35SHerbert Xu * __copy_skb_header above instead. 105482c49a35SHerbert Xu */ 1055e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 10561da177e4SLinus Torvalds { 10571da177e4SLinus Torvalds #define C(x) n->x = skb->x 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds n->next = n->prev = NULL; 10601da177e4SLinus Torvalds n->sk = NULL; 1061dec18810SHerbert Xu __copy_skb_header(n, skb); 1062dec18810SHerbert Xu 10631da177e4SLinus Torvalds C(len); 10641da177e4SLinus Torvalds C(data_len); 10653e6b3b2eSAlexey Dobriyan C(mac_len); 1066334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 106702f1c89dSPaul Moore n->cloned = 1; 10681da177e4SLinus Torvalds n->nohdr = 0; 1069b13dda9fSEric Dumazet n->peeked = 0; 1070e78bfb07SStefano Brivio C(pfmemalloc); 10716a5bcd84SIlias Apalodimas C(pp_recycle); 10721da177e4SLinus Torvalds n->destructor = NULL; 10731da177e4SLinus Torvalds C(tail); 10741da177e4SLinus Torvalds C(end); 107502f1c89dSPaul Moore C(head); 1076d3836f21SEric Dumazet C(head_frag); 107702f1c89dSPaul Moore C(data); 107802f1c89dSPaul Moore C(truesize); 107963354797SReshetova, Elena refcount_set(&n->users, 1); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 10821da177e4SLinus Torvalds skb->cloned = 1; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds return n; 1085e0053ec0SHerbert Xu #undef C 1086e0053ec0SHerbert Xu } 1087e0053ec0SHerbert Xu 1088e0053ec0SHerbert Xu /** 1089da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1090da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1091da29e4b4SJakub Kicinski */ 1092da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1093da29e4b4SJakub Kicinski { 1094da29e4b4SJakub Kicinski struct sk_buff *n; 1095da29e4b4SJakub Kicinski 1096da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1097da29e4b4SJakub Kicinski if (!n) 1098da29e4b4SJakub Kicinski return NULL; 1099da29e4b4SJakub Kicinski 1100da29e4b4SJakub Kicinski n->len = first->len; 1101da29e4b4SJakub Kicinski n->data_len = first->len; 1102da29e4b4SJakub Kicinski n->truesize = first->truesize; 1103da29e4b4SJakub Kicinski 1104da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1105da29e4b4SJakub Kicinski 1106da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1107da29e4b4SJakub Kicinski n->destructor = NULL; 1108da29e4b4SJakub Kicinski 1109da29e4b4SJakub Kicinski return n; 1110da29e4b4SJakub Kicinski } 1111da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1112da29e4b4SJakub Kicinski 1113da29e4b4SJakub Kicinski /** 1114e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1115e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1116e0053ec0SHerbert Xu * @src: the skb to supply the contents 1117e0053ec0SHerbert Xu * 1118e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1119e0053ec0SHerbert Xu * supplied by the user. 1120e0053ec0SHerbert Xu * 1121e0053ec0SHerbert Xu * The target skb is returned upon exit. 1122e0053ec0SHerbert Xu */ 1123e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1124e0053ec0SHerbert Xu { 11252d4baff8SHerbert Xu skb_release_all(dst); 1126e0053ec0SHerbert Xu return __skb_clone(dst, src); 1127e0053ec0SHerbert Xu } 1128e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1129e0053ec0SHerbert Xu 11306f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1131a91dbff5SWillem de Bruijn { 1132a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1133a91dbff5SWillem de Bruijn struct user_struct *user; 1134a91dbff5SWillem de Bruijn 1135a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1136a91dbff5SWillem de Bruijn return 0; 1137a91dbff5SWillem de Bruijn 1138a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1139a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1140a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1141a91dbff5SWillem de Bruijn 1142a91dbff5SWillem de Bruijn do { 1143a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 1144a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1145a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1146a91dbff5SWillem de Bruijn return -ENOBUFS; 1147a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 1148a91dbff5SWillem de Bruijn old_pg); 1149a91dbff5SWillem de Bruijn 1150a91dbff5SWillem de Bruijn if (!mmp->user) { 1151a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1152a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1153a91dbff5SWillem de Bruijn } else { 1154a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1155a91dbff5SWillem de Bruijn } 1156a91dbff5SWillem de Bruijn 1157a91dbff5SWillem de Bruijn return 0; 1158a91dbff5SWillem de Bruijn } 11596f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1160a91dbff5SWillem de Bruijn 11616f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1162a91dbff5SWillem de Bruijn { 1163a91dbff5SWillem de Bruijn if (mmp->user) { 1164a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1165a91dbff5SWillem de Bruijn free_uid(mmp->user); 1166a91dbff5SWillem de Bruijn } 1167a91dbff5SWillem de Bruijn } 11686f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1169a91dbff5SWillem de Bruijn 1170c67b627eSDavid Ahern static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 117152267790SWillem de Bruijn { 117252267790SWillem de Bruijn struct ubuf_info *uarg; 117352267790SWillem de Bruijn struct sk_buff *skb; 117452267790SWillem de Bruijn 117552267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 117652267790SWillem de Bruijn 117752267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 117852267790SWillem de Bruijn if (!skb) 117952267790SWillem de Bruijn return NULL; 118052267790SWillem de Bruijn 118152267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 118252267790SWillem de Bruijn uarg = (void *)skb->cb; 1183a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1184a91dbff5SWillem de Bruijn 1185a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1186a91dbff5SWillem de Bruijn kfree_skb(skb); 1187a91dbff5SWillem de Bruijn return NULL; 1188a91dbff5SWillem de Bruijn } 118952267790SWillem de Bruijn 11908c793822SJonathan Lemon uarg->callback = msg_zerocopy_callback; 11914ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 11924ab6c99dSWillem de Bruijn uarg->len = 1; 11934ab6c99dSWillem de Bruijn uarg->bytelen = size; 119452267790SWillem de Bruijn uarg->zerocopy = 1; 119504c2d33eSJonathan Lemon uarg->flags = SKBFL_ZEROCOPY_FRAG; 1196c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 119752267790SWillem de Bruijn sock_hold(sk); 119852267790SWillem de Bruijn 119952267790SWillem de Bruijn return uarg; 120052267790SWillem de Bruijn } 120152267790SWillem de Bruijn 120252267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 120352267790SWillem de Bruijn { 120452267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 120552267790SWillem de Bruijn } 120652267790SWillem de Bruijn 12078c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 12084ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 12094ab6c99dSWillem de Bruijn { 12104ab6c99dSWillem de Bruijn if (uarg) { 12114ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 12124ab6c99dSWillem de Bruijn u32 bytelen, next; 12134ab6c99dSWillem de Bruijn 12144ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 12154ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 12164ab6c99dSWillem de Bruijn */ 12174ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 12184ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 12194ab6c99dSWillem de Bruijn return NULL; 12204ab6c99dSWillem de Bruijn } 12214ab6c99dSWillem de Bruijn 12224ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 12234ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 12244ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 12254ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 12264ab6c99dSWillem de Bruijn goto new_alloc; 12274ab6c99dSWillem de Bruijn return NULL; 12284ab6c99dSWillem de Bruijn } 12294ab6c99dSWillem de Bruijn 12304ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 12314ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 1232a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 1233a91dbff5SWillem de Bruijn return NULL; 12344ab6c99dSWillem de Bruijn uarg->len++; 12354ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 12364ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1237100f6d8eSWillem de Bruijn 1238100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1239100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 12408e044917SJonathan Lemon net_zcopy_get(uarg); 1241100f6d8eSWillem de Bruijn 12424ab6c99dSWillem de Bruijn return uarg; 12434ab6c99dSWillem de Bruijn } 12444ab6c99dSWillem de Bruijn } 12454ab6c99dSWillem de Bruijn 12464ab6c99dSWillem de Bruijn new_alloc: 12478c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 12484ab6c99dSWillem de Bruijn } 12498c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 12504ab6c99dSWillem de Bruijn 12514ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 12524ab6c99dSWillem de Bruijn { 12534ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 12544ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 12554ab6c99dSWillem de Bruijn u64 sum_len; 12564ab6c99dSWillem de Bruijn 12574ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 12584ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 12594ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 12604ab6c99dSWillem de Bruijn 12614ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 12624ab6c99dSWillem de Bruijn return false; 12634ab6c99dSWillem de Bruijn 12644ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 12654ab6c99dSWillem de Bruijn return false; 12664ab6c99dSWillem de Bruijn 12674ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 12684ab6c99dSWillem de Bruijn return true; 12694ab6c99dSWillem de Bruijn } 12704ab6c99dSWillem de Bruijn 12718c793822SJonathan Lemon static void __msg_zerocopy_callback(struct ubuf_info *uarg) 127252267790SWillem de Bruijn { 12734ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 127452267790SWillem de Bruijn struct sock_exterr_skb *serr; 127552267790SWillem de Bruijn struct sock *sk = skb->sk; 12764ab6c99dSWillem de Bruijn struct sk_buff_head *q; 12774ab6c99dSWillem de Bruijn unsigned long flags; 12783bdd5ee0SWillem de Bruijn bool is_zerocopy; 12794ab6c99dSWillem de Bruijn u32 lo, hi; 12804ab6c99dSWillem de Bruijn u16 len; 128152267790SWillem de Bruijn 1282ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1283ccaffff1SWillem de Bruijn 12844ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 12854ab6c99dSWillem de Bruijn * so do not queue a completion notification 12864ab6c99dSWillem de Bruijn */ 12874ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 128852267790SWillem de Bruijn goto release; 128952267790SWillem de Bruijn 12904ab6c99dSWillem de Bruijn len = uarg->len; 12914ab6c99dSWillem de Bruijn lo = uarg->id; 12924ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 12933bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 12944ab6c99dSWillem de Bruijn 129552267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 129652267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 129752267790SWillem de Bruijn serr->ee.ee_errno = 0; 129852267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 12994ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 13004ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 13013bdd5ee0SWillem de Bruijn if (!is_zerocopy) 130252267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 130352267790SWillem de Bruijn 13044ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 13054ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 13064ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 13074ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 13084ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 13094ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 131052267790SWillem de Bruijn skb = NULL; 13114ab6c99dSWillem de Bruijn } 13124ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 131352267790SWillem de Bruijn 1314e3ae2365SAlexander Aring sk_error_report(sk); 131552267790SWillem de Bruijn 131652267790SWillem de Bruijn release: 131752267790SWillem de Bruijn consume_skb(skb); 131852267790SWillem de Bruijn sock_put(sk); 131952267790SWillem de Bruijn } 132075518851SJonathan Lemon 13218c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 132236177832SJonathan Lemon bool success) 132375518851SJonathan Lemon { 132475518851SJonathan Lemon uarg->zerocopy = uarg->zerocopy & success; 132575518851SJonathan Lemon 132675518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 13278c793822SJonathan Lemon __msg_zerocopy_callback(uarg); 132875518851SJonathan Lemon } 13298c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 133052267790SWillem de Bruijn 13318c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 133252267790SWillem de Bruijn { 133352267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 133452267790SWillem de Bruijn 133552267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 13364ab6c99dSWillem de Bruijn uarg->len--; 133752267790SWillem de Bruijn 133852900d22SWillem de Bruijn if (have_uref) 13398c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 134052267790SWillem de Bruijn } 13418c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 134252267790SWillem de Bruijn 134352267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 134452267790SWillem de Bruijn struct msghdr *msg, int len, 134552267790SWillem de Bruijn struct ubuf_info *uarg) 134652267790SWillem de Bruijn { 13474ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 134852267790SWillem de Bruijn int err, orig_len = skb->len; 134952267790SWillem de Bruijn 13504ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 13514ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 13524ab6c99dSWillem de Bruijn */ 13534ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 13544ab6c99dSWillem de Bruijn return -EEXIST; 13554ab6c99dSWillem de Bruijn 135652267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 135752267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 135854d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 135954d43117SWillem de Bruijn 136052267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 1361829b7bddSPavel Begunkov iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 136254d43117SWillem de Bruijn skb->sk = sk; 136352267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 136454d43117SWillem de Bruijn skb->sk = save_sk; 136552267790SWillem de Bruijn return err; 136652267790SWillem de Bruijn } 136752267790SWillem de Bruijn 136852900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 136952267790SWillem de Bruijn return skb->len - orig_len; 137052267790SWillem de Bruijn } 137152267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 137252267790SWillem de Bruijn 13731f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 137452267790SWillem de Bruijn gfp_t gfp_mask) 137552267790SWillem de Bruijn { 137652267790SWillem de Bruijn if (skb_zcopy(orig)) { 137752267790SWillem de Bruijn if (skb_zcopy(nskb)) { 137852267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 137952267790SWillem de Bruijn if (!gfp_mask) { 138052267790SWillem de Bruijn WARN_ON_ONCE(1); 138152267790SWillem de Bruijn return -ENOMEM; 138252267790SWillem de Bruijn } 138352267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 138452267790SWillem de Bruijn return 0; 138552267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 138652267790SWillem de Bruijn return -EIO; 138752267790SWillem de Bruijn } 138852900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 138952267790SWillem de Bruijn } 139052267790SWillem de Bruijn return 0; 139152267790SWillem de Bruijn } 139252267790SWillem de Bruijn 13932c53040fSBen Hutchings /** 13942c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 139548c83012SMichael S. Tsirkin * @skb: the skb to modify 139648c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 139748c83012SMichael S. Tsirkin * 139806b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 139948c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 140048c83012SMichael S. Tsirkin * to userspace pages. 140148c83012SMichael S. Tsirkin * 140248c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 140348c83012SMichael S. Tsirkin * %GFP_ATOMIC. 140448c83012SMichael S. Tsirkin * 140548c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 140648c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 140748c83012SMichael S. Tsirkin */ 140848c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1409a6686f2fSShirley Ma { 1410a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1411a6686f2fSShirley Ma struct page *page, *head = NULL; 14123ece7826SWillem de Bruijn int i, new_frags; 14133ece7826SWillem de Bruijn u32 d_off; 1414a6686f2fSShirley Ma 14153ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 14163ece7826SWillem de Bruijn return -EINVAL; 14173ece7826SWillem de Bruijn 1418f72c4ac6SWillem de Bruijn if (!num_frags) 1419f72c4ac6SWillem de Bruijn goto release; 1420f72c4ac6SWillem de Bruijn 14213ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 14223ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 142302756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1424a6686f2fSShirley Ma if (!page) { 1425a6686f2fSShirley Ma while (head) { 142640dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1427a6686f2fSShirley Ma put_page(head); 1428a6686f2fSShirley Ma head = next; 1429a6686f2fSShirley Ma } 1430a6686f2fSShirley Ma return -ENOMEM; 1431a6686f2fSShirley Ma } 14323ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 14333ece7826SWillem de Bruijn head = page; 14343ece7826SWillem de Bruijn } 14353ece7826SWillem de Bruijn 14363ece7826SWillem de Bruijn page = head; 14373ece7826SWillem de Bruijn d_off = 0; 14383ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 14393ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 14403ece7826SWillem de Bruijn u32 p_off, p_len, copied; 14413ece7826SWillem de Bruijn struct page *p; 14423ece7826SWillem de Bruijn u8 *vaddr; 1443c613c209SWillem de Bruijn 1444b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1445c613c209SWillem de Bruijn p, p_off, p_len, copied) { 14463ece7826SWillem de Bruijn u32 copy, done = 0; 1447c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 14483ece7826SWillem de Bruijn 14493ece7826SWillem de Bruijn while (done < p_len) { 14503ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 14513ece7826SWillem de Bruijn d_off = 0; 14523ece7826SWillem de Bruijn page = (struct page *)page_private(page); 14533ece7826SWillem de Bruijn } 14543ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 14553ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 14563ece7826SWillem de Bruijn vaddr + p_off + done, copy); 14573ece7826SWillem de Bruijn done += copy; 14583ece7826SWillem de Bruijn d_off += copy; 14593ece7826SWillem de Bruijn } 146051c56b00SEric Dumazet kunmap_atomic(vaddr); 1461c613c209SWillem de Bruijn } 1462a6686f2fSShirley Ma } 1463a6686f2fSShirley Ma 1464a6686f2fSShirley Ma /* skb frags release userspace buffers */ 146502756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1466a8605c60SIan Campbell skb_frag_unref(skb, i); 1467a6686f2fSShirley Ma 1468a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 14693ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 14703ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 147140dadff2SSunghan Suh head = (struct page *)page_private(head); 1472a6686f2fSShirley Ma } 14733ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 14743ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 147548c83012SMichael S. Tsirkin 1476b90ddd56SWillem de Bruijn release: 14771f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1478a6686f2fSShirley Ma return 0; 1479a6686f2fSShirley Ma } 1480dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1481a6686f2fSShirley Ma 1482e0053ec0SHerbert Xu /** 1483e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1484e0053ec0SHerbert Xu * @skb: buffer to clone 1485e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1486e0053ec0SHerbert Xu * 1487e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1488e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1489e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1490e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1491e0053ec0SHerbert Xu * 1492e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1493e0053ec0SHerbert Xu * %GFP_ATOMIC. 1494e0053ec0SHerbert Xu */ 1495e0053ec0SHerbert Xu 1496e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1497e0053ec0SHerbert Xu { 1498d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1499d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1500d0bf4a9eSEric Dumazet skb1); 15016ffe75ebSEric Dumazet struct sk_buff *n; 1502e0053ec0SHerbert Xu 150370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1504a6686f2fSShirley Ma return NULL; 1505a6686f2fSShirley Ma 1506e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 15072638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 15086ffe75ebSEric Dumazet n = &fclones->skb2; 15092638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1510e0053ec0SHerbert Xu } else { 1511c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1512c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1513c93bdd0eSMel Gorman 1514e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1515e0053ec0SHerbert Xu if (!n) 1516e0053ec0SHerbert Xu return NULL; 1517fe55f6d5SVegard Nossum 1518e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1519e0053ec0SHerbert Xu } 1520e0053ec0SHerbert Xu 1521e0053ec0SHerbert Xu return __skb_clone(n, skb); 15221da177e4SLinus Torvalds } 1523b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 15241da177e4SLinus Torvalds 1525b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1526f5b17294SPravin B Shelar { 1527030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1528030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1529030737bcSEric Dumazet skb->csum_start += off; 1530f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1531f5b17294SPravin B Shelar skb->transport_header += off; 1532f5b17294SPravin B Shelar skb->network_header += off; 1533f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1534f5b17294SPravin B Shelar skb->mac_header += off; 1535f5b17294SPravin B Shelar skb->inner_transport_header += off; 1536f5b17294SPravin B Shelar skb->inner_network_header += off; 1537aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1538f5b17294SPravin B Shelar } 1539b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1540f5b17294SPravin B Shelar 154108303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 15421da177e4SLinus Torvalds { 1543dec18810SHerbert Xu __copy_skb_header(new, old); 1544dec18810SHerbert Xu 15457967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 15467967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 15477967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 15481da177e4SLinus Torvalds } 154908303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 15501da177e4SLinus Torvalds 1551c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1552c93bdd0eSMel Gorman { 1553c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1554c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1555c93bdd0eSMel Gorman return 0; 1556c93bdd0eSMel Gorman } 1557c93bdd0eSMel Gorman 15581da177e4SLinus Torvalds /** 15591da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 15601da177e4SLinus Torvalds * @skb: buffer to copy 15611da177e4SLinus Torvalds * @gfp_mask: allocation priority 15621da177e4SLinus Torvalds * 15631da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 15641da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 15651da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 15661da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15671da177e4SLinus Torvalds * 15681da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 15691da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 15701da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 15711da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 15721da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 15731da177e4SLinus Torvalds */ 15741da177e4SLinus Torvalds 1575dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 15761da177e4SLinus Torvalds { 15776602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1578ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1579c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1580c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 15816602cebbSEric Dumazet 15821da177e4SLinus Torvalds if (!n) 15831da177e4SLinus Torvalds return NULL; 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds /* Set the data pointer */ 15861da177e4SLinus Torvalds skb_reserve(n, headerlen); 15871da177e4SLinus Torvalds /* Set the tail pointer and length */ 15881da177e4SLinus Torvalds skb_put(n, skb->len); 15891da177e4SLinus Torvalds 15909f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 15911da177e4SLinus Torvalds 159208303c18SIlya Lesokhin skb_copy_header(n, skb); 15931da177e4SLinus Torvalds return n; 15941da177e4SLinus Torvalds } 1595b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /** 1598bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 15991da177e4SLinus Torvalds * @skb: buffer to copy 1600117632e6SEric Dumazet * @headroom: headroom of new skb 16011da177e4SLinus Torvalds * @gfp_mask: allocation priority 1602bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1603bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1604bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 16051da177e4SLinus Torvalds * 16061da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 16071da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 16081da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 16091da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 16101da177e4SLinus Torvalds * or the pointer to the buffer on success. 16111da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 16121da177e4SLinus Torvalds */ 16131da177e4SLinus Torvalds 1614bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1615bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 16161da177e4SLinus Torvalds { 1617117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1618bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1619bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 16206602cebbSEric Dumazet 16211da177e4SLinus Torvalds if (!n) 16221da177e4SLinus Torvalds goto out; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds /* Set the data pointer */ 1625117632e6SEric Dumazet skb_reserve(n, headroom); 16261da177e4SLinus Torvalds /* Set the tail pointer and length */ 16271da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 16281da177e4SLinus Torvalds /* Copy the bytes */ 1629d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 16301da177e4SLinus Torvalds 163125f484a6SHerbert Xu n->truesize += skb->data_len; 16321da177e4SLinus Torvalds n->data_len = skb->data_len; 16331da177e4SLinus Torvalds n->len = skb->len; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 16361da177e4SLinus Torvalds int i; 16371da177e4SLinus Torvalds 16381f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 16391f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 16401511022cSDan Carpenter kfree_skb(n); 16411511022cSDan Carpenter n = NULL; 1642a6686f2fSShirley Ma goto out; 1643a6686f2fSShirley Ma } 16441da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16451da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1646ea2ab693SIan Campbell skb_frag_ref(skb, i); 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 165121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 16521da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 16531da177e4SLinus Torvalds skb_clone_fraglist(n); 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds 165608303c18SIlya Lesokhin skb_copy_header(n, skb); 16571da177e4SLinus Torvalds out: 16581da177e4SLinus Torvalds return n; 16591da177e4SLinus Torvalds } 1660bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /** 16631da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 16641da177e4SLinus Torvalds * @skb: buffer to reallocate 16651da177e4SLinus Torvalds * @nhead: room to add at head 16661da177e4SLinus Torvalds * @ntail: room to add at tail 16671da177e4SLinus Torvalds * @gfp_mask: allocation priority 16681da177e4SLinus Torvalds * 1669bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1670bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 16711da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 16721da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 16731da177e4SLinus Torvalds * 16741da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 16751da177e4SLinus Torvalds * reloaded after call to this function. 16761da177e4SLinus Torvalds */ 16771da177e4SLinus Torvalds 167886a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1679dd0fc66fSAl Viro gfp_t gfp_mask) 16801da177e4SLinus Torvalds { 1681158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1682158f323bSEric Dumazet int size = osize + nhead + ntail; 16831da177e4SLinus Torvalds long off; 1684158f323bSEric Dumazet u8 *data; 16851da177e4SLinus Torvalds 16864edd87adSHerbert Xu BUG_ON(nhead < 0); 16874edd87adSHerbert Xu 16889f77fad3STim Hansen BUG_ON(skb_shared(skb)); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 16911da177e4SLinus Torvalds 1692c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1693c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1694c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1695c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 16961da177e4SLinus Torvalds if (!data) 16971da177e4SLinus Torvalds goto nodata; 169887151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 17016602cebbSEric Dumazet * optimized for the cases when header is void. 17026602cebbSEric Dumazet */ 17036602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 17046602cebbSEric Dumazet 17056602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 17066602cebbSEric Dumazet skb_shinfo(skb), 1707fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 17081da177e4SLinus Torvalds 17093e24591aSAlexander Duyck /* 17103e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 17113e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 17123e24591aSAlexander Duyck * be since all we did is relocate the values 17133e24591aSAlexander Duyck */ 17143e24591aSAlexander Duyck if (skb_cloned(skb)) { 171570008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1716a6686f2fSShirley Ma goto nofrags; 17171f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1718c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 17191da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1720ea2ab693SIan Campbell skb_frag_ref(skb, i); 17211da177e4SLinus Torvalds 172221dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 17231da177e4SLinus Torvalds skb_clone_fraglist(skb); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds skb_release_data(skb); 17263e24591aSAlexander Duyck } else { 17273e24591aSAlexander Duyck skb_free_head(skb); 17281fd63041SEric Dumazet } 17291da177e4SLinus Torvalds off = (data + nhead) - skb->head; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds skb->head = data; 1732d3836f21SEric Dumazet skb->head_frag = 0; 17331da177e4SLinus Torvalds skb->data += off; 1734763087daSEric Dumazet 1735763087daSEric Dumazet skb_set_end_offset(skb, size); 17364305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 173756eb8882SPatrick McHardy off = nhead; 173856eb8882SPatrick McHardy #endif 173927a884dcSArnaldo Carvalho de Melo skb->tail += off; 1740b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 17411da177e4SLinus Torvalds skb->cloned = 0; 1742334a8132SPatrick McHardy skb->hdr_len = 0; 17431da177e4SLinus Torvalds skb->nohdr = 0; 17441da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1745158f323bSEric Dumazet 1746de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1747de8f3a83SDaniel Borkmann 1748158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1749158f323bSEric Dumazet * For the moment, we really care of rx path, or 1750158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1751158f323bSEric Dumazet */ 1752158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1753158f323bSEric Dumazet skb->truesize += size - osize; 1754158f323bSEric Dumazet 17551da177e4SLinus Torvalds return 0; 17561da177e4SLinus Torvalds 1757a6686f2fSShirley Ma nofrags: 1758a6686f2fSShirley Ma kfree(data); 17591da177e4SLinus Torvalds nodata: 17601da177e4SLinus Torvalds return -ENOMEM; 17611da177e4SLinus Torvalds } 1762b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 17671da177e4SLinus Torvalds { 17681da177e4SLinus Torvalds struct sk_buff *skb2; 17691da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds if (delta <= 0) 17721da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 17731da177e4SLinus Torvalds else { 17741da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 17751da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 17761da177e4SLinus Torvalds GFP_ATOMIC)) { 17771da177e4SLinus Torvalds kfree_skb(skb2); 17781da177e4SLinus Torvalds skb2 = NULL; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds return skb2; 17821da177e4SLinus Torvalds } 1783b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 17841da177e4SLinus Torvalds 17852b88cba5SEric Dumazet int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 17862b88cba5SEric Dumazet { 17872b88cba5SEric Dumazet unsigned int saved_end_offset, saved_truesize; 17882b88cba5SEric Dumazet struct skb_shared_info *shinfo; 17892b88cba5SEric Dumazet int res; 17902b88cba5SEric Dumazet 17912b88cba5SEric Dumazet saved_end_offset = skb_end_offset(skb); 17922b88cba5SEric Dumazet saved_truesize = skb->truesize; 17932b88cba5SEric Dumazet 17942b88cba5SEric Dumazet res = pskb_expand_head(skb, 0, 0, pri); 17952b88cba5SEric Dumazet if (res) 17962b88cba5SEric Dumazet return res; 17972b88cba5SEric Dumazet 17982b88cba5SEric Dumazet skb->truesize = saved_truesize; 17992b88cba5SEric Dumazet 18002b88cba5SEric Dumazet if (likely(skb_end_offset(skb) == saved_end_offset)) 18012b88cba5SEric Dumazet return 0; 18022b88cba5SEric Dumazet 18032b88cba5SEric Dumazet shinfo = skb_shinfo(skb); 18042b88cba5SEric Dumazet 18052b88cba5SEric Dumazet /* We are about to change back skb->end, 18062b88cba5SEric Dumazet * we need to move skb_shinfo() to its new location. 18072b88cba5SEric Dumazet */ 18082b88cba5SEric Dumazet memmove(skb->head + saved_end_offset, 18092b88cba5SEric Dumazet shinfo, 18102b88cba5SEric Dumazet offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 18112b88cba5SEric Dumazet 18122b88cba5SEric Dumazet skb_set_end_offset(skb, saved_end_offset); 18132b88cba5SEric Dumazet 18142b88cba5SEric Dumazet return 0; 18152b88cba5SEric Dumazet } 18162b88cba5SEric Dumazet 18171da177e4SLinus Torvalds /** 1818f1260ff1SVasily Averin * skb_expand_head - reallocate header of &sk_buff 1819f1260ff1SVasily Averin * @skb: buffer to reallocate 1820f1260ff1SVasily Averin * @headroom: needed headroom 1821f1260ff1SVasily Averin * 1822f1260ff1SVasily Averin * Unlike skb_realloc_headroom, this one does not allocate a new skb 1823f1260ff1SVasily Averin * if possible; copies skb->sk to new skb as needed 1824f1260ff1SVasily Averin * and frees original skb in case of failures. 1825f1260ff1SVasily Averin * 1826f1260ff1SVasily Averin * It expect increased headroom and generates warning otherwise. 1827f1260ff1SVasily Averin */ 1828f1260ff1SVasily Averin 1829f1260ff1SVasily Averin struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 1830f1260ff1SVasily Averin { 1831f1260ff1SVasily Averin int delta = headroom - skb_headroom(skb); 18327f678defSVasily Averin int osize = skb_end_offset(skb); 18337f678defSVasily Averin struct sock *sk = skb->sk; 1834f1260ff1SVasily Averin 1835f1260ff1SVasily Averin if (WARN_ONCE(delta <= 0, 1836f1260ff1SVasily Averin "%s is expecting an increase in the headroom", __func__)) 1837f1260ff1SVasily Averin return skb; 1838f1260ff1SVasily Averin 18397f678defSVasily Averin delta = SKB_DATA_ALIGN(delta); 18407f678defSVasily Averin /* pskb_expand_head() might crash, if skb is shared. */ 18417f678defSVasily Averin if (skb_shared(skb) || !is_skb_wmem(skb)) { 1842f1260ff1SVasily Averin struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 1843f1260ff1SVasily Averin 18447f678defSVasily Averin if (unlikely(!nskb)) 18457f678defSVasily Averin goto fail; 18467f678defSVasily Averin 18477f678defSVasily Averin if (sk) 18487f678defSVasily Averin skb_set_owner_w(nskb, sk); 1849f1260ff1SVasily Averin consume_skb(skb); 1850f1260ff1SVasily Averin skb = nskb; 1851f1260ff1SVasily Averin } 18527f678defSVasily Averin if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 18537f678defSVasily Averin goto fail; 18547f678defSVasily Averin 18557f678defSVasily Averin if (sk && is_skb_wmem(skb)) { 18567f678defSVasily Averin delta = skb_end_offset(skb) - osize; 18577f678defSVasily Averin refcount_add(delta, &sk->sk_wmem_alloc); 18587f678defSVasily Averin skb->truesize += delta; 1859f1260ff1SVasily Averin } 1860f1260ff1SVasily Averin return skb; 18617f678defSVasily Averin 18627f678defSVasily Averin fail: 18637f678defSVasily Averin kfree_skb(skb); 18647f678defSVasily Averin return NULL; 1865f1260ff1SVasily Averin } 1866f1260ff1SVasily Averin EXPORT_SYMBOL(skb_expand_head); 1867f1260ff1SVasily Averin 1868f1260ff1SVasily Averin /** 18691da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 18701da177e4SLinus Torvalds * @skb: buffer to copy 18711da177e4SLinus Torvalds * @newheadroom: new free bytes at head 18721da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 18731da177e4SLinus Torvalds * @gfp_mask: allocation priority 18741da177e4SLinus Torvalds * 18751da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 18761da177e4SLinus Torvalds * allocate additional space. 18771da177e4SLinus Torvalds * 18781da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 18791da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 18801da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 18811da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 18821da177e4SLinus Torvalds * 18831da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 18841da177e4SLinus Torvalds * is called from an interrupt. 18851da177e4SLinus Torvalds */ 18861da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 188786a76cafSVictor Fusco int newheadroom, int newtailroom, 1888dd0fc66fSAl Viro gfp_t gfp_mask) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds /* 18911da177e4SLinus Torvalds * Allocate the copy buffer 18921da177e4SLinus Torvalds */ 1893c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1894c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1895c93bdd0eSMel Gorman NUMA_NO_NODE); 1896efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 18971da177e4SLinus Torvalds int head_copy_len, head_copy_off; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds if (!n) 19001da177e4SLinus Torvalds return NULL; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds skb_reserve(n, newheadroom); 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds /* Set the tail pointer and length */ 19051da177e4SLinus Torvalds skb_put(n, skb->len); 19061da177e4SLinus Torvalds 1907efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 19081da177e4SLinus Torvalds head_copy_off = 0; 19091da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 19101da177e4SLinus Torvalds head_copy_len = newheadroom; 19111da177e4SLinus Torvalds else 19121da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds /* Copy the linear header and data. */ 19159f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 19169f77fad3STim Hansen skb->len + head_copy_len)); 19171da177e4SLinus Torvalds 191808303c18SIlya Lesokhin skb_copy_header(n, skb); 19191da177e4SLinus Torvalds 1920030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1921efd1e8d5SPatrick McHardy 19221da177e4SLinus Torvalds return n; 19231da177e4SLinus Torvalds } 1924b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds /** 1927cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 19281da177e4SLinus Torvalds * @skb: buffer to pad 19291da177e4SLinus Torvalds * @pad: space to pad 1930cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 19311da177e4SLinus Torvalds * 19321da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 19331da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 19341da177e4SLinus Torvalds * beyond the buffer end onto the wire. 19351da177e4SLinus Torvalds * 1936cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1937cd0a137aSFlorian Fainelli * if @free_on_error is true. 19381da177e4SLinus Torvalds */ 19391da177e4SLinus Torvalds 1940cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 19411da177e4SLinus Torvalds { 19425b057c6bSHerbert Xu int err; 19435b057c6bSHerbert Xu int ntail; 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 19465b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 19471da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 19485b057c6bSHerbert Xu return 0; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19514305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 19525b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 19535b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 19545b057c6bSHerbert Xu if (unlikely(err)) 19555b057c6bSHerbert Xu goto free_skb; 19565b057c6bSHerbert Xu } 19575b057c6bSHerbert Xu 19585b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 19595b057c6bSHerbert Xu * to be audited. 19605b057c6bSHerbert Xu */ 19615b057c6bSHerbert Xu err = skb_linearize(skb); 19625b057c6bSHerbert Xu if (unlikely(err)) 19635b057c6bSHerbert Xu goto free_skb; 19645b057c6bSHerbert Xu 19655b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 19665b057c6bSHerbert Xu return 0; 19675b057c6bSHerbert Xu 19685b057c6bSHerbert Xu free_skb: 1969cd0a137aSFlorian Fainelli if (free_on_error) 19701da177e4SLinus Torvalds kfree_skb(skb); 19715b057c6bSHerbert Xu return err; 19721da177e4SLinus Torvalds } 1973cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 19741da177e4SLinus Torvalds 19750dde3e16SIlpo Järvinen /** 19760c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 19770c7ddf36SMathias Krause * @skb: start of the buffer to use 19780c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 19790c7ddf36SMathias Krause * @len: amount of data to add 19800c7ddf36SMathias Krause * 19810c7ddf36SMathias Krause * This function extends the used data area of the potentially 19820c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 19830c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 19840c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 19850c7ddf36SMathias Krause * returned. 19860c7ddf36SMathias Krause */ 19870c7ddf36SMathias Krause 19884df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 19890c7ddf36SMathias Krause { 19900c7ddf36SMathias Krause if (tail != skb) { 19910c7ddf36SMathias Krause skb->data_len += len; 19920c7ddf36SMathias Krause skb->len += len; 19930c7ddf36SMathias Krause } 19940c7ddf36SMathias Krause return skb_put(tail, len); 19950c7ddf36SMathias Krause } 19960c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 19970c7ddf36SMathias Krause 19980c7ddf36SMathias Krause /** 19990dde3e16SIlpo Järvinen * skb_put - add data to a buffer 20000dde3e16SIlpo Järvinen * @skb: buffer to use 20010dde3e16SIlpo Järvinen * @len: amount of data to add 20020dde3e16SIlpo Järvinen * 20030dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 20040dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 20050dde3e16SIlpo Järvinen * first byte of the extra data is returned. 20060dde3e16SIlpo Järvinen */ 20074df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 20080dde3e16SIlpo Järvinen { 20094df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 20100dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 20110dde3e16SIlpo Järvinen skb->tail += len; 20120dde3e16SIlpo Järvinen skb->len += len; 20130dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 20140dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 20150dde3e16SIlpo Järvinen return tmp; 20160dde3e16SIlpo Järvinen } 20170dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 20180dde3e16SIlpo Järvinen 20196be8ac2fSIlpo Järvinen /** 2020c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 2021c2aa270aSIlpo Järvinen * @skb: buffer to use 2022c2aa270aSIlpo Järvinen * @len: amount of data to add 2023c2aa270aSIlpo Järvinen * 2024c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 2025c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 2026c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 2027c2aa270aSIlpo Järvinen */ 2028d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 2029c2aa270aSIlpo Järvinen { 2030c2aa270aSIlpo Järvinen skb->data -= len; 2031c2aa270aSIlpo Järvinen skb->len += len; 2032c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 2033c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 2034c2aa270aSIlpo Järvinen return skb->data; 2035c2aa270aSIlpo Järvinen } 2036c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 2037c2aa270aSIlpo Järvinen 2038c2aa270aSIlpo Järvinen /** 20396be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 20406be8ac2fSIlpo Järvinen * @skb: buffer to use 20416be8ac2fSIlpo Järvinen * @len: amount of data to remove 20426be8ac2fSIlpo Järvinen * 20436be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 20446be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 20456be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 20466be8ac2fSIlpo Järvinen * the old data. 20476be8ac2fSIlpo Järvinen */ 2048af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 20496be8ac2fSIlpo Järvinen { 205047d29646SDavid S. Miller return skb_pull_inline(skb, len); 20516be8ac2fSIlpo Järvinen } 20526be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 20536be8ac2fSIlpo Järvinen 2054419ae74eSIlpo Järvinen /** 205513244cccSLuiz Augusto von Dentz * skb_pull_data - remove data from the start of a buffer returning its 205613244cccSLuiz Augusto von Dentz * original position. 205713244cccSLuiz Augusto von Dentz * @skb: buffer to use 205813244cccSLuiz Augusto von Dentz * @len: amount of data to remove 205913244cccSLuiz Augusto von Dentz * 206013244cccSLuiz Augusto von Dentz * This function removes data from the start of a buffer, returning 206113244cccSLuiz Augusto von Dentz * the memory to the headroom. A pointer to the original data in the buffer 206213244cccSLuiz Augusto von Dentz * is returned after checking if there is enough data to pull. Once the 206313244cccSLuiz Augusto von Dentz * data has been pulled future pushes will overwrite the old data. 206413244cccSLuiz Augusto von Dentz */ 206513244cccSLuiz Augusto von Dentz void *skb_pull_data(struct sk_buff *skb, size_t len) 206613244cccSLuiz Augusto von Dentz { 206713244cccSLuiz Augusto von Dentz void *data = skb->data; 206813244cccSLuiz Augusto von Dentz 206913244cccSLuiz Augusto von Dentz if (skb->len < len) 207013244cccSLuiz Augusto von Dentz return NULL; 207113244cccSLuiz Augusto von Dentz 207213244cccSLuiz Augusto von Dentz skb_pull(skb, len); 207313244cccSLuiz Augusto von Dentz 207413244cccSLuiz Augusto von Dentz return data; 207513244cccSLuiz Augusto von Dentz } 207613244cccSLuiz Augusto von Dentz EXPORT_SYMBOL(skb_pull_data); 207713244cccSLuiz Augusto von Dentz 207813244cccSLuiz Augusto von Dentz /** 2079419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 2080419ae74eSIlpo Järvinen * @skb: buffer to alter 2081419ae74eSIlpo Järvinen * @len: new length 2082419ae74eSIlpo Järvinen * 2083419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 2084419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 2085419ae74eSIlpo Järvinen * The skb must be linear. 2086419ae74eSIlpo Järvinen */ 2087419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 2088419ae74eSIlpo Järvinen { 2089419ae74eSIlpo Järvinen if (skb->len > len) 2090419ae74eSIlpo Järvinen __skb_trim(skb, len); 2091419ae74eSIlpo Järvinen } 2092419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 2093419ae74eSIlpo Järvinen 20943cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 20951da177e4SLinus Torvalds */ 20961da177e4SLinus Torvalds 20973cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 20981da177e4SLinus Torvalds { 209927b437c8SHerbert Xu struct sk_buff **fragp; 210027b437c8SHerbert Xu struct sk_buff *frag; 21011da177e4SLinus Torvalds int offset = skb_headlen(skb); 21021da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 21031da177e4SLinus Torvalds int i; 210427b437c8SHerbert Xu int err; 210527b437c8SHerbert Xu 210627b437c8SHerbert Xu if (skb_cloned(skb) && 210727b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 210827b437c8SHerbert Xu return err; 21091da177e4SLinus Torvalds 2110f4d26fb3SHerbert Xu i = 0; 2111f4d26fb3SHerbert Xu if (offset >= len) 2112f4d26fb3SHerbert Xu goto drop_pages; 2113f4d26fb3SHerbert Xu 2114f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 21159e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 211627b437c8SHerbert Xu 211727b437c8SHerbert Xu if (end < len) { 21181da177e4SLinus Torvalds offset = end; 211927b437c8SHerbert Xu continue; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 21229e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 212327b437c8SHerbert Xu 2124f4d26fb3SHerbert Xu drop_pages: 212527b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 212627b437c8SHerbert Xu 212727b437c8SHerbert Xu for (; i < nfrags; i++) 2128ea2ab693SIan Campbell skb_frag_unref(skb, i); 212927b437c8SHerbert Xu 213021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 213127b437c8SHerbert Xu skb_drop_fraglist(skb); 2132f4d26fb3SHerbert Xu goto done; 213327b437c8SHerbert Xu } 213427b437c8SHerbert Xu 213527b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 213627b437c8SHerbert Xu fragp = &frag->next) { 213727b437c8SHerbert Xu int end = offset + frag->len; 213827b437c8SHerbert Xu 213927b437c8SHerbert Xu if (skb_shared(frag)) { 214027b437c8SHerbert Xu struct sk_buff *nfrag; 214127b437c8SHerbert Xu 214227b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 214327b437c8SHerbert Xu if (unlikely(!nfrag)) 214427b437c8SHerbert Xu return -ENOMEM; 214527b437c8SHerbert Xu 214627b437c8SHerbert Xu nfrag->next = frag->next; 214785bb2a60SEric Dumazet consume_skb(frag); 214827b437c8SHerbert Xu frag = nfrag; 214927b437c8SHerbert Xu *fragp = frag; 215027b437c8SHerbert Xu } 215127b437c8SHerbert Xu 215227b437c8SHerbert Xu if (end < len) { 215327b437c8SHerbert Xu offset = end; 215427b437c8SHerbert Xu continue; 215527b437c8SHerbert Xu } 215627b437c8SHerbert Xu 215727b437c8SHerbert Xu if (end > len && 215827b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 215927b437c8SHerbert Xu return err; 216027b437c8SHerbert Xu 216127b437c8SHerbert Xu if (frag->next) 216227b437c8SHerbert Xu skb_drop_list(&frag->next); 216327b437c8SHerbert Xu break; 216427b437c8SHerbert Xu } 216527b437c8SHerbert Xu 2166f4d26fb3SHerbert Xu done: 216727b437c8SHerbert Xu if (len > skb_headlen(skb)) { 21681da177e4SLinus Torvalds skb->data_len -= skb->len - len; 21691da177e4SLinus Torvalds skb->len = len; 21701da177e4SLinus Torvalds } else { 21711da177e4SLinus Torvalds skb->len = len; 21721da177e4SLinus Torvalds skb->data_len = 0; 217327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 2176c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2177c21b48ccSEric Dumazet skb_condense(skb); 21781da177e4SLinus Torvalds return 0; 21791da177e4SLinus Torvalds } 2180b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 21811da177e4SLinus Torvalds 218288078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 218388078d98SEric Dumazet */ 218488078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 218588078d98SEric Dumazet { 218688078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 218788078d98SEric Dumazet int delta = skb->len - len; 218888078d98SEric Dumazet 2189d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2190d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2191d55bef50SDimitris Michailidis len); 219254970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 219354970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 219454970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 219554970a2fSVasily Averin 219654970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 219754970a2fSVasily Averin return -EINVAL; 219888078d98SEric Dumazet } 219988078d98SEric Dumazet return __pskb_trim(skb, len); 220088078d98SEric Dumazet } 220188078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 220288078d98SEric Dumazet 22031da177e4SLinus Torvalds /** 22041da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 22051da177e4SLinus Torvalds * @skb: buffer to reallocate 22061da177e4SLinus Torvalds * @delta: number of bytes to advance tail 22071da177e4SLinus Torvalds * 22081da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 22091da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 22101da177e4SLinus Torvalds * data from fragmented part. 22111da177e4SLinus Torvalds * 22121da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 22131da177e4SLinus Torvalds * 22141da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 22151da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 22161da177e4SLinus Torvalds * 22171da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 22181da177e4SLinus Torvalds * reloaded after call to this function. 22191da177e4SLinus Torvalds */ 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 22221da177e4SLinus Torvalds * when it is necessary. 22231da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 22241da177e4SLinus Torvalds * 2. It may change skb pointers. 22251da177e4SLinus Torvalds * 22261da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 22271da177e4SLinus Torvalds */ 2228af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 22291da177e4SLinus Torvalds { 22301da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 22311da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 22321da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 22331da177e4SLinus Torvalds */ 22344305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 22371da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 22381da177e4SLinus Torvalds GFP_ATOMIC)) 22391da177e4SLinus Torvalds return NULL; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22429f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 22439f77fad3STim Hansen skb_tail_pointer(skb), delta)); 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 22461da177e4SLinus Torvalds * size of pulled pages. Superb. 22471da177e4SLinus Torvalds */ 224821dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 22491da177e4SLinus Torvalds goto pull_pages; 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 22521da177e4SLinus Torvalds eat = delta; 22531da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22549e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 22559e903e08SEric Dumazet 22569e903e08SEric Dumazet if (size >= eat) 22571da177e4SLinus Torvalds goto pull_pages; 22589e903e08SEric Dumazet eat -= size; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 226209001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 22631da177e4SLinus Torvalds * but taking into account that pulling is expected to 22641da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 22651da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 22661da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 22671da177e4SLinus Torvalds */ 22681da177e4SLinus Torvalds if (eat) { 22691da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 22701da177e4SLinus Torvalds struct sk_buff *clone = NULL; 22711da177e4SLinus Torvalds struct sk_buff *insp = NULL; 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds do { 22741da177e4SLinus Torvalds if (list->len <= eat) { 22751da177e4SLinus Torvalds /* Eaten as whole. */ 22761da177e4SLinus Torvalds eat -= list->len; 22771da177e4SLinus Torvalds list = list->next; 22781da177e4SLinus Torvalds insp = list; 22791da177e4SLinus Torvalds } else { 22801da177e4SLinus Torvalds /* Eaten partially. */ 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds if (skb_shared(list)) { 22831da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 22841da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 22851da177e4SLinus Torvalds if (!clone) 22861da177e4SLinus Torvalds return NULL; 22871da177e4SLinus Torvalds insp = list->next; 22881da177e4SLinus Torvalds list = clone; 22891da177e4SLinus Torvalds } else { 22901da177e4SLinus Torvalds /* This may be pulled without 22911da177e4SLinus Torvalds * problems. */ 22921da177e4SLinus Torvalds insp = list; 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 22951da177e4SLinus Torvalds kfree_skb(clone); 22961da177e4SLinus Torvalds return NULL; 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds break; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds } while (eat); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds /* Free pulled out fragments. */ 23031da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 23041da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 2305ef527f96SEric Dumazet consume_skb(list); 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds /* And insert new clone at head. */ 23081da177e4SLinus Torvalds if (clone) { 23091da177e4SLinus Torvalds clone->next = list; 23101da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds pull_pages: 23161da177e4SLinus Torvalds eat = delta; 23171da177e4SLinus Torvalds k = 0; 23181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 23199e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 23209e903e08SEric Dumazet 23219e903e08SEric Dumazet if (size <= eat) { 2322ea2ab693SIan Campbell skb_frag_unref(skb, i); 23239e903e08SEric Dumazet eat -= size; 23241da177e4SLinus Torvalds } else { 2325b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2326b54c9d5bSJonathan Lemon 2327b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 23281da177e4SLinus Torvalds if (eat) { 2329b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2330b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 23313ccc6c6fSlinzhang if (!i) 23323ccc6c6fSlinzhang goto end; 23331da177e4SLinus Torvalds eat = 0; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds k++; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 23391da177e4SLinus Torvalds 23403ccc6c6fSlinzhang end: 23411da177e4SLinus Torvalds skb->tail += delta; 23421da177e4SLinus Torvalds skb->data_len -= delta; 23431da177e4SLinus Torvalds 23441f8b977aSWillem de Bruijn if (!skb->data_len) 23451f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 23461f8b977aSWillem de Bruijn 234727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 23481da177e4SLinus Torvalds } 2349b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 23501da177e4SLinus Torvalds 235122019b17SEric Dumazet /** 235222019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 235322019b17SEric Dumazet * @skb: source skb 235422019b17SEric Dumazet * @offset: offset in source 235522019b17SEric Dumazet * @to: destination buffer 235622019b17SEric Dumazet * @len: number of bytes to copy 235722019b17SEric Dumazet * 235822019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 235922019b17SEric Dumazet * destination buffer. 236022019b17SEric Dumazet * 236122019b17SEric Dumazet * CAUTION ! : 236222019b17SEric Dumazet * If its prototype is ever changed, 236322019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 236422019b17SEric Dumazet * since it is called from BPF assembly code. 236522019b17SEric Dumazet */ 23661da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 23671da177e4SLinus Torvalds { 23681a028e50SDavid S. Miller int start = skb_headlen(skb); 2369fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2370fbb398a8SDavid S. Miller int i, copy; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds if (offset > (int)skb->len - len) 23731da177e4SLinus Torvalds goto fault; 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds /* Copy header. */ 23761a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 23771da177e4SLinus Torvalds if (copy > len) 23781da177e4SLinus Torvalds copy = len; 2379d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 23801da177e4SLinus Torvalds if ((len -= copy) == 0) 23811da177e4SLinus Torvalds return 0; 23821da177e4SLinus Torvalds offset += copy; 23831da177e4SLinus Torvalds to += copy; 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 23861da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 23871a028e50SDavid S. Miller int end; 238851c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 23891da177e4SLinus Torvalds 2390547b792cSIlpo Järvinen WARN_ON(start > offset + len); 23911a028e50SDavid S. Miller 239251c56b00SEric Dumazet end = start + skb_frag_size(f); 23931da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2394c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2395c613c209SWillem de Bruijn struct page *p; 23961da177e4SLinus Torvalds u8 *vaddr; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds if (copy > len) 23991da177e4SLinus Torvalds copy = len; 24001da177e4SLinus Torvalds 2401c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2402b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2403c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2404c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2405c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 240651c56b00SEric Dumazet kunmap_atomic(vaddr); 2407c613c209SWillem de Bruijn } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds if ((len -= copy) == 0) 24101da177e4SLinus Torvalds return 0; 24111da177e4SLinus Torvalds offset += copy; 24121da177e4SLinus Torvalds to += copy; 24131da177e4SLinus Torvalds } 24141a028e50SDavid S. Miller start = end; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 2417fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 24181a028e50SDavid S. Miller int end; 24191da177e4SLinus Torvalds 2420547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24211a028e50SDavid S. Miller 2422fbb398a8SDavid S. Miller end = start + frag_iter->len; 24231da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 24241da177e4SLinus Torvalds if (copy > len) 24251da177e4SLinus Torvalds copy = len; 2426fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 24271da177e4SLinus Torvalds goto fault; 24281da177e4SLinus Torvalds if ((len -= copy) == 0) 24291da177e4SLinus Torvalds return 0; 24301da177e4SLinus Torvalds offset += copy; 24311da177e4SLinus Torvalds to += copy; 24321da177e4SLinus Torvalds } 24331a028e50SDavid S. Miller start = end; 24341da177e4SLinus Torvalds } 2435a6686f2fSShirley Ma 24361da177e4SLinus Torvalds if (!len) 24371da177e4SLinus Torvalds return 0; 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds fault: 24401da177e4SLinus Torvalds return -EFAULT; 24411da177e4SLinus Torvalds } 2442b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 24431da177e4SLinus Torvalds 24449c55e01cSJens Axboe /* 24459c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 24469c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 24479c55e01cSJens Axboe */ 24489c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 24499c55e01cSJens Axboe { 24508b9d3728SJarek Poplawski put_page(spd->pages[i]); 24518b9d3728SJarek Poplawski } 24529c55e01cSJens Axboe 2453a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 24544fb66994SJarek Poplawski unsigned int *offset, 245518aafc62SEric Dumazet struct sock *sk) 24568b9d3728SJarek Poplawski { 24575640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 24588b9d3728SJarek Poplawski 24595640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 24608b9d3728SJarek Poplawski return NULL; 24614fb66994SJarek Poplawski 24625640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 24634fb66994SJarek Poplawski 24645640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 24655640f768SEric Dumazet page_address(page) + *offset, *len); 24665640f768SEric Dumazet *offset = pfrag->offset; 24675640f768SEric Dumazet pfrag->offset += *len; 24684fb66994SJarek Poplawski 24695640f768SEric Dumazet return pfrag->page; 24709c55e01cSJens Axboe } 24719c55e01cSJens Axboe 247241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 247341c73a0dSEric Dumazet struct page *page, 247441c73a0dSEric Dumazet unsigned int offset) 247541c73a0dSEric Dumazet { 247641c73a0dSEric Dumazet return spd->nr_pages && 247741c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 247841c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 247941c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 248041c73a0dSEric Dumazet } 248141c73a0dSEric Dumazet 24829c55e01cSJens Axboe /* 24839c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 24849c55e01cSJens Axboe */ 2485a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 248635f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 24874fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 248818aafc62SEric Dumazet bool linear, 24897a67e56fSJarek Poplawski struct sock *sk) 24909c55e01cSJens Axboe { 249141c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2492a108d5f3SDavid S. Miller return true; 24939c55e01cSJens Axboe 24948b9d3728SJarek Poplawski if (linear) { 249518aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 24968b9d3728SJarek Poplawski if (!page) 2497a108d5f3SDavid S. Miller return true; 249841c73a0dSEric Dumazet } 249941c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 250041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2501a108d5f3SDavid S. Miller return false; 250241c73a0dSEric Dumazet } 25038b9d3728SJarek Poplawski get_page(page); 25049c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 25054fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 25069c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 25079c55e01cSJens Axboe spd->nr_pages++; 25088b9d3728SJarek Poplawski 2509a108d5f3SDavid S. Miller return false; 25109c55e01cSJens Axboe } 25119c55e01cSJens Axboe 2512a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 25132870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 251418aafc62SEric Dumazet unsigned int *len, 2515d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 251635f3d14dSJens Axboe struct sock *sk, 251735f3d14dSJens Axboe struct pipe_inode_info *pipe) 25189c55e01cSJens Axboe { 25192870c43dSOctavian Purdila if (!*len) 2520a108d5f3SDavid S. Miller return true; 25219c55e01cSJens Axboe 25222870c43dSOctavian Purdila /* skip this segment if already processed */ 25232870c43dSOctavian Purdila if (*off >= plen) { 25242870c43dSOctavian Purdila *off -= plen; 2525a108d5f3SDavid S. Miller return false; 25262870c43dSOctavian Purdila } 25272870c43dSOctavian Purdila 25282870c43dSOctavian Purdila /* ignore any bits we already processed */ 25299ca1b22dSEric Dumazet poff += *off; 25309ca1b22dSEric Dumazet plen -= *off; 25312870c43dSOctavian Purdila *off = 0; 25322870c43dSOctavian Purdila 253318aafc62SEric Dumazet do { 253418aafc62SEric Dumazet unsigned int flen = min(*len, plen); 25352870c43dSOctavian Purdila 253618aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 253718aafc62SEric Dumazet linear, sk)) 2538a108d5f3SDavid S. Miller return true; 253918aafc62SEric Dumazet poff += flen; 254018aafc62SEric Dumazet plen -= flen; 25412870c43dSOctavian Purdila *len -= flen; 254218aafc62SEric Dumazet } while (*len && plen); 25432870c43dSOctavian Purdila 2544a108d5f3SDavid S. Miller return false; 2545db43a282SOctavian Purdila } 25469c55e01cSJens Axboe 25479c55e01cSJens Axboe /* 2548a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 25492870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 25509c55e01cSJens Axboe */ 2551a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 255235f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 255335f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 25542870c43dSOctavian Purdila { 25552870c43dSOctavian Purdila int seg; 2556fa9835e5STom Herbert struct sk_buff *iter; 25579c55e01cSJens Axboe 25581d0c0b32SEric Dumazet /* map the linear part : 25592996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 25602996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 25612996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 25629c55e01cSJens Axboe */ 25632870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 25642870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 25652870c43dSOctavian Purdila skb_headlen(skb), 256618aafc62SEric Dumazet offset, len, spd, 25673a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 25681d0c0b32SEric Dumazet sk, pipe)) 2569a108d5f3SDavid S. Miller return true; 25709c55e01cSJens Axboe 25719c55e01cSJens Axboe /* 25729c55e01cSJens Axboe * then map the fragments 25739c55e01cSJens Axboe */ 25749c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 25759c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 25769c55e01cSJens Axboe 2577ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2578b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 257918aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2580a108d5f3SDavid S. Miller return true; 25819c55e01cSJens Axboe } 25829c55e01cSJens Axboe 2583fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2584fa9835e5STom Herbert if (*offset >= iter->len) { 2585fa9835e5STom Herbert *offset -= iter->len; 2586fa9835e5STom Herbert continue; 2587fa9835e5STom Herbert } 2588fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2589fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2590fa9835e5STom Herbert * case. 2591fa9835e5STom Herbert */ 2592fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2593fa9835e5STom Herbert return true; 2594fa9835e5STom Herbert } 2595fa9835e5STom Herbert 2596a108d5f3SDavid S. Miller return false; 25979c55e01cSJens Axboe } 25989c55e01cSJens Axboe 25999c55e01cSJens Axboe /* 26009c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2601fa9835e5STom Herbert * the fragments, and the frag list. 26029c55e01cSJens Axboe */ 2603a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 26049c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 260525869262SAl Viro unsigned int flags) 26069c55e01cSJens Axboe { 260741c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 260841c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 26099c55e01cSJens Axboe struct splice_pipe_desc spd = { 26109c55e01cSJens Axboe .pages = pages, 26119c55e01cSJens Axboe .partial = partial, 2612047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 261328a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 26149c55e01cSJens Axboe .spd_release = sock_spd_release, 26159c55e01cSJens Axboe }; 261635f3d14dSJens Axboe int ret = 0; 261735f3d14dSJens Axboe 2618fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 26199c55e01cSJens Axboe 2620a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 262125869262SAl Viro ret = splice_to_pipe(pipe, &spd); 26229c55e01cSJens Axboe 262335f3d14dSJens Axboe return ret; 26249c55e01cSJens Axboe } 26252b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 26269c55e01cSJens Axboe 26270739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 26280739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 26290739cd28SCong Wang { 26300739cd28SCong Wang struct socket *sock = sk->sk_socket; 26310739cd28SCong Wang 26320739cd28SCong Wang if (!sock) 26330739cd28SCong Wang return -EINVAL; 26340739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 26350739cd28SCong Wang } 26360739cd28SCong Wang 26370739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 26380739cd28SCong Wang size_t size, int flags) 26390739cd28SCong Wang { 26400739cd28SCong Wang struct socket *sock = sk->sk_socket; 26410739cd28SCong Wang 26420739cd28SCong Wang if (!sock) 26430739cd28SCong Wang return -EINVAL; 26440739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 26450739cd28SCong Wang } 26460739cd28SCong Wang 26470739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 26480739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 26490739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 26500739cd28SCong Wang size_t size, int flags); 26510739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 26520739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 265320bf50deSTom Herbert { 265420bf50deSTom Herbert unsigned int orig_len = len; 265520bf50deSTom Herbert struct sk_buff *head = skb; 265620bf50deSTom Herbert unsigned short fragidx; 265720bf50deSTom Herbert int slen, ret; 265820bf50deSTom Herbert 265920bf50deSTom Herbert do_frag_list: 266020bf50deSTom Herbert 266120bf50deSTom Herbert /* Deal with head data */ 266220bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 266320bf50deSTom Herbert struct kvec kv; 266420bf50deSTom Herbert struct msghdr msg; 266520bf50deSTom Herbert 266620bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 266720bf50deSTom Herbert kv.iov_base = skb->data + offset; 2668db5980d8SJohn Fastabend kv.iov_len = slen; 266920bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2670bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 267120bf50deSTom Herbert 26720739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 26730739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 267420bf50deSTom Herbert if (ret <= 0) 267520bf50deSTom Herbert goto error; 267620bf50deSTom Herbert 267720bf50deSTom Herbert offset += ret; 267820bf50deSTom Herbert len -= ret; 267920bf50deSTom Herbert } 268020bf50deSTom Herbert 268120bf50deSTom Herbert /* All the data was skb head? */ 268220bf50deSTom Herbert if (!len) 268320bf50deSTom Herbert goto out; 268420bf50deSTom Herbert 268520bf50deSTom Herbert /* Make offset relative to start of frags */ 268620bf50deSTom Herbert offset -= skb_headlen(skb); 268720bf50deSTom Herbert 268820bf50deSTom Herbert /* Find where we are in frag list */ 268920bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 269020bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 269120bf50deSTom Herbert 2692d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 269320bf50deSTom Herbert break; 269420bf50deSTom Herbert 2695d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 269620bf50deSTom Herbert } 269720bf50deSTom Herbert 269820bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 269920bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 270020bf50deSTom Herbert 2701d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 270220bf50deSTom Herbert 270320bf50deSTom Herbert while (slen) { 27040739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 27050739cd28SCong Wang sendpage_unlocked, sk, 27060739cd28SCong Wang skb_frag_page(frag), 2707b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 270820bf50deSTom Herbert slen, MSG_DONTWAIT); 270920bf50deSTom Herbert if (ret <= 0) 271020bf50deSTom Herbert goto error; 271120bf50deSTom Herbert 271220bf50deSTom Herbert len -= ret; 271320bf50deSTom Herbert offset += ret; 271420bf50deSTom Herbert slen -= ret; 271520bf50deSTom Herbert } 271620bf50deSTom Herbert 271720bf50deSTom Herbert offset = 0; 271820bf50deSTom Herbert } 271920bf50deSTom Herbert 272020bf50deSTom Herbert if (len) { 272120bf50deSTom Herbert /* Process any frag lists */ 272220bf50deSTom Herbert 272320bf50deSTom Herbert if (skb == head) { 272420bf50deSTom Herbert if (skb_has_frag_list(skb)) { 272520bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 272620bf50deSTom Herbert goto do_frag_list; 272720bf50deSTom Herbert } 272820bf50deSTom Herbert } else if (skb->next) { 272920bf50deSTom Herbert skb = skb->next; 273020bf50deSTom Herbert goto do_frag_list; 273120bf50deSTom Herbert } 273220bf50deSTom Herbert } 273320bf50deSTom Herbert 273420bf50deSTom Herbert out: 273520bf50deSTom Herbert return orig_len - len; 273620bf50deSTom Herbert 273720bf50deSTom Herbert error: 273820bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 273920bf50deSTom Herbert } 27400739cd28SCong Wang 27410739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 27420739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 27430739cd28SCong Wang int len) 27440739cd28SCong Wang { 27450739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 27460739cd28SCong Wang kernel_sendpage_locked); 27470739cd28SCong Wang } 274820bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 274920bf50deSTom Herbert 27500739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 27510739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 27520739cd28SCong Wang { 27530739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 27540739cd28SCong Wang sendpage_unlocked); 27550739cd28SCong Wang } 27560739cd28SCong Wang 2757357b40a1SHerbert Xu /** 2758357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2759357b40a1SHerbert Xu * @skb: destination buffer 2760357b40a1SHerbert Xu * @offset: offset in destination 2761357b40a1SHerbert Xu * @from: source buffer 2762357b40a1SHerbert Xu * @len: number of bytes to copy 2763357b40a1SHerbert Xu * 2764357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2765357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2766357b40a1SHerbert Xu * traversing fragment lists and such. 2767357b40a1SHerbert Xu */ 2768357b40a1SHerbert Xu 27690c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2770357b40a1SHerbert Xu { 27711a028e50SDavid S. Miller int start = skb_headlen(skb); 2772fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2773fbb398a8SDavid S. Miller int i, copy; 2774357b40a1SHerbert Xu 2775357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2776357b40a1SHerbert Xu goto fault; 2777357b40a1SHerbert Xu 27781a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2779357b40a1SHerbert Xu if (copy > len) 2780357b40a1SHerbert Xu copy = len; 278127d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2782357b40a1SHerbert Xu if ((len -= copy) == 0) 2783357b40a1SHerbert Xu return 0; 2784357b40a1SHerbert Xu offset += copy; 2785357b40a1SHerbert Xu from += copy; 2786357b40a1SHerbert Xu } 2787357b40a1SHerbert Xu 2788357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2789357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 27901a028e50SDavid S. Miller int end; 2791357b40a1SHerbert Xu 2792547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27931a028e50SDavid S. Miller 27949e903e08SEric Dumazet end = start + skb_frag_size(frag); 2795357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2796c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2797c613c209SWillem de Bruijn struct page *p; 2798357b40a1SHerbert Xu u8 *vaddr; 2799357b40a1SHerbert Xu 2800357b40a1SHerbert Xu if (copy > len) 2801357b40a1SHerbert Xu copy = len; 2802357b40a1SHerbert Xu 2803c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2804b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2805c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2806c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2807c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 280851c56b00SEric Dumazet kunmap_atomic(vaddr); 2809c613c209SWillem de Bruijn } 2810357b40a1SHerbert Xu 2811357b40a1SHerbert Xu if ((len -= copy) == 0) 2812357b40a1SHerbert Xu return 0; 2813357b40a1SHerbert Xu offset += copy; 2814357b40a1SHerbert Xu from += copy; 2815357b40a1SHerbert Xu } 28161a028e50SDavid S. Miller start = end; 2817357b40a1SHerbert Xu } 2818357b40a1SHerbert Xu 2819fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 28201a028e50SDavid S. Miller int end; 2821357b40a1SHerbert Xu 2822547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28231a028e50SDavid S. Miller 2824fbb398a8SDavid S. Miller end = start + frag_iter->len; 2825357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2826357b40a1SHerbert Xu if (copy > len) 2827357b40a1SHerbert Xu copy = len; 2828fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 28291a028e50SDavid S. Miller from, copy)) 2830357b40a1SHerbert Xu goto fault; 2831357b40a1SHerbert Xu if ((len -= copy) == 0) 2832357b40a1SHerbert Xu return 0; 2833357b40a1SHerbert Xu offset += copy; 2834357b40a1SHerbert Xu from += copy; 2835357b40a1SHerbert Xu } 28361a028e50SDavid S. Miller start = end; 2837357b40a1SHerbert Xu } 2838357b40a1SHerbert Xu if (!len) 2839357b40a1SHerbert Xu return 0; 2840357b40a1SHerbert Xu 2841357b40a1SHerbert Xu fault: 2842357b40a1SHerbert Xu return -EFAULT; 2843357b40a1SHerbert Xu } 2844357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2845357b40a1SHerbert Xu 28461da177e4SLinus Torvalds /* Checksum skb data. */ 28472817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 28482817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 28491da177e4SLinus Torvalds { 28501a028e50SDavid S. Miller int start = skb_headlen(skb); 28511a028e50SDavid S. Miller int i, copy = start - offset; 2852fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 28531da177e4SLinus Torvalds int pos = 0; 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds /* Checksum header. */ 28561da177e4SLinus Torvalds if (copy > 0) { 28571da177e4SLinus Torvalds if (copy > len) 28581da177e4SLinus Torvalds copy = len; 28592544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 28602544af03SMatteo Croce skb->data + offset, copy, csum); 28611da177e4SLinus Torvalds if ((len -= copy) == 0) 28621da177e4SLinus Torvalds return csum; 28631da177e4SLinus Torvalds offset += copy; 28641da177e4SLinus Torvalds pos = copy; 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 28681a028e50SDavid S. Miller int end; 286951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 28701da177e4SLinus Torvalds 2871547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28721a028e50SDavid S. Miller 287351c56b00SEric Dumazet end = start + skb_frag_size(frag); 28741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2875c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2876c613c209SWillem de Bruijn struct page *p; 287744bb9363SAl Viro __wsum csum2; 28781da177e4SLinus Torvalds u8 *vaddr; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds if (copy > len) 28811da177e4SLinus Torvalds copy = len; 2882c613c209SWillem de Bruijn 2883c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2884b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2885c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2886c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 28872544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 28882544af03SMatteo Croce csum_partial_ext, 28892544af03SMatteo Croce vaddr + p_off, p_len, 0); 289051c56b00SEric Dumazet kunmap_atomic(vaddr); 28912544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 28922544af03SMatteo Croce csum_block_add_ext, csum, 28932544af03SMatteo Croce csum2, pos, p_len); 2894c613c209SWillem de Bruijn pos += p_len; 2895c613c209SWillem de Bruijn } 2896c613c209SWillem de Bruijn 28971da177e4SLinus Torvalds if (!(len -= copy)) 28981da177e4SLinus Torvalds return csum; 28991da177e4SLinus Torvalds offset += copy; 29001da177e4SLinus Torvalds } 29011a028e50SDavid S. Miller start = end; 29021da177e4SLinus Torvalds } 29031da177e4SLinus Torvalds 2904fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 29051a028e50SDavid S. Miller int end; 29061da177e4SLinus Torvalds 2907547b792cSIlpo Järvinen WARN_ON(start > offset + len); 29081a028e50SDavid S. Miller 2909fbb398a8SDavid S. Miller end = start + frag_iter->len; 29101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 29115f92a738SAl Viro __wsum csum2; 29121da177e4SLinus Torvalds if (copy > len) 29131da177e4SLinus Torvalds copy = len; 29142817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 29152817a336SDaniel Borkmann copy, 0, ops); 29162544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 29172544af03SMatteo Croce csum, csum2, pos, copy); 29181da177e4SLinus Torvalds if ((len -= copy) == 0) 29191da177e4SLinus Torvalds return csum; 29201da177e4SLinus Torvalds offset += copy; 29211da177e4SLinus Torvalds pos += copy; 29221da177e4SLinus Torvalds } 29231a028e50SDavid S. Miller start = end; 29241da177e4SLinus Torvalds } 292509a62660SKris Katterjohn BUG_ON(len); 29261da177e4SLinus Torvalds 29271da177e4SLinus Torvalds return csum; 29281da177e4SLinus Torvalds } 29292817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 29302817a336SDaniel Borkmann 29312817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 29322817a336SDaniel Borkmann int len, __wsum csum) 29332817a336SDaniel Borkmann { 29342817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2935cea80ea8SDaniel Borkmann .update = csum_partial_ext, 29362817a336SDaniel Borkmann .combine = csum_block_add_ext, 29372817a336SDaniel Borkmann }; 29382817a336SDaniel Borkmann 29392817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 29402817a336SDaniel Borkmann } 2941b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds /* Both of above in one bottle. */ 29441da177e4SLinus Torvalds 294581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 29468d5930dfSAl Viro u8 *to, int len) 29471da177e4SLinus Torvalds { 29481a028e50SDavid S. Miller int start = skb_headlen(skb); 29491a028e50SDavid S. Miller int i, copy = start - offset; 2950fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 29511da177e4SLinus Torvalds int pos = 0; 29528d5930dfSAl Viro __wsum csum = 0; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds /* Copy header. */ 29551da177e4SLinus Torvalds if (copy > 0) { 29561da177e4SLinus Torvalds if (copy > len) 29571da177e4SLinus Torvalds copy = len; 29581da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 2959cc44c17bSAl Viro copy); 29601da177e4SLinus Torvalds if ((len -= copy) == 0) 29611da177e4SLinus Torvalds return csum; 29621da177e4SLinus Torvalds offset += copy; 29631da177e4SLinus Torvalds to += copy; 29641da177e4SLinus Torvalds pos = copy; 29651da177e4SLinus Torvalds } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 29681a028e50SDavid S. Miller int end; 29691da177e4SLinus Torvalds 2970547b792cSIlpo Järvinen WARN_ON(start > offset + len); 29711a028e50SDavid S. Miller 29729e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 29731da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2974c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2975c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2976c613c209SWillem de Bruijn struct page *p; 29775084205fSAl Viro __wsum csum2; 29781da177e4SLinus Torvalds u8 *vaddr; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds if (copy > len) 29811da177e4SLinus Torvalds copy = len; 2982c613c209SWillem de Bruijn 2983c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2984b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2985c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2986c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2987c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2988c613c209SWillem de Bruijn to + copied, 2989cc44c17bSAl Viro p_len); 299051c56b00SEric Dumazet kunmap_atomic(vaddr); 29911da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2992c613c209SWillem de Bruijn pos += p_len; 2993c613c209SWillem de Bruijn } 2994c613c209SWillem de Bruijn 29951da177e4SLinus Torvalds if (!(len -= copy)) 29961da177e4SLinus Torvalds return csum; 29971da177e4SLinus Torvalds offset += copy; 29981da177e4SLinus Torvalds to += copy; 29991da177e4SLinus Torvalds } 30001a028e50SDavid S. Miller start = end; 30011da177e4SLinus Torvalds } 30021da177e4SLinus Torvalds 3003fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 300481d77662SAl Viro __wsum csum2; 30051a028e50SDavid S. Miller int end; 30061da177e4SLinus Torvalds 3007547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30081a028e50SDavid S. Miller 3009fbb398a8SDavid S. Miller end = start + frag_iter->len; 30101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 30111da177e4SLinus Torvalds if (copy > len) 30121da177e4SLinus Torvalds copy = len; 3013fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 30141a028e50SDavid S. Miller offset - start, 30158d5930dfSAl Viro to, copy); 30161da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 30171da177e4SLinus Torvalds if ((len -= copy) == 0) 30181da177e4SLinus Torvalds return csum; 30191da177e4SLinus Torvalds offset += copy; 30201da177e4SLinus Torvalds to += copy; 30211da177e4SLinus Torvalds pos += copy; 30221da177e4SLinus Torvalds } 30231a028e50SDavid S. Miller start = end; 30241da177e4SLinus Torvalds } 302509a62660SKris Katterjohn BUG_ON(len); 30261da177e4SLinus Torvalds return csum; 30271da177e4SLinus Torvalds } 3028b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 30291da177e4SLinus Torvalds 303049f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 303149f8e832SCong Wang { 303249f8e832SCong Wang __sum16 sum; 303349f8e832SCong Wang 303449f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 303514641931SCong Wang /* See comments in __skb_checksum_complete(). */ 303649f8e832SCong Wang if (likely(!sum)) { 303749f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 303849f8e832SCong Wang !skb->csum_complete_sw) 30397fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 304049f8e832SCong Wang } 304149f8e832SCong Wang if (!skb_shared(skb)) 304249f8e832SCong Wang skb->csum_valid = !sum; 304349f8e832SCong Wang return sum; 304449f8e832SCong Wang } 304549f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 304649f8e832SCong Wang 304714641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 304814641931SCong Wang * which has been changed from the hardware checksum, for example, by 304914641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 305014641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 305114641931SCong Wang * 305214641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 305314641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 305414641931SCong Wang * shared. 305514641931SCong Wang */ 305649f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 305749f8e832SCong Wang { 305849f8e832SCong Wang __wsum csum; 305949f8e832SCong Wang __sum16 sum; 306049f8e832SCong Wang 306149f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 306249f8e832SCong Wang 306349f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 306414641931SCong Wang /* This check is inverted, because we already knew the hardware 306514641931SCong Wang * checksum is invalid before calling this function. So, if the 306614641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 306714641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 306814641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 306914641931SCong Wang * when moving skb->data around. 307014641931SCong Wang */ 307149f8e832SCong Wang if (likely(!sum)) { 307249f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 307349f8e832SCong Wang !skb->csum_complete_sw) 30747fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 307549f8e832SCong Wang } 307649f8e832SCong Wang 307749f8e832SCong Wang if (!skb_shared(skb)) { 307849f8e832SCong Wang /* Save full packet checksum */ 307949f8e832SCong Wang skb->csum = csum; 308049f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 308149f8e832SCong Wang skb->csum_complete_sw = 1; 308249f8e832SCong Wang skb->csum_valid = !sum; 308349f8e832SCong Wang } 308449f8e832SCong Wang 308549f8e832SCong Wang return sum; 308649f8e832SCong Wang } 308749f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 308849f8e832SCong Wang 30899617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 30909617813dSDavide Caratti { 30919617813dSDavide Caratti net_warn_ratelimited( 30929617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 30939617813dSDavide Caratti __func__); 30949617813dSDavide Caratti return 0; 30959617813dSDavide Caratti } 30969617813dSDavide Caratti 30979617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 30989617813dSDavide Caratti int offset, int len) 30999617813dSDavide Caratti { 31009617813dSDavide Caratti net_warn_ratelimited( 31019617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 31029617813dSDavide Caratti __func__); 31039617813dSDavide Caratti return 0; 31049617813dSDavide Caratti } 31059617813dSDavide Caratti 31069617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 31079617813dSDavide Caratti .update = warn_crc32c_csum_update, 31089617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 31099617813dSDavide Caratti }; 31109617813dSDavide Caratti 31119617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 31129617813dSDavide Caratti &default_crc32c_ops; 31139617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 31149617813dSDavide Caratti 3115af2806f8SThomas Graf /** 3116af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3117af2806f8SThomas Graf * @from: source buffer 3118af2806f8SThomas Graf * 3119af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3120af2806f8SThomas Graf * into skb_zerocopy(). 3121af2806f8SThomas Graf */ 3122af2806f8SThomas Graf unsigned int 3123af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3124af2806f8SThomas Graf { 3125af2806f8SThomas Graf unsigned int hlen = 0; 3126af2806f8SThomas Graf 3127af2806f8SThomas Graf if (!from->head_frag || 3128af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3129a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3130af2806f8SThomas Graf hlen = skb_headlen(from); 3131a17ad096SPravin B Shelar if (!hlen) 3132a17ad096SPravin B Shelar hlen = from->len; 3133a17ad096SPravin B Shelar } 3134af2806f8SThomas Graf 3135af2806f8SThomas Graf if (skb_has_frag_list(from)) 3136af2806f8SThomas Graf hlen = from->len; 3137af2806f8SThomas Graf 3138af2806f8SThomas Graf return hlen; 3139af2806f8SThomas Graf } 3140af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3141af2806f8SThomas Graf 3142af2806f8SThomas Graf /** 3143af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3144af2806f8SThomas Graf * @to: destination buffer 31457fceb4deSMasanari Iida * @from: source buffer 3146af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3147af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3148af2806f8SThomas Graf * 3149af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3150af2806f8SThomas Graf * to the frags in the source buffer. 3151af2806f8SThomas Graf * 3152af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3153af2806f8SThomas Graf * headroom in the `to` buffer. 315436d5fe6aSZoltan Kiss * 315536d5fe6aSZoltan Kiss * Return value: 315636d5fe6aSZoltan Kiss * 0: everything is OK 315736d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 315836d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3159af2806f8SThomas Graf */ 316036d5fe6aSZoltan Kiss int 316136d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3162af2806f8SThomas Graf { 3163af2806f8SThomas Graf int i, j = 0; 3164af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 316536d5fe6aSZoltan Kiss int ret; 3166af2806f8SThomas Graf struct page *page; 3167af2806f8SThomas Graf unsigned int offset; 3168af2806f8SThomas Graf 3169af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3170af2806f8SThomas Graf 3171af2806f8SThomas Graf /* dont bother with small payloads */ 317236d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 317336d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3174af2806f8SThomas Graf 3175af2806f8SThomas Graf if (hlen) { 317636d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 317736d5fe6aSZoltan Kiss if (unlikely(ret)) 317836d5fe6aSZoltan Kiss return ret; 3179af2806f8SThomas Graf len -= hlen; 3180af2806f8SThomas Graf } else { 3181af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3182af2806f8SThomas Graf if (plen) { 3183af2806f8SThomas Graf page = virt_to_head_page(from->head); 3184af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3185af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3186af2806f8SThomas Graf get_page(page); 3187af2806f8SThomas Graf j = 1; 3188af2806f8SThomas Graf len -= plen; 3189af2806f8SThomas Graf } 3190af2806f8SThomas Graf } 3191af2806f8SThomas Graf 3192af2806f8SThomas Graf to->truesize += len + plen; 3193af2806f8SThomas Graf to->len += len + plen; 3194af2806f8SThomas Graf to->data_len += len + plen; 3195af2806f8SThomas Graf 319636d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 319736d5fe6aSZoltan Kiss skb_tx_error(from); 319836d5fe6aSZoltan Kiss return -ENOMEM; 319936d5fe6aSZoltan Kiss } 32001f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 320136d5fe6aSZoltan Kiss 3202af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3203d8e18a51SMatthew Wilcox (Oracle) int size; 3204d8e18a51SMatthew Wilcox (Oracle) 3205af2806f8SThomas Graf if (!len) 3206af2806f8SThomas Graf break; 3207af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3208d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3209d8e18a51SMatthew Wilcox (Oracle) len); 3210d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3211d8e18a51SMatthew Wilcox (Oracle) len -= size; 3212af2806f8SThomas Graf skb_frag_ref(to, j); 3213af2806f8SThomas Graf j++; 3214af2806f8SThomas Graf } 3215af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 321636d5fe6aSZoltan Kiss 321736d5fe6aSZoltan Kiss return 0; 3218af2806f8SThomas Graf } 3219af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3220af2806f8SThomas Graf 32211da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 32221da177e4SLinus Torvalds { 3223d3bc23e7SAl Viro __wsum csum; 32241da177e4SLinus Torvalds long csstart; 32251da177e4SLinus Torvalds 322684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 322755508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 32281da177e4SLinus Torvalds else 32291da177e4SLinus Torvalds csstart = skb_headlen(skb); 32301da177e4SLinus Torvalds 323109a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 32321da177e4SLinus Torvalds 3233d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds csum = 0; 32361da177e4SLinus Torvalds if (csstart != skb->len) 32371da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 32388d5930dfSAl Viro skb->len - csstart); 32391da177e4SLinus Torvalds 324084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3241ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 32421da177e4SLinus Torvalds 3243d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 32441da177e4SLinus Torvalds } 32451da177e4SLinus Torvalds } 3246b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 32471da177e4SLinus Torvalds 32481da177e4SLinus Torvalds /** 32491da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 32501da177e4SLinus Torvalds * @list: list to dequeue from 32511da177e4SLinus Torvalds * 32521da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 32531da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 32541da177e4SLinus Torvalds * returned or %NULL if the list is empty. 32551da177e4SLinus Torvalds */ 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 32581da177e4SLinus Torvalds { 32591da177e4SLinus Torvalds unsigned long flags; 32601da177e4SLinus Torvalds struct sk_buff *result; 32611da177e4SLinus Torvalds 32621da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 32631da177e4SLinus Torvalds result = __skb_dequeue(list); 32641da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 32651da177e4SLinus Torvalds return result; 32661da177e4SLinus Torvalds } 3267b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 32681da177e4SLinus Torvalds 32691da177e4SLinus Torvalds /** 32701da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 32711da177e4SLinus Torvalds * @list: list to dequeue from 32721da177e4SLinus Torvalds * 32731da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 32741da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 32751da177e4SLinus Torvalds * returned or %NULL if the list is empty. 32761da177e4SLinus Torvalds */ 32771da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 32781da177e4SLinus Torvalds { 32791da177e4SLinus Torvalds unsigned long flags; 32801da177e4SLinus Torvalds struct sk_buff *result; 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 32831da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 32841da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 32851da177e4SLinus Torvalds return result; 32861da177e4SLinus Torvalds } 3287b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 32881da177e4SLinus Torvalds 32891da177e4SLinus Torvalds /** 32901da177e4SLinus Torvalds * skb_queue_purge - empty a list 32911da177e4SLinus Torvalds * @list: list to empty 32921da177e4SLinus Torvalds * 32931da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 32941da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 32951da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 32961da177e4SLinus Torvalds */ 32971da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 32981da177e4SLinus Torvalds { 32991da177e4SLinus Torvalds struct sk_buff *skb; 33001da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 33011da177e4SLinus Torvalds kfree_skb(skb); 33021da177e4SLinus Torvalds } 3303b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds /** 33069f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 33079f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3308385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 33099f5afeaeSYaogong Wang * 33109f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 33119f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 33129f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 33139f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 33149f5afeaeSYaogong Wang */ 3315385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 33169f5afeaeSYaogong Wang { 33177c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3318385114deSPeter Oskolkov unsigned int sum = 0; 33199f5afeaeSYaogong Wang 33207c90584cSEric Dumazet while (p) { 33217c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 33227c90584cSEric Dumazet 33237c90584cSEric Dumazet p = rb_next(p); 33247c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3325385114deSPeter Oskolkov sum += skb->truesize; 33269f5afeaeSYaogong Wang kfree_skb(skb); 33277c90584cSEric Dumazet } 3328385114deSPeter Oskolkov return sum; 33299f5afeaeSYaogong Wang } 33309f5afeaeSYaogong Wang 33319f5afeaeSYaogong Wang /** 33321da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 33331da177e4SLinus Torvalds * @list: list to use 33341da177e4SLinus Torvalds * @newsk: buffer to queue 33351da177e4SLinus Torvalds * 33361da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 33371da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 33381da177e4SLinus Torvalds * safely. 33391da177e4SLinus Torvalds * 33401da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 33411da177e4SLinus Torvalds */ 33421da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 33431da177e4SLinus Torvalds { 33441da177e4SLinus Torvalds unsigned long flags; 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 33471da177e4SLinus Torvalds __skb_queue_head(list, newsk); 33481da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 33491da177e4SLinus Torvalds } 3350b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 33511da177e4SLinus Torvalds 33521da177e4SLinus Torvalds /** 33531da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 33541da177e4SLinus Torvalds * @list: list to use 33551da177e4SLinus Torvalds * @newsk: buffer to queue 33561da177e4SLinus Torvalds * 33571da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 33581da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 33591da177e4SLinus Torvalds * safely. 33601da177e4SLinus Torvalds * 33611da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 33621da177e4SLinus Torvalds */ 33631da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 33641da177e4SLinus Torvalds { 33651da177e4SLinus Torvalds unsigned long flags; 33661da177e4SLinus Torvalds 33671da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 33681da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 33691da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 33701da177e4SLinus Torvalds } 3371b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 33728728b834SDavid S. Miller 33731da177e4SLinus Torvalds /** 33741da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 33751da177e4SLinus Torvalds * @skb: buffer to remove 33768728b834SDavid S. Miller * @list: list to use 33771da177e4SLinus Torvalds * 33788728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 33798728b834SDavid S. Miller * function is atomic with respect to other list locked calls 33801da177e4SLinus Torvalds * 33818728b834SDavid S. Miller * You must know what list the SKB is on. 33821da177e4SLinus Torvalds */ 33838728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 33841da177e4SLinus Torvalds { 33851da177e4SLinus Torvalds unsigned long flags; 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 33888728b834SDavid S. Miller __skb_unlink(skb, list); 33891da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 33901da177e4SLinus Torvalds } 3391b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 33921da177e4SLinus Torvalds 33931da177e4SLinus Torvalds /** 33941da177e4SLinus Torvalds * skb_append - append a buffer 33951da177e4SLinus Torvalds * @old: buffer to insert after 33961da177e4SLinus Torvalds * @newsk: buffer to insert 33978728b834SDavid S. Miller * @list: list to use 33981da177e4SLinus Torvalds * 33991da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 34001da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 34011da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 34021da177e4SLinus Torvalds */ 34038728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 34041da177e4SLinus Torvalds { 34051da177e4SLinus Torvalds unsigned long flags; 34061da177e4SLinus Torvalds 34078728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 34087de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 34098728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 34101da177e4SLinus Torvalds } 3411b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 34121da177e4SLinus Torvalds 34131da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 34141da177e4SLinus Torvalds struct sk_buff* skb1, 34151da177e4SLinus Torvalds const u32 len, const int pos) 34161da177e4SLinus Torvalds { 34171da177e4SLinus Torvalds int i; 34181da177e4SLinus Torvalds 3419d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3420d626f62bSArnaldo Carvalho de Melo pos - len); 34211da177e4SLinus Torvalds /* And move data appendix as is. */ 34221da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 34231da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 34261da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 34271da177e4SLinus Torvalds skb1->data_len = skb->data_len; 34281da177e4SLinus Torvalds skb1->len += skb1->data_len; 34291da177e4SLinus Torvalds skb->data_len = 0; 34301da177e4SLinus Torvalds skb->len = len; 343127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 34321da177e4SLinus Torvalds } 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 34351da177e4SLinus Torvalds struct sk_buff* skb1, 34361da177e4SLinus Torvalds const u32 len, int pos) 34371da177e4SLinus Torvalds { 34381da177e4SLinus Torvalds int i, k = 0; 34391da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 34401da177e4SLinus Torvalds 34411da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 34421da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 34431da177e4SLinus Torvalds skb->len = len; 34441da177e4SLinus Torvalds skb->data_len = len - pos; 34451da177e4SLinus Torvalds 34461da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 34479e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds if (pos + size > len) { 34501da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 34511da177e4SLinus Torvalds 34521da177e4SLinus Torvalds if (pos < len) { 34531da177e4SLinus Torvalds /* Split frag. 34541da177e4SLinus Torvalds * We have two variants in this case: 34551da177e4SLinus Torvalds * 1. Move all the frag to the second 34561da177e4SLinus Torvalds * part, if it is possible. F.e. 34571da177e4SLinus Torvalds * this approach is mandatory for TUX, 34581da177e4SLinus Torvalds * where splitting is expensive. 34591da177e4SLinus Torvalds * 2. Split is accurately. We make this. 34601da177e4SLinus Torvalds */ 3461ea2ab693SIan Campbell skb_frag_ref(skb, i); 3462b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 34639e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 34649e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 34651da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 34661da177e4SLinus Torvalds } 34671da177e4SLinus Torvalds k++; 34681da177e4SLinus Torvalds } else 34691da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 34701da177e4SLinus Torvalds pos += size; 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 34731da177e4SLinus Torvalds } 34741da177e4SLinus Torvalds 34751da177e4SLinus Torvalds /** 34761da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 34771da177e4SLinus Torvalds * @skb: the buffer to split 34781da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 34791da177e4SLinus Torvalds * @len: new length for skb 34801da177e4SLinus Torvalds */ 34811da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 34821da177e4SLinus Torvalds { 34831da177e4SLinus Torvalds int pos = skb_headlen(skb); 34849b65b17dSTalal Ahmad const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 34851da177e4SLinus Torvalds 34869b65b17dSTalal Ahmad skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 34871f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 34881da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 34891da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 34901da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 34911da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 34921da177e4SLinus Torvalds } 3493b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 34941da177e4SLinus Torvalds 34959f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 34969f782db3SIlpo Järvinen * 34979f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 34989f782db3SIlpo Järvinen */ 3499832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3500832d11c5SIlpo Järvinen { 3501c4777efaSEric Dumazet return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3502832d11c5SIlpo Järvinen } 3503832d11c5SIlpo Järvinen 3504832d11c5SIlpo Järvinen /** 3505832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3506832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3507832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3508832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3509832d11c5SIlpo Järvinen * 3510832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 351120e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3512832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3513832d11c5SIlpo Järvinen * 3514832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3515832d11c5SIlpo Järvinen * 3516832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3517832d11c5SIlpo Järvinen * to have non-paged data as well. 3518832d11c5SIlpo Järvinen * 3519832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3520832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3521832d11c5SIlpo Järvinen */ 3522832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3523832d11c5SIlpo Järvinen { 3524832d11c5SIlpo Järvinen int from, to, merge, todo; 3525d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3526832d11c5SIlpo Järvinen 3527832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3528f8071cdeSEric Dumazet 3529f8071cdeSEric Dumazet if (skb_headlen(skb)) 3530f8071cdeSEric Dumazet return 0; 35311f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 35321f8b977aSWillem de Bruijn return 0; 3533832d11c5SIlpo Järvinen 3534832d11c5SIlpo Järvinen todo = shiftlen; 3535832d11c5SIlpo Järvinen from = 0; 3536832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3537832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3538832d11c5SIlpo Järvinen 3539832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3540832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3541832d11c5SIlpo Järvinen */ 3542832d11c5SIlpo Järvinen if (!to || 3543ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3544b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3545832d11c5SIlpo Järvinen merge = -1; 3546832d11c5SIlpo Järvinen } else { 3547832d11c5SIlpo Järvinen merge = to - 1; 3548832d11c5SIlpo Järvinen 35499e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3550832d11c5SIlpo Järvinen if (todo < 0) { 3551832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3552832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3553832d11c5SIlpo Järvinen return 0; 3554832d11c5SIlpo Järvinen 35559f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 35569f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3557832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3558832d11c5SIlpo Järvinen 35599e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 35609e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3561b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3562832d11c5SIlpo Järvinen 3563832d11c5SIlpo Järvinen goto onlymerged; 3564832d11c5SIlpo Järvinen } 3565832d11c5SIlpo Järvinen 3566832d11c5SIlpo Järvinen from++; 3567832d11c5SIlpo Järvinen } 3568832d11c5SIlpo Järvinen 3569832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3570832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3571832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3572832d11c5SIlpo Järvinen return 0; 3573832d11c5SIlpo Järvinen 3574832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3575832d11c5SIlpo Järvinen return 0; 3576832d11c5SIlpo Järvinen 3577832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3578832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3579832d11c5SIlpo Järvinen return 0; 3580832d11c5SIlpo Järvinen 3581832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3582832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3583832d11c5SIlpo Järvinen 35849e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3585832d11c5SIlpo Järvinen *fragto = *fragfrom; 35869e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3587832d11c5SIlpo Järvinen from++; 3588832d11c5SIlpo Järvinen to++; 3589832d11c5SIlpo Järvinen 3590832d11c5SIlpo Järvinen } else { 3591ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3592b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3593b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 35949e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3595832d11c5SIlpo Järvinen 3596b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 35979e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3598832d11c5SIlpo Järvinen todo = 0; 3599832d11c5SIlpo Järvinen 3600832d11c5SIlpo Järvinen to++; 3601832d11c5SIlpo Järvinen break; 3602832d11c5SIlpo Järvinen } 3603832d11c5SIlpo Järvinen } 3604832d11c5SIlpo Järvinen 3605832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3606832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3607832d11c5SIlpo Järvinen 3608832d11c5SIlpo Järvinen if (merge >= 0) { 3609832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3610832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3611832d11c5SIlpo Järvinen 36129e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 36136a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3614832d11c5SIlpo Järvinen } 3615832d11c5SIlpo Järvinen 3616832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3617832d11c5SIlpo Järvinen to = 0; 3618832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3619832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3620832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3621832d11c5SIlpo Järvinen 3622832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3623832d11c5SIlpo Järvinen 3624832d11c5SIlpo Järvinen onlymerged: 3625832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3626832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3627832d11c5SIlpo Järvinen */ 3628832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3629832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3630832d11c5SIlpo Järvinen 3631832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3632832d11c5SIlpo Järvinen skb->len -= shiftlen; 3633832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3634832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3635832d11c5SIlpo Järvinen tgt->len += shiftlen; 3636832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3637832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3638832d11c5SIlpo Järvinen 3639832d11c5SIlpo Järvinen return shiftlen; 3640832d11c5SIlpo Järvinen } 3641832d11c5SIlpo Järvinen 3642677e90edSThomas Graf /** 3643677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3644677e90edSThomas Graf * @skb: the buffer to read 3645677e90edSThomas Graf * @from: lower offset of data to be read 3646677e90edSThomas Graf * @to: upper offset of data to be read 3647677e90edSThomas Graf * @st: state variable 3648677e90edSThomas Graf * 3649677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3650677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3651677e90edSThomas Graf */ 3652677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3653677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3654677e90edSThomas Graf { 3655677e90edSThomas Graf st->lower_offset = from; 3656677e90edSThomas Graf st->upper_offset = to; 3657677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3658677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3659677e90edSThomas Graf st->frag_data = NULL; 366097550f6fSWillem de Bruijn st->frag_off = 0; 3661677e90edSThomas Graf } 3662b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3663677e90edSThomas Graf 3664677e90edSThomas Graf /** 3665677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3666677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3667677e90edSThomas Graf * @data: destination pointer for data to be returned 3668677e90edSThomas Graf * @st: state variable 3669677e90edSThomas Graf * 3670bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3671677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3672bc32383cSMathias Krause * the head of the data block to @data and returns the length 3673677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3674677e90edSThomas Graf * offset has been reached. 3675677e90edSThomas Graf * 3676677e90edSThomas Graf * The caller is not required to consume all of the data 3677bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3678677e90edSThomas Graf * of bytes already consumed and the next call to 3679677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3680677e90edSThomas Graf * 368125985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3682e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3683677e90edSThomas Graf * reads of potentially non linear data. 3684677e90edSThomas Graf * 3685bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3686677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3687677e90edSThomas Graf * a stack for this purpose. 3688677e90edSThomas Graf */ 3689677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3690677e90edSThomas Graf struct skb_seq_state *st) 3691677e90edSThomas Graf { 3692677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3693677e90edSThomas Graf skb_frag_t *frag; 3694677e90edSThomas Graf 3695aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3696aeb193eaSWedson Almeida Filho if (st->frag_data) { 3697aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3698aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3699aeb193eaSWedson Almeida Filho } 3700677e90edSThomas Graf return 0; 3701aeb193eaSWedson Almeida Filho } 3702677e90edSThomas Graf 3703677e90edSThomas Graf next_skb: 370495e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3705677e90edSThomas Graf 3706995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 370795e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3708677e90edSThomas Graf return block_limit - abs_offset; 3709677e90edSThomas Graf } 3710677e90edSThomas Graf 3711677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3712677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3713677e90edSThomas Graf 3714677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 371597550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 3716677e90edSThomas Graf 371797550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 371897550f6fSWillem de Bruijn 371997550f6fSWillem de Bruijn pg_idx = 0; 372097550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 372197550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 372297550f6fSWillem de Bruijn 372397550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 372497550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 372597550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 372697550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 372797550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 372897550f6fSWillem de Bruijn } 372997550f6fSWillem de Bruijn 373097550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 3731677e90edSThomas Graf if (abs_offset < block_limit) { 3732677e90edSThomas Graf if (!st->frag_data) 373397550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3734677e90edSThomas Graf 373597550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 3736677e90edSThomas Graf (abs_offset - st->stepped_offset); 3737677e90edSThomas Graf 3738677e90edSThomas Graf return block_limit - abs_offset; 3739677e90edSThomas Graf } 3740677e90edSThomas Graf 3741677e90edSThomas Graf if (st->frag_data) { 374251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3743677e90edSThomas Graf st->frag_data = NULL; 3744677e90edSThomas Graf } 3745677e90edSThomas Graf 374697550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 374797550f6fSWillem de Bruijn st->frag_off += pg_sz; 374897550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 374997550f6fSWillem de Bruijn st->frag_off = 0; 3750677e90edSThomas Graf st->frag_idx++; 375197550f6fSWillem de Bruijn } 3752677e90edSThomas Graf } 3753677e90edSThomas Graf 37545b5a60daSOlaf Kirch if (st->frag_data) { 375551c56b00SEric Dumazet kunmap_atomic(st->frag_data); 37565b5a60daSOlaf Kirch st->frag_data = NULL; 37575b5a60daSOlaf Kirch } 37585b5a60daSOlaf Kirch 375921dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3760677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 376195e3b24cSHerbert Xu st->frag_idx = 0; 3762677e90edSThomas Graf goto next_skb; 376371b3346dSShyam Iyer } else if (st->cur_skb->next) { 376471b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 376571b3346dSShyam Iyer st->frag_idx = 0; 3766677e90edSThomas Graf goto next_skb; 3767677e90edSThomas Graf } 3768677e90edSThomas Graf 3769677e90edSThomas Graf return 0; 3770677e90edSThomas Graf } 3771b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3772677e90edSThomas Graf 3773677e90edSThomas Graf /** 3774677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3775677e90edSThomas Graf * @st: state variable 3776677e90edSThomas Graf * 3777677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3778677e90edSThomas Graf * returned 0. 3779677e90edSThomas Graf */ 3780677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3781677e90edSThomas Graf { 3782677e90edSThomas Graf if (st->frag_data) 378351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3784677e90edSThomas Graf } 3785b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3786677e90edSThomas Graf 37873fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 37883fc7e8a6SThomas Graf 37893fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 37903fc7e8a6SThomas Graf struct ts_config *conf, 37913fc7e8a6SThomas Graf struct ts_state *state) 37923fc7e8a6SThomas Graf { 37933fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 37943fc7e8a6SThomas Graf } 37953fc7e8a6SThomas Graf 37963fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 37973fc7e8a6SThomas Graf { 37983fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 37993fc7e8a6SThomas Graf } 38003fc7e8a6SThomas Graf 38013fc7e8a6SThomas Graf /** 38023fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 38033fc7e8a6SThomas Graf * @skb: the buffer to look in 38043fc7e8a6SThomas Graf * @from: search offset 38053fc7e8a6SThomas Graf * @to: search limit 38063fc7e8a6SThomas Graf * @config: textsearch configuration 38073fc7e8a6SThomas Graf * 38083fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 38093fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 38103fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 38113fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 38123fc7e8a6SThomas Graf */ 38133fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3814059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 38153fc7e8a6SThomas Graf { 3816059a2440SBojan Prtvar struct ts_state state; 3817f72b948dSPhil Oester unsigned int ret; 3818f72b948dSPhil Oester 3819b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 3820b228c9b0SWillem de Bruijn 38213fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 38223fc7e8a6SThomas Graf config->finish = skb_ts_finish; 38233fc7e8a6SThomas Graf 3824059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 38253fc7e8a6SThomas Graf 3826059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3827f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 38283fc7e8a6SThomas Graf } 3829b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 38303fc7e8a6SThomas Graf 3831be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3832be12a1feSHannes Frederic Sowa int offset, size_t size) 3833be12a1feSHannes Frederic Sowa { 3834be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3835be12a1feSHannes Frederic Sowa 3836be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3837be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3838be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3839be12a1feSHannes Frederic Sowa get_page(page); 3840be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3841be12a1feSHannes Frederic Sowa } else { 3842be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3843be12a1feSHannes Frederic Sowa } 3844be12a1feSHannes Frederic Sowa 3845be12a1feSHannes Frederic Sowa return 0; 3846be12a1feSHannes Frederic Sowa } 3847be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3848be12a1feSHannes Frederic Sowa 3849cbb042f9SHerbert Xu /** 3850cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3851cbb042f9SHerbert Xu * @skb: buffer to update 3852cbb042f9SHerbert Xu * @len: length of data pulled 3853cbb042f9SHerbert Xu * 3854cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3855fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 385684fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 385784fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 385884fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3859cbb042f9SHerbert Xu */ 3860af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3861cbb042f9SHerbert Xu { 386231b33dfbSPravin B Shelar unsigned char *data = skb->data; 386331b33dfbSPravin B Shelar 3864cbb042f9SHerbert Xu BUG_ON(len > skb->len); 386531b33dfbSPravin B Shelar __skb_pull(skb, len); 386631b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 386731b33dfbSPravin B Shelar return skb->data; 3868cbb042f9SHerbert Xu } 3869f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3870f94691acSArnaldo Carvalho de Melo 387113acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 387213acc94eSYonghong Song { 387313acc94eSYonghong Song skb_frag_t head_frag; 387413acc94eSYonghong Song struct page *page; 387513acc94eSYonghong Song 387613acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 3877d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 3878b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 3879b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 3880d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 388113acc94eSYonghong Song return head_frag; 388213acc94eSYonghong Song } 388313acc94eSYonghong Song 38843a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 38853a1296a3SSteffen Klassert netdev_features_t features, 38863a1296a3SSteffen Klassert unsigned int offset) 38873a1296a3SSteffen Klassert { 38883a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 38893a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 38903a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 38913a1296a3SSteffen Klassert unsigned int delta_len = 0; 38923a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 389353475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 3894cf3ab8d4SLina Wang int len_diff, err; 38953a1296a3SSteffen Klassert 38963a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 38973a1296a3SSteffen Klassert 38983a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 38993a1296a3SSteffen Klassert 39003a1296a3SSteffen Klassert do { 39013a1296a3SSteffen Klassert nskb = list_skb; 39023a1296a3SSteffen Klassert list_skb = list_skb->next; 39033a1296a3SSteffen Klassert 390453475c5dSDongseok Yi err = 0; 3905224102deSlena wang delta_truesize += nskb->truesize; 390653475c5dSDongseok Yi if (skb_shared(nskb)) { 390753475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 390853475c5dSDongseok Yi if (tmp) { 390953475c5dSDongseok Yi consume_skb(nskb); 391053475c5dSDongseok Yi nskb = tmp; 391153475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 391253475c5dSDongseok Yi } else { 391353475c5dSDongseok Yi err = -ENOMEM; 391453475c5dSDongseok Yi } 391553475c5dSDongseok Yi } 391653475c5dSDongseok Yi 39173a1296a3SSteffen Klassert if (!tail) 39183a1296a3SSteffen Klassert skb->next = nskb; 39193a1296a3SSteffen Klassert else 39203a1296a3SSteffen Klassert tail->next = nskb; 39213a1296a3SSteffen Klassert 392253475c5dSDongseok Yi if (unlikely(err)) { 392353475c5dSDongseok Yi nskb->next = list_skb; 392453475c5dSDongseok Yi goto err_linearize; 392553475c5dSDongseok Yi } 392653475c5dSDongseok Yi 39273a1296a3SSteffen Klassert tail = nskb; 39283a1296a3SSteffen Klassert 39293a1296a3SSteffen Klassert delta_len += nskb->len; 39303a1296a3SSteffen Klassert 39313a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 39323a1296a3SSteffen Klassert 3933cf673ed0SFlorian Westphal skb_release_head_state(nskb); 3934cf3ab8d4SLina Wang len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 39353a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 39363a1296a3SSteffen Klassert 39373a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 3938cf3ab8d4SLina Wang nskb->transport_header += len_diff; 39393a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 39403a1296a3SSteffen Klassert nskb->data - tnl_hlen, 39413a1296a3SSteffen Klassert offset + tnl_hlen); 39423a1296a3SSteffen Klassert 39433a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 39443a1296a3SSteffen Klassert __skb_linearize(nskb)) 39453a1296a3SSteffen Klassert goto err_linearize; 39463a1296a3SSteffen Klassert 39473a1296a3SSteffen Klassert } while (list_skb); 39483a1296a3SSteffen Klassert 39493a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 39503a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 39513a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 39523a1296a3SSteffen Klassert 39533a1296a3SSteffen Klassert skb_gso_reset(skb); 39543a1296a3SSteffen Klassert 39553a1296a3SSteffen Klassert skb->prev = tail; 39563a1296a3SSteffen Klassert 39573a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 39583a1296a3SSteffen Klassert __skb_linearize(skb)) 39593a1296a3SSteffen Klassert goto err_linearize; 39603a1296a3SSteffen Klassert 39613a1296a3SSteffen Klassert skb_get(skb); 39623a1296a3SSteffen Klassert 39633a1296a3SSteffen Klassert return skb; 39643a1296a3SSteffen Klassert 39653a1296a3SSteffen Klassert err_linearize: 39663a1296a3SSteffen Klassert kfree_skb_list(skb->next); 39673a1296a3SSteffen Klassert skb->next = NULL; 39683a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 39693a1296a3SSteffen Klassert } 39703a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 39713a1296a3SSteffen Klassert 3972f4c50d99SHerbert Xu /** 3973f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3974df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3975576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3976f4c50d99SHerbert Xu * 3977f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 39784c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 39794c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3980f4c50d99SHerbert Xu */ 3981df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3982df5771ffSMichael S. Tsirkin netdev_features_t features) 3983f4c50d99SHerbert Xu { 3984f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3985f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 39861a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3987df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3988df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3989df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 39901fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3991f4c50d99SHerbert Xu unsigned int offset = doffset; 3992df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3993802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3994f4c50d99SHerbert Xu unsigned int headroom; 3995802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3996ec5f0615SPravin B Shelar __be16 proto; 399736c98382SAlexander Duyck bool csum, sg; 3998df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3999f4c50d99SHerbert Xu int err = -ENOMEM; 4000f4c50d99SHerbert Xu int i = 0; 4001f4c50d99SHerbert Xu int pos; 4002f4c50d99SHerbert Xu 40033dcbdb13SShmulik Ladkani if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 40043dcbdb13SShmulik Ladkani (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 40053dcbdb13SShmulik Ladkani /* gso_size is untrusted, and we have a frag_list with a linear 40063dcbdb13SShmulik Ladkani * non head_frag head. 40073dcbdb13SShmulik Ladkani * 40083dcbdb13SShmulik Ladkani * (we assume checking the first list_skb member suffices; 40093dcbdb13SShmulik Ladkani * i.e if either of the list_skb members have non head_frag 40103dcbdb13SShmulik Ladkani * head, then the first one has too). 40113dcbdb13SShmulik Ladkani * 40123dcbdb13SShmulik Ladkani * If head_skb's headlen does not fit requested gso_size, it 40133dcbdb13SShmulik Ladkani * means that the frag_list members do NOT terminate on exact 40143dcbdb13SShmulik Ladkani * gso_size boundaries. Hence we cannot perform skb_frag_t page 40153dcbdb13SShmulik Ladkani * sharing. Therefore we must fallback to copying the frag_list 40163dcbdb13SShmulik Ladkani * skbs; we do so by disabling SG. 40173dcbdb13SShmulik Ladkani */ 40183dcbdb13SShmulik Ladkani if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 40193dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 40203dcbdb13SShmulik Ladkani } 40213dcbdb13SShmulik Ladkani 40225882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 40232f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 4024ec5f0615SPravin B Shelar if (unlikely(!proto)) 4025ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 4026ec5f0615SPravin B Shelar 402736c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 4028f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 40297e2b10c1STom Herbert 403007b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 403107b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 403207b26c94SSteffen Klassert struct sk_buff *iter; 403343170c4eSIlan Tayari unsigned int frag_len; 403407b26c94SSteffen Klassert 403507b26c94SSteffen Klassert if (!list_skb || 403607b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 403707b26c94SSteffen Klassert goto normal; 403807b26c94SSteffen Klassert 403943170c4eSIlan Tayari /* If we get here then all the required 404043170c4eSIlan Tayari * GSO features except frag_list are supported. 404143170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 404243170c4eSIlan Tayari * with no frag_list. 404343170c4eSIlan Tayari * Currently we can do that only when the buffers don't 404443170c4eSIlan Tayari * have a linear part and all the buffers except 404543170c4eSIlan Tayari * the last are of the same length. 404607b26c94SSteffen Klassert */ 404743170c4eSIlan Tayari frag_len = list_skb->len; 404807b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 404943170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 405043170c4eSIlan Tayari goto normal; 4051eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 405207b26c94SSteffen Klassert goto normal; 405307b26c94SSteffen Klassert 405407b26c94SSteffen Klassert len -= iter->len; 405507b26c94SSteffen Klassert } 405643170c4eSIlan Tayari 405743170c4eSIlan Tayari if (len != frag_len) 405843170c4eSIlan Tayari goto normal; 405907b26c94SSteffen Klassert } 406007b26c94SSteffen Klassert 4061802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 4062802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 4063802ab55aSAlexander Duyck * now. 4064802ab55aSAlexander Duyck */ 4065802ab55aSAlexander Duyck partial_segs = len / mss; 4066d7fb5a80SAlexander Duyck if (partial_segs > 1) 4067802ab55aSAlexander Duyck mss *= partial_segs; 4068d7fb5a80SAlexander Duyck else 4069d7fb5a80SAlexander Duyck partial_segs = 0; 4070802ab55aSAlexander Duyck } 4071802ab55aSAlexander Duyck 407207b26c94SSteffen Klassert normal: 4073df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4074df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4075f4c50d99SHerbert Xu 4076f4c50d99SHerbert Xu do { 4077f4c50d99SHerbert Xu struct sk_buff *nskb; 40788cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4079c8884eddSHerbert Xu int hsize; 4080f4c50d99SHerbert Xu int size; 4081f4c50d99SHerbert Xu 40823953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 40833953c46cSMarcelo Ricardo Leitner len = list_skb->len; 40843953c46cSMarcelo Ricardo Leitner } else { 4085df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4086f4c50d99SHerbert Xu if (len > mss) 4087f4c50d99SHerbert Xu len = mss; 40883953c46cSMarcelo Ricardo Leitner } 4089f4c50d99SHerbert Xu 4090df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4091f4c50d99SHerbert Xu 4092dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 40931a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 40941a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 409589319d38SHerbert Xu 40969d8506ccSHerbert Xu i = 0; 40971a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 40981a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 40991fd819ecSMichael S. Tsirkin frag_skb = list_skb; 41001a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 41019d8506ccSHerbert Xu 41029d8506ccSHerbert Xu while (pos < offset + len) { 41039d8506ccSHerbert Xu BUG_ON(i >= nfrags); 41049d8506ccSHerbert Xu 41054e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 41069d8506ccSHerbert Xu if (pos + size > offset + len) 41079d8506ccSHerbert Xu break; 41089d8506ccSHerbert Xu 41099d8506ccSHerbert Xu i++; 41109d8506ccSHerbert Xu pos += size; 41114e1beba1SMichael S. Tsirkin frag++; 41129d8506ccSHerbert Xu } 41139d8506ccSHerbert Xu 41141a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 41151a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 411689319d38SHerbert Xu 4117f4c50d99SHerbert Xu if (unlikely(!nskb)) 4118f4c50d99SHerbert Xu goto err; 4119f4c50d99SHerbert Xu 41209d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 41219d8506ccSHerbert Xu kfree_skb(nskb); 41229d8506ccSHerbert Xu goto err; 41239d8506ccSHerbert Xu } 41249d8506ccSHerbert Xu 4125ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 412689319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 412789319d38SHerbert Xu kfree_skb(nskb); 412889319d38SHerbert Xu goto err; 412989319d38SHerbert Xu } 413089319d38SHerbert Xu 4131ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 413289319d38SHerbert Xu skb_release_head_state(nskb); 413389319d38SHerbert Xu __skb_push(nskb, doffset); 413489319d38SHerbert Xu } else { 413500b229f7SPaolo Abeni if (hsize < 0) 413600b229f7SPaolo Abeni hsize = 0; 4137dbd50f23SXin Long if (hsize > len || !sg) 4138dbd50f23SXin Long hsize = len; 4139dbd50f23SXin Long 4140c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4141df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4142c93bdd0eSMel Gorman NUMA_NO_NODE); 414389319d38SHerbert Xu 414489319d38SHerbert Xu if (unlikely(!nskb)) 414589319d38SHerbert Xu goto err; 414689319d38SHerbert Xu 414789319d38SHerbert Xu skb_reserve(nskb, headroom); 414889319d38SHerbert Xu __skb_put(nskb, doffset); 414989319d38SHerbert Xu } 415089319d38SHerbert Xu 4151f4c50d99SHerbert Xu if (segs) 4152f4c50d99SHerbert Xu tail->next = nskb; 4153f4c50d99SHerbert Xu else 4154f4c50d99SHerbert Xu segs = nskb; 4155f4c50d99SHerbert Xu tail = nskb; 4156f4c50d99SHerbert Xu 4157df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4158f4c50d99SHerbert Xu 4159030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4160fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 416168c33163SPravin B Shelar 4162df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 416368c33163SPravin B Shelar nskb->data - tnl_hlen, 416468c33163SPravin B Shelar doffset + tnl_hlen); 416589319d38SHerbert Xu 41669d8506ccSHerbert Xu if (nskb->len == len + doffset) 41671cdbcb79SSimon Horman goto perform_csum_check; 416889319d38SHerbert Xu 41697fbeffedSAlexander Duyck if (!sg) { 41701454c9faSYadu Kishore if (!csum) { 41717fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 41726f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 417376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 417476443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 41751454c9faSYadu Kishore skb_put(nskb, 41761454c9faSYadu Kishore len), 41778d5930dfSAl Viro len); 41787e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4179de843723STom Herbert skb_headroom(nskb) + doffset; 41801454c9faSYadu Kishore } else { 41811454c9faSYadu Kishore skb_copy_bits(head_skb, offset, 41821454c9faSYadu Kishore skb_put(nskb, len), 41831454c9faSYadu Kishore len); 41841454c9faSYadu Kishore } 4185f4c50d99SHerbert Xu continue; 4186f4c50d99SHerbert Xu } 4187f4c50d99SHerbert Xu 41888cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4189f4c50d99SHerbert Xu 4190df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4191d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4192f4c50d99SHerbert Xu 419306b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 419406b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4195cef401deSEric Dumazet 4196bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4197bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4198bf5c25d6SWillem de Bruijn goto err; 4199bf5c25d6SWillem de Bruijn 42009d8506ccSHerbert Xu while (pos < offset + len) { 42019d8506ccSHerbert Xu if (i >= nfrags) { 42029d8506ccSHerbert Xu i = 0; 42031a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 42041a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 42051fd819ecSMichael S. Tsirkin frag_skb = list_skb; 420613acc94eSYonghong Song if (!skb_headlen(list_skb)) { 42079d8506ccSHerbert Xu BUG_ON(!nfrags); 420813acc94eSYonghong Song } else { 420913acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 42109d8506ccSHerbert Xu 421113acc94eSYonghong Song /* to make room for head_frag. */ 421213acc94eSYonghong Song i--; 421313acc94eSYonghong Song frag--; 421413acc94eSYonghong Song } 4215bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4216bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4217bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4218bf5c25d6SWillem de Bruijn goto err; 4219bf5c25d6SWillem de Bruijn 42201a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 42219d8506ccSHerbert Xu } 42229d8506ccSHerbert Xu 42239d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 42249d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 42259d8506ccSHerbert Xu net_warn_ratelimited( 42269d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 42279d8506ccSHerbert Xu pos, mss); 4228ff907a11SEric Dumazet err = -EINVAL; 42299d8506ccSHerbert Xu goto err; 42309d8506ccSHerbert Xu } 42319d8506ccSHerbert Xu 423213acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 42338cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 42348cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4235f4c50d99SHerbert Xu 4236f4c50d99SHerbert Xu if (pos < offset) { 4237b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 42388cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4239f4c50d99SHerbert Xu } 4240f4c50d99SHerbert Xu 424189319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4242f4c50d99SHerbert Xu 4243f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4244f4c50d99SHerbert Xu i++; 42454e1beba1SMichael S. Tsirkin frag++; 4246f4c50d99SHerbert Xu pos += size; 4247f4c50d99SHerbert Xu } else { 42488cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 424989319d38SHerbert Xu goto skip_fraglist; 4250f4c50d99SHerbert Xu } 4251f4c50d99SHerbert Xu 42528cb19905SMichael S. Tsirkin nskb_frag++; 4253f4c50d99SHerbert Xu } 4254f4c50d99SHerbert Xu 425589319d38SHerbert Xu skip_fraglist: 4256f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4257f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4258f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4259ec5f0615SPravin B Shelar 42601cdbcb79SSimon Horman perform_csum_check: 42617fbeffedSAlexander Duyck if (!csum) { 4262ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4263ff907a11SEric Dumazet __skb_linearize(nskb)) 4264ddff00d4SAlexander Duyck goto err; 4265ff907a11SEric Dumazet 42667fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4267ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 426876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 426976443456SAlexander Duyck skb_checksum(nskb, doffset, 427076443456SAlexander Duyck nskb->len - doffset, 0); 42717e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 42727e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4273ec5f0615SPravin B Shelar } 4274df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4275f4c50d99SHerbert Xu 4276bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4277bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4278bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4279bec3cfdcSEric Dumazet */ 4280bec3cfdcSEric Dumazet segs->prev = tail; 4281432c856fSToshiaki Makita 4282802ab55aSAlexander Duyck if (partial_segs) { 428307b26c94SSteffen Klassert struct sk_buff *iter; 4284802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 428507b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4286802ab55aSAlexander Duyck 4287802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 428807b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4289802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4290802ab55aSAlexander Duyck 4291802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4292802ab55aSAlexander Duyck * our way back down the stack of protocols. 4293802ab55aSAlexander Duyck */ 429407b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 429507b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 429607b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 429707b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 429807b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 429907b26c94SSteffen Klassert } 430007b26c94SSteffen Klassert 430107b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 430207b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 430307b26c94SSteffen Klassert else if (tail != segs) 430407b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4305802ab55aSAlexander Duyck } 4306802ab55aSAlexander Duyck 4307432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4308432c856fSToshiaki Makita * using skb_set_owner_w(). 4309432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4310432c856fSToshiaki Makita */ 4311432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4312432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4313432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4314432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4315432c856fSToshiaki Makita } 4316f4c50d99SHerbert Xu return segs; 4317f4c50d99SHerbert Xu 4318f4c50d99SHerbert Xu err: 4319289dccbeSEric Dumazet kfree_skb_list(segs); 4320f4c50d99SHerbert Xu return ERR_PTR(err); 4321f4c50d99SHerbert Xu } 4322f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4323f4c50d99SHerbert Xu 4324df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4325df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4326df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4327df5042f4SFlorian Westphal 4328df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4329df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4330df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4331df5042f4SFlorian Westphal #endif 43324165079bSFlorian Westphal #ifdef CONFIG_XFRM 43334165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 43344165079bSFlorian Westphal #endif 433595a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 433695a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 433795a7233cSPaul Blakey #endif 43383ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 43393ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 43403ee17bc7SMat Martineau #endif 434178476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 434278476d31SJeremy Kerr [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 434378476d31SJeremy Kerr #endif 4344df5042f4SFlorian Westphal }; 4345df5042f4SFlorian Westphal 4346df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4347df5042f4SFlorian Westphal { 4348df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4349df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4350df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4351df5042f4SFlorian Westphal #endif 43524165079bSFlorian Westphal #ifdef CONFIG_XFRM 43534165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 43544165079bSFlorian Westphal #endif 435595a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 435695a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 435795a7233cSPaul Blakey #endif 43583ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 43593ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 43603ee17bc7SMat Martineau #endif 436178476d31SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP_FLOWS) 436278476d31SJeremy Kerr skb_ext_type_len[SKB_EXT_MCTP] + 436378476d31SJeremy Kerr #endif 4364df5042f4SFlorian Westphal 0; 4365df5042f4SFlorian Westphal } 4366df5042f4SFlorian Westphal 4367df5042f4SFlorian Westphal static void skb_extensions_init(void) 4368df5042f4SFlorian Westphal { 4369df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4370df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4371df5042f4SFlorian Westphal 4372df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4373df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4374df5042f4SFlorian Westphal 0, 4375df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4376df5042f4SFlorian Westphal NULL); 4377df5042f4SFlorian Westphal } 4378df5042f4SFlorian Westphal #else 4379df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4380df5042f4SFlorian Westphal #endif 4381df5042f4SFlorian Westphal 43821da177e4SLinus Torvalds void __init skb_init(void) 43831da177e4SLinus Torvalds { 438479a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 43851da177e4SLinus Torvalds sizeof(struct sk_buff), 43861da177e4SLinus Torvalds 0, 4387e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 438879a8a642SKees Cook offsetof(struct sk_buff, cb), 438979a8a642SKees Cook sizeof_field(struct sk_buff, cb), 439020c2df83SPaul Mundt NULL); 4391d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4392d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4393d179cd12SDavid S. Miller 0, 4394e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 439520c2df83SPaul Mundt NULL); 4396df5042f4SFlorian Westphal skb_extensions_init(); 43971da177e4SLinus Torvalds } 43981da177e4SLinus Torvalds 439951c739d1SDavid S. Miller static int 440048a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 440148a1df65SJason A. Donenfeld unsigned int recursion_level) 4402716ea3a7SDavid Howells { 44031a028e50SDavid S. Miller int start = skb_headlen(skb); 44041a028e50SDavid S. Miller int i, copy = start - offset; 4405fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4406716ea3a7SDavid Howells int elt = 0; 4407716ea3a7SDavid Howells 440848a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 440948a1df65SJason A. Donenfeld return -EMSGSIZE; 441048a1df65SJason A. Donenfeld 4411716ea3a7SDavid Howells if (copy > 0) { 4412716ea3a7SDavid Howells if (copy > len) 4413716ea3a7SDavid Howells copy = len; 4414642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4415716ea3a7SDavid Howells elt++; 4416716ea3a7SDavid Howells if ((len -= copy) == 0) 4417716ea3a7SDavid Howells return elt; 4418716ea3a7SDavid Howells offset += copy; 4419716ea3a7SDavid Howells } 4420716ea3a7SDavid Howells 4421716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 44221a028e50SDavid S. Miller int end; 4423716ea3a7SDavid Howells 4424547b792cSIlpo Järvinen WARN_ON(start > offset + len); 44251a028e50SDavid S. Miller 44269e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4427716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4428716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 442948a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 443048a1df65SJason A. Donenfeld return -EMSGSIZE; 4431716ea3a7SDavid Howells 4432716ea3a7SDavid Howells if (copy > len) 4433716ea3a7SDavid Howells copy = len; 4434ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4435b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4436716ea3a7SDavid Howells elt++; 4437716ea3a7SDavid Howells if (!(len -= copy)) 4438716ea3a7SDavid Howells return elt; 4439716ea3a7SDavid Howells offset += copy; 4440716ea3a7SDavid Howells } 44411a028e50SDavid S. Miller start = end; 4442716ea3a7SDavid Howells } 4443716ea3a7SDavid Howells 4444fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 444548a1df65SJason A. Donenfeld int end, ret; 4446716ea3a7SDavid Howells 4447547b792cSIlpo Järvinen WARN_ON(start > offset + len); 44481a028e50SDavid S. Miller 4449fbb398a8SDavid S. Miller end = start + frag_iter->len; 4450716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 445148a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 445248a1df65SJason A. Donenfeld return -EMSGSIZE; 445348a1df65SJason A. Donenfeld 4454716ea3a7SDavid Howells if (copy > len) 4455716ea3a7SDavid Howells copy = len; 445648a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 445748a1df65SJason A. Donenfeld copy, recursion_level + 1); 445848a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 445948a1df65SJason A. Donenfeld return ret; 446048a1df65SJason A. Donenfeld elt += ret; 4461716ea3a7SDavid Howells if ((len -= copy) == 0) 4462716ea3a7SDavid Howells return elt; 4463716ea3a7SDavid Howells offset += copy; 4464716ea3a7SDavid Howells } 44651a028e50SDavid S. Miller start = end; 4466716ea3a7SDavid Howells } 4467716ea3a7SDavid Howells BUG_ON(len); 4468716ea3a7SDavid Howells return elt; 4469716ea3a7SDavid Howells } 4470716ea3a7SDavid Howells 447148a1df65SJason A. Donenfeld /** 447248a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 447348a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 447448a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 447548a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 447648a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 447748a1df65SJason A. Donenfeld * 447848a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 447948a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 448048a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 448148a1df65SJason A. Donenfeld * could not fit. 448248a1df65SJason A. Donenfeld */ 448348a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 448448a1df65SJason A. Donenfeld { 448548a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 448648a1df65SJason A. Donenfeld 448748a1df65SJason A. Donenfeld if (nsg <= 0) 448848a1df65SJason A. Donenfeld return nsg; 448948a1df65SJason A. Donenfeld 449048a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 449148a1df65SJason A. Donenfeld 449248a1df65SJason A. Donenfeld return nsg; 449348a1df65SJason A. Donenfeld } 449448a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 449548a1df65SJason A. Donenfeld 449625a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 449725a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 449825a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 449925a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 450025a91d8dSFan Du * 450125a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 450225a91d8dSFan Du * 1. sg_init_table 450325a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 450425a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 450525a91d8dSFan Du * 450625a91d8dSFan Du * This is equivalent to: 450725a91d8dSFan Du * 1. sg_init_table 450825a91d8dSFan Du * 2. skb_to_sgvec(payload1) 450925a91d8dSFan Du * 3. sg_unmark_end 451025a91d8dSFan Du * 4. skb_to_sgvec(payload2) 451125a91d8dSFan Du * 451225a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 451325a91d8dSFan Du * is more preferable. 451425a91d8dSFan Du */ 451525a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 451625a91d8dSFan Du int offset, int len) 451725a91d8dSFan Du { 451848a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 451925a91d8dSFan Du } 452025a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 452125a91d8dSFan Du 452251c739d1SDavid S. Miller 452351c739d1SDavid S. Miller 4524716ea3a7SDavid Howells /** 4525716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4526716ea3a7SDavid Howells * @skb: The socket buffer to check. 4527716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4528716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4529716ea3a7SDavid Howells * 4530716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4531716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4532716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4533716ea3a7SDavid Howells * 4534716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4535716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4536716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4537716ea3a7SDavid Howells * 4538716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4539716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4540716ea3a7SDavid Howells */ 4541716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4542716ea3a7SDavid Howells { 4543716ea3a7SDavid Howells int copyflag; 4544716ea3a7SDavid Howells int elt; 4545716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4546716ea3a7SDavid Howells 4547716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4548716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4549716ea3a7SDavid Howells * at the moment even if they are anonymous). 4550716ea3a7SDavid Howells */ 4551716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4552c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4553716ea3a7SDavid Howells return -ENOMEM; 4554716ea3a7SDavid Howells 4555716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 455621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4557716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4558716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4559716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4560716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4561716ea3a7SDavid Howells 4562716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4563716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4564716ea3a7SDavid Howells return -ENOMEM; 4565716ea3a7SDavid Howells 4566716ea3a7SDavid Howells /* Voila! */ 4567716ea3a7SDavid Howells *trailer = skb; 4568716ea3a7SDavid Howells return 1; 4569716ea3a7SDavid Howells } 4570716ea3a7SDavid Howells 4571716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4572716ea3a7SDavid Howells 4573716ea3a7SDavid Howells elt = 1; 4574716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4575716ea3a7SDavid Howells copyflag = 0; 4576716ea3a7SDavid Howells 4577716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4578716ea3a7SDavid Howells int ntail = 0; 4579716ea3a7SDavid Howells 4580716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4581716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4582716ea3a7SDavid Howells * after it. */ 4583716ea3a7SDavid Howells 4584716ea3a7SDavid Howells if (skb_shared(skb1)) 4585716ea3a7SDavid Howells copyflag = 1; 4586716ea3a7SDavid Howells 4587716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4588716ea3a7SDavid Howells 4589716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4590716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 459121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4592716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4593716ea3a7SDavid Howells ntail = tailbits + 128; 4594716ea3a7SDavid Howells } 4595716ea3a7SDavid Howells 4596716ea3a7SDavid Howells if (copyflag || 4597716ea3a7SDavid Howells skb_cloned(skb1) || 4598716ea3a7SDavid Howells ntail || 4599716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 460021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4601716ea3a7SDavid Howells struct sk_buff *skb2; 4602716ea3a7SDavid Howells 4603716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4604716ea3a7SDavid Howells if (ntail == 0) 4605716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4606716ea3a7SDavid Howells else 4607716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4608716ea3a7SDavid Howells skb_headroom(skb1), 4609716ea3a7SDavid Howells ntail, 4610716ea3a7SDavid Howells GFP_ATOMIC); 4611716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4612716ea3a7SDavid Howells return -ENOMEM; 4613716ea3a7SDavid Howells 4614716ea3a7SDavid Howells if (skb1->sk) 4615716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4616716ea3a7SDavid Howells 4617716ea3a7SDavid Howells /* Looking around. Are we still alive? 4618716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4619716ea3a7SDavid Howells 4620716ea3a7SDavid Howells skb2->next = skb1->next; 4621716ea3a7SDavid Howells *skb_p = skb2; 4622716ea3a7SDavid Howells kfree_skb(skb1); 4623716ea3a7SDavid Howells skb1 = skb2; 4624716ea3a7SDavid Howells } 4625716ea3a7SDavid Howells elt++; 4626716ea3a7SDavid Howells *trailer = skb1; 4627716ea3a7SDavid Howells skb_p = &skb1->next; 4628716ea3a7SDavid Howells } 4629716ea3a7SDavid Howells 4630716ea3a7SDavid Howells return elt; 4631716ea3a7SDavid Howells } 4632b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4633716ea3a7SDavid Howells 4634b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4635b1faf566SEric Dumazet { 4636b1faf566SEric Dumazet struct sock *sk = skb->sk; 4637b1faf566SEric Dumazet 4638b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4639b1faf566SEric Dumazet } 4640b1faf566SEric Dumazet 46418605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 46428605330aSSoheil Hassas Yeganeh { 46438605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 46448605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 46458605330aSSoheil Hassas Yeganeh */ 46468605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 46478605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 46488605330aSSoheil Hassas Yeganeh } 46498605330aSSoheil Hassas Yeganeh 4650b1faf566SEric Dumazet /* 4651b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4652b1faf566SEric Dumazet */ 4653b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4654b1faf566SEric Dumazet { 4655b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4656ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4657b1faf566SEric Dumazet return -ENOMEM; 4658b1faf566SEric Dumazet 4659b1faf566SEric Dumazet skb_orphan(skb); 4660b1faf566SEric Dumazet skb->sk = sk; 4661b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4662b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 46638605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4664b1faf566SEric Dumazet 4665abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4666abb57ea4SEric Dumazet skb_dst_force(skb); 4667abb57ea4SEric Dumazet 4668b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4669b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4670e3ae2365SAlexander Aring sk_error_report(sk); 4671b1faf566SEric Dumazet return 0; 4672b1faf566SEric Dumazet } 4673b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4674b1faf566SEric Dumazet 467583a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 467683a1a1a7SSoheil Hassas Yeganeh { 467783a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 467883a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 467983a1a1a7SSoheil Hassas Yeganeh } 468083a1a1a7SSoheil Hassas Yeganeh 4681364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4682364a9e93SWillem de Bruijn { 4683364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 468483a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 468583a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4686997d5c3fSEric Dumazet unsigned long flags; 4687364a9e93SWillem de Bruijn 4688997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4689364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 469038b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 469183a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 469238b25793SSoheil Hassas Yeganeh if (icmp_next) 4693985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 469438b25793SSoheil Hassas Yeganeh } 4695997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4696364a9e93SWillem de Bruijn 469783a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 469883a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 469983a1a1a7SSoheil Hassas Yeganeh 470083a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4701e3ae2365SAlexander Aring sk_error_report(sk); 4702364a9e93SWillem de Bruijn 4703364a9e93SWillem de Bruijn return skb; 4704364a9e93SWillem de Bruijn } 4705364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4706364a9e93SWillem de Bruijn 4707cab41c47SAlexander Duyck /** 4708cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4709cab41c47SAlexander Duyck * @skb: the skb to clone 4710cab41c47SAlexander Duyck * 4711cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4712cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4713cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4714cab41c47SAlexander Duyck * 4715cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4716cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4717cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4718cab41c47SAlexander Duyck * the sk_error_queue. 4719cab41c47SAlexander Duyck */ 472062bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 472162bccb8cSAlexander Duyck { 472262bccb8cSAlexander Duyck struct sock *sk = skb->sk; 472362bccb8cSAlexander Duyck struct sk_buff *clone; 472462bccb8cSAlexander Duyck 472541c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 472662bccb8cSAlexander Duyck return NULL; 472762bccb8cSAlexander Duyck 472862bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 472962bccb8cSAlexander Duyck if (!clone) { 473062bccb8cSAlexander Duyck sock_put(sk); 473162bccb8cSAlexander Duyck return NULL; 473262bccb8cSAlexander Duyck } 473362bccb8cSAlexander Duyck 473462bccb8cSAlexander Duyck clone->sk = sk; 473562bccb8cSAlexander Duyck clone->destructor = sock_efree; 473662bccb8cSAlexander Duyck 473762bccb8cSAlexander Duyck return clone; 473862bccb8cSAlexander Duyck } 473962bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 474062bccb8cSAlexander Duyck 474137846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 474237846ef0SAlexander Duyck struct sock *sk, 47434ef1b286SSoheil Hassas Yeganeh int tstype, 47444ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4745ac45f602SPatrick Ohly { 4746ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4747ac45f602SPatrick Ohly int err; 4748ac45f602SPatrick Ohly 47494ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 47504ef1b286SSoheil Hassas Yeganeh 4751ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4752ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4753ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4754ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4755e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 47564ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 47571862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 47584ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 475909c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 476042f67eeaSEric Dumazet if (sk_is_tcp(sk)) 4761a1cdec57SEric Dumazet serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 47624ed2d765SWillem de Bruijn } 476329030374SEric Dumazet 4764ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 476529030374SEric Dumazet 4766ac45f602SPatrick Ohly if (err) 4767ac45f602SPatrick Ohly kfree_skb(skb); 4768ac45f602SPatrick Ohly } 476937846ef0SAlexander Duyck 4770b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4771b245be1fSWillem de Bruijn { 4772b245be1fSWillem de Bruijn bool ret; 4773b245be1fSWillem de Bruijn 4774b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4775b245be1fSWillem de Bruijn return true; 4776b245be1fSWillem de Bruijn 4777b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4778b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4779b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4780b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4781b245be1fSWillem de Bruijn return ret; 4782b245be1fSWillem de Bruijn } 4783b245be1fSWillem de Bruijn 478437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 478537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 478637846ef0SAlexander Duyck { 478737846ef0SAlexander Duyck struct sock *sk = skb->sk; 478837846ef0SAlexander Duyck 4789b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 479035b99dffSWillem de Bruijn goto err; 4791b245be1fSWillem de Bruijn 47929ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 47939ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 47949ac25fc0SEric Dumazet */ 479541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 479637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 47974ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 479837846ef0SAlexander Duyck sock_put(sk); 479935b99dffSWillem de Bruijn return; 480037846ef0SAlexander Duyck } 480135b99dffSWillem de Bruijn 480235b99dffSWillem de Bruijn err: 480335b99dffSWillem de Bruijn kfree_skb(skb); 48049ac25fc0SEric Dumazet } 480537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 480637846ef0SAlexander Duyck 480737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 4808e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 480937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 481037846ef0SAlexander Duyck struct sock *sk, int tstype) 481137846ef0SAlexander Duyck { 481237846ef0SAlexander Duyck struct sk_buff *skb; 48134ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 481437846ef0SAlexander Duyck 48153a8dd971SWillem de Bruijn if (!sk) 48163a8dd971SWillem de Bruijn return; 48173a8dd971SWillem de Bruijn 4818b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4819b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4820b50a5c70SMiroslav Lichvar return; 4821b50a5c70SMiroslav Lichvar 48223a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 48233a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 482437846ef0SAlexander Duyck return; 482537846ef0SAlexander Duyck 48261c885808SFrancis Yan if (tsonly) { 48271c885808SFrancis Yan #ifdef CONFIG_INET 48281c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 482942f67eeaSEric Dumazet sk_is_tcp(sk)) { 4830e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 4831e7ed11eeSYousuk Seung ack_skb); 48324ef1b286SSoheil Hassas Yeganeh opt_stats = true; 48334ef1b286SSoheil Hassas Yeganeh } else 48341c885808SFrancis Yan #endif 48351c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 48361c885808SFrancis Yan } else { 483737846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 48381c885808SFrancis Yan } 483937846ef0SAlexander Duyck if (!skb) 484037846ef0SAlexander Duyck return; 484137846ef0SAlexander Duyck 484249ca0d8bSWillem de Bruijn if (tsonly) { 4843fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4844fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 484549ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 484649ca0d8bSWillem de Bruijn } 484749ca0d8bSWillem de Bruijn 484849ca0d8bSWillem de Bruijn if (hwtstamps) 484949ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 485049ca0d8bSWillem de Bruijn else 4851d93376f5SMartin KaFai Lau __net_timestamp(skb); 485249ca0d8bSWillem de Bruijn 48534ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 485437846ef0SAlexander Duyck } 4855e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4856e7fd2885SWillem de Bruijn 4857e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4858e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4859e7fd2885SWillem de Bruijn { 4860e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 4861e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4862e7fd2885SWillem de Bruijn } 4863ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4864ac45f602SPatrick Ohly 48656e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 48666e3e939fSJohannes Berg { 48676e3e939fSJohannes Berg struct sock *sk = skb->sk; 48686e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4869dd4f1072SEric Dumazet int err = 1; 48706e3e939fSJohannes Berg 48716e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 48726e3e939fSJohannes Berg skb->wifi_acked = acked; 48736e3e939fSJohannes Berg 48746e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 48756e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 48766e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 48776e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 48786e3e939fSJohannes Berg 4879dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4880dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4881dd4f1072SEric Dumazet */ 488241c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 48836e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4884dd4f1072SEric Dumazet sock_put(sk); 4885dd4f1072SEric Dumazet } 48866e3e939fSJohannes Berg if (err) 48876e3e939fSJohannes Berg kfree_skb(skb); 48886e3e939fSJohannes Berg } 48896e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 48906e3e939fSJohannes Berg 4891f35d9d8aSRusty Russell /** 4892f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4893f35d9d8aSRusty Russell * @skb: the skb to set 4894f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4895f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4896f35d9d8aSRusty Russell * 4897f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4898f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4899f35d9d8aSRusty Russell * 4900f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4901f35d9d8aSRusty Russell * returns false you should drop the packet. 4902f35d9d8aSRusty Russell */ 4903f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4904f35d9d8aSRusty Russell { 490552b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 490652b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 490752b5d6f5SEric Dumazet 490852b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 490952b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 491052b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4911f35d9d8aSRusty Russell return false; 4912f35d9d8aSRusty Russell } 4913f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 491452b5d6f5SEric Dumazet skb->csum_start = csum_start; 4915f35d9d8aSRusty Russell skb->csum_offset = off; 4916e5d5decaSJason Wang skb_set_transport_header(skb, start); 4917f35d9d8aSRusty Russell return true; 4918f35d9d8aSRusty Russell } 4919b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4920f35d9d8aSRusty Russell 4921ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4922ed1f50c3SPaul Durrant unsigned int max) 4923ed1f50c3SPaul Durrant { 4924ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4925ed1f50c3SPaul Durrant return 0; 4926ed1f50c3SPaul Durrant 4927ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4928ed1f50c3SPaul Durrant * won't need to do it again. 4929ed1f50c3SPaul Durrant */ 4930ed1f50c3SPaul Durrant if (max > skb->len) 4931ed1f50c3SPaul Durrant max = skb->len; 4932ed1f50c3SPaul Durrant 4933ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4934ed1f50c3SPaul Durrant return -ENOMEM; 4935ed1f50c3SPaul Durrant 4936ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4937ed1f50c3SPaul Durrant return -EPROTO; 4938ed1f50c3SPaul Durrant 4939ed1f50c3SPaul Durrant return 0; 4940ed1f50c3SPaul Durrant } 4941ed1f50c3SPaul Durrant 4942f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4943f9708b43SJan Beulich 4944f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4945f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4946f9708b43SJan Beulich unsigned int off) 4947f9708b43SJan Beulich { 4948f9708b43SJan Beulich int err; 4949f9708b43SJan Beulich 4950161d1792SKees Cook switch (proto) { 4951f9708b43SJan Beulich case IPPROTO_TCP: 4952f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4953f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4954f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4955f9708b43SJan Beulich offsetof(struct tcphdr, 4956f9708b43SJan Beulich check))) 4957f9708b43SJan Beulich err = -EPROTO; 4958f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4959f9708b43SJan Beulich 4960f9708b43SJan Beulich case IPPROTO_UDP: 4961f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4962f9708b43SJan Beulich off + sizeof(struct udphdr)); 4963f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4964f9708b43SJan Beulich offsetof(struct udphdr, 4965f9708b43SJan Beulich check))) 4966f9708b43SJan Beulich err = -EPROTO; 4967f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4968f9708b43SJan Beulich } 4969f9708b43SJan Beulich 4970f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4971f9708b43SJan Beulich } 4972f9708b43SJan Beulich 4973ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4974ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4975ed1f50c3SPaul Durrant */ 4976ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4977ed1f50c3SPaul Durrant 4978f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4979ed1f50c3SPaul Durrant { 4980ed1f50c3SPaul Durrant unsigned int off; 4981ed1f50c3SPaul Durrant bool fragment; 4982f9708b43SJan Beulich __sum16 *csum; 4983ed1f50c3SPaul Durrant int err; 4984ed1f50c3SPaul Durrant 4985ed1f50c3SPaul Durrant fragment = false; 4986ed1f50c3SPaul Durrant 4987ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4988ed1f50c3SPaul Durrant sizeof(struct iphdr), 4989ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4990ed1f50c3SPaul Durrant if (err < 0) 4991ed1f50c3SPaul Durrant goto out; 4992ed1f50c3SPaul Durrant 499311f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 4994ed1f50c3SPaul Durrant fragment = true; 4995ed1f50c3SPaul Durrant 4996ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4997ed1f50c3SPaul Durrant 4998ed1f50c3SPaul Durrant err = -EPROTO; 4999ed1f50c3SPaul Durrant 5000ed1f50c3SPaul Durrant if (fragment) 5001ed1f50c3SPaul Durrant goto out; 5002ed1f50c3SPaul Durrant 5003f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5004f9708b43SJan Beulich if (IS_ERR(csum)) 5005f9708b43SJan Beulich return PTR_ERR(csum); 5006ed1f50c3SPaul Durrant 5007ed1f50c3SPaul Durrant if (recalculate) 5008f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5009ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5010ed1f50c3SPaul Durrant skb->len - off, 5011f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5012ed1f50c3SPaul Durrant err = 0; 5013ed1f50c3SPaul Durrant 5014ed1f50c3SPaul Durrant out: 5015ed1f50c3SPaul Durrant return err; 5016ed1f50c3SPaul Durrant } 5017ed1f50c3SPaul Durrant 5018ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5019ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5020ed1f50c3SPaul Durrant */ 5021ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5022ed1f50c3SPaul Durrant 5023ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5024ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5025ed1f50c3SPaul Durrant 5026ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5027ed1f50c3SPaul Durrant { 5028ed1f50c3SPaul Durrant int err; 5029ed1f50c3SPaul Durrant u8 nexthdr; 5030ed1f50c3SPaul Durrant unsigned int off; 5031ed1f50c3SPaul Durrant unsigned int len; 5032ed1f50c3SPaul Durrant bool fragment; 5033ed1f50c3SPaul Durrant bool done; 5034f9708b43SJan Beulich __sum16 *csum; 5035ed1f50c3SPaul Durrant 5036ed1f50c3SPaul Durrant fragment = false; 5037ed1f50c3SPaul Durrant done = false; 5038ed1f50c3SPaul Durrant 5039ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5040ed1f50c3SPaul Durrant 5041ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5042ed1f50c3SPaul Durrant if (err < 0) 5043ed1f50c3SPaul Durrant goto out; 5044ed1f50c3SPaul Durrant 5045ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5046ed1f50c3SPaul Durrant 5047ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5048ed1f50c3SPaul Durrant while (off <= len && !done) { 5049ed1f50c3SPaul Durrant switch (nexthdr) { 5050ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5051ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5052ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5053ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5054ed1f50c3SPaul Durrant 5055ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5056ed1f50c3SPaul Durrant off + 5057ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5058ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5059ed1f50c3SPaul Durrant if (err < 0) 5060ed1f50c3SPaul Durrant goto out; 5061ed1f50c3SPaul Durrant 5062ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5063ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5064ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5065ed1f50c3SPaul Durrant break; 5066ed1f50c3SPaul Durrant } 5067ed1f50c3SPaul Durrant case IPPROTO_AH: { 5068ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5069ed1f50c3SPaul Durrant 5070ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5071ed1f50c3SPaul Durrant off + 5072ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5073ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5074ed1f50c3SPaul Durrant if (err < 0) 5075ed1f50c3SPaul Durrant goto out; 5076ed1f50c3SPaul Durrant 5077ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5078ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5079ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5080ed1f50c3SPaul Durrant break; 5081ed1f50c3SPaul Durrant } 5082ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5083ed1f50c3SPaul Durrant struct frag_hdr *hp; 5084ed1f50c3SPaul Durrant 5085ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5086ed1f50c3SPaul Durrant off + 5087ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5088ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5089ed1f50c3SPaul Durrant if (err < 0) 5090ed1f50c3SPaul Durrant goto out; 5091ed1f50c3SPaul Durrant 5092ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5093ed1f50c3SPaul Durrant 5094ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5095ed1f50c3SPaul Durrant fragment = true; 5096ed1f50c3SPaul Durrant 5097ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5098ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5099ed1f50c3SPaul Durrant break; 5100ed1f50c3SPaul Durrant } 5101ed1f50c3SPaul Durrant default: 5102ed1f50c3SPaul Durrant done = true; 5103ed1f50c3SPaul Durrant break; 5104ed1f50c3SPaul Durrant } 5105ed1f50c3SPaul Durrant } 5106ed1f50c3SPaul Durrant 5107ed1f50c3SPaul Durrant err = -EPROTO; 5108ed1f50c3SPaul Durrant 5109ed1f50c3SPaul Durrant if (!done || fragment) 5110ed1f50c3SPaul Durrant goto out; 5111ed1f50c3SPaul Durrant 5112f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5113f9708b43SJan Beulich if (IS_ERR(csum)) 5114f9708b43SJan Beulich return PTR_ERR(csum); 5115ed1f50c3SPaul Durrant 5116ed1f50c3SPaul Durrant if (recalculate) 5117f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5118ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5119f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5120ed1f50c3SPaul Durrant err = 0; 5121ed1f50c3SPaul Durrant 5122ed1f50c3SPaul Durrant out: 5123ed1f50c3SPaul Durrant return err; 5124ed1f50c3SPaul Durrant } 5125ed1f50c3SPaul Durrant 5126ed1f50c3SPaul Durrant /** 5127ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5128ed1f50c3SPaul Durrant * @skb: the skb to set up 5129ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5130ed1f50c3SPaul Durrant */ 5131ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5132ed1f50c3SPaul Durrant { 5133ed1f50c3SPaul Durrant int err; 5134ed1f50c3SPaul Durrant 5135ed1f50c3SPaul Durrant switch (skb->protocol) { 5136ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5137f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5138ed1f50c3SPaul Durrant break; 5139ed1f50c3SPaul Durrant 5140ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5141ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5142ed1f50c3SPaul Durrant break; 5143ed1f50c3SPaul Durrant 5144ed1f50c3SPaul Durrant default: 5145ed1f50c3SPaul Durrant err = -EPROTO; 5146ed1f50c3SPaul Durrant break; 5147ed1f50c3SPaul Durrant } 5148ed1f50c3SPaul Durrant 5149ed1f50c3SPaul Durrant return err; 5150ed1f50c3SPaul Durrant } 5151ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5152ed1f50c3SPaul Durrant 51539afd85c9SLinus Lüssing /** 51549afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 51559afd85c9SLinus Lüssing * @skb: the skb to check 51569afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 51579afd85c9SLinus Lüssing * 51589afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 51599afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 51609afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 51619afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 51629afd85c9SLinus Lüssing * 5163a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5164a516993fSLinus Lüssing * differs from the provided skb. 51659afd85c9SLinus Lüssing */ 51669afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 51679afd85c9SLinus Lüssing unsigned int transport_len) 51689afd85c9SLinus Lüssing { 51699afd85c9SLinus Lüssing struct sk_buff *skb_chk; 51709afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 51719afd85c9SLinus Lüssing int ret; 51729afd85c9SLinus Lüssing 5173a516993fSLinus Lüssing if (skb->len < len) 51749afd85c9SLinus Lüssing return NULL; 5175a516993fSLinus Lüssing else if (skb->len == len) 51769afd85c9SLinus Lüssing return skb; 51779afd85c9SLinus Lüssing 51789afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 51799afd85c9SLinus Lüssing if (!skb_chk) 51809afd85c9SLinus Lüssing return NULL; 51819afd85c9SLinus Lüssing 51829afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 51839afd85c9SLinus Lüssing if (ret) { 51849afd85c9SLinus Lüssing kfree_skb(skb_chk); 51859afd85c9SLinus Lüssing return NULL; 51869afd85c9SLinus Lüssing } 51879afd85c9SLinus Lüssing 51889afd85c9SLinus Lüssing return skb_chk; 51899afd85c9SLinus Lüssing } 51909afd85c9SLinus Lüssing 51919afd85c9SLinus Lüssing /** 51929afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 51939afd85c9SLinus Lüssing * @skb: the skb to check 51949afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 51959afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 51969afd85c9SLinus Lüssing * 51979afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 51989afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 51999afd85c9SLinus Lüssing * 52009afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 52019afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 52029afd85c9SLinus Lüssing * 5203a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5204a516993fSLinus Lüssing * differs from the provided skb. 52059afd85c9SLinus Lüssing */ 52069afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 52079afd85c9SLinus Lüssing unsigned int transport_len, 52089afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 52099afd85c9SLinus Lüssing { 52109afd85c9SLinus Lüssing struct sk_buff *skb_chk; 52119afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5212fcba67c9SLinus Lüssing __sum16 ret; 52139afd85c9SLinus Lüssing 52149afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 52159afd85c9SLinus Lüssing if (!skb_chk) 5216a516993fSLinus Lüssing goto err; 52179afd85c9SLinus Lüssing 5218a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5219a516993fSLinus Lüssing goto err; 52209afd85c9SLinus Lüssing 52219b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 52229afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 52239b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 52249afd85c9SLinus Lüssing 5225a516993fSLinus Lüssing if (ret) 5226a516993fSLinus Lüssing goto err; 52279afd85c9SLinus Lüssing 52289afd85c9SLinus Lüssing return skb_chk; 5229a516993fSLinus Lüssing 5230a516993fSLinus Lüssing err: 5231a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5232a516993fSLinus Lüssing kfree_skb(skb_chk); 5233a516993fSLinus Lüssing 5234a516993fSLinus Lüssing return NULL; 5235a516993fSLinus Lüssing 52369afd85c9SLinus Lüssing } 52379afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 52389afd85c9SLinus Lüssing 52394497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 52404497b076SBen Hutchings { 5241e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5242e87cc472SJoe Perches skb->dev->name); 52434497b076SBen Hutchings } 52444497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5245bad43ca8SEric Dumazet 5246bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5247bad43ca8SEric Dumazet { 52483d861f66SEric Dumazet if (head_stolen) { 52493d861f66SEric Dumazet skb_release_head_state(skb); 5250bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 52513d861f66SEric Dumazet } else { 5252bad43ca8SEric Dumazet __kfree_skb(skb); 5253bad43ca8SEric Dumazet } 52543d861f66SEric Dumazet } 5255bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5256bad43ca8SEric Dumazet 5257bad43ca8SEric Dumazet /** 5258bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5259bad43ca8SEric Dumazet * @to: prior buffer 5260bad43ca8SEric Dumazet * @from: buffer to add 5261bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5262c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5263bad43ca8SEric Dumazet */ 5264bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5265bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5266bad43ca8SEric Dumazet { 5267c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5268bad43ca8SEric Dumazet int i, delta, len = from->len; 5269bad43ca8SEric Dumazet 5270bad43ca8SEric Dumazet *fragstolen = false; 5271bad43ca8SEric Dumazet 5272bad43ca8SEric Dumazet if (skb_cloned(to)) 5273bad43ca8SEric Dumazet return false; 5274bad43ca8SEric Dumazet 52751effe8caSJean-Philippe Brucker /* In general, avoid mixing slab allocated and page_pool allocated 52761effe8caSJean-Philippe Brucker * pages within the same SKB. However when @to is not pp_recycle and 52771effe8caSJean-Philippe Brucker * @from is cloned, we can transition frag pages from page_pool to 52781effe8caSJean-Philippe Brucker * reference counted. 52791effe8caSJean-Philippe Brucker * 52801effe8caSJean-Philippe Brucker * On the other hand, don't allow coalescing two pp_recycle SKBs if 52811effe8caSJean-Philippe Brucker * @from is cloned, in case the SKB is using page_pool fragment 52821effe8caSJean-Philippe Brucker * references (PP_FLAG_PAGE_FRAG). Since we only take full page 52831effe8caSJean-Philippe Brucker * references for cloned SKBs at the moment that would result in 52841effe8caSJean-Philippe Brucker * inconsistent reference counts. 52856a5bcd84SIlias Apalodimas */ 52861effe8caSJean-Philippe Brucker if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from))) 52876a5bcd84SIlias Apalodimas return false; 52886a5bcd84SIlias Apalodimas 5289bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5290e93a0435SEric Dumazet if (len) 5291bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5292bad43ca8SEric Dumazet *delta_truesize = 0; 5293bad43ca8SEric Dumazet return true; 5294bad43ca8SEric Dumazet } 5295bad43ca8SEric Dumazet 5296c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5297c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5298c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5299bad43ca8SEric Dumazet return false; 53001f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 53011f8b977aSWillem de Bruijn return false; 5302bad43ca8SEric Dumazet 5303bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5304bad43ca8SEric Dumazet struct page *page; 5305bad43ca8SEric Dumazet unsigned int offset; 5306bad43ca8SEric Dumazet 5307c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5308c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5309bad43ca8SEric Dumazet return false; 5310bad43ca8SEric Dumazet 5311bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5312bad43ca8SEric Dumazet return false; 5313bad43ca8SEric Dumazet 5314bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5315bad43ca8SEric Dumazet 5316bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5317bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5318bad43ca8SEric Dumazet 5319c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5320bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5321bad43ca8SEric Dumazet *fragstolen = true; 5322bad43ca8SEric Dumazet } else { 5323c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5324c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5325bad43ca8SEric Dumazet return false; 5326bad43ca8SEric Dumazet 5327f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5328bad43ca8SEric Dumazet } 5329bad43ca8SEric Dumazet 5330bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5331bad43ca8SEric Dumazet 5332c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5333c818fa9eSEric Dumazet from_shinfo->frags, 5334c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5335c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5336bad43ca8SEric Dumazet 5337bad43ca8SEric Dumazet if (!skb_cloned(from)) 5338c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5339bad43ca8SEric Dumazet 53408ea853fdSLi RongQing /* if the skb is not cloned this does nothing 53418ea853fdSLi RongQing * since we set nr_frags to 0. 53428ea853fdSLi RongQing */ 5343c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5344c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5345bad43ca8SEric Dumazet 5346bad43ca8SEric Dumazet to->truesize += delta; 5347bad43ca8SEric Dumazet to->len += len; 5348bad43ca8SEric Dumazet to->data_len += len; 5349bad43ca8SEric Dumazet 5350bad43ca8SEric Dumazet *delta_truesize = delta; 5351bad43ca8SEric Dumazet return true; 5352bad43ca8SEric Dumazet } 5353bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5354621e84d6SNicolas Dichtel 5355621e84d6SNicolas Dichtel /** 53568b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5357621e84d6SNicolas Dichtel * 5358621e84d6SNicolas Dichtel * @skb: buffer to clean 53598b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5360621e84d6SNicolas Dichtel * 53618b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 53628b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 53638b27f277SNicolas Dichtel * operations. 53648b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 53658b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 53668b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5367621e84d6SNicolas Dichtel */ 53688b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5369621e84d6SNicolas Dichtel { 5370621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5371621e84d6SNicolas Dichtel skb->skb_iif = 0; 537260ff7467SWANG Cong skb->ignore_df = 0; 5373621e84d6SNicolas Dichtel skb_dst_drop(skb); 5374174e2381SFlorian Westphal skb_ext_reset(skb); 5375895b5c9fSFlorian Westphal nf_reset_ct(skb); 5376621e84d6SNicolas Dichtel nf_reset_trace(skb); 5377213dd74aSHerbert Xu 53786f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 53796f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5380875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 53816f9a5069SPetr Machata #endif 53826f9a5069SPetr Machata 5383213dd74aSHerbert Xu if (!xnet) 5384213dd74aSHerbert Xu return; 5385213dd74aSHerbert Xu 53862b5ec1a5SYe Yin ipvs_reset(skb); 5387213dd74aSHerbert Xu skb->mark = 0; 5388de799101SMartin KaFai Lau skb_clear_tstamp(skb); 5389621e84d6SNicolas Dichtel } 5390621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5391de960aa9SFlorian Westphal 5392de960aa9SFlorian Westphal /** 5393de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5394de960aa9SFlorian Westphal * 5395de960aa9SFlorian Westphal * @skb: GSO skb 5396de960aa9SFlorian Westphal * 5397de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5398de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5399de960aa9SFlorian Westphal * 5400de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5401de960aa9SFlorian Westphal */ 5402a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5403de960aa9SFlorian Westphal { 5404de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5405f993bc25SFlorian Westphal unsigned int thlen = 0; 5406f993bc25SFlorian Westphal 5407f993bc25SFlorian Westphal if (skb->encapsulation) { 5408f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5409f993bc25SFlorian Westphal skb_transport_header(skb); 5410de960aa9SFlorian Westphal 5411de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5412f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5413f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5414f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 54151dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 541690017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5417ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5418ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5419f993bc25SFlorian Westphal } 54206d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 54216d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 54226d39d589SFlorian Westphal * accounted for. 54236d39d589SFlorian Westphal */ 5424f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5425de960aa9SFlorian Westphal } 5426a4a77718SDaniel Axtens 5427a4a77718SDaniel Axtens /** 5428a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5429a4a77718SDaniel Axtens * 5430a4a77718SDaniel Axtens * @skb: GSO skb 5431a4a77718SDaniel Axtens * 5432a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5433a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5434a4a77718SDaniel Axtens * 5435a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5436a4a77718SDaniel Axtens */ 5437a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5438a4a77718SDaniel Axtens { 5439a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5440a4a77718SDaniel Axtens skb_network_header(skb); 5441a4a77718SDaniel Axtens 5442a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5443a4a77718SDaniel Axtens } 5444a4a77718SDaniel Axtens 5445a4a77718SDaniel Axtens /** 5446a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5447a4a77718SDaniel Axtens * 5448a4a77718SDaniel Axtens * @skb: GSO skb 5449a4a77718SDaniel Axtens * 5450a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5451a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5452a4a77718SDaniel Axtens * headers (TCP/UDP). 5453a4a77718SDaniel Axtens */ 5454a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5455a4a77718SDaniel Axtens { 5456a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5457a4a77718SDaniel Axtens 5458a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5459a4a77718SDaniel Axtens } 54600d5501c1SVlad Yasevich 5461ae7ef81eSMarcelo Ricardo Leitner /** 54622b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 54632b16f048SDaniel Axtens * 54642b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 54652b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 54662b16f048SDaniel Axtens * 54672b16f048SDaniel Axtens * We might want to check: 54682b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 54692b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 54702b16f048SDaniel Axtens * 54712b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 54722b16f048SDaniel Axtens * 547349682bfaSMathieu Malaterre * @skb: GSO skb 547449682bfaSMathieu Malaterre * 54752b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 54762b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 54772b16f048SDaniel Axtens * 54782b16f048SDaniel Axtens * @max_len: The maximum permissible length. 54792b16f048SDaniel Axtens * 54802b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 54812b16f048SDaniel Axtens */ 54822b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 54832b16f048SDaniel Axtens unsigned int seg_len, 54842b16f048SDaniel Axtens unsigned int max_len) { 54852b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 54862b16f048SDaniel Axtens const struct sk_buff *iter; 54872b16f048SDaniel Axtens 54882b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 54892b16f048SDaniel Axtens return seg_len <= max_len; 54902b16f048SDaniel Axtens 54912b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 54922b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 54932b16f048SDaniel Axtens 54942b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 54952b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 54962b16f048SDaniel Axtens return false; 54972b16f048SDaniel Axtens } 54982b16f048SDaniel Axtens 54992b16f048SDaniel Axtens return true; 55002b16f048SDaniel Axtens } 55012b16f048SDaniel Axtens 55022b16f048SDaniel Axtens /** 5503779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5504ae7ef81eSMarcelo Ricardo Leitner * 5505ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 550676f21b99SDavid S. Miller * @mtu: MTU to validate against 5507ae7ef81eSMarcelo Ricardo Leitner * 5508779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5509779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5510779b7931SDaniel Axtens * payload. 5511ae7ef81eSMarcelo Ricardo Leitner */ 5512779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5513ae7ef81eSMarcelo Ricardo Leitner { 55142b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5515ae7ef81eSMarcelo Ricardo Leitner } 5516779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5517ae7ef81eSMarcelo Ricardo Leitner 55182b16f048SDaniel Axtens /** 55192b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 55202b16f048SDaniel Axtens * 55212b16f048SDaniel Axtens * @skb: GSO skb 55222b16f048SDaniel Axtens * @len: length to validate against 55232b16f048SDaniel Axtens * 55242b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 55252b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 55262b16f048SDaniel Axtens */ 55272b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 55282b16f048SDaniel Axtens { 55292b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 55302b16f048SDaniel Axtens } 55312b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 55322b16f048SDaniel Axtens 55330d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 55340d5501c1SVlad Yasevich { 5535d85e8be2SYuya Kusakabe int mac_len, meta_len; 5536d85e8be2SYuya Kusakabe void *meta; 55374bbb3e0eSToshiaki Makita 55380d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 55390d5501c1SVlad Yasevich kfree_skb(skb); 55400d5501c1SVlad Yasevich return NULL; 55410d5501c1SVlad Yasevich } 55420d5501c1SVlad Yasevich 55434bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5544ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 55454bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 55464bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5547ae474573SToshiaki Makita } 5548d85e8be2SYuya Kusakabe 5549d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5550d85e8be2SYuya Kusakabe if (meta_len) { 5551d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5552d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5553d85e8be2SYuya Kusakabe } 5554d85e8be2SYuya Kusakabe 55550d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 55560d5501c1SVlad Yasevich return skb; 55570d5501c1SVlad Yasevich } 55580d5501c1SVlad Yasevich 55590d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 55600d5501c1SVlad Yasevich { 55610d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 55620d5501c1SVlad Yasevich u16 vlan_tci; 55630d5501c1SVlad Yasevich 5564df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 55650d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 55660d5501c1SVlad Yasevich return skb; 55670d5501c1SVlad Yasevich } 55680d5501c1SVlad Yasevich 55690d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 55700d5501c1SVlad Yasevich if (unlikely(!skb)) 55710d5501c1SVlad Yasevich goto err_free; 557255eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 557355eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 55740d5501c1SVlad Yasevich goto err_free; 55750d5501c1SVlad Yasevich 55760d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 55770d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 55780d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 55790d5501c1SVlad Yasevich 55800d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 55810d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 55820d5501c1SVlad Yasevich 55830d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 55840d5501c1SVlad Yasevich if (unlikely(!skb)) 55850d5501c1SVlad Yasevich goto err_free; 55860d5501c1SVlad Yasevich 55870d5501c1SVlad Yasevich skb_reset_network_header(skb); 55888be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 55890d5501c1SVlad Yasevich skb_reset_transport_header(skb); 55900d5501c1SVlad Yasevich skb_reset_mac_len(skb); 55910d5501c1SVlad Yasevich 55920d5501c1SVlad Yasevich return skb; 55930d5501c1SVlad Yasevich 55940d5501c1SVlad Yasevich err_free: 55950d5501c1SVlad Yasevich kfree_skb(skb); 55960d5501c1SVlad Yasevich return NULL; 55970d5501c1SVlad Yasevich } 55980d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 55992e4e4410SEric Dumazet 560092ece280SLiu Jian int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len) 5601e2195121SJiri Pirko { 5602e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5603e2195121SJiri Pirko return -ENOMEM; 5604e2195121SJiri Pirko 5605e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5606e2195121SJiri Pirko return 0; 5607e2195121SJiri Pirko 5608e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5609e2195121SJiri Pirko } 5610e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5611e2195121SJiri Pirko 5612bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5613bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5614bfca4c52SShmulik Ladkani */ 5615bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 561693515d53SJiri Pirko { 561793515d53SJiri Pirko struct vlan_hdr *vhdr; 5618b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 561993515d53SJiri Pirko int err; 562093515d53SJiri Pirko 5621b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5622b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5623b6a79208SShmulik Ladkani offset)) { 5624b6a79208SShmulik Ladkani return -EINVAL; 5625b6a79208SShmulik Ladkani } 5626b6a79208SShmulik Ladkani 562793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 562893515d53SJiri Pirko if (unlikely(err)) 5629b6a79208SShmulik Ladkani return err; 563093515d53SJiri Pirko 563193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 563293515d53SJiri Pirko 563393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 563493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 563593515d53SJiri Pirko 563693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 563793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 563893515d53SJiri Pirko 563993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 564093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 564193515d53SJiri Pirko 564293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 564393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 564493515d53SJiri Pirko 564593515d53SJiri Pirko skb_reset_mac_len(skb); 564693515d53SJiri Pirko 564793515d53SJiri Pirko return err; 564893515d53SJiri Pirko } 5649bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 565093515d53SJiri Pirko 5651b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5652b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5653b6a79208SShmulik Ladkani */ 565493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 565593515d53SJiri Pirko { 565693515d53SJiri Pirko u16 vlan_tci; 565793515d53SJiri Pirko __be16 vlan_proto; 565893515d53SJiri Pirko int err; 565993515d53SJiri Pirko 5660df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5661b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 566293515d53SJiri Pirko } else { 5663ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 566493515d53SJiri Pirko return 0; 566593515d53SJiri Pirko 566693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 566793515d53SJiri Pirko if (err) 566893515d53SJiri Pirko return err; 566993515d53SJiri Pirko } 567093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5671ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 567293515d53SJiri Pirko return 0; 567393515d53SJiri Pirko 567493515d53SJiri Pirko vlan_proto = skb->protocol; 567593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 567693515d53SJiri Pirko if (unlikely(err)) 567793515d53SJiri Pirko return err; 567893515d53SJiri Pirko 567993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 568093515d53SJiri Pirko return 0; 568193515d53SJiri Pirko } 568293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 568393515d53SJiri Pirko 5684b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5685b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5686b6a79208SShmulik Ladkani */ 568793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 568893515d53SJiri Pirko { 5689df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5690b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 569193515d53SJiri Pirko int err; 569293515d53SJiri Pirko 5693b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5694b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5695b6a79208SShmulik Ladkani offset)) { 5696b6a79208SShmulik Ladkani return -EINVAL; 5697b6a79208SShmulik Ladkani } 5698b6a79208SShmulik Ladkani 569993515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5700df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5701b6a79208SShmulik Ladkani if (err) 570293515d53SJiri Pirko return err; 57039241e2dfSDaniel Borkmann 570493515d53SJiri Pirko skb->protocol = skb->vlan_proto; 570593515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 570693515d53SJiri Pirko 57076b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 570893515d53SJiri Pirko } 570993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 571093515d53SJiri Pirko return 0; 571193515d53SJiri Pirko } 571293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 571393515d53SJiri Pirko 571419fbcb36SGuillaume Nault /** 571519fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 571619fbcb36SGuillaume Nault * 571719fbcb36SGuillaume Nault * @skb: Socket buffer to modify 571819fbcb36SGuillaume Nault * 571919fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 572019fbcb36SGuillaume Nault * 572119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 572219fbcb36SGuillaume Nault * present. 572319fbcb36SGuillaume Nault * 572419fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 572519fbcb36SGuillaume Nault */ 572619fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 572719fbcb36SGuillaume Nault { 572819fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 572919fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 573019fbcb36SGuillaume Nault return -EPROTO; 573119fbcb36SGuillaume Nault 573219fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 573319fbcb36SGuillaume Nault skb_reset_mac_header(skb); 573419fbcb36SGuillaume Nault skb_reset_mac_len(skb); 573519fbcb36SGuillaume Nault 573619fbcb36SGuillaume Nault return 0; 573719fbcb36SGuillaume Nault } 573819fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 573919fbcb36SGuillaume Nault 574019fbcb36SGuillaume Nault /** 574119fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 574219fbcb36SGuillaume Nault * 574319fbcb36SGuillaume Nault * @skb: Socket buffer to modify 574419fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 574519fbcb36SGuillaume Nault * @src: Source MAC address of the new header 574619fbcb36SGuillaume Nault * 574719fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 574819fbcb36SGuillaume Nault * 574919fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 575019fbcb36SGuillaume Nault * 575119fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 575219fbcb36SGuillaume Nault */ 575319fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 575419fbcb36SGuillaume Nault const unsigned char *src) 575519fbcb36SGuillaume Nault { 575619fbcb36SGuillaume Nault struct ethhdr *eth; 575719fbcb36SGuillaume Nault int err; 575819fbcb36SGuillaume Nault 575919fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 576019fbcb36SGuillaume Nault return -EPROTO; 576119fbcb36SGuillaume Nault 576219fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 576319fbcb36SGuillaume Nault if (err < 0) 576419fbcb36SGuillaume Nault return err; 576519fbcb36SGuillaume Nault 576619fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 576719fbcb36SGuillaume Nault skb_reset_mac_header(skb); 576819fbcb36SGuillaume Nault skb_reset_mac_len(skb); 576919fbcb36SGuillaume Nault 577019fbcb36SGuillaume Nault eth = eth_hdr(skb); 577119fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 577219fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 577319fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 577419fbcb36SGuillaume Nault 577519fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 577619fbcb36SGuillaume Nault 577719fbcb36SGuillaume Nault return 0; 577819fbcb36SGuillaume Nault } 577919fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 578019fbcb36SGuillaume Nault 57818822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 57828822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 57838822e270SJohn Hurley __be16 ethertype) 57848822e270SJohn Hurley { 57858822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 57868822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 57878822e270SJohn Hurley 57888822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 57898822e270SJohn Hurley } 57908822e270SJohn Hurley 57918822e270SJohn Hurley hdr->h_proto = ethertype; 57928822e270SJohn Hurley } 57938822e270SJohn Hurley 57948822e270SJohn Hurley /** 5795e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5796e7dbfed1SMartin Varghese * the packet 57978822e270SJohn Hurley * 57988822e270SJohn Hurley * @skb: buffer 57998822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 58008822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5801fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 5802e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5803e7dbfed1SMartin Varghese * ethernet 58048822e270SJohn Hurley * 58058822e270SJohn Hurley * Expects skb->data at mac header. 58068822e270SJohn Hurley * 58078822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 58088822e270SJohn Hurley */ 5809fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5810d04ac224SMartin Varghese int mac_len, bool ethernet) 58118822e270SJohn Hurley { 58128822e270SJohn Hurley struct mpls_shim_hdr *lse; 58138822e270SJohn Hurley int err; 58148822e270SJohn Hurley 58158822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 58168822e270SJohn Hurley return -EINVAL; 58178822e270SJohn Hurley 58188822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 58198822e270SJohn Hurley if (skb->encapsulation) 58208822e270SJohn Hurley return -EINVAL; 58218822e270SJohn Hurley 58228822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 58238822e270SJohn Hurley if (unlikely(err)) 58248822e270SJohn Hurley return err; 58258822e270SJohn Hurley 58268822e270SJohn Hurley if (!skb->inner_protocol) { 5827e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 58288822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 58298822e270SJohn Hurley } 58308822e270SJohn Hurley 58318822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 58328822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5833fa4e0f88SDavide Caratti mac_len); 58348822e270SJohn Hurley skb_reset_mac_header(skb); 5835fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5836e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 58378822e270SJohn Hurley 58388822e270SJohn Hurley lse = mpls_hdr(skb); 58398822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 58408822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 58418822e270SJohn Hurley 58424296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 58438822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 58448822e270SJohn Hurley skb->protocol = mpls_proto; 58458822e270SJohn Hurley 58468822e270SJohn Hurley return 0; 58478822e270SJohn Hurley } 58488822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 58498822e270SJohn Hurley 58502e4e4410SEric Dumazet /** 5851ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 5852ed246ceeSJohn Hurley * 5853ed246ceeSJohn Hurley * @skb: buffer 5854ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 5855fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 585676f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 5857ed246ceeSJohn Hurley * 5858ed246ceeSJohn Hurley * Expects skb->data at mac header. 5859ed246ceeSJohn Hurley * 5860ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 5861ed246ceeSJohn Hurley */ 5862040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 5863040b5cfbSMartin Varghese bool ethernet) 5864ed246ceeSJohn Hurley { 5865ed246ceeSJohn Hurley int err; 5866ed246ceeSJohn Hurley 5867ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5868dedc5a08SDavide Caratti return 0; 5869ed246ceeSJohn Hurley 5870fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 5871ed246ceeSJohn Hurley if (unlikely(err)) 5872ed246ceeSJohn Hurley return err; 5873ed246ceeSJohn Hurley 5874ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 5875ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 5876fa4e0f88SDavide Caratti mac_len); 5877ed246ceeSJohn Hurley 5878ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 5879ed246ceeSJohn Hurley skb_reset_mac_header(skb); 5880fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5881ed246ceeSJohn Hurley 58824296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 5883ed246ceeSJohn Hurley struct ethhdr *hdr; 5884ed246ceeSJohn Hurley 5885ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 5886ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 5887ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 5888ed246ceeSJohn Hurley } 5889ed246ceeSJohn Hurley skb->protocol = next_proto; 5890ed246ceeSJohn Hurley 5891ed246ceeSJohn Hurley return 0; 5892ed246ceeSJohn Hurley } 5893ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 5894ed246ceeSJohn Hurley 5895ed246ceeSJohn Hurley /** 5896d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 5897d27cf5c5SJohn Hurley * 5898d27cf5c5SJohn Hurley * @skb: buffer 5899d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 5900d27cf5c5SJohn Hurley * 5901d27cf5c5SJohn Hurley * Expects skb->data at mac header. 5902d27cf5c5SJohn Hurley * 5903d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 5904d27cf5c5SJohn Hurley */ 5905d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 5906d27cf5c5SJohn Hurley { 5907d27cf5c5SJohn Hurley int err; 5908d27cf5c5SJohn Hurley 5909d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5910d27cf5c5SJohn Hurley return -EINVAL; 5911d27cf5c5SJohn Hurley 5912d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 5913d27cf5c5SJohn Hurley if (unlikely(err)) 5914d27cf5c5SJohn Hurley return err; 5915d27cf5c5SJohn Hurley 5916d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 5917d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 5918d27cf5c5SJohn Hurley 5919d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5920d27cf5c5SJohn Hurley } 5921d27cf5c5SJohn Hurley 5922d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 5923d27cf5c5SJohn Hurley 5924d27cf5c5SJohn Hurley return 0; 5925d27cf5c5SJohn Hurley } 5926d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 5927d27cf5c5SJohn Hurley 5928d27cf5c5SJohn Hurley /** 59292a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 59302a2ea508SJohn Hurley * 59312a2ea508SJohn Hurley * @skb: buffer 59322a2ea508SJohn Hurley * 59332a2ea508SJohn Hurley * Expects skb->data at mac header. 59342a2ea508SJohn Hurley * 59352a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 59362a2ea508SJohn Hurley */ 59372a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 59382a2ea508SJohn Hurley { 59392a2ea508SJohn Hurley u32 lse; 59402a2ea508SJohn Hurley u8 ttl; 59412a2ea508SJohn Hurley 59422a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 59432a2ea508SJohn Hurley return -EINVAL; 59442a2ea508SJohn Hurley 594513de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 594613de4ed9SDavide Caratti return -ENOMEM; 594713de4ed9SDavide Caratti 59482a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 59492a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 59502a2ea508SJohn Hurley if (!--ttl) 59512a2ea508SJohn Hurley return -EINVAL; 59522a2ea508SJohn Hurley 59532a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 59542a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 59552a2ea508SJohn Hurley 59562a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 59572a2ea508SJohn Hurley } 59582a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 59592a2ea508SJohn Hurley 59602a2ea508SJohn Hurley /** 59612e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 59622e4e4410SEric Dumazet * 5963de3f0d0eSMasanari Iida * @header_len: size of linear part 5964de3f0d0eSMasanari Iida * @data_len: needed length in frags 5965de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5966de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5967de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 59682e4e4410SEric Dumazet * 59692e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 59702e4e4410SEric Dumazet */ 59712e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 59722e4e4410SEric Dumazet unsigned long data_len, 59732e4e4410SEric Dumazet int max_page_order, 59742e4e4410SEric Dumazet int *errcode, 59752e4e4410SEric Dumazet gfp_t gfp_mask) 59762e4e4410SEric Dumazet { 59772e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 59782e4e4410SEric Dumazet unsigned long chunk; 59792e4e4410SEric Dumazet struct sk_buff *skb; 59802e4e4410SEric Dumazet struct page *page; 59812e4e4410SEric Dumazet int i; 59822e4e4410SEric Dumazet 59832e4e4410SEric Dumazet *errcode = -EMSGSIZE; 59842e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 59852e4e4410SEric Dumazet * high order pages... 59862e4e4410SEric Dumazet */ 59872e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 59882e4e4410SEric Dumazet return NULL; 59892e4e4410SEric Dumazet 59902e4e4410SEric Dumazet *errcode = -ENOBUFS; 5991f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 59922e4e4410SEric Dumazet if (!skb) 59932e4e4410SEric Dumazet return NULL; 59942e4e4410SEric Dumazet 59952e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 59962e4e4410SEric Dumazet 59972e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 59982e4e4410SEric Dumazet int order = max_page_order; 59992e4e4410SEric Dumazet 60002e4e4410SEric Dumazet while (order) { 60012e4e4410SEric Dumazet if (npages >= 1 << order) { 6002d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 60032e4e4410SEric Dumazet __GFP_COMP | 6004d14b56f5SMichal Hocko __GFP_NOWARN, 60052e4e4410SEric Dumazet order); 60062e4e4410SEric Dumazet if (page) 60072e4e4410SEric Dumazet goto fill_page; 60082e4e4410SEric Dumazet /* Do not retry other high order allocations */ 60092e4e4410SEric Dumazet order = 1; 60102e4e4410SEric Dumazet max_page_order = 0; 60112e4e4410SEric Dumazet } 60122e4e4410SEric Dumazet order--; 60132e4e4410SEric Dumazet } 60142e4e4410SEric Dumazet page = alloc_page(gfp_mask); 60152e4e4410SEric Dumazet if (!page) 60162e4e4410SEric Dumazet goto failure; 60172e4e4410SEric Dumazet fill_page: 60182e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 60192e4e4410SEric Dumazet PAGE_SIZE << order); 60202e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 60212e4e4410SEric Dumazet data_len -= chunk; 60222e4e4410SEric Dumazet npages -= 1 << order; 60232e4e4410SEric Dumazet } 60242e4e4410SEric Dumazet return skb; 60252e4e4410SEric Dumazet 60262e4e4410SEric Dumazet failure: 60272e4e4410SEric Dumazet kfree_skb(skb); 60282e4e4410SEric Dumazet return NULL; 60292e4e4410SEric Dumazet } 60302e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 60316fa01ccdSSowmini Varadhan 60326fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 60336fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 60346fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 60356fa01ccdSSowmini Varadhan { 60366fa01ccdSSowmini Varadhan int i; 60376fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 60386fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 60396fa01ccdSSowmini Varadhan u8 *data; 60406fa01ccdSSowmini Varadhan 60416fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 60426fa01ccdSSowmini Varadhan 60436fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 60446fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 60456fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 60466fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 60476fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 60486fa01ccdSSowmini Varadhan if (!data) 60496fa01ccdSSowmini Varadhan return -ENOMEM; 60506fa01ccdSSowmini Varadhan 60516fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 60526fa01ccdSSowmini Varadhan 60536fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 60546fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 60556fa01ccdSSowmini Varadhan skb->len -= off; 60566fa01ccdSSowmini Varadhan 60576fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 60586fa01ccdSSowmini Varadhan skb_shinfo(skb), 60596fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 60606fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 60616fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 60626fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 60636fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 60646fa01ccdSSowmini Varadhan kfree(data); 60656fa01ccdSSowmini Varadhan return -ENOMEM; 60666fa01ccdSSowmini Varadhan } 60676fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 60686fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 60696fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 60706fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 60716fa01ccdSSowmini Varadhan skb_release_data(skb); 60726fa01ccdSSowmini Varadhan } else { 60736fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 60746fa01ccdSSowmini Varadhan * relocate values 60756fa01ccdSSowmini Varadhan */ 60766fa01ccdSSowmini Varadhan skb_free_head(skb); 60776fa01ccdSSowmini Varadhan } 60786fa01ccdSSowmini Varadhan 60796fa01ccdSSowmini Varadhan skb->head = data; 60806fa01ccdSSowmini Varadhan skb->data = data; 60816fa01ccdSSowmini Varadhan skb->head_frag = 0; 6082763087daSEric Dumazet skb_set_end_offset(skb, size); 60836fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 60846fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 60856fa01ccdSSowmini Varadhan skb->cloned = 0; 60866fa01ccdSSowmini Varadhan skb->hdr_len = 0; 60876fa01ccdSSowmini Varadhan skb->nohdr = 0; 60886fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 60896fa01ccdSSowmini Varadhan 60906fa01ccdSSowmini Varadhan return 0; 60916fa01ccdSSowmini Varadhan } 60926fa01ccdSSowmini Varadhan 60936fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 60946fa01ccdSSowmini Varadhan 60956fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 60966fa01ccdSSowmini Varadhan * pskb_carve() 60976fa01ccdSSowmini Varadhan */ 60986fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 60996fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 61006fa01ccdSSowmini Varadhan gfp_t gfp_mask) 61016fa01ccdSSowmini Varadhan { 61026fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 61036fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 61046fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 61056fa01ccdSSowmini Varadhan 61066fa01ccdSSowmini Varadhan do { 61076fa01ccdSSowmini Varadhan if (!list) { 61086fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 61096fa01ccdSSowmini Varadhan return -EFAULT; 61106fa01ccdSSowmini Varadhan } 61116fa01ccdSSowmini Varadhan if (list->len <= eat) { 61126fa01ccdSSowmini Varadhan /* Eaten as whole. */ 61136fa01ccdSSowmini Varadhan eat -= list->len; 61146fa01ccdSSowmini Varadhan list = list->next; 61156fa01ccdSSowmini Varadhan insp = list; 61166fa01ccdSSowmini Varadhan } else { 61176fa01ccdSSowmini Varadhan /* Eaten partially. */ 61186fa01ccdSSowmini Varadhan if (skb_shared(list)) { 61196fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 61206fa01ccdSSowmini Varadhan if (!clone) 61216fa01ccdSSowmini Varadhan return -ENOMEM; 61226fa01ccdSSowmini Varadhan insp = list->next; 61236fa01ccdSSowmini Varadhan list = clone; 61246fa01ccdSSowmini Varadhan } else { 61256fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 61266fa01ccdSSowmini Varadhan insp = list; 61276fa01ccdSSowmini Varadhan } 61286fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 61296fa01ccdSSowmini Varadhan kfree_skb(clone); 61306fa01ccdSSowmini Varadhan return -ENOMEM; 61316fa01ccdSSowmini Varadhan } 61326fa01ccdSSowmini Varadhan break; 61336fa01ccdSSowmini Varadhan } 61346fa01ccdSSowmini Varadhan } while (eat); 61356fa01ccdSSowmini Varadhan 61366fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 61376fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 61386fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 6139ef527f96SEric Dumazet consume_skb(list); 61406fa01ccdSSowmini Varadhan } 61416fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 61426fa01ccdSSowmini Varadhan if (clone) { 61436fa01ccdSSowmini Varadhan clone->next = list; 61446fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 61456fa01ccdSSowmini Varadhan } 61466fa01ccdSSowmini Varadhan return 0; 61476fa01ccdSSowmini Varadhan } 61486fa01ccdSSowmini Varadhan 61496fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 61506fa01ccdSSowmini Varadhan * non-linear part of skb 61516fa01ccdSSowmini Varadhan */ 61526fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 61536fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 61546fa01ccdSSowmini Varadhan { 61556fa01ccdSSowmini Varadhan int i, k = 0; 61566fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 61576fa01ccdSSowmini Varadhan u8 *data; 61586fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 61596fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 61606fa01ccdSSowmini Varadhan 61616fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 61626fa01ccdSSowmini Varadhan 61636fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 61646fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 61656fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 61666fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 61676fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 61686fa01ccdSSowmini Varadhan if (!data) 61696fa01ccdSSowmini Varadhan return -ENOMEM; 61706fa01ccdSSowmini Varadhan 61716fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 61726fa01ccdSSowmini Varadhan 61736fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6174e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 61756fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 61766fa01ccdSSowmini Varadhan kfree(data); 61776fa01ccdSSowmini Varadhan return -ENOMEM; 61786fa01ccdSSowmini Varadhan } 61796fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 61806fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 61816fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 61826fa01ccdSSowmini Varadhan 61836fa01ccdSSowmini Varadhan if (pos + fsize > off) { 61846fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 61856fa01ccdSSowmini Varadhan 61866fa01ccdSSowmini Varadhan if (pos < off) { 61876fa01ccdSSowmini Varadhan /* Split frag. 61886fa01ccdSSowmini Varadhan * We have two variants in this case: 61896fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 61906fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 61916fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 61926fa01ccdSSowmini Varadhan * where splitting is expensive. 61936fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 61946fa01ccdSSowmini Varadhan */ 6195b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 61966fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 61976fa01ccdSSowmini Varadhan } 61986fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 61996fa01ccdSSowmini Varadhan k++; 62006fa01ccdSSowmini Varadhan } 62016fa01ccdSSowmini Varadhan pos += fsize; 62026fa01ccdSSowmini Varadhan } 62036fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 62046fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 62056fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 62066fa01ccdSSowmini Varadhan 62076fa01ccdSSowmini Varadhan /* split line is in frag list */ 6208eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6209eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6210eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6211eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6212eabe8618SMiaohe Lin kfree(data); 6213eabe8618SMiaohe Lin return -ENOMEM; 62146fa01ccdSSowmini Varadhan } 62156fa01ccdSSowmini Varadhan skb_release_data(skb); 62166fa01ccdSSowmini Varadhan 62176fa01ccdSSowmini Varadhan skb->head = data; 62186fa01ccdSSowmini Varadhan skb->head_frag = 0; 62196fa01ccdSSowmini Varadhan skb->data = data; 6220763087daSEric Dumazet skb_set_end_offset(skb, size); 62216fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 62226fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 62236fa01ccdSSowmini Varadhan skb->cloned = 0; 62246fa01ccdSSowmini Varadhan skb->hdr_len = 0; 62256fa01ccdSSowmini Varadhan skb->nohdr = 0; 62266fa01ccdSSowmini Varadhan skb->len -= off; 62276fa01ccdSSowmini Varadhan skb->data_len = skb->len; 62286fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 62296fa01ccdSSowmini Varadhan return 0; 62306fa01ccdSSowmini Varadhan } 62316fa01ccdSSowmini Varadhan 62326fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 62336fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 62346fa01ccdSSowmini Varadhan { 62356fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 62366fa01ccdSSowmini Varadhan 62376fa01ccdSSowmini Varadhan if (len < headlen) 62386fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 62396fa01ccdSSowmini Varadhan else 62406fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 62416fa01ccdSSowmini Varadhan } 62426fa01ccdSSowmini Varadhan 62436fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 62446fa01ccdSSowmini Varadhan * a new skb 62456fa01ccdSSowmini Varadhan */ 62466fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 62476fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 62486fa01ccdSSowmini Varadhan { 62496fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 62506fa01ccdSSowmini Varadhan 62516fa01ccdSSowmini Varadhan if (!clone) 62526fa01ccdSSowmini Varadhan return NULL; 62536fa01ccdSSowmini Varadhan 62546fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 62556fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 62566fa01ccdSSowmini Varadhan kfree_skb(clone); 62576fa01ccdSSowmini Varadhan return NULL; 62586fa01ccdSSowmini Varadhan } 62596fa01ccdSSowmini Varadhan return clone; 62606fa01ccdSSowmini Varadhan } 62616fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6262c8c8b127SEric Dumazet 6263c8c8b127SEric Dumazet /** 6264c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6265c8c8b127SEric Dumazet * @skb: buffer 6266c8c8b127SEric Dumazet * 6267c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6268c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6269c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6270c8c8b127SEric Dumazet * truesize. 6271c8c8b127SEric Dumazet * Notes: 6272c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6273c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6274c8c8b127SEric Dumazet */ 6275c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6276c8c8b127SEric Dumazet { 62773174fed9SEric Dumazet if (skb->data_len) { 62783174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6279c8c8b127SEric Dumazet skb_cloned(skb)) 6280c8c8b127SEric Dumazet return; 6281c8c8b127SEric Dumazet 6282c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6283c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 62843174fed9SEric Dumazet } 62853174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 62863174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 62873174fed9SEric Dumazet * their truesize. 62883174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 62893174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 62903174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6291c8c8b127SEric Dumazet */ 6292c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6293c8c8b127SEric Dumazet } 6294df5042f4SFlorian Westphal 6295df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6296df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6297df5042f4SFlorian Westphal { 6298df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6299df5042f4SFlorian Westphal } 6300df5042f4SFlorian Westphal 63018b69a803SPaolo Abeni /** 63028b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 63038b69a803SPaolo Abeni * 63044930f483SFlorian Westphal * @flags: See kmalloc(). 63054930f483SFlorian Westphal * 63068b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 63078b69a803SPaolo Abeni * skb via __skb_ext_set(). 63088b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 63098b69a803SPaolo Abeni */ 63104930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6311df5042f4SFlorian Westphal { 63124930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6313df5042f4SFlorian Westphal 6314df5042f4SFlorian Westphal if (new) { 6315df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6316df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6317df5042f4SFlorian Westphal } 6318df5042f4SFlorian Westphal 6319df5042f4SFlorian Westphal return new; 6320df5042f4SFlorian Westphal } 6321df5042f4SFlorian Westphal 63224165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 63234165079bSFlorian Westphal unsigned int old_active) 6324df5042f4SFlorian Westphal { 6325df5042f4SFlorian Westphal struct skb_ext *new; 6326df5042f4SFlorian Westphal 6327df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6328df5042f4SFlorian Westphal return old; 6329df5042f4SFlorian Westphal 6330df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6331df5042f4SFlorian Westphal if (!new) 6332df5042f4SFlorian Westphal return NULL; 6333df5042f4SFlorian Westphal 6334df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6335df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6336df5042f4SFlorian Westphal 63374165079bSFlorian Westphal #ifdef CONFIG_XFRM 63384165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 63394165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 63404165079bSFlorian Westphal unsigned int i; 63414165079bSFlorian Westphal 63424165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 63434165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 63444165079bSFlorian Westphal } 63454165079bSFlorian Westphal #endif 6346df5042f4SFlorian Westphal __skb_ext_put(old); 6347df5042f4SFlorian Westphal return new; 6348df5042f4SFlorian Westphal } 6349df5042f4SFlorian Westphal 6350df5042f4SFlorian Westphal /** 63518b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 63528b69a803SPaolo Abeni * @skb: buffer 63538b69a803SPaolo Abeni * @id: extension id 63548b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 63558b69a803SPaolo Abeni * 63568b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 63578b69a803SPaolo Abeni * 63588b69a803SPaolo Abeni * Returns the pointer to the extension. 63598b69a803SPaolo Abeni */ 63608b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 63618b69a803SPaolo Abeni struct skb_ext *ext) 63628b69a803SPaolo Abeni { 63638b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 63648b69a803SPaolo Abeni 63658b69a803SPaolo Abeni skb_ext_put(skb); 63668b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 63678b69a803SPaolo Abeni ext->chunks = newlen; 63688b69a803SPaolo Abeni ext->offset[id] = newoff; 63698b69a803SPaolo Abeni skb->extensions = ext; 63708b69a803SPaolo Abeni skb->active_extensions = 1 << id; 63718b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 63728b69a803SPaolo Abeni } 63738b69a803SPaolo Abeni 63748b69a803SPaolo Abeni /** 6375df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6376df5042f4SFlorian Westphal * @skb: buffer 6377df5042f4SFlorian Westphal * @id: extension to allocate space for 6378df5042f4SFlorian Westphal * 6379df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6380df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6381df5042f4SFlorian Westphal * is returned. 6382df5042f4SFlorian Westphal * 6383df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6384df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6385df5042f4SFlorian Westphal * 6386df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6387df5042f4SFlorian Westphal */ 6388df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6389df5042f4SFlorian Westphal { 6390df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6391df5042f4SFlorian Westphal unsigned int newlen, newoff; 6392df5042f4SFlorian Westphal 6393df5042f4SFlorian Westphal if (skb->active_extensions) { 6394df5042f4SFlorian Westphal old = skb->extensions; 6395df5042f4SFlorian Westphal 63964165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6397df5042f4SFlorian Westphal if (!new) 6398df5042f4SFlorian Westphal return NULL; 6399df5042f4SFlorian Westphal 6400682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6401df5042f4SFlorian Westphal goto set_active; 6402df5042f4SFlorian Westphal 6403e94e50bdSPaolo Abeni newoff = new->chunks; 6404df5042f4SFlorian Westphal } else { 6405df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6406df5042f4SFlorian Westphal 64074930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6408df5042f4SFlorian Westphal if (!new) 6409df5042f4SFlorian Westphal return NULL; 6410df5042f4SFlorian Westphal } 6411df5042f4SFlorian Westphal 6412df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6413df5042f4SFlorian Westphal new->chunks = newlen; 6414df5042f4SFlorian Westphal new->offset[id] = newoff; 6415df5042f4SFlorian Westphal set_active: 6416b0999f38SPaolo Abeni skb->slow_gro = 1; 6417682ec859SPaolo Abeni skb->extensions = new; 6418df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6419df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6420df5042f4SFlorian Westphal } 6421df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6422df5042f4SFlorian Westphal 64234165079bSFlorian Westphal #ifdef CONFIG_XFRM 64244165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 64254165079bSFlorian Westphal { 64264165079bSFlorian Westphal unsigned int i; 64274165079bSFlorian Westphal 64284165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 64294165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 64304165079bSFlorian Westphal } 64314165079bSFlorian Westphal #endif 64324165079bSFlorian Westphal 643378476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 643478476d31SJeremy Kerr static void skb_ext_put_mctp(struct mctp_flow *flow) 643578476d31SJeremy Kerr { 643678476d31SJeremy Kerr if (flow->key) 643778476d31SJeremy Kerr mctp_key_unref(flow->key); 643878476d31SJeremy Kerr } 643978476d31SJeremy Kerr #endif 644078476d31SJeremy Kerr 6441df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6442df5042f4SFlorian Westphal { 6443df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6444df5042f4SFlorian Westphal 6445df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6446df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6447df5042f4SFlorian Westphal skb->extensions = NULL; 6448df5042f4SFlorian Westphal __skb_ext_put(ext); 64494165079bSFlorian Westphal #ifdef CONFIG_XFRM 64504165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 64514165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 64524165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 64534165079bSFlorian Westphal 64544165079bSFlorian Westphal skb_ext_put_sp(sp); 64554165079bSFlorian Westphal sp->len = 0; 64564165079bSFlorian Westphal #endif 6457df5042f4SFlorian Westphal } 6458df5042f4SFlorian Westphal } 6459df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6460df5042f4SFlorian Westphal 6461df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6462df5042f4SFlorian Westphal { 6463df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6464df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6465df5042f4SFlorian Westphal */ 6466df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6467df5042f4SFlorian Westphal goto free_now; 6468df5042f4SFlorian Westphal 6469df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6470df5042f4SFlorian Westphal return; 6471df5042f4SFlorian Westphal free_now: 64724165079bSFlorian Westphal #ifdef CONFIG_XFRM 64734165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 64744165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 64754165079bSFlorian Westphal #endif 647678476d31SJeremy Kerr #ifdef CONFIG_MCTP_FLOWS 647778476d31SJeremy Kerr if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 647878476d31SJeremy Kerr skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 647978476d31SJeremy Kerr #endif 64804165079bSFlorian Westphal 6481df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6482df5042f4SFlorian Westphal } 6483df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6484df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 648568822bdfSEric Dumazet 648668822bdfSEric Dumazet /** 648768822bdfSEric Dumazet * skb_attempt_defer_free - queue skb for remote freeing 648868822bdfSEric Dumazet * @skb: buffer 648968822bdfSEric Dumazet * 649068822bdfSEric Dumazet * Put @skb in a per-cpu list, using the cpu which 649168822bdfSEric Dumazet * allocated the skb/pages to reduce false sharing 649268822bdfSEric Dumazet * and memory zone spinlock contention. 649368822bdfSEric Dumazet */ 649468822bdfSEric Dumazet void skb_attempt_defer_free(struct sk_buff *skb) 649568822bdfSEric Dumazet { 649668822bdfSEric Dumazet int cpu = skb->alloc_cpu; 649768822bdfSEric Dumazet struct softnet_data *sd; 649868822bdfSEric Dumazet unsigned long flags; 649968822bdfSEric Dumazet bool kick; 650068822bdfSEric Dumazet 650168822bdfSEric Dumazet if (WARN_ON_ONCE(cpu >= nr_cpu_ids) || 650268822bdfSEric Dumazet !cpu_online(cpu) || 650368822bdfSEric Dumazet cpu == raw_smp_processor_id()) { 650468822bdfSEric Dumazet __kfree_skb(skb); 650568822bdfSEric Dumazet return; 650668822bdfSEric Dumazet } 650768822bdfSEric Dumazet 650868822bdfSEric Dumazet sd = &per_cpu(softnet_data, cpu); 650968822bdfSEric Dumazet /* We do not send an IPI or any signal. 651068822bdfSEric Dumazet * Remote cpu will eventually call skb_defer_free_flush() 651168822bdfSEric Dumazet */ 651268822bdfSEric Dumazet spin_lock_irqsave(&sd->defer_lock, flags); 651368822bdfSEric Dumazet skb->next = sd->defer_list; 651468822bdfSEric Dumazet /* Paired with READ_ONCE() in skb_defer_free_flush() */ 651568822bdfSEric Dumazet WRITE_ONCE(sd->defer_list, skb); 651668822bdfSEric Dumazet sd->defer_count++; 651768822bdfSEric Dumazet 651868822bdfSEric Dumazet /* kick every time queue length reaches 128. 6519*97e719a8SEric Dumazet * This condition should hardly be hit under normal conditions, 652068822bdfSEric Dumazet * unless cpu suddenly stopped to receive NIC interrupts. 652168822bdfSEric Dumazet */ 652268822bdfSEric Dumazet kick = sd->defer_count == 128; 652368822bdfSEric Dumazet 652468822bdfSEric Dumazet spin_unlock_irqrestore(&sd->defer_lock, flags); 652568822bdfSEric Dumazet 652668822bdfSEric Dumazet /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU 652768822bdfSEric Dumazet * if we are unlucky enough (this seems very unlikely). 652868822bdfSEric Dumazet */ 6529*97e719a8SEric Dumazet if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) 653068822bdfSEric Dumazet smp_call_function_single_async(cpu, &sd->defer_csd); 653168822bdfSEric Dumazet } 6532