Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines having to do with the 'struct sk_buff' memory handlers. |
| 3 | * |
| 4 | * Authors: Alan Cox <iiitac@pyr.swan.ac.uk> |
| 5 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
| 6 | * |
| 7 | * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $ |
| 8 | * |
| 9 | * Fixes: |
| 10 | * Alan Cox : Fixed the worst of the load |
| 11 | * balancer bugs. |
| 12 | * Dave Platt : Interrupt stacking fix. |
| 13 | * Richard Kooijman : Timestamp fixes. |
| 14 | * Alan Cox : Changed buffer format. |
| 15 | * Alan Cox : destructor hook for AF_UNIX etc. |
| 16 | * Linus Torvalds : Better skb_clone. |
| 17 | * Alan Cox : Added skb_copy. |
| 18 | * Alan Cox : Added all the changed routines Linus |
| 19 | * only put in the headers |
| 20 | * Ray VanTassle : Fixed --skb->lock in free |
| 21 | * Alan Cox : skb_copy copy arp field |
| 22 | * Andi Kleen : slabified it. |
| 23 | * Robert Olsson : Removed skb_head_pool |
| 24 | * |
| 25 | * NOTE: |
| 26 | * The __skb_ routines should be called with interrupts |
| 27 | * disabled, or you better be *real* sure that the operation is atomic |
| 28 | * with respect to whatever list is being frobbed (e.g. via lock_sock() |
| 29 | * or via disabling bottom half handlers, etc). |
| 30 | * |
| 31 | * This program is free software; you can redistribute it and/or |
| 32 | * modify it under the terms of the GNU General Public License |
| 33 | * as published by the Free Software Foundation; either version |
| 34 | * 2 of the License, or (at your option) any later version. |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * The functions in this file will not compile correctly with gcc 2.4.x |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/types.h> |
| 43 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/mm.h> |
| 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/in.h> |
| 47 | #include <linux/inet.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/netdevice.h> |
| 50 | #ifdef CONFIG_NET_CLS_ACT |
| 51 | #include <net/pkt_sched.h> |
| 52 | #endif |
| 53 | #include <linux/string.h> |
| 54 | #include <linux/skbuff.h> |
| 55 | #include <linux/cache.h> |
| 56 | #include <linux/rtnetlink.h> |
| 57 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | #include <net/protocol.h> |
| 60 | #include <net/dst.h> |
| 61 | #include <net/sock.h> |
| 62 | #include <net/checksum.h> |
| 63 | #include <net/xfrm.h> |
| 64 | |
| 65 | #include <asm/uaccess.h> |
| 66 | #include <asm/system.h> |
| 67 | |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 68 | #include "kmap_skb.h" |
| 69 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 70 | static struct kmem_cache *skbuff_head_cache __read_mostly; |
| 71 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Keep out-of-line to prevent kernel bloat. |
| 75 | * __builtin_return_address is not used because it is not always |
| 76 | * reliable. |
| 77 | */ |
| 78 | |
| 79 | /** |
| 80 | * skb_over_panic - private function |
| 81 | * @skb: buffer |
| 82 | * @sz: size |
| 83 | * @here: address |
| 84 | * |
| 85 | * Out of line support code for skb_put(). Not user callable. |
| 86 | */ |
| 87 | void skb_over_panic(struct sk_buff *skb, int sz, void *here) |
| 88 | { |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 89 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 90 | "data:%p tail:%#lx end:%p dev:%s\n", |
| 91 | here, skb->len, sz, skb->head, skb->data, |
| 92 | (unsigned long)skb->tail, skb->end, |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 93 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | BUG(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * skb_under_panic - private function |
| 99 | * @skb: buffer |
| 100 | * @sz: size |
| 101 | * @here: address |
| 102 | * |
| 103 | * Out of line support code for skb_push(). Not user callable. |
| 104 | */ |
| 105 | |
| 106 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) |
| 107 | { |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 108 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 109 | "data:%p tail:%#lx end:%p dev:%s\n", |
| 110 | here, skb->len, sz, skb->head, skb->data, |
| 111 | (unsigned long)skb->tail, skb->end, |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 112 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | BUG(); |
| 114 | } |
| 115 | |
David S. Miller | dc6de33 | 2006-04-20 00:10:50 -0700 | [diff] [blame] | 116 | void skb_truesize_bug(struct sk_buff *skb) |
| 117 | { |
| 118 | printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " |
| 119 | "len=%u, sizeof(sk_buff)=%Zd\n", |
| 120 | skb->truesize, skb->len, sizeof(struct sk_buff)); |
| 121 | } |
| 122 | EXPORT_SYMBOL(skb_truesize_bug); |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 125 | * 'private' fields and also do memory statistics to find all the |
| 126 | * [BEEP] leaks. |
| 127 | * |
| 128 | */ |
| 129 | |
| 130 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 131 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * @size: size to allocate |
| 133 | * @gfp_mask: allocation mask |
Randy Dunlap | c83c248 | 2005-10-18 22:07:41 -0700 | [diff] [blame] | 134 | * @fclone: allocate from fclone cache instead of head cache |
| 135 | * and allocate a cloned (child) skb |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 136 | * @node: numa node to allocate memory on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * |
| 138 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
| 139 | * tail room of size bytes. The object has a reference count of one. |
| 140 | * The return is the buffer. On a failure the return is %NULL. |
| 141 | * |
| 142 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 143 | * %GFP_ATOMIC. |
| 144 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 145 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 146 | int fclone, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 148 | struct kmem_cache *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 149 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | struct sk_buff *skb; |
| 151 | u8 *data; |
| 152 | |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 153 | cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; |
| 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* Get the HEAD */ |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 156 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (!skb) |
| 158 | goto out; |
| 159 | |
| 160 | /* Get the DATA. Size must match skb_add_mtu(). */ |
| 161 | size = SKB_DATA_ALIGN(size); |
Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 162 | data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), |
| 163 | gfp_mask, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (!data) |
| 165 | goto nodata; |
| 166 | |
| 167 | memset(skb, 0, offsetof(struct sk_buff, truesize)); |
| 168 | skb->truesize = size + sizeof(struct sk_buff); |
| 169 | atomic_set(&skb->users, 1); |
| 170 | skb->head = data; |
| 171 | skb->data = data; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 172 | skb_reset_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | skb->end = data + size; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 174 | /* make sure we initialize shinfo sequentially */ |
| 175 | shinfo = skb_shinfo(skb); |
| 176 | atomic_set(&shinfo->dataref, 1); |
| 177 | shinfo->nr_frags = 0; |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 178 | shinfo->gso_size = 0; |
| 179 | shinfo->gso_segs = 0; |
| 180 | shinfo->gso_type = 0; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 181 | shinfo->ip6_frag_id = 0; |
| 182 | shinfo->frag_list = NULL; |
| 183 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 184 | if (fclone) { |
| 185 | struct sk_buff *child = skb + 1; |
| 186 | atomic_t *fclone_ref = (atomic_t *) (child + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 188 | skb->fclone = SKB_FCLONE_ORIG; |
| 189 | atomic_set(fclone_ref, 1); |
| 190 | |
| 191 | child->fclone = SKB_FCLONE_UNAVAILABLE; |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | out: |
| 194 | return skb; |
| 195 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 196 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | skb = NULL; |
| 198 | goto out; |
| 199 | } |
| 200 | |
| 201 | /** |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 202 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 203 | * @dev: network device to receive on |
| 204 | * @length: length to allocate |
| 205 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 206 | * |
| 207 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 208 | * buffer has unspecified headroom built in. Users should allocate |
| 209 | * the headroom they think they need without accounting for the |
| 210 | * built in space. The built in space is used for optimisations. |
| 211 | * |
| 212 | * %NULL is returned if there is no free memory. |
| 213 | */ |
| 214 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, |
| 215 | unsigned int length, gfp_t gfp_mask) |
| 216 | { |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 217 | int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 218 | struct sk_buff *skb; |
| 219 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 220 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 221 | if (likely(skb)) { |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 222 | skb_reserve(skb, NET_SKB_PAD); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 223 | skb->dev = dev; |
| 224 | } |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 225 | return skb; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 228 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 230 | struct sk_buff *list = *listp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 232 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | do { |
| 235 | struct sk_buff *this = list; |
| 236 | list = list->next; |
| 237 | kfree_skb(this); |
| 238 | } while (list); |
| 239 | } |
| 240 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 241 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 242 | { |
| 243 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 247 | { |
| 248 | struct sk_buff *list; |
| 249 | |
| 250 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) |
| 251 | skb_get(list); |
| 252 | } |
| 253 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 254 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | if (!skb->cloned || |
| 257 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 258 | &skb_shinfo(skb)->dataref)) { |
| 259 | if (skb_shinfo(skb)->nr_frags) { |
| 260 | int i; |
| 261 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 262 | put_page(skb_shinfo(skb)->frags[i].page); |
| 263 | } |
| 264 | |
| 265 | if (skb_shinfo(skb)->frag_list) |
| 266 | skb_drop_fraglist(skb); |
| 267 | |
| 268 | kfree(skb->head); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Free an skbuff by memory without cleaning the state. |
| 274 | */ |
| 275 | void kfree_skbmem(struct sk_buff *skb) |
| 276 | { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 277 | struct sk_buff *other; |
| 278 | atomic_t *fclone_ref; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | skb_release_data(skb); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 281 | switch (skb->fclone) { |
| 282 | case SKB_FCLONE_UNAVAILABLE: |
| 283 | kmem_cache_free(skbuff_head_cache, skb); |
| 284 | break; |
| 285 | |
| 286 | case SKB_FCLONE_ORIG: |
| 287 | fclone_ref = (atomic_t *) (skb + 2); |
| 288 | if (atomic_dec_and_test(fclone_ref)) |
| 289 | kmem_cache_free(skbuff_fclone_cache, skb); |
| 290 | break; |
| 291 | |
| 292 | case SKB_FCLONE_CLONE: |
| 293 | fclone_ref = (atomic_t *) (skb + 1); |
| 294 | other = skb - 1; |
| 295 | |
| 296 | /* The clone portion is available for |
| 297 | * fast-cloning again. |
| 298 | */ |
| 299 | skb->fclone = SKB_FCLONE_UNAVAILABLE; |
| 300 | |
| 301 | if (atomic_dec_and_test(fclone_ref)) |
| 302 | kmem_cache_free(skbuff_fclone_cache, other); |
| 303 | break; |
| 304 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * __kfree_skb - private function |
| 309 | * @skb: buffer |
| 310 | * |
| 311 | * Free an sk_buff. Release anything attached to the buffer. |
| 312 | * Clean the state. This is an internal helper function. Users should |
| 313 | * always call kfree_skb |
| 314 | */ |
| 315 | |
| 316 | void __kfree_skb(struct sk_buff *skb) |
| 317 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | dst_release(skb->dst); |
| 319 | #ifdef CONFIG_XFRM |
| 320 | secpath_put(skb->sp); |
| 321 | #endif |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 322 | if (skb->destructor) { |
| 323 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | skb->destructor(skb); |
| 325 | } |
| 326 | #ifdef CONFIG_NETFILTER |
| 327 | nf_conntrack_put(skb->nfct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 328 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 329 | nf_conntrack_put_reasm(skb->nfct_reasm); |
| 330 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 332 | nf_bridge_put(skb->nf_bridge); |
| 333 | #endif |
| 334 | #endif |
| 335 | /* XXX: IS this still necessary? - JHS */ |
| 336 | #ifdef CONFIG_NET_SCHED |
| 337 | skb->tc_index = 0; |
| 338 | #ifdef CONFIG_NET_CLS_ACT |
| 339 | skb->tc_verd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | #endif |
| 341 | #endif |
| 342 | |
| 343 | kfree_skbmem(skb); |
| 344 | } |
| 345 | |
| 346 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 347 | * kfree_skb - free an sk_buff |
| 348 | * @skb: buffer to free |
| 349 | * |
| 350 | * Drop a reference to the buffer and free it if the usage count has |
| 351 | * hit zero. |
| 352 | */ |
| 353 | void kfree_skb(struct sk_buff *skb) |
| 354 | { |
| 355 | if (unlikely(!skb)) |
| 356 | return; |
| 357 | if (likely(atomic_read(&skb->users) == 1)) |
| 358 | smp_rmb(); |
| 359 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 360 | return; |
| 361 | __kfree_skb(skb); |
| 362 | } |
| 363 | |
| 364 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * skb_clone - duplicate an sk_buff |
| 366 | * @skb: buffer to clone |
| 367 | * @gfp_mask: allocation priority |
| 368 | * |
| 369 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 370 | * copies share the same packet data but not structure. The new |
| 371 | * buffer has a reference count of 1. If the allocation fails the |
| 372 | * function returns %NULL otherwise the new buffer is returned. |
| 373 | * |
| 374 | * If this function is called from an interrupt gfp_mask() must be |
| 375 | * %GFP_ATOMIC. |
| 376 | */ |
| 377 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 378 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 380 | struct sk_buff *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 382 | n = skb + 1; |
| 383 | if (skb->fclone == SKB_FCLONE_ORIG && |
| 384 | n->fclone == SKB_FCLONE_UNAVAILABLE) { |
| 385 | atomic_t *fclone_ref = (atomic_t *) (n + 1); |
| 386 | n->fclone = SKB_FCLONE_CLONE; |
| 387 | atomic_inc(fclone_ref); |
| 388 | } else { |
| 389 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 390 | if (!n) |
| 391 | return NULL; |
| 392 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | #define C(x) n->x = skb->x |
| 396 | |
| 397 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | n->sk = NULL; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 399 | C(tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | C(dev); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 401 | C(transport_header); |
| 402 | C(network_header); |
| 403 | C(mac_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | C(dst); |
| 405 | dst_clone(skb->dst); |
| 406 | C(sp); |
| 407 | #ifdef CONFIG_INET |
| 408 | secpath_get(skb->sp); |
| 409 | #endif |
| 410 | memcpy(n->cb, skb->cb, sizeof(skb->cb)); |
| 411 | C(len); |
| 412 | C(data_len); |
Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 413 | C(mac_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | C(csum); |
| 415 | C(local_df); |
| 416 | n->cloned = 1; |
| 417 | n->nohdr = 0; |
| 418 | C(pkt_type); |
| 419 | C(ip_summed); |
| 420 | C(priority); |
YOSHIFUJI Hideaki | a8372f0 | 2006-02-19 22:32:06 -0800 | [diff] [blame] | 421 | #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) |
| 422 | C(ipvs_property); |
| 423 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | C(protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | n->destructor = NULL; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 426 | C(mark); |
Yasuyuki Kozakai | edda553 | 2007-03-14 16:43:37 -0700 | [diff] [blame] | 427 | __nf_copy(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | #ifdef CONFIG_NET_SCHED |
| 429 | C(tc_index); |
| 430 | #ifdef CONFIG_NET_CLS_ACT |
| 431 | n->tc_verd = SET_TC_VERD(skb->tc_verd,0); |
David S. Miller | b72f6ec | 2005-07-19 14:13:54 -0700 | [diff] [blame] | 432 | n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd); |
| 433 | n->tc_verd = CLR_TC_MUNGED(n->tc_verd); |
Patrick McHardy | c01003c | 2007-03-29 11:46:52 -0700 | [diff] [blame] | 434 | C(iif); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | #endif |
James Morris | 984bc16 | 2006-06-09 00:29:17 -0700 | [diff] [blame] | 436 | skb_copy_secmark(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | #endif |
| 438 | C(truesize); |
| 439 | atomic_set(&n->users, 1); |
| 440 | C(head); |
| 441 | C(data); |
| 442 | C(tail); |
| 443 | C(end); |
| 444 | |
| 445 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 446 | skb->cloned = 1; |
| 447 | |
| 448 | return n; |
| 449 | } |
| 450 | |
| 451 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 452 | { |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 453 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | /* |
| 455 | * Shift between the two data areas in bytes |
| 456 | */ |
| 457 | unsigned long offset = new->data - old->data; |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 458 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | new->sk = NULL; |
| 460 | new->dev = old->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | new->priority = old->priority; |
| 462 | new->protocol = old->protocol; |
| 463 | new->dst = dst_clone(old->dst); |
| 464 | #ifdef CONFIG_INET |
| 465 | new->sp = secpath_get(old->sp); |
| 466 | #endif |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 467 | new->transport_header = old->transport_header; |
| 468 | new->network_header = old->network_header; |
| 469 | new->mac_header = old->mac_header; |
| 470 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
| 471 | /* {transport,network,mac}_header are relative to skb->head */ |
| 472 | new->transport_header += offset; |
| 473 | new->network_header += offset; |
| 474 | new->mac_header += offset; |
| 475 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
| 477 | new->local_df = old->local_df; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 478 | new->fclone = SKB_FCLONE_UNAVAILABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | new->pkt_type = old->pkt_type; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 480 | new->tstamp = old->tstamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | new->destructor = NULL; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 482 | new->mark = old->mark; |
Yasuyuki Kozakai | edda553 | 2007-03-14 16:43:37 -0700 | [diff] [blame] | 483 | __nf_copy(new, old); |
Julian Anastasov | c98d80e | 2005-10-22 13:39:21 +0300 | [diff] [blame] | 484 | #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) |
| 485 | new->ipvs_property = old->ipvs_property; |
| 486 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | #ifdef CONFIG_NET_SCHED |
| 488 | #ifdef CONFIG_NET_CLS_ACT |
| 489 | new->tc_verd = old->tc_verd; |
| 490 | #endif |
| 491 | new->tc_index = old->tc_index; |
| 492 | #endif |
James Morris | 984bc16 | 2006-06-09 00:29:17 -0700 | [diff] [blame] | 493 | skb_copy_secmark(new, old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | atomic_set(&new->users, 1); |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 495 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 496 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 497 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
| 501 | * skb_copy - create private copy of an sk_buff |
| 502 | * @skb: buffer to copy |
| 503 | * @gfp_mask: allocation priority |
| 504 | * |
| 505 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 506 | * caller wishes to modify the data and needs a private copy of the |
| 507 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 508 | * on success. The returned buffer has a reference count of 1. |
| 509 | * |
| 510 | * As by-product this function converts non-linear &sk_buff to linear |
| 511 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 512 | * to modify all the data of returned buffer. This means that this |
| 513 | * function is not recommended for use in circumstances when only |
| 514 | * header is going to be modified. Use pskb_copy() instead. |
| 515 | */ |
| 516 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 517 | struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
| 519 | int headerlen = skb->data - skb->head; |
| 520 | /* |
| 521 | * Allocate the copy buffer |
| 522 | */ |
| 523 | struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len, |
| 524 | gfp_mask); |
| 525 | if (!n) |
| 526 | return NULL; |
| 527 | |
| 528 | /* Set the data pointer */ |
| 529 | skb_reserve(n, headerlen); |
| 530 | /* Set the tail pointer and length */ |
| 531 | skb_put(n, skb->len); |
| 532 | n->csum = skb->csum; |
| 533 | n->ip_summed = skb->ip_summed; |
| 534 | |
| 535 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 536 | BUG(); |
| 537 | |
| 538 | copy_skb_header(n, skb); |
| 539 | return n; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /** |
| 544 | * pskb_copy - create copy of an sk_buff with private head. |
| 545 | * @skb: buffer to copy |
| 546 | * @gfp_mask: allocation priority |
| 547 | * |
| 548 | * Make a copy of both an &sk_buff and part of its data, located |
| 549 | * in header. Fragmented data remain shared. This is used when |
| 550 | * the caller wishes to modify only header of &sk_buff and needs |
| 551 | * private copy of the header to alter. Returns %NULL on failure |
| 552 | * or the pointer to the buffer on success. |
| 553 | * The returned buffer has a reference count of 1. |
| 554 | */ |
| 555 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 556 | struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | /* |
| 559 | * Allocate the copy buffer |
| 560 | */ |
| 561 | struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask); |
| 562 | |
| 563 | if (!n) |
| 564 | goto out; |
| 565 | |
| 566 | /* Set the data pointer */ |
| 567 | skb_reserve(n, skb->data - skb->head); |
| 568 | /* Set the tail pointer and length */ |
| 569 | skb_put(n, skb_headlen(skb)); |
| 570 | /* Copy the bytes */ |
| 571 | memcpy(n->data, skb->data, n->len); |
| 572 | n->csum = skb->csum; |
| 573 | n->ip_summed = skb->ip_summed; |
| 574 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 575 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | n->data_len = skb->data_len; |
| 577 | n->len = skb->len; |
| 578 | |
| 579 | if (skb_shinfo(skb)->nr_frags) { |
| 580 | int i; |
| 581 | |
| 582 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 583 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 584 | get_page(skb_shinfo(n)->frags[i].page); |
| 585 | } |
| 586 | skb_shinfo(n)->nr_frags = i; |
| 587 | } |
| 588 | |
| 589 | if (skb_shinfo(skb)->frag_list) { |
| 590 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 591 | skb_clone_fraglist(n); |
| 592 | } |
| 593 | |
| 594 | copy_skb_header(n, skb); |
| 595 | out: |
| 596 | return n; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * pskb_expand_head - reallocate header of &sk_buff |
| 601 | * @skb: buffer to reallocate |
| 602 | * @nhead: room to add at head |
| 603 | * @ntail: room to add at tail |
| 604 | * @gfp_mask: allocation priority |
| 605 | * |
| 606 | * Expands (or creates identical copy, if &nhead and &ntail are zero) |
| 607 | * header of skb. &sk_buff itself is not changed. &sk_buff MUST have |
| 608 | * reference count of 1. Returns zero in the case of success or error, |
| 609 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 610 | * |
| 611 | * All the pointers pointing into skb header may change and must be |
| 612 | * reloaded after call to this function. |
| 613 | */ |
| 614 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 615 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 616 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
| 618 | int i; |
| 619 | u8 *data; |
| 620 | int size = nhead + (skb->end - skb->head) + ntail; |
| 621 | long off; |
| 622 | |
| 623 | if (skb_shared(skb)) |
| 624 | BUG(); |
| 625 | |
| 626 | size = SKB_DATA_ALIGN(size); |
| 627 | |
| 628 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); |
| 629 | if (!data) |
| 630 | goto nodata; |
| 631 | |
| 632 | /* Copy only real data... and, alas, header. This should be |
| 633 | * optimized for the cases when header is void. */ |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 634 | memcpy(data + nhead, skb->head, |
| 635 | skb->tail |
| 636 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
| 637 | - skb->head |
| 638 | #endif |
| 639 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | memcpy(data + size, skb->end, sizeof(struct skb_shared_info)); |
| 641 | |
| 642 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 643 | get_page(skb_shinfo(skb)->frags[i].page); |
| 644 | |
| 645 | if (skb_shinfo(skb)->frag_list) |
| 646 | skb_clone_fraglist(skb); |
| 647 | |
| 648 | skb_release_data(skb); |
| 649 | |
| 650 | off = (data + nhead) - skb->head; |
| 651 | |
| 652 | skb->head = data; |
| 653 | skb->end = data + size; |
| 654 | skb->data += off; |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 655 | #ifndef NET_SKBUFF_DATA_USES_OFFSET |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 656 | /* {transport,network,mac}_header and tail are relative to skb->head */ |
| 657 | skb->tail += off; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 658 | skb->transport_header += off; |
| 659 | skb->network_header += off; |
| 660 | skb->mac_header += off; |
Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 661 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | skb->cloned = 0; |
| 663 | skb->nohdr = 0; |
| 664 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 665 | return 0; |
| 666 | |
| 667 | nodata: |
| 668 | return -ENOMEM; |
| 669 | } |
| 670 | |
| 671 | /* Make private copy of skb with writable head and some headroom */ |
| 672 | |
| 673 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 674 | { |
| 675 | struct sk_buff *skb2; |
| 676 | int delta = headroom - skb_headroom(skb); |
| 677 | |
| 678 | if (delta <= 0) |
| 679 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 680 | else { |
| 681 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 682 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 683 | GFP_ATOMIC)) { |
| 684 | kfree_skb(skb2); |
| 685 | skb2 = NULL; |
| 686 | } |
| 687 | } |
| 688 | return skb2; |
| 689 | } |
| 690 | |
| 691 | |
| 692 | /** |
| 693 | * skb_copy_expand - copy and expand sk_buff |
| 694 | * @skb: buffer to copy |
| 695 | * @newheadroom: new free bytes at head |
| 696 | * @newtailroom: new free bytes at tail |
| 697 | * @gfp_mask: allocation priority |
| 698 | * |
| 699 | * Make a copy of both an &sk_buff and its data and while doing so |
| 700 | * allocate additional space. |
| 701 | * |
| 702 | * This is used when the caller wishes to modify the data and needs a |
| 703 | * private copy of the data to alter as well as more space for new fields. |
| 704 | * Returns %NULL on failure or the pointer to the buffer |
| 705 | * on success. The returned buffer has a reference count of 1. |
| 706 | * |
| 707 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 708 | * is called from an interrupt. |
| 709 | * |
| 710 | * BUG ALERT: ip_summed is not copied. Why does this work? Is it used |
| 711 | * only by netfilter in the cases when checksum is recalculated? --ANK |
| 712 | */ |
| 713 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 714 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 715 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
| 717 | /* |
| 718 | * Allocate the copy buffer |
| 719 | */ |
| 720 | struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, |
| 721 | gfp_mask); |
| 722 | int head_copy_len, head_copy_off; |
| 723 | |
| 724 | if (!n) |
| 725 | return NULL; |
| 726 | |
| 727 | skb_reserve(n, newheadroom); |
| 728 | |
| 729 | /* Set the tail pointer and length */ |
| 730 | skb_put(n, skb->len); |
| 731 | |
| 732 | head_copy_len = skb_headroom(skb); |
| 733 | head_copy_off = 0; |
| 734 | if (newheadroom <= head_copy_len) |
| 735 | head_copy_len = newheadroom; |
| 736 | else |
| 737 | head_copy_off = newheadroom - head_copy_len; |
| 738 | |
| 739 | /* Copy the linear header and data. */ |
| 740 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 741 | skb->len + head_copy_len)) |
| 742 | BUG(); |
| 743 | |
| 744 | copy_skb_header(n, skb); |
| 745 | |
| 746 | return n; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * skb_pad - zero pad the tail of an skb |
| 751 | * @skb: buffer to pad |
| 752 | * @pad: space to pad |
| 753 | * |
| 754 | * Ensure that a buffer is followed by a padding area that is zero |
| 755 | * filled. Used by network drivers which may DMA or transfer data |
| 756 | * beyond the buffer end onto the wire. |
| 757 | * |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 758 | * May return error in out of memory cases. The skb is freed on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 760 | |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 761 | int skb_pad(struct sk_buff *skb, int pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 763 | int err; |
| 764 | int ntail; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 767 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 771 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 772 | ntail = skb->data_len + pad - (skb->end - skb_tail_pointer(skb)); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 773 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 774 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 775 | if (unlikely(err)) |
| 776 | goto free_skb; |
| 777 | } |
| 778 | |
| 779 | /* FIXME: The use of this function with non-linear skb's really needs |
| 780 | * to be audited. |
| 781 | */ |
| 782 | err = skb_linearize(skb); |
| 783 | if (unlikely(err)) |
| 784 | goto free_skb; |
| 785 | |
| 786 | memset(skb->data + skb->len, 0, pad); |
| 787 | return 0; |
| 788 | |
| 789 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 791 | return err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 792 | } |
| 793 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 794 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | */ |
| 796 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 797 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 799 | struct sk_buff **fragp; |
| 800 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | int offset = skb_headlen(skb); |
| 802 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 803 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 804 | int err; |
| 805 | |
| 806 | if (skb_cloned(skb) && |
| 807 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 808 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 810 | i = 0; |
| 811 | if (offset >= len) |
| 812 | goto drop_pages; |
| 813 | |
| 814 | for (; i < nfrags; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | int end = offset + skb_shinfo(skb)->frags[i].size; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 816 | |
| 817 | if (end < len) { |
| 818 | offset = end; |
| 819 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 821 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 822 | skb_shinfo(skb)->frags[i++].size = len - offset; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 823 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 824 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 825 | skb_shinfo(skb)->nr_frags = i; |
| 826 | |
| 827 | for (; i < nfrags; i++) |
| 828 | put_page(skb_shinfo(skb)->frags[i].page); |
| 829 | |
| 830 | if (skb_shinfo(skb)->frag_list) |
| 831 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 832 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 835 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 836 | fragp = &frag->next) { |
| 837 | int end = offset + frag->len; |
| 838 | |
| 839 | if (skb_shared(frag)) { |
| 840 | struct sk_buff *nfrag; |
| 841 | |
| 842 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 843 | if (unlikely(!nfrag)) |
| 844 | return -ENOMEM; |
| 845 | |
| 846 | nfrag->next = frag->next; |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 847 | kfree_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 848 | frag = nfrag; |
| 849 | *fragp = frag; |
| 850 | } |
| 851 | |
| 852 | if (end < len) { |
| 853 | offset = end; |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | if (end > len && |
| 858 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 859 | return err; |
| 860 | |
| 861 | if (frag->next) |
| 862 | skb_drop_list(&frag->next); |
| 863 | break; |
| 864 | } |
| 865 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 866 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 867 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | skb->data_len -= skb->len - len; |
| 869 | skb->len = len; |
| 870 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 871 | skb->len = len; |
| 872 | skb->data_len = 0; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 873 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * __pskb_pull_tail - advance tail of skb header |
| 881 | * @skb: buffer to reallocate |
| 882 | * @delta: number of bytes to advance tail |
| 883 | * |
| 884 | * The function makes a sense only on a fragmented &sk_buff, |
| 885 | * it expands header moving its tail forward and copying necessary |
| 886 | * data from fragmented part. |
| 887 | * |
| 888 | * &sk_buff MUST have reference count of 1. |
| 889 | * |
| 890 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 891 | * or value of new tail of skb in the case of success. |
| 892 | * |
| 893 | * All the pointers pointing into skb header may change and must be |
| 894 | * reloaded after call to this function. |
| 895 | */ |
| 896 | |
| 897 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 898 | * when it is necessary. |
| 899 | * 1. It may fail due to malloc failure. |
| 900 | * 2. It may change skb pointers. |
| 901 | * |
| 902 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 903 | */ |
| 904 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 905 | { |
| 906 | /* If skb has not enough free space at tail, get new one |
| 907 | * plus 128 bytes for future expansions. If we have enough |
| 908 | * room at tail, reallocate without expansion only if skb is cloned. |
| 909 | */ |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 910 | int i, k, eat = (skb_tail_pointer(skb) + delta) - skb->end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | if (eat > 0 || skb_cloned(skb)) { |
| 913 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 914 | GFP_ATOMIC)) |
| 915 | return NULL; |
| 916 | } |
| 917 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 918 | if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | BUG(); |
| 920 | |
| 921 | /* Optimization: no fragments, no reasons to preestimate |
| 922 | * size of pulled pages. Superb. |
| 923 | */ |
| 924 | if (!skb_shinfo(skb)->frag_list) |
| 925 | goto pull_pages; |
| 926 | |
| 927 | /* Estimate size of pulled pages. */ |
| 928 | eat = delta; |
| 929 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 930 | if (skb_shinfo(skb)->frags[i].size >= eat) |
| 931 | goto pull_pages; |
| 932 | eat -= skb_shinfo(skb)->frags[i].size; |
| 933 | } |
| 934 | |
| 935 | /* If we need update frag list, we are in troubles. |
| 936 | * Certainly, it possible to add an offset to skb data, |
| 937 | * but taking into account that pulling is expected to |
| 938 | * be very rare operation, it is worth to fight against |
| 939 | * further bloating skb head and crucify ourselves here instead. |
| 940 | * Pure masohism, indeed. 8)8) |
| 941 | */ |
| 942 | if (eat) { |
| 943 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 944 | struct sk_buff *clone = NULL; |
| 945 | struct sk_buff *insp = NULL; |
| 946 | |
| 947 | do { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 948 | BUG_ON(!list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | if (list->len <= eat) { |
| 951 | /* Eaten as whole. */ |
| 952 | eat -= list->len; |
| 953 | list = list->next; |
| 954 | insp = list; |
| 955 | } else { |
| 956 | /* Eaten partially. */ |
| 957 | |
| 958 | if (skb_shared(list)) { |
| 959 | /* Sucks! We need to fork list. :-( */ |
| 960 | clone = skb_clone(list, GFP_ATOMIC); |
| 961 | if (!clone) |
| 962 | return NULL; |
| 963 | insp = list->next; |
| 964 | list = clone; |
| 965 | } else { |
| 966 | /* This may be pulled without |
| 967 | * problems. */ |
| 968 | insp = list; |
| 969 | } |
| 970 | if (!pskb_pull(list, eat)) { |
| 971 | if (clone) |
| 972 | kfree_skb(clone); |
| 973 | return NULL; |
| 974 | } |
| 975 | break; |
| 976 | } |
| 977 | } while (eat); |
| 978 | |
| 979 | /* Free pulled out fragments. */ |
| 980 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 981 | skb_shinfo(skb)->frag_list = list->next; |
| 982 | kfree_skb(list); |
| 983 | } |
| 984 | /* And insert new clone at head. */ |
| 985 | if (clone) { |
| 986 | clone->next = list; |
| 987 | skb_shinfo(skb)->frag_list = clone; |
| 988 | } |
| 989 | } |
| 990 | /* Success! Now we may commit changes to skb data. */ |
| 991 | |
| 992 | pull_pages: |
| 993 | eat = delta; |
| 994 | k = 0; |
| 995 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 996 | if (skb_shinfo(skb)->frags[i].size <= eat) { |
| 997 | put_page(skb_shinfo(skb)->frags[i].page); |
| 998 | eat -= skb_shinfo(skb)->frags[i].size; |
| 999 | } else { |
| 1000 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1001 | if (eat) { |
| 1002 | skb_shinfo(skb)->frags[k].page_offset += eat; |
| 1003 | skb_shinfo(skb)->frags[k].size -= eat; |
| 1004 | eat = 0; |
| 1005 | } |
| 1006 | k++; |
| 1007 | } |
| 1008 | } |
| 1009 | skb_shinfo(skb)->nr_frags = k; |
| 1010 | |
| 1011 | skb->tail += delta; |
| 1012 | skb->data_len -= delta; |
| 1013 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 1014 | return skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* Copy some data bits from skb to kernel buffer. */ |
| 1018 | |
| 1019 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 1020 | { |
| 1021 | int i, copy; |
| 1022 | int start = skb_headlen(skb); |
| 1023 | |
| 1024 | if (offset > (int)skb->len - len) |
| 1025 | goto fault; |
| 1026 | |
| 1027 | /* Copy header. */ |
| 1028 | if ((copy = start - offset) > 0) { |
| 1029 | if (copy > len) |
| 1030 | copy = len; |
| 1031 | memcpy(to, skb->data + offset, copy); |
| 1032 | if ((len -= copy) == 0) |
| 1033 | return 0; |
| 1034 | offset += copy; |
| 1035 | to += copy; |
| 1036 | } |
| 1037 | |
| 1038 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1039 | int end; |
| 1040 | |
| 1041 | BUG_TRAP(start <= offset + len); |
| 1042 | |
| 1043 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1044 | if ((copy = end - offset) > 0) { |
| 1045 | u8 *vaddr; |
| 1046 | |
| 1047 | if (copy > len) |
| 1048 | copy = len; |
| 1049 | |
| 1050 | vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); |
| 1051 | memcpy(to, |
| 1052 | vaddr + skb_shinfo(skb)->frags[i].page_offset+ |
| 1053 | offset - start, copy); |
| 1054 | kunmap_skb_frag(vaddr); |
| 1055 | |
| 1056 | if ((len -= copy) == 0) |
| 1057 | return 0; |
| 1058 | offset += copy; |
| 1059 | to += copy; |
| 1060 | } |
| 1061 | start = end; |
| 1062 | } |
| 1063 | |
| 1064 | if (skb_shinfo(skb)->frag_list) { |
| 1065 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1066 | |
| 1067 | for (; list; list = list->next) { |
| 1068 | int end; |
| 1069 | |
| 1070 | BUG_TRAP(start <= offset + len); |
| 1071 | |
| 1072 | end = start + list->len; |
| 1073 | if ((copy = end - offset) > 0) { |
| 1074 | if (copy > len) |
| 1075 | copy = len; |
| 1076 | if (skb_copy_bits(list, offset - start, |
| 1077 | to, copy)) |
| 1078 | goto fault; |
| 1079 | if ((len -= copy) == 0) |
| 1080 | return 0; |
| 1081 | offset += copy; |
| 1082 | to += copy; |
| 1083 | } |
| 1084 | start = end; |
| 1085 | } |
| 1086 | } |
| 1087 | if (!len) |
| 1088 | return 0; |
| 1089 | |
| 1090 | fault: |
| 1091 | return -EFAULT; |
| 1092 | } |
| 1093 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1094 | /** |
| 1095 | * skb_store_bits - store bits from kernel buffer to skb |
| 1096 | * @skb: destination buffer |
| 1097 | * @offset: offset in destination |
| 1098 | * @from: source buffer |
| 1099 | * @len: number of bytes to copy |
| 1100 | * |
| 1101 | * Copy the specified number of bytes from the source buffer to the |
| 1102 | * destination skb. This function handles all the messy bits of |
| 1103 | * traversing fragment lists and such. |
| 1104 | */ |
| 1105 | |
| 1106 | int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len) |
| 1107 | { |
| 1108 | int i, copy; |
| 1109 | int start = skb_headlen(skb); |
| 1110 | |
| 1111 | if (offset > (int)skb->len - len) |
| 1112 | goto fault; |
| 1113 | |
| 1114 | if ((copy = start - offset) > 0) { |
| 1115 | if (copy > len) |
| 1116 | copy = len; |
| 1117 | memcpy(skb->data + offset, from, copy); |
| 1118 | if ((len -= copy) == 0) |
| 1119 | return 0; |
| 1120 | offset += copy; |
| 1121 | from += copy; |
| 1122 | } |
| 1123 | |
| 1124 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1125 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1126 | int end; |
| 1127 | |
| 1128 | BUG_TRAP(start <= offset + len); |
| 1129 | |
| 1130 | end = start + frag->size; |
| 1131 | if ((copy = end - offset) > 0) { |
| 1132 | u8 *vaddr; |
| 1133 | |
| 1134 | if (copy > len) |
| 1135 | copy = len; |
| 1136 | |
| 1137 | vaddr = kmap_skb_frag(frag); |
| 1138 | memcpy(vaddr + frag->page_offset + offset - start, |
| 1139 | from, copy); |
| 1140 | kunmap_skb_frag(vaddr); |
| 1141 | |
| 1142 | if ((len -= copy) == 0) |
| 1143 | return 0; |
| 1144 | offset += copy; |
| 1145 | from += copy; |
| 1146 | } |
| 1147 | start = end; |
| 1148 | } |
| 1149 | |
| 1150 | if (skb_shinfo(skb)->frag_list) { |
| 1151 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1152 | |
| 1153 | for (; list; list = list->next) { |
| 1154 | int end; |
| 1155 | |
| 1156 | BUG_TRAP(start <= offset + len); |
| 1157 | |
| 1158 | end = start + list->len; |
| 1159 | if ((copy = end - offset) > 0) { |
| 1160 | if (copy > len) |
| 1161 | copy = len; |
| 1162 | if (skb_store_bits(list, offset - start, |
| 1163 | from, copy)) |
| 1164 | goto fault; |
| 1165 | if ((len -= copy) == 0) |
| 1166 | return 0; |
| 1167 | offset += copy; |
| 1168 | from += copy; |
| 1169 | } |
| 1170 | start = end; |
| 1171 | } |
| 1172 | } |
| 1173 | if (!len) |
| 1174 | return 0; |
| 1175 | |
| 1176 | fault: |
| 1177 | return -EFAULT; |
| 1178 | } |
| 1179 | |
| 1180 | EXPORT_SYMBOL(skb_store_bits); |
| 1181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | /* Checksum skb data. */ |
| 1183 | |
Al Viro | 2bbbc86 | 2006-11-14 21:37:14 -0800 | [diff] [blame] | 1184 | __wsum skb_checksum(const struct sk_buff *skb, int offset, |
| 1185 | int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | { |
| 1187 | int start = skb_headlen(skb); |
| 1188 | int i, copy = start - offset; |
| 1189 | int pos = 0; |
| 1190 | |
| 1191 | /* Checksum header. */ |
| 1192 | if (copy > 0) { |
| 1193 | if (copy > len) |
| 1194 | copy = len; |
| 1195 | csum = csum_partial(skb->data + offset, copy, csum); |
| 1196 | if ((len -= copy) == 0) |
| 1197 | return csum; |
| 1198 | offset += copy; |
| 1199 | pos = copy; |
| 1200 | } |
| 1201 | |
| 1202 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1203 | int end; |
| 1204 | |
| 1205 | BUG_TRAP(start <= offset + len); |
| 1206 | |
| 1207 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1208 | if ((copy = end - offset) > 0) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 1209 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | u8 *vaddr; |
| 1211 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1212 | |
| 1213 | if (copy > len) |
| 1214 | copy = len; |
| 1215 | vaddr = kmap_skb_frag(frag); |
| 1216 | csum2 = csum_partial(vaddr + frag->page_offset + |
| 1217 | offset - start, copy, 0); |
| 1218 | kunmap_skb_frag(vaddr); |
| 1219 | csum = csum_block_add(csum, csum2, pos); |
| 1220 | if (!(len -= copy)) |
| 1221 | return csum; |
| 1222 | offset += copy; |
| 1223 | pos += copy; |
| 1224 | } |
| 1225 | start = end; |
| 1226 | } |
| 1227 | |
| 1228 | if (skb_shinfo(skb)->frag_list) { |
| 1229 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1230 | |
| 1231 | for (; list; list = list->next) { |
| 1232 | int end; |
| 1233 | |
| 1234 | BUG_TRAP(start <= offset + len); |
| 1235 | |
| 1236 | end = start + list->len; |
| 1237 | if ((copy = end - offset) > 0) { |
Al Viro | 5f92a73 | 2006-11-14 21:36:54 -0800 | [diff] [blame] | 1238 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (copy > len) |
| 1240 | copy = len; |
| 1241 | csum2 = skb_checksum(list, offset - start, |
| 1242 | copy, 0); |
| 1243 | csum = csum_block_add(csum, csum2, pos); |
| 1244 | if ((len -= copy) == 0) |
| 1245 | return csum; |
| 1246 | offset += copy; |
| 1247 | pos += copy; |
| 1248 | } |
| 1249 | start = end; |
| 1250 | } |
| 1251 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1252 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
| 1254 | return csum; |
| 1255 | } |
| 1256 | |
| 1257 | /* Both of above in one bottle. */ |
| 1258 | |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 1259 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 1260 | u8 *to, int len, __wsum csum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | { |
| 1262 | int start = skb_headlen(skb); |
| 1263 | int i, copy = start - offset; |
| 1264 | int pos = 0; |
| 1265 | |
| 1266 | /* Copy header. */ |
| 1267 | if (copy > 0) { |
| 1268 | if (copy > len) |
| 1269 | copy = len; |
| 1270 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 1271 | copy, csum); |
| 1272 | if ((len -= copy) == 0) |
| 1273 | return csum; |
| 1274 | offset += copy; |
| 1275 | to += copy; |
| 1276 | pos = copy; |
| 1277 | } |
| 1278 | |
| 1279 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1280 | int end; |
| 1281 | |
| 1282 | BUG_TRAP(start <= offset + len); |
| 1283 | |
| 1284 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1285 | if ((copy = end - offset) > 0) { |
Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 1286 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | u8 *vaddr; |
| 1288 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1289 | |
| 1290 | if (copy > len) |
| 1291 | copy = len; |
| 1292 | vaddr = kmap_skb_frag(frag); |
| 1293 | csum2 = csum_partial_copy_nocheck(vaddr + |
| 1294 | frag->page_offset + |
| 1295 | offset - start, to, |
| 1296 | copy, 0); |
| 1297 | kunmap_skb_frag(vaddr); |
| 1298 | csum = csum_block_add(csum, csum2, pos); |
| 1299 | if (!(len -= copy)) |
| 1300 | return csum; |
| 1301 | offset += copy; |
| 1302 | to += copy; |
| 1303 | pos += copy; |
| 1304 | } |
| 1305 | start = end; |
| 1306 | } |
| 1307 | |
| 1308 | if (skb_shinfo(skb)->frag_list) { |
| 1309 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1310 | |
| 1311 | for (; list; list = list->next) { |
Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 1312 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | int end; |
| 1314 | |
| 1315 | BUG_TRAP(start <= offset + len); |
| 1316 | |
| 1317 | end = start + list->len; |
| 1318 | if ((copy = end - offset) > 0) { |
| 1319 | if (copy > len) |
| 1320 | copy = len; |
| 1321 | csum2 = skb_copy_and_csum_bits(list, |
| 1322 | offset - start, |
| 1323 | to, copy, 0); |
| 1324 | csum = csum_block_add(csum, csum2, pos); |
| 1325 | if ((len -= copy) == 0) |
| 1326 | return csum; |
| 1327 | offset += copy; |
| 1328 | to += copy; |
| 1329 | pos += copy; |
| 1330 | } |
| 1331 | start = end; |
| 1332 | } |
| 1333 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1334 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | return csum; |
| 1336 | } |
| 1337 | |
| 1338 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 1339 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1340 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | long csstart; |
| 1342 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1343 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 1344 | csstart = skb_transport_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | else |
| 1346 | csstart = skb_headlen(skb); |
| 1347 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1348 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
| 1350 | memcpy(to, skb->data, csstart); |
| 1351 | |
| 1352 | csum = 0; |
| 1353 | if (csstart != skb->len) |
| 1354 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 1355 | skb->len - csstart, 0); |
| 1356 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1357 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 1358 | long csstuff = csstart + skb->csum_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1360 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * skb_dequeue - remove from the head of the queue |
| 1366 | * @list: list to dequeue from |
| 1367 | * |
| 1368 | * Remove the head of the list. The list lock is taken so the function |
| 1369 | * may be used safely with other locking list functions. The head item is |
| 1370 | * returned or %NULL if the list is empty. |
| 1371 | */ |
| 1372 | |
| 1373 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 1374 | { |
| 1375 | unsigned long flags; |
| 1376 | struct sk_buff *result; |
| 1377 | |
| 1378 | spin_lock_irqsave(&list->lock, flags); |
| 1379 | result = __skb_dequeue(list); |
| 1380 | spin_unlock_irqrestore(&list->lock, flags); |
| 1381 | return result; |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * skb_dequeue_tail - remove from the tail of the queue |
| 1386 | * @list: list to dequeue from |
| 1387 | * |
| 1388 | * Remove the tail of the list. The list lock is taken so the function |
| 1389 | * may be used safely with other locking list functions. The tail item is |
| 1390 | * returned or %NULL if the list is empty. |
| 1391 | */ |
| 1392 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 1393 | { |
| 1394 | unsigned long flags; |
| 1395 | struct sk_buff *result; |
| 1396 | |
| 1397 | spin_lock_irqsave(&list->lock, flags); |
| 1398 | result = __skb_dequeue_tail(list); |
| 1399 | spin_unlock_irqrestore(&list->lock, flags); |
| 1400 | return result; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * skb_queue_purge - empty a list |
| 1405 | * @list: list to empty |
| 1406 | * |
| 1407 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 1408 | * the list and one reference dropped. This function takes the list |
| 1409 | * lock and is atomic with respect to other list locking functions. |
| 1410 | */ |
| 1411 | void skb_queue_purge(struct sk_buff_head *list) |
| 1412 | { |
| 1413 | struct sk_buff *skb; |
| 1414 | while ((skb = skb_dequeue(list)) != NULL) |
| 1415 | kfree_skb(skb); |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * skb_queue_head - queue a buffer at the list head |
| 1420 | * @list: list to use |
| 1421 | * @newsk: buffer to queue |
| 1422 | * |
| 1423 | * Queue a buffer at the start of the list. This function takes the |
| 1424 | * list lock and can be used safely with other locking &sk_buff functions |
| 1425 | * safely. |
| 1426 | * |
| 1427 | * A buffer cannot be placed on two lists at the same time. |
| 1428 | */ |
| 1429 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1430 | { |
| 1431 | unsigned long flags; |
| 1432 | |
| 1433 | spin_lock_irqsave(&list->lock, flags); |
| 1434 | __skb_queue_head(list, newsk); |
| 1435 | spin_unlock_irqrestore(&list->lock, flags); |
| 1436 | } |
| 1437 | |
| 1438 | /** |
| 1439 | * skb_queue_tail - queue a buffer at the list tail |
| 1440 | * @list: list to use |
| 1441 | * @newsk: buffer to queue |
| 1442 | * |
| 1443 | * Queue a buffer at the tail of the list. This function takes the |
| 1444 | * list lock and can be used safely with other locking &sk_buff functions |
| 1445 | * safely. |
| 1446 | * |
| 1447 | * A buffer cannot be placed on two lists at the same time. |
| 1448 | */ |
| 1449 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1450 | { |
| 1451 | unsigned long flags; |
| 1452 | |
| 1453 | spin_lock_irqsave(&list->lock, flags); |
| 1454 | __skb_queue_tail(list, newsk); |
| 1455 | spin_unlock_irqrestore(&list->lock, flags); |
| 1456 | } |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | /** |
| 1459 | * skb_unlink - remove a buffer from a list |
| 1460 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1461 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1463 | * Remove a packet from a list. The list locks are taken and this |
| 1464 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1466 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1468 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1470 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1472 | spin_lock_irqsave(&list->lock, flags); |
| 1473 | __skb_unlink(skb, list); |
| 1474 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | /** |
| 1478 | * skb_append - append a buffer |
| 1479 | * @old: buffer to insert after |
| 1480 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1481 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | * |
| 1483 | * Place a packet after a given packet in a list. The list locks are taken |
| 1484 | * and this function is atomic with respect to other list locked calls. |
| 1485 | * A buffer cannot be placed on two lists at the same time. |
| 1486 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1487 | void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | { |
| 1489 | unsigned long flags; |
| 1490 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1491 | spin_lock_irqsave(&list->lock, flags); |
| 1492 | __skb_append(old, newsk, list); |
| 1493 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | /** |
| 1498 | * skb_insert - insert a buffer |
| 1499 | * @old: buffer to insert before |
| 1500 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1501 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1503 | * Place a packet before a given packet in a list. The list locks are |
| 1504 | * taken and this function is atomic with respect to other list locked |
| 1505 | * calls. |
| 1506 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | * A buffer cannot be placed on two lists at the same time. |
| 1508 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1509 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | { |
| 1511 | unsigned long flags; |
| 1512 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1513 | spin_lock_irqsave(&list->lock, flags); |
| 1514 | __skb_insert(newsk, old->prev, old, list); |
| 1515 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | #if 0 |
| 1519 | /* |
| 1520 | * Tune the memory allocator for a new MTU size. |
| 1521 | */ |
| 1522 | void skb_add_mtu(int mtu) |
| 1523 | { |
| 1524 | /* Must match allocation in alloc_skb */ |
| 1525 | mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info); |
| 1526 | |
| 1527 | kmem_add_cache_size(mtu); |
| 1528 | } |
| 1529 | #endif |
| 1530 | |
| 1531 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 1532 | struct sk_buff* skb1, |
| 1533 | const u32 len, const int pos) |
| 1534 | { |
| 1535 | int i; |
| 1536 | |
| 1537 | memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len); |
| 1538 | |
| 1539 | /* And move data appendix as is. */ |
| 1540 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 1541 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 1542 | |
| 1543 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 1544 | skb_shinfo(skb)->nr_frags = 0; |
| 1545 | skb1->data_len = skb->data_len; |
| 1546 | skb1->len += skb1->data_len; |
| 1547 | skb->data_len = 0; |
| 1548 | skb->len = len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame^] | 1549 | skb_set_tail_pointer(skb, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 1553 | struct sk_buff* skb1, |
| 1554 | const u32 len, int pos) |
| 1555 | { |
| 1556 | int i, k = 0; |
| 1557 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 1558 | |
| 1559 | skb_shinfo(skb)->nr_frags = 0; |
| 1560 | skb1->len = skb1->data_len = skb->len - len; |
| 1561 | skb->len = len; |
| 1562 | skb->data_len = len - pos; |
| 1563 | |
| 1564 | for (i = 0; i < nfrags; i++) { |
| 1565 | int size = skb_shinfo(skb)->frags[i].size; |
| 1566 | |
| 1567 | if (pos + size > len) { |
| 1568 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1569 | |
| 1570 | if (pos < len) { |
| 1571 | /* Split frag. |
| 1572 | * We have two variants in this case: |
| 1573 | * 1. Move all the frag to the second |
| 1574 | * part, if it is possible. F.e. |
| 1575 | * this approach is mandatory for TUX, |
| 1576 | * where splitting is expensive. |
| 1577 | * 2. Split is accurately. We make this. |
| 1578 | */ |
| 1579 | get_page(skb_shinfo(skb)->frags[i].page); |
| 1580 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
| 1581 | skb_shinfo(skb1)->frags[0].size -= len - pos; |
| 1582 | skb_shinfo(skb)->frags[i].size = len - pos; |
| 1583 | skb_shinfo(skb)->nr_frags++; |
| 1584 | } |
| 1585 | k++; |
| 1586 | } else |
| 1587 | skb_shinfo(skb)->nr_frags++; |
| 1588 | pos += size; |
| 1589 | } |
| 1590 | skb_shinfo(skb1)->nr_frags = k; |
| 1591 | } |
| 1592 | |
| 1593 | /** |
| 1594 | * skb_split - Split fragmented skb to two parts at length len. |
| 1595 | * @skb: the buffer to split |
| 1596 | * @skb1: the buffer to receive the second part |
| 1597 | * @len: new length for skb |
| 1598 | */ |
| 1599 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 1600 | { |
| 1601 | int pos = skb_headlen(skb); |
| 1602 | |
| 1603 | if (len < pos) /* Split line is inside header. */ |
| 1604 | skb_split_inside_header(skb, skb1, len, pos); |
| 1605 | else /* Second chunk has no header, nothing to copy. */ |
| 1606 | skb_split_no_header(skb, skb1, len, pos); |
| 1607 | } |
| 1608 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 1609 | /** |
| 1610 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 1611 | * @skb: the buffer to read |
| 1612 | * @from: lower offset of data to be read |
| 1613 | * @to: upper offset of data to be read |
| 1614 | * @st: state variable |
| 1615 | * |
| 1616 | * Initializes the specified state variable. Must be called before |
| 1617 | * invoking skb_seq_read() for the first time. |
| 1618 | */ |
| 1619 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 1620 | unsigned int to, struct skb_seq_state *st) |
| 1621 | { |
| 1622 | st->lower_offset = from; |
| 1623 | st->upper_offset = to; |
| 1624 | st->root_skb = st->cur_skb = skb; |
| 1625 | st->frag_idx = st->stepped_offset = 0; |
| 1626 | st->frag_data = NULL; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * skb_seq_read - Sequentially read skb data |
| 1631 | * @consumed: number of bytes consumed by the caller so far |
| 1632 | * @data: destination pointer for data to be returned |
| 1633 | * @st: state variable |
| 1634 | * |
| 1635 | * Reads a block of skb data at &consumed relative to the |
| 1636 | * lower offset specified to skb_prepare_seq_read(). Assigns |
| 1637 | * the head of the data block to &data and returns the length |
| 1638 | * of the block or 0 if the end of the skb data or the upper |
| 1639 | * offset has been reached. |
| 1640 | * |
| 1641 | * The caller is not required to consume all of the data |
| 1642 | * returned, i.e. &consumed is typically set to the number |
| 1643 | * of bytes already consumed and the next call to |
| 1644 | * skb_seq_read() will return the remaining part of the block. |
| 1645 | * |
| 1646 | * Note: The size of each block of data returned can be arbitary, |
| 1647 | * this limitation is the cost for zerocopy seqeuental |
| 1648 | * reads of potentially non linear data. |
| 1649 | * |
| 1650 | * Note: Fragment lists within fragments are not implemented |
| 1651 | * at the moment, state->root_skb could be replaced with |
| 1652 | * a stack for this purpose. |
| 1653 | */ |
| 1654 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 1655 | struct skb_seq_state *st) |
| 1656 | { |
| 1657 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 1658 | skb_frag_t *frag; |
| 1659 | |
| 1660 | if (unlikely(abs_offset >= st->upper_offset)) |
| 1661 | return 0; |
| 1662 | |
| 1663 | next_skb: |
| 1664 | block_limit = skb_headlen(st->cur_skb); |
| 1665 | |
| 1666 | if (abs_offset < block_limit) { |
| 1667 | *data = st->cur_skb->data + abs_offset; |
| 1668 | return block_limit - abs_offset; |
| 1669 | } |
| 1670 | |
| 1671 | if (st->frag_idx == 0 && !st->frag_data) |
| 1672 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 1673 | |
| 1674 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 1675 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
| 1676 | block_limit = frag->size + st->stepped_offset; |
| 1677 | |
| 1678 | if (abs_offset < block_limit) { |
| 1679 | if (!st->frag_data) |
| 1680 | st->frag_data = kmap_skb_frag(frag); |
| 1681 | |
| 1682 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 1683 | (abs_offset - st->stepped_offset); |
| 1684 | |
| 1685 | return block_limit - abs_offset; |
| 1686 | } |
| 1687 | |
| 1688 | if (st->frag_data) { |
| 1689 | kunmap_skb_frag(st->frag_data); |
| 1690 | st->frag_data = NULL; |
| 1691 | } |
| 1692 | |
| 1693 | st->frag_idx++; |
| 1694 | st->stepped_offset += frag->size; |
| 1695 | } |
| 1696 | |
| 1697 | if (st->cur_skb->next) { |
| 1698 | st->cur_skb = st->cur_skb->next; |
| 1699 | st->frag_idx = 0; |
| 1700 | goto next_skb; |
| 1701 | } else if (st->root_skb == st->cur_skb && |
| 1702 | skb_shinfo(st->root_skb)->frag_list) { |
| 1703 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
| 1704 | goto next_skb; |
| 1705 | } |
| 1706 | |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /** |
| 1711 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 1712 | * @st: state variable |
| 1713 | * |
| 1714 | * Must be called if skb_seq_read() was not called until it |
| 1715 | * returned 0. |
| 1716 | */ |
| 1717 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 1718 | { |
| 1719 | if (st->frag_data) |
| 1720 | kunmap_skb_frag(st->frag_data); |
| 1721 | } |
| 1722 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1723 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 1724 | |
| 1725 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 1726 | struct ts_config *conf, |
| 1727 | struct ts_state *state) |
| 1728 | { |
| 1729 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 1730 | } |
| 1731 | |
| 1732 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 1733 | { |
| 1734 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * skb_find_text - Find a text pattern in skb data |
| 1739 | * @skb: the buffer to look in |
| 1740 | * @from: search offset |
| 1741 | * @to: search limit |
| 1742 | * @config: textsearch configuration |
| 1743 | * @state: uninitialized textsearch state variable |
| 1744 | * |
| 1745 | * Finds a pattern in the skb data according to the specified |
| 1746 | * textsearch configuration. Use textsearch_next() to retrieve |
| 1747 | * subsequent occurrences of the pattern. Returns the offset |
| 1748 | * to the first occurrence or UINT_MAX if no match was found. |
| 1749 | */ |
| 1750 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
| 1751 | unsigned int to, struct ts_config *config, |
| 1752 | struct ts_state *state) |
| 1753 | { |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 1754 | unsigned int ret; |
| 1755 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1756 | config->get_next_block = skb_ts_get_next_block; |
| 1757 | config->finish = skb_ts_finish; |
| 1758 | |
| 1759 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); |
| 1760 | |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 1761 | ret = textsearch_find(config, state); |
| 1762 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1765 | /** |
| 1766 | * skb_append_datato_frags: - append the user data to a skb |
| 1767 | * @sk: sock structure |
| 1768 | * @skb: skb structure to be appened with user data. |
| 1769 | * @getfrag: call back function to be used for getting the user data |
| 1770 | * @from: pointer to user message iov |
| 1771 | * @length: length of the iov message |
| 1772 | * |
| 1773 | * Description: This procedure append the user data in the fragment part |
| 1774 | * of the skb if any page alloc fails user this procedure returns -ENOMEM |
| 1775 | */ |
| 1776 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, |
Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 1777 | int (*getfrag)(void *from, char *to, int offset, |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1778 | int len, int odd, struct sk_buff *skb), |
| 1779 | void *from, int length) |
| 1780 | { |
| 1781 | int frg_cnt = 0; |
| 1782 | skb_frag_t *frag = NULL; |
| 1783 | struct page *page = NULL; |
| 1784 | int copy, left; |
| 1785 | int offset = 0; |
| 1786 | int ret; |
| 1787 | |
| 1788 | do { |
| 1789 | /* Return error if we don't have space for new frag */ |
| 1790 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 1791 | if (frg_cnt >= MAX_SKB_FRAGS) |
| 1792 | return -EFAULT; |
| 1793 | |
| 1794 | /* allocate a new page for next frag */ |
| 1795 | page = alloc_pages(sk->sk_allocation, 0); |
| 1796 | |
| 1797 | /* If alloc_page fails just return failure and caller will |
| 1798 | * free previous allocated pages by doing kfree_skb() |
| 1799 | */ |
| 1800 | if (page == NULL) |
| 1801 | return -ENOMEM; |
| 1802 | |
| 1803 | /* initialize the next frag */ |
| 1804 | sk->sk_sndmsg_page = page; |
| 1805 | sk->sk_sndmsg_off = 0; |
| 1806 | skb_fill_page_desc(skb, frg_cnt, page, 0, 0); |
| 1807 | skb->truesize += PAGE_SIZE; |
| 1808 | atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); |
| 1809 | |
| 1810 | /* get the new initialized frag */ |
| 1811 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 1812 | frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; |
| 1813 | |
| 1814 | /* copy the user data to page */ |
| 1815 | left = PAGE_SIZE - frag->page_offset; |
| 1816 | copy = (length > left)? left : length; |
| 1817 | |
| 1818 | ret = getfrag(from, (page_address(frag->page) + |
| 1819 | frag->page_offset + frag->size), |
| 1820 | offset, copy, 0, skb); |
| 1821 | if (ret < 0) |
| 1822 | return -EFAULT; |
| 1823 | |
| 1824 | /* copy was successful so update the size parameters */ |
| 1825 | sk->sk_sndmsg_off += copy; |
| 1826 | frag->size += copy; |
| 1827 | skb->len += copy; |
| 1828 | skb->data_len += copy; |
| 1829 | offset += copy; |
| 1830 | length -= copy; |
| 1831 | |
| 1832 | } while (length > 0); |
| 1833 | |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 1837 | /** |
| 1838 | * skb_pull_rcsum - pull skb and update receive checksum |
| 1839 | * @skb: buffer to update |
| 1840 | * @start: start of data before pull |
| 1841 | * @len: length of data pulled |
| 1842 | * |
| 1843 | * This function performs an skb_pull on the packet and updates |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1844 | * update the CHECKSUM_COMPLETE checksum. It should be used on |
| 1845 | * receive path processing instead of skb_pull unless you know |
| 1846 | * that the checksum difference is zero (e.g., a valid IP header) |
| 1847 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 1848 | */ |
| 1849 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 1850 | { |
| 1851 | BUG_ON(len > skb->len); |
| 1852 | skb->len -= len; |
| 1853 | BUG_ON(skb->len < skb->data_len); |
| 1854 | skb_postpull_rcsum(skb, skb->data, len); |
| 1855 | return skb->data += len; |
| 1856 | } |
| 1857 | |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 1858 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 1859 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1860 | /** |
| 1861 | * skb_segment - Perform protocol segmentation on skb. |
| 1862 | * @skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1863 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1864 | * |
| 1865 | * This function performs segmentation on the given skb. It returns |
| 1866 | * the segment at the given position. It returns NULL if there are |
| 1867 | * no more segments to generate, or when an error is encountered. |
| 1868 | */ |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1869 | struct sk_buff *skb_segment(struct sk_buff *skb, int features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1870 | { |
| 1871 | struct sk_buff *segs = NULL; |
| 1872 | struct sk_buff *tail = NULL; |
| 1873 | unsigned int mss = skb_shinfo(skb)->gso_size; |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 1874 | unsigned int doffset = skb->data - skb_mac_header(skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1875 | unsigned int offset = doffset; |
| 1876 | unsigned int headroom; |
| 1877 | unsigned int len; |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1878 | int sg = features & NETIF_F_SG; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1879 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1880 | int err = -ENOMEM; |
| 1881 | int i = 0; |
| 1882 | int pos; |
| 1883 | |
| 1884 | __skb_push(skb, doffset); |
| 1885 | headroom = skb_headroom(skb); |
| 1886 | pos = skb_headlen(skb); |
| 1887 | |
| 1888 | do { |
| 1889 | struct sk_buff *nskb; |
| 1890 | skb_frag_t *frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1891 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1892 | int k; |
| 1893 | int size; |
| 1894 | |
| 1895 | len = skb->len - offset; |
| 1896 | if (len > mss) |
| 1897 | len = mss; |
| 1898 | |
| 1899 | hsize = skb_headlen(skb) - offset; |
| 1900 | if (hsize < 0) |
| 1901 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1902 | if (hsize > len || !sg) |
| 1903 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1904 | |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1905 | nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1906 | if (unlikely(!nskb)) |
| 1907 | goto err; |
| 1908 | |
| 1909 | if (segs) |
| 1910 | tail->next = nskb; |
| 1911 | else |
| 1912 | segs = nskb; |
| 1913 | tail = nskb; |
| 1914 | |
| 1915 | nskb->dev = skb->dev; |
| 1916 | nskb->priority = skb->priority; |
| 1917 | nskb->protocol = skb->protocol; |
| 1918 | nskb->dst = dst_clone(skb->dst); |
| 1919 | memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); |
| 1920 | nskb->pkt_type = skb->pkt_type; |
| 1921 | nskb->mac_len = skb->mac_len; |
| 1922 | |
| 1923 | skb_reserve(nskb, headroom); |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 1924 | skb_reset_mac_header(nskb); |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 1925 | skb_set_network_header(nskb, skb->mac_len); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1926 | nskb->transport_header = (nskb->network_header + |
| 1927 | skb_network_header_len(skb)); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1928 | memcpy(skb_put(nskb, doffset), skb->data, doffset); |
| 1929 | |
| 1930 | if (!sg) { |
| 1931 | nskb->csum = skb_copy_and_csum_bits(skb, offset, |
| 1932 | skb_put(nskb, len), |
| 1933 | len, 0); |
| 1934 | continue; |
| 1935 | } |
| 1936 | |
| 1937 | frag = skb_shinfo(nskb)->frags; |
| 1938 | k = 0; |
| 1939 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1940 | nskb->ip_summed = CHECKSUM_PARTIAL; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1941 | nskb->csum = skb->csum; |
| 1942 | memcpy(skb_put(nskb, hsize), skb->data + offset, hsize); |
| 1943 | |
| 1944 | while (pos < offset + len) { |
| 1945 | BUG_ON(i >= nfrags); |
| 1946 | |
| 1947 | *frag = skb_shinfo(skb)->frags[i]; |
| 1948 | get_page(frag->page); |
| 1949 | size = frag->size; |
| 1950 | |
| 1951 | if (pos < offset) { |
| 1952 | frag->page_offset += offset - pos; |
| 1953 | frag->size -= offset - pos; |
| 1954 | } |
| 1955 | |
| 1956 | k++; |
| 1957 | |
| 1958 | if (pos + size <= offset + len) { |
| 1959 | i++; |
| 1960 | pos += size; |
| 1961 | } else { |
| 1962 | frag->size -= pos + size - (offset + len); |
| 1963 | break; |
| 1964 | } |
| 1965 | |
| 1966 | frag++; |
| 1967 | } |
| 1968 | |
| 1969 | skb_shinfo(nskb)->nr_frags = k; |
| 1970 | nskb->data_len = len - hsize; |
| 1971 | nskb->len += nskb->data_len; |
| 1972 | nskb->truesize += nskb->data_len; |
| 1973 | } while ((offset += len) < skb->len); |
| 1974 | |
| 1975 | return segs; |
| 1976 | |
| 1977 | err: |
| 1978 | while ((skb = segs)) { |
| 1979 | segs = skb->next; |
Patrick McHardy | b08d584 | 2007-02-27 09:57:37 -0800 | [diff] [blame] | 1980 | kfree_skb(skb); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1981 | } |
| 1982 | return ERR_PTR(err); |
| 1983 | } |
| 1984 | |
| 1985 | EXPORT_SYMBOL_GPL(skb_segment); |
| 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | void __init skb_init(void) |
| 1988 | { |
| 1989 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 1990 | sizeof(struct sk_buff), |
| 1991 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 1992 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | NULL, NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 1994 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
| 1995 | (2*sizeof(struct sk_buff)) + |
| 1996 | sizeof(atomic_t), |
| 1997 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 1998 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 1999 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | EXPORT_SYMBOL(___pskb_trim); |
| 2003 | EXPORT_SYMBOL(__kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 2004 | EXPORT_SYMBOL(kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | EXPORT_SYMBOL(__pskb_pull_tail); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2006 | EXPORT_SYMBOL(__alloc_skb); |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 2007 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | EXPORT_SYMBOL(pskb_copy); |
| 2009 | EXPORT_SYMBOL(pskb_expand_head); |
| 2010 | EXPORT_SYMBOL(skb_checksum); |
| 2011 | EXPORT_SYMBOL(skb_clone); |
| 2012 | EXPORT_SYMBOL(skb_clone_fraglist); |
| 2013 | EXPORT_SYMBOL(skb_copy); |
| 2014 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
| 2015 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
| 2016 | EXPORT_SYMBOL(skb_copy_bits); |
| 2017 | EXPORT_SYMBOL(skb_copy_expand); |
| 2018 | EXPORT_SYMBOL(skb_over_panic); |
| 2019 | EXPORT_SYMBOL(skb_pad); |
| 2020 | EXPORT_SYMBOL(skb_realloc_headroom); |
| 2021 | EXPORT_SYMBOL(skb_under_panic); |
| 2022 | EXPORT_SYMBOL(skb_dequeue); |
| 2023 | EXPORT_SYMBOL(skb_dequeue_tail); |
| 2024 | EXPORT_SYMBOL(skb_insert); |
| 2025 | EXPORT_SYMBOL(skb_queue_purge); |
| 2026 | EXPORT_SYMBOL(skb_queue_head); |
| 2027 | EXPORT_SYMBOL(skb_queue_tail); |
| 2028 | EXPORT_SYMBOL(skb_unlink); |
| 2029 | EXPORT_SYMBOL(skb_append); |
| 2030 | EXPORT_SYMBOL(skb_split); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2031 | EXPORT_SYMBOL(skb_prepare_seq_read); |
| 2032 | EXPORT_SYMBOL(skb_seq_read); |
| 2033 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2034 | EXPORT_SYMBOL(skb_find_text); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2035 | EXPORT_SYMBOL(skb_append_datato_frags); |