11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Fixes: 81da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 91da177e4SLinus Torvalds * balancer bugs. 101da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 111da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 121da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 131da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 141da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 151da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 161da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 171da177e4SLinus Torvalds * only put in the headers 181da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 191da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 201da177e4SLinus Torvalds * Andi Kleen : slabified it. 211da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * NOTE: 241da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 251da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 261da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 271da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 301da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 311da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 321da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds 39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 40e005d193SJoe Perches 411da177e4SLinus Torvalds #include <linux/module.h> 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 44fe55f6d5SVegard Nossum #include <linux/kmemcheck.h> 451da177e4SLinus Torvalds #include <linux/mm.h> 461da177e4SLinus Torvalds #include <linux/interrupt.h> 471da177e4SLinus Torvalds #include <linux/in.h> 481da177e4SLinus Torvalds #include <linux/inet.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/netdevice.h> 511da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 521da177e4SLinus Torvalds #include <net/pkt_sched.h> 531da177e4SLinus Torvalds #endif 541da177e4SLinus Torvalds #include <linux/string.h> 551da177e4SLinus Torvalds #include <linux/skbuff.h> 569c55e01cSJens Axboe #include <linux/splice.h> 571da177e4SLinus Torvalds #include <linux/cache.h> 581da177e4SLinus Torvalds #include <linux/rtnetlink.h> 591da177e4SLinus Torvalds #include <linux/init.h> 60716ea3a7SDavid Howells #include <linux/scatterlist.h> 61ac45f602SPatrick Ohly #include <linux/errqueue.h> 62268bb0ceSLinus Torvalds #include <linux/prefetch.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #include <net/protocol.h> 651da177e4SLinus Torvalds #include <net/dst.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/checksum.h> 681da177e4SLinus Torvalds #include <net/xfrm.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #include <asm/uaccess.h> 71ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7251c56b00SEric Dumazet #include <linux/highmem.h> 73a1f8e7f7SAl Viro 74d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 75e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 761da177e4SLinus Torvalds 779c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 789c55e01cSJens Axboe struct pipe_buffer *buf) 799c55e01cSJens Axboe { 808b9d3728SJarek Poplawski put_page(buf->page); 819c55e01cSJens Axboe } 829c55e01cSJens Axboe 839c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 849c55e01cSJens Axboe struct pipe_buffer *buf) 859c55e01cSJens Axboe { 868b9d3728SJarek Poplawski get_page(buf->page); 879c55e01cSJens Axboe } 889c55e01cSJens Axboe 899c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 909c55e01cSJens Axboe struct pipe_buffer *buf) 919c55e01cSJens Axboe { 929c55e01cSJens Axboe return 1; 939c55e01cSJens Axboe } 949c55e01cSJens Axboe 959c55e01cSJens Axboe 969c55e01cSJens Axboe /* Pipe buffer operations for a socket. */ 9728dfef8fSAlexey Dobriyan static const struct pipe_buf_operations sock_pipe_buf_ops = { 989c55e01cSJens Axboe .can_merge = 0, 999c55e01cSJens Axboe .map = generic_pipe_buf_map, 1009c55e01cSJens Axboe .unmap = generic_pipe_buf_unmap, 1019c55e01cSJens Axboe .confirm = generic_pipe_buf_confirm, 1029c55e01cSJens Axboe .release = sock_pipe_buf_release, 1039c55e01cSJens Axboe .steal = sock_pipe_buf_steal, 1049c55e01cSJens Axboe .get = sock_pipe_buf_get, 1059c55e01cSJens Axboe }; 1069c55e01cSJens Axboe 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 1091da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 1101da177e4SLinus Torvalds * reliable. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /** 1141da177e4SLinus Torvalds * skb_over_panic - private function 1151da177e4SLinus Torvalds * @skb: buffer 1161da177e4SLinus Torvalds * @sz: size 1171da177e4SLinus Torvalds * @here: address 1181da177e4SLinus Torvalds * 1191da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 1201da177e4SLinus Torvalds */ 121ccb7c773SRami Rosen static void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1221da177e4SLinus Torvalds { 123e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 124e005d193SJoe Perches __func__, here, skb->len, sz, skb->head, skb->data, 1254305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12626095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1271da177e4SLinus Torvalds BUG(); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /** 1311da177e4SLinus Torvalds * skb_under_panic - private function 1321da177e4SLinus Torvalds * @skb: buffer 1331da177e4SLinus Torvalds * @sz: size 1341da177e4SLinus Torvalds * @here: address 1351da177e4SLinus Torvalds * 1361da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1371da177e4SLinus Torvalds */ 1381da177e4SLinus Torvalds 139ccb7c773SRami Rosen static void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1401da177e4SLinus Torvalds { 141e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 142e005d193SJoe Perches __func__, here, skb->len, sz, skb->head, skb->data, 1434305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 14426095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1451da177e4SLinus Torvalds BUG(); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 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 16394b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 16494b6042cSBen Hutchings * of one. 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 248d3836f21SEric Dumazet * @frag_size: size of fragment, or 0 if head was kmalloced 249b2b5ce9dSEric Dumazet * 250b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 251b2b5ce9dSEric Dumazet * skb_shared_info. @data must have been allocated by kmalloc() 252b2b5ce9dSEric Dumazet * The return is the new skb buffer. 253b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 254b2b5ce9dSEric Dumazet * Notes : 255b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 256b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 257b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 258b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 259b2b5ce9dSEric Dumazet * before giving packet to stack. 260b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 261b2b5ce9dSEric Dumazet */ 262d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 263b2b5ce9dSEric Dumazet { 264b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 265b2b5ce9dSEric Dumazet struct sk_buff *skb; 266d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 267b2b5ce9dSEric Dumazet 268b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 269b2b5ce9dSEric Dumazet if (!skb) 270b2b5ce9dSEric Dumazet return NULL; 271b2b5ce9dSEric Dumazet 272d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 273b2b5ce9dSEric Dumazet 274b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 275b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 276d3836f21SEric Dumazet skb->head_frag = frag_size != 0; 277b2b5ce9dSEric Dumazet atomic_set(&skb->users, 1); 278b2b5ce9dSEric Dumazet skb->head = data; 279b2b5ce9dSEric Dumazet skb->data = data; 280b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 281b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 282b2b5ce9dSEric Dumazet #ifdef NET_SKBUFF_DATA_USES_OFFSET 283b2b5ce9dSEric Dumazet skb->mac_header = ~0U; 284b2b5ce9dSEric Dumazet #endif 285b2b5ce9dSEric Dumazet 286b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 287b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 288b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 289b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 290b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 291b2b5ce9dSEric Dumazet 292b2b5ce9dSEric Dumazet return skb; 293b2b5ce9dSEric Dumazet } 294b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 295b2b5ce9dSEric Dumazet 296a1c7fff7SEric Dumazet struct netdev_alloc_cache { 297a1c7fff7SEric Dumazet struct page *page; 298a1c7fff7SEric Dumazet unsigned int offset; 299540eb7bfSAlexander Duyck unsigned int pagecnt_bias; 300a1c7fff7SEric Dumazet }; 301a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); 302a1c7fff7SEric Dumazet 303540eb7bfSAlexander Duyck #define NETDEV_PAGECNT_BIAS (PAGE_SIZE / SMP_CACHE_BYTES) 304540eb7bfSAlexander Duyck 305b2b5ce9dSEric Dumazet /** 3066f532612SEric Dumazet * netdev_alloc_frag - allocate a page fragment 3076f532612SEric Dumazet * @fragsz: fragment size 3086f532612SEric Dumazet * 3096f532612SEric Dumazet * Allocates a frag from a page for receive buffer. 3106f532612SEric Dumazet * Uses GFP_ATOMIC allocations. 3116f532612SEric Dumazet */ 3126f532612SEric Dumazet void *netdev_alloc_frag(unsigned int fragsz) 3136f532612SEric Dumazet { 3146f532612SEric Dumazet struct netdev_alloc_cache *nc; 3156f532612SEric Dumazet void *data = NULL; 3166f532612SEric Dumazet unsigned long flags; 3176f532612SEric Dumazet 3186f532612SEric Dumazet local_irq_save(flags); 3196f532612SEric Dumazet nc = &__get_cpu_var(netdev_alloc_cache); 3206f532612SEric Dumazet if (unlikely(!nc->page)) { 3216f532612SEric Dumazet refill: 3226f532612SEric Dumazet nc->page = alloc_page(GFP_ATOMIC | __GFP_COLD); 323540eb7bfSAlexander Duyck if (unlikely(!nc->page)) 324540eb7bfSAlexander Duyck goto end; 325540eb7bfSAlexander Duyck recycle: 326540eb7bfSAlexander Duyck atomic_set(&nc->page->_count, NETDEV_PAGECNT_BIAS); 327540eb7bfSAlexander Duyck nc->pagecnt_bias = NETDEV_PAGECNT_BIAS; 3286f532612SEric Dumazet nc->offset = 0; 3296f532612SEric Dumazet } 330540eb7bfSAlexander Duyck 3316f532612SEric Dumazet if (nc->offset + fragsz > PAGE_SIZE) { 332540eb7bfSAlexander Duyck /* avoid unnecessary locked operations if possible */ 333540eb7bfSAlexander Duyck if ((atomic_read(&nc->page->_count) == nc->pagecnt_bias) || 334540eb7bfSAlexander Duyck atomic_sub_and_test(nc->pagecnt_bias, &nc->page->_count)) 335540eb7bfSAlexander Duyck goto recycle; 3366f532612SEric Dumazet goto refill; 3376f532612SEric Dumazet } 338540eb7bfSAlexander Duyck 3396f532612SEric Dumazet data = page_address(nc->page) + nc->offset; 3406f532612SEric Dumazet nc->offset += fragsz; 341540eb7bfSAlexander Duyck nc->pagecnt_bias--; 342540eb7bfSAlexander Duyck end: 3436f532612SEric Dumazet local_irq_restore(flags); 3446f532612SEric Dumazet return data; 3456f532612SEric Dumazet } 3466f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3476f532612SEric Dumazet 3486f532612SEric Dumazet /** 3498af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 3508af27456SChristoph Hellwig * @dev: network device to receive on 3518af27456SChristoph Hellwig * @length: length to allocate 3528af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 3538af27456SChristoph Hellwig * 3548af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 3558af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 3568af27456SChristoph Hellwig * the headroom they think they need without accounting for the 3578af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 3588af27456SChristoph Hellwig * 3598af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 3608af27456SChristoph Hellwig */ 3618af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 3628af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 3638af27456SChristoph Hellwig { 3646f532612SEric Dumazet struct sk_buff *skb = NULL; 365a1c7fff7SEric Dumazet unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + 366a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 3678af27456SChristoph Hellwig 368310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 3696f532612SEric Dumazet void *data = netdev_alloc_frag(fragsz); 370a1c7fff7SEric Dumazet 3716f532612SEric Dumazet if (likely(data)) { 3726f532612SEric Dumazet skb = build_skb(data, fragsz); 3736f532612SEric Dumazet if (unlikely(!skb)) 3746f532612SEric Dumazet put_page(virt_to_head_page(data)); 375a1c7fff7SEric Dumazet } 376a1c7fff7SEric Dumazet } else { 377564824b0SEric Dumazet skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE); 378a1c7fff7SEric Dumazet } 3797b2e497aSChristoph Hellwig if (likely(skb)) { 3808af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 3817b2e497aSChristoph Hellwig skb->dev = dev; 3827b2e497aSChristoph Hellwig } 3838af27456SChristoph Hellwig return skb; 3848af27456SChristoph Hellwig } 385b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 3861da177e4SLinus Torvalds 387654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 38850269e19SEric Dumazet int size, unsigned int truesize) 389654bed16SPeter Zijlstra { 390654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 391654bed16SPeter Zijlstra skb->len += size; 392654bed16SPeter Zijlstra skb->data_len += size; 39350269e19SEric Dumazet skb->truesize += truesize; 394654bed16SPeter Zijlstra } 395654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 396654bed16SPeter Zijlstra 39727b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 3981da177e4SLinus Torvalds { 39927b437c8SHerbert Xu struct sk_buff *list = *listp; 4001da177e4SLinus Torvalds 40127b437c8SHerbert Xu *listp = NULL; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds do { 4041da177e4SLinus Torvalds struct sk_buff *this = list; 4051da177e4SLinus Torvalds list = list->next; 4061da177e4SLinus Torvalds kfree_skb(this); 4071da177e4SLinus Torvalds } while (list); 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 41027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 41127b437c8SHerbert Xu { 41227b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 41327b437c8SHerbert Xu } 41427b437c8SHerbert Xu 4151da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds struct sk_buff *list; 4181da177e4SLinus Torvalds 419fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 4201da177e4SLinus Torvalds skb_get(list); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 423d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 424d3836f21SEric Dumazet { 425d3836f21SEric Dumazet if (skb->head_frag) 426d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 427d3836f21SEric Dumazet else 428d3836f21SEric Dumazet kfree(skb->head); 429d3836f21SEric Dumazet } 430d3836f21SEric Dumazet 4315bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds if (!skb->cloned || 4341da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 4351da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 4361da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 4371da177e4SLinus Torvalds int i; 4381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 439ea2ab693SIan Campbell skb_frag_unref(skb, i); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 442a6686f2fSShirley Ma /* 443a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 444a6686f2fSShirley Ma * the lower device DMA has done; 445a6686f2fSShirley Ma */ 446a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 447a6686f2fSShirley Ma struct ubuf_info *uarg; 448a6686f2fSShirley Ma 449a6686f2fSShirley Ma uarg = skb_shinfo(skb)->destructor_arg; 450a6686f2fSShirley Ma if (uarg->callback) 451a6686f2fSShirley Ma uarg->callback(uarg); 452a6686f2fSShirley Ma } 453a6686f2fSShirley Ma 45421dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 4551da177e4SLinus Torvalds skb_drop_fraglist(skb); 4561da177e4SLinus Torvalds 457d3836f21SEric Dumazet skb_free_head(skb); 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* 4621da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 4631da177e4SLinus Torvalds */ 4642d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 4651da177e4SLinus Torvalds { 466d179cd12SDavid S. Miller struct sk_buff *other; 467d179cd12SDavid S. Miller atomic_t *fclone_ref; 468d179cd12SDavid S. Miller 469d179cd12SDavid S. Miller switch (skb->fclone) { 470d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 4711da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 472d179cd12SDavid S. Miller break; 473d179cd12SDavid S. Miller 474d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 475d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 476d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 477d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 478d179cd12SDavid S. Miller break; 479d179cd12SDavid S. Miller 480d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 481d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 482d179cd12SDavid S. Miller other = skb - 1; 483d179cd12SDavid S. Miller 484d179cd12SDavid S. Miller /* The clone portion is available for 485d179cd12SDavid S. Miller * fast-cloning again. 486d179cd12SDavid S. Miller */ 487d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 488d179cd12SDavid S. Miller 489d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 490d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 491d179cd12SDavid S. Miller break; 4923ff50b79SStephen Hemminger } 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 49504a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 4961da177e4SLinus Torvalds { 497adf30907SEric Dumazet skb_dst_drop(skb); 4981da177e4SLinus Torvalds #ifdef CONFIG_XFRM 4991da177e4SLinus Torvalds secpath_put(skb->sp); 5001da177e4SLinus Torvalds #endif 5011da177e4SLinus Torvalds if (skb->destructor) { 5029c2b3328SStephen Hemminger WARN_ON(in_irq()); 5031da177e4SLinus Torvalds skb->destructor(skb); 5041da177e4SLinus Torvalds } 505a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 5065f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 5072fc72c7bSKOVACS Krisztian #endif 5082fc72c7bSKOVACS Krisztian #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED 5099fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 5109fb9cbb1SYasuyuki Kozakai #endif 5111da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 5121da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 5131da177e4SLinus Torvalds #endif 5141da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 5151da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5161da177e4SLinus Torvalds skb->tc_index = 0; 5171da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 5181da177e4SLinus Torvalds skb->tc_verd = 0; 5191da177e4SLinus Torvalds #endif 5201da177e4SLinus Torvalds #endif 52104a4bb55SLennert Buytenhek } 52204a4bb55SLennert Buytenhek 52304a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 52404a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 52504a4bb55SLennert Buytenhek { 52604a4bb55SLennert Buytenhek skb_release_head_state(skb); 5272d4baff8SHerbert Xu skb_release_data(skb); 5282d4baff8SHerbert Xu } 5291da177e4SLinus Torvalds 5302d4baff8SHerbert Xu /** 5312d4baff8SHerbert Xu * __kfree_skb - private function 5322d4baff8SHerbert Xu * @skb: buffer 5332d4baff8SHerbert Xu * 5342d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 5352d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 5362d4baff8SHerbert Xu * always call kfree_skb 5372d4baff8SHerbert Xu */ 5382d4baff8SHerbert Xu 5392d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 5402d4baff8SHerbert Xu { 5412d4baff8SHerbert Xu skb_release_all(skb); 5421da177e4SLinus Torvalds kfree_skbmem(skb); 5431da177e4SLinus Torvalds } 544b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /** 547231d06aeSJörn Engel * kfree_skb - free an sk_buff 548231d06aeSJörn Engel * @skb: buffer to free 549231d06aeSJörn Engel * 550231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 551231d06aeSJörn Engel * hit zero. 552231d06aeSJörn Engel */ 553231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 554231d06aeSJörn Engel { 555231d06aeSJörn Engel if (unlikely(!skb)) 556231d06aeSJörn Engel return; 557231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 558231d06aeSJörn Engel smp_rmb(); 559231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 560231d06aeSJörn Engel return; 561ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 562231d06aeSJörn Engel __kfree_skb(skb); 563231d06aeSJörn Engel } 564b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 565231d06aeSJörn Engel 566d1a203eaSStephen Hemminger /** 567ead2ceb0SNeil Horman * consume_skb - free an skbuff 568ead2ceb0SNeil Horman * @skb: buffer to free 569ead2ceb0SNeil Horman * 570ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 571ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 572ead2ceb0SNeil Horman * is being dropped after a failure and notes that 573ead2ceb0SNeil Horman */ 574ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 575ead2ceb0SNeil Horman { 576ead2ceb0SNeil Horman if (unlikely(!skb)) 577ead2ceb0SNeil Horman return; 578ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 579ead2ceb0SNeil Horman smp_rmb(); 580ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 581ead2ceb0SNeil Horman return; 58207dc22e7SKoki Sanagi trace_consume_skb(skb); 583ead2ceb0SNeil Horman __kfree_skb(skb); 584ead2ceb0SNeil Horman } 585ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 586ead2ceb0SNeil Horman 587ead2ceb0SNeil Horman /** 5883d153a7cSAndy Fleming * skb_recycle - clean up an skb for reuse 5893d153a7cSAndy Fleming * @skb: buffer 5903d153a7cSAndy Fleming * 5913d153a7cSAndy Fleming * Recycles the skb to be reused as a receive buffer. This 5923d153a7cSAndy Fleming * function does any necessary reference count dropping, and 5933d153a7cSAndy Fleming * cleans up the skbuff as if it just came from __alloc_skb(). 5943d153a7cSAndy Fleming */ 5953d153a7cSAndy Fleming void skb_recycle(struct sk_buff *skb) 5963d153a7cSAndy Fleming { 5973d153a7cSAndy Fleming struct skb_shared_info *shinfo; 5983d153a7cSAndy Fleming 5993d153a7cSAndy Fleming skb_release_head_state(skb); 6003d153a7cSAndy Fleming 6013d153a7cSAndy Fleming shinfo = skb_shinfo(skb); 6023d153a7cSAndy Fleming memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 6033d153a7cSAndy Fleming atomic_set(&shinfo->dataref, 1); 6043d153a7cSAndy Fleming 6053d153a7cSAndy Fleming memset(skb, 0, offsetof(struct sk_buff, tail)); 6063d153a7cSAndy Fleming skb->data = skb->head + NET_SKB_PAD; 6073d153a7cSAndy Fleming skb_reset_tail_pointer(skb); 6083d153a7cSAndy Fleming } 6093d153a7cSAndy Fleming EXPORT_SYMBOL(skb_recycle); 6103d153a7cSAndy Fleming 6113d153a7cSAndy Fleming /** 612d1a203eaSStephen Hemminger * skb_recycle_check - check if skb can be reused for receive 613d1a203eaSStephen Hemminger * @skb: buffer 614d1a203eaSStephen Hemminger * @skb_size: minimum receive buffer size 615d1a203eaSStephen Hemminger * 616d1a203eaSStephen Hemminger * Checks that the skb passed in is not shared or cloned, and 617d1a203eaSStephen Hemminger * that it is linear and its head portion at least as large as 618d1a203eaSStephen Hemminger * skb_size so that it can be recycled as a receive buffer. 619d1a203eaSStephen Hemminger * If these conditions are met, this function does any necessary 620d1a203eaSStephen Hemminger * reference count dropping and cleans up the skbuff as if it 621d1a203eaSStephen Hemminger * just came from __alloc_skb(). 622d1a203eaSStephen Hemminger */ 6235b0daa34SChangli Gao bool skb_recycle_check(struct sk_buff *skb, int skb_size) 62404a4bb55SLennert Buytenhek { 6253d153a7cSAndy Fleming if (!skb_is_recycleable(skb, skb_size)) 6265b0daa34SChangli Gao return false; 627e84af6ddSAnton Vorontsov 6283d153a7cSAndy Fleming skb_recycle(skb); 62904a4bb55SLennert Buytenhek 6305b0daa34SChangli Gao return true; 63104a4bb55SLennert Buytenhek } 63204a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check); 63304a4bb55SLennert Buytenhek 634dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 635dec18810SHerbert Xu { 636dec18810SHerbert Xu new->tstamp = old->tstamp; 637dec18810SHerbert Xu new->dev = old->dev; 638dec18810SHerbert Xu new->transport_header = old->transport_header; 639dec18810SHerbert Xu new->network_header = old->network_header; 640dec18810SHerbert Xu new->mac_header = old->mac_header; 6417fee226aSEric Dumazet skb_dst_copy(new, old); 6420a9627f2STom Herbert new->rxhash = old->rxhash; 6436461be3aSChangli Gao new->ooo_okay = old->ooo_okay; 644bdeab991STom Herbert new->l4_rxhash = old->l4_rxhash; 6453bdc0ebaSBen Greear new->no_fcs = old->no_fcs; 646def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 647dec18810SHerbert Xu new->sp = secpath_get(old->sp); 648dec18810SHerbert Xu #endif 649dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 6509bcb97caSHerbert Xu new->csum = old->csum; 651dec18810SHerbert Xu new->local_df = old->local_df; 652dec18810SHerbert Xu new->pkt_type = old->pkt_type; 653dec18810SHerbert Xu new->ip_summed = old->ip_summed; 654dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 655dec18810SHerbert Xu new->priority = old->priority; 656a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_IP_VS) 657dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 658dec18810SHerbert Xu #endif 659dec18810SHerbert Xu new->protocol = old->protocol; 660dec18810SHerbert Xu new->mark = old->mark; 6618964be4aSEric Dumazet new->skb_iif = old->skb_iif; 662dec18810SHerbert Xu __nf_copy(new, old); 663a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 664dec18810SHerbert Xu new->nf_trace = old->nf_trace; 665dec18810SHerbert Xu #endif 666dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 667dec18810SHerbert Xu new->tc_index = old->tc_index; 668dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 669dec18810SHerbert Xu new->tc_verd = old->tc_verd; 670dec18810SHerbert Xu #endif 671dec18810SHerbert Xu #endif 6726aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 6736aa895b0SPatrick McHardy 674dec18810SHerbert Xu skb_copy_secmark(new, old); 675dec18810SHerbert Xu } 676dec18810SHerbert Xu 67782c49a35SHerbert Xu /* 67882c49a35SHerbert Xu * You should not add any new code to this function. Add it to 67982c49a35SHerbert Xu * __copy_skb_header above instead. 68082c49a35SHerbert Xu */ 681e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 6821da177e4SLinus Torvalds { 6831da177e4SLinus Torvalds #define C(x) n->x = skb->x 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds n->next = n->prev = NULL; 6861da177e4SLinus Torvalds n->sk = NULL; 687dec18810SHerbert Xu __copy_skb_header(n, skb); 688dec18810SHerbert Xu 6891da177e4SLinus Torvalds C(len); 6901da177e4SLinus Torvalds C(data_len); 6913e6b3b2eSAlexey Dobriyan C(mac_len); 692334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 69302f1c89dSPaul Moore n->cloned = 1; 6941da177e4SLinus Torvalds n->nohdr = 0; 6951da177e4SLinus Torvalds n->destructor = NULL; 6961da177e4SLinus Torvalds C(tail); 6971da177e4SLinus Torvalds C(end); 69802f1c89dSPaul Moore C(head); 699d3836f21SEric Dumazet C(head_frag); 70002f1c89dSPaul Moore C(data); 70102f1c89dSPaul Moore C(truesize); 70202f1c89dSPaul Moore atomic_set(&n->users, 1); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 7051da177e4SLinus Torvalds skb->cloned = 1; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds return n; 708e0053ec0SHerbert Xu #undef C 709e0053ec0SHerbert Xu } 710e0053ec0SHerbert Xu 711e0053ec0SHerbert Xu /** 712e0053ec0SHerbert Xu * skb_morph - morph one skb into another 713e0053ec0SHerbert Xu * @dst: the skb to receive the contents 714e0053ec0SHerbert Xu * @src: the skb to supply the contents 715e0053ec0SHerbert Xu * 716e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 717e0053ec0SHerbert Xu * supplied by the user. 718e0053ec0SHerbert Xu * 719e0053ec0SHerbert Xu * The target skb is returned upon exit. 720e0053ec0SHerbert Xu */ 721e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 722e0053ec0SHerbert Xu { 7232d4baff8SHerbert Xu skb_release_all(dst); 724e0053ec0SHerbert Xu return __skb_clone(dst, src); 725e0053ec0SHerbert Xu } 726e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 727e0053ec0SHerbert Xu 7282c53040fSBen Hutchings /** 7292c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 73048c83012SMichael S. Tsirkin * @skb: the skb to modify 73148c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 73248c83012SMichael S. Tsirkin * 73348c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 73448c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 73548c83012SMichael S. Tsirkin * to userspace pages. 73648c83012SMichael S. Tsirkin * 73748c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 73848c83012SMichael S. Tsirkin * %GFP_ATOMIC. 73948c83012SMichael S. Tsirkin * 74048c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 74148c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 74248c83012SMichael S. Tsirkin */ 74348c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 744a6686f2fSShirley Ma { 745a6686f2fSShirley Ma int i; 746a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 747a6686f2fSShirley Ma struct page *page, *head = NULL; 748a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 749a6686f2fSShirley Ma 750a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 751a6686f2fSShirley Ma u8 *vaddr; 752a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 753a6686f2fSShirley Ma 75402756ed4SKrishna Kumar page = alloc_page(gfp_mask); 755a6686f2fSShirley Ma if (!page) { 756a6686f2fSShirley Ma while (head) { 757a6686f2fSShirley Ma struct page *next = (struct page *)head->private; 758a6686f2fSShirley Ma put_page(head); 759a6686f2fSShirley Ma head = next; 760a6686f2fSShirley Ma } 761a6686f2fSShirley Ma return -ENOMEM; 762a6686f2fSShirley Ma } 76351c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 764a6686f2fSShirley Ma memcpy(page_address(page), 7659e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 76651c56b00SEric Dumazet kunmap_atomic(vaddr); 767a6686f2fSShirley Ma page->private = (unsigned long)head; 768a6686f2fSShirley Ma head = page; 769a6686f2fSShirley Ma } 770a6686f2fSShirley Ma 771a6686f2fSShirley Ma /* skb frags release userspace buffers */ 77202756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 773a8605c60SIan Campbell skb_frag_unref(skb, i); 774a6686f2fSShirley Ma 775a6686f2fSShirley Ma uarg->callback(uarg); 776a6686f2fSShirley Ma 777a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 77802756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 77902756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 78002756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 781a6686f2fSShirley Ma head = (struct page *)head->private; 782a6686f2fSShirley Ma } 78348c83012SMichael S. Tsirkin 78448c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 785a6686f2fSShirley Ma return 0; 786a6686f2fSShirley Ma } 787a6686f2fSShirley Ma 788a6686f2fSShirley Ma 789e0053ec0SHerbert Xu /** 790e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 791e0053ec0SHerbert Xu * @skb: buffer to clone 792e0053ec0SHerbert Xu * @gfp_mask: allocation priority 793e0053ec0SHerbert Xu * 794e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 795e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 796e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 797e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 798e0053ec0SHerbert Xu * 799e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 800e0053ec0SHerbert Xu * %GFP_ATOMIC. 801e0053ec0SHerbert Xu */ 802e0053ec0SHerbert Xu 803e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 804e0053ec0SHerbert Xu { 805e0053ec0SHerbert Xu struct sk_buff *n; 806e0053ec0SHerbert Xu 80770008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 808a6686f2fSShirley Ma return NULL; 809a6686f2fSShirley Ma 810e0053ec0SHerbert Xu n = skb + 1; 811e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 812e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 813e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 814e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 815e0053ec0SHerbert Xu atomic_inc(fclone_ref); 816e0053ec0SHerbert Xu } else { 817e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 818e0053ec0SHerbert Xu if (!n) 819e0053ec0SHerbert Xu return NULL; 820fe55f6d5SVegard Nossum 821fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 822fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags2); 823e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 824e0053ec0SHerbert Xu } 825e0053ec0SHerbert Xu 826e0053ec0SHerbert Xu return __skb_clone(n, skb); 8271da177e4SLinus Torvalds } 828b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 8311da177e4SLinus Torvalds { 8322e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 8331da177e4SLinus Torvalds /* 8341da177e4SLinus Torvalds * Shift between the two data areas in bytes 8351da177e4SLinus Torvalds */ 8361da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 8372e07fa9cSArnaldo Carvalho de Melo #endif 838dec18810SHerbert Xu 839dec18810SHerbert Xu __copy_skb_header(new, old); 840dec18810SHerbert Xu 8412e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 8422e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 8432e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 8442e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 845603a8bbeSStephen Hemminger if (skb_mac_header_was_set(new)) 8462e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 8472e07fa9cSArnaldo Carvalho de Melo #endif 8487967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 8497967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 8507967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds /** 8541da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 8551da177e4SLinus Torvalds * @skb: buffer to copy 8561da177e4SLinus Torvalds * @gfp_mask: allocation priority 8571da177e4SLinus Torvalds * 8581da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 8591da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 8601da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 8611da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 8621da177e4SLinus Torvalds * 8631da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 8641da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 8651da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 8661da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 8671da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 8681da177e4SLinus Torvalds */ 8691da177e4SLinus Torvalds 870dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 8711da177e4SLinus Torvalds { 8726602cebbSEric Dumazet int headerlen = skb_headroom(skb); 873ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 8746602cebbSEric Dumazet struct sk_buff *n = alloc_skb(size, gfp_mask); 8756602cebbSEric Dumazet 8761da177e4SLinus Torvalds if (!n) 8771da177e4SLinus Torvalds return NULL; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* Set the data pointer */ 8801da177e4SLinus Torvalds skb_reserve(n, headerlen); 8811da177e4SLinus Torvalds /* Set the tail pointer and length */ 8821da177e4SLinus Torvalds skb_put(n, skb->len); 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 8851da177e4SLinus Torvalds BUG(); 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds copy_skb_header(n, skb); 8881da177e4SLinus Torvalds return n; 8891da177e4SLinus Torvalds } 890b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /** 893117632e6SEric Dumazet * __pskb_copy - create copy of an sk_buff with private head. 8941da177e4SLinus Torvalds * @skb: buffer to copy 895117632e6SEric Dumazet * @headroom: headroom of new skb 8961da177e4SLinus Torvalds * @gfp_mask: allocation priority 8971da177e4SLinus Torvalds * 8981da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 8991da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 9001da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 9011da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 9021da177e4SLinus Torvalds * or the pointer to the buffer on success. 9031da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds 906117632e6SEric Dumazet struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask) 9071da177e4SLinus Torvalds { 908117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 9096602cebbSEric Dumazet struct sk_buff *n = alloc_skb(size, gfp_mask); 9106602cebbSEric Dumazet 9111da177e4SLinus Torvalds if (!n) 9121da177e4SLinus Torvalds goto out; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* Set the data pointer */ 915117632e6SEric Dumazet skb_reserve(n, headroom); 9161da177e4SLinus Torvalds /* Set the tail pointer and length */ 9171da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 9181da177e4SLinus Torvalds /* Copy the bytes */ 919d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 9201da177e4SLinus Torvalds 92125f484a6SHerbert Xu n->truesize += skb->data_len; 9221da177e4SLinus Torvalds n->data_len = skb->data_len; 9231da177e4SLinus Torvalds n->len = skb->len; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 9261da177e4SLinus Torvalds int i; 9271da177e4SLinus Torvalds 92870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 9291511022cSDan Carpenter kfree_skb(n); 9301511022cSDan Carpenter n = NULL; 931a6686f2fSShirley Ma goto out; 932a6686f2fSShirley Ma } 9331da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 9341da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 935ea2ab693SIan Campbell skb_frag_ref(skb, i); 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 94021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 9411da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 9421da177e4SLinus Torvalds skb_clone_fraglist(n); 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds copy_skb_header(n, skb); 9461da177e4SLinus Torvalds out: 9471da177e4SLinus Torvalds return n; 9481da177e4SLinus Torvalds } 949117632e6SEric Dumazet EXPORT_SYMBOL(__pskb_copy); 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /** 9521da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 9531da177e4SLinus Torvalds * @skb: buffer to reallocate 9541da177e4SLinus Torvalds * @nhead: room to add at head 9551da177e4SLinus Torvalds * @ntail: room to add at tail 9561da177e4SLinus Torvalds * @gfp_mask: allocation priority 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 9591da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 9601da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 9611da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 9621da177e4SLinus Torvalds * 9631da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 9641da177e4SLinus Torvalds * reloaded after call to this function. 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds 96786a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 968dd0fc66fSAl Viro gfp_t gfp_mask) 9691da177e4SLinus Torvalds { 9701da177e4SLinus Torvalds int i; 9711da177e4SLinus Torvalds u8 *data; 972ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 9731da177e4SLinus Torvalds long off; 9741da177e4SLinus Torvalds 9754edd87adSHerbert Xu BUG_ON(nhead < 0); 9764edd87adSHerbert Xu 9771da177e4SLinus Torvalds if (skb_shared(skb)) 9781da177e4SLinus Torvalds BUG(); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 9811da177e4SLinus Torvalds 98287151b86SEric Dumazet data = kmalloc(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 98387151b86SEric Dumazet gfp_mask); 9841da177e4SLinus Torvalds if (!data) 9851da177e4SLinus Torvalds goto nodata; 98687151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 9896602cebbSEric Dumazet * optimized for the cases when header is void. 9906602cebbSEric Dumazet */ 9916602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 9926602cebbSEric Dumazet 9936602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 9946602cebbSEric Dumazet skb_shinfo(skb), 995fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 9961da177e4SLinus Torvalds 9973e24591aSAlexander Duyck /* 9983e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 9993e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 10003e24591aSAlexander Duyck * be since all we did is relocate the values 10013e24591aSAlexander Duyck */ 10023e24591aSAlexander Duyck if (skb_cloned(skb)) { 1003a6686f2fSShirley Ma /* copy this zero copy skb frags */ 100470008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1005a6686f2fSShirley Ma goto nofrags; 10061da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1007ea2ab693SIan Campbell skb_frag_ref(skb, i); 10081da177e4SLinus Torvalds 100921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 10101da177e4SLinus Torvalds skb_clone_fraglist(skb); 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds skb_release_data(skb); 10133e24591aSAlexander Duyck } else { 10143e24591aSAlexander Duyck skb_free_head(skb); 10151fd63041SEric Dumazet } 10161da177e4SLinus Torvalds off = (data + nhead) - skb->head; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds skb->head = data; 1019d3836f21SEric Dumazet skb->head_frag = 0; 10201da177e4SLinus Torvalds skb->data += off; 10214305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 10224305b541SArnaldo Carvalho de Melo skb->end = size; 102356eb8882SPatrick McHardy off = nhead; 10244305b541SArnaldo Carvalho de Melo #else 10254305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 102656eb8882SPatrick McHardy #endif 102727a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 102827a884dcSArnaldo Carvalho de Melo skb->tail += off; 1029b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 1030b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 1031603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1032b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 103300c5a983SAndrea Shepard /* Only adjust this if it actually is csum_start rather than csum */ 103400c5a983SAndrea Shepard if (skb->ip_summed == CHECKSUM_PARTIAL) 1035172a863fSHerbert Xu skb->csum_start += nhead; 10361da177e4SLinus Torvalds skb->cloned = 0; 1037334a8132SPatrick McHardy skb->hdr_len = 0; 10381da177e4SLinus Torvalds skb->nohdr = 0; 10391da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 10401da177e4SLinus Torvalds return 0; 10411da177e4SLinus Torvalds 1042a6686f2fSShirley Ma nofrags: 1043a6686f2fSShirley Ma kfree(data); 10441da177e4SLinus Torvalds nodata: 10451da177e4SLinus Torvalds return -ENOMEM; 10461da177e4SLinus Torvalds } 1047b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds struct sk_buff *skb2; 10541da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (delta <= 0) 10571da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 10581da177e4SLinus Torvalds else { 10591da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 10601da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 10611da177e4SLinus Torvalds GFP_ATOMIC)) { 10621da177e4SLinus Torvalds kfree_skb(skb2); 10631da177e4SLinus Torvalds skb2 = NULL; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds return skb2; 10671da177e4SLinus Torvalds } 1068b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /** 10711da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 10721da177e4SLinus Torvalds * @skb: buffer to copy 10731da177e4SLinus Torvalds * @newheadroom: new free bytes at head 10741da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 10751da177e4SLinus Torvalds * @gfp_mask: allocation priority 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 10781da177e4SLinus Torvalds * allocate additional space. 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 10811da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 10821da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 10831da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10841da177e4SLinus Torvalds * 10851da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 10861da177e4SLinus Torvalds * is called from an interrupt. 10871da177e4SLinus Torvalds */ 10881da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 108986a76cafSVictor Fusco int newheadroom, int newtailroom, 1090dd0fc66fSAl Viro gfp_t gfp_mask) 10911da177e4SLinus Torvalds { 10921da177e4SLinus Torvalds /* 10931da177e4SLinus Torvalds * Allocate the copy buffer 10941da177e4SLinus Torvalds */ 10951da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 10961da177e4SLinus Torvalds gfp_mask); 1097efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 10981da177e4SLinus Torvalds int head_copy_len, head_copy_off; 109952886051SHerbert Xu int off; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds if (!n) 11021da177e4SLinus Torvalds return NULL; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds skb_reserve(n, newheadroom); 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* Set the tail pointer and length */ 11071da177e4SLinus Torvalds skb_put(n, skb->len); 11081da177e4SLinus Torvalds 1109efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 11101da177e4SLinus Torvalds head_copy_off = 0; 11111da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 11121da177e4SLinus Torvalds head_copy_len = newheadroom; 11131da177e4SLinus Torvalds else 11141da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /* Copy the linear header and data. */ 11171da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 11181da177e4SLinus Torvalds skb->len + head_copy_len)) 11191da177e4SLinus Torvalds BUG(); 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds copy_skb_header(n, skb); 11221da177e4SLinus Torvalds 1123efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 1124be2b6e62SDavid S. Miller if (n->ip_summed == CHECKSUM_PARTIAL) 112552886051SHerbert Xu n->csum_start += off; 112652886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 1127efd1e8d5SPatrick McHardy n->transport_header += off; 1128efd1e8d5SPatrick McHardy n->network_header += off; 1129603a8bbeSStephen Hemminger if (skb_mac_header_was_set(skb)) 1130efd1e8d5SPatrick McHardy n->mac_header += off; 113152886051SHerbert Xu #endif 1132efd1e8d5SPatrick McHardy 11331da177e4SLinus Torvalds return n; 11341da177e4SLinus Torvalds } 1135b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /** 11381da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 11391da177e4SLinus Torvalds * @skb: buffer to pad 11401da177e4SLinus Torvalds * @pad: space to pad 11411da177e4SLinus Torvalds * 11421da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 11431da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 11441da177e4SLinus Torvalds * beyond the buffer end onto the wire. 11451da177e4SLinus Torvalds * 11465b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 11471da177e4SLinus Torvalds */ 11481da177e4SLinus Torvalds 11495b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 11501da177e4SLinus Torvalds { 11515b057c6bSHerbert Xu int err; 11525b057c6bSHerbert Xu int ntail; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 11555b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 11561da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 11575b057c6bSHerbert Xu return 0; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11604305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 11615b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 11625b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 11635b057c6bSHerbert Xu if (unlikely(err)) 11645b057c6bSHerbert Xu goto free_skb; 11655b057c6bSHerbert Xu } 11665b057c6bSHerbert Xu 11675b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 11685b057c6bSHerbert Xu * to be audited. 11695b057c6bSHerbert Xu */ 11705b057c6bSHerbert Xu err = skb_linearize(skb); 11715b057c6bSHerbert Xu if (unlikely(err)) 11725b057c6bSHerbert Xu goto free_skb; 11735b057c6bSHerbert Xu 11745b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 11755b057c6bSHerbert Xu return 0; 11765b057c6bSHerbert Xu 11775b057c6bSHerbert Xu free_skb: 11781da177e4SLinus Torvalds kfree_skb(skb); 11795b057c6bSHerbert Xu return err; 11801da177e4SLinus Torvalds } 1181b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 11821da177e4SLinus Torvalds 11830dde3e16SIlpo Järvinen /** 11840dde3e16SIlpo Järvinen * skb_put - add data to a buffer 11850dde3e16SIlpo Järvinen * @skb: buffer to use 11860dde3e16SIlpo Järvinen * @len: amount of data to add 11870dde3e16SIlpo Järvinen * 11880dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 11890dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 11900dde3e16SIlpo Järvinen * first byte of the extra data is returned. 11910dde3e16SIlpo Järvinen */ 11920dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 11930dde3e16SIlpo Järvinen { 11940dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 11950dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 11960dde3e16SIlpo Järvinen skb->tail += len; 11970dde3e16SIlpo Järvinen skb->len += len; 11980dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 11990dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 12000dde3e16SIlpo Järvinen return tmp; 12010dde3e16SIlpo Järvinen } 12020dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 12030dde3e16SIlpo Järvinen 12046be8ac2fSIlpo Järvinen /** 1205c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1206c2aa270aSIlpo Järvinen * @skb: buffer to use 1207c2aa270aSIlpo Järvinen * @len: amount of data to add 1208c2aa270aSIlpo Järvinen * 1209c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1210c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1211c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1212c2aa270aSIlpo Järvinen */ 1213c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1214c2aa270aSIlpo Järvinen { 1215c2aa270aSIlpo Järvinen skb->data -= len; 1216c2aa270aSIlpo Järvinen skb->len += len; 1217c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1218c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1219c2aa270aSIlpo Järvinen return skb->data; 1220c2aa270aSIlpo Järvinen } 1221c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1222c2aa270aSIlpo Järvinen 1223c2aa270aSIlpo Järvinen /** 12246be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 12256be8ac2fSIlpo Järvinen * @skb: buffer to use 12266be8ac2fSIlpo Järvinen * @len: amount of data to remove 12276be8ac2fSIlpo Järvinen * 12286be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 12296be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 12306be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 12316be8ac2fSIlpo Järvinen * the old data. 12326be8ac2fSIlpo Järvinen */ 12336be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 12346be8ac2fSIlpo Järvinen { 123547d29646SDavid S. Miller return skb_pull_inline(skb, len); 12366be8ac2fSIlpo Järvinen } 12376be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 12386be8ac2fSIlpo Järvinen 1239419ae74eSIlpo Järvinen /** 1240419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1241419ae74eSIlpo Järvinen * @skb: buffer to alter 1242419ae74eSIlpo Järvinen * @len: new length 1243419ae74eSIlpo Järvinen * 1244419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1245419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1246419ae74eSIlpo Järvinen * The skb must be linear. 1247419ae74eSIlpo Järvinen */ 1248419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1249419ae74eSIlpo Järvinen { 1250419ae74eSIlpo Järvinen if (skb->len > len) 1251419ae74eSIlpo Järvinen __skb_trim(skb, len); 1252419ae74eSIlpo Järvinen } 1253419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1254419ae74eSIlpo Järvinen 12553cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 12561da177e4SLinus Torvalds */ 12571da177e4SLinus Torvalds 12583cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 12591da177e4SLinus Torvalds { 126027b437c8SHerbert Xu struct sk_buff **fragp; 126127b437c8SHerbert Xu struct sk_buff *frag; 12621da177e4SLinus Torvalds int offset = skb_headlen(skb); 12631da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 12641da177e4SLinus Torvalds int i; 126527b437c8SHerbert Xu int err; 126627b437c8SHerbert Xu 126727b437c8SHerbert Xu if (skb_cloned(skb) && 126827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 126927b437c8SHerbert Xu return err; 12701da177e4SLinus Torvalds 1271f4d26fb3SHerbert Xu i = 0; 1272f4d26fb3SHerbert Xu if (offset >= len) 1273f4d26fb3SHerbert Xu goto drop_pages; 1274f4d26fb3SHerbert Xu 1275f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 12769e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 127727b437c8SHerbert Xu 127827b437c8SHerbert Xu if (end < len) { 12791da177e4SLinus Torvalds offset = end; 128027b437c8SHerbert Xu continue; 12811da177e4SLinus Torvalds } 12821da177e4SLinus Torvalds 12839e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 128427b437c8SHerbert Xu 1285f4d26fb3SHerbert Xu drop_pages: 128627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 128727b437c8SHerbert Xu 128827b437c8SHerbert Xu for (; i < nfrags; i++) 1289ea2ab693SIan Campbell skb_frag_unref(skb, i); 129027b437c8SHerbert Xu 129121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 129227b437c8SHerbert Xu skb_drop_fraglist(skb); 1293f4d26fb3SHerbert Xu goto done; 129427b437c8SHerbert Xu } 129527b437c8SHerbert Xu 129627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 129727b437c8SHerbert Xu fragp = &frag->next) { 129827b437c8SHerbert Xu int end = offset + frag->len; 129927b437c8SHerbert Xu 130027b437c8SHerbert Xu if (skb_shared(frag)) { 130127b437c8SHerbert Xu struct sk_buff *nfrag; 130227b437c8SHerbert Xu 130327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 130427b437c8SHerbert Xu if (unlikely(!nfrag)) 130527b437c8SHerbert Xu return -ENOMEM; 130627b437c8SHerbert Xu 130727b437c8SHerbert Xu nfrag->next = frag->next; 130885bb2a60SEric Dumazet consume_skb(frag); 130927b437c8SHerbert Xu frag = nfrag; 131027b437c8SHerbert Xu *fragp = frag; 131127b437c8SHerbert Xu } 131227b437c8SHerbert Xu 131327b437c8SHerbert Xu if (end < len) { 131427b437c8SHerbert Xu offset = end; 131527b437c8SHerbert Xu continue; 131627b437c8SHerbert Xu } 131727b437c8SHerbert Xu 131827b437c8SHerbert Xu if (end > len && 131927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 132027b437c8SHerbert Xu return err; 132127b437c8SHerbert Xu 132227b437c8SHerbert Xu if (frag->next) 132327b437c8SHerbert Xu skb_drop_list(&frag->next); 132427b437c8SHerbert Xu break; 132527b437c8SHerbert Xu } 132627b437c8SHerbert Xu 1327f4d26fb3SHerbert Xu done: 132827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 13291da177e4SLinus Torvalds skb->data_len -= skb->len - len; 13301da177e4SLinus Torvalds skb->len = len; 13311da177e4SLinus Torvalds } else { 13321da177e4SLinus Torvalds skb->len = len; 13331da177e4SLinus Torvalds skb->data_len = 0; 133427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds return 0; 13381da177e4SLinus Torvalds } 1339b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds /** 13421da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 13431da177e4SLinus Torvalds * @skb: buffer to reallocate 13441da177e4SLinus Torvalds * @delta: number of bytes to advance tail 13451da177e4SLinus Torvalds * 13461da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 13471da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 13481da177e4SLinus Torvalds * data from fragmented part. 13491da177e4SLinus Torvalds * 13501da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 13511da177e4SLinus Torvalds * 13521da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 13531da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 13541da177e4SLinus Torvalds * 13551da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 13561da177e4SLinus Torvalds * reloaded after call to this function. 13571da177e4SLinus Torvalds */ 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 13601da177e4SLinus Torvalds * when it is necessary. 13611da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 13621da177e4SLinus Torvalds * 2. It may change skb pointers. 13631da177e4SLinus Torvalds * 13641da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 13651da177e4SLinus Torvalds */ 13661da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 13691da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 13701da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 13711da177e4SLinus Torvalds */ 13724305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 13751da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 13761da177e4SLinus Torvalds GFP_ATOMIC)) 13771da177e4SLinus Torvalds return NULL; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 138027a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 13811da177e4SLinus Torvalds BUG(); 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 13841da177e4SLinus Torvalds * size of pulled pages. Superb. 13851da177e4SLinus Torvalds */ 138621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 13871da177e4SLinus Torvalds goto pull_pages; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 13901da177e4SLinus Torvalds eat = delta; 13911da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 13929e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 13939e903e08SEric Dumazet 13949e903e08SEric Dumazet if (size >= eat) 13951da177e4SLinus Torvalds goto pull_pages; 13969e903e08SEric Dumazet eat -= size; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 14001da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 14011da177e4SLinus Torvalds * but taking into account that pulling is expected to 14021da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 14031da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 14041da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 14051da177e4SLinus Torvalds */ 14061da177e4SLinus Torvalds if (eat) { 14071da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 14081da177e4SLinus Torvalds struct sk_buff *clone = NULL; 14091da177e4SLinus Torvalds struct sk_buff *insp = NULL; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds do { 141209a62660SKris Katterjohn BUG_ON(!list); 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds if (list->len <= eat) { 14151da177e4SLinus Torvalds /* Eaten as whole. */ 14161da177e4SLinus Torvalds eat -= list->len; 14171da177e4SLinus Torvalds list = list->next; 14181da177e4SLinus Torvalds insp = list; 14191da177e4SLinus Torvalds } else { 14201da177e4SLinus Torvalds /* Eaten partially. */ 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds if (skb_shared(list)) { 14231da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 14241da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 14251da177e4SLinus Torvalds if (!clone) 14261da177e4SLinus Torvalds return NULL; 14271da177e4SLinus Torvalds insp = list->next; 14281da177e4SLinus Torvalds list = clone; 14291da177e4SLinus Torvalds } else { 14301da177e4SLinus Torvalds /* This may be pulled without 14311da177e4SLinus Torvalds * problems. */ 14321da177e4SLinus Torvalds insp = list; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 14351da177e4SLinus Torvalds kfree_skb(clone); 14361da177e4SLinus Torvalds return NULL; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds break; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds } while (eat); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds /* Free pulled out fragments. */ 14431da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 14441da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 14451da177e4SLinus Torvalds kfree_skb(list); 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds /* And insert new clone at head. */ 14481da177e4SLinus Torvalds if (clone) { 14491da177e4SLinus Torvalds clone->next = list; 14501da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds pull_pages: 14561da177e4SLinus Torvalds eat = delta; 14571da177e4SLinus Torvalds k = 0; 14581da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14599e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 14609e903e08SEric Dumazet 14619e903e08SEric Dumazet if (size <= eat) { 1462ea2ab693SIan Campbell skb_frag_unref(skb, i); 14639e903e08SEric Dumazet eat -= size; 14641da177e4SLinus Torvalds } else { 14651da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 14661da177e4SLinus Torvalds if (eat) { 14671da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 14689e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 14691da177e4SLinus Torvalds eat = 0; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds k++; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds skb->tail += delta; 14771da177e4SLinus Torvalds skb->data_len -= delta; 14781da177e4SLinus Torvalds 147927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 14801da177e4SLinus Torvalds } 1481b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 14821da177e4SLinus Torvalds 148322019b17SEric Dumazet /** 148422019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 148522019b17SEric Dumazet * @skb: source skb 148622019b17SEric Dumazet * @offset: offset in source 148722019b17SEric Dumazet * @to: destination buffer 148822019b17SEric Dumazet * @len: number of bytes to copy 148922019b17SEric Dumazet * 149022019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 149122019b17SEric Dumazet * destination buffer. 149222019b17SEric Dumazet * 149322019b17SEric Dumazet * CAUTION ! : 149422019b17SEric Dumazet * If its prototype is ever changed, 149522019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 149622019b17SEric Dumazet * since it is called from BPF assembly code. 149722019b17SEric Dumazet */ 14981da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 14991da177e4SLinus Torvalds { 15001a028e50SDavid S. Miller int start = skb_headlen(skb); 1501fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1502fbb398a8SDavid S. Miller int i, copy; 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds if (offset > (int)skb->len - len) 15051da177e4SLinus Torvalds goto fault; 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* Copy header. */ 15081a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 15091da177e4SLinus Torvalds if (copy > len) 15101da177e4SLinus Torvalds copy = len; 1511d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 15121da177e4SLinus Torvalds if ((len -= copy) == 0) 15131da177e4SLinus Torvalds return 0; 15141da177e4SLinus Torvalds offset += copy; 15151da177e4SLinus Torvalds to += copy; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15191a028e50SDavid S. Miller int end; 152051c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 15211da177e4SLinus Torvalds 1522547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15231a028e50SDavid S. Miller 152451c56b00SEric Dumazet end = start + skb_frag_size(f); 15251da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15261da177e4SLinus Torvalds u8 *vaddr; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds if (copy > len) 15291da177e4SLinus Torvalds copy = len; 15301da177e4SLinus Torvalds 153151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 15321da177e4SLinus Torvalds memcpy(to, 153351c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 153451c56b00SEric Dumazet copy); 153551c56b00SEric Dumazet kunmap_atomic(vaddr); 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds if ((len -= copy) == 0) 15381da177e4SLinus Torvalds return 0; 15391da177e4SLinus Torvalds offset += copy; 15401da177e4SLinus Torvalds to += copy; 15411da177e4SLinus Torvalds } 15421a028e50SDavid S. Miller start = end; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 1545fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 15461a028e50SDavid S. Miller int end; 15471da177e4SLinus Torvalds 1548547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15491a028e50SDavid S. Miller 1550fbb398a8SDavid S. Miller end = start + frag_iter->len; 15511da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15521da177e4SLinus Torvalds if (copy > len) 15531da177e4SLinus Torvalds copy = len; 1554fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 15551da177e4SLinus Torvalds goto fault; 15561da177e4SLinus Torvalds if ((len -= copy) == 0) 15571da177e4SLinus Torvalds return 0; 15581da177e4SLinus Torvalds offset += copy; 15591da177e4SLinus Torvalds to += copy; 15601da177e4SLinus Torvalds } 15611a028e50SDavid S. Miller start = end; 15621da177e4SLinus Torvalds } 1563a6686f2fSShirley Ma 15641da177e4SLinus Torvalds if (!len) 15651da177e4SLinus Torvalds return 0; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds fault: 15681da177e4SLinus Torvalds return -EFAULT; 15691da177e4SLinus Torvalds } 1570b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 15711da177e4SLinus Torvalds 15729c55e01cSJens Axboe /* 15739c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 15749c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 15759c55e01cSJens Axboe */ 15769c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 15779c55e01cSJens Axboe { 15788b9d3728SJarek Poplawski put_page(spd->pages[i]); 15798b9d3728SJarek Poplawski } 15809c55e01cSJens Axboe 1581a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 15824fb66994SJarek Poplawski unsigned int *offset, 15837a67e56fSJarek Poplawski struct sk_buff *skb, struct sock *sk) 15848b9d3728SJarek Poplawski { 15854fb66994SJarek Poplawski struct page *p = sk->sk_sndmsg_page; 15864fb66994SJarek Poplawski unsigned int off; 15878b9d3728SJarek Poplawski 15884fb66994SJarek Poplawski if (!p) { 15894fb66994SJarek Poplawski new_page: 15904fb66994SJarek Poplawski p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0); 15918b9d3728SJarek Poplawski if (!p) 15928b9d3728SJarek Poplawski return NULL; 15934fb66994SJarek Poplawski 15944fb66994SJarek Poplawski off = sk->sk_sndmsg_off = 0; 15954fb66994SJarek Poplawski /* hold one ref to this page until it's full */ 15964fb66994SJarek Poplawski } else { 15974fb66994SJarek Poplawski unsigned int mlen; 15984fb66994SJarek Poplawski 1599e66e9a31SEric Dumazet /* If we are the only user of the page, we can reset offset */ 1600e66e9a31SEric Dumazet if (page_count(p) == 1) 1601e66e9a31SEric Dumazet sk->sk_sndmsg_off = 0; 16024fb66994SJarek Poplawski off = sk->sk_sndmsg_off; 16034fb66994SJarek Poplawski mlen = PAGE_SIZE - off; 16044fb66994SJarek Poplawski if (mlen < 64 && mlen < *len) { 16054fb66994SJarek Poplawski put_page(p); 16064fb66994SJarek Poplawski goto new_page; 16074fb66994SJarek Poplawski } 16084fb66994SJarek Poplawski 16094fb66994SJarek Poplawski *len = min_t(unsigned int, *len, mlen); 16104fb66994SJarek Poplawski } 16114fb66994SJarek Poplawski 16124fb66994SJarek Poplawski memcpy(page_address(p) + off, page_address(page) + *offset, *len); 16134fb66994SJarek Poplawski sk->sk_sndmsg_off += *len; 16144fb66994SJarek Poplawski *offset = off; 16158b9d3728SJarek Poplawski 16168b9d3728SJarek Poplawski return p; 16179c55e01cSJens Axboe } 16189c55e01cSJens Axboe 161941c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 162041c73a0dSEric Dumazet struct page *page, 162141c73a0dSEric Dumazet unsigned int offset) 162241c73a0dSEric Dumazet { 162341c73a0dSEric Dumazet return spd->nr_pages && 162441c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 162541c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 162641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 162741c73a0dSEric Dumazet } 162841c73a0dSEric Dumazet 16299c55e01cSJens Axboe /* 16309c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 16319c55e01cSJens Axboe */ 1632a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 163335f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 16344fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 1635d7ccf7c0SEric Dumazet struct sk_buff *skb, bool linear, 16367a67e56fSJarek Poplawski struct sock *sk) 16379c55e01cSJens Axboe { 163841c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1639a108d5f3SDavid S. Miller return true; 16409c55e01cSJens Axboe 16418b9d3728SJarek Poplawski if (linear) { 16427a67e56fSJarek Poplawski page = linear_to_page(page, len, &offset, skb, sk); 16438b9d3728SJarek Poplawski if (!page) 1644a108d5f3SDavid S. Miller return true; 164541c73a0dSEric Dumazet } 164641c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 164741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1648a108d5f3SDavid S. Miller return false; 164941c73a0dSEric Dumazet } 16508b9d3728SJarek Poplawski get_page(page); 16519c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 16524fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 16539c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 16549c55e01cSJens Axboe spd->nr_pages++; 16558b9d3728SJarek Poplawski 1656a108d5f3SDavid S. Miller return false; 16579c55e01cSJens Axboe } 16589c55e01cSJens Axboe 16592870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 16602870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 16612870c43dSOctavian Purdila { 1662ce3dd395SJarek Poplawski unsigned long n; 1663ce3dd395SJarek Poplawski 16642870c43dSOctavian Purdila *poff += off; 1665ce3dd395SJarek Poplawski n = *poff / PAGE_SIZE; 1666ce3dd395SJarek Poplawski if (n) 1667ce3dd395SJarek Poplawski *page = nth_page(*page, n); 1668ce3dd395SJarek Poplawski 16692870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 16702870c43dSOctavian Purdila *plen -= off; 16712870c43dSOctavian Purdila } 16722870c43dSOctavian Purdila 1673a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 16742870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 16752870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 1676d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 167735f3d14dSJens Axboe struct sock *sk, 167835f3d14dSJens Axboe struct pipe_inode_info *pipe) 16799c55e01cSJens Axboe { 16802870c43dSOctavian Purdila if (!*len) 1681a108d5f3SDavid S. Miller return true; 16829c55e01cSJens Axboe 16832870c43dSOctavian Purdila /* skip this segment if already processed */ 16842870c43dSOctavian Purdila if (*off >= plen) { 16852870c43dSOctavian Purdila *off -= plen; 1686a108d5f3SDavid S. Miller return false; 16872870c43dSOctavian Purdila } 16882870c43dSOctavian Purdila 16892870c43dSOctavian Purdila /* ignore any bits we already processed */ 16902870c43dSOctavian Purdila if (*off) { 16912870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 16922870c43dSOctavian Purdila *off = 0; 16932870c43dSOctavian Purdila } 16942870c43dSOctavian Purdila 16952870c43dSOctavian Purdila do { 16962870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 16972870c43dSOctavian Purdila 16982870c43dSOctavian Purdila /* the linear region may spread across several pages */ 16992870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 17002870c43dSOctavian Purdila 170135f3d14dSJens Axboe if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk)) 1702a108d5f3SDavid S. Miller return true; 17032870c43dSOctavian Purdila 17042870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 17052870c43dSOctavian Purdila *len -= flen; 17062870c43dSOctavian Purdila 17072870c43dSOctavian Purdila } while (*len && plen); 17082870c43dSOctavian Purdila 1709a108d5f3SDavid S. Miller return false; 1710db43a282SOctavian Purdila } 17119c55e01cSJens Axboe 17129c55e01cSJens Axboe /* 1713a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 17142870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 17159c55e01cSJens Axboe */ 1716a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 171735f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 171835f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 17192870c43dSOctavian Purdila { 17202870c43dSOctavian Purdila int seg; 17219c55e01cSJens Axboe 17221d0c0b32SEric Dumazet /* map the linear part : 17232996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 17242996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 17252996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 17269c55e01cSJens Axboe */ 17272870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 17282870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 17292870c43dSOctavian Purdila skb_headlen(skb), 17301d0c0b32SEric Dumazet offset, len, skb, spd, 17313a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 17321d0c0b32SEric Dumazet sk, pipe)) 1733a108d5f3SDavid S. Miller return true; 17349c55e01cSJens Axboe 17359c55e01cSJens Axboe /* 17369c55e01cSJens Axboe * then map the fragments 17379c55e01cSJens Axboe */ 17389c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 17399c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 17409c55e01cSJens Axboe 1741ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 17429e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 1743d7ccf7c0SEric Dumazet offset, len, skb, spd, false, sk, pipe)) 1744a108d5f3SDavid S. Miller return true; 17459c55e01cSJens Axboe } 17469c55e01cSJens Axboe 1747a108d5f3SDavid S. Miller return false; 17489c55e01cSJens Axboe } 17499c55e01cSJens Axboe 17509c55e01cSJens Axboe /* 17519c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 17529c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 17539c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 17549c55e01cSJens Axboe * handle that cleanly. 17559c55e01cSJens Axboe */ 17568b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 17579c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 17589c55e01cSJens Axboe unsigned int flags) 17599c55e01cSJens Axboe { 176041c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 176141c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 17629c55e01cSJens Axboe struct splice_pipe_desc spd = { 17639c55e01cSJens Axboe .pages = pages, 17649c55e01cSJens Axboe .partial = partial, 1765047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 17669c55e01cSJens Axboe .flags = flags, 17679c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 17689c55e01cSJens Axboe .spd_release = sock_spd_release, 17699c55e01cSJens Axboe }; 1770fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 17717a67e56fSJarek Poplawski struct sock *sk = skb->sk; 177235f3d14dSJens Axboe int ret = 0; 177335f3d14dSJens Axboe 17749c55e01cSJens Axboe /* 17759c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 17769c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 17779c55e01cSJens Axboe */ 177835f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 17799c55e01cSJens Axboe goto done; 17809c55e01cSJens Axboe else if (!tlen) 17819c55e01cSJens Axboe goto done; 17829c55e01cSJens Axboe 17839c55e01cSJens Axboe /* 17849c55e01cSJens Axboe * now see if we have a frag_list to map 17859c55e01cSJens Axboe */ 1786fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1787fbb398a8SDavid S. Miller if (!tlen) 17889c55e01cSJens Axboe break; 178935f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1790fbb398a8SDavid S. Miller break; 17919c55e01cSJens Axboe } 17929c55e01cSJens Axboe 17939c55e01cSJens Axboe done: 17949c55e01cSJens Axboe if (spd.nr_pages) { 17959c55e01cSJens Axboe /* 17969c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 17979c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 17989c55e01cSJens Axboe * here as compared to sendfile(). We enter here 17999c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 18009c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 18019c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 18029c55e01cSJens Axboe * and networking will grab the socket lock. 18039c55e01cSJens Axboe */ 1804293ad604SOctavian Purdila release_sock(sk); 18059c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1806293ad604SOctavian Purdila lock_sock(sk); 18079c55e01cSJens Axboe } 18089c55e01cSJens Axboe 180935f3d14dSJens Axboe return ret; 18109c55e01cSJens Axboe } 18119c55e01cSJens Axboe 1812357b40a1SHerbert Xu /** 1813357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1814357b40a1SHerbert Xu * @skb: destination buffer 1815357b40a1SHerbert Xu * @offset: offset in destination 1816357b40a1SHerbert Xu * @from: source buffer 1817357b40a1SHerbert Xu * @len: number of bytes to copy 1818357b40a1SHerbert Xu * 1819357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1820357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1821357b40a1SHerbert Xu * traversing fragment lists and such. 1822357b40a1SHerbert Xu */ 1823357b40a1SHerbert Xu 18240c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1825357b40a1SHerbert Xu { 18261a028e50SDavid S. Miller int start = skb_headlen(skb); 1827fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1828fbb398a8SDavid S. Miller int i, copy; 1829357b40a1SHerbert Xu 1830357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1831357b40a1SHerbert Xu goto fault; 1832357b40a1SHerbert Xu 18331a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1834357b40a1SHerbert Xu if (copy > len) 1835357b40a1SHerbert Xu copy = len; 183627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1837357b40a1SHerbert Xu if ((len -= copy) == 0) 1838357b40a1SHerbert Xu return 0; 1839357b40a1SHerbert Xu offset += copy; 1840357b40a1SHerbert Xu from += copy; 1841357b40a1SHerbert Xu } 1842357b40a1SHerbert Xu 1843357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1844357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 18451a028e50SDavid S. Miller int end; 1846357b40a1SHerbert Xu 1847547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18481a028e50SDavid S. Miller 18499e903e08SEric Dumazet end = start + skb_frag_size(frag); 1850357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1851357b40a1SHerbert Xu u8 *vaddr; 1852357b40a1SHerbert Xu 1853357b40a1SHerbert Xu if (copy > len) 1854357b40a1SHerbert Xu copy = len; 1855357b40a1SHerbert Xu 185651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 18571a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 18581a028e50SDavid S. Miller from, copy); 185951c56b00SEric Dumazet kunmap_atomic(vaddr); 1860357b40a1SHerbert Xu 1861357b40a1SHerbert Xu if ((len -= copy) == 0) 1862357b40a1SHerbert Xu return 0; 1863357b40a1SHerbert Xu offset += copy; 1864357b40a1SHerbert Xu from += copy; 1865357b40a1SHerbert Xu } 18661a028e50SDavid S. Miller start = end; 1867357b40a1SHerbert Xu } 1868357b40a1SHerbert Xu 1869fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 18701a028e50SDavid S. Miller int end; 1871357b40a1SHerbert Xu 1872547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18731a028e50SDavid S. Miller 1874fbb398a8SDavid S. Miller end = start + frag_iter->len; 1875357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1876357b40a1SHerbert Xu if (copy > len) 1877357b40a1SHerbert Xu copy = len; 1878fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 18791a028e50SDavid S. Miller from, copy)) 1880357b40a1SHerbert Xu goto fault; 1881357b40a1SHerbert Xu if ((len -= copy) == 0) 1882357b40a1SHerbert Xu return 0; 1883357b40a1SHerbert Xu offset += copy; 1884357b40a1SHerbert Xu from += copy; 1885357b40a1SHerbert Xu } 18861a028e50SDavid S. Miller start = end; 1887357b40a1SHerbert Xu } 1888357b40a1SHerbert Xu if (!len) 1889357b40a1SHerbert Xu return 0; 1890357b40a1SHerbert Xu 1891357b40a1SHerbert Xu fault: 1892357b40a1SHerbert Xu return -EFAULT; 1893357b40a1SHerbert Xu } 1894357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1895357b40a1SHerbert Xu 18961da177e4SLinus Torvalds /* Checksum skb data. */ 18971da177e4SLinus Torvalds 18982bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 18992bbbc868SAl Viro int len, __wsum csum) 19001da177e4SLinus Torvalds { 19011a028e50SDavid S. Miller int start = skb_headlen(skb); 19021a028e50SDavid S. Miller int i, copy = start - offset; 1903fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19041da177e4SLinus Torvalds int pos = 0; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds /* Checksum header. */ 19071da177e4SLinus Torvalds if (copy > 0) { 19081da177e4SLinus Torvalds if (copy > len) 19091da177e4SLinus Torvalds copy = len; 19101da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 19111da177e4SLinus Torvalds if ((len -= copy) == 0) 19121da177e4SLinus Torvalds return csum; 19131da177e4SLinus Torvalds offset += copy; 19141da177e4SLinus Torvalds pos = copy; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19181a028e50SDavid S. Miller int end; 191951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19201da177e4SLinus Torvalds 1921547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19221a028e50SDavid S. Miller 192351c56b00SEric Dumazet end = start + skb_frag_size(frag); 19241da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 192544bb9363SAl Viro __wsum csum2; 19261da177e4SLinus Torvalds u8 *vaddr; 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds if (copy > len) 19291da177e4SLinus Torvalds copy = len; 193051c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19311a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 19321a028e50SDavid S. Miller offset - start, copy, 0); 193351c56b00SEric Dumazet kunmap_atomic(vaddr); 19341da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19351da177e4SLinus Torvalds if (!(len -= copy)) 19361da177e4SLinus Torvalds return csum; 19371da177e4SLinus Torvalds offset += copy; 19381da177e4SLinus Torvalds pos += copy; 19391da177e4SLinus Torvalds } 19401a028e50SDavid S. Miller start = end; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 1943fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19441a028e50SDavid S. Miller int end; 19451da177e4SLinus Torvalds 1946547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19471a028e50SDavid S. Miller 1948fbb398a8SDavid S. Miller end = start + frag_iter->len; 19491da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19505f92a738SAl Viro __wsum csum2; 19511da177e4SLinus Torvalds if (copy > len) 19521da177e4SLinus Torvalds copy = len; 1953fbb398a8SDavid S. Miller csum2 = skb_checksum(frag_iter, offset - start, 19541a028e50SDavid S. Miller copy, 0); 19551da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 19561da177e4SLinus Torvalds if ((len -= copy) == 0) 19571da177e4SLinus Torvalds return csum; 19581da177e4SLinus Torvalds offset += copy; 19591da177e4SLinus Torvalds pos += copy; 19601da177e4SLinus Torvalds } 19611a028e50SDavid S. Miller start = end; 19621da177e4SLinus Torvalds } 196309a62660SKris Katterjohn BUG_ON(len); 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds return csum; 19661da177e4SLinus Torvalds } 1967b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds /* Both of above in one bottle. */ 19701da177e4SLinus Torvalds 197181d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 197281d77662SAl Viro u8 *to, int len, __wsum csum) 19731da177e4SLinus Torvalds { 19741a028e50SDavid S. Miller int start = skb_headlen(skb); 19751a028e50SDavid S. Miller int i, copy = start - offset; 1976fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19771da177e4SLinus Torvalds int pos = 0; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* Copy header. */ 19801da177e4SLinus Torvalds if (copy > 0) { 19811da177e4SLinus Torvalds if (copy > len) 19821da177e4SLinus Torvalds copy = len; 19831da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 19841da177e4SLinus Torvalds copy, csum); 19851da177e4SLinus Torvalds if ((len -= copy) == 0) 19861da177e4SLinus Torvalds return csum; 19871da177e4SLinus Torvalds offset += copy; 19881da177e4SLinus Torvalds to += copy; 19891da177e4SLinus Torvalds pos = copy; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19931a028e50SDavid S. Miller int end; 19941da177e4SLinus Torvalds 1995547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19961a028e50SDavid S. Miller 19979e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 19981da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19995084205fSAl Viro __wsum csum2; 20001da177e4SLinus Torvalds u8 *vaddr; 20011da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (copy > len) 20041da177e4SLinus Torvalds copy = len; 200551c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20061da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 20071a028e50SDavid S. Miller frag->page_offset + 20081a028e50SDavid S. Miller offset - start, to, 20091a028e50SDavid S. Miller copy, 0); 201051c56b00SEric Dumazet kunmap_atomic(vaddr); 20111da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20121da177e4SLinus Torvalds if (!(len -= copy)) 20131da177e4SLinus Torvalds return csum; 20141da177e4SLinus Torvalds offset += copy; 20151da177e4SLinus Torvalds to += copy; 20161da177e4SLinus Torvalds pos += copy; 20171da177e4SLinus Torvalds } 20181a028e50SDavid S. Miller start = end; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 2021fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 202281d77662SAl Viro __wsum csum2; 20231a028e50SDavid S. Miller int end; 20241da177e4SLinus Torvalds 2025547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20261a028e50SDavid S. Miller 2027fbb398a8SDavid S. Miller end = start + frag_iter->len; 20281da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20291da177e4SLinus Torvalds if (copy > len) 20301da177e4SLinus Torvalds copy = len; 2031fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 20321a028e50SDavid S. Miller offset - start, 20331da177e4SLinus Torvalds to, copy, 0); 20341da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20351da177e4SLinus Torvalds if ((len -= copy) == 0) 20361da177e4SLinus Torvalds return csum; 20371da177e4SLinus Torvalds offset += copy; 20381da177e4SLinus Torvalds to += copy; 20391da177e4SLinus Torvalds pos += copy; 20401da177e4SLinus Torvalds } 20411a028e50SDavid S. Miller start = end; 20421da177e4SLinus Torvalds } 204309a62660SKris Katterjohn BUG_ON(len); 20441da177e4SLinus Torvalds return csum; 20451da177e4SLinus Torvalds } 2046b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 20491da177e4SLinus Torvalds { 2050d3bc23e7SAl Viro __wsum csum; 20511da177e4SLinus Torvalds long csstart; 20521da177e4SLinus Torvalds 205384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 205455508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 20551da177e4SLinus Torvalds else 20561da177e4SLinus Torvalds csstart = skb_headlen(skb); 20571da177e4SLinus Torvalds 205809a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 20591da177e4SLinus Torvalds 2060d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds csum = 0; 20631da177e4SLinus Torvalds if (csstart != skb->len) 20641da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 20651da177e4SLinus Torvalds skb->len - csstart, 0); 20661da177e4SLinus Torvalds 206784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2068ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 20691da177e4SLinus Torvalds 2070d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds } 2073b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds /** 20761da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 20771da177e4SLinus Torvalds * @list: list to dequeue from 20781da177e4SLinus Torvalds * 20791da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 20801da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 20811da177e4SLinus Torvalds * returned or %NULL if the list is empty. 20821da177e4SLinus Torvalds */ 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 20851da177e4SLinus Torvalds { 20861da177e4SLinus Torvalds unsigned long flags; 20871da177e4SLinus Torvalds struct sk_buff *result; 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 20901da177e4SLinus Torvalds result = __skb_dequeue(list); 20911da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 20921da177e4SLinus Torvalds return result; 20931da177e4SLinus Torvalds } 2094b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /** 20971da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 20981da177e4SLinus Torvalds * @list: list to dequeue from 20991da177e4SLinus Torvalds * 21001da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 21011da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 21021da177e4SLinus Torvalds * returned or %NULL if the list is empty. 21031da177e4SLinus Torvalds */ 21041da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 21051da177e4SLinus Torvalds { 21061da177e4SLinus Torvalds unsigned long flags; 21071da177e4SLinus Torvalds struct sk_buff *result; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21101da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 21111da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21121da177e4SLinus Torvalds return result; 21131da177e4SLinus Torvalds } 2114b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds /** 21171da177e4SLinus Torvalds * skb_queue_purge - empty a list 21181da177e4SLinus Torvalds * @list: list to empty 21191da177e4SLinus Torvalds * 21201da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 21211da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 21221da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 21231da177e4SLinus Torvalds */ 21241da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 21251da177e4SLinus Torvalds { 21261da177e4SLinus Torvalds struct sk_buff *skb; 21271da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 21281da177e4SLinus Torvalds kfree_skb(skb); 21291da177e4SLinus Torvalds } 2130b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds /** 21331da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 21341da177e4SLinus Torvalds * @list: list to use 21351da177e4SLinus Torvalds * @newsk: buffer to queue 21361da177e4SLinus Torvalds * 21371da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 21381da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21391da177e4SLinus Torvalds * safely. 21401da177e4SLinus Torvalds * 21411da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21421da177e4SLinus Torvalds */ 21431da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 21441da177e4SLinus Torvalds { 21451da177e4SLinus Torvalds unsigned long flags; 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21481da177e4SLinus Torvalds __skb_queue_head(list, newsk); 21491da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21501da177e4SLinus Torvalds } 2151b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds /** 21541da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 21551da177e4SLinus Torvalds * @list: list to use 21561da177e4SLinus Torvalds * @newsk: buffer to queue 21571da177e4SLinus Torvalds * 21581da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 21591da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 21601da177e4SLinus Torvalds * safely. 21611da177e4SLinus Torvalds * 21621da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 21631da177e4SLinus Torvalds */ 21641da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds unsigned long flags; 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21691da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 21701da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21711da177e4SLinus Torvalds } 2172b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 21738728b834SDavid S. Miller 21741da177e4SLinus Torvalds /** 21751da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 21761da177e4SLinus Torvalds * @skb: buffer to remove 21778728b834SDavid S. Miller * @list: list to use 21781da177e4SLinus Torvalds * 21798728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 21808728b834SDavid S. Miller * function is atomic with respect to other list locked calls 21811da177e4SLinus Torvalds * 21828728b834SDavid S. Miller * You must know what list the SKB is on. 21831da177e4SLinus Torvalds */ 21848728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 21851da177e4SLinus Torvalds { 21861da177e4SLinus Torvalds unsigned long flags; 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 21898728b834SDavid S. Miller __skb_unlink(skb, list); 21901da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 21911da177e4SLinus Torvalds } 2192b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds /** 21951da177e4SLinus Torvalds * skb_append - append a buffer 21961da177e4SLinus Torvalds * @old: buffer to insert after 21971da177e4SLinus Torvalds * @newsk: buffer to insert 21988728b834SDavid S. Miller * @list: list to use 21991da177e4SLinus Torvalds * 22001da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 22011da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 22021da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 22031da177e4SLinus Torvalds */ 22048728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 22051da177e4SLinus Torvalds { 22061da177e4SLinus Torvalds unsigned long flags; 22071da177e4SLinus Torvalds 22088728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 22097de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 22108728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 22111da177e4SLinus Torvalds } 2212b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds /** 22151da177e4SLinus Torvalds * skb_insert - insert a buffer 22161da177e4SLinus Torvalds * @old: buffer to insert before 22171da177e4SLinus Torvalds * @newsk: buffer to insert 22188728b834SDavid S. Miller * @list: list to use 22191da177e4SLinus Torvalds * 22208728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 22218728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 22228728b834SDavid S. Miller * calls. 22238728b834SDavid S. Miller * 22241da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 22251da177e4SLinus Torvalds */ 22268728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds unsigned long flags; 22291da177e4SLinus Torvalds 22308728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 22318728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 22328728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 22331da177e4SLinus Torvalds } 2234b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 22371da177e4SLinus Torvalds struct sk_buff* skb1, 22381da177e4SLinus Torvalds const u32 len, const int pos) 22391da177e4SLinus Torvalds { 22401da177e4SLinus Torvalds int i; 22411da177e4SLinus Torvalds 2242d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2243d626f62bSArnaldo Carvalho de Melo pos - len); 22441da177e4SLinus Torvalds /* And move data appendix as is. */ 22451da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 22461da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 22491da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22501da177e4SLinus Torvalds skb1->data_len = skb->data_len; 22511da177e4SLinus Torvalds skb1->len += skb1->data_len; 22521da177e4SLinus Torvalds skb->data_len = 0; 22531da177e4SLinus Torvalds skb->len = len; 225427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 22581da177e4SLinus Torvalds struct sk_buff* skb1, 22591da177e4SLinus Torvalds const u32 len, int pos) 22601da177e4SLinus Torvalds { 22611da177e4SLinus Torvalds int i, k = 0; 22621da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 22631da177e4SLinus Torvalds 22641da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 22651da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 22661da177e4SLinus Torvalds skb->len = len; 22671da177e4SLinus Torvalds skb->data_len = len - pos; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 22709e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds if (pos + size > len) { 22731da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds if (pos < len) { 22761da177e4SLinus Torvalds /* Split frag. 22771da177e4SLinus Torvalds * We have two variants in this case: 22781da177e4SLinus Torvalds * 1. Move all the frag to the second 22791da177e4SLinus Torvalds * part, if it is possible. F.e. 22801da177e4SLinus Torvalds * this approach is mandatory for TUX, 22811da177e4SLinus Torvalds * where splitting is expensive. 22821da177e4SLinus Torvalds * 2. Split is accurately. We make this. 22831da177e4SLinus Torvalds */ 2284ea2ab693SIan Campbell skb_frag_ref(skb, i); 22851da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 22869e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 22879e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 22881da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 22891da177e4SLinus Torvalds } 22901da177e4SLinus Torvalds k++; 22911da177e4SLinus Torvalds } else 22921da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 22931da177e4SLinus Torvalds pos += size; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds /** 22991da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 23001da177e4SLinus Torvalds * @skb: the buffer to split 23011da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 23021da177e4SLinus Torvalds * @len: new length for skb 23031da177e4SLinus Torvalds */ 23041da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 23051da177e4SLinus Torvalds { 23061da177e4SLinus Torvalds int pos = skb_headlen(skb); 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 23091da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 23101da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 23111da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 23121da177e4SLinus Torvalds } 2313b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 23141da177e4SLinus Torvalds 23159f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 23169f782db3SIlpo Järvinen * 23179f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 23189f782db3SIlpo Järvinen */ 2319832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2320832d11c5SIlpo Järvinen { 23210ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2322832d11c5SIlpo Järvinen } 2323832d11c5SIlpo Järvinen 2324832d11c5SIlpo Järvinen /** 2325832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2326832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2327832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2328832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2329832d11c5SIlpo Järvinen * 2330832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 233120e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2332832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2333832d11c5SIlpo Järvinen * 2334832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2335832d11c5SIlpo Järvinen * 2336832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2337832d11c5SIlpo Järvinen * to have non-paged data as well. 2338832d11c5SIlpo Järvinen * 2339832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2340832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2341832d11c5SIlpo Järvinen */ 2342832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2343832d11c5SIlpo Järvinen { 2344832d11c5SIlpo Järvinen int from, to, merge, todo; 2345832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2346832d11c5SIlpo Järvinen 2347832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2348832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2349832d11c5SIlpo Järvinen 2350832d11c5SIlpo Järvinen todo = shiftlen; 2351832d11c5SIlpo Järvinen from = 0; 2352832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2353832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2354832d11c5SIlpo Järvinen 2355832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2356832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2357832d11c5SIlpo Järvinen */ 2358832d11c5SIlpo Järvinen if (!to || 2359ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2360ea2ab693SIan Campbell fragfrom->page_offset)) { 2361832d11c5SIlpo Järvinen merge = -1; 2362832d11c5SIlpo Järvinen } else { 2363832d11c5SIlpo Järvinen merge = to - 1; 2364832d11c5SIlpo Järvinen 23659e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2366832d11c5SIlpo Järvinen if (todo < 0) { 2367832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2368832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2369832d11c5SIlpo Järvinen return 0; 2370832d11c5SIlpo Järvinen 23719f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 23729f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2373832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2374832d11c5SIlpo Järvinen 23759e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 23769e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2377832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2378832d11c5SIlpo Järvinen 2379832d11c5SIlpo Järvinen goto onlymerged; 2380832d11c5SIlpo Järvinen } 2381832d11c5SIlpo Järvinen 2382832d11c5SIlpo Järvinen from++; 2383832d11c5SIlpo Järvinen } 2384832d11c5SIlpo Järvinen 2385832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2386832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2387832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2388832d11c5SIlpo Järvinen return 0; 2389832d11c5SIlpo Järvinen 2390832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2391832d11c5SIlpo Järvinen return 0; 2392832d11c5SIlpo Järvinen 2393832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2394832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2395832d11c5SIlpo Järvinen return 0; 2396832d11c5SIlpo Järvinen 2397832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2398832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2399832d11c5SIlpo Järvinen 24009e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2401832d11c5SIlpo Järvinen *fragto = *fragfrom; 24029e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2403832d11c5SIlpo Järvinen from++; 2404832d11c5SIlpo Järvinen to++; 2405832d11c5SIlpo Järvinen 2406832d11c5SIlpo Järvinen } else { 2407ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2408832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2409832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 24109e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2411832d11c5SIlpo Järvinen 2412832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 24139e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2414832d11c5SIlpo Järvinen todo = 0; 2415832d11c5SIlpo Järvinen 2416832d11c5SIlpo Järvinen to++; 2417832d11c5SIlpo Järvinen break; 2418832d11c5SIlpo Järvinen } 2419832d11c5SIlpo Järvinen } 2420832d11c5SIlpo Järvinen 2421832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2422832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2423832d11c5SIlpo Järvinen 2424832d11c5SIlpo Järvinen if (merge >= 0) { 2425832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2426832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2427832d11c5SIlpo Järvinen 24289e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2429ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2430832d11c5SIlpo Järvinen } 2431832d11c5SIlpo Järvinen 2432832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2433832d11c5SIlpo Järvinen to = 0; 2434832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2435832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2436832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2437832d11c5SIlpo Järvinen 2438832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2439832d11c5SIlpo Järvinen 2440832d11c5SIlpo Järvinen onlymerged: 2441832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2442832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2443832d11c5SIlpo Järvinen */ 2444832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2445832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2446832d11c5SIlpo Järvinen 2447832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2448832d11c5SIlpo Järvinen skb->len -= shiftlen; 2449832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2450832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2451832d11c5SIlpo Järvinen tgt->len += shiftlen; 2452832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2453832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2454832d11c5SIlpo Järvinen 2455832d11c5SIlpo Järvinen return shiftlen; 2456832d11c5SIlpo Järvinen } 2457832d11c5SIlpo Järvinen 2458677e90edSThomas Graf /** 2459677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2460677e90edSThomas Graf * @skb: the buffer to read 2461677e90edSThomas Graf * @from: lower offset of data to be read 2462677e90edSThomas Graf * @to: upper offset of data to be read 2463677e90edSThomas Graf * @st: state variable 2464677e90edSThomas Graf * 2465677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2466677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2467677e90edSThomas Graf */ 2468677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2469677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2470677e90edSThomas Graf { 2471677e90edSThomas Graf st->lower_offset = from; 2472677e90edSThomas Graf st->upper_offset = to; 2473677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2474677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2475677e90edSThomas Graf st->frag_data = NULL; 2476677e90edSThomas Graf } 2477b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2478677e90edSThomas Graf 2479677e90edSThomas Graf /** 2480677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2481677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2482677e90edSThomas Graf * @data: destination pointer for data to be returned 2483677e90edSThomas Graf * @st: state variable 2484677e90edSThomas Graf * 2485677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 2486677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2487677e90edSThomas Graf * the head of the data block to &data and returns the length 2488677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2489677e90edSThomas Graf * offset has been reached. 2490677e90edSThomas Graf * 2491677e90edSThomas Graf * The caller is not required to consume all of the data 2492677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 2493677e90edSThomas Graf * of bytes already consumed and the next call to 2494677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2495677e90edSThomas Graf * 249625985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2497677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 2498677e90edSThomas Graf * reads of potentially non linear data. 2499677e90edSThomas Graf * 2500bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2501677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2502677e90edSThomas Graf * a stack for this purpose. 2503677e90edSThomas Graf */ 2504677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2505677e90edSThomas Graf struct skb_seq_state *st) 2506677e90edSThomas Graf { 2507677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2508677e90edSThomas Graf skb_frag_t *frag; 2509677e90edSThomas Graf 2510677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2511677e90edSThomas Graf return 0; 2512677e90edSThomas Graf 2513677e90edSThomas Graf next_skb: 251495e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2515677e90edSThomas Graf 2516995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 251795e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2518677e90edSThomas Graf return block_limit - abs_offset; 2519677e90edSThomas Graf } 2520677e90edSThomas Graf 2521677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2522677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2523677e90edSThomas Graf 2524677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2525677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 25269e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2527677e90edSThomas Graf 2528677e90edSThomas Graf if (abs_offset < block_limit) { 2529677e90edSThomas Graf if (!st->frag_data) 253051c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2531677e90edSThomas Graf 2532677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2533677e90edSThomas Graf (abs_offset - st->stepped_offset); 2534677e90edSThomas Graf 2535677e90edSThomas Graf return block_limit - abs_offset; 2536677e90edSThomas Graf } 2537677e90edSThomas Graf 2538677e90edSThomas Graf if (st->frag_data) { 253951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2540677e90edSThomas Graf st->frag_data = NULL; 2541677e90edSThomas Graf } 2542677e90edSThomas Graf 2543677e90edSThomas Graf st->frag_idx++; 25449e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2545677e90edSThomas Graf } 2546677e90edSThomas Graf 25475b5a60daSOlaf Kirch if (st->frag_data) { 254851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 25495b5a60daSOlaf Kirch st->frag_data = NULL; 25505b5a60daSOlaf Kirch } 25515b5a60daSOlaf Kirch 255221dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2553677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 255495e3b24cSHerbert Xu st->frag_idx = 0; 2555677e90edSThomas Graf goto next_skb; 255671b3346dSShyam Iyer } else if (st->cur_skb->next) { 255771b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 255871b3346dSShyam Iyer st->frag_idx = 0; 2559677e90edSThomas Graf goto next_skb; 2560677e90edSThomas Graf } 2561677e90edSThomas Graf 2562677e90edSThomas Graf return 0; 2563677e90edSThomas Graf } 2564b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2565677e90edSThomas Graf 2566677e90edSThomas Graf /** 2567677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2568677e90edSThomas Graf * @st: state variable 2569677e90edSThomas Graf * 2570677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2571677e90edSThomas Graf * returned 0. 2572677e90edSThomas Graf */ 2573677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2574677e90edSThomas Graf { 2575677e90edSThomas Graf if (st->frag_data) 257651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2577677e90edSThomas Graf } 2578b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2579677e90edSThomas Graf 25803fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 25813fc7e8a6SThomas Graf 25823fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 25833fc7e8a6SThomas Graf struct ts_config *conf, 25843fc7e8a6SThomas Graf struct ts_state *state) 25853fc7e8a6SThomas Graf { 25863fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 25873fc7e8a6SThomas Graf } 25883fc7e8a6SThomas Graf 25893fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 25903fc7e8a6SThomas Graf { 25913fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 25923fc7e8a6SThomas Graf } 25933fc7e8a6SThomas Graf 25943fc7e8a6SThomas Graf /** 25953fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 25963fc7e8a6SThomas Graf * @skb: the buffer to look in 25973fc7e8a6SThomas Graf * @from: search offset 25983fc7e8a6SThomas Graf * @to: search limit 25993fc7e8a6SThomas Graf * @config: textsearch configuration 26003fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 26013fc7e8a6SThomas Graf * 26023fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 26033fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 26043fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 26053fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 26063fc7e8a6SThomas Graf */ 26073fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 26083fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 26093fc7e8a6SThomas Graf struct ts_state *state) 26103fc7e8a6SThomas Graf { 2611f72b948dSPhil Oester unsigned int ret; 2612f72b948dSPhil Oester 26133fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 26143fc7e8a6SThomas Graf config->finish = skb_ts_finish; 26153fc7e8a6SThomas Graf 26163fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 26173fc7e8a6SThomas Graf 2618f72b948dSPhil Oester ret = textsearch_find(config, state); 2619f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 26203fc7e8a6SThomas Graf } 2621b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 26223fc7e8a6SThomas Graf 2623e89e9cf5SAnanda Raju /** 26242c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2625e89e9cf5SAnanda Raju * @sk: sock structure 2626e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2627e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2628e89e9cf5SAnanda Raju * @from: pointer to user message iov 2629e89e9cf5SAnanda Raju * @length: length of the iov message 2630e89e9cf5SAnanda Raju * 2631e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2632e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2633e89e9cf5SAnanda Raju */ 2634e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2635dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2636e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2637e89e9cf5SAnanda Raju void *from, int length) 2638e89e9cf5SAnanda Raju { 2639e89e9cf5SAnanda Raju int frg_cnt = 0; 2640e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2641e89e9cf5SAnanda Raju struct page *page = NULL; 2642e89e9cf5SAnanda Raju int copy, left; 2643e89e9cf5SAnanda Raju int offset = 0; 2644e89e9cf5SAnanda Raju int ret; 2645e89e9cf5SAnanda Raju 2646e89e9cf5SAnanda Raju do { 2647e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2648e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2649e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2650e89e9cf5SAnanda Raju return -EFAULT; 2651e89e9cf5SAnanda Raju 2652e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2653e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2654e89e9cf5SAnanda Raju 2655e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2656e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2657e89e9cf5SAnanda Raju */ 2658e89e9cf5SAnanda Raju if (page == NULL) 2659e89e9cf5SAnanda Raju return -ENOMEM; 2660e89e9cf5SAnanda Raju 2661e89e9cf5SAnanda Raju /* initialize the next frag */ 2662e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2663e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2664e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2665e89e9cf5SAnanda Raju 2666e89e9cf5SAnanda Raju /* get the new initialized frag */ 2667e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2668e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2669e89e9cf5SAnanda Raju 2670e89e9cf5SAnanda Raju /* copy the user data to page */ 2671e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2672e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2673e89e9cf5SAnanda Raju 26749e903e08SEric Dumazet ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag), 2675e89e9cf5SAnanda Raju offset, copy, 0, skb); 2676e89e9cf5SAnanda Raju if (ret < 0) 2677e89e9cf5SAnanda Raju return -EFAULT; 2678e89e9cf5SAnanda Raju 2679e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 26809e903e08SEric Dumazet skb_frag_size_add(frag, copy); 2681e89e9cf5SAnanda Raju skb->len += copy; 2682e89e9cf5SAnanda Raju skb->data_len += copy; 2683e89e9cf5SAnanda Raju offset += copy; 2684e89e9cf5SAnanda Raju length -= copy; 2685e89e9cf5SAnanda Raju 2686e89e9cf5SAnanda Raju } while (length > 0); 2687e89e9cf5SAnanda Raju 2688e89e9cf5SAnanda Raju return 0; 2689e89e9cf5SAnanda Raju } 2690b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2691e89e9cf5SAnanda Raju 2692cbb042f9SHerbert Xu /** 2693cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2694cbb042f9SHerbert Xu * @skb: buffer to update 2695cbb042f9SHerbert Xu * @len: length of data pulled 2696cbb042f9SHerbert Xu * 2697cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2698fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 269984fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 270084fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 270184fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2702cbb042f9SHerbert Xu */ 2703cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2704cbb042f9SHerbert Xu { 2705cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2706cbb042f9SHerbert Xu skb->len -= len; 2707cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2708cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2709cbb042f9SHerbert Xu return skb->data += len; 2710cbb042f9SHerbert Xu } 2711f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2712f94691acSArnaldo Carvalho de Melo 2713f4c50d99SHerbert Xu /** 2714f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2715f4c50d99SHerbert Xu * @skb: buffer to segment 2716576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2717f4c50d99SHerbert Xu * 2718f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 27194c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 27204c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2721f4c50d99SHerbert Xu */ 2722c8f44affSMichał Mirosław struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features) 2723f4c50d99SHerbert Xu { 2724f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2725f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 272689319d38SHerbert Xu struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2727f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 272898e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2729f4c50d99SHerbert Xu unsigned int offset = doffset; 2730f4c50d99SHerbert Xu unsigned int headroom; 2731f4c50d99SHerbert Xu unsigned int len; 273204ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2733f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2734f4c50d99SHerbert Xu int err = -ENOMEM; 2735f4c50d99SHerbert Xu int i = 0; 2736f4c50d99SHerbert Xu int pos; 2737f4c50d99SHerbert Xu 2738f4c50d99SHerbert Xu __skb_push(skb, doffset); 2739f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2740f4c50d99SHerbert Xu pos = skb_headlen(skb); 2741f4c50d99SHerbert Xu 2742f4c50d99SHerbert Xu do { 2743f4c50d99SHerbert Xu struct sk_buff *nskb; 2744f4c50d99SHerbert Xu skb_frag_t *frag; 2745c8884eddSHerbert Xu int hsize; 2746f4c50d99SHerbert Xu int size; 2747f4c50d99SHerbert Xu 2748f4c50d99SHerbert Xu len = skb->len - offset; 2749f4c50d99SHerbert Xu if (len > mss) 2750f4c50d99SHerbert Xu len = mss; 2751f4c50d99SHerbert Xu 2752f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2753f4c50d99SHerbert Xu if (hsize < 0) 2754f4c50d99SHerbert Xu hsize = 0; 2755c8884eddSHerbert Xu if (hsize > len || !sg) 2756c8884eddSHerbert Xu hsize = len; 2757f4c50d99SHerbert Xu 275889319d38SHerbert Xu if (!hsize && i >= nfrags) { 275989319d38SHerbert Xu BUG_ON(fskb->len != len); 276089319d38SHerbert Xu 276189319d38SHerbert Xu pos += len; 276289319d38SHerbert Xu nskb = skb_clone(fskb, GFP_ATOMIC); 276389319d38SHerbert Xu fskb = fskb->next; 276489319d38SHerbert Xu 2765f4c50d99SHerbert Xu if (unlikely(!nskb)) 2766f4c50d99SHerbert Xu goto err; 2767f4c50d99SHerbert Xu 2768ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 276989319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 277089319d38SHerbert Xu kfree_skb(nskb); 277189319d38SHerbert Xu goto err; 277289319d38SHerbert Xu } 277389319d38SHerbert Xu 2774ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 277589319d38SHerbert Xu skb_release_head_state(nskb); 277689319d38SHerbert Xu __skb_push(nskb, doffset); 277789319d38SHerbert Xu } else { 277889319d38SHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, 277989319d38SHerbert Xu GFP_ATOMIC); 278089319d38SHerbert Xu 278189319d38SHerbert Xu if (unlikely(!nskb)) 278289319d38SHerbert Xu goto err; 278389319d38SHerbert Xu 278489319d38SHerbert Xu skb_reserve(nskb, headroom); 278589319d38SHerbert Xu __skb_put(nskb, doffset); 278689319d38SHerbert Xu } 278789319d38SHerbert Xu 2788f4c50d99SHerbert Xu if (segs) 2789f4c50d99SHerbert Xu tail->next = nskb; 2790f4c50d99SHerbert Xu else 2791f4c50d99SHerbert Xu segs = nskb; 2792f4c50d99SHerbert Xu tail = nskb; 2793f4c50d99SHerbert Xu 27946f85a124SHerbert Xu __copy_skb_header(nskb, skb); 2795f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2796f4c50d99SHerbert Xu 27973d3be433SEric Dumazet /* nskb and skb might have different headroom */ 27983d3be433SEric Dumazet if (nskb->ip_summed == CHECKSUM_PARTIAL) 27993d3be433SEric Dumazet nskb->csum_start += skb_headroom(nskb) - headroom; 28003d3be433SEric Dumazet 2801459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2802ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2803b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2804b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 280589319d38SHerbert Xu skb_copy_from_linear_data(skb, nskb->data, doffset); 280689319d38SHerbert Xu 28072f181855SHerbert Xu if (fskb != skb_shinfo(skb)->frag_list) 280889319d38SHerbert Xu continue; 280989319d38SHerbert Xu 2810f4c50d99SHerbert Xu if (!sg) { 28116f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2812f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2813f4c50d99SHerbert Xu skb_put(nskb, len), 2814f4c50d99SHerbert Xu len, 0); 2815f4c50d99SHerbert Xu continue; 2816f4c50d99SHerbert Xu } 2817f4c50d99SHerbert Xu 2818f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2819f4c50d99SHerbert Xu 2820d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2821d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2822f4c50d99SHerbert Xu 282389319d38SHerbert Xu while (pos < offset + len && i < nfrags) { 2824f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2825ea2ab693SIan Campbell __skb_frag_ref(frag); 28269e903e08SEric Dumazet size = skb_frag_size(frag); 2827f4c50d99SHerbert Xu 2828f4c50d99SHerbert Xu if (pos < offset) { 2829f4c50d99SHerbert Xu frag->page_offset += offset - pos; 28309e903e08SEric Dumazet skb_frag_size_sub(frag, offset - pos); 2831f4c50d99SHerbert Xu } 2832f4c50d99SHerbert Xu 283389319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 2834f4c50d99SHerbert Xu 2835f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2836f4c50d99SHerbert Xu i++; 2837f4c50d99SHerbert Xu pos += size; 2838f4c50d99SHerbert Xu } else { 28399e903e08SEric Dumazet skb_frag_size_sub(frag, pos + size - (offset + len)); 284089319d38SHerbert Xu goto skip_fraglist; 2841f4c50d99SHerbert Xu } 2842f4c50d99SHerbert Xu 2843f4c50d99SHerbert Xu frag++; 2844f4c50d99SHerbert Xu } 2845f4c50d99SHerbert Xu 284689319d38SHerbert Xu if (pos < offset + len) { 284789319d38SHerbert Xu struct sk_buff *fskb2 = fskb; 284889319d38SHerbert Xu 284989319d38SHerbert Xu BUG_ON(pos + fskb->len != offset + len); 285089319d38SHerbert Xu 285189319d38SHerbert Xu pos += fskb->len; 285289319d38SHerbert Xu fskb = fskb->next; 285389319d38SHerbert Xu 285489319d38SHerbert Xu if (fskb2->next) { 285589319d38SHerbert Xu fskb2 = skb_clone(fskb2, GFP_ATOMIC); 285689319d38SHerbert Xu if (!fskb2) 285789319d38SHerbert Xu goto err; 285889319d38SHerbert Xu } else 285989319d38SHerbert Xu skb_get(fskb2); 286089319d38SHerbert Xu 2861fbb398a8SDavid S. Miller SKB_FRAG_ASSERT(nskb); 286289319d38SHerbert Xu skb_shinfo(nskb)->frag_list = fskb2; 286389319d38SHerbert Xu } 286489319d38SHerbert Xu 286589319d38SHerbert Xu skip_fraglist: 2866f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2867f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2868f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2869f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2870f4c50d99SHerbert Xu 2871f4c50d99SHerbert Xu return segs; 2872f4c50d99SHerbert Xu 2873f4c50d99SHerbert Xu err: 2874f4c50d99SHerbert Xu while ((skb = segs)) { 2875f4c50d99SHerbert Xu segs = skb->next; 2876b08d5840SPatrick McHardy kfree_skb(skb); 2877f4c50d99SHerbert Xu } 2878f4c50d99SHerbert Xu return ERR_PTR(err); 2879f4c50d99SHerbert Xu } 2880f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2881f4c50d99SHerbert Xu 288271d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 288371d93b39SHerbert Xu { 288471d93b39SHerbert Xu struct sk_buff *p = *head; 288571d93b39SHerbert Xu struct sk_buff *nskb; 28869aaa156cSHerbert Xu struct skb_shared_info *skbinfo = skb_shinfo(skb); 28879aaa156cSHerbert Xu struct skb_shared_info *pinfo = skb_shinfo(p); 288871d93b39SHerbert Xu unsigned int headroom; 288986911732SHerbert Xu unsigned int len = skb_gro_len(skb); 289067147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 289167147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 2892715dc1f3SEric Dumazet unsigned int delta_truesize; 289371d93b39SHerbert Xu 289486911732SHerbert Xu if (p->len + len >= 65536) 289571d93b39SHerbert Xu return -E2BIG; 289671d93b39SHerbert Xu 28979aaa156cSHerbert Xu if (pinfo->frag_list) 289871d93b39SHerbert Xu goto merge; 289967147ba9SHerbert Xu else if (headlen <= offset) { 290042da6994SHerbert Xu skb_frag_t *frag; 290166e92fcfSHerbert Xu skb_frag_t *frag2; 29029aaa156cSHerbert Xu int i = skbinfo->nr_frags; 29039aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 290442da6994SHerbert Xu 290566e92fcfSHerbert Xu offset -= headlen; 290666e92fcfSHerbert Xu 290766e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 290881705ad1SHerbert Xu return -E2BIG; 290981705ad1SHerbert Xu 29109aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 29119aaa156cSHerbert Xu skbinfo->nr_frags = 0; 2912f5572068SHerbert Xu 29139aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 29149aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 291566e92fcfSHerbert Xu do { 291666e92fcfSHerbert Xu *--frag = *--frag2; 291766e92fcfSHerbert Xu } while (--i); 291866e92fcfSHerbert Xu 291966e92fcfSHerbert Xu frag->page_offset += offset; 29209e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 292166e92fcfSHerbert Xu 2922715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 2923ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 2924ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 2925715dc1f3SEric Dumazet 2926f5572068SHerbert Xu skb->truesize -= skb->data_len; 2927f5572068SHerbert Xu skb->len -= skb->data_len; 2928f5572068SHerbert Xu skb->data_len = 0; 2929f5572068SHerbert Xu 2930715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 29315d38a079SHerbert Xu goto done; 2932d7e8883cSEric Dumazet } else if (skb->head_frag) { 2933d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 2934d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 2935d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 2936d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 2937d7e8883cSEric Dumazet unsigned int first_offset; 2938d7e8883cSEric Dumazet 2939d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 2940d7e8883cSEric Dumazet return -E2BIG; 2941d7e8883cSEric Dumazet 2942d7e8883cSEric Dumazet first_offset = skb->data - 2943d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 2944d7e8883cSEric Dumazet offset; 2945d7e8883cSEric Dumazet 2946d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 2947d7e8883cSEric Dumazet 2948d7e8883cSEric Dumazet frag->page.p = page; 2949d7e8883cSEric Dumazet frag->page_offset = first_offset; 2950d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 2951d7e8883cSEric Dumazet 2952d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 2953d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 2954d7e8883cSEric Dumazet 2955715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 2956d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 2957d7e8883cSEric Dumazet goto done; 295869c0cab1SHerbert Xu } else if (skb_gro_len(p) != pinfo->gso_size) 295969c0cab1SHerbert Xu return -E2BIG; 296071d93b39SHerbert Xu 296171d93b39SHerbert Xu headroom = skb_headroom(p); 29623d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 296371d93b39SHerbert Xu if (unlikely(!nskb)) 296471d93b39SHerbert Xu return -ENOMEM; 296571d93b39SHerbert Xu 296671d93b39SHerbert Xu __copy_skb_header(nskb, p); 296771d93b39SHerbert Xu nskb->mac_len = p->mac_len; 296871d93b39SHerbert Xu 296971d93b39SHerbert Xu skb_reserve(nskb, headroom); 297086911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 297171d93b39SHerbert Xu 297286911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 297371d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 297471d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 297571d93b39SHerbert Xu 297686911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 297786911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 297886911732SHerbert Xu p->data - skb_mac_header(p)); 297971d93b39SHerbert Xu 298071d93b39SHerbert Xu *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p); 298171d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 29829aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 2983622e0ca1SHerbert Xu pinfo->gso_size = 0; 298471d93b39SHerbert Xu skb_header_release(p); 298571d93b39SHerbert Xu nskb->prev = p; 298671d93b39SHerbert Xu 298771d93b39SHerbert Xu nskb->data_len += p->len; 2988de8261c2SEric Dumazet nskb->truesize += p->truesize; 298971d93b39SHerbert Xu nskb->len += p->len; 299071d93b39SHerbert Xu 299171d93b39SHerbert Xu *head = nskb; 299271d93b39SHerbert Xu nskb->next = p->next; 299371d93b39SHerbert Xu p->next = NULL; 299471d93b39SHerbert Xu 299571d93b39SHerbert Xu p = nskb; 299671d93b39SHerbert Xu 299771d93b39SHerbert Xu merge: 2998715dc1f3SEric Dumazet delta_truesize = skb->truesize; 299967147ba9SHerbert Xu if (offset > headlen) { 3000d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3001d1dc7abfSMichal Schmidt 3002d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 30039e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3004d1dc7abfSMichal Schmidt skb->data_len -= eat; 3005d1dc7abfSMichal Schmidt skb->len -= eat; 300667147ba9SHerbert Xu offset = headlen; 300756035022SHerbert Xu } 300856035022SHerbert Xu 300967147ba9SHerbert Xu __skb_pull(skb, offset); 301056035022SHerbert Xu 301171d93b39SHerbert Xu p->prev->next = skb; 301271d93b39SHerbert Xu p->prev = skb; 301371d93b39SHerbert Xu skb_header_release(skb); 301471d93b39SHerbert Xu 30155d38a079SHerbert Xu done: 30165d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 301737fe4732SHerbert Xu p->data_len += len; 3018715dc1f3SEric Dumazet p->truesize += delta_truesize; 301937fe4732SHerbert Xu p->len += len; 302071d93b39SHerbert Xu 302171d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 302271d93b39SHerbert Xu return 0; 302371d93b39SHerbert Xu } 302471d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive); 302571d93b39SHerbert Xu 30261da177e4SLinus Torvalds void __init skb_init(void) 30271da177e4SLinus Torvalds { 30281da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 30291da177e4SLinus Torvalds sizeof(struct sk_buff), 30301da177e4SLinus Torvalds 0, 3031e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 303220c2df83SPaul Mundt NULL); 3033d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3034d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 3035d179cd12SDavid S. Miller sizeof(atomic_t), 3036d179cd12SDavid S. Miller 0, 3037e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 303820c2df83SPaul Mundt NULL); 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds 3041716ea3a7SDavid Howells /** 3042716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3043716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3044716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3045716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3046716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3047716ea3a7SDavid Howells * 3048716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3049716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3050716ea3a7SDavid Howells */ 305151c739d1SDavid S. Miller static int 305251c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3053716ea3a7SDavid Howells { 30541a028e50SDavid S. Miller int start = skb_headlen(skb); 30551a028e50SDavid S. Miller int i, copy = start - offset; 3056fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3057716ea3a7SDavid Howells int elt = 0; 3058716ea3a7SDavid Howells 3059716ea3a7SDavid Howells if (copy > 0) { 3060716ea3a7SDavid Howells if (copy > len) 3061716ea3a7SDavid Howells copy = len; 3062642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3063716ea3a7SDavid Howells elt++; 3064716ea3a7SDavid Howells if ((len -= copy) == 0) 3065716ea3a7SDavid Howells return elt; 3066716ea3a7SDavid Howells offset += copy; 3067716ea3a7SDavid Howells } 3068716ea3a7SDavid Howells 3069716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 30701a028e50SDavid S. Miller int end; 3071716ea3a7SDavid Howells 3072547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30731a028e50SDavid S. Miller 30749e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3075716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3076716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3077716ea3a7SDavid Howells 3078716ea3a7SDavid Howells if (copy > len) 3079716ea3a7SDavid Howells copy = len; 3080ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3081642f1490SJens Axboe frag->page_offset+offset-start); 3082716ea3a7SDavid Howells elt++; 3083716ea3a7SDavid Howells if (!(len -= copy)) 3084716ea3a7SDavid Howells return elt; 3085716ea3a7SDavid Howells offset += copy; 3086716ea3a7SDavid Howells } 30871a028e50SDavid S. Miller start = end; 3088716ea3a7SDavid Howells } 3089716ea3a7SDavid Howells 3090fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 30911a028e50SDavid S. Miller int end; 3092716ea3a7SDavid Howells 3093547b792cSIlpo Järvinen WARN_ON(start > offset + len); 30941a028e50SDavid S. Miller 3095fbb398a8SDavid S. Miller end = start + frag_iter->len; 3096716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3097716ea3a7SDavid Howells if (copy > len) 3098716ea3a7SDavid Howells copy = len; 3099fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 310051c739d1SDavid S. Miller copy); 3101716ea3a7SDavid Howells if ((len -= copy) == 0) 3102716ea3a7SDavid Howells return elt; 3103716ea3a7SDavid Howells offset += copy; 3104716ea3a7SDavid Howells } 31051a028e50SDavid S. Miller start = end; 3106716ea3a7SDavid Howells } 3107716ea3a7SDavid Howells BUG_ON(len); 3108716ea3a7SDavid Howells return elt; 3109716ea3a7SDavid Howells } 3110716ea3a7SDavid Howells 311151c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 311251c739d1SDavid S. Miller { 311351c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 311451c739d1SDavid S. Miller 3115c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 311651c739d1SDavid S. Miller 311751c739d1SDavid S. Miller return nsg; 311851c739d1SDavid S. Miller } 3119b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 312051c739d1SDavid S. Miller 3121716ea3a7SDavid Howells /** 3122716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3123716ea3a7SDavid Howells * @skb: The socket buffer to check. 3124716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3125716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3126716ea3a7SDavid Howells * 3127716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3128716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3129716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3130716ea3a7SDavid Howells * 3131716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3132716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3133716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3134716ea3a7SDavid Howells * 3135716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3136716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3137716ea3a7SDavid Howells */ 3138716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3139716ea3a7SDavid Howells { 3140716ea3a7SDavid Howells int copyflag; 3141716ea3a7SDavid Howells int elt; 3142716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3143716ea3a7SDavid Howells 3144716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3145716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3146716ea3a7SDavid Howells * at the moment even if they are anonymous). 3147716ea3a7SDavid Howells */ 3148716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3149716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3150716ea3a7SDavid Howells return -ENOMEM; 3151716ea3a7SDavid Howells 3152716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 315321dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3154716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3155716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3156716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3157716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3158716ea3a7SDavid Howells 3159716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3160716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3161716ea3a7SDavid Howells return -ENOMEM; 3162716ea3a7SDavid Howells 3163716ea3a7SDavid Howells /* Voila! */ 3164716ea3a7SDavid Howells *trailer = skb; 3165716ea3a7SDavid Howells return 1; 3166716ea3a7SDavid Howells } 3167716ea3a7SDavid Howells 3168716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3169716ea3a7SDavid Howells 3170716ea3a7SDavid Howells elt = 1; 3171716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3172716ea3a7SDavid Howells copyflag = 0; 3173716ea3a7SDavid Howells 3174716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3175716ea3a7SDavid Howells int ntail = 0; 3176716ea3a7SDavid Howells 3177716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3178716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3179716ea3a7SDavid Howells * after it. */ 3180716ea3a7SDavid Howells 3181716ea3a7SDavid Howells if (skb_shared(skb1)) 3182716ea3a7SDavid Howells copyflag = 1; 3183716ea3a7SDavid Howells 3184716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3185716ea3a7SDavid Howells 3186716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3187716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 318821dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3189716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3190716ea3a7SDavid Howells ntail = tailbits + 128; 3191716ea3a7SDavid Howells } 3192716ea3a7SDavid Howells 3193716ea3a7SDavid Howells if (copyflag || 3194716ea3a7SDavid Howells skb_cloned(skb1) || 3195716ea3a7SDavid Howells ntail || 3196716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 319721dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3198716ea3a7SDavid Howells struct sk_buff *skb2; 3199716ea3a7SDavid Howells 3200716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3201716ea3a7SDavid Howells if (ntail == 0) 3202716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3203716ea3a7SDavid Howells else 3204716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3205716ea3a7SDavid Howells skb_headroom(skb1), 3206716ea3a7SDavid Howells ntail, 3207716ea3a7SDavid Howells GFP_ATOMIC); 3208716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3209716ea3a7SDavid Howells return -ENOMEM; 3210716ea3a7SDavid Howells 3211716ea3a7SDavid Howells if (skb1->sk) 3212716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3213716ea3a7SDavid Howells 3214716ea3a7SDavid Howells /* Looking around. Are we still alive? 3215716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3216716ea3a7SDavid Howells 3217716ea3a7SDavid Howells skb2->next = skb1->next; 3218716ea3a7SDavid Howells *skb_p = skb2; 3219716ea3a7SDavid Howells kfree_skb(skb1); 3220716ea3a7SDavid Howells skb1 = skb2; 3221716ea3a7SDavid Howells } 3222716ea3a7SDavid Howells elt++; 3223716ea3a7SDavid Howells *trailer = skb1; 3224716ea3a7SDavid Howells skb_p = &skb1->next; 3225716ea3a7SDavid Howells } 3226716ea3a7SDavid Howells 3227716ea3a7SDavid Howells return elt; 3228716ea3a7SDavid Howells } 3229b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3230716ea3a7SDavid Howells 3231b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3232b1faf566SEric Dumazet { 3233b1faf566SEric Dumazet struct sock *sk = skb->sk; 3234b1faf566SEric Dumazet 3235b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3236b1faf566SEric Dumazet } 3237b1faf566SEric Dumazet 3238b1faf566SEric Dumazet /* 3239b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3240b1faf566SEric Dumazet */ 3241b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3242b1faf566SEric Dumazet { 3243110c4330SEric Dumazet int len = skb->len; 3244110c4330SEric Dumazet 3245b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 324695c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3247b1faf566SEric Dumazet return -ENOMEM; 3248b1faf566SEric Dumazet 3249b1faf566SEric Dumazet skb_orphan(skb); 3250b1faf566SEric Dumazet skb->sk = sk; 3251b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3252b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3253b1faf566SEric Dumazet 3254abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3255abb57ea4SEric Dumazet skb_dst_force(skb); 3256abb57ea4SEric Dumazet 3257b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3258b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3259110c4330SEric Dumazet sk->sk_data_ready(sk, len); 3260b1faf566SEric Dumazet return 0; 3261b1faf566SEric Dumazet } 3262b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3263b1faf566SEric Dumazet 3264ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb, 3265ac45f602SPatrick Ohly struct skb_shared_hwtstamps *hwtstamps) 3266ac45f602SPatrick Ohly { 3267ac45f602SPatrick Ohly struct sock *sk = orig_skb->sk; 3268ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3269ac45f602SPatrick Ohly struct sk_buff *skb; 3270ac45f602SPatrick Ohly int err; 3271ac45f602SPatrick Ohly 3272ac45f602SPatrick Ohly if (!sk) 3273ac45f602SPatrick Ohly return; 3274ac45f602SPatrick Ohly 3275ac45f602SPatrick Ohly skb = skb_clone(orig_skb, GFP_ATOMIC); 3276ac45f602SPatrick Ohly if (!skb) 3277ac45f602SPatrick Ohly return; 3278ac45f602SPatrick Ohly 3279ac45f602SPatrick Ohly if (hwtstamps) { 3280ac45f602SPatrick Ohly *skb_hwtstamps(skb) = 3281ac45f602SPatrick Ohly *hwtstamps; 3282ac45f602SPatrick Ohly } else { 3283ac45f602SPatrick Ohly /* 3284ac45f602SPatrick Ohly * no hardware time stamps available, 32852244d07bSOliver Hartkopp * so keep the shared tx_flags and only 3286ac45f602SPatrick Ohly * store software time stamp 3287ac45f602SPatrick Ohly */ 3288ac45f602SPatrick Ohly skb->tstamp = ktime_get_real(); 3289ac45f602SPatrick Ohly } 3290ac45f602SPatrick Ohly 3291ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3292ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3293ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3294ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 329529030374SEric Dumazet 3296ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 329729030374SEric Dumazet 3298ac45f602SPatrick Ohly if (err) 3299ac45f602SPatrick Ohly kfree_skb(skb); 3300ac45f602SPatrick Ohly } 3301ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3302ac45f602SPatrick Ohly 33036e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 33046e3e939fSJohannes Berg { 33056e3e939fSJohannes Berg struct sock *sk = skb->sk; 33066e3e939fSJohannes Berg struct sock_exterr_skb *serr; 33076e3e939fSJohannes Berg int err; 33086e3e939fSJohannes Berg 33096e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 33106e3e939fSJohannes Berg skb->wifi_acked = acked; 33116e3e939fSJohannes Berg 33126e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 33136e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 33146e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 33156e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 33166e3e939fSJohannes Berg 33176e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 33186e3e939fSJohannes Berg if (err) 33196e3e939fSJohannes Berg kfree_skb(skb); 33206e3e939fSJohannes Berg } 33216e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 33226e3e939fSJohannes Berg 3323ac45f602SPatrick Ohly 3324f35d9d8aSRusty Russell /** 3325f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3326f35d9d8aSRusty Russell * @skb: the skb to set 3327f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3328f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3329f35d9d8aSRusty Russell * 3330f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3331f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3332f35d9d8aSRusty Russell * 3333f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3334f35d9d8aSRusty Russell * returns false you should drop the packet. 3335f35d9d8aSRusty Russell */ 3336f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3337f35d9d8aSRusty Russell { 33385ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 33395ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3340e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 33415ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3342f35d9d8aSRusty Russell return false; 3343f35d9d8aSRusty Russell } 3344f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3345f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3346f35d9d8aSRusty Russell skb->csum_offset = off; 3347f35d9d8aSRusty Russell return true; 3348f35d9d8aSRusty Russell } 3349b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3350f35d9d8aSRusty Russell 33514497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 33524497b076SBen Hutchings { 3353e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 3354e87cc472SJoe Perches skb->dev->name); 33554497b076SBen Hutchings } 33564497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3357bad43ca8SEric Dumazet 3358bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 3359bad43ca8SEric Dumazet { 3360bad43ca8SEric Dumazet if (head_stolen) 3361bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 3362bad43ca8SEric Dumazet else 3363bad43ca8SEric Dumazet __kfree_skb(skb); 3364bad43ca8SEric Dumazet } 3365bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 3366bad43ca8SEric Dumazet 3367bad43ca8SEric Dumazet /** 3368bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 3369bad43ca8SEric Dumazet * @to: prior buffer 3370bad43ca8SEric Dumazet * @from: buffer to add 3371bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 3372c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 3373bad43ca8SEric Dumazet */ 3374bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 3375bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 3376bad43ca8SEric Dumazet { 3377bad43ca8SEric Dumazet int i, delta, len = from->len; 3378bad43ca8SEric Dumazet 3379bad43ca8SEric Dumazet *fragstolen = false; 3380bad43ca8SEric Dumazet 3381bad43ca8SEric Dumazet if (skb_cloned(to)) 3382bad43ca8SEric Dumazet return false; 3383bad43ca8SEric Dumazet 3384bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 3385bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 3386bad43ca8SEric Dumazet *delta_truesize = 0; 3387bad43ca8SEric Dumazet return true; 3388bad43ca8SEric Dumazet } 3389bad43ca8SEric Dumazet 3390bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 3391bad43ca8SEric Dumazet return false; 3392bad43ca8SEric Dumazet 3393bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 3394bad43ca8SEric Dumazet struct page *page; 3395bad43ca8SEric Dumazet unsigned int offset; 3396bad43ca8SEric Dumazet 3397bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3398bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 3399bad43ca8SEric Dumazet return false; 3400bad43ca8SEric Dumazet 3401bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 3402bad43ca8SEric Dumazet return false; 3403bad43ca8SEric Dumazet 3404bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3405bad43ca8SEric Dumazet 3406bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 3407bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 3408bad43ca8SEric Dumazet 3409bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 3410bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 3411bad43ca8SEric Dumazet *fragstolen = true; 3412bad43ca8SEric Dumazet } else { 3413bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3414bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 3415bad43ca8SEric Dumazet return false; 3416bad43ca8SEric Dumazet 3417bad43ca8SEric Dumazet delta = from->truesize - 3418bad43ca8SEric Dumazet SKB_TRUESIZE(skb_end_pointer(from) - from->head); 3419bad43ca8SEric Dumazet } 3420bad43ca8SEric Dumazet 3421bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 3422bad43ca8SEric Dumazet 3423bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 3424bad43ca8SEric Dumazet skb_shinfo(from)->frags, 3425bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 3426bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 3427bad43ca8SEric Dumazet 3428bad43ca8SEric Dumazet if (!skb_cloned(from)) 3429bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 3430bad43ca8SEric Dumazet 3431bad43ca8SEric Dumazet /* if the skb is cloned this does nothing since we set nr_frags to 0 */ 3432bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 3433bad43ca8SEric Dumazet skb_frag_ref(from, i); 3434bad43ca8SEric Dumazet 3435bad43ca8SEric Dumazet to->truesize += delta; 3436bad43ca8SEric Dumazet to->len += len; 3437bad43ca8SEric Dumazet to->data_len += len; 3438bad43ca8SEric Dumazet 3439bad43ca8SEric Dumazet *delta_truesize = delta; 3440bad43ca8SEric Dumazet return true; 3441bad43ca8SEric Dumazet } 3442bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 3443