11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Fixes: 81da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 91da177e4SLinus Torvalds * balancer bugs. 101da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 111da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 121da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 131da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 141da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 151da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 161da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 171da177e4SLinus Torvalds * only put in the headers 181da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 191da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 201da177e4SLinus Torvalds * Andi Kleen : slabified it. 211da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * NOTE: 241da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 251da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 261da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 271da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 301da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 311da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 321da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds 39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 40e005d193SJoe Perches 411da177e4SLinus Torvalds #include <linux/module.h> 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 44fe55f6d5SVegard Nossum #include <linux/kmemcheck.h> 451da177e4SLinus Torvalds #include <linux/mm.h> 461da177e4SLinus Torvalds #include <linux/interrupt.h> 471da177e4SLinus Torvalds #include <linux/in.h> 481da177e4SLinus Torvalds #include <linux/inet.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/netdevice.h> 511da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 531da177e4SLinus Torvalds #endif 541da177e4SLinus Torvalds #include <linux/string.h> 551da177e4SLinus Torvalds #include <linux/skbuff.h> 569c55e01cSJens Axboe #include <linux/splice.h> 571da177e4SLinus Torvalds #include <linux/cache.h> 581da177e4SLinus Torvalds #include <linux/rtnetlink.h> 591da177e4SLinus Torvalds #include <linux/init.h> 60716ea3a7SDavid Howells #include <linux/scatterlist.h> 61ac45f602SPatrick Ohly #include <linux/errqueue.h> 62268bb0ceSLinus Torvalds #include <linux/prefetch.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #include <net/protocol.h> 651da177e4SLinus Torvalds #include <net/dst.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/checksum.h> 681da177e4SLinus Torvalds #include <net/xfrm.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #include <asm/uaccess.h> 71ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7251c56b00SEric Dumazet #include <linux/highmem.h> 73a1f8e7f7SAl Viro 74d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 75e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 761da177e4SLinus Torvalds 779c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 789c55e01cSJens Axboe struct pipe_buffer *buf) 799c55e01cSJens Axboe { 808b9d3728SJarek Poplawski put_page(buf->page); 819c55e01cSJens Axboe } 829c55e01cSJens Axboe 839c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 849c55e01cSJens Axboe struct pipe_buffer *buf) 859c55e01cSJens Axboe { 868b9d3728SJarek Poplawski get_page(buf->page); 879c55e01cSJens Axboe } 889c55e01cSJens Axboe 899c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 909c55e01cSJens Axboe struct pipe_buffer *buf) 919c55e01cSJens Axboe { 929c55e01cSJens Axboe return 1; 939c55e01cSJens Axboe } 949c55e01cSJens Axboe 959c55e01cSJens Axboe 969c55e01cSJens Axboe /* Pipe buffer operations for a socket. */ 9728dfef8fSAlexey Dobriyan static const struct pipe_buf_operations sock_pipe_buf_ops = { 989c55e01cSJens Axboe .can_merge = 0, 999c55e01cSJens Axboe .map = generic_pipe_buf_map, 1009c55e01cSJens Axboe .unmap = generic_pipe_buf_unmap, 1019c55e01cSJens Axboe .confirm = generic_pipe_buf_confirm, 1029c55e01cSJens Axboe .release = sock_pipe_buf_release, 1039c55e01cSJens Axboe .steal = sock_pipe_buf_steal, 1049c55e01cSJens Axboe .get = sock_pipe_buf_get, 1059c55e01cSJens Axboe }; 1069c55e01cSJens Axboe 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 1091da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 1101da177e4SLinus Torvalds * reliable. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /** 1141da177e4SLinus Torvalds * skb_over_panic - private function 1151da177e4SLinus Torvalds * @skb: buffer 1161da177e4SLinus Torvalds * @sz: size 1171da177e4SLinus Torvalds * @here: address 1181da177e4SLinus Torvalds * 1191da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 1201da177e4SLinus Torvalds */ 121ccb7c773SRami Rosen static void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1221da177e4SLinus Torvalds { 123e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 124e005d193SJoe Perches __func__, here, skb->len, sz, skb->head, skb->data, 1254305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12626095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1271da177e4SLinus Torvalds BUG(); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /** 1311da177e4SLinus Torvalds * skb_under_panic - private function 1321da177e4SLinus Torvalds * @skb: buffer 1331da177e4SLinus Torvalds * @sz: size 1341da177e4SLinus Torvalds * @here: address 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1371da177e4SLinus Torvalds */ 1381da177e4SLinus Torvalds 139ccb7c773SRami Rosen static void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1401da177e4SLinus Torvalds { 141e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 142e005d193SJoe Perches __func__, here, skb->len, sz, skb->head, skb->data, 1434305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 14426095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1451da177e4SLinus Torvalds BUG(); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 148c93bdd0eSMel Gorman 149c93bdd0eSMel Gorman /* 150c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 151c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 152c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 153c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 154c93bdd0eSMel Gorman * memory is free 155c93bdd0eSMel Gorman */ 156c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 157c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 158c93bdd0eSMel Gorman void *__kmalloc_reserve(size_t size, gfp_t flags, int node, unsigned long ip, 159c93bdd0eSMel Gorman bool *pfmemalloc) 160c93bdd0eSMel Gorman { 161c93bdd0eSMel Gorman void *obj; 162c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 163c93bdd0eSMel Gorman 164c93bdd0eSMel Gorman /* 165c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 166c93bdd0eSMel Gorman * to the reserves, fail. 167c93bdd0eSMel Gorman */ 168c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 169c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 170c93bdd0eSMel Gorman node); 171c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 172c93bdd0eSMel Gorman goto out; 173c93bdd0eSMel Gorman 174c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 175c93bdd0eSMel Gorman ret_pfmemalloc = true; 176c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 177c93bdd0eSMel Gorman 178c93bdd0eSMel Gorman out: 179c93bdd0eSMel Gorman if (pfmemalloc) 180c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 181c93bdd0eSMel Gorman 182c93bdd0eSMel Gorman return obj; 183c93bdd0eSMel Gorman } 184c93bdd0eSMel Gorman 1851da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1861da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1871da177e4SLinus Torvalds * [BEEP] leaks. 1881da177e4SLinus Torvalds * 1891da177e4SLinus Torvalds */ 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /** 192d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1931da177e4SLinus Torvalds * @size: size to allocate 1941da177e4SLinus Torvalds * @gfp_mask: allocation mask 195c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 196c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 197c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 198c93bdd0eSMel Gorman * allocations in case the data is required for writeback 199b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 2001da177e4SLinus Torvalds * 2011da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 20294b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 20394b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 2041da177e4SLinus Torvalds * 2051da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 2061da177e4SLinus Torvalds * %GFP_ATOMIC. 2071da177e4SLinus Torvalds */ 208dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 209c93bdd0eSMel Gorman int flags, int node) 2101da177e4SLinus Torvalds { 211e18b890bSChristoph Lameter struct kmem_cache *cache; 2124947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 2131da177e4SLinus Torvalds struct sk_buff *skb; 2141da177e4SLinus Torvalds u8 *data; 215c93bdd0eSMel Gorman bool pfmemalloc; 2161da177e4SLinus Torvalds 217c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 218c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 219c93bdd0eSMel Gorman 220c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 221c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 2228798b3fbSHerbert Xu 2231da177e4SLinus Torvalds /* Get the HEAD */ 224b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 2251da177e4SLinus Torvalds if (!skb) 2261da177e4SLinus Torvalds goto out; 227ec7d2f2cSEric Dumazet prefetchw(skb); 2281da177e4SLinus Torvalds 22987fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 23087fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 23187fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 23287fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 23387fb4b7bSEric Dumazet */ 234bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 23587fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 236c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2371da177e4SLinus Torvalds if (!data) 2381da177e4SLinus Torvalds goto nodata; 23987fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 24087fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 24187fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 24287fb4b7bSEric Dumazet */ 24387fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 244ec7d2f2cSEric Dumazet prefetchw(data + size); 2451da177e4SLinus Torvalds 246ca0605a7SArnaldo Carvalho de Melo /* 247c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 248c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 249c8005785SJohannes Berg * the tail pointer in struct sk_buff! 250ca0605a7SArnaldo Carvalho de Melo */ 251ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 25287fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 25387fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 254c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 2551da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2561da177e4SLinus Torvalds skb->head = data; 2571da177e4SLinus Torvalds skb->data = data; 25827a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2594305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 26019633e12SStephen Hemminger #ifdef NET_SKBUFF_DATA_USES_OFFSET 26119633e12SStephen Hemminger skb->mac_header = ~0U; 26219633e12SStephen Hemminger #endif 26319633e12SStephen Hemminger 2644947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2654947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 266ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2674947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 268c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2694947d3efSBenjamin LaHaise 270c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 271d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 272d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 2731da177e4SLinus Torvalds 274fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags1); 275fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags2); 276d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 277d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 278d179cd12SDavid S. Miller 279d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 280c93bdd0eSMel Gorman child->pfmemalloc = pfmemalloc; 281d179cd12SDavid S. Miller } 2821da177e4SLinus Torvalds out: 2831da177e4SLinus Torvalds return skb; 2841da177e4SLinus Torvalds nodata: 2858798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2861da177e4SLinus Torvalds skb = NULL; 2871da177e4SLinus Torvalds goto out; 2881da177e4SLinus Torvalds } 289b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /** 292b2b5ce9dSEric Dumazet * build_skb - build a network buffer 293b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 294d3836f21SEric Dumazet * @frag_size: size of fragment, or 0 if head was kmalloced 295b2b5ce9dSEric Dumazet * 296b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 297b2b5ce9dSEric Dumazet * skb_shared_info. @data must have been allocated by kmalloc() 298b2b5ce9dSEric Dumazet * The return is the new skb buffer. 299b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 300b2b5ce9dSEric Dumazet * Notes : 301b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 302b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 303b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 304b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 305b2b5ce9dSEric Dumazet * before giving packet to stack. 306b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 307b2b5ce9dSEric Dumazet */ 308d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 309b2b5ce9dSEric Dumazet { 310b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 311b2b5ce9dSEric Dumazet struct sk_buff *skb; 312d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 313b2b5ce9dSEric Dumazet 314b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 315b2b5ce9dSEric Dumazet if (!skb) 316b2b5ce9dSEric Dumazet return NULL; 317b2b5ce9dSEric Dumazet 318d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 319b2b5ce9dSEric Dumazet 320b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 321b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 322d3836f21SEric Dumazet skb->head_frag = frag_size != 0; 323b2b5ce9dSEric Dumazet atomic_set(&skb->users, 1); 324b2b5ce9dSEric Dumazet skb->head = data; 325b2b5ce9dSEric Dumazet skb->data = data; 326b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 327b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 328b2b5ce9dSEric Dumazet #ifdef NET_SKBUFF_DATA_USES_OFFSET 329b2b5ce9dSEric Dumazet skb->mac_header = ~0U; 330b2b5ce9dSEric Dumazet #endif 331b2b5ce9dSEric Dumazet 332b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 333b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 334b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 335b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 336b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 337b2b5ce9dSEric Dumazet 338b2b5ce9dSEric Dumazet return skb; 339b2b5ce9dSEric Dumazet } 340b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 341b2b5ce9dSEric Dumazet 342a1c7fff7SEric Dumazet struct netdev_alloc_cache { 34369b08f62SEric Dumazet struct page_frag frag; 34469b08f62SEric Dumazet /* we maintain a pagecount bias, so that we dont dirty cache line 34569b08f62SEric Dumazet * containing page->_count every time we allocate a fragment. 34669b08f62SEric Dumazet */ 347540eb7bfSAlexander Duyck unsigned int pagecnt_bias; 348a1c7fff7SEric Dumazet }; 349a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); 350a1c7fff7SEric Dumazet 35169b08f62SEric Dumazet #define NETDEV_FRAG_PAGE_MAX_ORDER get_order(32768) 35269b08f62SEric Dumazet #define NETDEV_FRAG_PAGE_MAX_SIZE (PAGE_SIZE << NETDEV_FRAG_PAGE_MAX_ORDER) 35369b08f62SEric Dumazet #define NETDEV_PAGECNT_MAX_BIAS NETDEV_FRAG_PAGE_MAX_SIZE 354540eb7bfSAlexander Duyck 355c93bdd0eSMel Gorman static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 3566f532612SEric Dumazet { 3576f532612SEric Dumazet struct netdev_alloc_cache *nc; 3586f532612SEric Dumazet void *data = NULL; 35969b08f62SEric Dumazet int order; 3606f532612SEric Dumazet unsigned long flags; 3616f532612SEric Dumazet 3626f532612SEric Dumazet local_irq_save(flags); 3636f532612SEric Dumazet nc = &__get_cpu_var(netdev_alloc_cache); 36469b08f62SEric Dumazet if (unlikely(!nc->frag.page)) { 3656f532612SEric Dumazet refill: 36669b08f62SEric Dumazet for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) { 36769b08f62SEric Dumazet gfp_t gfp = gfp_mask; 36869b08f62SEric Dumazet 36969b08f62SEric Dumazet if (order) 37069b08f62SEric Dumazet gfp |= __GFP_COMP | __GFP_NOWARN; 37169b08f62SEric Dumazet nc->frag.page = alloc_pages(gfp, order); 37269b08f62SEric Dumazet if (likely(nc->frag.page)) 37369b08f62SEric Dumazet break; 37469b08f62SEric Dumazet if (--order < 0) 375540eb7bfSAlexander Duyck goto end; 37669b08f62SEric Dumazet } 37769b08f62SEric Dumazet nc->frag.size = PAGE_SIZE << order; 378540eb7bfSAlexander Duyck recycle: 37969b08f62SEric Dumazet atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS); 38069b08f62SEric Dumazet nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 38169b08f62SEric Dumazet nc->frag.offset = 0; 3826f532612SEric Dumazet } 383540eb7bfSAlexander Duyck 38469b08f62SEric Dumazet if (nc->frag.offset + fragsz > nc->frag.size) { 385540eb7bfSAlexander Duyck /* avoid unnecessary locked operations if possible */ 38669b08f62SEric Dumazet if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) || 38769b08f62SEric Dumazet atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count)) 388540eb7bfSAlexander Duyck goto recycle; 3896f532612SEric Dumazet goto refill; 3906f532612SEric Dumazet } 391540eb7bfSAlexander Duyck 39269b08f62SEric Dumazet data = page_address(nc->frag.page) + nc->frag.offset; 39369b08f62SEric Dumazet nc->frag.offset += fragsz; 394540eb7bfSAlexander Duyck nc->pagecnt_bias--; 395540eb7bfSAlexander Duyck end: 3966f532612SEric Dumazet local_irq_restore(flags); 3976f532612SEric Dumazet return data; 3986f532612SEric Dumazet } 399c93bdd0eSMel Gorman 400c93bdd0eSMel Gorman /** 401c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 402c93bdd0eSMel Gorman * @fragsz: fragment size 403c93bdd0eSMel Gorman * 404c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 405c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 406c93bdd0eSMel Gorman */ 407c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 408c93bdd0eSMel Gorman { 409c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 410c93bdd0eSMel Gorman } 4116f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 4126f532612SEric Dumazet 4136f532612SEric Dumazet /** 4148af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 4158af27456SChristoph Hellwig * @dev: network device to receive on 4168af27456SChristoph Hellwig * @length: length to allocate 4178af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 4188af27456SChristoph Hellwig * 4198af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 4208af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 4218af27456SChristoph Hellwig * the headroom they think they need without accounting for the 4228af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 4238af27456SChristoph Hellwig * 4248af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 4258af27456SChristoph Hellwig */ 4268af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 4278af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 4288af27456SChristoph Hellwig { 4296f532612SEric Dumazet struct sk_buff *skb = NULL; 430a1c7fff7SEric Dumazet unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + 431a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4328af27456SChristoph Hellwig 433310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 434c93bdd0eSMel Gorman void *data; 435c93bdd0eSMel Gorman 436c93bdd0eSMel Gorman if (sk_memalloc_socks()) 437c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 438c93bdd0eSMel Gorman 439c93bdd0eSMel Gorman data = __netdev_alloc_frag(fragsz, gfp_mask); 440a1c7fff7SEric Dumazet 4416f532612SEric Dumazet if (likely(data)) { 4426f532612SEric Dumazet skb = build_skb(data, fragsz); 4436f532612SEric Dumazet if (unlikely(!skb)) 4446f532612SEric Dumazet put_page(virt_to_head_page(data)); 445a1c7fff7SEric Dumazet } 446a1c7fff7SEric Dumazet } else { 447c93bdd0eSMel Gorman skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 448c93bdd0eSMel Gorman SKB_ALLOC_RX, NUMA_NO_NODE); 449a1c7fff7SEric Dumazet } 4507b2e497aSChristoph Hellwig if (likely(skb)) { 4518af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4527b2e497aSChristoph Hellwig skb->dev = dev; 4537b2e497aSChristoph Hellwig } 4548af27456SChristoph Hellwig return skb; 4558af27456SChristoph Hellwig } 456b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4571da177e4SLinus Torvalds 458654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 45950269e19SEric Dumazet int size, unsigned int truesize) 460654bed16SPeter Zijlstra { 461654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 462654bed16SPeter Zijlstra skb->len += size; 463654bed16SPeter Zijlstra skb->data_len += size; 46450269e19SEric Dumazet skb->truesize += truesize; 465654bed16SPeter Zijlstra } 466654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 467654bed16SPeter Zijlstra 46827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 4691da177e4SLinus Torvalds { 47027b437c8SHerbert Xu struct sk_buff *list = *listp; 4711da177e4SLinus Torvalds 47227b437c8SHerbert Xu *listp = NULL; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds do { 4751da177e4SLinus Torvalds struct sk_buff *this = list; 4761da177e4SLinus Torvalds list = list->next; 4771da177e4SLinus Torvalds kfree_skb(this); 4781da177e4SLinus Torvalds } while (list); 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 48127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 48227b437c8SHerbert Xu { 48327b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 48427b437c8SHerbert Xu } 48527b437c8SHerbert Xu 4861da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds struct sk_buff *list; 4891da177e4SLinus Torvalds 490fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 4911da177e4SLinus Torvalds skb_get(list); 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 495d3836f21SEric Dumazet { 496d3836f21SEric Dumazet if (skb->head_frag) 497d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 498d3836f21SEric Dumazet else 499d3836f21SEric Dumazet kfree(skb->head); 500d3836f21SEric Dumazet } 501d3836f21SEric Dumazet 5025bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds if (!skb->cloned || 5051da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 5061da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 5071da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 5081da177e4SLinus Torvalds int i; 5091da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 510ea2ab693SIan Campbell skb_frag_unref(skb, i); 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 513a6686f2fSShirley Ma /* 514a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 515a6686f2fSShirley Ma * the lower device DMA has done; 516a6686f2fSShirley Ma */ 517a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 518a6686f2fSShirley Ma struct ubuf_info *uarg; 519a6686f2fSShirley Ma 520a6686f2fSShirley Ma uarg = skb_shinfo(skb)->destructor_arg; 521a6686f2fSShirley Ma if (uarg->callback) 522a6686f2fSShirley Ma uarg->callback(uarg); 523a6686f2fSShirley Ma } 524a6686f2fSShirley Ma 52521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 5261da177e4SLinus Torvalds skb_drop_fraglist(skb); 5271da177e4SLinus Torvalds 528d3836f21SEric Dumazet skb_free_head(skb); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /* 5331da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5341da177e4SLinus Torvalds */ 5352d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5361da177e4SLinus Torvalds { 537d179cd12SDavid S. Miller struct sk_buff *other; 538d179cd12SDavid S. Miller atomic_t *fclone_ref; 539d179cd12SDavid S. Miller 540d179cd12SDavid S. Miller switch (skb->fclone) { 541d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5421da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 543d179cd12SDavid S. Miller break; 544d179cd12SDavid S. Miller 545d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 546d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 547d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 548d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 549d179cd12SDavid S. Miller break; 550d179cd12SDavid S. Miller 551d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 552d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 553d179cd12SDavid S. Miller other = skb - 1; 554d179cd12SDavid S. Miller 555d179cd12SDavid S. Miller /* The clone portion is available for 556d179cd12SDavid S. Miller * fast-cloning again. 557d179cd12SDavid S. Miller */ 558d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 559d179cd12SDavid S. Miller 560d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 561d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 562d179cd12SDavid S. Miller break; 5633ff50b79SStephen Hemminger } 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 56604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 5671da177e4SLinus Torvalds { 568adf30907SEric Dumazet skb_dst_drop(skb); 5691da177e4SLinus Torvalds #ifdef CONFIG_XFRM 5701da177e4SLinus Torvalds secpath_put(skb->sp); 5711da177e4SLinus Torvalds #endif 5721da177e4SLinus Torvalds if (skb->destructor) { 5739c2b3328SStephen Hemminger WARN_ON(in_irq()); 5741da177e4SLinus Torvalds skb->destructor(skb); 5751da177e4SLinus Torvalds } 576a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 5775f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 5782fc72c7bSKOVACS Krisztian #endif 5792fc72c7bSKOVACS Krisztian #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED 5809fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 5819fb9cbb1SYasuyuki Kozakai #endif 5821da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 5831da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 5841da177e4SLinus Torvalds #endif 5851da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 5861da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5871da177e4SLinus Torvalds skb->tc_index = 0; 5881da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 5891da177e4SLinus Torvalds skb->tc_verd = 0; 5901da177e4SLinus Torvalds #endif 5911da177e4SLinus Torvalds #endif 59204a4bb55SLennert Buytenhek } 59304a4bb55SLennert Buytenhek 59404a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 59504a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 59604a4bb55SLennert Buytenhek { 59704a4bb55SLennert Buytenhek skb_release_head_state(skb); 5982d4baff8SHerbert Xu skb_release_data(skb); 5992d4baff8SHerbert Xu } 6001da177e4SLinus Torvalds 6012d4baff8SHerbert Xu /** 6022d4baff8SHerbert Xu * __kfree_skb - private function 6032d4baff8SHerbert Xu * @skb: buffer 6042d4baff8SHerbert Xu * 6052d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6062d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6072d4baff8SHerbert Xu * always call kfree_skb 6082d4baff8SHerbert Xu */ 6092d4baff8SHerbert Xu 6102d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6112d4baff8SHerbert Xu { 6122d4baff8SHerbert Xu skb_release_all(skb); 6131da177e4SLinus Torvalds kfree_skbmem(skb); 6141da177e4SLinus Torvalds } 615b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds /** 618231d06aeSJörn Engel * kfree_skb - free an sk_buff 619231d06aeSJörn Engel * @skb: buffer to free 620231d06aeSJörn Engel * 621231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 622231d06aeSJörn Engel * hit zero. 623231d06aeSJörn Engel */ 624231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 625231d06aeSJörn Engel { 626231d06aeSJörn Engel if (unlikely(!skb)) 627231d06aeSJörn Engel return; 628231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 629231d06aeSJörn Engel smp_rmb(); 630231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 631231d06aeSJörn Engel return; 632ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 633231d06aeSJörn Engel __kfree_skb(skb); 634231d06aeSJörn Engel } 635b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 636231d06aeSJörn Engel 637d1a203eaSStephen Hemminger /** 638ead2ceb0SNeil Horman * consume_skb - free an skbuff 639ead2ceb0SNeil Horman * @skb: buffer to free 640ead2ceb0SNeil Horman * 641ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 642ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 643ead2ceb0SNeil Horman * is being dropped after a failure and notes that 644ead2ceb0SNeil Horman */ 645ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 646ead2ceb0SNeil Horman { 647ead2ceb0SNeil Horman if (unlikely(!skb)) 648ead2ceb0SNeil Horman return; 649ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 650ead2ceb0SNeil Horman smp_rmb(); 651ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 652ead2ceb0SNeil Horman return; 65307dc22e7SKoki Sanagi trace_consume_skb(skb); 654ead2ceb0SNeil Horman __kfree_skb(skb); 655ead2ceb0SNeil Horman } 656ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 657ead2ceb0SNeil Horman 658dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 659dec18810SHerbert Xu { 660dec18810SHerbert Xu new->tstamp = old->tstamp; 661dec18810SHerbert Xu new->dev = old->dev; 662dec18810SHerbert Xu new->transport_header = old->transport_header; 663dec18810SHerbert Xu new->network_header = old->network_header; 664dec18810SHerbert Xu new->mac_header = old->mac_header; 6657fee226aSEric Dumazet skb_dst_copy(new, old); 6660a9627f2STom Herbert new->rxhash = old->rxhash; 6676461be3aSChangli Gao new->ooo_okay = old->ooo_okay; 668bdeab991STom Herbert new->l4_rxhash = old->l4_rxhash; 6693bdc0ebaSBen Greear new->no_fcs = old->no_fcs; 670def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 671dec18810SHerbert Xu new->sp = secpath_get(old->sp); 672dec18810SHerbert Xu #endif 673dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 6749bcb97caSHerbert Xu new->csum = old->csum; 675dec18810SHerbert Xu new->local_df = old->local_df; 676dec18810SHerbert Xu new->pkt_type = old->pkt_type; 677dec18810SHerbert Xu new->ip_summed = old->ip_summed; 678dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 679dec18810SHerbert Xu new->priority = old->priority; 680a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_IP_VS) 681dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 682dec18810SHerbert Xu #endif 683c93bdd0eSMel Gorman new->pfmemalloc = old->pfmemalloc; 684dec18810SHerbert Xu new->protocol = old->protocol; 685dec18810SHerbert Xu new->mark = old->mark; 6868964be4aSEric Dumazet new->skb_iif = old->skb_iif; 687dec18810SHerbert Xu __nf_copy(new, old); 688a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 689dec18810SHerbert Xu new->nf_trace = old->nf_trace; 690dec18810SHerbert Xu #endif 691dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 692dec18810SHerbert Xu new->tc_index = old->tc_index; 693dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 694dec18810SHerbert Xu new->tc_verd = old->tc_verd; 695dec18810SHerbert Xu #endif 696dec18810SHerbert Xu #endif 6976aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 6986aa895b0SPatrick McHardy 699dec18810SHerbert Xu skb_copy_secmark(new, old); 700dec18810SHerbert Xu } 701dec18810SHerbert Xu 70282c49a35SHerbert Xu /* 70382c49a35SHerbert Xu * You should not add any new code to this function. Add it to 70482c49a35SHerbert Xu * __copy_skb_header above instead. 70582c49a35SHerbert Xu */ 706e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds #define C(x) n->x = skb->x 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds n->next = n->prev = NULL; 7111da177e4SLinus Torvalds n->sk = NULL; 712dec18810SHerbert Xu __copy_skb_header(n, skb); 713dec18810SHerbert Xu 7141da177e4SLinus Torvalds C(len); 7151da177e4SLinus Torvalds C(data_len); 7163e6b3b2eSAlexey Dobriyan C(mac_len); 717334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 71802f1c89dSPaul Moore n->cloned = 1; 7191da177e4SLinus Torvalds n->nohdr = 0; 7201da177e4SLinus Torvalds n->destructor = NULL; 7211da177e4SLinus Torvalds C(tail); 7221da177e4SLinus Torvalds C(end); 72302f1c89dSPaul Moore C(head); 724d3836f21SEric Dumazet C(head_frag); 72502f1c89dSPaul Moore C(data); 72602f1c89dSPaul Moore C(truesize); 72702f1c89dSPaul Moore atomic_set(&n->users, 1); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 7301da177e4SLinus Torvalds skb->cloned = 1; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds return n; 733e0053ec0SHerbert Xu #undef C 734e0053ec0SHerbert Xu } 735e0053ec0SHerbert Xu 736e0053ec0SHerbert Xu /** 737e0053ec0SHerbert Xu * skb_morph - morph one skb into another 738e0053ec0SHerbert Xu * @dst: the skb to receive the contents 739e0053ec0SHerbert Xu * @src: the skb to supply the contents 740e0053ec0SHerbert Xu * 741e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 742e0053ec0SHerbert Xu * supplied by the user. 743e0053ec0SHerbert Xu * 744e0053ec0SHerbert Xu * The target skb is returned upon exit. 745e0053ec0SHerbert Xu */ 746e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 747e0053ec0SHerbert Xu { 7482d4baff8SHerbert Xu skb_release_all(dst); 749e0053ec0SHerbert Xu return __skb_clone(dst, src); 750e0053ec0SHerbert Xu } 751e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 752e0053ec0SHerbert Xu 7532c53040fSBen Hutchings /** 7542c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 75548c83012SMichael S. Tsirkin * @skb: the skb to modify 75648c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 75748c83012SMichael S. Tsirkin * 75848c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 75948c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 76048c83012SMichael S. Tsirkin * to userspace pages. 76148c83012SMichael S. Tsirkin * 76248c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 76348c83012SMichael S. Tsirkin * %GFP_ATOMIC. 76448c83012SMichael S. Tsirkin * 76548c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 76648c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 76748c83012SMichael S. Tsirkin */ 76848c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 769a6686f2fSShirley Ma { 770a6686f2fSShirley Ma int i; 771a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 772a6686f2fSShirley Ma struct page *page, *head = NULL; 773a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 774a6686f2fSShirley Ma 775a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 776a6686f2fSShirley Ma u8 *vaddr; 777a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 778a6686f2fSShirley Ma 77902756ed4SKrishna Kumar page = alloc_page(gfp_mask); 780a6686f2fSShirley Ma if (!page) { 781a6686f2fSShirley Ma while (head) { 782a6686f2fSShirley Ma struct page *next = (struct page *)head->private; 783a6686f2fSShirley Ma put_page(head); 784a6686f2fSShirley Ma head = next; 785a6686f2fSShirley Ma } 786a6686f2fSShirley Ma return -ENOMEM; 787a6686f2fSShirley Ma } 78851c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 789a6686f2fSShirley Ma memcpy(page_address(page), 7909e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 79151c56b00SEric Dumazet kunmap_atomic(vaddr); 792a6686f2fSShirley Ma page->private = (unsigned long)head; 793a6686f2fSShirley Ma head = page; 794a6686f2fSShirley Ma } 795a6686f2fSShirley Ma 796a6686f2fSShirley Ma /* skb frags release userspace buffers */ 79702756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 798a8605c60SIan Campbell skb_frag_unref(skb, i); 799a6686f2fSShirley Ma 800a6686f2fSShirley Ma uarg->callback(uarg); 801a6686f2fSShirley Ma 802a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 80302756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 80402756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 80502756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 806a6686f2fSShirley Ma head = (struct page *)head->private; 807a6686f2fSShirley Ma } 80848c83012SMichael S. Tsirkin 80948c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 810a6686f2fSShirley Ma return 0; 811a6686f2fSShirley Ma } 812dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 813a6686f2fSShirley Ma 814e0053ec0SHerbert Xu /** 815e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 816e0053ec0SHerbert Xu * @skb: buffer to clone 817e0053ec0SHerbert Xu * @gfp_mask: allocation priority 818e0053ec0SHerbert Xu * 819e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 820e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 821e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 822e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 823e0053ec0SHerbert Xu * 824e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 825e0053ec0SHerbert Xu * %GFP_ATOMIC. 826e0053ec0SHerbert Xu */ 827e0053ec0SHerbert Xu 828e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 829e0053ec0SHerbert Xu { 830e0053ec0SHerbert Xu struct sk_buff *n; 831e0053ec0SHerbert Xu 83270008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 833a6686f2fSShirley Ma return NULL; 834a6686f2fSShirley Ma 835e0053ec0SHerbert Xu n = skb + 1; 836e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 837e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 838e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 839e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 840e0053ec0SHerbert Xu atomic_inc(fclone_ref); 841e0053ec0SHerbert Xu } else { 842c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 843c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 844c93bdd0eSMel Gorman 845e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 846e0053ec0SHerbert Xu if (!n) 847e0053ec0SHerbert Xu return NULL; 848fe55f6d5SVegard Nossum 849fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 850fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags2); 851e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 852e0053ec0SHerbert Xu } 853e0053ec0SHerbert Xu 854e0053ec0SHerbert Xu return __skb_clone(n, skb); 8551da177e4SLinus Torvalds } 856b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 8591da177e4SLinus Torvalds { 8602e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 8611da177e4SLinus Torvalds /* 8621da177e4SLinus Torvalds * Shift between the two data areas in bytes 8631da177e4SLinus Torvalds */ 8641da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 8652e07fa9cSArnaldo Carvalho de Melo #endif 866dec18810SHerbert Xu 867dec18810SHerbert Xu __copy_skb_header(new, old); 868dec18810SHerbert Xu 8692e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 8702e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 8712e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 8722e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 873603a8bbeSStephen Hemminger if (skb_mac_header_was_set(new)) 8742e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 8752e07fa9cSArnaldo Carvalho de Melo #endif 8767967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 8777967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 8787967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 881c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 882c93bdd0eSMel Gorman { 883c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 884c93bdd0eSMel Gorman return SKB_ALLOC_RX; 885c93bdd0eSMel Gorman return 0; 886c93bdd0eSMel Gorman } 887c93bdd0eSMel Gorman 8881da177e4SLinus Torvalds /** 8891da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 8901da177e4SLinus Torvalds * @skb: buffer to copy 8911da177e4SLinus Torvalds * @gfp_mask: allocation priority 8921da177e4SLinus Torvalds * 8931da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 8941da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 8951da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 8961da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 8971da177e4SLinus Torvalds * 8981da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 8991da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 9001da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 9011da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 9021da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 9031da177e4SLinus Torvalds */ 9041da177e4SLinus Torvalds 905dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 9061da177e4SLinus Torvalds { 9076602cebbSEric Dumazet int headerlen = skb_headroom(skb); 908ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 909c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 910c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 9116602cebbSEric Dumazet 9121da177e4SLinus Torvalds if (!n) 9131da177e4SLinus Torvalds return NULL; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds /* Set the data pointer */ 9161da177e4SLinus Torvalds skb_reserve(n, headerlen); 9171da177e4SLinus Torvalds /* Set the tail pointer and length */ 9181da177e4SLinus Torvalds skb_put(n, skb->len); 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 9211da177e4SLinus Torvalds BUG(); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds copy_skb_header(n, skb); 9241da177e4SLinus Torvalds return n; 9251da177e4SLinus Torvalds } 926b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /** 929117632e6SEric Dumazet * __pskb_copy - create copy of an sk_buff with private head. 9301da177e4SLinus Torvalds * @skb: buffer to copy 931117632e6SEric Dumazet * @headroom: headroom of new skb 9321da177e4SLinus Torvalds * @gfp_mask: allocation priority 9331da177e4SLinus Torvalds * 9341da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 9351da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 9361da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 9371da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 9381da177e4SLinus Torvalds * or the pointer to the buffer on success. 9391da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds 942117632e6SEric Dumazet struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask) 9431da177e4SLinus Torvalds { 944117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 945c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 946c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 9476602cebbSEric Dumazet 9481da177e4SLinus Torvalds if (!n) 9491da177e4SLinus Torvalds goto out; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /* Set the data pointer */ 952117632e6SEric Dumazet skb_reserve(n, headroom); 9531da177e4SLinus Torvalds /* Set the tail pointer and length */ 9541da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 9551da177e4SLinus Torvalds /* Copy the bytes */ 956d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 9571da177e4SLinus Torvalds 95825f484a6SHerbert Xu n->truesize += skb->data_len; 9591da177e4SLinus Torvalds n->data_len = skb->data_len; 9601da177e4SLinus Torvalds n->len = skb->len; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 9631da177e4SLinus Torvalds int i; 9641da177e4SLinus Torvalds 96570008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 9661511022cSDan Carpenter kfree_skb(n); 9671511022cSDan Carpenter n = NULL; 968a6686f2fSShirley Ma goto out; 969a6686f2fSShirley Ma } 9701da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 9711da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 972ea2ab693SIan Campbell skb_frag_ref(skb, i); 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 97721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 9781da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 9791da177e4SLinus Torvalds skb_clone_fraglist(n); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds copy_skb_header(n, skb); 9831da177e4SLinus Torvalds out: 9841da177e4SLinus Torvalds return n; 9851da177e4SLinus Torvalds } 986117632e6SEric Dumazet EXPORT_SYMBOL(__pskb_copy); 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /** 9891da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 9901da177e4SLinus Torvalds * @skb: buffer to reallocate 9911da177e4SLinus Torvalds * @nhead: room to add at head 9921da177e4SLinus Torvalds * @ntail: room to add at tail 9931da177e4SLinus Torvalds * @gfp_mask: allocation priority 9941da177e4SLinus Torvalds * 9951da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 9961da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 9971da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 9981da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 9991da177e4SLinus Torvalds * 10001da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 10011da177e4SLinus Torvalds * reloaded after call to this function. 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds 100486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1005dd0fc66fSAl Viro gfp_t gfp_mask) 10061da177e4SLinus Torvalds { 10071da177e4SLinus Torvalds int i; 10081da177e4SLinus Torvalds u8 *data; 1009ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 10101da177e4SLinus Torvalds long off; 10111da177e4SLinus Torvalds 10124edd87adSHerbert Xu BUG_ON(nhead < 0); 10134edd87adSHerbert Xu 10141da177e4SLinus Torvalds if (skb_shared(skb)) 10151da177e4SLinus Torvalds BUG(); 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 10181da177e4SLinus Torvalds 1019c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1020c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1021c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1022c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 10231da177e4SLinus Torvalds if (!data) 10241da177e4SLinus Torvalds goto nodata; 102587151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 10286602cebbSEric Dumazet * optimized for the cases when header is void. 10296602cebbSEric Dumazet */ 10306602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 10316602cebbSEric Dumazet 10326602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 10336602cebbSEric Dumazet skb_shinfo(skb), 1034fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 10351da177e4SLinus Torvalds 10363e24591aSAlexander Duyck /* 10373e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 10383e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 10393e24591aSAlexander Duyck * be since all we did is relocate the values 10403e24591aSAlexander Duyck */ 10413e24591aSAlexander Duyck if (skb_cloned(skb)) { 1042a6686f2fSShirley Ma /* copy this zero copy skb frags */ 104370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1044a6686f2fSShirley Ma goto nofrags; 10451da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1046ea2ab693SIan Campbell skb_frag_ref(skb, i); 10471da177e4SLinus Torvalds 104821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 10491da177e4SLinus Torvalds skb_clone_fraglist(skb); 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds skb_release_data(skb); 10523e24591aSAlexander Duyck } else { 10533e24591aSAlexander Duyck skb_free_head(skb); 10541fd63041SEric Dumazet } 10551da177e4SLinus Torvalds off = (data + nhead) - skb->head; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds skb->head = data; 1058d3836f21SEric Dumazet skb->head_frag = 0; 10591da177e4SLinus Torvalds skb->data += off; 10604305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 10614305b541SArnaldo Carvalho de Melo skb->end = size; 106256eb8882SPatrick McHardy off = nhead; 10634305b541SArnaldo Carvalho de Melo #else 10644305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 106556eb8882SPatrick McHardy #endif 106627a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 106727a884dcSArnaldo Carvalho de Melo skb->tail += off; 1068b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 1069b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 1070603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1071b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 107200c5a983SAndrea Shepard /* Only adjust this if it actually is csum_start rather than csum */ 107300c5a983SAndrea Shepard if (skb->ip_summed == CHECKSUM_PARTIAL) 1074172a863fSHerbert Xu skb->csum_start += nhead; 10751da177e4SLinus Torvalds skb->cloned = 0; 1076334a8132SPatrick McHardy skb->hdr_len = 0; 10771da177e4SLinus Torvalds skb->nohdr = 0; 10781da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 10791da177e4SLinus Torvalds return 0; 10801da177e4SLinus Torvalds 1081a6686f2fSShirley Ma nofrags: 1082a6686f2fSShirley Ma kfree(data); 10831da177e4SLinus Torvalds nodata: 10841da177e4SLinus Torvalds return -ENOMEM; 10851da177e4SLinus Torvalds } 1086b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 10911da177e4SLinus Torvalds { 10921da177e4SLinus Torvalds struct sk_buff *skb2; 10931da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds if (delta <= 0) 10961da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 10971da177e4SLinus Torvalds else { 10981da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 10991da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 11001da177e4SLinus Torvalds GFP_ATOMIC)) { 11011da177e4SLinus Torvalds kfree_skb(skb2); 11021da177e4SLinus Torvalds skb2 = NULL; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds return skb2; 11061da177e4SLinus Torvalds } 1107b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /** 11101da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 11111da177e4SLinus Torvalds * @skb: buffer to copy 11121da177e4SLinus Torvalds * @newheadroom: new free bytes at head 11131da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 11141da177e4SLinus Torvalds * @gfp_mask: allocation priority 11151da177e4SLinus Torvalds * 11161da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 11171da177e4SLinus Torvalds * allocate additional space. 11181da177e4SLinus Torvalds * 11191da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 11201da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 11211da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 11221da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 11231da177e4SLinus Torvalds * 11241da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 11251da177e4SLinus Torvalds * is called from an interrupt. 11261da177e4SLinus Torvalds */ 11271da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 112886a76cafSVictor Fusco int newheadroom, int newtailroom, 1129dd0fc66fSAl Viro gfp_t gfp_mask) 11301da177e4SLinus Torvalds { 11311da177e4SLinus Torvalds /* 11321da177e4SLinus Torvalds * Allocate the copy buffer 11331da177e4SLinus Torvalds */ 1134c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1135c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1136c93bdd0eSMel Gorman NUMA_NO_NODE); 1137efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 11381da177e4SLinus Torvalds int head_copy_len, head_copy_off; 113952886051SHerbert Xu int off; 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds if (!n) 11421da177e4SLinus Torvalds return NULL; 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds skb_reserve(n, newheadroom); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /* Set the tail pointer and length */ 11471da177e4SLinus Torvalds skb_put(n, skb->len); 11481da177e4SLinus Torvalds 1149efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 11501da177e4SLinus Torvalds head_copy_off = 0; 11511da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 11521da177e4SLinus Torvalds head_copy_len = newheadroom; 11531da177e4SLinus Torvalds else 11541da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* Copy the linear header and data. */ 11571da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 11581da177e4SLinus Torvalds skb->len + head_copy_len)) 11591da177e4SLinus Torvalds BUG(); 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds copy_skb_header(n, skb); 11621da177e4SLinus Torvalds 1163efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 1164be2b6e62SDavid S. Miller if (n->ip_summed == CHECKSUM_PARTIAL) 116552886051SHerbert Xu n->csum_start += off; 116652886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 1167efd1e8d5SPatrick McHardy n->transport_header += off; 1168efd1e8d5SPatrick McHardy n->network_header += off; 1169603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1170efd1e8d5SPatrick McHardy n->mac_header += off; 117152886051SHerbert Xu #endif 1172efd1e8d5SPatrick McHardy 11731da177e4SLinus Torvalds return n; 11741da177e4SLinus Torvalds } 1175b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /** 11781da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 11791da177e4SLinus Torvalds * @skb: buffer to pad 11801da177e4SLinus Torvalds * @pad: space to pad 11811da177e4SLinus Torvalds * 11821da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 11831da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 11841da177e4SLinus Torvalds * beyond the buffer end onto the wire. 11851da177e4SLinus Torvalds * 11865b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 11871da177e4SLinus Torvalds */ 11881da177e4SLinus Torvalds 11895b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 11901da177e4SLinus Torvalds { 11915b057c6bSHerbert Xu int err; 11925b057c6bSHerbert Xu int ntail; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 11955b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 11961da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 11975b057c6bSHerbert Xu return 0; 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds 12004305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 12015b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 12025b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 12035b057c6bSHerbert Xu if (unlikely(err)) 12045b057c6bSHerbert Xu goto free_skb; 12055b057c6bSHerbert Xu } 12065b057c6bSHerbert Xu 12075b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 12085b057c6bSHerbert Xu * to be audited. 12095b057c6bSHerbert Xu */ 12105b057c6bSHerbert Xu err = skb_linearize(skb); 12115b057c6bSHerbert Xu if (unlikely(err)) 12125b057c6bSHerbert Xu goto free_skb; 12135b057c6bSHerbert Xu 12145b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 12155b057c6bSHerbert Xu return 0; 12165b057c6bSHerbert Xu 12175b057c6bSHerbert Xu free_skb: 12181da177e4SLinus Torvalds kfree_skb(skb); 12195b057c6bSHerbert Xu return err; 12201da177e4SLinus Torvalds } 1221b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 12221da177e4SLinus Torvalds 12230dde3e16SIlpo Järvinen /** 12240dde3e16SIlpo Järvinen * skb_put - add data to a buffer 12250dde3e16SIlpo Järvinen * @skb: buffer to use 12260dde3e16SIlpo Järvinen * @len: amount of data to add 12270dde3e16SIlpo Järvinen * 12280dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 12290dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 12300dde3e16SIlpo Järvinen * first byte of the extra data is returned. 12310dde3e16SIlpo Järvinen */ 12320dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 12330dde3e16SIlpo Järvinen { 12340dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 12350dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 12360dde3e16SIlpo Järvinen skb->tail += len; 12370dde3e16SIlpo Järvinen skb->len += len; 12380dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 12390dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 12400dde3e16SIlpo Järvinen return tmp; 12410dde3e16SIlpo Järvinen } 12420dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 12430dde3e16SIlpo Järvinen 12446be8ac2fSIlpo Järvinen /** 1245c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1246c2aa270aSIlpo Järvinen * @skb: buffer to use 1247c2aa270aSIlpo Järvinen * @len: amount of data to add 1248c2aa270aSIlpo Järvinen * 1249c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1250c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1251c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1252c2aa270aSIlpo Järvinen */ 1253c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1254c2aa270aSIlpo Järvinen { 1255c2aa270aSIlpo Järvinen skb->data -= len; 1256c2aa270aSIlpo Järvinen skb->len += len; 1257c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1258c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1259c2aa270aSIlpo Järvinen return skb->data; 1260c2aa270aSIlpo Järvinen } 1261c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1262c2aa270aSIlpo Järvinen 1263c2aa270aSIlpo Järvinen /** 12646be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 12656be8ac2fSIlpo Järvinen * @skb: buffer to use 12666be8ac2fSIlpo Järvinen * @len: amount of data to remove 12676be8ac2fSIlpo Järvinen * 12686be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 12696be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 12706be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 12716be8ac2fSIlpo Järvinen * the old data. 12726be8ac2fSIlpo Järvinen */ 12736be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 12746be8ac2fSIlpo Järvinen { 127547d29646SDavid S. Miller return skb_pull_inline(skb, len); 12766be8ac2fSIlpo Järvinen } 12776be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 12786be8ac2fSIlpo Järvinen 1279419ae74eSIlpo Järvinen /** 1280419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1281419ae74eSIlpo Järvinen * @skb: buffer to alter 1282419ae74eSIlpo Järvinen * @len: new length 1283419ae74eSIlpo Järvinen * 1284419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1285419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1286419ae74eSIlpo Järvinen * The skb must be linear. 1287419ae74eSIlpo Järvinen */ 1288419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1289419ae74eSIlpo Järvinen { 1290419ae74eSIlpo Järvinen if (skb->len > len) 1291419ae74eSIlpo Järvinen __skb_trim(skb, len); 1292419ae74eSIlpo Järvinen } 1293419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1294419ae74eSIlpo Järvinen 12953cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 12961da177e4SLinus Torvalds */ 12971da177e4SLinus Torvalds 12983cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 12991da177e4SLinus Torvalds { 130027b437c8SHerbert Xu struct sk_buff **fragp; 130127b437c8SHerbert Xu struct sk_buff *frag; 13021da177e4SLinus Torvalds int offset = skb_headlen(skb); 13031da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 13041da177e4SLinus Torvalds int i; 130527b437c8SHerbert Xu int err; 130627b437c8SHerbert Xu 130727b437c8SHerbert Xu if (skb_cloned(skb) && 130827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 130927b437c8SHerbert Xu return err; 13101da177e4SLinus Torvalds 1311f4d26fb3SHerbert Xu i = 0; 1312f4d26fb3SHerbert Xu if (offset >= len) 1313f4d26fb3SHerbert Xu goto drop_pages; 1314f4d26fb3SHerbert Xu 1315f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 13169e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 131727b437c8SHerbert Xu 131827b437c8SHerbert Xu if (end < len) { 13191da177e4SLinus Torvalds offset = end; 132027b437c8SHerbert Xu continue; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13239e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 132427b437c8SHerbert Xu 1325f4d26fb3SHerbert Xu drop_pages: 132627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 132727b437c8SHerbert Xu 132827b437c8SHerbert Xu for (; i < nfrags; i++) 1329ea2ab693SIan Campbell skb_frag_unref(skb, i); 133027b437c8SHerbert Xu 133121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 133227b437c8SHerbert Xu skb_drop_fraglist(skb); 1333f4d26fb3SHerbert Xu goto done; 133427b437c8SHerbert Xu } 133527b437c8SHerbert Xu 133627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 133727b437c8SHerbert Xu fragp = &frag->next) { 133827b437c8SHerbert Xu int end = offset + frag->len; 133927b437c8SHerbert Xu 134027b437c8SHerbert Xu if (skb_shared(frag)) { 134127b437c8SHerbert Xu struct sk_buff *nfrag; 134227b437c8SHerbert Xu 134327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 134427b437c8SHerbert Xu if (unlikely(!nfrag)) 134527b437c8SHerbert Xu return -ENOMEM; 134627b437c8SHerbert Xu 134727b437c8SHerbert Xu nfrag->next = frag->next; 134885bb2a60SEric Dumazet consume_skb(frag); 134927b437c8SHerbert Xu frag = nfrag; 135027b437c8SHerbert Xu *fragp = frag; 135127b437c8SHerbert Xu } 135227b437c8SHerbert Xu 135327b437c8SHerbert Xu if (end < len) { 135427b437c8SHerbert Xu offset = end; 135527b437c8SHerbert Xu continue; 135627b437c8SHerbert Xu } 135727b437c8SHerbert Xu 135827b437c8SHerbert Xu if (end > len && 135927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 136027b437c8SHerbert Xu return err; 136127b437c8SHerbert Xu 136227b437c8SHerbert Xu if (frag->next) 136327b437c8SHerbert Xu skb_drop_list(&frag->next); 136427b437c8SHerbert Xu break; 136527b437c8SHerbert Xu } 136627b437c8SHerbert Xu 1367f4d26fb3SHerbert Xu done: 136827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 13691da177e4SLinus Torvalds skb->data_len -= skb->len - len; 13701da177e4SLinus Torvalds skb->len = len; 13711da177e4SLinus Torvalds } else { 13721da177e4SLinus Torvalds skb->len = len; 13731da177e4SLinus Torvalds skb->data_len = 0; 137427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds return 0; 13781da177e4SLinus Torvalds } 1379b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /** 13821da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 13831da177e4SLinus Torvalds * @skb: buffer to reallocate 13841da177e4SLinus Torvalds * @delta: number of bytes to advance tail 13851da177e4SLinus Torvalds * 13861da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 13871da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 13881da177e4SLinus Torvalds * data from fragmented part. 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 13911da177e4SLinus Torvalds * 13921da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 13931da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 13941da177e4SLinus Torvalds * 13951da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 13961da177e4SLinus Torvalds * reloaded after call to this function. 13971da177e4SLinus Torvalds */ 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 14001da177e4SLinus Torvalds * when it is necessary. 14011da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 14021da177e4SLinus Torvalds * 2. It may change skb pointers. 14031da177e4SLinus Torvalds * 14041da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 14051da177e4SLinus Torvalds */ 14061da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 14071da177e4SLinus Torvalds { 14081da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 14091da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 14101da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 14111da177e4SLinus Torvalds */ 14124305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 14151da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 14161da177e4SLinus Torvalds GFP_ATOMIC)) 14171da177e4SLinus Torvalds return NULL; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 142027a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 14211da177e4SLinus Torvalds BUG(); 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 14241da177e4SLinus Torvalds * size of pulled pages. Superb. 14251da177e4SLinus Torvalds */ 142621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 14271da177e4SLinus Torvalds goto pull_pages; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 14301da177e4SLinus Torvalds eat = delta; 14311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14329e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 14339e903e08SEric Dumazet 14349e903e08SEric Dumazet if (size >= eat) 14351da177e4SLinus Torvalds goto pull_pages; 14369e903e08SEric Dumazet eat -= size; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 14401da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 14411da177e4SLinus Torvalds * but taking into account that pulling is expected to 14421da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 14431da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 14441da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds if (eat) { 14471da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 14481da177e4SLinus Torvalds struct sk_buff *clone = NULL; 14491da177e4SLinus Torvalds struct sk_buff *insp = NULL; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds do { 145209a62660SKris Katterjohn BUG_ON(!list); 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds if (list->len <= eat) { 14551da177e4SLinus Torvalds /* Eaten as whole. */ 14561da177e4SLinus Torvalds eat -= list->len; 14571da177e4SLinus Torvalds list = list->next; 14581da177e4SLinus Torvalds insp = list; 14591da177e4SLinus Torvalds } else { 14601da177e4SLinus Torvalds /* Eaten partially. */ 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds if (skb_shared(list)) { 14631da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 14641da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 14651da177e4SLinus Torvalds if (!clone) 14661da177e4SLinus Torvalds return NULL; 14671da177e4SLinus Torvalds insp = list->next; 14681da177e4SLinus Torvalds list = clone; 14691da177e4SLinus Torvalds } else { 14701da177e4SLinus Torvalds /* This may be pulled without 14711da177e4SLinus Torvalds * problems. */ 14721da177e4SLinus Torvalds insp = list; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 14751da177e4SLinus Torvalds kfree_skb(clone); 14761da177e4SLinus Torvalds return NULL; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds break; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds } while (eat); 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds /* Free pulled out fragments. */ 14831da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 14841da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 14851da177e4SLinus Torvalds kfree_skb(list); 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds /* And insert new clone at head. */ 14881da177e4SLinus Torvalds if (clone) { 14891da177e4SLinus Torvalds clone->next = list; 14901da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds pull_pages: 14961da177e4SLinus Torvalds eat = delta; 14971da177e4SLinus Torvalds k = 0; 14981da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14999e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 15009e903e08SEric Dumazet 15019e903e08SEric Dumazet if (size <= eat) { 1502ea2ab693SIan Campbell skb_frag_unref(skb, i); 15039e903e08SEric Dumazet eat -= size; 15041da177e4SLinus Torvalds } else { 15051da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 15061da177e4SLinus Torvalds if (eat) { 15071da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 15089e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 15091da177e4SLinus Torvalds eat = 0; 15101da177e4SLinus Torvalds } 15111da177e4SLinus Torvalds k++; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds skb->tail += delta; 15171da177e4SLinus Torvalds skb->data_len -= delta; 15181da177e4SLinus Torvalds 151927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 15201da177e4SLinus Torvalds } 1521b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 15221da177e4SLinus Torvalds 152322019b17SEric Dumazet /** 152422019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 152522019b17SEric Dumazet * @skb: source skb 152622019b17SEric Dumazet * @offset: offset in source 152722019b17SEric Dumazet * @to: destination buffer 152822019b17SEric Dumazet * @len: number of bytes to copy 152922019b17SEric Dumazet * 153022019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 153122019b17SEric Dumazet * destination buffer. 153222019b17SEric Dumazet * 153322019b17SEric Dumazet * CAUTION ! : 153422019b17SEric Dumazet * If its prototype is ever changed, 153522019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 153622019b17SEric Dumazet * since it is called from BPF assembly code. 153722019b17SEric Dumazet */ 15381da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 15391da177e4SLinus Torvalds { 15401a028e50SDavid S. Miller int start = skb_headlen(skb); 1541fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1542fbb398a8SDavid S. Miller int i, copy; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds if (offset > (int)skb->len - len) 15451da177e4SLinus Torvalds goto fault; 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* Copy header. */ 15481a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 15491da177e4SLinus Torvalds if (copy > len) 15501da177e4SLinus Torvalds copy = len; 1551d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 15521da177e4SLinus Torvalds if ((len -= copy) == 0) 15531da177e4SLinus Torvalds return 0; 15541da177e4SLinus Torvalds offset += copy; 15551da177e4SLinus Torvalds to += copy; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15591a028e50SDavid S. Miller int end; 156051c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 15611da177e4SLinus Torvalds 1562547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15631a028e50SDavid S. Miller 156451c56b00SEric Dumazet end = start + skb_frag_size(f); 15651da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15661da177e4SLinus Torvalds u8 *vaddr; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds if (copy > len) 15691da177e4SLinus Torvalds copy = len; 15701da177e4SLinus Torvalds 157151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 15721da177e4SLinus Torvalds memcpy(to, 157351c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 157451c56b00SEric Dumazet copy); 157551c56b00SEric Dumazet kunmap_atomic(vaddr); 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds if ((len -= copy) == 0) 15781da177e4SLinus Torvalds return 0; 15791da177e4SLinus Torvalds offset += copy; 15801da177e4SLinus Torvalds to += copy; 15811da177e4SLinus Torvalds } 15821a028e50SDavid S. Miller start = end; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 1585fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 15861a028e50SDavid S. Miller int end; 15871da177e4SLinus Torvalds 1588547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15891a028e50SDavid S. Miller 1590fbb398a8SDavid S. Miller end = start + frag_iter->len; 15911da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15921da177e4SLinus Torvalds if (copy > len) 15931da177e4SLinus Torvalds copy = len; 1594fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 15951da177e4SLinus Torvalds goto fault; 15961da177e4SLinus Torvalds if ((len -= copy) == 0) 15971da177e4SLinus Torvalds return 0; 15981da177e4SLinus Torvalds offset += copy; 15991da177e4SLinus Torvalds to += copy; 16001da177e4SLinus Torvalds } 16011a028e50SDavid S. Miller start = end; 16021da177e4SLinus Torvalds } 1603a6686f2fSShirley Ma 16041da177e4SLinus Torvalds if (!len) 16051da177e4SLinus Torvalds return 0; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds fault: 16081da177e4SLinus Torvalds return -EFAULT; 16091da177e4SLinus Torvalds } 1610b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 16111da177e4SLinus Torvalds 16129c55e01cSJens Axboe /* 16139c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 16149c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 16159c55e01cSJens Axboe */ 16169c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 16179c55e01cSJens Axboe { 16188b9d3728SJarek Poplawski put_page(spd->pages[i]); 16198b9d3728SJarek Poplawski } 16209c55e01cSJens Axboe 1621a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 16224fb66994SJarek Poplawski unsigned int *offset, 16237a67e56fSJarek Poplawski struct sk_buff *skb, struct sock *sk) 16248b9d3728SJarek Poplawski { 16255640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 16268b9d3728SJarek Poplawski 16275640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 16288b9d3728SJarek Poplawski return NULL; 16294fb66994SJarek Poplawski 16305640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 16314fb66994SJarek Poplawski 16325640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 16335640f768SEric Dumazet page_address(page) + *offset, *len); 16345640f768SEric Dumazet *offset = pfrag->offset; 16355640f768SEric Dumazet pfrag->offset += *len; 16364fb66994SJarek Poplawski 16375640f768SEric Dumazet return pfrag->page; 16389c55e01cSJens Axboe } 16399c55e01cSJens Axboe 164041c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 164141c73a0dSEric Dumazet struct page *page, 164241c73a0dSEric Dumazet unsigned int offset) 164341c73a0dSEric Dumazet { 164441c73a0dSEric Dumazet return spd->nr_pages && 164541c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 164641c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 164741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 164841c73a0dSEric Dumazet } 164941c73a0dSEric Dumazet 16509c55e01cSJens Axboe /* 16519c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 16529c55e01cSJens Axboe */ 1653a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 165435f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 16554fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 1656d7ccf7c0SEric Dumazet struct sk_buff *skb, bool linear, 16577a67e56fSJarek Poplawski struct sock *sk) 16589c55e01cSJens Axboe { 165941c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1660a108d5f3SDavid S. Miller return true; 16619c55e01cSJens Axboe 16628b9d3728SJarek Poplawski if (linear) { 16637a67e56fSJarek Poplawski page = linear_to_page(page, len, &offset, skb, sk); 16648b9d3728SJarek Poplawski if (!page) 1665a108d5f3SDavid S. Miller return true; 166641c73a0dSEric Dumazet } 166741c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 166841c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1669a108d5f3SDavid S. Miller return false; 167041c73a0dSEric Dumazet } 16718b9d3728SJarek Poplawski get_page(page); 16729c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 16734fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 16749c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 16759c55e01cSJens Axboe spd->nr_pages++; 16768b9d3728SJarek Poplawski 1677a108d5f3SDavid S. Miller return false; 16789c55e01cSJens Axboe } 16799c55e01cSJens Axboe 16802870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 16812870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 16822870c43dSOctavian Purdila { 1683ce3dd395SJarek Poplawski unsigned long n; 1684ce3dd395SJarek Poplawski 16852870c43dSOctavian Purdila *poff += off; 1686ce3dd395SJarek Poplawski n = *poff / PAGE_SIZE; 1687ce3dd395SJarek Poplawski if (n) 1688ce3dd395SJarek Poplawski *page = nth_page(*page, n); 1689ce3dd395SJarek Poplawski 16902870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 16912870c43dSOctavian Purdila *plen -= off; 16922870c43dSOctavian Purdila } 16932870c43dSOctavian Purdila 1694a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 16952870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 16962870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 1697d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 169835f3d14dSJens Axboe struct sock *sk, 169935f3d14dSJens Axboe struct pipe_inode_info *pipe) 17009c55e01cSJens Axboe { 17012870c43dSOctavian Purdila if (!*len) 1702a108d5f3SDavid S. Miller return true; 17039c55e01cSJens Axboe 17042870c43dSOctavian Purdila /* skip this segment if already processed */ 17052870c43dSOctavian Purdila if (*off >= plen) { 17062870c43dSOctavian Purdila *off -= plen; 1707a108d5f3SDavid S. Miller return false; 17082870c43dSOctavian Purdila } 17092870c43dSOctavian Purdila 17102870c43dSOctavian Purdila /* ignore any bits we already processed */ 17112870c43dSOctavian Purdila if (*off) { 17122870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 17132870c43dSOctavian Purdila *off = 0; 17142870c43dSOctavian Purdila } 17152870c43dSOctavian Purdila 17162870c43dSOctavian Purdila do { 17172870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 17182870c43dSOctavian Purdila 17192870c43dSOctavian Purdila /* the linear region may spread across several pages */ 17202870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 17212870c43dSOctavian Purdila 172235f3d14dSJens Axboe if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk)) 1723a108d5f3SDavid S. Miller return true; 17242870c43dSOctavian Purdila 17252870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 17262870c43dSOctavian Purdila *len -= flen; 17272870c43dSOctavian Purdila 17282870c43dSOctavian Purdila } while (*len && plen); 17292870c43dSOctavian Purdila 1730a108d5f3SDavid S. Miller return false; 1731db43a282SOctavian Purdila } 17329c55e01cSJens Axboe 17339c55e01cSJens Axboe /* 1734a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 17352870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 17369c55e01cSJens Axboe */ 1737a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 173835f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 173935f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 17402870c43dSOctavian Purdila { 17412870c43dSOctavian Purdila int seg; 17429c55e01cSJens Axboe 17431d0c0b32SEric Dumazet /* map the linear part : 17442996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 17452996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 17462996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 17479c55e01cSJens Axboe */ 17482870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 17492870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 17502870c43dSOctavian Purdila skb_headlen(skb), 17511d0c0b32SEric Dumazet offset, len, skb, spd, 17523a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 17531d0c0b32SEric Dumazet sk, pipe)) 1754a108d5f3SDavid S. Miller return true; 17559c55e01cSJens Axboe 17569c55e01cSJens Axboe /* 17579c55e01cSJens Axboe * then map the fragments 17589c55e01cSJens Axboe */ 17599c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 17609c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 17619c55e01cSJens Axboe 1762ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 17639e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 1764d7ccf7c0SEric Dumazet offset, len, skb, spd, false, sk, pipe)) 1765a108d5f3SDavid S. Miller return true; 17669c55e01cSJens Axboe } 17679c55e01cSJens Axboe 1768a108d5f3SDavid S. Miller return false; 17699c55e01cSJens Axboe } 17709c55e01cSJens Axboe 17719c55e01cSJens Axboe /* 17729c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 17739c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 17749c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 17759c55e01cSJens Axboe * handle that cleanly. 17769c55e01cSJens Axboe */ 17778b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 17789c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 17799c55e01cSJens Axboe unsigned int flags) 17809c55e01cSJens Axboe { 178141c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 178241c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 17839c55e01cSJens Axboe struct splice_pipe_desc spd = { 17849c55e01cSJens Axboe .pages = pages, 17859c55e01cSJens Axboe .partial = partial, 1786047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 17879c55e01cSJens Axboe .flags = flags, 17889c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 17899c55e01cSJens Axboe .spd_release = sock_spd_release, 17909c55e01cSJens Axboe }; 1791fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 17927a67e56fSJarek Poplawski struct sock *sk = skb->sk; 179335f3d14dSJens Axboe int ret = 0; 179435f3d14dSJens Axboe 17959c55e01cSJens Axboe /* 17969c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 17979c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 17989c55e01cSJens Axboe */ 179935f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 18009c55e01cSJens Axboe goto done; 18019c55e01cSJens Axboe else if (!tlen) 18029c55e01cSJens Axboe goto done; 18039c55e01cSJens Axboe 18049c55e01cSJens Axboe /* 18059c55e01cSJens Axboe * now see if we have a frag_list to map 18069c55e01cSJens Axboe */ 1807fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1808fbb398a8SDavid S. Miller if (!tlen) 18099c55e01cSJens Axboe break; 181035f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1811fbb398a8SDavid S. Miller break; 18129c55e01cSJens Axboe } 18139c55e01cSJens Axboe 18149c55e01cSJens Axboe done: 18159c55e01cSJens Axboe if (spd.nr_pages) { 18169c55e01cSJens Axboe /* 18179c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 18189c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 18199c55e01cSJens Axboe * here as compared to sendfile(). We enter here 18209c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 18219c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 18229c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 18239c55e01cSJens Axboe * and networking will grab the socket lock. 18249c55e01cSJens Axboe */ 1825293ad604SOctavian Purdila release_sock(sk); 18269c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1827293ad604SOctavian Purdila lock_sock(sk); 18289c55e01cSJens Axboe } 18299c55e01cSJens Axboe 183035f3d14dSJens Axboe return ret; 18319c55e01cSJens Axboe } 18329c55e01cSJens Axboe 1833357b40a1SHerbert Xu /** 1834357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1835357b40a1SHerbert Xu * @skb: destination buffer 1836357b40a1SHerbert Xu * @offset: offset in destination 1837357b40a1SHerbert Xu * @from: source buffer 1838357b40a1SHerbert Xu * @len: number of bytes to copy 1839357b40a1SHerbert Xu * 1840357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1841357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1842357b40a1SHerbert Xu * traversing fragment lists and such. 1843357b40a1SHerbert Xu */ 1844357b40a1SHerbert Xu 18450c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1846357b40a1SHerbert Xu { 18471a028e50SDavid S. Miller int start = skb_headlen(skb); 1848fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1849fbb398a8SDavid S. Miller int i, copy; 1850357b40a1SHerbert Xu 1851357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1852357b40a1SHerbert Xu goto fault; 1853357b40a1SHerbert Xu 18541a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1855357b40a1SHerbert Xu if (copy > len) 1856357b40a1SHerbert Xu copy = len; 185727d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1858357b40a1SHerbert Xu if ((len -= copy) == 0) 1859357b40a1SHerbert Xu return 0; 1860357b40a1SHerbert Xu offset += copy; 1861357b40a1SHerbert Xu from += copy; 1862357b40a1SHerbert Xu } 1863357b40a1SHerbert Xu 1864357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1865357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 18661a028e50SDavid S. Miller int end; 1867357b40a1SHerbert Xu 1868547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18691a028e50SDavid S. Miller 18709e903e08SEric Dumazet end = start + skb_frag_size(frag); 1871357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1872357b40a1SHerbert Xu u8 *vaddr; 1873357b40a1SHerbert Xu 1874357b40a1SHerbert Xu if (copy > len) 1875357b40a1SHerbert Xu copy = len; 1876357b40a1SHerbert Xu 187751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 18781a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 18791a028e50SDavid S. Miller from, copy); 188051c56b00SEric Dumazet kunmap_atomic(vaddr); 1881357b40a1SHerbert Xu 1882357b40a1SHerbert Xu if ((len -= copy) == 0) 1883357b40a1SHerbert Xu return 0; 1884357b40a1SHerbert Xu offset += copy; 1885357b40a1SHerbert Xu from += copy; 1886357b40a1SHerbert Xu } 18871a028e50SDavid S. Miller start = end; 1888357b40a1SHerbert Xu } 1889357b40a1SHerbert Xu 1890fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 18911a028e50SDavid S. Miller int end; 1892357b40a1SHerbert Xu 1893547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18941a028e50SDavid S. Miller 1895fbb398a8SDavid S. Miller end = start + frag_iter->len; 1896357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1897357b40a1SHerbert Xu if (copy > len) 1898357b40a1SHerbert Xu copy = len; 1899fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 19001a028e50SDavid S. Miller from, copy)) 1901357b40a1SHerbert Xu goto fault; 1902357b40a1SHerbert Xu if ((len -= copy) == 0) 1903357b40a1SHerbert Xu return 0; 1904357b40a1SHerbert Xu offset += copy; 1905357b40a1SHerbert Xu from += copy; 1906357b40a1SHerbert Xu } 19071a028e50SDavid S. Miller start = end; 1908357b40a1SHerbert Xu } 1909357b40a1SHerbert Xu if (!len) 1910357b40a1SHerbert Xu return 0; 1911357b40a1SHerbert Xu 1912357b40a1SHerbert Xu fault: 1913357b40a1SHerbert Xu return -EFAULT; 1914357b40a1SHerbert Xu } 1915357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1916357b40a1SHerbert Xu 19171da177e4SLinus Torvalds /* Checksum skb data. */ 19181da177e4SLinus Torvalds 19192bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 19202bbbc868SAl Viro int len, __wsum csum) 19211da177e4SLinus Torvalds { 19221a028e50SDavid S. Miller int start = skb_headlen(skb); 19231a028e50SDavid S. Miller int i, copy = start - offset; 1924fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19251da177e4SLinus Torvalds int pos = 0; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds /* Checksum header. */ 19281da177e4SLinus Torvalds if (copy > 0) { 19291da177e4SLinus Torvalds if (copy > len) 19301da177e4SLinus Torvalds copy = len; 19311da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 19321da177e4SLinus Torvalds if ((len -= copy) == 0) 19331da177e4SLinus Torvalds return csum; 19341da177e4SLinus Torvalds offset += copy; 19351da177e4SLinus Torvalds pos = copy; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19391a028e50SDavid S. Miller int end; 194051c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19411da177e4SLinus Torvalds 1942547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19431a028e50SDavid S. Miller 194451c56b00SEric Dumazet end = start + skb_frag_size(frag); 19451da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 194644bb9363SAl Viro __wsum csum2; 19471da177e4SLinus Torvalds u8 *vaddr; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds if (copy > len) 19501da177e4SLinus Torvalds copy = len; 195151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19521a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 19531a028e50SDavid S. Miller offset - start, copy, 0); 195451c56b00SEric Dumazet kunmap_atomic(vaddr); 19551da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19561da177e4SLinus Torvalds if (!(len -= copy)) 19571da177e4SLinus Torvalds return csum; 19581da177e4SLinus Torvalds offset += copy; 19591da177e4SLinus Torvalds pos += copy; 19601da177e4SLinus Torvalds } 19611a028e50SDavid S. Miller start = end; 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 1964fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19651a028e50SDavid S. Miller int end; 19661da177e4SLinus Torvalds 1967547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19681a028e50SDavid S. Miller 1969fbb398a8SDavid S. Miller end = start + frag_iter->len; 19701da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19715f92a738SAl Viro __wsum csum2; 19721da177e4SLinus Torvalds if (copy > len) 19731da177e4SLinus Torvalds copy = len; 1974fbb398a8SDavid S. Miller csum2 = skb_checksum(frag_iter, offset - start, 19751a028e50SDavid S. Miller copy, 0); 19761da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19771da177e4SLinus Torvalds if ((len -= copy) == 0) 19781da177e4SLinus Torvalds return csum; 19791da177e4SLinus Torvalds offset += copy; 19801da177e4SLinus Torvalds pos += copy; 19811da177e4SLinus Torvalds } 19821a028e50SDavid S. Miller start = end; 19831da177e4SLinus Torvalds } 198409a62660SKris Katterjohn BUG_ON(len); 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds return csum; 19871da177e4SLinus Torvalds } 1988b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /* Both of above in one bottle. */ 19911da177e4SLinus Torvalds 199281d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 199381d77662SAl Viro u8 *to, int len, __wsum csum) 19941da177e4SLinus Torvalds { 19951a028e50SDavid S. Miller int start = skb_headlen(skb); 19961a028e50SDavid S. Miller int i, copy = start - offset; 1997fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19981da177e4SLinus Torvalds int pos = 0; 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds /* Copy header. */ 20011da177e4SLinus Torvalds if (copy > 0) { 20021da177e4SLinus Torvalds if (copy > len) 20031da177e4SLinus Torvalds copy = len; 20041da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 20051da177e4SLinus Torvalds copy, csum); 20061da177e4SLinus Torvalds if ((len -= copy) == 0) 20071da177e4SLinus Torvalds return csum; 20081da177e4SLinus Torvalds offset += copy; 20091da177e4SLinus Torvalds to += copy; 20101da177e4SLinus Torvalds pos = copy; 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20141a028e50SDavid S. Miller int end; 20151da177e4SLinus Torvalds 2016547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20171a028e50SDavid S. Miller 20189e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 20191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20205084205fSAl Viro __wsum csum2; 20211da177e4SLinus Torvalds u8 *vaddr; 20221da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds if (copy > len) 20251da177e4SLinus Torvalds copy = len; 202651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20271da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 20281a028e50SDavid S. Miller frag->page_offset + 20291a028e50SDavid S. Miller offset - start, to, 20301a028e50SDavid S. Miller copy, 0); 203151c56b00SEric Dumazet kunmap_atomic(vaddr); 20321da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20331da177e4SLinus Torvalds if (!(len -= copy)) 20341da177e4SLinus Torvalds return csum; 20351da177e4SLinus Torvalds offset += copy; 20361da177e4SLinus Torvalds to += copy; 20371da177e4SLinus Torvalds pos += copy; 20381da177e4SLinus Torvalds } 20391a028e50SDavid S. Miller start = end; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 2042fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 204381d77662SAl Viro __wsum csum2; 20441a028e50SDavid S. Miller int end; 20451da177e4SLinus Torvalds 2046547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20471a028e50SDavid S. Miller 2048fbb398a8SDavid S. Miller end = start + frag_iter->len; 20491da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20501da177e4SLinus Torvalds if (copy > len) 20511da177e4SLinus Torvalds copy = len; 2052fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 20531a028e50SDavid S. Miller offset - start, 20541da177e4SLinus Torvalds to, copy, 0); 20551da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20561da177e4SLinus Torvalds if ((len -= copy) == 0) 20571da177e4SLinus Torvalds return csum; 20581da177e4SLinus Torvalds offset += copy; 20591da177e4SLinus Torvalds to += copy; 20601da177e4SLinus Torvalds pos += copy; 20611da177e4SLinus Torvalds } 20621a028e50SDavid S. Miller start = end; 20631da177e4SLinus Torvalds } 206409a62660SKris Katterjohn BUG_ON(len); 20651da177e4SLinus Torvalds return csum; 20661da177e4SLinus Torvalds } 2067b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 20701da177e4SLinus Torvalds { 2071d3bc23e7SAl Viro __wsum csum; 20721da177e4SLinus Torvalds long csstart; 20731da177e4SLinus Torvalds 207484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 207555508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 20761da177e4SLinus Torvalds else 20771da177e4SLinus Torvalds csstart = skb_headlen(skb); 20781da177e4SLinus Torvalds 207909a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 20801da177e4SLinus Torvalds 2081d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds csum = 0; 20841da177e4SLinus Torvalds if (csstart != skb->len) 20851da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 20861da177e4SLinus Torvalds skb->len - csstart, 0); 20871da177e4SLinus Torvalds 208884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2089ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 20901da177e4SLinus Torvalds 2091d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 20921da177e4SLinus Torvalds } 20931da177e4SLinus Torvalds } 2094b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /** 20971da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 20981da177e4SLinus Torvalds * @list: list to dequeue from 20991da177e4SLinus Torvalds * 21001da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 21011da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 21021da177e4SLinus Torvalds * returned or %NULL if the list is empty. 21031da177e4SLinus Torvalds */ 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 21061da177e4SLinus Torvalds { 21071da177e4SLinus Torvalds unsigned long flags; 21081da177e4SLinus Torvalds struct sk_buff *result; 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21111da177e4SLinus Torvalds result = __skb_dequeue(list); 21121da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21131da177e4SLinus Torvalds return result; 21141da177e4SLinus Torvalds } 2115b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds /** 21181da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 21191da177e4SLinus Torvalds * @list: list to dequeue from 21201da177e4SLinus Torvalds * 21211da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 21221da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 21231da177e4SLinus Torvalds * returned or %NULL if the list is empty. 21241da177e4SLinus Torvalds */ 21251da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 21261da177e4SLinus Torvalds { 21271da177e4SLinus Torvalds unsigned long flags; 21281da177e4SLinus Torvalds struct sk_buff *result; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21311da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 21321da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21331da177e4SLinus Torvalds return result; 21341da177e4SLinus Torvalds } 2135b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds /** 21381da177e4SLinus Torvalds * skb_queue_purge - empty a list 21391da177e4SLinus Torvalds * @list: list to empty 21401da177e4SLinus Torvalds * 21411da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 21421da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 21431da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 21441da177e4SLinus Torvalds */ 21451da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 21461da177e4SLinus Torvalds { 21471da177e4SLinus Torvalds struct sk_buff *skb; 21481da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 21491da177e4SLinus Torvalds kfree_skb(skb); 21501da177e4SLinus Torvalds } 2151b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds /** 21541da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 21551da177e4SLinus Torvalds * @list: list to use 21561da177e4SLinus Torvalds * @newsk: buffer to queue 21571da177e4SLinus Torvalds * 21581da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 21591da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21601da177e4SLinus Torvalds * safely. 21611da177e4SLinus Torvalds * 21621da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21631da177e4SLinus Torvalds */ 21641da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds unsigned long flags; 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21691da177e4SLinus Torvalds __skb_queue_head(list, newsk); 21701da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21711da177e4SLinus Torvalds } 2172b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds /** 21751da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 21761da177e4SLinus Torvalds * @list: list to use 21771da177e4SLinus Torvalds * @newsk: buffer to queue 21781da177e4SLinus Torvalds * 21791da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 21801da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21811da177e4SLinus Torvalds * safely. 21821da177e4SLinus Torvalds * 21831da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21841da177e4SLinus Torvalds */ 21851da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 21861da177e4SLinus Torvalds { 21871da177e4SLinus Torvalds unsigned long flags; 21881da177e4SLinus Torvalds 21891da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21901da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 21911da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21921da177e4SLinus Torvalds } 2193b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 21948728b834SDavid S. Miller 21951da177e4SLinus Torvalds /** 21961da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 21971da177e4SLinus Torvalds * @skb: buffer to remove 21988728b834SDavid S. Miller * @list: list to use 21991da177e4SLinus Torvalds * 22008728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 22018728b834SDavid S. Miller * function is atomic with respect to other list locked calls 22021da177e4SLinus Torvalds * 22038728b834SDavid S. Miller * You must know what list the SKB is on. 22041da177e4SLinus Torvalds */ 22058728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 22061da177e4SLinus Torvalds { 22071da177e4SLinus Torvalds unsigned long flags; 22081da177e4SLinus Torvalds 22091da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 22108728b834SDavid S. Miller __skb_unlink(skb, list); 22111da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 22121da177e4SLinus Torvalds } 2213b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds /** 22161da177e4SLinus Torvalds * skb_append - append a buffer 22171da177e4SLinus Torvalds * @old: buffer to insert after 22181da177e4SLinus Torvalds * @newsk: buffer to insert 22198728b834SDavid S. Miller * @list: list to use 22201da177e4SLinus Torvalds * 22211da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 22221da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 22231da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 22241da177e4SLinus Torvalds */ 22258728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 22261da177e4SLinus Torvalds { 22271da177e4SLinus Torvalds unsigned long flags; 22281da177e4SLinus Torvalds 22298728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 22307de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 22318728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 22321da177e4SLinus Torvalds } 2233b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds /** 22361da177e4SLinus Torvalds * skb_insert - insert a buffer 22371da177e4SLinus Torvalds * @old: buffer to insert before 22381da177e4SLinus Torvalds * @newsk: buffer to insert 22398728b834SDavid S. Miller * @list: list to use 22401da177e4SLinus Torvalds * 22418728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 22428728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 22438728b834SDavid S. Miller * calls. 22448728b834SDavid S. Miller * 22451da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 22461da177e4SLinus Torvalds */ 22478728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 22481da177e4SLinus Torvalds { 22491da177e4SLinus Torvalds unsigned long flags; 22501da177e4SLinus Torvalds 22518728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 22528728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 22538728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 22541da177e4SLinus Torvalds } 2255b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 22581da177e4SLinus Torvalds struct sk_buff* skb1, 22591da177e4SLinus Torvalds const u32 len, const int pos) 22601da177e4SLinus Torvalds { 22611da177e4SLinus Torvalds int i; 22621da177e4SLinus Torvalds 2263d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2264d626f62bSArnaldo Carvalho de Melo pos - len); 22651da177e4SLinus Torvalds /* And move data appendix as is. */ 22661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 22671da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 22701da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22711da177e4SLinus Torvalds skb1->data_len = skb->data_len; 22721da177e4SLinus Torvalds skb1->len += skb1->data_len; 22731da177e4SLinus Torvalds skb->data_len = 0; 22741da177e4SLinus Torvalds skb->len = len; 227527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 22791da177e4SLinus Torvalds struct sk_buff* skb1, 22801da177e4SLinus Torvalds const u32 len, int pos) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds int i, k = 0; 22831da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22861da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 22871da177e4SLinus Torvalds skb->len = len; 22881da177e4SLinus Torvalds skb->data_len = len - pos; 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 22919e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds if (pos + size > len) { 22941da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds if (pos < len) { 22971da177e4SLinus Torvalds /* Split frag. 22981da177e4SLinus Torvalds * We have two variants in this case: 22991da177e4SLinus Torvalds * 1. Move all the frag to the second 23001da177e4SLinus Torvalds * part, if it is possible. F.e. 23011da177e4SLinus Torvalds * this approach is mandatory for TUX, 23021da177e4SLinus Torvalds * where splitting is expensive. 23031da177e4SLinus Torvalds * 2. Split is accurately. We make this. 23041da177e4SLinus Torvalds */ 2305ea2ab693SIan Campbell skb_frag_ref(skb, i); 23061da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 23079e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 23089e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 23091da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds k++; 23121da177e4SLinus Torvalds } else 23131da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 23141da177e4SLinus Torvalds pos += size; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds /** 23201da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 23211da177e4SLinus Torvalds * @skb: the buffer to split 23221da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 23231da177e4SLinus Torvalds * @len: new length for skb 23241da177e4SLinus Torvalds */ 23251da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 23261da177e4SLinus Torvalds { 23271da177e4SLinus Torvalds int pos = skb_headlen(skb); 23281da177e4SLinus Torvalds 23291da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 23301da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 23311da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 23321da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 23331da177e4SLinus Torvalds } 2334b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 23351da177e4SLinus Torvalds 23369f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 23379f782db3SIlpo Järvinen * 23389f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 23399f782db3SIlpo Järvinen */ 2340832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2341832d11c5SIlpo Järvinen { 23420ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2343832d11c5SIlpo Järvinen } 2344832d11c5SIlpo Järvinen 2345832d11c5SIlpo Järvinen /** 2346832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2347832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2348832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2349832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2350832d11c5SIlpo Järvinen * 2351832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 235220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2353832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2354832d11c5SIlpo Järvinen * 2355832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2356832d11c5SIlpo Järvinen * 2357832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2358832d11c5SIlpo Järvinen * to have non-paged data as well. 2359832d11c5SIlpo Järvinen * 2360832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2361832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2362832d11c5SIlpo Järvinen */ 2363832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2364832d11c5SIlpo Järvinen { 2365832d11c5SIlpo Järvinen int from, to, merge, todo; 2366832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2367832d11c5SIlpo Järvinen 2368832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2369832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2370832d11c5SIlpo Järvinen 2371832d11c5SIlpo Järvinen todo = shiftlen; 2372832d11c5SIlpo Järvinen from = 0; 2373832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2374832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2375832d11c5SIlpo Järvinen 2376832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2377832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2378832d11c5SIlpo Järvinen */ 2379832d11c5SIlpo Järvinen if (!to || 2380ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2381ea2ab693SIan Campbell fragfrom->page_offset)) { 2382832d11c5SIlpo Järvinen merge = -1; 2383832d11c5SIlpo Järvinen } else { 2384832d11c5SIlpo Järvinen merge = to - 1; 2385832d11c5SIlpo Järvinen 23869e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2387832d11c5SIlpo Järvinen if (todo < 0) { 2388832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2389832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2390832d11c5SIlpo Järvinen return 0; 2391832d11c5SIlpo Järvinen 23929f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 23939f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2394832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2395832d11c5SIlpo Järvinen 23969e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 23979e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2398832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2399832d11c5SIlpo Järvinen 2400832d11c5SIlpo Järvinen goto onlymerged; 2401832d11c5SIlpo Järvinen } 2402832d11c5SIlpo Järvinen 2403832d11c5SIlpo Järvinen from++; 2404832d11c5SIlpo Järvinen } 2405832d11c5SIlpo Järvinen 2406832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2407832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2408832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2409832d11c5SIlpo Järvinen return 0; 2410832d11c5SIlpo Järvinen 2411832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2412832d11c5SIlpo Järvinen return 0; 2413832d11c5SIlpo Järvinen 2414832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2415832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2416832d11c5SIlpo Järvinen return 0; 2417832d11c5SIlpo Järvinen 2418832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2419832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2420832d11c5SIlpo Järvinen 24219e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2422832d11c5SIlpo Järvinen *fragto = *fragfrom; 24239e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2424832d11c5SIlpo Järvinen from++; 2425832d11c5SIlpo Järvinen to++; 2426832d11c5SIlpo Järvinen 2427832d11c5SIlpo Järvinen } else { 2428ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2429832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2430832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 24319e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2432832d11c5SIlpo Järvinen 2433832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 24349e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2435832d11c5SIlpo Järvinen todo = 0; 2436832d11c5SIlpo Järvinen 2437832d11c5SIlpo Järvinen to++; 2438832d11c5SIlpo Järvinen break; 2439832d11c5SIlpo Järvinen } 2440832d11c5SIlpo Järvinen } 2441832d11c5SIlpo Järvinen 2442832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2443832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2444832d11c5SIlpo Järvinen 2445832d11c5SIlpo Järvinen if (merge >= 0) { 2446832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2447832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2448832d11c5SIlpo Järvinen 24499e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2450ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2451832d11c5SIlpo Järvinen } 2452832d11c5SIlpo Järvinen 2453832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2454832d11c5SIlpo Järvinen to = 0; 2455832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2456832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2457832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2458832d11c5SIlpo Järvinen 2459832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2460832d11c5SIlpo Järvinen 2461832d11c5SIlpo Järvinen onlymerged: 2462832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2463832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2464832d11c5SIlpo Järvinen */ 2465832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2466832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2467832d11c5SIlpo Järvinen 2468832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2469832d11c5SIlpo Järvinen skb->len -= shiftlen; 2470832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2471832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2472832d11c5SIlpo Järvinen tgt->len += shiftlen; 2473832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2474832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2475832d11c5SIlpo Järvinen 2476832d11c5SIlpo Järvinen return shiftlen; 2477832d11c5SIlpo Järvinen } 2478832d11c5SIlpo Järvinen 2479677e90edSThomas Graf /** 2480677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2481677e90edSThomas Graf * @skb: the buffer to read 2482677e90edSThomas Graf * @from: lower offset of data to be read 2483677e90edSThomas Graf * @to: upper offset of data to be read 2484677e90edSThomas Graf * @st: state variable 2485677e90edSThomas Graf * 2486677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2487677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2488677e90edSThomas Graf */ 2489677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2490677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2491677e90edSThomas Graf { 2492677e90edSThomas Graf st->lower_offset = from; 2493677e90edSThomas Graf st->upper_offset = to; 2494677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2495677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2496677e90edSThomas Graf st->frag_data = NULL; 2497677e90edSThomas Graf } 2498b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2499677e90edSThomas Graf 2500677e90edSThomas Graf /** 2501677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2502677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2503677e90edSThomas Graf * @data: destination pointer for data to be returned 2504677e90edSThomas Graf * @st: state variable 2505677e90edSThomas Graf * 2506677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 2507677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2508677e90edSThomas Graf * the head of the data block to &data and returns the length 2509677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2510677e90edSThomas Graf * offset has been reached. 2511677e90edSThomas Graf * 2512677e90edSThomas Graf * The caller is not required to consume all of the data 2513677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 2514677e90edSThomas Graf * of bytes already consumed and the next call to 2515677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2516677e90edSThomas Graf * 251725985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2518677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 2519677e90edSThomas Graf * reads of potentially non linear data. 2520677e90edSThomas Graf * 2521bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2522677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2523677e90edSThomas Graf * a stack for this purpose. 2524677e90edSThomas Graf */ 2525677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2526677e90edSThomas Graf struct skb_seq_state *st) 2527677e90edSThomas Graf { 2528677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2529677e90edSThomas Graf skb_frag_t *frag; 2530677e90edSThomas Graf 2531677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2532677e90edSThomas Graf return 0; 2533677e90edSThomas Graf 2534677e90edSThomas Graf next_skb: 253595e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2536677e90edSThomas Graf 2537995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 253895e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2539677e90edSThomas Graf return block_limit - abs_offset; 2540677e90edSThomas Graf } 2541677e90edSThomas Graf 2542677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2543677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2544677e90edSThomas Graf 2545677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2546677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 25479e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2548677e90edSThomas Graf 2549677e90edSThomas Graf if (abs_offset < block_limit) { 2550677e90edSThomas Graf if (!st->frag_data) 255151c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2552677e90edSThomas Graf 2553677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2554677e90edSThomas Graf (abs_offset - st->stepped_offset); 2555677e90edSThomas Graf 2556677e90edSThomas Graf return block_limit - abs_offset; 2557677e90edSThomas Graf } 2558677e90edSThomas Graf 2559677e90edSThomas Graf if (st->frag_data) { 256051c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2561677e90edSThomas Graf st->frag_data = NULL; 2562677e90edSThomas Graf } 2563677e90edSThomas Graf 2564677e90edSThomas Graf st->frag_idx++; 25659e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2566677e90edSThomas Graf } 2567677e90edSThomas Graf 25685b5a60daSOlaf Kirch if (st->frag_data) { 256951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 25705b5a60daSOlaf Kirch st->frag_data = NULL; 25715b5a60daSOlaf Kirch } 25725b5a60daSOlaf Kirch 257321dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2574677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 257595e3b24cSHerbert Xu st->frag_idx = 0; 2576677e90edSThomas Graf goto next_skb; 257771b3346dSShyam Iyer } else if (st->cur_skb->next) { 257871b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 257971b3346dSShyam Iyer st->frag_idx = 0; 2580677e90edSThomas Graf goto next_skb; 2581677e90edSThomas Graf } 2582677e90edSThomas Graf 2583677e90edSThomas Graf return 0; 2584677e90edSThomas Graf } 2585b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2586677e90edSThomas Graf 2587677e90edSThomas Graf /** 2588677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2589677e90edSThomas Graf * @st: state variable 2590677e90edSThomas Graf * 2591677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2592677e90edSThomas Graf * returned 0. 2593677e90edSThomas Graf */ 2594677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2595677e90edSThomas Graf { 2596677e90edSThomas Graf if (st->frag_data) 259751c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2598677e90edSThomas Graf } 2599b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2600677e90edSThomas Graf 26013fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 26023fc7e8a6SThomas Graf 26033fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 26043fc7e8a6SThomas Graf struct ts_config *conf, 26053fc7e8a6SThomas Graf struct ts_state *state) 26063fc7e8a6SThomas Graf { 26073fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 26083fc7e8a6SThomas Graf } 26093fc7e8a6SThomas Graf 26103fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 26113fc7e8a6SThomas Graf { 26123fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 26133fc7e8a6SThomas Graf } 26143fc7e8a6SThomas Graf 26153fc7e8a6SThomas Graf /** 26163fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 26173fc7e8a6SThomas Graf * @skb: the buffer to look in 26183fc7e8a6SThomas Graf * @from: search offset 26193fc7e8a6SThomas Graf * @to: search limit 26203fc7e8a6SThomas Graf * @config: textsearch configuration 26213fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 26223fc7e8a6SThomas Graf * 26233fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 26243fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 26253fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 26263fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 26273fc7e8a6SThomas Graf */ 26283fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 26293fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 26303fc7e8a6SThomas Graf struct ts_state *state) 26313fc7e8a6SThomas Graf { 2632f72b948dSPhil Oester unsigned int ret; 2633f72b948dSPhil Oester 26343fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 26353fc7e8a6SThomas Graf config->finish = skb_ts_finish; 26363fc7e8a6SThomas Graf 26373fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 26383fc7e8a6SThomas Graf 2639f72b948dSPhil Oester ret = textsearch_find(config, state); 2640f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 26413fc7e8a6SThomas Graf } 2642b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 26433fc7e8a6SThomas Graf 2644e89e9cf5SAnanda Raju /** 26452c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2646e89e9cf5SAnanda Raju * @sk: sock structure 2647e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2648e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2649e89e9cf5SAnanda Raju * @from: pointer to user message iov 2650e89e9cf5SAnanda Raju * @length: length of the iov message 2651e89e9cf5SAnanda Raju * 2652e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2653e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2654e89e9cf5SAnanda Raju */ 2655e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2656dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2657e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2658e89e9cf5SAnanda Raju void *from, int length) 2659e89e9cf5SAnanda Raju { 2660e89e9cf5SAnanda Raju int frg_cnt = 0; 2661e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2662e89e9cf5SAnanda Raju struct page *page = NULL; 2663e89e9cf5SAnanda Raju int copy, left; 2664e89e9cf5SAnanda Raju int offset = 0; 2665e89e9cf5SAnanda Raju int ret; 2666e89e9cf5SAnanda Raju 2667e89e9cf5SAnanda Raju do { 2668e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2669e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2670e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2671e89e9cf5SAnanda Raju return -EFAULT; 2672e89e9cf5SAnanda Raju 2673e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2674e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2675e89e9cf5SAnanda Raju 2676e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2677e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2678e89e9cf5SAnanda Raju */ 2679e89e9cf5SAnanda Raju if (page == NULL) 2680e89e9cf5SAnanda Raju return -ENOMEM; 2681e89e9cf5SAnanda Raju 2682e89e9cf5SAnanda Raju /* initialize the next frag */ 2683e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2684e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2685e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2686e89e9cf5SAnanda Raju 2687e89e9cf5SAnanda Raju /* get the new initialized frag */ 2688e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2689e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2690e89e9cf5SAnanda Raju 2691e89e9cf5SAnanda Raju /* copy the user data to page */ 2692e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2693e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2694e89e9cf5SAnanda Raju 26959e903e08SEric Dumazet ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag), 2696e89e9cf5SAnanda Raju offset, copy, 0, skb); 2697e89e9cf5SAnanda Raju if (ret < 0) 2698e89e9cf5SAnanda Raju return -EFAULT; 2699e89e9cf5SAnanda Raju 2700e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 27019e903e08SEric Dumazet skb_frag_size_add(frag, copy); 2702e89e9cf5SAnanda Raju skb->len += copy; 2703e89e9cf5SAnanda Raju skb->data_len += copy; 2704e89e9cf5SAnanda Raju offset += copy; 2705e89e9cf5SAnanda Raju length -= copy; 2706e89e9cf5SAnanda Raju 2707e89e9cf5SAnanda Raju } while (length > 0); 2708e89e9cf5SAnanda Raju 2709e89e9cf5SAnanda Raju return 0; 2710e89e9cf5SAnanda Raju } 2711b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2712e89e9cf5SAnanda Raju 2713cbb042f9SHerbert Xu /** 2714cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2715cbb042f9SHerbert Xu * @skb: buffer to update 2716cbb042f9SHerbert Xu * @len: length of data pulled 2717cbb042f9SHerbert Xu * 2718cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2719fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 272084fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 272184fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 272284fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2723cbb042f9SHerbert Xu */ 2724cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2725cbb042f9SHerbert Xu { 2726cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2727cbb042f9SHerbert Xu skb->len -= len; 2728cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2729cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2730cbb042f9SHerbert Xu return skb->data += len; 2731cbb042f9SHerbert Xu } 2732f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2733f94691acSArnaldo Carvalho de Melo 2734f4c50d99SHerbert Xu /** 2735f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2736f4c50d99SHerbert Xu * @skb: buffer to segment 2737576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2738f4c50d99SHerbert Xu * 2739f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 27404c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 27414c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2742f4c50d99SHerbert Xu */ 2743c8f44affSMichał Mirosław struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features) 2744f4c50d99SHerbert Xu { 2745f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2746f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 274789319d38SHerbert Xu struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2748f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 274998e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2750f4c50d99SHerbert Xu unsigned int offset = doffset; 2751f4c50d99SHerbert Xu unsigned int headroom; 2752f4c50d99SHerbert Xu unsigned int len; 275304ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2754f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2755f4c50d99SHerbert Xu int err = -ENOMEM; 2756f4c50d99SHerbert Xu int i = 0; 2757f4c50d99SHerbert Xu int pos; 2758f4c50d99SHerbert Xu 2759f4c50d99SHerbert Xu __skb_push(skb, doffset); 2760f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2761f4c50d99SHerbert Xu pos = skb_headlen(skb); 2762f4c50d99SHerbert Xu 2763f4c50d99SHerbert Xu do { 2764f4c50d99SHerbert Xu struct sk_buff *nskb; 2765f4c50d99SHerbert Xu skb_frag_t *frag; 2766c8884eddSHerbert Xu int hsize; 2767f4c50d99SHerbert Xu int size; 2768f4c50d99SHerbert Xu 2769f4c50d99SHerbert Xu len = skb->len - offset; 2770f4c50d99SHerbert Xu if (len > mss) 2771f4c50d99SHerbert Xu len = mss; 2772f4c50d99SHerbert Xu 2773f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2774f4c50d99SHerbert Xu if (hsize < 0) 2775f4c50d99SHerbert Xu hsize = 0; 2776c8884eddSHerbert Xu if (hsize > len || !sg) 2777c8884eddSHerbert Xu hsize = len; 2778f4c50d99SHerbert Xu 277989319d38SHerbert Xu if (!hsize && i >= nfrags) { 278089319d38SHerbert Xu BUG_ON(fskb->len != len); 278189319d38SHerbert Xu 278289319d38SHerbert Xu pos += len; 278389319d38SHerbert Xu nskb = skb_clone(fskb, GFP_ATOMIC); 278489319d38SHerbert Xu fskb = fskb->next; 278589319d38SHerbert Xu 2786f4c50d99SHerbert Xu if (unlikely(!nskb)) 2787f4c50d99SHerbert Xu goto err; 2788f4c50d99SHerbert Xu 2789ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 279089319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 279189319d38SHerbert Xu kfree_skb(nskb); 279289319d38SHerbert Xu goto err; 279389319d38SHerbert Xu } 279489319d38SHerbert Xu 2795ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 279689319d38SHerbert Xu skb_release_head_state(nskb); 279789319d38SHerbert Xu __skb_push(nskb, doffset); 279889319d38SHerbert Xu } else { 2799c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 2800c93bdd0eSMel Gorman GFP_ATOMIC, skb_alloc_rx_flag(skb), 2801c93bdd0eSMel Gorman NUMA_NO_NODE); 280289319d38SHerbert Xu 280389319d38SHerbert Xu if (unlikely(!nskb)) 280489319d38SHerbert Xu goto err; 280589319d38SHerbert Xu 280689319d38SHerbert Xu skb_reserve(nskb, headroom); 280789319d38SHerbert Xu __skb_put(nskb, doffset); 280889319d38SHerbert Xu } 280989319d38SHerbert Xu 2810f4c50d99SHerbert Xu if (segs) 2811f4c50d99SHerbert Xu tail->next = nskb; 2812f4c50d99SHerbert Xu else 2813f4c50d99SHerbert Xu segs = nskb; 2814f4c50d99SHerbert Xu tail = nskb; 2815f4c50d99SHerbert Xu 28166f85a124SHerbert Xu __copy_skb_header(nskb, skb); 2817f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2818f4c50d99SHerbert Xu 28193d3be433SEric Dumazet /* nskb and skb might have different headroom */ 28203d3be433SEric Dumazet if (nskb->ip_summed == CHECKSUM_PARTIAL) 28213d3be433SEric Dumazet nskb->csum_start += skb_headroom(nskb) - headroom; 28223d3be433SEric Dumazet 2823459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2824ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2825b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2826b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 282789319d38SHerbert Xu skb_copy_from_linear_data(skb, nskb->data, doffset); 282889319d38SHerbert Xu 28292f181855SHerbert Xu if (fskb != skb_shinfo(skb)->frag_list) 283089319d38SHerbert Xu continue; 283189319d38SHerbert Xu 2832f4c50d99SHerbert Xu if (!sg) { 28336f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2834f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2835f4c50d99SHerbert Xu skb_put(nskb, len), 2836f4c50d99SHerbert Xu len, 0); 2837f4c50d99SHerbert Xu continue; 2838f4c50d99SHerbert Xu } 2839f4c50d99SHerbert Xu 2840f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2841f4c50d99SHerbert Xu 2842d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2843d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2844f4c50d99SHerbert Xu 284589319d38SHerbert Xu while (pos < offset + len && i < nfrags) { 2846f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2847ea2ab693SIan Campbell __skb_frag_ref(frag); 28489e903e08SEric Dumazet size = skb_frag_size(frag); 2849f4c50d99SHerbert Xu 2850f4c50d99SHerbert Xu if (pos < offset) { 2851f4c50d99SHerbert Xu frag->page_offset += offset - pos; 28529e903e08SEric Dumazet skb_frag_size_sub(frag, offset - pos); 2853f4c50d99SHerbert Xu } 2854f4c50d99SHerbert Xu 285589319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 2856f4c50d99SHerbert Xu 2857f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2858f4c50d99SHerbert Xu i++; 2859f4c50d99SHerbert Xu pos += size; 2860f4c50d99SHerbert Xu } else { 28619e903e08SEric Dumazet skb_frag_size_sub(frag, pos + size - (offset + len)); 286289319d38SHerbert Xu goto skip_fraglist; 2863f4c50d99SHerbert Xu } 2864f4c50d99SHerbert Xu 2865f4c50d99SHerbert Xu frag++; 2866f4c50d99SHerbert Xu } 2867f4c50d99SHerbert Xu 286889319d38SHerbert Xu if (pos < offset + len) { 286989319d38SHerbert Xu struct sk_buff *fskb2 = fskb; 287089319d38SHerbert Xu 287189319d38SHerbert Xu BUG_ON(pos + fskb->len != offset + len); 287289319d38SHerbert Xu 287389319d38SHerbert Xu pos += fskb->len; 287489319d38SHerbert Xu fskb = fskb->next; 287589319d38SHerbert Xu 287689319d38SHerbert Xu if (fskb2->next) { 287789319d38SHerbert Xu fskb2 = skb_clone(fskb2, GFP_ATOMIC); 287889319d38SHerbert Xu if (!fskb2) 287989319d38SHerbert Xu goto err; 288089319d38SHerbert Xu } else 288189319d38SHerbert Xu skb_get(fskb2); 288289319d38SHerbert Xu 2883fbb398a8SDavid S. Miller SKB_FRAG_ASSERT(nskb); 288489319d38SHerbert Xu skb_shinfo(nskb)->frag_list = fskb2; 288589319d38SHerbert Xu } 288689319d38SHerbert Xu 288789319d38SHerbert Xu skip_fraglist: 2888f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2889f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2890f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2891f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2892f4c50d99SHerbert Xu 2893f4c50d99SHerbert Xu return segs; 2894f4c50d99SHerbert Xu 2895f4c50d99SHerbert Xu err: 2896f4c50d99SHerbert Xu while ((skb = segs)) { 2897f4c50d99SHerbert Xu segs = skb->next; 2898b08d5840SPatrick McHardy kfree_skb(skb); 2899f4c50d99SHerbert Xu } 2900f4c50d99SHerbert Xu return ERR_PTR(err); 2901f4c50d99SHerbert Xu } 2902f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2903f4c50d99SHerbert Xu 290471d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 290571d93b39SHerbert Xu { 290671d93b39SHerbert Xu struct sk_buff *p = *head; 290771d93b39SHerbert Xu struct sk_buff *nskb; 29089aaa156cSHerbert Xu struct skb_shared_info *skbinfo = skb_shinfo(skb); 29099aaa156cSHerbert Xu struct skb_shared_info *pinfo = skb_shinfo(p); 291071d93b39SHerbert Xu unsigned int headroom; 291186911732SHerbert Xu unsigned int len = skb_gro_len(skb); 291267147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 291367147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 2914715dc1f3SEric Dumazet unsigned int delta_truesize; 291571d93b39SHerbert Xu 291686911732SHerbert Xu if (p->len + len >= 65536) 291771d93b39SHerbert Xu return -E2BIG; 291871d93b39SHerbert Xu 29199aaa156cSHerbert Xu if (pinfo->frag_list) 292071d93b39SHerbert Xu goto merge; 292167147ba9SHerbert Xu else if (headlen <= offset) { 292242da6994SHerbert Xu skb_frag_t *frag; 292366e92fcfSHerbert Xu skb_frag_t *frag2; 29249aaa156cSHerbert Xu int i = skbinfo->nr_frags; 29259aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 292642da6994SHerbert Xu 292766e92fcfSHerbert Xu offset -= headlen; 292866e92fcfSHerbert Xu 292966e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 293081705ad1SHerbert Xu return -E2BIG; 293181705ad1SHerbert Xu 29329aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 29339aaa156cSHerbert Xu skbinfo->nr_frags = 0; 2934f5572068SHerbert Xu 29359aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 29369aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 293766e92fcfSHerbert Xu do { 293866e92fcfSHerbert Xu *--frag = *--frag2; 293966e92fcfSHerbert Xu } while (--i); 294066e92fcfSHerbert Xu 294166e92fcfSHerbert Xu frag->page_offset += offset; 29429e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 294366e92fcfSHerbert Xu 2944715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 2945ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 2946ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 2947715dc1f3SEric Dumazet 2948f5572068SHerbert Xu skb->truesize -= skb->data_len; 2949f5572068SHerbert Xu skb->len -= skb->data_len; 2950f5572068SHerbert Xu skb->data_len = 0; 2951f5572068SHerbert Xu 2952715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 29535d38a079SHerbert Xu goto done; 2954d7e8883cSEric Dumazet } else if (skb->head_frag) { 2955d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 2956d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 2957d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 2958d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 2959d7e8883cSEric Dumazet unsigned int first_offset; 2960d7e8883cSEric Dumazet 2961d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 2962d7e8883cSEric Dumazet return -E2BIG; 2963d7e8883cSEric Dumazet 2964d7e8883cSEric Dumazet first_offset = skb->data - 2965d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 2966d7e8883cSEric Dumazet offset; 2967d7e8883cSEric Dumazet 2968d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 2969d7e8883cSEric Dumazet 2970d7e8883cSEric Dumazet frag->page.p = page; 2971d7e8883cSEric Dumazet frag->page_offset = first_offset; 2972d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 2973d7e8883cSEric Dumazet 2974d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 2975d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 2976d7e8883cSEric Dumazet 2977715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 2978d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 2979d7e8883cSEric Dumazet goto done; 298069c0cab1SHerbert Xu } else if (skb_gro_len(p) != pinfo->gso_size) 298169c0cab1SHerbert Xu return -E2BIG; 298271d93b39SHerbert Xu 298371d93b39SHerbert Xu headroom = skb_headroom(p); 29843d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 298571d93b39SHerbert Xu if (unlikely(!nskb)) 298671d93b39SHerbert Xu return -ENOMEM; 298771d93b39SHerbert Xu 298871d93b39SHerbert Xu __copy_skb_header(nskb, p); 298971d93b39SHerbert Xu nskb->mac_len = p->mac_len; 299071d93b39SHerbert Xu 299171d93b39SHerbert Xu skb_reserve(nskb, headroom); 299286911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 299371d93b39SHerbert Xu 299486911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 299571d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 299671d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 299771d93b39SHerbert Xu 299886911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 299986911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 300086911732SHerbert Xu p->data - skb_mac_header(p)); 300171d93b39SHerbert Xu 300271d93b39SHerbert Xu *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p); 300371d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 30049aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3005622e0ca1SHerbert Xu pinfo->gso_size = 0; 300671d93b39SHerbert Xu skb_header_release(p); 3007c3c7c254SEric Dumazet NAPI_GRO_CB(nskb)->last = p; 300871d93b39SHerbert Xu 300971d93b39SHerbert Xu nskb->data_len += p->len; 3010de8261c2SEric Dumazet nskb->truesize += p->truesize; 301171d93b39SHerbert Xu nskb->len += p->len; 301271d93b39SHerbert Xu 301371d93b39SHerbert Xu *head = nskb; 301471d93b39SHerbert Xu nskb->next = p->next; 301571d93b39SHerbert Xu p->next = NULL; 301671d93b39SHerbert Xu 301771d93b39SHerbert Xu p = nskb; 301871d93b39SHerbert Xu 301971d93b39SHerbert Xu merge: 3020715dc1f3SEric Dumazet delta_truesize = skb->truesize; 302167147ba9SHerbert Xu if (offset > headlen) { 3022d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3023d1dc7abfSMichal Schmidt 3024d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 30259e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3026d1dc7abfSMichal Schmidt skb->data_len -= eat; 3027d1dc7abfSMichal Schmidt skb->len -= eat; 302867147ba9SHerbert Xu offset = headlen; 302956035022SHerbert Xu } 303056035022SHerbert Xu 303167147ba9SHerbert Xu __skb_pull(skb, offset); 303256035022SHerbert Xu 3033c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3034c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 303571d93b39SHerbert Xu skb_header_release(skb); 303671d93b39SHerbert Xu 30375d38a079SHerbert Xu done: 30385d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 303937fe4732SHerbert Xu p->data_len += len; 3040715dc1f3SEric Dumazet p->truesize += delta_truesize; 304137fe4732SHerbert Xu p->len += len; 304271d93b39SHerbert Xu 304371d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 304471d93b39SHerbert Xu return 0; 304571d93b39SHerbert Xu } 304671d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive); 304771d93b39SHerbert Xu 30481da177e4SLinus Torvalds void __init skb_init(void) 30491da177e4SLinus Torvalds { 30501da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 30511da177e4SLinus Torvalds sizeof(struct sk_buff), 30521da177e4SLinus Torvalds 0, 3053e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 305420c2df83SPaul Mundt NULL); 3055d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3056d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 3057d179cd12SDavid S. Miller sizeof(atomic_t), 3058d179cd12SDavid S. Miller 0, 3059e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 306020c2df83SPaul Mundt NULL); 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 3063716ea3a7SDavid Howells /** 3064716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3065716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3066716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3067716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3068716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3069716ea3a7SDavid Howells * 3070716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3071716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3072716ea3a7SDavid Howells */ 307351c739d1SDavid S. Miller static int 307451c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3075716ea3a7SDavid Howells { 30761a028e50SDavid S. Miller int start = skb_headlen(skb); 30771a028e50SDavid S. Miller int i, copy = start - offset; 3078fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3079716ea3a7SDavid Howells int elt = 0; 3080716ea3a7SDavid Howells 3081716ea3a7SDavid Howells if (copy > 0) { 3082716ea3a7SDavid Howells if (copy > len) 3083716ea3a7SDavid Howells copy = len; 3084642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3085716ea3a7SDavid Howells elt++; 3086716ea3a7SDavid Howells if ((len -= copy) == 0) 3087716ea3a7SDavid Howells return elt; 3088716ea3a7SDavid Howells offset += copy; 3089716ea3a7SDavid Howells } 3090716ea3a7SDavid Howells 3091716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 30921a028e50SDavid S. Miller int end; 3093716ea3a7SDavid Howells 3094547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30951a028e50SDavid S. Miller 30969e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3097716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3098716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3099716ea3a7SDavid Howells 3100716ea3a7SDavid Howells if (copy > len) 3101716ea3a7SDavid Howells copy = len; 3102ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3103642f1490SJens Axboe frag->page_offset+offset-start); 3104716ea3a7SDavid Howells elt++; 3105716ea3a7SDavid Howells if (!(len -= copy)) 3106716ea3a7SDavid Howells return elt; 3107716ea3a7SDavid Howells offset += copy; 3108716ea3a7SDavid Howells } 31091a028e50SDavid S. Miller start = end; 3110716ea3a7SDavid Howells } 3111716ea3a7SDavid Howells 3112fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 31131a028e50SDavid S. Miller int end; 3114716ea3a7SDavid Howells 3115547b792cSIlpo Järvinen WARN_ON(start > offset + len); 31161a028e50SDavid S. Miller 3117fbb398a8SDavid S. Miller end = start + frag_iter->len; 3118716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3119716ea3a7SDavid Howells if (copy > len) 3120716ea3a7SDavid Howells copy = len; 3121fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 312251c739d1SDavid S. Miller copy); 3123716ea3a7SDavid Howells if ((len -= copy) == 0) 3124716ea3a7SDavid Howells return elt; 3125716ea3a7SDavid Howells offset += copy; 3126716ea3a7SDavid Howells } 31271a028e50SDavid S. Miller start = end; 3128716ea3a7SDavid Howells } 3129716ea3a7SDavid Howells BUG_ON(len); 3130716ea3a7SDavid Howells return elt; 3131716ea3a7SDavid Howells } 3132716ea3a7SDavid Howells 313351c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 313451c739d1SDavid S. Miller { 313551c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 313651c739d1SDavid S. Miller 3137c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 313851c739d1SDavid S. Miller 313951c739d1SDavid S. Miller return nsg; 314051c739d1SDavid S. Miller } 3141b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 314251c739d1SDavid S. Miller 3143716ea3a7SDavid Howells /** 3144716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3145716ea3a7SDavid Howells * @skb: The socket buffer to check. 3146716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3147716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3148716ea3a7SDavid Howells * 3149716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3150716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3151716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3152716ea3a7SDavid Howells * 3153716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3154716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3155716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3156716ea3a7SDavid Howells * 3157716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3158716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3159716ea3a7SDavid Howells */ 3160716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3161716ea3a7SDavid Howells { 3162716ea3a7SDavid Howells int copyflag; 3163716ea3a7SDavid Howells int elt; 3164716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3165716ea3a7SDavid Howells 3166716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3167716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3168716ea3a7SDavid Howells * at the moment even if they are anonymous). 3169716ea3a7SDavid Howells */ 3170716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3171716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3172716ea3a7SDavid Howells return -ENOMEM; 3173716ea3a7SDavid Howells 3174716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 317521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3176716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3177716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3178716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3179716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3180716ea3a7SDavid Howells 3181716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3182716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3183716ea3a7SDavid Howells return -ENOMEM; 3184716ea3a7SDavid Howells 3185716ea3a7SDavid Howells /* Voila! */ 3186716ea3a7SDavid Howells *trailer = skb; 3187716ea3a7SDavid Howells return 1; 3188716ea3a7SDavid Howells } 3189716ea3a7SDavid Howells 3190716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3191716ea3a7SDavid Howells 3192716ea3a7SDavid Howells elt = 1; 3193716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3194716ea3a7SDavid Howells copyflag = 0; 3195716ea3a7SDavid Howells 3196716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3197716ea3a7SDavid Howells int ntail = 0; 3198716ea3a7SDavid Howells 3199716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3200716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3201716ea3a7SDavid Howells * after it. */ 3202716ea3a7SDavid Howells 3203716ea3a7SDavid Howells if (skb_shared(skb1)) 3204716ea3a7SDavid Howells copyflag = 1; 3205716ea3a7SDavid Howells 3206716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3207716ea3a7SDavid Howells 3208716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3209716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 321021dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3211716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3212716ea3a7SDavid Howells ntail = tailbits + 128; 3213716ea3a7SDavid Howells } 3214716ea3a7SDavid Howells 3215716ea3a7SDavid Howells if (copyflag || 3216716ea3a7SDavid Howells skb_cloned(skb1) || 3217716ea3a7SDavid Howells ntail || 3218716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 321921dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3220716ea3a7SDavid Howells struct sk_buff *skb2; 3221716ea3a7SDavid Howells 3222716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3223716ea3a7SDavid Howells if (ntail == 0) 3224716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3225716ea3a7SDavid Howells else 3226716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3227716ea3a7SDavid Howells skb_headroom(skb1), 3228716ea3a7SDavid Howells ntail, 3229716ea3a7SDavid Howells GFP_ATOMIC); 3230716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3231716ea3a7SDavid Howells return -ENOMEM; 3232716ea3a7SDavid Howells 3233716ea3a7SDavid Howells if (skb1->sk) 3234716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3235716ea3a7SDavid Howells 3236716ea3a7SDavid Howells /* Looking around. Are we still alive? 3237716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3238716ea3a7SDavid Howells 3239716ea3a7SDavid Howells skb2->next = skb1->next; 3240716ea3a7SDavid Howells *skb_p = skb2; 3241716ea3a7SDavid Howells kfree_skb(skb1); 3242716ea3a7SDavid Howells skb1 = skb2; 3243716ea3a7SDavid Howells } 3244716ea3a7SDavid Howells elt++; 3245716ea3a7SDavid Howells *trailer = skb1; 3246716ea3a7SDavid Howells skb_p = &skb1->next; 3247716ea3a7SDavid Howells } 3248716ea3a7SDavid Howells 3249716ea3a7SDavid Howells return elt; 3250716ea3a7SDavid Howells } 3251b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3252716ea3a7SDavid Howells 3253b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3254b1faf566SEric Dumazet { 3255b1faf566SEric Dumazet struct sock *sk = skb->sk; 3256b1faf566SEric Dumazet 3257b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3258b1faf566SEric Dumazet } 3259b1faf566SEric Dumazet 3260b1faf566SEric Dumazet /* 3261b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3262b1faf566SEric Dumazet */ 3263b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3264b1faf566SEric Dumazet { 3265110c4330SEric Dumazet int len = skb->len; 3266110c4330SEric Dumazet 3267b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 326895c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3269b1faf566SEric Dumazet return -ENOMEM; 3270b1faf566SEric Dumazet 3271b1faf566SEric Dumazet skb_orphan(skb); 3272b1faf566SEric Dumazet skb->sk = sk; 3273b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3274b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3275b1faf566SEric Dumazet 3276abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3277abb57ea4SEric Dumazet skb_dst_force(skb); 3278abb57ea4SEric Dumazet 3279b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3280b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3281110c4330SEric Dumazet sk->sk_data_ready(sk, len); 3282b1faf566SEric Dumazet return 0; 3283b1faf566SEric Dumazet } 3284b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3285b1faf566SEric Dumazet 3286ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb, 3287ac45f602SPatrick Ohly struct skb_shared_hwtstamps *hwtstamps) 3288ac45f602SPatrick Ohly { 3289ac45f602SPatrick Ohly struct sock *sk = orig_skb->sk; 3290ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3291ac45f602SPatrick Ohly struct sk_buff *skb; 3292ac45f602SPatrick Ohly int err; 3293ac45f602SPatrick Ohly 3294ac45f602SPatrick Ohly if (!sk) 3295ac45f602SPatrick Ohly return; 3296ac45f602SPatrick Ohly 3297ac45f602SPatrick Ohly skb = skb_clone(orig_skb, GFP_ATOMIC); 3298ac45f602SPatrick Ohly if (!skb) 3299ac45f602SPatrick Ohly return; 3300ac45f602SPatrick Ohly 3301ac45f602SPatrick Ohly if (hwtstamps) { 3302ac45f602SPatrick Ohly *skb_hwtstamps(skb) = 3303ac45f602SPatrick Ohly *hwtstamps; 3304ac45f602SPatrick Ohly } else { 3305ac45f602SPatrick Ohly /* 3306ac45f602SPatrick Ohly * no hardware time stamps available, 33072244d07bSOliver Hartkopp * so keep the shared tx_flags and only 3308ac45f602SPatrick Ohly * store software time stamp 3309ac45f602SPatrick Ohly */ 3310ac45f602SPatrick Ohly skb->tstamp = ktime_get_real(); 3311ac45f602SPatrick Ohly } 3312ac45f602SPatrick Ohly 3313ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3314ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3315ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3316ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 331729030374SEric Dumazet 3318ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 331929030374SEric Dumazet 3320ac45f602SPatrick Ohly if (err) 3321ac45f602SPatrick Ohly kfree_skb(skb); 3322ac45f602SPatrick Ohly } 3323ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3324ac45f602SPatrick Ohly 33256e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 33266e3e939fSJohannes Berg { 33276e3e939fSJohannes Berg struct sock *sk = skb->sk; 33286e3e939fSJohannes Berg struct sock_exterr_skb *serr; 33296e3e939fSJohannes Berg int err; 33306e3e939fSJohannes Berg 33316e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 33326e3e939fSJohannes Berg skb->wifi_acked = acked; 33336e3e939fSJohannes Berg 33346e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 33356e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 33366e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 33376e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 33386e3e939fSJohannes Berg 33396e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 33406e3e939fSJohannes Berg if (err) 33416e3e939fSJohannes Berg kfree_skb(skb); 33426e3e939fSJohannes Berg } 33436e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 33446e3e939fSJohannes Berg 3345ac45f602SPatrick Ohly 3346f35d9d8aSRusty Russell /** 3347f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3348f35d9d8aSRusty Russell * @skb: the skb to set 3349f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3350f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3351f35d9d8aSRusty Russell * 3352f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3353f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3354f35d9d8aSRusty Russell * 3355f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3356f35d9d8aSRusty Russell * returns false you should drop the packet. 3357f35d9d8aSRusty Russell */ 3358f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3359f35d9d8aSRusty Russell { 33605ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 33615ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3362e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 33635ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3364f35d9d8aSRusty Russell return false; 3365f35d9d8aSRusty Russell } 3366f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3367f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3368f35d9d8aSRusty Russell skb->csum_offset = off; 3369f35d9d8aSRusty Russell return true; 3370f35d9d8aSRusty Russell } 3371b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3372f35d9d8aSRusty Russell 33734497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 33744497b076SBen Hutchings { 3375e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 3376e87cc472SJoe Perches skb->dev->name); 33774497b076SBen Hutchings } 33784497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3379bad43ca8SEric Dumazet 3380bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 3381bad43ca8SEric Dumazet { 33823d861f66SEric Dumazet if (head_stolen) { 33833d861f66SEric Dumazet skb_release_head_state(skb); 3384bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 33853d861f66SEric Dumazet } else { 3386bad43ca8SEric Dumazet __kfree_skb(skb); 3387bad43ca8SEric Dumazet } 33883d861f66SEric Dumazet } 3389bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 3390bad43ca8SEric Dumazet 3391bad43ca8SEric Dumazet /** 3392bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 3393bad43ca8SEric Dumazet * @to: prior buffer 3394bad43ca8SEric Dumazet * @from: buffer to add 3395bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 3396c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 3397bad43ca8SEric Dumazet */ 3398bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 3399bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 3400bad43ca8SEric Dumazet { 3401bad43ca8SEric Dumazet int i, delta, len = from->len; 3402bad43ca8SEric Dumazet 3403bad43ca8SEric Dumazet *fragstolen = false; 3404bad43ca8SEric Dumazet 3405bad43ca8SEric Dumazet if (skb_cloned(to)) 3406bad43ca8SEric Dumazet return false; 3407bad43ca8SEric Dumazet 3408bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 3409bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 3410bad43ca8SEric Dumazet *delta_truesize = 0; 3411bad43ca8SEric Dumazet return true; 3412bad43ca8SEric Dumazet } 3413bad43ca8SEric Dumazet 3414bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 3415bad43ca8SEric Dumazet return false; 3416bad43ca8SEric Dumazet 3417bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 3418bad43ca8SEric Dumazet struct page *page; 3419bad43ca8SEric Dumazet unsigned int offset; 3420bad43ca8SEric Dumazet 3421bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3422bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 3423bad43ca8SEric Dumazet return false; 3424bad43ca8SEric Dumazet 3425bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 3426bad43ca8SEric Dumazet return false; 3427bad43ca8SEric Dumazet 3428bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3429bad43ca8SEric Dumazet 3430bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 3431bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 3432bad43ca8SEric Dumazet 3433bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 3434bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 3435bad43ca8SEric Dumazet *fragstolen = true; 3436bad43ca8SEric Dumazet } else { 3437bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3438bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 3439bad43ca8SEric Dumazet return false; 3440bad43ca8SEric Dumazet 3441f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 3442bad43ca8SEric Dumazet } 3443bad43ca8SEric Dumazet 3444bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 3445bad43ca8SEric Dumazet 3446bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 3447bad43ca8SEric Dumazet skb_shinfo(from)->frags, 3448bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 3449bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 3450bad43ca8SEric Dumazet 3451bad43ca8SEric Dumazet if (!skb_cloned(from)) 3452bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 3453bad43ca8SEric Dumazet 34548ea853fdSLi RongQing /* if the skb is not cloned this does nothing 34558ea853fdSLi RongQing * since we set nr_frags to 0. 34568ea853fdSLi RongQing */ 3457bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 3458bad43ca8SEric Dumazet skb_frag_ref(from, i); 3459bad43ca8SEric Dumazet 3460bad43ca8SEric Dumazet to->truesize += delta; 3461bad43ca8SEric Dumazet to->len += len; 3462bad43ca8SEric Dumazet to->data_len += len; 3463bad43ca8SEric Dumazet 3464bad43ca8SEric Dumazet *delta_truesize = delta; 3465bad43ca8SEric Dumazet return true; 3466bad43ca8SEric Dumazet } 3467bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 3468