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