blob: 8e1c385e5ba914e2f50bee987ed0c96ff516ba9c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <net/protocol.h>
61#include <net/dst.h>
62#include <net/sock.h>
63#include <net/checksum.h>
64#include <net/xfrm.h>
65
66#include <asm/uaccess.h>
67#include <asm/system.h>
68
Al Viroa1f8e7f72006-10-19 16:08:53 -040069#include "kmap_skb.h"
70
Eric Dumazetba899662005-08-26 12:05:31 -070071static kmem_cache_t *skbuff_head_cache __read_mostly;
72static kmem_cache_t *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
79
80/**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89{
Patrick McHardy26095452005-04-21 16:43:02 -070090 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
Patrick McHardy26095452005-04-21 16:43:02 -0700108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 BUG();
113}
114
David S. Millerdc6de332006-04-20 00:10:50 -0700115void skb_truesize_bug(struct sk_buff *skb)
116{
117 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb->truesize, skb->len, sizeof(struct sk_buff));
120}
121EXPORT_SYMBOL(skb_truesize_bug);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
125 * [BEEP] leaks.
126 *
127 */
128
129/**
David S. Millerd179cd12005-08-17 14:57:30 -0700130 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * @size: size to allocate
132 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
Al Virodd0fc662005-10-07 07:46:04 +0100143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
David S. Millerd179cd12005-08-17 14:57:30 -0700144 int fclone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Herbert Xu8798b3f2006-01-23 16:32:45 -0800146 kmem_cache_t *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800147 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct sk_buff *skb;
149 u8 *data;
150
Herbert Xu8798b3f2006-01-23 16:32:45 -0800151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Get the HEAD */
Herbert Xu8798b3f2006-01-23 16:32:45 -0800154 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -0700160 data = kmalloc_track_caller(size + sizeof(struct skb_shared_info),
161 gfp_mask);
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 */
213struct sk_buff *alloc_skb_from_cache(kmem_cache_t *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{
270 struct sk_buff *skb;
271
272 skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700273 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700274 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700275 skb->dev = dev;
276 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700277 return skb;
278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Herbert Xu27b437c2006-07-13 19:26:39 -0700280static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Herbert Xu27b437c2006-07-13 19:26:39 -0700282 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Herbert Xu27b437c2006-07-13 19:26:39 -0700284 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 do {
287 struct sk_buff *this = list;
288 list = list->next;
289 kfree_skb(this);
290 } while (list);
291}
292
Herbert Xu27b437c2006-07-13 19:26:39 -0700293static inline void skb_drop_fraglist(struct sk_buff *skb)
294{
295 skb_drop_list(&skb_shinfo(skb)->frag_list);
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void skb_clone_fraglist(struct sk_buff *skb)
299{
300 struct sk_buff *list;
301
302 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
303 skb_get(list);
304}
305
Adrian Bunk5bba1712006-06-29 13:02:35 -0700306static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 if (!skb->cloned ||
309 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
310 &skb_shinfo(skb)->dataref)) {
311 if (skb_shinfo(skb)->nr_frags) {
312 int i;
313 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
314 put_page(skb_shinfo(skb)->frags[i].page);
315 }
316
317 if (skb_shinfo(skb)->frag_list)
318 skb_drop_fraglist(skb);
319
320 kfree(skb->head);
321 }
322}
323
324/*
325 * Free an skbuff by memory without cleaning the state.
326 */
327void kfree_skbmem(struct sk_buff *skb)
328{
David S. Millerd179cd12005-08-17 14:57:30 -0700329 struct sk_buff *other;
330 atomic_t *fclone_ref;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700333 switch (skb->fclone) {
334 case SKB_FCLONE_UNAVAILABLE:
335 kmem_cache_free(skbuff_head_cache, skb);
336 break;
337
338 case SKB_FCLONE_ORIG:
339 fclone_ref = (atomic_t *) (skb + 2);
340 if (atomic_dec_and_test(fclone_ref))
341 kmem_cache_free(skbuff_fclone_cache, skb);
342 break;
343
344 case SKB_FCLONE_CLONE:
345 fclone_ref = (atomic_t *) (skb + 1);
346 other = skb - 1;
347
348 /* The clone portion is available for
349 * fast-cloning again.
350 */
351 skb->fclone = SKB_FCLONE_UNAVAILABLE;
352
353 if (atomic_dec_and_test(fclone_ref))
354 kmem_cache_free(skbuff_fclone_cache, other);
355 break;
356 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/**
360 * __kfree_skb - private function
361 * @skb: buffer
362 *
363 * Free an sk_buff. Release anything attached to the buffer.
364 * Clean the state. This is an internal helper function. Users should
365 * always call kfree_skb
366 */
367
368void __kfree_skb(struct sk_buff *skb)
369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 dst_release(skb->dst);
371#ifdef CONFIG_XFRM
372 secpath_put(skb->sp);
373#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700374 if (skb->destructor) {
375 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 skb->destructor(skb);
377 }
378#ifdef CONFIG_NETFILTER
379 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800380#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
381 nf_conntrack_put_reasm(skb->nfct_reasm);
382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#ifdef CONFIG_BRIDGE_NETFILTER
384 nf_bridge_put(skb->nf_bridge);
385#endif
386#endif
387/* XXX: IS this still necessary? - JHS */
388#ifdef CONFIG_NET_SCHED
389 skb->tc_index = 0;
390#ifdef CONFIG_NET_CLS_ACT
391 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif
393#endif
394
395 kfree_skbmem(skb);
396}
397
398/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800399 * kfree_skb - free an sk_buff
400 * @skb: buffer to free
401 *
402 * Drop a reference to the buffer and free it if the usage count has
403 * hit zero.
404 */
405void kfree_skb(struct sk_buff *skb)
406{
407 if (unlikely(!skb))
408 return;
409 if (likely(atomic_read(&skb->users) == 1))
410 smp_rmb();
411 else if (likely(!atomic_dec_and_test(&skb->users)))
412 return;
413 __kfree_skb(skb);
414}
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * skb_clone - duplicate an sk_buff
418 * @skb: buffer to clone
419 * @gfp_mask: allocation priority
420 *
421 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
422 * copies share the same packet data but not structure. The new
423 * buffer has a reference count of 1. If the allocation fails the
424 * function returns %NULL otherwise the new buffer is returned.
425 *
426 * If this function is called from an interrupt gfp_mask() must be
427 * %GFP_ATOMIC.
428 */
429
Al Virodd0fc662005-10-07 07:46:04 +0100430struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
David S. Millerd179cd12005-08-17 14:57:30 -0700432 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
David S. Millerd179cd12005-08-17 14:57:30 -0700434 n = skb + 1;
435 if (skb->fclone == SKB_FCLONE_ORIG &&
436 n->fclone == SKB_FCLONE_UNAVAILABLE) {
437 atomic_t *fclone_ref = (atomic_t *) (n + 1);
438 n->fclone = SKB_FCLONE_CLONE;
439 atomic_inc(fclone_ref);
440 } else {
441 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
442 if (!n)
443 return NULL;
444 n->fclone = SKB_FCLONE_UNAVAILABLE;
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447#define C(x) n->x = skb->x
448
449 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700451 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 C(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 C(h);
454 C(nh);
455 C(mac);
456 C(dst);
457 dst_clone(skb->dst);
458 C(sp);
459#ifdef CONFIG_INET
460 secpath_get(skb->sp);
461#endif
462 memcpy(n->cb, skb->cb, sizeof(skb->cb));
463 C(len);
464 C(data_len);
465 C(csum);
466 C(local_df);
467 n->cloned = 1;
468 n->nohdr = 0;
469 C(pkt_type);
470 C(ip_summed);
471 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800472#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
473 C(ipvs_property);
474#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800477 C(mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#ifdef CONFIG_NETFILTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 C(nfct);
480 nf_conntrack_get(skb->nfct);
481 C(nfctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800482#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
483 C(nfct_reasm);
484 nf_conntrack_get_reasm(skb->nfct_reasm);
485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#ifdef CONFIG_BRIDGE_NETFILTER
487 C(nf_bridge);
488 nf_bridge_get(skb->nf_bridge);
489#endif
490#endif /*CONFIG_NETFILTER*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#ifdef CONFIG_NET_SCHED
492 C(tc_index);
493#ifdef CONFIG_NET_CLS_ACT
494 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700495 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
496 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 C(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498#endif
James Morris984bc162006-06-09 00:29:17 -0700499 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#endif
501 C(truesize);
502 atomic_set(&n->users, 1);
503 C(head);
504 C(data);
505 C(tail);
506 C(end);
507
508 atomic_inc(&(skb_shinfo(skb)->dataref));
509 skb->cloned = 1;
510
511 return n;
512}
513
514static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
515{
516 /*
517 * Shift between the two data areas in bytes
518 */
519 unsigned long offset = new->data - old->data;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 new->sk = NULL;
522 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 new->priority = old->priority;
524 new->protocol = old->protocol;
525 new->dst = dst_clone(old->dst);
526#ifdef CONFIG_INET
527 new->sp = secpath_get(old->sp);
528#endif
529 new->h.raw = old->h.raw + offset;
530 new->nh.raw = old->nh.raw + offset;
531 new->mac.raw = old->mac.raw + offset;
532 memcpy(new->cb, old->cb, sizeof(old->cb));
533 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700534 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700536 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800538 new->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#ifdef CONFIG_NETFILTER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 new->nfct = old->nfct;
541 nf_conntrack_get(old->nfct);
542 new->nfctinfo = old->nfctinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800543#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
544 new->nfct_reasm = old->nfct_reasm;
545 nf_conntrack_get_reasm(old->nfct_reasm);
546#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300547#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
548 new->ipvs_property = old->ipvs_property;
549#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_BRIDGE_NETFILTER
551 new->nf_bridge = old->nf_bridge;
552 nf_bridge_get(old->nf_bridge);
553#endif
554#endif
555#ifdef CONFIG_NET_SCHED
556#ifdef CONFIG_NET_CLS_ACT
557 new->tc_verd = old->tc_verd;
558#endif
559 new->tc_index = old->tc_index;
560#endif
James Morris984bc162006-06-09 00:29:17 -0700561 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700563 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
564 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
565 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/**
569 * skb_copy - create private copy of an sk_buff
570 * @skb: buffer to copy
571 * @gfp_mask: allocation priority
572 *
573 * Make a copy of both an &sk_buff and its data. This is used when the
574 * caller wishes to modify the data and needs a private copy of the
575 * data to alter. Returns %NULL on failure or the pointer to the buffer
576 * on success. The returned buffer has a reference count of 1.
577 *
578 * As by-product this function converts non-linear &sk_buff to linear
579 * one, so that &sk_buff becomes completely private and caller is allowed
580 * to modify all the data of returned buffer. This means that this
581 * function is not recommended for use in circumstances when only
582 * header is going to be modified. Use pskb_copy() instead.
583 */
584
Al Virodd0fc662005-10-07 07:46:04 +0100585struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 int headerlen = skb->data - skb->head;
588 /*
589 * Allocate the copy buffer
590 */
591 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
592 gfp_mask);
593 if (!n)
594 return NULL;
595
596 /* Set the data pointer */
597 skb_reserve(n, headerlen);
598 /* Set the tail pointer and length */
599 skb_put(n, skb->len);
600 n->csum = skb->csum;
601 n->ip_summed = skb->ip_summed;
602
603 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
604 BUG();
605
606 copy_skb_header(n, skb);
607 return n;
608}
609
610
611/**
612 * pskb_copy - create copy of an sk_buff with private head.
613 * @skb: buffer to copy
614 * @gfp_mask: allocation priority
615 *
616 * Make a copy of both an &sk_buff and part of its data, located
617 * in header. Fragmented data remain shared. This is used when
618 * the caller wishes to modify only header of &sk_buff and needs
619 * private copy of the header to alter. Returns %NULL on failure
620 * or the pointer to the buffer on success.
621 * The returned buffer has a reference count of 1.
622 */
623
Al Virodd0fc662005-10-07 07:46:04 +0100624struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 /*
627 * Allocate the copy buffer
628 */
629 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
630
631 if (!n)
632 goto out;
633
634 /* Set the data pointer */
635 skb_reserve(n, skb->data - skb->head);
636 /* Set the tail pointer and length */
637 skb_put(n, skb_headlen(skb));
638 /* Copy the bytes */
639 memcpy(n->data, skb->data, n->len);
640 n->csum = skb->csum;
641 n->ip_summed = skb->ip_summed;
642
Herbert Xu25f484a2006-11-07 14:57:15 -0800643 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 n->data_len = skb->data_len;
645 n->len = skb->len;
646
647 if (skb_shinfo(skb)->nr_frags) {
648 int i;
649
650 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
651 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
652 get_page(skb_shinfo(n)->frags[i].page);
653 }
654 skb_shinfo(n)->nr_frags = i;
655 }
656
657 if (skb_shinfo(skb)->frag_list) {
658 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
659 skb_clone_fraglist(n);
660 }
661
662 copy_skb_header(n, skb);
663out:
664 return n;
665}
666
667/**
668 * pskb_expand_head - reallocate header of &sk_buff
669 * @skb: buffer to reallocate
670 * @nhead: room to add at head
671 * @ntail: room to add at tail
672 * @gfp_mask: allocation priority
673 *
674 * Expands (or creates identical copy, if &nhead and &ntail are zero)
675 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
676 * reference count of 1. Returns zero in the case of success or error,
677 * if expansion failed. In the last case, &sk_buff is not changed.
678 *
679 * All the pointers pointing into skb header may change and must be
680 * reloaded after call to this function.
681 */
682
Victor Fusco86a76ca2005-07-08 14:57:47 -0700683int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100684 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int i;
687 u8 *data;
688 int size = nhead + (skb->end - skb->head) + ntail;
689 long off;
690
691 if (skb_shared(skb))
692 BUG();
693
694 size = SKB_DATA_ALIGN(size);
695
696 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
697 if (!data)
698 goto nodata;
699
700 /* Copy only real data... and, alas, header. This should be
701 * optimized for the cases when header is void. */
702 memcpy(data + nhead, skb->head, skb->tail - skb->head);
703 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
704
705 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
706 get_page(skb_shinfo(skb)->frags[i].page);
707
708 if (skb_shinfo(skb)->frag_list)
709 skb_clone_fraglist(skb);
710
711 skb_release_data(skb);
712
713 off = (data + nhead) - skb->head;
714
715 skb->head = data;
716 skb->end = data + size;
717 skb->data += off;
718 skb->tail += off;
719 skb->mac.raw += off;
720 skb->h.raw += off;
721 skb->nh.raw += off;
722 skb->cloned = 0;
723 skb->nohdr = 0;
724 atomic_set(&skb_shinfo(skb)->dataref, 1);
725 return 0;
726
727nodata:
728 return -ENOMEM;
729}
730
731/* Make private copy of skb with writable head and some headroom */
732
733struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
734{
735 struct sk_buff *skb2;
736 int delta = headroom - skb_headroom(skb);
737
738 if (delta <= 0)
739 skb2 = pskb_copy(skb, GFP_ATOMIC);
740 else {
741 skb2 = skb_clone(skb, GFP_ATOMIC);
742 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
743 GFP_ATOMIC)) {
744 kfree_skb(skb2);
745 skb2 = NULL;
746 }
747 }
748 return skb2;
749}
750
751
752/**
753 * skb_copy_expand - copy and expand sk_buff
754 * @skb: buffer to copy
755 * @newheadroom: new free bytes at head
756 * @newtailroom: new free bytes at tail
757 * @gfp_mask: allocation priority
758 *
759 * Make a copy of both an &sk_buff and its data and while doing so
760 * allocate additional space.
761 *
762 * This is used when the caller wishes to modify the data and needs a
763 * private copy of the data to alter as well as more space for new fields.
764 * Returns %NULL on failure or the pointer to the buffer
765 * on success. The returned buffer has a reference count of 1.
766 *
767 * You must pass %GFP_ATOMIC as the allocation priority if this function
768 * is called from an interrupt.
769 *
770 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
771 * only by netfilter in the cases when checksum is recalculated? --ANK
772 */
773struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700774 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100775 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 /*
778 * Allocate the copy buffer
779 */
780 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
781 gfp_mask);
782 int head_copy_len, head_copy_off;
783
784 if (!n)
785 return NULL;
786
787 skb_reserve(n, newheadroom);
788
789 /* Set the tail pointer and length */
790 skb_put(n, skb->len);
791
792 head_copy_len = skb_headroom(skb);
793 head_copy_off = 0;
794 if (newheadroom <= head_copy_len)
795 head_copy_len = newheadroom;
796 else
797 head_copy_off = newheadroom - head_copy_len;
798
799 /* Copy the linear header and data. */
800 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
801 skb->len + head_copy_len))
802 BUG();
803
804 copy_skb_header(n, skb);
805
806 return n;
807}
808
809/**
810 * skb_pad - zero pad the tail of an skb
811 * @skb: buffer to pad
812 * @pad: space to pad
813 *
814 * Ensure that a buffer is followed by a padding area that is zero
815 * filled. Used by network drivers which may DMA or transfer data
816 * beyond the buffer end onto the wire.
817 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700818 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
820
Herbert Xu5b057c62006-06-23 02:06:41 -0700821int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Herbert Xu5b057c62006-06-23 02:06:41 -0700823 int err;
824 int ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700827 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700831
832 ntail = skb->data_len + pad - (skb->end - skb->tail);
833 if (likely(skb_cloned(skb) || ntail > 0)) {
834 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
835 if (unlikely(err))
836 goto free_skb;
837 }
838
839 /* FIXME: The use of this function with non-linear skb's really needs
840 * to be audited.
841 */
842 err = skb_linearize(skb);
843 if (unlikely(err))
844 goto free_skb;
845
846 memset(skb->data + skb->len, 0, pad);
847 return 0;
848
849free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Herbert Xu3cc0e872006-06-09 16:13:38 -0700854/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
856
Herbert Xu3cc0e872006-06-09 16:13:38 -0700857int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Herbert Xu27b437c2006-07-13 19:26:39 -0700859 struct sk_buff **fragp;
860 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 int offset = skb_headlen(skb);
862 int nfrags = skb_shinfo(skb)->nr_frags;
863 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700864 int err;
865
866 if (skb_cloned(skb) &&
867 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
868 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700870 i = 0;
871 if (offset >= len)
872 goto drop_pages;
873
874 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700876
877 if (end < len) {
878 offset = end;
879 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700881
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700882 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700883
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700884drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700885 skb_shinfo(skb)->nr_frags = i;
886
887 for (; i < nfrags; i++)
888 put_page(skb_shinfo(skb)->frags[i].page);
889
890 if (skb_shinfo(skb)->frag_list)
891 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700892 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
Herbert Xu27b437c2006-07-13 19:26:39 -0700895 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
896 fragp = &frag->next) {
897 int end = offset + frag->len;
898
899 if (skb_shared(frag)) {
900 struct sk_buff *nfrag;
901
902 nfrag = skb_clone(frag, GFP_ATOMIC);
903 if (unlikely(!nfrag))
904 return -ENOMEM;
905
906 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700907 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700908 frag = nfrag;
909 *fragp = frag;
910 }
911
912 if (end < len) {
913 offset = end;
914 continue;
915 }
916
917 if (end > len &&
918 unlikely((err = pskb_trim(frag, len - offset))))
919 return err;
920
921 if (frag->next)
922 skb_drop_list(&frag->next);
923 break;
924 }
925
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700926done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700927 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 skb->data_len -= skb->len - len;
929 skb->len = len;
930 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700931 skb->len = len;
932 skb->data_len = 0;
933 skb->tail = skb->data + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 return 0;
937}
938
939/**
940 * __pskb_pull_tail - advance tail of skb header
941 * @skb: buffer to reallocate
942 * @delta: number of bytes to advance tail
943 *
944 * The function makes a sense only on a fragmented &sk_buff,
945 * it expands header moving its tail forward and copying necessary
946 * data from fragmented part.
947 *
948 * &sk_buff MUST have reference count of 1.
949 *
950 * Returns %NULL (and &sk_buff does not change) if pull failed
951 * or value of new tail of skb in the case of success.
952 *
953 * All the pointers pointing into skb header may change and must be
954 * reloaded after call to this function.
955 */
956
957/* Moves tail of skb head forward, copying data from fragmented part,
958 * when it is necessary.
959 * 1. It may fail due to malloc failure.
960 * 2. It may change skb pointers.
961 *
962 * It is pretty complicated. Luckily, it is called only in exceptional cases.
963 */
964unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
965{
966 /* If skb has not enough free space at tail, get new one
967 * plus 128 bytes for future expansions. If we have enough
968 * room at tail, reallocate without expansion only if skb is cloned.
969 */
970 int i, k, eat = (skb->tail + delta) - skb->end;
971
972 if (eat > 0 || skb_cloned(skb)) {
973 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
974 GFP_ATOMIC))
975 return NULL;
976 }
977
978 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
979 BUG();
980
981 /* Optimization: no fragments, no reasons to preestimate
982 * size of pulled pages. Superb.
983 */
984 if (!skb_shinfo(skb)->frag_list)
985 goto pull_pages;
986
987 /* Estimate size of pulled pages. */
988 eat = delta;
989 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
990 if (skb_shinfo(skb)->frags[i].size >= eat)
991 goto pull_pages;
992 eat -= skb_shinfo(skb)->frags[i].size;
993 }
994
995 /* If we need update frag list, we are in troubles.
996 * Certainly, it possible to add an offset to skb data,
997 * but taking into account that pulling is expected to
998 * be very rare operation, it is worth to fight against
999 * further bloating skb head and crucify ourselves here instead.
1000 * Pure masohism, indeed. 8)8)
1001 */
1002 if (eat) {
1003 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1004 struct sk_buff *clone = NULL;
1005 struct sk_buff *insp = NULL;
1006
1007 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001008 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (list->len <= eat) {
1011 /* Eaten as whole. */
1012 eat -= list->len;
1013 list = list->next;
1014 insp = list;
1015 } else {
1016 /* Eaten partially. */
1017
1018 if (skb_shared(list)) {
1019 /* Sucks! We need to fork list. :-( */
1020 clone = skb_clone(list, GFP_ATOMIC);
1021 if (!clone)
1022 return NULL;
1023 insp = list->next;
1024 list = clone;
1025 } else {
1026 /* This may be pulled without
1027 * problems. */
1028 insp = list;
1029 }
1030 if (!pskb_pull(list, eat)) {
1031 if (clone)
1032 kfree_skb(clone);
1033 return NULL;
1034 }
1035 break;
1036 }
1037 } while (eat);
1038
1039 /* Free pulled out fragments. */
1040 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1041 skb_shinfo(skb)->frag_list = list->next;
1042 kfree_skb(list);
1043 }
1044 /* And insert new clone at head. */
1045 if (clone) {
1046 clone->next = list;
1047 skb_shinfo(skb)->frag_list = clone;
1048 }
1049 }
1050 /* Success! Now we may commit changes to skb data. */
1051
1052pull_pages:
1053 eat = delta;
1054 k = 0;
1055 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1056 if (skb_shinfo(skb)->frags[i].size <= eat) {
1057 put_page(skb_shinfo(skb)->frags[i].page);
1058 eat -= skb_shinfo(skb)->frags[i].size;
1059 } else {
1060 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1061 if (eat) {
1062 skb_shinfo(skb)->frags[k].page_offset += eat;
1063 skb_shinfo(skb)->frags[k].size -= eat;
1064 eat = 0;
1065 }
1066 k++;
1067 }
1068 }
1069 skb_shinfo(skb)->nr_frags = k;
1070
1071 skb->tail += delta;
1072 skb->data_len -= delta;
1073
1074 return skb->tail;
1075}
1076
1077/* Copy some data bits from skb to kernel buffer. */
1078
1079int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1080{
1081 int i, copy;
1082 int start = skb_headlen(skb);
1083
1084 if (offset > (int)skb->len - len)
1085 goto fault;
1086
1087 /* Copy header. */
1088 if ((copy = start - offset) > 0) {
1089 if (copy > len)
1090 copy = len;
1091 memcpy(to, skb->data + offset, copy);
1092 if ((len -= copy) == 0)
1093 return 0;
1094 offset += copy;
1095 to += copy;
1096 }
1097
1098 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1099 int end;
1100
1101 BUG_TRAP(start <= offset + len);
1102
1103 end = start + skb_shinfo(skb)->frags[i].size;
1104 if ((copy = end - offset) > 0) {
1105 u8 *vaddr;
1106
1107 if (copy > len)
1108 copy = len;
1109
1110 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1111 memcpy(to,
1112 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1113 offset - start, copy);
1114 kunmap_skb_frag(vaddr);
1115
1116 if ((len -= copy) == 0)
1117 return 0;
1118 offset += copy;
1119 to += copy;
1120 }
1121 start = end;
1122 }
1123
1124 if (skb_shinfo(skb)->frag_list) {
1125 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1126
1127 for (; list; list = list->next) {
1128 int end;
1129
1130 BUG_TRAP(start <= offset + len);
1131
1132 end = start + list->len;
1133 if ((copy = end - offset) > 0) {
1134 if (copy > len)
1135 copy = len;
1136 if (skb_copy_bits(list, offset - start,
1137 to, copy))
1138 goto fault;
1139 if ((len -= copy) == 0)
1140 return 0;
1141 offset += copy;
1142 to += copy;
1143 }
1144 start = end;
1145 }
1146 }
1147 if (!len)
1148 return 0;
1149
1150fault:
1151 return -EFAULT;
1152}
1153
Herbert Xu357b40a2005-04-19 22:30:14 -07001154/**
1155 * skb_store_bits - store bits from kernel buffer to skb
1156 * @skb: destination buffer
1157 * @offset: offset in destination
1158 * @from: source buffer
1159 * @len: number of bytes to copy
1160 *
1161 * Copy the specified number of bytes from the source buffer to the
1162 * destination skb. This function handles all the messy bits of
1163 * traversing fragment lists and such.
1164 */
1165
1166int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1167{
1168 int i, copy;
1169 int start = skb_headlen(skb);
1170
1171 if (offset > (int)skb->len - len)
1172 goto fault;
1173
1174 if ((copy = start - offset) > 0) {
1175 if (copy > len)
1176 copy = len;
1177 memcpy(skb->data + offset, from, copy);
1178 if ((len -= copy) == 0)
1179 return 0;
1180 offset += copy;
1181 from += copy;
1182 }
1183
1184 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1185 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1186 int end;
1187
1188 BUG_TRAP(start <= offset + len);
1189
1190 end = start + frag->size;
1191 if ((copy = end - offset) > 0) {
1192 u8 *vaddr;
1193
1194 if (copy > len)
1195 copy = len;
1196
1197 vaddr = kmap_skb_frag(frag);
1198 memcpy(vaddr + frag->page_offset + offset - start,
1199 from, copy);
1200 kunmap_skb_frag(vaddr);
1201
1202 if ((len -= copy) == 0)
1203 return 0;
1204 offset += copy;
1205 from += copy;
1206 }
1207 start = end;
1208 }
1209
1210 if (skb_shinfo(skb)->frag_list) {
1211 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1212
1213 for (; list; list = list->next) {
1214 int end;
1215
1216 BUG_TRAP(start <= offset + len);
1217
1218 end = start + list->len;
1219 if ((copy = end - offset) > 0) {
1220 if (copy > len)
1221 copy = len;
1222 if (skb_store_bits(list, offset - start,
1223 from, copy))
1224 goto fault;
1225 if ((len -= copy) == 0)
1226 return 0;
1227 offset += copy;
1228 from += copy;
1229 }
1230 start = end;
1231 }
1232 }
1233 if (!len)
1234 return 0;
1235
1236fault:
1237 return -EFAULT;
1238}
1239
1240EXPORT_SYMBOL(skb_store_bits);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/* Checksum skb data. */
1243
Al Viro2bbbc862006-11-14 21:37:14 -08001244__wsum skb_checksum(const struct sk_buff *skb, int offset,
1245 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 int start = skb_headlen(skb);
1248 int i, copy = start - offset;
1249 int pos = 0;
1250
1251 /* Checksum header. */
1252 if (copy > 0) {
1253 if (copy > len)
1254 copy = len;
1255 csum = csum_partial(skb->data + offset, copy, csum);
1256 if ((len -= copy) == 0)
1257 return csum;
1258 offset += copy;
1259 pos = copy;
1260 }
1261
1262 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1263 int end;
1264
1265 BUG_TRAP(start <= offset + len);
1266
1267 end = start + skb_shinfo(skb)->frags[i].size;
1268 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001269 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 u8 *vaddr;
1271 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1272
1273 if (copy > len)
1274 copy = len;
1275 vaddr = kmap_skb_frag(frag);
1276 csum2 = csum_partial(vaddr + frag->page_offset +
1277 offset - start, copy, 0);
1278 kunmap_skb_frag(vaddr);
1279 csum = csum_block_add(csum, csum2, pos);
1280 if (!(len -= copy))
1281 return csum;
1282 offset += copy;
1283 pos += copy;
1284 }
1285 start = end;
1286 }
1287
1288 if (skb_shinfo(skb)->frag_list) {
1289 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1290
1291 for (; list; list = list->next) {
1292 int end;
1293
1294 BUG_TRAP(start <= offset + len);
1295
1296 end = start + list->len;
1297 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001298 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (copy > len)
1300 copy = len;
1301 csum2 = skb_checksum(list, offset - start,
1302 copy, 0);
1303 csum = csum_block_add(csum, csum2, pos);
1304 if ((len -= copy) == 0)
1305 return csum;
1306 offset += copy;
1307 pos += copy;
1308 }
1309 start = end;
1310 }
1311 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001312 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 return csum;
1315}
1316
1317/* Both of above in one bottle. */
1318
Al Viro81d77662006-11-14 21:37:33 -08001319__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1320 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 int start = skb_headlen(skb);
1323 int i, copy = start - offset;
1324 int pos = 0;
1325
1326 /* Copy header. */
1327 if (copy > 0) {
1328 if (copy > len)
1329 copy = len;
1330 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1331 copy, csum);
1332 if ((len -= copy) == 0)
1333 return csum;
1334 offset += copy;
1335 to += copy;
1336 pos = copy;
1337 }
1338
1339 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1340 int end;
1341
1342 BUG_TRAP(start <= offset + len);
1343
1344 end = start + skb_shinfo(skb)->frags[i].size;
1345 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001346 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 u8 *vaddr;
1348 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1349
1350 if (copy > len)
1351 copy = len;
1352 vaddr = kmap_skb_frag(frag);
1353 csum2 = csum_partial_copy_nocheck(vaddr +
1354 frag->page_offset +
1355 offset - start, to,
1356 copy, 0);
1357 kunmap_skb_frag(vaddr);
1358 csum = csum_block_add(csum, csum2, pos);
1359 if (!(len -= copy))
1360 return csum;
1361 offset += copy;
1362 to += copy;
1363 pos += copy;
1364 }
1365 start = end;
1366 }
1367
1368 if (skb_shinfo(skb)->frag_list) {
1369 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1370
1371 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001372 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int end;
1374
1375 BUG_TRAP(start <= offset + len);
1376
1377 end = start + list->len;
1378 if ((copy = end - offset) > 0) {
1379 if (copy > len)
1380 copy = len;
1381 csum2 = skb_copy_and_csum_bits(list,
1382 offset - start,
1383 to, copy, 0);
1384 csum = csum_block_add(csum, csum2, pos);
1385 if ((len -= copy) == 0)
1386 return csum;
1387 offset += copy;
1388 to += copy;
1389 pos += copy;
1390 }
1391 start = end;
1392 }
1393 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001394 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return csum;
1396}
1397
1398void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1399{
Al Virod3bc23e2006-11-14 21:24:49 -08001400 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 long csstart;
1402
Patrick McHardy84fa7932006-08-29 16:44:56 -07001403 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 csstart = skb->h.raw - skb->data;
1405 else
1406 csstart = skb_headlen(skb);
1407
Kris Katterjohn09a62662006-01-08 22:24:28 -08001408 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 memcpy(to, skb->data, csstart);
1411
1412 csum = 0;
1413 if (csstart != skb->len)
1414 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1415 skb->len - csstart, 0);
1416
Patrick McHardy84fa7932006-08-29 16:44:56 -07001417 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001418 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Al Virod3bc23e2006-11-14 21:24:49 -08001420 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422}
1423
1424/**
1425 * skb_dequeue - remove from the head of the queue
1426 * @list: list to dequeue from
1427 *
1428 * Remove the head of the list. The list lock is taken so the function
1429 * may be used safely with other locking list functions. The head item is
1430 * returned or %NULL if the list is empty.
1431 */
1432
1433struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1434{
1435 unsigned long flags;
1436 struct sk_buff *result;
1437
1438 spin_lock_irqsave(&list->lock, flags);
1439 result = __skb_dequeue(list);
1440 spin_unlock_irqrestore(&list->lock, flags);
1441 return result;
1442}
1443
1444/**
1445 * skb_dequeue_tail - remove from the tail of the queue
1446 * @list: list to dequeue from
1447 *
1448 * Remove the tail of the list. The list lock is taken so the function
1449 * may be used safely with other locking list functions. The tail item is
1450 * returned or %NULL if the list is empty.
1451 */
1452struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1453{
1454 unsigned long flags;
1455 struct sk_buff *result;
1456
1457 spin_lock_irqsave(&list->lock, flags);
1458 result = __skb_dequeue_tail(list);
1459 spin_unlock_irqrestore(&list->lock, flags);
1460 return result;
1461}
1462
1463/**
1464 * skb_queue_purge - empty a list
1465 * @list: list to empty
1466 *
1467 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1468 * the list and one reference dropped. This function takes the list
1469 * lock and is atomic with respect to other list locking functions.
1470 */
1471void skb_queue_purge(struct sk_buff_head *list)
1472{
1473 struct sk_buff *skb;
1474 while ((skb = skb_dequeue(list)) != NULL)
1475 kfree_skb(skb);
1476}
1477
1478/**
1479 * skb_queue_head - queue a buffer at the list head
1480 * @list: list to use
1481 * @newsk: buffer to queue
1482 *
1483 * Queue a buffer at the start of the list. This function takes the
1484 * list lock and can be used safely with other locking &sk_buff functions
1485 * safely.
1486 *
1487 * A buffer cannot be placed on two lists at the same time.
1488 */
1489void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1490{
1491 unsigned long flags;
1492
1493 spin_lock_irqsave(&list->lock, flags);
1494 __skb_queue_head(list, newsk);
1495 spin_unlock_irqrestore(&list->lock, flags);
1496}
1497
1498/**
1499 * skb_queue_tail - queue a buffer at the list tail
1500 * @list: list to use
1501 * @newsk: buffer to queue
1502 *
1503 * Queue a buffer at the tail of the list. This function takes the
1504 * list lock and can be used safely with other locking &sk_buff functions
1505 * safely.
1506 *
1507 * A buffer cannot be placed on two lists at the same time.
1508 */
1509void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1510{
1511 unsigned long flags;
1512
1513 spin_lock_irqsave(&list->lock, flags);
1514 __skb_queue_tail(list, newsk);
1515 spin_unlock_irqrestore(&list->lock, flags);
1516}
David S. Miller8728b832005-08-09 19:25:21 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/**
1519 * skb_unlink - remove a buffer from a list
1520 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001521 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 *
David S. Miller8728b832005-08-09 19:25:21 -07001523 * Remove a packet from a list. The list locks are taken and this
1524 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 *
David S. Miller8728b832005-08-09 19:25:21 -07001526 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
David S. Miller8728b832005-08-09 19:25:21 -07001528void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
David S. Miller8728b832005-08-09 19:25:21 -07001530 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
David S. Miller8728b832005-08-09 19:25:21 -07001532 spin_lock_irqsave(&list->lock, flags);
1533 __skb_unlink(skb, list);
1534 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
1538 * skb_append - append a buffer
1539 * @old: buffer to insert after
1540 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001541 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *
1543 * Place a packet after a given packet in a list. The list locks are taken
1544 * and this function is atomic with respect to other list locked calls.
1545 * A buffer cannot be placed on two lists at the same time.
1546 */
David S. Miller8728b832005-08-09 19:25:21 -07001547void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 unsigned long flags;
1550
David S. Miller8728b832005-08-09 19:25:21 -07001551 spin_lock_irqsave(&list->lock, flags);
1552 __skb_append(old, newsk, list);
1553 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556
1557/**
1558 * skb_insert - insert a buffer
1559 * @old: buffer to insert before
1560 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001561 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 *
David S. Miller8728b832005-08-09 19:25:21 -07001563 * Place a packet before a given packet in a list. The list locks are
1564 * taken and this function is atomic with respect to other list locked
1565 * calls.
1566 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * A buffer cannot be placed on two lists at the same time.
1568 */
David S. Miller8728b832005-08-09 19:25:21 -07001569void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 unsigned long flags;
1572
David S. Miller8728b832005-08-09 19:25:21 -07001573 spin_lock_irqsave(&list->lock, flags);
1574 __skb_insert(newsk, old->prev, old, list);
1575 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578#if 0
1579/*
1580 * Tune the memory allocator for a new MTU size.
1581 */
1582void skb_add_mtu(int mtu)
1583{
1584 /* Must match allocation in alloc_skb */
1585 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1586
1587 kmem_add_cache_size(mtu);
1588}
1589#endif
1590
1591static inline void skb_split_inside_header(struct sk_buff *skb,
1592 struct sk_buff* skb1,
1593 const u32 len, const int pos)
1594{
1595 int i;
1596
1597 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1598
1599 /* And move data appendix as is. */
1600 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1601 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1602
1603 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1604 skb_shinfo(skb)->nr_frags = 0;
1605 skb1->data_len = skb->data_len;
1606 skb1->len += skb1->data_len;
1607 skb->data_len = 0;
1608 skb->len = len;
1609 skb->tail = skb->data + len;
1610}
1611
1612static inline void skb_split_no_header(struct sk_buff *skb,
1613 struct sk_buff* skb1,
1614 const u32 len, int pos)
1615{
1616 int i, k = 0;
1617 const int nfrags = skb_shinfo(skb)->nr_frags;
1618
1619 skb_shinfo(skb)->nr_frags = 0;
1620 skb1->len = skb1->data_len = skb->len - len;
1621 skb->len = len;
1622 skb->data_len = len - pos;
1623
1624 for (i = 0; i < nfrags; i++) {
1625 int size = skb_shinfo(skb)->frags[i].size;
1626
1627 if (pos + size > len) {
1628 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1629
1630 if (pos < len) {
1631 /* Split frag.
1632 * We have two variants in this case:
1633 * 1. Move all the frag to the second
1634 * part, if it is possible. F.e.
1635 * this approach is mandatory for TUX,
1636 * where splitting is expensive.
1637 * 2. Split is accurately. We make this.
1638 */
1639 get_page(skb_shinfo(skb)->frags[i].page);
1640 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1641 skb_shinfo(skb1)->frags[0].size -= len - pos;
1642 skb_shinfo(skb)->frags[i].size = len - pos;
1643 skb_shinfo(skb)->nr_frags++;
1644 }
1645 k++;
1646 } else
1647 skb_shinfo(skb)->nr_frags++;
1648 pos += size;
1649 }
1650 skb_shinfo(skb1)->nr_frags = k;
1651}
1652
1653/**
1654 * skb_split - Split fragmented skb to two parts at length len.
1655 * @skb: the buffer to split
1656 * @skb1: the buffer to receive the second part
1657 * @len: new length for skb
1658 */
1659void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1660{
1661 int pos = skb_headlen(skb);
1662
1663 if (len < pos) /* Split line is inside header. */
1664 skb_split_inside_header(skb, skb1, len, pos);
1665 else /* Second chunk has no header, nothing to copy. */
1666 skb_split_no_header(skb, skb1, len, pos);
1667}
1668
Thomas Graf677e90e2005-06-23 20:59:51 -07001669/**
1670 * skb_prepare_seq_read - Prepare a sequential read of skb data
1671 * @skb: the buffer to read
1672 * @from: lower offset of data to be read
1673 * @to: upper offset of data to be read
1674 * @st: state variable
1675 *
1676 * Initializes the specified state variable. Must be called before
1677 * invoking skb_seq_read() for the first time.
1678 */
1679void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1680 unsigned int to, struct skb_seq_state *st)
1681{
1682 st->lower_offset = from;
1683 st->upper_offset = to;
1684 st->root_skb = st->cur_skb = skb;
1685 st->frag_idx = st->stepped_offset = 0;
1686 st->frag_data = NULL;
1687}
1688
1689/**
1690 * skb_seq_read - Sequentially read skb data
1691 * @consumed: number of bytes consumed by the caller so far
1692 * @data: destination pointer for data to be returned
1693 * @st: state variable
1694 *
1695 * Reads a block of skb data at &consumed relative to the
1696 * lower offset specified to skb_prepare_seq_read(). Assigns
1697 * the head of the data block to &data and returns the length
1698 * of the block or 0 if the end of the skb data or the upper
1699 * offset has been reached.
1700 *
1701 * The caller is not required to consume all of the data
1702 * returned, i.e. &consumed is typically set to the number
1703 * of bytes already consumed and the next call to
1704 * skb_seq_read() will return the remaining part of the block.
1705 *
1706 * Note: The size of each block of data returned can be arbitary,
1707 * this limitation is the cost for zerocopy seqeuental
1708 * reads of potentially non linear data.
1709 *
1710 * Note: Fragment lists within fragments are not implemented
1711 * at the moment, state->root_skb could be replaced with
1712 * a stack for this purpose.
1713 */
1714unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1715 struct skb_seq_state *st)
1716{
1717 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1718 skb_frag_t *frag;
1719
1720 if (unlikely(abs_offset >= st->upper_offset))
1721 return 0;
1722
1723next_skb:
1724 block_limit = skb_headlen(st->cur_skb);
1725
1726 if (abs_offset < block_limit) {
1727 *data = st->cur_skb->data + abs_offset;
1728 return block_limit - abs_offset;
1729 }
1730
1731 if (st->frag_idx == 0 && !st->frag_data)
1732 st->stepped_offset += skb_headlen(st->cur_skb);
1733
1734 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1735 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1736 block_limit = frag->size + st->stepped_offset;
1737
1738 if (abs_offset < block_limit) {
1739 if (!st->frag_data)
1740 st->frag_data = kmap_skb_frag(frag);
1741
1742 *data = (u8 *) st->frag_data + frag->page_offset +
1743 (abs_offset - st->stepped_offset);
1744
1745 return block_limit - abs_offset;
1746 }
1747
1748 if (st->frag_data) {
1749 kunmap_skb_frag(st->frag_data);
1750 st->frag_data = NULL;
1751 }
1752
1753 st->frag_idx++;
1754 st->stepped_offset += frag->size;
1755 }
1756
1757 if (st->cur_skb->next) {
1758 st->cur_skb = st->cur_skb->next;
1759 st->frag_idx = 0;
1760 goto next_skb;
1761 } else if (st->root_skb == st->cur_skb &&
1762 skb_shinfo(st->root_skb)->frag_list) {
1763 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1764 goto next_skb;
1765 }
1766
1767 return 0;
1768}
1769
1770/**
1771 * skb_abort_seq_read - Abort a sequential read of skb data
1772 * @st: state variable
1773 *
1774 * Must be called if skb_seq_read() was not called until it
1775 * returned 0.
1776 */
1777void skb_abort_seq_read(struct skb_seq_state *st)
1778{
1779 if (st->frag_data)
1780 kunmap_skb_frag(st->frag_data);
1781}
1782
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001783#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1784
1785static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1786 struct ts_config *conf,
1787 struct ts_state *state)
1788{
1789 return skb_seq_read(offset, text, TS_SKB_CB(state));
1790}
1791
1792static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1793{
1794 skb_abort_seq_read(TS_SKB_CB(state));
1795}
1796
1797/**
1798 * skb_find_text - Find a text pattern in skb data
1799 * @skb: the buffer to look in
1800 * @from: search offset
1801 * @to: search limit
1802 * @config: textsearch configuration
1803 * @state: uninitialized textsearch state variable
1804 *
1805 * Finds a pattern in the skb data according to the specified
1806 * textsearch configuration. Use textsearch_next() to retrieve
1807 * subsequent occurrences of the pattern. Returns the offset
1808 * to the first occurrence or UINT_MAX if no match was found.
1809 */
1810unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1811 unsigned int to, struct ts_config *config,
1812 struct ts_state *state)
1813{
Phil Oesterf72b9482006-06-26 00:00:57 -07001814 unsigned int ret;
1815
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001816 config->get_next_block = skb_ts_get_next_block;
1817 config->finish = skb_ts_finish;
1818
1819 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1820
Phil Oesterf72b9482006-06-26 00:00:57 -07001821 ret = textsearch_find(config, state);
1822 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001823}
1824
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001825/**
1826 * skb_append_datato_frags: - append the user data to a skb
1827 * @sk: sock structure
1828 * @skb: skb structure to be appened with user data.
1829 * @getfrag: call back function to be used for getting the user data
1830 * @from: pointer to user message iov
1831 * @length: length of the iov message
1832 *
1833 * Description: This procedure append the user data in the fragment part
1834 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1835 */
1836int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001837 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001838 int len, int odd, struct sk_buff *skb),
1839 void *from, int length)
1840{
1841 int frg_cnt = 0;
1842 skb_frag_t *frag = NULL;
1843 struct page *page = NULL;
1844 int copy, left;
1845 int offset = 0;
1846 int ret;
1847
1848 do {
1849 /* Return error if we don't have space for new frag */
1850 frg_cnt = skb_shinfo(skb)->nr_frags;
1851 if (frg_cnt >= MAX_SKB_FRAGS)
1852 return -EFAULT;
1853
1854 /* allocate a new page for next frag */
1855 page = alloc_pages(sk->sk_allocation, 0);
1856
1857 /* If alloc_page fails just return failure and caller will
1858 * free previous allocated pages by doing kfree_skb()
1859 */
1860 if (page == NULL)
1861 return -ENOMEM;
1862
1863 /* initialize the next frag */
1864 sk->sk_sndmsg_page = page;
1865 sk->sk_sndmsg_off = 0;
1866 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1867 skb->truesize += PAGE_SIZE;
1868 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1869
1870 /* get the new initialized frag */
1871 frg_cnt = skb_shinfo(skb)->nr_frags;
1872 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1873
1874 /* copy the user data to page */
1875 left = PAGE_SIZE - frag->page_offset;
1876 copy = (length > left)? left : length;
1877
1878 ret = getfrag(from, (page_address(frag->page) +
1879 frag->page_offset + frag->size),
1880 offset, copy, 0, skb);
1881 if (ret < 0)
1882 return -EFAULT;
1883
1884 /* copy was successful so update the size parameters */
1885 sk->sk_sndmsg_off += copy;
1886 frag->size += copy;
1887 skb->len += copy;
1888 skb->data_len += copy;
1889 offset += copy;
1890 length -= copy;
1891
1892 } while (length > 0);
1893
1894 return 0;
1895}
1896
Herbert Xucbb042f2006-03-20 22:43:56 -08001897/**
1898 * skb_pull_rcsum - pull skb and update receive checksum
1899 * @skb: buffer to update
1900 * @start: start of data before pull
1901 * @len: length of data pulled
1902 *
1903 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001904 * update the CHECKSUM_COMPLETE checksum. It should be used on
1905 * receive path processing instead of skb_pull unless you know
1906 * that the checksum difference is zero (e.g., a valid IP header)
1907 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001908 */
1909unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1910{
1911 BUG_ON(len > skb->len);
1912 skb->len -= len;
1913 BUG_ON(skb->len < skb->data_len);
1914 skb_postpull_rcsum(skb, skb->data, len);
1915 return skb->data += len;
1916}
1917
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001918EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1919
Herbert Xuf4c50d92006-06-22 03:02:40 -07001920/**
1921 * skb_segment - Perform protocol segmentation on skb.
1922 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001923 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001924 *
1925 * This function performs segmentation on the given skb. It returns
1926 * the segment at the given position. It returns NULL if there are
1927 * no more segments to generate, or when an error is encountered.
1928 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001929struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001930{
1931 struct sk_buff *segs = NULL;
1932 struct sk_buff *tail = NULL;
1933 unsigned int mss = skb_shinfo(skb)->gso_size;
1934 unsigned int doffset = skb->data - skb->mac.raw;
1935 unsigned int offset = doffset;
1936 unsigned int headroom;
1937 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001938 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001939 int nfrags = skb_shinfo(skb)->nr_frags;
1940 int err = -ENOMEM;
1941 int i = 0;
1942 int pos;
1943
1944 __skb_push(skb, doffset);
1945 headroom = skb_headroom(skb);
1946 pos = skb_headlen(skb);
1947
1948 do {
1949 struct sk_buff *nskb;
1950 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001951 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001952 int k;
1953 int size;
1954
1955 len = skb->len - offset;
1956 if (len > mss)
1957 len = mss;
1958
1959 hsize = skb_headlen(skb) - offset;
1960 if (hsize < 0)
1961 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001962 if (hsize > len || !sg)
1963 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001964
Herbert Xuc8884ed2006-10-29 15:59:41 -08001965 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001966 if (unlikely(!nskb))
1967 goto err;
1968
1969 if (segs)
1970 tail->next = nskb;
1971 else
1972 segs = nskb;
1973 tail = nskb;
1974
1975 nskb->dev = skb->dev;
1976 nskb->priority = skb->priority;
1977 nskb->protocol = skb->protocol;
1978 nskb->dst = dst_clone(skb->dst);
1979 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1980 nskb->pkt_type = skb->pkt_type;
1981 nskb->mac_len = skb->mac_len;
1982
1983 skb_reserve(nskb, headroom);
1984 nskb->mac.raw = nskb->data;
1985 nskb->nh.raw = nskb->data + skb->mac_len;
1986 nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1987 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1988
1989 if (!sg) {
1990 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1991 skb_put(nskb, len),
1992 len, 0);
1993 continue;
1994 }
1995
1996 frag = skb_shinfo(nskb)->frags;
1997 k = 0;
1998
Patrick McHardy84fa7932006-08-29 16:44:56 -07001999 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002000 nskb->csum = skb->csum;
2001 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
2002
2003 while (pos < offset + len) {
2004 BUG_ON(i >= nfrags);
2005
2006 *frag = skb_shinfo(skb)->frags[i];
2007 get_page(frag->page);
2008 size = frag->size;
2009
2010 if (pos < offset) {
2011 frag->page_offset += offset - pos;
2012 frag->size -= offset - pos;
2013 }
2014
2015 k++;
2016
2017 if (pos + size <= offset + len) {
2018 i++;
2019 pos += size;
2020 } else {
2021 frag->size -= pos + size - (offset + len);
2022 break;
2023 }
2024
2025 frag++;
2026 }
2027
2028 skb_shinfo(nskb)->nr_frags = k;
2029 nskb->data_len = len - hsize;
2030 nskb->len += nskb->data_len;
2031 nskb->truesize += nskb->data_len;
2032 } while ((offset += len) < skb->len);
2033
2034 return segs;
2035
2036err:
2037 while ((skb = segs)) {
2038 segs = skb->next;
2039 kfree(skb);
2040 }
2041 return ERR_PTR(err);
2042}
2043
2044EXPORT_SYMBOL_GPL(skb_segment);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046void __init skb_init(void)
2047{
2048 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2049 sizeof(struct sk_buff),
2050 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002051 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002053 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2054 (2*sizeof(struct sk_buff)) +
2055 sizeof(atomic_t),
2056 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002057 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002058 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
2061EXPORT_SYMBOL(___pskb_trim);
2062EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002063EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002065EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002066EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067EXPORT_SYMBOL(pskb_copy);
2068EXPORT_SYMBOL(pskb_expand_head);
2069EXPORT_SYMBOL(skb_checksum);
2070EXPORT_SYMBOL(skb_clone);
2071EXPORT_SYMBOL(skb_clone_fraglist);
2072EXPORT_SYMBOL(skb_copy);
2073EXPORT_SYMBOL(skb_copy_and_csum_bits);
2074EXPORT_SYMBOL(skb_copy_and_csum_dev);
2075EXPORT_SYMBOL(skb_copy_bits);
2076EXPORT_SYMBOL(skb_copy_expand);
2077EXPORT_SYMBOL(skb_over_panic);
2078EXPORT_SYMBOL(skb_pad);
2079EXPORT_SYMBOL(skb_realloc_headroom);
2080EXPORT_SYMBOL(skb_under_panic);
2081EXPORT_SYMBOL(skb_dequeue);
2082EXPORT_SYMBOL(skb_dequeue_tail);
2083EXPORT_SYMBOL(skb_insert);
2084EXPORT_SYMBOL(skb_queue_purge);
2085EXPORT_SYMBOL(skb_queue_head);
2086EXPORT_SYMBOL(skb_queue_tail);
2087EXPORT_SYMBOL(skb_unlink);
2088EXPORT_SYMBOL(skb_append);
2089EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002090EXPORT_SYMBOL(skb_prepare_seq_read);
2091EXPORT_SYMBOL(skb_seq_read);
2092EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002093EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002094EXPORT_SYMBOL(skb_append_datato_frags);