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