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