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 3662d4baff8SHerbert Xu /* Free everything but the sk_buff shell. */ 3672d4baff8SHerbert Xu static void skb_release_all(struct sk_buff *skb) 3681da177e4SLinus Torvalds { 3691da177e4SLinus Torvalds dst_release(skb->dst); 3701da177e4SLinus Torvalds #ifdef CONFIG_XFRM 3711da177e4SLinus Torvalds secpath_put(skb->sp); 3721da177e4SLinus Torvalds #endif 3731da177e4SLinus Torvalds if (skb->destructor) { 3749c2b3328SStephen Hemminger WARN_ON(in_irq()); 3751da177e4SLinus Torvalds skb->destructor(skb); 3761da177e4SLinus Torvalds } 3779fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 3785f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 3799fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 3809fb9cbb1SYasuyuki Kozakai #endif 3811da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 3821da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 3831da177e4SLinus Torvalds #endif 3841da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 3851da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 3861da177e4SLinus Torvalds skb->tc_index = 0; 3871da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 3881da177e4SLinus Torvalds skb->tc_verd = 0; 3891da177e4SLinus Torvalds #endif 3901da177e4SLinus Torvalds #endif 3912d4baff8SHerbert Xu skb_release_data(skb); 3922d4baff8SHerbert Xu } 3931da177e4SLinus Torvalds 3942d4baff8SHerbert Xu /** 3952d4baff8SHerbert Xu * __kfree_skb - private function 3962d4baff8SHerbert Xu * @skb: buffer 3972d4baff8SHerbert Xu * 3982d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 3992d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 4002d4baff8SHerbert Xu * always call kfree_skb 4012d4baff8SHerbert Xu */ 4022d4baff8SHerbert Xu 4032d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 4042d4baff8SHerbert Xu { 4052d4baff8SHerbert Xu skb_release_all(skb); 4061da177e4SLinus Torvalds kfree_skbmem(skb); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /** 410231d06aeSJörn Engel * kfree_skb - free an sk_buff 411231d06aeSJörn Engel * @skb: buffer to free 412231d06aeSJörn Engel * 413231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 414231d06aeSJörn Engel * hit zero. 415231d06aeSJörn Engel */ 416231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 417231d06aeSJörn Engel { 418231d06aeSJörn Engel if (unlikely(!skb)) 419231d06aeSJörn Engel return; 420231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 421231d06aeSJörn Engel smp_rmb(); 422231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 423231d06aeSJörn Engel return; 424231d06aeSJörn Engel __kfree_skb(skb); 425231d06aeSJörn Engel } 426231d06aeSJörn Engel 427dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 428dec18810SHerbert Xu { 429dec18810SHerbert Xu new->tstamp = old->tstamp; 430dec18810SHerbert Xu new->dev = old->dev; 431dec18810SHerbert Xu new->transport_header = old->transport_header; 432dec18810SHerbert Xu new->network_header = old->network_header; 433dec18810SHerbert Xu new->mac_header = old->mac_header; 434dec18810SHerbert Xu new->dst = dst_clone(old->dst); 435dec18810SHerbert Xu #ifdef CONFIG_INET 436dec18810SHerbert Xu new->sp = secpath_get(old->sp); 437dec18810SHerbert Xu #endif 438dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 439dec18810SHerbert Xu new->csum_start = old->csum_start; 440dec18810SHerbert Xu new->csum_offset = old->csum_offset; 441dec18810SHerbert Xu new->local_df = old->local_df; 442dec18810SHerbert Xu new->pkt_type = old->pkt_type; 443dec18810SHerbert Xu new->ip_summed = old->ip_summed; 444dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 445dec18810SHerbert Xu new->priority = old->priority; 446dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 447dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 448dec18810SHerbert Xu #endif 449dec18810SHerbert Xu new->protocol = old->protocol; 450dec18810SHerbert Xu new->mark = old->mark; 451dec18810SHerbert Xu __nf_copy(new, old); 452dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 453dec18810SHerbert Xu defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 454dec18810SHerbert Xu new->nf_trace = old->nf_trace; 455dec18810SHerbert Xu #endif 456dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 457dec18810SHerbert Xu new->tc_index = old->tc_index; 458dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 459dec18810SHerbert Xu new->tc_verd = old->tc_verd; 460dec18810SHerbert Xu #endif 461dec18810SHerbert Xu #endif 4626aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 4636aa895b0SPatrick McHardy 464dec18810SHerbert Xu skb_copy_secmark(new, old); 465dec18810SHerbert Xu } 466dec18810SHerbert Xu 467e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds #define C(x) n->x = skb->x 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds n->next = n->prev = NULL; 4721da177e4SLinus Torvalds n->sk = NULL; 473dec18810SHerbert Xu __copy_skb_header(n, skb); 474dec18810SHerbert Xu 4751da177e4SLinus Torvalds C(len); 4761da177e4SLinus Torvalds C(data_len); 4773e6b3b2eSAlexey Dobriyan C(mac_len); 478334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 47902f1c89dSPaul Moore n->cloned = 1; 4801da177e4SLinus Torvalds n->nohdr = 0; 4811da177e4SLinus Torvalds n->destructor = NULL; 48202f1c89dSPaul Moore C(iif); 4831da177e4SLinus Torvalds C(tail); 4841da177e4SLinus Torvalds C(end); 48502f1c89dSPaul Moore C(head); 48602f1c89dSPaul Moore C(data); 48702f1c89dSPaul Moore C(truesize); 488d0f09804SJohannes Berg #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE) 489d0f09804SJohannes Berg C(do_not_encrypt); 490d0f09804SJohannes Berg #endif 49102f1c89dSPaul Moore atomic_set(&n->users, 1); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 4941da177e4SLinus Torvalds skb->cloned = 1; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds return n; 497e0053ec0SHerbert Xu #undef C 498e0053ec0SHerbert Xu } 499e0053ec0SHerbert Xu 500e0053ec0SHerbert Xu /** 501e0053ec0SHerbert Xu * skb_morph - morph one skb into another 502e0053ec0SHerbert Xu * @dst: the skb to receive the contents 503e0053ec0SHerbert Xu * @src: the skb to supply the contents 504e0053ec0SHerbert Xu * 505e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 506e0053ec0SHerbert Xu * supplied by the user. 507e0053ec0SHerbert Xu * 508e0053ec0SHerbert Xu * The target skb is returned upon exit. 509e0053ec0SHerbert Xu */ 510e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 511e0053ec0SHerbert Xu { 5122d4baff8SHerbert Xu skb_release_all(dst); 513e0053ec0SHerbert Xu return __skb_clone(dst, src); 514e0053ec0SHerbert Xu } 515e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 516e0053ec0SHerbert Xu 517e0053ec0SHerbert Xu /** 518e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 519e0053ec0SHerbert Xu * @skb: buffer to clone 520e0053ec0SHerbert Xu * @gfp_mask: allocation priority 521e0053ec0SHerbert Xu * 522e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 523e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 524e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 525e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 526e0053ec0SHerbert Xu * 527e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 528e0053ec0SHerbert Xu * %GFP_ATOMIC. 529e0053ec0SHerbert Xu */ 530e0053ec0SHerbert Xu 531e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 532e0053ec0SHerbert Xu { 533e0053ec0SHerbert Xu struct sk_buff *n; 534e0053ec0SHerbert Xu 535e0053ec0SHerbert Xu n = skb + 1; 536e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 537e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 538e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 539e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 540e0053ec0SHerbert Xu atomic_inc(fclone_ref); 541e0053ec0SHerbert Xu } else { 542e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 543e0053ec0SHerbert Xu if (!n) 544e0053ec0SHerbert Xu return NULL; 545e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 546e0053ec0SHerbert Xu } 547e0053ec0SHerbert Xu 548e0053ec0SHerbert Xu return __skb_clone(n, skb); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 5521da177e4SLinus Torvalds { 5532e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 5541da177e4SLinus Torvalds /* 5551da177e4SLinus Torvalds * Shift between the two data areas in bytes 5561da177e4SLinus Torvalds */ 5571da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 5582e07fa9cSArnaldo Carvalho de Melo #endif 559dec18810SHerbert Xu 560dec18810SHerbert Xu __copy_skb_header(new, old); 561dec18810SHerbert Xu 5622e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 5632e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 5642e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 5652e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 5662e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 5672e07fa9cSArnaldo Carvalho de Melo #endif 5687967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 5697967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 5707967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /** 5741da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 5751da177e4SLinus Torvalds * @skb: buffer to copy 5761da177e4SLinus Torvalds * @gfp_mask: allocation priority 5771da177e4SLinus Torvalds * 5781da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 5791da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 5801da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 5811da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 5821da177e4SLinus Torvalds * 5831da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 5841da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 5851da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 5861da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 5871da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 5881da177e4SLinus Torvalds */ 5891da177e4SLinus Torvalds 590dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds int headerlen = skb->data - skb->head; 5931da177e4SLinus Torvalds /* 5941da177e4SLinus Torvalds * Allocate the copy buffer 5951da177e4SLinus Torvalds */ 5964305b541SArnaldo Carvalho de Melo struct sk_buff *n; 5974305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 5984305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end + skb->data_len, gfp_mask); 5994305b541SArnaldo Carvalho de Melo #else 6004305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 6014305b541SArnaldo Carvalho de Melo #endif 6021da177e4SLinus Torvalds if (!n) 6031da177e4SLinus Torvalds return NULL; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* Set the data pointer */ 6061da177e4SLinus Torvalds skb_reserve(n, headerlen); 6071da177e4SLinus Torvalds /* Set the tail pointer and length */ 6081da177e4SLinus Torvalds skb_put(n, skb->len); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 6111da177e4SLinus Torvalds BUG(); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds copy_skb_header(n, skb); 6141da177e4SLinus Torvalds return n; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /** 6191da177e4SLinus Torvalds * pskb_copy - create copy of an sk_buff with private head. 6201da177e4SLinus Torvalds * @skb: buffer to copy 6211da177e4SLinus Torvalds * @gfp_mask: allocation priority 6221da177e4SLinus Torvalds * 6231da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 6241da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 6251da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 6261da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 6271da177e4SLinus Torvalds * or the pointer to the buffer on success. 6281da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 6291da177e4SLinus Torvalds */ 6301da177e4SLinus Torvalds 631dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 6321da177e4SLinus Torvalds { 6331da177e4SLinus Torvalds /* 6341da177e4SLinus Torvalds * Allocate the copy buffer 6351da177e4SLinus Torvalds */ 6364305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6374305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6384305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end, gfp_mask); 6394305b541SArnaldo Carvalho de Melo #else 6404305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head, gfp_mask); 6414305b541SArnaldo Carvalho de Melo #endif 6421da177e4SLinus Torvalds if (!n) 6431da177e4SLinus Torvalds goto out; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* Set the data pointer */ 6461da177e4SLinus Torvalds skb_reserve(n, skb->data - skb->head); 6471da177e4SLinus Torvalds /* Set the tail pointer and length */ 6481da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 6491da177e4SLinus Torvalds /* Copy the bytes */ 650d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 6511da177e4SLinus Torvalds 65225f484a6SHerbert Xu n->truesize += skb->data_len; 6531da177e4SLinus Torvalds n->data_len = skb->data_len; 6541da177e4SLinus Torvalds n->len = skb->len; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 6571da177e4SLinus Torvalds int i; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 6601da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 6611da177e4SLinus Torvalds get_page(skb_shinfo(n)->frags[i].page); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 6671da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 6681da177e4SLinus Torvalds skb_clone_fraglist(n); 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds copy_skb_header(n, skb); 6721da177e4SLinus Torvalds out: 6731da177e4SLinus Torvalds return n; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /** 6771da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 6781da177e4SLinus Torvalds * @skb: buffer to reallocate 6791da177e4SLinus Torvalds * @nhead: room to add at head 6801da177e4SLinus Torvalds * @ntail: room to add at tail 6811da177e4SLinus Torvalds * @gfp_mask: allocation priority 6821da177e4SLinus Torvalds * 6831da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 6841da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 6851da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 6861da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 6871da177e4SLinus Torvalds * 6881da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 6891da177e4SLinus Torvalds * reloaded after call to this function. 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds 69286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 693dd0fc66fSAl Viro gfp_t gfp_mask) 6941da177e4SLinus Torvalds { 6951da177e4SLinus Torvalds int i; 6961da177e4SLinus Torvalds u8 *data; 6974305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6984305b541SArnaldo Carvalho de Melo int size = nhead + skb->end + ntail; 6994305b541SArnaldo Carvalho de Melo #else 7001da177e4SLinus Torvalds int size = nhead + (skb->end - skb->head) + ntail; 7014305b541SArnaldo Carvalho de Melo #endif 7021da177e4SLinus Torvalds long off; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds if (skb_shared(skb)) 7051da177e4SLinus Torvalds BUG(); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 7101da177e4SLinus Torvalds if (!data) 7111da177e4SLinus Torvalds goto nodata; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 7141da177e4SLinus Torvalds * optimized for the cases when header is void. */ 7154305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 716b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail); 7174305b541SArnaldo Carvalho de Melo #else 718b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail - skb->head); 71927a884dcSArnaldo Carvalho de Melo #endif 7204305b541SArnaldo Carvalho de Melo memcpy(data + size, skb_end_pointer(skb), 7214305b541SArnaldo Carvalho de Melo sizeof(struct skb_shared_info)); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 7241da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 7271da177e4SLinus Torvalds skb_clone_fraglist(skb); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds skb_release_data(skb); 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds off = (data + nhead) - skb->head; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds skb->head = data; 7341da177e4SLinus Torvalds skb->data += off; 7354305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7364305b541SArnaldo Carvalho de Melo skb->end = size; 73756eb8882SPatrick McHardy off = nhead; 7384305b541SArnaldo Carvalho de Melo #else 7394305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 74056eb8882SPatrick McHardy #endif 74127a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 74227a884dcSArnaldo Carvalho de Melo skb->tail += off; 743b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 744b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 745b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 746172a863fSHerbert Xu skb->csum_start += nhead; 7471da177e4SLinus Torvalds skb->cloned = 0; 748334a8132SPatrick McHardy skb->hdr_len = 0; 7491da177e4SLinus Torvalds skb->nohdr = 0; 7501da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 7511da177e4SLinus Torvalds return 0; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds nodata: 7541da177e4SLinus Torvalds return -ENOMEM; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct sk_buff *skb2; 7621da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds if (delta <= 0) 7651da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 7661da177e4SLinus Torvalds else { 7671da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 7681da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 7691da177e4SLinus Torvalds GFP_ATOMIC)) { 7701da177e4SLinus Torvalds kfree_skb(skb2); 7711da177e4SLinus Torvalds skb2 = NULL; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds return skb2; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /** 7791da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 7801da177e4SLinus Torvalds * @skb: buffer to copy 7811da177e4SLinus Torvalds * @newheadroom: new free bytes at head 7821da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 7831da177e4SLinus Torvalds * @gfp_mask: allocation priority 7841da177e4SLinus Torvalds * 7851da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 7861da177e4SLinus Torvalds * allocate additional space. 7871da177e4SLinus Torvalds * 7881da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 7891da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 7901da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 7911da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 7921da177e4SLinus Torvalds * 7931da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 7941da177e4SLinus Torvalds * is called from an interrupt. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 79786a76cafSVictor Fusco int newheadroom, int newtailroom, 798dd0fc66fSAl Viro gfp_t gfp_mask) 7991da177e4SLinus Torvalds { 8001da177e4SLinus Torvalds /* 8011da177e4SLinus Torvalds * Allocate the copy buffer 8021da177e4SLinus Torvalds */ 8031da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 8041da177e4SLinus Torvalds gfp_mask); 805efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 8061da177e4SLinus Torvalds int head_copy_len, head_copy_off; 80752886051SHerbert Xu int off; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds if (!n) 8101da177e4SLinus Torvalds return NULL; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds skb_reserve(n, newheadroom); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds /* Set the tail pointer and length */ 8151da177e4SLinus Torvalds skb_put(n, skb->len); 8161da177e4SLinus Torvalds 817efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 8181da177e4SLinus Torvalds head_copy_off = 0; 8191da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 8201da177e4SLinus Torvalds head_copy_len = newheadroom; 8211da177e4SLinus Torvalds else 8221da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* Copy the linear header and data. */ 8251da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 8261da177e4SLinus Torvalds skb->len + head_copy_len)) 8271da177e4SLinus Torvalds BUG(); 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds copy_skb_header(n, skb); 8301da177e4SLinus Torvalds 831efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 83252886051SHerbert Xu n->csum_start += off; 83352886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 834efd1e8d5SPatrick McHardy n->transport_header += off; 835efd1e8d5SPatrick McHardy n->network_header += off; 836efd1e8d5SPatrick McHardy n->mac_header += off; 83752886051SHerbert Xu #endif 838efd1e8d5SPatrick McHardy 8391da177e4SLinus Torvalds return n; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds /** 8431da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 8441da177e4SLinus Torvalds * @skb: buffer to pad 8451da177e4SLinus Torvalds * @pad: space to pad 8461da177e4SLinus Torvalds * 8471da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 8481da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 8491da177e4SLinus Torvalds * beyond the buffer end onto the wire. 8501da177e4SLinus Torvalds * 8515b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 8521da177e4SLinus Torvalds */ 8531da177e4SLinus Torvalds 8545b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 8551da177e4SLinus Torvalds { 8565b057c6bSHerbert Xu int err; 8575b057c6bSHerbert Xu int ntail; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 8605b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 8611da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 8625b057c6bSHerbert Xu return 0; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8654305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 8665b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 8675b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 8685b057c6bSHerbert Xu if (unlikely(err)) 8695b057c6bSHerbert Xu goto free_skb; 8705b057c6bSHerbert Xu } 8715b057c6bSHerbert Xu 8725b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 8735b057c6bSHerbert Xu * to be audited. 8745b057c6bSHerbert Xu */ 8755b057c6bSHerbert Xu err = skb_linearize(skb); 8765b057c6bSHerbert Xu if (unlikely(err)) 8775b057c6bSHerbert Xu goto free_skb; 8785b057c6bSHerbert Xu 8795b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 8805b057c6bSHerbert Xu return 0; 8815b057c6bSHerbert Xu 8825b057c6bSHerbert Xu free_skb: 8831da177e4SLinus Torvalds kfree_skb(skb); 8845b057c6bSHerbert Xu return err; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds 8870dde3e16SIlpo Järvinen /** 8880dde3e16SIlpo Järvinen * skb_put - add data to a buffer 8890dde3e16SIlpo Järvinen * @skb: buffer to use 8900dde3e16SIlpo Järvinen * @len: amount of data to add 8910dde3e16SIlpo Järvinen * 8920dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 8930dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 8940dde3e16SIlpo Järvinen * first byte of the extra data is returned. 8950dde3e16SIlpo Järvinen */ 8960dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 8970dde3e16SIlpo Järvinen { 8980dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 8990dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 9000dde3e16SIlpo Järvinen skb->tail += len; 9010dde3e16SIlpo Järvinen skb->len += len; 9020dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 9030dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 9040dde3e16SIlpo Järvinen return tmp; 9050dde3e16SIlpo Järvinen } 9060dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 9070dde3e16SIlpo Järvinen 9086be8ac2fSIlpo Järvinen /** 909c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 910c2aa270aSIlpo Järvinen * @skb: buffer to use 911c2aa270aSIlpo Järvinen * @len: amount of data to add 912c2aa270aSIlpo Järvinen * 913c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 914c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 915c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 916c2aa270aSIlpo Järvinen */ 917c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 918c2aa270aSIlpo Järvinen { 919c2aa270aSIlpo Järvinen skb->data -= len; 920c2aa270aSIlpo Järvinen skb->len += len; 921c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 922c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 923c2aa270aSIlpo Järvinen return skb->data; 924c2aa270aSIlpo Järvinen } 925c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 926c2aa270aSIlpo Järvinen 927c2aa270aSIlpo Järvinen /** 9286be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 9296be8ac2fSIlpo Järvinen * @skb: buffer to use 9306be8ac2fSIlpo Järvinen * @len: amount of data to remove 9316be8ac2fSIlpo Järvinen * 9326be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 9336be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 9346be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 9356be8ac2fSIlpo Järvinen * the old data. 9366be8ac2fSIlpo Järvinen */ 9376be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 9386be8ac2fSIlpo Järvinen { 9396be8ac2fSIlpo Järvinen return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 9406be8ac2fSIlpo Järvinen } 9416be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 9426be8ac2fSIlpo Järvinen 943419ae74eSIlpo Järvinen /** 944419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 945419ae74eSIlpo Järvinen * @skb: buffer to alter 946419ae74eSIlpo Järvinen * @len: new length 947419ae74eSIlpo Järvinen * 948419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 949419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 950419ae74eSIlpo Järvinen * The skb must be linear. 951419ae74eSIlpo Järvinen */ 952419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 953419ae74eSIlpo Järvinen { 954419ae74eSIlpo Järvinen if (skb->len > len) 955419ae74eSIlpo Järvinen __skb_trim(skb, len); 956419ae74eSIlpo Järvinen } 957419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 958419ae74eSIlpo Järvinen 9593cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 9601da177e4SLinus Torvalds */ 9611da177e4SLinus Torvalds 9623cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 9631da177e4SLinus Torvalds { 96427b437c8SHerbert Xu struct sk_buff **fragp; 96527b437c8SHerbert Xu struct sk_buff *frag; 9661da177e4SLinus Torvalds int offset = skb_headlen(skb); 9671da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 9681da177e4SLinus Torvalds int i; 96927b437c8SHerbert Xu int err; 97027b437c8SHerbert Xu 97127b437c8SHerbert Xu if (skb_cloned(skb) && 97227b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 97327b437c8SHerbert Xu return err; 9741da177e4SLinus Torvalds 975f4d26fb3SHerbert Xu i = 0; 976f4d26fb3SHerbert Xu if (offset >= len) 977f4d26fb3SHerbert Xu goto drop_pages; 978f4d26fb3SHerbert Xu 979f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 9801da177e4SLinus Torvalds int end = offset + skb_shinfo(skb)->frags[i].size; 98127b437c8SHerbert Xu 98227b437c8SHerbert Xu if (end < len) { 9831da177e4SLinus Torvalds offset = end; 98427b437c8SHerbert Xu continue; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 98727b437c8SHerbert Xu skb_shinfo(skb)->frags[i++].size = len - offset; 98827b437c8SHerbert Xu 989f4d26fb3SHerbert Xu drop_pages: 99027b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 99127b437c8SHerbert Xu 99227b437c8SHerbert Xu for (; i < nfrags; i++) 99327b437c8SHerbert Xu put_page(skb_shinfo(skb)->frags[i].page); 99427b437c8SHerbert Xu 99527b437c8SHerbert Xu if (skb_shinfo(skb)->frag_list) 99627b437c8SHerbert Xu skb_drop_fraglist(skb); 997f4d26fb3SHerbert Xu goto done; 99827b437c8SHerbert Xu } 99927b437c8SHerbert Xu 100027b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 100127b437c8SHerbert Xu fragp = &frag->next) { 100227b437c8SHerbert Xu int end = offset + frag->len; 100327b437c8SHerbert Xu 100427b437c8SHerbert Xu if (skb_shared(frag)) { 100527b437c8SHerbert Xu struct sk_buff *nfrag; 100627b437c8SHerbert Xu 100727b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 100827b437c8SHerbert Xu if (unlikely(!nfrag)) 100927b437c8SHerbert Xu return -ENOMEM; 101027b437c8SHerbert Xu 101127b437c8SHerbert Xu nfrag->next = frag->next; 1012f4d26fb3SHerbert Xu kfree_skb(frag); 101327b437c8SHerbert Xu frag = nfrag; 101427b437c8SHerbert Xu *fragp = frag; 101527b437c8SHerbert Xu } 101627b437c8SHerbert Xu 101727b437c8SHerbert Xu if (end < len) { 101827b437c8SHerbert Xu offset = end; 101927b437c8SHerbert Xu continue; 102027b437c8SHerbert Xu } 102127b437c8SHerbert Xu 102227b437c8SHerbert Xu if (end > len && 102327b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 102427b437c8SHerbert Xu return err; 102527b437c8SHerbert Xu 102627b437c8SHerbert Xu if (frag->next) 102727b437c8SHerbert Xu skb_drop_list(&frag->next); 102827b437c8SHerbert Xu break; 102927b437c8SHerbert Xu } 103027b437c8SHerbert Xu 1031f4d26fb3SHerbert Xu done: 103227b437c8SHerbert Xu if (len > skb_headlen(skb)) { 10331da177e4SLinus Torvalds skb->data_len -= skb->len - len; 10341da177e4SLinus Torvalds skb->len = len; 10351da177e4SLinus Torvalds } else { 10361da177e4SLinus Torvalds skb->len = len; 10371da177e4SLinus Torvalds skb->data_len = 0; 103827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds return 0; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /** 10451da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 10461da177e4SLinus Torvalds * @skb: buffer to reallocate 10471da177e4SLinus Torvalds * @delta: number of bytes to advance tail 10481da177e4SLinus Torvalds * 10491da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 10501da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 10511da177e4SLinus Torvalds * data from fragmented part. 10521da177e4SLinus Torvalds * 10531da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 10541da177e4SLinus Torvalds * 10551da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 10561da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 10571da177e4SLinus Torvalds * 10581da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 10591da177e4SLinus Torvalds * reloaded after call to this function. 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 10631da177e4SLinus Torvalds * when it is necessary. 10641da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 10651da177e4SLinus Torvalds * 2. It may change skb pointers. 10661da177e4SLinus Torvalds * 10671da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 10681da177e4SLinus Torvalds */ 10691da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 10721da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 10731da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 10741da177e4SLinus Torvalds */ 10754305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 10781da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 10791da177e4SLinus Torvalds GFP_ATOMIC)) 10801da177e4SLinus Torvalds return NULL; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 108327a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 10841da177e4SLinus Torvalds BUG(); 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 10871da177e4SLinus Torvalds * size of pulled pages. Superb. 10881da177e4SLinus Torvalds */ 10891da177e4SLinus Torvalds if (!skb_shinfo(skb)->frag_list) 10901da177e4SLinus Torvalds goto pull_pages; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 10931da177e4SLinus Torvalds eat = delta; 10941da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10951da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size >= eat) 10961da177e4SLinus Torvalds goto pull_pages; 10971da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 11011da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 11021da177e4SLinus Torvalds * but taking into account that pulling is expected to 11031da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 11041da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 11051da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 11061da177e4SLinus Torvalds */ 11071da177e4SLinus Torvalds if (eat) { 11081da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 11091da177e4SLinus Torvalds struct sk_buff *clone = NULL; 11101da177e4SLinus Torvalds struct sk_buff *insp = NULL; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds do { 111309a62660SKris Katterjohn BUG_ON(!list); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds if (list->len <= eat) { 11161da177e4SLinus Torvalds /* Eaten as whole. */ 11171da177e4SLinus Torvalds eat -= list->len; 11181da177e4SLinus Torvalds list = list->next; 11191da177e4SLinus Torvalds insp = list; 11201da177e4SLinus Torvalds } else { 11211da177e4SLinus Torvalds /* Eaten partially. */ 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds if (skb_shared(list)) { 11241da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 11251da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 11261da177e4SLinus Torvalds if (!clone) 11271da177e4SLinus Torvalds return NULL; 11281da177e4SLinus Torvalds insp = list->next; 11291da177e4SLinus Torvalds list = clone; 11301da177e4SLinus Torvalds } else { 11311da177e4SLinus Torvalds /* This may be pulled without 11321da177e4SLinus Torvalds * problems. */ 11331da177e4SLinus Torvalds insp = list; 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 11361da177e4SLinus Torvalds if (clone) 11371da177e4SLinus Torvalds kfree_skb(clone); 11381da177e4SLinus Torvalds return NULL; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds break; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds } while (eat); 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds /* Free pulled out fragments. */ 11451da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 11461da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 11471da177e4SLinus Torvalds kfree_skb(list); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds /* And insert new clone at head. */ 11501da177e4SLinus Torvalds if (clone) { 11511da177e4SLinus Torvalds clone->next = list; 11521da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds pull_pages: 11581da177e4SLinus Torvalds eat = delta; 11591da177e4SLinus Torvalds k = 0; 11601da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11611da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size <= eat) { 11621da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 11631da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 11641da177e4SLinus Torvalds } else { 11651da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 11661da177e4SLinus Torvalds if (eat) { 11671da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 11681da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].size -= eat; 11691da177e4SLinus Torvalds eat = 0; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds k++; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds skb->tail += delta; 11771da177e4SLinus Torvalds skb->data_len -= delta; 11781da177e4SLinus Torvalds 117927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */ 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 11851da177e4SLinus Torvalds { 11861da177e4SLinus Torvalds int i, copy; 11871a028e50SDavid S. Miller int start = skb_headlen(skb); 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds if (offset > (int)skb->len - len) 11901da177e4SLinus Torvalds goto fault; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds /* Copy header. */ 11931a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 11941da177e4SLinus Torvalds if (copy > len) 11951da177e4SLinus Torvalds copy = len; 1196d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 11971da177e4SLinus Torvalds if ((len -= copy) == 0) 11981da177e4SLinus Torvalds return 0; 11991da177e4SLinus Torvalds offset += copy; 12001da177e4SLinus Torvalds to += copy; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12041a028e50SDavid S. Miller int end; 12051da177e4SLinus Torvalds 1206547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12071a028e50SDavid S. Miller 12081a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 12091da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12101da177e4SLinus Torvalds u8 *vaddr; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds if (copy > len) 12131da177e4SLinus Torvalds copy = len; 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 12161da177e4SLinus Torvalds memcpy(to, 12171a028e50SDavid S. Miller vaddr + skb_shinfo(skb)->frags[i].page_offset+ 12181a028e50SDavid S. Miller offset - start, copy); 12191da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds if ((len -= copy) == 0) 12221da177e4SLinus Torvalds return 0; 12231da177e4SLinus Torvalds offset += copy; 12241da177e4SLinus Torvalds to += copy; 12251da177e4SLinus Torvalds } 12261a028e50SDavid S. Miller start = end; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 12301da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds for (; list; list = list->next) { 12331a028e50SDavid S. Miller int end; 12341da177e4SLinus Torvalds 1235547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12361a028e50SDavid S. Miller 12371a028e50SDavid S. Miller end = start + list->len; 12381da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12391da177e4SLinus Torvalds if (copy > len) 12401da177e4SLinus Torvalds copy = len; 12411a028e50SDavid S. Miller if (skb_copy_bits(list, offset - start, 12421a028e50SDavid S. Miller to, copy)) 12431da177e4SLinus Torvalds goto fault; 12441da177e4SLinus Torvalds if ((len -= copy) == 0) 12451da177e4SLinus Torvalds return 0; 12461da177e4SLinus Torvalds offset += copy; 12471da177e4SLinus Torvalds to += copy; 12481da177e4SLinus Torvalds } 12491a028e50SDavid S. Miller start = end; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds if (!len) 12531da177e4SLinus Torvalds return 0; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds fault: 12561da177e4SLinus Torvalds return -EFAULT; 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds 12599c55e01cSJens Axboe /* 12609c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 12619c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 12629c55e01cSJens Axboe */ 12639c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 12649c55e01cSJens Axboe { 12659c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; 12669c55e01cSJens Axboe 12679c55e01cSJens Axboe kfree_skb(skb); 12689c55e01cSJens Axboe } 12699c55e01cSJens Axboe 12709c55e01cSJens Axboe /* 12719c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 12729c55e01cSJens Axboe */ 12739c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, 12749c55e01cSJens Axboe unsigned int len, unsigned int offset, 12759c55e01cSJens Axboe struct sk_buff *skb) 12769c55e01cSJens Axboe { 12779c55e01cSJens Axboe if (unlikely(spd->nr_pages == PIPE_BUFFERS)) 12789c55e01cSJens Axboe return 1; 12799c55e01cSJens Axboe 12809c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 12819c55e01cSJens Axboe spd->partial[spd->nr_pages].len = len; 12829c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 12839c55e01cSJens Axboe spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); 12849c55e01cSJens Axboe spd->nr_pages++; 12859c55e01cSJens Axboe return 0; 12869c55e01cSJens Axboe } 12879c55e01cSJens Axboe 12882870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 12892870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 12902870c43dSOctavian Purdila { 12912870c43dSOctavian Purdila *poff += off; 12922870c43dSOctavian Purdila *page += *poff / PAGE_SIZE; 12932870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 12942870c43dSOctavian Purdila *plen -= off; 12952870c43dSOctavian Purdila } 12962870c43dSOctavian Purdila 12972870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff, 12982870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 12992870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 13009c55e01cSJens Axboe struct splice_pipe_desc *spd) 13019c55e01cSJens Axboe { 13022870c43dSOctavian Purdila if (!*len) 13032870c43dSOctavian Purdila return 1; 13049c55e01cSJens Axboe 13052870c43dSOctavian Purdila /* skip this segment if already processed */ 13062870c43dSOctavian Purdila if (*off >= plen) { 13072870c43dSOctavian Purdila *off -= plen; 13082870c43dSOctavian Purdila return 0; 13092870c43dSOctavian Purdila } 13102870c43dSOctavian Purdila 13112870c43dSOctavian Purdila /* ignore any bits we already processed */ 13122870c43dSOctavian Purdila if (*off) { 13132870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 13142870c43dSOctavian Purdila *off = 0; 13152870c43dSOctavian Purdila } 13162870c43dSOctavian Purdila 13172870c43dSOctavian Purdila do { 13182870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 13192870c43dSOctavian Purdila 13202870c43dSOctavian Purdila /* the linear region may spread across several pages */ 13212870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 13222870c43dSOctavian Purdila 13232870c43dSOctavian Purdila if (spd_fill_page(spd, page, flen, poff, skb)) 13242870c43dSOctavian Purdila return 1; 13252870c43dSOctavian Purdila 13262870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 13272870c43dSOctavian Purdila *len -= flen; 13282870c43dSOctavian Purdila 13292870c43dSOctavian Purdila } while (*len && plen); 13302870c43dSOctavian Purdila 13312870c43dSOctavian Purdila return 0; 1332db43a282SOctavian Purdila } 13339c55e01cSJens Axboe 13349c55e01cSJens Axboe /* 13352870c43dSOctavian Purdila * Map linear and fragment data from the skb to spd. It reports failure if the 13362870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 13379c55e01cSJens Axboe */ 13387b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, 13392870c43dSOctavian Purdila unsigned int *len, 13402870c43dSOctavian Purdila struct splice_pipe_desc *spd) 13412870c43dSOctavian Purdila { 13422870c43dSOctavian Purdila int seg; 13439c55e01cSJens Axboe 13449c55e01cSJens Axboe /* 13452870c43dSOctavian Purdila * map the linear part 13469c55e01cSJens Axboe */ 13472870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 13482870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 13492870c43dSOctavian Purdila skb_headlen(skb), 13502870c43dSOctavian Purdila offset, len, skb, spd)) 13512870c43dSOctavian Purdila return 1; 13529c55e01cSJens Axboe 13539c55e01cSJens Axboe /* 13549c55e01cSJens Axboe * then map the fragments 13559c55e01cSJens Axboe */ 13569c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 13579c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 13589c55e01cSJens Axboe 13592870c43dSOctavian Purdila if (__splice_segment(f->page, f->page_offset, f->size, 13602870c43dSOctavian Purdila offset, len, skb, spd)) 13612870c43dSOctavian Purdila return 1; 13629c55e01cSJens Axboe } 13639c55e01cSJens Axboe 13649c55e01cSJens Axboe return 0; 13659c55e01cSJens Axboe } 13669c55e01cSJens Axboe 13679c55e01cSJens Axboe /* 13689c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 13699c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 13709c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 13719c55e01cSJens Axboe * handle that cleanly. 13729c55e01cSJens Axboe */ 13739c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, 13749c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 13759c55e01cSJens Axboe unsigned int flags) 13769c55e01cSJens Axboe { 13779c55e01cSJens Axboe struct partial_page partial[PIPE_BUFFERS]; 13789c55e01cSJens Axboe struct page *pages[PIPE_BUFFERS]; 13799c55e01cSJens Axboe struct splice_pipe_desc spd = { 13809c55e01cSJens Axboe .pages = pages, 13819c55e01cSJens Axboe .partial = partial, 13829c55e01cSJens Axboe .flags = flags, 13839c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 13849c55e01cSJens Axboe .spd_release = sock_spd_release, 13859c55e01cSJens Axboe }; 13869c55e01cSJens Axboe struct sk_buff *skb; 13879c55e01cSJens Axboe 13889c55e01cSJens Axboe /* 13899c55e01cSJens Axboe * I'd love to avoid the clone here, but tcp_read_sock() 13909c55e01cSJens Axboe * ignores reference counts and unconditonally kills the sk_buff 13919c55e01cSJens Axboe * on return from the actor. 13929c55e01cSJens Axboe */ 13939c55e01cSJens Axboe skb = skb_clone(__skb, GFP_KERNEL); 13949c55e01cSJens Axboe if (unlikely(!skb)) 13959c55e01cSJens Axboe return -ENOMEM; 13969c55e01cSJens Axboe 13979c55e01cSJens Axboe /* 13989c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 13999c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 14009c55e01cSJens Axboe */ 14019c55e01cSJens Axboe if (__skb_splice_bits(skb, &offset, &tlen, &spd)) 14029c55e01cSJens Axboe goto done; 14039c55e01cSJens Axboe else if (!tlen) 14049c55e01cSJens Axboe goto done; 14059c55e01cSJens Axboe 14069c55e01cSJens Axboe /* 14079c55e01cSJens Axboe * now see if we have a frag_list to map 14089c55e01cSJens Axboe */ 14099c55e01cSJens Axboe if (skb_shinfo(skb)->frag_list) { 14109c55e01cSJens Axboe struct sk_buff *list = skb_shinfo(skb)->frag_list; 14119c55e01cSJens Axboe 14129c55e01cSJens Axboe for (; list && tlen; list = list->next) { 14139c55e01cSJens Axboe if (__skb_splice_bits(list, &offset, &tlen, &spd)) 14149c55e01cSJens Axboe break; 14159c55e01cSJens Axboe } 14169c55e01cSJens Axboe } 14179c55e01cSJens Axboe 14189c55e01cSJens Axboe done: 14199c55e01cSJens Axboe /* 14209c55e01cSJens Axboe * drop our reference to the clone, the pipe consumption will 14219c55e01cSJens Axboe * drop the rest. 14229c55e01cSJens Axboe */ 14239c55e01cSJens Axboe kfree_skb(skb); 14249c55e01cSJens Axboe 14259c55e01cSJens Axboe if (spd.nr_pages) { 14269c55e01cSJens Axboe int ret; 1427293ad604SOctavian Purdila struct sock *sk = __skb->sk; 14289c55e01cSJens Axboe 14299c55e01cSJens Axboe /* 14309c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 14319c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 14329c55e01cSJens Axboe * here as compared to sendfile(). We enter here 14339c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 14349c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 14359c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 14369c55e01cSJens Axboe * and networking will grab the socket lock. 14379c55e01cSJens Axboe */ 1438293ad604SOctavian Purdila release_sock(sk); 14399c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1440293ad604SOctavian Purdila lock_sock(sk); 14419c55e01cSJens Axboe return ret; 14429c55e01cSJens Axboe } 14439c55e01cSJens Axboe 14449c55e01cSJens Axboe return 0; 14459c55e01cSJens Axboe } 14469c55e01cSJens Axboe 1447357b40a1SHerbert Xu /** 1448357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1449357b40a1SHerbert Xu * @skb: destination buffer 1450357b40a1SHerbert Xu * @offset: offset in destination 1451357b40a1SHerbert Xu * @from: source buffer 1452357b40a1SHerbert Xu * @len: number of bytes to copy 1453357b40a1SHerbert Xu * 1454357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1455357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1456357b40a1SHerbert Xu * traversing fragment lists and such. 1457357b40a1SHerbert Xu */ 1458357b40a1SHerbert Xu 14590c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1460357b40a1SHerbert Xu { 1461357b40a1SHerbert Xu int i, copy; 14621a028e50SDavid S. Miller int start = skb_headlen(skb); 1463357b40a1SHerbert Xu 1464357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1465357b40a1SHerbert Xu goto fault; 1466357b40a1SHerbert Xu 14671a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1468357b40a1SHerbert Xu if (copy > len) 1469357b40a1SHerbert Xu copy = len; 147027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1471357b40a1SHerbert Xu if ((len -= copy) == 0) 1472357b40a1SHerbert Xu return 0; 1473357b40a1SHerbert Xu offset += copy; 1474357b40a1SHerbert Xu from += copy; 1475357b40a1SHerbert Xu } 1476357b40a1SHerbert Xu 1477357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1478357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 14791a028e50SDavid S. Miller int end; 1480357b40a1SHerbert Xu 1481547b792cSIlpo Järvinen WARN_ON(start > offset + len); 14821a028e50SDavid S. Miller 14831a028e50SDavid S. Miller end = start + frag->size; 1484357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1485357b40a1SHerbert Xu u8 *vaddr; 1486357b40a1SHerbert Xu 1487357b40a1SHerbert Xu if (copy > len) 1488357b40a1SHerbert Xu copy = len; 1489357b40a1SHerbert Xu 1490357b40a1SHerbert Xu vaddr = kmap_skb_frag(frag); 14911a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 14921a028e50SDavid S. Miller from, copy); 1493357b40a1SHerbert Xu kunmap_skb_frag(vaddr); 1494357b40a1SHerbert Xu 1495357b40a1SHerbert Xu if ((len -= copy) == 0) 1496357b40a1SHerbert Xu return 0; 1497357b40a1SHerbert Xu offset += copy; 1498357b40a1SHerbert Xu from += copy; 1499357b40a1SHerbert Xu } 15001a028e50SDavid S. Miller start = end; 1501357b40a1SHerbert Xu } 1502357b40a1SHerbert Xu 1503357b40a1SHerbert Xu if (skb_shinfo(skb)->frag_list) { 1504357b40a1SHerbert Xu struct sk_buff *list = skb_shinfo(skb)->frag_list; 1505357b40a1SHerbert Xu 1506357b40a1SHerbert Xu for (; list; list = list->next) { 15071a028e50SDavid S. Miller int end; 1508357b40a1SHerbert Xu 1509547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15101a028e50SDavid S. Miller 15111a028e50SDavid S. Miller end = start + list->len; 1512357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1513357b40a1SHerbert Xu if (copy > len) 1514357b40a1SHerbert Xu copy = len; 15151a028e50SDavid S. Miller if (skb_store_bits(list, offset - start, 15161a028e50SDavid S. Miller from, copy)) 1517357b40a1SHerbert Xu goto fault; 1518357b40a1SHerbert Xu if ((len -= copy) == 0) 1519357b40a1SHerbert Xu return 0; 1520357b40a1SHerbert Xu offset += copy; 1521357b40a1SHerbert Xu from += copy; 1522357b40a1SHerbert Xu } 15231a028e50SDavid S. Miller start = end; 1524357b40a1SHerbert Xu } 1525357b40a1SHerbert Xu } 1526357b40a1SHerbert Xu if (!len) 1527357b40a1SHerbert Xu return 0; 1528357b40a1SHerbert Xu 1529357b40a1SHerbert Xu fault: 1530357b40a1SHerbert Xu return -EFAULT; 1531357b40a1SHerbert Xu } 1532357b40a1SHerbert Xu 1533357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1534357b40a1SHerbert Xu 15351da177e4SLinus Torvalds /* Checksum skb data. */ 15361da177e4SLinus Torvalds 15372bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 15382bbbc868SAl Viro int len, __wsum csum) 15391da177e4SLinus Torvalds { 15401a028e50SDavid S. Miller int start = skb_headlen(skb); 15411a028e50SDavid S. Miller int i, copy = start - offset; 15421da177e4SLinus Torvalds int pos = 0; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* Checksum header. */ 15451da177e4SLinus Torvalds if (copy > 0) { 15461da177e4SLinus Torvalds if (copy > len) 15471da177e4SLinus Torvalds copy = len; 15481da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 15491da177e4SLinus Torvalds if ((len -= copy) == 0) 15501da177e4SLinus Torvalds return csum; 15511da177e4SLinus Torvalds offset += copy; 15521da177e4SLinus Torvalds pos = copy; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15561a028e50SDavid S. Miller int end; 15571da177e4SLinus Torvalds 1558547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15591a028e50SDavid S. Miller 15601a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 15611da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 156244bb9363SAl Viro __wsum csum2; 15631da177e4SLinus Torvalds u8 *vaddr; 15641da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds if (copy > len) 15671da177e4SLinus Torvalds copy = len; 15681da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 15691a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 15701a028e50SDavid S. Miller offset - start, copy, 0); 15711da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 15721da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 15731da177e4SLinus Torvalds if (!(len -= copy)) 15741da177e4SLinus Torvalds return csum; 15751da177e4SLinus Torvalds offset += copy; 15761da177e4SLinus Torvalds pos += copy; 15771da177e4SLinus Torvalds } 15781a028e50SDavid S. Miller start = end; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 15821da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds for (; list; list = list->next) { 15851a028e50SDavid S. Miller int end; 15861da177e4SLinus Torvalds 1587547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15881a028e50SDavid S. Miller 15891a028e50SDavid S. Miller end = start + list->len; 15901da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15915f92a738SAl Viro __wsum csum2; 15921da177e4SLinus Torvalds if (copy > len) 15931da177e4SLinus Torvalds copy = len; 15941a028e50SDavid S. Miller csum2 = skb_checksum(list, offset - start, 15951a028e50SDavid S. Miller copy, 0); 15961da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 15971da177e4SLinus Torvalds if ((len -= copy) == 0) 15981da177e4SLinus Torvalds return csum; 15991da177e4SLinus Torvalds offset += copy; 16001da177e4SLinus Torvalds pos += copy; 16011da177e4SLinus Torvalds } 16021a028e50SDavid S. Miller start = end; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds } 160509a62660SKris Katterjohn BUG_ON(len); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds return csum; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /* Both of above in one bottle. */ 16111da177e4SLinus Torvalds 161281d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 161381d77662SAl Viro u8 *to, int len, __wsum csum) 16141da177e4SLinus Torvalds { 16151a028e50SDavid S. Miller int start = skb_headlen(skb); 16161a028e50SDavid S. Miller int i, copy = start - offset; 16171da177e4SLinus Torvalds int pos = 0; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds /* Copy header. */ 16201da177e4SLinus Torvalds if (copy > 0) { 16211da177e4SLinus Torvalds if (copy > len) 16221da177e4SLinus Torvalds copy = len; 16231da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 16241da177e4SLinus Torvalds copy, csum); 16251da177e4SLinus Torvalds if ((len -= copy) == 0) 16261da177e4SLinus Torvalds return csum; 16271da177e4SLinus Torvalds offset += copy; 16281da177e4SLinus Torvalds to += copy; 16291da177e4SLinus Torvalds pos = copy; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16331a028e50SDavid S. Miller int end; 16341da177e4SLinus Torvalds 1635547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16361a028e50SDavid S. Miller 16371a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 16381da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16395084205fSAl Viro __wsum csum2; 16401da177e4SLinus Torvalds u8 *vaddr; 16411da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds if (copy > len) 16441da177e4SLinus Torvalds copy = len; 16451da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 16461da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 16471a028e50SDavid S. Miller frag->page_offset + 16481a028e50SDavid S. Miller offset - start, to, 16491a028e50SDavid S. Miller copy, 0); 16501da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 16511da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16521da177e4SLinus Torvalds if (!(len -= copy)) 16531da177e4SLinus Torvalds return csum; 16541da177e4SLinus Torvalds offset += copy; 16551da177e4SLinus Torvalds to += copy; 16561da177e4SLinus Torvalds pos += copy; 16571da177e4SLinus Torvalds } 16581a028e50SDavid S. Miller start = end; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 16621da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds for (; list; list = list->next) { 166581d77662SAl Viro __wsum csum2; 16661a028e50SDavid S. Miller int end; 16671da177e4SLinus Torvalds 1668547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16691a028e50SDavid S. Miller 16701a028e50SDavid S. Miller end = start + list->len; 16711da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16721da177e4SLinus Torvalds if (copy > len) 16731da177e4SLinus Torvalds copy = len; 16741a028e50SDavid S. Miller csum2 = skb_copy_and_csum_bits(list, 16751a028e50SDavid S. Miller offset - start, 16761da177e4SLinus Torvalds to, copy, 0); 16771da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16781da177e4SLinus Torvalds if ((len -= copy) == 0) 16791da177e4SLinus Torvalds return csum; 16801da177e4SLinus Torvalds offset += copy; 16811da177e4SLinus Torvalds to += copy; 16821da177e4SLinus Torvalds pos += copy; 16831da177e4SLinus Torvalds } 16841a028e50SDavid S. Miller start = end; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds } 168709a62660SKris Katterjohn BUG_ON(len); 16881da177e4SLinus Torvalds return csum; 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 16921da177e4SLinus Torvalds { 1693d3bc23e7SAl Viro __wsum csum; 16941da177e4SLinus Torvalds long csstart; 16951da177e4SLinus Torvalds 169684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 1697663ead3bSHerbert Xu csstart = skb->csum_start - skb_headroom(skb); 16981da177e4SLinus Torvalds else 16991da177e4SLinus Torvalds csstart = skb_headlen(skb); 17001da177e4SLinus Torvalds 170109a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 17021da177e4SLinus Torvalds 1703d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds csum = 0; 17061da177e4SLinus Torvalds if (csstart != skb->len) 17071da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 17081da177e4SLinus Torvalds skb->len - csstart, 0); 17091da177e4SLinus Torvalds 171084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 1711ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 17121da177e4SLinus Torvalds 1713d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds /** 17181da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 17191da177e4SLinus Torvalds * @list: list to dequeue from 17201da177e4SLinus Torvalds * 17211da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 17221da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 17231da177e4SLinus Torvalds * returned or %NULL if the list is empty. 17241da177e4SLinus Torvalds */ 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 17271da177e4SLinus Torvalds { 17281da177e4SLinus Torvalds unsigned long flags; 17291da177e4SLinus Torvalds struct sk_buff *result; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17321da177e4SLinus Torvalds result = __skb_dequeue(list); 17331da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17341da177e4SLinus Torvalds return result; 17351da177e4SLinus Torvalds } 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /** 17381da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 17391da177e4SLinus Torvalds * @list: list to dequeue from 17401da177e4SLinus Torvalds * 17411da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 17421da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 17431da177e4SLinus Torvalds * returned or %NULL if the list is empty. 17441da177e4SLinus Torvalds */ 17451da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds unsigned long flags; 17481da177e4SLinus Torvalds struct sk_buff *result; 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17511da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 17521da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17531da177e4SLinus Torvalds return result; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds /** 17571da177e4SLinus Torvalds * skb_queue_purge - empty a list 17581da177e4SLinus Torvalds * @list: list to empty 17591da177e4SLinus Torvalds * 17601da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 17611da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 17621da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 17631da177e4SLinus Torvalds */ 17641da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 17651da177e4SLinus Torvalds { 17661da177e4SLinus Torvalds struct sk_buff *skb; 17671da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 17681da177e4SLinus Torvalds kfree_skb(skb); 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /** 17721da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 17731da177e4SLinus Torvalds * @list: list to use 17741da177e4SLinus Torvalds * @newsk: buffer to queue 17751da177e4SLinus Torvalds * 17761da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 17771da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 17781da177e4SLinus Torvalds * safely. 17791da177e4SLinus Torvalds * 17801da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 17811da177e4SLinus Torvalds */ 17821da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 17831da177e4SLinus Torvalds { 17841da177e4SLinus Torvalds unsigned long flags; 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17871da177e4SLinus Torvalds __skb_queue_head(list, newsk); 17881da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17891da177e4SLinus Torvalds } 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /** 17921da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 17931da177e4SLinus Torvalds * @list: list to use 17941da177e4SLinus Torvalds * @newsk: buffer to queue 17951da177e4SLinus Torvalds * 17961da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 17971da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 17981da177e4SLinus Torvalds * safely. 17991da177e4SLinus Torvalds * 18001da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18011da177e4SLinus Torvalds */ 18021da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 18031da177e4SLinus Torvalds { 18041da177e4SLinus Torvalds unsigned long flags; 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18071da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 18081da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18091da177e4SLinus Torvalds } 18108728b834SDavid S. Miller 18111da177e4SLinus Torvalds /** 18121da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 18131da177e4SLinus Torvalds * @skb: buffer to remove 18148728b834SDavid S. Miller * @list: list to use 18151da177e4SLinus Torvalds * 18168728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 18178728b834SDavid S. Miller * function is atomic with respect to other list locked calls 18181da177e4SLinus Torvalds * 18198728b834SDavid S. Miller * You must know what list the SKB is on. 18201da177e4SLinus Torvalds */ 18218728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds unsigned long flags; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18268728b834SDavid S. Miller __skb_unlink(skb, list); 18271da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds /** 18311da177e4SLinus Torvalds * skb_append - append a buffer 18321da177e4SLinus Torvalds * @old: buffer to insert after 18331da177e4SLinus Torvalds * @newsk: buffer to insert 18348728b834SDavid S. Miller * @list: list to use 18351da177e4SLinus Torvalds * 18361da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 18371da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 18381da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18391da177e4SLinus Torvalds */ 18408728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 18411da177e4SLinus Torvalds { 18421da177e4SLinus Torvalds unsigned long flags; 18431da177e4SLinus Torvalds 18448728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 18457de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 18468728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /** 18511da177e4SLinus Torvalds * skb_insert - insert a buffer 18521da177e4SLinus Torvalds * @old: buffer to insert before 18531da177e4SLinus Torvalds * @newsk: buffer to insert 18548728b834SDavid S. Miller * @list: list to use 18551da177e4SLinus Torvalds * 18568728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 18578728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 18588728b834SDavid S. Miller * calls. 18598728b834SDavid S. Miller * 18601da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18611da177e4SLinus Torvalds */ 18628728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 18631da177e4SLinus Torvalds { 18641da177e4SLinus Torvalds unsigned long flags; 18651da177e4SLinus Torvalds 18668728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 18678728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 18688728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 18721da177e4SLinus Torvalds struct sk_buff* skb1, 18731da177e4SLinus Torvalds const u32 len, const int pos) 18741da177e4SLinus Torvalds { 18751da177e4SLinus Torvalds int i; 18761da177e4SLinus Torvalds 1877d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1878d626f62bSArnaldo Carvalho de Melo pos - len); 18791da177e4SLinus Torvalds /* And move data appendix as is. */ 18801da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 18811da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 18841da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 18851da177e4SLinus Torvalds skb1->data_len = skb->data_len; 18861da177e4SLinus Torvalds skb1->len += skb1->data_len; 18871da177e4SLinus Torvalds skb->data_len = 0; 18881da177e4SLinus Torvalds skb->len = len; 188927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 18931da177e4SLinus Torvalds struct sk_buff* skb1, 18941da177e4SLinus Torvalds const u32 len, int pos) 18951da177e4SLinus Torvalds { 18961da177e4SLinus Torvalds int i, k = 0; 18971da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 19001da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 19011da177e4SLinus Torvalds skb->len = len; 19021da177e4SLinus Torvalds skb->data_len = len - pos; 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 19051da177e4SLinus Torvalds int size = skb_shinfo(skb)->frags[i].size; 19061da177e4SLinus Torvalds 19071da177e4SLinus Torvalds if (pos + size > len) { 19081da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds if (pos < len) { 19111da177e4SLinus Torvalds /* Split frag. 19121da177e4SLinus Torvalds * We have two variants in this case: 19131da177e4SLinus Torvalds * 1. Move all the frag to the second 19141da177e4SLinus Torvalds * part, if it is possible. F.e. 19151da177e4SLinus Torvalds * this approach is mandatory for TUX, 19161da177e4SLinus Torvalds * where splitting is expensive. 19171da177e4SLinus Torvalds * 2. Split is accurately. We make this. 19181da177e4SLinus Torvalds */ 19191da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 19201da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 19211da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].size -= len - pos; 19221da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = len - pos; 19231da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds k++; 19261da177e4SLinus Torvalds } else 19271da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19281da177e4SLinus Torvalds pos += size; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds /** 19341da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 19351da177e4SLinus Torvalds * @skb: the buffer to split 19361da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 19371da177e4SLinus Torvalds * @len: new length for skb 19381da177e4SLinus Torvalds */ 19391da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 19401da177e4SLinus Torvalds { 19411da177e4SLinus Torvalds int pos = skb_headlen(skb); 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 19441da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 19451da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 19461da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 1949677e90edSThomas Graf /** 1950677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 1951677e90edSThomas Graf * @skb: the buffer to read 1952677e90edSThomas Graf * @from: lower offset of data to be read 1953677e90edSThomas Graf * @to: upper offset of data to be read 1954677e90edSThomas Graf * @st: state variable 1955677e90edSThomas Graf * 1956677e90edSThomas Graf * Initializes the specified state variable. Must be called before 1957677e90edSThomas Graf * invoking skb_seq_read() for the first time. 1958677e90edSThomas Graf */ 1959677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1960677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 1961677e90edSThomas Graf { 1962677e90edSThomas Graf st->lower_offset = from; 1963677e90edSThomas Graf st->upper_offset = to; 1964677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 1965677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 1966677e90edSThomas Graf st->frag_data = NULL; 1967677e90edSThomas Graf } 1968677e90edSThomas Graf 1969677e90edSThomas Graf /** 1970677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 1971677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 1972677e90edSThomas Graf * @data: destination pointer for data to be returned 1973677e90edSThomas Graf * @st: state variable 1974677e90edSThomas Graf * 1975677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 1976677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 1977677e90edSThomas Graf * the head of the data block to &data and returns the length 1978677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 1979677e90edSThomas Graf * offset has been reached. 1980677e90edSThomas Graf * 1981677e90edSThomas Graf * The caller is not required to consume all of the data 1982677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 1983677e90edSThomas Graf * of bytes already consumed and the next call to 1984677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 1985677e90edSThomas Graf * 1986bc2cda1eSRandy Dunlap * Note 1: The size of each block of data returned can be arbitary, 1987677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 1988677e90edSThomas Graf * reads of potentially non linear data. 1989677e90edSThomas Graf * 1990bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 1991677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 1992677e90edSThomas Graf * a stack for this purpose. 1993677e90edSThomas Graf */ 1994677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 1995677e90edSThomas Graf struct skb_seq_state *st) 1996677e90edSThomas Graf { 1997677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 1998677e90edSThomas Graf skb_frag_t *frag; 1999677e90edSThomas Graf 2000677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2001677e90edSThomas Graf return 0; 2002677e90edSThomas Graf 2003677e90edSThomas Graf next_skb: 2004677e90edSThomas Graf block_limit = skb_headlen(st->cur_skb); 2005677e90edSThomas Graf 2006677e90edSThomas Graf if (abs_offset < block_limit) { 2007677e90edSThomas Graf *data = st->cur_skb->data + abs_offset; 2008677e90edSThomas Graf return block_limit - abs_offset; 2009677e90edSThomas Graf } 2010677e90edSThomas Graf 2011677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2012677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2013677e90edSThomas Graf 2014677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2015677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2016677e90edSThomas Graf block_limit = frag->size + st->stepped_offset; 2017677e90edSThomas Graf 2018677e90edSThomas Graf if (abs_offset < block_limit) { 2019677e90edSThomas Graf if (!st->frag_data) 2020677e90edSThomas Graf st->frag_data = kmap_skb_frag(frag); 2021677e90edSThomas Graf 2022677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2023677e90edSThomas Graf (abs_offset - st->stepped_offset); 2024677e90edSThomas Graf 2025677e90edSThomas Graf return block_limit - abs_offset; 2026677e90edSThomas Graf } 2027677e90edSThomas Graf 2028677e90edSThomas Graf if (st->frag_data) { 2029677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2030677e90edSThomas Graf st->frag_data = NULL; 2031677e90edSThomas Graf } 2032677e90edSThomas Graf 2033677e90edSThomas Graf st->frag_idx++; 2034677e90edSThomas Graf st->stepped_offset += frag->size; 2035677e90edSThomas Graf } 2036677e90edSThomas Graf 20375b5a60daSOlaf Kirch if (st->frag_data) { 20385b5a60daSOlaf Kirch kunmap_skb_frag(st->frag_data); 20395b5a60daSOlaf Kirch st->frag_data = NULL; 20405b5a60daSOlaf Kirch } 20415b5a60daSOlaf Kirch 2042677e90edSThomas Graf if (st->cur_skb->next) { 2043677e90edSThomas Graf st->cur_skb = st->cur_skb->next; 2044677e90edSThomas Graf st->frag_idx = 0; 2045677e90edSThomas Graf goto next_skb; 2046677e90edSThomas Graf } else if (st->root_skb == st->cur_skb && 2047677e90edSThomas Graf skb_shinfo(st->root_skb)->frag_list) { 2048677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2049677e90edSThomas Graf goto next_skb; 2050677e90edSThomas Graf } 2051677e90edSThomas Graf 2052677e90edSThomas Graf return 0; 2053677e90edSThomas Graf } 2054677e90edSThomas Graf 2055677e90edSThomas Graf /** 2056677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2057677e90edSThomas Graf * @st: state variable 2058677e90edSThomas Graf * 2059677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2060677e90edSThomas Graf * returned 0. 2061677e90edSThomas Graf */ 2062677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2063677e90edSThomas Graf { 2064677e90edSThomas Graf if (st->frag_data) 2065677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2066677e90edSThomas Graf } 2067677e90edSThomas Graf 20683fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 20693fc7e8a6SThomas Graf 20703fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 20713fc7e8a6SThomas Graf struct ts_config *conf, 20723fc7e8a6SThomas Graf struct ts_state *state) 20733fc7e8a6SThomas Graf { 20743fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 20753fc7e8a6SThomas Graf } 20763fc7e8a6SThomas Graf 20773fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 20783fc7e8a6SThomas Graf { 20793fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 20803fc7e8a6SThomas Graf } 20813fc7e8a6SThomas Graf 20823fc7e8a6SThomas Graf /** 20833fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 20843fc7e8a6SThomas Graf * @skb: the buffer to look in 20853fc7e8a6SThomas Graf * @from: search offset 20863fc7e8a6SThomas Graf * @to: search limit 20873fc7e8a6SThomas Graf * @config: textsearch configuration 20883fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 20893fc7e8a6SThomas Graf * 20903fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 20913fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 20923fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 20933fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 20943fc7e8a6SThomas Graf */ 20953fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 20963fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 20973fc7e8a6SThomas Graf struct ts_state *state) 20983fc7e8a6SThomas Graf { 2099f72b948dSPhil Oester unsigned int ret; 2100f72b948dSPhil Oester 21013fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 21023fc7e8a6SThomas Graf config->finish = skb_ts_finish; 21033fc7e8a6SThomas Graf 21043fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 21053fc7e8a6SThomas Graf 2106f72b948dSPhil Oester ret = textsearch_find(config, state); 2107f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 21083fc7e8a6SThomas Graf } 21093fc7e8a6SThomas Graf 2110e89e9cf5SAnanda Raju /** 2111e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 2112e89e9cf5SAnanda Raju * @sk: sock structure 2113e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2114e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2115e89e9cf5SAnanda Raju * @from: pointer to user message iov 2116e89e9cf5SAnanda Raju * @length: length of the iov message 2117e89e9cf5SAnanda Raju * 2118e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2119e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2120e89e9cf5SAnanda Raju */ 2121e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2122dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2123e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2124e89e9cf5SAnanda Raju void *from, int length) 2125e89e9cf5SAnanda Raju { 2126e89e9cf5SAnanda Raju int frg_cnt = 0; 2127e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2128e89e9cf5SAnanda Raju struct page *page = NULL; 2129e89e9cf5SAnanda Raju int copy, left; 2130e89e9cf5SAnanda Raju int offset = 0; 2131e89e9cf5SAnanda Raju int ret; 2132e89e9cf5SAnanda Raju 2133e89e9cf5SAnanda Raju do { 2134e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2135e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2136e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2137e89e9cf5SAnanda Raju return -EFAULT; 2138e89e9cf5SAnanda Raju 2139e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2140e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2141e89e9cf5SAnanda Raju 2142e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2143e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2144e89e9cf5SAnanda Raju */ 2145e89e9cf5SAnanda Raju if (page == NULL) 2146e89e9cf5SAnanda Raju return -ENOMEM; 2147e89e9cf5SAnanda Raju 2148e89e9cf5SAnanda Raju /* initialize the next frag */ 2149e89e9cf5SAnanda Raju sk->sk_sndmsg_page = page; 2150e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 2151e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2152e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2153e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2154e89e9cf5SAnanda Raju 2155e89e9cf5SAnanda Raju /* get the new initialized frag */ 2156e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2157e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2158e89e9cf5SAnanda Raju 2159e89e9cf5SAnanda Raju /* copy the user data to page */ 2160e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2161e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2162e89e9cf5SAnanda Raju 2163e89e9cf5SAnanda Raju ret = getfrag(from, (page_address(frag->page) + 2164e89e9cf5SAnanda Raju frag->page_offset + frag->size), 2165e89e9cf5SAnanda Raju offset, copy, 0, skb); 2166e89e9cf5SAnanda Raju if (ret < 0) 2167e89e9cf5SAnanda Raju return -EFAULT; 2168e89e9cf5SAnanda Raju 2169e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2170e89e9cf5SAnanda Raju sk->sk_sndmsg_off += copy; 2171e89e9cf5SAnanda Raju frag->size += copy; 2172e89e9cf5SAnanda Raju skb->len += copy; 2173e89e9cf5SAnanda Raju skb->data_len += copy; 2174e89e9cf5SAnanda Raju offset += copy; 2175e89e9cf5SAnanda Raju length -= copy; 2176e89e9cf5SAnanda Raju 2177e89e9cf5SAnanda Raju } while (length > 0); 2178e89e9cf5SAnanda Raju 2179e89e9cf5SAnanda Raju return 0; 2180e89e9cf5SAnanda Raju } 2181e89e9cf5SAnanda Raju 2182cbb042f9SHerbert Xu /** 2183cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2184cbb042f9SHerbert Xu * @skb: buffer to update 2185cbb042f9SHerbert Xu * @len: length of data pulled 2186cbb042f9SHerbert Xu * 2187cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2188fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 218984fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 219084fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 219184fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2192cbb042f9SHerbert Xu */ 2193cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2194cbb042f9SHerbert Xu { 2195cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2196cbb042f9SHerbert Xu skb->len -= len; 2197cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2198cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2199cbb042f9SHerbert Xu return skb->data += len; 2200cbb042f9SHerbert Xu } 2201cbb042f9SHerbert Xu 2202f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2203f94691acSArnaldo Carvalho de Melo 2204f4c50d99SHerbert Xu /** 2205f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2206f4c50d99SHerbert Xu * @skb: buffer to segment 2207576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2208f4c50d99SHerbert Xu * 2209f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 22104c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 22114c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2212f4c50d99SHerbert Xu */ 2213576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2214f4c50d99SHerbert Xu { 2215f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2216f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 2217f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 221898e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2219f4c50d99SHerbert Xu unsigned int offset = doffset; 2220f4c50d99SHerbert Xu unsigned int headroom; 2221f4c50d99SHerbert Xu unsigned int len; 2222576a30ebSHerbert Xu int sg = features & NETIF_F_SG; 2223f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2224f4c50d99SHerbert Xu int err = -ENOMEM; 2225f4c50d99SHerbert Xu int i = 0; 2226f4c50d99SHerbert Xu int pos; 2227f4c50d99SHerbert Xu 2228f4c50d99SHerbert Xu __skb_push(skb, doffset); 2229f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2230f4c50d99SHerbert Xu pos = skb_headlen(skb); 2231f4c50d99SHerbert Xu 2232f4c50d99SHerbert Xu do { 2233f4c50d99SHerbert Xu struct sk_buff *nskb; 2234f4c50d99SHerbert Xu skb_frag_t *frag; 2235c8884eddSHerbert Xu int hsize; 2236f4c50d99SHerbert Xu int k; 2237f4c50d99SHerbert Xu int size; 2238f4c50d99SHerbert Xu 2239f4c50d99SHerbert Xu len = skb->len - offset; 2240f4c50d99SHerbert Xu if (len > mss) 2241f4c50d99SHerbert Xu len = mss; 2242f4c50d99SHerbert Xu 2243f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2244f4c50d99SHerbert Xu if (hsize < 0) 2245f4c50d99SHerbert Xu hsize = 0; 2246c8884eddSHerbert Xu if (hsize > len || !sg) 2247c8884eddSHerbert Xu hsize = len; 2248f4c50d99SHerbert Xu 2249c8884eddSHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2250f4c50d99SHerbert Xu if (unlikely(!nskb)) 2251f4c50d99SHerbert Xu goto err; 2252f4c50d99SHerbert Xu 2253f4c50d99SHerbert Xu if (segs) 2254f4c50d99SHerbert Xu tail->next = nskb; 2255f4c50d99SHerbert Xu else 2256f4c50d99SHerbert Xu segs = nskb; 2257f4c50d99SHerbert Xu tail = nskb; 2258f4c50d99SHerbert Xu 2259f4c50d99SHerbert Xu nskb->dev = skb->dev; 2260f25f4e44SPeter P Waskiewicz Jr skb_copy_queue_mapping(nskb, skb); 2261f4c50d99SHerbert Xu nskb->priority = skb->priority; 2262f4c50d99SHerbert Xu nskb->protocol = skb->protocol; 22636aa895b0SPatrick McHardy nskb->vlan_tci = skb->vlan_tci; 2264f4c50d99SHerbert Xu nskb->dst = dst_clone(skb->dst); 2265f4c50d99SHerbert Xu memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 2266f4c50d99SHerbert Xu nskb->pkt_type = skb->pkt_type; 2267f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2268f4c50d99SHerbert Xu 2269f4c50d99SHerbert Xu skb_reserve(nskb, headroom); 2270459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2271ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2272b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2273b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 2274d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 2275d626f62bSArnaldo Carvalho de Melo doffset); 2276f4c50d99SHerbert Xu if (!sg) { 2277f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2278f4c50d99SHerbert Xu skb_put(nskb, len), 2279f4c50d99SHerbert Xu len, 0); 2280f4c50d99SHerbert Xu continue; 2281f4c50d99SHerbert Xu } 2282f4c50d99SHerbert Xu 2283f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2284f4c50d99SHerbert Xu k = 0; 2285f4c50d99SHerbert Xu 228684fa7933SPatrick McHardy nskb->ip_summed = CHECKSUM_PARTIAL; 2287f4c50d99SHerbert Xu nskb->csum = skb->csum; 2288d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2289d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2290f4c50d99SHerbert Xu 2291f4c50d99SHerbert Xu while (pos < offset + len) { 2292f4c50d99SHerbert Xu BUG_ON(i >= nfrags); 2293f4c50d99SHerbert Xu 2294f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2295f4c50d99SHerbert Xu get_page(frag->page); 2296f4c50d99SHerbert Xu size = frag->size; 2297f4c50d99SHerbert Xu 2298f4c50d99SHerbert Xu if (pos < offset) { 2299f4c50d99SHerbert Xu frag->page_offset += offset - pos; 2300f4c50d99SHerbert Xu frag->size -= offset - pos; 2301f4c50d99SHerbert Xu } 2302f4c50d99SHerbert Xu 2303f4c50d99SHerbert Xu k++; 2304f4c50d99SHerbert Xu 2305f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2306f4c50d99SHerbert Xu i++; 2307f4c50d99SHerbert Xu pos += size; 2308f4c50d99SHerbert Xu } else { 2309f4c50d99SHerbert Xu frag->size -= pos + size - (offset + len); 2310f4c50d99SHerbert Xu break; 2311f4c50d99SHerbert Xu } 2312f4c50d99SHerbert Xu 2313f4c50d99SHerbert Xu frag++; 2314f4c50d99SHerbert Xu } 2315f4c50d99SHerbert Xu 2316f4c50d99SHerbert Xu skb_shinfo(nskb)->nr_frags = k; 2317f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2318f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2319f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2320f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2321f4c50d99SHerbert Xu 2322f4c50d99SHerbert Xu return segs; 2323f4c50d99SHerbert Xu 2324f4c50d99SHerbert Xu err: 2325f4c50d99SHerbert Xu while ((skb = segs)) { 2326f4c50d99SHerbert Xu segs = skb->next; 2327b08d5840SPatrick McHardy kfree_skb(skb); 2328f4c50d99SHerbert Xu } 2329f4c50d99SHerbert Xu return ERR_PTR(err); 2330f4c50d99SHerbert Xu } 2331f4c50d99SHerbert Xu 2332f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2333f4c50d99SHerbert Xu 23341da177e4SLinus Torvalds void __init skb_init(void) 23351da177e4SLinus Torvalds { 23361da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 23371da177e4SLinus Torvalds sizeof(struct sk_buff), 23381da177e4SLinus Torvalds 0, 2339e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 234020c2df83SPaul Mundt NULL); 2341d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2342d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2343d179cd12SDavid S. Miller sizeof(atomic_t), 2344d179cd12SDavid S. Miller 0, 2345e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 234620c2df83SPaul Mundt NULL); 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 2349716ea3a7SDavid Howells /** 2350716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2351716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2352716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2353716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2354716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2355716ea3a7SDavid Howells * 2356716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2357716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2358716ea3a7SDavid Howells */ 235951c739d1SDavid S. Miller static int 236051c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2361716ea3a7SDavid Howells { 23621a028e50SDavid S. Miller int start = skb_headlen(skb); 23631a028e50SDavid S. Miller int i, copy = start - offset; 2364716ea3a7SDavid Howells int elt = 0; 2365716ea3a7SDavid Howells 2366716ea3a7SDavid Howells if (copy > 0) { 2367716ea3a7SDavid Howells if (copy > len) 2368716ea3a7SDavid Howells copy = len; 2369642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 2370716ea3a7SDavid Howells elt++; 2371716ea3a7SDavid Howells if ((len -= copy) == 0) 2372716ea3a7SDavid Howells return elt; 2373716ea3a7SDavid Howells offset += copy; 2374716ea3a7SDavid Howells } 2375716ea3a7SDavid Howells 2376716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 23771a028e50SDavid S. Miller int end; 2378716ea3a7SDavid Howells 2379547b792cSIlpo Järvinen WARN_ON(start > offset + len); 23801a028e50SDavid S. Miller 23811a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 2382716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2383716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2384716ea3a7SDavid Howells 2385716ea3a7SDavid Howells if (copy > len) 2386716ea3a7SDavid Howells copy = len; 2387642f1490SJens Axboe sg_set_page(&sg[elt], frag->page, copy, 2388642f1490SJens Axboe frag->page_offset+offset-start); 2389716ea3a7SDavid Howells elt++; 2390716ea3a7SDavid Howells if (!(len -= copy)) 2391716ea3a7SDavid Howells return elt; 2392716ea3a7SDavid Howells offset += copy; 2393716ea3a7SDavid Howells } 23941a028e50SDavid S. Miller start = end; 2395716ea3a7SDavid Howells } 2396716ea3a7SDavid Howells 2397716ea3a7SDavid Howells if (skb_shinfo(skb)->frag_list) { 2398716ea3a7SDavid Howells struct sk_buff *list = skb_shinfo(skb)->frag_list; 2399716ea3a7SDavid Howells 2400716ea3a7SDavid Howells for (; list; list = list->next) { 24011a028e50SDavid S. Miller int end; 2402716ea3a7SDavid Howells 2403547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24041a028e50SDavid S. Miller 24051a028e50SDavid S. Miller end = start + list->len; 2406716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2407716ea3a7SDavid Howells if (copy > len) 2408716ea3a7SDavid Howells copy = len; 240951c739d1SDavid S. Miller elt += __skb_to_sgvec(list, sg+elt, offset - start, 241051c739d1SDavid S. Miller copy); 2411716ea3a7SDavid Howells if ((len -= copy) == 0) 2412716ea3a7SDavid Howells return elt; 2413716ea3a7SDavid Howells offset += copy; 2414716ea3a7SDavid Howells } 24151a028e50SDavid S. Miller start = end; 2416716ea3a7SDavid Howells } 2417716ea3a7SDavid Howells } 2418716ea3a7SDavid Howells BUG_ON(len); 2419716ea3a7SDavid Howells return elt; 2420716ea3a7SDavid Howells } 2421716ea3a7SDavid Howells 242251c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 242351c739d1SDavid S. Miller { 242451c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 242551c739d1SDavid S. Miller 2426c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 242751c739d1SDavid S. Miller 242851c739d1SDavid S. Miller return nsg; 242951c739d1SDavid S. Miller } 243051c739d1SDavid S. Miller 2431716ea3a7SDavid Howells /** 2432716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 2433716ea3a7SDavid Howells * @skb: The socket buffer to check. 2434716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 2435716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 2436716ea3a7SDavid Howells * 2437716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 2438716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 2439716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 2440716ea3a7SDavid Howells * 2441716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 2442716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 2443716ea3a7SDavid Howells * set to point to the skb in which this space begins. 2444716ea3a7SDavid Howells * 2445716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 2446716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 2447716ea3a7SDavid Howells */ 2448716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2449716ea3a7SDavid Howells { 2450716ea3a7SDavid Howells int copyflag; 2451716ea3a7SDavid Howells int elt; 2452716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 2453716ea3a7SDavid Howells 2454716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 2455716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 2456716ea3a7SDavid Howells * at the moment even if they are anonymous). 2457716ea3a7SDavid Howells */ 2458716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2459716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2460716ea3a7SDavid Howells return -ENOMEM; 2461716ea3a7SDavid Howells 2462716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 2463716ea3a7SDavid Howells if (!skb_shinfo(skb)->frag_list) { 2464716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 2465716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 2466716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 2467716ea3a7SDavid Howells * space, 128 bytes is fair. */ 2468716ea3a7SDavid Howells 2469716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 2470716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2471716ea3a7SDavid Howells return -ENOMEM; 2472716ea3a7SDavid Howells 2473716ea3a7SDavid Howells /* Voila! */ 2474716ea3a7SDavid Howells *trailer = skb; 2475716ea3a7SDavid Howells return 1; 2476716ea3a7SDavid Howells } 2477716ea3a7SDavid Howells 2478716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 2479716ea3a7SDavid Howells 2480716ea3a7SDavid Howells elt = 1; 2481716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 2482716ea3a7SDavid Howells copyflag = 0; 2483716ea3a7SDavid Howells 2484716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 2485716ea3a7SDavid Howells int ntail = 0; 2486716ea3a7SDavid Howells 2487716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 2488716ea3a7SDavid Howells * this can happen on input. Copy it and everything 2489716ea3a7SDavid Howells * after it. */ 2490716ea3a7SDavid Howells 2491716ea3a7SDavid Howells if (skb_shared(skb1)) 2492716ea3a7SDavid Howells copyflag = 1; 2493716ea3a7SDavid Howells 2494716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 2495716ea3a7SDavid Howells 2496716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 2497716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 2498716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list || 2499716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 2500716ea3a7SDavid Howells ntail = tailbits + 128; 2501716ea3a7SDavid Howells } 2502716ea3a7SDavid Howells 2503716ea3a7SDavid Howells if (copyflag || 2504716ea3a7SDavid Howells skb_cloned(skb1) || 2505716ea3a7SDavid Howells ntail || 2506716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 2507716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list) { 2508716ea3a7SDavid Howells struct sk_buff *skb2; 2509716ea3a7SDavid Howells 2510716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 2511716ea3a7SDavid Howells if (ntail == 0) 2512716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 2513716ea3a7SDavid Howells else 2514716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 2515716ea3a7SDavid Howells skb_headroom(skb1), 2516716ea3a7SDavid Howells ntail, 2517716ea3a7SDavid Howells GFP_ATOMIC); 2518716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 2519716ea3a7SDavid Howells return -ENOMEM; 2520716ea3a7SDavid Howells 2521716ea3a7SDavid Howells if (skb1->sk) 2522716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 2523716ea3a7SDavid Howells 2524716ea3a7SDavid Howells /* Looking around. Are we still alive? 2525716ea3a7SDavid Howells * OK, link new skb, drop old one */ 2526716ea3a7SDavid Howells 2527716ea3a7SDavid Howells skb2->next = skb1->next; 2528716ea3a7SDavid Howells *skb_p = skb2; 2529716ea3a7SDavid Howells kfree_skb(skb1); 2530716ea3a7SDavid Howells skb1 = skb2; 2531716ea3a7SDavid Howells } 2532716ea3a7SDavid Howells elt++; 2533716ea3a7SDavid Howells *trailer = skb1; 2534716ea3a7SDavid Howells skb_p = &skb1->next; 2535716ea3a7SDavid Howells } 2536716ea3a7SDavid Howells 2537716ea3a7SDavid Howells return elt; 2538716ea3a7SDavid Howells } 2539716ea3a7SDavid Howells 2540f35d9d8aSRusty Russell /** 2541f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 2542f35d9d8aSRusty Russell * @skb: the skb to set 2543f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 2544f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 2545f35d9d8aSRusty Russell * 2546f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 2547f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 2548f35d9d8aSRusty Russell * 2549f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 2550f35d9d8aSRusty Russell * returns false you should drop the packet. 2551f35d9d8aSRusty Russell */ 2552f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 2553f35d9d8aSRusty Russell { 2554f35d9d8aSRusty Russell if (unlikely(start > skb->len - 2) || 2555f35d9d8aSRusty Russell unlikely((int)start + off > skb->len - 2)) { 2556f35d9d8aSRusty Russell if (net_ratelimit()) 2557f35d9d8aSRusty Russell printk(KERN_WARNING 2558f35d9d8aSRusty Russell "bad partial csum: csum=%u/%u len=%u\n", 2559f35d9d8aSRusty Russell start, off, skb->len); 2560f35d9d8aSRusty Russell return false; 2561f35d9d8aSRusty Russell } 2562f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 2563f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 2564f35d9d8aSRusty Russell skb->csum_offset = off; 2565f35d9d8aSRusty Russell return true; 2566f35d9d8aSRusty Russell } 2567f35d9d8aSRusty Russell 25684497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 25694497b076SBen Hutchings { 25704497b076SBen Hutchings if (net_ratelimit()) 25714497b076SBen Hutchings pr_warning("%s: received packets cannot be forwarded" 25724497b076SBen Hutchings " while LRO is enabled\n", skb->dev->name); 25734497b076SBen Hutchings } 25744497b076SBen Hutchings 25751da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim); 25761da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb); 2577231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb); 25781da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail); 2579d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 25808af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb); 25811da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy); 25821da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head); 25831da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum); 25841da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone); 25851da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy); 25861da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits); 25871da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev); 25881da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits); 25891da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand); 25901da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic); 25911da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad); 25921da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom); 25931da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic); 25941da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue); 25951da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail); 25961da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert); 25971da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge); 25981da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head); 25991da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail); 26001da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink); 26011da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append); 26021da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split); 2603677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read); 2604677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read); 2605677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read); 26063fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text); 2607e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags); 26084497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 2609716ea3a7SDavid Howells 2610716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec); 2611716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data); 2612f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set); 2613