11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Fixes: 81da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 91da177e4SLinus Torvalds * balancer bugs. 101da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 111da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 121da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 131da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 141da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 151da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 161da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 171da177e4SLinus Torvalds * only put in the headers 181da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 191da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 201da177e4SLinus Torvalds * Andi Kleen : slabified it. 211da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * NOTE: 241da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 251da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 261da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 271da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 301da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 311da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 321da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <linux/module.h> 401da177e4SLinus Torvalds #include <linux/types.h> 411da177e4SLinus Torvalds #include <linux/kernel.h> 421da177e4SLinus Torvalds #include <linux/mm.h> 431da177e4SLinus Torvalds #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/in.h> 451da177e4SLinus Torvalds #include <linux/inet.h> 461da177e4SLinus Torvalds #include <linux/slab.h> 471da177e4SLinus Torvalds #include <linux/netdevice.h> 481da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 491da177e4SLinus Torvalds #include <net/pkt_sched.h> 501da177e4SLinus Torvalds #endif 511da177e4SLinus Torvalds #include <linux/string.h> 521da177e4SLinus Torvalds #include <linux/skbuff.h> 539c55e01cSJens Axboe #include <linux/splice.h> 541da177e4SLinus Torvalds #include <linux/cache.h> 551da177e4SLinus Torvalds #include <linux/rtnetlink.h> 561da177e4SLinus Torvalds #include <linux/init.h> 57716ea3a7SDavid Howells #include <linux/scatterlist.h> 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #include <net/protocol.h> 601da177e4SLinus Torvalds #include <net/dst.h> 611da177e4SLinus Torvalds #include <net/sock.h> 621da177e4SLinus Torvalds #include <net/checksum.h> 631da177e4SLinus Torvalds #include <net/xfrm.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <asm/uaccess.h> 661da177e4SLinus Torvalds #include <asm/system.h> 671da177e4SLinus Torvalds 68a1f8e7f7SAl Viro #include "kmap_skb.h" 69a1f8e7f7SAl Viro 70e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly; 71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 721da177e4SLinus Torvalds 739c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 749c55e01cSJens Axboe struct pipe_buffer *buf) 759c55e01cSJens Axboe { 769c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 779c55e01cSJens Axboe 789c55e01cSJens Axboe kfree_skb(skb); 799c55e01cSJens Axboe } 809c55e01cSJens Axboe 819c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 829c55e01cSJens Axboe struct pipe_buffer *buf) 839c55e01cSJens Axboe { 849c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) buf->private; 859c55e01cSJens Axboe 869c55e01cSJens Axboe skb_get(skb); 879c55e01cSJens Axboe } 889c55e01cSJens Axboe 899c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 909c55e01cSJens Axboe struct pipe_buffer *buf) 919c55e01cSJens Axboe { 929c55e01cSJens Axboe return 1; 939c55e01cSJens Axboe } 949c55e01cSJens Axboe 959c55e01cSJens Axboe 969c55e01cSJens Axboe /* Pipe buffer operations for a socket. */ 979c55e01cSJens Axboe static struct pipe_buf_operations sock_pipe_buf_ops = { 989c55e01cSJens Axboe .can_merge = 0, 999c55e01cSJens Axboe .map = generic_pipe_buf_map, 1009c55e01cSJens Axboe .unmap = generic_pipe_buf_unmap, 1019c55e01cSJens Axboe .confirm = generic_pipe_buf_confirm, 1029c55e01cSJens Axboe .release = sock_pipe_buf_release, 1039c55e01cSJens Axboe .steal = sock_pipe_buf_steal, 1049c55e01cSJens Axboe .get = sock_pipe_buf_get, 1059c55e01cSJens Axboe }; 1069c55e01cSJens Axboe 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 1091da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 1101da177e4SLinus Torvalds * reliable. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /** 1141da177e4SLinus Torvalds * skb_over_panic - private function 1151da177e4SLinus Torvalds * @skb: buffer 1161da177e4SLinus Torvalds * @sz: size 1171da177e4SLinus Torvalds * @here: address 1181da177e4SLinus Torvalds * 1191da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here) 1221da177e4SLinus Torvalds { 12326095455SPatrick McHardy printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 1244305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 12527a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1264305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 12726095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1281da177e4SLinus Torvalds BUG(); 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /** 1321da177e4SLinus Torvalds * skb_under_panic - private function 1331da177e4SLinus Torvalds * @skb: buffer 1341da177e4SLinus Torvalds * @sz: size 1351da177e4SLinus Torvalds * @here: address 1361da177e4SLinus Torvalds * 1371da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1411da177e4SLinus Torvalds { 14226095455SPatrick McHardy printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 1434305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 14427a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1454305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 14626095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1471da177e4SLinus Torvalds BUG(); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 150dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb) 151dc6de336SDavid S. Miller { 152dc6de336SDavid S. Miller printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " 153dc6de336SDavid S. Miller "len=%u, sizeof(sk_buff)=%Zd\n", 154dc6de336SDavid S. Miller skb->truesize, skb->len, sizeof(struct sk_buff)); 155dc6de336SDavid S. Miller } 156dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug); 157dc6de336SDavid S. Miller 1581da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1591da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1601da177e4SLinus Torvalds * [BEEP] leaks. 1611da177e4SLinus Torvalds * 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds /** 165d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1661da177e4SLinus Torvalds * @size: size to allocate 1671da177e4SLinus Torvalds * @gfp_mask: allocation mask 168c83c2486SRandy Dunlap * @fclone: allocate from fclone cache instead of head cache 169c83c2486SRandy Dunlap * and allocate a cloned (child) skb 170b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1711da177e4SLinus Torvalds * 1721da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 1731da177e4SLinus Torvalds * tail room of size bytes. The object has a reference count of one. 1741da177e4SLinus Torvalds * The return is the buffer. On a failure the return is %NULL. 1751da177e4SLinus Torvalds * 1761da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1771da177e4SLinus Torvalds * %GFP_ATOMIC. 1781da177e4SLinus Torvalds */ 179dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 180b30973f8SChristoph Hellwig int fclone, int node) 1811da177e4SLinus Torvalds { 182e18b890bSChristoph Lameter struct kmem_cache *cache; 1834947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1841da177e4SLinus Torvalds struct sk_buff *skb; 1851da177e4SLinus Torvalds u8 *data; 1861da177e4SLinus Torvalds 1878798b3fbSHerbert Xu cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 1888798b3fbSHerbert Xu 1891da177e4SLinus Torvalds /* Get the HEAD */ 190b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1911da177e4SLinus Torvalds if (!skb) 1921da177e4SLinus Torvalds goto out; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 195b30973f8SChristoph Hellwig data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 196b30973f8SChristoph Hellwig gfp_mask, node); 1971da177e4SLinus Torvalds if (!data) 1981da177e4SLinus Torvalds goto nodata; 1991da177e4SLinus Torvalds 200ca0605a7SArnaldo Carvalho de Melo /* 201c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 202c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 203c8005785SJohannes Berg * the tail pointer in struct sk_buff! 204ca0605a7SArnaldo Carvalho de Melo */ 205ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 2061da177e4SLinus Torvalds skb->truesize = size + sizeof(struct sk_buff); 2071da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2081da177e4SLinus Torvalds skb->head = data; 2091da177e4SLinus Torvalds skb->data = data; 21027a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2114305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 2124947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2134947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 2144947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 2154947d3efSBenjamin LaHaise shinfo->nr_frags = 0; 2167967168cSHerbert Xu shinfo->gso_size = 0; 2177967168cSHerbert Xu shinfo->gso_segs = 0; 2187967168cSHerbert Xu shinfo->gso_type = 0; 2194947d3efSBenjamin LaHaise shinfo->ip6_frag_id = 0; 2204947d3efSBenjamin LaHaise shinfo->frag_list = NULL; 2214947d3efSBenjamin LaHaise 222d179cd12SDavid S. Miller if (fclone) { 223d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 224d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 2251da177e4SLinus Torvalds 226d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 227d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 228d179cd12SDavid S. Miller 229d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 230d179cd12SDavid S. Miller } 2311da177e4SLinus Torvalds out: 2321da177e4SLinus Torvalds return skb; 2331da177e4SLinus Torvalds nodata: 2348798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2351da177e4SLinus Torvalds skb = NULL; 2361da177e4SLinus Torvalds goto out; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /** 2408af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 2418af27456SChristoph Hellwig * @dev: network device to receive on 2428af27456SChristoph Hellwig * @length: length to allocate 2438af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 2448af27456SChristoph Hellwig * 2458af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 2468af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 2478af27456SChristoph Hellwig * the headroom they think they need without accounting for the 2488af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 2498af27456SChristoph Hellwig * 2508af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 2518af27456SChristoph Hellwig */ 2528af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 2538af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 2548af27456SChristoph Hellwig { 25543cb76d9SGreg Kroah-Hartman int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 2568af27456SChristoph Hellwig struct sk_buff *skb; 2578af27456SChristoph Hellwig 258b30973f8SChristoph Hellwig skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); 2597b2e497aSChristoph Hellwig if (likely(skb)) { 2608af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 2617b2e497aSChristoph Hellwig skb->dev = dev; 2627b2e497aSChristoph Hellwig } 2638af27456SChristoph Hellwig return skb; 2648af27456SChristoph Hellwig } 2651da177e4SLinus Torvalds 266654bed16SPeter Zijlstra struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask) 267654bed16SPeter Zijlstra { 268654bed16SPeter Zijlstra int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 269654bed16SPeter Zijlstra struct page *page; 270654bed16SPeter Zijlstra 271654bed16SPeter Zijlstra page = alloc_pages_node(node, gfp_mask, 0); 272654bed16SPeter Zijlstra return page; 273654bed16SPeter Zijlstra } 274654bed16SPeter Zijlstra EXPORT_SYMBOL(__netdev_alloc_page); 275654bed16SPeter Zijlstra 276654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 277654bed16SPeter Zijlstra int size) 278654bed16SPeter Zijlstra { 279654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 280654bed16SPeter Zijlstra skb->len += size; 281654bed16SPeter Zijlstra skb->data_len += size; 282654bed16SPeter Zijlstra skb->truesize += size; 283654bed16SPeter Zijlstra } 284654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 285654bed16SPeter Zijlstra 286f58518e6SIlpo Järvinen /** 287f58518e6SIlpo Järvinen * dev_alloc_skb - allocate an skbuff for receiving 288f58518e6SIlpo Järvinen * @length: length to allocate 289f58518e6SIlpo Järvinen * 290f58518e6SIlpo Järvinen * Allocate a new &sk_buff and assign it a usage count of one. The 291f58518e6SIlpo Järvinen * buffer has unspecified headroom built in. Users should allocate 292f58518e6SIlpo Järvinen * the headroom they think they need without accounting for the 293f58518e6SIlpo Järvinen * built in space. The built in space is used for optimisations. 294f58518e6SIlpo Järvinen * 295f58518e6SIlpo Järvinen * %NULL is returned if there is no free memory. Although this function 296f58518e6SIlpo Järvinen * allocates memory it can be called from an interrupt. 297f58518e6SIlpo Järvinen */ 298f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length) 299f58518e6SIlpo Järvinen { 3001483b874SDenys Vlasenko /* 3011483b874SDenys Vlasenko * There is more code here than it seems: 302a0f55e0eSDavid S. Miller * __dev_alloc_skb is an inline 3031483b874SDenys Vlasenko */ 304f58518e6SIlpo Järvinen return __dev_alloc_skb(length, GFP_ATOMIC); 305f58518e6SIlpo Järvinen } 306f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb); 307f58518e6SIlpo Järvinen 30827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 3091da177e4SLinus Torvalds { 31027b437c8SHerbert Xu struct sk_buff *list = *listp; 3111da177e4SLinus Torvalds 31227b437c8SHerbert Xu *listp = NULL; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds do { 3151da177e4SLinus Torvalds struct sk_buff *this = list; 3161da177e4SLinus Torvalds list = list->next; 3171da177e4SLinus Torvalds kfree_skb(this); 3181da177e4SLinus Torvalds } while (list); 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 32127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 32227b437c8SHerbert Xu { 32327b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 32427b437c8SHerbert Xu } 32527b437c8SHerbert Xu 3261da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds struct sk_buff *list; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds for (list = skb_shinfo(skb)->frag_list; list; list = list->next) 3311da177e4SLinus Torvalds skb_get(list); 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3345bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds if (!skb->cloned || 3371da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 3381da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 3391da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 3401da177e4SLinus Torvalds int i; 3411da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 3421da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 3461da177e4SLinus Torvalds skb_drop_fraglist(skb); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds kfree(skb->head); 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* 3531da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 3541da177e4SLinus Torvalds */ 3552d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 3561da177e4SLinus Torvalds { 357d179cd12SDavid S. Miller struct sk_buff *other; 358d179cd12SDavid S. Miller atomic_t *fclone_ref; 359d179cd12SDavid S. Miller 360d179cd12SDavid S. Miller switch (skb->fclone) { 361d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 3621da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 363d179cd12SDavid S. Miller break; 364d179cd12SDavid S. Miller 365d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 366d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 367d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 368d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 369d179cd12SDavid S. Miller break; 370d179cd12SDavid S. Miller 371d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 372d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 373d179cd12SDavid S. Miller other = skb - 1; 374d179cd12SDavid S. Miller 375d179cd12SDavid S. Miller /* The clone portion is available for 376d179cd12SDavid S. Miller * fast-cloning again. 377d179cd12SDavid S. Miller */ 378d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 379d179cd12SDavid S. Miller 380d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 381d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 382d179cd12SDavid S. Miller break; 3833ff50b79SStephen Hemminger } 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 38604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds dst_release(skb->dst); 3891da177e4SLinus Torvalds #ifdef CONFIG_XFRM 3901da177e4SLinus Torvalds secpath_put(skb->sp); 3911da177e4SLinus Torvalds #endif 3921da177e4SLinus Torvalds if (skb->destructor) { 3939c2b3328SStephen Hemminger WARN_ON(in_irq()); 3941da177e4SLinus Torvalds skb->destructor(skb); 3951da177e4SLinus Torvalds } 3969fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 3975f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 3989fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 3999fb9cbb1SYasuyuki Kozakai #endif 4001da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 4011da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 4021da177e4SLinus Torvalds #endif 4031da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 4041da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4051da177e4SLinus Torvalds skb->tc_index = 0; 4061da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 4071da177e4SLinus Torvalds skb->tc_verd = 0; 4081da177e4SLinus Torvalds #endif 4091da177e4SLinus Torvalds #endif 41004a4bb55SLennert Buytenhek } 41104a4bb55SLennert Buytenhek 41204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 41304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 41404a4bb55SLennert Buytenhek { 41504a4bb55SLennert Buytenhek skb_release_head_state(skb); 4162d4baff8SHerbert Xu skb_release_data(skb); 4172d4baff8SHerbert Xu } 4181da177e4SLinus Torvalds 4192d4baff8SHerbert Xu /** 4202d4baff8SHerbert Xu * __kfree_skb - private function 4212d4baff8SHerbert Xu * @skb: buffer 4222d4baff8SHerbert Xu * 4232d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 4242d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 4252d4baff8SHerbert Xu * always call kfree_skb 4262d4baff8SHerbert Xu */ 4272d4baff8SHerbert Xu 4282d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 4292d4baff8SHerbert Xu { 4302d4baff8SHerbert Xu skb_release_all(skb); 4311da177e4SLinus Torvalds kfree_skbmem(skb); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /** 435231d06aeSJörn Engel * kfree_skb - free an sk_buff 436231d06aeSJörn Engel * @skb: buffer to free 437231d06aeSJörn Engel * 438231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 439231d06aeSJörn Engel * hit zero. 440231d06aeSJörn Engel */ 441231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 442231d06aeSJörn Engel { 443231d06aeSJörn Engel if (unlikely(!skb)) 444231d06aeSJörn Engel return; 445231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 446231d06aeSJörn Engel smp_rmb(); 447231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 448231d06aeSJörn Engel return; 449231d06aeSJörn Engel __kfree_skb(skb); 450231d06aeSJörn Engel } 451231d06aeSJörn Engel 45204a4bb55SLennert Buytenhek int skb_recycle_check(struct sk_buff *skb, int skb_size) 45304a4bb55SLennert Buytenhek { 45404a4bb55SLennert Buytenhek struct skb_shared_info *shinfo; 45504a4bb55SLennert Buytenhek 45604a4bb55SLennert Buytenhek if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE) 45704a4bb55SLennert Buytenhek return 0; 45804a4bb55SLennert Buytenhek 45904a4bb55SLennert Buytenhek skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD); 46004a4bb55SLennert Buytenhek if (skb_end_pointer(skb) - skb->head < skb_size) 46104a4bb55SLennert Buytenhek return 0; 46204a4bb55SLennert Buytenhek 46304a4bb55SLennert Buytenhek if (skb_shared(skb) || skb_cloned(skb)) 46404a4bb55SLennert Buytenhek return 0; 46504a4bb55SLennert Buytenhek 46604a4bb55SLennert Buytenhek skb_release_head_state(skb); 46704a4bb55SLennert Buytenhek shinfo = skb_shinfo(skb); 46804a4bb55SLennert Buytenhek atomic_set(&shinfo->dataref, 1); 46904a4bb55SLennert Buytenhek shinfo->nr_frags = 0; 47004a4bb55SLennert Buytenhek shinfo->gso_size = 0; 47104a4bb55SLennert Buytenhek shinfo->gso_segs = 0; 47204a4bb55SLennert Buytenhek shinfo->gso_type = 0; 47304a4bb55SLennert Buytenhek shinfo->ip6_frag_id = 0; 47404a4bb55SLennert Buytenhek shinfo->frag_list = NULL; 47504a4bb55SLennert Buytenhek 47604a4bb55SLennert Buytenhek memset(skb, 0, offsetof(struct sk_buff, tail)); 47704a4bb55SLennert Buytenhek skb_reset_tail_pointer(skb); 47804a4bb55SLennert Buytenhek skb->data = skb->head + NET_SKB_PAD; 47904a4bb55SLennert Buytenhek 48004a4bb55SLennert Buytenhek return 1; 48104a4bb55SLennert Buytenhek } 48204a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check); 48304a4bb55SLennert Buytenhek 484dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 485dec18810SHerbert Xu { 486dec18810SHerbert Xu new->tstamp = old->tstamp; 487dec18810SHerbert Xu new->dev = old->dev; 488dec18810SHerbert Xu new->transport_header = old->transport_header; 489dec18810SHerbert Xu new->network_header = old->network_header; 490dec18810SHerbert Xu new->mac_header = old->mac_header; 491dec18810SHerbert Xu new->dst = dst_clone(old->dst); 492def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 493dec18810SHerbert Xu new->sp = secpath_get(old->sp); 494dec18810SHerbert Xu #endif 495dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 496dec18810SHerbert Xu new->csum_start = old->csum_start; 497dec18810SHerbert Xu new->csum_offset = old->csum_offset; 498dec18810SHerbert Xu new->local_df = old->local_df; 499dec18810SHerbert Xu new->pkt_type = old->pkt_type; 500dec18810SHerbert Xu new->ip_summed = old->ip_summed; 501dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 502dec18810SHerbert Xu new->priority = old->priority; 503dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 504dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 505dec18810SHerbert Xu #endif 506dec18810SHerbert Xu new->protocol = old->protocol; 507dec18810SHerbert Xu new->mark = old->mark; 508dec18810SHerbert Xu __nf_copy(new, old); 509dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 510dec18810SHerbert Xu defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 511dec18810SHerbert Xu new->nf_trace = old->nf_trace; 512dec18810SHerbert Xu #endif 513dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 514dec18810SHerbert Xu new->tc_index = old->tc_index; 515dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 516dec18810SHerbert Xu new->tc_verd = old->tc_verd; 517dec18810SHerbert Xu #endif 518dec18810SHerbert Xu #endif 5196aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 5206aa895b0SPatrick McHardy 521dec18810SHerbert Xu skb_copy_secmark(new, old); 522dec18810SHerbert Xu } 523dec18810SHerbert Xu 524e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds #define C(x) n->x = skb->x 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds n->next = n->prev = NULL; 5291da177e4SLinus Torvalds n->sk = NULL; 530dec18810SHerbert Xu __copy_skb_header(n, skb); 531dec18810SHerbert Xu 5321da177e4SLinus Torvalds C(len); 5331da177e4SLinus Torvalds C(data_len); 5343e6b3b2eSAlexey Dobriyan C(mac_len); 535334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 53602f1c89dSPaul Moore n->cloned = 1; 5371da177e4SLinus Torvalds n->nohdr = 0; 5381da177e4SLinus Torvalds n->destructor = NULL; 53902f1c89dSPaul Moore C(iif); 5401da177e4SLinus Torvalds C(tail); 5411da177e4SLinus Torvalds C(end); 54202f1c89dSPaul Moore C(head); 54302f1c89dSPaul Moore C(data); 54402f1c89dSPaul Moore C(truesize); 545d0f09804SJohannes Berg #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE) 546d0f09804SJohannes Berg C(do_not_encrypt); 5478b30b1feSSujith C(requeue); 548d0f09804SJohannes Berg #endif 54902f1c89dSPaul Moore atomic_set(&n->users, 1); 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 5521da177e4SLinus Torvalds skb->cloned = 1; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds return n; 555e0053ec0SHerbert Xu #undef C 556e0053ec0SHerbert Xu } 557e0053ec0SHerbert Xu 558e0053ec0SHerbert Xu /** 559e0053ec0SHerbert Xu * skb_morph - morph one skb into another 560e0053ec0SHerbert Xu * @dst: the skb to receive the contents 561e0053ec0SHerbert Xu * @src: the skb to supply the contents 562e0053ec0SHerbert Xu * 563e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 564e0053ec0SHerbert Xu * supplied by the user. 565e0053ec0SHerbert Xu * 566e0053ec0SHerbert Xu * The target skb is returned upon exit. 567e0053ec0SHerbert Xu */ 568e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 569e0053ec0SHerbert Xu { 5702d4baff8SHerbert Xu skb_release_all(dst); 571e0053ec0SHerbert Xu return __skb_clone(dst, src); 572e0053ec0SHerbert Xu } 573e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 574e0053ec0SHerbert Xu 575e0053ec0SHerbert Xu /** 576e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 577e0053ec0SHerbert Xu * @skb: buffer to clone 578e0053ec0SHerbert Xu * @gfp_mask: allocation priority 579e0053ec0SHerbert Xu * 580e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 581e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 582e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 583e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 584e0053ec0SHerbert Xu * 585e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 586e0053ec0SHerbert Xu * %GFP_ATOMIC. 587e0053ec0SHerbert Xu */ 588e0053ec0SHerbert Xu 589e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 590e0053ec0SHerbert Xu { 591e0053ec0SHerbert Xu struct sk_buff *n; 592e0053ec0SHerbert Xu 593e0053ec0SHerbert Xu n = skb + 1; 594e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 595e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 596e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 597e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 598e0053ec0SHerbert Xu atomic_inc(fclone_ref); 599e0053ec0SHerbert Xu } else { 600e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 601e0053ec0SHerbert Xu if (!n) 602e0053ec0SHerbert Xu return NULL; 603e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 604e0053ec0SHerbert Xu } 605e0053ec0SHerbert Xu 606e0053ec0SHerbert Xu return __skb_clone(n, skb); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 6101da177e4SLinus Torvalds { 6112e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 6121da177e4SLinus Torvalds /* 6131da177e4SLinus Torvalds * Shift between the two data areas in bytes 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 6162e07fa9cSArnaldo Carvalho de Melo #endif 617dec18810SHerbert Xu 618dec18810SHerbert Xu __copy_skb_header(new, old); 619dec18810SHerbert Xu 6202e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 6212e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 6222e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 6232e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 6242e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 6252e07fa9cSArnaldo Carvalho de Melo #endif 6267967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 6277967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 6287967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /** 6321da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 6331da177e4SLinus Torvalds * @skb: buffer to copy 6341da177e4SLinus Torvalds * @gfp_mask: allocation priority 6351da177e4SLinus Torvalds * 6361da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 6371da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 6381da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 6391da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 6401da177e4SLinus Torvalds * 6411da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 6421da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 6431da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 6441da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 6451da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 6461da177e4SLinus Torvalds */ 6471da177e4SLinus Torvalds 648dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds int headerlen = skb->data - skb->head; 6511da177e4SLinus Torvalds /* 6521da177e4SLinus Torvalds * Allocate the copy buffer 6531da177e4SLinus Torvalds */ 6544305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6554305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6564305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end + skb->data_len, gfp_mask); 6574305b541SArnaldo Carvalho de Melo #else 6584305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 6594305b541SArnaldo Carvalho de Melo #endif 6601da177e4SLinus Torvalds if (!n) 6611da177e4SLinus Torvalds return NULL; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds /* Set the data pointer */ 6641da177e4SLinus Torvalds skb_reserve(n, headerlen); 6651da177e4SLinus Torvalds /* Set the tail pointer and length */ 6661da177e4SLinus Torvalds skb_put(n, skb->len); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 6691da177e4SLinus Torvalds BUG(); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds copy_skb_header(n, skb); 6721da177e4SLinus Torvalds return n; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /** 6771da177e4SLinus Torvalds * pskb_copy - create copy of an sk_buff with private head. 6781da177e4SLinus Torvalds * @skb: buffer to copy 6791da177e4SLinus Torvalds * @gfp_mask: allocation priority 6801da177e4SLinus Torvalds * 6811da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 6821da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 6831da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 6841da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 6851da177e4SLinus Torvalds * or the pointer to the buffer on success. 6861da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds 689dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds /* 6921da177e4SLinus Torvalds * Allocate the copy buffer 6931da177e4SLinus Torvalds */ 6944305b541SArnaldo Carvalho de Melo struct sk_buff *n; 6954305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6964305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end, gfp_mask); 6974305b541SArnaldo Carvalho de Melo #else 6984305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head, gfp_mask); 6994305b541SArnaldo Carvalho de Melo #endif 7001da177e4SLinus Torvalds if (!n) 7011da177e4SLinus Torvalds goto out; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds /* Set the data pointer */ 7041da177e4SLinus Torvalds skb_reserve(n, skb->data - skb->head); 7051da177e4SLinus Torvalds /* Set the tail pointer and length */ 7061da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 7071da177e4SLinus Torvalds /* Copy the bytes */ 708d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 7091da177e4SLinus Torvalds 71025f484a6SHerbert Xu n->truesize += skb->data_len; 7111da177e4SLinus Torvalds n->data_len = skb->data_len; 7121da177e4SLinus Torvalds n->len = skb->len; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 7151da177e4SLinus Torvalds int i; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 7181da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 7191da177e4SLinus Torvalds get_page(skb_shinfo(n)->frags[i].page); 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 7251da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 7261da177e4SLinus Torvalds skb_clone_fraglist(n); 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds copy_skb_header(n, skb); 7301da177e4SLinus Torvalds out: 7311da177e4SLinus Torvalds return n; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /** 7351da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 7361da177e4SLinus Torvalds * @skb: buffer to reallocate 7371da177e4SLinus Torvalds * @nhead: room to add at head 7381da177e4SLinus Torvalds * @ntail: room to add at tail 7391da177e4SLinus Torvalds * @gfp_mask: allocation priority 7401da177e4SLinus Torvalds * 7411da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 7421da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 7431da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 7441da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 7451da177e4SLinus Torvalds * 7461da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 7471da177e4SLinus Torvalds * reloaded after call to this function. 7481da177e4SLinus Torvalds */ 7491da177e4SLinus Torvalds 75086a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 751dd0fc66fSAl Viro gfp_t gfp_mask) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds int i; 7541da177e4SLinus Torvalds u8 *data; 7554305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7564305b541SArnaldo Carvalho de Melo int size = nhead + skb->end + ntail; 7574305b541SArnaldo Carvalho de Melo #else 7581da177e4SLinus Torvalds int size = nhead + (skb->end - skb->head) + ntail; 7594305b541SArnaldo Carvalho de Melo #endif 7601da177e4SLinus Torvalds long off; 7611da177e4SLinus Torvalds 7624edd87adSHerbert Xu BUG_ON(nhead < 0); 7634edd87adSHerbert Xu 7641da177e4SLinus Torvalds if (skb_shared(skb)) 7651da177e4SLinus Torvalds BUG(); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 7701da177e4SLinus Torvalds if (!data) 7711da177e4SLinus Torvalds goto nodata; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 7741da177e4SLinus Torvalds * optimized for the cases when header is void. */ 7754305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 776b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail); 7774305b541SArnaldo Carvalho de Melo #else 778b6ccc67dSMikael Pettersson memcpy(data + nhead, skb->head, skb->tail - skb->head); 77927a884dcSArnaldo Carvalho de Melo #endif 7804305b541SArnaldo Carvalho de Melo memcpy(data + size, skb_end_pointer(skb), 7814305b541SArnaldo Carvalho de Melo sizeof(struct skb_shared_info)); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 7841da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 7871da177e4SLinus Torvalds skb_clone_fraglist(skb); 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds skb_release_data(skb); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds off = (data + nhead) - skb->head; 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds skb->head = data; 7941da177e4SLinus Torvalds skb->data += off; 7954305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 7964305b541SArnaldo Carvalho de Melo skb->end = size; 79756eb8882SPatrick McHardy off = nhead; 7984305b541SArnaldo Carvalho de Melo #else 7994305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 80056eb8882SPatrick McHardy #endif 80127a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 80227a884dcSArnaldo Carvalho de Melo skb->tail += off; 803b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 804b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 805b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 806172a863fSHerbert Xu skb->csum_start += nhead; 8071da177e4SLinus Torvalds skb->cloned = 0; 808334a8132SPatrick McHardy skb->hdr_len = 0; 8091da177e4SLinus Torvalds skb->nohdr = 0; 8101da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 8111da177e4SLinus Torvalds return 0; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds nodata: 8141da177e4SLinus Torvalds return -ENOMEM; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct sk_buff *skb2; 8221da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (delta <= 0) 8251da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 8261da177e4SLinus Torvalds else { 8271da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 8281da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 8291da177e4SLinus Torvalds GFP_ATOMIC)) { 8301da177e4SLinus Torvalds kfree_skb(skb2); 8311da177e4SLinus Torvalds skb2 = NULL; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds return skb2; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds /** 8391da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 8401da177e4SLinus Torvalds * @skb: buffer to copy 8411da177e4SLinus Torvalds * @newheadroom: new free bytes at head 8421da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 8431da177e4SLinus Torvalds * @gfp_mask: allocation priority 8441da177e4SLinus Torvalds * 8451da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 8461da177e4SLinus Torvalds * allocate additional space. 8471da177e4SLinus Torvalds * 8481da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 8491da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 8501da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 8511da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 8521da177e4SLinus Torvalds * 8531da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 8541da177e4SLinus Torvalds * is called from an interrupt. 8551da177e4SLinus Torvalds */ 8561da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 85786a76cafSVictor Fusco int newheadroom, int newtailroom, 858dd0fc66fSAl Viro gfp_t gfp_mask) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds /* 8611da177e4SLinus Torvalds * Allocate the copy buffer 8621da177e4SLinus Torvalds */ 8631da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 8641da177e4SLinus Torvalds gfp_mask); 865efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 8661da177e4SLinus Torvalds int head_copy_len, head_copy_off; 86752886051SHerbert Xu int off; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds if (!n) 8701da177e4SLinus Torvalds return NULL; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds skb_reserve(n, newheadroom); 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds /* Set the tail pointer and length */ 8751da177e4SLinus Torvalds skb_put(n, skb->len); 8761da177e4SLinus Torvalds 877efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 8781da177e4SLinus Torvalds head_copy_off = 0; 8791da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 8801da177e4SLinus Torvalds head_copy_len = newheadroom; 8811da177e4SLinus Torvalds else 8821da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* Copy the linear header and data. */ 8851da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 8861da177e4SLinus Torvalds skb->len + head_copy_len)) 8871da177e4SLinus Torvalds BUG(); 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds copy_skb_header(n, skb); 8901da177e4SLinus Torvalds 891efd1e8d5SPatrick McHardy off = newheadroom - oldheadroom; 89252886051SHerbert Xu n->csum_start += off; 89352886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET 894efd1e8d5SPatrick McHardy n->transport_header += off; 895efd1e8d5SPatrick McHardy n->network_header += off; 896efd1e8d5SPatrick McHardy n->mac_header += off; 89752886051SHerbert Xu #endif 898efd1e8d5SPatrick McHardy 8991da177e4SLinus Torvalds return n; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /** 9031da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 9041da177e4SLinus Torvalds * @skb: buffer to pad 9051da177e4SLinus Torvalds * @pad: space to pad 9061da177e4SLinus Torvalds * 9071da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 9081da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 9091da177e4SLinus Torvalds * beyond the buffer end onto the wire. 9101da177e4SLinus Torvalds * 9115b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 9121da177e4SLinus Torvalds */ 9131da177e4SLinus Torvalds 9145b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 9151da177e4SLinus Torvalds { 9165b057c6bSHerbert Xu int err; 9175b057c6bSHerbert Xu int ntail; 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 9205b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 9211da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 9225b057c6bSHerbert Xu return 0; 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds 9254305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 9265b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 9275b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 9285b057c6bSHerbert Xu if (unlikely(err)) 9295b057c6bSHerbert Xu goto free_skb; 9305b057c6bSHerbert Xu } 9315b057c6bSHerbert Xu 9325b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 9335b057c6bSHerbert Xu * to be audited. 9345b057c6bSHerbert Xu */ 9355b057c6bSHerbert Xu err = skb_linearize(skb); 9365b057c6bSHerbert Xu if (unlikely(err)) 9375b057c6bSHerbert Xu goto free_skb; 9385b057c6bSHerbert Xu 9395b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 9405b057c6bSHerbert Xu return 0; 9415b057c6bSHerbert Xu 9425b057c6bSHerbert Xu free_skb: 9431da177e4SLinus Torvalds kfree_skb(skb); 9445b057c6bSHerbert Xu return err; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9470dde3e16SIlpo Järvinen /** 9480dde3e16SIlpo Järvinen * skb_put - add data to a buffer 9490dde3e16SIlpo Järvinen * @skb: buffer to use 9500dde3e16SIlpo Järvinen * @len: amount of data to add 9510dde3e16SIlpo Järvinen * 9520dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 9530dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 9540dde3e16SIlpo Järvinen * first byte of the extra data is returned. 9550dde3e16SIlpo Järvinen */ 9560dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 9570dde3e16SIlpo Järvinen { 9580dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 9590dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 9600dde3e16SIlpo Järvinen skb->tail += len; 9610dde3e16SIlpo Järvinen skb->len += len; 9620dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 9630dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 9640dde3e16SIlpo Järvinen return tmp; 9650dde3e16SIlpo Järvinen } 9660dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 9670dde3e16SIlpo Järvinen 9686be8ac2fSIlpo Järvinen /** 969c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 970c2aa270aSIlpo Järvinen * @skb: buffer to use 971c2aa270aSIlpo Järvinen * @len: amount of data to add 972c2aa270aSIlpo Järvinen * 973c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 974c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 975c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 976c2aa270aSIlpo Järvinen */ 977c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 978c2aa270aSIlpo Järvinen { 979c2aa270aSIlpo Järvinen skb->data -= len; 980c2aa270aSIlpo Järvinen skb->len += len; 981c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 982c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 983c2aa270aSIlpo Järvinen return skb->data; 984c2aa270aSIlpo Järvinen } 985c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 986c2aa270aSIlpo Järvinen 987c2aa270aSIlpo Järvinen /** 9886be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 9896be8ac2fSIlpo Järvinen * @skb: buffer to use 9906be8ac2fSIlpo Järvinen * @len: amount of data to remove 9916be8ac2fSIlpo Järvinen * 9926be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 9936be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 9946be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 9956be8ac2fSIlpo Järvinen * the old data. 9966be8ac2fSIlpo Järvinen */ 9976be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 9986be8ac2fSIlpo Järvinen { 9996be8ac2fSIlpo Järvinen return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 10006be8ac2fSIlpo Järvinen } 10016be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 10026be8ac2fSIlpo Järvinen 1003419ae74eSIlpo Järvinen /** 1004419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1005419ae74eSIlpo Järvinen * @skb: buffer to alter 1006419ae74eSIlpo Järvinen * @len: new length 1007419ae74eSIlpo Järvinen * 1008419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1009419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1010419ae74eSIlpo Järvinen * The skb must be linear. 1011419ae74eSIlpo Järvinen */ 1012419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1013419ae74eSIlpo Järvinen { 1014419ae74eSIlpo Järvinen if (skb->len > len) 1015419ae74eSIlpo Järvinen __skb_trim(skb, len); 1016419ae74eSIlpo Järvinen } 1017419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1018419ae74eSIlpo Järvinen 10193cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 10201da177e4SLinus Torvalds */ 10211da177e4SLinus Torvalds 10223cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 10231da177e4SLinus Torvalds { 102427b437c8SHerbert Xu struct sk_buff **fragp; 102527b437c8SHerbert Xu struct sk_buff *frag; 10261da177e4SLinus Torvalds int offset = skb_headlen(skb); 10271da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 10281da177e4SLinus Torvalds int i; 102927b437c8SHerbert Xu int err; 103027b437c8SHerbert Xu 103127b437c8SHerbert Xu if (skb_cloned(skb) && 103227b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 103327b437c8SHerbert Xu return err; 10341da177e4SLinus Torvalds 1035f4d26fb3SHerbert Xu i = 0; 1036f4d26fb3SHerbert Xu if (offset >= len) 1037f4d26fb3SHerbert Xu goto drop_pages; 1038f4d26fb3SHerbert Xu 1039f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 10401da177e4SLinus Torvalds int end = offset + skb_shinfo(skb)->frags[i].size; 104127b437c8SHerbert Xu 104227b437c8SHerbert Xu if (end < len) { 10431da177e4SLinus Torvalds offset = end; 104427b437c8SHerbert Xu continue; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 104727b437c8SHerbert Xu skb_shinfo(skb)->frags[i++].size = len - offset; 104827b437c8SHerbert Xu 1049f4d26fb3SHerbert Xu drop_pages: 105027b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 105127b437c8SHerbert Xu 105227b437c8SHerbert Xu for (; i < nfrags; i++) 105327b437c8SHerbert Xu put_page(skb_shinfo(skb)->frags[i].page); 105427b437c8SHerbert Xu 105527b437c8SHerbert Xu if (skb_shinfo(skb)->frag_list) 105627b437c8SHerbert Xu skb_drop_fraglist(skb); 1057f4d26fb3SHerbert Xu goto done; 105827b437c8SHerbert Xu } 105927b437c8SHerbert Xu 106027b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 106127b437c8SHerbert Xu fragp = &frag->next) { 106227b437c8SHerbert Xu int end = offset + frag->len; 106327b437c8SHerbert Xu 106427b437c8SHerbert Xu if (skb_shared(frag)) { 106527b437c8SHerbert Xu struct sk_buff *nfrag; 106627b437c8SHerbert Xu 106727b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 106827b437c8SHerbert Xu if (unlikely(!nfrag)) 106927b437c8SHerbert Xu return -ENOMEM; 107027b437c8SHerbert Xu 107127b437c8SHerbert Xu nfrag->next = frag->next; 1072f4d26fb3SHerbert Xu kfree_skb(frag); 107327b437c8SHerbert Xu frag = nfrag; 107427b437c8SHerbert Xu *fragp = frag; 107527b437c8SHerbert Xu } 107627b437c8SHerbert Xu 107727b437c8SHerbert Xu if (end < len) { 107827b437c8SHerbert Xu offset = end; 107927b437c8SHerbert Xu continue; 108027b437c8SHerbert Xu } 108127b437c8SHerbert Xu 108227b437c8SHerbert Xu if (end > len && 108327b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 108427b437c8SHerbert Xu return err; 108527b437c8SHerbert Xu 108627b437c8SHerbert Xu if (frag->next) 108727b437c8SHerbert Xu skb_drop_list(&frag->next); 108827b437c8SHerbert Xu break; 108927b437c8SHerbert Xu } 109027b437c8SHerbert Xu 1091f4d26fb3SHerbert Xu done: 109227b437c8SHerbert Xu if (len > skb_headlen(skb)) { 10931da177e4SLinus Torvalds skb->data_len -= skb->len - len; 10941da177e4SLinus Torvalds skb->len = len; 10951da177e4SLinus Torvalds } else { 10961da177e4SLinus Torvalds skb->len = len; 10971da177e4SLinus Torvalds skb->data_len = 0; 109827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds return 0; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds /** 11051da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 11061da177e4SLinus Torvalds * @skb: buffer to reallocate 11071da177e4SLinus Torvalds * @delta: number of bytes to advance tail 11081da177e4SLinus Torvalds * 11091da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 11101da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 11111da177e4SLinus Torvalds * data from fragmented part. 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 11141da177e4SLinus Torvalds * 11151da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 11161da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 11171da177e4SLinus Torvalds * 11181da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11191da177e4SLinus Torvalds * reloaded after call to this function. 11201da177e4SLinus Torvalds */ 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 11231da177e4SLinus Torvalds * when it is necessary. 11241da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 11251da177e4SLinus Torvalds * 2. It may change skb pointers. 11261da177e4SLinus Torvalds * 11271da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 11281da177e4SLinus Torvalds */ 11291da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 11301da177e4SLinus Torvalds { 11311da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 11321da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 11331da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 11341da177e4SLinus Torvalds */ 11354305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 11381da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 11391da177e4SLinus Torvalds GFP_ATOMIC)) 11401da177e4SLinus Torvalds return NULL; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 114327a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 11441da177e4SLinus Torvalds BUG(); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 11471da177e4SLinus Torvalds * size of pulled pages. Superb. 11481da177e4SLinus Torvalds */ 11491da177e4SLinus Torvalds if (!skb_shinfo(skb)->frag_list) 11501da177e4SLinus Torvalds goto pull_pages; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 11531da177e4SLinus Torvalds eat = delta; 11541da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11551da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size >= eat) 11561da177e4SLinus Torvalds goto pull_pages; 11571da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 11611da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 11621da177e4SLinus Torvalds * but taking into account that pulling is expected to 11631da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 11641da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 11651da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 11661da177e4SLinus Torvalds */ 11671da177e4SLinus Torvalds if (eat) { 11681da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 11691da177e4SLinus Torvalds struct sk_buff *clone = NULL; 11701da177e4SLinus Torvalds struct sk_buff *insp = NULL; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds do { 117309a62660SKris Katterjohn BUG_ON(!list); 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds if (list->len <= eat) { 11761da177e4SLinus Torvalds /* Eaten as whole. */ 11771da177e4SLinus Torvalds eat -= list->len; 11781da177e4SLinus Torvalds list = list->next; 11791da177e4SLinus Torvalds insp = list; 11801da177e4SLinus Torvalds } else { 11811da177e4SLinus Torvalds /* Eaten partially. */ 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds if (skb_shared(list)) { 11841da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 11851da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 11861da177e4SLinus Torvalds if (!clone) 11871da177e4SLinus Torvalds return NULL; 11881da177e4SLinus Torvalds insp = list->next; 11891da177e4SLinus Torvalds list = clone; 11901da177e4SLinus Torvalds } else { 11911da177e4SLinus Torvalds /* This may be pulled without 11921da177e4SLinus Torvalds * problems. */ 11931da177e4SLinus Torvalds insp = list; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 11961da177e4SLinus Torvalds if (clone) 11971da177e4SLinus Torvalds kfree_skb(clone); 11981da177e4SLinus Torvalds return NULL; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds break; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds } while (eat); 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds /* Free pulled out fragments. */ 12051da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 12061da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 12071da177e4SLinus Torvalds kfree_skb(list); 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds /* And insert new clone at head. */ 12101da177e4SLinus Torvalds if (clone) { 12111da177e4SLinus Torvalds clone->next = list; 12121da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds pull_pages: 12181da177e4SLinus Torvalds eat = delta; 12191da177e4SLinus Torvalds k = 0; 12201da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12211da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size <= eat) { 12221da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 12231da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 12241da177e4SLinus Torvalds } else { 12251da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 12261da177e4SLinus Torvalds if (eat) { 12271da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 12281da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].size -= eat; 12291da177e4SLinus Torvalds eat = 0; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds k++; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds skb->tail += delta; 12371da177e4SLinus Torvalds skb->data_len -= delta; 12381da177e4SLinus Torvalds 123927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */ 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 12451da177e4SLinus Torvalds { 12461da177e4SLinus Torvalds int i, copy; 12471a028e50SDavid S. Miller int start = skb_headlen(skb); 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (offset > (int)skb->len - len) 12501da177e4SLinus Torvalds goto fault; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds /* Copy header. */ 12531a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 12541da177e4SLinus Torvalds if (copy > len) 12551da177e4SLinus Torvalds copy = len; 1256d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 12571da177e4SLinus Torvalds if ((len -= copy) == 0) 12581da177e4SLinus Torvalds return 0; 12591da177e4SLinus Torvalds offset += copy; 12601da177e4SLinus Torvalds to += copy; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12641a028e50SDavid S. Miller int end; 12651da177e4SLinus Torvalds 1266547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12671a028e50SDavid S. Miller 12681a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 12691da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12701da177e4SLinus Torvalds u8 *vaddr; 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds if (copy > len) 12731da177e4SLinus Torvalds copy = len; 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 12761da177e4SLinus Torvalds memcpy(to, 12771a028e50SDavid S. Miller vaddr + skb_shinfo(skb)->frags[i].page_offset+ 12781a028e50SDavid S. Miller offset - start, copy); 12791da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if ((len -= copy) == 0) 12821da177e4SLinus Torvalds return 0; 12831da177e4SLinus Torvalds offset += copy; 12841da177e4SLinus Torvalds to += copy; 12851da177e4SLinus Torvalds } 12861a028e50SDavid S. Miller start = end; 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 12901da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds for (; list; list = list->next) { 12931a028e50SDavid S. Miller int end; 12941da177e4SLinus Torvalds 1295547b792cSIlpo Järvinen WARN_ON(start > offset + len); 12961a028e50SDavid S. Miller 12971a028e50SDavid S. Miller end = start + list->len; 12981da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12991da177e4SLinus Torvalds if (copy > len) 13001da177e4SLinus Torvalds copy = len; 13011a028e50SDavid S. Miller if (skb_copy_bits(list, offset - start, 13021a028e50SDavid S. Miller to, copy)) 13031da177e4SLinus Torvalds goto fault; 13041da177e4SLinus Torvalds if ((len -= copy) == 0) 13051da177e4SLinus Torvalds return 0; 13061da177e4SLinus Torvalds offset += copy; 13071da177e4SLinus Torvalds to += copy; 13081da177e4SLinus Torvalds } 13091a028e50SDavid S. Miller start = end; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds if (!len) 13131da177e4SLinus Torvalds return 0; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds fault: 13161da177e4SLinus Torvalds return -EFAULT; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13199c55e01cSJens Axboe /* 13209c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 13219c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 13229c55e01cSJens Axboe */ 13239c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 13249c55e01cSJens Axboe { 13259c55e01cSJens Axboe struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; 13269c55e01cSJens Axboe 13279c55e01cSJens Axboe kfree_skb(skb); 13289c55e01cSJens Axboe } 13299c55e01cSJens Axboe 13309c55e01cSJens Axboe /* 13319c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 13329c55e01cSJens Axboe */ 13339c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, 13349c55e01cSJens Axboe unsigned int len, unsigned int offset, 13359c55e01cSJens Axboe struct sk_buff *skb) 13369c55e01cSJens Axboe { 13379c55e01cSJens Axboe if (unlikely(spd->nr_pages == PIPE_BUFFERS)) 13389c55e01cSJens Axboe return 1; 13399c55e01cSJens Axboe 13409c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 13419c55e01cSJens Axboe spd->partial[spd->nr_pages].len = len; 13429c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 13439c55e01cSJens Axboe spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); 13449c55e01cSJens Axboe spd->nr_pages++; 13459c55e01cSJens Axboe return 0; 13469c55e01cSJens Axboe } 13479c55e01cSJens Axboe 13482870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff, 13492870c43dSOctavian Purdila unsigned int *plen, unsigned int off) 13502870c43dSOctavian Purdila { 13512870c43dSOctavian Purdila *poff += off; 13522870c43dSOctavian Purdila *page += *poff / PAGE_SIZE; 13532870c43dSOctavian Purdila *poff = *poff % PAGE_SIZE; 13542870c43dSOctavian Purdila *plen -= off; 13552870c43dSOctavian Purdila } 13562870c43dSOctavian Purdila 13572870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff, 13582870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 13592870c43dSOctavian Purdila unsigned int *len, struct sk_buff *skb, 13609c55e01cSJens Axboe struct splice_pipe_desc *spd) 13619c55e01cSJens Axboe { 13622870c43dSOctavian Purdila if (!*len) 13632870c43dSOctavian Purdila return 1; 13649c55e01cSJens Axboe 13652870c43dSOctavian Purdila /* skip this segment if already processed */ 13662870c43dSOctavian Purdila if (*off >= plen) { 13672870c43dSOctavian Purdila *off -= plen; 13682870c43dSOctavian Purdila return 0; 13692870c43dSOctavian Purdila } 13702870c43dSOctavian Purdila 13712870c43dSOctavian Purdila /* ignore any bits we already processed */ 13722870c43dSOctavian Purdila if (*off) { 13732870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, *off); 13742870c43dSOctavian Purdila *off = 0; 13752870c43dSOctavian Purdila } 13762870c43dSOctavian Purdila 13772870c43dSOctavian Purdila do { 13782870c43dSOctavian Purdila unsigned int flen = min(*len, plen); 13792870c43dSOctavian Purdila 13802870c43dSOctavian Purdila /* the linear region may spread across several pages */ 13812870c43dSOctavian Purdila flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 13822870c43dSOctavian Purdila 13832870c43dSOctavian Purdila if (spd_fill_page(spd, page, flen, poff, skb)) 13842870c43dSOctavian Purdila return 1; 13852870c43dSOctavian Purdila 13862870c43dSOctavian Purdila __segment_seek(&page, &poff, &plen, flen); 13872870c43dSOctavian Purdila *len -= flen; 13882870c43dSOctavian Purdila 13892870c43dSOctavian Purdila } while (*len && plen); 13902870c43dSOctavian Purdila 13912870c43dSOctavian Purdila return 0; 1392db43a282SOctavian Purdila } 13939c55e01cSJens Axboe 13949c55e01cSJens Axboe /* 13952870c43dSOctavian Purdila * Map linear and fragment data from the skb to spd. It reports failure if the 13962870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 13979c55e01cSJens Axboe */ 13987b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, 13992870c43dSOctavian Purdila unsigned int *len, 14002870c43dSOctavian Purdila struct splice_pipe_desc *spd) 14012870c43dSOctavian Purdila { 14022870c43dSOctavian Purdila int seg; 14039c55e01cSJens Axboe 14049c55e01cSJens Axboe /* 14052870c43dSOctavian Purdila * map the linear part 14069c55e01cSJens Axboe */ 14072870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 14082870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 14092870c43dSOctavian Purdila skb_headlen(skb), 14102870c43dSOctavian Purdila offset, len, skb, spd)) 14112870c43dSOctavian Purdila return 1; 14129c55e01cSJens Axboe 14139c55e01cSJens Axboe /* 14149c55e01cSJens Axboe * then map the fragments 14159c55e01cSJens Axboe */ 14169c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 14179c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 14189c55e01cSJens Axboe 14192870c43dSOctavian Purdila if (__splice_segment(f->page, f->page_offset, f->size, 14202870c43dSOctavian Purdila offset, len, skb, spd)) 14212870c43dSOctavian Purdila return 1; 14229c55e01cSJens Axboe } 14239c55e01cSJens Axboe 14249c55e01cSJens Axboe return 0; 14259c55e01cSJens Axboe } 14269c55e01cSJens Axboe 14279c55e01cSJens Axboe /* 14289c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 14299c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 14309c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 14319c55e01cSJens Axboe * handle that cleanly. 14329c55e01cSJens Axboe */ 14339c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, 14349c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 14359c55e01cSJens Axboe unsigned int flags) 14369c55e01cSJens Axboe { 14379c55e01cSJens Axboe struct partial_page partial[PIPE_BUFFERS]; 14389c55e01cSJens Axboe struct page *pages[PIPE_BUFFERS]; 14399c55e01cSJens Axboe struct splice_pipe_desc spd = { 14409c55e01cSJens Axboe .pages = pages, 14419c55e01cSJens Axboe .partial = partial, 14429c55e01cSJens Axboe .flags = flags, 14439c55e01cSJens Axboe .ops = &sock_pipe_buf_ops, 14449c55e01cSJens Axboe .spd_release = sock_spd_release, 14459c55e01cSJens Axboe }; 14469c55e01cSJens Axboe struct sk_buff *skb; 14479c55e01cSJens Axboe 14489c55e01cSJens Axboe /* 14499c55e01cSJens Axboe * I'd love to avoid the clone here, but tcp_read_sock() 14509c55e01cSJens Axboe * ignores reference counts and unconditonally kills the sk_buff 14519c55e01cSJens Axboe * on return from the actor. 14529c55e01cSJens Axboe */ 14539c55e01cSJens Axboe skb = skb_clone(__skb, GFP_KERNEL); 14549c55e01cSJens Axboe if (unlikely(!skb)) 14559c55e01cSJens Axboe return -ENOMEM; 14569c55e01cSJens Axboe 14579c55e01cSJens Axboe /* 14589c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 14599c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 14609c55e01cSJens Axboe */ 14619c55e01cSJens Axboe if (__skb_splice_bits(skb, &offset, &tlen, &spd)) 14629c55e01cSJens Axboe goto done; 14639c55e01cSJens Axboe else if (!tlen) 14649c55e01cSJens Axboe goto done; 14659c55e01cSJens Axboe 14669c55e01cSJens Axboe /* 14679c55e01cSJens Axboe * now see if we have a frag_list to map 14689c55e01cSJens Axboe */ 14699c55e01cSJens Axboe if (skb_shinfo(skb)->frag_list) { 14709c55e01cSJens Axboe struct sk_buff *list = skb_shinfo(skb)->frag_list; 14719c55e01cSJens Axboe 14729c55e01cSJens Axboe for (; list && tlen; list = list->next) { 14739c55e01cSJens Axboe if (__skb_splice_bits(list, &offset, &tlen, &spd)) 14749c55e01cSJens Axboe break; 14759c55e01cSJens Axboe } 14769c55e01cSJens Axboe } 14779c55e01cSJens Axboe 14789c55e01cSJens Axboe done: 14799c55e01cSJens Axboe /* 14809c55e01cSJens Axboe * drop our reference to the clone, the pipe consumption will 14819c55e01cSJens Axboe * drop the rest. 14829c55e01cSJens Axboe */ 14839c55e01cSJens Axboe kfree_skb(skb); 14849c55e01cSJens Axboe 14859c55e01cSJens Axboe if (spd.nr_pages) { 14869c55e01cSJens Axboe int ret; 1487293ad604SOctavian Purdila struct sock *sk = __skb->sk; 14889c55e01cSJens Axboe 14899c55e01cSJens Axboe /* 14909c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 14919c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 14929c55e01cSJens Axboe * here as compared to sendfile(). We enter here 14939c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 14949c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 14959c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 14969c55e01cSJens Axboe * and networking will grab the socket lock. 14979c55e01cSJens Axboe */ 1498293ad604SOctavian Purdila release_sock(sk); 14999c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1500293ad604SOctavian Purdila lock_sock(sk); 15019c55e01cSJens Axboe return ret; 15029c55e01cSJens Axboe } 15039c55e01cSJens Axboe 15049c55e01cSJens Axboe return 0; 15059c55e01cSJens Axboe } 15069c55e01cSJens Axboe 1507357b40a1SHerbert Xu /** 1508357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1509357b40a1SHerbert Xu * @skb: destination buffer 1510357b40a1SHerbert Xu * @offset: offset in destination 1511357b40a1SHerbert Xu * @from: source buffer 1512357b40a1SHerbert Xu * @len: number of bytes to copy 1513357b40a1SHerbert Xu * 1514357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1515357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1516357b40a1SHerbert Xu * traversing fragment lists and such. 1517357b40a1SHerbert Xu */ 1518357b40a1SHerbert Xu 15190c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1520357b40a1SHerbert Xu { 1521357b40a1SHerbert Xu int i, copy; 15221a028e50SDavid S. Miller int start = skb_headlen(skb); 1523357b40a1SHerbert Xu 1524357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1525357b40a1SHerbert Xu goto fault; 1526357b40a1SHerbert Xu 15271a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1528357b40a1SHerbert Xu if (copy > len) 1529357b40a1SHerbert Xu copy = len; 153027d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1531357b40a1SHerbert Xu if ((len -= copy) == 0) 1532357b40a1SHerbert Xu return 0; 1533357b40a1SHerbert Xu offset += copy; 1534357b40a1SHerbert Xu from += copy; 1535357b40a1SHerbert Xu } 1536357b40a1SHerbert Xu 1537357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1538357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 15391a028e50SDavid S. Miller int end; 1540357b40a1SHerbert Xu 1541547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15421a028e50SDavid S. Miller 15431a028e50SDavid S. Miller end = start + frag->size; 1544357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1545357b40a1SHerbert Xu u8 *vaddr; 1546357b40a1SHerbert Xu 1547357b40a1SHerbert Xu if (copy > len) 1548357b40a1SHerbert Xu copy = len; 1549357b40a1SHerbert Xu 1550357b40a1SHerbert Xu vaddr = kmap_skb_frag(frag); 15511a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 15521a028e50SDavid S. Miller from, copy); 1553357b40a1SHerbert Xu kunmap_skb_frag(vaddr); 1554357b40a1SHerbert Xu 1555357b40a1SHerbert Xu if ((len -= copy) == 0) 1556357b40a1SHerbert Xu return 0; 1557357b40a1SHerbert Xu offset += copy; 1558357b40a1SHerbert Xu from += copy; 1559357b40a1SHerbert Xu } 15601a028e50SDavid S. Miller start = end; 1561357b40a1SHerbert Xu } 1562357b40a1SHerbert Xu 1563357b40a1SHerbert Xu if (skb_shinfo(skb)->frag_list) { 1564357b40a1SHerbert Xu struct sk_buff *list = skb_shinfo(skb)->frag_list; 1565357b40a1SHerbert Xu 1566357b40a1SHerbert Xu for (; list; list = list->next) { 15671a028e50SDavid S. Miller int end; 1568357b40a1SHerbert Xu 1569547b792cSIlpo Järvinen WARN_ON(start > offset + len); 15701a028e50SDavid S. Miller 15711a028e50SDavid S. Miller end = start + list->len; 1572357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1573357b40a1SHerbert Xu if (copy > len) 1574357b40a1SHerbert Xu copy = len; 15751a028e50SDavid S. Miller if (skb_store_bits(list, offset - start, 15761a028e50SDavid S. Miller from, copy)) 1577357b40a1SHerbert Xu goto fault; 1578357b40a1SHerbert Xu if ((len -= copy) == 0) 1579357b40a1SHerbert Xu return 0; 1580357b40a1SHerbert Xu offset += copy; 1581357b40a1SHerbert Xu from += copy; 1582357b40a1SHerbert Xu } 15831a028e50SDavid S. Miller start = end; 1584357b40a1SHerbert Xu } 1585357b40a1SHerbert Xu } 1586357b40a1SHerbert Xu if (!len) 1587357b40a1SHerbert Xu return 0; 1588357b40a1SHerbert Xu 1589357b40a1SHerbert Xu fault: 1590357b40a1SHerbert Xu return -EFAULT; 1591357b40a1SHerbert Xu } 1592357b40a1SHerbert Xu 1593357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1594357b40a1SHerbert Xu 15951da177e4SLinus Torvalds /* Checksum skb data. */ 15961da177e4SLinus Torvalds 15972bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 15982bbbc868SAl Viro int len, __wsum csum) 15991da177e4SLinus Torvalds { 16001a028e50SDavid S. Miller int start = skb_headlen(skb); 16011a028e50SDavid S. Miller int i, copy = start - offset; 16021da177e4SLinus Torvalds int pos = 0; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* Checksum header. */ 16051da177e4SLinus Torvalds if (copy > 0) { 16061da177e4SLinus Torvalds if (copy > len) 16071da177e4SLinus Torvalds copy = len; 16081da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 16091da177e4SLinus Torvalds if ((len -= copy) == 0) 16101da177e4SLinus Torvalds return csum; 16111da177e4SLinus Torvalds offset += copy; 16121da177e4SLinus Torvalds pos = copy; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16161a028e50SDavid S. Miller int end; 16171da177e4SLinus Torvalds 1618547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16191a028e50SDavid S. Miller 16201a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 16211da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 162244bb9363SAl Viro __wsum csum2; 16231da177e4SLinus Torvalds u8 *vaddr; 16241da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds if (copy > len) 16271da177e4SLinus Torvalds copy = len; 16281da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 16291a028e50SDavid S. Miller csum2 = csum_partial(vaddr + frag->page_offset + 16301a028e50SDavid S. Miller offset - start, copy, 0); 16311da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 16321da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16331da177e4SLinus Torvalds if (!(len -= copy)) 16341da177e4SLinus Torvalds return csum; 16351da177e4SLinus Torvalds offset += copy; 16361da177e4SLinus Torvalds pos += copy; 16371da177e4SLinus Torvalds } 16381a028e50SDavid S. Miller start = end; 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 16421da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds for (; list; list = list->next) { 16451a028e50SDavid S. Miller int end; 16461da177e4SLinus Torvalds 1647547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16481a028e50SDavid S. Miller 16491a028e50SDavid S. Miller end = start + list->len; 16501da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16515f92a738SAl Viro __wsum csum2; 16521da177e4SLinus Torvalds if (copy > len) 16531da177e4SLinus Torvalds copy = len; 16541a028e50SDavid S. Miller csum2 = skb_checksum(list, offset - start, 16551a028e50SDavid S. Miller copy, 0); 16561da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 16571da177e4SLinus Torvalds if ((len -= copy) == 0) 16581da177e4SLinus Torvalds return csum; 16591da177e4SLinus Torvalds offset += copy; 16601da177e4SLinus Torvalds pos += copy; 16611da177e4SLinus Torvalds } 16621a028e50SDavid S. Miller start = end; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds } 166509a62660SKris Katterjohn BUG_ON(len); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds return csum; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* Both of above in one bottle. */ 16711da177e4SLinus Torvalds 167281d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 167381d77662SAl Viro u8 *to, int len, __wsum csum) 16741da177e4SLinus Torvalds { 16751a028e50SDavid S. Miller int start = skb_headlen(skb); 16761a028e50SDavid S. Miller int i, copy = start - offset; 16771da177e4SLinus Torvalds int pos = 0; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* Copy header. */ 16801da177e4SLinus Torvalds if (copy > 0) { 16811da177e4SLinus Torvalds if (copy > len) 16821da177e4SLinus Torvalds copy = len; 16831da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 16841da177e4SLinus Torvalds copy, csum); 16851da177e4SLinus Torvalds if ((len -= copy) == 0) 16861da177e4SLinus Torvalds return csum; 16871da177e4SLinus Torvalds offset += copy; 16881da177e4SLinus Torvalds to += copy; 16891da177e4SLinus Torvalds pos = copy; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16931a028e50SDavid S. Miller int end; 16941da177e4SLinus Torvalds 1695547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16961a028e50SDavid S. Miller 16971a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 16981da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16995084205fSAl Viro __wsum csum2; 17001da177e4SLinus Torvalds u8 *vaddr; 17011da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds if (copy > len) 17041da177e4SLinus Torvalds copy = len; 17051da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 17061da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 17071a028e50SDavid S. Miller frag->page_offset + 17081a028e50SDavid S. Miller offset - start, to, 17091a028e50SDavid S. Miller copy, 0); 17101da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 17111da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 17121da177e4SLinus Torvalds if (!(len -= copy)) 17131da177e4SLinus Torvalds return csum; 17141da177e4SLinus Torvalds offset += copy; 17151da177e4SLinus Torvalds to += copy; 17161da177e4SLinus Torvalds pos += copy; 17171da177e4SLinus Torvalds } 17181a028e50SDavid S. Miller start = end; 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 17221da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds for (; list; list = list->next) { 172581d77662SAl Viro __wsum csum2; 17261a028e50SDavid S. Miller int end; 17271da177e4SLinus Torvalds 1728547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17291a028e50SDavid S. Miller 17301a028e50SDavid S. Miller end = start + list->len; 17311da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17321da177e4SLinus Torvalds if (copy > len) 17331da177e4SLinus Torvalds copy = len; 17341a028e50SDavid S. Miller csum2 = skb_copy_and_csum_bits(list, 17351a028e50SDavid S. Miller offset - start, 17361da177e4SLinus Torvalds to, copy, 0); 17371da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 17381da177e4SLinus Torvalds if ((len -= copy) == 0) 17391da177e4SLinus Torvalds return csum; 17401da177e4SLinus Torvalds offset += copy; 17411da177e4SLinus Torvalds to += copy; 17421da177e4SLinus Torvalds pos += copy; 17431da177e4SLinus Torvalds } 17441a028e50SDavid S. Miller start = end; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds } 174709a62660SKris Katterjohn BUG_ON(len); 17481da177e4SLinus Torvalds return csum; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 17521da177e4SLinus Torvalds { 1753d3bc23e7SAl Viro __wsum csum; 17541da177e4SLinus Torvalds long csstart; 17551da177e4SLinus Torvalds 175684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 1757663ead3bSHerbert Xu csstart = skb->csum_start - skb_headroom(skb); 17581da177e4SLinus Torvalds else 17591da177e4SLinus Torvalds csstart = skb_headlen(skb); 17601da177e4SLinus Torvalds 176109a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 17621da177e4SLinus Torvalds 1763d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds csum = 0; 17661da177e4SLinus Torvalds if (csstart != skb->len) 17671da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 17681da177e4SLinus Torvalds skb->len - csstart, 0); 17691da177e4SLinus Torvalds 177084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 1771ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 17721da177e4SLinus Torvalds 1773d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds /** 17781da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 17791da177e4SLinus Torvalds * @list: list to dequeue from 17801da177e4SLinus Torvalds * 17811da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 17821da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 17831da177e4SLinus Torvalds * returned or %NULL if the list is empty. 17841da177e4SLinus Torvalds */ 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds unsigned long flags; 17891da177e4SLinus Torvalds struct sk_buff *result; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 17921da177e4SLinus Torvalds result = __skb_dequeue(list); 17931da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 17941da177e4SLinus Torvalds return result; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds /** 17981da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 17991da177e4SLinus Torvalds * @list: list to dequeue from 18001da177e4SLinus Torvalds * 18011da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 18021da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 18031da177e4SLinus Torvalds * returned or %NULL if the list is empty. 18041da177e4SLinus Torvalds */ 18051da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 18061da177e4SLinus Torvalds { 18071da177e4SLinus Torvalds unsigned long flags; 18081da177e4SLinus Torvalds struct sk_buff *result; 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18111da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 18121da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18131da177e4SLinus Torvalds return result; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds /** 18171da177e4SLinus Torvalds * skb_queue_purge - empty a list 18181da177e4SLinus Torvalds * @list: list to empty 18191da177e4SLinus Torvalds * 18201da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 18211da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 18221da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 18231da177e4SLinus Torvalds */ 18241da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 18251da177e4SLinus Torvalds { 18261da177e4SLinus Torvalds struct sk_buff *skb; 18271da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 18281da177e4SLinus Torvalds kfree_skb(skb); 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /** 18321da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 18331da177e4SLinus Torvalds * @list: list to use 18341da177e4SLinus Torvalds * @newsk: buffer to queue 18351da177e4SLinus Torvalds * 18361da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 18371da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 18381da177e4SLinus Torvalds * safely. 18391da177e4SLinus Torvalds * 18401da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18411da177e4SLinus Torvalds */ 18421da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 18431da177e4SLinus Torvalds { 18441da177e4SLinus Torvalds unsigned long flags; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18471da177e4SLinus Torvalds __skb_queue_head(list, newsk); 18481da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds /** 18521da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 18531da177e4SLinus Torvalds * @list: list to use 18541da177e4SLinus Torvalds * @newsk: buffer to queue 18551da177e4SLinus Torvalds * 18561da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 18571da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 18581da177e4SLinus Torvalds * safely. 18591da177e4SLinus Torvalds * 18601da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18611da177e4SLinus Torvalds */ 18621da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 18631da177e4SLinus Torvalds { 18641da177e4SLinus Torvalds unsigned long flags; 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18671da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 18681da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18691da177e4SLinus Torvalds } 18708728b834SDavid S. Miller 18711da177e4SLinus Torvalds /** 18721da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 18731da177e4SLinus Torvalds * @skb: buffer to remove 18748728b834SDavid S. Miller * @list: list to use 18751da177e4SLinus Torvalds * 18768728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 18778728b834SDavid S. Miller * function is atomic with respect to other list locked calls 18781da177e4SLinus Torvalds * 18798728b834SDavid S. Miller * You must know what list the SKB is on. 18801da177e4SLinus Torvalds */ 18818728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 18821da177e4SLinus Torvalds { 18831da177e4SLinus Torvalds unsigned long flags; 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 18868728b834SDavid S. Miller __skb_unlink(skb, list); 18871da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds /** 18911da177e4SLinus Torvalds * skb_append - append a buffer 18921da177e4SLinus Torvalds * @old: buffer to insert after 18931da177e4SLinus Torvalds * @newsk: buffer to insert 18948728b834SDavid S. Miller * @list: list to use 18951da177e4SLinus Torvalds * 18961da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 18971da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 18981da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 18991da177e4SLinus Torvalds */ 19008728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 19011da177e4SLinus Torvalds { 19021da177e4SLinus Torvalds unsigned long flags; 19031da177e4SLinus Torvalds 19048728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 19057de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 19068728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /** 19111da177e4SLinus Torvalds * skb_insert - insert a buffer 19121da177e4SLinus Torvalds * @old: buffer to insert before 19131da177e4SLinus Torvalds * @newsk: buffer to insert 19148728b834SDavid S. Miller * @list: list to use 19151da177e4SLinus Torvalds * 19168728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 19178728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 19188728b834SDavid S. Miller * calls. 19198728b834SDavid S. Miller * 19201da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 19211da177e4SLinus Torvalds */ 19228728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 19231da177e4SLinus Torvalds { 19241da177e4SLinus Torvalds unsigned long flags; 19251da177e4SLinus Torvalds 19268728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 19278728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 19288728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 19321da177e4SLinus Torvalds struct sk_buff* skb1, 19331da177e4SLinus Torvalds const u32 len, const int pos) 19341da177e4SLinus Torvalds { 19351da177e4SLinus Torvalds int i; 19361da177e4SLinus Torvalds 1937d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1938d626f62bSArnaldo Carvalho de Melo pos - len); 19391da177e4SLinus Torvalds /* And move data appendix as is. */ 19401da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 19411da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 19441da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 19451da177e4SLinus Torvalds skb1->data_len = skb->data_len; 19461da177e4SLinus Torvalds skb1->len += skb1->data_len; 19471da177e4SLinus Torvalds skb->data_len = 0; 19481da177e4SLinus Torvalds skb->len = len; 194927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 19521da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 19531da177e4SLinus Torvalds struct sk_buff* skb1, 19541da177e4SLinus Torvalds const u32 len, int pos) 19551da177e4SLinus Torvalds { 19561da177e4SLinus Torvalds int i, k = 0; 19571da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 19601da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 19611da177e4SLinus Torvalds skb->len = len; 19621da177e4SLinus Torvalds skb->data_len = len - pos; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 19651da177e4SLinus Torvalds int size = skb_shinfo(skb)->frags[i].size; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds if (pos + size > len) { 19681da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if (pos < len) { 19711da177e4SLinus Torvalds /* Split frag. 19721da177e4SLinus Torvalds * We have two variants in this case: 19731da177e4SLinus Torvalds * 1. Move all the frag to the second 19741da177e4SLinus Torvalds * part, if it is possible. F.e. 19751da177e4SLinus Torvalds * this approach is mandatory for TUX, 19761da177e4SLinus Torvalds * where splitting is expensive. 19771da177e4SLinus Torvalds * 2. Split is accurately. We make this. 19781da177e4SLinus Torvalds */ 19791da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 19801da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 19811da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].size -= len - pos; 19821da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = len - pos; 19831da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds k++; 19861da177e4SLinus Torvalds } else 19871da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 19881da177e4SLinus Torvalds pos += size; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds /** 19941da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 19951da177e4SLinus Torvalds * @skb: the buffer to split 19961da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 19971da177e4SLinus Torvalds * @len: new length for skb 19981da177e4SLinus Torvalds */ 19991da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 20001da177e4SLinus Torvalds { 20011da177e4SLinus Torvalds int pos = skb_headlen(skb); 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 20041da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 20051da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 20061da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 2009677e90edSThomas Graf /** 2010677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2011677e90edSThomas Graf * @skb: the buffer to read 2012677e90edSThomas Graf * @from: lower offset of data to be read 2013677e90edSThomas Graf * @to: upper offset of data to be read 2014677e90edSThomas Graf * @st: state variable 2015677e90edSThomas Graf * 2016677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2017677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2018677e90edSThomas Graf */ 2019677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2020677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2021677e90edSThomas Graf { 2022677e90edSThomas Graf st->lower_offset = from; 2023677e90edSThomas Graf st->upper_offset = to; 2024677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2025677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2026677e90edSThomas Graf st->frag_data = NULL; 2027677e90edSThomas Graf } 2028677e90edSThomas Graf 2029677e90edSThomas Graf /** 2030677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2031677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2032677e90edSThomas Graf * @data: destination pointer for data to be returned 2033677e90edSThomas Graf * @st: state variable 2034677e90edSThomas Graf * 2035677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 2036677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2037677e90edSThomas Graf * the head of the data block to &data and returns the length 2038677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2039677e90edSThomas Graf * offset has been reached. 2040677e90edSThomas Graf * 2041677e90edSThomas Graf * The caller is not required to consume all of the data 2042677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 2043677e90edSThomas Graf * of bytes already consumed and the next call to 2044677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2045677e90edSThomas Graf * 2046bc2cda1eSRandy Dunlap * Note 1: The size of each block of data returned can be arbitary, 2047677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 2048677e90edSThomas Graf * reads of potentially non linear data. 2049677e90edSThomas Graf * 2050bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2051677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2052677e90edSThomas Graf * a stack for this purpose. 2053677e90edSThomas Graf */ 2054677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2055677e90edSThomas Graf struct skb_seq_state *st) 2056677e90edSThomas Graf { 2057677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2058677e90edSThomas Graf skb_frag_t *frag; 2059677e90edSThomas Graf 2060677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 2061677e90edSThomas Graf return 0; 2062677e90edSThomas Graf 2063677e90edSThomas Graf next_skb: 2064677e90edSThomas Graf block_limit = skb_headlen(st->cur_skb); 2065677e90edSThomas Graf 2066677e90edSThomas Graf if (abs_offset < block_limit) { 2067677e90edSThomas Graf *data = st->cur_skb->data + abs_offset; 2068677e90edSThomas Graf return block_limit - abs_offset; 2069677e90edSThomas Graf } 2070677e90edSThomas Graf 2071677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2072677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2073677e90edSThomas Graf 2074677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2075677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2076677e90edSThomas Graf block_limit = frag->size + st->stepped_offset; 2077677e90edSThomas Graf 2078677e90edSThomas Graf if (abs_offset < block_limit) { 2079677e90edSThomas Graf if (!st->frag_data) 2080677e90edSThomas Graf st->frag_data = kmap_skb_frag(frag); 2081677e90edSThomas Graf 2082677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2083677e90edSThomas Graf (abs_offset - st->stepped_offset); 2084677e90edSThomas Graf 2085677e90edSThomas Graf return block_limit - abs_offset; 2086677e90edSThomas Graf } 2087677e90edSThomas Graf 2088677e90edSThomas Graf if (st->frag_data) { 2089677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2090677e90edSThomas Graf st->frag_data = NULL; 2091677e90edSThomas Graf } 2092677e90edSThomas Graf 2093677e90edSThomas Graf st->frag_idx++; 2094677e90edSThomas Graf st->stepped_offset += frag->size; 2095677e90edSThomas Graf } 2096677e90edSThomas Graf 20975b5a60daSOlaf Kirch if (st->frag_data) { 20985b5a60daSOlaf Kirch kunmap_skb_frag(st->frag_data); 20995b5a60daSOlaf Kirch st->frag_data = NULL; 21005b5a60daSOlaf Kirch } 21015b5a60daSOlaf Kirch 2102677e90edSThomas Graf if (st->cur_skb->next) { 2103677e90edSThomas Graf st->cur_skb = st->cur_skb->next; 2104677e90edSThomas Graf st->frag_idx = 0; 2105677e90edSThomas Graf goto next_skb; 2106677e90edSThomas Graf } else if (st->root_skb == st->cur_skb && 2107677e90edSThomas Graf skb_shinfo(st->root_skb)->frag_list) { 2108677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2109677e90edSThomas Graf goto next_skb; 2110677e90edSThomas Graf } 2111677e90edSThomas Graf 2112677e90edSThomas Graf return 0; 2113677e90edSThomas Graf } 2114677e90edSThomas Graf 2115677e90edSThomas Graf /** 2116677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2117677e90edSThomas Graf * @st: state variable 2118677e90edSThomas Graf * 2119677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2120677e90edSThomas Graf * returned 0. 2121677e90edSThomas Graf */ 2122677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2123677e90edSThomas Graf { 2124677e90edSThomas Graf if (st->frag_data) 2125677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 2126677e90edSThomas Graf } 2127677e90edSThomas Graf 21283fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 21293fc7e8a6SThomas Graf 21303fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 21313fc7e8a6SThomas Graf struct ts_config *conf, 21323fc7e8a6SThomas Graf struct ts_state *state) 21333fc7e8a6SThomas Graf { 21343fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 21353fc7e8a6SThomas Graf } 21363fc7e8a6SThomas Graf 21373fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 21383fc7e8a6SThomas Graf { 21393fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 21403fc7e8a6SThomas Graf } 21413fc7e8a6SThomas Graf 21423fc7e8a6SThomas Graf /** 21433fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 21443fc7e8a6SThomas Graf * @skb: the buffer to look in 21453fc7e8a6SThomas Graf * @from: search offset 21463fc7e8a6SThomas Graf * @to: search limit 21473fc7e8a6SThomas Graf * @config: textsearch configuration 21483fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 21493fc7e8a6SThomas Graf * 21503fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 21513fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 21523fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 21533fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 21543fc7e8a6SThomas Graf */ 21553fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 21563fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 21573fc7e8a6SThomas Graf struct ts_state *state) 21583fc7e8a6SThomas Graf { 2159f72b948dSPhil Oester unsigned int ret; 2160f72b948dSPhil Oester 21613fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 21623fc7e8a6SThomas Graf config->finish = skb_ts_finish; 21633fc7e8a6SThomas Graf 21643fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 21653fc7e8a6SThomas Graf 2166f72b948dSPhil Oester ret = textsearch_find(config, state); 2167f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 21683fc7e8a6SThomas Graf } 21693fc7e8a6SThomas Graf 2170e89e9cf5SAnanda Raju /** 2171e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 2172e89e9cf5SAnanda Raju * @sk: sock structure 2173e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 2174e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2175e89e9cf5SAnanda Raju * @from: pointer to user message iov 2176e89e9cf5SAnanda Raju * @length: length of the iov message 2177e89e9cf5SAnanda Raju * 2178e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2179e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2180e89e9cf5SAnanda Raju */ 2181e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2182dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2183e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2184e89e9cf5SAnanda Raju void *from, int length) 2185e89e9cf5SAnanda Raju { 2186e89e9cf5SAnanda Raju int frg_cnt = 0; 2187e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 2188e89e9cf5SAnanda Raju struct page *page = NULL; 2189e89e9cf5SAnanda Raju int copy, left; 2190e89e9cf5SAnanda Raju int offset = 0; 2191e89e9cf5SAnanda Raju int ret; 2192e89e9cf5SAnanda Raju 2193e89e9cf5SAnanda Raju do { 2194e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2195e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2196e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2197e89e9cf5SAnanda Raju return -EFAULT; 2198e89e9cf5SAnanda Raju 2199e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 2200e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 2201e89e9cf5SAnanda Raju 2202e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 2203e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 2204e89e9cf5SAnanda Raju */ 2205e89e9cf5SAnanda Raju if (page == NULL) 2206e89e9cf5SAnanda Raju return -ENOMEM; 2207e89e9cf5SAnanda Raju 2208e89e9cf5SAnanda Raju /* initialize the next frag */ 2209e89e9cf5SAnanda Raju sk->sk_sndmsg_page = page; 2210e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 2211e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2212e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 2213e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2214e89e9cf5SAnanda Raju 2215e89e9cf5SAnanda Raju /* get the new initialized frag */ 2216e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 2217e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2218e89e9cf5SAnanda Raju 2219e89e9cf5SAnanda Raju /* copy the user data to page */ 2220e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 2221e89e9cf5SAnanda Raju copy = (length > left)? left : length; 2222e89e9cf5SAnanda Raju 2223e89e9cf5SAnanda Raju ret = getfrag(from, (page_address(frag->page) + 2224e89e9cf5SAnanda Raju frag->page_offset + frag->size), 2225e89e9cf5SAnanda Raju offset, copy, 0, skb); 2226e89e9cf5SAnanda Raju if (ret < 0) 2227e89e9cf5SAnanda Raju return -EFAULT; 2228e89e9cf5SAnanda Raju 2229e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2230e89e9cf5SAnanda Raju sk->sk_sndmsg_off += copy; 2231e89e9cf5SAnanda Raju frag->size += copy; 2232e89e9cf5SAnanda Raju skb->len += copy; 2233e89e9cf5SAnanda Raju skb->data_len += copy; 2234e89e9cf5SAnanda Raju offset += copy; 2235e89e9cf5SAnanda Raju length -= copy; 2236e89e9cf5SAnanda Raju 2237e89e9cf5SAnanda Raju } while (length > 0); 2238e89e9cf5SAnanda Raju 2239e89e9cf5SAnanda Raju return 0; 2240e89e9cf5SAnanda Raju } 2241e89e9cf5SAnanda Raju 2242cbb042f9SHerbert Xu /** 2243cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2244cbb042f9SHerbert Xu * @skb: buffer to update 2245cbb042f9SHerbert Xu * @len: length of data pulled 2246cbb042f9SHerbert Xu * 2247cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2248fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 224984fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 225084fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 225184fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2252cbb042f9SHerbert Xu */ 2253cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2254cbb042f9SHerbert Xu { 2255cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2256cbb042f9SHerbert Xu skb->len -= len; 2257cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2258cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2259cbb042f9SHerbert Xu return skb->data += len; 2260cbb042f9SHerbert Xu } 2261cbb042f9SHerbert Xu 2262f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2263f94691acSArnaldo Carvalho de Melo 2264f4c50d99SHerbert Xu /** 2265f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2266f4c50d99SHerbert Xu * @skb: buffer to segment 2267576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2268f4c50d99SHerbert Xu * 2269f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 22704c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 22714c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2272f4c50d99SHerbert Xu */ 2273576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2274f4c50d99SHerbert Xu { 2275f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2276f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 2277f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 227898e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 2279f4c50d99SHerbert Xu unsigned int offset = doffset; 2280f4c50d99SHerbert Xu unsigned int headroom; 2281f4c50d99SHerbert Xu unsigned int len; 2282576a30ebSHerbert Xu int sg = features & NETIF_F_SG; 2283f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 2284f4c50d99SHerbert Xu int err = -ENOMEM; 2285f4c50d99SHerbert Xu int i = 0; 2286f4c50d99SHerbert Xu int pos; 2287f4c50d99SHerbert Xu 2288f4c50d99SHerbert Xu __skb_push(skb, doffset); 2289f4c50d99SHerbert Xu headroom = skb_headroom(skb); 2290f4c50d99SHerbert Xu pos = skb_headlen(skb); 2291f4c50d99SHerbert Xu 2292f4c50d99SHerbert Xu do { 2293f4c50d99SHerbert Xu struct sk_buff *nskb; 2294f4c50d99SHerbert Xu skb_frag_t *frag; 2295c8884eddSHerbert Xu int hsize; 2296f4c50d99SHerbert Xu int k; 2297f4c50d99SHerbert Xu int size; 2298f4c50d99SHerbert Xu 2299f4c50d99SHerbert Xu len = skb->len - offset; 2300f4c50d99SHerbert Xu if (len > mss) 2301f4c50d99SHerbert Xu len = mss; 2302f4c50d99SHerbert Xu 2303f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 2304f4c50d99SHerbert Xu if (hsize < 0) 2305f4c50d99SHerbert Xu hsize = 0; 2306c8884eddSHerbert Xu if (hsize > len || !sg) 2307c8884eddSHerbert Xu hsize = len; 2308f4c50d99SHerbert Xu 2309c8884eddSHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2310f4c50d99SHerbert Xu if (unlikely(!nskb)) 2311f4c50d99SHerbert Xu goto err; 2312f4c50d99SHerbert Xu 2313f4c50d99SHerbert Xu if (segs) 2314f4c50d99SHerbert Xu tail->next = nskb; 2315f4c50d99SHerbert Xu else 2316f4c50d99SHerbert Xu segs = nskb; 2317f4c50d99SHerbert Xu tail = nskb; 2318f4c50d99SHerbert Xu 23196f85a124SHerbert Xu __copy_skb_header(nskb, skb); 2320f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 2321f4c50d99SHerbert Xu 2322f4c50d99SHerbert Xu skb_reserve(nskb, headroom); 2323459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 2324ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 2325b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 2326b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 2327d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 2328d626f62bSArnaldo Carvalho de Melo doffset); 2329f4c50d99SHerbert Xu if (!sg) { 23306f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2331f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 2332f4c50d99SHerbert Xu skb_put(nskb, len), 2333f4c50d99SHerbert Xu len, 0); 2334f4c50d99SHerbert Xu continue; 2335f4c50d99SHerbert Xu } 2336f4c50d99SHerbert Xu 2337f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 2338f4c50d99SHerbert Xu k = 0; 2339f4c50d99SHerbert Xu 2340d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 2341d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 2342f4c50d99SHerbert Xu 2343f4c50d99SHerbert Xu while (pos < offset + len) { 2344f4c50d99SHerbert Xu BUG_ON(i >= nfrags); 2345f4c50d99SHerbert Xu 2346f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 2347f4c50d99SHerbert Xu get_page(frag->page); 2348f4c50d99SHerbert Xu size = frag->size; 2349f4c50d99SHerbert Xu 2350f4c50d99SHerbert Xu if (pos < offset) { 2351f4c50d99SHerbert Xu frag->page_offset += offset - pos; 2352f4c50d99SHerbert Xu frag->size -= offset - pos; 2353f4c50d99SHerbert Xu } 2354f4c50d99SHerbert Xu 2355f4c50d99SHerbert Xu k++; 2356f4c50d99SHerbert Xu 2357f4c50d99SHerbert Xu if (pos + size <= offset + len) { 2358f4c50d99SHerbert Xu i++; 2359f4c50d99SHerbert Xu pos += size; 2360f4c50d99SHerbert Xu } else { 2361f4c50d99SHerbert Xu frag->size -= pos + size - (offset + len); 2362f4c50d99SHerbert Xu break; 2363f4c50d99SHerbert Xu } 2364f4c50d99SHerbert Xu 2365f4c50d99SHerbert Xu frag++; 2366f4c50d99SHerbert Xu } 2367f4c50d99SHerbert Xu 2368f4c50d99SHerbert Xu skb_shinfo(nskb)->nr_frags = k; 2369f4c50d99SHerbert Xu nskb->data_len = len - hsize; 2370f4c50d99SHerbert Xu nskb->len += nskb->data_len; 2371f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 2372f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 2373f4c50d99SHerbert Xu 2374f4c50d99SHerbert Xu return segs; 2375f4c50d99SHerbert Xu 2376f4c50d99SHerbert Xu err: 2377f4c50d99SHerbert Xu while ((skb = segs)) { 2378f4c50d99SHerbert Xu segs = skb->next; 2379b08d5840SPatrick McHardy kfree_skb(skb); 2380f4c50d99SHerbert Xu } 2381f4c50d99SHerbert Xu return ERR_PTR(err); 2382f4c50d99SHerbert Xu } 2383f4c50d99SHerbert Xu 2384f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 2385f4c50d99SHerbert Xu 23861da177e4SLinus Torvalds void __init skb_init(void) 23871da177e4SLinus Torvalds { 23881da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 23891da177e4SLinus Torvalds sizeof(struct sk_buff), 23901da177e4SLinus Torvalds 0, 2391e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 239220c2df83SPaul Mundt NULL); 2393d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2394d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2395d179cd12SDavid S. Miller sizeof(atomic_t), 2396d179cd12SDavid S. Miller 0, 2397e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 239820c2df83SPaul Mundt NULL); 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 2401716ea3a7SDavid Howells /** 2402716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2403716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2404716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2405716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2406716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2407716ea3a7SDavid Howells * 2408716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2409716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2410716ea3a7SDavid Howells */ 241151c739d1SDavid S. Miller static int 241251c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2413716ea3a7SDavid Howells { 24141a028e50SDavid S. Miller int start = skb_headlen(skb); 24151a028e50SDavid S. Miller int i, copy = start - offset; 2416716ea3a7SDavid Howells int elt = 0; 2417716ea3a7SDavid Howells 2418716ea3a7SDavid Howells if (copy > 0) { 2419716ea3a7SDavid Howells if (copy > len) 2420716ea3a7SDavid Howells copy = len; 2421642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 2422716ea3a7SDavid Howells elt++; 2423716ea3a7SDavid Howells if ((len -= copy) == 0) 2424716ea3a7SDavid Howells return elt; 2425716ea3a7SDavid Howells offset += copy; 2426716ea3a7SDavid Howells } 2427716ea3a7SDavid Howells 2428716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24291a028e50SDavid S. Miller int end; 2430716ea3a7SDavid Howells 2431547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24321a028e50SDavid S. Miller 24331a028e50SDavid S. Miller end = start + skb_shinfo(skb)->frags[i].size; 2434716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2435716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2436716ea3a7SDavid Howells 2437716ea3a7SDavid Howells if (copy > len) 2438716ea3a7SDavid Howells copy = len; 2439642f1490SJens Axboe sg_set_page(&sg[elt], frag->page, copy, 2440642f1490SJens Axboe frag->page_offset+offset-start); 2441716ea3a7SDavid Howells elt++; 2442716ea3a7SDavid Howells if (!(len -= copy)) 2443716ea3a7SDavid Howells return elt; 2444716ea3a7SDavid Howells offset += copy; 2445716ea3a7SDavid Howells } 24461a028e50SDavid S. Miller start = end; 2447716ea3a7SDavid Howells } 2448716ea3a7SDavid Howells 2449716ea3a7SDavid Howells if (skb_shinfo(skb)->frag_list) { 2450716ea3a7SDavid Howells struct sk_buff *list = skb_shinfo(skb)->frag_list; 2451716ea3a7SDavid Howells 2452716ea3a7SDavid Howells for (; list; list = list->next) { 24531a028e50SDavid S. Miller int end; 2454716ea3a7SDavid Howells 2455547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24561a028e50SDavid S. Miller 24571a028e50SDavid S. Miller end = start + list->len; 2458716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2459716ea3a7SDavid Howells if (copy > len) 2460716ea3a7SDavid Howells copy = len; 246151c739d1SDavid S. Miller elt += __skb_to_sgvec(list, sg+elt, offset - start, 246251c739d1SDavid S. Miller copy); 2463716ea3a7SDavid Howells if ((len -= copy) == 0) 2464716ea3a7SDavid Howells return elt; 2465716ea3a7SDavid Howells offset += copy; 2466716ea3a7SDavid Howells } 24671a028e50SDavid S. Miller start = end; 2468716ea3a7SDavid Howells } 2469716ea3a7SDavid Howells } 2470716ea3a7SDavid Howells BUG_ON(len); 2471716ea3a7SDavid Howells return elt; 2472716ea3a7SDavid Howells } 2473716ea3a7SDavid Howells 247451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 247551c739d1SDavid S. Miller { 247651c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 247751c739d1SDavid S. Miller 2478c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 247951c739d1SDavid S. Miller 248051c739d1SDavid S. Miller return nsg; 248151c739d1SDavid S. Miller } 248251c739d1SDavid S. Miller 2483716ea3a7SDavid Howells /** 2484716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 2485716ea3a7SDavid Howells * @skb: The socket buffer to check. 2486716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 2487716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 2488716ea3a7SDavid Howells * 2489716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 2490716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 2491716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 2492716ea3a7SDavid Howells * 2493716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 2494716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 2495716ea3a7SDavid Howells * set to point to the skb in which this space begins. 2496716ea3a7SDavid Howells * 2497716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 2498716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 2499716ea3a7SDavid Howells */ 2500716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2501716ea3a7SDavid Howells { 2502716ea3a7SDavid Howells int copyflag; 2503716ea3a7SDavid Howells int elt; 2504716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 2505716ea3a7SDavid Howells 2506716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 2507716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 2508716ea3a7SDavid Howells * at the moment even if they are anonymous). 2509716ea3a7SDavid Howells */ 2510716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2511716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2512716ea3a7SDavid Howells return -ENOMEM; 2513716ea3a7SDavid Howells 2514716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 2515716ea3a7SDavid Howells if (!skb_shinfo(skb)->frag_list) { 2516716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 2517716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 2518716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 2519716ea3a7SDavid Howells * space, 128 bytes is fair. */ 2520716ea3a7SDavid Howells 2521716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 2522716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2523716ea3a7SDavid Howells return -ENOMEM; 2524716ea3a7SDavid Howells 2525716ea3a7SDavid Howells /* Voila! */ 2526716ea3a7SDavid Howells *trailer = skb; 2527716ea3a7SDavid Howells return 1; 2528716ea3a7SDavid Howells } 2529716ea3a7SDavid Howells 2530716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 2531716ea3a7SDavid Howells 2532716ea3a7SDavid Howells elt = 1; 2533716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 2534716ea3a7SDavid Howells copyflag = 0; 2535716ea3a7SDavid Howells 2536716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 2537716ea3a7SDavid Howells int ntail = 0; 2538716ea3a7SDavid Howells 2539716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 2540716ea3a7SDavid Howells * this can happen on input. Copy it and everything 2541716ea3a7SDavid Howells * after it. */ 2542716ea3a7SDavid Howells 2543716ea3a7SDavid Howells if (skb_shared(skb1)) 2544716ea3a7SDavid Howells copyflag = 1; 2545716ea3a7SDavid Howells 2546716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 2547716ea3a7SDavid Howells 2548716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 2549716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 2550716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list || 2551716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 2552716ea3a7SDavid Howells ntail = tailbits + 128; 2553716ea3a7SDavid Howells } 2554716ea3a7SDavid Howells 2555716ea3a7SDavid Howells if (copyflag || 2556716ea3a7SDavid Howells skb_cloned(skb1) || 2557716ea3a7SDavid Howells ntail || 2558716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 2559716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list) { 2560716ea3a7SDavid Howells struct sk_buff *skb2; 2561716ea3a7SDavid Howells 2562716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 2563716ea3a7SDavid Howells if (ntail == 0) 2564716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 2565716ea3a7SDavid Howells else 2566716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 2567716ea3a7SDavid Howells skb_headroom(skb1), 2568716ea3a7SDavid Howells ntail, 2569716ea3a7SDavid Howells GFP_ATOMIC); 2570716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 2571716ea3a7SDavid Howells return -ENOMEM; 2572716ea3a7SDavid Howells 2573716ea3a7SDavid Howells if (skb1->sk) 2574716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 2575716ea3a7SDavid Howells 2576716ea3a7SDavid Howells /* Looking around. Are we still alive? 2577716ea3a7SDavid Howells * OK, link new skb, drop old one */ 2578716ea3a7SDavid Howells 2579716ea3a7SDavid Howells skb2->next = skb1->next; 2580716ea3a7SDavid Howells *skb_p = skb2; 2581716ea3a7SDavid Howells kfree_skb(skb1); 2582716ea3a7SDavid Howells skb1 = skb2; 2583716ea3a7SDavid Howells } 2584716ea3a7SDavid Howells elt++; 2585716ea3a7SDavid Howells *trailer = skb1; 2586716ea3a7SDavid Howells skb_p = &skb1->next; 2587716ea3a7SDavid Howells } 2588716ea3a7SDavid Howells 2589716ea3a7SDavid Howells return elt; 2590716ea3a7SDavid Howells } 2591716ea3a7SDavid Howells 2592f35d9d8aSRusty Russell /** 2593f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 2594f35d9d8aSRusty Russell * @skb: the skb to set 2595f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 2596f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 2597f35d9d8aSRusty Russell * 2598f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 2599f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 2600f35d9d8aSRusty Russell * 2601f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 2602f35d9d8aSRusty Russell * returns false you should drop the packet. 2603f35d9d8aSRusty Russell */ 2604f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 2605f35d9d8aSRusty Russell { 2606f35d9d8aSRusty Russell if (unlikely(start > skb->len - 2) || 2607f35d9d8aSRusty Russell unlikely((int)start + off > skb->len - 2)) { 2608f35d9d8aSRusty Russell if (net_ratelimit()) 2609f35d9d8aSRusty Russell printk(KERN_WARNING 2610f35d9d8aSRusty Russell "bad partial csum: csum=%u/%u len=%u\n", 2611f35d9d8aSRusty Russell start, off, skb->len); 2612f35d9d8aSRusty Russell return false; 2613f35d9d8aSRusty Russell } 2614f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 2615f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 2616f35d9d8aSRusty Russell skb->csum_offset = off; 2617f35d9d8aSRusty Russell return true; 2618f35d9d8aSRusty Russell } 2619f35d9d8aSRusty Russell 26204497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 26214497b076SBen Hutchings { 26224497b076SBen Hutchings if (net_ratelimit()) 26234497b076SBen Hutchings pr_warning("%s: received packets cannot be forwarded" 26244497b076SBen Hutchings " while LRO is enabled\n", skb->dev->name); 26254497b076SBen Hutchings } 26264497b076SBen Hutchings 26271da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim); 26281da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb); 2629231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb); 26301da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail); 2631d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 26328af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb); 26331da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy); 26341da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head); 26351da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum); 26361da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone); 26371da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy); 26381da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits); 26391da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev); 26401da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits); 26411da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand); 26421da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic); 26431da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad); 26441da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom); 26451da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic); 26461da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue); 26471da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail); 26481da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert); 26491da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge); 26501da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head); 26511da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail); 26521da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink); 26531da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append); 26541da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split); 2655677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read); 2656677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read); 2657677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read); 26583fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text); 2659e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags); 26604497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 2661716ea3a7SDavid Howells 2662716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec); 2663716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data); 2664f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set); 2665