11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: Alan Cox <iiitac@pyr.swan.ac.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Fixes: 81da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 91da177e4SLinus Torvalds * balancer bugs. 101da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 111da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 121da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 131da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 141da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 151da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 161da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 171da177e4SLinus Torvalds * only put in the headers 181da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 191da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 201da177e4SLinus Torvalds * Andi Kleen : slabified it. 211da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * NOTE: 241da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 251da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 261da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 271da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 301da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 311da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 321da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <linux/module.h> 401da177e4SLinus Torvalds #include <linux/types.h> 411da177e4SLinus Torvalds #include <linux/kernel.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 431da177e4SLinus Torvalds #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/in.h> 451da177e4SLinus Torvalds #include <linux/inet.h> 461da177e4SLinus Torvalds #include <linux/slab.h> 471da177e4SLinus Torvalds #include <linux/netdevice.h> 481da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 491da177e4SLinus Torvalds #include <net/pkt_sched.h> 501da177e4SLinus Torvalds #endif 511da177e4SLinus Torvalds #include <linux/string.h> 521da177e4SLinus Torvalds #include <linux/skbuff.h> 539c55e01cSJens Axboe #include <linux/splice.h> 541da177e4SLinus Torvalds #include <linux/cache.h> 551da177e4SLinus Torvalds #include <linux/rtnetlink.h> 561da177e4SLinus Torvalds #include <linux/init.h> 57716ea3a7SDavid Howells #include <linux/scatterlist.h> 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #include <net/protocol.h> 601da177e4SLinus Torvalds #include <net/dst.h> 611da177e4SLinus Torvalds #include <net/sock.h> 621da177e4SLinus Torvalds #include <net/checksum.h> 631da177e4SLinus Torvalds #include <net/xfrm.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <asm/uaccess.h> 661da177e4SLinus Torvalds #include <asm/system.h> 671da177e4SLinus Torvalds 68a1f8e7f7SAl Viro #include "kmap_skb.h" 69a1f8e7f7SAl Viro 70e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly; 71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 721da177e4SLinus Torvalds 739c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 749c55e01cSJens Axboe struct pipe_buffer *buf) 759c55e01cSJens Axboe { 769c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 779c55e01cSJens Axboe 789c55e01cSJens Axboe kfree_skb(skb); 799c55e01cSJens Axboe } 809c55e01cSJens Axboe 819c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 829c55e01cSJens Axboe struct pipe_buffer *buf) 839c55e01cSJens Axboe { 849c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 859c55e01cSJens Axboe 869c55e01cSJens Axboe skb_get(skb); 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. */ 979c55e01cSJens Axboe static 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 */ 1211da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1221da177e4SLinus Torvalds { 12326095455SPatrick McHardy printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 1244305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 12527a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1264305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12726095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1281da177e4SLinus Torvalds BUG(); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /** 1321da177e4SLinus Torvalds * skb_under_panic - private function 1331da177e4SLinus Torvalds * @skb: buffer 1341da177e4SLinus Torvalds * @sz: size 1351da177e4SLinus Torvalds * @here: address 1361da177e4SLinus Torvalds * 1371da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1411da177e4SLinus Torvalds { 14226095455SPatrick McHardy printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 1434305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 14427a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1454305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 14626095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1471da177e4SLinus Torvalds BUG(); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 150dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb) 151dc6de336SDavid S. Miller { 152dc6de336SDavid S. Miller printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " 153dc6de336SDavid S. Miller "len=%u, sizeof(sk_buff)=%Zd\n", 154dc6de336SDavid S. Miller skb->truesize, skb->len, sizeof(struct sk_buff)); 155dc6de336SDavid S. Miller } 156dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug); 157dc6de336SDavid S. Miller 1581da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1591da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1601da177e4SLinus Torvalds * [BEEP] leaks. 1611da177e4SLinus Torvalds * 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /** 165d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1661da177e4SLinus Torvalds * @size: size to allocate 1671da177e4SLinus Torvalds * @gfp_mask: allocation mask 168c83c2486SRandy Dunlap * @fclone: allocate from fclone cache instead of head cache 169c83c2486SRandy Dunlap * and allocate a cloned (child) skb 170b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1711da177e4SLinus Torvalds * 1721da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 1731da177e4SLinus Torvalds * tail room of size bytes. The object has a reference count of one. 1741da177e4SLinus Torvalds * The return is the buffer. On a failure the return is %NULL. 1751da177e4SLinus Torvalds * 1761da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1771da177e4SLinus Torvalds * %GFP_ATOMIC. 1781da177e4SLinus Torvalds */ 179dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 180b30973f8SChristoph Hellwig int fclone, int node) 1811da177e4SLinus Torvalds { 182e18b890bSChristoph Lameter struct kmem_cache *cache; 1834947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1841da177e4SLinus Torvalds struct sk_buff *skb; 1851da177e4SLinus Torvalds u8 *data; 1861da177e4SLinus Torvalds 1878798b3fbSHerbert Xu cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 1888798b3fbSHerbert Xu 1891da177e4SLinus Torvalds /* Get the HEAD */ 190b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1911da177e4SLinus Torvalds if (!skb) 1921da177e4SLinus Torvalds goto out; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 195b30973f8SChristoph Hellwig data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 196b30973f8SChristoph Hellwig gfp_mask, node); 1971da177e4SLinus Torvalds if (!data) 1981da177e4SLinus Torvalds goto nodata; 1991da177e4SLinus Torvalds 200ca0605a7SArnaldo Carvalho de Melo /* 201c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 202c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 203c8005785SJohannes Berg * the tail pointer in struct sk_buff! 204ca0605a7SArnaldo Carvalho de Melo */ 205ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 2061da177e4SLinus Torvalds skb->truesize = size + sizeof(struct sk_buff); 2071da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2081da177e4SLinus Torvalds skb->head = data; 2091da177e4SLinus Torvalds skb->data = data; 21027a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2114305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 2124947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2134947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 2144947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 2154947d3efSBenjamin LaHaise shinfo->nr_frags = 0; 2167967168cSHerbert Xu shinfo->gso_size = 0; 2177967168cSHerbert Xu shinfo->gso_segs = 0; 2187967168cSHerbert Xu shinfo->gso_type = 0; 2194947d3efSBenjamin LaHaise shinfo->ip6_frag_id = 0; 2204947d3efSBenjamin LaHaise shinfo->frag_list = NULL; 2214947d3efSBenjamin LaHaise 222d179cd12SDavid S. Miller if (fclone) { 223d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 224d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 2251da177e4SLinus Torvalds 226d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 227d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 228d179cd12SDavid S. Miller 229d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 230d179cd12SDavid S. Miller } 2311da177e4SLinus Torvalds out: 2321da177e4SLinus Torvalds return skb; 2331da177e4SLinus Torvalds nodata: 2348798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2351da177e4SLinus Torvalds skb = NULL; 2361da177e4SLinus Torvalds goto out; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /** 2408af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 2418af27456SChristoph Hellwig * @dev: network device to receive on 2428af27456SChristoph Hellwig * @length: length to allocate 2438af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 2448af27456SChristoph Hellwig * 2458af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 2468af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 2478af27456SChristoph Hellwig * the headroom they think they need without accounting for the 2488af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 2498af27456SChristoph Hellwig * 2508af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 2518af27456SChristoph Hellwig */ 2528af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 2538af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 2548af27456SChristoph Hellwig { 25543cb76d9SGreg Kroah-Hartman int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 2568af27456SChristoph Hellwig struct sk_buff *skb; 2578af27456SChristoph Hellwig 258b30973f8SChristoph Hellwig skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); 2597b2e497aSChristoph Hellwig if (likely(skb)) { 2608af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 2617b2e497aSChristoph Hellwig skb->dev = dev; 2627b2e497aSChristoph Hellwig } 2638af27456SChristoph Hellwig return skb; 2648af27456SChristoph Hellwig } 2651da177e4SLinus Torvalds 266f58518e6SIlpo Järvinen /** 267f58518e6SIlpo Järvinen * dev_alloc_skb - allocate an skbuff for receiving 268f58518e6SIlpo Järvinen * @length: length to allocate 269f58518e6SIlpo Järvinen * 270f58518e6SIlpo Järvinen * Allocate a new &sk_buff and assign it a usage count of one. The 271f58518e6SIlpo Järvinen * buffer has unspecified headroom built in. Users should allocate 272f58518e6SIlpo Järvinen * the headroom they think they need without accounting for the 273f58518e6SIlpo Järvinen * built in space. The built in space is used for optimisations. 274f58518e6SIlpo Järvinen * 275f58518e6SIlpo Järvinen * %NULL is returned if there is no free memory. Although this function 276f58518e6SIlpo Järvinen * allocates memory it can be called from an interrupt. 277f58518e6SIlpo Järvinen */ 278f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length) 279f58518e6SIlpo Järvinen { 2801483b874SDenys Vlasenko /* 2811483b874SDenys Vlasenko * There is more code here than it seems: 282a0f55e0eSDavid S. Miller * __dev_alloc_skb is an inline 2831483b874SDenys Vlasenko */ 284f58518e6SIlpo Järvinen return __dev_alloc_skb(length, GFP_ATOMIC); 285f58518e6SIlpo Järvinen } 286f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb); 287f58518e6SIlpo Järvinen 28827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 2891da177e4SLinus Torvalds { 29027b437c8SHerbert Xu struct sk_buff *list = *listp; 2911da177e4SLinus Torvalds 29227b437c8SHerbert Xu *listp = NULL; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds do { 2951da177e4SLinus Torvalds struct sk_buff *this = list; 2961da177e4SLinus Torvalds list = list->next; 2971da177e4SLinus Torvalds kfree_skb(this); 2981da177e4SLinus Torvalds } while (list); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 30127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 30227b437c8SHerbert Xu { 30327b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 30427b437c8SHerbert Xu } 30527b437c8SHerbert Xu 3061da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds struct sk_buff *list; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds for (list = skb_shinfo(skb)->frag_list; list; list = list->next) 3111da177e4SLinus Torvalds skb_get(list); 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3145bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds if (!skb->cloned || 3171da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 3181da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 3191da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 3201da177e4SLinus Torvalds int i; 3211da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 3221da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 3261da177e4SLinus Torvalds skb_drop_fraglist(skb); 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds kfree(skb->head); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* 3331da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 3341da177e4SLinus Torvalds */ 3352d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 3361da177e4SLinus Torvalds { 337d179cd12SDavid S. Miller struct sk_buff *other; 338d179cd12SDavid S. Miller atomic_t *fclone_ref; 339d179cd12SDavid S. Miller 340d179cd12SDavid S. Miller switch (skb->fclone) { 341d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 3421da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 343d179cd12SDavid S. Miller break; 344d179cd12SDavid S. Miller 345d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 346d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 347d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 348d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 349d179cd12SDavid S. Miller break; 350d179cd12SDavid S. Miller 351d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 352d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 353d179cd12SDavid S. Miller other = skb - 1; 354d179cd12SDavid S. Miller 355d179cd12SDavid S. Miller /* The clone portion is available for 356d179cd12SDavid S. Miller * fast-cloning again. 357d179cd12SDavid S. Miller */ 358d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 359d179cd12SDavid S. Miller 360d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 361d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 362d179cd12SDavid S. Miller break; 3633ff50b79SStephen Hemminger } 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 36604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 3671da177e4SLinus Torvalds { 3681da177e4SLinus Torvalds dst_release(skb->dst); 3691da177e4SLinus Torvalds #ifdef CONFIG_XFRM 3701da177e4SLinus Torvalds secpath_put(skb->sp); 3711da177e4SLinus Torvalds #endif 3721da177e4SLinus Torvalds if (skb->destructor) { 3739c2b3328SStephen Hemminger WARN_ON(in_irq()); 3741da177e4SLinus Torvalds skb->destructor(skb); 3751da177e4SLinus Torvalds } 3769fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 3775f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 3789fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 3799fb9cbb1SYasuyuki Kozakai #endif 3801da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 3811da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 3821da177e4SLinus Torvalds #endif 3831da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 3841da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 3851da177e4SLinus Torvalds skb->tc_index = 0; 3861da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 3871da177e4SLinus Torvalds skb->tc_verd = 0; 3881da177e4SLinus Torvalds #endif 3891da177e4SLinus Torvalds #endif 39004a4bb55SLennert Buytenhek } 39104a4bb55SLennert Buytenhek 39204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 39304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 39404a4bb55SLennert Buytenhek { 39504a4bb55SLennert Buytenhek skb_release_head_state(skb); 3962d4baff8SHerbert Xu skb_release_data(skb); 3972d4baff8SHerbert Xu } 3981da177e4SLinus Torvalds 3992d4baff8SHerbert Xu /** 4002d4baff8SHerbert Xu * __kfree_skb - private function 4012d4baff8SHerbert Xu * @skb: buffer 4022d4baff8SHerbert Xu * 4032d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 4042d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 4052d4baff8SHerbert Xu * always call kfree_skb 4062d4baff8SHerbert Xu */ 4072d4baff8SHerbert Xu 4082d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 4092d4baff8SHerbert Xu { 4102d4baff8SHerbert Xu skb_release_all(skb); 4111da177e4SLinus Torvalds kfree_skbmem(skb); 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /** 415231d06aeSJörn Engel * kfree_skb - free an sk_buff 416231d06aeSJörn Engel * @skb: buffer to free 417231d06aeSJörn Engel * 418231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 419231d06aeSJörn Engel * hit zero. 420231d06aeSJörn Engel */ 421231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 422231d06aeSJörn Engel { 423231d06aeSJörn Engel if (unlikely(!skb)) 424231d06aeSJörn Engel return; 425231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 426231d06aeSJörn Engel smp_rmb(); 427231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 428231d06aeSJörn Engel return; 429231d06aeSJörn Engel __kfree_skb(skb); 430231d06aeSJörn Engel } 431231d06aeSJörn Engel 43204a4bb55SLennert Buytenhek int skb_recycle_check(struct sk_buff *skb, int skb_size) 43304a4bb55SLennert Buytenhek { 43404a4bb55SLennert Buytenhek struct skb_shared_info *shinfo; 43504a4bb55SLennert Buytenhek 43604a4bb55SLennert Buytenhek if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE) 43704a4bb55SLennert Buytenhek return 0; 43804a4bb55SLennert Buytenhek 43904a4bb55SLennert Buytenhek skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD); 44004a4bb55SLennert Buytenhek if (skb_end_pointer(skb) - skb->head < skb_size) 44104a4bb55SLennert Buytenhek return 0; 44204a4bb55SLennert Buytenhek 44304a4bb55SLennert Buytenhek if (skb_shared(skb) || skb_cloned(skb)) 44404a4bb55SLennert Buytenhek return 0; 44504a4bb55SLennert Buytenhek 44604a4bb55SLennert Buytenhek skb_release_head_state(skb); 44704a4bb55SLennert Buytenhek shinfo = skb_shinfo(skb); 44804a4bb55SLennert Buytenhek atomic_set(&shinfo->dataref, 1); 44904a4bb55SLennert Buytenhek shinfo->nr_frags = 0; 45004a4bb55SLennert Buytenhek shinfo->gso_size = 0; 45104a4bb55SLennert Buytenhek shinfo->gso_segs = 0; 45204a4bb55SLennert Buytenhek shinfo->gso_type = 0; 45304a4bb55SLennert Buytenhek shinfo->ip6_frag_id = 0; 45404a4bb55SLennert Buytenhek shinfo->frag_list = NULL; 45504a4bb55SLennert Buytenhek 45604a4bb55SLennert Buytenhek memset(skb, 0, offsetof(struct sk_buff, tail)); 45704a4bb55SLennert Buytenhek skb_reset_tail_pointer(skb); 45804a4bb55SLennert Buytenhek skb->data = skb->head + NET_SKB_PAD; 45904a4bb55SLennert Buytenhek 46004a4bb55SLennert Buytenhek return 1; 46104a4bb55SLennert Buytenhek } 46204a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check); 46304a4bb55SLennert Buytenhek 464dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 465dec18810SHerbert Xu { 466dec18810SHerbert Xu new->tstamp = old->tstamp; 467dec18810SHerbert Xu new->dev = old->dev; 468dec18810SHerbert Xu new->transport_header = old->transport_header; 469dec18810SHerbert Xu new->network_header = old->network_header; 470dec18810SHerbert Xu new->mac_header = old->mac_header; 471dec18810SHerbert Xu new->dst = dst_clone(old->dst); 472dec18810SHerbert Xu #ifdef CONFIG_INET 473dec18810SHerbert Xu new->sp = secpath_get(old->sp); 474dec18810SHerbert Xu #endif 475dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 476dec18810SHerbert Xu new->csum_start = old->csum_start; 477dec18810SHerbert Xu new->csum_offset = old->csum_offset; 478dec18810SHerbert Xu new->local_df = old->local_df; 479dec18810SHerbert Xu new->pkt_type = old->pkt_type; 480dec18810SHerbert Xu new->ip_summed = old->ip_summed; 481dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 482dec18810SHerbert Xu new->priority = old->priority; 483dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 484dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 485dec18810SHerbert Xu #endif 486dec18810SHerbert Xu new->protocol = old->protocol; 487dec18810SHerbert Xu new->mark = old->mark; 488dec18810SHerbert Xu __nf_copy(new, old); 489dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 490dec18810SHerbert Xu defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 491dec18810SHerbert Xu new->nf_trace = old->nf_trace; 492dec18810SHerbert Xu #endif 493dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 494dec18810SHerbert Xu new->tc_index = old->tc_index; 495dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 496dec18810SHerbert Xu new->tc_verd = old->tc_verd; 497dec18810SHerbert Xu #endif 498dec18810SHerbert Xu #endif 4996aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 5006aa895b0SPatrick McHardy 501dec18810SHerbert Xu skb_copy_secmark(new, old); 502dec18810SHerbert Xu } 503dec18810SHerbert Xu 504e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 5051da177e4SLinus Torvalds { 5061da177e4SLinus Torvalds #define C(x) n->x = skb->x 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds n->next = n->prev = NULL; 5091da177e4SLinus Torvalds n->sk = NULL; 510dec18810SHerbert Xu __copy_skb_header(n, skb); 511dec18810SHerbert Xu 5121da177e4SLinus Torvalds C(len); 5131da177e4SLinus Torvalds C(data_len); 5143e6b3b2eSAlexey Dobriyan C(mac_len); 515334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 51602f1c89dSPaul Moore n->cloned = 1; 5171da177e4SLinus Torvalds n->nohdr = 0; 5181da177e4SLinus Torvalds n->destructor = NULL; 51902f1c89dSPaul Moore C(iif); 5201da177e4SLinus Torvalds C(tail); 5211da177e4SLinus Torvalds C(end); 52202f1c89dSPaul Moore C(head); 52302f1c89dSPaul Moore C(data); 52402f1c89dSPaul Moore C(truesize); 525d0f09804SJohannes Berg #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE) 526d0f09804SJohannes Berg C(do_not_encrypt); 527d0f09804SJohannes Berg #endif 52802f1c89dSPaul Moore atomic_set(&n->users, 1); 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 5311da177e4SLinus Torvalds skb->cloned = 1; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds return n; 534e0053ec0SHerbert Xu #undef C 535e0053ec0SHerbert Xu } 536e0053ec0SHerbert Xu 537e0053ec0SHerbert Xu /** 538e0053ec0SHerbert Xu * skb_morph - morph one skb into another 539e0053ec0SHerbert Xu * @dst: the skb to receive the contents 540e0053ec0SHerbert Xu * @src: the skb to supply the contents 541e0053ec0SHerbert Xu * 542e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 543e0053ec0SHerbert Xu * supplied by the user. 544e0053ec0SHerbert Xu * 545e0053ec0SHerbert Xu * The target skb is returned upon exit. 546e0053ec0SHerbert Xu */ 547e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 548e0053ec0SHerbert Xu { 5492d4baff8SHerbert Xu skb_release_all(dst); 550e0053ec0SHerbert Xu return __skb_clone(dst, src); 551e0053ec0SHerbert Xu } 552e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 553e0053ec0SHerbert Xu 554e0053ec0SHerbert Xu /** 555e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 556e0053ec0SHerbert Xu * @skb: buffer to clone 557e0053ec0SHerbert Xu * @gfp_mask: allocation priority 558e0053ec0SHerbert Xu * 559e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 560e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 561e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 562e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 563e0053ec0SHerbert Xu * 564e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 565e0053ec0SHerbert Xu * %GFP_ATOMIC. 566e0053ec0SHerbert Xu */ 567e0053ec0SHerbert Xu 568e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 569e0053ec0SHerbert Xu { 570e0053ec0SHerbert Xu struct sk_buff *n; 571e0053ec0SHerbert Xu 572e0053ec0SHerbert Xu n = skb + 1; 573e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 574e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 575e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 576e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 577e0053ec0SHerbert Xu atomic_inc(fclone_ref); 578e0053ec0SHerbert Xu } else { 579e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 580e0053ec0SHerbert Xu if (!n) 581e0053ec0SHerbert Xu return NULL; 582e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 583e0053ec0SHerbert Xu } 584e0053ec0SHerbert Xu 585e0053ec0SHerbert Xu return __skb_clone(n, skb); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 5891da177e4SLinus Torvalds { 5902e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * Shift between the two data areas in bytes 5931da177e4SLinus Torvalds */ 5941da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 5952e07fa9cSArnaldo Carvalho de Melo #endif 596dec18810SHerbert Xu 597dec18810SHerbert Xu __copy_skb_header(new, old); 598dec18810SHerbert Xu 5992e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 6002e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 6012e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 6022e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 6032e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 6042e07fa9cSArnaldo Carvalho de Melo #endif 6057967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 6067967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 6077967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /** 6111da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 6121da177e4SLinus Torvalds * @skb: buffer to copy 6131da177e4SLinus Torvalds * @gfp_mask: allocation priority 6141da177e4SLinus Torvalds * 6151da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 6161da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 6171da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 6181da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 6191da177e4SLinus Torvalds * 6201da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 6211da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 6221da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 6231da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 6241da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 6251da177e4SLinus Torvalds */ 6261da177e4SLinus Torvalds 627dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 6281da177e4SLinus Torvalds { 6291da177e4SLinus Torvalds int headerlen = skb->data - skb->head; 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * Allocate the copy buffer 6321da177e4SLinus Torvalds */ 6334305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6344305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6354305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end + skb->data_len, gfp_mask); 6364305b541SArnaldo Carvalho de Melo #else 6374305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 6384305b541SArnaldo Carvalho de Melo #endif 6391da177e4SLinus Torvalds if (!n) 6401da177e4SLinus Torvalds return NULL; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* Set the data pointer */ 6431da177e4SLinus Torvalds skb_reserve(n, headerlen); 6441da177e4SLinus Torvalds /* Set the tail pointer and length */ 6451da177e4SLinus Torvalds skb_put(n, skb->len); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 6481da177e4SLinus Torvalds BUG(); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds copy_skb_header(n, skb); 6511da177e4SLinus Torvalds return n; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds /** 6561da177e4SLinus Torvalds * pskb_copy - create copy of an sk_buff with private head. 6571da177e4SLinus Torvalds * @skb: buffer to copy 6581da177e4SLinus Torvalds * @gfp_mask: allocation priority 6591da177e4SLinus Torvalds * 6601da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 6611da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 6621da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 6631da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 6641da177e4SLinus Torvalds * or the pointer to the buffer on success. 6651da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 6661da177e4SLinus Torvalds */ 6671da177e4SLinus Torvalds 668dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds /* 6711da177e4SLinus Torvalds * Allocate the copy buffer 6721da177e4SLinus Torvalds */ 6734305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6744305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6754305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end, gfp_mask); 6764305b541SArnaldo Carvalho de Melo #else 6774305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head, gfp_mask); 6784305b541SArnaldo Carvalho de Melo #endif 6791da177e4SLinus Torvalds if (!n) 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds /* Set the data pointer */ 6831da177e4SLinus Torvalds skb_reserve(n, skb->data - skb->head); 6841da177e4SLinus Torvalds /* Set the tail pointer and length */ 6851da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 6861da177e4SLinus Torvalds /* Copy the bytes */ 687d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 6881da177e4SLinus Torvalds 68925f484a6SHerbert Xu n->truesize += skb->data_len; 6901da177e4SLinus Torvalds n->data_len = skb->data_len; 6911da177e4SLinus Torvalds n->len = skb->len; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 6941da177e4SLinus Torvalds int i; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 6971da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 6981da177e4SLinus Torvalds get_page(skb_shinfo(n)->frags[i].page); 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 7041da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 7051da177e4SLinus Torvalds skb_clone_fraglist(n); 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds copy_skb_header(n, skb); 7091da177e4SLinus Torvalds out: 7101da177e4SLinus Torvalds return n; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /** 7141da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 7151da177e4SLinus Torvalds * @skb: buffer to reallocate 7161da177e4SLinus Torvalds * @nhead: room to add at head 7171da177e4SLinus Torvalds * @ntail: room to add at tail 7181da177e4SLinus Torvalds * @gfp_mask: allocation priority 7191da177e4SLinus Torvalds * 7201da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 7211da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 7221da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 7231da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 7241da177e4SLinus Torvalds * 7251da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 7261da177e4SLinus Torvalds * reloaded after call to this function. 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds 72986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 730dd0fc66fSAl Viro gfp_t gfp_mask) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds int i; 7331da177e4SLinus Torvalds u8 *data; 7344305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7354305b541SArnaldo Carvalho de Melo int size = nhead + skb->end + ntail; 7364305b541SArnaldo Carvalho de Melo #else 7371da177e4SLinus Torvalds int size = nhead + (skb->end - skb->head) + ntail; 7384305b541SArnaldo Carvalho de Melo #endif 7391da177e4SLinus Torvalds long off; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds if (skb_shared(skb)) 7421da177e4SLinus Torvalds BUG(); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 7471da177e4SLinus Torvalds if (!data) 7481da177e4SLinus Torvalds goto nodata; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 7511da177e4SLinus Torvalds * optimized for the cases when header is void. */ 7524305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 753b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail); 7544305b541SArnaldo Carvalho de Melo #else 755b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail - skb->head); 75627a884dcSArnaldo Carvalho de Melo #endif 7574305b541SArnaldo Carvalho de Melo memcpy(data + size, skb_end_pointer(skb), 7584305b541SArnaldo Carvalho de Melo sizeof(struct skb_shared_info)); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 7611da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 7641da177e4SLinus Torvalds skb_clone_fraglist(skb); 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds skb_release_data(skb); 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds off = (data + nhead) - skb->head; 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds skb->head = data; 7711da177e4SLinus Torvalds skb->data += off; 7724305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7734305b541SArnaldo Carvalho de Melo skb->end = size; 77456eb8882SPatrick McHardy off = nhead; 7754305b541SArnaldo Carvalho de Melo #else 7764305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 77756eb8882SPatrick McHardy #endif 77827a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 77927a884dcSArnaldo Carvalho de Melo skb->tail += off; 780b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 781b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 782b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 783172a863fSHerbert Xu skb->csum_start += nhead; 7841da177e4SLinus Torvalds skb->cloned = 0; 785334a8132SPatrick McHardy skb->hdr_len = 0; 7861da177e4SLinus Torvalds skb->nohdr = 0; 7871da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 7881da177e4SLinus Torvalds return 0; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds nodata: 7911da177e4SLinus Torvalds return -ENOMEM; 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 7971da177e4SLinus Torvalds { 7981da177e4SLinus Torvalds struct sk_buff *skb2; 7991da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds if (delta <= 0) 8021da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 8031da177e4SLinus Torvalds else { 8041da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 8051da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 8061da177e4SLinus Torvalds GFP_ATOMIC)) { 8071da177e4SLinus Torvalds kfree_skb(skb2); 8081da177e4SLinus Torvalds skb2 = NULL; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds return skb2; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds /** 8161da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 8171da177e4SLinus Torvalds * @skb: buffer to copy 8181da177e4SLinus Torvalds * @newheadroom: new free bytes at head 8191da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 8201da177e4SLinus Torvalds * @gfp_mask: allocation priority 8211da177e4SLinus Torvalds * 8221da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 8231da177e4SLinus Torvalds * allocate additional space. 8241da177e4SLinus Torvalds * 8251da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 8261da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 8271da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 8281da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 8291da177e4SLinus Torvalds * 8301da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 8311da177e4SLinus Torvalds * is called from an interrupt. 8321da177e4SLinus Torvalds */ 8331da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 83486a76cafSVictor Fusco int newheadroom, int newtailroom, 835dd0fc66fSAl Viro gfp_t gfp_mask) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds /* 8381da177e4SLinus Torvalds * Allocate the copy buffer 8391da177e4SLinus Torvalds */ 8401da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 8411da177e4SLinus Torvalds gfp_mask); 842efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 8431da177e4SLinus Torvalds int head_copy_len, head_copy_off; 84452886051SHerbert Xu int off; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds if (!n) 8471da177e4SLinus Torvalds return NULL; 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds skb_reserve(n, newheadroom); 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds /* Set the tail pointer and length */ 8521da177e4SLinus Torvalds skb_put(n, skb->len); 8531da177e4SLinus Torvalds 854efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 8551da177e4SLinus Torvalds head_copy_off = 0; 8561da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 8571da177e4SLinus Torvalds head_copy_len = newheadroom; 8581da177e4SLinus Torvalds else 8591da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* Copy the linear header and data. */ 8621da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 8631da177e4SLinus Torvalds skb->len + head_copy_len)) 8641da177e4SLinus Torvalds BUG(); 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds copy_skb_header(n, skb); 8671da177e4SLinus Torvalds 868efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 86952886051SHerbert Xu n->csum_start += off; 87052886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 871efd1e8d5SPatrick McHardy n->transport_header += off; 872efd1e8d5SPatrick McHardy n->network_header += off; 873efd1e8d5SPatrick McHardy n->mac_header += off; 87452886051SHerbert Xu #endif 875efd1e8d5SPatrick McHardy 8761da177e4SLinus Torvalds return n; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /** 8801da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 8811da177e4SLinus Torvalds * @skb: buffer to pad 8821da177e4SLinus Torvalds * @pad: space to pad 8831da177e4SLinus Torvalds * 8841da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 8851da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 8861da177e4SLinus Torvalds * beyond the buffer end onto the wire. 8871da177e4SLinus Torvalds * 8885b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 8891da177e4SLinus Torvalds */ 8901da177e4SLinus Torvalds 8915b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 8921da177e4SLinus Torvalds { 8935b057c6bSHerbert Xu int err; 8945b057c6bSHerbert Xu int ntail; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 8975b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 8981da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 8995b057c6bSHerbert Xu return 0; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9024305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 9035b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 9045b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 9055b057c6bSHerbert Xu if (unlikely(err)) 9065b057c6bSHerbert Xu goto free_skb; 9075b057c6bSHerbert Xu } 9085b057c6bSHerbert Xu 9095b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 9105b057c6bSHerbert Xu * to be audited. 9115b057c6bSHerbert Xu */ 9125b057c6bSHerbert Xu err = skb_linearize(skb); 9135b057c6bSHerbert Xu if (unlikely(err)) 9145b057c6bSHerbert Xu goto free_skb; 9155b057c6bSHerbert Xu 9165b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 9175b057c6bSHerbert Xu return 0; 9185b057c6bSHerbert Xu 9195b057c6bSHerbert Xu free_skb: 9201da177e4SLinus Torvalds kfree_skb(skb); 9215b057c6bSHerbert Xu return err; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 9240dde3e16SIlpo Järvinen /** 9250dde3e16SIlpo Järvinen * skb_put - add data to a buffer 9260dde3e16SIlpo Järvinen * @skb: buffer to use 9270dde3e16SIlpo Järvinen * @len: amount of data to add 9280dde3e16SIlpo Järvinen * 9290dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 9300dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 9310dde3e16SIlpo Järvinen * first byte of the extra data is returned. 9320dde3e16SIlpo Järvinen */ 9330dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 9340dde3e16SIlpo Järvinen { 9350dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 9360dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 9370dde3e16SIlpo Järvinen skb->tail += len; 9380dde3e16SIlpo Järvinen skb->len += len; 9390dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 9400dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 9410dde3e16SIlpo Järvinen return tmp; 9420dde3e16SIlpo Järvinen } 9430dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 9440dde3e16SIlpo Järvinen 9456be8ac2fSIlpo Järvinen /** 946c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 947c2aa270aSIlpo Järvinen * @skb: buffer to use 948c2aa270aSIlpo Järvinen * @len: amount of data to add 949c2aa270aSIlpo Järvinen * 950c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 951c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 952c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 953c2aa270aSIlpo Järvinen */ 954c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 955c2aa270aSIlpo Järvinen { 956c2aa270aSIlpo Järvinen skb->data -= len; 957c2aa270aSIlpo Järvinen skb->len += len; 958c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 959c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 960c2aa270aSIlpo Järvinen return skb->data; 961c2aa270aSIlpo Järvinen } 962c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 963c2aa270aSIlpo Järvinen 964c2aa270aSIlpo Järvinen /** 9656be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 9666be8ac2fSIlpo Järvinen * @skb: buffer to use 9676be8ac2fSIlpo Järvinen * @len: amount of data to remove 9686be8ac2fSIlpo Järvinen * 9696be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 9706be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 9716be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 9726be8ac2fSIlpo Järvinen * the old data. 9736be8ac2fSIlpo Järvinen */ 9746be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 9756be8ac2fSIlpo Järvinen { 9766be8ac2fSIlpo Järvinen return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 9776be8ac2fSIlpo Järvinen } 9786be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 9796be8ac2fSIlpo Järvinen 980419ae74eSIlpo Järvinen /** 981419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 982419ae74eSIlpo Järvinen * @skb: buffer to alter 983419ae74eSIlpo Järvinen * @len: new length 984419ae74eSIlpo Järvinen * 985419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 986419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 987419ae74eSIlpo Järvinen * The skb must be linear. 988419ae74eSIlpo Järvinen */ 989419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 990419ae74eSIlpo Järvinen { 991419ae74eSIlpo Järvinen if (skb->len > len) 992419ae74eSIlpo Järvinen __skb_trim(skb, len); 993419ae74eSIlpo Järvinen } 994419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 995419ae74eSIlpo Järvinen 9963cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds 9993cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 10001da177e4SLinus Torvalds { 100127b437c8SHerbert Xu struct sk_buff **fragp; 100227b437c8SHerbert Xu struct sk_buff *frag; 10031da177e4SLinus Torvalds int offset = skb_headlen(skb); 10041da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 10051da177e4SLinus Torvalds int i; 100627b437c8SHerbert Xu int err; 100727b437c8SHerbert Xu 100827b437c8SHerbert Xu if (skb_cloned(skb) && 100927b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 101027b437c8SHerbert Xu return err; 10111da177e4SLinus Torvalds 1012f4d26fb3SHerbert Xu i = 0; 1013f4d26fb3SHerbert Xu if (offset >= len) 1014f4d26fb3SHerbert Xu goto drop_pages; 1015f4d26fb3SHerbert Xu 1016f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 10171da177e4SLinus Torvalds int end = offset + skb_shinfo(skb)->frags[i].size; 101827b437c8SHerbert Xu 101927b437c8SHerbert Xu if (end < len) { 10201da177e4SLinus Torvalds offset = end; 102127b437c8SHerbert Xu continue; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 102427b437c8SHerbert Xu skb_shinfo(skb)->frags[i++].size = len - offset; 102527b437c8SHerbert Xu 1026f4d26fb3SHerbert Xu drop_pages: 102727b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 102827b437c8SHerbert Xu 102927b437c8SHerbert Xu for (; i < nfrags; i++) 103027b437c8SHerbert Xu put_page(skb_shinfo(skb)->frags[i].page); 103127b437c8SHerbert Xu 103227b437c8SHerbert Xu if (skb_shinfo(skb)->frag_list) 103327b437c8SHerbert Xu skb_drop_fraglist(skb); 1034f4d26fb3SHerbert Xu goto done; 103527b437c8SHerbert Xu } 103627b437c8SHerbert Xu 103727b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 103827b437c8SHerbert Xu fragp = &frag->next) { 103927b437c8SHerbert Xu int end = offset + frag->len; 104027b437c8SHerbert Xu 104127b437c8SHerbert Xu if (skb_shared(frag)) { 104227b437c8SHerbert Xu struct sk_buff *nfrag; 104327b437c8SHerbert Xu 104427b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 104527b437c8SHerbert Xu if (unlikely(!nfrag)) 104627b437c8SHerbert Xu return -ENOMEM; 104727b437c8SHerbert Xu 104827b437c8SHerbert Xu nfrag->next = frag->next; 1049f4d26fb3SHerbert Xu kfree_skb(frag); 105027b437c8SHerbert Xu frag = nfrag; 105127b437c8SHerbert Xu *fragp = frag; 105227b437c8SHerbert Xu } 105327b437c8SHerbert Xu 105427b437c8SHerbert Xu if (end < len) { 105527b437c8SHerbert Xu offset = end; 105627b437c8SHerbert Xu continue; 105727b437c8SHerbert Xu } 105827b437c8SHerbert Xu 105927b437c8SHerbert Xu if (end > len && 106027b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 106127b437c8SHerbert Xu return err; 106227b437c8SHerbert Xu 106327b437c8SHerbert Xu if (frag->next) 106427b437c8SHerbert Xu skb_drop_list(&frag->next); 106527b437c8SHerbert Xu break; 106627b437c8SHerbert Xu } 106727b437c8SHerbert Xu 1068f4d26fb3SHerbert Xu done: 106927b437c8SHerbert Xu if (len > skb_headlen(skb)) { 10701da177e4SLinus Torvalds skb->data_len -= skb->len - len; 10711da177e4SLinus Torvalds skb->len = len; 10721da177e4SLinus Torvalds } else { 10731da177e4SLinus Torvalds skb->len = len; 10741da177e4SLinus Torvalds skb->data_len = 0; 107527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds return 0; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds /** 10821da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 10831da177e4SLinus Torvalds * @skb: buffer to reallocate 10841da177e4SLinus Torvalds * @delta: number of bytes to advance tail 10851da177e4SLinus Torvalds * 10861da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 10871da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 10881da177e4SLinus Torvalds * data from fragmented part. 10891da177e4SLinus Torvalds * 10901da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 10911da177e4SLinus Torvalds * 10921da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 10931da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 10941da177e4SLinus Torvalds * 10951da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 10961da177e4SLinus Torvalds * reloaded after call to this function. 10971da177e4SLinus Torvalds */ 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 11001da177e4SLinus Torvalds * when it is necessary. 11011da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 11021da177e4SLinus Torvalds * 2. It may change skb pointers. 11031da177e4SLinus Torvalds * 11041da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 11051da177e4SLinus Torvalds */ 11061da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 11091da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 11101da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 11111da177e4SLinus Torvalds */ 11124305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 11151da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 11161da177e4SLinus Torvalds GFP_ATOMIC)) 11171da177e4SLinus Torvalds return NULL; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 112027a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 11211da177e4SLinus Torvalds BUG(); 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 11241da177e4SLinus Torvalds * size of pulled pages. Superb. 11251da177e4SLinus Torvalds */ 11261da177e4SLinus Torvalds if (!skb_shinfo(skb)->frag_list) 11271da177e4SLinus Torvalds goto pull_pages; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 11301da177e4SLinus Torvalds eat = delta; 11311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11321da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size >= eat) 11331da177e4SLinus Torvalds goto pull_pages; 11341da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 11381da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 11391da177e4SLinus Torvalds * but taking into account that pulling is expected to 11401da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 11411da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 11421da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 11431da177e4SLinus Torvalds */ 11441da177e4SLinus Torvalds if (eat) { 11451da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 11461da177e4SLinus Torvalds struct sk_buff *clone = NULL; 11471da177e4SLinus Torvalds struct sk_buff *insp = NULL; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds do { 115009a62660SKris Katterjohn BUG_ON(!list); 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds if (list->len <= eat) { 11531da177e4SLinus Torvalds /* Eaten as whole. */ 11541da177e4SLinus Torvalds eat -= list->len; 11551da177e4SLinus Torvalds list = list->next; 11561da177e4SLinus Torvalds insp = list; 11571da177e4SLinus Torvalds } else { 11581da177e4SLinus Torvalds /* Eaten partially. */ 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (skb_shared(list)) { 11611da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 11621da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 11631da177e4SLinus Torvalds if (!clone) 11641da177e4SLinus Torvalds return NULL; 11651da177e4SLinus Torvalds insp = list->next; 11661da177e4SLinus Torvalds list = clone; 11671da177e4SLinus Torvalds } else { 11681da177e4SLinus Torvalds /* This may be pulled without 11691da177e4SLinus Torvalds * problems. */ 11701da177e4SLinus Torvalds insp = list; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 11731da177e4SLinus Torvalds if (clone) 11741da177e4SLinus Torvalds kfree_skb(clone); 11751da177e4SLinus Torvalds return NULL; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds break; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds } while (eat); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* Free pulled out fragments. */ 11821da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 11831da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 11841da177e4SLinus Torvalds kfree_skb(list); 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds /* And insert new clone at head. */ 11871da177e4SLinus Torvalds if (clone) { 11881da177e4SLinus Torvalds clone->next = list; 11891da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds pull_pages: 11951da177e4SLinus Torvalds eat = delta; 11961da177e4SLinus Torvalds k = 0; 11971da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11981da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size <= eat) { 11991da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 12001da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 12011da177e4SLinus Torvalds } else { 12021da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 12031da177e4SLinus Torvalds if (eat) { 12041da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 12051da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].size -= eat; 12061da177e4SLinus Torvalds eat = 0; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds k++; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds skb->tail += delta; 12141da177e4SLinus Torvalds skb->data_len -= delta; 12151da177e4SLinus Torvalds 121627a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */ 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds int i, copy; 12241a028e50SDavid S. Miller int start = skb_headlen(skb); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (offset > (int)skb->len - len) 12271da177e4SLinus Torvalds goto fault; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* Copy header. */ 12301a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 12311da177e4SLinus Torvalds if (copy > len) 12321da177e4SLinus Torvalds copy = len; 1233d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 12341da177e4SLinus Torvalds if ((len -= copy) == 0) 12351da177e4SLinus Torvalds return 0; 12361da177e4SLinus Torvalds offset += copy; 12371da177e4SLinus Torvalds to += copy; 12381da177e4SLinus Torvalds } 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12411a028e50SDavid S. Miller int end; 12421da177e4SLinus Torvalds 1243547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12441a028e50SDavid S. Miller 12451a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 12461da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12471da177e4SLinus Torvalds u8 *vaddr; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (copy > len) 12501da177e4SLinus Torvalds copy = len; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 12531da177e4SLinus Torvalds memcpy(to, 12541a028e50SDavid S. Miller vaddr + skb_shinfo(skb)->frags[i].page_offset+ 12551a028e50SDavid S. Miller offset - start, copy); 12561da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds if ((len -= copy) == 0) 12591da177e4SLinus Torvalds return 0; 12601da177e4SLinus Torvalds offset += copy; 12611da177e4SLinus Torvalds to += copy; 12621da177e4SLinus Torvalds } 12631a028e50SDavid S. Miller start = end; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 12671da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds for (; list; list = list->next) { 12701a028e50SDavid S. Miller int end; 12711da177e4SLinus Torvalds 1272547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12731a028e50SDavid S. Miller 12741a028e50SDavid S. Miller end = start + list->len; 12751da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12761da177e4SLinus Torvalds if (copy > len) 12771da177e4SLinus Torvalds copy = len; 12781a028e50SDavid S. Miller if (skb_copy_bits(list, offset - start, 12791a028e50SDavid S. Miller to, copy)) 12801da177e4SLinus Torvalds goto fault; 12811da177e4SLinus Torvalds if ((len -= copy) == 0) 12821da177e4SLinus Torvalds return 0; 12831da177e4SLinus Torvalds offset += copy; 12841da177e4SLinus Torvalds to += copy; 12851da177e4SLinus Torvalds } 12861a028e50SDavid S. Miller start = end; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds if (!len) 12901da177e4SLinus Torvalds return 0; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds fault: 12931da177e4SLinus Torvalds return -EFAULT; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12969c55e01cSJens Axboe /* 12979c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 12989c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 12999c55e01cSJens Axboe */ 13009c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 13019c55e01cSJens Axboe { 13029c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; 13039c55e01cSJens Axboe 13049c55e01cSJens Axboe kfree_skb(skb); 13059c55e01cSJens Axboe } 13069c55e01cSJens Axboe 13079c55e01cSJens Axboe /* 13089c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 13099c55e01cSJens Axboe */ 13109c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, 13119c55e01cSJens Axboe unsigned int len, unsigned int offset, 13129c55e01cSJens Axboe struct sk_buff *skb) 13139c55e01cSJens Axboe { 13149c55e01cSJens Axboe if (unlikely(spd->nr_pages == PIPE_BUFFERS)) 13159c55e01cSJens Axboe return 1; 13169c55e01cSJens Axboe 13179c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 13189c55e01cSJens Axboe spd->partial[spd->nr_pages].len = len; 13199c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 13209c55e01cSJens Axboe spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); 13219c55e01cSJens Axboe spd->nr_pages++; 13229c55e01cSJens Axboe return 0; 13239c55e01cSJens Axboe } 13249c55e01cSJens Axboe 13252870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 13262870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 13272870c43dSOctavian Purdila { 13282870c43dSOctavian Purdila *poff += off; 13292870c43dSOctavian Purdila *page += *poff / PAGE_SIZE; 13302870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 13312870c43dSOctavian Purdila *plen -= off; 13322870c43dSOctavian Purdila } 13332870c43dSOctavian Purdila 13342870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff, 13352870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 13362870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 13379c55e01cSJens Axboe struct splice_pipe_desc *spd) 13389c55e01cSJens Axboe { 13392870c43dSOctavian Purdila if (!*len) 13402870c43dSOctavian Purdila return 1; 13419c55e01cSJens Axboe 13422870c43dSOctavian Purdila /* skip this segment if already processed */ 13432870c43dSOctavian Purdila if (*off >= plen) { 13442870c43dSOctavian Purdila *off -= plen; 13452870c43dSOctavian Purdila return 0; 13462870c43dSOctavian Purdila } 13472870c43dSOctavian Purdila 13482870c43dSOctavian Purdila /* ignore any bits we already processed */ 13492870c43dSOctavian Purdila if (*off) { 13502870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 13512870c43dSOctavian Purdila *off = 0; 13522870c43dSOctavian Purdila } 13532870c43dSOctavian Purdila 13542870c43dSOctavian Purdila do { 13552870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 13562870c43dSOctavian Purdila 13572870c43dSOctavian Purdila /* the linear region may spread across several pages */ 13582870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 13592870c43dSOctavian Purdila 13602870c43dSOctavian Purdila if (spd_fill_page(spd, page, flen, poff, skb)) 13612870c43dSOctavian Purdila return 1; 13622870c43dSOctavian Purdila 13632870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 13642870c43dSOctavian Purdila *len -= flen; 13652870c43dSOctavian Purdila 13662870c43dSOctavian Purdila } while (*len && plen); 13672870c43dSOctavian Purdila 13682870c43dSOctavian Purdila return 0; 1369db43a282SOctavian Purdila } 13709c55e01cSJens Axboe 13719c55e01cSJens Axboe /* 13722870c43dSOctavian Purdila * Map linear and fragment data from the skb to spd. It reports failure if the 13732870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 13749c55e01cSJens Axboe */ 13757b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, 13762870c43dSOctavian Purdila unsigned int *len, 13772870c43dSOctavian Purdila struct splice_pipe_desc *spd) 13782870c43dSOctavian Purdila { 13792870c43dSOctavian Purdila int seg; 13809c55e01cSJens Axboe 13819c55e01cSJens Axboe /* 13822870c43dSOctavian Purdila * map the linear part 13839c55e01cSJens Axboe */ 13842870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 13852870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 13862870c43dSOctavian Purdila skb_headlen(skb), 13872870c43dSOctavian Purdila offset, len, skb, spd)) 13882870c43dSOctavian Purdila return 1; 13899c55e01cSJens Axboe 13909c55e01cSJens Axboe /* 13919c55e01cSJens Axboe * then map the fragments 13929c55e01cSJens Axboe */ 13939c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 13949c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 13959c55e01cSJens Axboe 13962870c43dSOctavian Purdila if (__splice_segment(f->page, f->page_offset, f->size, 13972870c43dSOctavian Purdila offset, len, skb, spd)) 13982870c43dSOctavian Purdila return 1; 13999c55e01cSJens Axboe } 14009c55e01cSJens Axboe 14019c55e01cSJens Axboe return 0; 14029c55e01cSJens Axboe } 14039c55e01cSJens Axboe 14049c55e01cSJens Axboe /* 14059c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 14069c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 14079c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 14089c55e01cSJens Axboe * handle that cleanly. 14099c55e01cSJens Axboe */ 14109c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, 14119c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 14129c55e01cSJens Axboe unsigned int flags) 14139c55e01cSJens Axboe { 14149c55e01cSJens Axboe struct partial_page partial[PIPE_BUFFERS]; 14159c55e01cSJens Axboe struct page *pages[PIPE_BUFFERS]; 14169c55e01cSJens Axboe struct splice_pipe_desc spd = { 14179c55e01cSJens Axboe .pages = pages, 14189c55e01cSJens Axboe .partial = partial, 14199c55e01cSJens Axboe .flags = flags, 14209c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 14219c55e01cSJens Axboe .spd_release = sock_spd_release, 14229c55e01cSJens Axboe }; 14239c55e01cSJens Axboe struct sk_buff *skb; 14249c55e01cSJens Axboe 14259c55e01cSJens Axboe /* 14269c55e01cSJens Axboe * I'd love to avoid the clone here, but tcp_read_sock() 14279c55e01cSJens Axboe * ignores reference counts and unconditonally kills the sk_buff 14289c55e01cSJens Axboe * on return from the actor. 14299c55e01cSJens Axboe */ 14309c55e01cSJens Axboe skb = skb_clone(__skb, GFP_KERNEL); 14319c55e01cSJens Axboe if (unlikely(!skb)) 14329c55e01cSJens Axboe return -ENOMEM; 14339c55e01cSJens Axboe 14349c55e01cSJens Axboe /* 14359c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 14369c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 14379c55e01cSJens Axboe */ 14389c55e01cSJens Axboe if (__skb_splice_bits(skb, &offset, &tlen, &spd)) 14399c55e01cSJens Axboe goto done; 14409c55e01cSJens Axboe else if (!tlen) 14419c55e01cSJens Axboe goto done; 14429c55e01cSJens Axboe 14439c55e01cSJens Axboe /* 14449c55e01cSJens Axboe * now see if we have a frag_list to map 14459c55e01cSJens Axboe */ 14469c55e01cSJens Axboe if (skb_shinfo(skb)->frag_list) { 14479c55e01cSJens Axboe struct sk_buff *list = skb_shinfo(skb)->frag_list; 14489c55e01cSJens Axboe 14499c55e01cSJens Axboe for (; list && tlen; list = list->next) { 14509c55e01cSJens Axboe if (__skb_splice_bits(list, &offset, &tlen, &spd)) 14519c55e01cSJens Axboe break; 14529c55e01cSJens Axboe } 14539c55e01cSJens Axboe } 14549c55e01cSJens Axboe 14559c55e01cSJens Axboe done: 14569c55e01cSJens Axboe /* 14579c55e01cSJens Axboe * drop our reference to the clone, the pipe consumption will 14589c55e01cSJens Axboe * drop the rest. 14599c55e01cSJens Axboe */ 14609c55e01cSJens Axboe kfree_skb(skb); 14619c55e01cSJens Axboe 14629c55e01cSJens Axboe if (spd.nr_pages) { 14639c55e01cSJens Axboe int ret; 1464293ad604SOctavian Purdila struct sock *sk = __skb->sk; 14659c55e01cSJens Axboe 14669c55e01cSJens Axboe /* 14679c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 14689c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 14699c55e01cSJens Axboe * here as compared to sendfile(). We enter here 14709c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 14719c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 14729c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 14739c55e01cSJens Axboe * and networking will grab the socket lock. 14749c55e01cSJens Axboe */ 1475293ad604SOctavian Purdila release_sock(sk); 14769c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1477293ad604SOctavian Purdila lock_sock(sk); 14789c55e01cSJens Axboe return ret; 14799c55e01cSJens Axboe } 14809c55e01cSJens Axboe 14819c55e01cSJens Axboe return 0; 14829c55e01cSJens Axboe } 14839c55e01cSJens Axboe 1484357b40a1SHerbert Xu /** 1485357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1486357b40a1SHerbert Xu * @skb: destination buffer 1487357b40a1SHerbert Xu * @offset: offset in destination 1488357b40a1SHerbert Xu * @from: source buffer 1489357b40a1SHerbert Xu * @len: number of bytes to copy 1490357b40a1SHerbert Xu * 1491357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1492357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1493357b40a1SHerbert Xu * traversing fragment lists and such. 1494357b40a1SHerbert Xu */ 1495357b40a1SHerbert Xu 14960c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1497357b40a1SHerbert Xu { 1498357b40a1SHerbert Xu int i, copy; 14991a028e50SDavid S. Miller int start = skb_headlen(skb); 1500357b40a1SHerbert Xu 1501357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1502357b40a1SHerbert Xu goto fault; 1503357b40a1SHerbert Xu 15041a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1505357b40a1SHerbert Xu if (copy > len) 1506357b40a1SHerbert Xu copy = len; 150727d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1508357b40a1SHerbert Xu if ((len -= copy) == 0) 1509357b40a1SHerbert Xu return 0; 1510357b40a1SHerbert Xu offset += copy; 1511357b40a1SHerbert Xu from += copy; 1512357b40a1SHerbert Xu } 1513357b40a1SHerbert Xu 1514357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1515357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 15161a028e50SDavid S. Miller int end; 1517357b40a1SHerbert Xu 1518547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15191a028e50SDavid S. Miller 15201a028e50SDavid S. Miller end = start + frag->size; 1521357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1522357b40a1SHerbert Xu u8 *vaddr; 1523357b40a1SHerbert Xu 1524357b40a1SHerbert Xu if (copy > len) 1525357b40a1SHerbert Xu copy = len; 1526357b40a1SHerbert Xu 1527357b40a1SHerbert Xu vaddr = kmap_skb_frag(frag); 15281a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 15291a028e50SDavid S. Miller from, copy); 1530357b40a1SHerbert Xu kunmap_skb_frag(vaddr); 1531357b40a1SHerbert Xu 1532357b40a1SHerbert Xu if ((len -= copy) == 0) 1533357b40a1SHerbert Xu return 0; 1534357b40a1SHerbert Xu offset += copy; 1535357b40a1SHerbert Xu from += copy; 1536357b40a1SHerbert Xu } 15371a028e50SDavid S. Miller start = end; 1538357b40a1SHerbert Xu } 1539357b40a1SHerbert Xu 1540357b40a1SHerbert Xu if (skb_shinfo(skb)->frag_list) { 1541357b40a1SHerbert Xu struct sk_buff *list = skb_shinfo(skb)->frag_list; 1542357b40a1SHerbert Xu 1543357b40a1SHerbert Xu for (; list; list = list->next) { 15441a028e50SDavid S. Miller int end; 1545357b40a1SHerbert Xu 1546547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15471a028e50SDavid S. Miller 15481a028e50SDavid S. Miller end = start + list->len; 1549357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1550357b40a1SHerbert Xu if (copy > len) 1551357b40a1SHerbert Xu copy = len; 15521a028e50SDavid S. Miller if (skb_store_bits(list, offset - start, 15531a028e50SDavid S. Miller from, copy)) 1554357b40a1SHerbert Xu goto fault; 1555357b40a1SHerbert Xu if ((len -= copy) == 0) 1556357b40a1SHerbert Xu return 0; 1557357b40a1SHerbert Xu offset += copy; 1558357b40a1SHerbert Xu from += copy; 1559357b40a1SHerbert Xu } 15601a028e50SDavid S. Miller start = end; 1561357b40a1SHerbert Xu } 1562357b40a1SHerbert Xu } 1563357b40a1SHerbert Xu if (!len) 1564357b40a1SHerbert Xu return 0; 1565357b40a1SHerbert Xu 1566357b40a1SHerbert Xu fault: 1567357b40a1SHerbert Xu return -EFAULT; 1568357b40a1SHerbert Xu } 1569357b40a1SHerbert Xu 1570357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1571357b40a1SHerbert Xu 15721da177e4SLinus Torvalds /* Checksum skb data. */ 15731da177e4SLinus Torvalds 15742bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 15752bbbc868SAl Viro int len, __wsum csum) 15761da177e4SLinus Torvalds { 15771a028e50SDavid S. Miller int start = skb_headlen(skb); 15781a028e50SDavid S. Miller int i, copy = start - offset; 15791da177e4SLinus Torvalds int pos = 0; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* Checksum header. */ 15821da177e4SLinus Torvalds if (copy > 0) { 15831da177e4SLinus Torvalds if (copy > len) 15841da177e4SLinus Torvalds copy = len; 15851da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 15861da177e4SLinus Torvalds if ((len -= copy) == 0) 15871da177e4SLinus Torvalds return csum; 15881da177e4SLinus Torvalds offset += copy; 15891da177e4SLinus Torvalds pos = copy; 15901da177e4SLinus Torvalds } 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15931a028e50SDavid S. Miller int end; 15941da177e4SLinus Torvalds 1595547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15961a028e50SDavid S. Miller 15971a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 15981da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 159944bb9363SAl Viro __wsum csum2; 16001da177e4SLinus Torvalds u8 *vaddr; 16011da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds if (copy > len) 16041da177e4SLinus Torvalds copy = len; 16051da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 16061a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 16071a028e50SDavid S. Miller offset - start, copy, 0); 16081da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 16091da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16101da177e4SLinus Torvalds if (!(len -= copy)) 16111da177e4SLinus Torvalds return csum; 16121da177e4SLinus Torvalds offset += copy; 16131da177e4SLinus Torvalds pos += copy; 16141da177e4SLinus Torvalds } 16151a028e50SDavid S. Miller start = end; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 16191da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds for (; list; list = list->next) { 16221a028e50SDavid S. Miller int end; 16231da177e4SLinus Torvalds 1624547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16251a028e50SDavid S. Miller 16261a028e50SDavid S. Miller end = start + list->len; 16271da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16285f92a738SAl Viro __wsum csum2; 16291da177e4SLinus Torvalds if (copy > len) 16301da177e4SLinus Torvalds copy = len; 16311a028e50SDavid S. Miller csum2 = skb_checksum(list, offset - start, 16321a028e50SDavid S. Miller copy, 0); 16331da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16341da177e4SLinus Torvalds if ((len -= copy) == 0) 16351da177e4SLinus Torvalds return csum; 16361da177e4SLinus Torvalds offset += copy; 16371da177e4SLinus Torvalds pos += copy; 16381da177e4SLinus Torvalds } 16391a028e50SDavid S. Miller start = end; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds } 164209a62660SKris Katterjohn BUG_ON(len); 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds return csum; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* Both of above in one bottle. */ 16481da177e4SLinus Torvalds 164981d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 165081d77662SAl Viro u8 *to, int len, __wsum csum) 16511da177e4SLinus Torvalds { 16521a028e50SDavid S. Miller int start = skb_headlen(skb); 16531a028e50SDavid S. Miller int i, copy = start - offset; 16541da177e4SLinus Torvalds int pos = 0; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds /* Copy header. */ 16571da177e4SLinus Torvalds if (copy > 0) { 16581da177e4SLinus Torvalds if (copy > len) 16591da177e4SLinus Torvalds copy = len; 16601da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 16611da177e4SLinus Torvalds copy, csum); 16621da177e4SLinus Torvalds if ((len -= copy) == 0) 16631da177e4SLinus Torvalds return csum; 16641da177e4SLinus Torvalds offset += copy; 16651da177e4SLinus Torvalds to += copy; 16661da177e4SLinus Torvalds pos = copy; 16671da177e4SLinus Torvalds } 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16701a028e50SDavid S. Miller int end; 16711da177e4SLinus Torvalds 1672547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16731a028e50SDavid S. Miller 16741a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 16751da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16765084205fSAl Viro __wsum csum2; 16771da177e4SLinus Torvalds u8 *vaddr; 16781da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds if (copy > len) 16811da177e4SLinus Torvalds copy = len; 16821da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 16831da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 16841a028e50SDavid S. Miller frag->page_offset + 16851a028e50SDavid S. Miller offset - start, to, 16861a028e50SDavid S. Miller copy, 0); 16871da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 16881da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16891da177e4SLinus Torvalds if (!(len -= copy)) 16901da177e4SLinus Torvalds return csum; 16911da177e4SLinus Torvalds offset += copy; 16921da177e4SLinus Torvalds to += copy; 16931da177e4SLinus Torvalds pos += copy; 16941da177e4SLinus Torvalds } 16951a028e50SDavid S. Miller start = end; 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 16991da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds for (; list; list = list->next) { 170281d77662SAl Viro __wsum csum2; 17031a028e50SDavid S. Miller int end; 17041da177e4SLinus Torvalds 1705547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17061a028e50SDavid S. Miller 17071a028e50SDavid S. Miller end = start + list->len; 17081da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17091da177e4SLinus Torvalds if (copy > len) 17101da177e4SLinus Torvalds copy = len; 17111a028e50SDavid S. Miller csum2 = skb_copy_and_csum_bits(list, 17121a028e50SDavid S. Miller offset - start, 17131da177e4SLinus Torvalds to, copy, 0); 17141da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 17151da177e4SLinus Torvalds if ((len -= copy) == 0) 17161da177e4SLinus Torvalds return csum; 17171da177e4SLinus Torvalds offset += copy; 17181da177e4SLinus Torvalds to += copy; 17191da177e4SLinus Torvalds pos += copy; 17201da177e4SLinus Torvalds } 17211a028e50SDavid S. Miller start = end; 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds } 172409a62660SKris Katterjohn BUG_ON(len); 17251da177e4SLinus Torvalds return csum; 17261da177e4SLinus Torvalds } 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 17291da177e4SLinus Torvalds { 1730d3bc23e7SAl Viro __wsum csum; 17311da177e4SLinus Torvalds long csstart; 17321da177e4SLinus Torvalds 173384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 1734663ead3bSHerbert Xu csstart = skb->csum_start - skb_headroom(skb); 17351da177e4SLinus Torvalds else 17361da177e4SLinus Torvalds csstart = skb_headlen(skb); 17371da177e4SLinus Torvalds 173809a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 17391da177e4SLinus Torvalds 1740d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds csum = 0; 17431da177e4SLinus Torvalds if (csstart != skb->len) 17441da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 17451da177e4SLinus Torvalds skb->len - csstart, 0); 17461da177e4SLinus Torvalds 174784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 1748ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 17491da177e4SLinus Torvalds 1750d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /** 17551da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 17561da177e4SLinus Torvalds * @list: list to dequeue from 17571da177e4SLinus Torvalds * 17581da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 17591da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 17601da177e4SLinus Torvalds * returned or %NULL if the list is empty. 17611da177e4SLinus Torvalds */ 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds unsigned long flags; 17661da177e4SLinus Torvalds struct sk_buff *result; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17691da177e4SLinus Torvalds result = __skb_dequeue(list); 17701da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17711da177e4SLinus Torvalds return result; 17721da177e4SLinus Torvalds } 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds /** 17751da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 17761da177e4SLinus Torvalds * @list: list to dequeue from 17771da177e4SLinus Torvalds * 17781da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 17791da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 17801da177e4SLinus Torvalds * returned or %NULL if the list is empty. 17811da177e4SLinus Torvalds */ 17821da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 17831da177e4SLinus Torvalds { 17841da177e4SLinus Torvalds unsigned long flags; 17851da177e4SLinus Torvalds struct sk_buff *result; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17881da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 17891da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17901da177e4SLinus Torvalds return result; 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds /** 17941da177e4SLinus Torvalds * skb_queue_purge - empty a list 17951da177e4SLinus Torvalds * @list: list to empty 17961da177e4SLinus Torvalds * 17971da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 17981da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 17991da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 18001da177e4SLinus Torvalds */ 18011da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds struct sk_buff *skb; 18041da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 18051da177e4SLinus Torvalds kfree_skb(skb); 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /** 18091da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 18101da177e4SLinus Torvalds * @list: list to use 18111da177e4SLinus Torvalds * @newsk: buffer to queue 18121da177e4SLinus Torvalds * 18131da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 18141da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 18151da177e4SLinus Torvalds * safely. 18161da177e4SLinus Torvalds * 18171da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18181da177e4SLinus Torvalds */ 18191da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 18201da177e4SLinus Torvalds { 18211da177e4SLinus Torvalds unsigned long flags; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18241da177e4SLinus Torvalds __skb_queue_head(list, newsk); 18251da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds /** 18291da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 18301da177e4SLinus Torvalds * @list: list to use 18311da177e4SLinus Torvalds * @newsk: buffer to queue 18321da177e4SLinus Torvalds * 18331da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 18341da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 18351da177e4SLinus Torvalds * safely. 18361da177e4SLinus Torvalds * 18371da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18381da177e4SLinus Torvalds */ 18391da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 18401da177e4SLinus Torvalds { 18411da177e4SLinus Torvalds unsigned long flags; 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18441da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 18451da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18461da177e4SLinus Torvalds } 18478728b834SDavid S. Miller 18481da177e4SLinus Torvalds /** 18491da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 18501da177e4SLinus Torvalds * @skb: buffer to remove 18518728b834SDavid S. Miller * @list: list to use 18521da177e4SLinus Torvalds * 18538728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 18548728b834SDavid S. Miller * function is atomic with respect to other list locked calls 18551da177e4SLinus Torvalds * 18568728b834SDavid S. Miller * You must know what list the SKB is on. 18571da177e4SLinus Torvalds */ 18588728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds unsigned long flags; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18638728b834SDavid S. Miller __skb_unlink(skb, list); 18641da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /** 18681da177e4SLinus Torvalds * skb_append - append a buffer 18691da177e4SLinus Torvalds * @old: buffer to insert after 18701da177e4SLinus Torvalds * @newsk: buffer to insert 18718728b834SDavid S. Miller * @list: list to use 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 18741da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 18751da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18761da177e4SLinus Torvalds */ 18778728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 18781da177e4SLinus Torvalds { 18791da177e4SLinus Torvalds unsigned long flags; 18801da177e4SLinus Torvalds 18818728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 18827de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 18838728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /** 18881da177e4SLinus Torvalds * skb_insert - insert a buffer 18891da177e4SLinus Torvalds * @old: buffer to insert before 18901da177e4SLinus Torvalds * @newsk: buffer to insert 18918728b834SDavid S. Miller * @list: list to use 18921da177e4SLinus Torvalds * 18938728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 18948728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 18958728b834SDavid S. Miller * calls. 18968728b834SDavid S. Miller * 18971da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18981da177e4SLinus Torvalds */ 18998728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 19001da177e4SLinus Torvalds { 19011da177e4SLinus Torvalds unsigned long flags; 19021da177e4SLinus Torvalds 19038728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 19048728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 19058728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 19091da177e4SLinus Torvalds struct sk_buff* skb1, 19101da177e4SLinus Torvalds const u32 len, const int pos) 19111da177e4SLinus Torvalds { 19121da177e4SLinus Torvalds int i; 19131da177e4SLinus Torvalds 1914d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1915d626f62bSArnaldo Carvalho de Melo pos - len); 19161da177e4SLinus Torvalds /* And move data appendix as is. */ 19171da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 19181da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 19211da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 19221da177e4SLinus Torvalds skb1->data_len = skb->data_len; 19231da177e4SLinus Torvalds skb1->len += skb1->data_len; 19241da177e4SLinus Torvalds skb->data_len = 0; 19251da177e4SLinus Torvalds skb->len = len; 192627a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 19301da177e4SLinus Torvalds struct sk_buff* skb1, 19311da177e4SLinus Torvalds const u32 len, int pos) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds int i, k = 0; 19341da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 19371da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 19381da177e4SLinus Torvalds skb->len = len; 19391da177e4SLinus Torvalds skb->data_len = len - pos; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 19421da177e4SLinus Torvalds int size = skb_shinfo(skb)->frags[i].size; 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds if (pos + size > len) { 19451da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds if (pos < len) { 19481da177e4SLinus Torvalds /* Split frag. 19491da177e4SLinus Torvalds * We have two variants in this case: 19501da177e4SLinus Torvalds * 1. Move all the frag to the second 19511da177e4SLinus Torvalds * part, if it is possible. F.e. 19521da177e4SLinus Torvalds * this approach is mandatory for TUX, 19531da177e4SLinus Torvalds * where splitting is expensive. 19541da177e4SLinus Torvalds * 2. Split is accurately. We make this. 19551da177e4SLinus Torvalds */ 19561da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 19571da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 19581da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].size -= len - pos; 19591da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = len - pos; 19601da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds k++; 19631da177e4SLinus Torvalds } else 19641da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19651da177e4SLinus Torvalds pos += size; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /** 19711da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 19721da177e4SLinus Torvalds * @skb: the buffer to split 19731da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 19741da177e4SLinus Torvalds * @len: new length for skb 19751da177e4SLinus Torvalds */ 19761da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 19771da177e4SLinus Torvalds { 19781da177e4SLinus Torvalds int pos = skb_headlen(skb); 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 19811da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 19821da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 19831da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 1986677e90edSThomas Graf /** 1987677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 1988677e90edSThomas Graf * @skb: the buffer to read 1989677e90edSThomas Graf * @from: lower offset of data to be read 1990677e90edSThomas Graf * @to: upper offset of data to be read 1991677e90edSThomas Graf * @st: state variable 1992677e90edSThomas Graf * 1993677e90edSThomas Graf * Initializes the specified state variable. Must be called before 1994677e90edSThomas Graf * invoking skb_seq_read() for the first time. 1995677e90edSThomas Graf */ 1996677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1997677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 1998677e90edSThomas Graf { 1999677e90edSThomas Graf st->lower_offset = from; 2000677e90edSThomas Graf st->upper_offset = to; 2001677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2002677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2003677e90edSThomas Graf st->frag_data = NULL; 2004677e90edSThomas Graf } 2005677e90edSThomas Graf 2006677e90edSThomas Graf /** 2007677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2008677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2009677e90edSThomas Graf * @data: destination pointer for data to be returned 2010677e90edSThomas Graf * @st: state variable 2011677e90edSThomas Graf * 2012677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 2013677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2014677e90edSThomas Graf * the head of the data block to &data and returns the length 2015677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2016677e90edSThomas Graf * offset has been reached. 2017677e90edSThomas Graf * 2018677e90edSThomas Graf * The caller is not required to consume all of the data 2019677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 2020677e90edSThomas Graf * of bytes already consumed and the next call to 2021677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2022677e90edSThomas Graf * 2023bc2cda1eSRandy Dunlap * Note 1: The size of each block of data returned can be arbitary, 2024677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 2025677e90edSThomas Graf * reads of potentially non linear data. 2026677e90edSThomas Graf * 2027bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2028677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2029677e90edSThomas Graf * a stack for this purpose. 2030677e90edSThomas Graf */ 2031677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2032677e90edSThomas Graf struct skb_seq_state *st) 2033677e90edSThomas Graf { 2034677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2035677e90edSThomas Graf skb_frag_t *frag; 2036677e90edSThomas Graf 2037677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2038677e90edSThomas Graf return 0; 2039677e90edSThomas Graf 2040677e90edSThomas Graf next_skb: 2041677e90edSThomas Graf block_limit = skb_headlen(st->cur_skb); 2042677e90edSThomas Graf 2043677e90edSThomas Graf if (abs_offset < block_limit) { 2044677e90edSThomas Graf *data = st->cur_skb->data + abs_offset; 2045677e90edSThomas Graf return block_limit - abs_offset; 2046677e90edSThomas Graf } 2047677e90edSThomas Graf 2048677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2049677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2050677e90edSThomas Graf 2051677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2052677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2053677e90edSThomas Graf block_limit = frag->size + st->stepped_offset; 2054677e90edSThomas Graf 2055677e90edSThomas Graf if (abs_offset < block_limit) { 2056677e90edSThomas Graf if (!st->frag_data) 2057677e90edSThomas Graf st->frag_data = kmap_skb_frag(frag); 2058677e90edSThomas Graf 2059677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2060677e90edSThomas Graf (abs_offset - st->stepped_offset); 2061677e90edSThomas Graf 2062677e90edSThomas Graf return block_limit - abs_offset; 2063677e90edSThomas Graf } 2064677e90edSThomas Graf 2065677e90edSThomas Graf if (st->frag_data) { 2066677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2067677e90edSThomas Graf st->frag_data = NULL; 2068677e90edSThomas Graf } 2069677e90edSThomas Graf 2070677e90edSThomas Graf st->frag_idx++; 2071677e90edSThomas Graf st->stepped_offset += frag->size; 2072677e90edSThomas Graf } 2073677e90edSThomas Graf 20745b5a60daSOlaf Kirch if (st->frag_data) { 20755b5a60daSOlaf Kirch kunmap_skb_frag(st->frag_data); 20765b5a60daSOlaf Kirch st->frag_data = NULL; 20775b5a60daSOlaf Kirch } 20785b5a60daSOlaf Kirch 2079677e90edSThomas Graf if (st->cur_skb->next) { 2080677e90edSThomas Graf st->cur_skb = st->cur_skb->next; 2081677e90edSThomas Graf st->frag_idx = 0; 2082677e90edSThomas Graf goto next_skb; 2083677e90edSThomas Graf } else if (st->root_skb == st->cur_skb && 2084677e90edSThomas Graf skb_shinfo(st->root_skb)->frag_list) { 2085677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2086677e90edSThomas Graf goto next_skb; 2087677e90edSThomas Graf } 2088677e90edSThomas Graf 2089677e90edSThomas Graf return 0; 2090677e90edSThomas Graf } 2091677e90edSThomas Graf 2092677e90edSThomas Graf /** 2093677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2094677e90edSThomas Graf * @st: state variable 2095677e90edSThomas Graf * 2096677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2097677e90edSThomas Graf * returned 0. 2098677e90edSThomas Graf */ 2099677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2100677e90edSThomas Graf { 2101677e90edSThomas Graf if (st->frag_data) 2102677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2103677e90edSThomas Graf } 2104677e90edSThomas Graf 21053fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 21063fc7e8a6SThomas Graf 21073fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 21083fc7e8a6SThomas Graf struct ts_config *conf, 21093fc7e8a6SThomas Graf struct ts_state *state) 21103fc7e8a6SThomas Graf { 21113fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 21123fc7e8a6SThomas Graf } 21133fc7e8a6SThomas Graf 21143fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 21153fc7e8a6SThomas Graf { 21163fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 21173fc7e8a6SThomas Graf } 21183fc7e8a6SThomas Graf 21193fc7e8a6SThomas Graf /** 21203fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 21213fc7e8a6SThomas Graf * @skb: the buffer to look in 21223fc7e8a6SThomas Graf * @from: search offset 21233fc7e8a6SThomas Graf * @to: search limit 21243fc7e8a6SThomas Graf * @config: textsearch configuration 21253fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 21263fc7e8a6SThomas Graf * 21273fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 21283fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 21293fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 21303fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 21313fc7e8a6SThomas Graf */ 21323fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 21333fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 21343fc7e8a6SThomas Graf struct ts_state *state) 21353fc7e8a6SThomas Graf { 2136f72b948dSPhil Oester unsigned int ret; 2137f72b948dSPhil Oester 21383fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 21393fc7e8a6SThomas Graf config->finish = skb_ts_finish; 21403fc7e8a6SThomas Graf 21413fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 21423fc7e8a6SThomas Graf 2143f72b948dSPhil Oester ret = textsearch_find(config, state); 2144f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 21453fc7e8a6SThomas Graf } 21463fc7e8a6SThomas Graf 2147e89e9cf5SAnanda Raju /** 2148e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 2149e89e9cf5SAnanda Raju * @sk: sock structure 2150e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2151e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2152e89e9cf5SAnanda Raju * @from: pointer to user message iov 2153e89e9cf5SAnanda Raju * @length: length of the iov message 2154e89e9cf5SAnanda Raju * 2155e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2156e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2157e89e9cf5SAnanda Raju */ 2158e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2159dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2160e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2161e89e9cf5SAnanda Raju void *from, int length) 2162e89e9cf5SAnanda Raju { 2163e89e9cf5SAnanda Raju int frg_cnt = 0; 2164e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2165e89e9cf5SAnanda Raju struct page *page = NULL; 2166e89e9cf5SAnanda Raju int copy, left; 2167e89e9cf5SAnanda Raju int offset = 0; 2168e89e9cf5SAnanda Raju int ret; 2169e89e9cf5SAnanda Raju 2170e89e9cf5SAnanda Raju do { 2171e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2172e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2173e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2174e89e9cf5SAnanda Raju return -EFAULT; 2175e89e9cf5SAnanda Raju 2176e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2177e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2178e89e9cf5SAnanda Raju 2179e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2180e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2181e89e9cf5SAnanda Raju */ 2182e89e9cf5SAnanda Raju if (page == NULL) 2183e89e9cf5SAnanda Raju return -ENOMEM; 2184e89e9cf5SAnanda Raju 2185e89e9cf5SAnanda Raju /* initialize the next frag */ 2186e89e9cf5SAnanda Raju sk->sk_sndmsg_page = page; 2187e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 2188e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2189e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2190e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2191e89e9cf5SAnanda Raju 2192e89e9cf5SAnanda Raju /* get the new initialized frag */ 2193e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2194e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2195e89e9cf5SAnanda Raju 2196e89e9cf5SAnanda Raju /* copy the user data to page */ 2197e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2198e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2199e89e9cf5SAnanda Raju 2200e89e9cf5SAnanda Raju ret = getfrag(from, (page_address(frag->page) + 2201e89e9cf5SAnanda Raju frag->page_offset + frag->size), 2202e89e9cf5SAnanda Raju offset, copy, 0, skb); 2203e89e9cf5SAnanda Raju if (ret < 0) 2204e89e9cf5SAnanda Raju return -EFAULT; 2205e89e9cf5SAnanda Raju 2206e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2207e89e9cf5SAnanda Raju sk->sk_sndmsg_off += copy; 2208e89e9cf5SAnanda Raju frag->size += copy; 2209e89e9cf5SAnanda Raju skb->len += copy; 2210e89e9cf5SAnanda Raju skb->data_len += copy; 2211e89e9cf5SAnanda Raju offset += copy; 2212e89e9cf5SAnanda Raju length -= copy; 2213e89e9cf5SAnanda Raju 2214e89e9cf5SAnanda Raju } while (length > 0); 2215e89e9cf5SAnanda Raju 2216e89e9cf5SAnanda Raju return 0; 2217e89e9cf5SAnanda Raju } 2218e89e9cf5SAnanda Raju 2219cbb042f9SHerbert Xu /** 2220cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2221cbb042f9SHerbert Xu * @skb: buffer to update 2222cbb042f9SHerbert Xu * @len: length of data pulled 2223cbb042f9SHerbert Xu * 2224cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2225fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 222684fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 222784fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 222884fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2229cbb042f9SHerbert Xu */ 2230cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2231cbb042f9SHerbert Xu { 2232cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2233cbb042f9SHerbert Xu skb->len -= len; 2234cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2235cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2236cbb042f9SHerbert Xu return skb->data += len; 2237cbb042f9SHerbert Xu } 2238cbb042f9SHerbert Xu 2239f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2240f94691acSArnaldo Carvalho de Melo 2241f4c50d99SHerbert Xu /** 2242f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2243f4c50d99SHerbert Xu * @skb: buffer to segment 2244576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2245f4c50d99SHerbert Xu * 2246f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 22474c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 22484c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2249f4c50d99SHerbert Xu */ 2250576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2251f4c50d99SHerbert Xu { 2252f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2253f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 2254f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 225598e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2256f4c50d99SHerbert Xu unsigned int offset = doffset; 2257f4c50d99SHerbert Xu unsigned int headroom; 2258f4c50d99SHerbert Xu unsigned int len; 2259576a30ebSHerbert Xu int sg = features & NETIF_F_SG; 2260f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2261f4c50d99SHerbert Xu int err = -ENOMEM; 2262f4c50d99SHerbert Xu int i = 0; 2263f4c50d99SHerbert Xu int pos; 2264f4c50d99SHerbert Xu 2265f4c50d99SHerbert Xu __skb_push(skb, doffset); 2266f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2267f4c50d99SHerbert Xu pos = skb_headlen(skb); 2268f4c50d99SHerbert Xu 2269f4c50d99SHerbert Xu do { 2270f4c50d99SHerbert Xu struct sk_buff *nskb; 2271f4c50d99SHerbert Xu skb_frag_t *frag; 2272c8884eddSHerbert Xu int hsize; 2273f4c50d99SHerbert Xu int k; 2274f4c50d99SHerbert Xu int size; 2275f4c50d99SHerbert Xu 2276f4c50d99SHerbert Xu len = skb->len - offset; 2277f4c50d99SHerbert Xu if (len > mss) 2278f4c50d99SHerbert Xu len = mss; 2279f4c50d99SHerbert Xu 2280f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2281f4c50d99SHerbert Xu if (hsize < 0) 2282f4c50d99SHerbert Xu hsize = 0; 2283c8884eddSHerbert Xu if (hsize > len || !sg) 2284c8884eddSHerbert Xu hsize = len; 2285f4c50d99SHerbert Xu 2286c8884eddSHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2287f4c50d99SHerbert Xu if (unlikely(!nskb)) 2288f4c50d99SHerbert Xu goto err; 2289f4c50d99SHerbert Xu 2290f4c50d99SHerbert Xu if (segs) 2291f4c50d99SHerbert Xu tail->next = nskb; 2292f4c50d99SHerbert Xu else 2293f4c50d99SHerbert Xu segs = nskb; 2294f4c50d99SHerbert Xu tail = nskb; 2295f4c50d99SHerbert Xu 22966f85a124SHerbert Xu __copy_skb_header(nskb, skb); 2297f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2298f4c50d99SHerbert Xu 2299f4c50d99SHerbert Xu skb_reserve(nskb, headroom); 2300459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2301ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2302b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2303b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 2304d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 2305d626f62bSArnaldo Carvalho de Melo doffset); 2306f4c50d99SHerbert Xu if (!sg) { 23076f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2308f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2309f4c50d99SHerbert Xu skb_put(nskb, len), 2310f4c50d99SHerbert Xu len, 0); 2311f4c50d99SHerbert Xu continue; 2312f4c50d99SHerbert Xu } 2313f4c50d99SHerbert Xu 2314f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2315f4c50d99SHerbert Xu k = 0; 2316f4c50d99SHerbert Xu 2317d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2318d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2319f4c50d99SHerbert Xu 2320f4c50d99SHerbert Xu while (pos < offset + len) { 2321f4c50d99SHerbert Xu BUG_ON(i >= nfrags); 2322f4c50d99SHerbert Xu 2323f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2324f4c50d99SHerbert Xu get_page(frag->page); 2325f4c50d99SHerbert Xu size = frag->size; 2326f4c50d99SHerbert Xu 2327f4c50d99SHerbert Xu if (pos < offset) { 2328f4c50d99SHerbert Xu frag->page_offset += offset - pos; 2329f4c50d99SHerbert Xu frag->size -= offset - pos; 2330f4c50d99SHerbert Xu } 2331f4c50d99SHerbert Xu 2332f4c50d99SHerbert Xu k++; 2333f4c50d99SHerbert Xu 2334f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2335f4c50d99SHerbert Xu i++; 2336f4c50d99SHerbert Xu pos += size; 2337f4c50d99SHerbert Xu } else { 2338f4c50d99SHerbert Xu frag->size -= pos + size - (offset + len); 2339f4c50d99SHerbert Xu break; 2340f4c50d99SHerbert Xu } 2341f4c50d99SHerbert Xu 2342f4c50d99SHerbert Xu frag++; 2343f4c50d99SHerbert Xu } 2344f4c50d99SHerbert Xu 2345f4c50d99SHerbert Xu skb_shinfo(nskb)->nr_frags = k; 2346f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2347f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2348f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2349f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2350f4c50d99SHerbert Xu 2351f4c50d99SHerbert Xu return segs; 2352f4c50d99SHerbert Xu 2353f4c50d99SHerbert Xu err: 2354f4c50d99SHerbert Xu while ((skb = segs)) { 2355f4c50d99SHerbert Xu segs = skb->next; 2356b08d5840SPatrick McHardy kfree_skb(skb); 2357f4c50d99SHerbert Xu } 2358f4c50d99SHerbert Xu return ERR_PTR(err); 2359f4c50d99SHerbert Xu } 2360f4c50d99SHerbert Xu 2361f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2362f4c50d99SHerbert Xu 23631da177e4SLinus Torvalds void __init skb_init(void) 23641da177e4SLinus Torvalds { 23651da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 23661da177e4SLinus Torvalds sizeof(struct sk_buff), 23671da177e4SLinus Torvalds 0, 2368e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 236920c2df83SPaul Mundt NULL); 2370d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2371d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2372d179cd12SDavid S. Miller sizeof(atomic_t), 2373d179cd12SDavid S. Miller 0, 2374e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 237520c2df83SPaul Mundt NULL); 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 2378716ea3a7SDavid Howells /** 2379716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2380716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2381716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2382716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2383716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2384716ea3a7SDavid Howells * 2385716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2386716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2387716ea3a7SDavid Howells */ 238851c739d1SDavid S. Miller static int 238951c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2390716ea3a7SDavid Howells { 23911a028e50SDavid S. Miller int start = skb_headlen(skb); 23921a028e50SDavid S. Miller int i, copy = start - offset; 2393716ea3a7SDavid Howells int elt = 0; 2394716ea3a7SDavid Howells 2395716ea3a7SDavid Howells if (copy > 0) { 2396716ea3a7SDavid Howells if (copy > len) 2397716ea3a7SDavid Howells copy = len; 2398642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 2399716ea3a7SDavid Howells elt++; 2400716ea3a7SDavid Howells if ((len -= copy) == 0) 2401716ea3a7SDavid Howells return elt; 2402716ea3a7SDavid Howells offset += copy; 2403716ea3a7SDavid Howells } 2404716ea3a7SDavid Howells 2405716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24061a028e50SDavid S. Miller int end; 2407716ea3a7SDavid Howells 2408547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24091a028e50SDavid S. Miller 24101a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 2411716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2412716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2413716ea3a7SDavid Howells 2414716ea3a7SDavid Howells if (copy > len) 2415716ea3a7SDavid Howells copy = len; 2416642f1490SJens Axboe sg_set_page(&sg[elt], frag->page, copy, 2417642f1490SJens Axboe frag->page_offset+offset-start); 2418716ea3a7SDavid Howells elt++; 2419716ea3a7SDavid Howells if (!(len -= copy)) 2420716ea3a7SDavid Howells return elt; 2421716ea3a7SDavid Howells offset += copy; 2422716ea3a7SDavid Howells } 24231a028e50SDavid S. Miller start = end; 2424716ea3a7SDavid Howells } 2425716ea3a7SDavid Howells 2426716ea3a7SDavid Howells if (skb_shinfo(skb)->frag_list) { 2427716ea3a7SDavid Howells struct sk_buff *list = skb_shinfo(skb)->frag_list; 2428716ea3a7SDavid Howells 2429716ea3a7SDavid Howells for (; list; list = list->next) { 24301a028e50SDavid S. Miller int end; 2431716ea3a7SDavid Howells 2432547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24331a028e50SDavid S. Miller 24341a028e50SDavid S. Miller end = start + list->len; 2435716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2436716ea3a7SDavid Howells if (copy > len) 2437716ea3a7SDavid Howells copy = len; 243851c739d1SDavid S. Miller elt += __skb_to_sgvec(list, sg+elt, offset - start, 243951c739d1SDavid S. Miller copy); 2440716ea3a7SDavid Howells if ((len -= copy) == 0) 2441716ea3a7SDavid Howells return elt; 2442716ea3a7SDavid Howells offset += copy; 2443716ea3a7SDavid Howells } 24441a028e50SDavid S. Miller start = end; 2445716ea3a7SDavid Howells } 2446716ea3a7SDavid Howells } 2447716ea3a7SDavid Howells BUG_ON(len); 2448716ea3a7SDavid Howells return elt; 2449716ea3a7SDavid Howells } 2450716ea3a7SDavid Howells 245151c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 245251c739d1SDavid S. Miller { 245351c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 245451c739d1SDavid S. Miller 2455c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 245651c739d1SDavid S. Miller 245751c739d1SDavid S. Miller return nsg; 245851c739d1SDavid S. Miller } 245951c739d1SDavid S. Miller 2460716ea3a7SDavid Howells /** 2461716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 2462716ea3a7SDavid Howells * @skb: The socket buffer to check. 2463716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 2464716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 2465716ea3a7SDavid Howells * 2466716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 2467716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 2468716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 2469716ea3a7SDavid Howells * 2470716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 2471716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 2472716ea3a7SDavid Howells * set to point to the skb in which this space begins. 2473716ea3a7SDavid Howells * 2474716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 2475716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 2476716ea3a7SDavid Howells */ 2477716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2478716ea3a7SDavid Howells { 2479716ea3a7SDavid Howells int copyflag; 2480716ea3a7SDavid Howells int elt; 2481716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 2482716ea3a7SDavid Howells 2483716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 2484716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 2485716ea3a7SDavid Howells * at the moment even if they are anonymous). 2486716ea3a7SDavid Howells */ 2487716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2488716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2489716ea3a7SDavid Howells return -ENOMEM; 2490716ea3a7SDavid Howells 2491716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 2492716ea3a7SDavid Howells if (!skb_shinfo(skb)->frag_list) { 2493716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 2494716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 2495716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 2496716ea3a7SDavid Howells * space, 128 bytes is fair. */ 2497716ea3a7SDavid Howells 2498716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 2499716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2500716ea3a7SDavid Howells return -ENOMEM; 2501716ea3a7SDavid Howells 2502716ea3a7SDavid Howells /* Voila! */ 2503716ea3a7SDavid Howells *trailer = skb; 2504716ea3a7SDavid Howells return 1; 2505716ea3a7SDavid Howells } 2506716ea3a7SDavid Howells 2507716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 2508716ea3a7SDavid Howells 2509716ea3a7SDavid Howells elt = 1; 2510716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 2511716ea3a7SDavid Howells copyflag = 0; 2512716ea3a7SDavid Howells 2513716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 2514716ea3a7SDavid Howells int ntail = 0; 2515716ea3a7SDavid Howells 2516716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 2517716ea3a7SDavid Howells * this can happen on input. Copy it and everything 2518716ea3a7SDavid Howells * after it. */ 2519716ea3a7SDavid Howells 2520716ea3a7SDavid Howells if (skb_shared(skb1)) 2521716ea3a7SDavid Howells copyflag = 1; 2522716ea3a7SDavid Howells 2523716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 2524716ea3a7SDavid Howells 2525716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 2526716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 2527716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list || 2528716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 2529716ea3a7SDavid Howells ntail = tailbits + 128; 2530716ea3a7SDavid Howells } 2531716ea3a7SDavid Howells 2532716ea3a7SDavid Howells if (copyflag || 2533716ea3a7SDavid Howells skb_cloned(skb1) || 2534716ea3a7SDavid Howells ntail || 2535716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 2536716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list) { 2537716ea3a7SDavid Howells struct sk_buff *skb2; 2538716ea3a7SDavid Howells 2539716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 2540716ea3a7SDavid Howells if (ntail == 0) 2541716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 2542716ea3a7SDavid Howells else 2543716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 2544716ea3a7SDavid Howells skb_headroom(skb1), 2545716ea3a7SDavid Howells ntail, 2546716ea3a7SDavid Howells GFP_ATOMIC); 2547716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 2548716ea3a7SDavid Howells return -ENOMEM; 2549716ea3a7SDavid Howells 2550716ea3a7SDavid Howells if (skb1->sk) 2551716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 2552716ea3a7SDavid Howells 2553716ea3a7SDavid Howells /* Looking around. Are we still alive? 2554716ea3a7SDavid Howells * OK, link new skb, drop old one */ 2555716ea3a7SDavid Howells 2556716ea3a7SDavid Howells skb2->next = skb1->next; 2557716ea3a7SDavid Howells *skb_p = skb2; 2558716ea3a7SDavid Howells kfree_skb(skb1); 2559716ea3a7SDavid Howells skb1 = skb2; 2560716ea3a7SDavid Howells } 2561716ea3a7SDavid Howells elt++; 2562716ea3a7SDavid Howells *trailer = skb1; 2563716ea3a7SDavid Howells skb_p = &skb1->next; 2564716ea3a7SDavid Howells } 2565716ea3a7SDavid Howells 2566716ea3a7SDavid Howells return elt; 2567716ea3a7SDavid Howells } 2568716ea3a7SDavid Howells 2569f35d9d8aSRusty Russell /** 2570f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 2571f35d9d8aSRusty Russell * @skb: the skb to set 2572f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 2573f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 2574f35d9d8aSRusty Russell * 2575f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 2576f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 2577f35d9d8aSRusty Russell * 2578f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 2579f35d9d8aSRusty Russell * returns false you should drop the packet. 2580f35d9d8aSRusty Russell */ 2581f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 2582f35d9d8aSRusty Russell { 2583f35d9d8aSRusty Russell if (unlikely(start > skb->len - 2) || 2584f35d9d8aSRusty Russell unlikely((int)start + off > skb->len - 2)) { 2585f35d9d8aSRusty Russell if (net_ratelimit()) 2586f35d9d8aSRusty Russell printk(KERN_WARNING 2587f35d9d8aSRusty Russell "bad partial csum: csum=%u/%u len=%u\n", 2588f35d9d8aSRusty Russell start, off, skb->len); 2589f35d9d8aSRusty Russell return false; 2590f35d9d8aSRusty Russell } 2591f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 2592f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 2593f35d9d8aSRusty Russell skb->csum_offset = off; 2594f35d9d8aSRusty Russell return true; 2595f35d9d8aSRusty Russell } 2596f35d9d8aSRusty Russell 25974497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 25984497b076SBen Hutchings { 25994497b076SBen Hutchings if (net_ratelimit()) 26004497b076SBen Hutchings pr_warning("%s: received packets cannot be forwarded" 26014497b076SBen Hutchings " while LRO is enabled\n", skb->dev->name); 26024497b076SBen Hutchings } 26034497b076SBen Hutchings 26041da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim); 26051da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb); 2606231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb); 26071da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail); 2608d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 26098af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb); 26101da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy); 26111da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head); 26121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum); 26131da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone); 26141da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy); 26151da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits); 26161da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev); 26171da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits); 26181da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand); 26191da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic); 26201da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad); 26211da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom); 26221da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic); 26231da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue); 26241da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail); 26251da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert); 26261da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge); 26271da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head); 26281da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail); 26291da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink); 26301da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append); 26311da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split); 2632677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read); 2633677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read); 2634677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read); 26353fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text); 2636e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags); 26374497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 2638716ea3a7SDavid Howells 2639716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec); 2640716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data); 2641f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set); 2642