11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: Alan Cox <iiitac@pyr.swan.ac.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $ 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Fixes: 101da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 111da177e4SLinus Torvalds * balancer bugs. 121da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 131da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 141da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 151da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 161da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 171da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 181da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 191da177e4SLinus Torvalds * only put in the headers 201da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 211da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 221da177e4SLinus Torvalds * Andi Kleen : slabified it. 231da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * NOTE: 261da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 271da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 281da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 291da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 321da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 331da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 341da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/module.h> 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/mm.h> 451da177e4SLinus Torvalds #include <linux/interrupt.h> 461da177e4SLinus Torvalds #include <linux/in.h> 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/netdevice.h> 501da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 511da177e4SLinus Torvalds #include <net/pkt_sched.h> 521da177e4SLinus Torvalds #endif 531da177e4SLinus Torvalds #include <linux/string.h> 541da177e4SLinus Torvalds #include <linux/skbuff.h> 551da177e4SLinus Torvalds #include <linux/cache.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <linux/init.h> 58716ea3a7SDavid Howells #include <linux/scatterlist.h> 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #include <net/protocol.h> 611da177e4SLinus Torvalds #include <net/dst.h> 621da177e4SLinus Torvalds #include <net/sock.h> 631da177e4SLinus Torvalds #include <net/checksum.h> 641da177e4SLinus Torvalds #include <net/xfrm.h> 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <asm/uaccess.h> 671da177e4SLinus Torvalds #include <asm/system.h> 681da177e4SLinus Torvalds 69a1f8e7f7SAl Viro #include "kmap_skb.h" 70a1f8e7f7SAl Viro 71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly; 72e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* 751da177e4SLinus Torvalds * Keep out-of-line to prevent kernel bloat. 761da177e4SLinus Torvalds * __builtin_return_address is not used because it is not always 771da177e4SLinus Torvalds * reliable. 781da177e4SLinus Torvalds */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /** 811da177e4SLinus Torvalds * skb_over_panic - private function 821da177e4SLinus Torvalds * @skb: buffer 831da177e4SLinus Torvalds * @sz: size 841da177e4SLinus Torvalds * @here: address 851da177e4SLinus Torvalds * 861da177e4SLinus Torvalds * Out of line support code for skb_put(). Not user callable. 871da177e4SLinus Torvalds */ 881da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here) 891da177e4SLinus Torvalds { 9026095455SPatrick McHardy printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 914305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 9227a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 934305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 9426095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 951da177e4SLinus Torvalds BUG(); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /** 991da177e4SLinus Torvalds * skb_under_panic - private function 1001da177e4SLinus Torvalds * @skb: buffer 1011da177e4SLinus Torvalds * @sz: size 1021da177e4SLinus Torvalds * @here: address 1031da177e4SLinus Torvalds * 1041da177e4SLinus Torvalds * Out of line support code for skb_push(). Not user callable. 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here) 1081da177e4SLinus Torvalds { 10926095455SPatrick McHardy printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 1104305b541SArnaldo Carvalho de Melo "data:%p tail:%#lx end:%#lx dev:%s\n", 11127a884dcSArnaldo Carvalho de Melo here, skb->len, sz, skb->head, skb->data, 1124305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 11326095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1141da177e4SLinus Torvalds BUG(); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 117dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb) 118dc6de336SDavid S. Miller { 119dc6de336SDavid S. Miller printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " 120dc6de336SDavid S. Miller "len=%u, sizeof(sk_buff)=%Zd\n", 121dc6de336SDavid S. Miller skb->truesize, skb->len, sizeof(struct sk_buff)); 122dc6de336SDavid S. Miller } 123dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug); 124dc6de336SDavid S. Miller 1251da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1261da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1271da177e4SLinus Torvalds * [BEEP] leaks. 1281da177e4SLinus Torvalds * 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /** 132d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1331da177e4SLinus Torvalds * @size: size to allocate 1341da177e4SLinus Torvalds * @gfp_mask: allocation mask 135c83c2486SRandy Dunlap * @fclone: allocate from fclone cache instead of head cache 136c83c2486SRandy Dunlap * and allocate a cloned (child) skb 137b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1381da177e4SLinus Torvalds * 1391da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 1401da177e4SLinus Torvalds * tail room of size bytes. The object has a reference count of one. 1411da177e4SLinus Torvalds * The return is the buffer. On a failure the return is %NULL. 1421da177e4SLinus Torvalds * 1431da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1441da177e4SLinus Torvalds * %GFP_ATOMIC. 1451da177e4SLinus Torvalds */ 146dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 147b30973f8SChristoph Hellwig int fclone, int node) 1481da177e4SLinus Torvalds { 149e18b890bSChristoph Lameter struct kmem_cache *cache; 1504947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1511da177e4SLinus Torvalds struct sk_buff *skb; 1521da177e4SLinus Torvalds u8 *data; 1531da177e4SLinus Torvalds 1548798b3fbSHerbert Xu cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 1558798b3fbSHerbert Xu 1561da177e4SLinus Torvalds /* Get the HEAD */ 157b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1581da177e4SLinus Torvalds if (!skb) 1591da177e4SLinus Torvalds goto out; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 162b30973f8SChristoph Hellwig data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 163b30973f8SChristoph Hellwig gfp_mask, node); 1641da177e4SLinus Torvalds if (!data) 1651da177e4SLinus Torvalds goto nodata; 1661da177e4SLinus Torvalds 167ca0605a7SArnaldo Carvalho de Melo /* 168ca0605a7SArnaldo Carvalho de Melo * See comment in sk_buff definition, just before the 'tail' member 169ca0605a7SArnaldo Carvalho de Melo */ 170ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 1711da177e4SLinus Torvalds skb->truesize = size + sizeof(struct sk_buff); 1721da177e4SLinus Torvalds atomic_set(&skb->users, 1); 1731da177e4SLinus Torvalds skb->head = data; 1741da177e4SLinus Torvalds skb->data = data; 17527a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 1764305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 1774947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 1784947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 1794947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 1804947d3efSBenjamin LaHaise shinfo->nr_frags = 0; 1817967168cSHerbert Xu shinfo->gso_size = 0; 1827967168cSHerbert Xu shinfo->gso_segs = 0; 1837967168cSHerbert Xu shinfo->gso_type = 0; 1844947d3efSBenjamin LaHaise shinfo->ip6_frag_id = 0; 1854947d3efSBenjamin LaHaise shinfo->frag_list = NULL; 1864947d3efSBenjamin LaHaise 187d179cd12SDavid S. Miller if (fclone) { 188d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 189d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 1901da177e4SLinus Torvalds 191d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 192d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 193d179cd12SDavid S. Miller 194d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 195d179cd12SDavid S. Miller } 1961da177e4SLinus Torvalds out: 1971da177e4SLinus Torvalds return skb; 1981da177e4SLinus Torvalds nodata: 1998798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2001da177e4SLinus Torvalds skb = NULL; 2011da177e4SLinus Torvalds goto out; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /** 2058af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 2068af27456SChristoph Hellwig * @dev: network device to receive on 2078af27456SChristoph Hellwig * @length: length to allocate 2088af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 2098af27456SChristoph Hellwig * 2108af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 2118af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 2128af27456SChristoph Hellwig * the headroom they think they need without accounting for the 2138af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 2148af27456SChristoph Hellwig * 2158af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 2168af27456SChristoph Hellwig */ 2178af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 2188af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 2198af27456SChristoph Hellwig { 22043cb76d9SGreg Kroah-Hartman int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 2218af27456SChristoph Hellwig struct sk_buff *skb; 2228af27456SChristoph Hellwig 223b30973f8SChristoph Hellwig skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); 2247b2e497aSChristoph Hellwig if (likely(skb)) { 2258af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 2267b2e497aSChristoph Hellwig skb->dev = dev; 2277b2e497aSChristoph Hellwig } 2288af27456SChristoph Hellwig return skb; 2298af27456SChristoph Hellwig } 2301da177e4SLinus Torvalds 23127b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 2321da177e4SLinus Torvalds { 23327b437c8SHerbert Xu struct sk_buff *list = *listp; 2341da177e4SLinus Torvalds 23527b437c8SHerbert Xu *listp = NULL; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds do { 2381da177e4SLinus Torvalds struct sk_buff *this = list; 2391da177e4SLinus Torvalds list = list->next; 2401da177e4SLinus Torvalds kfree_skb(this); 2411da177e4SLinus Torvalds } while (list); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 24427b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 24527b437c8SHerbert Xu { 24627b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 24727b437c8SHerbert Xu } 24827b437c8SHerbert Xu 2491da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds struct sk_buff *list; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds for (list = skb_shinfo(skb)->frag_list; list; list = list->next) 2541da177e4SLinus Torvalds skb_get(list); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2575bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds if (!skb->cloned || 2601da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 2611da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 2621da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 2631da177e4SLinus Torvalds int i; 2641da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2651da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 2691da177e4SLinus Torvalds skb_drop_fraglist(skb); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds kfree(skb->head); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds void kfree_skbmem(struct sk_buff *skb) 2791da177e4SLinus Torvalds { 280d179cd12SDavid S. Miller struct sk_buff *other; 281d179cd12SDavid S. Miller atomic_t *fclone_ref; 282d179cd12SDavid S. Miller 2831da177e4SLinus Torvalds skb_release_data(skb); 284d179cd12SDavid S. Miller switch (skb->fclone) { 285d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 2861da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 287d179cd12SDavid S. Miller break; 288d179cd12SDavid S. Miller 289d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 290d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 291d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 292d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 293d179cd12SDavid S. Miller break; 294d179cd12SDavid S. Miller 295d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 296d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 297d179cd12SDavid S. Miller other = skb - 1; 298d179cd12SDavid S. Miller 299d179cd12SDavid S. Miller /* The clone portion is available for 300d179cd12SDavid S. Miller * fast-cloning again. 301d179cd12SDavid S. Miller */ 302d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 303d179cd12SDavid S. Miller 304d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 305d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 306d179cd12SDavid S. Miller break; 307d179cd12SDavid S. Miller }; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /** 3111da177e4SLinus Torvalds * __kfree_skb - private function 3121da177e4SLinus Torvalds * @skb: buffer 3131da177e4SLinus Torvalds * 3141da177e4SLinus Torvalds * Free an sk_buff. Release anything attached to the buffer. 3151da177e4SLinus Torvalds * Clean the state. This is an internal helper function. Users should 3161da177e4SLinus Torvalds * always call kfree_skb 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds void __kfree_skb(struct sk_buff *skb) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds dst_release(skb->dst); 3221da177e4SLinus Torvalds #ifdef CONFIG_XFRM 3231da177e4SLinus Torvalds secpath_put(skb->sp); 3241da177e4SLinus Torvalds #endif 3251da177e4SLinus Torvalds if (skb->destructor) { 3269c2b3328SStephen Hemminger WARN_ON(in_irq()); 3271da177e4SLinus Torvalds skb->destructor(skb); 3281da177e4SLinus Torvalds } 3299fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 3305f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 3319fb9cbb1SYasuyuki Kozakai nf_conntrack_put_reasm(skb->nfct_reasm); 3329fb9cbb1SYasuyuki Kozakai #endif 3331da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 3341da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 3351da177e4SLinus Torvalds #endif 3361da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 3371da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 3381da177e4SLinus Torvalds skb->tc_index = 0; 3391da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 3401da177e4SLinus Torvalds skb->tc_verd = 0; 3411da177e4SLinus Torvalds #endif 3421da177e4SLinus Torvalds #endif 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds kfree_skbmem(skb); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /** 348231d06aeSJörn Engel * kfree_skb - free an sk_buff 349231d06aeSJörn Engel * @skb: buffer to free 350231d06aeSJörn Engel * 351231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 352231d06aeSJörn Engel * hit zero. 353231d06aeSJörn Engel */ 354231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 355231d06aeSJörn Engel { 356231d06aeSJörn Engel if (unlikely(!skb)) 357231d06aeSJörn Engel return; 358231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 359231d06aeSJörn Engel smp_rmb(); 360231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 361231d06aeSJörn Engel return; 362231d06aeSJörn Engel __kfree_skb(skb); 363231d06aeSJörn Engel } 364231d06aeSJörn Engel 365231d06aeSJörn Engel /** 3661da177e4SLinus Torvalds * skb_clone - duplicate an sk_buff 3671da177e4SLinus Torvalds * @skb: buffer to clone 3681da177e4SLinus Torvalds * @gfp_mask: allocation priority 3691da177e4SLinus Torvalds * 3701da177e4SLinus Torvalds * Duplicate an &sk_buff. The new one is not owned by a socket. Both 3711da177e4SLinus Torvalds * copies share the same packet data but not structure. The new 3721da177e4SLinus Torvalds * buffer has a reference count of 1. If the allocation fails the 3731da177e4SLinus Torvalds * function returns %NULL otherwise the new buffer is returned. 3741da177e4SLinus Torvalds * 3751da177e4SLinus Torvalds * If this function is called from an interrupt gfp_mask() must be 3761da177e4SLinus Torvalds * %GFP_ATOMIC. 3771da177e4SLinus Torvalds */ 3781da177e4SLinus Torvalds 379dd0fc66fSAl Viro struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 3801da177e4SLinus Torvalds { 381d179cd12SDavid S. Miller struct sk_buff *n; 3821da177e4SLinus Torvalds 383d179cd12SDavid S. Miller n = skb + 1; 384d179cd12SDavid S. Miller if (skb->fclone == SKB_FCLONE_ORIG && 385d179cd12SDavid S. Miller n->fclone == SKB_FCLONE_UNAVAILABLE) { 386d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (n + 1); 387d179cd12SDavid S. Miller n->fclone = SKB_FCLONE_CLONE; 388d179cd12SDavid S. Miller atomic_inc(fclone_ref); 389d179cd12SDavid S. Miller } else { 390d179cd12SDavid S. Miller n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 3911da177e4SLinus Torvalds if (!n) 3921da177e4SLinus Torvalds return NULL; 393d179cd12SDavid S. Miller n->fclone = SKB_FCLONE_UNAVAILABLE; 394d179cd12SDavid S. Miller } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds #define C(x) n->x = skb->x 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds n->next = n->prev = NULL; 3991da177e4SLinus Torvalds n->sk = NULL; 400a61bbcf2SPatrick McHardy C(tstamp); 4011da177e4SLinus Torvalds C(dev); 402b0e380b1SArnaldo Carvalho de Melo C(transport_header); 403b0e380b1SArnaldo Carvalho de Melo C(network_header); 404b0e380b1SArnaldo Carvalho de Melo C(mac_header); 4051da177e4SLinus Torvalds C(dst); 4061da177e4SLinus Torvalds dst_clone(skb->dst); 4071da177e4SLinus Torvalds C(sp); 4081da177e4SLinus Torvalds #ifdef CONFIG_INET 4091da177e4SLinus Torvalds secpath_get(skb->sp); 4101da177e4SLinus Torvalds #endif 4111da177e4SLinus Torvalds memcpy(n->cb, skb->cb, sizeof(skb->cb)); 4121da177e4SLinus Torvalds C(len); 4131da177e4SLinus Torvalds C(data_len); 4143e6b3b2eSAlexey Dobriyan C(mac_len); 4151da177e4SLinus Torvalds C(csum); 4161da177e4SLinus Torvalds C(local_df); 4171da177e4SLinus Torvalds n->cloned = 1; 4181da177e4SLinus Torvalds n->nohdr = 0; 4191da177e4SLinus Torvalds C(pkt_type); 4201da177e4SLinus Torvalds C(ip_summed); 4211da177e4SLinus Torvalds C(priority); 422a8372f03SYOSHIFUJI Hideaki #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 423a8372f03SYOSHIFUJI Hideaki C(ipvs_property); 424a8372f03SYOSHIFUJI Hideaki #endif 4251da177e4SLinus Torvalds C(protocol); 4261da177e4SLinus Torvalds n->destructor = NULL; 42782e91ffeSThomas Graf C(mark); 428edda553cSYasuyuki Kozakai __nf_copy(n, skb); 4291da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4301da177e4SLinus Torvalds C(tc_index); 4311da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 4321da177e4SLinus Torvalds n->tc_verd = SET_TC_VERD(skb->tc_verd,0); 433b72f6eccSDavid S. Miller n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd); 434b72f6eccSDavid S. Miller n->tc_verd = CLR_TC_MUNGED(n->tc_verd); 435c01003c2SPatrick McHardy C(iif); 4361da177e4SLinus Torvalds #endif 437984bc16cSJames Morris skb_copy_secmark(n, skb); 4381da177e4SLinus Torvalds #endif 4391da177e4SLinus Torvalds C(truesize); 4401da177e4SLinus Torvalds atomic_set(&n->users, 1); 4411da177e4SLinus Torvalds C(head); 4421da177e4SLinus Torvalds C(data); 4431da177e4SLinus Torvalds C(tail); 4441da177e4SLinus Torvalds C(end); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 4471da177e4SLinus Torvalds skb->cloned = 1; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds return n; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 4531da177e4SLinus Torvalds { 4542e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Shift between the two data areas in bytes 4571da177e4SLinus Torvalds */ 4581da177e4SLinus Torvalds unsigned long offset = new->data - old->data; 4592e07fa9cSArnaldo Carvalho de Melo #endif 4601da177e4SLinus Torvalds new->sk = NULL; 4611da177e4SLinus Torvalds new->dev = old->dev; 4621da177e4SLinus Torvalds new->priority = old->priority; 4631da177e4SLinus Torvalds new->protocol = old->protocol; 4641da177e4SLinus Torvalds new->dst = dst_clone(old->dst); 4651da177e4SLinus Torvalds #ifdef CONFIG_INET 4661da177e4SLinus Torvalds new->sp = secpath_get(old->sp); 4671da177e4SLinus Torvalds #endif 4682e07fa9cSArnaldo Carvalho de Melo new->transport_header = old->transport_header; 4692e07fa9cSArnaldo Carvalho de Melo new->network_header = old->network_header; 4702e07fa9cSArnaldo Carvalho de Melo new->mac_header = old->mac_header; 4712e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET 4722e07fa9cSArnaldo Carvalho de Melo /* {transport,network,mac}_header are relative to skb->head */ 4732e07fa9cSArnaldo Carvalho de Melo new->transport_header += offset; 4742e07fa9cSArnaldo Carvalho de Melo new->network_header += offset; 4752e07fa9cSArnaldo Carvalho de Melo new->mac_header += offset; 4762e07fa9cSArnaldo Carvalho de Melo #endif 4771da177e4SLinus Torvalds memcpy(new->cb, old->cb, sizeof(old->cb)); 4781da177e4SLinus Torvalds new->local_df = old->local_df; 479d179cd12SDavid S. Miller new->fclone = SKB_FCLONE_UNAVAILABLE; 4801da177e4SLinus Torvalds new->pkt_type = old->pkt_type; 481a61bbcf2SPatrick McHardy new->tstamp = old->tstamp; 4821da177e4SLinus Torvalds new->destructor = NULL; 48382e91ffeSThomas Graf new->mark = old->mark; 484edda553cSYasuyuki Kozakai __nf_copy(new, old); 485c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 486c98d80edSJulian Anastasov new->ipvs_property = old->ipvs_property; 487c98d80edSJulian Anastasov #endif 4881da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 4891da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 4901da177e4SLinus Torvalds new->tc_verd = old->tc_verd; 4911da177e4SLinus Torvalds #endif 4921da177e4SLinus Torvalds new->tc_index = old->tc_index; 4931da177e4SLinus Torvalds #endif 494984bc16cSJames Morris skb_copy_secmark(new, old); 4951da177e4SLinus Torvalds atomic_set(&new->users, 1); 4967967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 4977967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 4987967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /** 5021da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 5031da177e4SLinus Torvalds * @skb: buffer to copy 5041da177e4SLinus Torvalds * @gfp_mask: allocation priority 5051da177e4SLinus Torvalds * 5061da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 5071da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 5081da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 5091da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 5101da177e4SLinus Torvalds * 5111da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 5121da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 5131da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 5141da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 5151da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 5161da177e4SLinus Torvalds */ 5171da177e4SLinus Torvalds 518dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 5191da177e4SLinus Torvalds { 5201da177e4SLinus Torvalds int headerlen = skb->data - skb->head; 5211da177e4SLinus Torvalds /* 5221da177e4SLinus Torvalds * Allocate the copy buffer 5231da177e4SLinus Torvalds */ 5244305b541SArnaldo Carvalho de Melo struct sk_buff *n; 5254305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 5264305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end + skb->data_len, gfp_mask); 5274305b541SArnaldo Carvalho de Melo #else 5284305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 5294305b541SArnaldo Carvalho de Melo #endif 5301da177e4SLinus Torvalds if (!n) 5311da177e4SLinus Torvalds return NULL; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds /* Set the data pointer */ 5341da177e4SLinus Torvalds skb_reserve(n, headerlen); 5351da177e4SLinus Torvalds /* Set the tail pointer and length */ 5361da177e4SLinus Torvalds skb_put(n, skb->len); 5371da177e4SLinus Torvalds n->csum = skb->csum; 5381da177e4SLinus Torvalds n->ip_summed = skb->ip_summed; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 5411da177e4SLinus Torvalds BUG(); 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds copy_skb_header(n, skb); 5441da177e4SLinus Torvalds return n; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /** 5491da177e4SLinus Torvalds * pskb_copy - create copy of an sk_buff with private head. 5501da177e4SLinus Torvalds * @skb: buffer to copy 5511da177e4SLinus Torvalds * @gfp_mask: allocation priority 5521da177e4SLinus Torvalds * 5531da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 5541da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 5551da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 5561da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 5571da177e4SLinus Torvalds * or the pointer to the buffer on success. 5581da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 5591da177e4SLinus Torvalds */ 5601da177e4SLinus Torvalds 561dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 5621da177e4SLinus Torvalds { 5631da177e4SLinus Torvalds /* 5641da177e4SLinus Torvalds * Allocate the copy buffer 5651da177e4SLinus Torvalds */ 5664305b541SArnaldo Carvalho de Melo struct sk_buff *n; 5674305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 5684305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end, gfp_mask); 5694305b541SArnaldo Carvalho de Melo #else 5704305b541SArnaldo Carvalho de Melo n = alloc_skb(skb->end - skb->head, gfp_mask); 5714305b541SArnaldo Carvalho de Melo #endif 5721da177e4SLinus Torvalds if (!n) 5731da177e4SLinus Torvalds goto out; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* Set the data pointer */ 5761da177e4SLinus Torvalds skb_reserve(n, skb->data - skb->head); 5771da177e4SLinus Torvalds /* Set the tail pointer and length */ 5781da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 5791da177e4SLinus Torvalds /* Copy the bytes */ 580d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 5811da177e4SLinus Torvalds n->csum = skb->csum; 5821da177e4SLinus Torvalds n->ip_summed = skb->ip_summed; 5831da177e4SLinus Torvalds 58425f484a6SHerbert Xu n->truesize += skb->data_len; 5851da177e4SLinus Torvalds n->data_len = skb->data_len; 5861da177e4SLinus Torvalds n->len = skb->len; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 5891da177e4SLinus Torvalds int i; 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 5921da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 5931da177e4SLinus Torvalds get_page(skb_shinfo(n)->frags[i].page); 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 5991da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 6001da177e4SLinus Torvalds skb_clone_fraglist(n); 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds copy_skb_header(n, skb); 6041da177e4SLinus Torvalds out: 6051da177e4SLinus Torvalds return n; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /** 6091da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 6101da177e4SLinus Torvalds * @skb: buffer to reallocate 6111da177e4SLinus Torvalds * @nhead: room to add at head 6121da177e4SLinus Torvalds * @ntail: room to add at tail 6131da177e4SLinus Torvalds * @gfp_mask: allocation priority 6141da177e4SLinus Torvalds * 6151da177e4SLinus Torvalds * Expands (or creates identical copy, if &nhead and &ntail are zero) 6161da177e4SLinus Torvalds * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 6171da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 6181da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 6191da177e4SLinus Torvalds * 6201da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 6211da177e4SLinus Torvalds * reloaded after call to this function. 6221da177e4SLinus Torvalds */ 6231da177e4SLinus Torvalds 62486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 625dd0fc66fSAl Viro gfp_t gfp_mask) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds int i; 6281da177e4SLinus Torvalds u8 *data; 6294305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6304305b541SArnaldo Carvalho de Melo int size = nhead + skb->end + ntail; 6314305b541SArnaldo Carvalho de Melo #else 6321da177e4SLinus Torvalds int size = nhead + (skb->end - skb->head) + ntail; 6334305b541SArnaldo Carvalho de Melo #endif 6341da177e4SLinus Torvalds long off; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (skb_shared(skb)) 6371da177e4SLinus Torvalds BUG(); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 6421da177e4SLinus Torvalds if (!data) 6431da177e4SLinus Torvalds goto nodata; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 6461da177e4SLinus Torvalds * optimized for the cases when header is void. */ 64727a884dcSArnaldo Carvalho de Melo memcpy(data + nhead, skb->head, 6484305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6494305b541SArnaldo Carvalho de Melo skb->tail); 6504305b541SArnaldo Carvalho de Melo #else 6514305b541SArnaldo Carvalho de Melo skb->tail - skb->head); 65227a884dcSArnaldo Carvalho de Melo #endif 6534305b541SArnaldo Carvalho de Melo memcpy(data + size, skb_end_pointer(skb), 6544305b541SArnaldo Carvalho de Melo sizeof(struct skb_shared_info)); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 6571da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) 6601da177e4SLinus Torvalds skb_clone_fraglist(skb); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds skb_release_data(skb); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds off = (data + nhead) - skb->head; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds skb->head = data; 6671da177e4SLinus Torvalds skb->data += off; 6684305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 6694305b541SArnaldo Carvalho de Melo skb->end = size; 67056eb8882SPatrick McHardy off = nhead; 6714305b541SArnaldo Carvalho de Melo #else 6724305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 67356eb8882SPatrick McHardy #endif 67427a884dcSArnaldo Carvalho de Melo /* {transport,network,mac}_header and tail are relative to skb->head */ 67527a884dcSArnaldo Carvalho de Melo skb->tail += off; 676b0e380b1SArnaldo Carvalho de Melo skb->transport_header += off; 677b0e380b1SArnaldo Carvalho de Melo skb->network_header += off; 678b0e380b1SArnaldo Carvalho de Melo skb->mac_header += off; 6791da177e4SLinus Torvalds skb->cloned = 0; 6801da177e4SLinus Torvalds skb->nohdr = 0; 6811da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 6821da177e4SLinus Torvalds return 0; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds nodata: 6851da177e4SLinus Torvalds return -ENOMEM; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 6911da177e4SLinus Torvalds { 6921da177e4SLinus Torvalds struct sk_buff *skb2; 6931da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds if (delta <= 0) 6961da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 6971da177e4SLinus Torvalds else { 6981da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 6991da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 7001da177e4SLinus Torvalds GFP_ATOMIC)) { 7011da177e4SLinus Torvalds kfree_skb(skb2); 7021da177e4SLinus Torvalds skb2 = NULL; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds return skb2; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /** 7101da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 7111da177e4SLinus Torvalds * @skb: buffer to copy 7121da177e4SLinus Torvalds * @newheadroom: new free bytes at head 7131da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 7141da177e4SLinus Torvalds * @gfp_mask: allocation priority 7151da177e4SLinus Torvalds * 7161da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 7171da177e4SLinus Torvalds * allocate additional space. 7181da177e4SLinus Torvalds * 7191da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 7201da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 7211da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 7221da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 7231da177e4SLinus Torvalds * 7241da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 7251da177e4SLinus Torvalds * is called from an interrupt. 7261da177e4SLinus Torvalds * 7271da177e4SLinus Torvalds * BUG ALERT: ip_summed is not copied. Why does this work? Is it used 7281da177e4SLinus Torvalds * only by netfilter in the cases when checksum is recalculated? --ANK 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 73186a76cafSVictor Fusco int newheadroom, int newtailroom, 732dd0fc66fSAl Viro gfp_t gfp_mask) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds /* 7351da177e4SLinus Torvalds * Allocate the copy buffer 7361da177e4SLinus Torvalds */ 7371da177e4SLinus Torvalds struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 7381da177e4SLinus Torvalds gfp_mask); 7391da177e4SLinus Torvalds int head_copy_len, head_copy_off; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds if (!n) 7421da177e4SLinus Torvalds return NULL; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds skb_reserve(n, newheadroom); 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* Set the tail pointer and length */ 7471da177e4SLinus Torvalds skb_put(n, skb->len); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds head_copy_len = skb_headroom(skb); 7501da177e4SLinus Torvalds head_copy_off = 0; 7511da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 7521da177e4SLinus Torvalds head_copy_len = newheadroom; 7531da177e4SLinus Torvalds else 7541da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /* Copy the linear header and data. */ 7571da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 7581da177e4SLinus Torvalds skb->len + head_copy_len)) 7591da177e4SLinus Torvalds BUG(); 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds copy_skb_header(n, skb); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds return n; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /** 7671da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 7681da177e4SLinus Torvalds * @skb: buffer to pad 7691da177e4SLinus Torvalds * @pad: space to pad 7701da177e4SLinus Torvalds * 7711da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 7721da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 7731da177e4SLinus Torvalds * beyond the buffer end onto the wire. 7741da177e4SLinus Torvalds * 7755b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 7761da177e4SLinus Torvalds */ 7771da177e4SLinus Torvalds 7785b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 7791da177e4SLinus Torvalds { 7805b057c6bSHerbert Xu int err; 7815b057c6bSHerbert Xu int ntail; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 7845b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 7851da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 7865b057c6bSHerbert Xu return 0; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7894305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 7905b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 7915b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 7925b057c6bSHerbert Xu if (unlikely(err)) 7935b057c6bSHerbert Xu goto free_skb; 7945b057c6bSHerbert Xu } 7955b057c6bSHerbert Xu 7965b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 7975b057c6bSHerbert Xu * to be audited. 7985b057c6bSHerbert Xu */ 7995b057c6bSHerbert Xu err = skb_linearize(skb); 8005b057c6bSHerbert Xu if (unlikely(err)) 8015b057c6bSHerbert Xu goto free_skb; 8025b057c6bSHerbert Xu 8035b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 8045b057c6bSHerbert Xu return 0; 8055b057c6bSHerbert Xu 8065b057c6bSHerbert Xu free_skb: 8071da177e4SLinus Torvalds kfree_skb(skb); 8085b057c6bSHerbert Xu return err; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8113cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds 8143cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 8151da177e4SLinus Torvalds { 81627b437c8SHerbert Xu struct sk_buff **fragp; 81727b437c8SHerbert Xu struct sk_buff *frag; 8181da177e4SLinus Torvalds int offset = skb_headlen(skb); 8191da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 8201da177e4SLinus Torvalds int i; 82127b437c8SHerbert Xu int err; 82227b437c8SHerbert Xu 82327b437c8SHerbert Xu if (skb_cloned(skb) && 82427b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 82527b437c8SHerbert Xu return err; 8261da177e4SLinus Torvalds 827f4d26fb3SHerbert Xu i = 0; 828f4d26fb3SHerbert Xu if (offset >= len) 829f4d26fb3SHerbert Xu goto drop_pages; 830f4d26fb3SHerbert Xu 831f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 8321da177e4SLinus Torvalds int end = offset + skb_shinfo(skb)->frags[i].size; 83327b437c8SHerbert Xu 83427b437c8SHerbert Xu if (end < len) { 8351da177e4SLinus Torvalds offset = end; 83627b437c8SHerbert Xu continue; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 83927b437c8SHerbert Xu skb_shinfo(skb)->frags[i++].size = len - offset; 84027b437c8SHerbert Xu 841f4d26fb3SHerbert Xu drop_pages: 84227b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 84327b437c8SHerbert Xu 84427b437c8SHerbert Xu for (; i < nfrags; i++) 84527b437c8SHerbert Xu put_page(skb_shinfo(skb)->frags[i].page); 84627b437c8SHerbert Xu 84727b437c8SHerbert Xu if (skb_shinfo(skb)->frag_list) 84827b437c8SHerbert Xu skb_drop_fraglist(skb); 849f4d26fb3SHerbert Xu goto done; 85027b437c8SHerbert Xu } 85127b437c8SHerbert Xu 85227b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 85327b437c8SHerbert Xu fragp = &frag->next) { 85427b437c8SHerbert Xu int end = offset + frag->len; 85527b437c8SHerbert Xu 85627b437c8SHerbert Xu if (skb_shared(frag)) { 85727b437c8SHerbert Xu struct sk_buff *nfrag; 85827b437c8SHerbert Xu 85927b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 86027b437c8SHerbert Xu if (unlikely(!nfrag)) 86127b437c8SHerbert Xu return -ENOMEM; 86227b437c8SHerbert Xu 86327b437c8SHerbert Xu nfrag->next = frag->next; 864f4d26fb3SHerbert Xu kfree_skb(frag); 86527b437c8SHerbert Xu frag = nfrag; 86627b437c8SHerbert Xu *fragp = frag; 86727b437c8SHerbert Xu } 86827b437c8SHerbert Xu 86927b437c8SHerbert Xu if (end < len) { 87027b437c8SHerbert Xu offset = end; 87127b437c8SHerbert Xu continue; 87227b437c8SHerbert Xu } 87327b437c8SHerbert Xu 87427b437c8SHerbert Xu if (end > len && 87527b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 87627b437c8SHerbert Xu return err; 87727b437c8SHerbert Xu 87827b437c8SHerbert Xu if (frag->next) 87927b437c8SHerbert Xu skb_drop_list(&frag->next); 88027b437c8SHerbert Xu break; 88127b437c8SHerbert Xu } 88227b437c8SHerbert Xu 883f4d26fb3SHerbert Xu done: 88427b437c8SHerbert Xu if (len > skb_headlen(skb)) { 8851da177e4SLinus Torvalds skb->data_len -= skb->len - len; 8861da177e4SLinus Torvalds skb->len = len; 8871da177e4SLinus Torvalds } else { 8881da177e4SLinus Torvalds skb->len = len; 8891da177e4SLinus Torvalds skb->data_len = 0; 89027a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds return 0; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /** 8971da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 8981da177e4SLinus Torvalds * @skb: buffer to reallocate 8991da177e4SLinus Torvalds * @delta: number of bytes to advance tail 9001da177e4SLinus Torvalds * 9011da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 9021da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 9031da177e4SLinus Torvalds * data from fragmented part. 9041da177e4SLinus Torvalds * 9051da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 9061da177e4SLinus Torvalds * 9071da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 9081da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 9091da177e4SLinus Torvalds * 9101da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 9111da177e4SLinus Torvalds * reloaded after call to this function. 9121da177e4SLinus Torvalds */ 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 9151da177e4SLinus Torvalds * when it is necessary. 9161da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 9171da177e4SLinus Torvalds * 2. It may change skb pointers. 9181da177e4SLinus Torvalds * 9191da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 9221da177e4SLinus Torvalds { 9231da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 9241da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 9251da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 9261da177e4SLinus Torvalds */ 9274305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 9301da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 9311da177e4SLinus Torvalds GFP_ATOMIC)) 9321da177e4SLinus Torvalds return NULL; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 93527a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 9361da177e4SLinus Torvalds BUG(); 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 9391da177e4SLinus Torvalds * size of pulled pages. Superb. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds if (!skb_shinfo(skb)->frag_list) 9421da177e4SLinus Torvalds goto pull_pages; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 9451da177e4SLinus Torvalds eat = delta; 9461da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 9471da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size >= eat) 9481da177e4SLinus Torvalds goto pull_pages; 9491da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 9531da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 9541da177e4SLinus Torvalds * but taking into account that pulling is expected to 9551da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 9561da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 9571da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 9581da177e4SLinus Torvalds */ 9591da177e4SLinus Torvalds if (eat) { 9601da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 9611da177e4SLinus Torvalds struct sk_buff *clone = NULL; 9621da177e4SLinus Torvalds struct sk_buff *insp = NULL; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds do { 96509a62660SKris Katterjohn BUG_ON(!list); 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds if (list->len <= eat) { 9681da177e4SLinus Torvalds /* Eaten as whole. */ 9691da177e4SLinus Torvalds eat -= list->len; 9701da177e4SLinus Torvalds list = list->next; 9711da177e4SLinus Torvalds insp = list; 9721da177e4SLinus Torvalds } else { 9731da177e4SLinus Torvalds /* Eaten partially. */ 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds if (skb_shared(list)) { 9761da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 9771da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 9781da177e4SLinus Torvalds if (!clone) 9791da177e4SLinus Torvalds return NULL; 9801da177e4SLinus Torvalds insp = list->next; 9811da177e4SLinus Torvalds list = clone; 9821da177e4SLinus Torvalds } else { 9831da177e4SLinus Torvalds /* This may be pulled without 9841da177e4SLinus Torvalds * problems. */ 9851da177e4SLinus Torvalds insp = list; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 9881da177e4SLinus Torvalds if (clone) 9891da177e4SLinus Torvalds kfree_skb(clone); 9901da177e4SLinus Torvalds return NULL; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds break; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds } while (eat); 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /* Free pulled out fragments. */ 9971da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 9981da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 9991da177e4SLinus Torvalds kfree_skb(list); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds /* And insert new clone at head. */ 10021da177e4SLinus Torvalds if (clone) { 10031da177e4SLinus Torvalds clone->next = list; 10041da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds pull_pages: 10101da177e4SLinus Torvalds eat = delta; 10111da177e4SLinus Torvalds k = 0; 10121da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10131da177e4SLinus Torvalds if (skb_shinfo(skb)->frags[i].size <= eat) { 10141da177e4SLinus Torvalds put_page(skb_shinfo(skb)->frags[i].page); 10151da177e4SLinus Torvalds eat -= skb_shinfo(skb)->frags[i].size; 10161da177e4SLinus Torvalds } else { 10171da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 10181da177e4SLinus Torvalds if (eat) { 10191da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 10201da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].size -= eat; 10211da177e4SLinus Torvalds eat = 0; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds k++; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds skb->tail += delta; 10291da177e4SLinus Torvalds skb->data_len -= delta; 10301da177e4SLinus Torvalds 103127a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */ 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds int i, copy; 10391da177e4SLinus Torvalds int start = skb_headlen(skb); 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds if (offset > (int)skb->len - len) 10421da177e4SLinus Torvalds goto fault; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* Copy header. */ 10451da177e4SLinus Torvalds if ((copy = start - offset) > 0) { 10461da177e4SLinus Torvalds if (copy > len) 10471da177e4SLinus Torvalds copy = len; 1048d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 10491da177e4SLinus Torvalds if ((len -= copy) == 0) 10501da177e4SLinus Torvalds return 0; 10511da177e4SLinus Torvalds offset += copy; 10521da177e4SLinus Torvalds to += copy; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10561da177e4SLinus Torvalds int end; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds end = start + skb_shinfo(skb)->frags[i].size; 10611da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 10621da177e4SLinus Torvalds u8 *vaddr; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds if (copy > len) 10651da177e4SLinus Torvalds copy = len; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 10681da177e4SLinus Torvalds memcpy(to, 10691da177e4SLinus Torvalds vaddr + skb_shinfo(skb)->frags[i].page_offset+ 10701da177e4SLinus Torvalds offset - start, copy); 10711da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if ((len -= copy) == 0) 10741da177e4SLinus Torvalds return 0; 10751da177e4SLinus Torvalds offset += copy; 10761da177e4SLinus Torvalds to += copy; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds start = end; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 10821da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds for (; list; list = list->next) { 10851da177e4SLinus Torvalds int end; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds end = start + list->len; 10901da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 10911da177e4SLinus Torvalds if (copy > len) 10921da177e4SLinus Torvalds copy = len; 10931da177e4SLinus Torvalds if (skb_copy_bits(list, offset - start, 10941da177e4SLinus Torvalds to, copy)) 10951da177e4SLinus Torvalds goto fault; 10961da177e4SLinus Torvalds if ((len -= copy) == 0) 10971da177e4SLinus Torvalds return 0; 10981da177e4SLinus Torvalds offset += copy; 10991da177e4SLinus Torvalds to += copy; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds start = end; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds if (!len) 11051da177e4SLinus Torvalds return 0; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds fault: 11081da177e4SLinus Torvalds return -EFAULT; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 1111357b40a1SHerbert Xu /** 1112357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1113357b40a1SHerbert Xu * @skb: destination buffer 1114357b40a1SHerbert Xu * @offset: offset in destination 1115357b40a1SHerbert Xu * @from: source buffer 1116357b40a1SHerbert Xu * @len: number of bytes to copy 1117357b40a1SHerbert Xu * 1118357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1119357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1120357b40a1SHerbert Xu * traversing fragment lists and such. 1121357b40a1SHerbert Xu */ 1122357b40a1SHerbert Xu 1123357b40a1SHerbert Xu int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len) 1124357b40a1SHerbert Xu { 1125357b40a1SHerbert Xu int i, copy; 1126357b40a1SHerbert Xu int start = skb_headlen(skb); 1127357b40a1SHerbert Xu 1128357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1129357b40a1SHerbert Xu goto fault; 1130357b40a1SHerbert Xu 1131357b40a1SHerbert Xu if ((copy = start - offset) > 0) { 1132357b40a1SHerbert Xu if (copy > len) 1133357b40a1SHerbert Xu copy = len; 113427d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1135357b40a1SHerbert Xu if ((len -= copy) == 0) 1136357b40a1SHerbert Xu return 0; 1137357b40a1SHerbert Xu offset += copy; 1138357b40a1SHerbert Xu from += copy; 1139357b40a1SHerbert Xu } 1140357b40a1SHerbert Xu 1141357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1142357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1143357b40a1SHerbert Xu int end; 1144357b40a1SHerbert Xu 1145357b40a1SHerbert Xu BUG_TRAP(start <= offset + len); 1146357b40a1SHerbert Xu 1147357b40a1SHerbert Xu end = start + frag->size; 1148357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1149357b40a1SHerbert Xu u8 *vaddr; 1150357b40a1SHerbert Xu 1151357b40a1SHerbert Xu if (copy > len) 1152357b40a1SHerbert Xu copy = len; 1153357b40a1SHerbert Xu 1154357b40a1SHerbert Xu vaddr = kmap_skb_frag(frag); 1155357b40a1SHerbert Xu memcpy(vaddr + frag->page_offset + offset - start, 1156357b40a1SHerbert Xu from, copy); 1157357b40a1SHerbert Xu kunmap_skb_frag(vaddr); 1158357b40a1SHerbert Xu 1159357b40a1SHerbert Xu if ((len -= copy) == 0) 1160357b40a1SHerbert Xu return 0; 1161357b40a1SHerbert Xu offset += copy; 1162357b40a1SHerbert Xu from += copy; 1163357b40a1SHerbert Xu } 1164357b40a1SHerbert Xu start = end; 1165357b40a1SHerbert Xu } 1166357b40a1SHerbert Xu 1167357b40a1SHerbert Xu if (skb_shinfo(skb)->frag_list) { 1168357b40a1SHerbert Xu struct sk_buff *list = skb_shinfo(skb)->frag_list; 1169357b40a1SHerbert Xu 1170357b40a1SHerbert Xu for (; list; list = list->next) { 1171357b40a1SHerbert Xu int end; 1172357b40a1SHerbert Xu 1173357b40a1SHerbert Xu BUG_TRAP(start <= offset + len); 1174357b40a1SHerbert Xu 1175357b40a1SHerbert Xu end = start + list->len; 1176357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1177357b40a1SHerbert Xu if (copy > len) 1178357b40a1SHerbert Xu copy = len; 1179357b40a1SHerbert Xu if (skb_store_bits(list, offset - start, 1180357b40a1SHerbert Xu from, copy)) 1181357b40a1SHerbert Xu goto fault; 1182357b40a1SHerbert Xu if ((len -= copy) == 0) 1183357b40a1SHerbert Xu return 0; 1184357b40a1SHerbert Xu offset += copy; 1185357b40a1SHerbert Xu from += copy; 1186357b40a1SHerbert Xu } 1187357b40a1SHerbert Xu start = end; 1188357b40a1SHerbert Xu } 1189357b40a1SHerbert Xu } 1190357b40a1SHerbert Xu if (!len) 1191357b40a1SHerbert Xu return 0; 1192357b40a1SHerbert Xu 1193357b40a1SHerbert Xu fault: 1194357b40a1SHerbert Xu return -EFAULT; 1195357b40a1SHerbert Xu } 1196357b40a1SHerbert Xu 1197357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1198357b40a1SHerbert Xu 11991da177e4SLinus Torvalds /* Checksum skb data. */ 12001da177e4SLinus Torvalds 12012bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset, 12022bbbc868SAl Viro int len, __wsum csum) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds int start = skb_headlen(skb); 12051da177e4SLinus Torvalds int i, copy = start - offset; 12061da177e4SLinus Torvalds int pos = 0; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* Checksum header. */ 12091da177e4SLinus Torvalds if (copy > 0) { 12101da177e4SLinus Torvalds if (copy > len) 12111da177e4SLinus Torvalds copy = len; 12121da177e4SLinus Torvalds csum = csum_partial(skb->data + offset, copy, csum); 12131da177e4SLinus Torvalds if ((len -= copy) == 0) 12141da177e4SLinus Torvalds return csum; 12151da177e4SLinus Torvalds offset += copy; 12161da177e4SLinus Torvalds pos = copy; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12201da177e4SLinus Torvalds int end; 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds end = start + skb_shinfo(skb)->frags[i].size; 12251da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 122644bb9363SAl Viro __wsum csum2; 12271da177e4SLinus Torvalds u8 *vaddr; 12281da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds if (copy > len) 12311da177e4SLinus Torvalds copy = len; 12321da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 12331da177e4SLinus Torvalds csum2 = csum_partial(vaddr + frag->page_offset + 12341da177e4SLinus Torvalds offset - start, copy, 0); 12351da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 12361da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 12371da177e4SLinus Torvalds if (!(len -= copy)) 12381da177e4SLinus Torvalds return csum; 12391da177e4SLinus Torvalds offset += copy; 12401da177e4SLinus Torvalds pos += copy; 12411da177e4SLinus Torvalds } 12421da177e4SLinus Torvalds start = end; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 12461da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds for (; list; list = list->next) { 12491da177e4SLinus Torvalds int end; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds end = start + list->len; 12541da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 12555f92a738SAl Viro __wsum csum2; 12561da177e4SLinus Torvalds if (copy > len) 12571da177e4SLinus Torvalds copy = len; 12581da177e4SLinus Torvalds csum2 = skb_checksum(list, offset - start, 12591da177e4SLinus Torvalds copy, 0); 12601da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 12611da177e4SLinus Torvalds if ((len -= copy) == 0) 12621da177e4SLinus Torvalds return csum; 12631da177e4SLinus Torvalds offset += copy; 12641da177e4SLinus Torvalds pos += copy; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds start = end; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds } 126909a62660SKris Katterjohn BUG_ON(len); 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds return csum; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds /* Both of above in one bottle. */ 12751da177e4SLinus Torvalds 127681d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 127781d77662SAl Viro u8 *to, int len, __wsum csum) 12781da177e4SLinus Torvalds { 12791da177e4SLinus Torvalds int start = skb_headlen(skb); 12801da177e4SLinus Torvalds int i, copy = start - offset; 12811da177e4SLinus Torvalds int pos = 0; 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds /* Copy header. */ 12841da177e4SLinus Torvalds if (copy > 0) { 12851da177e4SLinus Torvalds if (copy > len) 12861da177e4SLinus Torvalds copy = len; 12871da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 12881da177e4SLinus Torvalds copy, csum); 12891da177e4SLinus Torvalds if ((len -= copy) == 0) 12901da177e4SLinus Torvalds return csum; 12911da177e4SLinus Torvalds offset += copy; 12921da177e4SLinus Torvalds to += copy; 12931da177e4SLinus Torvalds pos = copy; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 12971da177e4SLinus Torvalds int end; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds end = start + skb_shinfo(skb)->frags[i].size; 13021da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 13035084205fSAl Viro __wsum csum2; 13041da177e4SLinus Torvalds u8 *vaddr; 13051da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds if (copy > len) 13081da177e4SLinus Torvalds copy = len; 13091da177e4SLinus Torvalds vaddr = kmap_skb_frag(frag); 13101da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 13111da177e4SLinus Torvalds frag->page_offset + 13121da177e4SLinus Torvalds offset - start, to, 13131da177e4SLinus Torvalds copy, 0); 13141da177e4SLinus Torvalds kunmap_skb_frag(vaddr); 13151da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 13161da177e4SLinus Torvalds if (!(len -= copy)) 13171da177e4SLinus Torvalds return csum; 13181da177e4SLinus Torvalds offset += copy; 13191da177e4SLinus Torvalds to += copy; 13201da177e4SLinus Torvalds pos += copy; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds start = end; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds if (skb_shinfo(skb)->frag_list) { 13261da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds for (; list; list = list->next) { 132981d77662SAl Viro __wsum csum2; 13301da177e4SLinus Torvalds int end; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds BUG_TRAP(start <= offset + len); 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds end = start + list->len; 13351da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 13361da177e4SLinus Torvalds if (copy > len) 13371da177e4SLinus Torvalds copy = len; 13381da177e4SLinus Torvalds csum2 = skb_copy_and_csum_bits(list, 13391da177e4SLinus Torvalds offset - start, 13401da177e4SLinus Torvalds to, copy, 0); 13411da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 13421da177e4SLinus Torvalds if ((len -= copy) == 0) 13431da177e4SLinus Torvalds return csum; 13441da177e4SLinus Torvalds offset += copy; 13451da177e4SLinus Torvalds to += copy; 13461da177e4SLinus Torvalds pos += copy; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds start = end; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds } 135109a62660SKris Katterjohn BUG_ON(len); 13521da177e4SLinus Torvalds return csum; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 13561da177e4SLinus Torvalds { 1357d3bc23e7SAl Viro __wsum csum; 13581da177e4SLinus Torvalds long csstart; 13591da177e4SLinus Torvalds 136084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 1361ea2ae17dSArnaldo Carvalho de Melo csstart = skb_transport_offset(skb); 13621da177e4SLinus Torvalds else 13631da177e4SLinus Torvalds csstart = skb_headlen(skb); 13641da177e4SLinus Torvalds 136509a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 13661da177e4SLinus Torvalds 1367d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds csum = 0; 13701da177e4SLinus Torvalds if (csstart != skb->len) 13711da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 13721da177e4SLinus Torvalds skb->len - csstart, 0); 13731da177e4SLinus Torvalds 137484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 1375ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 13761da177e4SLinus Torvalds 1377d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /** 13821da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 13831da177e4SLinus Torvalds * @list: list to dequeue from 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 13861da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 13871da177e4SLinus Torvalds * returned or %NULL if the list is empty. 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 13911da177e4SLinus Torvalds { 13921da177e4SLinus Torvalds unsigned long flags; 13931da177e4SLinus Torvalds struct sk_buff *result; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 13961da177e4SLinus Torvalds result = __skb_dequeue(list); 13971da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 13981da177e4SLinus Torvalds return result; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /** 14021da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 14031da177e4SLinus Torvalds * @list: list to dequeue from 14041da177e4SLinus Torvalds * 14051da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 14061da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 14071da177e4SLinus Torvalds * returned or %NULL if the list is empty. 14081da177e4SLinus Torvalds */ 14091da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 14101da177e4SLinus Torvalds { 14111da177e4SLinus Torvalds unsigned long flags; 14121da177e4SLinus Torvalds struct sk_buff *result; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 14151da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 14161da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 14171da177e4SLinus Torvalds return result; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /** 14211da177e4SLinus Torvalds * skb_queue_purge - empty a list 14221da177e4SLinus Torvalds * @list: list to empty 14231da177e4SLinus Torvalds * 14241da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 14251da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 14261da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 14271da177e4SLinus Torvalds */ 14281da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 14291da177e4SLinus Torvalds { 14301da177e4SLinus Torvalds struct sk_buff *skb; 14311da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 14321da177e4SLinus Torvalds kfree_skb(skb); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /** 14361da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 14371da177e4SLinus Torvalds * @list: list to use 14381da177e4SLinus Torvalds * @newsk: buffer to queue 14391da177e4SLinus Torvalds * 14401da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 14411da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 14421da177e4SLinus Torvalds * safely. 14431da177e4SLinus Torvalds * 14441da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds unsigned long flags; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 14511da177e4SLinus Torvalds __skb_queue_head(list, newsk); 14521da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds /** 14561da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 14571da177e4SLinus Torvalds * @list: list to use 14581da177e4SLinus Torvalds * @newsk: buffer to queue 14591da177e4SLinus Torvalds * 14601da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 14611da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 14621da177e4SLinus Torvalds * safely. 14631da177e4SLinus Torvalds * 14641da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 14651da177e4SLinus Torvalds */ 14661da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 14671da177e4SLinus Torvalds { 14681da177e4SLinus Torvalds unsigned long flags; 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 14711da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 14721da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 14731da177e4SLinus Torvalds } 14748728b834SDavid S. Miller 14751da177e4SLinus Torvalds /** 14761da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 14771da177e4SLinus Torvalds * @skb: buffer to remove 14788728b834SDavid S. Miller * @list: list to use 14791da177e4SLinus Torvalds * 14808728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 14818728b834SDavid S. Miller * function is atomic with respect to other list locked calls 14821da177e4SLinus Torvalds * 14838728b834SDavid S. Miller * You must know what list the SKB is on. 14841da177e4SLinus Torvalds */ 14858728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 14861da177e4SLinus Torvalds { 14871da177e4SLinus Torvalds unsigned long flags; 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 14908728b834SDavid S. Miller __skb_unlink(skb, list); 14911da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds /** 14951da177e4SLinus Torvalds * skb_append - append a buffer 14961da177e4SLinus Torvalds * @old: buffer to insert after 14971da177e4SLinus Torvalds * @newsk: buffer to insert 14988728b834SDavid S. Miller * @list: list to use 14991da177e4SLinus Torvalds * 15001da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 15011da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 15021da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 15031da177e4SLinus Torvalds */ 15048728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 15051da177e4SLinus Torvalds { 15061da177e4SLinus Torvalds unsigned long flags; 15071da177e4SLinus Torvalds 15088728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 15098728b834SDavid S. Miller __skb_append(old, newsk, list); 15108728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /** 15151da177e4SLinus Torvalds * skb_insert - insert a buffer 15161da177e4SLinus Torvalds * @old: buffer to insert before 15171da177e4SLinus Torvalds * @newsk: buffer to insert 15188728b834SDavid S. Miller * @list: list to use 15191da177e4SLinus Torvalds * 15208728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 15218728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 15228728b834SDavid S. Miller * calls. 15238728b834SDavid S. Miller * 15241da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 15251da177e4SLinus Torvalds */ 15268728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds unsigned long flags; 15291da177e4SLinus Torvalds 15308728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 15318728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 15328728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 15361da177e4SLinus Torvalds struct sk_buff* skb1, 15371da177e4SLinus Torvalds const u32 len, const int pos) 15381da177e4SLinus Torvalds { 15391da177e4SLinus Torvalds int i; 15401da177e4SLinus Torvalds 1541d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1542d626f62bSArnaldo Carvalho de Melo pos - len); 15431da177e4SLinus Torvalds /* And move data appendix as is. */ 15441da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 15451da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 15481da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 15491da177e4SLinus Torvalds skb1->data_len = skb->data_len; 15501da177e4SLinus Torvalds skb1->len += skb1->data_len; 15511da177e4SLinus Torvalds skb->data_len = 0; 15521da177e4SLinus Torvalds skb->len = len; 155327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 15571da177e4SLinus Torvalds struct sk_buff* skb1, 15581da177e4SLinus Torvalds const u32 len, int pos) 15591da177e4SLinus Torvalds { 15601da177e4SLinus Torvalds int i, k = 0; 15611da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 15641da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 15651da177e4SLinus Torvalds skb->len = len; 15661da177e4SLinus Torvalds skb->data_len = len - pos; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 15691da177e4SLinus Torvalds int size = skb_shinfo(skb)->frags[i].size; 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds if (pos + size > len) { 15721da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds if (pos < len) { 15751da177e4SLinus Torvalds /* Split frag. 15761da177e4SLinus Torvalds * We have two variants in this case: 15771da177e4SLinus Torvalds * 1. Move all the frag to the second 15781da177e4SLinus Torvalds * part, if it is possible. F.e. 15791da177e4SLinus Torvalds * this approach is mandatory for TUX, 15801da177e4SLinus Torvalds * where splitting is expensive. 15811da177e4SLinus Torvalds * 2. Split is accurately. We make this. 15821da177e4SLinus Torvalds */ 15831da177e4SLinus Torvalds get_page(skb_shinfo(skb)->frags[i].page); 15841da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 15851da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].size -= len - pos; 15861da177e4SLinus Torvalds skb_shinfo(skb)->frags[i].size = len - pos; 15871da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds k++; 15901da177e4SLinus Torvalds } else 15911da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 15921da177e4SLinus Torvalds pos += size; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 15951da177e4SLinus Torvalds } 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /** 15981da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 15991da177e4SLinus Torvalds * @skb: the buffer to split 16001da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 16011da177e4SLinus Torvalds * @len: new length for skb 16021da177e4SLinus Torvalds */ 16031da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds int pos = skb_headlen(skb); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 16081da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 16091da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 16101da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 1613677e90edSThomas Graf /** 1614677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 1615677e90edSThomas Graf * @skb: the buffer to read 1616677e90edSThomas Graf * @from: lower offset of data to be read 1617677e90edSThomas Graf * @to: upper offset of data to be read 1618677e90edSThomas Graf * @st: state variable 1619677e90edSThomas Graf * 1620677e90edSThomas Graf * Initializes the specified state variable. Must be called before 1621677e90edSThomas Graf * invoking skb_seq_read() for the first time. 1622677e90edSThomas Graf */ 1623677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1624677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 1625677e90edSThomas Graf { 1626677e90edSThomas Graf st->lower_offset = from; 1627677e90edSThomas Graf st->upper_offset = to; 1628677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 1629677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 1630677e90edSThomas Graf st->frag_data = NULL; 1631677e90edSThomas Graf } 1632677e90edSThomas Graf 1633677e90edSThomas Graf /** 1634677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 1635677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 1636677e90edSThomas Graf * @data: destination pointer for data to be returned 1637677e90edSThomas Graf * @st: state variable 1638677e90edSThomas Graf * 1639677e90edSThomas Graf * Reads a block of skb data at &consumed relative to the 1640677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 1641677e90edSThomas Graf * the head of the data block to &data and returns the length 1642677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 1643677e90edSThomas Graf * offset has been reached. 1644677e90edSThomas Graf * 1645677e90edSThomas Graf * The caller is not required to consume all of the data 1646677e90edSThomas Graf * returned, i.e. &consumed is typically set to the number 1647677e90edSThomas Graf * of bytes already consumed and the next call to 1648677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 1649677e90edSThomas Graf * 1650677e90edSThomas Graf * Note: The size of each block of data returned can be arbitary, 1651677e90edSThomas Graf * this limitation is the cost for zerocopy seqeuental 1652677e90edSThomas Graf * reads of potentially non linear data. 1653677e90edSThomas Graf * 1654677e90edSThomas Graf * Note: Fragment lists within fragments are not implemented 1655677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 1656677e90edSThomas Graf * a stack for this purpose. 1657677e90edSThomas Graf */ 1658677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 1659677e90edSThomas Graf struct skb_seq_state *st) 1660677e90edSThomas Graf { 1661677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 1662677e90edSThomas Graf skb_frag_t *frag; 1663677e90edSThomas Graf 1664677e90edSThomas Graf if (unlikely(abs_offset >= st->upper_offset)) 1665677e90edSThomas Graf return 0; 1666677e90edSThomas Graf 1667677e90edSThomas Graf next_skb: 1668677e90edSThomas Graf block_limit = skb_headlen(st->cur_skb); 1669677e90edSThomas Graf 1670677e90edSThomas Graf if (abs_offset < block_limit) { 1671677e90edSThomas Graf *data = st->cur_skb->data + abs_offset; 1672677e90edSThomas Graf return block_limit - abs_offset; 1673677e90edSThomas Graf } 1674677e90edSThomas Graf 1675677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 1676677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 1677677e90edSThomas Graf 1678677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 1679677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 1680677e90edSThomas Graf block_limit = frag->size + st->stepped_offset; 1681677e90edSThomas Graf 1682677e90edSThomas Graf if (abs_offset < block_limit) { 1683677e90edSThomas Graf if (!st->frag_data) 1684677e90edSThomas Graf st->frag_data = kmap_skb_frag(frag); 1685677e90edSThomas Graf 1686677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 1687677e90edSThomas Graf (abs_offset - st->stepped_offset); 1688677e90edSThomas Graf 1689677e90edSThomas Graf return block_limit - abs_offset; 1690677e90edSThomas Graf } 1691677e90edSThomas Graf 1692677e90edSThomas Graf if (st->frag_data) { 1693677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 1694677e90edSThomas Graf st->frag_data = NULL; 1695677e90edSThomas Graf } 1696677e90edSThomas Graf 1697677e90edSThomas Graf st->frag_idx++; 1698677e90edSThomas Graf st->stepped_offset += frag->size; 1699677e90edSThomas Graf } 1700677e90edSThomas Graf 1701677e90edSThomas Graf if (st->cur_skb->next) { 1702677e90edSThomas Graf st->cur_skb = st->cur_skb->next; 1703677e90edSThomas Graf st->frag_idx = 0; 1704677e90edSThomas Graf goto next_skb; 1705677e90edSThomas Graf } else if (st->root_skb == st->cur_skb && 1706677e90edSThomas Graf skb_shinfo(st->root_skb)->frag_list) { 1707677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 1708677e90edSThomas Graf goto next_skb; 1709677e90edSThomas Graf } 1710677e90edSThomas Graf 1711677e90edSThomas Graf return 0; 1712677e90edSThomas Graf } 1713677e90edSThomas Graf 1714677e90edSThomas Graf /** 1715677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 1716677e90edSThomas Graf * @st: state variable 1717677e90edSThomas Graf * 1718677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 1719677e90edSThomas Graf * returned 0. 1720677e90edSThomas Graf */ 1721677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 1722677e90edSThomas Graf { 1723677e90edSThomas Graf if (st->frag_data) 1724677e90edSThomas Graf kunmap_skb_frag(st->frag_data); 1725677e90edSThomas Graf } 1726677e90edSThomas Graf 17273fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 17283fc7e8a6SThomas Graf 17293fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 17303fc7e8a6SThomas Graf struct ts_config *conf, 17313fc7e8a6SThomas Graf struct ts_state *state) 17323fc7e8a6SThomas Graf { 17333fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 17343fc7e8a6SThomas Graf } 17353fc7e8a6SThomas Graf 17363fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 17373fc7e8a6SThomas Graf { 17383fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 17393fc7e8a6SThomas Graf } 17403fc7e8a6SThomas Graf 17413fc7e8a6SThomas Graf /** 17423fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 17433fc7e8a6SThomas Graf * @skb: the buffer to look in 17443fc7e8a6SThomas Graf * @from: search offset 17453fc7e8a6SThomas Graf * @to: search limit 17463fc7e8a6SThomas Graf * @config: textsearch configuration 17473fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 17483fc7e8a6SThomas Graf * 17493fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 17503fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 17513fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 17523fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 17533fc7e8a6SThomas Graf */ 17543fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 17553fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 17563fc7e8a6SThomas Graf struct ts_state *state) 17573fc7e8a6SThomas Graf { 1758f72b948dSPhil Oester unsigned int ret; 1759f72b948dSPhil Oester 17603fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 17613fc7e8a6SThomas Graf config->finish = skb_ts_finish; 17623fc7e8a6SThomas Graf 17633fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 17643fc7e8a6SThomas Graf 1765f72b948dSPhil Oester ret = textsearch_find(config, state); 1766f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 17673fc7e8a6SThomas Graf } 17683fc7e8a6SThomas Graf 1769e89e9cf5SAnanda Raju /** 1770e89e9cf5SAnanda Raju * skb_append_datato_frags: - append the user data to a skb 1771e89e9cf5SAnanda Raju * @sk: sock structure 1772e89e9cf5SAnanda Raju * @skb: skb structure to be appened with user data. 1773e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 1774e89e9cf5SAnanda Raju * @from: pointer to user message iov 1775e89e9cf5SAnanda Raju * @length: length of the iov message 1776e89e9cf5SAnanda Raju * 1777e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 1778e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 1779e89e9cf5SAnanda Raju */ 1780e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 1781dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 1782e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 1783e89e9cf5SAnanda Raju void *from, int length) 1784e89e9cf5SAnanda Raju { 1785e89e9cf5SAnanda Raju int frg_cnt = 0; 1786e89e9cf5SAnanda Raju skb_frag_t *frag = NULL; 1787e89e9cf5SAnanda Raju struct page *page = NULL; 1788e89e9cf5SAnanda Raju int copy, left; 1789e89e9cf5SAnanda Raju int offset = 0; 1790e89e9cf5SAnanda Raju int ret; 1791e89e9cf5SAnanda Raju 1792e89e9cf5SAnanda Raju do { 1793e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 1794e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 1795e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 1796e89e9cf5SAnanda Raju return -EFAULT; 1797e89e9cf5SAnanda Raju 1798e89e9cf5SAnanda Raju /* allocate a new page for next frag */ 1799e89e9cf5SAnanda Raju page = alloc_pages(sk->sk_allocation, 0); 1800e89e9cf5SAnanda Raju 1801e89e9cf5SAnanda Raju /* If alloc_page fails just return failure and caller will 1802e89e9cf5SAnanda Raju * free previous allocated pages by doing kfree_skb() 1803e89e9cf5SAnanda Raju */ 1804e89e9cf5SAnanda Raju if (page == NULL) 1805e89e9cf5SAnanda Raju return -ENOMEM; 1806e89e9cf5SAnanda Raju 1807e89e9cf5SAnanda Raju /* initialize the next frag */ 1808e89e9cf5SAnanda Raju sk->sk_sndmsg_page = page; 1809e89e9cf5SAnanda Raju sk->sk_sndmsg_off = 0; 1810e89e9cf5SAnanda Raju skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 1811e89e9cf5SAnanda Raju skb->truesize += PAGE_SIZE; 1812e89e9cf5SAnanda Raju atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 1813e89e9cf5SAnanda Raju 1814e89e9cf5SAnanda Raju /* get the new initialized frag */ 1815e89e9cf5SAnanda Raju frg_cnt = skb_shinfo(skb)->nr_frags; 1816e89e9cf5SAnanda Raju frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 1817e89e9cf5SAnanda Raju 1818e89e9cf5SAnanda Raju /* copy the user data to page */ 1819e89e9cf5SAnanda Raju left = PAGE_SIZE - frag->page_offset; 1820e89e9cf5SAnanda Raju copy = (length > left)? left : length; 1821e89e9cf5SAnanda Raju 1822e89e9cf5SAnanda Raju ret = getfrag(from, (page_address(frag->page) + 1823e89e9cf5SAnanda Raju frag->page_offset + frag->size), 1824e89e9cf5SAnanda Raju offset, copy, 0, skb); 1825e89e9cf5SAnanda Raju if (ret < 0) 1826e89e9cf5SAnanda Raju return -EFAULT; 1827e89e9cf5SAnanda Raju 1828e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 1829e89e9cf5SAnanda Raju sk->sk_sndmsg_off += copy; 1830e89e9cf5SAnanda Raju frag->size += copy; 1831e89e9cf5SAnanda Raju skb->len += copy; 1832e89e9cf5SAnanda Raju skb->data_len += copy; 1833e89e9cf5SAnanda Raju offset += copy; 1834e89e9cf5SAnanda Raju length -= copy; 1835e89e9cf5SAnanda Raju 1836e89e9cf5SAnanda Raju } while (length > 0); 1837e89e9cf5SAnanda Raju 1838e89e9cf5SAnanda Raju return 0; 1839e89e9cf5SAnanda Raju } 1840e89e9cf5SAnanda Raju 1841cbb042f9SHerbert Xu /** 1842cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 1843cbb042f9SHerbert Xu * @skb: buffer to update 1844cbb042f9SHerbert Xu * @start: start of data before pull 1845cbb042f9SHerbert Xu * @len: length of data pulled 1846cbb042f9SHerbert Xu * 1847cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 184884fa7933SPatrick McHardy * update the CHECKSUM_COMPLETE checksum. It should be used on 184984fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 185084fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 185184fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 1852cbb042f9SHerbert Xu */ 1853cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 1854cbb042f9SHerbert Xu { 1855cbb042f9SHerbert Xu BUG_ON(len > skb->len); 1856cbb042f9SHerbert Xu skb->len -= len; 1857cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 1858cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 1859cbb042f9SHerbert Xu return skb->data += len; 1860cbb042f9SHerbert Xu } 1861cbb042f9SHerbert Xu 1862f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 1863f94691acSArnaldo Carvalho de Melo 1864f4c50d99SHerbert Xu /** 1865f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 1866f4c50d99SHerbert Xu * @skb: buffer to segment 1867576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 1868f4c50d99SHerbert Xu * 1869f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 1870f4c50d99SHerbert Xu * the segment at the given position. It returns NULL if there are 1871f4c50d99SHerbert Xu * no more segments to generate, or when an error is encountered. 1872f4c50d99SHerbert Xu */ 1873576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features) 1874f4c50d99SHerbert Xu { 1875f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 1876f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 1877f4c50d99SHerbert Xu unsigned int mss = skb_shinfo(skb)->gso_size; 187898e399f8SArnaldo Carvalho de Melo unsigned int doffset = skb->data - skb_mac_header(skb); 1879f4c50d99SHerbert Xu unsigned int offset = doffset; 1880f4c50d99SHerbert Xu unsigned int headroom; 1881f4c50d99SHerbert Xu unsigned int len; 1882576a30ebSHerbert Xu int sg = features & NETIF_F_SG; 1883f4c50d99SHerbert Xu int nfrags = skb_shinfo(skb)->nr_frags; 1884f4c50d99SHerbert Xu int err = -ENOMEM; 1885f4c50d99SHerbert Xu int i = 0; 1886f4c50d99SHerbert Xu int pos; 1887f4c50d99SHerbert Xu 1888f4c50d99SHerbert Xu __skb_push(skb, doffset); 1889f4c50d99SHerbert Xu headroom = skb_headroom(skb); 1890f4c50d99SHerbert Xu pos = skb_headlen(skb); 1891f4c50d99SHerbert Xu 1892f4c50d99SHerbert Xu do { 1893f4c50d99SHerbert Xu struct sk_buff *nskb; 1894f4c50d99SHerbert Xu skb_frag_t *frag; 1895c8884eddSHerbert Xu int hsize; 1896f4c50d99SHerbert Xu int k; 1897f4c50d99SHerbert Xu int size; 1898f4c50d99SHerbert Xu 1899f4c50d99SHerbert Xu len = skb->len - offset; 1900f4c50d99SHerbert Xu if (len > mss) 1901f4c50d99SHerbert Xu len = mss; 1902f4c50d99SHerbert Xu 1903f4c50d99SHerbert Xu hsize = skb_headlen(skb) - offset; 1904f4c50d99SHerbert Xu if (hsize < 0) 1905f4c50d99SHerbert Xu hsize = 0; 1906c8884eddSHerbert Xu if (hsize > len || !sg) 1907c8884eddSHerbert Xu hsize = len; 1908f4c50d99SHerbert Xu 1909c8884eddSHerbert Xu nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 1910f4c50d99SHerbert Xu if (unlikely(!nskb)) 1911f4c50d99SHerbert Xu goto err; 1912f4c50d99SHerbert Xu 1913f4c50d99SHerbert Xu if (segs) 1914f4c50d99SHerbert Xu tail->next = nskb; 1915f4c50d99SHerbert Xu else 1916f4c50d99SHerbert Xu segs = nskb; 1917f4c50d99SHerbert Xu tail = nskb; 1918f4c50d99SHerbert Xu 1919f4c50d99SHerbert Xu nskb->dev = skb->dev; 1920f4c50d99SHerbert Xu nskb->priority = skb->priority; 1921f4c50d99SHerbert Xu nskb->protocol = skb->protocol; 1922f4c50d99SHerbert Xu nskb->dst = dst_clone(skb->dst); 1923f4c50d99SHerbert Xu memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 1924f4c50d99SHerbert Xu nskb->pkt_type = skb->pkt_type; 1925f4c50d99SHerbert Xu nskb->mac_len = skb->mac_len; 1926f4c50d99SHerbert Xu 1927f4c50d99SHerbert Xu skb_reserve(nskb, headroom); 1928459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(nskb); 1929ddc7b8e3SArnaldo Carvalho de Melo skb_set_network_header(nskb, skb->mac_len); 1930b0e380b1SArnaldo Carvalho de Melo nskb->transport_header = (nskb->network_header + 1931b0e380b1SArnaldo Carvalho de Melo skb_network_header_len(skb)); 1932d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 1933d626f62bSArnaldo Carvalho de Melo doffset); 1934f4c50d99SHerbert Xu if (!sg) { 1935f4c50d99SHerbert Xu nskb->csum = skb_copy_and_csum_bits(skb, offset, 1936f4c50d99SHerbert Xu skb_put(nskb, len), 1937f4c50d99SHerbert Xu len, 0); 1938f4c50d99SHerbert Xu continue; 1939f4c50d99SHerbert Xu } 1940f4c50d99SHerbert Xu 1941f4c50d99SHerbert Xu frag = skb_shinfo(nskb)->frags; 1942f4c50d99SHerbert Xu k = 0; 1943f4c50d99SHerbert Xu 194484fa7933SPatrick McHardy nskb->ip_summed = CHECKSUM_PARTIAL; 1945f4c50d99SHerbert Xu nskb->csum = skb->csum; 1946d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, 1947d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 1948f4c50d99SHerbert Xu 1949f4c50d99SHerbert Xu while (pos < offset + len) { 1950f4c50d99SHerbert Xu BUG_ON(i >= nfrags); 1951f4c50d99SHerbert Xu 1952f4c50d99SHerbert Xu *frag = skb_shinfo(skb)->frags[i]; 1953f4c50d99SHerbert Xu get_page(frag->page); 1954f4c50d99SHerbert Xu size = frag->size; 1955f4c50d99SHerbert Xu 1956f4c50d99SHerbert Xu if (pos < offset) { 1957f4c50d99SHerbert Xu frag->page_offset += offset - pos; 1958f4c50d99SHerbert Xu frag->size -= offset - pos; 1959f4c50d99SHerbert Xu } 1960f4c50d99SHerbert Xu 1961f4c50d99SHerbert Xu k++; 1962f4c50d99SHerbert Xu 1963f4c50d99SHerbert Xu if (pos + size <= offset + len) { 1964f4c50d99SHerbert Xu i++; 1965f4c50d99SHerbert Xu pos += size; 1966f4c50d99SHerbert Xu } else { 1967f4c50d99SHerbert Xu frag->size -= pos + size - (offset + len); 1968f4c50d99SHerbert Xu break; 1969f4c50d99SHerbert Xu } 1970f4c50d99SHerbert Xu 1971f4c50d99SHerbert Xu frag++; 1972f4c50d99SHerbert Xu } 1973f4c50d99SHerbert Xu 1974f4c50d99SHerbert Xu skb_shinfo(nskb)->nr_frags = k; 1975f4c50d99SHerbert Xu nskb->data_len = len - hsize; 1976f4c50d99SHerbert Xu nskb->len += nskb->data_len; 1977f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 1978f4c50d99SHerbert Xu } while ((offset += len) < skb->len); 1979f4c50d99SHerbert Xu 1980f4c50d99SHerbert Xu return segs; 1981f4c50d99SHerbert Xu 1982f4c50d99SHerbert Xu err: 1983f4c50d99SHerbert Xu while ((skb = segs)) { 1984f4c50d99SHerbert Xu segs = skb->next; 1985b08d5840SPatrick McHardy kfree_skb(skb); 1986f4c50d99SHerbert Xu } 1987f4c50d99SHerbert Xu return ERR_PTR(err); 1988f4c50d99SHerbert Xu } 1989f4c50d99SHerbert Xu 1990f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 1991f4c50d99SHerbert Xu 19921da177e4SLinus Torvalds void __init skb_init(void) 19931da177e4SLinus Torvalds { 19941da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 19951da177e4SLinus Torvalds sizeof(struct sk_buff), 19961da177e4SLinus Torvalds 0, 1997e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 19981da177e4SLinus Torvalds NULL, NULL); 1999d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2000d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 2001d179cd12SDavid S. Miller sizeof(atomic_t), 2002d179cd12SDavid S. Miller 0, 2003e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 2004d179cd12SDavid S. Miller NULL, NULL); 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds 2007716ea3a7SDavid Howells /** 2008716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2009716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 2010716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 2011716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 2012716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 2013716ea3a7SDavid Howells * 2014716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 2015716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 2016716ea3a7SDavid Howells */ 2017716ea3a7SDavid Howells int 2018716ea3a7SDavid Howells skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2019716ea3a7SDavid Howells { 2020716ea3a7SDavid Howells int start = skb_headlen(skb); 2021716ea3a7SDavid Howells int i, copy = start - offset; 2022716ea3a7SDavid Howells int elt = 0; 2023716ea3a7SDavid Howells 2024716ea3a7SDavid Howells if (copy > 0) { 2025716ea3a7SDavid Howells if (copy > len) 2026716ea3a7SDavid Howells copy = len; 2027716ea3a7SDavid Howells sg[elt].page = virt_to_page(skb->data + offset); 2028716ea3a7SDavid Howells sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE; 2029716ea3a7SDavid Howells sg[elt].length = copy; 2030716ea3a7SDavid Howells elt++; 2031716ea3a7SDavid Howells if ((len -= copy) == 0) 2032716ea3a7SDavid Howells return elt; 2033716ea3a7SDavid Howells offset += copy; 2034716ea3a7SDavid Howells } 2035716ea3a7SDavid Howells 2036716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2037716ea3a7SDavid Howells int end; 2038716ea3a7SDavid Howells 2039716ea3a7SDavid Howells BUG_TRAP(start <= offset + len); 2040716ea3a7SDavid Howells 2041716ea3a7SDavid Howells end = start + skb_shinfo(skb)->frags[i].size; 2042716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2043716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2044716ea3a7SDavid Howells 2045716ea3a7SDavid Howells if (copy > len) 2046716ea3a7SDavid Howells copy = len; 2047716ea3a7SDavid Howells sg[elt].page = frag->page; 2048716ea3a7SDavid Howells sg[elt].offset = frag->page_offset+offset-start; 2049716ea3a7SDavid Howells sg[elt].length = copy; 2050716ea3a7SDavid Howells elt++; 2051716ea3a7SDavid Howells if (!(len -= copy)) 2052716ea3a7SDavid Howells return elt; 2053716ea3a7SDavid Howells offset += copy; 2054716ea3a7SDavid Howells } 2055716ea3a7SDavid Howells start = end; 2056716ea3a7SDavid Howells } 2057716ea3a7SDavid Howells 2058716ea3a7SDavid Howells if (skb_shinfo(skb)->frag_list) { 2059716ea3a7SDavid Howells struct sk_buff *list = skb_shinfo(skb)->frag_list; 2060716ea3a7SDavid Howells 2061716ea3a7SDavid Howells for (; list; list = list->next) { 2062716ea3a7SDavid Howells int end; 2063716ea3a7SDavid Howells 2064716ea3a7SDavid Howells BUG_TRAP(start <= offset + len); 2065716ea3a7SDavid Howells 2066716ea3a7SDavid Howells end = start + list->len; 2067716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 2068716ea3a7SDavid Howells if (copy > len) 2069716ea3a7SDavid Howells copy = len; 2070716ea3a7SDavid Howells elt += skb_to_sgvec(list, sg+elt, offset - start, copy); 2071716ea3a7SDavid Howells if ((len -= copy) == 0) 2072716ea3a7SDavid Howells return elt; 2073716ea3a7SDavid Howells offset += copy; 2074716ea3a7SDavid Howells } 2075716ea3a7SDavid Howells start = end; 2076716ea3a7SDavid Howells } 2077716ea3a7SDavid Howells } 2078716ea3a7SDavid Howells BUG_ON(len); 2079716ea3a7SDavid Howells return elt; 2080716ea3a7SDavid Howells } 2081716ea3a7SDavid Howells 2082716ea3a7SDavid Howells /** 2083716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 2084716ea3a7SDavid Howells * @skb: The socket buffer to check. 2085716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 2086716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 2087716ea3a7SDavid Howells * 2088716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 2089716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 2090716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 2091716ea3a7SDavid Howells * 2092716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 2093716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 2094716ea3a7SDavid Howells * set to point to the skb in which this space begins. 2095716ea3a7SDavid Howells * 2096716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 2097716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 2098716ea3a7SDavid Howells */ 2099716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2100716ea3a7SDavid Howells { 2101716ea3a7SDavid Howells int copyflag; 2102716ea3a7SDavid Howells int elt; 2103716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 2104716ea3a7SDavid Howells 2105716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 2106716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 2107716ea3a7SDavid Howells * at the moment even if they are anonymous). 2108716ea3a7SDavid Howells */ 2109716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2110716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2111716ea3a7SDavid Howells return -ENOMEM; 2112716ea3a7SDavid Howells 2113716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 2114716ea3a7SDavid Howells if (!skb_shinfo(skb)->frag_list) { 2115716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 2116716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 2117716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 2118716ea3a7SDavid Howells * space, 128 bytes is fair. */ 2119716ea3a7SDavid Howells 2120716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 2121716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2122716ea3a7SDavid Howells return -ENOMEM; 2123716ea3a7SDavid Howells 2124716ea3a7SDavid Howells /* Voila! */ 2125716ea3a7SDavid Howells *trailer = skb; 2126716ea3a7SDavid Howells return 1; 2127716ea3a7SDavid Howells } 2128716ea3a7SDavid Howells 2129716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 2130716ea3a7SDavid Howells 2131716ea3a7SDavid Howells elt = 1; 2132716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 2133716ea3a7SDavid Howells copyflag = 0; 2134716ea3a7SDavid Howells 2135716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 2136716ea3a7SDavid Howells int ntail = 0; 2137716ea3a7SDavid Howells 2138716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 2139716ea3a7SDavid Howells * this can happen on input. Copy it and everything 2140716ea3a7SDavid Howells * after it. */ 2141716ea3a7SDavid Howells 2142716ea3a7SDavid Howells if (skb_shared(skb1)) 2143716ea3a7SDavid Howells copyflag = 1; 2144716ea3a7SDavid Howells 2145716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 2146716ea3a7SDavid Howells 2147716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 2148716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 2149716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list || 2150716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 2151716ea3a7SDavid Howells ntail = tailbits + 128; 2152716ea3a7SDavid Howells } 2153716ea3a7SDavid Howells 2154716ea3a7SDavid Howells if (copyflag || 2155716ea3a7SDavid Howells skb_cloned(skb1) || 2156716ea3a7SDavid Howells ntail || 2157716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 2158716ea3a7SDavid Howells skb_shinfo(skb1)->frag_list) { 2159716ea3a7SDavid Howells struct sk_buff *skb2; 2160716ea3a7SDavid Howells 2161716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 2162716ea3a7SDavid Howells if (ntail == 0) 2163716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 2164716ea3a7SDavid Howells else 2165716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 2166716ea3a7SDavid Howells skb_headroom(skb1), 2167716ea3a7SDavid Howells ntail, 2168716ea3a7SDavid Howells GFP_ATOMIC); 2169716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 2170716ea3a7SDavid Howells return -ENOMEM; 2171716ea3a7SDavid Howells 2172716ea3a7SDavid Howells if (skb1->sk) 2173716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 2174716ea3a7SDavid Howells 2175716ea3a7SDavid Howells /* Looking around. Are we still alive? 2176716ea3a7SDavid Howells * OK, link new skb, drop old one */ 2177716ea3a7SDavid Howells 2178716ea3a7SDavid Howells skb2->next = skb1->next; 2179716ea3a7SDavid Howells *skb_p = skb2; 2180716ea3a7SDavid Howells kfree_skb(skb1); 2181716ea3a7SDavid Howells skb1 = skb2; 2182716ea3a7SDavid Howells } 2183716ea3a7SDavid Howells elt++; 2184716ea3a7SDavid Howells *trailer = skb1; 2185716ea3a7SDavid Howells skb_p = &skb1->next; 2186716ea3a7SDavid Howells } 2187716ea3a7SDavid Howells 2188716ea3a7SDavid Howells return elt; 2189716ea3a7SDavid Howells } 2190716ea3a7SDavid Howells 21911da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim); 21921da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb); 2193231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb); 21941da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail); 2195d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 21968af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb); 21971da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy); 21981da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head); 21991da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum); 22001da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone); 22011da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone_fraglist); 22021da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy); 22031da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits); 22041da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev); 22051da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits); 22061da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand); 22071da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic); 22081da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad); 22091da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom); 22101da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic); 22111da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue); 22121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail); 22131da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert); 22141da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge); 22151da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head); 22161da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail); 22171da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink); 22181da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append); 22191da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split); 2220677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read); 2221677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read); 2222677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read); 22233fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text); 2224e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags); 2225716ea3a7SDavid Howells 2226716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec); 2227716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data); 2228