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 391da177e4SLinus Torvalds #include <linux/module.h> 401da177e4SLinus Torvalds #include <linux/types.h> 411da177e4SLinus Torvalds #include <linux/kernel.h> 42fe55f6d5SVegard Nossum #include <linux/kmemcheck.h> 431da177e4SLinus Torvalds #include <linux/mm.h> 441da177e4SLinus Torvalds #include <linux/interrupt.h> 451da177e4SLinus Torvalds #include <linux/in.h> 461da177e4SLinus Torvalds #include <linux/inet.h> 471da177e4SLinus Torvalds #include <linux/slab.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 501da177e4SLinus Torvalds #include <net/pkt_sched.h> 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #include <linux/string.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 549c55e01cSJens Axboe #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/cache.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <linux/init.h> 58716ea3a7SDavid Howells #include <linux/scatterlist.h> 59ac45f602SPatrick Ohly #include <linux/errqueue.h> 60268bb0ceSLinus Torvalds #include <linux/prefetch.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #include <net/protocol.h> 631da177e4SLinus Torvalds #include <net/dst.h> 641da177e4SLinus Torvalds #include <net/sock.h> 651da177e4SLinus Torvalds #include <net/checksum.h> 661da177e4SLinus Torvalds #include <net/xfrm.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <asm/uaccess.h> 69ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7051c56b00SEric Dumazet #include <linux/highmem.h> 71a1f8e7f7SAl Viro 72e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly; 73e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 741da177e4SLinus Torvalds 759c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 769c55e01cSJens Axboe struct pipe_buffer *buf) 779c55e01cSJens Axboe { 788b9d3728SJarek Poplawski put_page(buf->page); 799c55e01cSJens Axboe } 809c55e01cSJens Axboe 819c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 829c55e01cSJens Axboe struct pipe_buffer *buf) 839c55e01cSJens Axboe { 848b9d3728SJarek Poplawski get_page(buf->page); 859c55e01cSJens Axboe } 869c55e01cSJens Axboe 879c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 889c55e01cSJens Axboe struct pipe_buffer *buf) 899c55e01cSJens Axboe { 909c55e01cSJens Axboe return 1; 919c55e01cSJens Axboe } 929c55e01cSJens Axboe 939c55e01cSJens Axboe 949c55e01cSJens Axboe /* Pipe buffer operations for a socket. */ 9528dfef8fSAlexey Dobriyan static const struct pipe_buf_operations sock_pipe_buf_ops = { 969c55e01cSJens Axboe .can_merge = 0, 979c55e01cSJens Axboe .map = generic_pipe_buf_map, 989c55e01cSJens Axboe .unmap = generic_pipe_buf_unmap, 999c55e01cSJens Axboe .confirm = generic_pipe_buf_confirm, 1009c55e01cSJens Axboe .release = sock_pipe_buf_release, 1019c55e01cSJens Axboe .steal = sock_pipe_buf_steal, 1029c55e01cSJens Axboe .get = sock_pipe_buf_get, 1039c55e01cSJens Axboe }; 1049c55e01cSJens Axboe 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 1071da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 1081da177e4SLinus Torvalds * reliable. 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /** 1121da177e4SLinus Torvalds * skb_over_panic - private function 1131da177e4SLinus Torvalds * @skb: buffer 1141da177e4SLinus Torvalds * @sz: size 1151da177e4SLinus Torvalds * @here: address 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 1181da177e4SLinus Torvalds */ 119ccb7c773SRami Rosen static void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1201da177e4SLinus Torvalds { 12126095455SPatrick McHardy printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 1224305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 12327a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1244305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12526095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1261da177e4SLinus Torvalds BUG(); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /** 1301da177e4SLinus Torvalds * skb_under_panic - private function 1311da177e4SLinus Torvalds * @skb: buffer 1321da177e4SLinus Torvalds * @sz: size 1331da177e4SLinus Torvalds * @here: address 1341da177e4SLinus Torvalds * 1351da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds 138ccb7c773SRami Rosen static void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1391da177e4SLinus Torvalds { 14026095455SPatrick McHardy printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 1414305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 14227a884dcSArnaldo Carvalho de Melo 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 1481da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1491da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1501da177e4SLinus Torvalds * [BEEP] leaks. 1511da177e4SLinus Torvalds * 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /** 155d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1561da177e4SLinus Torvalds * @size: size to allocate 1571da177e4SLinus Torvalds * @gfp_mask: allocation mask 158c83c2486SRandy Dunlap * @fclone: allocate from fclone cache instead of head cache 159c83c2486SRandy Dunlap * and allocate a cloned (child) skb 160b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1611da177e4SLinus Torvalds * 1621da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 1631da177e4SLinus Torvalds * tail room of size bytes. The object has a reference count of one. 1641da177e4SLinus Torvalds * The return is the buffer. On a failure the return is %NULL. 1651da177e4SLinus Torvalds * 1661da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1671da177e4SLinus Torvalds * %GFP_ATOMIC. 1681da177e4SLinus Torvalds */ 169dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 170b30973f8SChristoph Hellwig int fclone, int node) 1711da177e4SLinus Torvalds { 172e18b890bSChristoph Lameter struct kmem_cache *cache; 1734947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1741da177e4SLinus Torvalds struct sk_buff *skb; 1751da177e4SLinus Torvalds u8 *data; 1761da177e4SLinus Torvalds 1778798b3fbSHerbert Xu cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 1788798b3fbSHerbert Xu 1791da177e4SLinus Torvalds /* Get the HEAD */ 180b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1811da177e4SLinus Torvalds if (!skb) 1821da177e4SLinus Torvalds goto out; 183ec7d2f2cSEric Dumazet prefetchw(skb); 1841da177e4SLinus Torvalds 18587fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 18687fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 18787fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 18887fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 18987fb4b7bSEric Dumazet */ 190bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 19187fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 19287fb4b7bSEric Dumazet data = kmalloc_node_track_caller(size, gfp_mask, node); 1931da177e4SLinus Torvalds if (!data) 1941da177e4SLinus Torvalds goto nodata; 19587fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 19687fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 19787fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 19887fb4b7bSEric Dumazet */ 19987fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 200ec7d2f2cSEric Dumazet prefetchw(data + size); 2011da177e4SLinus Torvalds 202ca0605a7SArnaldo Carvalho de Melo /* 203c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 204c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 205c8005785SJohannes Berg * the tail pointer in struct sk_buff! 206ca0605a7SArnaldo Carvalho de Melo */ 207ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 20887fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 20987fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 2101da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2111da177e4SLinus Torvalds skb->head = data; 2121da177e4SLinus Torvalds skb->data = data; 21327a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2144305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 21519633e12SStephen Hemminger #ifdef NET_SKBUFF_DATA_USES_OFFSET 21619633e12SStephen Hemminger skb->mac_header = ~0U; 21719633e12SStephen Hemminger #endif 21819633e12SStephen Hemminger 2194947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2204947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 221ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2224947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 223c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2244947d3efSBenjamin LaHaise 225d179cd12SDavid S. Miller if (fclone) { 226d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 227d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 2281da177e4SLinus Torvalds 229fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags1); 230fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags2); 231d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 232d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 233d179cd12SDavid S. Miller 234d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 235d179cd12SDavid S. Miller } 2361da177e4SLinus Torvalds out: 2371da177e4SLinus Torvalds return skb; 2381da177e4SLinus Torvalds nodata: 2398798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2401da177e4SLinus Torvalds skb = NULL; 2411da177e4SLinus Torvalds goto out; 2421da177e4SLinus Torvalds } 243b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /** 246b2b5ce9dSEric Dumazet * build_skb - build a network buffer 247b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 248b2b5ce9dSEric Dumazet * 249b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 250b2b5ce9dSEric Dumazet * skb_shared_info. @data must have been allocated by kmalloc() 251b2b5ce9dSEric Dumazet * The return is the new skb buffer. 252b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 253b2b5ce9dSEric Dumazet * Notes : 254b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 255b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 256b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 257b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 258b2b5ce9dSEric Dumazet * before giving packet to stack. 259b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 260b2b5ce9dSEric Dumazet */ 261b2b5ce9dSEric Dumazet struct sk_buff *build_skb(void *data) 262b2b5ce9dSEric Dumazet { 263b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 264b2b5ce9dSEric Dumazet struct sk_buff *skb; 265b2b5ce9dSEric Dumazet unsigned int size; 266b2b5ce9dSEric Dumazet 267b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 268b2b5ce9dSEric Dumazet if (!skb) 269b2b5ce9dSEric Dumazet return NULL; 270b2b5ce9dSEric Dumazet 271b2b5ce9dSEric Dumazet size = ksize(data) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 272b2b5ce9dSEric Dumazet 273b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 274b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 275b2b5ce9dSEric Dumazet atomic_set(&skb->users, 1); 276b2b5ce9dSEric Dumazet skb->head = data; 277b2b5ce9dSEric Dumazet skb->data = data; 278b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 279b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 280b2b5ce9dSEric Dumazet #ifdef NET_SKBUFF_DATA_USES_OFFSET 281b2b5ce9dSEric Dumazet skb->mac_header = ~0U; 282b2b5ce9dSEric Dumazet #endif 283b2b5ce9dSEric Dumazet 284b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 285b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 286b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 287b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 288b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 289b2b5ce9dSEric Dumazet 290b2b5ce9dSEric Dumazet return skb; 291b2b5ce9dSEric Dumazet } 292b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 293b2b5ce9dSEric Dumazet 294b2b5ce9dSEric Dumazet /** 2958af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 2968af27456SChristoph Hellwig * @dev: network device to receive on 2978af27456SChristoph Hellwig * @length: length to allocate 2988af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 2998af27456SChristoph Hellwig * 3008af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 3018af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 3028af27456SChristoph Hellwig * the headroom they think they need without accounting for the 3038af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 3048af27456SChristoph Hellwig * 3058af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 3068af27456SChristoph Hellwig */ 3078af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 3088af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 3098af27456SChristoph Hellwig { 3108af27456SChristoph Hellwig struct sk_buff *skb; 3118af27456SChristoph Hellwig 312564824b0SEric Dumazet skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE); 3137b2e497aSChristoph Hellwig if (likely(skb)) { 3148af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 3157b2e497aSChristoph Hellwig skb->dev = dev; 3167b2e497aSChristoph Hellwig } 3178af27456SChristoph Hellwig return skb; 3188af27456SChristoph Hellwig } 319b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 3201da177e4SLinus Torvalds 321654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 32250269e19SEric Dumazet int size, unsigned int truesize) 323654bed16SPeter Zijlstra { 324654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 325654bed16SPeter Zijlstra skb->len += size; 326654bed16SPeter Zijlstra skb->data_len += size; 32750269e19SEric Dumazet skb->truesize += truesize; 328654bed16SPeter Zijlstra } 329654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 330654bed16SPeter Zijlstra 331f58518e6SIlpo Järvinen /** 332f58518e6SIlpo Järvinen * dev_alloc_skb - allocate an skbuff for receiving 333f58518e6SIlpo Järvinen * @length: length to allocate 334f58518e6SIlpo Järvinen * 335f58518e6SIlpo Järvinen * Allocate a new &sk_buff and assign it a usage count of one. The 336f58518e6SIlpo Järvinen * buffer has unspecified headroom built in. Users should allocate 337f58518e6SIlpo Järvinen * the headroom they think they need without accounting for the 338f58518e6SIlpo Järvinen * built in space. The built in space is used for optimisations. 339f58518e6SIlpo Järvinen * 340f58518e6SIlpo Järvinen * %NULL is returned if there is no free memory. Although this function 341f58518e6SIlpo Järvinen * allocates memory it can be called from an interrupt. 342f58518e6SIlpo Järvinen */ 343f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length) 344f58518e6SIlpo Järvinen { 3451483b874SDenys Vlasenko /* 3461483b874SDenys Vlasenko * There is more code here than it seems: 347a0f55e0eSDavid S. Miller * __dev_alloc_skb is an inline 3481483b874SDenys Vlasenko */ 349f58518e6SIlpo Järvinen return __dev_alloc_skb(length, GFP_ATOMIC); 350f58518e6SIlpo Järvinen } 351f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb); 352f58518e6SIlpo Järvinen 35327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 3541da177e4SLinus Torvalds { 35527b437c8SHerbert Xu struct sk_buff *list = *listp; 3561da177e4SLinus Torvalds 35727b437c8SHerbert Xu *listp = NULL; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds do { 3601da177e4SLinus Torvalds struct sk_buff *this = list; 3611da177e4SLinus Torvalds list = list->next; 3621da177e4SLinus Torvalds kfree_skb(this); 3631da177e4SLinus Torvalds } while (list); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 36627b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 36727b437c8SHerbert Xu { 36827b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 36927b437c8SHerbert Xu } 37027b437c8SHerbert Xu 3711da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds struct sk_buff *list; 3741da177e4SLinus Torvalds 375fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 3761da177e4SLinus Torvalds skb_get(list); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3795bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds if (!skb->cloned || 3821da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 3831da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 3841da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 3851da177e4SLinus Torvalds int i; 3861da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 387ea2ab693SIan Campbell skb_frag_unref(skb, i); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 390a6686f2fSShirley Ma /* 391a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 392a6686f2fSShirley Ma * the lower device DMA has done; 393a6686f2fSShirley Ma */ 394a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 395a6686f2fSShirley Ma struct ubuf_info *uarg; 396a6686f2fSShirley Ma 397a6686f2fSShirley Ma uarg = skb_shinfo(skb)->destructor_arg; 398a6686f2fSShirley Ma if (uarg->callback) 399a6686f2fSShirley Ma uarg->callback(uarg); 400a6686f2fSShirley Ma } 401a6686f2fSShirley Ma 40221dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 4031da177e4SLinus Torvalds skb_drop_fraglist(skb); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds kfree(skb->head); 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 4111da177e4SLinus Torvalds */ 4122d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 4131da177e4SLinus Torvalds { 414d179cd12SDavid S. Miller struct sk_buff *other; 415d179cd12SDavid S. Miller atomic_t *fclone_ref; 416d179cd12SDavid S. Miller 417d179cd12SDavid S. Miller switch (skb->fclone) { 418d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 4191da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 420d179cd12SDavid S. Miller break; 421d179cd12SDavid S. Miller 422d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 423d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 424d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 425d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 426d179cd12SDavid S. Miller break; 427d179cd12SDavid S. Miller 428d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 429d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 430d179cd12SDavid S. Miller other = skb - 1; 431d179cd12SDavid S. Miller 432d179cd12SDavid S. Miller /* The clone portion is available for 433d179cd12SDavid S. Miller * fast-cloning again. 434d179cd12SDavid S. Miller */ 435d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 436d179cd12SDavid S. Miller 437d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 438d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 439d179cd12SDavid S. Miller break; 4403ff50b79SStephen Hemminger } 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 44304a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 4441da177e4SLinus Torvalds { 445adf30907SEric Dumazet skb_dst_drop(skb); 4461da177e4SLinus Torvalds #ifdef CONFIG_XFRM 4471da177e4SLinus Torvalds secpath_put(skb->sp); 4481da177e4SLinus Torvalds #endif 4491da177e4SLinus Torvalds if (skb->destructor) { 4509c2b3328SStephen Hemminger WARN_ON(in_irq()); 4511da177e4SLinus Torvalds skb->destructor(skb); 4521da177e4SLinus Torvalds } 453a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 4545f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 4552fc72c7bSKOVACS Krisztian #endif 4562fc72c7bSKOVACS Krisztian #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED 4579fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 4589fb9cbb1SYasuyuki Kozakai #endif 4591da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 4601da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 4611da177e4SLinus Torvalds #endif 4621da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 4631da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4641da177e4SLinus Torvalds skb->tc_index = 0; 4651da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 4661da177e4SLinus Torvalds skb->tc_verd = 0; 4671da177e4SLinus Torvalds #endif 4681da177e4SLinus Torvalds #endif 46904a4bb55SLennert Buytenhek } 47004a4bb55SLennert Buytenhek 47104a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 47204a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 47304a4bb55SLennert Buytenhek { 47404a4bb55SLennert Buytenhek skb_release_head_state(skb); 4752d4baff8SHerbert Xu skb_release_data(skb); 4762d4baff8SHerbert Xu } 4771da177e4SLinus Torvalds 4782d4baff8SHerbert Xu /** 4792d4baff8SHerbert Xu * __kfree_skb - private function 4802d4baff8SHerbert Xu * @skb: buffer 4812d4baff8SHerbert Xu * 4822d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 4832d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 4842d4baff8SHerbert Xu * always call kfree_skb 4852d4baff8SHerbert Xu */ 4862d4baff8SHerbert Xu 4872d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 4882d4baff8SHerbert Xu { 4892d4baff8SHerbert Xu skb_release_all(skb); 4901da177e4SLinus Torvalds kfree_skbmem(skb); 4911da177e4SLinus Torvalds } 492b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /** 495231d06aeSJörn Engel * kfree_skb - free an sk_buff 496231d06aeSJörn Engel * @skb: buffer to free 497231d06aeSJörn Engel * 498231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 499231d06aeSJörn Engel * hit zero. 500231d06aeSJörn Engel */ 501231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 502231d06aeSJörn Engel { 503231d06aeSJörn Engel if (unlikely(!skb)) 504231d06aeSJörn Engel return; 505231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 506231d06aeSJörn Engel smp_rmb(); 507231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 508231d06aeSJörn Engel return; 509ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 510231d06aeSJörn Engel __kfree_skb(skb); 511231d06aeSJörn Engel } 512b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 513231d06aeSJörn Engel 514d1a203eaSStephen Hemminger /** 515ead2ceb0SNeil Horman * consume_skb - free an skbuff 516ead2ceb0SNeil Horman * @skb: buffer to free 517ead2ceb0SNeil Horman * 518ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 519ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 520ead2ceb0SNeil Horman * is being dropped after a failure and notes that 521ead2ceb0SNeil Horman */ 522ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 523ead2ceb0SNeil Horman { 524ead2ceb0SNeil Horman if (unlikely(!skb)) 525ead2ceb0SNeil Horman return; 526ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 527ead2ceb0SNeil Horman smp_rmb(); 528ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 529ead2ceb0SNeil Horman return; 53007dc22e7SKoki Sanagi trace_consume_skb(skb); 531ead2ceb0SNeil Horman __kfree_skb(skb); 532ead2ceb0SNeil Horman } 533ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 534ead2ceb0SNeil Horman 535ead2ceb0SNeil Horman /** 5363d153a7cSAndy Fleming * skb_recycle - clean up an skb for reuse 5373d153a7cSAndy Fleming * @skb: buffer 5383d153a7cSAndy Fleming * 5393d153a7cSAndy Fleming * Recycles the skb to be reused as a receive buffer. This 5403d153a7cSAndy Fleming * function does any necessary reference count dropping, and 5413d153a7cSAndy Fleming * cleans up the skbuff as if it just came from __alloc_skb(). 5423d153a7cSAndy Fleming */ 5433d153a7cSAndy Fleming void skb_recycle(struct sk_buff *skb) 5443d153a7cSAndy Fleming { 5453d153a7cSAndy Fleming struct skb_shared_info *shinfo; 5463d153a7cSAndy Fleming 5473d153a7cSAndy Fleming skb_release_head_state(skb); 5483d153a7cSAndy Fleming 5493d153a7cSAndy Fleming shinfo = skb_shinfo(skb); 5503d153a7cSAndy Fleming memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 5513d153a7cSAndy Fleming atomic_set(&shinfo->dataref, 1); 5523d153a7cSAndy Fleming 5533d153a7cSAndy Fleming memset(skb, 0, offsetof(struct sk_buff, tail)); 5543d153a7cSAndy Fleming skb->data = skb->head + NET_SKB_PAD; 5553d153a7cSAndy Fleming skb_reset_tail_pointer(skb); 5563d153a7cSAndy Fleming } 5573d153a7cSAndy Fleming EXPORT_SYMBOL(skb_recycle); 5583d153a7cSAndy Fleming 5593d153a7cSAndy Fleming /** 560d1a203eaSStephen Hemminger * skb_recycle_check - check if skb can be reused for receive 561d1a203eaSStephen Hemminger * @skb: buffer 562d1a203eaSStephen Hemminger * @skb_size: minimum receive buffer size 563d1a203eaSStephen Hemminger * 564d1a203eaSStephen Hemminger * Checks that the skb passed in is not shared or cloned, and 565d1a203eaSStephen Hemminger * that it is linear and its head portion at least as large as 566d1a203eaSStephen Hemminger * skb_size so that it can be recycled as a receive buffer. 567d1a203eaSStephen Hemminger * If these conditions are met, this function does any necessary 568d1a203eaSStephen Hemminger * reference count dropping and cleans up the skbuff as if it 569d1a203eaSStephen Hemminger * just came from __alloc_skb(). 570d1a203eaSStephen Hemminger */ 5715b0daa34SChangli Gao bool skb_recycle_check(struct sk_buff *skb, int skb_size) 57204a4bb55SLennert Buytenhek { 5733d153a7cSAndy Fleming if (!skb_is_recycleable(skb, skb_size)) 5745b0daa34SChangli Gao return false; 575e84af6ddSAnton Vorontsov 5763d153a7cSAndy Fleming skb_recycle(skb); 57704a4bb55SLennert Buytenhek 5785b0daa34SChangli Gao return true; 57904a4bb55SLennert Buytenhek } 58004a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check); 58104a4bb55SLennert Buytenhek 582dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 583dec18810SHerbert Xu { 584dec18810SHerbert Xu new->tstamp = old->tstamp; 585dec18810SHerbert Xu new->dev = old->dev; 586dec18810SHerbert Xu new->transport_header = old->transport_header; 587dec18810SHerbert Xu new->network_header = old->network_header; 588dec18810SHerbert Xu new->mac_header = old->mac_header; 5897fee226aSEric Dumazet skb_dst_copy(new, old); 5900a9627f2STom Herbert new->rxhash = old->rxhash; 5916461be3aSChangli Gao new->ooo_okay = old->ooo_okay; 592bdeab991STom Herbert new->l4_rxhash = old->l4_rxhash; 5933bdc0ebaSBen Greear new->no_fcs = old->no_fcs; 594def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 595dec18810SHerbert Xu new->sp = secpath_get(old->sp); 596dec18810SHerbert Xu #endif 597dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 5989bcb97caSHerbert Xu new->csum = old->csum; 599dec18810SHerbert Xu new->local_df = old->local_df; 600dec18810SHerbert Xu new->pkt_type = old->pkt_type; 601dec18810SHerbert Xu new->ip_summed = old->ip_summed; 602dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 603dec18810SHerbert Xu new->priority = old->priority; 604a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_IP_VS) 605dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 606dec18810SHerbert Xu #endif 607dec18810SHerbert Xu new->protocol = old->protocol; 608dec18810SHerbert Xu new->mark = old->mark; 6098964be4aSEric Dumazet new->skb_iif = old->skb_iif; 610dec18810SHerbert Xu __nf_copy(new, old); 611a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 612dec18810SHerbert Xu new->nf_trace = old->nf_trace; 613dec18810SHerbert Xu #endif 614dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 615dec18810SHerbert Xu new->tc_index = old->tc_index; 616dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 617dec18810SHerbert Xu new->tc_verd = old->tc_verd; 618dec18810SHerbert Xu #endif 619dec18810SHerbert Xu #endif 6206aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 6216aa895b0SPatrick McHardy 622dec18810SHerbert Xu skb_copy_secmark(new, old); 623dec18810SHerbert Xu } 624dec18810SHerbert Xu 62582c49a35SHerbert Xu /* 62682c49a35SHerbert Xu * You should not add any new code to this function. Add it to 62782c49a35SHerbert Xu * __copy_skb_header above instead. 62882c49a35SHerbert Xu */ 629e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds #define C(x) n->x = skb->x 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds n->next = n->prev = NULL; 6341da177e4SLinus Torvalds n->sk = NULL; 635dec18810SHerbert Xu __copy_skb_header(n, skb); 636dec18810SHerbert Xu 6371da177e4SLinus Torvalds C(len); 6381da177e4SLinus Torvalds C(data_len); 6393e6b3b2eSAlexey Dobriyan C(mac_len); 640334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 64102f1c89dSPaul Moore n->cloned = 1; 6421da177e4SLinus Torvalds n->nohdr = 0; 6431da177e4SLinus Torvalds n->destructor = NULL; 6441da177e4SLinus Torvalds C(tail); 6451da177e4SLinus Torvalds C(end); 64602f1c89dSPaul Moore C(head); 64702f1c89dSPaul Moore C(data); 64802f1c89dSPaul Moore C(truesize); 64902f1c89dSPaul Moore atomic_set(&n->users, 1); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 6521da177e4SLinus Torvalds skb->cloned = 1; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds return n; 655e0053ec0SHerbert Xu #undef C 656e0053ec0SHerbert Xu } 657e0053ec0SHerbert Xu 658e0053ec0SHerbert Xu /** 659e0053ec0SHerbert Xu * skb_morph - morph one skb into another 660e0053ec0SHerbert Xu * @dst: the skb to receive the contents 661e0053ec0SHerbert Xu * @src: the skb to supply the contents 662e0053ec0SHerbert Xu * 663e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 664e0053ec0SHerbert Xu * supplied by the user. 665e0053ec0SHerbert Xu * 666e0053ec0SHerbert Xu * The target skb is returned upon exit. 667e0053ec0SHerbert Xu */ 668e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 669e0053ec0SHerbert Xu { 6702d4baff8SHerbert Xu skb_release_all(dst); 671e0053ec0SHerbert Xu return __skb_clone(dst, src); 672e0053ec0SHerbert Xu } 673e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 674e0053ec0SHerbert Xu 67548c83012SMichael S. Tsirkin /* skb_copy_ubufs - copy userspace skb frags buffers to kernel 67648c83012SMichael S. Tsirkin * @skb: the skb to modify 67748c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 67848c83012SMichael S. Tsirkin * 67948c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 68048c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 68148c83012SMichael S. Tsirkin * to userspace pages. 68248c83012SMichael S. Tsirkin * 68348c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 68448c83012SMichael S. Tsirkin * %GFP_ATOMIC. 68548c83012SMichael S. Tsirkin * 68648c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 68748c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 68848c83012SMichael S. Tsirkin */ 68948c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 690a6686f2fSShirley Ma { 691a6686f2fSShirley Ma int i; 692a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 693a6686f2fSShirley Ma struct page *page, *head = NULL; 694a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 695a6686f2fSShirley Ma 696a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 697a6686f2fSShirley Ma u8 *vaddr; 698a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 699a6686f2fSShirley Ma 700a6686f2fSShirley Ma page = alloc_page(GFP_ATOMIC); 701a6686f2fSShirley Ma if (!page) { 702a6686f2fSShirley Ma while (head) { 703a6686f2fSShirley Ma struct page *next = (struct page *)head->private; 704a6686f2fSShirley Ma put_page(head); 705a6686f2fSShirley Ma head = next; 706a6686f2fSShirley Ma } 707a6686f2fSShirley Ma return -ENOMEM; 708a6686f2fSShirley Ma } 70951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 710a6686f2fSShirley Ma memcpy(page_address(page), 7119e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 71251c56b00SEric Dumazet kunmap_atomic(vaddr); 713a6686f2fSShirley Ma page->private = (unsigned long)head; 714a6686f2fSShirley Ma head = page; 715a6686f2fSShirley Ma } 716a6686f2fSShirley Ma 717a6686f2fSShirley Ma /* skb frags release userspace buffers */ 718a6686f2fSShirley Ma for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 719a8605c60SIan Campbell skb_frag_unref(skb, i); 720a6686f2fSShirley Ma 721a6686f2fSShirley Ma uarg->callback(uarg); 722a6686f2fSShirley Ma 723a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 724a6686f2fSShirley Ma for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) { 725a8605c60SIan Campbell __skb_fill_page_desc(skb, i-1, head, 0, 726a8605c60SIan Campbell skb_shinfo(skb)->frags[i - 1].size); 727a6686f2fSShirley Ma head = (struct page *)head->private; 728a6686f2fSShirley Ma } 72948c83012SMichael S. Tsirkin 73048c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 731a6686f2fSShirley Ma return 0; 732a6686f2fSShirley Ma } 733a6686f2fSShirley Ma 734a6686f2fSShirley Ma 735e0053ec0SHerbert Xu /** 736e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 737e0053ec0SHerbert Xu * @skb: buffer to clone 738e0053ec0SHerbert Xu * @gfp_mask: allocation priority 739e0053ec0SHerbert Xu * 740e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 741e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 742e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 743e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 744e0053ec0SHerbert Xu * 745e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 746e0053ec0SHerbert Xu * %GFP_ATOMIC. 747e0053ec0SHerbert Xu */ 748e0053ec0SHerbert Xu 749e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 750e0053ec0SHerbert Xu { 751e0053ec0SHerbert Xu struct sk_buff *n; 752e0053ec0SHerbert Xu 753a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 754a6686f2fSShirley Ma if (skb_copy_ubufs(skb, gfp_mask)) 755a6686f2fSShirley Ma return NULL; 756a6686f2fSShirley Ma } 757a6686f2fSShirley Ma 758e0053ec0SHerbert Xu n = skb + 1; 759e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 760e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 761e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 762e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 763e0053ec0SHerbert Xu atomic_inc(fclone_ref); 764e0053ec0SHerbert Xu } else { 765e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 766e0053ec0SHerbert Xu if (!n) 767e0053ec0SHerbert Xu return NULL; 768fe55f6d5SVegard Nossum 769fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 770fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags2); 771e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 772e0053ec0SHerbert Xu } 773e0053ec0SHerbert Xu 774e0053ec0SHerbert Xu return __skb_clone(n, skb); 7751da177e4SLinus Torvalds } 776b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 7791da177e4SLinus Torvalds { 7802e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 7811da177e4SLinus Torvalds /* 7821da177e4SLinus Torvalds * Shift between the two data areas in bytes 7831da177e4SLinus Torvalds */ 7841da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 7852e07fa9cSArnaldo Carvalho de Melo #endif 786dec18810SHerbert Xu 787dec18810SHerbert Xu __copy_skb_header(new, old); 788dec18810SHerbert Xu 7892e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 7902e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 7912e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 7922e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 793603a8bbeSStephen Hemminger if (skb_mac_header_was_set(new)) 7942e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 7952e07fa9cSArnaldo Carvalho de Melo #endif 7967967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 7977967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 7987967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /** 8021da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 8031da177e4SLinus Torvalds * @skb: buffer to copy 8041da177e4SLinus Torvalds * @gfp_mask: allocation priority 8051da177e4SLinus Torvalds * 8061da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 8071da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 8081da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 8091da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 8101da177e4SLinus Torvalds * 8111da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 8121da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 8131da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 8141da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 8151da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds 818dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 8191da177e4SLinus Torvalds { 8206602cebbSEric Dumazet int headerlen = skb_headroom(skb); 8216602cebbSEric Dumazet unsigned int size = (skb_end_pointer(skb) - skb->head) + skb->data_len; 8226602cebbSEric Dumazet struct sk_buff *n = alloc_skb(size, gfp_mask); 8236602cebbSEric Dumazet 8241da177e4SLinus Torvalds if (!n) 8251da177e4SLinus Torvalds return NULL; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* Set the data pointer */ 8281da177e4SLinus Torvalds skb_reserve(n, headerlen); 8291da177e4SLinus Torvalds /* Set the tail pointer and length */ 8301da177e4SLinus Torvalds skb_put(n, skb->len); 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 8331da177e4SLinus Torvalds BUG(); 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds copy_skb_header(n, skb); 8361da177e4SLinus Torvalds return n; 8371da177e4SLinus Torvalds } 838b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /** 841117632e6SEric Dumazet * __pskb_copy - create copy of an sk_buff with private head. 8421da177e4SLinus Torvalds * @skb: buffer to copy 843117632e6SEric Dumazet * @headroom: headroom of new skb 8441da177e4SLinus Torvalds * @gfp_mask: allocation priority 8451da177e4SLinus Torvalds * 8461da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 8471da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 8481da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 8491da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 8501da177e4SLinus Torvalds * or the pointer to the buffer on success. 8511da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 8521da177e4SLinus Torvalds */ 8531da177e4SLinus Torvalds 854117632e6SEric Dumazet struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask) 8551da177e4SLinus Torvalds { 856117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 8576602cebbSEric Dumazet struct sk_buff *n = alloc_skb(size, gfp_mask); 8586602cebbSEric Dumazet 8591da177e4SLinus Torvalds if (!n) 8601da177e4SLinus Torvalds goto out; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* Set the data pointer */ 863117632e6SEric Dumazet skb_reserve(n, headroom); 8641da177e4SLinus Torvalds /* Set the tail pointer and length */ 8651da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 8661da177e4SLinus Torvalds /* Copy the bytes */ 867d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 8681da177e4SLinus Torvalds 86925f484a6SHerbert Xu n->truesize += skb->data_len; 8701da177e4SLinus Torvalds n->data_len = skb->data_len; 8711da177e4SLinus Torvalds n->len = skb->len; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 8741da177e4SLinus Torvalds int i; 8751da177e4SLinus Torvalds 876a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 877a6686f2fSShirley Ma if (skb_copy_ubufs(skb, gfp_mask)) { 8781511022cSDan Carpenter kfree_skb(n); 8791511022cSDan Carpenter n = NULL; 880a6686f2fSShirley Ma goto out; 881a6686f2fSShirley Ma } 882a6686f2fSShirley Ma } 8831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 8841da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 885ea2ab693SIan Campbell skb_frag_ref(skb, i); 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 89021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 8911da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 8921da177e4SLinus Torvalds skb_clone_fraglist(n); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds copy_skb_header(n, skb); 8961da177e4SLinus Torvalds out: 8971da177e4SLinus Torvalds return n; 8981da177e4SLinus Torvalds } 899117632e6SEric Dumazet EXPORT_SYMBOL(__pskb_copy); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /** 9021da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 9031da177e4SLinus Torvalds * @skb: buffer to reallocate 9041da177e4SLinus Torvalds * @nhead: room to add at head 9051da177e4SLinus Torvalds * @ntail: room to add at tail 9061da177e4SLinus Torvalds * @gfp_mask: allocation priority 9071da177e4SLinus Torvalds * 9081da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 9091da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 9101da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 9111da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 9121da177e4SLinus Torvalds * 9131da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 9141da177e4SLinus Torvalds * reloaded after call to this function. 9151da177e4SLinus Torvalds */ 9161da177e4SLinus Torvalds 91786a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 918dd0fc66fSAl Viro gfp_t gfp_mask) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds int i; 9211da177e4SLinus Torvalds u8 *data; 9226602cebbSEric Dumazet int size = nhead + (skb_end_pointer(skb) - skb->head) + ntail; 9231da177e4SLinus Torvalds long off; 9241fd63041SEric Dumazet bool fastpath; 9251da177e4SLinus Torvalds 9264edd87adSHerbert Xu BUG_ON(nhead < 0); 9274edd87adSHerbert Xu 9281da177e4SLinus Torvalds if (skb_shared(skb)) 9291da177e4SLinus Torvalds BUG(); 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 9321da177e4SLinus Torvalds 933ca44ac38SChangli Gao /* Check if we can avoid taking references on fragments if we own 934ca44ac38SChangli Gao * the last reference on skb->head. (see skb_release_data()) 935ca44ac38SChangli Gao */ 936ca44ac38SChangli Gao if (!skb->cloned) 937ca44ac38SChangli Gao fastpath = true; 938ca44ac38SChangli Gao else { 939ca44ac38SChangli Gao int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1; 940ca44ac38SChangli Gao fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta; 941ca44ac38SChangli Gao } 942ca44ac38SChangli Gao 943ca44ac38SChangli Gao if (fastpath && 944ca44ac38SChangli Gao size + sizeof(struct skb_shared_info) <= ksize(skb->head)) { 945ca44ac38SChangli Gao memmove(skb->head + size, skb_shinfo(skb), 946ca44ac38SChangli Gao offsetof(struct skb_shared_info, 947ca44ac38SChangli Gao frags[skb_shinfo(skb)->nr_frags])); 948ca44ac38SChangli Gao memmove(skb->head + nhead, skb->head, 949ca44ac38SChangli Gao skb_tail_pointer(skb) - skb->head); 950ca44ac38SChangli Gao off = nhead; 951ca44ac38SChangli Gao goto adjust_others; 952ca44ac38SChangli Gao } 953ca44ac38SChangli Gao 95487151b86SEric Dumazet data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 95587151b86SEric Dumazet gfp_mask); 9561da177e4SLinus Torvalds if (!data) 9571da177e4SLinus Torvalds goto nodata; 95887151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 9616602cebbSEric Dumazet * optimized for the cases when header is void. 9626602cebbSEric Dumazet */ 9636602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 9646602cebbSEric Dumazet 9656602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 9666602cebbSEric Dumazet skb_shinfo(skb), 967fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 9681da177e4SLinus Torvalds 9691fd63041SEric Dumazet if (fastpath) { 9701fd63041SEric Dumazet kfree(skb->head); 9711fd63041SEric Dumazet } else { 972a6686f2fSShirley Ma /* copy this zero copy skb frags */ 973a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 974a6686f2fSShirley Ma if (skb_copy_ubufs(skb, gfp_mask)) 975a6686f2fSShirley Ma goto nofrags; 976a6686f2fSShirley Ma } 9771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 978ea2ab693SIan Campbell skb_frag_ref(skb, i); 9791da177e4SLinus Torvalds 98021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 9811da177e4SLinus Torvalds skb_clone_fraglist(skb); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds skb_release_data(skb); 9841fd63041SEric Dumazet } 9851da177e4SLinus Torvalds off = (data + nhead) - skb->head; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds skb->head = data; 988ca44ac38SChangli Gao adjust_others: 9891da177e4SLinus Torvalds skb->data += off; 9904305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 9914305b541SArnaldo Carvalho de Melo skb->end = size; 99256eb8882SPatrick McHardy off = nhead; 9934305b541SArnaldo Carvalho de Melo #else 9944305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 99556eb8882SPatrick McHardy #endif 99627a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 99727a884dcSArnaldo Carvalho de Melo skb->tail += off; 998b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 999b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 1000603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1001b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 100200c5a983SAndrea Shepard /* Only adjust this if it actually is csum_start rather than csum */ 100300c5a983SAndrea Shepard if (skb->ip_summed == CHECKSUM_PARTIAL) 1004172a863fSHerbert Xu skb->csum_start += nhead; 10051da177e4SLinus Torvalds skb->cloned = 0; 1006334a8132SPatrick McHardy skb->hdr_len = 0; 10071da177e4SLinus Torvalds skb->nohdr = 0; 10081da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 10091da177e4SLinus Torvalds return 0; 10101da177e4SLinus Torvalds 1011a6686f2fSShirley Ma nofrags: 1012a6686f2fSShirley Ma kfree(data); 10131da177e4SLinus Torvalds nodata: 10141da177e4SLinus Torvalds return -ENOMEM; 10151da177e4SLinus Torvalds } 1016b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds struct sk_buff *skb2; 10231da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (delta <= 0) 10261da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 10271da177e4SLinus Torvalds else { 10281da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 10291da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 10301da177e4SLinus Torvalds GFP_ATOMIC)) { 10311da177e4SLinus Torvalds kfree_skb(skb2); 10321da177e4SLinus Torvalds skb2 = NULL; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds return skb2; 10361da177e4SLinus Torvalds } 1037b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds /** 10401da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 10411da177e4SLinus Torvalds * @skb: buffer to copy 10421da177e4SLinus Torvalds * @newheadroom: new free bytes at head 10431da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 10441da177e4SLinus Torvalds * @gfp_mask: allocation priority 10451da177e4SLinus Torvalds * 10461da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 10471da177e4SLinus Torvalds * allocate additional space. 10481da177e4SLinus Torvalds * 10491da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 10501da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 10511da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 10521da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10531da177e4SLinus Torvalds * 10541da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 10551da177e4SLinus Torvalds * is called from an interrupt. 10561da177e4SLinus Torvalds */ 10571da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 105886a76cafSVictor Fusco int newheadroom, int newtailroom, 1059dd0fc66fSAl Viro gfp_t gfp_mask) 10601da177e4SLinus Torvalds { 10611da177e4SLinus Torvalds /* 10621da177e4SLinus Torvalds * Allocate the copy buffer 10631da177e4SLinus Torvalds */ 10641da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 10651da177e4SLinus Torvalds gfp_mask); 1066efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 10671da177e4SLinus Torvalds int head_copy_len, head_copy_off; 106852886051SHerbert Xu int off; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds if (!n) 10711da177e4SLinus Torvalds return NULL; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds skb_reserve(n, newheadroom); 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* Set the tail pointer and length */ 10761da177e4SLinus Torvalds skb_put(n, skb->len); 10771da177e4SLinus Torvalds 1078efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 10791da177e4SLinus Torvalds head_copy_off = 0; 10801da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 10811da177e4SLinus Torvalds head_copy_len = newheadroom; 10821da177e4SLinus Torvalds else 10831da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /* Copy the linear header and data. */ 10861da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 10871da177e4SLinus Torvalds skb->len + head_copy_len)) 10881da177e4SLinus Torvalds BUG(); 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds copy_skb_header(n, skb); 10911da177e4SLinus Torvalds 1092efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 1093be2b6e62SDavid S. Miller if (n->ip_summed == CHECKSUM_PARTIAL) 109452886051SHerbert Xu n->csum_start += off; 109552886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 1096efd1e8d5SPatrick McHardy n->transport_header += off; 1097efd1e8d5SPatrick McHardy n->network_header += off; 1098603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1099efd1e8d5SPatrick McHardy n->mac_header += off; 110052886051SHerbert Xu #endif 1101efd1e8d5SPatrick McHardy 11021da177e4SLinus Torvalds return n; 11031da177e4SLinus Torvalds } 1104b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /** 11071da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 11081da177e4SLinus Torvalds * @skb: buffer to pad 11091da177e4SLinus Torvalds * @pad: space to pad 11101da177e4SLinus Torvalds * 11111da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 11121da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 11131da177e4SLinus Torvalds * beyond the buffer end onto the wire. 11141da177e4SLinus Torvalds * 11155b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 11161da177e4SLinus Torvalds */ 11171da177e4SLinus Torvalds 11185b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 11191da177e4SLinus Torvalds { 11205b057c6bSHerbert Xu int err; 11215b057c6bSHerbert Xu int ntail; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 11245b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 11251da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 11265b057c6bSHerbert Xu return 0; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11294305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 11305b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 11315b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 11325b057c6bSHerbert Xu if (unlikely(err)) 11335b057c6bSHerbert Xu goto free_skb; 11345b057c6bSHerbert Xu } 11355b057c6bSHerbert Xu 11365b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 11375b057c6bSHerbert Xu * to be audited. 11385b057c6bSHerbert Xu */ 11395b057c6bSHerbert Xu err = skb_linearize(skb); 11405b057c6bSHerbert Xu if (unlikely(err)) 11415b057c6bSHerbert Xu goto free_skb; 11425b057c6bSHerbert Xu 11435b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 11445b057c6bSHerbert Xu return 0; 11455b057c6bSHerbert Xu 11465b057c6bSHerbert Xu free_skb: 11471da177e4SLinus Torvalds kfree_skb(skb); 11485b057c6bSHerbert Xu return err; 11491da177e4SLinus Torvalds } 1150b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 11511da177e4SLinus Torvalds 11520dde3e16SIlpo Järvinen /** 11530dde3e16SIlpo Järvinen * skb_put - add data to a buffer 11540dde3e16SIlpo Järvinen * @skb: buffer to use 11550dde3e16SIlpo Järvinen * @len: amount of data to add 11560dde3e16SIlpo Järvinen * 11570dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 11580dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 11590dde3e16SIlpo Järvinen * first byte of the extra data is returned. 11600dde3e16SIlpo Järvinen */ 11610dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 11620dde3e16SIlpo Järvinen { 11630dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 11640dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 11650dde3e16SIlpo Järvinen skb->tail += len; 11660dde3e16SIlpo Järvinen skb->len += len; 11670dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 11680dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 11690dde3e16SIlpo Järvinen return tmp; 11700dde3e16SIlpo Järvinen } 11710dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 11720dde3e16SIlpo Järvinen 11736be8ac2fSIlpo Järvinen /** 1174c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1175c2aa270aSIlpo Järvinen * @skb: buffer to use 1176c2aa270aSIlpo Järvinen * @len: amount of data to add 1177c2aa270aSIlpo Järvinen * 1178c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1179c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1180c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1181c2aa270aSIlpo Järvinen */ 1182c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1183c2aa270aSIlpo Järvinen { 1184c2aa270aSIlpo Järvinen skb->data -= len; 1185c2aa270aSIlpo Järvinen skb->len += len; 1186c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1187c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1188c2aa270aSIlpo Järvinen return skb->data; 1189c2aa270aSIlpo Järvinen } 1190c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1191c2aa270aSIlpo Järvinen 1192c2aa270aSIlpo Järvinen /** 11936be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 11946be8ac2fSIlpo Järvinen * @skb: buffer to use 11956be8ac2fSIlpo Järvinen * @len: amount of data to remove 11966be8ac2fSIlpo Järvinen * 11976be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 11986be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 11996be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 12006be8ac2fSIlpo Järvinen * the old data. 12016be8ac2fSIlpo Järvinen */ 12026be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 12036be8ac2fSIlpo Järvinen { 120447d29646SDavid S. Miller return skb_pull_inline(skb, len); 12056be8ac2fSIlpo Järvinen } 12066be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 12076be8ac2fSIlpo Järvinen 1208419ae74eSIlpo Järvinen /** 1209419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1210419ae74eSIlpo Järvinen * @skb: buffer to alter 1211419ae74eSIlpo Järvinen * @len: new length 1212419ae74eSIlpo Järvinen * 1213419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1214419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1215419ae74eSIlpo Järvinen * The skb must be linear. 1216419ae74eSIlpo Järvinen */ 1217419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1218419ae74eSIlpo Järvinen { 1219419ae74eSIlpo Järvinen if (skb->len > len) 1220419ae74eSIlpo Järvinen __skb_trim(skb, len); 1221419ae74eSIlpo Järvinen } 1222419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1223419ae74eSIlpo Järvinen 12243cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 12251da177e4SLinus Torvalds */ 12261da177e4SLinus Torvalds 12273cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 12281da177e4SLinus Torvalds { 122927b437c8SHerbert Xu struct sk_buff **fragp; 123027b437c8SHerbert Xu struct sk_buff *frag; 12311da177e4SLinus Torvalds int offset = skb_headlen(skb); 12321da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 12331da177e4SLinus Torvalds int i; 123427b437c8SHerbert Xu int err; 123527b437c8SHerbert Xu 123627b437c8SHerbert Xu if (skb_cloned(skb) && 123727b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 123827b437c8SHerbert Xu return err; 12391da177e4SLinus Torvalds 1240f4d26fb3SHerbert Xu i = 0; 1241f4d26fb3SHerbert Xu if (offset >= len) 1242f4d26fb3SHerbert Xu goto drop_pages; 1243f4d26fb3SHerbert Xu 1244f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 12459e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 124627b437c8SHerbert Xu 124727b437c8SHerbert Xu if (end < len) { 12481da177e4SLinus Torvalds offset = end; 124927b437c8SHerbert Xu continue; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 12529e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 125327b437c8SHerbert Xu 1254f4d26fb3SHerbert Xu drop_pages: 125527b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 125627b437c8SHerbert Xu 125727b437c8SHerbert Xu for (; i < nfrags; i++) 1258ea2ab693SIan Campbell skb_frag_unref(skb, i); 125927b437c8SHerbert Xu 126021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 126127b437c8SHerbert Xu skb_drop_fraglist(skb); 1262f4d26fb3SHerbert Xu goto done; 126327b437c8SHerbert Xu } 126427b437c8SHerbert Xu 126527b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 126627b437c8SHerbert Xu fragp = &frag->next) { 126727b437c8SHerbert Xu int end = offset + frag->len; 126827b437c8SHerbert Xu 126927b437c8SHerbert Xu if (skb_shared(frag)) { 127027b437c8SHerbert Xu struct sk_buff *nfrag; 127127b437c8SHerbert Xu 127227b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 127327b437c8SHerbert Xu if (unlikely(!nfrag)) 127427b437c8SHerbert Xu return -ENOMEM; 127527b437c8SHerbert Xu 127627b437c8SHerbert Xu nfrag->next = frag->next; 127785bb2a60SEric Dumazet consume_skb(frag); 127827b437c8SHerbert Xu frag = nfrag; 127927b437c8SHerbert Xu *fragp = frag; 128027b437c8SHerbert Xu } 128127b437c8SHerbert Xu 128227b437c8SHerbert Xu if (end < len) { 128327b437c8SHerbert Xu offset = end; 128427b437c8SHerbert Xu continue; 128527b437c8SHerbert Xu } 128627b437c8SHerbert Xu 128727b437c8SHerbert Xu if (end > len && 128827b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 128927b437c8SHerbert Xu return err; 129027b437c8SHerbert Xu 129127b437c8SHerbert Xu if (frag->next) 129227b437c8SHerbert Xu skb_drop_list(&frag->next); 129327b437c8SHerbert Xu break; 129427b437c8SHerbert Xu } 129527b437c8SHerbert Xu 1296f4d26fb3SHerbert Xu done: 129727b437c8SHerbert Xu if (len > skb_headlen(skb)) { 12981da177e4SLinus Torvalds skb->data_len -= skb->len - len; 12991da177e4SLinus Torvalds skb->len = len; 13001da177e4SLinus Torvalds } else { 13011da177e4SLinus Torvalds skb->len = len; 13021da177e4SLinus Torvalds skb->data_len = 0; 130327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds return 0; 13071da177e4SLinus Torvalds } 1308b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /** 13111da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 13121da177e4SLinus Torvalds * @skb: buffer to reallocate 13131da177e4SLinus Torvalds * @delta: number of bytes to advance tail 13141da177e4SLinus Torvalds * 13151da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 13161da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 13171da177e4SLinus Torvalds * data from fragmented part. 13181da177e4SLinus Torvalds * 13191da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 13201da177e4SLinus Torvalds * 13211da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 13221da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 13231da177e4SLinus Torvalds * 13241da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 13251da177e4SLinus Torvalds * reloaded after call to this function. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 13291da177e4SLinus Torvalds * when it is necessary. 13301da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 13311da177e4SLinus Torvalds * 2. It may change skb pointers. 13321da177e4SLinus Torvalds * 13331da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 13341da177e4SLinus Torvalds */ 13351da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 13361da177e4SLinus Torvalds { 13371da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 13381da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 13391da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 13401da177e4SLinus Torvalds */ 13414305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 13441da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 13451da177e4SLinus Torvalds GFP_ATOMIC)) 13461da177e4SLinus Torvalds return NULL; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 134927a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 13501da177e4SLinus Torvalds BUG(); 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 13531da177e4SLinus Torvalds * size of pulled pages. Superb. 13541da177e4SLinus Torvalds */ 135521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 13561da177e4SLinus Torvalds goto pull_pages; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 13591da177e4SLinus Torvalds eat = delta; 13601da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 13619e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 13629e903e08SEric Dumazet 13639e903e08SEric Dumazet if (size >= eat) 13641da177e4SLinus Torvalds goto pull_pages; 13659e903e08SEric Dumazet eat -= size; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 13691da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 13701da177e4SLinus Torvalds * but taking into account that pulling is expected to 13711da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 13721da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 13731da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 13741da177e4SLinus Torvalds */ 13751da177e4SLinus Torvalds if (eat) { 13761da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 13771da177e4SLinus Torvalds struct sk_buff *clone = NULL; 13781da177e4SLinus Torvalds struct sk_buff *insp = NULL; 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds do { 138109a62660SKris Katterjohn BUG_ON(!list); 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds if (list->len <= eat) { 13841da177e4SLinus Torvalds /* Eaten as whole. */ 13851da177e4SLinus Torvalds eat -= list->len; 13861da177e4SLinus Torvalds list = list->next; 13871da177e4SLinus Torvalds insp = list; 13881da177e4SLinus Torvalds } else { 13891da177e4SLinus Torvalds /* Eaten partially. */ 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if (skb_shared(list)) { 13921da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 13931da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 13941da177e4SLinus Torvalds if (!clone) 13951da177e4SLinus Torvalds return NULL; 13961da177e4SLinus Torvalds insp = list->next; 13971da177e4SLinus Torvalds list = clone; 13981da177e4SLinus Torvalds } else { 13991da177e4SLinus Torvalds /* This may be pulled without 14001da177e4SLinus Torvalds * problems. */ 14011da177e4SLinus Torvalds insp = list; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 14041da177e4SLinus Torvalds kfree_skb(clone); 14051da177e4SLinus Torvalds return NULL; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds break; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds } while (eat); 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /* Free pulled out fragments. */ 14121da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 14131da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 14141da177e4SLinus Torvalds kfree_skb(list); 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds /* And insert new clone at head. */ 14171da177e4SLinus Torvalds if (clone) { 14181da177e4SLinus Torvalds clone->next = list; 14191da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds pull_pages: 14251da177e4SLinus Torvalds eat = delta; 14261da177e4SLinus Torvalds k = 0; 14271da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14289e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 14299e903e08SEric Dumazet 14309e903e08SEric Dumazet if (size <= eat) { 1431ea2ab693SIan Campbell skb_frag_unref(skb, i); 14329e903e08SEric Dumazet eat -= size; 14331da177e4SLinus Torvalds } else { 14341da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 14351da177e4SLinus Torvalds if (eat) { 14361da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 14379e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 14381da177e4SLinus Torvalds eat = 0; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds k++; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds skb->tail += delta; 14461da177e4SLinus Torvalds skb->data_len -= delta; 14471da177e4SLinus Torvalds 144827a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 14491da177e4SLinus Torvalds } 1450b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 14511da177e4SLinus Torvalds 145222019b17SEric Dumazet /** 145322019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 145422019b17SEric Dumazet * @skb: source skb 145522019b17SEric Dumazet * @offset: offset in source 145622019b17SEric Dumazet * @to: destination buffer 145722019b17SEric Dumazet * @len: number of bytes to copy 145822019b17SEric Dumazet * 145922019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 146022019b17SEric Dumazet * destination buffer. 146122019b17SEric Dumazet * 146222019b17SEric Dumazet * CAUTION ! : 146322019b17SEric Dumazet * If its prototype is ever changed, 146422019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 146522019b17SEric Dumazet * since it is called from BPF assembly code. 146622019b17SEric Dumazet */ 14671da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 14681da177e4SLinus Torvalds { 14691a028e50SDavid S. Miller int start = skb_headlen(skb); 1470fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1471fbb398a8SDavid S. Miller int i, copy; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds if (offset > (int)skb->len - len) 14741da177e4SLinus Torvalds goto fault; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* Copy header. */ 14771a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 14781da177e4SLinus Torvalds if (copy > len) 14791da177e4SLinus Torvalds copy = len; 1480d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 14811da177e4SLinus Torvalds if ((len -= copy) == 0) 14821da177e4SLinus Torvalds return 0; 14831da177e4SLinus Torvalds offset += copy; 14841da177e4SLinus Torvalds to += copy; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14881a028e50SDavid S. Miller int end; 148951c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 14901da177e4SLinus Torvalds 1491547b792cSIlpo Järvinen WARN_ON(start > offset + len); 14921a028e50SDavid S. Miller 149351c56b00SEric Dumazet end = start + skb_frag_size(f); 14941da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 14951da177e4SLinus Torvalds u8 *vaddr; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds if (copy > len) 14981da177e4SLinus Torvalds copy = len; 14991da177e4SLinus Torvalds 150051c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 15011da177e4SLinus Torvalds memcpy(to, 150251c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 150351c56b00SEric Dumazet copy); 150451c56b00SEric Dumazet kunmap_atomic(vaddr); 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds if ((len -= copy) == 0) 15071da177e4SLinus Torvalds return 0; 15081da177e4SLinus Torvalds offset += copy; 15091da177e4SLinus Torvalds to += copy; 15101da177e4SLinus Torvalds } 15111a028e50SDavid S. Miller start = end; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 1514fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 15151a028e50SDavid S. Miller int end; 15161da177e4SLinus Torvalds 1517547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15181a028e50SDavid S. Miller 1519fbb398a8SDavid S. Miller end = start + frag_iter->len; 15201da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15211da177e4SLinus Torvalds if (copy > len) 15221da177e4SLinus Torvalds copy = len; 1523fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 15241da177e4SLinus Torvalds goto fault; 15251da177e4SLinus Torvalds if ((len -= copy) == 0) 15261da177e4SLinus Torvalds return 0; 15271da177e4SLinus Torvalds offset += copy; 15281da177e4SLinus Torvalds to += copy; 15291da177e4SLinus Torvalds } 15301a028e50SDavid S. Miller start = end; 15311da177e4SLinus Torvalds } 1532a6686f2fSShirley Ma 15331da177e4SLinus Torvalds if (!len) 15341da177e4SLinus Torvalds return 0; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds fault: 15371da177e4SLinus Torvalds return -EFAULT; 15381da177e4SLinus Torvalds } 1539b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 15401da177e4SLinus Torvalds 15419c55e01cSJens Axboe /* 15429c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 15439c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 15449c55e01cSJens Axboe */ 15459c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 15469c55e01cSJens Axboe { 15478b9d3728SJarek Poplawski put_page(spd->pages[i]); 15488b9d3728SJarek Poplawski } 15499c55e01cSJens Axboe 1550a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 15514fb66994SJarek Poplawski unsigned int *offset, 15527a67e56fSJarek Poplawski struct sk_buff *skb, struct sock *sk) 15538b9d3728SJarek Poplawski { 15544fb66994SJarek Poplawski struct page *p = sk->sk_sndmsg_page; 15554fb66994SJarek Poplawski unsigned int off; 15568b9d3728SJarek Poplawski 15574fb66994SJarek Poplawski if (!p) { 15584fb66994SJarek Poplawski new_page: 15594fb66994SJarek Poplawski p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0); 15608b9d3728SJarek Poplawski if (!p) 15618b9d3728SJarek Poplawski return NULL; 15624fb66994SJarek Poplawski 15634fb66994SJarek Poplawski off = sk->sk_sndmsg_off = 0; 15644fb66994SJarek Poplawski /* hold one ref to this page until it's full */ 15654fb66994SJarek Poplawski } else { 15664fb66994SJarek Poplawski unsigned int mlen; 15674fb66994SJarek Poplawski 1568e66e9a31SEric Dumazet /* If we are the only user of the page, we can reset offset */ 1569e66e9a31SEric Dumazet if (page_count(p) == 1) 1570e66e9a31SEric Dumazet sk->sk_sndmsg_off = 0; 15714fb66994SJarek Poplawski off = sk->sk_sndmsg_off; 15724fb66994SJarek Poplawski mlen = PAGE_SIZE - off; 15734fb66994SJarek Poplawski if (mlen < 64 && mlen < *len) { 15744fb66994SJarek Poplawski put_page(p); 15754fb66994SJarek Poplawski goto new_page; 15764fb66994SJarek Poplawski } 15774fb66994SJarek Poplawski 15784fb66994SJarek Poplawski *len = min_t(unsigned int, *len, mlen); 15794fb66994SJarek Poplawski } 15804fb66994SJarek Poplawski 15814fb66994SJarek Poplawski memcpy(page_address(p) + off, page_address(page) + *offset, *len); 15824fb66994SJarek Poplawski sk->sk_sndmsg_off += *len; 15834fb66994SJarek Poplawski *offset = off; 15848b9d3728SJarek Poplawski 15858b9d3728SJarek Poplawski return p; 15869c55e01cSJens Axboe } 15879c55e01cSJens Axboe 158841c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 158941c73a0dSEric Dumazet struct page *page, 159041c73a0dSEric Dumazet unsigned int offset) 159141c73a0dSEric Dumazet { 159241c73a0dSEric Dumazet return spd->nr_pages && 159341c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 159441c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 159541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 159641c73a0dSEric Dumazet } 159741c73a0dSEric Dumazet 15989c55e01cSJens Axboe /* 15999c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 16009c55e01cSJens Axboe */ 1601a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 160235f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 16034fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 16047a67e56fSJarek Poplawski struct sk_buff *skb, int linear, 16057a67e56fSJarek Poplawski struct sock *sk) 16069c55e01cSJens Axboe { 160741c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1608a108d5f3SDavid S. Miller return true; 16099c55e01cSJens Axboe 16108b9d3728SJarek Poplawski if (linear) { 16117a67e56fSJarek Poplawski page = linear_to_page(page, len, &offset, skb, sk); 16128b9d3728SJarek Poplawski if (!page) 1613a108d5f3SDavid S. Miller return true; 161441c73a0dSEric Dumazet } 161541c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 161641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1617a108d5f3SDavid S. Miller return false; 161841c73a0dSEric Dumazet } 16198b9d3728SJarek Poplawski get_page(page); 16209c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 16214fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 16229c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 16239c55e01cSJens Axboe spd->nr_pages++; 16248b9d3728SJarek Poplawski 1625a108d5f3SDavid S. Miller return false; 16269c55e01cSJens Axboe } 16279c55e01cSJens Axboe 16282870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 16292870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 16302870c43dSOctavian Purdila { 1631ce3dd395SJarek Poplawski unsigned long n; 1632ce3dd395SJarek Poplawski 16332870c43dSOctavian Purdila *poff += off; 1634ce3dd395SJarek Poplawski n = *poff / PAGE_SIZE; 1635ce3dd395SJarek Poplawski if (n) 1636ce3dd395SJarek Poplawski *page = nth_page(*page, n); 1637ce3dd395SJarek Poplawski 16382870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 16392870c43dSOctavian Purdila *plen -= off; 16402870c43dSOctavian Purdila } 16412870c43dSOctavian Purdila 1642a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 16432870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 16442870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 16457a67e56fSJarek Poplawski struct splice_pipe_desc *spd, int linear, 164635f3d14dSJens Axboe struct sock *sk, 164735f3d14dSJens Axboe struct pipe_inode_info *pipe) 16489c55e01cSJens Axboe { 16492870c43dSOctavian Purdila if (!*len) 1650a108d5f3SDavid S. Miller return true; 16519c55e01cSJens Axboe 16522870c43dSOctavian Purdila /* skip this segment if already processed */ 16532870c43dSOctavian Purdila if (*off >= plen) { 16542870c43dSOctavian Purdila *off -= plen; 1655a108d5f3SDavid S. Miller return false; 16562870c43dSOctavian Purdila } 16572870c43dSOctavian Purdila 16582870c43dSOctavian Purdila /* ignore any bits we already processed */ 16592870c43dSOctavian Purdila if (*off) { 16602870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 16612870c43dSOctavian Purdila *off = 0; 16622870c43dSOctavian Purdila } 16632870c43dSOctavian Purdila 16642870c43dSOctavian Purdila do { 16652870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 16662870c43dSOctavian Purdila 16672870c43dSOctavian Purdila /* the linear region may spread across several pages */ 16682870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 16692870c43dSOctavian Purdila 167035f3d14dSJens Axboe if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk)) 1671a108d5f3SDavid S. Miller return true; 16722870c43dSOctavian Purdila 16732870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 16742870c43dSOctavian Purdila *len -= flen; 16752870c43dSOctavian Purdila 16762870c43dSOctavian Purdila } while (*len && plen); 16772870c43dSOctavian Purdila 1678a108d5f3SDavid S. Miller return false; 1679db43a282SOctavian Purdila } 16809c55e01cSJens Axboe 16819c55e01cSJens Axboe /* 1682a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 16832870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 16849c55e01cSJens Axboe */ 1685a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 168635f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 168735f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 16882870c43dSOctavian Purdila { 16892870c43dSOctavian Purdila int seg; 16909c55e01cSJens Axboe 16919c55e01cSJens Axboe /* 16922870c43dSOctavian Purdila * map the linear part 16939c55e01cSJens Axboe */ 16942870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 16952870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 16962870c43dSOctavian Purdila skb_headlen(skb), 169735f3d14dSJens Axboe offset, len, skb, spd, 1, sk, pipe)) 1698a108d5f3SDavid S. Miller return true; 16999c55e01cSJens Axboe 17009c55e01cSJens Axboe /* 17019c55e01cSJens Axboe * then map the fragments 17029c55e01cSJens Axboe */ 17039c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 17049c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 17059c55e01cSJens Axboe 1706ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 17079e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 170835f3d14dSJens Axboe offset, len, skb, spd, 0, sk, pipe)) 1709a108d5f3SDavid S. Miller return true; 17109c55e01cSJens Axboe } 17119c55e01cSJens Axboe 1712a108d5f3SDavid S. Miller return false; 17139c55e01cSJens Axboe } 17149c55e01cSJens Axboe 17159c55e01cSJens Axboe /* 17169c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 17179c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 17189c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 17199c55e01cSJens Axboe * handle that cleanly. 17209c55e01cSJens Axboe */ 17218b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 17229c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 17239c55e01cSJens Axboe unsigned int flags) 17249c55e01cSJens Axboe { 172541c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 172641c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 17279c55e01cSJens Axboe struct splice_pipe_desc spd = { 17289c55e01cSJens Axboe .pages = pages, 17299c55e01cSJens Axboe .partial = partial, 17309c55e01cSJens Axboe .flags = flags, 17319c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 17329c55e01cSJens Axboe .spd_release = sock_spd_release, 17339c55e01cSJens Axboe }; 1734fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 17357a67e56fSJarek Poplawski struct sock *sk = skb->sk; 173635f3d14dSJens Axboe int ret = 0; 173735f3d14dSJens Axboe 17389c55e01cSJens Axboe /* 17399c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 17409c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 17419c55e01cSJens Axboe */ 174235f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 17439c55e01cSJens Axboe goto done; 17449c55e01cSJens Axboe else if (!tlen) 17459c55e01cSJens Axboe goto done; 17469c55e01cSJens Axboe 17479c55e01cSJens Axboe /* 17489c55e01cSJens Axboe * now see if we have a frag_list to map 17499c55e01cSJens Axboe */ 1750fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1751fbb398a8SDavid S. Miller if (!tlen) 17529c55e01cSJens Axboe break; 175335f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1754fbb398a8SDavid S. Miller break; 17559c55e01cSJens Axboe } 17569c55e01cSJens Axboe 17579c55e01cSJens Axboe done: 17589c55e01cSJens Axboe if (spd.nr_pages) { 17599c55e01cSJens Axboe /* 17609c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 17619c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 17629c55e01cSJens Axboe * here as compared to sendfile(). We enter here 17639c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 17649c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 17659c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 17669c55e01cSJens Axboe * and networking will grab the socket lock. 17679c55e01cSJens Axboe */ 1768293ad604SOctavian Purdila release_sock(sk); 17699c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1770293ad604SOctavian Purdila lock_sock(sk); 17719c55e01cSJens Axboe } 17729c55e01cSJens Axboe 177335f3d14dSJens Axboe return ret; 17749c55e01cSJens Axboe } 17759c55e01cSJens Axboe 1776357b40a1SHerbert Xu /** 1777357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1778357b40a1SHerbert Xu * @skb: destination buffer 1779357b40a1SHerbert Xu * @offset: offset in destination 1780357b40a1SHerbert Xu * @from: source buffer 1781357b40a1SHerbert Xu * @len: number of bytes to copy 1782357b40a1SHerbert Xu * 1783357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1784357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1785357b40a1SHerbert Xu * traversing fragment lists and such. 1786357b40a1SHerbert Xu */ 1787357b40a1SHerbert Xu 17880c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1789357b40a1SHerbert Xu { 17901a028e50SDavid S. Miller int start = skb_headlen(skb); 1791fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1792fbb398a8SDavid S. Miller int i, copy; 1793357b40a1SHerbert Xu 1794357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1795357b40a1SHerbert Xu goto fault; 1796357b40a1SHerbert Xu 17971a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1798357b40a1SHerbert Xu if (copy > len) 1799357b40a1SHerbert Xu copy = len; 180027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1801357b40a1SHerbert Xu if ((len -= copy) == 0) 1802357b40a1SHerbert Xu return 0; 1803357b40a1SHerbert Xu offset += copy; 1804357b40a1SHerbert Xu from += copy; 1805357b40a1SHerbert Xu } 1806357b40a1SHerbert Xu 1807357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1808357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 18091a028e50SDavid S. Miller int end; 1810357b40a1SHerbert Xu 1811547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18121a028e50SDavid S. Miller 18139e903e08SEric Dumazet end = start + skb_frag_size(frag); 1814357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1815357b40a1SHerbert Xu u8 *vaddr; 1816357b40a1SHerbert Xu 1817357b40a1SHerbert Xu if (copy > len) 1818357b40a1SHerbert Xu copy = len; 1819357b40a1SHerbert Xu 182051c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 18211a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 18221a028e50SDavid S. Miller from, copy); 182351c56b00SEric Dumazet kunmap_atomic(vaddr); 1824357b40a1SHerbert Xu 1825357b40a1SHerbert Xu if ((len -= copy) == 0) 1826357b40a1SHerbert Xu return 0; 1827357b40a1SHerbert Xu offset += copy; 1828357b40a1SHerbert Xu from += copy; 1829357b40a1SHerbert Xu } 18301a028e50SDavid S. Miller start = end; 1831357b40a1SHerbert Xu } 1832357b40a1SHerbert Xu 1833fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 18341a028e50SDavid S. Miller int end; 1835357b40a1SHerbert Xu 1836547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18371a028e50SDavid S. Miller 1838fbb398a8SDavid S. Miller end = start + frag_iter->len; 1839357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1840357b40a1SHerbert Xu if (copy > len) 1841357b40a1SHerbert Xu copy = len; 1842fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 18431a028e50SDavid S. Miller from, copy)) 1844357b40a1SHerbert Xu goto fault; 1845357b40a1SHerbert Xu if ((len -= copy) == 0) 1846357b40a1SHerbert Xu return 0; 1847357b40a1SHerbert Xu offset += copy; 1848357b40a1SHerbert Xu from += copy; 1849357b40a1SHerbert Xu } 18501a028e50SDavid S. Miller start = end; 1851357b40a1SHerbert Xu } 1852357b40a1SHerbert Xu if (!len) 1853357b40a1SHerbert Xu return 0; 1854357b40a1SHerbert Xu 1855357b40a1SHerbert Xu fault: 1856357b40a1SHerbert Xu return -EFAULT; 1857357b40a1SHerbert Xu } 1858357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1859357b40a1SHerbert Xu 18601da177e4SLinus Torvalds /* Checksum skb data. */ 18611da177e4SLinus Torvalds 18622bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 18632bbbc868SAl Viro int len, __wsum csum) 18641da177e4SLinus Torvalds { 18651a028e50SDavid S. Miller int start = skb_headlen(skb); 18661a028e50SDavid S. Miller int i, copy = start - offset; 1867fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 18681da177e4SLinus Torvalds int pos = 0; 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /* Checksum header. */ 18711da177e4SLinus Torvalds if (copy > 0) { 18721da177e4SLinus Torvalds if (copy > len) 18731da177e4SLinus Torvalds copy = len; 18741da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 18751da177e4SLinus Torvalds if ((len -= copy) == 0) 18761da177e4SLinus Torvalds return csum; 18771da177e4SLinus Torvalds offset += copy; 18781da177e4SLinus Torvalds pos = copy; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 18821a028e50SDavid S. Miller int end; 188351c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 18841da177e4SLinus Torvalds 1885547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18861a028e50SDavid S. Miller 188751c56b00SEric Dumazet end = start + skb_frag_size(frag); 18881da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 188944bb9363SAl Viro __wsum csum2; 18901da177e4SLinus Torvalds u8 *vaddr; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds if (copy > len) 18931da177e4SLinus Torvalds copy = len; 189451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 18951a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 18961a028e50SDavid S. Miller offset - start, copy, 0); 189751c56b00SEric Dumazet kunmap_atomic(vaddr); 18981da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 18991da177e4SLinus Torvalds if (!(len -= copy)) 19001da177e4SLinus Torvalds return csum; 19011da177e4SLinus Torvalds offset += copy; 19021da177e4SLinus Torvalds pos += copy; 19031da177e4SLinus Torvalds } 19041a028e50SDavid S. Miller start = end; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 1907fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19081a028e50SDavid S. Miller int end; 19091da177e4SLinus Torvalds 1910547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19111a028e50SDavid S. Miller 1912fbb398a8SDavid S. Miller end = start + frag_iter->len; 19131da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19145f92a738SAl Viro __wsum csum2; 19151da177e4SLinus Torvalds if (copy > len) 19161da177e4SLinus Torvalds copy = len; 1917fbb398a8SDavid S. Miller csum2 = skb_checksum(frag_iter, offset - start, 19181a028e50SDavid S. Miller copy, 0); 19191da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19201da177e4SLinus Torvalds if ((len -= copy) == 0) 19211da177e4SLinus Torvalds return csum; 19221da177e4SLinus Torvalds offset += copy; 19231da177e4SLinus Torvalds pos += copy; 19241da177e4SLinus Torvalds } 19251a028e50SDavid S. Miller start = end; 19261da177e4SLinus Torvalds } 192709a62660SKris Katterjohn BUG_ON(len); 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds return csum; 19301da177e4SLinus Torvalds } 1931b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds /* Both of above in one bottle. */ 19341da177e4SLinus Torvalds 193581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 193681d77662SAl Viro u8 *to, int len, __wsum csum) 19371da177e4SLinus Torvalds { 19381a028e50SDavid S. Miller int start = skb_headlen(skb); 19391a028e50SDavid S. Miller int i, copy = start - offset; 1940fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19411da177e4SLinus Torvalds int pos = 0; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* Copy header. */ 19441da177e4SLinus Torvalds if (copy > 0) { 19451da177e4SLinus Torvalds if (copy > len) 19461da177e4SLinus Torvalds copy = len; 19471da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 19481da177e4SLinus Torvalds copy, csum); 19491da177e4SLinus Torvalds if ((len -= copy) == 0) 19501da177e4SLinus Torvalds return csum; 19511da177e4SLinus Torvalds offset += copy; 19521da177e4SLinus Torvalds to += copy; 19531da177e4SLinus Torvalds pos = copy; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19571a028e50SDavid S. Miller int end; 19581da177e4SLinus Torvalds 1959547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19601a028e50SDavid S. Miller 19619e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 19621da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19635084205fSAl Viro __wsum csum2; 19641da177e4SLinus Torvalds u8 *vaddr; 19651da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds if (copy > len) 19681da177e4SLinus Torvalds copy = len; 196951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19701da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 19711a028e50SDavid S. Miller frag->page_offset + 19721a028e50SDavid S. Miller offset - start, to, 19731a028e50SDavid S. Miller copy, 0); 197451c56b00SEric Dumazet kunmap_atomic(vaddr); 19751da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19761da177e4SLinus Torvalds if (!(len -= copy)) 19771da177e4SLinus Torvalds return csum; 19781da177e4SLinus Torvalds offset += copy; 19791da177e4SLinus Torvalds to += copy; 19801da177e4SLinus Torvalds pos += copy; 19811da177e4SLinus Torvalds } 19821a028e50SDavid S. Miller start = end; 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 1985fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 198681d77662SAl Viro __wsum csum2; 19871a028e50SDavid S. Miller int end; 19881da177e4SLinus Torvalds 1989547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19901a028e50SDavid S. Miller 1991fbb398a8SDavid S. Miller end = start + frag_iter->len; 19921da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19931da177e4SLinus Torvalds if (copy > len) 19941da177e4SLinus Torvalds copy = len; 1995fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 19961a028e50SDavid S. Miller offset - start, 19971da177e4SLinus Torvalds to, copy, 0); 19981da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19991da177e4SLinus Torvalds if ((len -= copy) == 0) 20001da177e4SLinus Torvalds return csum; 20011da177e4SLinus Torvalds offset += copy; 20021da177e4SLinus Torvalds to += copy; 20031da177e4SLinus Torvalds pos += copy; 20041da177e4SLinus Torvalds } 20051a028e50SDavid S. Miller start = end; 20061da177e4SLinus Torvalds } 200709a62660SKris Katterjohn BUG_ON(len); 20081da177e4SLinus Torvalds return csum; 20091da177e4SLinus Torvalds } 2010b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 20131da177e4SLinus Torvalds { 2014d3bc23e7SAl Viro __wsum csum; 20151da177e4SLinus Torvalds long csstart; 20161da177e4SLinus Torvalds 201784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 201855508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 20191da177e4SLinus Torvalds else 20201da177e4SLinus Torvalds csstart = skb_headlen(skb); 20211da177e4SLinus Torvalds 202209a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 20231da177e4SLinus Torvalds 2024d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds csum = 0; 20271da177e4SLinus Torvalds if (csstart != skb->len) 20281da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 20291da177e4SLinus Torvalds skb->len - csstart, 0); 20301da177e4SLinus Torvalds 203184fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2032ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 20331da177e4SLinus Torvalds 2034d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 20351da177e4SLinus Torvalds } 20361da177e4SLinus Torvalds } 2037b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds /** 20401da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 20411da177e4SLinus Torvalds * @list: list to dequeue from 20421da177e4SLinus Torvalds * 20431da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 20441da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 20451da177e4SLinus Torvalds * returned or %NULL if the list is empty. 20461da177e4SLinus Torvalds */ 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 20491da177e4SLinus Torvalds { 20501da177e4SLinus Torvalds unsigned long flags; 20511da177e4SLinus Torvalds struct sk_buff *result; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 20541da177e4SLinus Torvalds result = __skb_dequeue(list); 20551da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 20561da177e4SLinus Torvalds return result; 20571da177e4SLinus Torvalds } 2058b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /** 20611da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 20621da177e4SLinus Torvalds * @list: list to dequeue from 20631da177e4SLinus Torvalds * 20641da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 20651da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 20661da177e4SLinus Torvalds * returned or %NULL if the list is empty. 20671da177e4SLinus Torvalds */ 20681da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 20691da177e4SLinus Torvalds { 20701da177e4SLinus Torvalds unsigned long flags; 20711da177e4SLinus Torvalds struct sk_buff *result; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 20741da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 20751da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 20761da177e4SLinus Torvalds return result; 20771da177e4SLinus Torvalds } 2078b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /** 20811da177e4SLinus Torvalds * skb_queue_purge - empty a list 20821da177e4SLinus Torvalds * @list: list to empty 20831da177e4SLinus Torvalds * 20841da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 20851da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 20861da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 20871da177e4SLinus Torvalds */ 20881da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 20891da177e4SLinus Torvalds { 20901da177e4SLinus Torvalds struct sk_buff *skb; 20911da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 20921da177e4SLinus Torvalds kfree_skb(skb); 20931da177e4SLinus Torvalds } 2094b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /** 20971da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 20981da177e4SLinus Torvalds * @list: list to use 20991da177e4SLinus Torvalds * @newsk: buffer to queue 21001da177e4SLinus Torvalds * 21011da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 21021da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21031da177e4SLinus Torvalds * safely. 21041da177e4SLinus Torvalds * 21051da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21061da177e4SLinus Torvalds */ 21071da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 21081da177e4SLinus Torvalds { 21091da177e4SLinus Torvalds unsigned long flags; 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21121da177e4SLinus Torvalds __skb_queue_head(list, newsk); 21131da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21141da177e4SLinus Torvalds } 2115b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds /** 21181da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 21191da177e4SLinus Torvalds * @list: list to use 21201da177e4SLinus Torvalds * @newsk: buffer to queue 21211da177e4SLinus Torvalds * 21221da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 21231da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21241da177e4SLinus Torvalds * safely. 21251da177e4SLinus Torvalds * 21261da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21271da177e4SLinus Torvalds */ 21281da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 21291da177e4SLinus Torvalds { 21301da177e4SLinus Torvalds unsigned long flags; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21331da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 21341da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21351da177e4SLinus Torvalds } 2136b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 21378728b834SDavid S. Miller 21381da177e4SLinus Torvalds /** 21391da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 21401da177e4SLinus Torvalds * @skb: buffer to remove 21418728b834SDavid S. Miller * @list: list to use 21421da177e4SLinus Torvalds * 21438728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 21448728b834SDavid S. Miller * function is atomic with respect to other list locked calls 21451da177e4SLinus Torvalds * 21468728b834SDavid S. Miller * You must know what list the SKB is on. 21471da177e4SLinus Torvalds */ 21488728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 21491da177e4SLinus Torvalds { 21501da177e4SLinus Torvalds unsigned long flags; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21538728b834SDavid S. Miller __skb_unlink(skb, list); 21541da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21551da177e4SLinus Torvalds } 2156b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds /** 21591da177e4SLinus Torvalds * skb_append - append a buffer 21601da177e4SLinus Torvalds * @old: buffer to insert after 21611da177e4SLinus Torvalds * @newsk: buffer to insert 21628728b834SDavid S. Miller * @list: list to use 21631da177e4SLinus Torvalds * 21641da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 21651da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 21661da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21671da177e4SLinus Torvalds */ 21688728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 21691da177e4SLinus Torvalds { 21701da177e4SLinus Torvalds unsigned long flags; 21711da177e4SLinus Torvalds 21728728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 21737de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 21748728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 21751da177e4SLinus Torvalds } 2176b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds /** 21791da177e4SLinus Torvalds * skb_insert - insert a buffer 21801da177e4SLinus Torvalds * @old: buffer to insert before 21811da177e4SLinus Torvalds * @newsk: buffer to insert 21828728b834SDavid S. Miller * @list: list to use 21831da177e4SLinus Torvalds * 21848728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 21858728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 21868728b834SDavid S. Miller * calls. 21878728b834SDavid S. Miller * 21881da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21891da177e4SLinus Torvalds */ 21908728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 21911da177e4SLinus Torvalds { 21921da177e4SLinus Torvalds unsigned long flags; 21931da177e4SLinus Torvalds 21948728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 21958728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 21968728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 21971da177e4SLinus Torvalds } 2198b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 22011da177e4SLinus Torvalds struct sk_buff* skb1, 22021da177e4SLinus Torvalds const u32 len, const int pos) 22031da177e4SLinus Torvalds { 22041da177e4SLinus Torvalds int i; 22051da177e4SLinus Torvalds 2206d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2207d626f62bSArnaldo Carvalho de Melo pos - len); 22081da177e4SLinus Torvalds /* And move data appendix as is. */ 22091da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 22101da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 22131da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22141da177e4SLinus Torvalds skb1->data_len = skb->data_len; 22151da177e4SLinus Torvalds skb1->len += skb1->data_len; 22161da177e4SLinus Torvalds skb->data_len = 0; 22171da177e4SLinus Torvalds skb->len = len; 221827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 22221da177e4SLinus Torvalds struct sk_buff* skb1, 22231da177e4SLinus Torvalds const u32 len, int pos) 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds int i, k = 0; 22261da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22291da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 22301da177e4SLinus Torvalds skb->len = len; 22311da177e4SLinus Torvalds skb->data_len = len - pos; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 22349e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds if (pos + size > len) { 22371da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds if (pos < len) { 22401da177e4SLinus Torvalds /* Split frag. 22411da177e4SLinus Torvalds * We have two variants in this case: 22421da177e4SLinus Torvalds * 1. Move all the frag to the second 22431da177e4SLinus Torvalds * part, if it is possible. F.e. 22441da177e4SLinus Torvalds * this approach is mandatory for TUX, 22451da177e4SLinus Torvalds * where splitting is expensive. 22461da177e4SLinus Torvalds * 2. Split is accurately. We make this. 22471da177e4SLinus Torvalds */ 2248ea2ab693SIan Campbell skb_frag_ref(skb, i); 22491da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 22509e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 22519e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 22521da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds k++; 22551da177e4SLinus Torvalds } else 22561da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 22571da177e4SLinus Torvalds pos += size; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 22621da177e4SLinus Torvalds /** 22631da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 22641da177e4SLinus Torvalds * @skb: the buffer to split 22651da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 22661da177e4SLinus Torvalds * @len: new length for skb 22671da177e4SLinus Torvalds */ 22681da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 22691da177e4SLinus Torvalds { 22701da177e4SLinus Torvalds int pos = skb_headlen(skb); 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 22731da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 22741da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 22751da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 22761da177e4SLinus Torvalds } 2277b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 22781da177e4SLinus Torvalds 22799f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 22809f782db3SIlpo Järvinen * 22819f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 22829f782db3SIlpo Järvinen */ 2283832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2284832d11c5SIlpo Järvinen { 22850ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2286832d11c5SIlpo Järvinen } 2287832d11c5SIlpo Järvinen 2288832d11c5SIlpo Järvinen /** 2289832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2290832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2291832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2292832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2293832d11c5SIlpo Järvinen * 2294832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 229520e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2296832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2297832d11c5SIlpo Järvinen * 2298832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2299832d11c5SIlpo Järvinen * 2300832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2301832d11c5SIlpo Järvinen * to have non-paged data as well. 2302832d11c5SIlpo Järvinen * 2303832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2304832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2305832d11c5SIlpo Järvinen */ 2306832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2307832d11c5SIlpo Järvinen { 2308832d11c5SIlpo Järvinen int from, to, merge, todo; 2309832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2310832d11c5SIlpo Järvinen 2311832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2312832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2313832d11c5SIlpo Järvinen 2314832d11c5SIlpo Järvinen todo = shiftlen; 2315832d11c5SIlpo Järvinen from = 0; 2316832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2317832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2318832d11c5SIlpo Järvinen 2319832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2320832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2321832d11c5SIlpo Järvinen */ 2322832d11c5SIlpo Järvinen if (!to || 2323ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2324ea2ab693SIan Campbell fragfrom->page_offset)) { 2325832d11c5SIlpo Järvinen merge = -1; 2326832d11c5SIlpo Järvinen } else { 2327832d11c5SIlpo Järvinen merge = to - 1; 2328832d11c5SIlpo Järvinen 23299e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2330832d11c5SIlpo Järvinen if (todo < 0) { 2331832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2332832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2333832d11c5SIlpo Järvinen return 0; 2334832d11c5SIlpo Järvinen 23359f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 23369f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2337832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2338832d11c5SIlpo Järvinen 23399e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 23409e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2341832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2342832d11c5SIlpo Järvinen 2343832d11c5SIlpo Järvinen goto onlymerged; 2344832d11c5SIlpo Järvinen } 2345832d11c5SIlpo Järvinen 2346832d11c5SIlpo Järvinen from++; 2347832d11c5SIlpo Järvinen } 2348832d11c5SIlpo Järvinen 2349832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2350832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2351832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2352832d11c5SIlpo Järvinen return 0; 2353832d11c5SIlpo Järvinen 2354832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2355832d11c5SIlpo Järvinen return 0; 2356832d11c5SIlpo Järvinen 2357832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2358832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2359832d11c5SIlpo Järvinen return 0; 2360832d11c5SIlpo Järvinen 2361832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2362832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2363832d11c5SIlpo Järvinen 23649e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2365832d11c5SIlpo Järvinen *fragto = *fragfrom; 23669e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2367832d11c5SIlpo Järvinen from++; 2368832d11c5SIlpo Järvinen to++; 2369832d11c5SIlpo Järvinen 2370832d11c5SIlpo Järvinen } else { 2371ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2372832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2373832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 23749e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2375832d11c5SIlpo Järvinen 2376832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 23779e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2378832d11c5SIlpo Järvinen todo = 0; 2379832d11c5SIlpo Järvinen 2380832d11c5SIlpo Järvinen to++; 2381832d11c5SIlpo Järvinen break; 2382832d11c5SIlpo Järvinen } 2383832d11c5SIlpo Järvinen } 2384832d11c5SIlpo Järvinen 2385832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2386832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2387832d11c5SIlpo Järvinen 2388832d11c5SIlpo Järvinen if (merge >= 0) { 2389832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2390832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2391832d11c5SIlpo Järvinen 23929e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2393ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2394832d11c5SIlpo Järvinen } 2395832d11c5SIlpo Järvinen 2396832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2397832d11c5SIlpo Järvinen to = 0; 2398832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2399832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2400832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2401832d11c5SIlpo Järvinen 2402832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2403832d11c5SIlpo Järvinen 2404832d11c5SIlpo Järvinen onlymerged: 2405832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2406832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2407832d11c5SIlpo Järvinen */ 2408832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2409832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2410832d11c5SIlpo Järvinen 2411832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2412832d11c5SIlpo Järvinen skb->len -= shiftlen; 2413832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2414832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2415832d11c5SIlpo Järvinen tgt->len += shiftlen; 2416832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2417832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2418832d11c5SIlpo Järvinen 2419832d11c5SIlpo Järvinen return shiftlen; 2420832d11c5SIlpo Järvinen } 2421832d11c5SIlpo Järvinen 2422677e90edSThomas Graf /** 2423677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2424677e90edSThomas Graf * @skb: the buffer to read 2425677e90edSThomas Graf * @from: lower offset of data to be read 2426677e90edSThomas Graf * @to: upper offset of data to be read 2427677e90edSThomas Graf * @st: state variable 2428677e90edSThomas Graf * 2429677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2430677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2431677e90edSThomas Graf */ 2432677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2433677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2434677e90edSThomas Graf { 2435677e90edSThomas Graf st->lower_offset = from; 2436677e90edSThomas Graf st->upper_offset = to; 2437677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2438677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2439677e90edSThomas Graf st->frag_data = NULL; 2440677e90edSThomas Graf } 2441b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2442677e90edSThomas Graf 2443677e90edSThomas Graf /** 2444677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2445677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2446677e90edSThomas Graf * @data: destination pointer for data to be returned 2447677e90edSThomas Graf * @st: state variable 2448677e90edSThomas Graf * 2449677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 2450677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2451677e90edSThomas Graf * the head of the data block to &data and returns the length 2452677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2453677e90edSThomas Graf * offset has been reached. 2454677e90edSThomas Graf * 2455677e90edSThomas Graf * The caller is not required to consume all of the data 2456677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 2457677e90edSThomas Graf * of bytes already consumed and the next call to 2458677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2459677e90edSThomas Graf * 246025985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2461677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 2462677e90edSThomas Graf * reads of potentially non linear data. 2463677e90edSThomas Graf * 2464bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2465677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2466677e90edSThomas Graf * a stack for this purpose. 2467677e90edSThomas Graf */ 2468677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2469677e90edSThomas Graf struct skb_seq_state *st) 2470677e90edSThomas Graf { 2471677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2472677e90edSThomas Graf skb_frag_t *frag; 2473677e90edSThomas Graf 2474677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2475677e90edSThomas Graf return 0; 2476677e90edSThomas Graf 2477677e90edSThomas Graf next_skb: 247895e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2479677e90edSThomas Graf 2480995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 248195e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2482677e90edSThomas Graf return block_limit - abs_offset; 2483677e90edSThomas Graf } 2484677e90edSThomas Graf 2485677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2486677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2487677e90edSThomas Graf 2488677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2489677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 24909e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2491677e90edSThomas Graf 2492677e90edSThomas Graf if (abs_offset < block_limit) { 2493677e90edSThomas Graf if (!st->frag_data) 249451c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2495677e90edSThomas Graf 2496677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2497677e90edSThomas Graf (abs_offset - st->stepped_offset); 2498677e90edSThomas Graf 2499677e90edSThomas Graf return block_limit - abs_offset; 2500677e90edSThomas Graf } 2501677e90edSThomas Graf 2502677e90edSThomas Graf if (st->frag_data) { 250351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2504677e90edSThomas Graf st->frag_data = NULL; 2505677e90edSThomas Graf } 2506677e90edSThomas Graf 2507677e90edSThomas Graf st->frag_idx++; 25089e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2509677e90edSThomas Graf } 2510677e90edSThomas Graf 25115b5a60daSOlaf Kirch if (st->frag_data) { 251251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 25135b5a60daSOlaf Kirch st->frag_data = NULL; 25145b5a60daSOlaf Kirch } 25155b5a60daSOlaf Kirch 251621dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2517677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 251895e3b24cSHerbert Xu st->frag_idx = 0; 2519677e90edSThomas Graf goto next_skb; 252071b3346dSShyam Iyer } else if (st->cur_skb->next) { 252171b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 252271b3346dSShyam Iyer st->frag_idx = 0; 2523677e90edSThomas Graf goto next_skb; 2524677e90edSThomas Graf } 2525677e90edSThomas Graf 2526677e90edSThomas Graf return 0; 2527677e90edSThomas Graf } 2528b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2529677e90edSThomas Graf 2530677e90edSThomas Graf /** 2531677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2532677e90edSThomas Graf * @st: state variable 2533677e90edSThomas Graf * 2534677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2535677e90edSThomas Graf * returned 0. 2536677e90edSThomas Graf */ 2537677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2538677e90edSThomas Graf { 2539677e90edSThomas Graf if (st->frag_data) 254051c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2541677e90edSThomas Graf } 2542b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2543677e90edSThomas Graf 25443fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 25453fc7e8a6SThomas Graf 25463fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 25473fc7e8a6SThomas Graf struct ts_config *conf, 25483fc7e8a6SThomas Graf struct ts_state *state) 25493fc7e8a6SThomas Graf { 25503fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 25513fc7e8a6SThomas Graf } 25523fc7e8a6SThomas Graf 25533fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 25543fc7e8a6SThomas Graf { 25553fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 25563fc7e8a6SThomas Graf } 25573fc7e8a6SThomas Graf 25583fc7e8a6SThomas Graf /** 25593fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 25603fc7e8a6SThomas Graf * @skb: the buffer to look in 25613fc7e8a6SThomas Graf * @from: search offset 25623fc7e8a6SThomas Graf * @to: search limit 25633fc7e8a6SThomas Graf * @config: textsearch configuration 25643fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 25653fc7e8a6SThomas Graf * 25663fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 25673fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 25683fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 25693fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 25703fc7e8a6SThomas Graf */ 25713fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 25723fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 25733fc7e8a6SThomas Graf struct ts_state *state) 25743fc7e8a6SThomas Graf { 2575f72b948dSPhil Oester unsigned int ret; 2576f72b948dSPhil Oester 25773fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 25783fc7e8a6SThomas Graf config->finish = skb_ts_finish; 25793fc7e8a6SThomas Graf 25803fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 25813fc7e8a6SThomas Graf 2582f72b948dSPhil Oester ret = textsearch_find(config, state); 2583f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 25843fc7e8a6SThomas Graf } 2585b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 25863fc7e8a6SThomas Graf 2587e89e9cf5SAnanda Raju /** 2588e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 2589e89e9cf5SAnanda Raju * @sk: sock structure 2590e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2591e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2592e89e9cf5SAnanda Raju * @from: pointer to user message iov 2593e89e9cf5SAnanda Raju * @length: length of the iov message 2594e89e9cf5SAnanda Raju * 2595e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2596e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2597e89e9cf5SAnanda Raju */ 2598e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2599dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2600e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2601e89e9cf5SAnanda Raju void *from, int length) 2602e89e9cf5SAnanda Raju { 2603e89e9cf5SAnanda Raju int frg_cnt = 0; 2604e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2605e89e9cf5SAnanda Raju struct page *page = NULL; 2606e89e9cf5SAnanda Raju int copy, left; 2607e89e9cf5SAnanda Raju int offset = 0; 2608e89e9cf5SAnanda Raju int ret; 2609e89e9cf5SAnanda Raju 2610e89e9cf5SAnanda Raju do { 2611e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2612e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2613e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2614e89e9cf5SAnanda Raju return -EFAULT; 2615e89e9cf5SAnanda Raju 2616e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2617e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2618e89e9cf5SAnanda Raju 2619e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2620e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2621e89e9cf5SAnanda Raju */ 2622e89e9cf5SAnanda Raju if (page == NULL) 2623e89e9cf5SAnanda Raju return -ENOMEM; 2624e89e9cf5SAnanda Raju 2625e89e9cf5SAnanda Raju /* initialize the next frag */ 2626e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2627e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2628e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2629e89e9cf5SAnanda Raju 2630e89e9cf5SAnanda Raju /* get the new initialized frag */ 2631e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2632e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2633e89e9cf5SAnanda Raju 2634e89e9cf5SAnanda Raju /* copy the user data to page */ 2635e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2636e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2637e89e9cf5SAnanda Raju 26389e903e08SEric Dumazet ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag), 2639e89e9cf5SAnanda Raju offset, copy, 0, skb); 2640e89e9cf5SAnanda Raju if (ret < 0) 2641e89e9cf5SAnanda Raju return -EFAULT; 2642e89e9cf5SAnanda Raju 2643e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 26449e903e08SEric Dumazet skb_frag_size_add(frag, copy); 2645e89e9cf5SAnanda Raju skb->len += copy; 2646e89e9cf5SAnanda Raju skb->data_len += copy; 2647e89e9cf5SAnanda Raju offset += copy; 2648e89e9cf5SAnanda Raju length -= copy; 2649e89e9cf5SAnanda Raju 2650e89e9cf5SAnanda Raju } while (length > 0); 2651e89e9cf5SAnanda Raju 2652e89e9cf5SAnanda Raju return 0; 2653e89e9cf5SAnanda Raju } 2654b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2655e89e9cf5SAnanda Raju 2656cbb042f9SHerbert Xu /** 2657cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2658cbb042f9SHerbert Xu * @skb: buffer to update 2659cbb042f9SHerbert Xu * @len: length of data pulled 2660cbb042f9SHerbert Xu * 2661cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2662fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 266384fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 266484fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 266584fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2666cbb042f9SHerbert Xu */ 2667cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2668cbb042f9SHerbert Xu { 2669cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2670cbb042f9SHerbert Xu skb->len -= len; 2671cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2672cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2673cbb042f9SHerbert Xu return skb->data += len; 2674cbb042f9SHerbert Xu } 2675f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2676f94691acSArnaldo Carvalho de Melo 2677f4c50d99SHerbert Xu /** 2678f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2679f4c50d99SHerbert Xu * @skb: buffer to segment 2680576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2681f4c50d99SHerbert Xu * 2682f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 26834c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 26844c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2685f4c50d99SHerbert Xu */ 2686c8f44affSMichał Mirosław struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features) 2687f4c50d99SHerbert Xu { 2688f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2689f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 269089319d38SHerbert Xu struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2691f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 269298e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2693f4c50d99SHerbert Xu unsigned int offset = doffset; 2694f4c50d99SHerbert Xu unsigned int headroom; 2695f4c50d99SHerbert Xu unsigned int len; 269604ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2697f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2698f4c50d99SHerbert Xu int err = -ENOMEM; 2699f4c50d99SHerbert Xu int i = 0; 2700f4c50d99SHerbert Xu int pos; 2701f4c50d99SHerbert Xu 2702f4c50d99SHerbert Xu __skb_push(skb, doffset); 2703f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2704f4c50d99SHerbert Xu pos = skb_headlen(skb); 2705f4c50d99SHerbert Xu 2706f4c50d99SHerbert Xu do { 2707f4c50d99SHerbert Xu struct sk_buff *nskb; 2708f4c50d99SHerbert Xu skb_frag_t *frag; 2709c8884eddSHerbert Xu int hsize; 2710f4c50d99SHerbert Xu int size; 2711f4c50d99SHerbert Xu 2712f4c50d99SHerbert Xu len = skb->len - offset; 2713f4c50d99SHerbert Xu if (len > mss) 2714f4c50d99SHerbert Xu len = mss; 2715f4c50d99SHerbert Xu 2716f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2717f4c50d99SHerbert Xu if (hsize < 0) 2718f4c50d99SHerbert Xu hsize = 0; 2719c8884eddSHerbert Xu if (hsize > len || !sg) 2720c8884eddSHerbert Xu hsize = len; 2721f4c50d99SHerbert Xu 272289319d38SHerbert Xu if (!hsize && i >= nfrags) { 272389319d38SHerbert Xu BUG_ON(fskb->len != len); 272489319d38SHerbert Xu 272589319d38SHerbert Xu pos += len; 272689319d38SHerbert Xu nskb = skb_clone(fskb, GFP_ATOMIC); 272789319d38SHerbert Xu fskb = fskb->next; 272889319d38SHerbert Xu 2729f4c50d99SHerbert Xu if (unlikely(!nskb)) 2730f4c50d99SHerbert Xu goto err; 2731f4c50d99SHerbert Xu 273289319d38SHerbert Xu hsize = skb_end_pointer(nskb) - nskb->head; 273389319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 273489319d38SHerbert Xu kfree_skb(nskb); 273589319d38SHerbert Xu goto err; 273689319d38SHerbert Xu } 273789319d38SHerbert Xu 273889319d38SHerbert Xu nskb->truesize += skb_end_pointer(nskb) - nskb->head - 273989319d38SHerbert Xu hsize; 274089319d38SHerbert Xu skb_release_head_state(nskb); 274189319d38SHerbert Xu __skb_push(nskb, doffset); 274289319d38SHerbert Xu } else { 274389319d38SHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, 274489319d38SHerbert Xu GFP_ATOMIC); 274589319d38SHerbert Xu 274689319d38SHerbert Xu if (unlikely(!nskb)) 274789319d38SHerbert Xu goto err; 274889319d38SHerbert Xu 274989319d38SHerbert Xu skb_reserve(nskb, headroom); 275089319d38SHerbert Xu __skb_put(nskb, doffset); 275189319d38SHerbert Xu } 275289319d38SHerbert Xu 2753f4c50d99SHerbert Xu if (segs) 2754f4c50d99SHerbert Xu tail->next = nskb; 2755f4c50d99SHerbert Xu else 2756f4c50d99SHerbert Xu segs = nskb; 2757f4c50d99SHerbert Xu tail = nskb; 2758f4c50d99SHerbert Xu 27596f85a124SHerbert Xu __copy_skb_header(nskb, skb); 2760f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2761f4c50d99SHerbert Xu 27623d3be433SEric Dumazet /* nskb and skb might have different headroom */ 27633d3be433SEric Dumazet if (nskb->ip_summed == CHECKSUM_PARTIAL) 27643d3be433SEric Dumazet nskb->csum_start += skb_headroom(nskb) - headroom; 27653d3be433SEric Dumazet 2766459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2767ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2768b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2769b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 277089319d38SHerbert Xu skb_copy_from_linear_data(skb, nskb->data, doffset); 277189319d38SHerbert Xu 27722f181855SHerbert Xu if (fskb != skb_shinfo(skb)->frag_list) 277389319d38SHerbert Xu continue; 277489319d38SHerbert Xu 2775f4c50d99SHerbert Xu if (!sg) { 27766f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2777f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2778f4c50d99SHerbert Xu skb_put(nskb, len), 2779f4c50d99SHerbert Xu len, 0); 2780f4c50d99SHerbert Xu continue; 2781f4c50d99SHerbert Xu } 2782f4c50d99SHerbert Xu 2783f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2784f4c50d99SHerbert Xu 2785d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2786d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2787f4c50d99SHerbert Xu 278889319d38SHerbert Xu while (pos < offset + len && i < nfrags) { 2789f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2790ea2ab693SIan Campbell __skb_frag_ref(frag); 27919e903e08SEric Dumazet size = skb_frag_size(frag); 2792f4c50d99SHerbert Xu 2793f4c50d99SHerbert Xu if (pos < offset) { 2794f4c50d99SHerbert Xu frag->page_offset += offset - pos; 27959e903e08SEric Dumazet skb_frag_size_sub(frag, offset - pos); 2796f4c50d99SHerbert Xu } 2797f4c50d99SHerbert Xu 279889319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 2799f4c50d99SHerbert Xu 2800f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2801f4c50d99SHerbert Xu i++; 2802f4c50d99SHerbert Xu pos += size; 2803f4c50d99SHerbert Xu } else { 28049e903e08SEric Dumazet skb_frag_size_sub(frag, pos + size - (offset + len)); 280589319d38SHerbert Xu goto skip_fraglist; 2806f4c50d99SHerbert Xu } 2807f4c50d99SHerbert Xu 2808f4c50d99SHerbert Xu frag++; 2809f4c50d99SHerbert Xu } 2810f4c50d99SHerbert Xu 281189319d38SHerbert Xu if (pos < offset + len) { 281289319d38SHerbert Xu struct sk_buff *fskb2 = fskb; 281389319d38SHerbert Xu 281489319d38SHerbert Xu BUG_ON(pos + fskb->len != offset + len); 281589319d38SHerbert Xu 281689319d38SHerbert Xu pos += fskb->len; 281789319d38SHerbert Xu fskb = fskb->next; 281889319d38SHerbert Xu 281989319d38SHerbert Xu if (fskb2->next) { 282089319d38SHerbert Xu fskb2 = skb_clone(fskb2, GFP_ATOMIC); 282189319d38SHerbert Xu if (!fskb2) 282289319d38SHerbert Xu goto err; 282389319d38SHerbert Xu } else 282489319d38SHerbert Xu skb_get(fskb2); 282589319d38SHerbert Xu 2826fbb398a8SDavid S. Miller SKB_FRAG_ASSERT(nskb); 282789319d38SHerbert Xu skb_shinfo(nskb)->frag_list = fskb2; 282889319d38SHerbert Xu } 282989319d38SHerbert Xu 283089319d38SHerbert Xu skip_fraglist: 2831f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2832f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2833f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2834f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2835f4c50d99SHerbert Xu 2836f4c50d99SHerbert Xu return segs; 2837f4c50d99SHerbert Xu 2838f4c50d99SHerbert Xu err: 2839f4c50d99SHerbert Xu while ((skb = segs)) { 2840f4c50d99SHerbert Xu segs = skb->next; 2841b08d5840SPatrick McHardy kfree_skb(skb); 2842f4c50d99SHerbert Xu } 2843f4c50d99SHerbert Xu return ERR_PTR(err); 2844f4c50d99SHerbert Xu } 2845f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2846f4c50d99SHerbert Xu 284771d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 284871d93b39SHerbert Xu { 284971d93b39SHerbert Xu struct sk_buff *p = *head; 285071d93b39SHerbert Xu struct sk_buff *nskb; 28519aaa156cSHerbert Xu struct skb_shared_info *skbinfo = skb_shinfo(skb); 28529aaa156cSHerbert Xu struct skb_shared_info *pinfo = skb_shinfo(p); 285371d93b39SHerbert Xu unsigned int headroom; 285486911732SHerbert Xu unsigned int len = skb_gro_len(skb); 285567147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 285667147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 285771d93b39SHerbert Xu 285886911732SHerbert Xu if (p->len + len >= 65536) 285971d93b39SHerbert Xu return -E2BIG; 286071d93b39SHerbert Xu 28619aaa156cSHerbert Xu if (pinfo->frag_list) 286271d93b39SHerbert Xu goto merge; 286367147ba9SHerbert Xu else if (headlen <= offset) { 286442da6994SHerbert Xu skb_frag_t *frag; 286566e92fcfSHerbert Xu skb_frag_t *frag2; 28669aaa156cSHerbert Xu int i = skbinfo->nr_frags; 28679aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 286842da6994SHerbert Xu 286966e92fcfSHerbert Xu offset -= headlen; 287066e92fcfSHerbert Xu 287166e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 287281705ad1SHerbert Xu return -E2BIG; 287381705ad1SHerbert Xu 28749aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 28759aaa156cSHerbert Xu skbinfo->nr_frags = 0; 2876f5572068SHerbert Xu 28779aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 28789aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 287966e92fcfSHerbert Xu do { 288066e92fcfSHerbert Xu *--frag = *--frag2; 288166e92fcfSHerbert Xu } while (--i); 288266e92fcfSHerbert Xu 288366e92fcfSHerbert Xu frag->page_offset += offset; 28849e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 288566e92fcfSHerbert Xu 2886f5572068SHerbert Xu skb->truesize -= skb->data_len; 2887f5572068SHerbert Xu skb->len -= skb->data_len; 2888f5572068SHerbert Xu skb->data_len = 0; 2889f5572068SHerbert Xu 28905d38a079SHerbert Xu NAPI_GRO_CB(skb)->free = 1; 28915d38a079SHerbert Xu goto done; 289269c0cab1SHerbert Xu } else if (skb_gro_len(p) != pinfo->gso_size) 289369c0cab1SHerbert Xu return -E2BIG; 289471d93b39SHerbert Xu 289571d93b39SHerbert Xu headroom = skb_headroom(p); 28963d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 289771d93b39SHerbert Xu if (unlikely(!nskb)) 289871d93b39SHerbert Xu return -ENOMEM; 289971d93b39SHerbert Xu 290071d93b39SHerbert Xu __copy_skb_header(nskb, p); 290171d93b39SHerbert Xu nskb->mac_len = p->mac_len; 290271d93b39SHerbert Xu 290371d93b39SHerbert Xu skb_reserve(nskb, headroom); 290486911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 290571d93b39SHerbert Xu 290686911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 290771d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 290871d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 290971d93b39SHerbert Xu 291086911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 291186911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 291286911732SHerbert Xu p->data - skb_mac_header(p)); 291371d93b39SHerbert Xu 291471d93b39SHerbert Xu *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p); 291571d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 29169aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 2917622e0ca1SHerbert Xu pinfo->gso_size = 0; 291871d93b39SHerbert Xu skb_header_release(p); 291971d93b39SHerbert Xu nskb->prev = p; 292071d93b39SHerbert Xu 292171d93b39SHerbert Xu nskb->data_len += p->len; 2922de8261c2SEric Dumazet nskb->truesize += p->truesize; 292371d93b39SHerbert Xu nskb->len += p->len; 292471d93b39SHerbert Xu 292571d93b39SHerbert Xu *head = nskb; 292671d93b39SHerbert Xu nskb->next = p->next; 292771d93b39SHerbert Xu p->next = NULL; 292871d93b39SHerbert Xu 292971d93b39SHerbert Xu p = nskb; 293071d93b39SHerbert Xu 293171d93b39SHerbert Xu merge: 2932de8261c2SEric Dumazet p->truesize += skb->truesize - len; 293367147ba9SHerbert Xu if (offset > headlen) { 2934d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 2935d1dc7abfSMichal Schmidt 2936d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 29379e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 2938d1dc7abfSMichal Schmidt skb->data_len -= eat; 2939d1dc7abfSMichal Schmidt skb->len -= eat; 294067147ba9SHerbert Xu offset = headlen; 294156035022SHerbert Xu } 294256035022SHerbert Xu 294367147ba9SHerbert Xu __skb_pull(skb, offset); 294456035022SHerbert Xu 294571d93b39SHerbert Xu p->prev->next = skb; 294671d93b39SHerbert Xu p->prev = skb; 294771d93b39SHerbert Xu skb_header_release(skb); 294871d93b39SHerbert Xu 29495d38a079SHerbert Xu done: 29505d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 295137fe4732SHerbert Xu p->data_len += len; 295237fe4732SHerbert Xu p->truesize += len; 295337fe4732SHerbert Xu p->len += len; 295471d93b39SHerbert Xu 295571d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 295671d93b39SHerbert Xu return 0; 295771d93b39SHerbert Xu } 295871d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive); 295971d93b39SHerbert Xu 29601da177e4SLinus Torvalds void __init skb_init(void) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 29631da177e4SLinus Torvalds sizeof(struct sk_buff), 29641da177e4SLinus Torvalds 0, 2965e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 296620c2df83SPaul Mundt NULL); 2967d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2968d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2969d179cd12SDavid S. Miller sizeof(atomic_t), 2970d179cd12SDavid S. Miller 0, 2971e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 297220c2df83SPaul Mundt NULL); 29731da177e4SLinus Torvalds } 29741da177e4SLinus Torvalds 2975716ea3a7SDavid Howells /** 2976716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2977716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2978716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2979716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2980716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2981716ea3a7SDavid Howells * 2982716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2983716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2984716ea3a7SDavid Howells */ 298551c739d1SDavid S. Miller static int 298651c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2987716ea3a7SDavid Howells { 29881a028e50SDavid S. Miller int start = skb_headlen(skb); 29891a028e50SDavid S. Miller int i, copy = start - offset; 2990fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2991716ea3a7SDavid Howells int elt = 0; 2992716ea3a7SDavid Howells 2993716ea3a7SDavid Howells if (copy > 0) { 2994716ea3a7SDavid Howells if (copy > len) 2995716ea3a7SDavid Howells copy = len; 2996642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 2997716ea3a7SDavid Howells elt++; 2998716ea3a7SDavid Howells if ((len -= copy) == 0) 2999716ea3a7SDavid Howells return elt; 3000716ea3a7SDavid Howells offset += copy; 3001716ea3a7SDavid Howells } 3002716ea3a7SDavid Howells 3003716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 30041a028e50SDavid S. Miller int end; 3005716ea3a7SDavid Howells 3006547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30071a028e50SDavid S. Miller 30089e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3009716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3010716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3011716ea3a7SDavid Howells 3012716ea3a7SDavid Howells if (copy > len) 3013716ea3a7SDavid Howells copy = len; 3014ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3015642f1490SJens Axboe frag->page_offset+offset-start); 3016716ea3a7SDavid Howells elt++; 3017716ea3a7SDavid Howells if (!(len -= copy)) 3018716ea3a7SDavid Howells return elt; 3019716ea3a7SDavid Howells offset += copy; 3020716ea3a7SDavid Howells } 30211a028e50SDavid S. Miller start = end; 3022716ea3a7SDavid Howells } 3023716ea3a7SDavid Howells 3024fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 30251a028e50SDavid S. Miller int end; 3026716ea3a7SDavid Howells 3027547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30281a028e50SDavid S. Miller 3029fbb398a8SDavid S. Miller end = start + frag_iter->len; 3030716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3031716ea3a7SDavid Howells if (copy > len) 3032716ea3a7SDavid Howells copy = len; 3033fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 303451c739d1SDavid S. Miller copy); 3035716ea3a7SDavid Howells if ((len -= copy) == 0) 3036716ea3a7SDavid Howells return elt; 3037716ea3a7SDavid Howells offset += copy; 3038716ea3a7SDavid Howells } 30391a028e50SDavid S. Miller start = end; 3040716ea3a7SDavid Howells } 3041716ea3a7SDavid Howells BUG_ON(len); 3042716ea3a7SDavid Howells return elt; 3043716ea3a7SDavid Howells } 3044716ea3a7SDavid Howells 304551c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 304651c739d1SDavid S. Miller { 304751c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 304851c739d1SDavid S. Miller 3049c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 305051c739d1SDavid S. Miller 305151c739d1SDavid S. Miller return nsg; 305251c739d1SDavid S. Miller } 3053b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 305451c739d1SDavid S. Miller 3055716ea3a7SDavid Howells /** 3056716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3057716ea3a7SDavid Howells * @skb: The socket buffer to check. 3058716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3059716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3060716ea3a7SDavid Howells * 3061716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3062716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3063716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3064716ea3a7SDavid Howells * 3065716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3066716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3067716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3068716ea3a7SDavid Howells * 3069716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3070716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3071716ea3a7SDavid Howells */ 3072716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3073716ea3a7SDavid Howells { 3074716ea3a7SDavid Howells int copyflag; 3075716ea3a7SDavid Howells int elt; 3076716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3077716ea3a7SDavid Howells 3078716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3079716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3080716ea3a7SDavid Howells * at the moment even if they are anonymous). 3081716ea3a7SDavid Howells */ 3082716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3083716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3084716ea3a7SDavid Howells return -ENOMEM; 3085716ea3a7SDavid Howells 3086716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 308721dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3088716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3089716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3090716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3091716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3092716ea3a7SDavid Howells 3093716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3094716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3095716ea3a7SDavid Howells return -ENOMEM; 3096716ea3a7SDavid Howells 3097716ea3a7SDavid Howells /* Voila! */ 3098716ea3a7SDavid Howells *trailer = skb; 3099716ea3a7SDavid Howells return 1; 3100716ea3a7SDavid Howells } 3101716ea3a7SDavid Howells 3102716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3103716ea3a7SDavid Howells 3104716ea3a7SDavid Howells elt = 1; 3105716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3106716ea3a7SDavid Howells copyflag = 0; 3107716ea3a7SDavid Howells 3108716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3109716ea3a7SDavid Howells int ntail = 0; 3110716ea3a7SDavid Howells 3111716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3112716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3113716ea3a7SDavid Howells * after it. */ 3114716ea3a7SDavid Howells 3115716ea3a7SDavid Howells if (skb_shared(skb1)) 3116716ea3a7SDavid Howells copyflag = 1; 3117716ea3a7SDavid Howells 3118716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3119716ea3a7SDavid Howells 3120716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3121716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 312221dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3123716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3124716ea3a7SDavid Howells ntail = tailbits + 128; 3125716ea3a7SDavid Howells } 3126716ea3a7SDavid Howells 3127716ea3a7SDavid Howells if (copyflag || 3128716ea3a7SDavid Howells skb_cloned(skb1) || 3129716ea3a7SDavid Howells ntail || 3130716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 313121dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3132716ea3a7SDavid Howells struct sk_buff *skb2; 3133716ea3a7SDavid Howells 3134716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3135716ea3a7SDavid Howells if (ntail == 0) 3136716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3137716ea3a7SDavid Howells else 3138716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3139716ea3a7SDavid Howells skb_headroom(skb1), 3140716ea3a7SDavid Howells ntail, 3141716ea3a7SDavid Howells GFP_ATOMIC); 3142716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3143716ea3a7SDavid Howells return -ENOMEM; 3144716ea3a7SDavid Howells 3145716ea3a7SDavid Howells if (skb1->sk) 3146716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3147716ea3a7SDavid Howells 3148716ea3a7SDavid Howells /* Looking around. Are we still alive? 3149716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3150716ea3a7SDavid Howells 3151716ea3a7SDavid Howells skb2->next = skb1->next; 3152716ea3a7SDavid Howells *skb_p = skb2; 3153716ea3a7SDavid Howells kfree_skb(skb1); 3154716ea3a7SDavid Howells skb1 = skb2; 3155716ea3a7SDavid Howells } 3156716ea3a7SDavid Howells elt++; 3157716ea3a7SDavid Howells *trailer = skb1; 3158716ea3a7SDavid Howells skb_p = &skb1->next; 3159716ea3a7SDavid Howells } 3160716ea3a7SDavid Howells 3161716ea3a7SDavid Howells return elt; 3162716ea3a7SDavid Howells } 3163b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3164716ea3a7SDavid Howells 3165b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3166b1faf566SEric Dumazet { 3167b1faf566SEric Dumazet struct sock *sk = skb->sk; 3168b1faf566SEric Dumazet 3169b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3170b1faf566SEric Dumazet } 3171b1faf566SEric Dumazet 3172b1faf566SEric Dumazet /* 3173b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3174b1faf566SEric Dumazet */ 3175b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3176b1faf566SEric Dumazet { 3177110c4330SEric Dumazet int len = skb->len; 3178110c4330SEric Dumazet 3179b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 318095c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3181b1faf566SEric Dumazet return -ENOMEM; 3182b1faf566SEric Dumazet 3183b1faf566SEric Dumazet skb_orphan(skb); 3184b1faf566SEric Dumazet skb->sk = sk; 3185b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3186b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3187b1faf566SEric Dumazet 3188abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3189abb57ea4SEric Dumazet skb_dst_force(skb); 3190abb57ea4SEric Dumazet 3191b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3192b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3193110c4330SEric Dumazet sk->sk_data_ready(sk, len); 3194b1faf566SEric Dumazet return 0; 3195b1faf566SEric Dumazet } 3196b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3197b1faf566SEric Dumazet 3198ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb, 3199ac45f602SPatrick Ohly struct skb_shared_hwtstamps *hwtstamps) 3200ac45f602SPatrick Ohly { 3201ac45f602SPatrick Ohly struct sock *sk = orig_skb->sk; 3202ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3203ac45f602SPatrick Ohly struct sk_buff *skb; 3204ac45f602SPatrick Ohly int err; 3205ac45f602SPatrick Ohly 3206ac45f602SPatrick Ohly if (!sk) 3207ac45f602SPatrick Ohly return; 3208ac45f602SPatrick Ohly 3209ac45f602SPatrick Ohly skb = skb_clone(orig_skb, GFP_ATOMIC); 3210ac45f602SPatrick Ohly if (!skb) 3211ac45f602SPatrick Ohly return; 3212ac45f602SPatrick Ohly 3213ac45f602SPatrick Ohly if (hwtstamps) { 3214ac45f602SPatrick Ohly *skb_hwtstamps(skb) = 3215ac45f602SPatrick Ohly *hwtstamps; 3216ac45f602SPatrick Ohly } else { 3217ac45f602SPatrick Ohly /* 3218ac45f602SPatrick Ohly * no hardware time stamps available, 32192244d07bSOliver Hartkopp * so keep the shared tx_flags and only 3220ac45f602SPatrick Ohly * store software time stamp 3221ac45f602SPatrick Ohly */ 3222ac45f602SPatrick Ohly skb->tstamp = ktime_get_real(); 3223ac45f602SPatrick Ohly } 3224ac45f602SPatrick Ohly 3225ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3226ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3227ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3228ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 322929030374SEric Dumazet 3230ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 323129030374SEric Dumazet 3232ac45f602SPatrick Ohly if (err) 3233ac45f602SPatrick Ohly kfree_skb(skb); 3234ac45f602SPatrick Ohly } 3235ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3236ac45f602SPatrick Ohly 32376e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 32386e3e939fSJohannes Berg { 32396e3e939fSJohannes Berg struct sock *sk = skb->sk; 32406e3e939fSJohannes Berg struct sock_exterr_skb *serr; 32416e3e939fSJohannes Berg int err; 32426e3e939fSJohannes Berg 32436e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 32446e3e939fSJohannes Berg skb->wifi_acked = acked; 32456e3e939fSJohannes Berg 32466e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 32476e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 32486e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 32496e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 32506e3e939fSJohannes Berg 32516e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 32526e3e939fSJohannes Berg if (err) 32536e3e939fSJohannes Berg kfree_skb(skb); 32546e3e939fSJohannes Berg } 32556e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 32566e3e939fSJohannes Berg 3257ac45f602SPatrick Ohly 3258f35d9d8aSRusty Russell /** 3259f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3260f35d9d8aSRusty Russell * @skb: the skb to set 3261f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3262f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3263f35d9d8aSRusty Russell * 3264f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3265f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3266f35d9d8aSRusty Russell * 3267f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3268f35d9d8aSRusty Russell * returns false you should drop the packet. 3269f35d9d8aSRusty Russell */ 3270f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3271f35d9d8aSRusty Russell { 32725ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 32735ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3274f35d9d8aSRusty Russell if (net_ratelimit()) 3275f35d9d8aSRusty Russell printk(KERN_WARNING 3276f35d9d8aSRusty Russell "bad partial csum: csum=%u/%u len=%u\n", 32775ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3278f35d9d8aSRusty Russell return false; 3279f35d9d8aSRusty Russell } 3280f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3281f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3282f35d9d8aSRusty Russell skb->csum_offset = off; 3283f35d9d8aSRusty Russell return true; 3284f35d9d8aSRusty Russell } 3285b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3286f35d9d8aSRusty Russell 32874497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 32884497b076SBen Hutchings { 32894497b076SBen Hutchings if (net_ratelimit()) 32904497b076SBen Hutchings pr_warning("%s: received packets cannot be forwarded" 32914497b076SBen Hutchings " while LRO is enabled\n", skb->dev->name); 32924497b076SBen Hutchings } 32934497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3294