blob: bb7210f4005e1a781908238d0bfbbb30a42838a1 [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
41#include <linux/config.h>
42#include <linux/module.h>
43#include <linux/types.h>
44#include <linux/kernel.h>
45#include <linux/sched.h>
46#include <linux/mm.h>
47#include <linux/interrupt.h>
48#include <linux/in.h>
49#include <linux/inet.h>
50#include <linux/slab.h>
51#include <linux/netdevice.h>
52#ifdef CONFIG_NET_CLS_ACT
53#include <net/pkt_sched.h>
54#endif
55#include <linux/string.h>
56#include <linux/skbuff.h>
57#include <linux/cache.h>
58#include <linux/rtnetlink.h>
59#include <linux/init.h>
60#include <linux/highmem.h>
61
62#include <net/protocol.h>
63#include <net/dst.h>
64#include <net/sock.h>
65#include <net/checksum.h>
66#include <net/xfrm.h>
67
68#include <asm/uaccess.h>
69#include <asm/system.h>
70
Eric Dumazetba899662005-08-26 12:05:31 -070071static kmem_cache_t *skbuff_head_cache __read_mostly;
72static kmem_cache_t *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
79
80/**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89{
Patrick McHardy26095452005-04-21 16:43:02 -070090 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
Patrick McHardy26095452005-04-21 16:43:02 -0700108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 BUG();
113}
114
David S. Millerdc6de332006-04-20 00:10:50 -0700115void skb_truesize_bug(struct sk_buff *skb)
116{
117 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb->truesize, skb->len, sizeof(struct sk_buff));
120}
121EXPORT_SYMBOL(skb_truesize_bug);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
125 * [BEEP] leaks.
126 *
127 */
128
129/**
David S. Millerd179cd12005-08-17 14:57:30 -0700130 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * @size: size to allocate
132 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
Al Virodd0fc662005-10-07 07:46:04 +0100143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
David S. Millerd179cd12005-08-17 14:57:30 -0700144 int fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Herbert Xu8798b3f2006-01-23 16:32:45 -0800146 kmem_cache_t *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800147 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct sk_buff *skb;
149 u8 *data;
150
Herbert Xu8798b3f2006-01-23 16:32:45 -0800151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Get the HEAD */
Herbert Xu8798b3f2006-01-23 16:32:45 -0800154 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
Al Viro871751e2006-03-25 03:06:39 -0800160 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (!data)
162 goto nodata;
163
164 memset(skb, 0, offsetof(struct sk_buff, truesize));
165 skb->truesize = size + sizeof(struct sk_buff);
166 atomic_set(&skb->users, 1);
167 skb->head = data;
168 skb->data = data;
169 skb->tail = data;
170 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800171 /* make sure we initialize shinfo sequentially */
172 shinfo = skb_shinfo(skb);
173 atomic_set(&shinfo->dataref, 1);
174 shinfo->nr_frags = 0;
175 shinfo->tso_size = 0;
176 shinfo->tso_segs = 0;
177 shinfo->ufo_size = 0;
178 shinfo->ip6_frag_id = 0;
179 shinfo->frag_list = NULL;
180
David S. Millerd179cd12005-08-17 14:57:30 -0700181 if (fclone) {
182 struct sk_buff *child = skb + 1;
183 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David S. Millerd179cd12005-08-17 14:57:30 -0700185 skb->fclone = SKB_FCLONE_ORIG;
186 atomic_set(fclone_ref, 1);
187
188 child->fclone = SKB_FCLONE_UNAVAILABLE;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190out:
191 return skb;
192nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800193 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 skb = NULL;
195 goto out;
196}
197
198/**
199 * alloc_skb_from_cache - allocate a network buffer
200 * @cp: kmem_cache from which to allocate the data area
201 * (object size must be big enough for @size bytes + skb overheads)
202 * @size: size to allocate
203 * @gfp_mask: allocation mask
204 *
205 * Allocate a new &sk_buff. The returned buffer has no headroom and
206 * tail room of size bytes. The object has a reference count of one.
207 * The return is the buffer. On a failure the return is %NULL.
208 *
209 * Buffers may only be allocated from interrupts using a @gfp_mask of
210 * %GFP_ATOMIC.
211 */
212struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700213 unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100214 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct sk_buff *skb;
217 u8 *data;
218
219 /* Get the HEAD */
220 skb = kmem_cache_alloc(skbuff_head_cache,
221 gfp_mask & ~__GFP_DMA);
222 if (!skb)
223 goto out;
224
225 /* Get the DATA. */
226 size = SKB_DATA_ALIGN(size);
227 data = kmem_cache_alloc(cp, gfp_mask);
228 if (!data)
229 goto nodata;
230
231 memset(skb, 0, offsetof(struct sk_buff, truesize));
232 skb->truesize = size + sizeof(struct sk_buff);
233 atomic_set(&skb->users, 1);
234 skb->head = data;
235 skb->data = data;
236 skb->tail = data;
237 skb->end = data + size;
238
239 atomic_set(&(skb_shinfo(skb)->dataref), 1);
240 skb_shinfo(skb)->nr_frags = 0;
241 skb_shinfo(skb)->tso_size = 0;
242 skb_shinfo(skb)->tso_segs = 0;
243 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
252
253static void skb_drop_fraglist(struct sk_buff *skb)
254{
255 struct sk_buff *list = skb_shinfo(skb)->frag_list;
256
257 skb_shinfo(skb)->frag_list = NULL;
258
259 do {
260 struct sk_buff *this = list;
261 list = list->next;
262 kfree_skb(this);
263 } while (list);
264}
265
266static void skb_clone_fraglist(struct sk_buff *skb)
267{
268 struct sk_buff *list;
269
270 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
271 skb_get(list);
272}
273
274void skb_release_data(struct sk_buff *skb)
275{
276 if (!skb->cloned ||
277 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
278 &skb_shinfo(skb)->dataref)) {
279 if (skb_shinfo(skb)->nr_frags) {
280 int i;
281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
282 put_page(skb_shinfo(skb)->frags[i].page);
283 }
284
285 if (skb_shinfo(skb)->frag_list)
286 skb_drop_fraglist(skb);
287
288 kfree(skb->head);
289 }
290}
291
292/*
293 * Free an skbuff by memory without cleaning the state.
294 */
295void kfree_skbmem(struct sk_buff *skb)
296{
David S. Millerd179cd12005-08-17 14:57:30 -0700297 struct sk_buff *other;
298 atomic_t *fclone_ref;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700301 switch (skb->fclone) {
302 case SKB_FCLONE_UNAVAILABLE:
303 kmem_cache_free(skbuff_head_cache, skb);
304 break;
305
306 case SKB_FCLONE_ORIG:
307 fclone_ref = (atomic_t *) (skb + 2);
308 if (atomic_dec_and_test(fclone_ref))
309 kmem_cache_free(skbuff_fclone_cache, skb);
310 break;
311
312 case SKB_FCLONE_CLONE:
313 fclone_ref = (atomic_t *) (skb + 1);
314 other = skb - 1;
315
316 /* The clone portion is available for
317 * fast-cloning again.
318 */
319 skb->fclone = SKB_FCLONE_UNAVAILABLE;
320
321 if (atomic_dec_and_test(fclone_ref))
322 kmem_cache_free(skbuff_fclone_cache, other);
323 break;
324 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327/**
328 * __kfree_skb - private function
329 * @skb: buffer
330 *
331 * Free an sk_buff. Release anything attached to the buffer.
332 * Clean the state. This is an internal helper function. Users should
333 * always call kfree_skb
334 */
335
336void __kfree_skb(struct sk_buff *skb)
337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dst_release(skb->dst);
339#ifdef CONFIG_XFRM
340 secpath_put(skb->sp);
341#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700342 if (skb->destructor) {
343 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 skb->destructor(skb);
345 }
346#ifdef CONFIG_NETFILTER
347 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800348#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
349 nf_conntrack_put_reasm(skb->nfct_reasm);
350#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#ifdef CONFIG_BRIDGE_NETFILTER
352 nf_bridge_put(skb->nf_bridge);
353#endif
354#endif
355/* XXX: IS this still necessary? - JHS */
356#ifdef CONFIG_NET_SCHED
357 skb->tc_index = 0;
358#ifdef CONFIG_NET_CLS_ACT
359 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
361#endif
362
363 kfree_skbmem(skb);
364}
365
366/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800367 * kfree_skb - free an sk_buff
368 * @skb: buffer to free
369 *
370 * Drop a reference to the buffer and free it if the usage count has
371 * hit zero.
372 */
373void kfree_skb(struct sk_buff *skb)
374{
375 if (unlikely(!skb))
376 return;
377 if (likely(atomic_read(&skb->users) == 1))
378 smp_rmb();
379 else if (likely(!atomic_dec_and_test(&skb->users)))
380 return;
381 __kfree_skb(skb);
382}
383
384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * skb_clone - duplicate an sk_buff
386 * @skb: buffer to clone
387 * @gfp_mask: allocation priority
388 *
389 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
390 * copies share the same packet data but not structure. The new
391 * buffer has a reference count of 1. If the allocation fails the
392 * function returns %NULL otherwise the new buffer is returned.
393 *
394 * If this function is called from an interrupt gfp_mask() must be
395 * %GFP_ATOMIC.
396 */
397
Al Virodd0fc662005-10-07 07:46:04 +0100398struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
David S. Millerd179cd12005-08-17 14:57:30 -0700400 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David S. Millerd179cd12005-08-17 14:57:30 -0700402 n = skb + 1;
403 if (skb->fclone == SKB_FCLONE_ORIG &&
404 n->fclone == SKB_FCLONE_UNAVAILABLE) {
405 atomic_t *fclone_ref = (atomic_t *) (n + 1);
406 n->fclone = SKB_FCLONE_CLONE;
407 atomic_inc(fclone_ref);
408 } else {
409 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
410 if (!n)
411 return NULL;
412 n->fclone = SKB_FCLONE_UNAVAILABLE;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415#define C(x) n->x = skb->x
416
417 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700419 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 C(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 C(h);
422 C(nh);
423 C(mac);
424 C(dst);
425 dst_clone(skb->dst);
426 C(sp);
427#ifdef CONFIG_INET
428 secpath_get(skb->sp);
429#endif
430 memcpy(n->cb, skb->cb, sizeof(skb->cb));
431 C(len);
432 C(data_len);
433 C(csum);
434 C(local_df);
435 n->cloned = 1;
436 n->nohdr = 0;
437 C(pkt_type);
438 C(ip_summed);
439 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800440#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
441 C(ipvs_property);
442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 n->destructor = NULL;
445#ifdef CONFIG_NETFILTER
446 C(nfmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 C(nfct);
448 nf_conntrack_get(skb->nfct);
449 C(nfctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800450#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
451 C(nfct_reasm);
452 nf_conntrack_get_reasm(skb->nfct_reasm);
453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#ifdef CONFIG_BRIDGE_NETFILTER
455 C(nf_bridge);
456 nf_bridge_get(skb->nf_bridge);
457#endif
458#endif /*CONFIG_NETFILTER*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#ifdef CONFIG_NET_SCHED
460 C(tc_index);
461#ifdef CONFIG_NET_CLS_ACT
462 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700463 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
464 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 C(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466#endif
James Morris984bc162006-06-09 00:29:17 -0700467 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#endif
469 C(truesize);
470 atomic_set(&n->users, 1);
471 C(head);
472 C(data);
473 C(tail);
474 C(end);
475
476 atomic_inc(&(skb_shinfo(skb)->dataref));
477 skb->cloned = 1;
478
479 return n;
480}
481
482static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
483{
484 /*
485 * Shift between the two data areas in bytes
486 */
487 unsigned long offset = new->data - old->data;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 new->sk = NULL;
490 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 new->priority = old->priority;
492 new->protocol = old->protocol;
493 new->dst = dst_clone(old->dst);
494#ifdef CONFIG_INET
495 new->sp = secpath_get(old->sp);
496#endif
497 new->h.raw = old->h.raw + offset;
498 new->nh.raw = old->nh.raw + offset;
499 new->mac.raw = old->mac.raw + offset;
500 memcpy(new->cb, old->cb, sizeof(old->cb));
501 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700502 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700504 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 new->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#ifdef CONFIG_NETFILTER
507 new->nfmark = old->nfmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 new->nfct = old->nfct;
509 nf_conntrack_get(old->nfct);
510 new->nfctinfo = old->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800511#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
512 new->nfct_reasm = old->nfct_reasm;
513 nf_conntrack_get_reasm(old->nfct_reasm);
514#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300515#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
516 new->ipvs_property = old->ipvs_property;
517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#ifdef CONFIG_BRIDGE_NETFILTER
519 new->nf_bridge = old->nf_bridge;
520 nf_bridge_get(old->nf_bridge);
521#endif
522#endif
523#ifdef CONFIG_NET_SCHED
524#ifdef CONFIG_NET_CLS_ACT
525 new->tc_verd = old->tc_verd;
526#endif
527 new->tc_index = old->tc_index;
528#endif
James Morris984bc162006-06-09 00:29:17 -0700529 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 atomic_set(&new->users, 1);
531 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
532 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
533}
534
535/**
536 * skb_copy - create private copy of an sk_buff
537 * @skb: buffer to copy
538 * @gfp_mask: allocation priority
539 *
540 * Make a copy of both an &sk_buff and its data. This is used when the
541 * caller wishes to modify the data and needs a private copy of the
542 * data to alter. Returns %NULL on failure or the pointer to the buffer
543 * on success. The returned buffer has a reference count of 1.
544 *
545 * As by-product this function converts non-linear &sk_buff to linear
546 * one, so that &sk_buff becomes completely private and caller is allowed
547 * to modify all the data of returned buffer. This means that this
548 * function is not recommended for use in circumstances when only
549 * header is going to be modified. Use pskb_copy() instead.
550 */
551
Al Virodd0fc662005-10-07 07:46:04 +0100552struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 int headerlen = skb->data - skb->head;
555 /*
556 * Allocate the copy buffer
557 */
558 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
559 gfp_mask);
560 if (!n)
561 return NULL;
562
563 /* Set the data pointer */
564 skb_reserve(n, headerlen);
565 /* Set the tail pointer and length */
566 skb_put(n, skb->len);
567 n->csum = skb->csum;
568 n->ip_summed = skb->ip_summed;
569
570 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
571 BUG();
572
573 copy_skb_header(n, skb);
574 return n;
575}
576
577
578/**
579 * pskb_copy - create copy of an sk_buff with private head.
580 * @skb: buffer to copy
581 * @gfp_mask: allocation priority
582 *
583 * Make a copy of both an &sk_buff and part of its data, located
584 * in header. Fragmented data remain shared. This is used when
585 * the caller wishes to modify only header of &sk_buff and needs
586 * private copy of the header to alter. Returns %NULL on failure
587 * or the pointer to the buffer on success.
588 * The returned buffer has a reference count of 1.
589 */
590
Al Virodd0fc662005-10-07 07:46:04 +0100591struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 /*
594 * Allocate the copy buffer
595 */
596 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
597
598 if (!n)
599 goto out;
600
601 /* Set the data pointer */
602 skb_reserve(n, skb->data - skb->head);
603 /* Set the tail pointer and length */
604 skb_put(n, skb_headlen(skb));
605 /* Copy the bytes */
606 memcpy(n->data, skb->data, n->len);
607 n->csum = skb->csum;
608 n->ip_summed = skb->ip_summed;
609
610 n->data_len = skb->data_len;
611 n->len = skb->len;
612
613 if (skb_shinfo(skb)->nr_frags) {
614 int i;
615
616 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
617 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
618 get_page(skb_shinfo(n)->frags[i].page);
619 }
620 skb_shinfo(n)->nr_frags = i;
621 }
622
623 if (skb_shinfo(skb)->frag_list) {
624 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
625 skb_clone_fraglist(n);
626 }
627
628 copy_skb_header(n, skb);
629out:
630 return n;
631}
632
633/**
634 * pskb_expand_head - reallocate header of &sk_buff
635 * @skb: buffer to reallocate
636 * @nhead: room to add at head
637 * @ntail: room to add at tail
638 * @gfp_mask: allocation priority
639 *
640 * Expands (or creates identical copy, if &nhead and &ntail are zero)
641 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
642 * reference count of 1. Returns zero in the case of success or error,
643 * if expansion failed. In the last case, &sk_buff is not changed.
644 *
645 * All the pointers pointing into skb header may change and must be
646 * reloaded after call to this function.
647 */
648
Victor Fusco86a76ca2005-07-08 14:57:47 -0700649int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100650 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 int i;
653 u8 *data;
654 int size = nhead + (skb->end - skb->head) + ntail;
655 long off;
656
657 if (skb_shared(skb))
658 BUG();
659
660 size = SKB_DATA_ALIGN(size);
661
662 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
663 if (!data)
664 goto nodata;
665
666 /* Copy only real data... and, alas, header. This should be
667 * optimized for the cases when header is void. */
668 memcpy(data + nhead, skb->head, skb->tail - skb->head);
669 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
670
671 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
672 get_page(skb_shinfo(skb)->frags[i].page);
673
674 if (skb_shinfo(skb)->frag_list)
675 skb_clone_fraglist(skb);
676
677 skb_release_data(skb);
678
679 off = (data + nhead) - skb->head;
680
681 skb->head = data;
682 skb->end = data + size;
683 skb->data += off;
684 skb->tail += off;
685 skb->mac.raw += off;
686 skb->h.raw += off;
687 skb->nh.raw += off;
688 skb->cloned = 0;
689 skb->nohdr = 0;
690 atomic_set(&skb_shinfo(skb)->dataref, 1);
691 return 0;
692
693nodata:
694 return -ENOMEM;
695}
696
697/* Make private copy of skb with writable head and some headroom */
698
699struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
700{
701 struct sk_buff *skb2;
702 int delta = headroom - skb_headroom(skb);
703
704 if (delta <= 0)
705 skb2 = pskb_copy(skb, GFP_ATOMIC);
706 else {
707 skb2 = skb_clone(skb, GFP_ATOMIC);
708 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
709 GFP_ATOMIC)) {
710 kfree_skb(skb2);
711 skb2 = NULL;
712 }
713 }
714 return skb2;
715}
716
717
718/**
719 * skb_copy_expand - copy and expand sk_buff
720 * @skb: buffer to copy
721 * @newheadroom: new free bytes at head
722 * @newtailroom: new free bytes at tail
723 * @gfp_mask: allocation priority
724 *
725 * Make a copy of both an &sk_buff and its data and while doing so
726 * allocate additional space.
727 *
728 * This is used when the caller wishes to modify the data and needs a
729 * private copy of the data to alter as well as more space for new fields.
730 * Returns %NULL on failure or the pointer to the buffer
731 * on success. The returned buffer has a reference count of 1.
732 *
733 * You must pass %GFP_ATOMIC as the allocation priority if this function
734 * is called from an interrupt.
735 *
736 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
737 * only by netfilter in the cases when checksum is recalculated? --ANK
738 */
739struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700740 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100741 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 /*
744 * Allocate the copy buffer
745 */
746 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
747 gfp_mask);
748 int head_copy_len, head_copy_off;
749
750 if (!n)
751 return NULL;
752
753 skb_reserve(n, newheadroom);
754
755 /* Set the tail pointer and length */
756 skb_put(n, skb->len);
757
758 head_copy_len = skb_headroom(skb);
759 head_copy_off = 0;
760 if (newheadroom <= head_copy_len)
761 head_copy_len = newheadroom;
762 else
763 head_copy_off = newheadroom - head_copy_len;
764
765 /* Copy the linear header and data. */
766 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
767 skb->len + head_copy_len))
768 BUG();
769
770 copy_skb_header(n, skb);
771
772 return n;
773}
774
775/**
776 * skb_pad - zero pad the tail of an skb
777 * @skb: buffer to pad
778 * @pad: space to pad
779 *
780 * Ensure that a buffer is followed by a padding area that is zero
781 * filled. Used by network drivers which may DMA or transfer data
782 * beyond the buffer end onto the wire.
783 *
784 * May return NULL in out of memory cases.
785 */
786
787struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
788{
789 struct sk_buff *nskb;
790
791 /* If the skbuff is non linear tailroom is always zero.. */
792 if (skb_tailroom(skb) >= pad) {
793 memset(skb->data+skb->len, 0, pad);
794 return skb;
795 }
796
797 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
798 kfree_skb(skb);
799 if (nskb)
800 memset(nskb->data+nskb->len, 0, pad);
801 return nskb;
802}
803
Herbert Xu3cc0e872006-06-09 16:13:38 -0700804/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
806
Herbert Xu3cc0e872006-06-09 16:13:38 -0700807int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 int offset = skb_headlen(skb);
810 int nfrags = skb_shinfo(skb)->nr_frags;
811 int i;
812
813 for (i = 0; i < nfrags; i++) {
814 int end = offset + skb_shinfo(skb)->frags[i].size;
815 if (end > len) {
816 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
818 return -ENOMEM;
819 }
820 if (len <= offset) {
821 put_page(skb_shinfo(skb)->frags[i].page);
822 skb_shinfo(skb)->nr_frags--;
823 } else {
824 skb_shinfo(skb)->frags[i].size = len - offset;
825 }
826 }
827 offset = end;
828 }
829
830 if (offset < len) {
831 skb->data_len -= skb->len - len;
832 skb->len = len;
833 } else {
834 if (len <= skb_headlen(skb)) {
835 skb->len = len;
836 skb->data_len = 0;
837 skb->tail = skb->data + len;
838 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
839 skb_drop_fraglist(skb);
840 } else {
841 skb->data_len -= skb->len - len;
842 skb->len = len;
843 }
844 }
845
846 return 0;
847}
848
849/**
850 * __pskb_pull_tail - advance tail of skb header
851 * @skb: buffer to reallocate
852 * @delta: number of bytes to advance tail
853 *
854 * The function makes a sense only on a fragmented &sk_buff,
855 * it expands header moving its tail forward and copying necessary
856 * data from fragmented part.
857 *
858 * &sk_buff MUST have reference count of 1.
859 *
860 * Returns %NULL (and &sk_buff does not change) if pull failed
861 * or value of new tail of skb in the case of success.
862 *
863 * All the pointers pointing into skb header may change and must be
864 * reloaded after call to this function.
865 */
866
867/* Moves tail of skb head forward, copying data from fragmented part,
868 * when it is necessary.
869 * 1. It may fail due to malloc failure.
870 * 2. It may change skb pointers.
871 *
872 * It is pretty complicated. Luckily, it is called only in exceptional cases.
873 */
874unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
875{
876 /* If skb has not enough free space at tail, get new one
877 * plus 128 bytes for future expansions. If we have enough
878 * room at tail, reallocate without expansion only if skb is cloned.
879 */
880 int i, k, eat = (skb->tail + delta) - skb->end;
881
882 if (eat > 0 || skb_cloned(skb)) {
883 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
884 GFP_ATOMIC))
885 return NULL;
886 }
887
888 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
889 BUG();
890
891 /* Optimization: no fragments, no reasons to preestimate
892 * size of pulled pages. Superb.
893 */
894 if (!skb_shinfo(skb)->frag_list)
895 goto pull_pages;
896
897 /* Estimate size of pulled pages. */
898 eat = delta;
899 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
900 if (skb_shinfo(skb)->frags[i].size >= eat)
901 goto pull_pages;
902 eat -= skb_shinfo(skb)->frags[i].size;
903 }
904
905 /* If we need update frag list, we are in troubles.
906 * Certainly, it possible to add an offset to skb data,
907 * but taking into account that pulling is expected to
908 * be very rare operation, it is worth to fight against
909 * further bloating skb head and crucify ourselves here instead.
910 * Pure masohism, indeed. 8)8)
911 */
912 if (eat) {
913 struct sk_buff *list = skb_shinfo(skb)->frag_list;
914 struct sk_buff *clone = NULL;
915 struct sk_buff *insp = NULL;
916
917 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800918 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (list->len <= eat) {
921 /* Eaten as whole. */
922 eat -= list->len;
923 list = list->next;
924 insp = list;
925 } else {
926 /* Eaten partially. */
927
928 if (skb_shared(list)) {
929 /* Sucks! We need to fork list. :-( */
930 clone = skb_clone(list, GFP_ATOMIC);
931 if (!clone)
932 return NULL;
933 insp = list->next;
934 list = clone;
935 } else {
936 /* This may be pulled without
937 * problems. */
938 insp = list;
939 }
940 if (!pskb_pull(list, eat)) {
941 if (clone)
942 kfree_skb(clone);
943 return NULL;
944 }
945 break;
946 }
947 } while (eat);
948
949 /* Free pulled out fragments. */
950 while ((list = skb_shinfo(skb)->frag_list) != insp) {
951 skb_shinfo(skb)->frag_list = list->next;
952 kfree_skb(list);
953 }
954 /* And insert new clone at head. */
955 if (clone) {
956 clone->next = list;
957 skb_shinfo(skb)->frag_list = clone;
958 }
959 }
960 /* Success! Now we may commit changes to skb data. */
961
962pull_pages:
963 eat = delta;
964 k = 0;
965 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
966 if (skb_shinfo(skb)->frags[i].size <= eat) {
967 put_page(skb_shinfo(skb)->frags[i].page);
968 eat -= skb_shinfo(skb)->frags[i].size;
969 } else {
970 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
971 if (eat) {
972 skb_shinfo(skb)->frags[k].page_offset += eat;
973 skb_shinfo(skb)->frags[k].size -= eat;
974 eat = 0;
975 }
976 k++;
977 }
978 }
979 skb_shinfo(skb)->nr_frags = k;
980
981 skb->tail += delta;
982 skb->data_len -= delta;
983
984 return skb->tail;
985}
986
987/* Copy some data bits from skb to kernel buffer. */
988
989int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
990{
991 int i, copy;
992 int start = skb_headlen(skb);
993
994 if (offset > (int)skb->len - len)
995 goto fault;
996
997 /* Copy header. */
998 if ((copy = start - offset) > 0) {
999 if (copy > len)
1000 copy = len;
1001 memcpy(to, skb->data + offset, copy);
1002 if ((len -= copy) == 0)
1003 return 0;
1004 offset += copy;
1005 to += copy;
1006 }
1007
1008 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1009 int end;
1010
1011 BUG_TRAP(start <= offset + len);
1012
1013 end = start + skb_shinfo(skb)->frags[i].size;
1014 if ((copy = end - offset) > 0) {
1015 u8 *vaddr;
1016
1017 if (copy > len)
1018 copy = len;
1019
1020 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1021 memcpy(to,
1022 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1023 offset - start, copy);
1024 kunmap_skb_frag(vaddr);
1025
1026 if ((len -= copy) == 0)
1027 return 0;
1028 offset += copy;
1029 to += copy;
1030 }
1031 start = end;
1032 }
1033
1034 if (skb_shinfo(skb)->frag_list) {
1035 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1036
1037 for (; list; list = list->next) {
1038 int end;
1039
1040 BUG_TRAP(start <= offset + len);
1041
1042 end = start + list->len;
1043 if ((copy = end - offset) > 0) {
1044 if (copy > len)
1045 copy = len;
1046 if (skb_copy_bits(list, offset - start,
1047 to, copy))
1048 goto fault;
1049 if ((len -= copy) == 0)
1050 return 0;
1051 offset += copy;
1052 to += copy;
1053 }
1054 start = end;
1055 }
1056 }
1057 if (!len)
1058 return 0;
1059
1060fault:
1061 return -EFAULT;
1062}
1063
Herbert Xu357b40a2005-04-19 22:30:14 -07001064/**
1065 * skb_store_bits - store bits from kernel buffer to skb
1066 * @skb: destination buffer
1067 * @offset: offset in destination
1068 * @from: source buffer
1069 * @len: number of bytes to copy
1070 *
1071 * Copy the specified number of bytes from the source buffer to the
1072 * destination skb. This function handles all the messy bits of
1073 * traversing fragment lists and such.
1074 */
1075
1076int skb_store_bits(const struct sk_buff *skb, int offset, void *from, 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 if ((copy = start - offset) > 0) {
1085 if (copy > len)
1086 copy = len;
1087 memcpy(skb->data + offset, from, copy);
1088 if ((len -= copy) == 0)
1089 return 0;
1090 offset += copy;
1091 from += copy;
1092 }
1093
1094 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1095 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1096 int end;
1097
1098 BUG_TRAP(start <= offset + len);
1099
1100 end = start + frag->size;
1101 if ((copy = end - offset) > 0) {
1102 u8 *vaddr;
1103
1104 if (copy > len)
1105 copy = len;
1106
1107 vaddr = kmap_skb_frag(frag);
1108 memcpy(vaddr + frag->page_offset + offset - start,
1109 from, copy);
1110 kunmap_skb_frag(vaddr);
1111
1112 if ((len -= copy) == 0)
1113 return 0;
1114 offset += copy;
1115 from += copy;
1116 }
1117 start = end;
1118 }
1119
1120 if (skb_shinfo(skb)->frag_list) {
1121 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1122
1123 for (; list; list = list->next) {
1124 int end;
1125
1126 BUG_TRAP(start <= offset + len);
1127
1128 end = start + list->len;
1129 if ((copy = end - offset) > 0) {
1130 if (copy > len)
1131 copy = len;
1132 if (skb_store_bits(list, offset - start,
1133 from, copy))
1134 goto fault;
1135 if ((len -= copy) == 0)
1136 return 0;
1137 offset += copy;
1138 from += copy;
1139 }
1140 start = end;
1141 }
1142 }
1143 if (!len)
1144 return 0;
1145
1146fault:
1147 return -EFAULT;
1148}
1149
1150EXPORT_SYMBOL(skb_store_bits);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/* Checksum skb data. */
1153
1154unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1155 int len, unsigned int csum)
1156{
1157 int start = skb_headlen(skb);
1158 int i, copy = start - offset;
1159 int pos = 0;
1160
1161 /* Checksum header. */
1162 if (copy > 0) {
1163 if (copy > len)
1164 copy = len;
1165 csum = csum_partial(skb->data + offset, copy, csum);
1166 if ((len -= copy) == 0)
1167 return csum;
1168 offset += copy;
1169 pos = copy;
1170 }
1171
1172 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1173 int end;
1174
1175 BUG_TRAP(start <= offset + len);
1176
1177 end = start + skb_shinfo(skb)->frags[i].size;
1178 if ((copy = end - offset) > 0) {
1179 unsigned int csum2;
1180 u8 *vaddr;
1181 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1182
1183 if (copy > len)
1184 copy = len;
1185 vaddr = kmap_skb_frag(frag);
1186 csum2 = csum_partial(vaddr + frag->page_offset +
1187 offset - start, copy, 0);
1188 kunmap_skb_frag(vaddr);
1189 csum = csum_block_add(csum, csum2, pos);
1190 if (!(len -= copy))
1191 return csum;
1192 offset += copy;
1193 pos += copy;
1194 }
1195 start = end;
1196 }
1197
1198 if (skb_shinfo(skb)->frag_list) {
1199 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1200
1201 for (; list; list = list->next) {
1202 int end;
1203
1204 BUG_TRAP(start <= offset + len);
1205
1206 end = start + list->len;
1207 if ((copy = end - offset) > 0) {
1208 unsigned int csum2;
1209 if (copy > len)
1210 copy = len;
1211 csum2 = skb_checksum(list, offset - start,
1212 copy, 0);
1213 csum = csum_block_add(csum, csum2, pos);
1214 if ((len -= copy) == 0)
1215 return csum;
1216 offset += copy;
1217 pos += copy;
1218 }
1219 start = end;
1220 }
1221 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001222 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 return csum;
1225}
1226
1227/* Both of above in one bottle. */
1228
1229unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1230 u8 *to, int len, unsigned int csum)
1231{
1232 int start = skb_headlen(skb);
1233 int i, copy = start - offset;
1234 int pos = 0;
1235
1236 /* Copy header. */
1237 if (copy > 0) {
1238 if (copy > len)
1239 copy = len;
1240 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1241 copy, csum);
1242 if ((len -= copy) == 0)
1243 return csum;
1244 offset += copy;
1245 to += copy;
1246 pos = copy;
1247 }
1248
1249 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1250 int end;
1251
1252 BUG_TRAP(start <= offset + len);
1253
1254 end = start + skb_shinfo(skb)->frags[i].size;
1255 if ((copy = end - offset) > 0) {
1256 unsigned int csum2;
1257 u8 *vaddr;
1258 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1259
1260 if (copy > len)
1261 copy = len;
1262 vaddr = kmap_skb_frag(frag);
1263 csum2 = csum_partial_copy_nocheck(vaddr +
1264 frag->page_offset +
1265 offset - start, to,
1266 copy, 0);
1267 kunmap_skb_frag(vaddr);
1268 csum = csum_block_add(csum, csum2, pos);
1269 if (!(len -= copy))
1270 return csum;
1271 offset += copy;
1272 to += copy;
1273 pos += copy;
1274 }
1275 start = end;
1276 }
1277
1278 if (skb_shinfo(skb)->frag_list) {
1279 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1280
1281 for (; list; list = list->next) {
1282 unsigned int csum2;
1283 int end;
1284
1285 BUG_TRAP(start <= offset + len);
1286
1287 end = start + list->len;
1288 if ((copy = end - offset) > 0) {
1289 if (copy > len)
1290 copy = len;
1291 csum2 = skb_copy_and_csum_bits(list,
1292 offset - start,
1293 to, copy, 0);
1294 csum = csum_block_add(csum, csum2, pos);
1295 if ((len -= copy) == 0)
1296 return csum;
1297 offset += copy;
1298 to += copy;
1299 pos += copy;
1300 }
1301 start = end;
1302 }
1303 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001304 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return csum;
1306}
1307
1308void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1309{
1310 unsigned int csum;
1311 long csstart;
1312
1313 if (skb->ip_summed == CHECKSUM_HW)
1314 csstart = skb->h.raw - skb->data;
1315 else
1316 csstart = skb_headlen(skb);
1317
Kris Katterjohn09a62662006-01-08 22:24:28 -08001318 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 memcpy(to, skb->data, csstart);
1321
1322 csum = 0;
1323 if (csstart != skb->len)
1324 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1325 skb->len - csstart, 0);
1326
1327 if (skb->ip_summed == CHECKSUM_HW) {
1328 long csstuff = csstart + skb->csum;
1329
1330 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1331 }
1332}
1333
1334/**
1335 * skb_dequeue - remove from the head of the queue
1336 * @list: list to dequeue from
1337 *
1338 * Remove the head of the list. The list lock is taken so the function
1339 * may be used safely with other locking list functions. The head item is
1340 * returned or %NULL if the list is empty.
1341 */
1342
1343struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1344{
1345 unsigned long flags;
1346 struct sk_buff *result;
1347
1348 spin_lock_irqsave(&list->lock, flags);
1349 result = __skb_dequeue(list);
1350 spin_unlock_irqrestore(&list->lock, flags);
1351 return result;
1352}
1353
1354/**
1355 * skb_dequeue_tail - remove from the tail of the queue
1356 * @list: list to dequeue from
1357 *
1358 * Remove the tail of the list. The list lock is taken so the function
1359 * may be used safely with other locking list functions. The tail item is
1360 * returned or %NULL if the list is empty.
1361 */
1362struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1363{
1364 unsigned long flags;
1365 struct sk_buff *result;
1366
1367 spin_lock_irqsave(&list->lock, flags);
1368 result = __skb_dequeue_tail(list);
1369 spin_unlock_irqrestore(&list->lock, flags);
1370 return result;
1371}
1372
1373/**
1374 * skb_queue_purge - empty a list
1375 * @list: list to empty
1376 *
1377 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1378 * the list and one reference dropped. This function takes the list
1379 * lock and is atomic with respect to other list locking functions.
1380 */
1381void skb_queue_purge(struct sk_buff_head *list)
1382{
1383 struct sk_buff *skb;
1384 while ((skb = skb_dequeue(list)) != NULL)
1385 kfree_skb(skb);
1386}
1387
1388/**
1389 * skb_queue_head - queue a buffer at the list head
1390 * @list: list to use
1391 * @newsk: buffer to queue
1392 *
1393 * Queue a buffer at the start of the list. This function takes the
1394 * list lock and can be used safely with other locking &sk_buff functions
1395 * safely.
1396 *
1397 * A buffer cannot be placed on two lists at the same time.
1398 */
1399void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1400{
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&list->lock, flags);
1404 __skb_queue_head(list, newsk);
1405 spin_unlock_irqrestore(&list->lock, flags);
1406}
1407
1408/**
1409 * skb_queue_tail - queue a buffer at the list tail
1410 * @list: list to use
1411 * @newsk: buffer to queue
1412 *
1413 * Queue a buffer at the tail of the list. This function takes the
1414 * list lock and can be used safely with other locking &sk_buff functions
1415 * safely.
1416 *
1417 * A buffer cannot be placed on two lists at the same time.
1418 */
1419void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1420{
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&list->lock, flags);
1424 __skb_queue_tail(list, newsk);
1425 spin_unlock_irqrestore(&list->lock, flags);
1426}
David S. Miller8728b832005-08-09 19:25:21 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/**
1429 * skb_unlink - remove a buffer from a list
1430 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001431 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *
David S. Miller8728b832005-08-09 19:25:21 -07001433 * Remove a packet from a list. The list locks are taken and this
1434 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 *
David S. Miller8728b832005-08-09 19:25:21 -07001436 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
David S. Miller8728b832005-08-09 19:25:21 -07001438void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
David S. Miller8728b832005-08-09 19:25:21 -07001440 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
David S. Miller8728b832005-08-09 19:25:21 -07001442 spin_lock_irqsave(&list->lock, flags);
1443 __skb_unlink(skb, list);
1444 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447/**
1448 * skb_append - append a buffer
1449 * @old: buffer to insert after
1450 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001451 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *
1453 * Place a packet after a given packet in a list. The list locks are taken
1454 * and this function is atomic with respect to other list locked calls.
1455 * A buffer cannot be placed on two lists at the same time.
1456 */
David S. Miller8728b832005-08-09 19:25:21 -07001457void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 unsigned long flags;
1460
David S. Miller8728b832005-08-09 19:25:21 -07001461 spin_lock_irqsave(&list->lock, flags);
1462 __skb_append(old, newsk, list);
1463 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466
1467/**
1468 * skb_insert - insert a buffer
1469 * @old: buffer to insert before
1470 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001471 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 *
David S. Miller8728b832005-08-09 19:25:21 -07001473 * Place a packet before a given packet in a list. The list locks are
1474 * taken and this function is atomic with respect to other list locked
1475 * calls.
1476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * A buffer cannot be placed on two lists at the same time.
1478 */
David S. Miller8728b832005-08-09 19:25:21 -07001479void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 unsigned long flags;
1482
David S. Miller8728b832005-08-09 19:25:21 -07001483 spin_lock_irqsave(&list->lock, flags);
1484 __skb_insert(newsk, old->prev, old, list);
1485 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488#if 0
1489/*
1490 * Tune the memory allocator for a new MTU size.
1491 */
1492void skb_add_mtu(int mtu)
1493{
1494 /* Must match allocation in alloc_skb */
1495 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1496
1497 kmem_add_cache_size(mtu);
1498}
1499#endif
1500
1501static inline void skb_split_inside_header(struct sk_buff *skb,
1502 struct sk_buff* skb1,
1503 const u32 len, const int pos)
1504{
1505 int i;
1506
1507 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1508
1509 /* And move data appendix as is. */
1510 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1511 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1512
1513 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1514 skb_shinfo(skb)->nr_frags = 0;
1515 skb1->data_len = skb->data_len;
1516 skb1->len += skb1->data_len;
1517 skb->data_len = 0;
1518 skb->len = len;
1519 skb->tail = skb->data + len;
1520}
1521
1522static inline void skb_split_no_header(struct sk_buff *skb,
1523 struct sk_buff* skb1,
1524 const u32 len, int pos)
1525{
1526 int i, k = 0;
1527 const int nfrags = skb_shinfo(skb)->nr_frags;
1528
1529 skb_shinfo(skb)->nr_frags = 0;
1530 skb1->len = skb1->data_len = skb->len - len;
1531 skb->len = len;
1532 skb->data_len = len - pos;
1533
1534 for (i = 0; i < nfrags; i++) {
1535 int size = skb_shinfo(skb)->frags[i].size;
1536
1537 if (pos + size > len) {
1538 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1539
1540 if (pos < len) {
1541 /* Split frag.
1542 * We have two variants in this case:
1543 * 1. Move all the frag to the second
1544 * part, if it is possible. F.e.
1545 * this approach is mandatory for TUX,
1546 * where splitting is expensive.
1547 * 2. Split is accurately. We make this.
1548 */
1549 get_page(skb_shinfo(skb)->frags[i].page);
1550 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1551 skb_shinfo(skb1)->frags[0].size -= len - pos;
1552 skb_shinfo(skb)->frags[i].size = len - pos;
1553 skb_shinfo(skb)->nr_frags++;
1554 }
1555 k++;
1556 } else
1557 skb_shinfo(skb)->nr_frags++;
1558 pos += size;
1559 }
1560 skb_shinfo(skb1)->nr_frags = k;
1561}
1562
1563/**
1564 * skb_split - Split fragmented skb to two parts at length len.
1565 * @skb: the buffer to split
1566 * @skb1: the buffer to receive the second part
1567 * @len: new length for skb
1568 */
1569void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1570{
1571 int pos = skb_headlen(skb);
1572
1573 if (len < pos) /* Split line is inside header. */
1574 skb_split_inside_header(skb, skb1, len, pos);
1575 else /* Second chunk has no header, nothing to copy. */
1576 skb_split_no_header(skb, skb1, len, pos);
1577}
1578
Thomas Graf677e90e2005-06-23 20:59:51 -07001579/**
1580 * skb_prepare_seq_read - Prepare a sequential read of skb data
1581 * @skb: the buffer to read
1582 * @from: lower offset of data to be read
1583 * @to: upper offset of data to be read
1584 * @st: state variable
1585 *
1586 * Initializes the specified state variable. Must be called before
1587 * invoking skb_seq_read() for the first time.
1588 */
1589void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1590 unsigned int to, struct skb_seq_state *st)
1591{
1592 st->lower_offset = from;
1593 st->upper_offset = to;
1594 st->root_skb = st->cur_skb = skb;
1595 st->frag_idx = st->stepped_offset = 0;
1596 st->frag_data = NULL;
1597}
1598
1599/**
1600 * skb_seq_read - Sequentially read skb data
1601 * @consumed: number of bytes consumed by the caller so far
1602 * @data: destination pointer for data to be returned
1603 * @st: state variable
1604 *
1605 * Reads a block of skb data at &consumed relative to the
1606 * lower offset specified to skb_prepare_seq_read(). Assigns
1607 * the head of the data block to &data and returns the length
1608 * of the block or 0 if the end of the skb data or the upper
1609 * offset has been reached.
1610 *
1611 * The caller is not required to consume all of the data
1612 * returned, i.e. &consumed is typically set to the number
1613 * of bytes already consumed and the next call to
1614 * skb_seq_read() will return the remaining part of the block.
1615 *
1616 * Note: The size of each block of data returned can be arbitary,
1617 * this limitation is the cost for zerocopy seqeuental
1618 * reads of potentially non linear data.
1619 *
1620 * Note: Fragment lists within fragments are not implemented
1621 * at the moment, state->root_skb could be replaced with
1622 * a stack for this purpose.
1623 */
1624unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1625 struct skb_seq_state *st)
1626{
1627 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1628 skb_frag_t *frag;
1629
1630 if (unlikely(abs_offset >= st->upper_offset))
1631 return 0;
1632
1633next_skb:
1634 block_limit = skb_headlen(st->cur_skb);
1635
1636 if (abs_offset < block_limit) {
1637 *data = st->cur_skb->data + abs_offset;
1638 return block_limit - abs_offset;
1639 }
1640
1641 if (st->frag_idx == 0 && !st->frag_data)
1642 st->stepped_offset += skb_headlen(st->cur_skb);
1643
1644 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1645 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1646 block_limit = frag->size + st->stepped_offset;
1647
1648 if (abs_offset < block_limit) {
1649 if (!st->frag_data)
1650 st->frag_data = kmap_skb_frag(frag);
1651
1652 *data = (u8 *) st->frag_data + frag->page_offset +
1653 (abs_offset - st->stepped_offset);
1654
1655 return block_limit - abs_offset;
1656 }
1657
1658 if (st->frag_data) {
1659 kunmap_skb_frag(st->frag_data);
1660 st->frag_data = NULL;
1661 }
1662
1663 st->frag_idx++;
1664 st->stepped_offset += frag->size;
1665 }
1666
1667 if (st->cur_skb->next) {
1668 st->cur_skb = st->cur_skb->next;
1669 st->frag_idx = 0;
1670 goto next_skb;
1671 } else if (st->root_skb == st->cur_skb &&
1672 skb_shinfo(st->root_skb)->frag_list) {
1673 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1674 goto next_skb;
1675 }
1676
1677 return 0;
1678}
1679
1680/**
1681 * skb_abort_seq_read - Abort a sequential read of skb data
1682 * @st: state variable
1683 *
1684 * Must be called if skb_seq_read() was not called until it
1685 * returned 0.
1686 */
1687void skb_abort_seq_read(struct skb_seq_state *st)
1688{
1689 if (st->frag_data)
1690 kunmap_skb_frag(st->frag_data);
1691}
1692
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001693#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1694
1695static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1696 struct ts_config *conf,
1697 struct ts_state *state)
1698{
1699 return skb_seq_read(offset, text, TS_SKB_CB(state));
1700}
1701
1702static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1703{
1704 skb_abort_seq_read(TS_SKB_CB(state));
1705}
1706
1707/**
1708 * skb_find_text - Find a text pattern in skb data
1709 * @skb: the buffer to look in
1710 * @from: search offset
1711 * @to: search limit
1712 * @config: textsearch configuration
1713 * @state: uninitialized textsearch state variable
1714 *
1715 * Finds a pattern in the skb data according to the specified
1716 * textsearch configuration. Use textsearch_next() to retrieve
1717 * subsequent occurrences of the pattern. Returns the offset
1718 * to the first occurrence or UINT_MAX if no match was found.
1719 */
1720unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1721 unsigned int to, struct ts_config *config,
1722 struct ts_state *state)
1723{
1724 config->get_next_block = skb_ts_get_next_block;
1725 config->finish = skb_ts_finish;
1726
1727 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1728
1729 return textsearch_find(config, state);
1730}
1731
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001732/**
1733 * skb_append_datato_frags: - append the user data to a skb
1734 * @sk: sock structure
1735 * @skb: skb structure to be appened with user data.
1736 * @getfrag: call back function to be used for getting the user data
1737 * @from: pointer to user message iov
1738 * @length: length of the iov message
1739 *
1740 * Description: This procedure append the user data in the fragment part
1741 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1742 */
1743int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001744 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001745 int len, int odd, struct sk_buff *skb),
1746 void *from, int length)
1747{
1748 int frg_cnt = 0;
1749 skb_frag_t *frag = NULL;
1750 struct page *page = NULL;
1751 int copy, left;
1752 int offset = 0;
1753 int ret;
1754
1755 do {
1756 /* Return error if we don't have space for new frag */
1757 frg_cnt = skb_shinfo(skb)->nr_frags;
1758 if (frg_cnt >= MAX_SKB_FRAGS)
1759 return -EFAULT;
1760
1761 /* allocate a new page for next frag */
1762 page = alloc_pages(sk->sk_allocation, 0);
1763
1764 /* If alloc_page fails just return failure and caller will
1765 * free previous allocated pages by doing kfree_skb()
1766 */
1767 if (page == NULL)
1768 return -ENOMEM;
1769
1770 /* initialize the next frag */
1771 sk->sk_sndmsg_page = page;
1772 sk->sk_sndmsg_off = 0;
1773 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1774 skb->truesize += PAGE_SIZE;
1775 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1776
1777 /* get the new initialized frag */
1778 frg_cnt = skb_shinfo(skb)->nr_frags;
1779 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1780
1781 /* copy the user data to page */
1782 left = PAGE_SIZE - frag->page_offset;
1783 copy = (length > left)? left : length;
1784
1785 ret = getfrag(from, (page_address(frag->page) +
1786 frag->page_offset + frag->size),
1787 offset, copy, 0, skb);
1788 if (ret < 0)
1789 return -EFAULT;
1790
1791 /* copy was successful so update the size parameters */
1792 sk->sk_sndmsg_off += copy;
1793 frag->size += copy;
1794 skb->len += copy;
1795 skb->data_len += copy;
1796 offset += copy;
1797 length -= copy;
1798
1799 } while (length > 0);
1800
1801 return 0;
1802}
1803
Herbert Xucbb042f2006-03-20 22:43:56 -08001804/**
1805 * skb_pull_rcsum - pull skb and update receive checksum
1806 * @skb: buffer to update
1807 * @start: start of data before pull
1808 * @len: length of data pulled
1809 *
1810 * This function performs an skb_pull on the packet and updates
1811 * update the CHECKSUM_HW checksum. It should be used on receive
1812 * path processing instead of skb_pull unless you know that the
1813 * checksum difference is zero (e.g., a valid IP header) or you
1814 * are setting ip_summed to CHECKSUM_NONE.
1815 */
1816unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1817{
1818 BUG_ON(len > skb->len);
1819 skb->len -= len;
1820 BUG_ON(skb->len < skb->data_len);
1821 skb_postpull_rcsum(skb, skb->data, len);
1822 return skb->data += len;
1823}
1824
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001825EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827void __init skb_init(void)
1828{
1829 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1830 sizeof(struct sk_buff),
1831 0,
1832 SLAB_HWCACHE_ALIGN,
1833 NULL, NULL);
1834 if (!skbuff_head_cache)
1835 panic("cannot create skbuff cache");
David S. Millerd179cd12005-08-17 14:57:30 -07001836
1837 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1838 (2*sizeof(struct sk_buff)) +
1839 sizeof(atomic_t),
1840 0,
1841 SLAB_HWCACHE_ALIGN,
1842 NULL, NULL);
1843 if (!skbuff_fclone_cache)
1844 panic("cannot create skbuff cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847EXPORT_SYMBOL(___pskb_trim);
1848EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08001849EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07001851EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852EXPORT_SYMBOL(pskb_copy);
1853EXPORT_SYMBOL(pskb_expand_head);
1854EXPORT_SYMBOL(skb_checksum);
1855EXPORT_SYMBOL(skb_clone);
1856EXPORT_SYMBOL(skb_clone_fraglist);
1857EXPORT_SYMBOL(skb_copy);
1858EXPORT_SYMBOL(skb_copy_and_csum_bits);
1859EXPORT_SYMBOL(skb_copy_and_csum_dev);
1860EXPORT_SYMBOL(skb_copy_bits);
1861EXPORT_SYMBOL(skb_copy_expand);
1862EXPORT_SYMBOL(skb_over_panic);
1863EXPORT_SYMBOL(skb_pad);
1864EXPORT_SYMBOL(skb_realloc_headroom);
1865EXPORT_SYMBOL(skb_under_panic);
1866EXPORT_SYMBOL(skb_dequeue);
1867EXPORT_SYMBOL(skb_dequeue_tail);
1868EXPORT_SYMBOL(skb_insert);
1869EXPORT_SYMBOL(skb_queue_purge);
1870EXPORT_SYMBOL(skb_queue_head);
1871EXPORT_SYMBOL(skb_queue_tail);
1872EXPORT_SYMBOL(skb_unlink);
1873EXPORT_SYMBOL(skb_append);
1874EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07001875EXPORT_SYMBOL(skb_prepare_seq_read);
1876EXPORT_SYMBOL(skb_seq_read);
1877EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001878EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001879EXPORT_SYMBOL(skb_append_datato_frags);