blob: 2960c8b82b232585ae9a152489d380fed25a944b [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
115/* Allocate a new skbuff. We do this ourselves so we can fill in a few
116 * 'private' fields and also do memory statistics to find all the
117 * [BEEP] leaks.
118 *
119 */
120
121/**
David S. Millerd179cd12005-08-17 14:57:30 -0700122 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * @size: size to allocate
124 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700125 * @fclone: allocate from fclone cache instead of head cache
126 * and allocate a cloned (child) skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * Allocate a new &sk_buff. The returned buffer has no headroom and a
129 * tail room of size bytes. The object has a reference count of one.
130 * The return is the buffer. On a failure the return is %NULL.
131 *
132 * Buffers may only be allocated from interrupts using a @gfp_mask of
133 * %GFP_ATOMIC.
134 */
Al Virodd0fc662005-10-07 07:46:04 +0100135struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
David S. Millerd179cd12005-08-17 14:57:30 -0700136 int fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Herbert Xu8798b3f2006-01-23 16:32:45 -0800138 kmem_cache_t *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800139 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct sk_buff *skb;
141 u8 *data;
142
Herbert Xu8798b3f2006-01-23 16:32:45 -0800143 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Get the HEAD */
Herbert Xu8798b3f2006-01-23 16:32:45 -0800146 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!skb)
148 goto out;
149
150 /* Get the DATA. Size must match skb_add_mtu(). */
151 size = SKB_DATA_ALIGN(size);
152 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
153 if (!data)
154 goto nodata;
155
156 memset(skb, 0, offsetof(struct sk_buff, truesize));
157 skb->truesize = size + sizeof(struct sk_buff);
158 atomic_set(&skb->users, 1);
159 skb->head = data;
160 skb->data = data;
161 skb->tail = data;
162 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800163 /* make sure we initialize shinfo sequentially */
164 shinfo = skb_shinfo(skb);
165 atomic_set(&shinfo->dataref, 1);
166 shinfo->nr_frags = 0;
167 shinfo->tso_size = 0;
168 shinfo->tso_segs = 0;
169 shinfo->ufo_size = 0;
170 shinfo->ip6_frag_id = 0;
171 shinfo->frag_list = NULL;
172
David S. Millerd179cd12005-08-17 14:57:30 -0700173 if (fclone) {
174 struct sk_buff *child = skb + 1;
175 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David S. Millerd179cd12005-08-17 14:57:30 -0700177 skb->fclone = SKB_FCLONE_ORIG;
178 atomic_set(fclone_ref, 1);
179
180 child->fclone = SKB_FCLONE_UNAVAILABLE;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182out:
183 return skb;
184nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800185 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 skb = NULL;
187 goto out;
188}
189
190/**
191 * alloc_skb_from_cache - allocate a network buffer
192 * @cp: kmem_cache from which to allocate the data area
193 * (object size must be big enough for @size bytes + skb overheads)
194 * @size: size to allocate
195 * @gfp_mask: allocation mask
196 *
197 * Allocate a new &sk_buff. The returned buffer has no headroom and
198 * tail room of size bytes. The object has a reference count of one.
199 * The return is the buffer. On a failure the return is %NULL.
200 *
201 * Buffers may only be allocated from interrupts using a @gfp_mask of
202 * %GFP_ATOMIC.
203 */
204struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700205 unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100206 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct sk_buff *skb;
209 u8 *data;
210
211 /* Get the HEAD */
212 skb = kmem_cache_alloc(skbuff_head_cache,
213 gfp_mask & ~__GFP_DMA);
214 if (!skb)
215 goto out;
216
217 /* Get the DATA. */
218 size = SKB_DATA_ALIGN(size);
219 data = kmem_cache_alloc(cp, gfp_mask);
220 if (!data)
221 goto nodata;
222
223 memset(skb, 0, offsetof(struct sk_buff, truesize));
224 skb->truesize = size + sizeof(struct sk_buff);
225 atomic_set(&skb->users, 1);
226 skb->head = data;
227 skb->data = data;
228 skb->tail = data;
229 skb->end = data + size;
230
231 atomic_set(&(skb_shinfo(skb)->dataref), 1);
232 skb_shinfo(skb)->nr_frags = 0;
233 skb_shinfo(skb)->tso_size = 0;
234 skb_shinfo(skb)->tso_segs = 0;
235 skb_shinfo(skb)->frag_list = NULL;
236out:
237 return skb;
238nodata:
239 kmem_cache_free(skbuff_head_cache, skb);
240 skb = NULL;
241 goto out;
242}
243
244
245static void skb_drop_fraglist(struct sk_buff *skb)
246{
247 struct sk_buff *list = skb_shinfo(skb)->frag_list;
248
249 skb_shinfo(skb)->frag_list = NULL;
250
251 do {
252 struct sk_buff *this = list;
253 list = list->next;
254 kfree_skb(this);
255 } while (list);
256}
257
258static void skb_clone_fraglist(struct sk_buff *skb)
259{
260 struct sk_buff *list;
261
262 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
263 skb_get(list);
264}
265
266void skb_release_data(struct sk_buff *skb)
267{
268 if (!skb->cloned ||
269 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
270 &skb_shinfo(skb)->dataref)) {
271 if (skb_shinfo(skb)->nr_frags) {
272 int i;
273 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
274 put_page(skb_shinfo(skb)->frags[i].page);
275 }
276
277 if (skb_shinfo(skb)->frag_list)
278 skb_drop_fraglist(skb);
279
280 kfree(skb->head);
281 }
282}
283
284/*
285 * Free an skbuff by memory without cleaning the state.
286 */
287void kfree_skbmem(struct sk_buff *skb)
288{
David S. Millerd179cd12005-08-17 14:57:30 -0700289 struct sk_buff *other;
290 atomic_t *fclone_ref;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700293 switch (skb->fclone) {
294 case SKB_FCLONE_UNAVAILABLE:
295 kmem_cache_free(skbuff_head_cache, skb);
296 break;
297
298 case SKB_FCLONE_ORIG:
299 fclone_ref = (atomic_t *) (skb + 2);
300 if (atomic_dec_and_test(fclone_ref))
301 kmem_cache_free(skbuff_fclone_cache, skb);
302 break;
303
304 case SKB_FCLONE_CLONE:
305 fclone_ref = (atomic_t *) (skb + 1);
306 other = skb - 1;
307
308 /* The clone portion is available for
309 * fast-cloning again.
310 */
311 skb->fclone = SKB_FCLONE_UNAVAILABLE;
312
313 if (atomic_dec_and_test(fclone_ref))
314 kmem_cache_free(skbuff_fclone_cache, other);
315 break;
316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319/**
320 * __kfree_skb - private function
321 * @skb: buffer
322 *
323 * Free an sk_buff. Release anything attached to the buffer.
324 * Clean the state. This is an internal helper function. Users should
325 * always call kfree_skb
326 */
327
328void __kfree_skb(struct sk_buff *skb)
329{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 dst_release(skb->dst);
331#ifdef CONFIG_XFRM
332 secpath_put(skb->sp);
333#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700334 if (skb->destructor) {
335 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 skb->destructor(skb);
337 }
338#ifdef CONFIG_NETFILTER
339 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800340#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
341 nf_conntrack_put_reasm(skb->nfct_reasm);
342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#ifdef CONFIG_BRIDGE_NETFILTER
344 nf_bridge_put(skb->nf_bridge);
345#endif
346#endif
347/* XXX: IS this still necessary? - JHS */
348#ifdef CONFIG_NET_SCHED
349 skb->tc_index = 0;
350#ifdef CONFIG_NET_CLS_ACT
351 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif
353#endif
354
355 kfree_skbmem(skb);
356}
357
358/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800359 * kfree_skb - free an sk_buff
360 * @skb: buffer to free
361 *
362 * Drop a reference to the buffer and free it if the usage count has
363 * hit zero.
364 */
365void kfree_skb(struct sk_buff *skb)
366{
367 if (unlikely(!skb))
368 return;
369 if (likely(atomic_read(&skb->users) == 1))
370 smp_rmb();
371 else if (likely(!atomic_dec_and_test(&skb->users)))
372 return;
373 __kfree_skb(skb);
374}
375
376/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * skb_clone - duplicate an sk_buff
378 * @skb: buffer to clone
379 * @gfp_mask: allocation priority
380 *
381 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
382 * copies share the same packet data but not structure. The new
383 * buffer has a reference count of 1. If the allocation fails the
384 * function returns %NULL otherwise the new buffer is returned.
385 *
386 * If this function is called from an interrupt gfp_mask() must be
387 * %GFP_ATOMIC.
388 */
389
Al Virodd0fc662005-10-07 07:46:04 +0100390struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
David S. Millerd179cd12005-08-17 14:57:30 -0700392 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David S. Millerd179cd12005-08-17 14:57:30 -0700394 n = skb + 1;
395 if (skb->fclone == SKB_FCLONE_ORIG &&
396 n->fclone == SKB_FCLONE_UNAVAILABLE) {
397 atomic_t *fclone_ref = (atomic_t *) (n + 1);
398 n->fclone = SKB_FCLONE_CLONE;
399 atomic_inc(fclone_ref);
400 } else {
401 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
402 if (!n)
403 return NULL;
404 n->fclone = SKB_FCLONE_UNAVAILABLE;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407#define C(x) n->x = skb->x
408
409 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700411 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 C(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 C(h);
414 C(nh);
415 C(mac);
416 C(dst);
417 dst_clone(skb->dst);
418 C(sp);
419#ifdef CONFIG_INET
420 secpath_get(skb->sp);
421#endif
422 memcpy(n->cb, skb->cb, sizeof(skb->cb));
423 C(len);
424 C(data_len);
425 C(csum);
426 C(local_df);
427 n->cloned = 1;
428 n->nohdr = 0;
429 C(pkt_type);
430 C(ip_summed);
431 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800432#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
433 C(ipvs_property);
434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 n->destructor = NULL;
437#ifdef CONFIG_NETFILTER
438 C(nfmark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 C(nfct);
440 nf_conntrack_get(skb->nfct);
441 C(nfctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800442#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
443 C(nfct_reasm);
444 nf_conntrack_get_reasm(skb->nfct_reasm);
445#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#ifdef CONFIG_BRIDGE_NETFILTER
447 C(nf_bridge);
448 nf_bridge_get(skb->nf_bridge);
449#endif
450#endif /*CONFIG_NETFILTER*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#ifdef CONFIG_NET_SCHED
452 C(tc_index);
453#ifdef CONFIG_NET_CLS_ACT
454 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700455 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
456 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 C(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
459
460#endif
461 C(truesize);
462 atomic_set(&n->users, 1);
463 C(head);
464 C(data);
465 C(tail);
466 C(end);
467
468 atomic_inc(&(skb_shinfo(skb)->dataref));
469 skb->cloned = 1;
470
471 return n;
472}
473
474static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
475{
476 /*
477 * Shift between the two data areas in bytes
478 */
479 unsigned long offset = new->data - old->data;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 new->sk = NULL;
482 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 new->priority = old->priority;
484 new->protocol = old->protocol;
485 new->dst = dst_clone(old->dst);
486#ifdef CONFIG_INET
487 new->sp = secpath_get(old->sp);
488#endif
489 new->h.raw = old->h.raw + offset;
490 new->nh.raw = old->nh.raw + offset;
491 new->mac.raw = old->mac.raw + offset;
492 memcpy(new->cb, old->cb, sizeof(old->cb));
493 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700494 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700496 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 new->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#ifdef CONFIG_NETFILTER
499 new->nfmark = old->nfmark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 new->nfct = old->nfct;
501 nf_conntrack_get(old->nfct);
502 new->nfctinfo = old->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800503#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
504 new->nfct_reasm = old->nfct_reasm;
505 nf_conntrack_get_reasm(old->nfct_reasm);
506#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300507#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
508 new->ipvs_property = old->ipvs_property;
509#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#ifdef CONFIG_BRIDGE_NETFILTER
511 new->nf_bridge = old->nf_bridge;
512 nf_bridge_get(old->nf_bridge);
513#endif
514#endif
515#ifdef CONFIG_NET_SCHED
516#ifdef CONFIG_NET_CLS_ACT
517 new->tc_verd = old->tc_verd;
518#endif
519 new->tc_index = old->tc_index;
520#endif
521 atomic_set(&new->users, 1);
522 skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size;
523 skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs;
524}
525
526/**
527 * skb_copy - create private copy of an sk_buff
528 * @skb: buffer to copy
529 * @gfp_mask: allocation priority
530 *
531 * Make a copy of both an &sk_buff and its data. This is used when the
532 * caller wishes to modify the data and needs a private copy of the
533 * data to alter. Returns %NULL on failure or the pointer to the buffer
534 * on success. The returned buffer has a reference count of 1.
535 *
536 * As by-product this function converts non-linear &sk_buff to linear
537 * one, so that &sk_buff becomes completely private and caller is allowed
538 * to modify all the data of returned buffer. This means that this
539 * function is not recommended for use in circumstances when only
540 * header is going to be modified. Use pskb_copy() instead.
541 */
542
Al Virodd0fc662005-10-07 07:46:04 +0100543struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 int headerlen = skb->data - skb->head;
546 /*
547 * Allocate the copy buffer
548 */
549 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
550 gfp_mask);
551 if (!n)
552 return NULL;
553
554 /* Set the data pointer */
555 skb_reserve(n, headerlen);
556 /* Set the tail pointer and length */
557 skb_put(n, skb->len);
558 n->csum = skb->csum;
559 n->ip_summed = skb->ip_summed;
560
561 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
562 BUG();
563
564 copy_skb_header(n, skb);
565 return n;
566}
567
568
569/**
570 * pskb_copy - create copy of an sk_buff with private head.
571 * @skb: buffer to copy
572 * @gfp_mask: allocation priority
573 *
574 * Make a copy of both an &sk_buff and part of its data, located
575 * in header. Fragmented data remain shared. This is used when
576 * the caller wishes to modify only header of &sk_buff and needs
577 * private copy of the header to alter. Returns %NULL on failure
578 * or the pointer to the buffer on success.
579 * The returned buffer has a reference count of 1.
580 */
581
Al Virodd0fc662005-10-07 07:46:04 +0100582struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 /*
585 * Allocate the copy buffer
586 */
587 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
588
589 if (!n)
590 goto out;
591
592 /* Set the data pointer */
593 skb_reserve(n, skb->data - skb->head);
594 /* Set the tail pointer and length */
595 skb_put(n, skb_headlen(skb));
596 /* Copy the bytes */
597 memcpy(n->data, skb->data, n->len);
598 n->csum = skb->csum;
599 n->ip_summed = skb->ip_summed;
600
601 n->data_len = skb->data_len;
602 n->len = skb->len;
603
604 if (skb_shinfo(skb)->nr_frags) {
605 int i;
606
607 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
608 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
609 get_page(skb_shinfo(n)->frags[i].page);
610 }
611 skb_shinfo(n)->nr_frags = i;
612 }
613
614 if (skb_shinfo(skb)->frag_list) {
615 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
616 skb_clone_fraglist(n);
617 }
618
619 copy_skb_header(n, skb);
620out:
621 return n;
622}
623
624/**
625 * pskb_expand_head - reallocate header of &sk_buff
626 * @skb: buffer to reallocate
627 * @nhead: room to add at head
628 * @ntail: room to add at tail
629 * @gfp_mask: allocation priority
630 *
631 * Expands (or creates identical copy, if &nhead and &ntail are zero)
632 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
633 * reference count of 1. Returns zero in the case of success or error,
634 * if expansion failed. In the last case, &sk_buff is not changed.
635 *
636 * All the pointers pointing into skb header may change and must be
637 * reloaded after call to this function.
638 */
639
Victor Fusco86a76ca2005-07-08 14:57:47 -0700640int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100641 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int i;
644 u8 *data;
645 int size = nhead + (skb->end - skb->head) + ntail;
646 long off;
647
648 if (skb_shared(skb))
649 BUG();
650
651 size = SKB_DATA_ALIGN(size);
652
653 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
654 if (!data)
655 goto nodata;
656
657 /* Copy only real data... and, alas, header. This should be
658 * optimized for the cases when header is void. */
659 memcpy(data + nhead, skb->head, skb->tail - skb->head);
660 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
661
662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
663 get_page(skb_shinfo(skb)->frags[i].page);
664
665 if (skb_shinfo(skb)->frag_list)
666 skb_clone_fraglist(skb);
667
668 skb_release_data(skb);
669
670 off = (data + nhead) - skb->head;
671
672 skb->head = data;
673 skb->end = data + size;
674 skb->data += off;
675 skb->tail += off;
676 skb->mac.raw += off;
677 skb->h.raw += off;
678 skb->nh.raw += off;
679 skb->cloned = 0;
680 skb->nohdr = 0;
681 atomic_set(&skb_shinfo(skb)->dataref, 1);
682 return 0;
683
684nodata:
685 return -ENOMEM;
686}
687
688/* Make private copy of skb with writable head and some headroom */
689
690struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
691{
692 struct sk_buff *skb2;
693 int delta = headroom - skb_headroom(skb);
694
695 if (delta <= 0)
696 skb2 = pskb_copy(skb, GFP_ATOMIC);
697 else {
698 skb2 = skb_clone(skb, GFP_ATOMIC);
699 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
700 GFP_ATOMIC)) {
701 kfree_skb(skb2);
702 skb2 = NULL;
703 }
704 }
705 return skb2;
706}
707
708
709/**
710 * skb_copy_expand - copy and expand sk_buff
711 * @skb: buffer to copy
712 * @newheadroom: new free bytes at head
713 * @newtailroom: new free bytes at tail
714 * @gfp_mask: allocation priority
715 *
716 * Make a copy of both an &sk_buff and its data and while doing so
717 * allocate additional space.
718 *
719 * This is used when the caller wishes to modify the data and needs a
720 * private copy of the data to alter as well as more space for new fields.
721 * Returns %NULL on failure or the pointer to the buffer
722 * on success. The returned buffer has a reference count of 1.
723 *
724 * You must pass %GFP_ATOMIC as the allocation priority if this function
725 * is called from an interrupt.
726 *
727 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
728 * only by netfilter in the cases when checksum is recalculated? --ANK
729 */
730struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700731 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100732 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 /*
735 * Allocate the copy buffer
736 */
737 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
738 gfp_mask);
739 int head_copy_len, head_copy_off;
740
741 if (!n)
742 return NULL;
743
744 skb_reserve(n, newheadroom);
745
746 /* Set the tail pointer and length */
747 skb_put(n, skb->len);
748
749 head_copy_len = skb_headroom(skb);
750 head_copy_off = 0;
751 if (newheadroom <= head_copy_len)
752 head_copy_len = newheadroom;
753 else
754 head_copy_off = newheadroom - head_copy_len;
755
756 /* Copy the linear header and data. */
757 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
758 skb->len + head_copy_len))
759 BUG();
760
761 copy_skb_header(n, skb);
762
763 return n;
764}
765
766/**
767 * skb_pad - zero pad the tail of an skb
768 * @skb: buffer to pad
769 * @pad: space to pad
770 *
771 * Ensure that a buffer is followed by a padding area that is zero
772 * filled. Used by network drivers which may DMA or transfer data
773 * beyond the buffer end onto the wire.
774 *
775 * May return NULL in out of memory cases.
776 */
777
778struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
779{
780 struct sk_buff *nskb;
781
782 /* If the skbuff is non linear tailroom is always zero.. */
783 if (skb_tailroom(skb) >= pad) {
784 memset(skb->data+skb->len, 0, pad);
785 return skb;
786 }
787
788 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
789 kfree_skb(skb);
790 if (nskb)
791 memset(nskb->data+nskb->len, 0, pad);
792 return nskb;
793}
794
795/* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
796 * If realloc==0 and trimming is impossible without change of data,
797 * it is BUG().
798 */
799
800int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
801{
802 int offset = skb_headlen(skb);
803 int nfrags = skb_shinfo(skb)->nr_frags;
804 int i;
805
806 for (i = 0; i < nfrags; i++) {
807 int end = offset + skb_shinfo(skb)->frags[i].size;
808 if (end > len) {
809 if (skb_cloned(skb)) {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800810 BUG_ON(!realloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
812 return -ENOMEM;
813 }
814 if (len <= offset) {
815 put_page(skb_shinfo(skb)->frags[i].page);
816 skb_shinfo(skb)->nr_frags--;
817 } else {
818 skb_shinfo(skb)->frags[i].size = len - offset;
819 }
820 }
821 offset = end;
822 }
823
824 if (offset < len) {
825 skb->data_len -= skb->len - len;
826 skb->len = len;
827 } else {
828 if (len <= skb_headlen(skb)) {
829 skb->len = len;
830 skb->data_len = 0;
831 skb->tail = skb->data + len;
832 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
833 skb_drop_fraglist(skb);
834 } else {
835 skb->data_len -= skb->len - len;
836 skb->len = len;
837 }
838 }
839
840 return 0;
841}
842
843/**
844 * __pskb_pull_tail - advance tail of skb header
845 * @skb: buffer to reallocate
846 * @delta: number of bytes to advance tail
847 *
848 * The function makes a sense only on a fragmented &sk_buff,
849 * it expands header moving its tail forward and copying necessary
850 * data from fragmented part.
851 *
852 * &sk_buff MUST have reference count of 1.
853 *
854 * Returns %NULL (and &sk_buff does not change) if pull failed
855 * or value of new tail of skb in the case of success.
856 *
857 * All the pointers pointing into skb header may change and must be
858 * reloaded after call to this function.
859 */
860
861/* Moves tail of skb head forward, copying data from fragmented part,
862 * when it is necessary.
863 * 1. It may fail due to malloc failure.
864 * 2. It may change skb pointers.
865 *
866 * It is pretty complicated. Luckily, it is called only in exceptional cases.
867 */
868unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
869{
870 /* If skb has not enough free space at tail, get new one
871 * plus 128 bytes for future expansions. If we have enough
872 * room at tail, reallocate without expansion only if skb is cloned.
873 */
874 int i, k, eat = (skb->tail + delta) - skb->end;
875
876 if (eat > 0 || skb_cloned(skb)) {
877 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
878 GFP_ATOMIC))
879 return NULL;
880 }
881
882 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
883 BUG();
884
885 /* Optimization: no fragments, no reasons to preestimate
886 * size of pulled pages. Superb.
887 */
888 if (!skb_shinfo(skb)->frag_list)
889 goto pull_pages;
890
891 /* Estimate size of pulled pages. */
892 eat = delta;
893 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
894 if (skb_shinfo(skb)->frags[i].size >= eat)
895 goto pull_pages;
896 eat -= skb_shinfo(skb)->frags[i].size;
897 }
898
899 /* If we need update frag list, we are in troubles.
900 * Certainly, it possible to add an offset to skb data,
901 * but taking into account that pulling is expected to
902 * be very rare operation, it is worth to fight against
903 * further bloating skb head and crucify ourselves here instead.
904 * Pure masohism, indeed. 8)8)
905 */
906 if (eat) {
907 struct sk_buff *list = skb_shinfo(skb)->frag_list;
908 struct sk_buff *clone = NULL;
909 struct sk_buff *insp = NULL;
910
911 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800912 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (list->len <= eat) {
915 /* Eaten as whole. */
916 eat -= list->len;
917 list = list->next;
918 insp = list;
919 } else {
920 /* Eaten partially. */
921
922 if (skb_shared(list)) {
923 /* Sucks! We need to fork list. :-( */
924 clone = skb_clone(list, GFP_ATOMIC);
925 if (!clone)
926 return NULL;
927 insp = list->next;
928 list = clone;
929 } else {
930 /* This may be pulled without
931 * problems. */
932 insp = list;
933 }
934 if (!pskb_pull(list, eat)) {
935 if (clone)
936 kfree_skb(clone);
937 return NULL;
938 }
939 break;
940 }
941 } while (eat);
942
943 /* Free pulled out fragments. */
944 while ((list = skb_shinfo(skb)->frag_list) != insp) {
945 skb_shinfo(skb)->frag_list = list->next;
946 kfree_skb(list);
947 }
948 /* And insert new clone at head. */
949 if (clone) {
950 clone->next = list;
951 skb_shinfo(skb)->frag_list = clone;
952 }
953 }
954 /* Success! Now we may commit changes to skb data. */
955
956pull_pages:
957 eat = delta;
958 k = 0;
959 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
960 if (skb_shinfo(skb)->frags[i].size <= eat) {
961 put_page(skb_shinfo(skb)->frags[i].page);
962 eat -= skb_shinfo(skb)->frags[i].size;
963 } else {
964 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
965 if (eat) {
966 skb_shinfo(skb)->frags[k].page_offset += eat;
967 skb_shinfo(skb)->frags[k].size -= eat;
968 eat = 0;
969 }
970 k++;
971 }
972 }
973 skb_shinfo(skb)->nr_frags = k;
974
975 skb->tail += delta;
976 skb->data_len -= delta;
977
978 return skb->tail;
979}
980
981/* Copy some data bits from skb to kernel buffer. */
982
983int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
984{
985 int i, copy;
986 int start = skb_headlen(skb);
987
988 if (offset > (int)skb->len - len)
989 goto fault;
990
991 /* Copy header. */
992 if ((copy = start - offset) > 0) {
993 if (copy > len)
994 copy = len;
995 memcpy(to, skb->data + offset, copy);
996 if ((len -= copy) == 0)
997 return 0;
998 offset += copy;
999 to += copy;
1000 }
1001
1002 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1003 int end;
1004
1005 BUG_TRAP(start <= offset + len);
1006
1007 end = start + skb_shinfo(skb)->frags[i].size;
1008 if ((copy = end - offset) > 0) {
1009 u8 *vaddr;
1010
1011 if (copy > len)
1012 copy = len;
1013
1014 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1015 memcpy(to,
1016 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1017 offset - start, copy);
1018 kunmap_skb_frag(vaddr);
1019
1020 if ((len -= copy) == 0)
1021 return 0;
1022 offset += copy;
1023 to += copy;
1024 }
1025 start = end;
1026 }
1027
1028 if (skb_shinfo(skb)->frag_list) {
1029 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1030
1031 for (; list; list = list->next) {
1032 int end;
1033
1034 BUG_TRAP(start <= offset + len);
1035
1036 end = start + list->len;
1037 if ((copy = end - offset) > 0) {
1038 if (copy > len)
1039 copy = len;
1040 if (skb_copy_bits(list, offset - start,
1041 to, copy))
1042 goto fault;
1043 if ((len -= copy) == 0)
1044 return 0;
1045 offset += copy;
1046 to += copy;
1047 }
1048 start = end;
1049 }
1050 }
1051 if (!len)
1052 return 0;
1053
1054fault:
1055 return -EFAULT;
1056}
1057
Herbert Xu357b40a2005-04-19 22:30:14 -07001058/**
1059 * skb_store_bits - store bits from kernel buffer to skb
1060 * @skb: destination buffer
1061 * @offset: offset in destination
1062 * @from: source buffer
1063 * @len: number of bytes to copy
1064 *
1065 * Copy the specified number of bytes from the source buffer to the
1066 * destination skb. This function handles all the messy bits of
1067 * traversing fragment lists and such.
1068 */
1069
1070int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1071{
1072 int i, copy;
1073 int start = skb_headlen(skb);
1074
1075 if (offset > (int)skb->len - len)
1076 goto fault;
1077
1078 if ((copy = start - offset) > 0) {
1079 if (copy > len)
1080 copy = len;
1081 memcpy(skb->data + offset, from, copy);
1082 if ((len -= copy) == 0)
1083 return 0;
1084 offset += copy;
1085 from += copy;
1086 }
1087
1088 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1089 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1090 int end;
1091
1092 BUG_TRAP(start <= offset + len);
1093
1094 end = start + frag->size;
1095 if ((copy = end - offset) > 0) {
1096 u8 *vaddr;
1097
1098 if (copy > len)
1099 copy = len;
1100
1101 vaddr = kmap_skb_frag(frag);
1102 memcpy(vaddr + frag->page_offset + offset - start,
1103 from, copy);
1104 kunmap_skb_frag(vaddr);
1105
1106 if ((len -= copy) == 0)
1107 return 0;
1108 offset += copy;
1109 from += copy;
1110 }
1111 start = end;
1112 }
1113
1114 if (skb_shinfo(skb)->frag_list) {
1115 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1116
1117 for (; list; list = list->next) {
1118 int end;
1119
1120 BUG_TRAP(start <= offset + len);
1121
1122 end = start + list->len;
1123 if ((copy = end - offset) > 0) {
1124 if (copy > len)
1125 copy = len;
1126 if (skb_store_bits(list, offset - start,
1127 from, copy))
1128 goto fault;
1129 if ((len -= copy) == 0)
1130 return 0;
1131 offset += copy;
1132 from += copy;
1133 }
1134 start = end;
1135 }
1136 }
1137 if (!len)
1138 return 0;
1139
1140fault:
1141 return -EFAULT;
1142}
1143
1144EXPORT_SYMBOL(skb_store_bits);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/* Checksum skb data. */
1147
1148unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1149 int len, unsigned int csum)
1150{
1151 int start = skb_headlen(skb);
1152 int i, copy = start - offset;
1153 int pos = 0;
1154
1155 /* Checksum header. */
1156 if (copy > 0) {
1157 if (copy > len)
1158 copy = len;
1159 csum = csum_partial(skb->data + offset, copy, csum);
1160 if ((len -= copy) == 0)
1161 return csum;
1162 offset += copy;
1163 pos = copy;
1164 }
1165
1166 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 int end;
1168
1169 BUG_TRAP(start <= offset + len);
1170
1171 end = start + skb_shinfo(skb)->frags[i].size;
1172 if ((copy = end - offset) > 0) {
1173 unsigned int csum2;
1174 u8 *vaddr;
1175 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1176
1177 if (copy > len)
1178 copy = len;
1179 vaddr = kmap_skb_frag(frag);
1180 csum2 = csum_partial(vaddr + frag->page_offset +
1181 offset - start, copy, 0);
1182 kunmap_skb_frag(vaddr);
1183 csum = csum_block_add(csum, csum2, pos);
1184 if (!(len -= copy))
1185 return csum;
1186 offset += copy;
1187 pos += copy;
1188 }
1189 start = end;
1190 }
1191
1192 if (skb_shinfo(skb)->frag_list) {
1193 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1194
1195 for (; list; list = list->next) {
1196 int end;
1197
1198 BUG_TRAP(start <= offset + len);
1199
1200 end = start + list->len;
1201 if ((copy = end - offset) > 0) {
1202 unsigned int csum2;
1203 if (copy > len)
1204 copy = len;
1205 csum2 = skb_checksum(list, offset - start,
1206 copy, 0);
1207 csum = csum_block_add(csum, csum2, pos);
1208 if ((len -= copy) == 0)
1209 return csum;
1210 offset += copy;
1211 pos += copy;
1212 }
1213 start = end;
1214 }
1215 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001216 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 return csum;
1219}
1220
1221/* Both of above in one bottle. */
1222
1223unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1224 u8 *to, int len, unsigned int csum)
1225{
1226 int start = skb_headlen(skb);
1227 int i, copy = start - offset;
1228 int pos = 0;
1229
1230 /* Copy header. */
1231 if (copy > 0) {
1232 if (copy > len)
1233 copy = len;
1234 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1235 copy, csum);
1236 if ((len -= copy) == 0)
1237 return csum;
1238 offset += copy;
1239 to += copy;
1240 pos = copy;
1241 }
1242
1243 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1244 int end;
1245
1246 BUG_TRAP(start <= offset + len);
1247
1248 end = start + skb_shinfo(skb)->frags[i].size;
1249 if ((copy = end - offset) > 0) {
1250 unsigned int csum2;
1251 u8 *vaddr;
1252 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1253
1254 if (copy > len)
1255 copy = len;
1256 vaddr = kmap_skb_frag(frag);
1257 csum2 = csum_partial_copy_nocheck(vaddr +
1258 frag->page_offset +
1259 offset - start, to,
1260 copy, 0);
1261 kunmap_skb_frag(vaddr);
1262 csum = csum_block_add(csum, csum2, pos);
1263 if (!(len -= copy))
1264 return csum;
1265 offset += copy;
1266 to += copy;
1267 pos += copy;
1268 }
1269 start = end;
1270 }
1271
1272 if (skb_shinfo(skb)->frag_list) {
1273 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1274
1275 for (; list; list = list->next) {
1276 unsigned int csum2;
1277 int end;
1278
1279 BUG_TRAP(start <= offset + len);
1280
1281 end = start + list->len;
1282 if ((copy = end - offset) > 0) {
1283 if (copy > len)
1284 copy = len;
1285 csum2 = skb_copy_and_csum_bits(list,
1286 offset - start,
1287 to, copy, 0);
1288 csum = csum_block_add(csum, csum2, pos);
1289 if ((len -= copy) == 0)
1290 return csum;
1291 offset += copy;
1292 to += copy;
1293 pos += copy;
1294 }
1295 start = end;
1296 }
1297 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001298 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return csum;
1300}
1301
1302void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1303{
1304 unsigned int csum;
1305 long csstart;
1306
1307 if (skb->ip_summed == CHECKSUM_HW)
1308 csstart = skb->h.raw - skb->data;
1309 else
1310 csstart = skb_headlen(skb);
1311
Kris Katterjohn09a62662006-01-08 22:24:28 -08001312 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 memcpy(to, skb->data, csstart);
1315
1316 csum = 0;
1317 if (csstart != skb->len)
1318 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1319 skb->len - csstart, 0);
1320
1321 if (skb->ip_summed == CHECKSUM_HW) {
1322 long csstuff = csstart + skb->csum;
1323
1324 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1325 }
1326}
1327
1328/**
1329 * skb_dequeue - remove from the head of the queue
1330 * @list: list to dequeue from
1331 *
1332 * Remove the head of the list. The list lock is taken so the function
1333 * may be used safely with other locking list functions. The head item is
1334 * returned or %NULL if the list is empty.
1335 */
1336
1337struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1338{
1339 unsigned long flags;
1340 struct sk_buff *result;
1341
1342 spin_lock_irqsave(&list->lock, flags);
1343 result = __skb_dequeue(list);
1344 spin_unlock_irqrestore(&list->lock, flags);
1345 return result;
1346}
1347
1348/**
1349 * skb_dequeue_tail - remove from the tail of the queue
1350 * @list: list to dequeue from
1351 *
1352 * Remove the tail of the list. The list lock is taken so the function
1353 * may be used safely with other locking list functions. The tail item is
1354 * returned or %NULL if the list is empty.
1355 */
1356struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1357{
1358 unsigned long flags;
1359 struct sk_buff *result;
1360
1361 spin_lock_irqsave(&list->lock, flags);
1362 result = __skb_dequeue_tail(list);
1363 spin_unlock_irqrestore(&list->lock, flags);
1364 return result;
1365}
1366
1367/**
1368 * skb_queue_purge - empty a list
1369 * @list: list to empty
1370 *
1371 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1372 * the list and one reference dropped. This function takes the list
1373 * lock and is atomic with respect to other list locking functions.
1374 */
1375void skb_queue_purge(struct sk_buff_head *list)
1376{
1377 struct sk_buff *skb;
1378 while ((skb = skb_dequeue(list)) != NULL)
1379 kfree_skb(skb);
1380}
1381
1382/**
1383 * skb_queue_head - queue a buffer at the list head
1384 * @list: list to use
1385 * @newsk: buffer to queue
1386 *
1387 * Queue a buffer at the start of the list. This function takes the
1388 * list lock and can be used safely with other locking &sk_buff functions
1389 * safely.
1390 *
1391 * A buffer cannot be placed on two lists at the same time.
1392 */
1393void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1394{
1395 unsigned long flags;
1396
1397 spin_lock_irqsave(&list->lock, flags);
1398 __skb_queue_head(list, newsk);
1399 spin_unlock_irqrestore(&list->lock, flags);
1400}
1401
1402/**
1403 * skb_queue_tail - queue a buffer at the list tail
1404 * @list: list to use
1405 * @newsk: buffer to queue
1406 *
1407 * Queue a buffer at the tail of the list. This function takes the
1408 * list lock and can be used safely with other locking &sk_buff functions
1409 * safely.
1410 *
1411 * A buffer cannot be placed on two lists at the same time.
1412 */
1413void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1414{
1415 unsigned long flags;
1416
1417 spin_lock_irqsave(&list->lock, flags);
1418 __skb_queue_tail(list, newsk);
1419 spin_unlock_irqrestore(&list->lock, flags);
1420}
David S. Miller8728b832005-08-09 19:25:21 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/**
1423 * skb_unlink - remove a buffer from a list
1424 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001425 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
David S. Miller8728b832005-08-09 19:25:21 -07001427 * Remove a packet from a list. The list locks are taken and this
1428 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 *
David S. Miller8728b832005-08-09 19:25:21 -07001430 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
David S. Miller8728b832005-08-09 19:25:21 -07001432void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
David S. Miller8728b832005-08-09 19:25:21 -07001434 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
David S. Miller8728b832005-08-09 19:25:21 -07001436 spin_lock_irqsave(&list->lock, flags);
1437 __skb_unlink(skb, list);
1438 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/**
1442 * skb_append - append a buffer
1443 * @old: buffer to insert after
1444 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001445 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 *
1447 * Place a packet after a given packet in a list. The list locks are taken
1448 * and this function is atomic with respect to other list locked calls.
1449 * A buffer cannot be placed on two lists at the same time.
1450 */
David S. Miller8728b832005-08-09 19:25:21 -07001451void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
1453 unsigned long flags;
1454
David S. Miller8728b832005-08-09 19:25:21 -07001455 spin_lock_irqsave(&list->lock, flags);
1456 __skb_append(old, newsk, list);
1457 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460
1461/**
1462 * skb_insert - insert a buffer
1463 * @old: buffer to insert before
1464 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001465 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 *
David S. Miller8728b832005-08-09 19:25:21 -07001467 * Place a packet before a given packet in a list. The list locks are
1468 * taken and this function is atomic with respect to other list locked
1469 * calls.
1470 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * A buffer cannot be placed on two lists at the same time.
1472 */
David S. Miller8728b832005-08-09 19:25:21 -07001473void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 unsigned long flags;
1476
David S. Miller8728b832005-08-09 19:25:21 -07001477 spin_lock_irqsave(&list->lock, flags);
1478 __skb_insert(newsk, old->prev, old, list);
1479 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482#if 0
1483/*
1484 * Tune the memory allocator for a new MTU size.
1485 */
1486void skb_add_mtu(int mtu)
1487{
1488 /* Must match allocation in alloc_skb */
1489 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1490
1491 kmem_add_cache_size(mtu);
1492}
1493#endif
1494
1495static inline void skb_split_inside_header(struct sk_buff *skb,
1496 struct sk_buff* skb1,
1497 const u32 len, const int pos)
1498{
1499 int i;
1500
1501 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1502
1503 /* And move data appendix as is. */
1504 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1505 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1506
1507 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1508 skb_shinfo(skb)->nr_frags = 0;
1509 skb1->data_len = skb->data_len;
1510 skb1->len += skb1->data_len;
1511 skb->data_len = 0;
1512 skb->len = len;
1513 skb->tail = skb->data + len;
1514}
1515
1516static inline void skb_split_no_header(struct sk_buff *skb,
1517 struct sk_buff* skb1,
1518 const u32 len, int pos)
1519{
1520 int i, k = 0;
1521 const int nfrags = skb_shinfo(skb)->nr_frags;
1522
1523 skb_shinfo(skb)->nr_frags = 0;
1524 skb1->len = skb1->data_len = skb->len - len;
1525 skb->len = len;
1526 skb->data_len = len - pos;
1527
1528 for (i = 0; i < nfrags; i++) {
1529 int size = skb_shinfo(skb)->frags[i].size;
1530
1531 if (pos + size > len) {
1532 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1533
1534 if (pos < len) {
1535 /* Split frag.
1536 * We have two variants in this case:
1537 * 1. Move all the frag to the second
1538 * part, if it is possible. F.e.
1539 * this approach is mandatory for TUX,
1540 * where splitting is expensive.
1541 * 2. Split is accurately. We make this.
1542 */
1543 get_page(skb_shinfo(skb)->frags[i].page);
1544 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1545 skb_shinfo(skb1)->frags[0].size -= len - pos;
1546 skb_shinfo(skb)->frags[i].size = len - pos;
1547 skb_shinfo(skb)->nr_frags++;
1548 }
1549 k++;
1550 } else
1551 skb_shinfo(skb)->nr_frags++;
1552 pos += size;
1553 }
1554 skb_shinfo(skb1)->nr_frags = k;
1555}
1556
1557/**
1558 * skb_split - Split fragmented skb to two parts at length len.
1559 * @skb: the buffer to split
1560 * @skb1: the buffer to receive the second part
1561 * @len: new length for skb
1562 */
1563void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1564{
1565 int pos = skb_headlen(skb);
1566
1567 if (len < pos) /* Split line is inside header. */
1568 skb_split_inside_header(skb, skb1, len, pos);
1569 else /* Second chunk has no header, nothing to copy. */
1570 skb_split_no_header(skb, skb1, len, pos);
1571}
1572
Thomas Graf677e90e2005-06-23 20:59:51 -07001573/**
1574 * skb_prepare_seq_read - Prepare a sequential read of skb data
1575 * @skb: the buffer to read
1576 * @from: lower offset of data to be read
1577 * @to: upper offset of data to be read
1578 * @st: state variable
1579 *
1580 * Initializes the specified state variable. Must be called before
1581 * invoking skb_seq_read() for the first time.
1582 */
1583void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1584 unsigned int to, struct skb_seq_state *st)
1585{
1586 st->lower_offset = from;
1587 st->upper_offset = to;
1588 st->root_skb = st->cur_skb = skb;
1589 st->frag_idx = st->stepped_offset = 0;
1590 st->frag_data = NULL;
1591}
1592
1593/**
1594 * skb_seq_read - Sequentially read skb data
1595 * @consumed: number of bytes consumed by the caller so far
1596 * @data: destination pointer for data to be returned
1597 * @st: state variable
1598 *
1599 * Reads a block of skb data at &consumed relative to the
1600 * lower offset specified to skb_prepare_seq_read(). Assigns
1601 * the head of the data block to &data and returns the length
1602 * of the block or 0 if the end of the skb data or the upper
1603 * offset has been reached.
1604 *
1605 * The caller is not required to consume all of the data
1606 * returned, i.e. &consumed is typically set to the number
1607 * of bytes already consumed and the next call to
1608 * skb_seq_read() will return the remaining part of the block.
1609 *
1610 * Note: The size of each block of data returned can be arbitary,
1611 * this limitation is the cost for zerocopy seqeuental
1612 * reads of potentially non linear data.
1613 *
1614 * Note: Fragment lists within fragments are not implemented
1615 * at the moment, state->root_skb could be replaced with
1616 * a stack for this purpose.
1617 */
1618unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1619 struct skb_seq_state *st)
1620{
1621 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1622 skb_frag_t *frag;
1623
1624 if (unlikely(abs_offset >= st->upper_offset))
1625 return 0;
1626
1627next_skb:
1628 block_limit = skb_headlen(st->cur_skb);
1629
1630 if (abs_offset < block_limit) {
1631 *data = st->cur_skb->data + abs_offset;
1632 return block_limit - abs_offset;
1633 }
1634
1635 if (st->frag_idx == 0 && !st->frag_data)
1636 st->stepped_offset += skb_headlen(st->cur_skb);
1637
1638 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1639 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1640 block_limit = frag->size + st->stepped_offset;
1641
1642 if (abs_offset < block_limit) {
1643 if (!st->frag_data)
1644 st->frag_data = kmap_skb_frag(frag);
1645
1646 *data = (u8 *) st->frag_data + frag->page_offset +
1647 (abs_offset - st->stepped_offset);
1648
1649 return block_limit - abs_offset;
1650 }
1651
1652 if (st->frag_data) {
1653 kunmap_skb_frag(st->frag_data);
1654 st->frag_data = NULL;
1655 }
1656
1657 st->frag_idx++;
1658 st->stepped_offset += frag->size;
1659 }
1660
1661 if (st->cur_skb->next) {
1662 st->cur_skb = st->cur_skb->next;
1663 st->frag_idx = 0;
1664 goto next_skb;
1665 } else if (st->root_skb == st->cur_skb &&
1666 skb_shinfo(st->root_skb)->frag_list) {
1667 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1668 goto next_skb;
1669 }
1670
1671 return 0;
1672}
1673
1674/**
1675 * skb_abort_seq_read - Abort a sequential read of skb data
1676 * @st: state variable
1677 *
1678 * Must be called if skb_seq_read() was not called until it
1679 * returned 0.
1680 */
1681void skb_abort_seq_read(struct skb_seq_state *st)
1682{
1683 if (st->frag_data)
1684 kunmap_skb_frag(st->frag_data);
1685}
1686
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001687#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1688
1689static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1690 struct ts_config *conf,
1691 struct ts_state *state)
1692{
1693 return skb_seq_read(offset, text, TS_SKB_CB(state));
1694}
1695
1696static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1697{
1698 skb_abort_seq_read(TS_SKB_CB(state));
1699}
1700
1701/**
1702 * skb_find_text - Find a text pattern in skb data
1703 * @skb: the buffer to look in
1704 * @from: search offset
1705 * @to: search limit
1706 * @config: textsearch configuration
1707 * @state: uninitialized textsearch state variable
1708 *
1709 * Finds a pattern in the skb data according to the specified
1710 * textsearch configuration. Use textsearch_next() to retrieve
1711 * subsequent occurrences of the pattern. Returns the offset
1712 * to the first occurrence or UINT_MAX if no match was found.
1713 */
1714unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1715 unsigned int to, struct ts_config *config,
1716 struct ts_state *state)
1717{
1718 config->get_next_block = skb_ts_get_next_block;
1719 config->finish = skb_ts_finish;
1720
1721 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1722
1723 return textsearch_find(config, state);
1724}
1725
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001726/**
1727 * skb_append_datato_frags: - append the user data to a skb
1728 * @sk: sock structure
1729 * @skb: skb structure to be appened with user data.
1730 * @getfrag: call back function to be used for getting the user data
1731 * @from: pointer to user message iov
1732 * @length: length of the iov message
1733 *
1734 * Description: This procedure append the user data in the fragment part
1735 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1736 */
1737int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001738 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001739 int len, int odd, struct sk_buff *skb),
1740 void *from, int length)
1741{
1742 int frg_cnt = 0;
1743 skb_frag_t *frag = NULL;
1744 struct page *page = NULL;
1745 int copy, left;
1746 int offset = 0;
1747 int ret;
1748
1749 do {
1750 /* Return error if we don't have space for new frag */
1751 frg_cnt = skb_shinfo(skb)->nr_frags;
1752 if (frg_cnt >= MAX_SKB_FRAGS)
1753 return -EFAULT;
1754
1755 /* allocate a new page for next frag */
1756 page = alloc_pages(sk->sk_allocation, 0);
1757
1758 /* If alloc_page fails just return failure and caller will
1759 * free previous allocated pages by doing kfree_skb()
1760 */
1761 if (page == NULL)
1762 return -ENOMEM;
1763
1764 /* initialize the next frag */
1765 sk->sk_sndmsg_page = page;
1766 sk->sk_sndmsg_off = 0;
1767 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1768 skb->truesize += PAGE_SIZE;
1769 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1770
1771 /* get the new initialized frag */
1772 frg_cnt = skb_shinfo(skb)->nr_frags;
1773 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1774
1775 /* copy the user data to page */
1776 left = PAGE_SIZE - frag->page_offset;
1777 copy = (length > left)? left : length;
1778
1779 ret = getfrag(from, (page_address(frag->page) +
1780 frag->page_offset + frag->size),
1781 offset, copy, 0, skb);
1782 if (ret < 0)
1783 return -EFAULT;
1784
1785 /* copy was successful so update the size parameters */
1786 sk->sk_sndmsg_off += copy;
1787 frag->size += copy;
1788 skb->len += copy;
1789 skb->data_len += copy;
1790 offset += copy;
1791 length -= copy;
1792
1793 } while (length > 0);
1794
1795 return 0;
1796}
1797
Herbert Xucbb042f2006-03-20 22:43:56 -08001798/**
1799 * skb_pull_rcsum - pull skb and update receive checksum
1800 * @skb: buffer to update
1801 * @start: start of data before pull
1802 * @len: length of data pulled
1803 *
1804 * This function performs an skb_pull on the packet and updates
1805 * update the CHECKSUM_HW checksum. It should be used on receive
1806 * path processing instead of skb_pull unless you know that the
1807 * checksum difference is zero (e.g., a valid IP header) or you
1808 * are setting ip_summed to CHECKSUM_NONE.
1809 */
1810unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1811{
1812 BUG_ON(len > skb->len);
1813 skb->len -= len;
1814 BUG_ON(skb->len < skb->data_len);
1815 skb_postpull_rcsum(skb, skb->data, len);
1816 return skb->data += len;
1817}
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819void __init skb_init(void)
1820{
1821 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1822 sizeof(struct sk_buff),
1823 0,
1824 SLAB_HWCACHE_ALIGN,
1825 NULL, NULL);
1826 if (!skbuff_head_cache)
1827 panic("cannot create skbuff cache");
David S. Millerd179cd12005-08-17 14:57:30 -07001828
1829 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1830 (2*sizeof(struct sk_buff)) +
1831 sizeof(atomic_t),
1832 0,
1833 SLAB_HWCACHE_ALIGN,
1834 NULL, NULL);
1835 if (!skbuff_fclone_cache)
1836 panic("cannot create skbuff cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
1839EXPORT_SYMBOL(___pskb_trim);
1840EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08001841EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07001843EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844EXPORT_SYMBOL(pskb_copy);
1845EXPORT_SYMBOL(pskb_expand_head);
1846EXPORT_SYMBOL(skb_checksum);
1847EXPORT_SYMBOL(skb_clone);
1848EXPORT_SYMBOL(skb_clone_fraglist);
1849EXPORT_SYMBOL(skb_copy);
1850EXPORT_SYMBOL(skb_copy_and_csum_bits);
1851EXPORT_SYMBOL(skb_copy_and_csum_dev);
1852EXPORT_SYMBOL(skb_copy_bits);
1853EXPORT_SYMBOL(skb_copy_expand);
1854EXPORT_SYMBOL(skb_over_panic);
1855EXPORT_SYMBOL(skb_pad);
1856EXPORT_SYMBOL(skb_realloc_headroom);
1857EXPORT_SYMBOL(skb_under_panic);
1858EXPORT_SYMBOL(skb_dequeue);
1859EXPORT_SYMBOL(skb_dequeue_tail);
1860EXPORT_SYMBOL(skb_insert);
1861EXPORT_SYMBOL(skb_queue_purge);
1862EXPORT_SYMBOL(skb_queue_head);
1863EXPORT_SYMBOL(skb_queue_tail);
1864EXPORT_SYMBOL(skb_unlink);
1865EXPORT_SYMBOL(skb_append);
1866EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07001867EXPORT_SYMBOL(skb_prepare_seq_read);
1868EXPORT_SYMBOL(skb_seq_read);
1869EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001870EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001871EXPORT_SYMBOL(skb_append_datato_frags);