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 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $ 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Fixes: 101da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 111da177e4SLinus Torvalds * balancer bugs. 121da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 131da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 141da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 151da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 161da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 171da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 181da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 191da177e4SLinus Torvalds * only put in the headers 201da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 211da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 221da177e4SLinus Torvalds * Andi Kleen : slabified it. 231da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * NOTE: 261da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 271da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 281da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 291da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 321da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 331da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 341da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/module.h> 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/mm.h> 451da177e4SLinus Torvalds #include <linux/interrupt.h> 461da177e4SLinus Torvalds #include <linux/in.h> 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/netdevice.h> 501da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 511da177e4SLinus Torvalds #include <net/pkt_sched.h> 521da177e4SLinus Torvalds #endif 531da177e4SLinus Torvalds #include <linux/string.h> 541da177e4SLinus Torvalds #include <linux/skbuff.h> 559c55e01cSJens Axboe #include <linux/splice.h> 561da177e4SLinus Torvalds #include <linux/cache.h> 571da177e4SLinus Torvalds #include <linux/rtnetlink.h> 581da177e4SLinus Torvalds #include <linux/init.h> 59716ea3a7SDavid Howells #include <linux/scatterlist.h> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #include <net/protocol.h> 621da177e4SLinus Torvalds #include <net/dst.h> 631da177e4SLinus Torvalds #include <net/sock.h> 641da177e4SLinus Torvalds #include <net/checksum.h> 651da177e4SLinus Torvalds #include <net/xfrm.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #include <asm/uaccess.h> 681da177e4SLinus Torvalds #include <asm/system.h> 691da177e4SLinus Torvalds 70a1f8e7f7SAl Viro #include "kmap_skb.h" 71a1f8e7f7SAl Viro 72e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly; 73e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 741da177e4SLinus Torvalds 759c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 769c55e01cSJens Axboe struct pipe_buffer *buf) 779c55e01cSJens Axboe { 789c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 799c55e01cSJens Axboe 809c55e01cSJens Axboe kfree_skb(skb); 819c55e01cSJens Axboe } 829c55e01cSJens Axboe 839c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 849c55e01cSJens Axboe struct pipe_buffer *buf) 859c55e01cSJens Axboe { 869c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 879c55e01cSJens Axboe 889c55e01cSJens Axboe skb_get(skb); 899c55e01cSJens Axboe } 909c55e01cSJens Axboe 919c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 929c55e01cSJens Axboe struct pipe_buffer *buf) 939c55e01cSJens Axboe { 949c55e01cSJens Axboe return 1; 959c55e01cSJens Axboe } 969c55e01cSJens Axboe 979c55e01cSJens Axboe 989c55e01cSJens Axboe /* Pipe buffer operations for a socket. */ 999c55e01cSJens Axboe static struct pipe_buf_operations sock_pipe_buf_ops = { 1009c55e01cSJens Axboe .can_merge = 0, 1019c55e01cSJens Axboe .map = generic_pipe_buf_map, 1029c55e01cSJens Axboe .unmap = generic_pipe_buf_unmap, 1039c55e01cSJens Axboe .confirm = generic_pipe_buf_confirm, 1049c55e01cSJens Axboe .release = sock_pipe_buf_release, 1059c55e01cSJens Axboe .steal = sock_pipe_buf_steal, 1069c55e01cSJens Axboe .get = sock_pipe_buf_get, 1079c55e01cSJens Axboe }; 1089c55e01cSJens Axboe 1091da177e4SLinus Torvalds /* 1101da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 1111da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 1121da177e4SLinus Torvalds * reliable. 1131da177e4SLinus Torvalds */ 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /** 1161da177e4SLinus Torvalds * skb_over_panic - private function 1171da177e4SLinus Torvalds * @skb: buffer 1181da177e4SLinus Torvalds * @sz: size 1191da177e4SLinus Torvalds * @here: address 1201da177e4SLinus Torvalds * 1211da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 1221da177e4SLinus Torvalds */ 1231da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1241da177e4SLinus Torvalds { 12526095455SPatrick McHardy printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 1264305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 12727a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1284305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12926095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1301da177e4SLinus Torvalds BUG(); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /** 1341da177e4SLinus Torvalds * skb_under_panic - private function 1351da177e4SLinus Torvalds * @skb: buffer 1361da177e4SLinus Torvalds * @sz: size 1371da177e4SLinus Torvalds * @here: address 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1431da177e4SLinus Torvalds { 14426095455SPatrick McHardy printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 1454305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 14627a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1474305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 14826095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1491da177e4SLinus Torvalds BUG(); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 152dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb) 153dc6de336SDavid S. Miller { 154dc6de336SDavid S. Miller printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " 155dc6de336SDavid S. Miller "len=%u, sizeof(sk_buff)=%Zd\n", 156dc6de336SDavid S. Miller skb->truesize, skb->len, sizeof(struct sk_buff)); 157dc6de336SDavid S. Miller } 158dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug); 159dc6de336SDavid S. Miller 1601da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1611da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1621da177e4SLinus Torvalds * [BEEP] leaks. 1631da177e4SLinus Torvalds * 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /** 167d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1681da177e4SLinus Torvalds * @size: size to allocate 1691da177e4SLinus Torvalds * @gfp_mask: allocation mask 170c83c2486SRandy Dunlap * @fclone: allocate from fclone cache instead of head cache 171c83c2486SRandy Dunlap * and allocate a cloned (child) skb 172b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1731da177e4SLinus Torvalds * 1741da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 1751da177e4SLinus Torvalds * tail room of size bytes. The object has a reference count of one. 1761da177e4SLinus Torvalds * The return is the buffer. On a failure the return is %NULL. 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1791da177e4SLinus Torvalds * %GFP_ATOMIC. 1801da177e4SLinus Torvalds */ 181dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 182b30973f8SChristoph Hellwig int fclone, int node) 1831da177e4SLinus Torvalds { 184e18b890bSChristoph Lameter struct kmem_cache *cache; 1854947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1861da177e4SLinus Torvalds struct sk_buff *skb; 1871da177e4SLinus Torvalds u8 *data; 1881da177e4SLinus Torvalds 1898798b3fbSHerbert Xu cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 1908798b3fbSHerbert Xu 1911da177e4SLinus Torvalds /* Get the HEAD */ 192b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1931da177e4SLinus Torvalds if (!skb) 1941da177e4SLinus Torvalds goto out; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 197b30973f8SChristoph Hellwig data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 198b30973f8SChristoph Hellwig gfp_mask, node); 1991da177e4SLinus Torvalds if (!data) 2001da177e4SLinus Torvalds goto nodata; 2011da177e4SLinus Torvalds 202ca0605a7SArnaldo Carvalho de Melo /* 203ca0605a7SArnaldo Carvalho de Melo * See comment in sk_buff definition, just before the 'tail' member 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 26627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 2671da177e4SLinus Torvalds { 26827b437c8SHerbert Xu struct sk_buff *list = *listp; 2691da177e4SLinus Torvalds 27027b437c8SHerbert Xu *listp = NULL; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds do { 2731da177e4SLinus Torvalds struct sk_buff *this = list; 2741da177e4SLinus Torvalds list = list->next; 2751da177e4SLinus Torvalds kfree_skb(this); 2761da177e4SLinus Torvalds } while (list); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 27927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 28027b437c8SHerbert Xu { 28127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 28227b437c8SHerbert Xu } 28327b437c8SHerbert Xu 2841da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds struct sk_buff *list; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds for (list = skb_shinfo(skb)->frag_list; list; list = list->next) 2891da177e4SLinus Torvalds skb_get(list); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2925bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds if (!skb->cloned || 2951da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 2961da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 2971da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 2981da177e4SLinus Torvalds int i; 2991da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 3001da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 3041da177e4SLinus Torvalds skb_drop_fraglist(skb); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds kfree(skb->head); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* 3111da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 3121da177e4SLinus Torvalds */ 3132d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 3141da177e4SLinus Torvalds { 315d179cd12SDavid S. Miller struct sk_buff *other; 316d179cd12SDavid S. Miller atomic_t *fclone_ref; 317d179cd12SDavid S. Miller 318d179cd12SDavid S. Miller switch (skb->fclone) { 319d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 3201da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 321d179cd12SDavid S. Miller break; 322d179cd12SDavid S. Miller 323d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 324d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 325d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 326d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 327d179cd12SDavid S. Miller break; 328d179cd12SDavid S. Miller 329d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 330d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 331d179cd12SDavid S. Miller other = skb - 1; 332d179cd12SDavid S. Miller 333d179cd12SDavid S. Miller /* The clone portion is available for 334d179cd12SDavid S. Miller * fast-cloning again. 335d179cd12SDavid S. Miller */ 336d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 337d179cd12SDavid S. Miller 338d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 339d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 340d179cd12SDavid S. Miller break; 3413ff50b79SStephen Hemminger } 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3442d4baff8SHerbert Xu /* Free everything but the sk_buff shell. */ 3452d4baff8SHerbert Xu static void skb_release_all(struct sk_buff *skb) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds dst_release(skb->dst); 3481da177e4SLinus Torvalds #ifdef CONFIG_XFRM 3491da177e4SLinus Torvalds secpath_put(skb->sp); 3501da177e4SLinus Torvalds #endif 3511da177e4SLinus Torvalds if (skb->destructor) { 3529c2b3328SStephen Hemminger WARN_ON(in_irq()); 3531da177e4SLinus Torvalds skb->destructor(skb); 3541da177e4SLinus Torvalds } 3559fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 3565f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 3579fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 3589fb9cbb1SYasuyuki Kozakai #endif 3591da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 3601da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 3611da177e4SLinus Torvalds #endif 3621da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 3631da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 3641da177e4SLinus Torvalds skb->tc_index = 0; 3651da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 3661da177e4SLinus Torvalds skb->tc_verd = 0; 3671da177e4SLinus Torvalds #endif 3681da177e4SLinus Torvalds #endif 3692d4baff8SHerbert Xu skb_release_data(skb); 3702d4baff8SHerbert Xu } 3711da177e4SLinus Torvalds 3722d4baff8SHerbert Xu /** 3732d4baff8SHerbert Xu * __kfree_skb - private function 3742d4baff8SHerbert Xu * @skb: buffer 3752d4baff8SHerbert Xu * 3762d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 3772d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 3782d4baff8SHerbert Xu * always call kfree_skb 3792d4baff8SHerbert Xu */ 3802d4baff8SHerbert Xu 3812d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 3822d4baff8SHerbert Xu { 3832d4baff8SHerbert Xu skb_release_all(skb); 3841da177e4SLinus Torvalds kfree_skbmem(skb); 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /** 388231d06aeSJörn Engel * kfree_skb - free an sk_buff 389231d06aeSJörn Engel * @skb: buffer to free 390231d06aeSJörn Engel * 391231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 392231d06aeSJörn Engel * hit zero. 393231d06aeSJörn Engel */ 394231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 395231d06aeSJörn Engel { 396231d06aeSJörn Engel if (unlikely(!skb)) 397231d06aeSJörn Engel return; 398231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 399231d06aeSJörn Engel smp_rmb(); 400231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 401231d06aeSJörn Engel return; 402231d06aeSJörn Engel __kfree_skb(skb); 403231d06aeSJörn Engel } 404231d06aeSJörn Engel 405dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 406dec18810SHerbert Xu { 407dec18810SHerbert Xu new->tstamp = old->tstamp; 408dec18810SHerbert Xu new->dev = old->dev; 409dec18810SHerbert Xu new->transport_header = old->transport_header; 410dec18810SHerbert Xu new->network_header = old->network_header; 411dec18810SHerbert Xu new->mac_header = old->mac_header; 412dec18810SHerbert Xu new->dst = dst_clone(old->dst); 413dec18810SHerbert Xu #ifdef CONFIG_INET 414dec18810SHerbert Xu new->sp = secpath_get(old->sp); 415dec18810SHerbert Xu #endif 416dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 417dec18810SHerbert Xu new->csum_start = old->csum_start; 418dec18810SHerbert Xu new->csum_offset = old->csum_offset; 419dec18810SHerbert Xu new->local_df = old->local_df; 420dec18810SHerbert Xu new->pkt_type = old->pkt_type; 421dec18810SHerbert Xu new->ip_summed = old->ip_summed; 422dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 423dec18810SHerbert Xu new->priority = old->priority; 424dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 425dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 426dec18810SHerbert Xu #endif 427dec18810SHerbert Xu new->protocol = old->protocol; 428dec18810SHerbert Xu new->mark = old->mark; 429dec18810SHerbert Xu __nf_copy(new, old); 430dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 431dec18810SHerbert Xu defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 432dec18810SHerbert Xu new->nf_trace = old->nf_trace; 433dec18810SHerbert Xu #endif 434dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 435dec18810SHerbert Xu new->tc_index = old->tc_index; 436dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 437dec18810SHerbert Xu new->tc_verd = old->tc_verd; 438dec18810SHerbert Xu #endif 439dec18810SHerbert Xu #endif 440dec18810SHerbert Xu skb_copy_secmark(new, old); 441dec18810SHerbert Xu } 442dec18810SHerbert Xu 443e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds #define C(x) n->x = skb->x 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds n->next = n->prev = NULL; 4481da177e4SLinus Torvalds n->sk = NULL; 449dec18810SHerbert Xu __copy_skb_header(n, skb); 450dec18810SHerbert Xu 4511da177e4SLinus Torvalds C(len); 4521da177e4SLinus Torvalds C(data_len); 4533e6b3b2eSAlexey Dobriyan C(mac_len); 454334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 45502f1c89dSPaul Moore n->cloned = 1; 4561da177e4SLinus Torvalds n->nohdr = 0; 4571da177e4SLinus Torvalds n->destructor = NULL; 45802f1c89dSPaul Moore C(iif); 4591da177e4SLinus Torvalds C(tail); 4601da177e4SLinus Torvalds C(end); 46102f1c89dSPaul Moore C(head); 46202f1c89dSPaul Moore C(data); 46302f1c89dSPaul Moore C(truesize); 46402f1c89dSPaul Moore atomic_set(&n->users, 1); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 4671da177e4SLinus Torvalds skb->cloned = 1; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds return n; 470e0053ec0SHerbert Xu #undef C 471e0053ec0SHerbert Xu } 472e0053ec0SHerbert Xu 473e0053ec0SHerbert Xu /** 474e0053ec0SHerbert Xu * skb_morph - morph one skb into another 475e0053ec0SHerbert Xu * @dst: the skb to receive the contents 476e0053ec0SHerbert Xu * @src: the skb to supply the contents 477e0053ec0SHerbert Xu * 478e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 479e0053ec0SHerbert Xu * supplied by the user. 480e0053ec0SHerbert Xu * 481e0053ec0SHerbert Xu * The target skb is returned upon exit. 482e0053ec0SHerbert Xu */ 483e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 484e0053ec0SHerbert Xu { 4852d4baff8SHerbert Xu skb_release_all(dst); 486e0053ec0SHerbert Xu return __skb_clone(dst, src); 487e0053ec0SHerbert Xu } 488e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 489e0053ec0SHerbert Xu 490e0053ec0SHerbert Xu /** 491e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 492e0053ec0SHerbert Xu * @skb: buffer to clone 493e0053ec0SHerbert Xu * @gfp_mask: allocation priority 494e0053ec0SHerbert Xu * 495e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 496e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 497e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 498e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 499e0053ec0SHerbert Xu * 500e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 501e0053ec0SHerbert Xu * %GFP_ATOMIC. 502e0053ec0SHerbert Xu */ 503e0053ec0SHerbert Xu 504e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 505e0053ec0SHerbert Xu { 506e0053ec0SHerbert Xu struct sk_buff *n; 507e0053ec0SHerbert Xu 508e0053ec0SHerbert Xu n = skb + 1; 509e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 510e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 511e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 512e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 513e0053ec0SHerbert Xu atomic_inc(fclone_ref); 514e0053ec0SHerbert Xu } else { 515e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 516e0053ec0SHerbert Xu if (!n) 517e0053ec0SHerbert Xu return NULL; 518e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 519e0053ec0SHerbert Xu } 520e0053ec0SHerbert Xu 521e0053ec0SHerbert Xu return __skb_clone(n, skb); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 5251da177e4SLinus Torvalds { 5262e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 5271da177e4SLinus Torvalds /* 5281da177e4SLinus Torvalds * Shift between the two data areas in bytes 5291da177e4SLinus Torvalds */ 5301da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 5312e07fa9cSArnaldo Carvalho de Melo #endif 532dec18810SHerbert Xu 533dec18810SHerbert Xu __copy_skb_header(new, old); 534dec18810SHerbert Xu 5352e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 5362e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 5372e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 5382e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 5392e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 5402e07fa9cSArnaldo Carvalho de Melo #endif 5417967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 5427967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 5437967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /** 5471da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 5481da177e4SLinus Torvalds * @skb: buffer to copy 5491da177e4SLinus Torvalds * @gfp_mask: allocation priority 5501da177e4SLinus Torvalds * 5511da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 5521da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 5531da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 5541da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 5551da177e4SLinus Torvalds * 5561da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 5571da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 5581da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 5591da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 5601da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 5611da177e4SLinus Torvalds */ 5621da177e4SLinus Torvalds 563dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds int headerlen = skb->data - skb->head; 5661da177e4SLinus Torvalds /* 5671da177e4SLinus Torvalds * Allocate the copy buffer 5681da177e4SLinus Torvalds */ 5694305b541SArnaldo Carvalho de Melo struct sk_buff *n; 5704305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 5714305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end + skb->data_len, gfp_mask); 5724305b541SArnaldo Carvalho de Melo #else 5734305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 5744305b541SArnaldo Carvalho de Melo #endif 5751da177e4SLinus Torvalds if (!n) 5761da177e4SLinus Torvalds return NULL; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* Set the data pointer */ 5791da177e4SLinus Torvalds skb_reserve(n, headerlen); 5801da177e4SLinus Torvalds /* Set the tail pointer and length */ 5811da177e4SLinus Torvalds skb_put(n, skb->len); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 5841da177e4SLinus Torvalds BUG(); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds copy_skb_header(n, skb); 5871da177e4SLinus Torvalds return n; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /** 5921da177e4SLinus Torvalds * pskb_copy - create copy of an sk_buff with private head. 5931da177e4SLinus Torvalds * @skb: buffer to copy 5941da177e4SLinus Torvalds * @gfp_mask: allocation priority 5951da177e4SLinus Torvalds * 5961da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 5971da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 5981da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 5991da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 6001da177e4SLinus Torvalds * or the pointer to the buffer on success. 6011da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 6021da177e4SLinus Torvalds */ 6031da177e4SLinus Torvalds 604dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds /* 6071da177e4SLinus Torvalds * Allocate the copy buffer 6081da177e4SLinus Torvalds */ 6094305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6104305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6114305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end, gfp_mask); 6124305b541SArnaldo Carvalho de Melo #else 6134305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head, gfp_mask); 6144305b541SArnaldo Carvalho de Melo #endif 6151da177e4SLinus Torvalds if (!n) 6161da177e4SLinus Torvalds goto out; 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* Set the data pointer */ 6191da177e4SLinus Torvalds skb_reserve(n, skb->data - skb->head); 6201da177e4SLinus Torvalds /* Set the tail pointer and length */ 6211da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 6221da177e4SLinus Torvalds /* Copy the bytes */ 623d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 6241da177e4SLinus Torvalds 62525f484a6SHerbert Xu n->truesize += skb->data_len; 6261da177e4SLinus Torvalds n->data_len = skb->data_len; 6271da177e4SLinus Torvalds n->len = skb->len; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 6301da177e4SLinus Torvalds int i; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 6331da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 6341da177e4SLinus Torvalds get_page(skb_shinfo(n)->frags[i].page); 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 6401da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 6411da177e4SLinus Torvalds skb_clone_fraglist(n); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds copy_skb_header(n, skb); 6451da177e4SLinus Torvalds out: 6461da177e4SLinus Torvalds return n; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /** 6501da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 6511da177e4SLinus Torvalds * @skb: buffer to reallocate 6521da177e4SLinus Torvalds * @nhead: room to add at head 6531da177e4SLinus Torvalds * @ntail: room to add at tail 6541da177e4SLinus Torvalds * @gfp_mask: allocation priority 6551da177e4SLinus Torvalds * 6561da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 6571da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 6581da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 6591da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 6601da177e4SLinus Torvalds * 6611da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 6621da177e4SLinus Torvalds * reloaded after call to this function. 6631da177e4SLinus Torvalds */ 6641da177e4SLinus Torvalds 66586a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 666dd0fc66fSAl Viro gfp_t gfp_mask) 6671da177e4SLinus Torvalds { 6681da177e4SLinus Torvalds int i; 6691da177e4SLinus Torvalds u8 *data; 6704305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6714305b541SArnaldo Carvalho de Melo int size = nhead + skb->end + ntail; 6724305b541SArnaldo Carvalho de Melo #else 6731da177e4SLinus Torvalds int size = nhead + (skb->end - skb->head) + ntail; 6744305b541SArnaldo Carvalho de Melo #endif 6751da177e4SLinus Torvalds long off; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds if (skb_shared(skb)) 6781da177e4SLinus Torvalds BUG(); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 6831da177e4SLinus Torvalds if (!data) 6841da177e4SLinus Torvalds goto nodata; 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 6871da177e4SLinus Torvalds * optimized for the cases when header is void. */ 6884305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 689b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail); 6904305b541SArnaldo Carvalho de Melo #else 691b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail - skb->head); 69227a884dcSArnaldo Carvalho de Melo #endif 6934305b541SArnaldo Carvalho de Melo memcpy(data + size, skb_end_pointer(skb), 6944305b541SArnaldo Carvalho de Melo sizeof(struct skb_shared_info)); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 6971da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 7001da177e4SLinus Torvalds skb_clone_fraglist(skb); 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds skb_release_data(skb); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds off = (data + nhead) - skb->head; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds skb->head = data; 7071da177e4SLinus Torvalds skb->data += off; 7084305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7094305b541SArnaldo Carvalho de Melo skb->end = size; 71056eb8882SPatrick McHardy off = nhead; 7114305b541SArnaldo Carvalho de Melo #else 7124305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 71356eb8882SPatrick McHardy #endif 71427a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 71527a884dcSArnaldo Carvalho de Melo skb->tail += off; 716b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 717b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 718b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 719172a863fSHerbert Xu skb->csum_start += nhead; 7201da177e4SLinus Torvalds skb->cloned = 0; 721334a8132SPatrick McHardy skb->hdr_len = 0; 7221da177e4SLinus Torvalds skb->nohdr = 0; 7231da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds nodata: 7271da177e4SLinus Torvalds return -ENOMEM; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds struct sk_buff *skb2; 7351da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds if (delta <= 0) 7381da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 7391da177e4SLinus Torvalds else { 7401da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 7411da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 7421da177e4SLinus Torvalds GFP_ATOMIC)) { 7431da177e4SLinus Torvalds kfree_skb(skb2); 7441da177e4SLinus Torvalds skb2 = NULL; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds return skb2; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /** 7521da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 7531da177e4SLinus Torvalds * @skb: buffer to copy 7541da177e4SLinus Torvalds * @newheadroom: new free bytes at head 7551da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 7561da177e4SLinus Torvalds * @gfp_mask: allocation priority 7571da177e4SLinus Torvalds * 7581da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 7591da177e4SLinus Torvalds * allocate additional space. 7601da177e4SLinus Torvalds * 7611da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 7621da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 7631da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 7641da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 7651da177e4SLinus Torvalds * 7661da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 7671da177e4SLinus Torvalds * is called from an interrupt. 7681da177e4SLinus Torvalds */ 7691da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 77086a76cafSVictor Fusco int newheadroom, int newtailroom, 771dd0fc66fSAl Viro gfp_t gfp_mask) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds /* 7741da177e4SLinus Torvalds * Allocate the copy buffer 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 7771da177e4SLinus Torvalds gfp_mask); 778efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 7791da177e4SLinus Torvalds int head_copy_len, head_copy_off; 78052886051SHerbert Xu int off; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds if (!n) 7831da177e4SLinus Torvalds return NULL; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds skb_reserve(n, newheadroom); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* Set the tail pointer and length */ 7881da177e4SLinus Torvalds skb_put(n, skb->len); 7891da177e4SLinus Torvalds 790efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 7911da177e4SLinus Torvalds head_copy_off = 0; 7921da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 7931da177e4SLinus Torvalds head_copy_len = newheadroom; 7941da177e4SLinus Torvalds else 7951da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* Copy the linear header and data. */ 7981da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 7991da177e4SLinus Torvalds skb->len + head_copy_len)) 8001da177e4SLinus Torvalds BUG(); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds copy_skb_header(n, skb); 8031da177e4SLinus Torvalds 804efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 80552886051SHerbert Xu n->csum_start += off; 80652886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 807efd1e8d5SPatrick McHardy n->transport_header += off; 808efd1e8d5SPatrick McHardy n->network_header += off; 809efd1e8d5SPatrick McHardy n->mac_header += off; 81052886051SHerbert Xu #endif 811efd1e8d5SPatrick McHardy 8121da177e4SLinus Torvalds return n; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds /** 8161da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 8171da177e4SLinus Torvalds * @skb: buffer to pad 8181da177e4SLinus Torvalds * @pad: space to pad 8191da177e4SLinus Torvalds * 8201da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 8211da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 8221da177e4SLinus Torvalds * beyond the buffer end onto the wire. 8231da177e4SLinus Torvalds * 8245b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 8251da177e4SLinus Torvalds */ 8261da177e4SLinus Torvalds 8275b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 8281da177e4SLinus Torvalds { 8295b057c6bSHerbert Xu int err; 8305b057c6bSHerbert Xu int ntail; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 8335b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 8341da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 8355b057c6bSHerbert Xu return 0; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8384305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 8395b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 8405b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 8415b057c6bSHerbert Xu if (unlikely(err)) 8425b057c6bSHerbert Xu goto free_skb; 8435b057c6bSHerbert Xu } 8445b057c6bSHerbert Xu 8455b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 8465b057c6bSHerbert Xu * to be audited. 8475b057c6bSHerbert Xu */ 8485b057c6bSHerbert Xu err = skb_linearize(skb); 8495b057c6bSHerbert Xu if (unlikely(err)) 8505b057c6bSHerbert Xu goto free_skb; 8515b057c6bSHerbert Xu 8525b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 8535b057c6bSHerbert Xu return 0; 8545b057c6bSHerbert Xu 8555b057c6bSHerbert Xu free_skb: 8561da177e4SLinus Torvalds kfree_skb(skb); 8575b057c6bSHerbert Xu return err; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 8603cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 8611da177e4SLinus Torvalds */ 8621da177e4SLinus Torvalds 8633cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 8641da177e4SLinus Torvalds { 86527b437c8SHerbert Xu struct sk_buff **fragp; 86627b437c8SHerbert Xu struct sk_buff *frag; 8671da177e4SLinus Torvalds int offset = skb_headlen(skb); 8681da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 8691da177e4SLinus Torvalds int i; 87027b437c8SHerbert Xu int err; 87127b437c8SHerbert Xu 87227b437c8SHerbert Xu if (skb_cloned(skb) && 87327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 87427b437c8SHerbert Xu return err; 8751da177e4SLinus Torvalds 876f4d26fb3SHerbert Xu i = 0; 877f4d26fb3SHerbert Xu if (offset >= len) 878f4d26fb3SHerbert Xu goto drop_pages; 879f4d26fb3SHerbert Xu 880f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 8811da177e4SLinus Torvalds int end = offset + skb_shinfo(skb)->frags[i].size; 88227b437c8SHerbert Xu 88327b437c8SHerbert Xu if (end < len) { 8841da177e4SLinus Torvalds offset = end; 88527b437c8SHerbert Xu continue; 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 88827b437c8SHerbert Xu skb_shinfo(skb)->frags[i++].size = len - offset; 88927b437c8SHerbert Xu 890f4d26fb3SHerbert Xu drop_pages: 89127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 89227b437c8SHerbert Xu 89327b437c8SHerbert Xu for (; i < nfrags; i++) 89427b437c8SHerbert Xu put_page(skb_shinfo(skb)->frags[i].page); 89527b437c8SHerbert Xu 89627b437c8SHerbert Xu if (skb_shinfo(skb)->frag_list) 89727b437c8SHerbert Xu skb_drop_fraglist(skb); 898f4d26fb3SHerbert Xu goto done; 89927b437c8SHerbert Xu } 90027b437c8SHerbert Xu 90127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 90227b437c8SHerbert Xu fragp = &frag->next) { 90327b437c8SHerbert Xu int end = offset + frag->len; 90427b437c8SHerbert Xu 90527b437c8SHerbert Xu if (skb_shared(frag)) { 90627b437c8SHerbert Xu struct sk_buff *nfrag; 90727b437c8SHerbert Xu 90827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 90927b437c8SHerbert Xu if (unlikely(!nfrag)) 91027b437c8SHerbert Xu return -ENOMEM; 91127b437c8SHerbert Xu 91227b437c8SHerbert Xu nfrag->next = frag->next; 913f4d26fb3SHerbert Xu kfree_skb(frag); 91427b437c8SHerbert Xu frag = nfrag; 91527b437c8SHerbert Xu *fragp = frag; 91627b437c8SHerbert Xu } 91727b437c8SHerbert Xu 91827b437c8SHerbert Xu if (end < len) { 91927b437c8SHerbert Xu offset = end; 92027b437c8SHerbert Xu continue; 92127b437c8SHerbert Xu } 92227b437c8SHerbert Xu 92327b437c8SHerbert Xu if (end > len && 92427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 92527b437c8SHerbert Xu return err; 92627b437c8SHerbert Xu 92727b437c8SHerbert Xu if (frag->next) 92827b437c8SHerbert Xu skb_drop_list(&frag->next); 92927b437c8SHerbert Xu break; 93027b437c8SHerbert Xu } 93127b437c8SHerbert Xu 932f4d26fb3SHerbert Xu done: 93327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 9341da177e4SLinus Torvalds skb->data_len -= skb->len - len; 9351da177e4SLinus Torvalds skb->len = len; 9361da177e4SLinus Torvalds } else { 9371da177e4SLinus Torvalds skb->len = len; 9381da177e4SLinus Torvalds skb->data_len = 0; 93927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds return 0; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds /** 9461da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 9471da177e4SLinus Torvalds * @skb: buffer to reallocate 9481da177e4SLinus Torvalds * @delta: number of bytes to advance tail 9491da177e4SLinus Torvalds * 9501da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 9511da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 9521da177e4SLinus Torvalds * data from fragmented part. 9531da177e4SLinus Torvalds * 9541da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 9551da177e4SLinus Torvalds * 9561da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 9571da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 9581da177e4SLinus Torvalds * 9591da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 9601da177e4SLinus Torvalds * reloaded after call to this function. 9611da177e4SLinus Torvalds */ 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 9641da177e4SLinus Torvalds * when it is necessary. 9651da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 9661da177e4SLinus Torvalds * 2. It may change skb pointers. 9671da177e4SLinus Torvalds * 9681da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 9731da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 9741da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 9751da177e4SLinus Torvalds */ 9764305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 9791da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 9801da177e4SLinus Torvalds GFP_ATOMIC)) 9811da177e4SLinus Torvalds return NULL; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 98427a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 9851da177e4SLinus Torvalds BUG(); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 9881da177e4SLinus Torvalds * size of pulled pages. Superb. 9891da177e4SLinus Torvalds */ 9901da177e4SLinus Torvalds if (!skb_shinfo(skb)->frag_list) 9911da177e4SLinus Torvalds goto pull_pages; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 9941da177e4SLinus Torvalds eat = delta; 9951da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 9961da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size >= eat) 9971da177e4SLinus Torvalds goto pull_pages; 9981da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 10021da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 10031da177e4SLinus Torvalds * but taking into account that pulling is expected to 10041da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 10051da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 10061da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 10071da177e4SLinus Torvalds */ 10081da177e4SLinus Torvalds if (eat) { 10091da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 10101da177e4SLinus Torvalds struct sk_buff *clone = NULL; 10111da177e4SLinus Torvalds struct sk_buff *insp = NULL; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds do { 101409a62660SKris Katterjohn BUG_ON(!list); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds if (list->len <= eat) { 10171da177e4SLinus Torvalds /* Eaten as whole. */ 10181da177e4SLinus Torvalds eat -= list->len; 10191da177e4SLinus Torvalds list = list->next; 10201da177e4SLinus Torvalds insp = list; 10211da177e4SLinus Torvalds } else { 10221da177e4SLinus Torvalds /* Eaten partially. */ 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds if (skb_shared(list)) { 10251da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 10261da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 10271da177e4SLinus Torvalds if (!clone) 10281da177e4SLinus Torvalds return NULL; 10291da177e4SLinus Torvalds insp = list->next; 10301da177e4SLinus Torvalds list = clone; 10311da177e4SLinus Torvalds } else { 10321da177e4SLinus Torvalds /* This may be pulled without 10331da177e4SLinus Torvalds * problems. */ 10341da177e4SLinus Torvalds insp = list; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 10371da177e4SLinus Torvalds if (clone) 10381da177e4SLinus Torvalds kfree_skb(clone); 10391da177e4SLinus Torvalds return NULL; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds break; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds } while (eat); 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /* Free pulled out fragments. */ 10461da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 10471da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 10481da177e4SLinus Torvalds kfree_skb(list); 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds /* And insert new clone at head. */ 10511da177e4SLinus Torvalds if (clone) { 10521da177e4SLinus Torvalds clone->next = list; 10531da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds pull_pages: 10591da177e4SLinus Torvalds eat = delta; 10601da177e4SLinus Torvalds k = 0; 10611da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10621da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size <= eat) { 10631da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 10641da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 10651da177e4SLinus Torvalds } else { 10661da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 10671da177e4SLinus Torvalds if (eat) { 10681da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 10691da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].size -= eat; 10701da177e4SLinus Torvalds eat = 0; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds k++; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds skb->tail += delta; 10781da177e4SLinus Torvalds skb->data_len -= delta; 10791da177e4SLinus Torvalds 108027a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */ 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds int i, copy; 10881a028e50SDavid S. Miller int start = skb_headlen(skb); 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (offset > (int)skb->len - len) 10911da177e4SLinus Torvalds goto fault; 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* Copy header. */ 10941a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 10951da177e4SLinus Torvalds if (copy > len) 10961da177e4SLinus Torvalds copy = len; 1097d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 10981da177e4SLinus Torvalds if ((len -= copy) == 0) 10991da177e4SLinus Torvalds return 0; 11001da177e4SLinus Torvalds offset += copy; 11011da177e4SLinus Torvalds to += copy; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11051a028e50SDavid S. Miller int end; 11061da177e4SLinus Torvalds 11071a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 11081a028e50SDavid S. Miller 11091a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 11101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 11111da177e4SLinus Torvalds u8 *vaddr; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds if (copy > len) 11141da177e4SLinus Torvalds copy = len; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 11171da177e4SLinus Torvalds memcpy(to, 11181a028e50SDavid S. Miller vaddr + skb_shinfo(skb)->frags[i].page_offset+ 11191a028e50SDavid S. Miller offset - start, copy); 11201da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds if ((len -= copy) == 0) 11231da177e4SLinus Torvalds return 0; 11241da177e4SLinus Torvalds offset += copy; 11251da177e4SLinus Torvalds to += copy; 11261da177e4SLinus Torvalds } 11271a028e50SDavid S. Miller start = end; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 11311da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds for (; list; list = list->next) { 11341a028e50SDavid S. Miller int end; 11351da177e4SLinus Torvalds 11361a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 11371a028e50SDavid S. Miller 11381a028e50SDavid S. Miller end = start + list->len; 11391da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 11401da177e4SLinus Torvalds if (copy > len) 11411da177e4SLinus Torvalds copy = len; 11421a028e50SDavid S. Miller if (skb_copy_bits(list, offset - start, 11431a028e50SDavid S. Miller to, copy)) 11441da177e4SLinus Torvalds goto fault; 11451da177e4SLinus Torvalds if ((len -= copy) == 0) 11461da177e4SLinus Torvalds return 0; 11471da177e4SLinus Torvalds offset += copy; 11481da177e4SLinus Torvalds to += copy; 11491da177e4SLinus Torvalds } 11501a028e50SDavid S. Miller start = end; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds if (!len) 11541da177e4SLinus Torvalds return 0; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds fault: 11571da177e4SLinus Torvalds return -EFAULT; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11609c55e01cSJens Axboe /* 11619c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 11629c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 11639c55e01cSJens Axboe */ 11649c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 11659c55e01cSJens Axboe { 11669c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; 11679c55e01cSJens Axboe 11689c55e01cSJens Axboe kfree_skb(skb); 11699c55e01cSJens Axboe } 11709c55e01cSJens Axboe 11719c55e01cSJens Axboe /* 11729c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 11739c55e01cSJens Axboe */ 11749c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, 11759c55e01cSJens Axboe unsigned int len, unsigned int offset, 11769c55e01cSJens Axboe struct sk_buff *skb) 11779c55e01cSJens Axboe { 11789c55e01cSJens Axboe if (unlikely(spd->nr_pages == PIPE_BUFFERS)) 11799c55e01cSJens Axboe return 1; 11809c55e01cSJens Axboe 11819c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 11829c55e01cSJens Axboe spd->partial[spd->nr_pages].len = len; 11839c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 11849c55e01cSJens Axboe spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); 11859c55e01cSJens Axboe spd->nr_pages++; 11869c55e01cSJens Axboe return 0; 11879c55e01cSJens Axboe } 11889c55e01cSJens Axboe 11899c55e01cSJens Axboe /* 11909c55e01cSJens Axboe * Map linear and fragment data from the skb to spd. Returns number of 11919c55e01cSJens Axboe * pages mapped. 11929c55e01cSJens Axboe */ 11939c55e01cSJens Axboe static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, 11949c55e01cSJens Axboe unsigned int *total_len, 11959c55e01cSJens Axboe struct splice_pipe_desc *spd) 11969c55e01cSJens Axboe { 11979c55e01cSJens Axboe unsigned int nr_pages = spd->nr_pages; 11989c55e01cSJens Axboe unsigned int poff, plen, len, toff, tlen; 11999c55e01cSJens Axboe int headlen, seg; 12009c55e01cSJens Axboe 12019c55e01cSJens Axboe toff = *offset; 12029c55e01cSJens Axboe tlen = *total_len; 12039c55e01cSJens Axboe if (!tlen) 12049c55e01cSJens Axboe goto err; 12059c55e01cSJens Axboe 12069c55e01cSJens Axboe /* 12079c55e01cSJens Axboe * if the offset is greater than the linear part, go directly to 12089c55e01cSJens Axboe * the fragments. 12099c55e01cSJens Axboe */ 12109c55e01cSJens Axboe headlen = skb_headlen(skb); 12119c55e01cSJens Axboe if (toff >= headlen) { 12129c55e01cSJens Axboe toff -= headlen; 12139c55e01cSJens Axboe goto map_frag; 12149c55e01cSJens Axboe } 12159c55e01cSJens Axboe 12169c55e01cSJens Axboe /* 12179c55e01cSJens Axboe * first map the linear region into the pages/partial map, skipping 12189c55e01cSJens Axboe * any potential initial offset. 12199c55e01cSJens Axboe */ 12209c55e01cSJens Axboe len = 0; 12219c55e01cSJens Axboe while (len < headlen) { 12229c55e01cSJens Axboe void *p = skb->data + len; 12239c55e01cSJens Axboe 12249c55e01cSJens Axboe poff = (unsigned long) p & (PAGE_SIZE - 1); 12259c55e01cSJens Axboe plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff); 12269c55e01cSJens Axboe len += plen; 12279c55e01cSJens Axboe 12289c55e01cSJens Axboe if (toff) { 12299c55e01cSJens Axboe if (plen <= toff) { 12309c55e01cSJens Axboe toff -= plen; 12319c55e01cSJens Axboe continue; 12329c55e01cSJens Axboe } 12339c55e01cSJens Axboe plen -= toff; 12349c55e01cSJens Axboe poff += toff; 12359c55e01cSJens Axboe toff = 0; 12369c55e01cSJens Axboe } 12379c55e01cSJens Axboe 12389c55e01cSJens Axboe plen = min(plen, tlen); 12399c55e01cSJens Axboe if (!plen) 12409c55e01cSJens Axboe break; 12419c55e01cSJens Axboe 12429c55e01cSJens Axboe /* 12439c55e01cSJens Axboe * just jump directly to update and return, no point 12449c55e01cSJens Axboe * in going over fragments when the output is full. 12459c55e01cSJens Axboe */ 12469c55e01cSJens Axboe if (spd_fill_page(spd, virt_to_page(p), plen, poff, skb)) 12479c55e01cSJens Axboe goto done; 12489c55e01cSJens Axboe 12499c55e01cSJens Axboe tlen -= plen; 12509c55e01cSJens Axboe } 12519c55e01cSJens Axboe 12529c55e01cSJens Axboe /* 12539c55e01cSJens Axboe * then map the fragments 12549c55e01cSJens Axboe */ 12559c55e01cSJens Axboe map_frag: 12569c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 12579c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 12589c55e01cSJens Axboe 12599c55e01cSJens Axboe plen = f->size; 12609c55e01cSJens Axboe poff = f->page_offset; 12619c55e01cSJens Axboe 12629c55e01cSJens Axboe if (toff) { 12639c55e01cSJens Axboe if (plen <= toff) { 12649c55e01cSJens Axboe toff -= plen; 12659c55e01cSJens Axboe continue; 12669c55e01cSJens Axboe } 12679c55e01cSJens Axboe plen -= toff; 12689c55e01cSJens Axboe poff += toff; 12699c55e01cSJens Axboe toff = 0; 12709c55e01cSJens Axboe } 12719c55e01cSJens Axboe 12729c55e01cSJens Axboe plen = min(plen, tlen); 12739c55e01cSJens Axboe if (!plen) 12749c55e01cSJens Axboe break; 12759c55e01cSJens Axboe 12769c55e01cSJens Axboe if (spd_fill_page(spd, f->page, plen, poff, skb)) 12779c55e01cSJens Axboe break; 12789c55e01cSJens Axboe 12799c55e01cSJens Axboe tlen -= plen; 12809c55e01cSJens Axboe } 12819c55e01cSJens Axboe 12829c55e01cSJens Axboe done: 12839c55e01cSJens Axboe if (spd->nr_pages - nr_pages) { 12849c55e01cSJens Axboe *offset = 0; 12859c55e01cSJens Axboe *total_len = tlen; 12869c55e01cSJens Axboe return 0; 12879c55e01cSJens Axboe } 12889c55e01cSJens Axboe err: 12899c55e01cSJens Axboe return 1; 12909c55e01cSJens Axboe } 12919c55e01cSJens Axboe 12929c55e01cSJens Axboe /* 12939c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 12949c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 12959c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 12969c55e01cSJens Axboe * handle that cleanly. 12979c55e01cSJens Axboe */ 12989c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, 12999c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 13009c55e01cSJens Axboe unsigned int flags) 13019c55e01cSJens Axboe { 13029c55e01cSJens Axboe struct partial_page partial[PIPE_BUFFERS]; 13039c55e01cSJens Axboe struct page *pages[PIPE_BUFFERS]; 13049c55e01cSJens Axboe struct splice_pipe_desc spd = { 13059c55e01cSJens Axboe .pages = pages, 13069c55e01cSJens Axboe .partial = partial, 13079c55e01cSJens Axboe .flags = flags, 13089c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 13099c55e01cSJens Axboe .spd_release = sock_spd_release, 13109c55e01cSJens Axboe }; 13119c55e01cSJens Axboe struct sk_buff *skb; 13129c55e01cSJens Axboe 13139c55e01cSJens Axboe /* 13149c55e01cSJens Axboe * I'd love to avoid the clone here, but tcp_read_sock() 13159c55e01cSJens Axboe * ignores reference counts and unconditonally kills the sk_buff 13169c55e01cSJens Axboe * on return from the actor. 13179c55e01cSJens Axboe */ 13189c55e01cSJens Axboe skb = skb_clone(__skb, GFP_KERNEL); 13199c55e01cSJens Axboe if (unlikely(!skb)) 13209c55e01cSJens Axboe return -ENOMEM; 13219c55e01cSJens Axboe 13229c55e01cSJens Axboe /* 13239c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 13249c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 13259c55e01cSJens Axboe */ 13269c55e01cSJens Axboe if (__skb_splice_bits(skb, &offset, &tlen, &spd)) 13279c55e01cSJens Axboe goto done; 13289c55e01cSJens Axboe else if (!tlen) 13299c55e01cSJens Axboe goto done; 13309c55e01cSJens Axboe 13319c55e01cSJens Axboe /* 13329c55e01cSJens Axboe * now see if we have a frag_list to map 13339c55e01cSJens Axboe */ 13349c55e01cSJens Axboe if (skb_shinfo(skb)->frag_list) { 13359c55e01cSJens Axboe struct sk_buff *list = skb_shinfo(skb)->frag_list; 13369c55e01cSJens Axboe 13379c55e01cSJens Axboe for (; list && tlen; list = list->next) { 13389c55e01cSJens Axboe if (__skb_splice_bits(list, &offset, &tlen, &spd)) 13399c55e01cSJens Axboe break; 13409c55e01cSJens Axboe } 13419c55e01cSJens Axboe } 13429c55e01cSJens Axboe 13439c55e01cSJens Axboe done: 13449c55e01cSJens Axboe /* 13459c55e01cSJens Axboe * drop our reference to the clone, the pipe consumption will 13469c55e01cSJens Axboe * drop the rest. 13479c55e01cSJens Axboe */ 13489c55e01cSJens Axboe kfree_skb(skb); 13499c55e01cSJens Axboe 13509c55e01cSJens Axboe if (spd.nr_pages) { 13519c55e01cSJens Axboe int ret; 13529c55e01cSJens Axboe 13539c55e01cSJens Axboe /* 13549c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 13559c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 13569c55e01cSJens Axboe * here as compared to sendfile(). We enter here 13579c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 13589c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 13599c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 13609c55e01cSJens Axboe * and networking will grab the socket lock. 13619c55e01cSJens Axboe */ 13629c55e01cSJens Axboe release_sock(__skb->sk); 13639c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 13649c55e01cSJens Axboe lock_sock(__skb->sk); 13659c55e01cSJens Axboe return ret; 13669c55e01cSJens Axboe } 13679c55e01cSJens Axboe 13689c55e01cSJens Axboe return 0; 13699c55e01cSJens Axboe } 13709c55e01cSJens Axboe 1371357b40a1SHerbert Xu /** 1372357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1373357b40a1SHerbert Xu * @skb: destination buffer 1374357b40a1SHerbert Xu * @offset: offset in destination 1375357b40a1SHerbert Xu * @from: source buffer 1376357b40a1SHerbert Xu * @len: number of bytes to copy 1377357b40a1SHerbert Xu * 1378357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1379357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1380357b40a1SHerbert Xu * traversing fragment lists and such. 1381357b40a1SHerbert Xu */ 1382357b40a1SHerbert Xu 13830c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1384357b40a1SHerbert Xu { 1385357b40a1SHerbert Xu int i, copy; 13861a028e50SDavid S. Miller int start = skb_headlen(skb); 1387357b40a1SHerbert Xu 1388357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1389357b40a1SHerbert Xu goto fault; 1390357b40a1SHerbert Xu 13911a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1392357b40a1SHerbert Xu if (copy > len) 1393357b40a1SHerbert Xu copy = len; 139427d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1395357b40a1SHerbert Xu if ((len -= copy) == 0) 1396357b40a1SHerbert Xu return 0; 1397357b40a1SHerbert Xu offset += copy; 1398357b40a1SHerbert Xu from += copy; 1399357b40a1SHerbert Xu } 1400357b40a1SHerbert Xu 1401357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1402357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 14031a028e50SDavid S. Miller int end; 1404357b40a1SHerbert Xu 14051a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 14061a028e50SDavid S. Miller 14071a028e50SDavid S. Miller end = start + frag->size; 1408357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1409357b40a1SHerbert Xu u8 *vaddr; 1410357b40a1SHerbert Xu 1411357b40a1SHerbert Xu if (copy > len) 1412357b40a1SHerbert Xu copy = len; 1413357b40a1SHerbert Xu 1414357b40a1SHerbert Xu vaddr = kmap_skb_frag(frag); 14151a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 14161a028e50SDavid S. Miller from, copy); 1417357b40a1SHerbert Xu kunmap_skb_frag(vaddr); 1418357b40a1SHerbert Xu 1419357b40a1SHerbert Xu if ((len -= copy) == 0) 1420357b40a1SHerbert Xu return 0; 1421357b40a1SHerbert Xu offset += copy; 1422357b40a1SHerbert Xu from += copy; 1423357b40a1SHerbert Xu } 14241a028e50SDavid S. Miller start = end; 1425357b40a1SHerbert Xu } 1426357b40a1SHerbert Xu 1427357b40a1SHerbert Xu if (skb_shinfo(skb)->frag_list) { 1428357b40a1SHerbert Xu struct sk_buff *list = skb_shinfo(skb)->frag_list; 1429357b40a1SHerbert Xu 1430357b40a1SHerbert Xu for (; list; list = list->next) { 14311a028e50SDavid S. Miller int end; 1432357b40a1SHerbert Xu 14331a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 14341a028e50SDavid S. Miller 14351a028e50SDavid S. Miller end = start + list->len; 1436357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1437357b40a1SHerbert Xu if (copy > len) 1438357b40a1SHerbert Xu copy = len; 14391a028e50SDavid S. Miller if (skb_store_bits(list, offset - start, 14401a028e50SDavid S. Miller from, copy)) 1441357b40a1SHerbert Xu goto fault; 1442357b40a1SHerbert Xu if ((len -= copy) == 0) 1443357b40a1SHerbert Xu return 0; 1444357b40a1SHerbert Xu offset += copy; 1445357b40a1SHerbert Xu from += copy; 1446357b40a1SHerbert Xu } 14471a028e50SDavid S. Miller start = end; 1448357b40a1SHerbert Xu } 1449357b40a1SHerbert Xu } 1450357b40a1SHerbert Xu if (!len) 1451357b40a1SHerbert Xu return 0; 1452357b40a1SHerbert Xu 1453357b40a1SHerbert Xu fault: 1454357b40a1SHerbert Xu return -EFAULT; 1455357b40a1SHerbert Xu } 1456357b40a1SHerbert Xu 1457357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1458357b40a1SHerbert Xu 14591da177e4SLinus Torvalds /* Checksum skb data. */ 14601da177e4SLinus Torvalds 14612bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 14622bbbc868SAl Viro int len, __wsum csum) 14631da177e4SLinus Torvalds { 14641a028e50SDavid S. Miller int start = skb_headlen(skb); 14651a028e50SDavid S. Miller int i, copy = start - offset; 14661da177e4SLinus Torvalds int pos = 0; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* Checksum header. */ 14691da177e4SLinus Torvalds if (copy > 0) { 14701da177e4SLinus Torvalds if (copy > len) 14711da177e4SLinus Torvalds copy = len; 14721da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 14731da177e4SLinus Torvalds if ((len -= copy) == 0) 14741da177e4SLinus Torvalds return csum; 14751da177e4SLinus Torvalds offset += copy; 14761da177e4SLinus Torvalds pos = copy; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14801a028e50SDavid S. Miller int end; 14811da177e4SLinus Torvalds 14821a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 14831a028e50SDavid S. Miller 14841a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 14851da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 148644bb9363SAl Viro __wsum csum2; 14871da177e4SLinus Torvalds u8 *vaddr; 14881da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds if (copy > len) 14911da177e4SLinus Torvalds copy = len; 14921da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 14931a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 14941a028e50SDavid S. Miller offset - start, copy, 0); 14951da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 14961da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 14971da177e4SLinus Torvalds if (!(len -= copy)) 14981da177e4SLinus Torvalds return csum; 14991da177e4SLinus Torvalds offset += copy; 15001da177e4SLinus Torvalds pos += copy; 15011da177e4SLinus Torvalds } 15021a028e50SDavid S. Miller start = end; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 15061da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds for (; list; list = list->next) { 15091a028e50SDavid S. Miller int end; 15101da177e4SLinus Torvalds 15111a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 15121a028e50SDavid S. Miller 15131a028e50SDavid S. Miller end = start + list->len; 15141da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15155f92a738SAl Viro __wsum csum2; 15161da177e4SLinus Torvalds if (copy > len) 15171da177e4SLinus Torvalds copy = len; 15181a028e50SDavid S. Miller csum2 = skb_checksum(list, offset - start, 15191a028e50SDavid S. Miller copy, 0); 15201da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 15211da177e4SLinus Torvalds if ((len -= copy) == 0) 15221da177e4SLinus Torvalds return csum; 15231da177e4SLinus Torvalds offset += copy; 15241da177e4SLinus Torvalds pos += copy; 15251da177e4SLinus Torvalds } 15261a028e50SDavid S. Miller start = end; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds } 152909a62660SKris Katterjohn BUG_ON(len); 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds return csum; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds /* Both of above in one bottle. */ 15351da177e4SLinus Torvalds 153681d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 153781d77662SAl Viro u8 *to, int len, __wsum csum) 15381da177e4SLinus Torvalds { 15391a028e50SDavid S. Miller int start = skb_headlen(skb); 15401a028e50SDavid S. Miller int i, copy = start - offset; 15411da177e4SLinus Torvalds int pos = 0; 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* Copy header. */ 15441da177e4SLinus Torvalds if (copy > 0) { 15451da177e4SLinus Torvalds if (copy > len) 15461da177e4SLinus Torvalds copy = len; 15471da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 15481da177e4SLinus Torvalds copy, csum); 15491da177e4SLinus Torvalds if ((len -= copy) == 0) 15501da177e4SLinus Torvalds return csum; 15511da177e4SLinus Torvalds offset += copy; 15521da177e4SLinus Torvalds to += copy; 15531da177e4SLinus Torvalds pos = copy; 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15571a028e50SDavid S. Miller int end; 15581da177e4SLinus Torvalds 15591a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 15601a028e50SDavid S. Miller 15611a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 15621da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15635084205fSAl Viro __wsum csum2; 15641da177e4SLinus Torvalds u8 *vaddr; 15651da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (copy > len) 15681da177e4SLinus Torvalds copy = len; 15691da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 15701da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 15711a028e50SDavid S. Miller frag->page_offset + 15721a028e50SDavid S. Miller offset - start, to, 15731a028e50SDavid S. Miller copy, 0); 15741da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 15751da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 15761da177e4SLinus Torvalds if (!(len -= copy)) 15771da177e4SLinus Torvalds return csum; 15781da177e4SLinus Torvalds offset += copy; 15791da177e4SLinus Torvalds to += copy; 15801da177e4SLinus Torvalds pos += copy; 15811da177e4SLinus Torvalds } 15821a028e50SDavid S. Miller start = end; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 15861da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds for (; list; list = list->next) { 158981d77662SAl Viro __wsum csum2; 15901a028e50SDavid S. Miller int end; 15911da177e4SLinus Torvalds 15921a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 15931a028e50SDavid S. Miller 15941a028e50SDavid S. Miller end = start + list->len; 15951da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 15961da177e4SLinus Torvalds if (copy > len) 15971da177e4SLinus Torvalds copy = len; 15981a028e50SDavid S. Miller csum2 = skb_copy_and_csum_bits(list, 15991a028e50SDavid S. Miller offset - start, 16001da177e4SLinus Torvalds to, copy, 0); 16011da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16021da177e4SLinus Torvalds if ((len -= copy) == 0) 16031da177e4SLinus Torvalds return csum; 16041da177e4SLinus Torvalds offset += copy; 16051da177e4SLinus Torvalds to += copy; 16061da177e4SLinus Torvalds pos += copy; 16071da177e4SLinus Torvalds } 16081a028e50SDavid S. Miller start = end; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds } 161109a62660SKris Katterjohn BUG_ON(len); 16121da177e4SLinus Torvalds return csum; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 16161da177e4SLinus Torvalds { 1617d3bc23e7SAl Viro __wsum csum; 16181da177e4SLinus Torvalds long csstart; 16191da177e4SLinus Torvalds 162084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 1621663ead3bSHerbert Xu csstart = skb->csum_start - skb_headroom(skb); 16221da177e4SLinus Torvalds else 16231da177e4SLinus Torvalds csstart = skb_headlen(skb); 16241da177e4SLinus Torvalds 162509a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 16261da177e4SLinus Torvalds 1627d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds csum = 0; 16301da177e4SLinus Torvalds if (csstart != skb->len) 16311da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 16321da177e4SLinus Torvalds skb->len - csstart, 0); 16331da177e4SLinus Torvalds 163484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 1635ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 16361da177e4SLinus Torvalds 1637d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds /** 16421da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 16431da177e4SLinus Torvalds * @list: list to dequeue from 16441da177e4SLinus Torvalds * 16451da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 16461da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 16471da177e4SLinus Torvalds * returned or %NULL if the list is empty. 16481da177e4SLinus Torvalds */ 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 16511da177e4SLinus Torvalds { 16521da177e4SLinus Torvalds unsigned long flags; 16531da177e4SLinus Torvalds struct sk_buff *result; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 16561da177e4SLinus Torvalds result = __skb_dequeue(list); 16571da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 16581da177e4SLinus Torvalds return result; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds /** 16621da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 16631da177e4SLinus Torvalds * @list: list to dequeue from 16641da177e4SLinus Torvalds * 16651da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 16661da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 16671da177e4SLinus Torvalds * returned or %NULL if the list is empty. 16681da177e4SLinus Torvalds */ 16691da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds unsigned long flags; 16721da177e4SLinus Torvalds struct sk_buff *result; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 16751da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 16761da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 16771da177e4SLinus Torvalds return result; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds /** 16811da177e4SLinus Torvalds * skb_queue_purge - empty a list 16821da177e4SLinus Torvalds * @list: list to empty 16831da177e4SLinus Torvalds * 16841da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 16851da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 16861da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 16871da177e4SLinus Torvalds */ 16881da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 16891da177e4SLinus Torvalds { 16901da177e4SLinus Torvalds struct sk_buff *skb; 16911da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 16921da177e4SLinus Torvalds kfree_skb(skb); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /** 16961da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 16971da177e4SLinus Torvalds * @list: list to use 16981da177e4SLinus Torvalds * @newsk: buffer to queue 16991da177e4SLinus Torvalds * 17001da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 17011da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 17021da177e4SLinus Torvalds * safely. 17031da177e4SLinus Torvalds * 17041da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 17051da177e4SLinus Torvalds */ 17061da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 17071da177e4SLinus Torvalds { 17081da177e4SLinus Torvalds unsigned long flags; 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17111da177e4SLinus Torvalds __skb_queue_head(list, newsk); 17121da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /** 17161da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 17171da177e4SLinus Torvalds * @list: list to use 17181da177e4SLinus Torvalds * @newsk: buffer to queue 17191da177e4SLinus Torvalds * 17201da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 17211da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 17221da177e4SLinus Torvalds * safely. 17231da177e4SLinus Torvalds * 17241da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 17251da177e4SLinus Torvalds */ 17261da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 17271da177e4SLinus Torvalds { 17281da177e4SLinus Torvalds unsigned long flags; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17311da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 17321da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17331da177e4SLinus Torvalds } 17348728b834SDavid S. Miller 17351da177e4SLinus Torvalds /** 17361da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 17371da177e4SLinus Torvalds * @skb: buffer to remove 17388728b834SDavid S. Miller * @list: list to use 17391da177e4SLinus Torvalds * 17408728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 17418728b834SDavid S. Miller * function is atomic with respect to other list locked calls 17421da177e4SLinus Torvalds * 17438728b834SDavid S. Miller * You must know what list the SKB is on. 17441da177e4SLinus Torvalds */ 17458728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 17461da177e4SLinus Torvalds { 17471da177e4SLinus Torvalds unsigned long flags; 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17508728b834SDavid S. Miller __skb_unlink(skb, list); 17511da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /** 17551da177e4SLinus Torvalds * skb_append - append a buffer 17561da177e4SLinus Torvalds * @old: buffer to insert after 17571da177e4SLinus Torvalds * @newsk: buffer to insert 17588728b834SDavid S. Miller * @list: list to use 17591da177e4SLinus Torvalds * 17601da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 17611da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 17621da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 17631da177e4SLinus Torvalds */ 17648728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 17651da177e4SLinus Torvalds { 17661da177e4SLinus Torvalds unsigned long flags; 17671da177e4SLinus Torvalds 17688728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 17698728b834SDavid S. Miller __skb_append(old, newsk, list); 17708728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 17711da177e4SLinus Torvalds } 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds /** 17751da177e4SLinus Torvalds * skb_insert - insert a buffer 17761da177e4SLinus Torvalds * @old: buffer to insert before 17771da177e4SLinus Torvalds * @newsk: buffer to insert 17788728b834SDavid S. Miller * @list: list to use 17791da177e4SLinus Torvalds * 17808728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 17818728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 17828728b834SDavid S. Miller * calls. 17838728b834SDavid S. Miller * 17841da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 17851da177e4SLinus Torvalds */ 17868728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds unsigned long flags; 17891da177e4SLinus Torvalds 17908728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 17918728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 17928728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 17961da177e4SLinus Torvalds struct sk_buff* skb1, 17971da177e4SLinus Torvalds const u32 len, const int pos) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds int i; 18001da177e4SLinus Torvalds 1801d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1802d626f62bSArnaldo Carvalho de Melo pos - len); 18031da177e4SLinus Torvalds /* And move data appendix as is. */ 18041da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 18051da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 18081da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 18091da177e4SLinus Torvalds skb1->data_len = skb->data_len; 18101da177e4SLinus Torvalds skb1->len += skb1->data_len; 18111da177e4SLinus Torvalds skb->data_len = 0; 18121da177e4SLinus Torvalds skb->len = len; 181327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 18171da177e4SLinus Torvalds struct sk_buff* skb1, 18181da177e4SLinus Torvalds const u32 len, int pos) 18191da177e4SLinus Torvalds { 18201da177e4SLinus Torvalds int i, k = 0; 18211da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 18241da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 18251da177e4SLinus Torvalds skb->len = len; 18261da177e4SLinus Torvalds skb->data_len = len - pos; 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 18291da177e4SLinus Torvalds int size = skb_shinfo(skb)->frags[i].size; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds if (pos + size > len) { 18321da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds if (pos < len) { 18351da177e4SLinus Torvalds /* Split frag. 18361da177e4SLinus Torvalds * We have two variants in this case: 18371da177e4SLinus Torvalds * 1. Move all the frag to the second 18381da177e4SLinus Torvalds * part, if it is possible. F.e. 18391da177e4SLinus Torvalds * this approach is mandatory for TUX, 18401da177e4SLinus Torvalds * where splitting is expensive. 18411da177e4SLinus Torvalds * 2. Split is accurately. We make this. 18421da177e4SLinus Torvalds */ 18431da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 18441da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 18451da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].size -= len - pos; 18461da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = len - pos; 18471da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds k++; 18501da177e4SLinus Torvalds } else 18511da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 18521da177e4SLinus Torvalds pos += size; 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds /** 18581da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 18591da177e4SLinus Torvalds * @skb: the buffer to split 18601da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 18611da177e4SLinus Torvalds * @len: new length for skb 18621da177e4SLinus Torvalds */ 18631da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 18641da177e4SLinus Torvalds { 18651da177e4SLinus Torvalds int pos = skb_headlen(skb); 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 18681da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 18691da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 18701da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 1873677e90edSThomas Graf /** 1874677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 1875677e90edSThomas Graf * @skb: the buffer to read 1876677e90edSThomas Graf * @from: lower offset of data to be read 1877677e90edSThomas Graf * @to: upper offset of data to be read 1878677e90edSThomas Graf * @st: state variable 1879677e90edSThomas Graf * 1880677e90edSThomas Graf * Initializes the specified state variable. Must be called before 1881677e90edSThomas Graf * invoking skb_seq_read() for the first time. 1882677e90edSThomas Graf */ 1883677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1884677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 1885677e90edSThomas Graf { 1886677e90edSThomas Graf st->lower_offset = from; 1887677e90edSThomas Graf st->upper_offset = to; 1888677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 1889677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 1890677e90edSThomas Graf st->frag_data = NULL; 1891677e90edSThomas Graf } 1892677e90edSThomas Graf 1893677e90edSThomas Graf /** 1894677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 1895677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 1896677e90edSThomas Graf * @data: destination pointer for data to be returned 1897677e90edSThomas Graf * @st: state variable 1898677e90edSThomas Graf * 1899677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 1900677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 1901677e90edSThomas Graf * the head of the data block to &data and returns the length 1902677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 1903677e90edSThomas Graf * offset has been reached. 1904677e90edSThomas Graf * 1905677e90edSThomas Graf * The caller is not required to consume all of the data 1906677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 1907677e90edSThomas Graf * of bytes already consumed and the next call to 1908677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 1909677e90edSThomas Graf * 1910bc2cda1eSRandy Dunlap * Note 1: The size of each block of data returned can be arbitary, 1911677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 1912677e90edSThomas Graf * reads of potentially non linear data. 1913677e90edSThomas Graf * 1914bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 1915677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 1916677e90edSThomas Graf * a stack for this purpose. 1917677e90edSThomas Graf */ 1918677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 1919677e90edSThomas Graf struct skb_seq_state *st) 1920677e90edSThomas Graf { 1921677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 1922677e90edSThomas Graf skb_frag_t *frag; 1923677e90edSThomas Graf 1924677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 1925677e90edSThomas Graf return 0; 1926677e90edSThomas Graf 1927677e90edSThomas Graf next_skb: 1928677e90edSThomas Graf block_limit = skb_headlen(st->cur_skb); 1929677e90edSThomas Graf 1930677e90edSThomas Graf if (abs_offset < block_limit) { 1931677e90edSThomas Graf *data = st->cur_skb->data + abs_offset; 1932677e90edSThomas Graf return block_limit - abs_offset; 1933677e90edSThomas Graf } 1934677e90edSThomas Graf 1935677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 1936677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 1937677e90edSThomas Graf 1938677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 1939677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 1940677e90edSThomas Graf block_limit = frag->size + st->stepped_offset; 1941677e90edSThomas Graf 1942677e90edSThomas Graf if (abs_offset < block_limit) { 1943677e90edSThomas Graf if (!st->frag_data) 1944677e90edSThomas Graf st->frag_data = kmap_skb_frag(frag); 1945677e90edSThomas Graf 1946677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 1947677e90edSThomas Graf (abs_offset - st->stepped_offset); 1948677e90edSThomas Graf 1949677e90edSThomas Graf return block_limit - abs_offset; 1950677e90edSThomas Graf } 1951677e90edSThomas Graf 1952677e90edSThomas Graf if (st->frag_data) { 1953677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 1954677e90edSThomas Graf st->frag_data = NULL; 1955677e90edSThomas Graf } 1956677e90edSThomas Graf 1957677e90edSThomas Graf st->frag_idx++; 1958677e90edSThomas Graf st->stepped_offset += frag->size; 1959677e90edSThomas Graf } 1960677e90edSThomas Graf 19615b5a60daSOlaf Kirch if (st->frag_data) { 19625b5a60daSOlaf Kirch kunmap_skb_frag(st->frag_data); 19635b5a60daSOlaf Kirch st->frag_data = NULL; 19645b5a60daSOlaf Kirch } 19655b5a60daSOlaf Kirch 1966677e90edSThomas Graf if (st->cur_skb->next) { 1967677e90edSThomas Graf st->cur_skb = st->cur_skb->next; 1968677e90edSThomas Graf st->frag_idx = 0; 1969677e90edSThomas Graf goto next_skb; 1970677e90edSThomas Graf } else if (st->root_skb == st->cur_skb && 1971677e90edSThomas Graf skb_shinfo(st->root_skb)->frag_list) { 1972677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 1973677e90edSThomas Graf goto next_skb; 1974677e90edSThomas Graf } 1975677e90edSThomas Graf 1976677e90edSThomas Graf return 0; 1977677e90edSThomas Graf } 1978677e90edSThomas Graf 1979677e90edSThomas Graf /** 1980677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 1981677e90edSThomas Graf * @st: state variable 1982677e90edSThomas Graf * 1983677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 1984677e90edSThomas Graf * returned 0. 1985677e90edSThomas Graf */ 1986677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 1987677e90edSThomas Graf { 1988677e90edSThomas Graf if (st->frag_data) 1989677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 1990677e90edSThomas Graf } 1991677e90edSThomas Graf 19923fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 19933fc7e8a6SThomas Graf 19943fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 19953fc7e8a6SThomas Graf struct ts_config *conf, 19963fc7e8a6SThomas Graf struct ts_state *state) 19973fc7e8a6SThomas Graf { 19983fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 19993fc7e8a6SThomas Graf } 20003fc7e8a6SThomas Graf 20013fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 20023fc7e8a6SThomas Graf { 20033fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 20043fc7e8a6SThomas Graf } 20053fc7e8a6SThomas Graf 20063fc7e8a6SThomas Graf /** 20073fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 20083fc7e8a6SThomas Graf * @skb: the buffer to look in 20093fc7e8a6SThomas Graf * @from: search offset 20103fc7e8a6SThomas Graf * @to: search limit 20113fc7e8a6SThomas Graf * @config: textsearch configuration 20123fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 20133fc7e8a6SThomas Graf * 20143fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 20153fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 20163fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 20173fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 20183fc7e8a6SThomas Graf */ 20193fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 20203fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 20213fc7e8a6SThomas Graf struct ts_state *state) 20223fc7e8a6SThomas Graf { 2023f72b948dSPhil Oester unsigned int ret; 2024f72b948dSPhil Oester 20253fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 20263fc7e8a6SThomas Graf config->finish = skb_ts_finish; 20273fc7e8a6SThomas Graf 20283fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 20293fc7e8a6SThomas Graf 2030f72b948dSPhil Oester ret = textsearch_find(config, state); 2031f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 20323fc7e8a6SThomas Graf } 20333fc7e8a6SThomas Graf 2034e89e9cf5SAnanda Raju /** 2035e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 2036e89e9cf5SAnanda Raju * @sk: sock structure 2037e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2038e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2039e89e9cf5SAnanda Raju * @from: pointer to user message iov 2040e89e9cf5SAnanda Raju * @length: length of the iov message 2041e89e9cf5SAnanda Raju * 2042e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2043e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2044e89e9cf5SAnanda Raju */ 2045e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2046dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2047e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2048e89e9cf5SAnanda Raju void *from, int length) 2049e89e9cf5SAnanda Raju { 2050e89e9cf5SAnanda Raju int frg_cnt = 0; 2051e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2052e89e9cf5SAnanda Raju struct page *page = NULL; 2053e89e9cf5SAnanda Raju int copy, left; 2054e89e9cf5SAnanda Raju int offset = 0; 2055e89e9cf5SAnanda Raju int ret; 2056e89e9cf5SAnanda Raju 2057e89e9cf5SAnanda Raju do { 2058e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2059e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2060e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2061e89e9cf5SAnanda Raju return -EFAULT; 2062e89e9cf5SAnanda Raju 2063e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2064e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2065e89e9cf5SAnanda Raju 2066e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2067e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2068e89e9cf5SAnanda Raju */ 2069e89e9cf5SAnanda Raju if (page == NULL) 2070e89e9cf5SAnanda Raju return -ENOMEM; 2071e89e9cf5SAnanda Raju 2072e89e9cf5SAnanda Raju /* initialize the next frag */ 2073e89e9cf5SAnanda Raju sk->sk_sndmsg_page = page; 2074e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 2075e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2076e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2077e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2078e89e9cf5SAnanda Raju 2079e89e9cf5SAnanda Raju /* get the new initialized frag */ 2080e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2081e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2082e89e9cf5SAnanda Raju 2083e89e9cf5SAnanda Raju /* copy the user data to page */ 2084e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2085e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2086e89e9cf5SAnanda Raju 2087e89e9cf5SAnanda Raju ret = getfrag(from, (page_address(frag->page) + 2088e89e9cf5SAnanda Raju frag->page_offset + frag->size), 2089e89e9cf5SAnanda Raju offset, copy, 0, skb); 2090e89e9cf5SAnanda Raju if (ret < 0) 2091e89e9cf5SAnanda Raju return -EFAULT; 2092e89e9cf5SAnanda Raju 2093e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2094e89e9cf5SAnanda Raju sk->sk_sndmsg_off += copy; 2095e89e9cf5SAnanda Raju frag->size += copy; 2096e89e9cf5SAnanda Raju skb->len += copy; 2097e89e9cf5SAnanda Raju skb->data_len += copy; 2098e89e9cf5SAnanda Raju offset += copy; 2099e89e9cf5SAnanda Raju length -= copy; 2100e89e9cf5SAnanda Raju 2101e89e9cf5SAnanda Raju } while (length > 0); 2102e89e9cf5SAnanda Raju 2103e89e9cf5SAnanda Raju return 0; 2104e89e9cf5SAnanda Raju } 2105e89e9cf5SAnanda Raju 2106cbb042f9SHerbert Xu /** 2107cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2108cbb042f9SHerbert Xu * @skb: buffer to update 2109cbb042f9SHerbert Xu * @start: start of data before pull 2110cbb042f9SHerbert Xu * @len: length of data pulled 2111cbb042f9SHerbert Xu * 2112cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 211384fa7933SPatrick McHardy * update the CHECKSUM_COMPLETE checksum. It should be used on 211484fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 211584fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 211684fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2117cbb042f9SHerbert Xu */ 2118cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2119cbb042f9SHerbert Xu { 2120cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2121cbb042f9SHerbert Xu skb->len -= len; 2122cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2123cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2124cbb042f9SHerbert Xu return skb->data += len; 2125cbb042f9SHerbert Xu } 2126cbb042f9SHerbert Xu 2127f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2128f94691acSArnaldo Carvalho de Melo 2129f4c50d99SHerbert Xu /** 2130f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2131f4c50d99SHerbert Xu * @skb: buffer to segment 2132576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2133f4c50d99SHerbert Xu * 2134f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 2135f4c50d99SHerbert Xu * the segment at the given position. It returns NULL if there are 2136f4c50d99SHerbert Xu * no more segments to generate, or when an error is encountered. 2137f4c50d99SHerbert Xu */ 2138576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2139f4c50d99SHerbert Xu { 2140f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2141f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 2142f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 214398e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2144f4c50d99SHerbert Xu unsigned int offset = doffset; 2145f4c50d99SHerbert Xu unsigned int headroom; 2146f4c50d99SHerbert Xu unsigned int len; 2147576a30ebSHerbert Xu int sg = features & NETIF_F_SG; 2148f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2149f4c50d99SHerbert Xu int err = -ENOMEM; 2150f4c50d99SHerbert Xu int i = 0; 2151f4c50d99SHerbert Xu int pos; 2152f4c50d99SHerbert Xu 2153f4c50d99SHerbert Xu __skb_push(skb, doffset); 2154f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2155f4c50d99SHerbert Xu pos = skb_headlen(skb); 2156f4c50d99SHerbert Xu 2157f4c50d99SHerbert Xu do { 2158f4c50d99SHerbert Xu struct sk_buff *nskb; 2159f4c50d99SHerbert Xu skb_frag_t *frag; 2160c8884eddSHerbert Xu int hsize; 2161f4c50d99SHerbert Xu int k; 2162f4c50d99SHerbert Xu int size; 2163f4c50d99SHerbert Xu 2164f4c50d99SHerbert Xu len = skb->len - offset; 2165f4c50d99SHerbert Xu if (len > mss) 2166f4c50d99SHerbert Xu len = mss; 2167f4c50d99SHerbert Xu 2168f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2169f4c50d99SHerbert Xu if (hsize < 0) 2170f4c50d99SHerbert Xu hsize = 0; 2171c8884eddSHerbert Xu if (hsize > len || !sg) 2172c8884eddSHerbert Xu hsize = len; 2173f4c50d99SHerbert Xu 2174c8884eddSHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2175f4c50d99SHerbert Xu if (unlikely(!nskb)) 2176f4c50d99SHerbert Xu goto err; 2177f4c50d99SHerbert Xu 2178f4c50d99SHerbert Xu if (segs) 2179f4c50d99SHerbert Xu tail->next = nskb; 2180f4c50d99SHerbert Xu else 2181f4c50d99SHerbert Xu segs = nskb; 2182f4c50d99SHerbert Xu tail = nskb; 2183f4c50d99SHerbert Xu 2184f4c50d99SHerbert Xu nskb->dev = skb->dev; 2185f25f4e44SPeter P Waskiewicz Jr skb_copy_queue_mapping(nskb, skb); 2186f4c50d99SHerbert Xu nskb->priority = skb->priority; 2187f4c50d99SHerbert Xu nskb->protocol = skb->protocol; 2188f4c50d99SHerbert Xu nskb->dst = dst_clone(skb->dst); 2189f4c50d99SHerbert Xu memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 2190f4c50d99SHerbert Xu nskb->pkt_type = skb->pkt_type; 2191f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2192f4c50d99SHerbert Xu 2193f4c50d99SHerbert Xu skb_reserve(nskb, headroom); 2194459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2195ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2196b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2197b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 2198d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 2199d626f62bSArnaldo Carvalho de Melo doffset); 2200f4c50d99SHerbert Xu if (!sg) { 2201f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2202f4c50d99SHerbert Xu skb_put(nskb, len), 2203f4c50d99SHerbert Xu len, 0); 2204f4c50d99SHerbert Xu continue; 2205f4c50d99SHerbert Xu } 2206f4c50d99SHerbert Xu 2207f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2208f4c50d99SHerbert Xu k = 0; 2209f4c50d99SHerbert Xu 221084fa7933SPatrick McHardy nskb->ip_summed = CHECKSUM_PARTIAL; 2211f4c50d99SHerbert Xu nskb->csum = skb->csum; 2212d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2213d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2214f4c50d99SHerbert Xu 2215f4c50d99SHerbert Xu while (pos < offset + len) { 2216f4c50d99SHerbert Xu BUG_ON(i >= nfrags); 2217f4c50d99SHerbert Xu 2218f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2219f4c50d99SHerbert Xu get_page(frag->page); 2220f4c50d99SHerbert Xu size = frag->size; 2221f4c50d99SHerbert Xu 2222f4c50d99SHerbert Xu if (pos < offset) { 2223f4c50d99SHerbert Xu frag->page_offset += offset - pos; 2224f4c50d99SHerbert Xu frag->size -= offset - pos; 2225f4c50d99SHerbert Xu } 2226f4c50d99SHerbert Xu 2227f4c50d99SHerbert Xu k++; 2228f4c50d99SHerbert Xu 2229f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2230f4c50d99SHerbert Xu i++; 2231f4c50d99SHerbert Xu pos += size; 2232f4c50d99SHerbert Xu } else { 2233f4c50d99SHerbert Xu frag->size -= pos + size - (offset + len); 2234f4c50d99SHerbert Xu break; 2235f4c50d99SHerbert Xu } 2236f4c50d99SHerbert Xu 2237f4c50d99SHerbert Xu frag++; 2238f4c50d99SHerbert Xu } 2239f4c50d99SHerbert Xu 2240f4c50d99SHerbert Xu skb_shinfo(nskb)->nr_frags = k; 2241f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2242f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2243f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2244f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2245f4c50d99SHerbert Xu 2246f4c50d99SHerbert Xu return segs; 2247f4c50d99SHerbert Xu 2248f4c50d99SHerbert Xu err: 2249f4c50d99SHerbert Xu while ((skb = segs)) { 2250f4c50d99SHerbert Xu segs = skb->next; 2251b08d5840SPatrick McHardy kfree_skb(skb); 2252f4c50d99SHerbert Xu } 2253f4c50d99SHerbert Xu return ERR_PTR(err); 2254f4c50d99SHerbert Xu } 2255f4c50d99SHerbert Xu 2256f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2257f4c50d99SHerbert Xu 22581da177e4SLinus Torvalds void __init skb_init(void) 22591da177e4SLinus Torvalds { 22601da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 22611da177e4SLinus Torvalds sizeof(struct sk_buff), 22621da177e4SLinus Torvalds 0, 2263e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 226420c2df83SPaul Mundt NULL); 2265d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2266d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2267d179cd12SDavid S. Miller sizeof(atomic_t), 2268d179cd12SDavid S. Miller 0, 2269e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 227020c2df83SPaul Mundt NULL); 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 2273716ea3a7SDavid Howells /** 2274716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2275716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2276716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2277716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2278716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2279716ea3a7SDavid Howells * 2280716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2281716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2282716ea3a7SDavid Howells */ 228351c739d1SDavid S. Miller static int 228451c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2285716ea3a7SDavid Howells { 22861a028e50SDavid S. Miller int start = skb_headlen(skb); 22871a028e50SDavid S. Miller int i, copy = start - offset; 2288716ea3a7SDavid Howells int elt = 0; 2289716ea3a7SDavid Howells 2290716ea3a7SDavid Howells if (copy > 0) { 2291716ea3a7SDavid Howells if (copy > len) 2292716ea3a7SDavid Howells copy = len; 2293642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 2294716ea3a7SDavid Howells elt++; 2295716ea3a7SDavid Howells if ((len -= copy) == 0) 2296716ea3a7SDavid Howells return elt; 2297716ea3a7SDavid Howells offset += copy; 2298716ea3a7SDavid Howells } 2299716ea3a7SDavid Howells 2300716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 23011a028e50SDavid S. Miller int end; 2302716ea3a7SDavid Howells 23031a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 23041a028e50SDavid S. Miller 23051a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 2306716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2307716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2308716ea3a7SDavid Howells 2309716ea3a7SDavid Howells if (copy > len) 2310716ea3a7SDavid Howells copy = len; 2311642f1490SJens Axboe sg_set_page(&sg[elt], frag->page, copy, 2312642f1490SJens Axboe frag->page_offset+offset-start); 2313716ea3a7SDavid Howells elt++; 2314716ea3a7SDavid Howells if (!(len -= copy)) 2315716ea3a7SDavid Howells return elt; 2316716ea3a7SDavid Howells offset += copy; 2317716ea3a7SDavid Howells } 23181a028e50SDavid S. Miller start = end; 2319716ea3a7SDavid Howells } 2320716ea3a7SDavid Howells 2321716ea3a7SDavid Howells if (skb_shinfo(skb)->frag_list) { 2322716ea3a7SDavid Howells struct sk_buff *list = skb_shinfo(skb)->frag_list; 2323716ea3a7SDavid Howells 2324716ea3a7SDavid Howells for (; list; list = list->next) { 23251a028e50SDavid S. Miller int end; 2326716ea3a7SDavid Howells 23271a028e50SDavid S. Miller BUG_TRAP(start <= offset + len); 23281a028e50SDavid S. Miller 23291a028e50SDavid S. Miller end = start + list->len; 2330716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2331716ea3a7SDavid Howells if (copy > len) 2332716ea3a7SDavid Howells copy = len; 233351c739d1SDavid S. Miller elt += __skb_to_sgvec(list, sg+elt, offset - start, 233451c739d1SDavid S. Miller copy); 2335716ea3a7SDavid Howells if ((len -= copy) == 0) 2336716ea3a7SDavid Howells return elt; 2337716ea3a7SDavid Howells offset += copy; 2338716ea3a7SDavid Howells } 23391a028e50SDavid S. Miller start = end; 2340716ea3a7SDavid Howells } 2341716ea3a7SDavid Howells } 2342716ea3a7SDavid Howells BUG_ON(len); 2343716ea3a7SDavid Howells return elt; 2344716ea3a7SDavid Howells } 2345716ea3a7SDavid Howells 234651c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 234751c739d1SDavid S. Miller { 234851c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 234951c739d1SDavid S. Miller 2350c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 235151c739d1SDavid S. Miller 235251c739d1SDavid S. Miller return nsg; 235351c739d1SDavid S. Miller } 235451c739d1SDavid S. Miller 2355716ea3a7SDavid Howells /** 2356716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 2357716ea3a7SDavid Howells * @skb: The socket buffer to check. 2358716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 2359716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 2360716ea3a7SDavid Howells * 2361716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 2362716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 2363716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 2364716ea3a7SDavid Howells * 2365716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 2366716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 2367716ea3a7SDavid Howells * set to point to the skb in which this space begins. 2368716ea3a7SDavid Howells * 2369716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 2370716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 2371716ea3a7SDavid Howells */ 2372716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2373716ea3a7SDavid Howells { 2374716ea3a7SDavid Howells int copyflag; 2375716ea3a7SDavid Howells int elt; 2376716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 2377716ea3a7SDavid Howells 2378716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 2379716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 2380716ea3a7SDavid Howells * at the moment even if they are anonymous). 2381716ea3a7SDavid Howells */ 2382716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2383716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2384716ea3a7SDavid Howells return -ENOMEM; 2385716ea3a7SDavid Howells 2386716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 2387716ea3a7SDavid Howells if (!skb_shinfo(skb)->frag_list) { 2388716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 2389716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 2390716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 2391716ea3a7SDavid Howells * space, 128 bytes is fair. */ 2392716ea3a7SDavid Howells 2393716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 2394716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2395716ea3a7SDavid Howells return -ENOMEM; 2396716ea3a7SDavid Howells 2397716ea3a7SDavid Howells /* Voila! */ 2398716ea3a7SDavid Howells *trailer = skb; 2399716ea3a7SDavid Howells return 1; 2400716ea3a7SDavid Howells } 2401716ea3a7SDavid Howells 2402716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 2403716ea3a7SDavid Howells 2404716ea3a7SDavid Howells elt = 1; 2405716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 2406716ea3a7SDavid Howells copyflag = 0; 2407716ea3a7SDavid Howells 2408716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 2409716ea3a7SDavid Howells int ntail = 0; 2410716ea3a7SDavid Howells 2411716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 2412716ea3a7SDavid Howells * this can happen on input. Copy it and everything 2413716ea3a7SDavid Howells * after it. */ 2414716ea3a7SDavid Howells 2415716ea3a7SDavid Howells if (skb_shared(skb1)) 2416716ea3a7SDavid Howells copyflag = 1; 2417716ea3a7SDavid Howells 2418716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 2419716ea3a7SDavid Howells 2420716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 2421716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 2422716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list || 2423716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 2424716ea3a7SDavid Howells ntail = tailbits + 128; 2425716ea3a7SDavid Howells } 2426716ea3a7SDavid Howells 2427716ea3a7SDavid Howells if (copyflag || 2428716ea3a7SDavid Howells skb_cloned(skb1) || 2429716ea3a7SDavid Howells ntail || 2430716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 2431716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list) { 2432716ea3a7SDavid Howells struct sk_buff *skb2; 2433716ea3a7SDavid Howells 2434716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 2435716ea3a7SDavid Howells if (ntail == 0) 2436716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 2437716ea3a7SDavid Howells else 2438716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 2439716ea3a7SDavid Howells skb_headroom(skb1), 2440716ea3a7SDavid Howells ntail, 2441716ea3a7SDavid Howells GFP_ATOMIC); 2442716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 2443716ea3a7SDavid Howells return -ENOMEM; 2444716ea3a7SDavid Howells 2445716ea3a7SDavid Howells if (skb1->sk) 2446716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 2447716ea3a7SDavid Howells 2448716ea3a7SDavid Howells /* Looking around. Are we still alive? 2449716ea3a7SDavid Howells * OK, link new skb, drop old one */ 2450716ea3a7SDavid Howells 2451716ea3a7SDavid Howells skb2->next = skb1->next; 2452716ea3a7SDavid Howells *skb_p = skb2; 2453716ea3a7SDavid Howells kfree_skb(skb1); 2454716ea3a7SDavid Howells skb1 = skb2; 2455716ea3a7SDavid Howells } 2456716ea3a7SDavid Howells elt++; 2457716ea3a7SDavid Howells *trailer = skb1; 2458716ea3a7SDavid Howells skb_p = &skb1->next; 2459716ea3a7SDavid Howells } 2460716ea3a7SDavid Howells 2461716ea3a7SDavid Howells return elt; 2462716ea3a7SDavid Howells } 2463716ea3a7SDavid Howells 2464f35d9d8aSRusty Russell /** 2465f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 2466f35d9d8aSRusty Russell * @skb: the skb to set 2467f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 2468f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 2469f35d9d8aSRusty Russell * 2470f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 2471f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 2472f35d9d8aSRusty Russell * 2473f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 2474f35d9d8aSRusty Russell * returns false you should drop the packet. 2475f35d9d8aSRusty Russell */ 2476f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 2477f35d9d8aSRusty Russell { 2478f35d9d8aSRusty Russell if (unlikely(start > skb->len - 2) || 2479f35d9d8aSRusty Russell unlikely((int)start + off > skb->len - 2)) { 2480f35d9d8aSRusty Russell if (net_ratelimit()) 2481f35d9d8aSRusty Russell printk(KERN_WARNING 2482f35d9d8aSRusty Russell "bad partial csum: csum=%u/%u len=%u\n", 2483f35d9d8aSRusty Russell start, off, skb->len); 2484f35d9d8aSRusty Russell return false; 2485f35d9d8aSRusty Russell } 2486f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 2487f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 2488f35d9d8aSRusty Russell skb->csum_offset = off; 2489f35d9d8aSRusty Russell return true; 2490f35d9d8aSRusty Russell } 2491f35d9d8aSRusty Russell 24921da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim); 24931da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb); 2494231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb); 24951da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail); 2496d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 24978af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb); 24981da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy); 24991da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head); 25001da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum); 25011da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone); 25021da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy); 25031da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits); 25041da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev); 25051da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits); 25061da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand); 25071da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic); 25081da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad); 25091da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom); 25101da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic); 25111da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue); 25121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail); 25131da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert); 25141da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge); 25151da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head); 25161da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail); 25171da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink); 25181da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append); 25191da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split); 2520677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read); 2521677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read); 2522677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read); 25233fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text); 2524e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags); 2525716ea3a7SDavid Howells 2526716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec); 2527716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data); 2528f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set); 2529