blob: c54f3664bce5b0e868928df71c392dc18f89f2a0 [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>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
50#include <linux/netdevice.h>
51#ifdef CONFIG_NET_CLS_ACT
52#include <net/pkt_sched.h>
53#endif
54#include <linux/string.h>
55#include <linux/skbuff.h>
56#include <linux/cache.h>
57#include <linux/rtnetlink.h>
58#include <linux/init.h>
59#include <linux/highmem.h>
60
61#include <net/protocol.h>
62#include <net/dst.h>
63#include <net/sock.h>
64#include <net/checksum.h>
65#include <net/xfrm.h>
66
67#include <asm/uaccess.h>
68#include <asm/system.h>
69
Eric Dumazetba899662005-08-26 12:05:31 -070070static kmem_cache_t *skbuff_head_cache __read_mostly;
71static kmem_cache_t *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 BUG();
94}
95
96/**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106{
Patrick McHardy26095452005-04-21 16:43:02 -0700107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 BUG();
112}
113
David S. Millerdc6de332006-04-20 00:10:50 -0700114void skb_truesize_bug(struct sk_buff *skb)
115{
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
119}
120EXPORT_SYMBOL(skb_truesize_bug);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
124 * [BEEP] leaks.
125 *
126 */
127
128/**
David S. Millerd179cd12005-08-17 14:57:30 -0700129 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * @size: size to allocate
131 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
138 *
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
140 * %GFP_ATOMIC.
141 */
Al Virodd0fc662005-10-07 07:46:04 +0100142struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
David S. Millerd179cd12005-08-17 14:57:30 -0700143 int fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Herbert Xu8798b3f2006-01-23 16:32:45 -0800145 kmem_cache_t *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800146 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct sk_buff *skb;
148 u8 *data;
149
Herbert Xu8798b3f2006-01-23 16:32:45 -0800150 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Get the HEAD */
Herbert Xu8798b3f2006-01-23 16:32:45 -0800153 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!skb)
155 goto out;
156
157 /* Get the DATA. Size must match skb_add_mtu(). */
158 size = SKB_DATA_ALIGN(size);
Al Viro871751e2006-03-25 03:06:39 -0800159 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!data)
161 goto nodata;
162
163 memset(skb, 0, offsetof(struct sk_buff, truesize));
164 skb->truesize = size + sizeof(struct sk_buff);
165 atomic_set(&skb->users, 1);
166 skb->head = data;
167 skb->data = data;
168 skb->tail = data;
169 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800170 /* make sure we initialize shinfo sequentially */
171 shinfo = skb_shinfo(skb);
172 atomic_set(&shinfo->dataref, 1);
173 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700174 shinfo->gso_size = 0;
175 shinfo->gso_segs = 0;
176 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800177 shinfo->ip6_frag_id = 0;
178 shinfo->frag_list = NULL;
179
David S. Millerd179cd12005-08-17 14:57:30 -0700180 if (fclone) {
181 struct sk_buff *child = skb + 1;
182 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David S. Millerd179cd12005-08-17 14:57:30 -0700184 skb->fclone = SKB_FCLONE_ORIG;
185 atomic_set(fclone_ref, 1);
186
187 child->fclone = SKB_FCLONE_UNAVAILABLE;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189out:
190 return skb;
191nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800192 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 skb = NULL;
194 goto out;
195}
196
197/**
198 * alloc_skb_from_cache - allocate a network buffer
199 * @cp: kmem_cache from which to allocate the data area
200 * (object size must be big enough for @size bytes + skb overheads)
201 * @size: size to allocate
202 * @gfp_mask: allocation mask
203 *
204 * Allocate a new &sk_buff. The returned buffer has no headroom and
205 * tail room of size bytes. The object has a reference count of one.
206 * The return is the buffer. On a failure the return is %NULL.
207 *
208 * Buffers may only be allocated from interrupts using a @gfp_mask of
209 * %GFP_ATOMIC.
210 */
211struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700212 unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100213 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct sk_buff *skb;
216 u8 *data;
217
218 /* Get the HEAD */
219 skb = kmem_cache_alloc(skbuff_head_cache,
220 gfp_mask & ~__GFP_DMA);
221 if (!skb)
222 goto out;
223
224 /* Get the DATA. */
225 size = SKB_DATA_ALIGN(size);
226 data = kmem_cache_alloc(cp, gfp_mask);
227 if (!data)
228 goto nodata;
229
230 memset(skb, 0, offsetof(struct sk_buff, truesize));
231 skb->truesize = size + sizeof(struct sk_buff);
232 atomic_set(&skb->users, 1);
233 skb->head = data;
234 skb->data = data;
235 skb->tail = data;
236 skb->end = data + size;
237
238 atomic_set(&(skb_shinfo(skb)->dataref), 1);
239 skb_shinfo(skb)->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700240 skb_shinfo(skb)->gso_size = 0;
241 skb_shinfo(skb)->gso_segs = 0;
242 skb_shinfo(skb)->gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 skb_shinfo(skb)->frag_list = NULL;
244out:
245 return skb;
246nodata:
247 kmem_cache_free(skbuff_head_cache, skb);
248 skb = NULL;
249 goto out;
250}
251
Christoph Hellwig8af27452006-07-31 22:35:23 -0700252/**
253 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
254 * @dev: network device to receive on
255 * @length: length to allocate
256 * @gfp_mask: get_free_pages mask, passed to alloc_skb
257 *
258 * Allocate a new &sk_buff and assign it a usage count of one. The
259 * buffer has unspecified headroom built in. Users should allocate
260 * the headroom they think they need without accounting for the
261 * built in space. The built in space is used for optimisations.
262 *
263 * %NULL is returned if there is no free memory.
264 */
265struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
266 unsigned int length, gfp_t gfp_mask)
267{
268 struct sk_buff *skb;
269
270 skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700271 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700272 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700273 skb->dev = dev;
274 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700275 return skb;
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Herbert Xu27b437c2006-07-13 19:26:39 -0700278static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Herbert Xu27b437c2006-07-13 19:26:39 -0700280 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Herbert Xu27b437c2006-07-13 19:26:39 -0700282 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 do {
285 struct sk_buff *this = list;
286 list = list->next;
287 kfree_skb(this);
288 } while (list);
289}
290
Herbert Xu27b437c2006-07-13 19:26:39 -0700291static inline void skb_drop_fraglist(struct sk_buff *skb)
292{
293 skb_drop_list(&skb_shinfo(skb)->frag_list);
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void skb_clone_fraglist(struct sk_buff *skb)
297{
298 struct sk_buff *list;
299
300 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
301 skb_get(list);
302}
303
Adrian Bunk5bba1712006-06-29 13:02:35 -0700304static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 if (!skb->cloned ||
307 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
308 &skb_shinfo(skb)->dataref)) {
309 if (skb_shinfo(skb)->nr_frags) {
310 int i;
311 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
312 put_page(skb_shinfo(skb)->frags[i].page);
313 }
314
315 if (skb_shinfo(skb)->frag_list)
316 skb_drop_fraglist(skb);
317
318 kfree(skb->head);
319 }
320}
321
322/*
323 * Free an skbuff by memory without cleaning the state.
324 */
325void kfree_skbmem(struct sk_buff *skb)
326{
David S. Millerd179cd12005-08-17 14:57:30 -0700327 struct sk_buff *other;
328 atomic_t *fclone_ref;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700331 switch (skb->fclone) {
332 case SKB_FCLONE_UNAVAILABLE:
333 kmem_cache_free(skbuff_head_cache, skb);
334 break;
335
336 case SKB_FCLONE_ORIG:
337 fclone_ref = (atomic_t *) (skb + 2);
338 if (atomic_dec_and_test(fclone_ref))
339 kmem_cache_free(skbuff_fclone_cache, skb);
340 break;
341
342 case SKB_FCLONE_CLONE:
343 fclone_ref = (atomic_t *) (skb + 1);
344 other = skb - 1;
345
346 /* The clone portion is available for
347 * fast-cloning again.
348 */
349 skb->fclone = SKB_FCLONE_UNAVAILABLE;
350
351 if (atomic_dec_and_test(fclone_ref))
352 kmem_cache_free(skbuff_fclone_cache, other);
353 break;
354 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/**
358 * __kfree_skb - private function
359 * @skb: buffer
360 *
361 * Free an sk_buff. Release anything attached to the buffer.
362 * Clean the state. This is an internal helper function. Users should
363 * always call kfree_skb
364 */
365
366void __kfree_skb(struct sk_buff *skb)
367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 dst_release(skb->dst);
369#ifdef CONFIG_XFRM
370 secpath_put(skb->sp);
371#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700372 if (skb->destructor) {
373 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb->destructor(skb);
375 }
376#ifdef CONFIG_NETFILTER
377 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800378#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
379 nf_conntrack_put_reasm(skb->nfct_reasm);
380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#ifdef CONFIG_BRIDGE_NETFILTER
382 nf_bridge_put(skb->nf_bridge);
383#endif
384#endif
385/* XXX: IS this still necessary? - JHS */
386#ifdef CONFIG_NET_SCHED
387 skb->tc_index = 0;
388#ifdef CONFIG_NET_CLS_ACT
389 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
391#endif
392
393 kfree_skbmem(skb);
394}
395
396/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800397 * kfree_skb - free an sk_buff
398 * @skb: buffer to free
399 *
400 * Drop a reference to the buffer and free it if the usage count has
401 * hit zero.
402 */
403void kfree_skb(struct sk_buff *skb)
404{
405 if (unlikely(!skb))
406 return;
407 if (likely(atomic_read(&skb->users) == 1))
408 smp_rmb();
409 else if (likely(!atomic_dec_and_test(&skb->users)))
410 return;
411 __kfree_skb(skb);
412}
413
414/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * skb_clone - duplicate an sk_buff
416 * @skb: buffer to clone
417 * @gfp_mask: allocation priority
418 *
419 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
420 * copies share the same packet data but not structure. The new
421 * buffer has a reference count of 1. If the allocation fails the
422 * function returns %NULL otherwise the new buffer is returned.
423 *
424 * If this function is called from an interrupt gfp_mask() must be
425 * %GFP_ATOMIC.
426 */
427
Al Virodd0fc662005-10-07 07:46:04 +0100428struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
David S. Millerd179cd12005-08-17 14:57:30 -0700430 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David S. Millerd179cd12005-08-17 14:57:30 -0700432 n = skb + 1;
433 if (skb->fclone == SKB_FCLONE_ORIG &&
434 n->fclone == SKB_FCLONE_UNAVAILABLE) {
435 atomic_t *fclone_ref = (atomic_t *) (n + 1);
436 n->fclone = SKB_FCLONE_CLONE;
437 atomic_inc(fclone_ref);
438 } else {
439 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
440 if (!n)
441 return NULL;
442 n->fclone = SKB_FCLONE_UNAVAILABLE;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445#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;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700449 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 C(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 C(h);
452 C(nh);
453 C(mac);
454 C(dst);
455 dst_clone(skb->dst);
456 C(sp);
457#ifdef CONFIG_INET
458 secpath_get(skb->sp);
459#endif
460 memcpy(n->cb, skb->cb, sizeof(skb->cb));
461 C(len);
462 C(data_len);
463 C(csum);
464 C(local_df);
465 n->cloned = 1;
466 n->nohdr = 0;
467 C(pkt_type);
468 C(ip_summed);
469 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800470#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
471 C(ipvs_property);
472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 n->destructor = NULL;
475#ifdef CONFIG_NETFILTER
476 C(nfmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 C(nfct);
478 nf_conntrack_get(skb->nfct);
479 C(nfctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800480#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
481 C(nfct_reasm);
482 nf_conntrack_get_reasm(skb->nfct_reasm);
483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#ifdef CONFIG_BRIDGE_NETFILTER
485 C(nf_bridge);
486 nf_bridge_get(skb->nf_bridge);
487#endif
488#endif /*CONFIG_NETFILTER*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#ifdef CONFIG_NET_SCHED
490 C(tc_index);
491#ifdef CONFIG_NET_CLS_ACT
492 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700493 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
494 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 C(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#endif
James Morris984bc162006-06-09 00:29:17 -0700497 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#endif
499 C(truesize);
500 atomic_set(&n->users, 1);
501 C(head);
502 C(data);
503 C(tail);
504 C(end);
505
506 atomic_inc(&(skb_shinfo(skb)->dataref));
507 skb->cloned = 1;
508
509 return n;
510}
511
512static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
513{
514 /*
515 * Shift between the two data areas in bytes
516 */
517 unsigned long offset = new->data - old->data;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 new->sk = NULL;
520 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 new->priority = old->priority;
522 new->protocol = old->protocol;
523 new->dst = dst_clone(old->dst);
524#ifdef CONFIG_INET
525 new->sp = secpath_get(old->sp);
526#endif
527 new->h.raw = old->h.raw + offset;
528 new->nh.raw = old->nh.raw + offset;
529 new->mac.raw = old->mac.raw + offset;
530 memcpy(new->cb, old->cb, sizeof(old->cb));
531 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700532 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700534 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 new->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#ifdef CONFIG_NETFILTER
537 new->nfmark = old->nfmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 new->nfct = old->nfct;
539 nf_conntrack_get(old->nfct);
540 new->nfctinfo = old->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800541#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
542 new->nfct_reasm = old->nfct_reasm;
543 nf_conntrack_get_reasm(old->nfct_reasm);
544#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300545#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
546 new->ipvs_property = old->ipvs_property;
547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef CONFIG_BRIDGE_NETFILTER
549 new->nf_bridge = old->nf_bridge;
550 nf_bridge_get(old->nf_bridge);
551#endif
552#endif
553#ifdef CONFIG_NET_SCHED
554#ifdef CONFIG_NET_CLS_ACT
555 new->tc_verd = old->tc_verd;
556#endif
557 new->tc_index = old->tc_index;
558#endif
James Morris984bc162006-06-09 00:29:17 -0700559 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700561 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
562 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
563 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/**
567 * skb_copy - create private copy of an sk_buff
568 * @skb: buffer to copy
569 * @gfp_mask: allocation priority
570 *
571 * Make a copy of both an &sk_buff and its data. This is used when the
572 * caller wishes to modify the data and needs a private copy of the
573 * data to alter. Returns %NULL on failure or the pointer to the buffer
574 * on success. The returned buffer has a reference count of 1.
575 *
576 * As by-product this function converts non-linear &sk_buff to linear
577 * one, so that &sk_buff becomes completely private and caller is allowed
578 * to modify all the data of returned buffer. This means that this
579 * function is not recommended for use in circumstances when only
580 * header is going to be modified. Use pskb_copy() instead.
581 */
582
Al Virodd0fc662005-10-07 07:46:04 +0100583struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 int headerlen = skb->data - skb->head;
586 /*
587 * Allocate the copy buffer
588 */
589 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
590 gfp_mask);
591 if (!n)
592 return NULL;
593
594 /* Set the data pointer */
595 skb_reserve(n, headerlen);
596 /* Set the tail pointer and length */
597 skb_put(n, skb->len);
598 n->csum = skb->csum;
599 n->ip_summed = skb->ip_summed;
600
601 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
602 BUG();
603
604 copy_skb_header(n, skb);
605 return n;
606}
607
608
609/**
610 * pskb_copy - create copy of an sk_buff with private head.
611 * @skb: buffer to copy
612 * @gfp_mask: allocation priority
613 *
614 * Make a copy of both an &sk_buff and part of its data, located
615 * in header. Fragmented data remain shared. This is used when
616 * the caller wishes to modify only header of &sk_buff and needs
617 * private copy of the header to alter. Returns %NULL on failure
618 * or the pointer to the buffer on success.
619 * The returned buffer has a reference count of 1.
620 */
621
Al Virodd0fc662005-10-07 07:46:04 +0100622struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 /*
625 * Allocate the copy buffer
626 */
627 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
628
629 if (!n)
630 goto out;
631
632 /* Set the data pointer */
633 skb_reserve(n, skb->data - skb->head);
634 /* Set the tail pointer and length */
635 skb_put(n, skb_headlen(skb));
636 /* Copy the bytes */
637 memcpy(n->data, skb->data, n->len);
638 n->csum = skb->csum;
639 n->ip_summed = skb->ip_summed;
640
641 n->data_len = skb->data_len;
642 n->len = skb->len;
643
644 if (skb_shinfo(skb)->nr_frags) {
645 int i;
646
647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
648 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
649 get_page(skb_shinfo(n)->frags[i].page);
650 }
651 skb_shinfo(n)->nr_frags = i;
652 }
653
654 if (skb_shinfo(skb)->frag_list) {
655 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
656 skb_clone_fraglist(n);
657 }
658
659 copy_skb_header(n, skb);
660out:
661 return n;
662}
663
664/**
665 * pskb_expand_head - reallocate header of &sk_buff
666 * @skb: buffer to reallocate
667 * @nhead: room to add at head
668 * @ntail: room to add at tail
669 * @gfp_mask: allocation priority
670 *
671 * Expands (or creates identical copy, if &nhead and &ntail are zero)
672 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
673 * reference count of 1. Returns zero in the case of success or error,
674 * if expansion failed. In the last case, &sk_buff is not changed.
675 *
676 * All the pointers pointing into skb header may change and must be
677 * reloaded after call to this function.
678 */
679
Victor Fusco86a76ca2005-07-08 14:57:47 -0700680int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100681 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 int i;
684 u8 *data;
685 int size = nhead + (skb->end - skb->head) + ntail;
686 long off;
687
688 if (skb_shared(skb))
689 BUG();
690
691 size = SKB_DATA_ALIGN(size);
692
693 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
694 if (!data)
695 goto nodata;
696
697 /* Copy only real data... and, alas, header. This should be
698 * optimized for the cases when header is void. */
699 memcpy(data + nhead, skb->head, skb->tail - skb->head);
700 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
701
702 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
703 get_page(skb_shinfo(skb)->frags[i].page);
704
705 if (skb_shinfo(skb)->frag_list)
706 skb_clone_fraglist(skb);
707
708 skb_release_data(skb);
709
710 off = (data + nhead) - skb->head;
711
712 skb->head = data;
713 skb->end = data + size;
714 skb->data += off;
715 skb->tail += off;
716 skb->mac.raw += off;
717 skb->h.raw += off;
718 skb->nh.raw += off;
719 skb->cloned = 0;
720 skb->nohdr = 0;
721 atomic_set(&skb_shinfo(skb)->dataref, 1);
722 return 0;
723
724nodata:
725 return -ENOMEM;
726}
727
728/* Make private copy of skb with writable head and some headroom */
729
730struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
731{
732 struct sk_buff *skb2;
733 int delta = headroom - skb_headroom(skb);
734
735 if (delta <= 0)
736 skb2 = pskb_copy(skb, GFP_ATOMIC);
737 else {
738 skb2 = skb_clone(skb, GFP_ATOMIC);
739 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
740 GFP_ATOMIC)) {
741 kfree_skb(skb2);
742 skb2 = NULL;
743 }
744 }
745 return skb2;
746}
747
748
749/**
750 * skb_copy_expand - copy and expand sk_buff
751 * @skb: buffer to copy
752 * @newheadroom: new free bytes at head
753 * @newtailroom: new free bytes at tail
754 * @gfp_mask: allocation priority
755 *
756 * Make a copy of both an &sk_buff and its data and while doing so
757 * allocate additional space.
758 *
759 * This is used when the caller wishes to modify the data and needs a
760 * private copy of the data to alter as well as more space for new fields.
761 * Returns %NULL on failure or the pointer to the buffer
762 * on success. The returned buffer has a reference count of 1.
763 *
764 * You must pass %GFP_ATOMIC as the allocation priority if this function
765 * is called from an interrupt.
766 *
767 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
768 * only by netfilter in the cases when checksum is recalculated? --ANK
769 */
770struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700771 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100772 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 /*
775 * Allocate the copy buffer
776 */
777 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
778 gfp_mask);
779 int head_copy_len, head_copy_off;
780
781 if (!n)
782 return NULL;
783
784 skb_reserve(n, newheadroom);
785
786 /* Set the tail pointer and length */
787 skb_put(n, skb->len);
788
789 head_copy_len = skb_headroom(skb);
790 head_copy_off = 0;
791 if (newheadroom <= head_copy_len)
792 head_copy_len = newheadroom;
793 else
794 head_copy_off = newheadroom - head_copy_len;
795
796 /* Copy the linear header and data. */
797 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
798 skb->len + head_copy_len))
799 BUG();
800
801 copy_skb_header(n, skb);
802
803 return n;
804}
805
806/**
807 * skb_pad - zero pad the tail of an skb
808 * @skb: buffer to pad
809 * @pad: space to pad
810 *
811 * Ensure that a buffer is followed by a padding area that is zero
812 * filled. Used by network drivers which may DMA or transfer data
813 * beyond the buffer end onto the wire.
814 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700815 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
817
Herbert Xu5b057c62006-06-23 02:06:41 -0700818int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Herbert Xu5b057c62006-06-23 02:06:41 -0700820 int err;
821 int ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700824 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700828
829 ntail = skb->data_len + pad - (skb->end - skb->tail);
830 if (likely(skb_cloned(skb) || ntail > 0)) {
831 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
832 if (unlikely(err))
833 goto free_skb;
834 }
835
836 /* FIXME: The use of this function with non-linear skb's really needs
837 * to be audited.
838 */
839 err = skb_linearize(skb);
840 if (unlikely(err))
841 goto free_skb;
842
843 memset(skb->data + skb->len, 0, pad);
844 return 0;
845
846free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700848 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Herbert Xu3cc0e872006-06-09 16:13:38 -0700851/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
853
Herbert Xu3cc0e872006-06-09 16:13:38 -0700854int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Herbert Xu27b437c2006-07-13 19:26:39 -0700856 struct sk_buff **fragp;
857 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int offset = skb_headlen(skb);
859 int nfrags = skb_shinfo(skb)->nr_frags;
860 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700861 int err;
862
863 if (skb_cloned(skb) &&
864 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
865 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700867 i = 0;
868 if (offset >= len)
869 goto drop_pages;
870
871 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700873
874 if (end < len) {
875 offset = end;
876 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700878
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700879 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700880
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700881drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700882 skb_shinfo(skb)->nr_frags = i;
883
884 for (; i < nfrags; i++)
885 put_page(skb_shinfo(skb)->frags[i].page);
886
887 if (skb_shinfo(skb)->frag_list)
888 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700889 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Herbert Xu27b437c2006-07-13 19:26:39 -0700892 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
893 fragp = &frag->next) {
894 int end = offset + frag->len;
895
896 if (skb_shared(frag)) {
897 struct sk_buff *nfrag;
898
899 nfrag = skb_clone(frag, GFP_ATOMIC);
900 if (unlikely(!nfrag))
901 return -ENOMEM;
902
903 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700904 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700905 frag = nfrag;
906 *fragp = frag;
907 }
908
909 if (end < len) {
910 offset = end;
911 continue;
912 }
913
914 if (end > len &&
915 unlikely((err = pskb_trim(frag, len - offset))))
916 return err;
917
918 if (frag->next)
919 skb_drop_list(&frag->next);
920 break;
921 }
922
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700923done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700924 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 skb->data_len -= skb->len - len;
926 skb->len = len;
927 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700928 skb->len = len;
929 skb->data_len = 0;
930 skb->tail = skb->data + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 return 0;
934}
935
936/**
937 * __pskb_pull_tail - advance tail of skb header
938 * @skb: buffer to reallocate
939 * @delta: number of bytes to advance tail
940 *
941 * The function makes a sense only on a fragmented &sk_buff,
942 * it expands header moving its tail forward and copying necessary
943 * data from fragmented part.
944 *
945 * &sk_buff MUST have reference count of 1.
946 *
947 * Returns %NULL (and &sk_buff does not change) if pull failed
948 * or value of new tail of skb in the case of success.
949 *
950 * All the pointers pointing into skb header may change and must be
951 * reloaded after call to this function.
952 */
953
954/* Moves tail of skb head forward, copying data from fragmented part,
955 * when it is necessary.
956 * 1. It may fail due to malloc failure.
957 * 2. It may change skb pointers.
958 *
959 * It is pretty complicated. Luckily, it is called only in exceptional cases.
960 */
961unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
962{
963 /* If skb has not enough free space at tail, get new one
964 * plus 128 bytes for future expansions. If we have enough
965 * room at tail, reallocate without expansion only if skb is cloned.
966 */
967 int i, k, eat = (skb->tail + delta) - skb->end;
968
969 if (eat > 0 || skb_cloned(skb)) {
970 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
971 GFP_ATOMIC))
972 return NULL;
973 }
974
975 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
976 BUG();
977
978 /* Optimization: no fragments, no reasons to preestimate
979 * size of pulled pages. Superb.
980 */
981 if (!skb_shinfo(skb)->frag_list)
982 goto pull_pages;
983
984 /* Estimate size of pulled pages. */
985 eat = delta;
986 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
987 if (skb_shinfo(skb)->frags[i].size >= eat)
988 goto pull_pages;
989 eat -= skb_shinfo(skb)->frags[i].size;
990 }
991
992 /* If we need update frag list, we are in troubles.
993 * Certainly, it possible to add an offset to skb data,
994 * but taking into account that pulling is expected to
995 * be very rare operation, it is worth to fight against
996 * further bloating skb head and crucify ourselves here instead.
997 * Pure masohism, indeed. 8)8)
998 */
999 if (eat) {
1000 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1001 struct sk_buff *clone = NULL;
1002 struct sk_buff *insp = NULL;
1003
1004 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001005 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (list->len <= eat) {
1008 /* Eaten as whole. */
1009 eat -= list->len;
1010 list = list->next;
1011 insp = list;
1012 } else {
1013 /* Eaten partially. */
1014
1015 if (skb_shared(list)) {
1016 /* Sucks! We need to fork list. :-( */
1017 clone = skb_clone(list, GFP_ATOMIC);
1018 if (!clone)
1019 return NULL;
1020 insp = list->next;
1021 list = clone;
1022 } else {
1023 /* This may be pulled without
1024 * problems. */
1025 insp = list;
1026 }
1027 if (!pskb_pull(list, eat)) {
1028 if (clone)
1029 kfree_skb(clone);
1030 return NULL;
1031 }
1032 break;
1033 }
1034 } while (eat);
1035
1036 /* Free pulled out fragments. */
1037 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1038 skb_shinfo(skb)->frag_list = list->next;
1039 kfree_skb(list);
1040 }
1041 /* And insert new clone at head. */
1042 if (clone) {
1043 clone->next = list;
1044 skb_shinfo(skb)->frag_list = clone;
1045 }
1046 }
1047 /* Success! Now we may commit changes to skb data. */
1048
1049pull_pages:
1050 eat = delta;
1051 k = 0;
1052 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1053 if (skb_shinfo(skb)->frags[i].size <= eat) {
1054 put_page(skb_shinfo(skb)->frags[i].page);
1055 eat -= skb_shinfo(skb)->frags[i].size;
1056 } else {
1057 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1058 if (eat) {
1059 skb_shinfo(skb)->frags[k].page_offset += eat;
1060 skb_shinfo(skb)->frags[k].size -= eat;
1061 eat = 0;
1062 }
1063 k++;
1064 }
1065 }
1066 skb_shinfo(skb)->nr_frags = k;
1067
1068 skb->tail += delta;
1069 skb->data_len -= delta;
1070
1071 return skb->tail;
1072}
1073
1074/* Copy some data bits from skb to kernel buffer. */
1075
1076int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1077{
1078 int i, copy;
1079 int start = skb_headlen(skb);
1080
1081 if (offset > (int)skb->len - len)
1082 goto fault;
1083
1084 /* Copy header. */
1085 if ((copy = start - offset) > 0) {
1086 if (copy > len)
1087 copy = len;
1088 memcpy(to, skb->data + offset, copy);
1089 if ((len -= copy) == 0)
1090 return 0;
1091 offset += copy;
1092 to += copy;
1093 }
1094
1095 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1096 int end;
1097
1098 BUG_TRAP(start <= offset + len);
1099
1100 end = start + skb_shinfo(skb)->frags[i].size;
1101 if ((copy = end - offset) > 0) {
1102 u8 *vaddr;
1103
1104 if (copy > len)
1105 copy = len;
1106
1107 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1108 memcpy(to,
1109 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1110 offset - start, copy);
1111 kunmap_skb_frag(vaddr);
1112
1113 if ((len -= copy) == 0)
1114 return 0;
1115 offset += copy;
1116 to += copy;
1117 }
1118 start = end;
1119 }
1120
1121 if (skb_shinfo(skb)->frag_list) {
1122 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1123
1124 for (; list; list = list->next) {
1125 int end;
1126
1127 BUG_TRAP(start <= offset + len);
1128
1129 end = start + list->len;
1130 if ((copy = end - offset) > 0) {
1131 if (copy > len)
1132 copy = len;
1133 if (skb_copy_bits(list, offset - start,
1134 to, copy))
1135 goto fault;
1136 if ((len -= copy) == 0)
1137 return 0;
1138 offset += copy;
1139 to += copy;
1140 }
1141 start = end;
1142 }
1143 }
1144 if (!len)
1145 return 0;
1146
1147fault:
1148 return -EFAULT;
1149}
1150
Herbert Xu357b40a2005-04-19 22:30:14 -07001151/**
1152 * skb_store_bits - store bits from kernel buffer to skb
1153 * @skb: destination buffer
1154 * @offset: offset in destination
1155 * @from: source buffer
1156 * @len: number of bytes to copy
1157 *
1158 * Copy the specified number of bytes from the source buffer to the
1159 * destination skb. This function handles all the messy bits of
1160 * traversing fragment lists and such.
1161 */
1162
1163int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1164{
1165 int i, copy;
1166 int start = skb_headlen(skb);
1167
1168 if (offset > (int)skb->len - len)
1169 goto fault;
1170
1171 if ((copy = start - offset) > 0) {
1172 if (copy > len)
1173 copy = len;
1174 memcpy(skb->data + offset, from, copy);
1175 if ((len -= copy) == 0)
1176 return 0;
1177 offset += copy;
1178 from += copy;
1179 }
1180
1181 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1182 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1183 int end;
1184
1185 BUG_TRAP(start <= offset + len);
1186
1187 end = start + frag->size;
1188 if ((copy = end - offset) > 0) {
1189 u8 *vaddr;
1190
1191 if (copy > len)
1192 copy = len;
1193
1194 vaddr = kmap_skb_frag(frag);
1195 memcpy(vaddr + frag->page_offset + offset - start,
1196 from, copy);
1197 kunmap_skb_frag(vaddr);
1198
1199 if ((len -= copy) == 0)
1200 return 0;
1201 offset += copy;
1202 from += copy;
1203 }
1204 start = end;
1205 }
1206
1207 if (skb_shinfo(skb)->frag_list) {
1208 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1209
1210 for (; list; list = list->next) {
1211 int end;
1212
1213 BUG_TRAP(start <= offset + len);
1214
1215 end = start + list->len;
1216 if ((copy = end - offset) > 0) {
1217 if (copy > len)
1218 copy = len;
1219 if (skb_store_bits(list, offset - start,
1220 from, copy))
1221 goto fault;
1222 if ((len -= copy) == 0)
1223 return 0;
1224 offset += copy;
1225 from += copy;
1226 }
1227 start = end;
1228 }
1229 }
1230 if (!len)
1231 return 0;
1232
1233fault:
1234 return -EFAULT;
1235}
1236
1237EXPORT_SYMBOL(skb_store_bits);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/* Checksum skb data. */
1240
1241unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1242 int len, unsigned int csum)
1243{
1244 int start = skb_headlen(skb);
1245 int i, copy = start - offset;
1246 int pos = 0;
1247
1248 /* Checksum header. */
1249 if (copy > 0) {
1250 if (copy > len)
1251 copy = len;
1252 csum = csum_partial(skb->data + offset, copy, csum);
1253 if ((len -= copy) == 0)
1254 return csum;
1255 offset += copy;
1256 pos = copy;
1257 }
1258
1259 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1260 int end;
1261
1262 BUG_TRAP(start <= offset + len);
1263
1264 end = start + skb_shinfo(skb)->frags[i].size;
1265 if ((copy = end - offset) > 0) {
1266 unsigned int csum2;
1267 u8 *vaddr;
1268 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1269
1270 if (copy > len)
1271 copy = len;
1272 vaddr = kmap_skb_frag(frag);
1273 csum2 = csum_partial(vaddr + frag->page_offset +
1274 offset - start, copy, 0);
1275 kunmap_skb_frag(vaddr);
1276 csum = csum_block_add(csum, csum2, pos);
1277 if (!(len -= copy))
1278 return csum;
1279 offset += copy;
1280 pos += copy;
1281 }
1282 start = end;
1283 }
1284
1285 if (skb_shinfo(skb)->frag_list) {
1286 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1287
1288 for (; list; list = list->next) {
1289 int end;
1290
1291 BUG_TRAP(start <= offset + len);
1292
1293 end = start + list->len;
1294 if ((copy = end - offset) > 0) {
1295 unsigned int csum2;
1296 if (copy > len)
1297 copy = len;
1298 csum2 = skb_checksum(list, offset - start,
1299 copy, 0);
1300 csum = csum_block_add(csum, csum2, pos);
1301 if ((len -= copy) == 0)
1302 return csum;
1303 offset += copy;
1304 pos += copy;
1305 }
1306 start = end;
1307 }
1308 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001309 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return csum;
1312}
1313
1314/* Both of above in one bottle. */
1315
1316unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1317 u8 *to, int len, unsigned int csum)
1318{
1319 int start = skb_headlen(skb);
1320 int i, copy = start - offset;
1321 int pos = 0;
1322
1323 /* Copy header. */
1324 if (copy > 0) {
1325 if (copy > len)
1326 copy = len;
1327 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1328 copy, csum);
1329 if ((len -= copy) == 0)
1330 return csum;
1331 offset += copy;
1332 to += copy;
1333 pos = copy;
1334 }
1335
1336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1337 int end;
1338
1339 BUG_TRAP(start <= offset + len);
1340
1341 end = start + skb_shinfo(skb)->frags[i].size;
1342 if ((copy = end - offset) > 0) {
1343 unsigned int csum2;
1344 u8 *vaddr;
1345 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1346
1347 if (copy > len)
1348 copy = len;
1349 vaddr = kmap_skb_frag(frag);
1350 csum2 = csum_partial_copy_nocheck(vaddr +
1351 frag->page_offset +
1352 offset - start, to,
1353 copy, 0);
1354 kunmap_skb_frag(vaddr);
1355 csum = csum_block_add(csum, csum2, pos);
1356 if (!(len -= copy))
1357 return csum;
1358 offset += copy;
1359 to += copy;
1360 pos += copy;
1361 }
1362 start = end;
1363 }
1364
1365 if (skb_shinfo(skb)->frag_list) {
1366 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1367
1368 for (; list; list = list->next) {
1369 unsigned int csum2;
1370 int end;
1371
1372 BUG_TRAP(start <= offset + len);
1373
1374 end = start + list->len;
1375 if ((copy = end - offset) > 0) {
1376 if (copy > len)
1377 copy = len;
1378 csum2 = skb_copy_and_csum_bits(list,
1379 offset - start,
1380 to, copy, 0);
1381 csum = csum_block_add(csum, csum2, pos);
1382 if ((len -= copy) == 0)
1383 return csum;
1384 offset += copy;
1385 to += copy;
1386 pos += copy;
1387 }
1388 start = end;
1389 }
1390 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001391 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return csum;
1393}
1394
1395void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1396{
1397 unsigned int csum;
1398 long csstart;
1399
1400 if (skb->ip_summed == CHECKSUM_HW)
1401 csstart = skb->h.raw - skb->data;
1402 else
1403 csstart = skb_headlen(skb);
1404
Kris Katterjohn09a62662006-01-08 22:24:28 -08001405 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 memcpy(to, skb->data, csstart);
1408
1409 csum = 0;
1410 if (csstart != skb->len)
1411 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1412 skb->len - csstart, 0);
1413
1414 if (skb->ip_summed == CHECKSUM_HW) {
1415 long csstuff = csstart + skb->csum;
1416
1417 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1418 }
1419}
1420
1421/**
1422 * skb_dequeue - remove from the head of the queue
1423 * @list: list to dequeue from
1424 *
1425 * Remove the head of the list. The list lock is taken so the function
1426 * may be used safely with other locking list functions. The head item is
1427 * returned or %NULL if the list is empty.
1428 */
1429
1430struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1431{
1432 unsigned long flags;
1433 struct sk_buff *result;
1434
1435 spin_lock_irqsave(&list->lock, flags);
1436 result = __skb_dequeue(list);
1437 spin_unlock_irqrestore(&list->lock, flags);
1438 return result;
1439}
1440
1441/**
1442 * skb_dequeue_tail - remove from the tail of the queue
1443 * @list: list to dequeue from
1444 *
1445 * Remove the tail of the list. The list lock is taken so the function
1446 * may be used safely with other locking list functions. The tail item is
1447 * returned or %NULL if the list is empty.
1448 */
1449struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1450{
1451 unsigned long flags;
1452 struct sk_buff *result;
1453
1454 spin_lock_irqsave(&list->lock, flags);
1455 result = __skb_dequeue_tail(list);
1456 spin_unlock_irqrestore(&list->lock, flags);
1457 return result;
1458}
1459
1460/**
1461 * skb_queue_purge - empty a list
1462 * @list: list to empty
1463 *
1464 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1465 * the list and one reference dropped. This function takes the list
1466 * lock and is atomic with respect to other list locking functions.
1467 */
1468void skb_queue_purge(struct sk_buff_head *list)
1469{
1470 struct sk_buff *skb;
1471 while ((skb = skb_dequeue(list)) != NULL)
1472 kfree_skb(skb);
1473}
1474
1475/**
1476 * skb_queue_head - queue a buffer at the list head
1477 * @list: list to use
1478 * @newsk: buffer to queue
1479 *
1480 * Queue a buffer at the start of the list. This function takes the
1481 * list lock and can be used safely with other locking &sk_buff functions
1482 * safely.
1483 *
1484 * A buffer cannot be placed on two lists at the same time.
1485 */
1486void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1487{
1488 unsigned long flags;
1489
1490 spin_lock_irqsave(&list->lock, flags);
1491 __skb_queue_head(list, newsk);
1492 spin_unlock_irqrestore(&list->lock, flags);
1493}
1494
1495/**
1496 * skb_queue_tail - queue a buffer at the list tail
1497 * @list: list to use
1498 * @newsk: buffer to queue
1499 *
1500 * Queue a buffer at the tail of the list. This function takes the
1501 * list lock and can be used safely with other locking &sk_buff functions
1502 * safely.
1503 *
1504 * A buffer cannot be placed on two lists at the same time.
1505 */
1506void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1507{
1508 unsigned long flags;
1509
1510 spin_lock_irqsave(&list->lock, flags);
1511 __skb_queue_tail(list, newsk);
1512 spin_unlock_irqrestore(&list->lock, flags);
1513}
David S. Miller8728b832005-08-09 19:25:21 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/**
1516 * skb_unlink - remove a buffer from a list
1517 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001518 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
David S. Miller8728b832005-08-09 19:25:21 -07001520 * Remove a packet from a list. The list locks are taken and this
1521 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 *
David S. Miller8728b832005-08-09 19:25:21 -07001523 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
David S. Miller8728b832005-08-09 19:25:21 -07001525void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
David S. Miller8728b832005-08-09 19:25:21 -07001527 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
David S. Miller8728b832005-08-09 19:25:21 -07001529 spin_lock_irqsave(&list->lock, flags);
1530 __skb_unlink(skb, list);
1531 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/**
1535 * skb_append - append a buffer
1536 * @old: buffer to insert after
1537 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001538 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
1540 * Place a packet after a given packet in a list. The list locks are taken
1541 * and this function is atomic with respect to other list locked calls.
1542 * A buffer cannot be placed on two lists at the same time.
1543 */
David S. Miller8728b832005-08-09 19:25:21 -07001544void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 unsigned long flags;
1547
David S. Miller8728b832005-08-09 19:25:21 -07001548 spin_lock_irqsave(&list->lock, flags);
1549 __skb_append(old, newsk, list);
1550 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553
1554/**
1555 * skb_insert - insert a buffer
1556 * @old: buffer to insert before
1557 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001558 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 *
David S. Miller8728b832005-08-09 19:25:21 -07001560 * Place a packet before a given packet in a list. The list locks are
1561 * taken and this function is atomic with respect to other list locked
1562 * calls.
1563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * A buffer cannot be placed on two lists at the same time.
1565 */
David S. Miller8728b832005-08-09 19:25:21 -07001566void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 unsigned long flags;
1569
David S. Miller8728b832005-08-09 19:25:21 -07001570 spin_lock_irqsave(&list->lock, flags);
1571 __skb_insert(newsk, old->prev, old, list);
1572 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575#if 0
1576/*
1577 * Tune the memory allocator for a new MTU size.
1578 */
1579void skb_add_mtu(int mtu)
1580{
1581 /* Must match allocation in alloc_skb */
1582 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1583
1584 kmem_add_cache_size(mtu);
1585}
1586#endif
1587
1588static inline void skb_split_inside_header(struct sk_buff *skb,
1589 struct sk_buff* skb1,
1590 const u32 len, const int pos)
1591{
1592 int i;
1593
1594 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1595
1596 /* And move data appendix as is. */
1597 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1598 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1599
1600 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1601 skb_shinfo(skb)->nr_frags = 0;
1602 skb1->data_len = skb->data_len;
1603 skb1->len += skb1->data_len;
1604 skb->data_len = 0;
1605 skb->len = len;
1606 skb->tail = skb->data + len;
1607}
1608
1609static inline void skb_split_no_header(struct sk_buff *skb,
1610 struct sk_buff* skb1,
1611 const u32 len, int pos)
1612{
1613 int i, k = 0;
1614 const int nfrags = skb_shinfo(skb)->nr_frags;
1615
1616 skb_shinfo(skb)->nr_frags = 0;
1617 skb1->len = skb1->data_len = skb->len - len;
1618 skb->len = len;
1619 skb->data_len = len - pos;
1620
1621 for (i = 0; i < nfrags; i++) {
1622 int size = skb_shinfo(skb)->frags[i].size;
1623
1624 if (pos + size > len) {
1625 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1626
1627 if (pos < len) {
1628 /* Split frag.
1629 * We have two variants in this case:
1630 * 1. Move all the frag to the second
1631 * part, if it is possible. F.e.
1632 * this approach is mandatory for TUX,
1633 * where splitting is expensive.
1634 * 2. Split is accurately. We make this.
1635 */
1636 get_page(skb_shinfo(skb)->frags[i].page);
1637 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1638 skb_shinfo(skb1)->frags[0].size -= len - pos;
1639 skb_shinfo(skb)->frags[i].size = len - pos;
1640 skb_shinfo(skb)->nr_frags++;
1641 }
1642 k++;
1643 } else
1644 skb_shinfo(skb)->nr_frags++;
1645 pos += size;
1646 }
1647 skb_shinfo(skb1)->nr_frags = k;
1648}
1649
1650/**
1651 * skb_split - Split fragmented skb to two parts at length len.
1652 * @skb: the buffer to split
1653 * @skb1: the buffer to receive the second part
1654 * @len: new length for skb
1655 */
1656void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1657{
1658 int pos = skb_headlen(skb);
1659
1660 if (len < pos) /* Split line is inside header. */
1661 skb_split_inside_header(skb, skb1, len, pos);
1662 else /* Second chunk has no header, nothing to copy. */
1663 skb_split_no_header(skb, skb1, len, pos);
1664}
1665
Thomas Graf677e90e2005-06-23 20:59:51 -07001666/**
1667 * skb_prepare_seq_read - Prepare a sequential read of skb data
1668 * @skb: the buffer to read
1669 * @from: lower offset of data to be read
1670 * @to: upper offset of data to be read
1671 * @st: state variable
1672 *
1673 * Initializes the specified state variable. Must be called before
1674 * invoking skb_seq_read() for the first time.
1675 */
1676void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1677 unsigned int to, struct skb_seq_state *st)
1678{
1679 st->lower_offset = from;
1680 st->upper_offset = to;
1681 st->root_skb = st->cur_skb = skb;
1682 st->frag_idx = st->stepped_offset = 0;
1683 st->frag_data = NULL;
1684}
1685
1686/**
1687 * skb_seq_read - Sequentially read skb data
1688 * @consumed: number of bytes consumed by the caller so far
1689 * @data: destination pointer for data to be returned
1690 * @st: state variable
1691 *
1692 * Reads a block of skb data at &consumed relative to the
1693 * lower offset specified to skb_prepare_seq_read(). Assigns
1694 * the head of the data block to &data and returns the length
1695 * of the block or 0 if the end of the skb data or the upper
1696 * offset has been reached.
1697 *
1698 * The caller is not required to consume all of the data
1699 * returned, i.e. &consumed is typically set to the number
1700 * of bytes already consumed and the next call to
1701 * skb_seq_read() will return the remaining part of the block.
1702 *
1703 * Note: The size of each block of data returned can be arbitary,
1704 * this limitation is the cost for zerocopy seqeuental
1705 * reads of potentially non linear data.
1706 *
1707 * Note: Fragment lists within fragments are not implemented
1708 * at the moment, state->root_skb could be replaced with
1709 * a stack for this purpose.
1710 */
1711unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1712 struct skb_seq_state *st)
1713{
1714 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1715 skb_frag_t *frag;
1716
1717 if (unlikely(abs_offset >= st->upper_offset))
1718 return 0;
1719
1720next_skb:
1721 block_limit = skb_headlen(st->cur_skb);
1722
1723 if (abs_offset < block_limit) {
1724 *data = st->cur_skb->data + abs_offset;
1725 return block_limit - abs_offset;
1726 }
1727
1728 if (st->frag_idx == 0 && !st->frag_data)
1729 st->stepped_offset += skb_headlen(st->cur_skb);
1730
1731 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1732 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1733 block_limit = frag->size + st->stepped_offset;
1734
1735 if (abs_offset < block_limit) {
1736 if (!st->frag_data)
1737 st->frag_data = kmap_skb_frag(frag);
1738
1739 *data = (u8 *) st->frag_data + frag->page_offset +
1740 (abs_offset - st->stepped_offset);
1741
1742 return block_limit - abs_offset;
1743 }
1744
1745 if (st->frag_data) {
1746 kunmap_skb_frag(st->frag_data);
1747 st->frag_data = NULL;
1748 }
1749
1750 st->frag_idx++;
1751 st->stepped_offset += frag->size;
1752 }
1753
1754 if (st->cur_skb->next) {
1755 st->cur_skb = st->cur_skb->next;
1756 st->frag_idx = 0;
1757 goto next_skb;
1758 } else if (st->root_skb == st->cur_skb &&
1759 skb_shinfo(st->root_skb)->frag_list) {
1760 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1761 goto next_skb;
1762 }
1763
1764 return 0;
1765}
1766
1767/**
1768 * skb_abort_seq_read - Abort a sequential read of skb data
1769 * @st: state variable
1770 *
1771 * Must be called if skb_seq_read() was not called until it
1772 * returned 0.
1773 */
1774void skb_abort_seq_read(struct skb_seq_state *st)
1775{
1776 if (st->frag_data)
1777 kunmap_skb_frag(st->frag_data);
1778}
1779
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001780#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1781
1782static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1783 struct ts_config *conf,
1784 struct ts_state *state)
1785{
1786 return skb_seq_read(offset, text, TS_SKB_CB(state));
1787}
1788
1789static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1790{
1791 skb_abort_seq_read(TS_SKB_CB(state));
1792}
1793
1794/**
1795 * skb_find_text - Find a text pattern in skb data
1796 * @skb: the buffer to look in
1797 * @from: search offset
1798 * @to: search limit
1799 * @config: textsearch configuration
1800 * @state: uninitialized textsearch state variable
1801 *
1802 * Finds a pattern in the skb data according to the specified
1803 * textsearch configuration. Use textsearch_next() to retrieve
1804 * subsequent occurrences of the pattern. Returns the offset
1805 * to the first occurrence or UINT_MAX if no match was found.
1806 */
1807unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1808 unsigned int to, struct ts_config *config,
1809 struct ts_state *state)
1810{
Phil Oesterf72b9482006-06-26 00:00:57 -07001811 unsigned int ret;
1812
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001813 config->get_next_block = skb_ts_get_next_block;
1814 config->finish = skb_ts_finish;
1815
1816 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1817
Phil Oesterf72b9482006-06-26 00:00:57 -07001818 ret = textsearch_find(config, state);
1819 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001820}
1821
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001822/**
1823 * skb_append_datato_frags: - append the user data to a skb
1824 * @sk: sock structure
1825 * @skb: skb structure to be appened with user data.
1826 * @getfrag: call back function to be used for getting the user data
1827 * @from: pointer to user message iov
1828 * @length: length of the iov message
1829 *
1830 * Description: This procedure append the user data in the fragment part
1831 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1832 */
1833int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001834 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001835 int len, int odd, struct sk_buff *skb),
1836 void *from, int length)
1837{
1838 int frg_cnt = 0;
1839 skb_frag_t *frag = NULL;
1840 struct page *page = NULL;
1841 int copy, left;
1842 int offset = 0;
1843 int ret;
1844
1845 do {
1846 /* Return error if we don't have space for new frag */
1847 frg_cnt = skb_shinfo(skb)->nr_frags;
1848 if (frg_cnt >= MAX_SKB_FRAGS)
1849 return -EFAULT;
1850
1851 /* allocate a new page for next frag */
1852 page = alloc_pages(sk->sk_allocation, 0);
1853
1854 /* If alloc_page fails just return failure and caller will
1855 * free previous allocated pages by doing kfree_skb()
1856 */
1857 if (page == NULL)
1858 return -ENOMEM;
1859
1860 /* initialize the next frag */
1861 sk->sk_sndmsg_page = page;
1862 sk->sk_sndmsg_off = 0;
1863 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1864 skb->truesize += PAGE_SIZE;
1865 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1866
1867 /* get the new initialized frag */
1868 frg_cnt = skb_shinfo(skb)->nr_frags;
1869 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1870
1871 /* copy the user data to page */
1872 left = PAGE_SIZE - frag->page_offset;
1873 copy = (length > left)? left : length;
1874
1875 ret = getfrag(from, (page_address(frag->page) +
1876 frag->page_offset + frag->size),
1877 offset, copy, 0, skb);
1878 if (ret < 0)
1879 return -EFAULT;
1880
1881 /* copy was successful so update the size parameters */
1882 sk->sk_sndmsg_off += copy;
1883 frag->size += copy;
1884 skb->len += copy;
1885 skb->data_len += copy;
1886 offset += copy;
1887 length -= copy;
1888
1889 } while (length > 0);
1890
1891 return 0;
1892}
1893
Herbert Xucbb042f2006-03-20 22:43:56 -08001894/**
1895 * skb_pull_rcsum - pull skb and update receive checksum
1896 * @skb: buffer to update
1897 * @start: start of data before pull
1898 * @len: length of data pulled
1899 *
1900 * This function performs an skb_pull on the packet and updates
1901 * update the CHECKSUM_HW checksum. It should be used on receive
1902 * path processing instead of skb_pull unless you know that the
1903 * checksum difference is zero (e.g., a valid IP header) or you
1904 * are setting ip_summed to CHECKSUM_NONE.
1905 */
1906unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1907{
1908 BUG_ON(len > skb->len);
1909 skb->len -= len;
1910 BUG_ON(skb->len < skb->data_len);
1911 skb_postpull_rcsum(skb, skb->data, len);
1912 return skb->data += len;
1913}
1914
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001915EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1916
Herbert Xuf4c50d92006-06-22 03:02:40 -07001917/**
1918 * skb_segment - Perform protocol segmentation on skb.
1919 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001920 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001921 *
1922 * This function performs segmentation on the given skb. It returns
1923 * the segment at the given position. It returns NULL if there are
1924 * no more segments to generate, or when an error is encountered.
1925 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001926struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001927{
1928 struct sk_buff *segs = NULL;
1929 struct sk_buff *tail = NULL;
1930 unsigned int mss = skb_shinfo(skb)->gso_size;
1931 unsigned int doffset = skb->data - skb->mac.raw;
1932 unsigned int offset = doffset;
1933 unsigned int headroom;
1934 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001935 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001936 int nfrags = skb_shinfo(skb)->nr_frags;
1937 int err = -ENOMEM;
1938 int i = 0;
1939 int pos;
1940
1941 __skb_push(skb, doffset);
1942 headroom = skb_headroom(skb);
1943 pos = skb_headlen(skb);
1944
1945 do {
1946 struct sk_buff *nskb;
1947 skb_frag_t *frag;
1948 int hsize, nsize;
1949 int k;
1950 int size;
1951
1952 len = skb->len - offset;
1953 if (len > mss)
1954 len = mss;
1955
1956 hsize = skb_headlen(skb) - offset;
1957 if (hsize < 0)
1958 hsize = 0;
1959 nsize = hsize + doffset;
1960 if (nsize > len + doffset || !sg)
1961 nsize = len + doffset;
1962
1963 nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
1964 if (unlikely(!nskb))
1965 goto err;
1966
1967 if (segs)
1968 tail->next = nskb;
1969 else
1970 segs = nskb;
1971 tail = nskb;
1972
1973 nskb->dev = skb->dev;
1974 nskb->priority = skb->priority;
1975 nskb->protocol = skb->protocol;
1976 nskb->dst = dst_clone(skb->dst);
1977 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1978 nskb->pkt_type = skb->pkt_type;
1979 nskb->mac_len = skb->mac_len;
1980
1981 skb_reserve(nskb, headroom);
1982 nskb->mac.raw = nskb->data;
1983 nskb->nh.raw = nskb->data + skb->mac_len;
1984 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1985 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1986
1987 if (!sg) {
1988 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1989 skb_put(nskb, len),
1990 len, 0);
1991 continue;
1992 }
1993
1994 frag = skb_shinfo(nskb)->frags;
1995 k = 0;
1996
1997 nskb->ip_summed = CHECKSUM_HW;
1998 nskb->csum = skb->csum;
1999 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2000
2001 while (pos < offset + len) {
2002 BUG_ON(i >= nfrags);
2003
2004 *frag = skb_shinfo(skb)->frags[i];
2005 get_page(frag->page);
2006 size = frag->size;
2007
2008 if (pos < offset) {
2009 frag->page_offset += offset - pos;
2010 frag->size -= offset - pos;
2011 }
2012
2013 k++;
2014
2015 if (pos + size <= offset + len) {
2016 i++;
2017 pos += size;
2018 } else {
2019 frag->size -= pos + size - (offset + len);
2020 break;
2021 }
2022
2023 frag++;
2024 }
2025
2026 skb_shinfo(nskb)->nr_frags = k;
2027 nskb->data_len = len - hsize;
2028 nskb->len += nskb->data_len;
2029 nskb->truesize += nskb->data_len;
2030 } while ((offset += len) < skb->len);
2031
2032 return segs;
2033
2034err:
2035 while ((skb = segs)) {
2036 segs = skb->next;
2037 kfree(skb);
2038 }
2039 return ERR_PTR(err);
2040}
2041
2042EXPORT_SYMBOL_GPL(skb_segment);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044void __init skb_init(void)
2045{
2046 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2047 sizeof(struct sk_buff),
2048 0,
2049 SLAB_HWCACHE_ALIGN,
2050 NULL, NULL);
2051 if (!skbuff_head_cache)
2052 panic("cannot create skbuff cache");
David S. Millerd179cd12005-08-17 14:57:30 -07002053
2054 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2055 (2*sizeof(struct sk_buff)) +
2056 sizeof(atomic_t),
2057 0,
2058 SLAB_HWCACHE_ALIGN,
2059 NULL, NULL);
2060 if (!skbuff_fclone_cache)
2061 panic("cannot create skbuff cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
2064EXPORT_SYMBOL(___pskb_trim);
2065EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002066EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002068EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002069EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070EXPORT_SYMBOL(pskb_copy);
2071EXPORT_SYMBOL(pskb_expand_head);
2072EXPORT_SYMBOL(skb_checksum);
2073EXPORT_SYMBOL(skb_clone);
2074EXPORT_SYMBOL(skb_clone_fraglist);
2075EXPORT_SYMBOL(skb_copy);
2076EXPORT_SYMBOL(skb_copy_and_csum_bits);
2077EXPORT_SYMBOL(skb_copy_and_csum_dev);
2078EXPORT_SYMBOL(skb_copy_bits);
2079EXPORT_SYMBOL(skb_copy_expand);
2080EXPORT_SYMBOL(skb_over_panic);
2081EXPORT_SYMBOL(skb_pad);
2082EXPORT_SYMBOL(skb_realloc_headroom);
2083EXPORT_SYMBOL(skb_under_panic);
2084EXPORT_SYMBOL(skb_dequeue);
2085EXPORT_SYMBOL(skb_dequeue_tail);
2086EXPORT_SYMBOL(skb_insert);
2087EXPORT_SYMBOL(skb_queue_purge);
2088EXPORT_SYMBOL(skb_queue_head);
2089EXPORT_SYMBOL(skb_queue_tail);
2090EXPORT_SYMBOL(skb_unlink);
2091EXPORT_SYMBOL(skb_append);
2092EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002093EXPORT_SYMBOL(skb_prepare_seq_read);
2094EXPORT_SYMBOL(skb_seq_read);
2095EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002096EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002097EXPORT_SYMBOL(skb_append_datato_frags);