blob: 2c218a0808b4d5fa0a09e90ae8edac6c2b27affe [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/types.h>
41#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/mm.h>
43#include <linux/interrupt.h>
44#include <linux/in.h>
45#include <linux/inet.h>
46#include <linux/slab.h>
47#include <linux/netdevice.h>
48#ifdef CONFIG_NET_CLS_ACT
49#include <net/pkt_sched.h>
50#endif
51#include <linux/string.h>
52#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080053#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/cache.h>
55#include <linux/rtnetlink.h>
56#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070057#include <linux/scatterlist.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
Jens Axboe9c55e012007-11-06 23:30:13 -080073static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
74 struct pipe_buffer *buf)
75{
76 struct sk_buff *skb = (struct sk_buff *) buf->private;
77
78 kfree_skb(skb);
79}
80
81static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
83{
84 struct sk_buff *skb = (struct sk_buff *) buf->private;
85
86 skb_get(skb);
87}
88
89static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
91{
92 return 1;
93}
94
95
96/* Pipe buffer operations for a socket. */
97static struct pipe_buf_operations sock_pipe_buf_ops = {
98 .can_merge = 0,
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
111 */
112
113/**
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
118 *
119 * Out of line support code for skb_put(). Not user callable.
120 */
121void skb_over_panic(struct sk_buff *skb, int sz, void *here)
122{
Patrick McHardy26095452005-04-21 16:43:02 -0700123 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700124 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700125 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700126 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700127 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 BUG();
129}
130
131/**
132 * skb_under_panic - private function
133 * @skb: buffer
134 * @sz: size
135 * @here: address
136 *
137 * Out of line support code for skb_push(). Not user callable.
138 */
139
140void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141{
Patrick McHardy26095452005-04-21 16:43:02 -0700142 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700143 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700144 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700145 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700146 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 BUG();
148}
149
David S. Millerdc6de332006-04-20 00:10:50 -0700150void skb_truesize_bug(struct sk_buff *skb)
151{
152 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
153 "len=%u, sizeof(sk_buff)=%Zd\n",
154 skb->truesize, skb->len, sizeof(struct sk_buff));
155}
156EXPORT_SYMBOL(skb_truesize_bug);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Allocate a new skbuff. We do this ourselves so we can fill in a few
159 * 'private' fields and also do memory statistics to find all the
160 * [BEEP] leaks.
161 *
162 */
163
164/**
David S. Millerd179cd12005-08-17 14:57:30 -0700165 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @size: size to allocate
167 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700168 * @fclone: allocate from fclone cache instead of head cache
169 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800170 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Allocate a new &sk_buff. The returned buffer has no headroom and a
173 * tail room of size bytes. The object has a reference count of one.
174 * The return is the buffer. On a failure the return is %NULL.
175 *
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 * %GFP_ATOMIC.
178 */
Al Virodd0fc662005-10-07 07:46:04 +0100179struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800180 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Christoph Lametere18b8902006-12-06 20:33:20 -0800182 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800183 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct sk_buff *skb;
185 u8 *data;
186
Herbert Xu8798b3f2006-01-23 16:32:45 -0800187 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800190 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!skb)
192 goto out;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800195 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
196 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!data)
198 goto nodata;
199
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300200 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700201 * Only clear those fields we need to clear, not those that we will
202 * actually initialise below. Hence, don't put any more fields after
203 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300204 */
205 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->truesize = size + sizeof(struct sk_buff);
207 atomic_set(&skb->users, 1);
208 skb->head = data;
209 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700210 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700211 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 /* make sure we initialize shinfo sequentially */
213 shinfo = skb_shinfo(skb);
214 atomic_set(&shinfo->dataref, 1);
215 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700216 shinfo->gso_size = 0;
217 shinfo->gso_segs = 0;
218 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800219 shinfo->ip6_frag_id = 0;
220 shinfo->frag_list = NULL;
221
David S. Millerd179cd12005-08-17 14:57:30 -0700222 if (fclone) {
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David S. Millerd179cd12005-08-17 14:57:30 -0700226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
228
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231out:
232 return skb;
233nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800234 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 skb = NULL;
236 goto out;
237}
238
239/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
244 *
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
249 *
250 * %NULL is returned if there is no free memory.
251 */
252struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700256 struct sk_buff *skb;
257
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700259 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700260 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700261 skb->dev = dev;
262 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700263 return skb;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700266/**
267 * dev_alloc_skb - allocate an skbuff for receiving
268 * @length: length to allocate
269 *
270 * Allocate a new &sk_buff and assign it a usage count of one. The
271 * buffer has unspecified headroom built in. Users should allocate
272 * the headroom they think they need without accounting for the
273 * built in space. The built in space is used for optimisations.
274 *
275 * %NULL is returned if there is no free memory. Although this function
276 * allocates memory it can be called from an interrupt.
277 */
278struct sk_buff *dev_alloc_skb(unsigned int length)
279{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700280 /*
281 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700282 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700283 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700284 return __dev_alloc_skb(length, GFP_ATOMIC);
285}
286EXPORT_SYMBOL(dev_alloc_skb);
287
Herbert Xu27b437c2006-07-13 19:26:39 -0700288static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Herbert Xu27b437c2006-07-13 19:26:39 -0700290 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Herbert Xu27b437c2006-07-13 19:26:39 -0700292 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 do {
295 struct sk_buff *this = list;
296 list = list->next;
297 kfree_skb(this);
298 } while (list);
299}
300
Herbert Xu27b437c2006-07-13 19:26:39 -0700301static inline void skb_drop_fraglist(struct sk_buff *skb)
302{
303 skb_drop_list(&skb_shinfo(skb)->frag_list);
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void skb_clone_fraglist(struct sk_buff *skb)
307{
308 struct sk_buff *list;
309
310 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
311 skb_get(list);
312}
313
Adrian Bunk5bba1712006-06-29 13:02:35 -0700314static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 if (!skb->cloned ||
317 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
318 &skb_shinfo(skb)->dataref)) {
319 if (skb_shinfo(skb)->nr_frags) {
320 int i;
321 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
322 put_page(skb_shinfo(skb)->frags[i].page);
323 }
324
325 if (skb_shinfo(skb)->frag_list)
326 skb_drop_fraglist(skb);
327
328 kfree(skb->head);
329 }
330}
331
332/*
333 * Free an skbuff by memory without cleaning the state.
334 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800335static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
David S. Millerd179cd12005-08-17 14:57:30 -0700337 struct sk_buff *other;
338 atomic_t *fclone_ref;
339
David S. Millerd179cd12005-08-17 14:57:30 -0700340 switch (skb->fclone) {
341 case SKB_FCLONE_UNAVAILABLE:
342 kmem_cache_free(skbuff_head_cache, skb);
343 break;
344
345 case SKB_FCLONE_ORIG:
346 fclone_ref = (atomic_t *) (skb + 2);
347 if (atomic_dec_and_test(fclone_ref))
348 kmem_cache_free(skbuff_fclone_cache, skb);
349 break;
350
351 case SKB_FCLONE_CLONE:
352 fclone_ref = (atomic_t *) (skb + 1);
353 other = skb - 1;
354
355 /* The clone portion is available for
356 * fast-cloning again.
357 */
358 skb->fclone = SKB_FCLONE_UNAVAILABLE;
359
360 if (atomic_dec_and_test(fclone_ref))
361 kmem_cache_free(skbuff_fclone_cache, other);
362 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700366static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 dst_release(skb->dst);
369#ifdef CONFIG_XFRM
370 secpath_put(skb->sp);
371#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700372 if (skb->destructor) {
373 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb->destructor(skb);
375 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800376#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700377 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800378 nf_conntrack_put_reasm(skb->nfct_reasm);
379#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#ifdef CONFIG_BRIDGE_NETFILTER
381 nf_bridge_put(skb->nf_bridge);
382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* XXX: IS this still necessary? - JHS */
384#ifdef CONFIG_NET_SCHED
385 skb->tc_index = 0;
386#ifdef CONFIG_NET_CLS_ACT
387 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388#endif
389#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700390}
391
392/* Free everything but the sk_buff shell. */
393static void skb_release_all(struct sk_buff *skb)
394{
395 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800396 skb_release_data(skb);
397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Herbert Xu2d4baff2007-11-26 23:11:19 +0800399/**
400 * __kfree_skb - private function
401 * @skb: buffer
402 *
403 * Free an sk_buff. Release anything attached to the buffer.
404 * Clean the state. This is an internal helper function. Users should
405 * always call kfree_skb
406 */
407
408void __kfree_skb(struct sk_buff *skb)
409{
410 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 kfree_skbmem(skb);
412}
413
414/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800415 * kfree_skb - free an sk_buff
416 * @skb: buffer to free
417 *
418 * Drop a reference to the buffer and free it if the usage count has
419 * hit zero.
420 */
421void kfree_skb(struct sk_buff *skb)
422{
423 if (unlikely(!skb))
424 return;
425 if (likely(atomic_read(&skb->users) == 1))
426 smp_rmb();
427 else if (likely(!atomic_dec_and_test(&skb->users)))
428 return;
429 __kfree_skb(skb);
430}
431
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700432int skb_recycle_check(struct sk_buff *skb, int skb_size)
433{
434 struct skb_shared_info *shinfo;
435
436 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
437 return 0;
438
439 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
440 if (skb_end_pointer(skb) - skb->head < skb_size)
441 return 0;
442
443 if (skb_shared(skb) || skb_cloned(skb))
444 return 0;
445
446 skb_release_head_state(skb);
447 shinfo = skb_shinfo(skb);
448 atomic_set(&shinfo->dataref, 1);
449 shinfo->nr_frags = 0;
450 shinfo->gso_size = 0;
451 shinfo->gso_segs = 0;
452 shinfo->gso_type = 0;
453 shinfo->ip6_frag_id = 0;
454 shinfo->frag_list = NULL;
455
456 memset(skb, 0, offsetof(struct sk_buff, tail));
457 skb_reset_tail_pointer(skb);
458 skb->data = skb->head + NET_SKB_PAD;
459
460 return 1;
461}
462EXPORT_SYMBOL(skb_recycle_check);
463
Herbert Xudec18812007-10-14 00:37:30 -0700464static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
465{
466 new->tstamp = old->tstamp;
467 new->dev = old->dev;
468 new->transport_header = old->transport_header;
469 new->network_header = old->network_header;
470 new->mac_header = old->mac_header;
471 new->dst = dst_clone(old->dst);
472#ifdef CONFIG_INET
473 new->sp = secpath_get(old->sp);
474#endif
475 memcpy(new->cb, old->cb, sizeof(old->cb));
476 new->csum_start = old->csum_start;
477 new->csum_offset = old->csum_offset;
478 new->local_df = old->local_df;
479 new->pkt_type = old->pkt_type;
480 new->ip_summed = old->ip_summed;
481 skb_copy_queue_mapping(new, old);
482 new->priority = old->priority;
483#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
484 new->ipvs_property = old->ipvs_property;
485#endif
486 new->protocol = old->protocol;
487 new->mark = old->mark;
488 __nf_copy(new, old);
489#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
490 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
491 new->nf_trace = old->nf_trace;
492#endif
493#ifdef CONFIG_NET_SCHED
494 new->tc_index = old->tc_index;
495#ifdef CONFIG_NET_CLS_ACT
496 new->tc_verd = old->tc_verd;
497#endif
498#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700499 new->vlan_tci = old->vlan_tci;
500
Herbert Xudec18812007-10-14 00:37:30 -0700501 skb_copy_secmark(new, old);
502}
503
Herbert Xue0053ec2007-10-14 00:37:52 -0700504static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#define C(x) n->x = skb->x
507
508 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700510 __copy_skb_header(n, skb);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 C(len);
513 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700514 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700515 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800516 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800519 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 C(tail);
521 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800522 C(head);
523 C(data);
524 C(truesize);
Johannes Bergd0f09802008-07-29 11:32:07 +0200525#if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
526 C(do_not_encrypt);
527#endif
Paul Moore02f1c892008-01-07 21:56:41 -0800528 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 atomic_inc(&(skb_shinfo(skb)->dataref));
531 skb->cloned = 1;
532
533 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700534#undef C
535}
536
537/**
538 * skb_morph - morph one skb into another
539 * @dst: the skb to receive the contents
540 * @src: the skb to supply the contents
541 *
542 * This is identical to skb_clone except that the target skb is
543 * supplied by the user.
544 *
545 * The target skb is returned upon exit.
546 */
547struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
548{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800549 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700550 return __skb_clone(dst, src);
551}
552EXPORT_SYMBOL_GPL(skb_morph);
553
554/**
555 * skb_clone - duplicate an sk_buff
556 * @skb: buffer to clone
557 * @gfp_mask: allocation priority
558 *
559 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
560 * copies share the same packet data but not structure. The new
561 * buffer has a reference count of 1. If the allocation fails the
562 * function returns %NULL otherwise the new buffer is returned.
563 *
564 * If this function is called from an interrupt gfp_mask() must be
565 * %GFP_ATOMIC.
566 */
567
568struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
569{
570 struct sk_buff *n;
571
572 n = skb + 1;
573 if (skb->fclone == SKB_FCLONE_ORIG &&
574 n->fclone == SKB_FCLONE_UNAVAILABLE) {
575 atomic_t *fclone_ref = (atomic_t *) (n + 1);
576 n->fclone = SKB_FCLONE_CLONE;
577 atomic_inc(fclone_ref);
578 } else {
579 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
580 if (!n)
581 return NULL;
582 n->fclone = SKB_FCLONE_UNAVAILABLE;
583 }
584
585 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
589{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700590#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
592 * Shift between the two data areas in bytes
593 */
594 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700595#endif
Herbert Xudec18812007-10-14 00:37:30 -0700596
597 __copy_skb_header(new, old);
598
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700599#ifndef NET_SKBUFF_DATA_USES_OFFSET
600 /* {transport,network,mac}_header are relative to skb->head */
601 new->transport_header += offset;
602 new->network_header += offset;
603 new->mac_header += offset;
604#endif
Herbert Xu79671682006-06-22 02:40:14 -0700605 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
606 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
607 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/**
611 * skb_copy - create private copy of an sk_buff
612 * @skb: buffer to copy
613 * @gfp_mask: allocation priority
614 *
615 * Make a copy of both an &sk_buff and its data. This is used when the
616 * caller wishes to modify the data and needs a private copy of the
617 * data to alter. Returns %NULL on failure or the pointer to the buffer
618 * on success. The returned buffer has a reference count of 1.
619 *
620 * As by-product this function converts non-linear &sk_buff to linear
621 * one, so that &sk_buff becomes completely private and caller is allowed
622 * to modify all the data of returned buffer. This means that this
623 * function is not recommended for use in circumstances when only
624 * header is going to be modified. Use pskb_copy() instead.
625 */
626
Al Virodd0fc662005-10-07 07:46:04 +0100627struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 int headerlen = skb->data - skb->head;
630 /*
631 * Allocate the copy buffer
632 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700633 struct sk_buff *n;
634#ifdef NET_SKBUFF_DATA_USES_OFFSET
635 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
636#else
637 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!n)
640 return NULL;
641
642 /* Set the data pointer */
643 skb_reserve(n, headerlen);
644 /* Set the tail pointer and length */
645 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
648 BUG();
649
650 copy_skb_header(n, skb);
651 return n;
652}
653
654
655/**
656 * pskb_copy - create copy of an sk_buff with private head.
657 * @skb: buffer to copy
658 * @gfp_mask: allocation priority
659 *
660 * Make a copy of both an &sk_buff and part of its data, located
661 * in header. Fragmented data remain shared. This is used when
662 * the caller wishes to modify only header of &sk_buff and needs
663 * private copy of the header to alter. Returns %NULL on failure
664 * or the pointer to the buffer on success.
665 * The returned buffer has a reference count of 1.
666 */
667
Al Virodd0fc662005-10-07 07:46:04 +0100668struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 /*
671 * Allocate the copy buffer
672 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700673 struct sk_buff *n;
674#ifdef NET_SKBUFF_DATA_USES_OFFSET
675 n = alloc_skb(skb->end, gfp_mask);
676#else
677 n = alloc_skb(skb->end - skb->head, gfp_mask);
678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!n)
680 goto out;
681
682 /* Set the data pointer */
683 skb_reserve(n, skb->data - skb->head);
684 /* Set the tail pointer and length */
685 skb_put(n, skb_headlen(skb));
686 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300687 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Herbert Xu25f484a2006-11-07 14:57:15 -0800689 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 n->data_len = skb->data_len;
691 n->len = skb->len;
692
693 if (skb_shinfo(skb)->nr_frags) {
694 int i;
695
696 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
697 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
698 get_page(skb_shinfo(n)->frags[i].page);
699 }
700 skb_shinfo(n)->nr_frags = i;
701 }
702
703 if (skb_shinfo(skb)->frag_list) {
704 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
705 skb_clone_fraglist(n);
706 }
707
708 copy_skb_header(n, skb);
709out:
710 return n;
711}
712
713/**
714 * pskb_expand_head - reallocate header of &sk_buff
715 * @skb: buffer to reallocate
716 * @nhead: room to add at head
717 * @ntail: room to add at tail
718 * @gfp_mask: allocation priority
719 *
720 * Expands (or creates identical copy, if &nhead and &ntail are zero)
721 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
722 * reference count of 1. Returns zero in the case of success or error,
723 * if expansion failed. In the last case, &sk_buff is not changed.
724 *
725 * All the pointers pointing into skb header may change and must be
726 * reloaded after call to this function.
727 */
728
Victor Fusco86a76ca2005-07-08 14:57:47 -0700729int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100730 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 int i;
733 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700734#ifdef NET_SKBUFF_DATA_USES_OFFSET
735 int size = nhead + skb->end + ntail;
736#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 long off;
740
741 if (skb_shared(skb))
742 BUG();
743
744 size = SKB_DATA_ALIGN(size);
745
746 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
747 if (!data)
748 goto nodata;
749
750 /* Copy only real data... and, alas, header. This should be
751 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700752#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700753 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700754#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700755 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700756#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700757 memcpy(data + size, skb_end_pointer(skb),
758 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
761 get_page(skb_shinfo(skb)->frags[i].page);
762
763 if (skb_shinfo(skb)->frag_list)
764 skb_clone_fraglist(skb);
765
766 skb_release_data(skb);
767
768 off = (data + nhead) - skb->head;
769
770 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700772#ifdef NET_SKBUFF_DATA_USES_OFFSET
773 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700774 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700775#else
776 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700777#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700778 /* {transport,network,mac}_header and tail are relative to skb->head */
779 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700780 skb->transport_header += off;
781 skb->network_header += off;
782 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700783 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700785 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 skb->nohdr = 0;
787 atomic_set(&skb_shinfo(skb)->dataref, 1);
788 return 0;
789
790nodata:
791 return -ENOMEM;
792}
793
794/* Make private copy of skb with writable head and some headroom */
795
796struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
797{
798 struct sk_buff *skb2;
799 int delta = headroom - skb_headroom(skb);
800
801 if (delta <= 0)
802 skb2 = pskb_copy(skb, GFP_ATOMIC);
803 else {
804 skb2 = skb_clone(skb, GFP_ATOMIC);
805 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
806 GFP_ATOMIC)) {
807 kfree_skb(skb2);
808 skb2 = NULL;
809 }
810 }
811 return skb2;
812}
813
814
815/**
816 * skb_copy_expand - copy and expand sk_buff
817 * @skb: buffer to copy
818 * @newheadroom: new free bytes at head
819 * @newtailroom: new free bytes at tail
820 * @gfp_mask: allocation priority
821 *
822 * Make a copy of both an &sk_buff and its data and while doing so
823 * allocate additional space.
824 *
825 * This is used when the caller wishes to modify the data and needs a
826 * private copy of the data to alter as well as more space for new fields.
827 * Returns %NULL on failure or the pointer to the buffer
828 * on success. The returned buffer has a reference count of 1.
829 *
830 * You must pass %GFP_ATOMIC as the allocation priority if this function
831 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700834 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100835 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 /*
838 * Allocate the copy buffer
839 */
840 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
841 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700842 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700844 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (!n)
847 return NULL;
848
849 skb_reserve(n, newheadroom);
850
851 /* Set the tail pointer and length */
852 skb_put(n, skb->len);
853
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700854 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 head_copy_off = 0;
856 if (newheadroom <= head_copy_len)
857 head_copy_len = newheadroom;
858 else
859 head_copy_off = newheadroom - head_copy_len;
860
861 /* Copy the linear header and data. */
862 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
863 skb->len + head_copy_len))
864 BUG();
865
866 copy_skb_header(n, skb);
867
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700868 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700869 n->csum_start += off;
870#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700871 n->transport_header += off;
872 n->network_header += off;
873 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700874#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return n;
877}
878
879/**
880 * skb_pad - zero pad the tail of an skb
881 * @skb: buffer to pad
882 * @pad: space to pad
883 *
884 * Ensure that a buffer is followed by a padding area that is zero
885 * filled. Used by network drivers which may DMA or transfer data
886 * beyond the buffer end onto the wire.
887 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700888 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900890
Herbert Xu5b057c62006-06-23 02:06:41 -0700891int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Herbert Xu5b057c62006-06-23 02:06:41 -0700893 int err;
894 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700897 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700901
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700902 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700903 if (likely(skb_cloned(skb) || ntail > 0)) {
904 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
905 if (unlikely(err))
906 goto free_skb;
907 }
908
909 /* FIXME: The use of this function with non-linear skb's really needs
910 * to be audited.
911 */
912 err = skb_linearize(skb);
913 if (unlikely(err))
914 goto free_skb;
915
916 memset(skb->data + skb->len, 0, pad);
917 return 0;
918
919free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700921 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900922}
923
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700924/**
925 * skb_put - add data to a buffer
926 * @skb: buffer to use
927 * @len: amount of data to add
928 *
929 * This function extends the used data area of the buffer. If this would
930 * exceed the total buffer size the kernel will panic. A pointer to the
931 * first byte of the extra data is returned.
932 */
933unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
934{
935 unsigned char *tmp = skb_tail_pointer(skb);
936 SKB_LINEAR_ASSERT(skb);
937 skb->tail += len;
938 skb->len += len;
939 if (unlikely(skb->tail > skb->end))
940 skb_over_panic(skb, len, __builtin_return_address(0));
941 return tmp;
942}
943EXPORT_SYMBOL(skb_put);
944
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700945/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -0700946 * skb_push - add data to the start of a buffer
947 * @skb: buffer to use
948 * @len: amount of data to add
949 *
950 * This function extends the used data area of the buffer at the buffer
951 * start. If this would exceed the total buffer headroom the kernel will
952 * panic. A pointer to the first byte of the extra data is returned.
953 */
954unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
955{
956 skb->data -= len;
957 skb->len += len;
958 if (unlikely(skb->data<skb->head))
959 skb_under_panic(skb, len, __builtin_return_address(0));
960 return skb->data;
961}
962EXPORT_SYMBOL(skb_push);
963
964/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700965 * skb_pull - remove data from the start of a buffer
966 * @skb: buffer to use
967 * @len: amount of data to remove
968 *
969 * This function removes data from the start of a buffer, returning
970 * the memory to the headroom. A pointer to the next data in the buffer
971 * is returned. Once the data has been pulled future pushes will overwrite
972 * the old data.
973 */
974unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
975{
976 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
977}
978EXPORT_SYMBOL(skb_pull);
979
Ilpo Järvinen419ae742008-03-27 17:54:01 -0700980/**
981 * skb_trim - remove end from a buffer
982 * @skb: buffer to alter
983 * @len: new length
984 *
985 * Cut the length of a buffer down by removing data from the tail. If
986 * the buffer is already under the length specified it is not modified.
987 * The skb must be linear.
988 */
989void skb_trim(struct sk_buff *skb, unsigned int len)
990{
991 if (skb->len > len)
992 __skb_trim(skb, len);
993}
994EXPORT_SYMBOL(skb_trim);
995
Herbert Xu3cc0e872006-06-09 16:13:38 -0700996/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
998
Herbert Xu3cc0e872006-06-09 16:13:38 -0700999int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Herbert Xu27b437c2006-07-13 19:26:39 -07001001 struct sk_buff **fragp;
1002 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int offset = skb_headlen(skb);
1004 int nfrags = skb_shinfo(skb)->nr_frags;
1005 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001006 int err;
1007
1008 if (skb_cloned(skb) &&
1009 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001012 i = 0;
1013 if (offset >= len)
1014 goto drop_pages;
1015
1016 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -07001018
1019 if (end < len) {
1020 offset = end;
1021 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001023
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001024 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -07001025
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001026drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001027 skb_shinfo(skb)->nr_frags = i;
1028
1029 for (; i < nfrags; i++)
1030 put_page(skb_shinfo(skb)->frags[i].page);
1031
1032 if (skb_shinfo(skb)->frag_list)
1033 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001034 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Herbert Xu27b437c2006-07-13 19:26:39 -07001037 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1038 fragp = &frag->next) {
1039 int end = offset + frag->len;
1040
1041 if (skb_shared(frag)) {
1042 struct sk_buff *nfrag;
1043
1044 nfrag = skb_clone(frag, GFP_ATOMIC);
1045 if (unlikely(!nfrag))
1046 return -ENOMEM;
1047
1048 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001049 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001050 frag = nfrag;
1051 *fragp = frag;
1052 }
1053
1054 if (end < len) {
1055 offset = end;
1056 continue;
1057 }
1058
1059 if (end > len &&
1060 unlikely((err = pskb_trim(frag, len - offset))))
1061 return err;
1062
1063 if (frag->next)
1064 skb_drop_list(&frag->next);
1065 break;
1066 }
1067
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001068done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001069 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 skb->data_len -= skb->len - len;
1071 skb->len = len;
1072 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001073 skb->len = len;
1074 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001075 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 return 0;
1079}
1080
1081/**
1082 * __pskb_pull_tail - advance tail of skb header
1083 * @skb: buffer to reallocate
1084 * @delta: number of bytes to advance tail
1085 *
1086 * The function makes a sense only on a fragmented &sk_buff,
1087 * it expands header moving its tail forward and copying necessary
1088 * data from fragmented part.
1089 *
1090 * &sk_buff MUST have reference count of 1.
1091 *
1092 * Returns %NULL (and &sk_buff does not change) if pull failed
1093 * or value of new tail of skb in the case of success.
1094 *
1095 * All the pointers pointing into skb header may change and must be
1096 * reloaded after call to this function.
1097 */
1098
1099/* Moves tail of skb head forward, copying data from fragmented part,
1100 * when it is necessary.
1101 * 1. It may fail due to malloc failure.
1102 * 2. It may change skb pointers.
1103 *
1104 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1105 */
1106unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1107{
1108 /* If skb has not enough free space at tail, get new one
1109 * plus 128 bytes for future expansions. If we have enough
1110 * room at tail, reallocate without expansion only if skb is cloned.
1111 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001112 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (eat > 0 || skb_cloned(skb)) {
1115 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1116 GFP_ATOMIC))
1117 return NULL;
1118 }
1119
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001120 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 BUG();
1122
1123 /* Optimization: no fragments, no reasons to preestimate
1124 * size of pulled pages. Superb.
1125 */
1126 if (!skb_shinfo(skb)->frag_list)
1127 goto pull_pages;
1128
1129 /* Estimate size of pulled pages. */
1130 eat = delta;
1131 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1132 if (skb_shinfo(skb)->frags[i].size >= eat)
1133 goto pull_pages;
1134 eat -= skb_shinfo(skb)->frags[i].size;
1135 }
1136
1137 /* If we need update frag list, we are in troubles.
1138 * Certainly, it possible to add an offset to skb data,
1139 * but taking into account that pulling is expected to
1140 * be very rare operation, it is worth to fight against
1141 * further bloating skb head and crucify ourselves here instead.
1142 * Pure masohism, indeed. 8)8)
1143 */
1144 if (eat) {
1145 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1146 struct sk_buff *clone = NULL;
1147 struct sk_buff *insp = NULL;
1148
1149 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001150 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (list->len <= eat) {
1153 /* Eaten as whole. */
1154 eat -= list->len;
1155 list = list->next;
1156 insp = list;
1157 } else {
1158 /* Eaten partially. */
1159
1160 if (skb_shared(list)) {
1161 /* Sucks! We need to fork list. :-( */
1162 clone = skb_clone(list, GFP_ATOMIC);
1163 if (!clone)
1164 return NULL;
1165 insp = list->next;
1166 list = clone;
1167 } else {
1168 /* This may be pulled without
1169 * problems. */
1170 insp = list;
1171 }
1172 if (!pskb_pull(list, eat)) {
1173 if (clone)
1174 kfree_skb(clone);
1175 return NULL;
1176 }
1177 break;
1178 }
1179 } while (eat);
1180
1181 /* Free pulled out fragments. */
1182 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1183 skb_shinfo(skb)->frag_list = list->next;
1184 kfree_skb(list);
1185 }
1186 /* And insert new clone at head. */
1187 if (clone) {
1188 clone->next = list;
1189 skb_shinfo(skb)->frag_list = clone;
1190 }
1191 }
1192 /* Success! Now we may commit changes to skb data. */
1193
1194pull_pages:
1195 eat = delta;
1196 k = 0;
1197 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1198 if (skb_shinfo(skb)->frags[i].size <= eat) {
1199 put_page(skb_shinfo(skb)->frags[i].page);
1200 eat -= skb_shinfo(skb)->frags[i].size;
1201 } else {
1202 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1203 if (eat) {
1204 skb_shinfo(skb)->frags[k].page_offset += eat;
1205 skb_shinfo(skb)->frags[k].size -= eat;
1206 eat = 0;
1207 }
1208 k++;
1209 }
1210 }
1211 skb_shinfo(skb)->nr_frags = k;
1212
1213 skb->tail += delta;
1214 skb->data_len -= delta;
1215
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001216 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219/* Copy some data bits from skb to kernel buffer. */
1220
1221int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1222{
1223 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001224 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (offset > (int)skb->len - len)
1227 goto fault;
1228
1229 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001230 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (copy > len)
1232 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001233 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if ((len -= copy) == 0)
1235 return 0;
1236 offset += copy;
1237 to += copy;
1238 }
1239
1240 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001241 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001243 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001244
1245 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if ((copy = end - offset) > 0) {
1247 u8 *vaddr;
1248
1249 if (copy > len)
1250 copy = len;
1251
1252 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1253 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001254 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1255 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 kunmap_skb_frag(vaddr);
1257
1258 if ((len -= copy) == 0)
1259 return 0;
1260 offset += copy;
1261 to += copy;
1262 }
David S. Miller1a028e52007-04-27 15:21:23 -07001263 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 if (skb_shinfo(skb)->frag_list) {
1267 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1268
1269 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001270 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001272 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001273
1274 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if ((copy = end - offset) > 0) {
1276 if (copy > len)
1277 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001278 if (skb_copy_bits(list, offset - start,
1279 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto fault;
1281 if ((len -= copy) == 0)
1282 return 0;
1283 offset += copy;
1284 to += copy;
1285 }
David S. Miller1a028e52007-04-27 15:21:23 -07001286 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 }
1289 if (!len)
1290 return 0;
1291
1292fault:
1293 return -EFAULT;
1294}
1295
Jens Axboe9c55e012007-11-06 23:30:13 -08001296/*
1297 * Callback from splice_to_pipe(), if we need to release some pages
1298 * at the end of the spd in case we error'ed out in filling the pipe.
1299 */
1300static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1301{
1302 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1303
1304 kfree_skb(skb);
1305}
1306
1307/*
1308 * Fill page/offset/length into spd, if it can hold more pages.
1309 */
1310static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1311 unsigned int len, unsigned int offset,
1312 struct sk_buff *skb)
1313{
1314 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1315 return 1;
1316
1317 spd->pages[spd->nr_pages] = page;
1318 spd->partial[spd->nr_pages].len = len;
1319 spd->partial[spd->nr_pages].offset = offset;
1320 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1321 spd->nr_pages++;
1322 return 0;
1323}
1324
Octavian Purdila2870c432008-07-15 00:49:11 -07001325static inline void __segment_seek(struct page **page, unsigned int *poff,
1326 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001327{
Octavian Purdila2870c432008-07-15 00:49:11 -07001328 *poff += off;
1329 *page += *poff / PAGE_SIZE;
1330 *poff = *poff % PAGE_SIZE;
1331 *plen -= off;
1332}
Jens Axboe9c55e012007-11-06 23:30:13 -08001333
Octavian Purdila2870c432008-07-15 00:49:11 -07001334static inline int __splice_segment(struct page *page, unsigned int poff,
1335 unsigned int plen, unsigned int *off,
1336 unsigned int *len, struct sk_buff *skb,
1337 struct splice_pipe_desc *spd)
1338{
1339 if (!*len)
1340 return 1;
1341
1342 /* skip this segment if already processed */
1343 if (*off >= plen) {
1344 *off -= plen;
1345 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001346 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001347
Octavian Purdila2870c432008-07-15 00:49:11 -07001348 /* ignore any bits we already processed */
1349 if (*off) {
1350 __segment_seek(&page, &poff, &plen, *off);
1351 *off = 0;
1352 }
1353
1354 do {
1355 unsigned int flen = min(*len, plen);
1356
1357 /* the linear region may spread across several pages */
1358 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1359
1360 if (spd_fill_page(spd, page, flen, poff, skb))
1361 return 1;
1362
1363 __segment_seek(&page, &poff, &plen, flen);
1364 *len -= flen;
1365
1366 } while (*len && plen);
1367
1368 return 0;
1369}
1370
1371/*
1372 * Map linear and fragment data from the skb to spd. It reports failure if the
1373 * pipe is full or if we already spliced the requested length.
1374 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001375static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Octavian Purdila2870c432008-07-15 00:49:11 -07001376 unsigned int *len,
1377 struct splice_pipe_desc *spd)
1378{
1379 int seg;
1380
Jens Axboe9c55e012007-11-06 23:30:13 -08001381 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001382 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001383 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001384 if (__splice_segment(virt_to_page(skb->data),
1385 (unsigned long) skb->data & (PAGE_SIZE - 1),
1386 skb_headlen(skb),
1387 offset, len, skb, spd))
1388 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001389
1390 /*
1391 * then map the fragments
1392 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001393 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1394 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1395
Octavian Purdila2870c432008-07-15 00:49:11 -07001396 if (__splice_segment(f->page, f->page_offset, f->size,
1397 offset, len, skb, spd))
1398 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001399 }
1400
Octavian Purdila2870c432008-07-15 00:49:11 -07001401 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001402}
1403
1404/*
1405 * Map data from the skb to a pipe. Should handle both the linear part,
1406 * the fragments, and the frag list. It does NOT handle frag lists within
1407 * the frag list, if such a thing exists. We'd probably need to recurse to
1408 * handle that cleanly.
1409 */
1410int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1411 struct pipe_inode_info *pipe, unsigned int tlen,
1412 unsigned int flags)
1413{
1414 struct partial_page partial[PIPE_BUFFERS];
1415 struct page *pages[PIPE_BUFFERS];
1416 struct splice_pipe_desc spd = {
1417 .pages = pages,
1418 .partial = partial,
1419 .flags = flags,
1420 .ops = &sock_pipe_buf_ops,
1421 .spd_release = sock_spd_release,
1422 };
1423 struct sk_buff *skb;
1424
1425 /*
1426 * I'd love to avoid the clone here, but tcp_read_sock()
1427 * ignores reference counts and unconditonally kills the sk_buff
1428 * on return from the actor.
1429 */
1430 skb = skb_clone(__skb, GFP_KERNEL);
1431 if (unlikely(!skb))
1432 return -ENOMEM;
1433
1434 /*
1435 * __skb_splice_bits() only fails if the output has no room left,
1436 * so no point in going over the frag_list for the error case.
1437 */
1438 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1439 goto done;
1440 else if (!tlen)
1441 goto done;
1442
1443 /*
1444 * now see if we have a frag_list to map
1445 */
1446 if (skb_shinfo(skb)->frag_list) {
1447 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1448
1449 for (; list && tlen; list = list->next) {
1450 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1451 break;
1452 }
1453 }
1454
1455done:
1456 /*
1457 * drop our reference to the clone, the pipe consumption will
1458 * drop the rest.
1459 */
1460 kfree_skb(skb);
1461
1462 if (spd.nr_pages) {
1463 int ret;
Octavian Purdila293ad602008-06-04 15:45:58 -07001464 struct sock *sk = __skb->sk;
Jens Axboe9c55e012007-11-06 23:30:13 -08001465
1466 /*
1467 * Drop the socket lock, otherwise we have reverse
1468 * locking dependencies between sk_lock and i_mutex
1469 * here as compared to sendfile(). We enter here
1470 * with the socket lock held, and splice_to_pipe() will
1471 * grab the pipe inode lock. For sendfile() emulation,
1472 * we call into ->sendpage() with the i_mutex lock held
1473 * and networking will grab the socket lock.
1474 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001475 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001476 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001477 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001478 return ret;
1479 }
1480
1481 return 0;
1482}
1483
Herbert Xu357b40a2005-04-19 22:30:14 -07001484/**
1485 * skb_store_bits - store bits from kernel buffer to skb
1486 * @skb: destination buffer
1487 * @offset: offset in destination
1488 * @from: source buffer
1489 * @len: number of bytes to copy
1490 *
1491 * Copy the specified number of bytes from the source buffer to the
1492 * destination skb. This function handles all the messy bits of
1493 * traversing fragment lists and such.
1494 */
1495
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001496int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001497{
1498 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001499 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001500
1501 if (offset > (int)skb->len - len)
1502 goto fault;
1503
David S. Miller1a028e52007-04-27 15:21:23 -07001504 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001505 if (copy > len)
1506 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001507 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001508 if ((len -= copy) == 0)
1509 return 0;
1510 offset += copy;
1511 from += copy;
1512 }
1513
1514 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1515 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001516 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001517
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001518 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001519
1520 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001521 if ((copy = end - offset) > 0) {
1522 u8 *vaddr;
1523
1524 if (copy > len)
1525 copy = len;
1526
1527 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001528 memcpy(vaddr + frag->page_offset + offset - start,
1529 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001530 kunmap_skb_frag(vaddr);
1531
1532 if ((len -= copy) == 0)
1533 return 0;
1534 offset += copy;
1535 from += copy;
1536 }
David S. Miller1a028e52007-04-27 15:21:23 -07001537 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001538 }
1539
1540 if (skb_shinfo(skb)->frag_list) {
1541 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1542
1543 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001544 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001545
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001546 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001547
1548 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001549 if ((copy = end - offset) > 0) {
1550 if (copy > len)
1551 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001552 if (skb_store_bits(list, offset - start,
1553 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001554 goto fault;
1555 if ((len -= copy) == 0)
1556 return 0;
1557 offset += copy;
1558 from += copy;
1559 }
David S. Miller1a028e52007-04-27 15:21:23 -07001560 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001561 }
1562 }
1563 if (!len)
1564 return 0;
1565
1566fault:
1567 return -EFAULT;
1568}
1569
1570EXPORT_SYMBOL(skb_store_bits);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572/* Checksum skb data. */
1573
Al Viro2bbbc862006-11-14 21:37:14 -08001574__wsum skb_checksum(const struct sk_buff *skb, int offset,
1575 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
David S. Miller1a028e52007-04-27 15:21:23 -07001577 int start = skb_headlen(skb);
1578 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int pos = 0;
1580
1581 /* Checksum header. */
1582 if (copy > 0) {
1583 if (copy > len)
1584 copy = len;
1585 csum = csum_partial(skb->data + offset, copy, csum);
1586 if ((len -= copy) == 0)
1587 return csum;
1588 offset += copy;
1589 pos = copy;
1590 }
1591
1592 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001593 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001595 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001596
1597 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001599 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 u8 *vaddr;
1601 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1602
1603 if (copy > len)
1604 copy = len;
1605 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001606 csum2 = csum_partial(vaddr + frag->page_offset +
1607 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 kunmap_skb_frag(vaddr);
1609 csum = csum_block_add(csum, csum2, pos);
1610 if (!(len -= copy))
1611 return csum;
1612 offset += copy;
1613 pos += copy;
1614 }
David S. Miller1a028e52007-04-27 15:21:23 -07001615 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 if (skb_shinfo(skb)->frag_list) {
1619 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1620
1621 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001622 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001624 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001625
1626 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001628 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (copy > len)
1630 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001631 csum2 = skb_checksum(list, offset - start,
1632 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 csum = csum_block_add(csum, csum2, pos);
1634 if ((len -= copy) == 0)
1635 return csum;
1636 offset += copy;
1637 pos += copy;
1638 }
David S. Miller1a028e52007-04-27 15:21:23 -07001639 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001642 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 return csum;
1645}
1646
1647/* Both of above in one bottle. */
1648
Al Viro81d77662006-11-14 21:37:33 -08001649__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1650 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
David S. Miller1a028e52007-04-27 15:21:23 -07001652 int start = skb_headlen(skb);
1653 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int pos = 0;
1655
1656 /* Copy header. */
1657 if (copy > 0) {
1658 if (copy > len)
1659 copy = len;
1660 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1661 copy, csum);
1662 if ((len -= copy) == 0)
1663 return csum;
1664 offset += copy;
1665 to += copy;
1666 pos = copy;
1667 }
1668
1669 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001670 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001672 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001673
1674 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001676 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 u8 *vaddr;
1678 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1679
1680 if (copy > len)
1681 copy = len;
1682 vaddr = kmap_skb_frag(frag);
1683 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001684 frag->page_offset +
1685 offset - start, to,
1686 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 kunmap_skb_frag(vaddr);
1688 csum = csum_block_add(csum, csum2, pos);
1689 if (!(len -= copy))
1690 return csum;
1691 offset += copy;
1692 to += copy;
1693 pos += copy;
1694 }
David S. Miller1a028e52007-04-27 15:21:23 -07001695 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
1698 if (skb_shinfo(skb)->frag_list) {
1699 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1700
1701 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001702 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001703 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001705 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001706
1707 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if ((copy = end - offset) > 0) {
1709 if (copy > len)
1710 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001711 csum2 = skb_copy_and_csum_bits(list,
1712 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 to, copy, 0);
1714 csum = csum_block_add(csum, csum2, pos);
1715 if ((len -= copy) == 0)
1716 return csum;
1717 offset += copy;
1718 to += copy;
1719 pos += copy;
1720 }
David S. Miller1a028e52007-04-27 15:21:23 -07001721 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001724 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return csum;
1726}
1727
1728void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1729{
Al Virod3bc23e2006-11-14 21:24:49 -08001730 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 long csstart;
1732
Patrick McHardy84fa7932006-08-29 16:44:56 -07001733 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001734 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 else
1736 csstart = skb_headlen(skb);
1737
Kris Katterjohn09a62662006-01-08 22:24:28 -08001738 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001740 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 csum = 0;
1743 if (csstart != skb->len)
1744 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1745 skb->len - csstart, 0);
1746
Patrick McHardy84fa7932006-08-29 16:44:56 -07001747 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001748 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Al Virod3bc23e2006-11-14 21:24:49 -08001750 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752}
1753
1754/**
1755 * skb_dequeue - remove from the head of the queue
1756 * @list: list to dequeue from
1757 *
1758 * Remove the head of the list. The list lock is taken so the function
1759 * may be used safely with other locking list functions. The head item is
1760 * returned or %NULL if the list is empty.
1761 */
1762
1763struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1764{
1765 unsigned long flags;
1766 struct sk_buff *result;
1767
1768 spin_lock_irqsave(&list->lock, flags);
1769 result = __skb_dequeue(list);
1770 spin_unlock_irqrestore(&list->lock, flags);
1771 return result;
1772}
1773
1774/**
1775 * skb_dequeue_tail - remove from the tail of the queue
1776 * @list: list to dequeue from
1777 *
1778 * Remove the tail of the list. The list lock is taken so the function
1779 * may be used safely with other locking list functions. The tail item is
1780 * returned or %NULL if the list is empty.
1781 */
1782struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1783{
1784 unsigned long flags;
1785 struct sk_buff *result;
1786
1787 spin_lock_irqsave(&list->lock, flags);
1788 result = __skb_dequeue_tail(list);
1789 spin_unlock_irqrestore(&list->lock, flags);
1790 return result;
1791}
1792
1793/**
1794 * skb_queue_purge - empty a list
1795 * @list: list to empty
1796 *
1797 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1798 * the list and one reference dropped. This function takes the list
1799 * lock and is atomic with respect to other list locking functions.
1800 */
1801void skb_queue_purge(struct sk_buff_head *list)
1802{
1803 struct sk_buff *skb;
1804 while ((skb = skb_dequeue(list)) != NULL)
1805 kfree_skb(skb);
1806}
1807
1808/**
1809 * skb_queue_head - queue a buffer at the list head
1810 * @list: list to use
1811 * @newsk: buffer to queue
1812 *
1813 * Queue a buffer at the start of the list. This function takes the
1814 * list lock and can be used safely with other locking &sk_buff functions
1815 * safely.
1816 *
1817 * A buffer cannot be placed on two lists at the same time.
1818 */
1819void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1820{
1821 unsigned long flags;
1822
1823 spin_lock_irqsave(&list->lock, flags);
1824 __skb_queue_head(list, newsk);
1825 spin_unlock_irqrestore(&list->lock, flags);
1826}
1827
1828/**
1829 * skb_queue_tail - queue a buffer at the list tail
1830 * @list: list to use
1831 * @newsk: buffer to queue
1832 *
1833 * Queue a buffer at the tail of the list. This function takes the
1834 * list lock and can be used safely with other locking &sk_buff functions
1835 * safely.
1836 *
1837 * A buffer cannot be placed on two lists at the same time.
1838 */
1839void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1840{
1841 unsigned long flags;
1842
1843 spin_lock_irqsave(&list->lock, flags);
1844 __skb_queue_tail(list, newsk);
1845 spin_unlock_irqrestore(&list->lock, flags);
1846}
David S. Miller8728b832005-08-09 19:25:21 -07001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/**
1849 * skb_unlink - remove a buffer from a list
1850 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001851 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 *
David S. Miller8728b832005-08-09 19:25:21 -07001853 * Remove a packet from a list. The list locks are taken and this
1854 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
David S. Miller8728b832005-08-09 19:25:21 -07001856 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
David S. Miller8728b832005-08-09 19:25:21 -07001858void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
David S. Miller8728b832005-08-09 19:25:21 -07001860 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
David S. Miller8728b832005-08-09 19:25:21 -07001862 spin_lock_irqsave(&list->lock, flags);
1863 __skb_unlink(skb, list);
1864 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/**
1868 * skb_append - append a buffer
1869 * @old: buffer to insert after
1870 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001871 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 *
1873 * Place a packet after a given packet in a list. The list locks are taken
1874 * and this function is atomic with respect to other list locked calls.
1875 * A buffer cannot be placed on two lists at the same time.
1876 */
David S. Miller8728b832005-08-09 19:25:21 -07001877void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 unsigned long flags;
1880
David S. Miller8728b832005-08-09 19:25:21 -07001881 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001882 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001883 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
1886
1887/**
1888 * skb_insert - insert a buffer
1889 * @old: buffer to insert before
1890 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001891 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 *
David S. Miller8728b832005-08-09 19:25:21 -07001893 * Place a packet before a given packet in a list. The list locks are
1894 * taken and this function is atomic with respect to other list locked
1895 * calls.
1896 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 * A buffer cannot be placed on two lists at the same time.
1898 */
David S. Miller8728b832005-08-09 19:25:21 -07001899void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 unsigned long flags;
1902
David S. Miller8728b832005-08-09 19:25:21 -07001903 spin_lock_irqsave(&list->lock, flags);
1904 __skb_insert(newsk, old->prev, old, list);
1905 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908static inline void skb_split_inside_header(struct sk_buff *skb,
1909 struct sk_buff* skb1,
1910 const u32 len, const int pos)
1911{
1912 int i;
1913
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001914 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1915 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* And move data appendix as is. */
1917 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1918 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1919
1920 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1921 skb_shinfo(skb)->nr_frags = 0;
1922 skb1->data_len = skb->data_len;
1923 skb1->len += skb1->data_len;
1924 skb->data_len = 0;
1925 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001926 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929static inline void skb_split_no_header(struct sk_buff *skb,
1930 struct sk_buff* skb1,
1931 const u32 len, int pos)
1932{
1933 int i, k = 0;
1934 const int nfrags = skb_shinfo(skb)->nr_frags;
1935
1936 skb_shinfo(skb)->nr_frags = 0;
1937 skb1->len = skb1->data_len = skb->len - len;
1938 skb->len = len;
1939 skb->data_len = len - pos;
1940
1941 for (i = 0; i < nfrags; i++) {
1942 int size = skb_shinfo(skb)->frags[i].size;
1943
1944 if (pos + size > len) {
1945 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1946
1947 if (pos < len) {
1948 /* Split frag.
1949 * We have two variants in this case:
1950 * 1. Move all the frag to the second
1951 * part, if it is possible. F.e.
1952 * this approach is mandatory for TUX,
1953 * where splitting is expensive.
1954 * 2. Split is accurately. We make this.
1955 */
1956 get_page(skb_shinfo(skb)->frags[i].page);
1957 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1958 skb_shinfo(skb1)->frags[0].size -= len - pos;
1959 skb_shinfo(skb)->frags[i].size = len - pos;
1960 skb_shinfo(skb)->nr_frags++;
1961 }
1962 k++;
1963 } else
1964 skb_shinfo(skb)->nr_frags++;
1965 pos += size;
1966 }
1967 skb_shinfo(skb1)->nr_frags = k;
1968}
1969
1970/**
1971 * skb_split - Split fragmented skb to two parts at length len.
1972 * @skb: the buffer to split
1973 * @skb1: the buffer to receive the second part
1974 * @len: new length for skb
1975 */
1976void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1977{
1978 int pos = skb_headlen(skb);
1979
1980 if (len < pos) /* Split line is inside header. */
1981 skb_split_inside_header(skb, skb1, len, pos);
1982 else /* Second chunk has no header, nothing to copy. */
1983 skb_split_no_header(skb, skb1, len, pos);
1984}
1985
Thomas Graf677e90e2005-06-23 20:59:51 -07001986/**
1987 * skb_prepare_seq_read - Prepare a sequential read of skb data
1988 * @skb: the buffer to read
1989 * @from: lower offset of data to be read
1990 * @to: upper offset of data to be read
1991 * @st: state variable
1992 *
1993 * Initializes the specified state variable. Must be called before
1994 * invoking skb_seq_read() for the first time.
1995 */
1996void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1997 unsigned int to, struct skb_seq_state *st)
1998{
1999 st->lower_offset = from;
2000 st->upper_offset = to;
2001 st->root_skb = st->cur_skb = skb;
2002 st->frag_idx = st->stepped_offset = 0;
2003 st->frag_data = NULL;
2004}
2005
2006/**
2007 * skb_seq_read - Sequentially read skb data
2008 * @consumed: number of bytes consumed by the caller so far
2009 * @data: destination pointer for data to be returned
2010 * @st: state variable
2011 *
2012 * Reads a block of skb data at &consumed relative to the
2013 * lower offset specified to skb_prepare_seq_read(). Assigns
2014 * the head of the data block to &data and returns the length
2015 * of the block or 0 if the end of the skb data or the upper
2016 * offset has been reached.
2017 *
2018 * The caller is not required to consume all of the data
2019 * returned, i.e. &consumed is typically set to the number
2020 * of bytes already consumed and the next call to
2021 * skb_seq_read() will return the remaining part of the block.
2022 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002023 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002024 * this limitation is the cost for zerocopy seqeuental
2025 * reads of potentially non linear data.
2026 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002027 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002028 * at the moment, state->root_skb could be replaced with
2029 * a stack for this purpose.
2030 */
2031unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2032 struct skb_seq_state *st)
2033{
2034 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2035 skb_frag_t *frag;
2036
2037 if (unlikely(abs_offset >= st->upper_offset))
2038 return 0;
2039
2040next_skb:
2041 block_limit = skb_headlen(st->cur_skb);
2042
2043 if (abs_offset < block_limit) {
2044 *data = st->cur_skb->data + abs_offset;
2045 return block_limit - abs_offset;
2046 }
2047
2048 if (st->frag_idx == 0 && !st->frag_data)
2049 st->stepped_offset += skb_headlen(st->cur_skb);
2050
2051 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2052 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2053 block_limit = frag->size + st->stepped_offset;
2054
2055 if (abs_offset < block_limit) {
2056 if (!st->frag_data)
2057 st->frag_data = kmap_skb_frag(frag);
2058
2059 *data = (u8 *) st->frag_data + frag->page_offset +
2060 (abs_offset - st->stepped_offset);
2061
2062 return block_limit - abs_offset;
2063 }
2064
2065 if (st->frag_data) {
2066 kunmap_skb_frag(st->frag_data);
2067 st->frag_data = NULL;
2068 }
2069
2070 st->frag_idx++;
2071 st->stepped_offset += frag->size;
2072 }
2073
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002074 if (st->frag_data) {
2075 kunmap_skb_frag(st->frag_data);
2076 st->frag_data = NULL;
2077 }
2078
Thomas Graf677e90e2005-06-23 20:59:51 -07002079 if (st->cur_skb->next) {
2080 st->cur_skb = st->cur_skb->next;
2081 st->frag_idx = 0;
2082 goto next_skb;
2083 } else if (st->root_skb == st->cur_skb &&
2084 skb_shinfo(st->root_skb)->frag_list) {
2085 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2086 goto next_skb;
2087 }
2088
2089 return 0;
2090}
2091
2092/**
2093 * skb_abort_seq_read - Abort a sequential read of skb data
2094 * @st: state variable
2095 *
2096 * Must be called if skb_seq_read() was not called until it
2097 * returned 0.
2098 */
2099void skb_abort_seq_read(struct skb_seq_state *st)
2100{
2101 if (st->frag_data)
2102 kunmap_skb_frag(st->frag_data);
2103}
2104
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002105#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2106
2107static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2108 struct ts_config *conf,
2109 struct ts_state *state)
2110{
2111 return skb_seq_read(offset, text, TS_SKB_CB(state));
2112}
2113
2114static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2115{
2116 skb_abort_seq_read(TS_SKB_CB(state));
2117}
2118
2119/**
2120 * skb_find_text - Find a text pattern in skb data
2121 * @skb: the buffer to look in
2122 * @from: search offset
2123 * @to: search limit
2124 * @config: textsearch configuration
2125 * @state: uninitialized textsearch state variable
2126 *
2127 * Finds a pattern in the skb data according to the specified
2128 * textsearch configuration. Use textsearch_next() to retrieve
2129 * subsequent occurrences of the pattern. Returns the offset
2130 * to the first occurrence or UINT_MAX if no match was found.
2131 */
2132unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2133 unsigned int to, struct ts_config *config,
2134 struct ts_state *state)
2135{
Phil Oesterf72b9482006-06-26 00:00:57 -07002136 unsigned int ret;
2137
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002138 config->get_next_block = skb_ts_get_next_block;
2139 config->finish = skb_ts_finish;
2140
2141 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2142
Phil Oesterf72b9482006-06-26 00:00:57 -07002143 ret = textsearch_find(config, state);
2144 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002145}
2146
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002147/**
2148 * skb_append_datato_frags: - append the user data to a skb
2149 * @sk: sock structure
2150 * @skb: skb structure to be appened with user data.
2151 * @getfrag: call back function to be used for getting the user data
2152 * @from: pointer to user message iov
2153 * @length: length of the iov message
2154 *
2155 * Description: This procedure append the user data in the fragment part
2156 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2157 */
2158int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002159 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002160 int len, int odd, struct sk_buff *skb),
2161 void *from, int length)
2162{
2163 int frg_cnt = 0;
2164 skb_frag_t *frag = NULL;
2165 struct page *page = NULL;
2166 int copy, left;
2167 int offset = 0;
2168 int ret;
2169
2170 do {
2171 /* Return error if we don't have space for new frag */
2172 frg_cnt = skb_shinfo(skb)->nr_frags;
2173 if (frg_cnt >= MAX_SKB_FRAGS)
2174 return -EFAULT;
2175
2176 /* allocate a new page for next frag */
2177 page = alloc_pages(sk->sk_allocation, 0);
2178
2179 /* If alloc_page fails just return failure and caller will
2180 * free previous allocated pages by doing kfree_skb()
2181 */
2182 if (page == NULL)
2183 return -ENOMEM;
2184
2185 /* initialize the next frag */
2186 sk->sk_sndmsg_page = page;
2187 sk->sk_sndmsg_off = 0;
2188 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2189 skb->truesize += PAGE_SIZE;
2190 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2191
2192 /* get the new initialized frag */
2193 frg_cnt = skb_shinfo(skb)->nr_frags;
2194 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2195
2196 /* copy the user data to page */
2197 left = PAGE_SIZE - frag->page_offset;
2198 copy = (length > left)? left : length;
2199
2200 ret = getfrag(from, (page_address(frag->page) +
2201 frag->page_offset + frag->size),
2202 offset, copy, 0, skb);
2203 if (ret < 0)
2204 return -EFAULT;
2205
2206 /* copy was successful so update the size parameters */
2207 sk->sk_sndmsg_off += copy;
2208 frag->size += copy;
2209 skb->len += copy;
2210 skb->data_len += copy;
2211 offset += copy;
2212 length -= copy;
2213
2214 } while (length > 0);
2215
2216 return 0;
2217}
2218
Herbert Xucbb042f2006-03-20 22:43:56 -08002219/**
2220 * skb_pull_rcsum - pull skb and update receive checksum
2221 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002222 * @len: length of data pulled
2223 *
2224 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002225 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002226 * receive path processing instead of skb_pull unless you know
2227 * that the checksum difference is zero (e.g., a valid IP header)
2228 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002229 */
2230unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2231{
2232 BUG_ON(len > skb->len);
2233 skb->len -= len;
2234 BUG_ON(skb->len < skb->data_len);
2235 skb_postpull_rcsum(skb, skb->data, len);
2236 return skb->data += len;
2237}
2238
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002239EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2240
Herbert Xuf4c50d92006-06-22 03:02:40 -07002241/**
2242 * skb_segment - Perform protocol segmentation on skb.
2243 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002244 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002245 *
2246 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002247 * a pointer to the first in a list of new skbs for the segments.
2248 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002249 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002250struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002251{
2252 struct sk_buff *segs = NULL;
2253 struct sk_buff *tail = NULL;
2254 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002255 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002256 unsigned int offset = doffset;
2257 unsigned int headroom;
2258 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002259 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002260 int nfrags = skb_shinfo(skb)->nr_frags;
2261 int err = -ENOMEM;
2262 int i = 0;
2263 int pos;
2264
2265 __skb_push(skb, doffset);
2266 headroom = skb_headroom(skb);
2267 pos = skb_headlen(skb);
2268
2269 do {
2270 struct sk_buff *nskb;
2271 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002272 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002273 int k;
2274 int size;
2275
2276 len = skb->len - offset;
2277 if (len > mss)
2278 len = mss;
2279
2280 hsize = skb_headlen(skb) - offset;
2281 if (hsize < 0)
2282 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002283 if (hsize > len || !sg)
2284 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002285
Herbert Xuc8884ed2006-10-29 15:59:41 -08002286 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002287 if (unlikely(!nskb))
2288 goto err;
2289
2290 if (segs)
2291 tail->next = nskb;
2292 else
2293 segs = nskb;
2294 tail = nskb;
2295
Herbert Xu6f85a122008-08-15 14:55:02 -07002296 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002297 nskb->mac_len = skb->mac_len;
2298
2299 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002300 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002301 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002302 nskb->transport_header = (nskb->network_header +
2303 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002304 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2305 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002306 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002307 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002308 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2309 skb_put(nskb, len),
2310 len, 0);
2311 continue;
2312 }
2313
2314 frag = skb_shinfo(nskb)->frags;
2315 k = 0;
2316
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002317 skb_copy_from_linear_data_offset(skb, offset,
2318 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002319
2320 while (pos < offset + len) {
2321 BUG_ON(i >= nfrags);
2322
2323 *frag = skb_shinfo(skb)->frags[i];
2324 get_page(frag->page);
2325 size = frag->size;
2326
2327 if (pos < offset) {
2328 frag->page_offset += offset - pos;
2329 frag->size -= offset - pos;
2330 }
2331
2332 k++;
2333
2334 if (pos + size <= offset + len) {
2335 i++;
2336 pos += size;
2337 } else {
2338 frag->size -= pos + size - (offset + len);
2339 break;
2340 }
2341
2342 frag++;
2343 }
2344
2345 skb_shinfo(nskb)->nr_frags = k;
2346 nskb->data_len = len - hsize;
2347 nskb->len += nskb->data_len;
2348 nskb->truesize += nskb->data_len;
2349 } while ((offset += len) < skb->len);
2350
2351 return segs;
2352
2353err:
2354 while ((skb = segs)) {
2355 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002356 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002357 }
2358 return ERR_PTR(err);
2359}
2360
2361EXPORT_SYMBOL_GPL(skb_segment);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363void __init skb_init(void)
2364{
2365 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2366 sizeof(struct sk_buff),
2367 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002368 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002369 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002370 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2371 (2*sizeof(struct sk_buff)) +
2372 sizeof(atomic_t),
2373 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002374 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002375 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
David Howells716ea3a2007-04-02 20:19:53 -07002378/**
2379 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2380 * @skb: Socket buffer containing the buffers to be mapped
2381 * @sg: The scatter-gather list to map into
2382 * @offset: The offset into the buffer's contents to start mapping
2383 * @len: Length of buffer space to be mapped
2384 *
2385 * Fill the specified scatter-gather list with mappings/pointers into a
2386 * region of the buffer space attached to a socket buffer.
2387 */
David S. Miller51c739d2007-10-30 21:29:29 -07002388static int
2389__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002390{
David S. Miller1a028e52007-04-27 15:21:23 -07002391 int start = skb_headlen(skb);
2392 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002393 int elt = 0;
2394
2395 if (copy > 0) {
2396 if (copy > len)
2397 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02002398 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002399 elt++;
2400 if ((len -= copy) == 0)
2401 return elt;
2402 offset += copy;
2403 }
2404
2405 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002406 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002407
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002408 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002409
2410 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002411 if ((copy = end - offset) > 0) {
2412 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2413
2414 if (copy > len)
2415 copy = len;
Jens Axboe642f149032007-10-24 11:20:47 +02002416 sg_set_page(&sg[elt], frag->page, copy,
2417 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002418 elt++;
2419 if (!(len -= copy))
2420 return elt;
2421 offset += copy;
2422 }
David S. Miller1a028e52007-04-27 15:21:23 -07002423 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002424 }
2425
2426 if (skb_shinfo(skb)->frag_list) {
2427 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2428
2429 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002430 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002431
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002432 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002433
2434 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002435 if ((copy = end - offset) > 0) {
2436 if (copy > len)
2437 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002438 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2439 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002440 if ((len -= copy) == 0)
2441 return elt;
2442 offset += copy;
2443 }
David S. Miller1a028e52007-04-27 15:21:23 -07002444 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002445 }
2446 }
2447 BUG_ON(len);
2448 return elt;
2449}
2450
David S. Miller51c739d2007-10-30 21:29:29 -07002451int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2452{
2453 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2454
Jens Axboec46f2332007-10-31 12:06:37 +01002455 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002456
2457 return nsg;
2458}
2459
David Howells716ea3a2007-04-02 20:19:53 -07002460/**
2461 * skb_cow_data - Check that a socket buffer's data buffers are writable
2462 * @skb: The socket buffer to check.
2463 * @tailbits: Amount of trailing space to be added
2464 * @trailer: Returned pointer to the skb where the @tailbits space begins
2465 *
2466 * Make sure that the data buffers attached to a socket buffer are
2467 * writable. If they are not, private copies are made of the data buffers
2468 * and the socket buffer is set to use these instead.
2469 *
2470 * If @tailbits is given, make sure that there is space to write @tailbits
2471 * bytes of data beyond current end of socket buffer. @trailer will be
2472 * set to point to the skb in which this space begins.
2473 *
2474 * The number of scatterlist elements required to completely map the
2475 * COW'd and extended socket buffer will be returned.
2476 */
2477int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2478{
2479 int copyflag;
2480 int elt;
2481 struct sk_buff *skb1, **skb_p;
2482
2483 /* If skb is cloned or its head is paged, reallocate
2484 * head pulling out all the pages (pages are considered not writable
2485 * at the moment even if they are anonymous).
2486 */
2487 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2488 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2489 return -ENOMEM;
2490
2491 /* Easy case. Most of packets will go this way. */
2492 if (!skb_shinfo(skb)->frag_list) {
2493 /* A little of trouble, not enough of space for trailer.
2494 * This should not happen, when stack is tuned to generate
2495 * good frames. OK, on miss we reallocate and reserve even more
2496 * space, 128 bytes is fair. */
2497
2498 if (skb_tailroom(skb) < tailbits &&
2499 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2500 return -ENOMEM;
2501
2502 /* Voila! */
2503 *trailer = skb;
2504 return 1;
2505 }
2506
2507 /* Misery. We are in troubles, going to mincer fragments... */
2508
2509 elt = 1;
2510 skb_p = &skb_shinfo(skb)->frag_list;
2511 copyflag = 0;
2512
2513 while ((skb1 = *skb_p) != NULL) {
2514 int ntail = 0;
2515
2516 /* The fragment is partially pulled by someone,
2517 * this can happen on input. Copy it and everything
2518 * after it. */
2519
2520 if (skb_shared(skb1))
2521 copyflag = 1;
2522
2523 /* If the skb is the last, worry about trailer. */
2524
2525 if (skb1->next == NULL && tailbits) {
2526 if (skb_shinfo(skb1)->nr_frags ||
2527 skb_shinfo(skb1)->frag_list ||
2528 skb_tailroom(skb1) < tailbits)
2529 ntail = tailbits + 128;
2530 }
2531
2532 if (copyflag ||
2533 skb_cloned(skb1) ||
2534 ntail ||
2535 skb_shinfo(skb1)->nr_frags ||
2536 skb_shinfo(skb1)->frag_list) {
2537 struct sk_buff *skb2;
2538
2539 /* Fuck, we are miserable poor guys... */
2540 if (ntail == 0)
2541 skb2 = skb_copy(skb1, GFP_ATOMIC);
2542 else
2543 skb2 = skb_copy_expand(skb1,
2544 skb_headroom(skb1),
2545 ntail,
2546 GFP_ATOMIC);
2547 if (unlikely(skb2 == NULL))
2548 return -ENOMEM;
2549
2550 if (skb1->sk)
2551 skb_set_owner_w(skb2, skb1->sk);
2552
2553 /* Looking around. Are we still alive?
2554 * OK, link new skb, drop old one */
2555
2556 skb2->next = skb1->next;
2557 *skb_p = skb2;
2558 kfree_skb(skb1);
2559 skb1 = skb2;
2560 }
2561 elt++;
2562 *trailer = skb1;
2563 skb_p = &skb1->next;
2564 }
2565
2566 return elt;
2567}
2568
Rusty Russellf35d9d82008-02-04 23:49:54 -05002569/**
2570 * skb_partial_csum_set - set up and verify partial csum values for packet
2571 * @skb: the skb to set
2572 * @start: the number of bytes after skb->data to start checksumming.
2573 * @off: the offset from start to place the checksum.
2574 *
2575 * For untrusted partially-checksummed packets, we need to make sure the values
2576 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2577 *
2578 * This function checks and sets those values and skb->ip_summed: if this
2579 * returns false you should drop the packet.
2580 */
2581bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2582{
2583 if (unlikely(start > skb->len - 2) ||
2584 unlikely((int)start + off > skb->len - 2)) {
2585 if (net_ratelimit())
2586 printk(KERN_WARNING
2587 "bad partial csum: csum=%u/%u len=%u\n",
2588 start, off, skb->len);
2589 return false;
2590 }
2591 skb->ip_summed = CHECKSUM_PARTIAL;
2592 skb->csum_start = skb_headroom(skb) + start;
2593 skb->csum_offset = off;
2594 return true;
2595}
2596
Ben Hutchings4497b072008-06-19 16:22:28 -07002597void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2598{
2599 if (net_ratelimit())
2600 pr_warning("%s: received packets cannot be forwarded"
2601 " while LRO is enabled\n", skb->dev->name);
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604EXPORT_SYMBOL(___pskb_trim);
2605EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002606EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002608EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002609EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610EXPORT_SYMBOL(pskb_copy);
2611EXPORT_SYMBOL(pskb_expand_head);
2612EXPORT_SYMBOL(skb_checksum);
2613EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614EXPORT_SYMBOL(skb_copy);
2615EXPORT_SYMBOL(skb_copy_and_csum_bits);
2616EXPORT_SYMBOL(skb_copy_and_csum_dev);
2617EXPORT_SYMBOL(skb_copy_bits);
2618EXPORT_SYMBOL(skb_copy_expand);
2619EXPORT_SYMBOL(skb_over_panic);
2620EXPORT_SYMBOL(skb_pad);
2621EXPORT_SYMBOL(skb_realloc_headroom);
2622EXPORT_SYMBOL(skb_under_panic);
2623EXPORT_SYMBOL(skb_dequeue);
2624EXPORT_SYMBOL(skb_dequeue_tail);
2625EXPORT_SYMBOL(skb_insert);
2626EXPORT_SYMBOL(skb_queue_purge);
2627EXPORT_SYMBOL(skb_queue_head);
2628EXPORT_SYMBOL(skb_queue_tail);
2629EXPORT_SYMBOL(skb_unlink);
2630EXPORT_SYMBOL(skb_append);
2631EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002632EXPORT_SYMBOL(skb_prepare_seq_read);
2633EXPORT_SYMBOL(skb_seq_read);
2634EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002635EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002636EXPORT_SYMBOL(skb_append_datato_frags);
Ben Hutchings4497b072008-06-19 16:22:28 -07002637EXPORT_SYMBOL(__skb_warn_lro_forwarding);
David Howells716ea3a2007-04-02 20:19:53 -07002638
2639EXPORT_SYMBOL_GPL(skb_to_sgvec);
2640EXPORT_SYMBOL_GPL(skb_cow_data);
Rusty Russellf35d9d82008-02-04 23:49:54 -05002641EXPORT_SYMBOL_GPL(skb_partial_csum_set);