blob: 820761f9eeef92df8929a1517e04e81779148d2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
49#include <linux/netdevice.h>
50#ifdef CONFIG_NET_CLS_ACT
51#include <net/pkt_sched.h>
52#endif
53#include <linux/string.h>
54#include <linux/skbuff.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <net/protocol.h>
60#include <net/dst.h>
61#include <net/sock.h>
62#include <net/checksum.h>
63#include <net/xfrm.h>
64
65#include <asm/uaccess.h>
66#include <asm/system.h>
67
Al Viroa1f8e7f72006-10-19 16:08:53 -040068#include "kmap_skb.h"
69
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *skbuff_head_cache __read_mostly;
71static struct kmem_cache *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 "
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090090 "data:%p tail:%p end:%p dev:%s\n",
Patrick McHardy26095452005-04-21 16:43:02 -070091 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 "
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900108 "data:%p tail:%p end:%p dev:%s\n",
Patrick McHardy26095452005-04-21 16:43:02 -0700109 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
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800134 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
Al Virodd0fc662005-10-07 07:46:04 +0100143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800144 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Christoph Lametere18b8902006-12-06 20:33:20 -0800146 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800147 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct sk_buff *skb;
149 u8 *data;
150
Herbert Xu8798b3f2006-01-23 16:32:45 -0800151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800154 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800160 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
161 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!data)
163 goto nodata;
164
165 memset(skb, 0, offsetof(struct sk_buff, truesize));
166 skb->truesize = size + sizeof(struct sk_buff);
167 atomic_set(&skb->users, 1);
168 skb->head = data;
169 skb->data = data;
170 skb->tail = data;
171 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800172 /* make sure we initialize shinfo sequentially */
173 shinfo = skb_shinfo(skb);
174 atomic_set(&shinfo->dataref, 1);
175 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700176 shinfo->gso_size = 0;
177 shinfo->gso_segs = 0;
178 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800179 shinfo->ip6_frag_id = 0;
180 shinfo->frag_list = NULL;
181
David S. Millerd179cd12005-08-17 14:57:30 -0700182 if (fclone) {
183 struct sk_buff *child = skb + 1;
184 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David S. Millerd179cd12005-08-17 14:57:30 -0700186 skb->fclone = SKB_FCLONE_ORIG;
187 atomic_set(fclone_ref, 1);
188
189 child->fclone = SKB_FCLONE_UNAVAILABLE;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191out:
192 return skb;
193nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800194 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 skb = NULL;
196 goto out;
197}
198
199/**
200 * alloc_skb_from_cache - allocate a network buffer
201 * @cp: kmem_cache from which to allocate the data area
202 * (object size must be big enough for @size bytes + skb overheads)
203 * @size: size to allocate
204 * @gfp_mask: allocation mask
205 *
206 * Allocate a new &sk_buff. The returned buffer has no headroom and
207 * tail room of size bytes. The object has a reference count of one.
208 * The return is the buffer. On a failure the return is %NULL.
209 *
210 * Buffers may only be allocated from interrupts using a @gfp_mask of
211 * %GFP_ATOMIC.
212 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800213struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700214 unsigned int size,
Al Virodd0fc662005-10-07 07:46:04 +0100215 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct sk_buff *skb;
218 u8 *data;
219
220 /* Get the HEAD */
221 skb = kmem_cache_alloc(skbuff_head_cache,
222 gfp_mask & ~__GFP_DMA);
223 if (!skb)
224 goto out;
225
226 /* Get the DATA. */
227 size = SKB_DATA_ALIGN(size);
228 data = kmem_cache_alloc(cp, gfp_mask);
229 if (!data)
230 goto nodata;
231
232 memset(skb, 0, offsetof(struct sk_buff, truesize));
233 skb->truesize = size + sizeof(struct sk_buff);
234 atomic_set(&skb->users, 1);
235 skb->head = data;
236 skb->data = data;
237 skb->tail = data;
238 skb->end = data + size;
239
240 atomic_set(&(skb_shinfo(skb)->dataref), 1);
241 skb_shinfo(skb)->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700242 skb_shinfo(skb)->gso_size = 0;
243 skb_shinfo(skb)->gso_segs = 0;
244 skb_shinfo(skb)->gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 skb_shinfo(skb)->frag_list = NULL;
246out:
247 return skb;
248nodata:
249 kmem_cache_free(skbuff_head_cache, skb);
250 skb = NULL;
251 goto out;
252}
253
Christoph Hellwig8af27452006-07-31 22:35:23 -0700254/**
255 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
256 * @dev: network device to receive on
257 * @length: length to allocate
258 * @gfp_mask: get_free_pages mask, passed to alloc_skb
259 *
260 * Allocate a new &sk_buff and assign it a usage count of one. The
261 * buffer has unspecified headroom built in. Users should allocate
262 * the headroom they think they need without accounting for the
263 * built in space. The built in space is used for optimisations.
264 *
265 * %NULL is returned if there is no free memory.
266 */
267struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
268 unsigned int length, gfp_t gfp_mask)
269{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700270 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700271 struct sk_buff *skb;
272
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900273 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700274 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700275 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700276 skb->dev = dev;
277 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700278 return skb;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Herbert Xu27b437c2006-07-13 19:26:39 -0700281static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Herbert Xu27b437c2006-07-13 19:26:39 -0700283 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Herbert Xu27b437c2006-07-13 19:26:39 -0700285 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 do {
288 struct sk_buff *this = list;
289 list = list->next;
290 kfree_skb(this);
291 } while (list);
292}
293
Herbert Xu27b437c2006-07-13 19:26:39 -0700294static inline void skb_drop_fraglist(struct sk_buff *skb)
295{
296 skb_drop_list(&skb_shinfo(skb)->frag_list);
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static void skb_clone_fraglist(struct sk_buff *skb)
300{
301 struct sk_buff *list;
302
303 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
304 skb_get(list);
305}
306
Adrian Bunk5bba1712006-06-29 13:02:35 -0700307static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 if (!skb->cloned ||
310 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
311 &skb_shinfo(skb)->dataref)) {
312 if (skb_shinfo(skb)->nr_frags) {
313 int i;
314 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
315 put_page(skb_shinfo(skb)->frags[i].page);
316 }
317
318 if (skb_shinfo(skb)->frag_list)
319 skb_drop_fraglist(skb);
320
321 kfree(skb->head);
322 }
323}
324
325/*
326 * Free an skbuff by memory without cleaning the state.
327 */
328void kfree_skbmem(struct sk_buff *skb)
329{
David S. Millerd179cd12005-08-17 14:57:30 -0700330 struct sk_buff *other;
331 atomic_t *fclone_ref;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700334 switch (skb->fclone) {
335 case SKB_FCLONE_UNAVAILABLE:
336 kmem_cache_free(skbuff_head_cache, skb);
337 break;
338
339 case SKB_FCLONE_ORIG:
340 fclone_ref = (atomic_t *) (skb + 2);
341 if (atomic_dec_and_test(fclone_ref))
342 kmem_cache_free(skbuff_fclone_cache, skb);
343 break;
344
345 case SKB_FCLONE_CLONE:
346 fclone_ref = (atomic_t *) (skb + 1);
347 other = skb - 1;
348
349 /* The clone portion is available for
350 * fast-cloning again.
351 */
352 skb->fclone = SKB_FCLONE_UNAVAILABLE;
353
354 if (atomic_dec_and_test(fclone_ref))
355 kmem_cache_free(skbuff_fclone_cache, other);
356 break;
357 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360/**
361 * __kfree_skb - private function
362 * @skb: buffer
363 *
364 * Free an sk_buff. Release anything attached to the buffer.
365 * Clean the state. This is an internal helper function. Users should
366 * always call kfree_skb
367 */
368
369void __kfree_skb(struct sk_buff *skb)
370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dst_release(skb->dst);
372#ifdef CONFIG_XFRM
373 secpath_put(skb->sp);
374#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700375 if (skb->destructor) {
376 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 skb->destructor(skb);
378 }
379#ifdef CONFIG_NETFILTER
380 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800381#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
382 nf_conntrack_put_reasm(skb->nfct_reasm);
383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#ifdef CONFIG_BRIDGE_NETFILTER
385 nf_bridge_put(skb->nf_bridge);
386#endif
387#endif
388/* XXX: IS this still necessary? - JHS */
389#ifdef CONFIG_NET_SCHED
390 skb->tc_index = 0;
391#ifdef CONFIG_NET_CLS_ACT
392 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
394#endif
395
396 kfree_skbmem(skb);
397}
398
399/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800400 * kfree_skb - free an sk_buff
401 * @skb: buffer to free
402 *
403 * Drop a reference to the buffer and free it if the usage count has
404 * hit zero.
405 */
406void kfree_skb(struct sk_buff *skb)
407{
408 if (unlikely(!skb))
409 return;
410 if (likely(atomic_read(&skb->users) == 1))
411 smp_rmb();
412 else if (likely(!atomic_dec_and_test(&skb->users)))
413 return;
414 __kfree_skb(skb);
415}
416
417/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * skb_clone - duplicate an sk_buff
419 * @skb: buffer to clone
420 * @gfp_mask: allocation priority
421 *
422 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
423 * copies share the same packet data but not structure. The new
424 * buffer has a reference count of 1. If the allocation fails the
425 * function returns %NULL otherwise the new buffer is returned.
426 *
427 * If this function is called from an interrupt gfp_mask() must be
428 * %GFP_ATOMIC.
429 */
430
Al Virodd0fc662005-10-07 07:46:04 +0100431struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
David S. Millerd179cd12005-08-17 14:57:30 -0700433 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
David S. Millerd179cd12005-08-17 14:57:30 -0700435 n = skb + 1;
436 if (skb->fclone == SKB_FCLONE_ORIG &&
437 n->fclone == SKB_FCLONE_UNAVAILABLE) {
438 atomic_t *fclone_ref = (atomic_t *) (n + 1);
439 n->fclone = SKB_FCLONE_CLONE;
440 atomic_inc(fclone_ref);
441 } else {
442 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
443 if (!n)
444 return NULL;
445 n->fclone = SKB_FCLONE_UNAVAILABLE;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448#define C(x) n->x = skb->x
449
450 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700452 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 C(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 C(h);
455 C(nh);
456 C(mac);
457 C(dst);
458 dst_clone(skb->dst);
459 C(sp);
460#ifdef CONFIG_INET
461 secpath_get(skb->sp);
462#endif
463 memcpy(n->cb, skb->cb, sizeof(skb->cb));
464 C(len);
465 C(data_len);
466 C(csum);
467 C(local_df);
468 n->cloned = 1;
469 n->nohdr = 0;
470 C(pkt_type);
471 C(ip_summed);
472 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800473#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
474 C(ipvs_property);
475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800478 C(mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#ifdef CONFIG_NETFILTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 C(nfct);
481 nf_conntrack_get(skb->nfct);
482 C(nfctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800483#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
484 C(nfct_reasm);
485 nf_conntrack_get_reasm(skb->nfct_reasm);
486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#ifdef CONFIG_BRIDGE_NETFILTER
488 C(nf_bridge);
489 nf_bridge_get(skb->nf_bridge);
490#endif
491#endif /*CONFIG_NETFILTER*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#ifdef CONFIG_NET_SCHED
493 C(tc_index);
494#ifdef CONFIG_NET_CLS_ACT
495 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700496 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
497 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 C(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#endif
James Morris984bc162006-06-09 00:29:17 -0700500 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501#endif
502 C(truesize);
503 atomic_set(&n->users, 1);
504 C(head);
505 C(data);
506 C(tail);
507 C(end);
508
509 atomic_inc(&(skb_shinfo(skb)->dataref));
510 skb->cloned = 1;
511
512 return n;
513}
514
515static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
516{
517 /*
518 * Shift between the two data areas in bytes
519 */
520 unsigned long offset = new->data - old->data;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 new->sk = NULL;
523 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 new->priority = old->priority;
525 new->protocol = old->protocol;
526 new->dst = dst_clone(old->dst);
527#ifdef CONFIG_INET
528 new->sp = secpath_get(old->sp);
529#endif
530 new->h.raw = old->h.raw + offset;
531 new->nh.raw = old->nh.raw + offset;
532 new->mac.raw = old->mac.raw + offset;
533 memcpy(new->cb, old->cb, sizeof(old->cb));
534 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700535 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700537 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800539 new->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#ifdef CONFIG_NETFILTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 new->nfct = old->nfct;
542 nf_conntrack_get(old->nfct);
543 new->nfctinfo = old->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800544#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
545 new->nfct_reasm = old->nfct_reasm;
546 nf_conntrack_get_reasm(old->nfct_reasm);
547#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300548#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
549 new->ipvs_property = old->ipvs_property;
550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef CONFIG_BRIDGE_NETFILTER
552 new->nf_bridge = old->nf_bridge;
553 nf_bridge_get(old->nf_bridge);
554#endif
555#endif
556#ifdef CONFIG_NET_SCHED
557#ifdef CONFIG_NET_CLS_ACT
558 new->tc_verd = old->tc_verd;
559#endif
560 new->tc_index = old->tc_index;
561#endif
James Morris984bc162006-06-09 00:29:17 -0700562 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700564 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
565 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
566 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/**
570 * skb_copy - create private copy of an sk_buff
571 * @skb: buffer to copy
572 * @gfp_mask: allocation priority
573 *
574 * Make a copy of both an &sk_buff and its data. This is used when the
575 * caller wishes to modify the data and needs a private copy of the
576 * data to alter. Returns %NULL on failure or the pointer to the buffer
577 * on success. The returned buffer has a reference count of 1.
578 *
579 * As by-product this function converts non-linear &sk_buff to linear
580 * one, so that &sk_buff becomes completely private and caller is allowed
581 * to modify all the data of returned buffer. This means that this
582 * function is not recommended for use in circumstances when only
583 * header is going to be modified. Use pskb_copy() instead.
584 */
585
Al Virodd0fc662005-10-07 07:46:04 +0100586struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int headerlen = skb->data - skb->head;
589 /*
590 * Allocate the copy buffer
591 */
592 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
593 gfp_mask);
594 if (!n)
595 return NULL;
596
597 /* Set the data pointer */
598 skb_reserve(n, headerlen);
599 /* Set the tail pointer and length */
600 skb_put(n, skb->len);
601 n->csum = skb->csum;
602 n->ip_summed = skb->ip_summed;
603
604 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
605 BUG();
606
607 copy_skb_header(n, skb);
608 return n;
609}
610
611
612/**
613 * pskb_copy - create copy of an sk_buff with private head.
614 * @skb: buffer to copy
615 * @gfp_mask: allocation priority
616 *
617 * Make a copy of both an &sk_buff and part of its data, located
618 * in header. Fragmented data remain shared. This is used when
619 * the caller wishes to modify only header of &sk_buff and needs
620 * private copy of the header to alter. Returns %NULL on failure
621 * or the pointer to the buffer on success.
622 * The returned buffer has a reference count of 1.
623 */
624
Al Virodd0fc662005-10-07 07:46:04 +0100625struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 /*
628 * Allocate the copy buffer
629 */
630 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
631
632 if (!n)
633 goto out;
634
635 /* Set the data pointer */
636 skb_reserve(n, skb->data - skb->head);
637 /* Set the tail pointer and length */
638 skb_put(n, skb_headlen(skb));
639 /* Copy the bytes */
640 memcpy(n->data, skb->data, n->len);
641 n->csum = skb->csum;
642 n->ip_summed = skb->ip_summed;
643
Herbert Xu25f484a2006-11-07 14:57:15 -0800644 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 n->data_len = skb->data_len;
646 n->len = skb->len;
647
648 if (skb_shinfo(skb)->nr_frags) {
649 int i;
650
651 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
652 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
653 get_page(skb_shinfo(n)->frags[i].page);
654 }
655 skb_shinfo(n)->nr_frags = i;
656 }
657
658 if (skb_shinfo(skb)->frag_list) {
659 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
660 skb_clone_fraglist(n);
661 }
662
663 copy_skb_header(n, skb);
664out:
665 return n;
666}
667
668/**
669 * pskb_expand_head - reallocate header of &sk_buff
670 * @skb: buffer to reallocate
671 * @nhead: room to add at head
672 * @ntail: room to add at tail
673 * @gfp_mask: allocation priority
674 *
675 * Expands (or creates identical copy, if &nhead and &ntail are zero)
676 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
677 * reference count of 1. Returns zero in the case of success or error,
678 * if expansion failed. In the last case, &sk_buff is not changed.
679 *
680 * All the pointers pointing into skb header may change and must be
681 * reloaded after call to this function.
682 */
683
Victor Fusco86a76ca2005-07-08 14:57:47 -0700684int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100685 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int i;
688 u8 *data;
689 int size = nhead + (skb->end - skb->head) + ntail;
690 long off;
691
692 if (skb_shared(skb))
693 BUG();
694
695 size = SKB_DATA_ALIGN(size);
696
697 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
698 if (!data)
699 goto nodata;
700
701 /* Copy only real data... and, alas, header. This should be
702 * optimized for the cases when header is void. */
703 memcpy(data + nhead, skb->head, skb->tail - skb->head);
704 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
705
706 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
707 get_page(skb_shinfo(skb)->frags[i].page);
708
709 if (skb_shinfo(skb)->frag_list)
710 skb_clone_fraglist(skb);
711
712 skb_release_data(skb);
713
714 off = (data + nhead) - skb->head;
715
716 skb->head = data;
717 skb->end = data + size;
718 skb->data += off;
719 skb->tail += off;
720 skb->mac.raw += off;
721 skb->h.raw += off;
722 skb->nh.raw += off;
723 skb->cloned = 0;
724 skb->nohdr = 0;
725 atomic_set(&skb_shinfo(skb)->dataref, 1);
726 return 0;
727
728nodata:
729 return -ENOMEM;
730}
731
732/* Make private copy of skb with writable head and some headroom */
733
734struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
735{
736 struct sk_buff *skb2;
737 int delta = headroom - skb_headroom(skb);
738
739 if (delta <= 0)
740 skb2 = pskb_copy(skb, GFP_ATOMIC);
741 else {
742 skb2 = skb_clone(skb, GFP_ATOMIC);
743 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
744 GFP_ATOMIC)) {
745 kfree_skb(skb2);
746 skb2 = NULL;
747 }
748 }
749 return skb2;
750}
751
752
753/**
754 * skb_copy_expand - copy and expand sk_buff
755 * @skb: buffer to copy
756 * @newheadroom: new free bytes at head
757 * @newtailroom: new free bytes at tail
758 * @gfp_mask: allocation priority
759 *
760 * Make a copy of both an &sk_buff and its data and while doing so
761 * allocate additional space.
762 *
763 * This is used when the caller wishes to modify the data and needs a
764 * private copy of the data to alter as well as more space for new fields.
765 * Returns %NULL on failure or the pointer to the buffer
766 * on success. The returned buffer has a reference count of 1.
767 *
768 * You must pass %GFP_ATOMIC as the allocation priority if this function
769 * is called from an interrupt.
770 *
771 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
772 * only by netfilter in the cases when checksum is recalculated? --ANK
773 */
774struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700775 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100776 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 /*
779 * Allocate the copy buffer
780 */
781 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
782 gfp_mask);
783 int head_copy_len, head_copy_off;
784
785 if (!n)
786 return NULL;
787
788 skb_reserve(n, newheadroom);
789
790 /* Set the tail pointer and length */
791 skb_put(n, skb->len);
792
793 head_copy_len = skb_headroom(skb);
794 head_copy_off = 0;
795 if (newheadroom <= head_copy_len)
796 head_copy_len = newheadroom;
797 else
798 head_copy_off = newheadroom - head_copy_len;
799
800 /* Copy the linear header and data. */
801 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
802 skb->len + head_copy_len))
803 BUG();
804
805 copy_skb_header(n, skb);
806
807 return n;
808}
809
810/**
811 * skb_pad - zero pad the tail of an skb
812 * @skb: buffer to pad
813 * @pad: space to pad
814 *
815 * Ensure that a buffer is followed by a padding area that is zero
816 * filled. Used by network drivers which may DMA or transfer data
817 * beyond the buffer end onto the wire.
818 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700819 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900821
Herbert Xu5b057c62006-06-23 02:06:41 -0700822int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Herbert Xu5b057c62006-06-23 02:06:41 -0700824 int err;
825 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700828 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700832
833 ntail = skb->data_len + pad - (skb->end - skb->tail);
834 if (likely(skb_cloned(skb) || ntail > 0)) {
835 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
836 if (unlikely(err))
837 goto free_skb;
838 }
839
840 /* FIXME: The use of this function with non-linear skb's really needs
841 * to be audited.
842 */
843 err = skb_linearize(skb);
844 if (unlikely(err))
845 goto free_skb;
846
847 memset(skb->data + skb->len, 0, pad);
848 return 0;
849
850free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700852 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900853}
854
Herbert Xu3cc0e872006-06-09 16:13:38 -0700855/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857
Herbert Xu3cc0e872006-06-09 16:13:38 -0700858int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Herbert Xu27b437c2006-07-13 19:26:39 -0700860 struct sk_buff **fragp;
861 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int offset = skb_headlen(skb);
863 int nfrags = skb_shinfo(skb)->nr_frags;
864 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700865 int err;
866
867 if (skb_cloned(skb) &&
868 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
869 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700871 i = 0;
872 if (offset >= len)
873 goto drop_pages;
874
875 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700877
878 if (end < len) {
879 offset = end;
880 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700882
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700883 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700884
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700885drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700886 skb_shinfo(skb)->nr_frags = i;
887
888 for (; i < nfrags; i++)
889 put_page(skb_shinfo(skb)->frags[i].page);
890
891 if (skb_shinfo(skb)->frag_list)
892 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700893 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Herbert Xu27b437c2006-07-13 19:26:39 -0700896 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
897 fragp = &frag->next) {
898 int end = offset + frag->len;
899
900 if (skb_shared(frag)) {
901 struct sk_buff *nfrag;
902
903 nfrag = skb_clone(frag, GFP_ATOMIC);
904 if (unlikely(!nfrag))
905 return -ENOMEM;
906
907 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700908 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700909 frag = nfrag;
910 *fragp = frag;
911 }
912
913 if (end < len) {
914 offset = end;
915 continue;
916 }
917
918 if (end > len &&
919 unlikely((err = pskb_trim(frag, len - offset))))
920 return err;
921
922 if (frag->next)
923 skb_drop_list(&frag->next);
924 break;
925 }
926
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700927done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700928 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 skb->data_len -= skb->len - len;
930 skb->len = len;
931 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700932 skb->len = len;
933 skb->data_len = 0;
934 skb->tail = skb->data + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 return 0;
938}
939
940/**
941 * __pskb_pull_tail - advance tail of skb header
942 * @skb: buffer to reallocate
943 * @delta: number of bytes to advance tail
944 *
945 * The function makes a sense only on a fragmented &sk_buff,
946 * it expands header moving its tail forward and copying necessary
947 * data from fragmented part.
948 *
949 * &sk_buff MUST have reference count of 1.
950 *
951 * Returns %NULL (and &sk_buff does not change) if pull failed
952 * or value of new tail of skb in the case of success.
953 *
954 * All the pointers pointing into skb header may change and must be
955 * reloaded after call to this function.
956 */
957
958/* Moves tail of skb head forward, copying data from fragmented part,
959 * when it is necessary.
960 * 1. It may fail due to malloc failure.
961 * 2. It may change skb pointers.
962 *
963 * It is pretty complicated. Luckily, it is called only in exceptional cases.
964 */
965unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
966{
967 /* If skb has not enough free space at tail, get new one
968 * plus 128 bytes for future expansions. If we have enough
969 * room at tail, reallocate without expansion only if skb is cloned.
970 */
971 int i, k, eat = (skb->tail + delta) - skb->end;
972
973 if (eat > 0 || skb_cloned(skb)) {
974 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
975 GFP_ATOMIC))
976 return NULL;
977 }
978
979 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
980 BUG();
981
982 /* Optimization: no fragments, no reasons to preestimate
983 * size of pulled pages. Superb.
984 */
985 if (!skb_shinfo(skb)->frag_list)
986 goto pull_pages;
987
988 /* Estimate size of pulled pages. */
989 eat = delta;
990 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
991 if (skb_shinfo(skb)->frags[i].size >= eat)
992 goto pull_pages;
993 eat -= skb_shinfo(skb)->frags[i].size;
994 }
995
996 /* If we need update frag list, we are in troubles.
997 * Certainly, it possible to add an offset to skb data,
998 * but taking into account that pulling is expected to
999 * be very rare operation, it is worth to fight against
1000 * further bloating skb head and crucify ourselves here instead.
1001 * Pure masohism, indeed. 8)8)
1002 */
1003 if (eat) {
1004 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1005 struct sk_buff *clone = NULL;
1006 struct sk_buff *insp = NULL;
1007
1008 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001009 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (list->len <= eat) {
1012 /* Eaten as whole. */
1013 eat -= list->len;
1014 list = list->next;
1015 insp = list;
1016 } else {
1017 /* Eaten partially. */
1018
1019 if (skb_shared(list)) {
1020 /* Sucks! We need to fork list. :-( */
1021 clone = skb_clone(list, GFP_ATOMIC);
1022 if (!clone)
1023 return NULL;
1024 insp = list->next;
1025 list = clone;
1026 } else {
1027 /* This may be pulled without
1028 * problems. */
1029 insp = list;
1030 }
1031 if (!pskb_pull(list, eat)) {
1032 if (clone)
1033 kfree_skb(clone);
1034 return NULL;
1035 }
1036 break;
1037 }
1038 } while (eat);
1039
1040 /* Free pulled out fragments. */
1041 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1042 skb_shinfo(skb)->frag_list = list->next;
1043 kfree_skb(list);
1044 }
1045 /* And insert new clone at head. */
1046 if (clone) {
1047 clone->next = list;
1048 skb_shinfo(skb)->frag_list = clone;
1049 }
1050 }
1051 /* Success! Now we may commit changes to skb data. */
1052
1053pull_pages:
1054 eat = delta;
1055 k = 0;
1056 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1057 if (skb_shinfo(skb)->frags[i].size <= eat) {
1058 put_page(skb_shinfo(skb)->frags[i].page);
1059 eat -= skb_shinfo(skb)->frags[i].size;
1060 } else {
1061 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1062 if (eat) {
1063 skb_shinfo(skb)->frags[k].page_offset += eat;
1064 skb_shinfo(skb)->frags[k].size -= eat;
1065 eat = 0;
1066 }
1067 k++;
1068 }
1069 }
1070 skb_shinfo(skb)->nr_frags = k;
1071
1072 skb->tail += delta;
1073 skb->data_len -= delta;
1074
1075 return skb->tail;
1076}
1077
1078/* Copy some data bits from skb to kernel buffer. */
1079
1080int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1081{
1082 int i, copy;
1083 int start = skb_headlen(skb);
1084
1085 if (offset > (int)skb->len - len)
1086 goto fault;
1087
1088 /* Copy header. */
1089 if ((copy = start - offset) > 0) {
1090 if (copy > len)
1091 copy = len;
1092 memcpy(to, skb->data + offset, copy);
1093 if ((len -= copy) == 0)
1094 return 0;
1095 offset += copy;
1096 to += copy;
1097 }
1098
1099 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1100 int end;
1101
1102 BUG_TRAP(start <= offset + len);
1103
1104 end = start + skb_shinfo(skb)->frags[i].size;
1105 if ((copy = end - offset) > 0) {
1106 u8 *vaddr;
1107
1108 if (copy > len)
1109 copy = len;
1110
1111 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1112 memcpy(to,
1113 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1114 offset - start, copy);
1115 kunmap_skb_frag(vaddr);
1116
1117 if ((len -= copy) == 0)
1118 return 0;
1119 offset += copy;
1120 to += copy;
1121 }
1122 start = end;
1123 }
1124
1125 if (skb_shinfo(skb)->frag_list) {
1126 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1127
1128 for (; list; list = list->next) {
1129 int end;
1130
1131 BUG_TRAP(start <= offset + len);
1132
1133 end = start + list->len;
1134 if ((copy = end - offset) > 0) {
1135 if (copy > len)
1136 copy = len;
1137 if (skb_copy_bits(list, offset - start,
1138 to, copy))
1139 goto fault;
1140 if ((len -= copy) == 0)
1141 return 0;
1142 offset += copy;
1143 to += copy;
1144 }
1145 start = end;
1146 }
1147 }
1148 if (!len)
1149 return 0;
1150
1151fault:
1152 return -EFAULT;
1153}
1154
Herbert Xu357b40a2005-04-19 22:30:14 -07001155/**
1156 * skb_store_bits - store bits from kernel buffer to skb
1157 * @skb: destination buffer
1158 * @offset: offset in destination
1159 * @from: source buffer
1160 * @len: number of bytes to copy
1161 *
1162 * Copy the specified number of bytes from the source buffer to the
1163 * destination skb. This function handles all the messy bits of
1164 * traversing fragment lists and such.
1165 */
1166
1167int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1168{
1169 int i, copy;
1170 int start = skb_headlen(skb);
1171
1172 if (offset > (int)skb->len - len)
1173 goto fault;
1174
1175 if ((copy = start - offset) > 0) {
1176 if (copy > len)
1177 copy = len;
1178 memcpy(skb->data + offset, from, copy);
1179 if ((len -= copy) == 0)
1180 return 0;
1181 offset += copy;
1182 from += copy;
1183 }
1184
1185 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1186 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1187 int end;
1188
1189 BUG_TRAP(start <= offset + len);
1190
1191 end = start + frag->size;
1192 if ((copy = end - offset) > 0) {
1193 u8 *vaddr;
1194
1195 if (copy > len)
1196 copy = len;
1197
1198 vaddr = kmap_skb_frag(frag);
1199 memcpy(vaddr + frag->page_offset + offset - start,
1200 from, copy);
1201 kunmap_skb_frag(vaddr);
1202
1203 if ((len -= copy) == 0)
1204 return 0;
1205 offset += copy;
1206 from += copy;
1207 }
1208 start = end;
1209 }
1210
1211 if (skb_shinfo(skb)->frag_list) {
1212 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1213
1214 for (; list; list = list->next) {
1215 int end;
1216
1217 BUG_TRAP(start <= offset + len);
1218
1219 end = start + list->len;
1220 if ((copy = end - offset) > 0) {
1221 if (copy > len)
1222 copy = len;
1223 if (skb_store_bits(list, offset - start,
1224 from, copy))
1225 goto fault;
1226 if ((len -= copy) == 0)
1227 return 0;
1228 offset += copy;
1229 from += copy;
1230 }
1231 start = end;
1232 }
1233 }
1234 if (!len)
1235 return 0;
1236
1237fault:
1238 return -EFAULT;
1239}
1240
1241EXPORT_SYMBOL(skb_store_bits);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/* Checksum skb data. */
1244
Al Viro2bbbc862006-11-14 21:37:14 -08001245__wsum skb_checksum(const struct sk_buff *skb, int offset,
1246 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 int start = skb_headlen(skb);
1249 int i, copy = start - offset;
1250 int pos = 0;
1251
1252 /* Checksum header. */
1253 if (copy > 0) {
1254 if (copy > len)
1255 copy = len;
1256 csum = csum_partial(skb->data + offset, copy, csum);
1257 if ((len -= copy) == 0)
1258 return csum;
1259 offset += copy;
1260 pos = copy;
1261 }
1262
1263 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1264 int end;
1265
1266 BUG_TRAP(start <= offset + len);
1267
1268 end = start + skb_shinfo(skb)->frags[i].size;
1269 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001270 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 u8 *vaddr;
1272 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1273
1274 if (copy > len)
1275 copy = len;
1276 vaddr = kmap_skb_frag(frag);
1277 csum2 = csum_partial(vaddr + frag->page_offset +
1278 offset - start, copy, 0);
1279 kunmap_skb_frag(vaddr);
1280 csum = csum_block_add(csum, csum2, pos);
1281 if (!(len -= copy))
1282 return csum;
1283 offset += copy;
1284 pos += copy;
1285 }
1286 start = end;
1287 }
1288
1289 if (skb_shinfo(skb)->frag_list) {
1290 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1291
1292 for (; list; list = list->next) {
1293 int end;
1294
1295 BUG_TRAP(start <= offset + len);
1296
1297 end = start + list->len;
1298 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001299 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (copy > len)
1301 copy = len;
1302 csum2 = skb_checksum(list, offset - start,
1303 copy, 0);
1304 csum = csum_block_add(csum, csum2, pos);
1305 if ((len -= copy) == 0)
1306 return csum;
1307 offset += copy;
1308 pos += copy;
1309 }
1310 start = end;
1311 }
1312 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001313 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 return csum;
1316}
1317
1318/* Both of above in one bottle. */
1319
Al Viro81d77662006-11-14 21:37:33 -08001320__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1321 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int start = skb_headlen(skb);
1324 int i, copy = start - offset;
1325 int pos = 0;
1326
1327 /* Copy header. */
1328 if (copy > 0) {
1329 if (copy > len)
1330 copy = len;
1331 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1332 copy, csum);
1333 if ((len -= copy) == 0)
1334 return csum;
1335 offset += copy;
1336 to += copy;
1337 pos = copy;
1338 }
1339
1340 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1341 int end;
1342
1343 BUG_TRAP(start <= offset + len);
1344
1345 end = start + skb_shinfo(skb)->frags[i].size;
1346 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001347 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 u8 *vaddr;
1349 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1350
1351 if (copy > len)
1352 copy = len;
1353 vaddr = kmap_skb_frag(frag);
1354 csum2 = csum_partial_copy_nocheck(vaddr +
1355 frag->page_offset +
1356 offset - start, to,
1357 copy, 0);
1358 kunmap_skb_frag(vaddr);
1359 csum = csum_block_add(csum, csum2, pos);
1360 if (!(len -= copy))
1361 return csum;
1362 offset += copy;
1363 to += copy;
1364 pos += copy;
1365 }
1366 start = end;
1367 }
1368
1369 if (skb_shinfo(skb)->frag_list) {
1370 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1371
1372 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001373 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 int end;
1375
1376 BUG_TRAP(start <= offset + len);
1377
1378 end = start + list->len;
1379 if ((copy = end - offset) > 0) {
1380 if (copy > len)
1381 copy = len;
1382 csum2 = skb_copy_and_csum_bits(list,
1383 offset - start,
1384 to, copy, 0);
1385 csum = csum_block_add(csum, csum2, pos);
1386 if ((len -= copy) == 0)
1387 return csum;
1388 offset += copy;
1389 to += copy;
1390 pos += copy;
1391 }
1392 start = end;
1393 }
1394 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001395 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return csum;
1397}
1398
1399void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1400{
Al Virod3bc23e2006-11-14 21:24:49 -08001401 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 long csstart;
1403
Patrick McHardy84fa7932006-08-29 16:44:56 -07001404 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 csstart = skb->h.raw - skb->data;
1406 else
1407 csstart = skb_headlen(skb);
1408
Kris Katterjohn09a62662006-01-08 22:24:28 -08001409 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 memcpy(to, skb->data, csstart);
1412
1413 csum = 0;
1414 if (csstart != skb->len)
1415 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1416 skb->len - csstart, 0);
1417
Patrick McHardy84fa7932006-08-29 16:44:56 -07001418 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001419 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Al Virod3bc23e2006-11-14 21:24:49 -08001421 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
1425/**
1426 * skb_dequeue - remove from the head of the queue
1427 * @list: list to dequeue from
1428 *
1429 * Remove the head of the list. The list lock is taken so the function
1430 * may be used safely with other locking list functions. The head item is
1431 * returned or %NULL if the list is empty.
1432 */
1433
1434struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1435{
1436 unsigned long flags;
1437 struct sk_buff *result;
1438
1439 spin_lock_irqsave(&list->lock, flags);
1440 result = __skb_dequeue(list);
1441 spin_unlock_irqrestore(&list->lock, flags);
1442 return result;
1443}
1444
1445/**
1446 * skb_dequeue_tail - remove from the tail of the queue
1447 * @list: list to dequeue from
1448 *
1449 * Remove the tail of the list. The list lock is taken so the function
1450 * may be used safely with other locking list functions. The tail item is
1451 * returned or %NULL if the list is empty.
1452 */
1453struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1454{
1455 unsigned long flags;
1456 struct sk_buff *result;
1457
1458 spin_lock_irqsave(&list->lock, flags);
1459 result = __skb_dequeue_tail(list);
1460 spin_unlock_irqrestore(&list->lock, flags);
1461 return result;
1462}
1463
1464/**
1465 * skb_queue_purge - empty a list
1466 * @list: list to empty
1467 *
1468 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1469 * the list and one reference dropped. This function takes the list
1470 * lock and is atomic with respect to other list locking functions.
1471 */
1472void skb_queue_purge(struct sk_buff_head *list)
1473{
1474 struct sk_buff *skb;
1475 while ((skb = skb_dequeue(list)) != NULL)
1476 kfree_skb(skb);
1477}
1478
1479/**
1480 * skb_queue_head - queue a buffer at the list head
1481 * @list: list to use
1482 * @newsk: buffer to queue
1483 *
1484 * Queue a buffer at the start of the list. This function takes the
1485 * list lock and can be used safely with other locking &sk_buff functions
1486 * safely.
1487 *
1488 * A buffer cannot be placed on two lists at the same time.
1489 */
1490void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1491{
1492 unsigned long flags;
1493
1494 spin_lock_irqsave(&list->lock, flags);
1495 __skb_queue_head(list, newsk);
1496 spin_unlock_irqrestore(&list->lock, flags);
1497}
1498
1499/**
1500 * skb_queue_tail - queue a buffer at the list tail
1501 * @list: list to use
1502 * @newsk: buffer to queue
1503 *
1504 * Queue a buffer at the tail of the list. This function takes the
1505 * list lock and can be used safely with other locking &sk_buff functions
1506 * safely.
1507 *
1508 * A buffer cannot be placed on two lists at the same time.
1509 */
1510void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1511{
1512 unsigned long flags;
1513
1514 spin_lock_irqsave(&list->lock, flags);
1515 __skb_queue_tail(list, newsk);
1516 spin_unlock_irqrestore(&list->lock, flags);
1517}
David S. Miller8728b832005-08-09 19:25:21 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/**
1520 * skb_unlink - remove a buffer from a list
1521 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001522 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 *
David S. Miller8728b832005-08-09 19:25:21 -07001524 * Remove a packet from a list. The list locks are taken and this
1525 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 *
David S. Miller8728b832005-08-09 19:25:21 -07001527 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
David S. Miller8728b832005-08-09 19:25:21 -07001529void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
David S. Miller8728b832005-08-09 19:25:21 -07001531 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
David S. Miller8728b832005-08-09 19:25:21 -07001533 spin_lock_irqsave(&list->lock, flags);
1534 __skb_unlink(skb, list);
1535 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/**
1539 * skb_append - append a buffer
1540 * @old: buffer to insert after
1541 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001542 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 *
1544 * Place a packet after a given packet in a list. The list locks are taken
1545 * and this function is atomic with respect to other list locked calls.
1546 * A buffer cannot be placed on two lists at the same time.
1547 */
David S. Miller8728b832005-08-09 19:25:21 -07001548void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 unsigned long flags;
1551
David S. Miller8728b832005-08-09 19:25:21 -07001552 spin_lock_irqsave(&list->lock, flags);
1553 __skb_append(old, newsk, list);
1554 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557
1558/**
1559 * skb_insert - insert a buffer
1560 * @old: buffer to insert before
1561 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001562 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
David S. Miller8728b832005-08-09 19:25:21 -07001564 * Place a packet before a given packet in a list. The list locks are
1565 * taken and this function is atomic with respect to other list locked
1566 * calls.
1567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * A buffer cannot be placed on two lists at the same time.
1569 */
David S. Miller8728b832005-08-09 19:25:21 -07001570void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 unsigned long flags;
1573
David S. Miller8728b832005-08-09 19:25:21 -07001574 spin_lock_irqsave(&list->lock, flags);
1575 __skb_insert(newsk, old->prev, old, list);
1576 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579#if 0
1580/*
1581 * Tune the memory allocator for a new MTU size.
1582 */
1583void skb_add_mtu(int mtu)
1584{
1585 /* Must match allocation in alloc_skb */
1586 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1587
1588 kmem_add_cache_size(mtu);
1589}
1590#endif
1591
1592static inline void skb_split_inside_header(struct sk_buff *skb,
1593 struct sk_buff* skb1,
1594 const u32 len, const int pos)
1595{
1596 int i;
1597
1598 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1599
1600 /* And move data appendix as is. */
1601 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1602 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1603
1604 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1605 skb_shinfo(skb)->nr_frags = 0;
1606 skb1->data_len = skb->data_len;
1607 skb1->len += skb1->data_len;
1608 skb->data_len = 0;
1609 skb->len = len;
1610 skb->tail = skb->data + len;
1611}
1612
1613static inline void skb_split_no_header(struct sk_buff *skb,
1614 struct sk_buff* skb1,
1615 const u32 len, int pos)
1616{
1617 int i, k = 0;
1618 const int nfrags = skb_shinfo(skb)->nr_frags;
1619
1620 skb_shinfo(skb)->nr_frags = 0;
1621 skb1->len = skb1->data_len = skb->len - len;
1622 skb->len = len;
1623 skb->data_len = len - pos;
1624
1625 for (i = 0; i < nfrags; i++) {
1626 int size = skb_shinfo(skb)->frags[i].size;
1627
1628 if (pos + size > len) {
1629 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1630
1631 if (pos < len) {
1632 /* Split frag.
1633 * We have two variants in this case:
1634 * 1. Move all the frag to the second
1635 * part, if it is possible. F.e.
1636 * this approach is mandatory for TUX,
1637 * where splitting is expensive.
1638 * 2. Split is accurately. We make this.
1639 */
1640 get_page(skb_shinfo(skb)->frags[i].page);
1641 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1642 skb_shinfo(skb1)->frags[0].size -= len - pos;
1643 skb_shinfo(skb)->frags[i].size = len - pos;
1644 skb_shinfo(skb)->nr_frags++;
1645 }
1646 k++;
1647 } else
1648 skb_shinfo(skb)->nr_frags++;
1649 pos += size;
1650 }
1651 skb_shinfo(skb1)->nr_frags = k;
1652}
1653
1654/**
1655 * skb_split - Split fragmented skb to two parts at length len.
1656 * @skb: the buffer to split
1657 * @skb1: the buffer to receive the second part
1658 * @len: new length for skb
1659 */
1660void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1661{
1662 int pos = skb_headlen(skb);
1663
1664 if (len < pos) /* Split line is inside header. */
1665 skb_split_inside_header(skb, skb1, len, pos);
1666 else /* Second chunk has no header, nothing to copy. */
1667 skb_split_no_header(skb, skb1, len, pos);
1668}
1669
Thomas Graf677e90e2005-06-23 20:59:51 -07001670/**
1671 * skb_prepare_seq_read - Prepare a sequential read of skb data
1672 * @skb: the buffer to read
1673 * @from: lower offset of data to be read
1674 * @to: upper offset of data to be read
1675 * @st: state variable
1676 *
1677 * Initializes the specified state variable. Must be called before
1678 * invoking skb_seq_read() for the first time.
1679 */
1680void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1681 unsigned int to, struct skb_seq_state *st)
1682{
1683 st->lower_offset = from;
1684 st->upper_offset = to;
1685 st->root_skb = st->cur_skb = skb;
1686 st->frag_idx = st->stepped_offset = 0;
1687 st->frag_data = NULL;
1688}
1689
1690/**
1691 * skb_seq_read - Sequentially read skb data
1692 * @consumed: number of bytes consumed by the caller so far
1693 * @data: destination pointer for data to be returned
1694 * @st: state variable
1695 *
1696 * Reads a block of skb data at &consumed relative to the
1697 * lower offset specified to skb_prepare_seq_read(). Assigns
1698 * the head of the data block to &data and returns the length
1699 * of the block or 0 if the end of the skb data or the upper
1700 * offset has been reached.
1701 *
1702 * The caller is not required to consume all of the data
1703 * returned, i.e. &consumed is typically set to the number
1704 * of bytes already consumed and the next call to
1705 * skb_seq_read() will return the remaining part of the block.
1706 *
1707 * Note: The size of each block of data returned can be arbitary,
1708 * this limitation is the cost for zerocopy seqeuental
1709 * reads of potentially non linear data.
1710 *
1711 * Note: Fragment lists within fragments are not implemented
1712 * at the moment, state->root_skb could be replaced with
1713 * a stack for this purpose.
1714 */
1715unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1716 struct skb_seq_state *st)
1717{
1718 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1719 skb_frag_t *frag;
1720
1721 if (unlikely(abs_offset >= st->upper_offset))
1722 return 0;
1723
1724next_skb:
1725 block_limit = skb_headlen(st->cur_skb);
1726
1727 if (abs_offset < block_limit) {
1728 *data = st->cur_skb->data + abs_offset;
1729 return block_limit - abs_offset;
1730 }
1731
1732 if (st->frag_idx == 0 && !st->frag_data)
1733 st->stepped_offset += skb_headlen(st->cur_skb);
1734
1735 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1736 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1737 block_limit = frag->size + st->stepped_offset;
1738
1739 if (abs_offset < block_limit) {
1740 if (!st->frag_data)
1741 st->frag_data = kmap_skb_frag(frag);
1742
1743 *data = (u8 *) st->frag_data + frag->page_offset +
1744 (abs_offset - st->stepped_offset);
1745
1746 return block_limit - abs_offset;
1747 }
1748
1749 if (st->frag_data) {
1750 kunmap_skb_frag(st->frag_data);
1751 st->frag_data = NULL;
1752 }
1753
1754 st->frag_idx++;
1755 st->stepped_offset += frag->size;
1756 }
1757
1758 if (st->cur_skb->next) {
1759 st->cur_skb = st->cur_skb->next;
1760 st->frag_idx = 0;
1761 goto next_skb;
1762 } else if (st->root_skb == st->cur_skb &&
1763 skb_shinfo(st->root_skb)->frag_list) {
1764 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1765 goto next_skb;
1766 }
1767
1768 return 0;
1769}
1770
1771/**
1772 * skb_abort_seq_read - Abort a sequential read of skb data
1773 * @st: state variable
1774 *
1775 * Must be called if skb_seq_read() was not called until it
1776 * returned 0.
1777 */
1778void skb_abort_seq_read(struct skb_seq_state *st)
1779{
1780 if (st->frag_data)
1781 kunmap_skb_frag(st->frag_data);
1782}
1783
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001784#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1785
1786static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1787 struct ts_config *conf,
1788 struct ts_state *state)
1789{
1790 return skb_seq_read(offset, text, TS_SKB_CB(state));
1791}
1792
1793static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1794{
1795 skb_abort_seq_read(TS_SKB_CB(state));
1796}
1797
1798/**
1799 * skb_find_text - Find a text pattern in skb data
1800 * @skb: the buffer to look in
1801 * @from: search offset
1802 * @to: search limit
1803 * @config: textsearch configuration
1804 * @state: uninitialized textsearch state variable
1805 *
1806 * Finds a pattern in the skb data according to the specified
1807 * textsearch configuration. Use textsearch_next() to retrieve
1808 * subsequent occurrences of the pattern. Returns the offset
1809 * to the first occurrence or UINT_MAX if no match was found.
1810 */
1811unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1812 unsigned int to, struct ts_config *config,
1813 struct ts_state *state)
1814{
Phil Oesterf72b9482006-06-26 00:00:57 -07001815 unsigned int ret;
1816
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001817 config->get_next_block = skb_ts_get_next_block;
1818 config->finish = skb_ts_finish;
1819
1820 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1821
Phil Oesterf72b9482006-06-26 00:00:57 -07001822 ret = textsearch_find(config, state);
1823 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001824}
1825
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001826/**
1827 * skb_append_datato_frags: - append the user data to a skb
1828 * @sk: sock structure
1829 * @skb: skb structure to be appened with user data.
1830 * @getfrag: call back function to be used for getting the user data
1831 * @from: pointer to user message iov
1832 * @length: length of the iov message
1833 *
1834 * Description: This procedure append the user data in the fragment part
1835 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1836 */
1837int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001838 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001839 int len, int odd, struct sk_buff *skb),
1840 void *from, int length)
1841{
1842 int frg_cnt = 0;
1843 skb_frag_t *frag = NULL;
1844 struct page *page = NULL;
1845 int copy, left;
1846 int offset = 0;
1847 int ret;
1848
1849 do {
1850 /* Return error if we don't have space for new frag */
1851 frg_cnt = skb_shinfo(skb)->nr_frags;
1852 if (frg_cnt >= MAX_SKB_FRAGS)
1853 return -EFAULT;
1854
1855 /* allocate a new page for next frag */
1856 page = alloc_pages(sk->sk_allocation, 0);
1857
1858 /* If alloc_page fails just return failure and caller will
1859 * free previous allocated pages by doing kfree_skb()
1860 */
1861 if (page == NULL)
1862 return -ENOMEM;
1863
1864 /* initialize the next frag */
1865 sk->sk_sndmsg_page = page;
1866 sk->sk_sndmsg_off = 0;
1867 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1868 skb->truesize += PAGE_SIZE;
1869 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1870
1871 /* get the new initialized frag */
1872 frg_cnt = skb_shinfo(skb)->nr_frags;
1873 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1874
1875 /* copy the user data to page */
1876 left = PAGE_SIZE - frag->page_offset;
1877 copy = (length > left)? left : length;
1878
1879 ret = getfrag(from, (page_address(frag->page) +
1880 frag->page_offset + frag->size),
1881 offset, copy, 0, skb);
1882 if (ret < 0)
1883 return -EFAULT;
1884
1885 /* copy was successful so update the size parameters */
1886 sk->sk_sndmsg_off += copy;
1887 frag->size += copy;
1888 skb->len += copy;
1889 skb->data_len += copy;
1890 offset += copy;
1891 length -= copy;
1892
1893 } while (length > 0);
1894
1895 return 0;
1896}
1897
Herbert Xucbb042f2006-03-20 22:43:56 -08001898/**
1899 * skb_pull_rcsum - pull skb and update receive checksum
1900 * @skb: buffer to update
1901 * @start: start of data before pull
1902 * @len: length of data pulled
1903 *
1904 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001905 * update the CHECKSUM_COMPLETE checksum. It should be used on
1906 * receive path processing instead of skb_pull unless you know
1907 * that the checksum difference is zero (e.g., a valid IP header)
1908 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001909 */
1910unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1911{
1912 BUG_ON(len > skb->len);
1913 skb->len -= len;
1914 BUG_ON(skb->len < skb->data_len);
1915 skb_postpull_rcsum(skb, skb->data, len);
1916 return skb->data += len;
1917}
1918
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001919EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1920
Herbert Xuf4c50d92006-06-22 03:02:40 -07001921/**
1922 * skb_segment - Perform protocol segmentation on skb.
1923 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001924 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001925 *
1926 * This function performs segmentation on the given skb. It returns
1927 * the segment at the given position. It returns NULL if there are
1928 * no more segments to generate, or when an error is encountered.
1929 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001930struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001931{
1932 struct sk_buff *segs = NULL;
1933 struct sk_buff *tail = NULL;
1934 unsigned int mss = skb_shinfo(skb)->gso_size;
1935 unsigned int doffset = skb->data - skb->mac.raw;
1936 unsigned int offset = doffset;
1937 unsigned int headroom;
1938 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001939 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001940 int nfrags = skb_shinfo(skb)->nr_frags;
1941 int err = -ENOMEM;
1942 int i = 0;
1943 int pos;
1944
1945 __skb_push(skb, doffset);
1946 headroom = skb_headroom(skb);
1947 pos = skb_headlen(skb);
1948
1949 do {
1950 struct sk_buff *nskb;
1951 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001952 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001953 int k;
1954 int size;
1955
1956 len = skb->len - offset;
1957 if (len > mss)
1958 len = mss;
1959
1960 hsize = skb_headlen(skb) - offset;
1961 if (hsize < 0)
1962 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001963 if (hsize > len || !sg)
1964 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001965
Herbert Xuc8884ed2006-10-29 15:59:41 -08001966 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001967 if (unlikely(!nskb))
1968 goto err;
1969
1970 if (segs)
1971 tail->next = nskb;
1972 else
1973 segs = nskb;
1974 tail = nskb;
1975
1976 nskb->dev = skb->dev;
1977 nskb->priority = skb->priority;
1978 nskb->protocol = skb->protocol;
1979 nskb->dst = dst_clone(skb->dst);
1980 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1981 nskb->pkt_type = skb->pkt_type;
1982 nskb->mac_len = skb->mac_len;
1983
1984 skb_reserve(nskb, headroom);
1985 nskb->mac.raw = nskb->data;
1986 nskb->nh.raw = nskb->data + skb->mac_len;
1987 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1988 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1989
1990 if (!sg) {
1991 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1992 skb_put(nskb, len),
1993 len, 0);
1994 continue;
1995 }
1996
1997 frag = skb_shinfo(nskb)->frags;
1998 k = 0;
1999
Patrick McHardy84fa7932006-08-29 16:44:56 -07002000 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002001 nskb->csum = skb->csum;
2002 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2003
2004 while (pos < offset + len) {
2005 BUG_ON(i >= nfrags);
2006
2007 *frag = skb_shinfo(skb)->frags[i];
2008 get_page(frag->page);
2009 size = frag->size;
2010
2011 if (pos < offset) {
2012 frag->page_offset += offset - pos;
2013 frag->size -= offset - pos;
2014 }
2015
2016 k++;
2017
2018 if (pos + size <= offset + len) {
2019 i++;
2020 pos += size;
2021 } else {
2022 frag->size -= pos + size - (offset + len);
2023 break;
2024 }
2025
2026 frag++;
2027 }
2028
2029 skb_shinfo(nskb)->nr_frags = k;
2030 nskb->data_len = len - hsize;
2031 nskb->len += nskb->data_len;
2032 nskb->truesize += nskb->data_len;
2033 } while ((offset += len) < skb->len);
2034
2035 return segs;
2036
2037err:
2038 while ((skb = segs)) {
2039 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002040 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002041 }
2042 return ERR_PTR(err);
2043}
2044
2045EXPORT_SYMBOL_GPL(skb_segment);
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047void __init skb_init(void)
2048{
2049 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2050 sizeof(struct sk_buff),
2051 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002052 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002054 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2055 (2*sizeof(struct sk_buff)) +
2056 sizeof(atomic_t),
2057 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002058 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002059 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
2062EXPORT_SYMBOL(___pskb_trim);
2063EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002064EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002066EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002067EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068EXPORT_SYMBOL(pskb_copy);
2069EXPORT_SYMBOL(pskb_expand_head);
2070EXPORT_SYMBOL(skb_checksum);
2071EXPORT_SYMBOL(skb_clone);
2072EXPORT_SYMBOL(skb_clone_fraglist);
2073EXPORT_SYMBOL(skb_copy);
2074EXPORT_SYMBOL(skb_copy_and_csum_bits);
2075EXPORT_SYMBOL(skb_copy_and_csum_dev);
2076EXPORT_SYMBOL(skb_copy_bits);
2077EXPORT_SYMBOL(skb_copy_expand);
2078EXPORT_SYMBOL(skb_over_panic);
2079EXPORT_SYMBOL(skb_pad);
2080EXPORT_SYMBOL(skb_realloc_headroom);
2081EXPORT_SYMBOL(skb_under_panic);
2082EXPORT_SYMBOL(skb_dequeue);
2083EXPORT_SYMBOL(skb_dequeue_tail);
2084EXPORT_SYMBOL(skb_insert);
2085EXPORT_SYMBOL(skb_queue_purge);
2086EXPORT_SYMBOL(skb_queue_head);
2087EXPORT_SYMBOL(skb_queue_tail);
2088EXPORT_SYMBOL(skb_unlink);
2089EXPORT_SYMBOL(skb_append);
2090EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002091EXPORT_SYMBOL(skb_prepare_seq_read);
2092EXPORT_SYMBOL(skb_seq_read);
2093EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002094EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002095EXPORT_SYMBOL(skb_append_datato_frags);