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