blob: 7cfbdb215ba2711cca6c5f8f92c3f0be640989d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
50#include <linux/netdevice.h>
51#ifdef CONFIG_NET_CLS_ACT
52#include <net/pkt_sched.h>
53#endif
54#include <linux/string.h>
55#include <linux/skbuff.h>
56#include <linux/cache.h>
57#include <linux/rtnetlink.h>
58#include <linux/init.h>
59#include <linux/highmem.h>
60
61#include <net/protocol.h>
62#include <net/dst.h>
63#include <net/sock.h>
64#include <net/checksum.h>
65#include <net/xfrm.h>
66
67#include <asm/uaccess.h>
68#include <asm/system.h>
69
Eric Dumazetba899662005-08-26 12:05:31 -070070static kmem_cache_t *skbuff_head_cache __read_mostly;
71static kmem_cache_t *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 BUG();
94}
95
96/**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106{
Patrick McHardy26095452005-04-21 16:43:02 -0700107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 BUG();
112}
113
David S. Millerdc6de332006-04-20 00:10:50 -0700114void skb_truesize_bug(struct sk_buff *skb)
115{
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
119}
120EXPORT_SYMBOL(skb_truesize_bug);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
124 * [BEEP] leaks.
125 *
126 */
127
128/**
David S. Millerd179cd12005-08-17 14:57:30 -0700129 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * @size: size to allocate
131 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
138 *
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
140 * %GFP_ATOMIC.
141 */
Al Virodd0fc662005-10-07 07:46:04 +0100142struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
David S. Millerd179cd12005-08-17 14:57:30 -0700143 int fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Herbert Xu8798b3f2006-01-23 16:32:45 -0800145 kmem_cache_t *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800146 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct sk_buff *skb;
148 u8 *data;
149
Herbert Xu8798b3f2006-01-23 16:32:45 -0800150 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Get the HEAD */
Herbert Xu8798b3f2006-01-23 16:32:45 -0800153 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!skb)
155 goto out;
156
157 /* Get the DATA. Size must match skb_add_mtu(). */
158 size = SKB_DATA_ALIGN(size);
Al Viro871751e2006-03-25 03:06:39 -0800159 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!data)
161 goto nodata;
162
163 memset(skb, 0, offsetof(struct sk_buff, truesize));
164 skb->truesize = size + sizeof(struct sk_buff);
165 atomic_set(&skb->users, 1);
166 skb->head = data;
167 skb->data = data;
168 skb->tail = data;
169 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800170 /* make sure we initialize shinfo sequentially */
171 shinfo = skb_shinfo(skb);
172 atomic_set(&shinfo->dataref, 1);
173 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700174 shinfo->gso_size = 0;
175 shinfo->gso_segs = 0;
176 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800177 shinfo->ip6_frag_id = 0;
178 shinfo->frag_list = NULL;
179
David S. Millerd179cd12005-08-17 14:57:30 -0700180 if (fclone) {
181 struct sk_buff *child = skb + 1;
182 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David S. Millerd179cd12005-08-17 14:57:30 -0700184 skb->fclone = SKB_FCLONE_ORIG;
185 atomic_set(fclone_ref, 1);
186
187 child->fclone = SKB_FCLONE_UNAVAILABLE;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189out:
190 return skb;
191nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800192 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 skb = NULL;
194 goto out;
195}
196
197/**
198 * alloc_skb_from_cache - allocate a network buffer
199 * @cp: kmem_cache from which to allocate the data area
200 * (object size must be big enough for @size bytes + skb overheads)
201 * @size: size to allocate
202 * @gfp_mask: allocation mask
203 *
204 * Allocate a new &sk_buff. The returned buffer has no headroom and
205 * tail room of size bytes. The object has a reference count of one.
206 * The return is the buffer. On a failure the return is %NULL.
207 *
208 * Buffers may only be allocated from interrupts using a @gfp_mask of
209 * %GFP_ATOMIC.
210 */
211struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700212 unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100213 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct sk_buff *skb;
216 u8 *data;
217
218 /* Get the HEAD */
219 skb = kmem_cache_alloc(skbuff_head_cache,
220 gfp_mask & ~__GFP_DMA);
221 if (!skb)
222 goto out;
223
224 /* Get the DATA. */
225 size = SKB_DATA_ALIGN(size);
226 data = kmem_cache_alloc(cp, gfp_mask);
227 if (!data)
228 goto nodata;
229
230 memset(skb, 0, offsetof(struct sk_buff, truesize));
231 skb->truesize = size + sizeof(struct sk_buff);
232 atomic_set(&skb->users, 1);
233 skb->head = data;
234 skb->data = data;
235 skb->tail = data;
236 skb->end = data + size;
237
238 atomic_set(&(skb_shinfo(skb)->dataref), 1);
239 skb_shinfo(skb)->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700240 skb_shinfo(skb)->gso_size = 0;
241 skb_shinfo(skb)->gso_segs = 0;
242 skb_shinfo(skb)->gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 skb_shinfo(skb)->frag_list = NULL;
244out:
245 return skb;
246nodata:
247 kmem_cache_free(skbuff_head_cache, skb);
248 skb = NULL;
249 goto out;
250}
251
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
Adrian Bunk5bba1712006-06-29 13:02:35 -0700274static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
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);
Herbert Xu79671682006-06-22 02:40:14 -0700531 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
532 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
533 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
537 * skb_copy - create private copy of an sk_buff
538 * @skb: buffer to copy
539 * @gfp_mask: allocation priority
540 *
541 * Make a copy of both an &sk_buff and its data. This is used when the
542 * caller wishes to modify the data and needs a private copy of the
543 * data to alter. Returns %NULL on failure or the pointer to the buffer
544 * on success. The returned buffer has a reference count of 1.
545 *
546 * As by-product this function converts non-linear &sk_buff to linear
547 * one, so that &sk_buff becomes completely private and caller is allowed
548 * to modify all the data of returned buffer. This means that this
549 * function is not recommended for use in circumstances when only
550 * header is going to be modified. Use pskb_copy() instead.
551 */
552
Al Virodd0fc662005-10-07 07:46:04 +0100553struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 int headerlen = skb->data - skb->head;
556 /*
557 * Allocate the copy buffer
558 */
559 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
560 gfp_mask);
561 if (!n)
562 return NULL;
563
564 /* Set the data pointer */
565 skb_reserve(n, headerlen);
566 /* Set the tail pointer and length */
567 skb_put(n, skb->len);
568 n->csum = skb->csum;
569 n->ip_summed = skb->ip_summed;
570
571 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
572 BUG();
573
574 copy_skb_header(n, skb);
575 return n;
576}
577
578
579/**
580 * pskb_copy - create copy of an sk_buff with private head.
581 * @skb: buffer to copy
582 * @gfp_mask: allocation priority
583 *
584 * Make a copy of both an &sk_buff and part of its data, located
585 * in header. Fragmented data remain shared. This is used when
586 * the caller wishes to modify only header of &sk_buff and needs
587 * private copy of the header to alter. Returns %NULL on failure
588 * or the pointer to the buffer on success.
589 * The returned buffer has a reference count of 1.
590 */
591
Al Virodd0fc662005-10-07 07:46:04 +0100592struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 /*
595 * Allocate the copy buffer
596 */
597 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
598
599 if (!n)
600 goto out;
601
602 /* Set the data pointer */
603 skb_reserve(n, skb->data - skb->head);
604 /* Set the tail pointer and length */
605 skb_put(n, skb_headlen(skb));
606 /* Copy the bytes */
607 memcpy(n->data, skb->data, n->len);
608 n->csum = skb->csum;
609 n->ip_summed = skb->ip_summed;
610
611 n->data_len = skb->data_len;
612 n->len = skb->len;
613
614 if (skb_shinfo(skb)->nr_frags) {
615 int i;
616
617 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
618 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
619 get_page(skb_shinfo(n)->frags[i].page);
620 }
621 skb_shinfo(n)->nr_frags = i;
622 }
623
624 if (skb_shinfo(skb)->frag_list) {
625 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
626 skb_clone_fraglist(n);
627 }
628
629 copy_skb_header(n, skb);
630out:
631 return n;
632}
633
634/**
635 * pskb_expand_head - reallocate header of &sk_buff
636 * @skb: buffer to reallocate
637 * @nhead: room to add at head
638 * @ntail: room to add at tail
639 * @gfp_mask: allocation priority
640 *
641 * Expands (or creates identical copy, if &nhead and &ntail are zero)
642 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
643 * reference count of 1. Returns zero in the case of success or error,
644 * if expansion failed. In the last case, &sk_buff is not changed.
645 *
646 * All the pointers pointing into skb header may change and must be
647 * reloaded after call to this function.
648 */
649
Victor Fusco86a76ca2005-07-08 14:57:47 -0700650int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100651 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 int i;
654 u8 *data;
655 int size = nhead + (skb->end - skb->head) + ntail;
656 long off;
657
658 if (skb_shared(skb))
659 BUG();
660
661 size = SKB_DATA_ALIGN(size);
662
663 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
664 if (!data)
665 goto nodata;
666
667 /* Copy only real data... and, alas, header. This should be
668 * optimized for the cases when header is void. */
669 memcpy(data + nhead, skb->head, skb->tail - skb->head);
670 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
671
672 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
673 get_page(skb_shinfo(skb)->frags[i].page);
674
675 if (skb_shinfo(skb)->frag_list)
676 skb_clone_fraglist(skb);
677
678 skb_release_data(skb);
679
680 off = (data + nhead) - skb->head;
681
682 skb->head = data;
683 skb->end = data + size;
684 skb->data += off;
685 skb->tail += off;
686 skb->mac.raw += off;
687 skb->h.raw += off;
688 skb->nh.raw += off;
689 skb->cloned = 0;
690 skb->nohdr = 0;
691 atomic_set(&skb_shinfo(skb)->dataref, 1);
692 return 0;
693
694nodata:
695 return -ENOMEM;
696}
697
698/* Make private copy of skb with writable head and some headroom */
699
700struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
701{
702 struct sk_buff *skb2;
703 int delta = headroom - skb_headroom(skb);
704
705 if (delta <= 0)
706 skb2 = pskb_copy(skb, GFP_ATOMIC);
707 else {
708 skb2 = skb_clone(skb, GFP_ATOMIC);
709 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
710 GFP_ATOMIC)) {
711 kfree_skb(skb2);
712 skb2 = NULL;
713 }
714 }
715 return skb2;
716}
717
718
719/**
720 * skb_copy_expand - copy and expand sk_buff
721 * @skb: buffer to copy
722 * @newheadroom: new free bytes at head
723 * @newtailroom: new free bytes at tail
724 * @gfp_mask: allocation priority
725 *
726 * Make a copy of both an &sk_buff and its data and while doing so
727 * allocate additional space.
728 *
729 * This is used when the caller wishes to modify the data and needs a
730 * private copy of the data to alter as well as more space for new fields.
731 * Returns %NULL on failure or the pointer to the buffer
732 * on success. The returned buffer has a reference count of 1.
733 *
734 * You must pass %GFP_ATOMIC as the allocation priority if this function
735 * is called from an interrupt.
736 *
737 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
738 * only by netfilter in the cases when checksum is recalculated? --ANK
739 */
740struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700741 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100742 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 /*
745 * Allocate the copy buffer
746 */
747 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
748 gfp_mask);
749 int head_copy_len, head_copy_off;
750
751 if (!n)
752 return NULL;
753
754 skb_reserve(n, newheadroom);
755
756 /* Set the tail pointer and length */
757 skb_put(n, skb->len);
758
759 head_copy_len = skb_headroom(skb);
760 head_copy_off = 0;
761 if (newheadroom <= head_copy_len)
762 head_copy_len = newheadroom;
763 else
764 head_copy_off = newheadroom - head_copy_len;
765
766 /* Copy the linear header and data. */
767 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
768 skb->len + head_copy_len))
769 BUG();
770
771 copy_skb_header(n, skb);
772
773 return n;
774}
775
776/**
777 * skb_pad - zero pad the tail of an skb
778 * @skb: buffer to pad
779 * @pad: space to pad
780 *
781 * Ensure that a buffer is followed by a padding area that is zero
782 * filled. Used by network drivers which may DMA or transfer data
783 * beyond the buffer end onto the wire.
784 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700785 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787
Herbert Xu5b057c62006-06-23 02:06:41 -0700788int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Herbert Xu5b057c62006-06-23 02:06:41 -0700790 int err;
791 int ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700794 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700798
799 ntail = skb->data_len + pad - (skb->end - skb->tail);
800 if (likely(skb_cloned(skb) || ntail > 0)) {
801 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
802 if (unlikely(err))
803 goto free_skb;
804 }
805
806 /* FIXME: The use of this function with non-linear skb's really needs
807 * to be audited.
808 */
809 err = skb_linearize(skb);
810 if (unlikely(err))
811 goto free_skb;
812
813 memset(skb->data + skb->len, 0, pad);
814 return 0;
815
816free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700818 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Herbert Xu3cc0e872006-06-09 16:13:38 -0700821/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
Herbert Xu3cc0e872006-06-09 16:13:38 -0700824int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int offset = skb_headlen(skb);
827 int nfrags = skb_shinfo(skb)->nr_frags;
828 int i;
829
830 for (i = 0; i < nfrags; i++) {
831 int end = offset + skb_shinfo(skb)->frags[i].size;
832 if (end > len) {
833 if (skb_cloned(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
835 return -ENOMEM;
836 }
837 if (len <= offset) {
838 put_page(skb_shinfo(skb)->frags[i].page);
839 skb_shinfo(skb)->nr_frags--;
840 } else {
841 skb_shinfo(skb)->frags[i].size = len - offset;
842 }
843 }
844 offset = end;
845 }
846
847 if (offset < len) {
848 skb->data_len -= skb->len - len;
849 skb->len = len;
850 } else {
851 if (len <= skb_headlen(skb)) {
852 skb->len = len;
853 skb->data_len = 0;
854 skb->tail = skb->data + len;
855 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
856 skb_drop_fraglist(skb);
857 } else {
858 skb->data_len -= skb->len - len;
859 skb->len = len;
860 }
861 }
862
863 return 0;
864}
865
866/**
867 * __pskb_pull_tail - advance tail of skb header
868 * @skb: buffer to reallocate
869 * @delta: number of bytes to advance tail
870 *
871 * The function makes a sense only on a fragmented &sk_buff,
872 * it expands header moving its tail forward and copying necessary
873 * data from fragmented part.
874 *
875 * &sk_buff MUST have reference count of 1.
876 *
877 * Returns %NULL (and &sk_buff does not change) if pull failed
878 * or value of new tail of skb in the case of success.
879 *
880 * All the pointers pointing into skb header may change and must be
881 * reloaded after call to this function.
882 */
883
884/* Moves tail of skb head forward, copying data from fragmented part,
885 * when it is necessary.
886 * 1. It may fail due to malloc failure.
887 * 2. It may change skb pointers.
888 *
889 * It is pretty complicated. Luckily, it is called only in exceptional cases.
890 */
891unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
892{
893 /* If skb has not enough free space at tail, get new one
894 * plus 128 bytes for future expansions. If we have enough
895 * room at tail, reallocate without expansion only if skb is cloned.
896 */
897 int i, k, eat = (skb->tail + delta) - skb->end;
898
899 if (eat > 0 || skb_cloned(skb)) {
900 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
901 GFP_ATOMIC))
902 return NULL;
903 }
904
905 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
906 BUG();
907
908 /* Optimization: no fragments, no reasons to preestimate
909 * size of pulled pages. Superb.
910 */
911 if (!skb_shinfo(skb)->frag_list)
912 goto pull_pages;
913
914 /* Estimate size of pulled pages. */
915 eat = delta;
916 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
917 if (skb_shinfo(skb)->frags[i].size >= eat)
918 goto pull_pages;
919 eat -= skb_shinfo(skb)->frags[i].size;
920 }
921
922 /* If we need update frag list, we are in troubles.
923 * Certainly, it possible to add an offset to skb data,
924 * but taking into account that pulling is expected to
925 * be very rare operation, it is worth to fight against
926 * further bloating skb head and crucify ourselves here instead.
927 * Pure masohism, indeed. 8)8)
928 */
929 if (eat) {
930 struct sk_buff *list = skb_shinfo(skb)->frag_list;
931 struct sk_buff *clone = NULL;
932 struct sk_buff *insp = NULL;
933
934 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800935 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (list->len <= eat) {
938 /* Eaten as whole. */
939 eat -= list->len;
940 list = list->next;
941 insp = list;
942 } else {
943 /* Eaten partially. */
944
945 if (skb_shared(list)) {
946 /* Sucks! We need to fork list. :-( */
947 clone = skb_clone(list, GFP_ATOMIC);
948 if (!clone)
949 return NULL;
950 insp = list->next;
951 list = clone;
952 } else {
953 /* This may be pulled without
954 * problems. */
955 insp = list;
956 }
957 if (!pskb_pull(list, eat)) {
958 if (clone)
959 kfree_skb(clone);
960 return NULL;
961 }
962 break;
963 }
964 } while (eat);
965
966 /* Free pulled out fragments. */
967 while ((list = skb_shinfo(skb)->frag_list) != insp) {
968 skb_shinfo(skb)->frag_list = list->next;
969 kfree_skb(list);
970 }
971 /* And insert new clone at head. */
972 if (clone) {
973 clone->next = list;
974 skb_shinfo(skb)->frag_list = clone;
975 }
976 }
977 /* Success! Now we may commit changes to skb data. */
978
979pull_pages:
980 eat = delta;
981 k = 0;
982 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
983 if (skb_shinfo(skb)->frags[i].size <= eat) {
984 put_page(skb_shinfo(skb)->frags[i].page);
985 eat -= skb_shinfo(skb)->frags[i].size;
986 } else {
987 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
988 if (eat) {
989 skb_shinfo(skb)->frags[k].page_offset += eat;
990 skb_shinfo(skb)->frags[k].size -= eat;
991 eat = 0;
992 }
993 k++;
994 }
995 }
996 skb_shinfo(skb)->nr_frags = k;
997
998 skb->tail += delta;
999 skb->data_len -= delta;
1000
1001 return skb->tail;
1002}
1003
1004/* Copy some data bits from skb to kernel buffer. */
1005
1006int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1007{
1008 int i, copy;
1009 int start = skb_headlen(skb);
1010
1011 if (offset > (int)skb->len - len)
1012 goto fault;
1013
1014 /* Copy header. */
1015 if ((copy = start - offset) > 0) {
1016 if (copy > len)
1017 copy = len;
1018 memcpy(to, skb->data + offset, copy);
1019 if ((len -= copy) == 0)
1020 return 0;
1021 offset += copy;
1022 to += copy;
1023 }
1024
1025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1026 int end;
1027
1028 BUG_TRAP(start <= offset + len);
1029
1030 end = start + skb_shinfo(skb)->frags[i].size;
1031 if ((copy = end - offset) > 0) {
1032 u8 *vaddr;
1033
1034 if (copy > len)
1035 copy = len;
1036
1037 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1038 memcpy(to,
1039 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1040 offset - start, copy);
1041 kunmap_skb_frag(vaddr);
1042
1043 if ((len -= copy) == 0)
1044 return 0;
1045 offset += copy;
1046 to += copy;
1047 }
1048 start = end;
1049 }
1050
1051 if (skb_shinfo(skb)->frag_list) {
1052 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1053
1054 for (; list; list = list->next) {
1055 int end;
1056
1057 BUG_TRAP(start <= offset + len);
1058
1059 end = start + list->len;
1060 if ((copy = end - offset) > 0) {
1061 if (copy > len)
1062 copy = len;
1063 if (skb_copy_bits(list, offset - start,
1064 to, copy))
1065 goto fault;
1066 if ((len -= copy) == 0)
1067 return 0;
1068 offset += copy;
1069 to += copy;
1070 }
1071 start = end;
1072 }
1073 }
1074 if (!len)
1075 return 0;
1076
1077fault:
1078 return -EFAULT;
1079}
1080
Herbert Xu357b40a2005-04-19 22:30:14 -07001081/**
1082 * skb_store_bits - store bits from kernel buffer to skb
1083 * @skb: destination buffer
1084 * @offset: offset in destination
1085 * @from: source buffer
1086 * @len: number of bytes to copy
1087 *
1088 * Copy the specified number of bytes from the source buffer to the
1089 * destination skb. This function handles all the messy bits of
1090 * traversing fragment lists and such.
1091 */
1092
1093int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1094{
1095 int i, copy;
1096 int start = skb_headlen(skb);
1097
1098 if (offset > (int)skb->len - len)
1099 goto fault;
1100
1101 if ((copy = start - offset) > 0) {
1102 if (copy > len)
1103 copy = len;
1104 memcpy(skb->data + offset, from, copy);
1105 if ((len -= copy) == 0)
1106 return 0;
1107 offset += copy;
1108 from += copy;
1109 }
1110
1111 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1112 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1113 int end;
1114
1115 BUG_TRAP(start <= offset + len);
1116
1117 end = start + frag->size;
1118 if ((copy = end - offset) > 0) {
1119 u8 *vaddr;
1120
1121 if (copy > len)
1122 copy = len;
1123
1124 vaddr = kmap_skb_frag(frag);
1125 memcpy(vaddr + frag->page_offset + offset - start,
1126 from, copy);
1127 kunmap_skb_frag(vaddr);
1128
1129 if ((len -= copy) == 0)
1130 return 0;
1131 offset += copy;
1132 from += copy;
1133 }
1134 start = end;
1135 }
1136
1137 if (skb_shinfo(skb)->frag_list) {
1138 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1139
1140 for (; list; list = list->next) {
1141 int end;
1142
1143 BUG_TRAP(start <= offset + len);
1144
1145 end = start + list->len;
1146 if ((copy = end - offset) > 0) {
1147 if (copy > len)
1148 copy = len;
1149 if (skb_store_bits(list, offset - start,
1150 from, copy))
1151 goto fault;
1152 if ((len -= copy) == 0)
1153 return 0;
1154 offset += copy;
1155 from += copy;
1156 }
1157 start = end;
1158 }
1159 }
1160 if (!len)
1161 return 0;
1162
1163fault:
1164 return -EFAULT;
1165}
1166
1167EXPORT_SYMBOL(skb_store_bits);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169/* Checksum skb data. */
1170
1171unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1172 int len, unsigned int csum)
1173{
1174 int start = skb_headlen(skb);
1175 int i, copy = start - offset;
1176 int pos = 0;
1177
1178 /* Checksum header. */
1179 if (copy > 0) {
1180 if (copy > len)
1181 copy = len;
1182 csum = csum_partial(skb->data + offset, copy, csum);
1183 if ((len -= copy) == 0)
1184 return csum;
1185 offset += copy;
1186 pos = copy;
1187 }
1188
1189 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1190 int end;
1191
1192 BUG_TRAP(start <= offset + len);
1193
1194 end = start + skb_shinfo(skb)->frags[i].size;
1195 if ((copy = end - offset) > 0) {
1196 unsigned int csum2;
1197 u8 *vaddr;
1198 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1199
1200 if (copy > len)
1201 copy = len;
1202 vaddr = kmap_skb_frag(frag);
1203 csum2 = csum_partial(vaddr + frag->page_offset +
1204 offset - start, copy, 0);
1205 kunmap_skb_frag(vaddr);
1206 csum = csum_block_add(csum, csum2, pos);
1207 if (!(len -= copy))
1208 return csum;
1209 offset += copy;
1210 pos += copy;
1211 }
1212 start = end;
1213 }
1214
1215 if (skb_shinfo(skb)->frag_list) {
1216 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1217
1218 for (; list; list = list->next) {
1219 int end;
1220
1221 BUG_TRAP(start <= offset + len);
1222
1223 end = start + list->len;
1224 if ((copy = end - offset) > 0) {
1225 unsigned int csum2;
1226 if (copy > len)
1227 copy = len;
1228 csum2 = skb_checksum(list, offset - start,
1229 copy, 0);
1230 csum = csum_block_add(csum, csum2, pos);
1231 if ((len -= copy) == 0)
1232 return csum;
1233 offset += copy;
1234 pos += copy;
1235 }
1236 start = end;
1237 }
1238 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001239 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 return csum;
1242}
1243
1244/* Both of above in one bottle. */
1245
1246unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1247 u8 *to, int len, unsigned int csum)
1248{
1249 int start = skb_headlen(skb);
1250 int i, copy = start - offset;
1251 int pos = 0;
1252
1253 /* Copy header. */
1254 if (copy > 0) {
1255 if (copy > len)
1256 copy = len;
1257 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1258 copy, csum);
1259 if ((len -= copy) == 0)
1260 return csum;
1261 offset += copy;
1262 to += copy;
1263 pos = copy;
1264 }
1265
1266 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1267 int end;
1268
1269 BUG_TRAP(start <= offset + len);
1270
1271 end = start + skb_shinfo(skb)->frags[i].size;
1272 if ((copy = end - offset) > 0) {
1273 unsigned int csum2;
1274 u8 *vaddr;
1275 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1276
1277 if (copy > len)
1278 copy = len;
1279 vaddr = kmap_skb_frag(frag);
1280 csum2 = csum_partial_copy_nocheck(vaddr +
1281 frag->page_offset +
1282 offset - start, to,
1283 copy, 0);
1284 kunmap_skb_frag(vaddr);
1285 csum = csum_block_add(csum, csum2, pos);
1286 if (!(len -= copy))
1287 return csum;
1288 offset += copy;
1289 to += copy;
1290 pos += copy;
1291 }
1292 start = end;
1293 }
1294
1295 if (skb_shinfo(skb)->frag_list) {
1296 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1297
1298 for (; list; list = list->next) {
1299 unsigned int csum2;
1300 int end;
1301
1302 BUG_TRAP(start <= offset + len);
1303
1304 end = start + list->len;
1305 if ((copy = end - offset) > 0) {
1306 if (copy > len)
1307 copy = len;
1308 csum2 = skb_copy_and_csum_bits(list,
1309 offset - start,
1310 to, copy, 0);
1311 csum = csum_block_add(csum, csum2, pos);
1312 if ((len -= copy) == 0)
1313 return csum;
1314 offset += copy;
1315 to += copy;
1316 pos += copy;
1317 }
1318 start = end;
1319 }
1320 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001321 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return csum;
1323}
1324
1325void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1326{
1327 unsigned int csum;
1328 long csstart;
1329
1330 if (skb->ip_summed == CHECKSUM_HW)
1331 csstart = skb->h.raw - skb->data;
1332 else
1333 csstart = skb_headlen(skb);
1334
Kris Katterjohn09a62662006-01-08 22:24:28 -08001335 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 memcpy(to, skb->data, csstart);
1338
1339 csum = 0;
1340 if (csstart != skb->len)
1341 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1342 skb->len - csstart, 0);
1343
1344 if (skb->ip_summed == CHECKSUM_HW) {
1345 long csstuff = csstart + skb->csum;
1346
1347 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1348 }
1349}
1350
1351/**
1352 * skb_dequeue - remove from the head of the queue
1353 * @list: list to dequeue from
1354 *
1355 * Remove the head of the list. The list lock is taken so the function
1356 * may be used safely with other locking list functions. The head item is
1357 * returned or %NULL if the list is empty.
1358 */
1359
1360struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1361{
1362 unsigned long flags;
1363 struct sk_buff *result;
1364
1365 spin_lock_irqsave(&list->lock, flags);
1366 result = __skb_dequeue(list);
1367 spin_unlock_irqrestore(&list->lock, flags);
1368 return result;
1369}
1370
1371/**
1372 * skb_dequeue_tail - remove from the tail of the queue
1373 * @list: list to dequeue from
1374 *
1375 * Remove the tail of the list. The list lock is taken so the function
1376 * may be used safely with other locking list functions. The tail item is
1377 * returned or %NULL if the list is empty.
1378 */
1379struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1380{
1381 unsigned long flags;
1382 struct sk_buff *result;
1383
1384 spin_lock_irqsave(&list->lock, flags);
1385 result = __skb_dequeue_tail(list);
1386 spin_unlock_irqrestore(&list->lock, flags);
1387 return result;
1388}
1389
1390/**
1391 * skb_queue_purge - empty a list
1392 * @list: list to empty
1393 *
1394 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1395 * the list and one reference dropped. This function takes the list
1396 * lock and is atomic with respect to other list locking functions.
1397 */
1398void skb_queue_purge(struct sk_buff_head *list)
1399{
1400 struct sk_buff *skb;
1401 while ((skb = skb_dequeue(list)) != NULL)
1402 kfree_skb(skb);
1403}
1404
1405/**
1406 * skb_queue_head - queue a buffer at the list head
1407 * @list: list to use
1408 * @newsk: buffer to queue
1409 *
1410 * Queue a buffer at the start of the list. This function takes the
1411 * list lock and can be used safely with other locking &sk_buff functions
1412 * safely.
1413 *
1414 * A buffer cannot be placed on two lists at the same time.
1415 */
1416void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1417{
1418 unsigned long flags;
1419
1420 spin_lock_irqsave(&list->lock, flags);
1421 __skb_queue_head(list, newsk);
1422 spin_unlock_irqrestore(&list->lock, flags);
1423}
1424
1425/**
1426 * skb_queue_tail - queue a buffer at the list tail
1427 * @list: list to use
1428 * @newsk: buffer to queue
1429 *
1430 * Queue a buffer at the tail of the list. This function takes the
1431 * list lock and can be used safely with other locking &sk_buff functions
1432 * safely.
1433 *
1434 * A buffer cannot be placed on two lists at the same time.
1435 */
1436void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1437{
1438 unsigned long flags;
1439
1440 spin_lock_irqsave(&list->lock, flags);
1441 __skb_queue_tail(list, newsk);
1442 spin_unlock_irqrestore(&list->lock, flags);
1443}
David S. Miller8728b832005-08-09 19:25:21 -07001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/**
1446 * skb_unlink - remove a buffer from a list
1447 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001448 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 *
David S. Miller8728b832005-08-09 19:25:21 -07001450 * Remove a packet from a list. The list locks are taken and this
1451 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *
David S. Miller8728b832005-08-09 19:25:21 -07001453 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
David S. Miller8728b832005-08-09 19:25:21 -07001455void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
David S. Miller8728b832005-08-09 19:25:21 -07001457 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David S. Miller8728b832005-08-09 19:25:21 -07001459 spin_lock_irqsave(&list->lock, flags);
1460 __skb_unlink(skb, list);
1461 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/**
1465 * skb_append - append a buffer
1466 * @old: buffer to insert after
1467 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001468 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 * Place a packet after a given packet in a list. The list locks are taken
1471 * and this function is atomic with respect to other list locked calls.
1472 * A buffer cannot be placed on two lists at the same time.
1473 */
David S. Miller8728b832005-08-09 19:25:21 -07001474void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 unsigned long flags;
1477
David S. Miller8728b832005-08-09 19:25:21 -07001478 spin_lock_irqsave(&list->lock, flags);
1479 __skb_append(old, newsk, list);
1480 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483
1484/**
1485 * skb_insert - insert a buffer
1486 * @old: buffer to insert before
1487 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001488 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
David S. Miller8728b832005-08-09 19:25:21 -07001490 * Place a packet before a given packet in a list. The list locks are
1491 * taken and this function is atomic with respect to other list locked
1492 * calls.
1493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * A buffer cannot be placed on two lists at the same time.
1495 */
David S. Miller8728b832005-08-09 19:25:21 -07001496void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 unsigned long flags;
1499
David S. Miller8728b832005-08-09 19:25:21 -07001500 spin_lock_irqsave(&list->lock, flags);
1501 __skb_insert(newsk, old->prev, old, list);
1502 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505#if 0
1506/*
1507 * Tune the memory allocator for a new MTU size.
1508 */
1509void skb_add_mtu(int mtu)
1510{
1511 /* Must match allocation in alloc_skb */
1512 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1513
1514 kmem_add_cache_size(mtu);
1515}
1516#endif
1517
1518static inline void skb_split_inside_header(struct sk_buff *skb,
1519 struct sk_buff* skb1,
1520 const u32 len, const int pos)
1521{
1522 int i;
1523
1524 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1525
1526 /* And move data appendix as is. */
1527 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1528 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1529
1530 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1531 skb_shinfo(skb)->nr_frags = 0;
1532 skb1->data_len = skb->data_len;
1533 skb1->len += skb1->data_len;
1534 skb->data_len = 0;
1535 skb->len = len;
1536 skb->tail = skb->data + len;
1537}
1538
1539static inline void skb_split_no_header(struct sk_buff *skb,
1540 struct sk_buff* skb1,
1541 const u32 len, int pos)
1542{
1543 int i, k = 0;
1544 const int nfrags = skb_shinfo(skb)->nr_frags;
1545
1546 skb_shinfo(skb)->nr_frags = 0;
1547 skb1->len = skb1->data_len = skb->len - len;
1548 skb->len = len;
1549 skb->data_len = len - pos;
1550
1551 for (i = 0; i < nfrags; i++) {
1552 int size = skb_shinfo(skb)->frags[i].size;
1553
1554 if (pos + size > len) {
1555 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1556
1557 if (pos < len) {
1558 /* Split frag.
1559 * We have two variants in this case:
1560 * 1. Move all the frag to the second
1561 * part, if it is possible. F.e.
1562 * this approach is mandatory for TUX,
1563 * where splitting is expensive.
1564 * 2. Split is accurately. We make this.
1565 */
1566 get_page(skb_shinfo(skb)->frags[i].page);
1567 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1568 skb_shinfo(skb1)->frags[0].size -= len - pos;
1569 skb_shinfo(skb)->frags[i].size = len - pos;
1570 skb_shinfo(skb)->nr_frags++;
1571 }
1572 k++;
1573 } else
1574 skb_shinfo(skb)->nr_frags++;
1575 pos += size;
1576 }
1577 skb_shinfo(skb1)->nr_frags = k;
1578}
1579
1580/**
1581 * skb_split - Split fragmented skb to two parts at length len.
1582 * @skb: the buffer to split
1583 * @skb1: the buffer to receive the second part
1584 * @len: new length for skb
1585 */
1586void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1587{
1588 int pos = skb_headlen(skb);
1589
1590 if (len < pos) /* Split line is inside header. */
1591 skb_split_inside_header(skb, skb1, len, pos);
1592 else /* Second chunk has no header, nothing to copy. */
1593 skb_split_no_header(skb, skb1, len, pos);
1594}
1595
Thomas Graf677e90e2005-06-23 20:59:51 -07001596/**
1597 * skb_prepare_seq_read - Prepare a sequential read of skb data
1598 * @skb: the buffer to read
1599 * @from: lower offset of data to be read
1600 * @to: upper offset of data to be read
1601 * @st: state variable
1602 *
1603 * Initializes the specified state variable. Must be called before
1604 * invoking skb_seq_read() for the first time.
1605 */
1606void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1607 unsigned int to, struct skb_seq_state *st)
1608{
1609 st->lower_offset = from;
1610 st->upper_offset = to;
1611 st->root_skb = st->cur_skb = skb;
1612 st->frag_idx = st->stepped_offset = 0;
1613 st->frag_data = NULL;
1614}
1615
1616/**
1617 * skb_seq_read - Sequentially read skb data
1618 * @consumed: number of bytes consumed by the caller so far
1619 * @data: destination pointer for data to be returned
1620 * @st: state variable
1621 *
1622 * Reads a block of skb data at &consumed relative to the
1623 * lower offset specified to skb_prepare_seq_read(). Assigns
1624 * the head of the data block to &data and returns the length
1625 * of the block or 0 if the end of the skb data or the upper
1626 * offset has been reached.
1627 *
1628 * The caller is not required to consume all of the data
1629 * returned, i.e. &consumed is typically set to the number
1630 * of bytes already consumed and the next call to
1631 * skb_seq_read() will return the remaining part of the block.
1632 *
1633 * Note: The size of each block of data returned can be arbitary,
1634 * this limitation is the cost for zerocopy seqeuental
1635 * reads of potentially non linear data.
1636 *
1637 * Note: Fragment lists within fragments are not implemented
1638 * at the moment, state->root_skb could be replaced with
1639 * a stack for this purpose.
1640 */
1641unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1642 struct skb_seq_state *st)
1643{
1644 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1645 skb_frag_t *frag;
1646
1647 if (unlikely(abs_offset >= st->upper_offset))
1648 return 0;
1649
1650next_skb:
1651 block_limit = skb_headlen(st->cur_skb);
1652
1653 if (abs_offset < block_limit) {
1654 *data = st->cur_skb->data + abs_offset;
1655 return block_limit - abs_offset;
1656 }
1657
1658 if (st->frag_idx == 0 && !st->frag_data)
1659 st->stepped_offset += skb_headlen(st->cur_skb);
1660
1661 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1662 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1663 block_limit = frag->size + st->stepped_offset;
1664
1665 if (abs_offset < block_limit) {
1666 if (!st->frag_data)
1667 st->frag_data = kmap_skb_frag(frag);
1668
1669 *data = (u8 *) st->frag_data + frag->page_offset +
1670 (abs_offset - st->stepped_offset);
1671
1672 return block_limit - abs_offset;
1673 }
1674
1675 if (st->frag_data) {
1676 kunmap_skb_frag(st->frag_data);
1677 st->frag_data = NULL;
1678 }
1679
1680 st->frag_idx++;
1681 st->stepped_offset += frag->size;
1682 }
1683
1684 if (st->cur_skb->next) {
1685 st->cur_skb = st->cur_skb->next;
1686 st->frag_idx = 0;
1687 goto next_skb;
1688 } else if (st->root_skb == st->cur_skb &&
1689 skb_shinfo(st->root_skb)->frag_list) {
1690 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1691 goto next_skb;
1692 }
1693
1694 return 0;
1695}
1696
1697/**
1698 * skb_abort_seq_read - Abort a sequential read of skb data
1699 * @st: state variable
1700 *
1701 * Must be called if skb_seq_read() was not called until it
1702 * returned 0.
1703 */
1704void skb_abort_seq_read(struct skb_seq_state *st)
1705{
1706 if (st->frag_data)
1707 kunmap_skb_frag(st->frag_data);
1708}
1709
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001710#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1711
1712static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1713 struct ts_config *conf,
1714 struct ts_state *state)
1715{
1716 return skb_seq_read(offset, text, TS_SKB_CB(state));
1717}
1718
1719static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1720{
1721 skb_abort_seq_read(TS_SKB_CB(state));
1722}
1723
1724/**
1725 * skb_find_text - Find a text pattern in skb data
1726 * @skb: the buffer to look in
1727 * @from: search offset
1728 * @to: search limit
1729 * @config: textsearch configuration
1730 * @state: uninitialized textsearch state variable
1731 *
1732 * Finds a pattern in the skb data according to the specified
1733 * textsearch configuration. Use textsearch_next() to retrieve
1734 * subsequent occurrences of the pattern. Returns the offset
1735 * to the first occurrence or UINT_MAX if no match was found.
1736 */
1737unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1738 unsigned int to, struct ts_config *config,
1739 struct ts_state *state)
1740{
Phil Oesterf72b9482006-06-26 00:00:57 -07001741 unsigned int ret;
1742
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001743 config->get_next_block = skb_ts_get_next_block;
1744 config->finish = skb_ts_finish;
1745
1746 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1747
Phil Oesterf72b9482006-06-26 00:00:57 -07001748 ret = textsearch_find(config, state);
1749 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001750}
1751
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001752/**
1753 * skb_append_datato_frags: - append the user data to a skb
1754 * @sk: sock structure
1755 * @skb: skb structure to be appened with user data.
1756 * @getfrag: call back function to be used for getting the user data
1757 * @from: pointer to user message iov
1758 * @length: length of the iov message
1759 *
1760 * Description: This procedure append the user data in the fragment part
1761 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1762 */
1763int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001764 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001765 int len, int odd, struct sk_buff *skb),
1766 void *from, int length)
1767{
1768 int frg_cnt = 0;
1769 skb_frag_t *frag = NULL;
1770 struct page *page = NULL;
1771 int copy, left;
1772 int offset = 0;
1773 int ret;
1774
1775 do {
1776 /* Return error if we don't have space for new frag */
1777 frg_cnt = skb_shinfo(skb)->nr_frags;
1778 if (frg_cnt >= MAX_SKB_FRAGS)
1779 return -EFAULT;
1780
1781 /* allocate a new page for next frag */
1782 page = alloc_pages(sk->sk_allocation, 0);
1783
1784 /* If alloc_page fails just return failure and caller will
1785 * free previous allocated pages by doing kfree_skb()
1786 */
1787 if (page == NULL)
1788 return -ENOMEM;
1789
1790 /* initialize the next frag */
1791 sk->sk_sndmsg_page = page;
1792 sk->sk_sndmsg_off = 0;
1793 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1794 skb->truesize += PAGE_SIZE;
1795 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1796
1797 /* get the new initialized frag */
1798 frg_cnt = skb_shinfo(skb)->nr_frags;
1799 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1800
1801 /* copy the user data to page */
1802 left = PAGE_SIZE - frag->page_offset;
1803 copy = (length > left)? left : length;
1804
1805 ret = getfrag(from, (page_address(frag->page) +
1806 frag->page_offset + frag->size),
1807 offset, copy, 0, skb);
1808 if (ret < 0)
1809 return -EFAULT;
1810
1811 /* copy was successful so update the size parameters */
1812 sk->sk_sndmsg_off += copy;
1813 frag->size += copy;
1814 skb->len += copy;
1815 skb->data_len += copy;
1816 offset += copy;
1817 length -= copy;
1818
1819 } while (length > 0);
1820
1821 return 0;
1822}
1823
Herbert Xucbb042f2006-03-20 22:43:56 -08001824/**
1825 * skb_pull_rcsum - pull skb and update receive checksum
1826 * @skb: buffer to update
1827 * @start: start of data before pull
1828 * @len: length of data pulled
1829 *
1830 * This function performs an skb_pull on the packet and updates
1831 * update the CHECKSUM_HW checksum. It should be used on receive
1832 * path processing instead of skb_pull unless you know that the
1833 * checksum difference is zero (e.g., a valid IP header) or you
1834 * are setting ip_summed to CHECKSUM_NONE.
1835 */
1836unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1837{
1838 BUG_ON(len > skb->len);
1839 skb->len -= len;
1840 BUG_ON(skb->len < skb->data_len);
1841 skb_postpull_rcsum(skb, skb->data, len);
1842 return skb->data += len;
1843}
1844
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001845EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1846
Herbert Xuf4c50d92006-06-22 03:02:40 -07001847/**
1848 * skb_segment - Perform protocol segmentation on skb.
1849 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001850 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001851 *
1852 * This function performs segmentation on the given skb. It returns
1853 * the segment at the given position. It returns NULL if there are
1854 * no more segments to generate, or when an error is encountered.
1855 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001856struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001857{
1858 struct sk_buff *segs = NULL;
1859 struct sk_buff *tail = NULL;
1860 unsigned int mss = skb_shinfo(skb)->gso_size;
1861 unsigned int doffset = skb->data - skb->mac.raw;
1862 unsigned int offset = doffset;
1863 unsigned int headroom;
1864 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001865 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001866 int nfrags = skb_shinfo(skb)->nr_frags;
1867 int err = -ENOMEM;
1868 int i = 0;
1869 int pos;
1870
1871 __skb_push(skb, doffset);
1872 headroom = skb_headroom(skb);
1873 pos = skb_headlen(skb);
1874
1875 do {
1876 struct sk_buff *nskb;
1877 skb_frag_t *frag;
1878 int hsize, nsize;
1879 int k;
1880 int size;
1881
1882 len = skb->len - offset;
1883 if (len > mss)
1884 len = mss;
1885
1886 hsize = skb_headlen(skb) - offset;
1887 if (hsize < 0)
1888 hsize = 0;
1889 nsize = hsize + doffset;
1890 if (nsize > len + doffset || !sg)
1891 nsize = len + doffset;
1892
1893 nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
1894 if (unlikely(!nskb))
1895 goto err;
1896
1897 if (segs)
1898 tail->next = nskb;
1899 else
1900 segs = nskb;
1901 tail = nskb;
1902
1903 nskb->dev = skb->dev;
1904 nskb->priority = skb->priority;
1905 nskb->protocol = skb->protocol;
1906 nskb->dst = dst_clone(skb->dst);
1907 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1908 nskb->pkt_type = skb->pkt_type;
1909 nskb->mac_len = skb->mac_len;
1910
1911 skb_reserve(nskb, headroom);
1912 nskb->mac.raw = nskb->data;
1913 nskb->nh.raw = nskb->data + skb->mac_len;
1914 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1915 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1916
1917 if (!sg) {
1918 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1919 skb_put(nskb, len),
1920 len, 0);
1921 continue;
1922 }
1923
1924 frag = skb_shinfo(nskb)->frags;
1925 k = 0;
1926
1927 nskb->ip_summed = CHECKSUM_HW;
1928 nskb->csum = skb->csum;
1929 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1930
1931 while (pos < offset + len) {
1932 BUG_ON(i >= nfrags);
1933
1934 *frag = skb_shinfo(skb)->frags[i];
1935 get_page(frag->page);
1936 size = frag->size;
1937
1938 if (pos < offset) {
1939 frag->page_offset += offset - pos;
1940 frag->size -= offset - pos;
1941 }
1942
1943 k++;
1944
1945 if (pos + size <= offset + len) {
1946 i++;
1947 pos += size;
1948 } else {
1949 frag->size -= pos + size - (offset + len);
1950 break;
1951 }
1952
1953 frag++;
1954 }
1955
1956 skb_shinfo(nskb)->nr_frags = k;
1957 nskb->data_len = len - hsize;
1958 nskb->len += nskb->data_len;
1959 nskb->truesize += nskb->data_len;
1960 } while ((offset += len) < skb->len);
1961
1962 return segs;
1963
1964err:
1965 while ((skb = segs)) {
1966 segs = skb->next;
1967 kfree(skb);
1968 }
1969 return ERR_PTR(err);
1970}
1971
1972EXPORT_SYMBOL_GPL(skb_segment);
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974void __init skb_init(void)
1975{
1976 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1977 sizeof(struct sk_buff),
1978 0,
1979 SLAB_HWCACHE_ALIGN,
1980 NULL, NULL);
1981 if (!skbuff_head_cache)
1982 panic("cannot create skbuff cache");
David S. Millerd179cd12005-08-17 14:57:30 -07001983
1984 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1985 (2*sizeof(struct sk_buff)) +
1986 sizeof(atomic_t),
1987 0,
1988 SLAB_HWCACHE_ALIGN,
1989 NULL, NULL);
1990 if (!skbuff_fclone_cache)
1991 panic("cannot create skbuff cache");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994EXPORT_SYMBOL(___pskb_trim);
1995EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08001996EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07001998EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999EXPORT_SYMBOL(pskb_copy);
2000EXPORT_SYMBOL(pskb_expand_head);
2001EXPORT_SYMBOL(skb_checksum);
2002EXPORT_SYMBOL(skb_clone);
2003EXPORT_SYMBOL(skb_clone_fraglist);
2004EXPORT_SYMBOL(skb_copy);
2005EXPORT_SYMBOL(skb_copy_and_csum_bits);
2006EXPORT_SYMBOL(skb_copy_and_csum_dev);
2007EXPORT_SYMBOL(skb_copy_bits);
2008EXPORT_SYMBOL(skb_copy_expand);
2009EXPORT_SYMBOL(skb_over_panic);
2010EXPORT_SYMBOL(skb_pad);
2011EXPORT_SYMBOL(skb_realloc_headroom);
2012EXPORT_SYMBOL(skb_under_panic);
2013EXPORT_SYMBOL(skb_dequeue);
2014EXPORT_SYMBOL(skb_dequeue_tail);
2015EXPORT_SYMBOL(skb_insert);
2016EXPORT_SYMBOL(skb_queue_purge);
2017EXPORT_SYMBOL(skb_queue_head);
2018EXPORT_SYMBOL(skb_queue_tail);
2019EXPORT_SYMBOL(skb_unlink);
2020EXPORT_SYMBOL(skb_append);
2021EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002022EXPORT_SYMBOL(skb_prepare_seq_read);
2023EXPORT_SYMBOL(skb_seq_read);
2024EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002025EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002026EXPORT_SYMBOL(skb_append_datato_frags);