blob: 3402eca768f87d90514da8971632194fd4b3f47b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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 Axboe9c55e012007-11-06 23:30:13 -080055#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/cache.h>
57#include <linux/rtnetlink.h>
58#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070059#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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 Viroa1f8e7f2006-10-19 16:08:53 -040070#include "kmap_skb.h"
71
Christoph Lametere18b8902006-12-06 20:33:20 -080072static struct kmem_cache *skbuff_head_cache __read_mostly;
73static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jens Axboe9c55e012007-11-06 23:30:13 -080075static 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
83static 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
91static 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. */
99static 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 Torvalds1da177e2005-04-16 15:20:36 -0700109/*
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 */
123void skb_over_panic(struct sk_buff *skb, int sz, void *here)
124{
Patrick McHardy26095452005-04-21 16:43:02 -0700125 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700126 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700127 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700128 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700129 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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
142void skb_under_panic(struct sk_buff *skb, int sz, void *here)
143{
Patrick McHardy26095452005-04-21 16:43:02 -0700144 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700145 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700146 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700147 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700148 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 BUG();
150}
151
David S. Millerdc6de332006-04-20 00:10:50 -0700152void 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}
158EXPORT_SYMBOL(skb_truesize_bug);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* 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. Millerd179cd12005-08-17 14:57:30 -0700167 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @size: size to allocate
169 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700170 * @fclone: allocate from fclone cache instead of head cache
171 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800172 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
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 Virodd0fc662005-10-07 07:46:04 +0100181struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800182 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Christoph Lametere18b8902006-12-06 20:33:20 -0800184 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800185 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct sk_buff *skb;
187 u8 *data;
188
Herbert Xu8798b3f2006-01-23 16:32:45 -0800189 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800192 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!skb)
194 goto out;
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800197 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
198 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!data)
200 goto nodata;
201
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300202 /*
203 * See comment in sk_buff definition, just before the 'tail' member
204 */
205 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->truesize = size + sizeof(struct sk_buff);
207 atomic_set(&skb->users, 1);
208 skb->head = data;
209 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700210 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700211 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 /* make sure we initialize shinfo sequentially */
213 shinfo = skb_shinfo(skb);
214 atomic_set(&shinfo->dataref, 1);
215 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700216 shinfo->gso_size = 0;
217 shinfo->gso_segs = 0;
218 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800219 shinfo->ip6_frag_id = 0;
220 shinfo->frag_list = NULL;
221
David S. Millerd179cd12005-08-17 14:57:30 -0700222 if (fclone) {
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David S. Millerd179cd12005-08-17 14:57:30 -0700226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
228
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231out:
232 return skb;
233nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800234 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 skb = NULL;
236 goto out;
237}
238
239/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700240 * __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 */
252struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700256 struct sk_buff *skb;
257
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700259 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700260 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700261 skb->dev = dev;
262 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700263 return skb;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Herbert Xu27b437c2006-07-13 19:26:39 -0700266static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Herbert Xu27b437c2006-07-13 19:26:39 -0700268 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Herbert Xu27b437c2006-07-13 19:26:39 -0700270 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 do {
273 struct sk_buff *this = list;
274 list = list->next;
275 kfree_skb(this);
276 } while (list);
277}
278
Herbert Xu27b437c2006-07-13 19:26:39 -0700279static inline void skb_drop_fraglist(struct sk_buff *skb)
280{
281 skb_drop_list(&skb_shinfo(skb)->frag_list);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static 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 Bunk5bba1712006-06-29 13:02:35 -0700292static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
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 Xu2d4baff2007-11-26 23:11:19 +0800313static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
David S. Millerd179cd12005-08-17 14:57:30 -0700315 struct sk_buff *other;
316 atomic_t *fclone_ref;
317
David S. Millerd179cd12005-08-17 14:57:30 -0700318 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 Hemminger3ff50b72007-04-20 17:09:22 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Herbert Xu2d4baff2007-11-26 23:11:19 +0800344/* Free everything but the sk_buff shell. */
345static void skb_release_all(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dst_release(skb->dst);
348#ifdef CONFIG_XFRM
349 secpath_put(skb->sp);
350#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700351 if (skb->destructor) {
352 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 skb->destructor(skb);
354 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800355#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700356 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800357 nf_conntrack_put_reasm(skb->nfct_reasm);
358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#ifdef CONFIG_BRIDGE_NETFILTER
360 nf_bridge_put(skb->nf_bridge);
361#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700367#endif
368#endif
Herbert Xu2d4baff2007-11-26 23:11:19 +0800369 skb_release_data(skb);
370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Herbert Xu2d4baff2007-11-26 23:11:19 +0800372/**
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
381void __kfree_skb(struct sk_buff *skb)
382{
383 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree_skbmem(skb);
385}
386
387/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800388 * 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 */
394void 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 Xudec18812007-10-14 00:37:30 -0700405static 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 Xue0053ec2007-10-14 00:37:52 -0700443static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#define C(x) n->x = skb->x
446
447 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700449 __copy_skb_header(n, skb);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 C(len);
452 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700453 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700454 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800455 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800458 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 C(tail);
460 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800461 C(head);
462 C(data);
463 C(truesize);
464 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 atomic_inc(&(skb_shinfo(skb)->dataref));
467 skb->cloned = 1;
468
469 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700470#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 */
483struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
484{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800485 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700486 return __skb_clone(dst, src);
487}
488EXPORT_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
504struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
525{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700526#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * Shift between the two data areas in bytes
529 */
530 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700531#endif
Herbert Xudec18812007-10-14 00:37:30 -0700532
533 __copy_skb_header(new, old);
534
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700535#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 Xu79671682006-06-22 02:40:14 -0700541 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 Torvalds1da177e2005-04-16 15:20:36 -0700544}
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 Virodd0fc662005-10-07 07:46:04 +0100563struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int headerlen = skb->data - skb->head;
566 /*
567 * Allocate the copy buffer
568 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700569 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 Torvalds1da177e2005-04-16 15:20:36 -0700575 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 Torvalds1da177e2005-04-16 15:20:36 -0700582
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 Virodd0fc662005-10-07 07:46:04 +0100604struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 /*
607 * Allocate the copy buffer
608 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700609 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 Torvalds1da177e2005-04-16 15:20:36 -0700615 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 Melod626f622007-03-27 18:55:52 -0300623 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Herbert Xu25f484a2006-11-07 14:57:15 -0800625 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 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);
645out:
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 Fusco86a76ca2005-07-08 14:57:47 -0700665int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100666 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 int i;
669 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700670#ifdef NET_SKBUFF_DATA_USES_OFFSET
671 int size = nhead + skb->end + ntail;
672#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 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 Melo4305b542007-04-19 20:43:29 -0700688#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700689 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700690#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700691 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700692#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700693 memcpy(data + size, skb_end_pointer(skb),
694 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
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 Torvalds1da177e2005-04-16 15:20:36 -0700707 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700708#ifdef NET_SKBUFF_DATA_USES_OFFSET
709 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700710 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700711#else
712 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700713#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700714 /* {transport,network,mac}_header and tail are relative to skb->head */
715 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700716 skb->transport_header += off;
717 skb->network_header += off;
718 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700719 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700721 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 skb->nohdr = 0;
723 atomic_set(&skb_shinfo(skb)->dataref, 1);
724 return 0;
725
726nodata:
727 return -ENOMEM;
728}
729
730/* Make private copy of skb with writable head and some headroom */
731
732struct 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 Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700770 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100771 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 /*
774 * Allocate the copy buffer
775 */
776 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
777 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700778 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700780 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
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 McHardyefd1e8d2007-04-10 18:30:09 -0700790 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 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 McHardyefd1e8d2007-04-10 18:30:09 -0700804 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700805 n->csum_start += off;
806#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700807 n->transport_header += off;
808 n->network_header += off;
809 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700810#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 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 Xu5b057c62006-06-23 02:06:41 -0700824 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900826
Herbert Xu5b057c62006-06-23 02:06:41 -0700827int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Herbert Xu5b057c62006-06-23 02:06:41 -0700829 int err;
830 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700833 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700837
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700838 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700839 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
855free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700857 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900858}
859
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700860/**
861 * skb_put - add data to a buffer
862 * @skb: buffer to use
863 * @len: amount of data to add
864 *
865 * This function extends the used data area of the buffer. If this would
866 * exceed the total buffer size the kernel will panic. A pointer to the
867 * first byte of the extra data is returned.
868 */
869unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
870{
871 unsigned char *tmp = skb_tail_pointer(skb);
872 SKB_LINEAR_ASSERT(skb);
873 skb->tail += len;
874 skb->len += len;
875 if (unlikely(skb->tail > skb->end))
876 skb_over_panic(skb, len, __builtin_return_address(0));
877 return tmp;
878}
879EXPORT_SYMBOL(skb_put);
880
Herbert Xu3cc0e872006-06-09 16:13:38 -0700881/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 */
883
Herbert Xu3cc0e872006-06-09 16:13:38 -0700884int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Herbert Xu27b437c2006-07-13 19:26:39 -0700886 struct sk_buff **fragp;
887 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int offset = skb_headlen(skb);
889 int nfrags = skb_shinfo(skb)->nr_frags;
890 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700891 int err;
892
893 if (skb_cloned(skb) &&
894 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
895 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700897 i = 0;
898 if (offset >= len)
899 goto drop_pages;
900
901 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700903
904 if (end < len) {
905 offset = end;
906 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700908
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700909 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700910
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700911drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700912 skb_shinfo(skb)->nr_frags = i;
913
914 for (; i < nfrags; i++)
915 put_page(skb_shinfo(skb)->frags[i].page);
916
917 if (skb_shinfo(skb)->frag_list)
918 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700919 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Herbert Xu27b437c2006-07-13 19:26:39 -0700922 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
923 fragp = &frag->next) {
924 int end = offset + frag->len;
925
926 if (skb_shared(frag)) {
927 struct sk_buff *nfrag;
928
929 nfrag = skb_clone(frag, GFP_ATOMIC);
930 if (unlikely(!nfrag))
931 return -ENOMEM;
932
933 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700934 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700935 frag = nfrag;
936 *fragp = frag;
937 }
938
939 if (end < len) {
940 offset = end;
941 continue;
942 }
943
944 if (end > len &&
945 unlikely((err = pskb_trim(frag, len - offset))))
946 return err;
947
948 if (frag->next)
949 skb_drop_list(&frag->next);
950 break;
951 }
952
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700953done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700954 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 skb->data_len -= skb->len - len;
956 skb->len = len;
957 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700958 skb->len = len;
959 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700960 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
963 return 0;
964}
965
966/**
967 * __pskb_pull_tail - advance tail of skb header
968 * @skb: buffer to reallocate
969 * @delta: number of bytes to advance tail
970 *
971 * The function makes a sense only on a fragmented &sk_buff,
972 * it expands header moving its tail forward and copying necessary
973 * data from fragmented part.
974 *
975 * &sk_buff MUST have reference count of 1.
976 *
977 * Returns %NULL (and &sk_buff does not change) if pull failed
978 * or value of new tail of skb in the case of success.
979 *
980 * All the pointers pointing into skb header may change and must be
981 * reloaded after call to this function.
982 */
983
984/* Moves tail of skb head forward, copying data from fragmented part,
985 * when it is necessary.
986 * 1. It may fail due to malloc failure.
987 * 2. It may change skb pointers.
988 *
989 * It is pretty complicated. Luckily, it is called only in exceptional cases.
990 */
991unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
992{
993 /* If skb has not enough free space at tail, get new one
994 * plus 128 bytes for future expansions. If we have enough
995 * room at tail, reallocate without expansion only if skb is cloned.
996 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700997 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (eat > 0 || skb_cloned(skb)) {
1000 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1001 GFP_ATOMIC))
1002 return NULL;
1003 }
1004
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001005 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 BUG();
1007
1008 /* Optimization: no fragments, no reasons to preestimate
1009 * size of pulled pages. Superb.
1010 */
1011 if (!skb_shinfo(skb)->frag_list)
1012 goto pull_pages;
1013
1014 /* Estimate size of pulled pages. */
1015 eat = delta;
1016 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1017 if (skb_shinfo(skb)->frags[i].size >= eat)
1018 goto pull_pages;
1019 eat -= skb_shinfo(skb)->frags[i].size;
1020 }
1021
1022 /* If we need update frag list, we are in troubles.
1023 * Certainly, it possible to add an offset to skb data,
1024 * but taking into account that pulling is expected to
1025 * be very rare operation, it is worth to fight against
1026 * further bloating skb head and crucify ourselves here instead.
1027 * Pure masohism, indeed. 8)8)
1028 */
1029 if (eat) {
1030 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1031 struct sk_buff *clone = NULL;
1032 struct sk_buff *insp = NULL;
1033
1034 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001035 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (list->len <= eat) {
1038 /* Eaten as whole. */
1039 eat -= list->len;
1040 list = list->next;
1041 insp = list;
1042 } else {
1043 /* Eaten partially. */
1044
1045 if (skb_shared(list)) {
1046 /* Sucks! We need to fork list. :-( */
1047 clone = skb_clone(list, GFP_ATOMIC);
1048 if (!clone)
1049 return NULL;
1050 insp = list->next;
1051 list = clone;
1052 } else {
1053 /* This may be pulled without
1054 * problems. */
1055 insp = list;
1056 }
1057 if (!pskb_pull(list, eat)) {
1058 if (clone)
1059 kfree_skb(clone);
1060 return NULL;
1061 }
1062 break;
1063 }
1064 } while (eat);
1065
1066 /* Free pulled out fragments. */
1067 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1068 skb_shinfo(skb)->frag_list = list->next;
1069 kfree_skb(list);
1070 }
1071 /* And insert new clone at head. */
1072 if (clone) {
1073 clone->next = list;
1074 skb_shinfo(skb)->frag_list = clone;
1075 }
1076 }
1077 /* Success! Now we may commit changes to skb data. */
1078
1079pull_pages:
1080 eat = delta;
1081 k = 0;
1082 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1083 if (skb_shinfo(skb)->frags[i].size <= eat) {
1084 put_page(skb_shinfo(skb)->frags[i].page);
1085 eat -= skb_shinfo(skb)->frags[i].size;
1086 } else {
1087 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1088 if (eat) {
1089 skb_shinfo(skb)->frags[k].page_offset += eat;
1090 skb_shinfo(skb)->frags[k].size -= eat;
1091 eat = 0;
1092 }
1093 k++;
1094 }
1095 }
1096 skb_shinfo(skb)->nr_frags = k;
1097
1098 skb->tail += delta;
1099 skb->data_len -= delta;
1100
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001101 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/* Copy some data bits from skb to kernel buffer. */
1105
1106int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1107{
1108 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001109 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (offset > (int)skb->len - len)
1112 goto fault;
1113
1114 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001115 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (copy > len)
1117 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001118 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if ((len -= copy) == 0)
1120 return 0;
1121 offset += copy;
1122 to += copy;
1123 }
1124
1125 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001126 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
David S. Miller1a028e52007-04-27 15:21:23 -07001128 BUG_TRAP(start <= offset + len);
1129
1130 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if ((copy = end - offset) > 0) {
1132 u8 *vaddr;
1133
1134 if (copy > len)
1135 copy = len;
1136
1137 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1138 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001139 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1140 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 kunmap_skb_frag(vaddr);
1142
1143 if ((len -= copy) == 0)
1144 return 0;
1145 offset += copy;
1146 to += copy;
1147 }
David S. Miller1a028e52007-04-27 15:21:23 -07001148 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
1151 if (skb_shinfo(skb)->frag_list) {
1152 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1153
1154 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001155 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
David S. Miller1a028e52007-04-27 15:21:23 -07001157 BUG_TRAP(start <= offset + len);
1158
1159 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if ((copy = end - offset) > 0) {
1161 if (copy > len)
1162 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001163 if (skb_copy_bits(list, offset - start,
1164 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto fault;
1166 if ((len -= copy) == 0)
1167 return 0;
1168 offset += copy;
1169 to += copy;
1170 }
David S. Miller1a028e52007-04-27 15:21:23 -07001171 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174 if (!len)
1175 return 0;
1176
1177fault:
1178 return -EFAULT;
1179}
1180
Jens Axboe9c55e012007-11-06 23:30:13 -08001181/*
1182 * Callback from splice_to_pipe(), if we need to release some pages
1183 * at the end of the spd in case we error'ed out in filling the pipe.
1184 */
1185static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1186{
1187 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1188
1189 kfree_skb(skb);
1190}
1191
1192/*
1193 * Fill page/offset/length into spd, if it can hold more pages.
1194 */
1195static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1196 unsigned int len, unsigned int offset,
1197 struct sk_buff *skb)
1198{
1199 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1200 return 1;
1201
1202 spd->pages[spd->nr_pages] = page;
1203 spd->partial[spd->nr_pages].len = len;
1204 spd->partial[spd->nr_pages].offset = offset;
1205 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1206 spd->nr_pages++;
1207 return 0;
1208}
1209
1210/*
1211 * Map linear and fragment data from the skb to spd. Returns number of
1212 * pages mapped.
1213 */
1214static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1215 unsigned int *total_len,
1216 struct splice_pipe_desc *spd)
1217{
1218 unsigned int nr_pages = spd->nr_pages;
1219 unsigned int poff, plen, len, toff, tlen;
1220 int headlen, seg;
1221
1222 toff = *offset;
1223 tlen = *total_len;
1224 if (!tlen)
1225 goto err;
1226
1227 /*
1228 * if the offset is greater than the linear part, go directly to
1229 * the fragments.
1230 */
1231 headlen = skb_headlen(skb);
1232 if (toff >= headlen) {
1233 toff -= headlen;
1234 goto map_frag;
1235 }
1236
1237 /*
1238 * first map the linear region into the pages/partial map, skipping
1239 * any potential initial offset.
1240 */
1241 len = 0;
1242 while (len < headlen) {
1243 void *p = skb->data + len;
1244
1245 poff = (unsigned long) p & (PAGE_SIZE - 1);
1246 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
1247 len += plen;
1248
1249 if (toff) {
1250 if (plen <= toff) {
1251 toff -= plen;
1252 continue;
1253 }
1254 plen -= toff;
1255 poff += toff;
1256 toff = 0;
1257 }
1258
1259 plen = min(plen, tlen);
1260 if (!plen)
1261 break;
1262
1263 /*
1264 * just jump directly to update and return, no point
1265 * in going over fragments when the output is full.
1266 */
1267 if (spd_fill_page(spd, virt_to_page(p), plen, poff, skb))
1268 goto done;
1269
1270 tlen -= plen;
1271 }
1272
1273 /*
1274 * then map the fragments
1275 */
1276map_frag:
1277 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1278 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1279
1280 plen = f->size;
1281 poff = f->page_offset;
1282
1283 if (toff) {
1284 if (plen <= toff) {
1285 toff -= plen;
1286 continue;
1287 }
1288 plen -= toff;
1289 poff += toff;
1290 toff = 0;
1291 }
1292
1293 plen = min(plen, tlen);
1294 if (!plen)
1295 break;
1296
1297 if (spd_fill_page(spd, f->page, plen, poff, skb))
1298 break;
1299
1300 tlen -= plen;
1301 }
1302
1303done:
1304 if (spd->nr_pages - nr_pages) {
1305 *offset = 0;
1306 *total_len = tlen;
1307 return 0;
1308 }
1309err:
1310 return 1;
1311}
1312
1313/*
1314 * Map data from the skb to a pipe. Should handle both the linear part,
1315 * the fragments, and the frag list. It does NOT handle frag lists within
1316 * the frag list, if such a thing exists. We'd probably need to recurse to
1317 * handle that cleanly.
1318 */
1319int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1320 struct pipe_inode_info *pipe, unsigned int tlen,
1321 unsigned int flags)
1322{
1323 struct partial_page partial[PIPE_BUFFERS];
1324 struct page *pages[PIPE_BUFFERS];
1325 struct splice_pipe_desc spd = {
1326 .pages = pages,
1327 .partial = partial,
1328 .flags = flags,
1329 .ops = &sock_pipe_buf_ops,
1330 .spd_release = sock_spd_release,
1331 };
1332 struct sk_buff *skb;
1333
1334 /*
1335 * I'd love to avoid the clone here, but tcp_read_sock()
1336 * ignores reference counts and unconditonally kills the sk_buff
1337 * on return from the actor.
1338 */
1339 skb = skb_clone(__skb, GFP_KERNEL);
1340 if (unlikely(!skb))
1341 return -ENOMEM;
1342
1343 /*
1344 * __skb_splice_bits() only fails if the output has no room left,
1345 * so no point in going over the frag_list for the error case.
1346 */
1347 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1348 goto done;
1349 else if (!tlen)
1350 goto done;
1351
1352 /*
1353 * now see if we have a frag_list to map
1354 */
1355 if (skb_shinfo(skb)->frag_list) {
1356 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1357
1358 for (; list && tlen; list = list->next) {
1359 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1360 break;
1361 }
1362 }
1363
1364done:
1365 /*
1366 * drop our reference to the clone, the pipe consumption will
1367 * drop the rest.
1368 */
1369 kfree_skb(skb);
1370
1371 if (spd.nr_pages) {
1372 int ret;
1373
1374 /*
1375 * Drop the socket lock, otherwise we have reverse
1376 * locking dependencies between sk_lock and i_mutex
1377 * here as compared to sendfile(). We enter here
1378 * with the socket lock held, and splice_to_pipe() will
1379 * grab the pipe inode lock. For sendfile() emulation,
1380 * we call into ->sendpage() with the i_mutex lock held
1381 * and networking will grab the socket lock.
1382 */
1383 release_sock(__skb->sk);
1384 ret = splice_to_pipe(pipe, &spd);
1385 lock_sock(__skb->sk);
1386 return ret;
1387 }
1388
1389 return 0;
1390}
1391
Herbert Xu357b40a2005-04-19 22:30:14 -07001392/**
1393 * skb_store_bits - store bits from kernel buffer to skb
1394 * @skb: destination buffer
1395 * @offset: offset in destination
1396 * @from: source buffer
1397 * @len: number of bytes to copy
1398 *
1399 * Copy the specified number of bytes from the source buffer to the
1400 * destination skb. This function handles all the messy bits of
1401 * traversing fragment lists and such.
1402 */
1403
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001404int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001405{
1406 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001407 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001408
1409 if (offset > (int)skb->len - len)
1410 goto fault;
1411
David S. Miller1a028e52007-04-27 15:21:23 -07001412 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001413 if (copy > len)
1414 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001415 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001416 if ((len -= copy) == 0)
1417 return 0;
1418 offset += copy;
1419 from += copy;
1420 }
1421
1422 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1423 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001424 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001425
David S. Miller1a028e52007-04-27 15:21:23 -07001426 BUG_TRAP(start <= offset + len);
1427
1428 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001429 if ((copy = end - offset) > 0) {
1430 u8 *vaddr;
1431
1432 if (copy > len)
1433 copy = len;
1434
1435 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001436 memcpy(vaddr + frag->page_offset + offset - start,
1437 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001438 kunmap_skb_frag(vaddr);
1439
1440 if ((len -= copy) == 0)
1441 return 0;
1442 offset += copy;
1443 from += copy;
1444 }
David S. Miller1a028e52007-04-27 15:21:23 -07001445 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001446 }
1447
1448 if (skb_shinfo(skb)->frag_list) {
1449 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1450
1451 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001452 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001453
David S. Miller1a028e52007-04-27 15:21:23 -07001454 BUG_TRAP(start <= offset + len);
1455
1456 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001457 if ((copy = end - offset) > 0) {
1458 if (copy > len)
1459 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001460 if (skb_store_bits(list, offset - start,
1461 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001462 goto fault;
1463 if ((len -= copy) == 0)
1464 return 0;
1465 offset += copy;
1466 from += copy;
1467 }
David S. Miller1a028e52007-04-27 15:21:23 -07001468 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001469 }
1470 }
1471 if (!len)
1472 return 0;
1473
1474fault:
1475 return -EFAULT;
1476}
1477
1478EXPORT_SYMBOL(skb_store_bits);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/* Checksum skb data. */
1481
Al Viro2bbbc862006-11-14 21:37:14 -08001482__wsum skb_checksum(const struct sk_buff *skb, int offset,
1483 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
David S. Miller1a028e52007-04-27 15:21:23 -07001485 int start = skb_headlen(skb);
1486 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int pos = 0;
1488
1489 /* Checksum header. */
1490 if (copy > 0) {
1491 if (copy > len)
1492 copy = len;
1493 csum = csum_partial(skb->data + offset, copy, csum);
1494 if ((len -= copy) == 0)
1495 return csum;
1496 offset += copy;
1497 pos = copy;
1498 }
1499
1500 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001501 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David S. Miller1a028e52007-04-27 15:21:23 -07001503 BUG_TRAP(start <= offset + len);
1504
1505 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001507 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 u8 *vaddr;
1509 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1510
1511 if (copy > len)
1512 copy = len;
1513 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001514 csum2 = csum_partial(vaddr + frag->page_offset +
1515 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 kunmap_skb_frag(vaddr);
1517 csum = csum_block_add(csum, csum2, pos);
1518 if (!(len -= copy))
1519 return csum;
1520 offset += copy;
1521 pos += copy;
1522 }
David S. Miller1a028e52007-04-27 15:21:23 -07001523 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
1526 if (skb_shinfo(skb)->frag_list) {
1527 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1528
1529 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001530 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
David S. Miller1a028e52007-04-27 15:21:23 -07001532 BUG_TRAP(start <= offset + len);
1533
1534 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001536 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (copy > len)
1538 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001539 csum2 = skb_checksum(list, offset - start,
1540 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 csum = csum_block_add(csum, csum2, pos);
1542 if ((len -= copy) == 0)
1543 return csum;
1544 offset += copy;
1545 pos += copy;
1546 }
David S. Miller1a028e52007-04-27 15:21:23 -07001547 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001550 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 return csum;
1553}
1554
1555/* Both of above in one bottle. */
1556
Al Viro81d77662006-11-14 21:37:33 -08001557__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1558 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
David S. Miller1a028e52007-04-27 15:21:23 -07001560 int start = skb_headlen(skb);
1561 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int pos = 0;
1563
1564 /* Copy header. */
1565 if (copy > 0) {
1566 if (copy > len)
1567 copy = len;
1568 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1569 copy, csum);
1570 if ((len -= copy) == 0)
1571 return csum;
1572 offset += copy;
1573 to += copy;
1574 pos = copy;
1575 }
1576
1577 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001578 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
David S. Miller1a028e52007-04-27 15:21:23 -07001580 BUG_TRAP(start <= offset + len);
1581
1582 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001584 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 u8 *vaddr;
1586 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1587
1588 if (copy > len)
1589 copy = len;
1590 vaddr = kmap_skb_frag(frag);
1591 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001592 frag->page_offset +
1593 offset - start, to,
1594 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 kunmap_skb_frag(vaddr);
1596 csum = csum_block_add(csum, csum2, pos);
1597 if (!(len -= copy))
1598 return csum;
1599 offset += copy;
1600 to += copy;
1601 pos += copy;
1602 }
David S. Miller1a028e52007-04-27 15:21:23 -07001603 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
1606 if (skb_shinfo(skb)->frag_list) {
1607 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1608
1609 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001610 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001611 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
David S. Miller1a028e52007-04-27 15:21:23 -07001613 BUG_TRAP(start <= offset + len);
1614
1615 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if ((copy = end - offset) > 0) {
1617 if (copy > len)
1618 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001619 csum2 = skb_copy_and_csum_bits(list,
1620 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 to, copy, 0);
1622 csum = csum_block_add(csum, csum2, pos);
1623 if ((len -= copy) == 0)
1624 return csum;
1625 offset += copy;
1626 to += copy;
1627 pos += copy;
1628 }
David S. Miller1a028e52007-04-27 15:21:23 -07001629 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001632 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return csum;
1634}
1635
1636void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1637{
Al Virod3bc23e2006-11-14 21:24:49 -08001638 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 long csstart;
1640
Patrick McHardy84fa7932006-08-29 16:44:56 -07001641 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001642 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 else
1644 csstart = skb_headlen(skb);
1645
Kris Katterjohn09a62662006-01-08 22:24:28 -08001646 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001648 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 csum = 0;
1651 if (csstart != skb->len)
1652 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1653 skb->len - csstart, 0);
1654
Patrick McHardy84fa7932006-08-29 16:44:56 -07001655 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001656 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Al Virod3bc23e2006-11-14 21:24:49 -08001658 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660}
1661
1662/**
1663 * skb_dequeue - remove from the head of the queue
1664 * @list: list to dequeue from
1665 *
1666 * Remove the head of the list. The list lock is taken so the function
1667 * may be used safely with other locking list functions. The head item is
1668 * returned or %NULL if the list is empty.
1669 */
1670
1671struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1672{
1673 unsigned long flags;
1674 struct sk_buff *result;
1675
1676 spin_lock_irqsave(&list->lock, flags);
1677 result = __skb_dequeue(list);
1678 spin_unlock_irqrestore(&list->lock, flags);
1679 return result;
1680}
1681
1682/**
1683 * skb_dequeue_tail - remove from the tail of the queue
1684 * @list: list to dequeue from
1685 *
1686 * Remove the tail of the list. The list lock is taken so the function
1687 * may be used safely with other locking list functions. The tail item is
1688 * returned or %NULL if the list is empty.
1689 */
1690struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1691{
1692 unsigned long flags;
1693 struct sk_buff *result;
1694
1695 spin_lock_irqsave(&list->lock, flags);
1696 result = __skb_dequeue_tail(list);
1697 spin_unlock_irqrestore(&list->lock, flags);
1698 return result;
1699}
1700
1701/**
1702 * skb_queue_purge - empty a list
1703 * @list: list to empty
1704 *
1705 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1706 * the list and one reference dropped. This function takes the list
1707 * lock and is atomic with respect to other list locking functions.
1708 */
1709void skb_queue_purge(struct sk_buff_head *list)
1710{
1711 struct sk_buff *skb;
1712 while ((skb = skb_dequeue(list)) != NULL)
1713 kfree_skb(skb);
1714}
1715
1716/**
1717 * skb_queue_head - queue a buffer at the list head
1718 * @list: list to use
1719 * @newsk: buffer to queue
1720 *
1721 * Queue a buffer at the start of the list. This function takes the
1722 * list lock and can be used safely with other locking &sk_buff functions
1723 * safely.
1724 *
1725 * A buffer cannot be placed on two lists at the same time.
1726 */
1727void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1728{
1729 unsigned long flags;
1730
1731 spin_lock_irqsave(&list->lock, flags);
1732 __skb_queue_head(list, newsk);
1733 spin_unlock_irqrestore(&list->lock, flags);
1734}
1735
1736/**
1737 * skb_queue_tail - queue a buffer at the list tail
1738 * @list: list to use
1739 * @newsk: buffer to queue
1740 *
1741 * Queue a buffer at the tail of the list. This function takes the
1742 * list lock and can be used safely with other locking &sk_buff functions
1743 * safely.
1744 *
1745 * A buffer cannot be placed on two lists at the same time.
1746 */
1747void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1748{
1749 unsigned long flags;
1750
1751 spin_lock_irqsave(&list->lock, flags);
1752 __skb_queue_tail(list, newsk);
1753 spin_unlock_irqrestore(&list->lock, flags);
1754}
David S. Miller8728b832005-08-09 19:25:21 -07001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/**
1757 * skb_unlink - remove a buffer from a list
1758 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001759 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
David S. Miller8728b832005-08-09 19:25:21 -07001761 * Remove a packet from a list. The list locks are taken and this
1762 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 *
David S. Miller8728b832005-08-09 19:25:21 -07001764 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
David S. Miller8728b832005-08-09 19:25:21 -07001766void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
David S. Miller8728b832005-08-09 19:25:21 -07001768 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
David S. Miller8728b832005-08-09 19:25:21 -07001770 spin_lock_irqsave(&list->lock, flags);
1771 __skb_unlink(skb, list);
1772 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775/**
1776 * skb_append - append a buffer
1777 * @old: buffer to insert after
1778 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001779 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *
1781 * Place a packet after a given packet in a list. The list locks are taken
1782 * and this function is atomic with respect to other list locked calls.
1783 * A buffer cannot be placed on two lists at the same time.
1784 */
David S. Miller8728b832005-08-09 19:25:21 -07001785void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 unsigned long flags;
1788
David S. Miller8728b832005-08-09 19:25:21 -07001789 spin_lock_irqsave(&list->lock, flags);
1790 __skb_append(old, newsk, list);
1791 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794
1795/**
1796 * skb_insert - insert a buffer
1797 * @old: buffer to insert before
1798 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001799 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *
David S. Miller8728b832005-08-09 19:25:21 -07001801 * Place a packet before a given packet in a list. The list locks are
1802 * taken and this function is atomic with respect to other list locked
1803 * calls.
1804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * A buffer cannot be placed on two lists at the same time.
1806 */
David S. Miller8728b832005-08-09 19:25:21 -07001807void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 unsigned long flags;
1810
David S. Miller8728b832005-08-09 19:25:21 -07001811 spin_lock_irqsave(&list->lock, flags);
1812 __skb_insert(newsk, old->prev, old, list);
1813 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816static inline void skb_split_inside_header(struct sk_buff *skb,
1817 struct sk_buff* skb1,
1818 const u32 len, const int pos)
1819{
1820 int i;
1821
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001822 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1823 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* And move data appendix as is. */
1825 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1826 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1827
1828 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1829 skb_shinfo(skb)->nr_frags = 0;
1830 skb1->data_len = skb->data_len;
1831 skb1->len += skb1->data_len;
1832 skb->data_len = 0;
1833 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001834 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static inline void skb_split_no_header(struct sk_buff *skb,
1838 struct sk_buff* skb1,
1839 const u32 len, int pos)
1840{
1841 int i, k = 0;
1842 const int nfrags = skb_shinfo(skb)->nr_frags;
1843
1844 skb_shinfo(skb)->nr_frags = 0;
1845 skb1->len = skb1->data_len = skb->len - len;
1846 skb->len = len;
1847 skb->data_len = len - pos;
1848
1849 for (i = 0; i < nfrags; i++) {
1850 int size = skb_shinfo(skb)->frags[i].size;
1851
1852 if (pos + size > len) {
1853 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1854
1855 if (pos < len) {
1856 /* Split frag.
1857 * We have two variants in this case:
1858 * 1. Move all the frag to the second
1859 * part, if it is possible. F.e.
1860 * this approach is mandatory for TUX,
1861 * where splitting is expensive.
1862 * 2. Split is accurately. We make this.
1863 */
1864 get_page(skb_shinfo(skb)->frags[i].page);
1865 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1866 skb_shinfo(skb1)->frags[0].size -= len - pos;
1867 skb_shinfo(skb)->frags[i].size = len - pos;
1868 skb_shinfo(skb)->nr_frags++;
1869 }
1870 k++;
1871 } else
1872 skb_shinfo(skb)->nr_frags++;
1873 pos += size;
1874 }
1875 skb_shinfo(skb1)->nr_frags = k;
1876}
1877
1878/**
1879 * skb_split - Split fragmented skb to two parts at length len.
1880 * @skb: the buffer to split
1881 * @skb1: the buffer to receive the second part
1882 * @len: new length for skb
1883 */
1884void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1885{
1886 int pos = skb_headlen(skb);
1887
1888 if (len < pos) /* Split line is inside header. */
1889 skb_split_inside_header(skb, skb1, len, pos);
1890 else /* Second chunk has no header, nothing to copy. */
1891 skb_split_no_header(skb, skb1, len, pos);
1892}
1893
Thomas Graf677e90e2005-06-23 20:59:51 -07001894/**
1895 * skb_prepare_seq_read - Prepare a sequential read of skb data
1896 * @skb: the buffer to read
1897 * @from: lower offset of data to be read
1898 * @to: upper offset of data to be read
1899 * @st: state variable
1900 *
1901 * Initializes the specified state variable. Must be called before
1902 * invoking skb_seq_read() for the first time.
1903 */
1904void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1905 unsigned int to, struct skb_seq_state *st)
1906{
1907 st->lower_offset = from;
1908 st->upper_offset = to;
1909 st->root_skb = st->cur_skb = skb;
1910 st->frag_idx = st->stepped_offset = 0;
1911 st->frag_data = NULL;
1912}
1913
1914/**
1915 * skb_seq_read - Sequentially read skb data
1916 * @consumed: number of bytes consumed by the caller so far
1917 * @data: destination pointer for data to be returned
1918 * @st: state variable
1919 *
1920 * Reads a block of skb data at &consumed relative to the
1921 * lower offset specified to skb_prepare_seq_read(). Assigns
1922 * the head of the data block to &data and returns the length
1923 * of the block or 0 if the end of the skb data or the upper
1924 * offset has been reached.
1925 *
1926 * The caller is not required to consume all of the data
1927 * returned, i.e. &consumed is typically set to the number
1928 * of bytes already consumed and the next call to
1929 * skb_seq_read() will return the remaining part of the block.
1930 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001931 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07001932 * this limitation is the cost for zerocopy seqeuental
1933 * reads of potentially non linear data.
1934 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001935 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07001936 * at the moment, state->root_skb could be replaced with
1937 * a stack for this purpose.
1938 */
1939unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1940 struct skb_seq_state *st)
1941{
1942 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1943 skb_frag_t *frag;
1944
1945 if (unlikely(abs_offset >= st->upper_offset))
1946 return 0;
1947
1948next_skb:
1949 block_limit = skb_headlen(st->cur_skb);
1950
1951 if (abs_offset < block_limit) {
1952 *data = st->cur_skb->data + abs_offset;
1953 return block_limit - abs_offset;
1954 }
1955
1956 if (st->frag_idx == 0 && !st->frag_data)
1957 st->stepped_offset += skb_headlen(st->cur_skb);
1958
1959 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1960 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1961 block_limit = frag->size + st->stepped_offset;
1962
1963 if (abs_offset < block_limit) {
1964 if (!st->frag_data)
1965 st->frag_data = kmap_skb_frag(frag);
1966
1967 *data = (u8 *) st->frag_data + frag->page_offset +
1968 (abs_offset - st->stepped_offset);
1969
1970 return block_limit - abs_offset;
1971 }
1972
1973 if (st->frag_data) {
1974 kunmap_skb_frag(st->frag_data);
1975 st->frag_data = NULL;
1976 }
1977
1978 st->frag_idx++;
1979 st->stepped_offset += frag->size;
1980 }
1981
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001982 if (st->frag_data) {
1983 kunmap_skb_frag(st->frag_data);
1984 st->frag_data = NULL;
1985 }
1986
Thomas Graf677e90e2005-06-23 20:59:51 -07001987 if (st->cur_skb->next) {
1988 st->cur_skb = st->cur_skb->next;
1989 st->frag_idx = 0;
1990 goto next_skb;
1991 } else if (st->root_skb == st->cur_skb &&
1992 skb_shinfo(st->root_skb)->frag_list) {
1993 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1994 goto next_skb;
1995 }
1996
1997 return 0;
1998}
1999
2000/**
2001 * skb_abort_seq_read - Abort a sequential read of skb data
2002 * @st: state variable
2003 *
2004 * Must be called if skb_seq_read() was not called until it
2005 * returned 0.
2006 */
2007void skb_abort_seq_read(struct skb_seq_state *st)
2008{
2009 if (st->frag_data)
2010 kunmap_skb_frag(st->frag_data);
2011}
2012
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002013#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2014
2015static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2016 struct ts_config *conf,
2017 struct ts_state *state)
2018{
2019 return skb_seq_read(offset, text, TS_SKB_CB(state));
2020}
2021
2022static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2023{
2024 skb_abort_seq_read(TS_SKB_CB(state));
2025}
2026
2027/**
2028 * skb_find_text - Find a text pattern in skb data
2029 * @skb: the buffer to look in
2030 * @from: search offset
2031 * @to: search limit
2032 * @config: textsearch configuration
2033 * @state: uninitialized textsearch state variable
2034 *
2035 * Finds a pattern in the skb data according to the specified
2036 * textsearch configuration. Use textsearch_next() to retrieve
2037 * subsequent occurrences of the pattern. Returns the offset
2038 * to the first occurrence or UINT_MAX if no match was found.
2039 */
2040unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2041 unsigned int to, struct ts_config *config,
2042 struct ts_state *state)
2043{
Phil Oesterf72b9482006-06-26 00:00:57 -07002044 unsigned int ret;
2045
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002046 config->get_next_block = skb_ts_get_next_block;
2047 config->finish = skb_ts_finish;
2048
2049 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2050
Phil Oesterf72b9482006-06-26 00:00:57 -07002051 ret = textsearch_find(config, state);
2052 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002053}
2054
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002055/**
2056 * skb_append_datato_frags: - append the user data to a skb
2057 * @sk: sock structure
2058 * @skb: skb structure to be appened with user data.
2059 * @getfrag: call back function to be used for getting the user data
2060 * @from: pointer to user message iov
2061 * @length: length of the iov message
2062 *
2063 * Description: This procedure append the user data in the fragment part
2064 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2065 */
2066int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002067 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002068 int len, int odd, struct sk_buff *skb),
2069 void *from, int length)
2070{
2071 int frg_cnt = 0;
2072 skb_frag_t *frag = NULL;
2073 struct page *page = NULL;
2074 int copy, left;
2075 int offset = 0;
2076 int ret;
2077
2078 do {
2079 /* Return error if we don't have space for new frag */
2080 frg_cnt = skb_shinfo(skb)->nr_frags;
2081 if (frg_cnt >= MAX_SKB_FRAGS)
2082 return -EFAULT;
2083
2084 /* allocate a new page for next frag */
2085 page = alloc_pages(sk->sk_allocation, 0);
2086
2087 /* If alloc_page fails just return failure and caller will
2088 * free previous allocated pages by doing kfree_skb()
2089 */
2090 if (page == NULL)
2091 return -ENOMEM;
2092
2093 /* initialize the next frag */
2094 sk->sk_sndmsg_page = page;
2095 sk->sk_sndmsg_off = 0;
2096 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2097 skb->truesize += PAGE_SIZE;
2098 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2099
2100 /* get the new initialized frag */
2101 frg_cnt = skb_shinfo(skb)->nr_frags;
2102 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2103
2104 /* copy the user data to page */
2105 left = PAGE_SIZE - frag->page_offset;
2106 copy = (length > left)? left : length;
2107
2108 ret = getfrag(from, (page_address(frag->page) +
2109 frag->page_offset + frag->size),
2110 offset, copy, 0, skb);
2111 if (ret < 0)
2112 return -EFAULT;
2113
2114 /* copy was successful so update the size parameters */
2115 sk->sk_sndmsg_off += copy;
2116 frag->size += copy;
2117 skb->len += copy;
2118 skb->data_len += copy;
2119 offset += copy;
2120 length -= copy;
2121
2122 } while (length > 0);
2123
2124 return 0;
2125}
2126
Herbert Xucbb042f2006-03-20 22:43:56 -08002127/**
2128 * skb_pull_rcsum - pull skb and update receive checksum
2129 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002130 * @len: length of data pulled
2131 *
2132 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002133 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002134 * receive path processing instead of skb_pull unless you know
2135 * that the checksum difference is zero (e.g., a valid IP header)
2136 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002137 */
2138unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2139{
2140 BUG_ON(len > skb->len);
2141 skb->len -= len;
2142 BUG_ON(skb->len < skb->data_len);
2143 skb_postpull_rcsum(skb, skb->data, len);
2144 return skb->data += len;
2145}
2146
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002147EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2148
Herbert Xuf4c50d92006-06-22 03:02:40 -07002149/**
2150 * skb_segment - Perform protocol segmentation on skb.
2151 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002152 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002153 *
2154 * This function performs segmentation on the given skb. It returns
2155 * the segment at the given position. It returns NULL if there are
2156 * no more segments to generate, or when an error is encountered.
2157 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002158struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002159{
2160 struct sk_buff *segs = NULL;
2161 struct sk_buff *tail = NULL;
2162 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002163 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002164 unsigned int offset = doffset;
2165 unsigned int headroom;
2166 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002167 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002168 int nfrags = skb_shinfo(skb)->nr_frags;
2169 int err = -ENOMEM;
2170 int i = 0;
2171 int pos;
2172
2173 __skb_push(skb, doffset);
2174 headroom = skb_headroom(skb);
2175 pos = skb_headlen(skb);
2176
2177 do {
2178 struct sk_buff *nskb;
2179 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002180 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002181 int k;
2182 int size;
2183
2184 len = skb->len - offset;
2185 if (len > mss)
2186 len = mss;
2187
2188 hsize = skb_headlen(skb) - offset;
2189 if (hsize < 0)
2190 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002191 if (hsize > len || !sg)
2192 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002193
Herbert Xuc8884ed2006-10-29 15:59:41 -08002194 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002195 if (unlikely(!nskb))
2196 goto err;
2197
2198 if (segs)
2199 tail->next = nskb;
2200 else
2201 segs = nskb;
2202 tail = nskb;
2203
2204 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002205 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002206 nskb->priority = skb->priority;
2207 nskb->protocol = skb->protocol;
2208 nskb->dst = dst_clone(skb->dst);
2209 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2210 nskb->pkt_type = skb->pkt_type;
2211 nskb->mac_len = skb->mac_len;
2212
2213 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002214 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002215 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002216 nskb->transport_header = (nskb->network_header +
2217 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002218 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2219 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002220 if (!sg) {
2221 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2222 skb_put(nskb, len),
2223 len, 0);
2224 continue;
2225 }
2226
2227 frag = skb_shinfo(nskb)->frags;
2228 k = 0;
2229
Patrick McHardy84fa7932006-08-29 16:44:56 -07002230 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002231 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002232 skb_copy_from_linear_data_offset(skb, offset,
2233 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002234
2235 while (pos < offset + len) {
2236 BUG_ON(i >= nfrags);
2237
2238 *frag = skb_shinfo(skb)->frags[i];
2239 get_page(frag->page);
2240 size = frag->size;
2241
2242 if (pos < offset) {
2243 frag->page_offset += offset - pos;
2244 frag->size -= offset - pos;
2245 }
2246
2247 k++;
2248
2249 if (pos + size <= offset + len) {
2250 i++;
2251 pos += size;
2252 } else {
2253 frag->size -= pos + size - (offset + len);
2254 break;
2255 }
2256
2257 frag++;
2258 }
2259
2260 skb_shinfo(nskb)->nr_frags = k;
2261 nskb->data_len = len - hsize;
2262 nskb->len += nskb->data_len;
2263 nskb->truesize += nskb->data_len;
2264 } while ((offset += len) < skb->len);
2265
2266 return segs;
2267
2268err:
2269 while ((skb = segs)) {
2270 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002271 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002272 }
2273 return ERR_PTR(err);
2274}
2275
2276EXPORT_SYMBOL_GPL(skb_segment);
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278void __init skb_init(void)
2279{
2280 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2281 sizeof(struct sk_buff),
2282 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002283 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002284 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002285 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2286 (2*sizeof(struct sk_buff)) +
2287 sizeof(atomic_t),
2288 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002289 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002290 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
David Howells716ea3a2007-04-02 20:19:53 -07002293/**
2294 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2295 * @skb: Socket buffer containing the buffers to be mapped
2296 * @sg: The scatter-gather list to map into
2297 * @offset: The offset into the buffer's contents to start mapping
2298 * @len: Length of buffer space to be mapped
2299 *
2300 * Fill the specified scatter-gather list with mappings/pointers into a
2301 * region of the buffer space attached to a socket buffer.
2302 */
David S. Miller51c739d2007-10-30 21:29:29 -07002303static int
2304__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002305{
David S. Miller1a028e52007-04-27 15:21:23 -07002306 int start = skb_headlen(skb);
2307 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002308 int elt = 0;
2309
2310 if (copy > 0) {
2311 if (copy > len)
2312 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002313 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002314 elt++;
2315 if ((len -= copy) == 0)
2316 return elt;
2317 offset += copy;
2318 }
2319
2320 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002321 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002322
David S. Miller1a028e52007-04-27 15:21:23 -07002323 BUG_TRAP(start <= offset + len);
2324
2325 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002326 if ((copy = end - offset) > 0) {
2327 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2328
2329 if (copy > len)
2330 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002331 sg_set_page(&sg[elt], frag->page, copy,
2332 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002333 elt++;
2334 if (!(len -= copy))
2335 return elt;
2336 offset += copy;
2337 }
David S. Miller1a028e52007-04-27 15:21:23 -07002338 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002339 }
2340
2341 if (skb_shinfo(skb)->frag_list) {
2342 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2343
2344 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002345 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002346
David S. Miller1a028e52007-04-27 15:21:23 -07002347 BUG_TRAP(start <= offset + len);
2348
2349 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002350 if ((copy = end - offset) > 0) {
2351 if (copy > len)
2352 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002353 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2354 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002355 if ((len -= copy) == 0)
2356 return elt;
2357 offset += copy;
2358 }
David S. Miller1a028e52007-04-27 15:21:23 -07002359 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002360 }
2361 }
2362 BUG_ON(len);
2363 return elt;
2364}
2365
David S. Miller51c739d2007-10-30 21:29:29 -07002366int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2367{
2368 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2369
Jens Axboec46f2332007-10-31 12:06:37 +01002370 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002371
2372 return nsg;
2373}
2374
David Howells716ea3a2007-04-02 20:19:53 -07002375/**
2376 * skb_cow_data - Check that a socket buffer's data buffers are writable
2377 * @skb: The socket buffer to check.
2378 * @tailbits: Amount of trailing space to be added
2379 * @trailer: Returned pointer to the skb where the @tailbits space begins
2380 *
2381 * Make sure that the data buffers attached to a socket buffer are
2382 * writable. If they are not, private copies are made of the data buffers
2383 * and the socket buffer is set to use these instead.
2384 *
2385 * If @tailbits is given, make sure that there is space to write @tailbits
2386 * bytes of data beyond current end of socket buffer. @trailer will be
2387 * set to point to the skb in which this space begins.
2388 *
2389 * The number of scatterlist elements required to completely map the
2390 * COW'd and extended socket buffer will be returned.
2391 */
2392int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2393{
2394 int copyflag;
2395 int elt;
2396 struct sk_buff *skb1, **skb_p;
2397
2398 /* If skb is cloned or its head is paged, reallocate
2399 * head pulling out all the pages (pages are considered not writable
2400 * at the moment even if they are anonymous).
2401 */
2402 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2403 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2404 return -ENOMEM;
2405
2406 /* Easy case. Most of packets will go this way. */
2407 if (!skb_shinfo(skb)->frag_list) {
2408 /* A little of trouble, not enough of space for trailer.
2409 * This should not happen, when stack is tuned to generate
2410 * good frames. OK, on miss we reallocate and reserve even more
2411 * space, 128 bytes is fair. */
2412
2413 if (skb_tailroom(skb) < tailbits &&
2414 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2415 return -ENOMEM;
2416
2417 /* Voila! */
2418 *trailer = skb;
2419 return 1;
2420 }
2421
2422 /* Misery. We are in troubles, going to mincer fragments... */
2423
2424 elt = 1;
2425 skb_p = &skb_shinfo(skb)->frag_list;
2426 copyflag = 0;
2427
2428 while ((skb1 = *skb_p) != NULL) {
2429 int ntail = 0;
2430
2431 /* The fragment is partially pulled by someone,
2432 * this can happen on input. Copy it and everything
2433 * after it. */
2434
2435 if (skb_shared(skb1))
2436 copyflag = 1;
2437
2438 /* If the skb is the last, worry about trailer. */
2439
2440 if (skb1->next == NULL && tailbits) {
2441 if (skb_shinfo(skb1)->nr_frags ||
2442 skb_shinfo(skb1)->frag_list ||
2443 skb_tailroom(skb1) < tailbits)
2444 ntail = tailbits + 128;
2445 }
2446
2447 if (copyflag ||
2448 skb_cloned(skb1) ||
2449 ntail ||
2450 skb_shinfo(skb1)->nr_frags ||
2451 skb_shinfo(skb1)->frag_list) {
2452 struct sk_buff *skb2;
2453
2454 /* Fuck, we are miserable poor guys... */
2455 if (ntail == 0)
2456 skb2 = skb_copy(skb1, GFP_ATOMIC);
2457 else
2458 skb2 = skb_copy_expand(skb1,
2459 skb_headroom(skb1),
2460 ntail,
2461 GFP_ATOMIC);
2462 if (unlikely(skb2 == NULL))
2463 return -ENOMEM;
2464
2465 if (skb1->sk)
2466 skb_set_owner_w(skb2, skb1->sk);
2467
2468 /* Looking around. Are we still alive?
2469 * OK, link new skb, drop old one */
2470
2471 skb2->next = skb1->next;
2472 *skb_p = skb2;
2473 kfree_skb(skb1);
2474 skb1 = skb2;
2475 }
2476 elt++;
2477 *trailer = skb1;
2478 skb_p = &skb1->next;
2479 }
2480
2481 return elt;
2482}
2483
Rusty Russellf35d9d82008-02-04 23:49:54 -05002484/**
2485 * skb_partial_csum_set - set up and verify partial csum values for packet
2486 * @skb: the skb to set
2487 * @start: the number of bytes after skb->data to start checksumming.
2488 * @off: the offset from start to place the checksum.
2489 *
2490 * For untrusted partially-checksummed packets, we need to make sure the values
2491 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2492 *
2493 * This function checks and sets those values and skb->ip_summed: if this
2494 * returns false you should drop the packet.
2495 */
2496bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2497{
2498 if (unlikely(start > skb->len - 2) ||
2499 unlikely((int)start + off > skb->len - 2)) {
2500 if (net_ratelimit())
2501 printk(KERN_WARNING
2502 "bad partial csum: csum=%u/%u len=%u\n",
2503 start, off, skb->len);
2504 return false;
2505 }
2506 skb->ip_summed = CHECKSUM_PARTIAL;
2507 skb->csum_start = skb_headroom(skb) + start;
2508 skb->csum_offset = off;
2509 return true;
2510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512EXPORT_SYMBOL(___pskb_trim);
2513EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002514EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002516EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002517EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518EXPORT_SYMBOL(pskb_copy);
2519EXPORT_SYMBOL(pskb_expand_head);
2520EXPORT_SYMBOL(skb_checksum);
2521EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522EXPORT_SYMBOL(skb_copy);
2523EXPORT_SYMBOL(skb_copy_and_csum_bits);
2524EXPORT_SYMBOL(skb_copy_and_csum_dev);
2525EXPORT_SYMBOL(skb_copy_bits);
2526EXPORT_SYMBOL(skb_copy_expand);
2527EXPORT_SYMBOL(skb_over_panic);
2528EXPORT_SYMBOL(skb_pad);
2529EXPORT_SYMBOL(skb_realloc_headroom);
2530EXPORT_SYMBOL(skb_under_panic);
2531EXPORT_SYMBOL(skb_dequeue);
2532EXPORT_SYMBOL(skb_dequeue_tail);
2533EXPORT_SYMBOL(skb_insert);
2534EXPORT_SYMBOL(skb_queue_purge);
2535EXPORT_SYMBOL(skb_queue_head);
2536EXPORT_SYMBOL(skb_queue_tail);
2537EXPORT_SYMBOL(skb_unlink);
2538EXPORT_SYMBOL(skb_append);
2539EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002540EXPORT_SYMBOL(skb_prepare_seq_read);
2541EXPORT_SYMBOL(skb_seq_read);
2542EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002543EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002544EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002545
2546EXPORT_SYMBOL_GPL(skb_to_sgvec);
2547EXPORT_SYMBOL_GPL(skb_cow_data);
Rusty Russellf35d9d82008-02-04 23:49:54 -05002548EXPORT_SYMBOL_GPL(skb_partial_csum_set);