blob: c3aa68ceed62e3f81dea98571f7b9c228109676d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
49#include <linux/netdevice.h>
50#ifdef CONFIG_NET_CLS_ACT
51#include <net/pkt_sched.h>
52#endif
53#include <linux/string.h>
54#include <linux/skbuff.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070058#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <net/protocol.h>
61#include <net/dst.h>
62#include <net/sock.h>
63#include <net/checksum.h>
64#include <net/xfrm.h>
65
66#include <asm/uaccess.h>
67#include <asm/system.h>
68
Al Viroa1f8e7f72006-10-19 16:08:53 -040069#include "kmap_skb.h"
70
Christoph Lametere18b8902006-12-06 20:33:20 -080071static struct kmem_cache *skbuff_head_cache __read_mostly;
72static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
79
80/**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89{
Patrick McHardy26095452005-04-21 16:43:02 -070090 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070091 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070092 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070093 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -070094 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 BUG();
96}
97
98/**
99 * skb_under_panic - private function
100 * @skb: buffer
101 * @sz: size
102 * @here: address
103 *
104 * Out of line support code for skb_push(). Not user callable.
105 */
106
107void skb_under_panic(struct sk_buff *skb, int sz, void *here)
108{
Patrick McHardy26095452005-04-21 16:43:02 -0700109 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700110 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700111 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700112 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700113 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 BUG();
115}
116
David S. Millerdc6de332006-04-20 00:10:50 -0700117void skb_truesize_bug(struct sk_buff *skb)
118{
119 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
120 "len=%u, sizeof(sk_buff)=%Zd\n",
121 skb->truesize, skb->len, sizeof(struct sk_buff));
122}
123EXPORT_SYMBOL(skb_truesize_bug);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* Allocate a new skbuff. We do this ourselves so we can fill in a few
126 * 'private' fields and also do memory statistics to find all the
127 * [BEEP] leaks.
128 *
129 */
130
131/**
David S. Millerd179cd12005-08-17 14:57:30 -0700132 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * @size: size to allocate
134 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700135 * @fclone: allocate from fclone cache instead of head cache
136 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800137 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 * Allocate a new &sk_buff. The returned buffer has no headroom and a
140 * tail room of size bytes. The object has a reference count of one.
141 * The return is the buffer. On a failure the return is %NULL.
142 *
143 * Buffers may only be allocated from interrupts using a @gfp_mask of
144 * %GFP_ATOMIC.
145 */
Al Virodd0fc662005-10-07 07:46:04 +0100146struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800147 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Christoph Lametere18b8902006-12-06 20:33:20 -0800149 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800150 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct sk_buff *skb;
152 u8 *data;
153
Herbert Xu8798b3f2006-01-23 16:32:45 -0800154 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800157 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!skb)
159 goto out;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800162 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
163 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!data)
165 goto nodata;
166
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300167 /*
168 * See comment in sk_buff definition, just before the 'tail' member
169 */
170 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 skb->truesize = size + sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
173 skb->head = data;
174 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700175 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700176 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800177 /* make sure we initialize shinfo sequentially */
178 shinfo = skb_shinfo(skb);
179 atomic_set(&shinfo->dataref, 1);
180 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700181 shinfo->gso_size = 0;
182 shinfo->gso_segs = 0;
183 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800184 shinfo->ip6_frag_id = 0;
185 shinfo->frag_list = NULL;
186
David S. Millerd179cd12005-08-17 14:57:30 -0700187 if (fclone) {
188 struct sk_buff *child = skb + 1;
189 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David S. Millerd179cd12005-08-17 14:57:30 -0700191 skb->fclone = SKB_FCLONE_ORIG;
192 atomic_set(fclone_ref, 1);
193
194 child->fclone = SKB_FCLONE_UNAVAILABLE;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
197 return skb;
198nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800199 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 skb = NULL;
201 goto out;
202}
203
204/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700205 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
206 * @dev: network device to receive on
207 * @length: length to allocate
208 * @gfp_mask: get_free_pages mask, passed to alloc_skb
209 *
210 * Allocate a new &sk_buff and assign it a usage count of one. The
211 * buffer has unspecified headroom built in. Users should allocate
212 * the headroom they think they need without accounting for the
213 * built in space. The built in space is used for optimisations.
214 *
215 * %NULL is returned if there is no free memory.
216 */
217struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
218 unsigned int length, gfp_t gfp_mask)
219{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700220 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700221 struct sk_buff *skb;
222
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900223 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700224 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700225 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700226 skb->dev = dev;
227 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700228 return skb;
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Herbert Xu27b437c2006-07-13 19:26:39 -0700231static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Herbert Xu27b437c2006-07-13 19:26:39 -0700233 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Herbert Xu27b437c2006-07-13 19:26:39 -0700235 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 do {
238 struct sk_buff *this = list;
239 list = list->next;
240 kfree_skb(this);
241 } while (list);
242}
243
Herbert Xu27b437c2006-07-13 19:26:39 -0700244static inline void skb_drop_fraglist(struct sk_buff *skb)
245{
246 skb_drop_list(&skb_shinfo(skb)->frag_list);
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static void skb_clone_fraglist(struct sk_buff *skb)
250{
251 struct sk_buff *list;
252
253 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
254 skb_get(list);
255}
256
Adrian Bunk5bba1712006-06-29 13:02:35 -0700257static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 if (!skb->cloned ||
260 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
261 &skb_shinfo(skb)->dataref)) {
262 if (skb_shinfo(skb)->nr_frags) {
263 int i;
264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
265 put_page(skb_shinfo(skb)->frags[i].page);
266 }
267
268 if (skb_shinfo(skb)->frag_list)
269 skb_drop_fraglist(skb);
270
271 kfree(skb->head);
272 }
273}
274
275/*
276 * Free an skbuff by memory without cleaning the state.
277 */
278void kfree_skbmem(struct sk_buff *skb)
279{
David S. Millerd179cd12005-08-17 14:57:30 -0700280 struct sk_buff *other;
281 atomic_t *fclone_ref;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700284 switch (skb->fclone) {
285 case SKB_FCLONE_UNAVAILABLE:
286 kmem_cache_free(skbuff_head_cache, skb);
287 break;
288
289 case SKB_FCLONE_ORIG:
290 fclone_ref = (atomic_t *) (skb + 2);
291 if (atomic_dec_and_test(fclone_ref))
292 kmem_cache_free(skbuff_fclone_cache, skb);
293 break;
294
295 case SKB_FCLONE_CLONE:
296 fclone_ref = (atomic_t *) (skb + 1);
297 other = skb - 1;
298
299 /* The clone portion is available for
300 * fast-cloning again.
301 */
302 skb->fclone = SKB_FCLONE_UNAVAILABLE;
303
304 if (atomic_dec_and_test(fclone_ref))
305 kmem_cache_free(skbuff_fclone_cache, other);
306 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/**
311 * __kfree_skb - private function
312 * @skb: buffer
313 *
314 * Free an sk_buff. Release anything attached to the buffer.
315 * Clean the state. This is an internal helper function. Users should
316 * always call kfree_skb
317 */
318
319void __kfree_skb(struct sk_buff *skb)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dst_release(skb->dst);
322#ifdef CONFIG_XFRM
323 secpath_put(skb->sp);
324#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700325 if (skb->destructor) {
326 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 skb->destructor(skb);
328 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800329#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700330 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800331 nf_conntrack_put_reasm(skb->nfct_reasm);
332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_BRIDGE_NETFILTER
334 nf_bridge_put(skb->nf_bridge);
335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/* XXX: IS this still necessary? - JHS */
337#ifdef CONFIG_NET_SCHED
338 skb->tc_index = 0;
339#ifdef CONFIG_NET_CLS_ACT
340 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
342#endif
343
344 kfree_skbmem(skb);
345}
346
347/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800348 * kfree_skb - free an sk_buff
349 * @skb: buffer to free
350 *
351 * Drop a reference to the buffer and free it if the usage count has
352 * hit zero.
353 */
354void kfree_skb(struct sk_buff *skb)
355{
356 if (unlikely(!skb))
357 return;
358 if (likely(atomic_read(&skb->users) == 1))
359 smp_rmb();
360 else if (likely(!atomic_dec_and_test(&skb->users)))
361 return;
362 __kfree_skb(skb);
363}
364
Herbert Xudec18812007-10-14 00:37:30 -0700365static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
366{
367 new->tstamp = old->tstamp;
368 new->dev = old->dev;
369 new->transport_header = old->transport_header;
370 new->network_header = old->network_header;
371 new->mac_header = old->mac_header;
372 new->dst = dst_clone(old->dst);
373#ifdef CONFIG_INET
374 new->sp = secpath_get(old->sp);
375#endif
376 memcpy(new->cb, old->cb, sizeof(old->cb));
377 new->csum_start = old->csum_start;
378 new->csum_offset = old->csum_offset;
379 new->local_df = old->local_df;
380 new->pkt_type = old->pkt_type;
381 new->ip_summed = old->ip_summed;
382 skb_copy_queue_mapping(new, old);
383 new->priority = old->priority;
384#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
385 new->ipvs_property = old->ipvs_property;
386#endif
387 new->protocol = old->protocol;
388 new->mark = old->mark;
389 __nf_copy(new, old);
390#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
391 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
392 new->nf_trace = old->nf_trace;
393#endif
394#ifdef CONFIG_NET_SCHED
395 new->tc_index = old->tc_index;
396#ifdef CONFIG_NET_CLS_ACT
397 new->tc_verd = old->tc_verd;
398#endif
399#endif
400 skb_copy_secmark(new, old);
401}
402
Herbert Xue0053ec2007-10-14 00:37:52 -0700403static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#define C(x) n->x = skb->x
406
407 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700409 __copy_skb_header(n, skb);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 C(len);
412 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700413 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 n->cloned = 1;
Patrick McHardy334a8132007-06-25 04:35:20 -0700415 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_NET_CLS_ACT
Herbert Xudec18812007-10-14 00:37:30 -0700419 /* FIXME What is this and why don't we do it in copy_skb_header? */
420 n->tc_verd = SET_TC_VERD(n->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700421 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
422 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700423 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 C(truesize);
426 atomic_set(&n->users, 1);
427 C(head);
428 C(data);
429 C(tail);
430 C(end);
431
432 atomic_inc(&(skb_shinfo(skb)->dataref));
433 skb->cloned = 1;
434
435 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700436#undef C
437}
438
439/**
440 * skb_morph - morph one skb into another
441 * @dst: the skb to receive the contents
442 * @src: the skb to supply the contents
443 *
444 * This is identical to skb_clone except that the target skb is
445 * supplied by the user.
446 *
447 * The target skb is returned upon exit.
448 */
449struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
450{
451 skb_release_data(dst);
452 return __skb_clone(dst, src);
453}
454EXPORT_SYMBOL_GPL(skb_morph);
455
456/**
457 * skb_clone - duplicate an sk_buff
458 * @skb: buffer to clone
459 * @gfp_mask: allocation priority
460 *
461 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
462 * copies share the same packet data but not structure. The new
463 * buffer has a reference count of 1. If the allocation fails the
464 * function returns %NULL otherwise the new buffer is returned.
465 *
466 * If this function is called from an interrupt gfp_mask() must be
467 * %GFP_ATOMIC.
468 */
469
470struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
471{
472 struct sk_buff *n;
473
474 n = skb + 1;
475 if (skb->fclone == SKB_FCLONE_ORIG &&
476 n->fclone == SKB_FCLONE_UNAVAILABLE) {
477 atomic_t *fclone_ref = (atomic_t *) (n + 1);
478 n->fclone = SKB_FCLONE_CLONE;
479 atomic_inc(fclone_ref);
480 } else {
481 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
482 if (!n)
483 return NULL;
484 n->fclone = SKB_FCLONE_UNAVAILABLE;
485 }
486
487 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
491{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700492#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Shift between the two data areas in bytes
495 */
496 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700497#endif
Herbert Xudec18812007-10-14 00:37:30 -0700498
499 __copy_skb_header(new, old);
500
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700501#ifndef NET_SKBUFF_DATA_USES_OFFSET
502 /* {transport,network,mac}_header are relative to skb->head */
503 new->transport_header += offset;
504 new->network_header += offset;
505 new->mac_header += offset;
506#endif
Herbert Xu79671682006-06-22 02:40:14 -0700507 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
508 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
509 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/**
513 * skb_copy - create private copy of an sk_buff
514 * @skb: buffer to copy
515 * @gfp_mask: allocation priority
516 *
517 * Make a copy of both an &sk_buff and its data. This is used when the
518 * caller wishes to modify the data and needs a private copy of the
519 * data to alter. Returns %NULL on failure or the pointer to the buffer
520 * on success. The returned buffer has a reference count of 1.
521 *
522 * As by-product this function converts non-linear &sk_buff to linear
523 * one, so that &sk_buff becomes completely private and caller is allowed
524 * to modify all the data of returned buffer. This means that this
525 * function is not recommended for use in circumstances when only
526 * header is going to be modified. Use pskb_copy() instead.
527 */
528
Al Virodd0fc662005-10-07 07:46:04 +0100529struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 int headerlen = skb->data - skb->head;
532 /*
533 * Allocate the copy buffer
534 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700535 struct sk_buff *n;
536#ifdef NET_SKBUFF_DATA_USES_OFFSET
537 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
538#else
539 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!n)
542 return NULL;
543
544 /* Set the data pointer */
545 skb_reserve(n, headerlen);
546 /* Set the tail pointer and length */
547 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
550 BUG();
551
552 copy_skb_header(n, skb);
553 return n;
554}
555
556
557/**
558 * pskb_copy - create copy of an sk_buff with private head.
559 * @skb: buffer to copy
560 * @gfp_mask: allocation priority
561 *
562 * Make a copy of both an &sk_buff and part of its data, located
563 * in header. Fragmented data remain shared. This is used when
564 * the caller wishes to modify only header of &sk_buff and needs
565 * private copy of the header to alter. Returns %NULL on failure
566 * or the pointer to the buffer on success.
567 * The returned buffer has a reference count of 1.
568 */
569
Al Virodd0fc662005-10-07 07:46:04 +0100570struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 /*
573 * Allocate the copy buffer
574 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700575 struct sk_buff *n;
576#ifdef NET_SKBUFF_DATA_USES_OFFSET
577 n = alloc_skb(skb->end, gfp_mask);
578#else
579 n = alloc_skb(skb->end - skb->head, gfp_mask);
580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!n)
582 goto out;
583
584 /* Set the data pointer */
585 skb_reserve(n, skb->data - skb->head);
586 /* Set the tail pointer and length */
587 skb_put(n, skb_headlen(skb));
588 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300589 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Herbert Xu25f484a2006-11-07 14:57:15 -0800591 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 n->data_len = skb->data_len;
593 n->len = skb->len;
594
595 if (skb_shinfo(skb)->nr_frags) {
596 int i;
597
598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
599 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
600 get_page(skb_shinfo(n)->frags[i].page);
601 }
602 skb_shinfo(n)->nr_frags = i;
603 }
604
605 if (skb_shinfo(skb)->frag_list) {
606 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
607 skb_clone_fraglist(n);
608 }
609
610 copy_skb_header(n, skb);
611out:
612 return n;
613}
614
615/**
616 * pskb_expand_head - reallocate header of &sk_buff
617 * @skb: buffer to reallocate
618 * @nhead: room to add at head
619 * @ntail: room to add at tail
620 * @gfp_mask: allocation priority
621 *
622 * Expands (or creates identical copy, if &nhead and &ntail are zero)
623 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
624 * reference count of 1. Returns zero in the case of success or error,
625 * if expansion failed. In the last case, &sk_buff is not changed.
626 *
627 * All the pointers pointing into skb header may change and must be
628 * reloaded after call to this function.
629 */
630
Victor Fusco86a76ca2005-07-08 14:57:47 -0700631int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100632 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 int i;
635 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700636#ifdef NET_SKBUFF_DATA_USES_OFFSET
637 int size = nhead + skb->end + ntail;
638#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700640#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 long off;
642
643 if (skb_shared(skb))
644 BUG();
645
646 size = SKB_DATA_ALIGN(size);
647
648 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
649 if (!data)
650 goto nodata;
651
652 /* Copy only real data... and, alas, header. This should be
653 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700654#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700655 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700656#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700657 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700658#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700659 memcpy(data + size, skb_end_pointer(skb),
660 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
663 get_page(skb_shinfo(skb)->frags[i].page);
664
665 if (skb_shinfo(skb)->frag_list)
666 skb_clone_fraglist(skb);
667
668 skb_release_data(skb);
669
670 off = (data + nhead) - skb->head;
671
672 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700674#ifdef NET_SKBUFF_DATA_USES_OFFSET
675 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700676 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700677#else
678 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700679#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700680 /* {transport,network,mac}_header and tail are relative to skb->head */
681 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700682 skb->transport_header += off;
683 skb->network_header += off;
684 skb->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700685 skb->csum_start += off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700687 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 skb->nohdr = 0;
689 atomic_set(&skb_shinfo(skb)->dataref, 1);
690 return 0;
691
692nodata:
693 return -ENOMEM;
694}
695
696/* Make private copy of skb with writable head and some headroom */
697
698struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
699{
700 struct sk_buff *skb2;
701 int delta = headroom - skb_headroom(skb);
702
703 if (delta <= 0)
704 skb2 = pskb_copy(skb, GFP_ATOMIC);
705 else {
706 skb2 = skb_clone(skb, GFP_ATOMIC);
707 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
708 GFP_ATOMIC)) {
709 kfree_skb(skb2);
710 skb2 = NULL;
711 }
712 }
713 return skb2;
714}
715
716
717/**
718 * skb_copy_expand - copy and expand sk_buff
719 * @skb: buffer to copy
720 * @newheadroom: new free bytes at head
721 * @newtailroom: new free bytes at tail
722 * @gfp_mask: allocation priority
723 *
724 * Make a copy of both an &sk_buff and its data and while doing so
725 * allocate additional space.
726 *
727 * This is used when the caller wishes to modify the data and needs a
728 * private copy of the data to alter as well as more space for new fields.
729 * Returns %NULL on failure or the pointer to the buffer
730 * on success. The returned buffer has a reference count of 1.
731 *
732 * You must pass %GFP_ATOMIC as the allocation priority if this function
733 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700736 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100737 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 /*
740 * Allocate the copy buffer
741 */
742 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
743 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700744 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700746 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (!n)
749 return NULL;
750
751 skb_reserve(n, newheadroom);
752
753 /* Set the tail pointer and length */
754 skb_put(n, skb->len);
755
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700756 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 head_copy_off = 0;
758 if (newheadroom <= head_copy_len)
759 head_copy_len = newheadroom;
760 else
761 head_copy_off = newheadroom - head_copy_len;
762
763 /* Copy the linear header and data. */
764 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
765 skb->len + head_copy_len))
766 BUG();
767
768 copy_skb_header(n, skb);
769
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700770 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700771 n->csum_start += off;
772#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700773 n->transport_header += off;
774 n->network_header += off;
775 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700776#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return n;
779}
780
781/**
782 * skb_pad - zero pad the tail of an skb
783 * @skb: buffer to pad
784 * @pad: space to pad
785 *
786 * Ensure that a buffer is followed by a padding area that is zero
787 * filled. Used by network drivers which may DMA or transfer data
788 * beyond the buffer end onto the wire.
789 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700790 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900792
Herbert Xu5b057c62006-06-23 02:06:41 -0700793int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Herbert Xu5b057c62006-06-23 02:06:41 -0700795 int err;
796 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700799 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700803
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700804 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700805 if (likely(skb_cloned(skb) || ntail > 0)) {
806 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
807 if (unlikely(err))
808 goto free_skb;
809 }
810
811 /* FIXME: The use of this function with non-linear skb's really needs
812 * to be audited.
813 */
814 err = skb_linearize(skb);
815 if (unlikely(err))
816 goto free_skb;
817
818 memset(skb->data + skb->len, 0, pad);
819 return 0;
820
821free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700823 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900824}
825
Herbert Xu3cc0e872006-06-09 16:13:38 -0700826/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
828
Herbert Xu3cc0e872006-06-09 16:13:38 -0700829int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Herbert Xu27b437c2006-07-13 19:26:39 -0700831 struct sk_buff **fragp;
832 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int offset = skb_headlen(skb);
834 int nfrags = skb_shinfo(skb)->nr_frags;
835 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700836 int err;
837
838 if (skb_cloned(skb) &&
839 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700842 i = 0;
843 if (offset >= len)
844 goto drop_pages;
845
846 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700848
849 if (end < len) {
850 offset = end;
851 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700853
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700854 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700855
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700856drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700857 skb_shinfo(skb)->nr_frags = i;
858
859 for (; i < nfrags; i++)
860 put_page(skb_shinfo(skb)->frags[i].page);
861
862 if (skb_shinfo(skb)->frag_list)
863 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700864 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Herbert Xu27b437c2006-07-13 19:26:39 -0700867 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
868 fragp = &frag->next) {
869 int end = offset + frag->len;
870
871 if (skb_shared(frag)) {
872 struct sk_buff *nfrag;
873
874 nfrag = skb_clone(frag, GFP_ATOMIC);
875 if (unlikely(!nfrag))
876 return -ENOMEM;
877
878 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700879 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700880 frag = nfrag;
881 *fragp = frag;
882 }
883
884 if (end < len) {
885 offset = end;
886 continue;
887 }
888
889 if (end > len &&
890 unlikely((err = pskb_trim(frag, len - offset))))
891 return err;
892
893 if (frag->next)
894 skb_drop_list(&frag->next);
895 break;
896 }
897
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700898done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700899 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 skb->data_len -= skb->len - len;
901 skb->len = len;
902 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700903 skb->len = len;
904 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700905 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 return 0;
909}
910
911/**
912 * __pskb_pull_tail - advance tail of skb header
913 * @skb: buffer to reallocate
914 * @delta: number of bytes to advance tail
915 *
916 * The function makes a sense only on a fragmented &sk_buff,
917 * it expands header moving its tail forward and copying necessary
918 * data from fragmented part.
919 *
920 * &sk_buff MUST have reference count of 1.
921 *
922 * Returns %NULL (and &sk_buff does not change) if pull failed
923 * or value of new tail of skb in the case of success.
924 *
925 * All the pointers pointing into skb header may change and must be
926 * reloaded after call to this function.
927 */
928
929/* Moves tail of skb head forward, copying data from fragmented part,
930 * when it is necessary.
931 * 1. It may fail due to malloc failure.
932 * 2. It may change skb pointers.
933 *
934 * It is pretty complicated. Luckily, it is called only in exceptional cases.
935 */
936unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
937{
938 /* If skb has not enough free space at tail, get new one
939 * plus 128 bytes for future expansions. If we have enough
940 * room at tail, reallocate without expansion only if skb is cloned.
941 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700942 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (eat > 0 || skb_cloned(skb)) {
945 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
946 GFP_ATOMIC))
947 return NULL;
948 }
949
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700950 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 BUG();
952
953 /* Optimization: no fragments, no reasons to preestimate
954 * size of pulled pages. Superb.
955 */
956 if (!skb_shinfo(skb)->frag_list)
957 goto pull_pages;
958
959 /* Estimate size of pulled pages. */
960 eat = delta;
961 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
962 if (skb_shinfo(skb)->frags[i].size >= eat)
963 goto pull_pages;
964 eat -= skb_shinfo(skb)->frags[i].size;
965 }
966
967 /* If we need update frag list, we are in troubles.
968 * Certainly, it possible to add an offset to skb data,
969 * but taking into account that pulling is expected to
970 * be very rare operation, it is worth to fight against
971 * further bloating skb head and crucify ourselves here instead.
972 * Pure masohism, indeed. 8)8)
973 */
974 if (eat) {
975 struct sk_buff *list = skb_shinfo(skb)->frag_list;
976 struct sk_buff *clone = NULL;
977 struct sk_buff *insp = NULL;
978
979 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800980 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (list->len <= eat) {
983 /* Eaten as whole. */
984 eat -= list->len;
985 list = list->next;
986 insp = list;
987 } else {
988 /* Eaten partially. */
989
990 if (skb_shared(list)) {
991 /* Sucks! We need to fork list. :-( */
992 clone = skb_clone(list, GFP_ATOMIC);
993 if (!clone)
994 return NULL;
995 insp = list->next;
996 list = clone;
997 } else {
998 /* This may be pulled without
999 * problems. */
1000 insp = list;
1001 }
1002 if (!pskb_pull(list, eat)) {
1003 if (clone)
1004 kfree_skb(clone);
1005 return NULL;
1006 }
1007 break;
1008 }
1009 } while (eat);
1010
1011 /* Free pulled out fragments. */
1012 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1013 skb_shinfo(skb)->frag_list = list->next;
1014 kfree_skb(list);
1015 }
1016 /* And insert new clone at head. */
1017 if (clone) {
1018 clone->next = list;
1019 skb_shinfo(skb)->frag_list = clone;
1020 }
1021 }
1022 /* Success! Now we may commit changes to skb data. */
1023
1024pull_pages:
1025 eat = delta;
1026 k = 0;
1027 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1028 if (skb_shinfo(skb)->frags[i].size <= eat) {
1029 put_page(skb_shinfo(skb)->frags[i].page);
1030 eat -= skb_shinfo(skb)->frags[i].size;
1031 } else {
1032 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1033 if (eat) {
1034 skb_shinfo(skb)->frags[k].page_offset += eat;
1035 skb_shinfo(skb)->frags[k].size -= eat;
1036 eat = 0;
1037 }
1038 k++;
1039 }
1040 }
1041 skb_shinfo(skb)->nr_frags = k;
1042
1043 skb->tail += delta;
1044 skb->data_len -= delta;
1045
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001046 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/* Copy some data bits from skb to kernel buffer. */
1050
1051int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1052{
1053 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001054 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (offset > (int)skb->len - len)
1057 goto fault;
1058
1059 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001060 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (copy > len)
1062 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001063 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if ((len -= copy) == 0)
1065 return 0;
1066 offset += copy;
1067 to += copy;
1068 }
1069
1070 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001071 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
David S. Miller1a028e52007-04-27 15:21:23 -07001073 BUG_TRAP(start <= offset + len);
1074
1075 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if ((copy = end - offset) > 0) {
1077 u8 *vaddr;
1078
1079 if (copy > len)
1080 copy = len;
1081
1082 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1083 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001084 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1085 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kunmap_skb_frag(vaddr);
1087
1088 if ((len -= copy) == 0)
1089 return 0;
1090 offset += copy;
1091 to += copy;
1092 }
David S. Miller1a028e52007-04-27 15:21:23 -07001093 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
1096 if (skb_shinfo(skb)->frag_list) {
1097 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1098
1099 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001100 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
David S. Miller1a028e52007-04-27 15:21:23 -07001102 BUG_TRAP(start <= offset + len);
1103
1104 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if ((copy = end - offset) > 0) {
1106 if (copy > len)
1107 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001108 if (skb_copy_bits(list, offset - start,
1109 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 goto fault;
1111 if ((len -= copy) == 0)
1112 return 0;
1113 offset += copy;
1114 to += copy;
1115 }
David S. Miller1a028e52007-04-27 15:21:23 -07001116 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 }
1119 if (!len)
1120 return 0;
1121
1122fault:
1123 return -EFAULT;
1124}
1125
Herbert Xu357b40a2005-04-19 22:30:14 -07001126/**
1127 * skb_store_bits - store bits from kernel buffer to skb
1128 * @skb: destination buffer
1129 * @offset: offset in destination
1130 * @from: source buffer
1131 * @len: number of bytes to copy
1132 *
1133 * Copy the specified number of bytes from the source buffer to the
1134 * destination skb. This function handles all the messy bits of
1135 * traversing fragment lists and such.
1136 */
1137
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001138int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001139{
1140 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001141 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001142
1143 if (offset > (int)skb->len - len)
1144 goto fault;
1145
David S. Miller1a028e52007-04-27 15:21:23 -07001146 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001147 if (copy > len)
1148 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001149 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001150 if ((len -= copy) == 0)
1151 return 0;
1152 offset += copy;
1153 from += copy;
1154 }
1155
1156 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1157 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001158 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001159
David S. Miller1a028e52007-04-27 15:21:23 -07001160 BUG_TRAP(start <= offset + len);
1161
1162 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001163 if ((copy = end - offset) > 0) {
1164 u8 *vaddr;
1165
1166 if (copy > len)
1167 copy = len;
1168
1169 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001170 memcpy(vaddr + frag->page_offset + offset - start,
1171 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001172 kunmap_skb_frag(vaddr);
1173
1174 if ((len -= copy) == 0)
1175 return 0;
1176 offset += copy;
1177 from += copy;
1178 }
David S. Miller1a028e52007-04-27 15:21:23 -07001179 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001180 }
1181
1182 if (skb_shinfo(skb)->frag_list) {
1183 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1184
1185 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001186 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001187
David S. Miller1a028e52007-04-27 15:21:23 -07001188 BUG_TRAP(start <= offset + len);
1189
1190 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001191 if ((copy = end - offset) > 0) {
1192 if (copy > len)
1193 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001194 if (skb_store_bits(list, offset - start,
1195 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001196 goto fault;
1197 if ((len -= copy) == 0)
1198 return 0;
1199 offset += copy;
1200 from += copy;
1201 }
David S. Miller1a028e52007-04-27 15:21:23 -07001202 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001203 }
1204 }
1205 if (!len)
1206 return 0;
1207
1208fault:
1209 return -EFAULT;
1210}
1211
1212EXPORT_SYMBOL(skb_store_bits);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/* Checksum skb data. */
1215
Al Viro2bbbc862006-11-14 21:37:14 -08001216__wsum skb_checksum(const struct sk_buff *skb, int offset,
1217 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
David S. Miller1a028e52007-04-27 15:21:23 -07001219 int start = skb_headlen(skb);
1220 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int pos = 0;
1222
1223 /* Checksum header. */
1224 if (copy > 0) {
1225 if (copy > len)
1226 copy = len;
1227 csum = csum_partial(skb->data + offset, copy, csum);
1228 if ((len -= copy) == 0)
1229 return csum;
1230 offset += copy;
1231 pos = copy;
1232 }
1233
1234 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001235 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
David S. Miller1a028e52007-04-27 15:21:23 -07001237 BUG_TRAP(start <= offset + len);
1238
1239 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001241 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 u8 *vaddr;
1243 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1244
1245 if (copy > len)
1246 copy = len;
1247 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001248 csum2 = csum_partial(vaddr + frag->page_offset +
1249 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 kunmap_skb_frag(vaddr);
1251 csum = csum_block_add(csum, csum2, pos);
1252 if (!(len -= copy))
1253 return csum;
1254 offset += copy;
1255 pos += copy;
1256 }
David S. Miller1a028e52007-04-27 15:21:23 -07001257 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 if (skb_shinfo(skb)->frag_list) {
1261 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1262
1263 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001264 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
David S. Miller1a028e52007-04-27 15:21:23 -07001266 BUG_TRAP(start <= offset + len);
1267
1268 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001270 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (copy > len)
1272 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001273 csum2 = skb_checksum(list, offset - start,
1274 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 csum = csum_block_add(csum, csum2, pos);
1276 if ((len -= copy) == 0)
1277 return csum;
1278 offset += copy;
1279 pos += copy;
1280 }
David S. Miller1a028e52007-04-27 15:21:23 -07001281 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001284 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 return csum;
1287}
1288
1289/* Both of above in one bottle. */
1290
Al Viro81d77662006-11-14 21:37:33 -08001291__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1292 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
David S. Miller1a028e52007-04-27 15:21:23 -07001294 int start = skb_headlen(skb);
1295 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int pos = 0;
1297
1298 /* Copy header. */
1299 if (copy > 0) {
1300 if (copy > len)
1301 copy = len;
1302 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1303 copy, csum);
1304 if ((len -= copy) == 0)
1305 return csum;
1306 offset += copy;
1307 to += copy;
1308 pos = copy;
1309 }
1310
1311 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001312 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David S. Miller1a028e52007-04-27 15:21:23 -07001314 BUG_TRAP(start <= offset + len);
1315
1316 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001318 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 u8 *vaddr;
1320 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1321
1322 if (copy > len)
1323 copy = len;
1324 vaddr = kmap_skb_frag(frag);
1325 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001326 frag->page_offset +
1327 offset - start, to,
1328 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 kunmap_skb_frag(vaddr);
1330 csum = csum_block_add(csum, csum2, pos);
1331 if (!(len -= copy))
1332 return csum;
1333 offset += copy;
1334 to += copy;
1335 pos += copy;
1336 }
David S. Miller1a028e52007-04-27 15:21:23 -07001337 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
1340 if (skb_shinfo(skb)->frag_list) {
1341 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1342
1343 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001344 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001345 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
David S. Miller1a028e52007-04-27 15:21:23 -07001347 BUG_TRAP(start <= offset + len);
1348
1349 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if ((copy = end - offset) > 0) {
1351 if (copy > len)
1352 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001353 csum2 = skb_copy_and_csum_bits(list,
1354 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 to, copy, 0);
1356 csum = csum_block_add(csum, csum2, pos);
1357 if ((len -= copy) == 0)
1358 return csum;
1359 offset += copy;
1360 to += copy;
1361 pos += copy;
1362 }
David S. Miller1a028e52007-04-27 15:21:23 -07001363 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001366 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return csum;
1368}
1369
1370void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1371{
Al Virod3bc23e2006-11-14 21:24:49 -08001372 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 long csstart;
1374
Patrick McHardy84fa7932006-08-29 16:44:56 -07001375 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001376 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 else
1378 csstart = skb_headlen(skb);
1379
Kris Katterjohn09a62662006-01-08 22:24:28 -08001380 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001382 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 csum = 0;
1385 if (csstart != skb->len)
1386 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1387 skb->len - csstart, 0);
1388
Patrick McHardy84fa7932006-08-29 16:44:56 -07001389 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001390 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Al Virod3bc23e2006-11-14 21:24:49 -08001392 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394}
1395
1396/**
1397 * skb_dequeue - remove from the head of the queue
1398 * @list: list to dequeue from
1399 *
1400 * Remove the head of the list. The list lock is taken so the function
1401 * may be used safely with other locking list functions. The head item is
1402 * returned or %NULL if the list is empty.
1403 */
1404
1405struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1406{
1407 unsigned long flags;
1408 struct sk_buff *result;
1409
1410 spin_lock_irqsave(&list->lock, flags);
1411 result = __skb_dequeue(list);
1412 spin_unlock_irqrestore(&list->lock, flags);
1413 return result;
1414}
1415
1416/**
1417 * skb_dequeue_tail - remove from the tail of the queue
1418 * @list: list to dequeue from
1419 *
1420 * Remove the tail of the list. The list lock is taken so the function
1421 * may be used safely with other locking list functions. The tail item is
1422 * returned or %NULL if the list is empty.
1423 */
1424struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1425{
1426 unsigned long flags;
1427 struct sk_buff *result;
1428
1429 spin_lock_irqsave(&list->lock, flags);
1430 result = __skb_dequeue_tail(list);
1431 spin_unlock_irqrestore(&list->lock, flags);
1432 return result;
1433}
1434
1435/**
1436 * skb_queue_purge - empty a list
1437 * @list: list to empty
1438 *
1439 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1440 * the list and one reference dropped. This function takes the list
1441 * lock and is atomic with respect to other list locking functions.
1442 */
1443void skb_queue_purge(struct sk_buff_head *list)
1444{
1445 struct sk_buff *skb;
1446 while ((skb = skb_dequeue(list)) != NULL)
1447 kfree_skb(skb);
1448}
1449
1450/**
1451 * skb_queue_head - queue a buffer at the list head
1452 * @list: list to use
1453 * @newsk: buffer to queue
1454 *
1455 * Queue a buffer at the start of the list. This function takes the
1456 * list lock and can be used safely with other locking &sk_buff functions
1457 * safely.
1458 *
1459 * A buffer cannot be placed on two lists at the same time.
1460 */
1461void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1462{
1463 unsigned long flags;
1464
1465 spin_lock_irqsave(&list->lock, flags);
1466 __skb_queue_head(list, newsk);
1467 spin_unlock_irqrestore(&list->lock, flags);
1468}
1469
1470/**
1471 * skb_queue_tail - queue a buffer at the list tail
1472 * @list: list to use
1473 * @newsk: buffer to queue
1474 *
1475 * Queue a buffer at the tail of the list. This function takes the
1476 * list lock and can be used safely with other locking &sk_buff functions
1477 * safely.
1478 *
1479 * A buffer cannot be placed on two lists at the same time.
1480 */
1481void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1482{
1483 unsigned long flags;
1484
1485 spin_lock_irqsave(&list->lock, flags);
1486 __skb_queue_tail(list, newsk);
1487 spin_unlock_irqrestore(&list->lock, flags);
1488}
David S. Miller8728b832005-08-09 19:25:21 -07001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/**
1491 * skb_unlink - remove a buffer from a list
1492 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001493 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
David S. Miller8728b832005-08-09 19:25:21 -07001495 * Remove a packet from a list. The list locks are taken and this
1496 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
David S. Miller8728b832005-08-09 19:25:21 -07001498 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
David S. Miller8728b832005-08-09 19:25:21 -07001500void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
David S. Miller8728b832005-08-09 19:25:21 -07001502 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
David S. Miller8728b832005-08-09 19:25:21 -07001504 spin_lock_irqsave(&list->lock, flags);
1505 __skb_unlink(skb, list);
1506 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/**
1510 * skb_append - append a buffer
1511 * @old: buffer to insert after
1512 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001513 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *
1515 * Place a packet after a given packet in a list. The list locks are taken
1516 * and this function is atomic with respect to other list locked calls.
1517 * A buffer cannot be placed on two lists at the same time.
1518 */
David S. Miller8728b832005-08-09 19:25:21 -07001519void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 unsigned long flags;
1522
David S. Miller8728b832005-08-09 19:25:21 -07001523 spin_lock_irqsave(&list->lock, flags);
1524 __skb_append(old, newsk, list);
1525 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528
1529/**
1530 * skb_insert - insert a buffer
1531 * @old: buffer to insert before
1532 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001533 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 *
David S. Miller8728b832005-08-09 19:25:21 -07001535 * Place a packet before a given packet in a list. The list locks are
1536 * taken and this function is atomic with respect to other list locked
1537 * calls.
1538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * A buffer cannot be placed on two lists at the same time.
1540 */
David S. Miller8728b832005-08-09 19:25:21 -07001541void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 unsigned long flags;
1544
David S. Miller8728b832005-08-09 19:25:21 -07001545 spin_lock_irqsave(&list->lock, flags);
1546 __skb_insert(newsk, old->prev, old, list);
1547 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static inline void skb_split_inside_header(struct sk_buff *skb,
1551 struct sk_buff* skb1,
1552 const u32 len, const int pos)
1553{
1554 int i;
1555
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001556 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1557 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* And move data appendix as is. */
1559 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1560 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1561
1562 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1563 skb_shinfo(skb)->nr_frags = 0;
1564 skb1->data_len = skb->data_len;
1565 skb1->len += skb1->data_len;
1566 skb->data_len = 0;
1567 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001568 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571static inline void skb_split_no_header(struct sk_buff *skb,
1572 struct sk_buff* skb1,
1573 const u32 len, int pos)
1574{
1575 int i, k = 0;
1576 const int nfrags = skb_shinfo(skb)->nr_frags;
1577
1578 skb_shinfo(skb)->nr_frags = 0;
1579 skb1->len = skb1->data_len = skb->len - len;
1580 skb->len = len;
1581 skb->data_len = len - pos;
1582
1583 for (i = 0; i < nfrags; i++) {
1584 int size = skb_shinfo(skb)->frags[i].size;
1585
1586 if (pos + size > len) {
1587 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1588
1589 if (pos < len) {
1590 /* Split frag.
1591 * We have two variants in this case:
1592 * 1. Move all the frag to the second
1593 * part, if it is possible. F.e.
1594 * this approach is mandatory for TUX,
1595 * where splitting is expensive.
1596 * 2. Split is accurately. We make this.
1597 */
1598 get_page(skb_shinfo(skb)->frags[i].page);
1599 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1600 skb_shinfo(skb1)->frags[0].size -= len - pos;
1601 skb_shinfo(skb)->frags[i].size = len - pos;
1602 skb_shinfo(skb)->nr_frags++;
1603 }
1604 k++;
1605 } else
1606 skb_shinfo(skb)->nr_frags++;
1607 pos += size;
1608 }
1609 skb_shinfo(skb1)->nr_frags = k;
1610}
1611
1612/**
1613 * skb_split - Split fragmented skb to two parts at length len.
1614 * @skb: the buffer to split
1615 * @skb1: the buffer to receive the second part
1616 * @len: new length for skb
1617 */
1618void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1619{
1620 int pos = skb_headlen(skb);
1621
1622 if (len < pos) /* Split line is inside header. */
1623 skb_split_inside_header(skb, skb1, len, pos);
1624 else /* Second chunk has no header, nothing to copy. */
1625 skb_split_no_header(skb, skb1, len, pos);
1626}
1627
Thomas Graf677e90e2005-06-23 20:59:51 -07001628/**
1629 * skb_prepare_seq_read - Prepare a sequential read of skb data
1630 * @skb: the buffer to read
1631 * @from: lower offset of data to be read
1632 * @to: upper offset of data to be read
1633 * @st: state variable
1634 *
1635 * Initializes the specified state variable. Must be called before
1636 * invoking skb_seq_read() for the first time.
1637 */
1638void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1639 unsigned int to, struct skb_seq_state *st)
1640{
1641 st->lower_offset = from;
1642 st->upper_offset = to;
1643 st->root_skb = st->cur_skb = skb;
1644 st->frag_idx = st->stepped_offset = 0;
1645 st->frag_data = NULL;
1646}
1647
1648/**
1649 * skb_seq_read - Sequentially read skb data
1650 * @consumed: number of bytes consumed by the caller so far
1651 * @data: destination pointer for data to be returned
1652 * @st: state variable
1653 *
1654 * Reads a block of skb data at &consumed relative to the
1655 * lower offset specified to skb_prepare_seq_read(). Assigns
1656 * the head of the data block to &data and returns the length
1657 * of the block or 0 if the end of the skb data or the upper
1658 * offset has been reached.
1659 *
1660 * The caller is not required to consume all of the data
1661 * returned, i.e. &consumed is typically set to the number
1662 * of bytes already consumed and the next call to
1663 * skb_seq_read() will return the remaining part of the block.
1664 *
1665 * Note: The size of each block of data returned can be arbitary,
1666 * this limitation is the cost for zerocopy seqeuental
1667 * reads of potentially non linear data.
1668 *
1669 * Note: Fragment lists within fragments are not implemented
1670 * at the moment, state->root_skb could be replaced with
1671 * a stack for this purpose.
1672 */
1673unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1674 struct skb_seq_state *st)
1675{
1676 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1677 skb_frag_t *frag;
1678
1679 if (unlikely(abs_offset >= st->upper_offset))
1680 return 0;
1681
1682next_skb:
1683 block_limit = skb_headlen(st->cur_skb);
1684
1685 if (abs_offset < block_limit) {
1686 *data = st->cur_skb->data + abs_offset;
1687 return block_limit - abs_offset;
1688 }
1689
1690 if (st->frag_idx == 0 && !st->frag_data)
1691 st->stepped_offset += skb_headlen(st->cur_skb);
1692
1693 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1694 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1695 block_limit = frag->size + st->stepped_offset;
1696
1697 if (abs_offset < block_limit) {
1698 if (!st->frag_data)
1699 st->frag_data = kmap_skb_frag(frag);
1700
1701 *data = (u8 *) st->frag_data + frag->page_offset +
1702 (abs_offset - st->stepped_offset);
1703
1704 return block_limit - abs_offset;
1705 }
1706
1707 if (st->frag_data) {
1708 kunmap_skb_frag(st->frag_data);
1709 st->frag_data = NULL;
1710 }
1711
1712 st->frag_idx++;
1713 st->stepped_offset += frag->size;
1714 }
1715
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001716 if (st->frag_data) {
1717 kunmap_skb_frag(st->frag_data);
1718 st->frag_data = NULL;
1719 }
1720
Thomas Graf677e90e2005-06-23 20:59:51 -07001721 if (st->cur_skb->next) {
1722 st->cur_skb = st->cur_skb->next;
1723 st->frag_idx = 0;
1724 goto next_skb;
1725 } else if (st->root_skb == st->cur_skb &&
1726 skb_shinfo(st->root_skb)->frag_list) {
1727 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1728 goto next_skb;
1729 }
1730
1731 return 0;
1732}
1733
1734/**
1735 * skb_abort_seq_read - Abort a sequential read of skb data
1736 * @st: state variable
1737 *
1738 * Must be called if skb_seq_read() was not called until it
1739 * returned 0.
1740 */
1741void skb_abort_seq_read(struct skb_seq_state *st)
1742{
1743 if (st->frag_data)
1744 kunmap_skb_frag(st->frag_data);
1745}
1746
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001747#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1748
1749static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1750 struct ts_config *conf,
1751 struct ts_state *state)
1752{
1753 return skb_seq_read(offset, text, TS_SKB_CB(state));
1754}
1755
1756static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1757{
1758 skb_abort_seq_read(TS_SKB_CB(state));
1759}
1760
1761/**
1762 * skb_find_text - Find a text pattern in skb data
1763 * @skb: the buffer to look in
1764 * @from: search offset
1765 * @to: search limit
1766 * @config: textsearch configuration
1767 * @state: uninitialized textsearch state variable
1768 *
1769 * Finds a pattern in the skb data according to the specified
1770 * textsearch configuration. Use textsearch_next() to retrieve
1771 * subsequent occurrences of the pattern. Returns the offset
1772 * to the first occurrence or UINT_MAX if no match was found.
1773 */
1774unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1775 unsigned int to, struct ts_config *config,
1776 struct ts_state *state)
1777{
Phil Oesterf72b9482006-06-26 00:00:57 -07001778 unsigned int ret;
1779
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001780 config->get_next_block = skb_ts_get_next_block;
1781 config->finish = skb_ts_finish;
1782
1783 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1784
Phil Oesterf72b9482006-06-26 00:00:57 -07001785 ret = textsearch_find(config, state);
1786 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001787}
1788
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001789/**
1790 * skb_append_datato_frags: - append the user data to a skb
1791 * @sk: sock structure
1792 * @skb: skb structure to be appened with user data.
1793 * @getfrag: call back function to be used for getting the user data
1794 * @from: pointer to user message iov
1795 * @length: length of the iov message
1796 *
1797 * Description: This procedure append the user data in the fragment part
1798 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1799 */
1800int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001801 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001802 int len, int odd, struct sk_buff *skb),
1803 void *from, int length)
1804{
1805 int frg_cnt = 0;
1806 skb_frag_t *frag = NULL;
1807 struct page *page = NULL;
1808 int copy, left;
1809 int offset = 0;
1810 int ret;
1811
1812 do {
1813 /* Return error if we don't have space for new frag */
1814 frg_cnt = skb_shinfo(skb)->nr_frags;
1815 if (frg_cnt >= MAX_SKB_FRAGS)
1816 return -EFAULT;
1817
1818 /* allocate a new page for next frag */
1819 page = alloc_pages(sk->sk_allocation, 0);
1820
1821 /* If alloc_page fails just return failure and caller will
1822 * free previous allocated pages by doing kfree_skb()
1823 */
1824 if (page == NULL)
1825 return -ENOMEM;
1826
1827 /* initialize the next frag */
1828 sk->sk_sndmsg_page = page;
1829 sk->sk_sndmsg_off = 0;
1830 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1831 skb->truesize += PAGE_SIZE;
1832 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1833
1834 /* get the new initialized frag */
1835 frg_cnt = skb_shinfo(skb)->nr_frags;
1836 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1837
1838 /* copy the user data to page */
1839 left = PAGE_SIZE - frag->page_offset;
1840 copy = (length > left)? left : length;
1841
1842 ret = getfrag(from, (page_address(frag->page) +
1843 frag->page_offset + frag->size),
1844 offset, copy, 0, skb);
1845 if (ret < 0)
1846 return -EFAULT;
1847
1848 /* copy was successful so update the size parameters */
1849 sk->sk_sndmsg_off += copy;
1850 frag->size += copy;
1851 skb->len += copy;
1852 skb->data_len += copy;
1853 offset += copy;
1854 length -= copy;
1855
1856 } while (length > 0);
1857
1858 return 0;
1859}
1860
Herbert Xucbb042f2006-03-20 22:43:56 -08001861/**
1862 * skb_pull_rcsum - pull skb and update receive checksum
1863 * @skb: buffer to update
1864 * @start: start of data before pull
1865 * @len: length of data pulled
1866 *
1867 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001868 * update the CHECKSUM_COMPLETE checksum. It should be used on
1869 * receive path processing instead of skb_pull unless you know
1870 * that the checksum difference is zero (e.g., a valid IP header)
1871 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001872 */
1873unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1874{
1875 BUG_ON(len > skb->len);
1876 skb->len -= len;
1877 BUG_ON(skb->len < skb->data_len);
1878 skb_postpull_rcsum(skb, skb->data, len);
1879 return skb->data += len;
1880}
1881
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001882EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1883
Herbert Xuf4c50d92006-06-22 03:02:40 -07001884/**
1885 * skb_segment - Perform protocol segmentation on skb.
1886 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001887 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001888 *
1889 * This function performs segmentation on the given skb. It returns
1890 * the segment at the given position. It returns NULL if there are
1891 * no more segments to generate, or when an error is encountered.
1892 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001893struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001894{
1895 struct sk_buff *segs = NULL;
1896 struct sk_buff *tail = NULL;
1897 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001898 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001899 unsigned int offset = doffset;
1900 unsigned int headroom;
1901 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001902 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001903 int nfrags = skb_shinfo(skb)->nr_frags;
1904 int err = -ENOMEM;
1905 int i = 0;
1906 int pos;
1907
1908 __skb_push(skb, doffset);
1909 headroom = skb_headroom(skb);
1910 pos = skb_headlen(skb);
1911
1912 do {
1913 struct sk_buff *nskb;
1914 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001915 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001916 int k;
1917 int size;
1918
1919 len = skb->len - offset;
1920 if (len > mss)
1921 len = mss;
1922
1923 hsize = skb_headlen(skb) - offset;
1924 if (hsize < 0)
1925 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001926 if (hsize > len || !sg)
1927 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001928
Herbert Xuc8884ed2006-10-29 15:59:41 -08001929 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001930 if (unlikely(!nskb))
1931 goto err;
1932
1933 if (segs)
1934 tail->next = nskb;
1935 else
1936 segs = nskb;
1937 tail = nskb;
1938
1939 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001940 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001941 nskb->priority = skb->priority;
1942 nskb->protocol = skb->protocol;
1943 nskb->dst = dst_clone(skb->dst);
1944 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1945 nskb->pkt_type = skb->pkt_type;
1946 nskb->mac_len = skb->mac_len;
1947
1948 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001949 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001950 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001951 nskb->transport_header = (nskb->network_header +
1952 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001953 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1954 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001955 if (!sg) {
1956 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1957 skb_put(nskb, len),
1958 len, 0);
1959 continue;
1960 }
1961
1962 frag = skb_shinfo(nskb)->frags;
1963 k = 0;
1964
Patrick McHardy84fa7932006-08-29 16:44:56 -07001965 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001966 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001967 skb_copy_from_linear_data_offset(skb, offset,
1968 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001969
1970 while (pos < offset + len) {
1971 BUG_ON(i >= nfrags);
1972
1973 *frag = skb_shinfo(skb)->frags[i];
1974 get_page(frag->page);
1975 size = frag->size;
1976
1977 if (pos < offset) {
1978 frag->page_offset += offset - pos;
1979 frag->size -= offset - pos;
1980 }
1981
1982 k++;
1983
1984 if (pos + size <= offset + len) {
1985 i++;
1986 pos += size;
1987 } else {
1988 frag->size -= pos + size - (offset + len);
1989 break;
1990 }
1991
1992 frag++;
1993 }
1994
1995 skb_shinfo(nskb)->nr_frags = k;
1996 nskb->data_len = len - hsize;
1997 nskb->len += nskb->data_len;
1998 nskb->truesize += nskb->data_len;
1999 } while ((offset += len) < skb->len);
2000
2001 return segs;
2002
2003err:
2004 while ((skb = segs)) {
2005 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002006 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002007 }
2008 return ERR_PTR(err);
2009}
2010
2011EXPORT_SYMBOL_GPL(skb_segment);
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013void __init skb_init(void)
2014{
2015 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2016 sizeof(struct sk_buff),
2017 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002018 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002019 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002020 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2021 (2*sizeof(struct sk_buff)) +
2022 sizeof(atomic_t),
2023 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002024 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002025 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
David Howells716ea3a2007-04-02 20:19:53 -07002028/**
2029 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2030 * @skb: Socket buffer containing the buffers to be mapped
2031 * @sg: The scatter-gather list to map into
2032 * @offset: The offset into the buffer's contents to start mapping
2033 * @len: Length of buffer space to be mapped
2034 *
2035 * Fill the specified scatter-gather list with mappings/pointers into a
2036 * region of the buffer space attached to a socket buffer.
2037 */
2038int
2039skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2040{
David S. Miller1a028e52007-04-27 15:21:23 -07002041 int start = skb_headlen(skb);
2042 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002043 int elt = 0;
2044
2045 if (copy > 0) {
2046 if (copy > len)
2047 copy = len;
2048 sg[elt].page = virt_to_page(skb->data + offset);
2049 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2050 sg[elt].length = copy;
2051 elt++;
2052 if ((len -= copy) == 0)
2053 return elt;
2054 offset += copy;
2055 }
2056
2057 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002058 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002059
David S. Miller1a028e52007-04-27 15:21:23 -07002060 BUG_TRAP(start <= offset + len);
2061
2062 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002063 if ((copy = end - offset) > 0) {
2064 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2065
2066 if (copy > len)
2067 copy = len;
2068 sg[elt].page = frag->page;
David S. Miller1a028e52007-04-27 15:21:23 -07002069 sg[elt].offset = frag->page_offset+offset-start;
David Howells716ea3a2007-04-02 20:19:53 -07002070 sg[elt].length = copy;
2071 elt++;
2072 if (!(len -= copy))
2073 return elt;
2074 offset += copy;
2075 }
David S. Miller1a028e52007-04-27 15:21:23 -07002076 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002077 }
2078
2079 if (skb_shinfo(skb)->frag_list) {
2080 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2081
2082 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002083 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002084
David S. Miller1a028e52007-04-27 15:21:23 -07002085 BUG_TRAP(start <= offset + len);
2086
2087 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002088 if ((copy = end - offset) > 0) {
2089 if (copy > len)
2090 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07002091 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002092 if ((len -= copy) == 0)
2093 return elt;
2094 offset += copy;
2095 }
David S. Miller1a028e52007-04-27 15:21:23 -07002096 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002097 }
2098 }
2099 BUG_ON(len);
2100 return elt;
2101}
2102
2103/**
2104 * skb_cow_data - Check that a socket buffer's data buffers are writable
2105 * @skb: The socket buffer to check.
2106 * @tailbits: Amount of trailing space to be added
2107 * @trailer: Returned pointer to the skb where the @tailbits space begins
2108 *
2109 * Make sure that the data buffers attached to a socket buffer are
2110 * writable. If they are not, private copies are made of the data buffers
2111 * and the socket buffer is set to use these instead.
2112 *
2113 * If @tailbits is given, make sure that there is space to write @tailbits
2114 * bytes of data beyond current end of socket buffer. @trailer will be
2115 * set to point to the skb in which this space begins.
2116 *
2117 * The number of scatterlist elements required to completely map the
2118 * COW'd and extended socket buffer will be returned.
2119 */
2120int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2121{
2122 int copyflag;
2123 int elt;
2124 struct sk_buff *skb1, **skb_p;
2125
2126 /* If skb is cloned or its head is paged, reallocate
2127 * head pulling out all the pages (pages are considered not writable
2128 * at the moment even if they are anonymous).
2129 */
2130 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2131 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2132 return -ENOMEM;
2133
2134 /* Easy case. Most of packets will go this way. */
2135 if (!skb_shinfo(skb)->frag_list) {
2136 /* A little of trouble, not enough of space for trailer.
2137 * This should not happen, when stack is tuned to generate
2138 * good frames. OK, on miss we reallocate and reserve even more
2139 * space, 128 bytes is fair. */
2140
2141 if (skb_tailroom(skb) < tailbits &&
2142 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2143 return -ENOMEM;
2144
2145 /* Voila! */
2146 *trailer = skb;
2147 return 1;
2148 }
2149
2150 /* Misery. We are in troubles, going to mincer fragments... */
2151
2152 elt = 1;
2153 skb_p = &skb_shinfo(skb)->frag_list;
2154 copyflag = 0;
2155
2156 while ((skb1 = *skb_p) != NULL) {
2157 int ntail = 0;
2158
2159 /* The fragment is partially pulled by someone,
2160 * this can happen on input. Copy it and everything
2161 * after it. */
2162
2163 if (skb_shared(skb1))
2164 copyflag = 1;
2165
2166 /* If the skb is the last, worry about trailer. */
2167
2168 if (skb1->next == NULL && tailbits) {
2169 if (skb_shinfo(skb1)->nr_frags ||
2170 skb_shinfo(skb1)->frag_list ||
2171 skb_tailroom(skb1) < tailbits)
2172 ntail = tailbits + 128;
2173 }
2174
2175 if (copyflag ||
2176 skb_cloned(skb1) ||
2177 ntail ||
2178 skb_shinfo(skb1)->nr_frags ||
2179 skb_shinfo(skb1)->frag_list) {
2180 struct sk_buff *skb2;
2181
2182 /* Fuck, we are miserable poor guys... */
2183 if (ntail == 0)
2184 skb2 = skb_copy(skb1, GFP_ATOMIC);
2185 else
2186 skb2 = skb_copy_expand(skb1,
2187 skb_headroom(skb1),
2188 ntail,
2189 GFP_ATOMIC);
2190 if (unlikely(skb2 == NULL))
2191 return -ENOMEM;
2192
2193 if (skb1->sk)
2194 skb_set_owner_w(skb2, skb1->sk);
2195
2196 /* Looking around. Are we still alive?
2197 * OK, link new skb, drop old one */
2198
2199 skb2->next = skb1->next;
2200 *skb_p = skb2;
2201 kfree_skb(skb1);
2202 skb1 = skb2;
2203 }
2204 elt++;
2205 *trailer = skb1;
2206 skb_p = &skb1->next;
2207 }
2208
2209 return elt;
2210}
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212EXPORT_SYMBOL(___pskb_trim);
2213EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002214EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002216EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002217EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218EXPORT_SYMBOL(pskb_copy);
2219EXPORT_SYMBOL(pskb_expand_head);
2220EXPORT_SYMBOL(skb_checksum);
2221EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222EXPORT_SYMBOL(skb_copy);
2223EXPORT_SYMBOL(skb_copy_and_csum_bits);
2224EXPORT_SYMBOL(skb_copy_and_csum_dev);
2225EXPORT_SYMBOL(skb_copy_bits);
2226EXPORT_SYMBOL(skb_copy_expand);
2227EXPORT_SYMBOL(skb_over_panic);
2228EXPORT_SYMBOL(skb_pad);
2229EXPORT_SYMBOL(skb_realloc_headroom);
2230EXPORT_SYMBOL(skb_under_panic);
2231EXPORT_SYMBOL(skb_dequeue);
2232EXPORT_SYMBOL(skb_dequeue_tail);
2233EXPORT_SYMBOL(skb_insert);
2234EXPORT_SYMBOL(skb_queue_purge);
2235EXPORT_SYMBOL(skb_queue_head);
2236EXPORT_SYMBOL(skb_queue_tail);
2237EXPORT_SYMBOL(skb_unlink);
2238EXPORT_SYMBOL(skb_append);
2239EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002240EXPORT_SYMBOL(skb_prepare_seq_read);
2241EXPORT_SYMBOL(skb_seq_read);
2242EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002243EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002244EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002245
2246EXPORT_SYMBOL_GPL(skb_to_sgvec);
2247EXPORT_SYMBOL_GPL(skb_cow_data);