blob: 5b4ce9b4dd2060c9fd5920bfad967493b7e7eea6 [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 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800278static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
David S. Millerd179cd12005-08-17 14:57:30 -0700280 struct sk_buff *other;
281 atomic_t *fclone_ref;
282
David S. Millerd179cd12005-08-17 14:57:30 -0700283 switch (skb->fclone) {
284 case SKB_FCLONE_UNAVAILABLE:
285 kmem_cache_free(skbuff_head_cache, skb);
286 break;
287
288 case SKB_FCLONE_ORIG:
289 fclone_ref = (atomic_t *) (skb + 2);
290 if (atomic_dec_and_test(fclone_ref))
291 kmem_cache_free(skbuff_fclone_cache, skb);
292 break;
293
294 case SKB_FCLONE_CLONE:
295 fclone_ref = (atomic_t *) (skb + 1);
296 other = skb - 1;
297
298 /* The clone portion is available for
299 * fast-cloning again.
300 */
301 skb->fclone = SKB_FCLONE_UNAVAILABLE;
302
303 if (atomic_dec_and_test(fclone_ref))
304 kmem_cache_free(skbuff_fclone_cache, other);
305 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Herbert Xu2d4baff2007-11-26 23:11:19 +0800309/* Free everything but the sk_buff shell. */
310static void skb_release_all(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 dst_release(skb->dst);
313#ifdef CONFIG_XFRM
314 secpath_put(skb->sp);
315#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700316 if (skb->destructor) {
317 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 skb->destructor(skb);
319 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800320#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700321 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800322 nf_conntrack_put_reasm(skb->nfct_reasm);
323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifdef CONFIG_BRIDGE_NETFILTER
325 nf_bridge_put(skb->nf_bridge);
326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* XXX: IS this still necessary? - JHS */
328#ifdef CONFIG_NET_SCHED
329 skb->tc_index = 0;
330#ifdef CONFIG_NET_CLS_ACT
331 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#endif
333#endif
Herbert Xu2d4baff2007-11-26 23:11:19 +0800334 skb_release_data(skb);
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Herbert Xu2d4baff2007-11-26 23:11:19 +0800337/**
338 * __kfree_skb - private function
339 * @skb: buffer
340 *
341 * Free an sk_buff. Release anything attached to the buffer.
342 * Clean the state. This is an internal helper function. Users should
343 * always call kfree_skb
344 */
345
346void __kfree_skb(struct sk_buff *skb)
347{
348 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree_skbmem(skb);
350}
351
352/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800353 * kfree_skb - free an sk_buff
354 * @skb: buffer to free
355 *
356 * Drop a reference to the buffer and free it if the usage count has
357 * hit zero.
358 */
359void kfree_skb(struct sk_buff *skb)
360{
361 if (unlikely(!skb))
362 return;
363 if (likely(atomic_read(&skb->users) == 1))
364 smp_rmb();
365 else if (likely(!atomic_dec_and_test(&skb->users)))
366 return;
367 __kfree_skb(skb);
368}
369
Herbert Xudec18812007-10-14 00:37:30 -0700370static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
371{
372 new->tstamp = old->tstamp;
373 new->dev = old->dev;
374 new->transport_header = old->transport_header;
375 new->network_header = old->network_header;
376 new->mac_header = old->mac_header;
377 new->dst = dst_clone(old->dst);
378#ifdef CONFIG_INET
379 new->sp = secpath_get(old->sp);
380#endif
381 memcpy(new->cb, old->cb, sizeof(old->cb));
382 new->csum_start = old->csum_start;
383 new->csum_offset = old->csum_offset;
384 new->local_df = old->local_df;
385 new->pkt_type = old->pkt_type;
386 new->ip_summed = old->ip_summed;
387 skb_copy_queue_mapping(new, old);
388 new->priority = old->priority;
389#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
390 new->ipvs_property = old->ipvs_property;
391#endif
392 new->protocol = old->protocol;
393 new->mark = old->mark;
394 __nf_copy(new, old);
395#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
396 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
397 new->nf_trace = old->nf_trace;
398#endif
399#ifdef CONFIG_NET_SCHED
400 new->tc_index = old->tc_index;
401#ifdef CONFIG_NET_CLS_ACT
402 new->tc_verd = old->tc_verd;
403#endif
404#endif
405 skb_copy_secmark(new, old);
406}
407
Herbert Xue0053ec2007-10-14 00:37:52 -0700408static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#define C(x) n->x = skb->x
411
412 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700414 __copy_skb_header(n, skb);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 C(len);
417 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700418 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 n->cloned = 1;
Patrick McHardy334a8132007-06-25 04:35:20 -0700420 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 C(truesize);
424 atomic_set(&n->users, 1);
425 C(head);
426 C(data);
427 C(tail);
428 C(end);
429
430 atomic_inc(&(skb_shinfo(skb)->dataref));
431 skb->cloned = 1;
432
433 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700434#undef C
435}
436
437/**
438 * skb_morph - morph one skb into another
439 * @dst: the skb to receive the contents
440 * @src: the skb to supply the contents
441 *
442 * This is identical to skb_clone except that the target skb is
443 * supplied by the user.
444 *
445 * The target skb is returned upon exit.
446 */
447struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
448{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800449 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700450 return __skb_clone(dst, src);
451}
452EXPORT_SYMBOL_GPL(skb_morph);
453
454/**
455 * skb_clone - duplicate an sk_buff
456 * @skb: buffer to clone
457 * @gfp_mask: allocation priority
458 *
459 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
460 * copies share the same packet data but not structure. The new
461 * buffer has a reference count of 1. If the allocation fails the
462 * function returns %NULL otherwise the new buffer is returned.
463 *
464 * If this function is called from an interrupt gfp_mask() must be
465 * %GFP_ATOMIC.
466 */
467
468struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
469{
470 struct sk_buff *n;
471
472 n = skb + 1;
473 if (skb->fclone == SKB_FCLONE_ORIG &&
474 n->fclone == SKB_FCLONE_UNAVAILABLE) {
475 atomic_t *fclone_ref = (atomic_t *) (n + 1);
476 n->fclone = SKB_FCLONE_CLONE;
477 atomic_inc(fclone_ref);
478 } else {
479 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
480 if (!n)
481 return NULL;
482 n->fclone = SKB_FCLONE_UNAVAILABLE;
483 }
484
485 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
489{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700490#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
492 * Shift between the two data areas in bytes
493 */
494 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700495#endif
Herbert Xudec18812007-10-14 00:37:30 -0700496
497 __copy_skb_header(new, old);
498
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700499#ifndef NET_SKBUFF_DATA_USES_OFFSET
500 /* {transport,network,mac}_header are relative to skb->head */
501 new->transport_header += offset;
502 new->network_header += offset;
503 new->mac_header += offset;
504#endif
Herbert Xu79671682006-06-22 02:40:14 -0700505 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
506 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
507 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/**
511 * skb_copy - create private copy of an sk_buff
512 * @skb: buffer to copy
513 * @gfp_mask: allocation priority
514 *
515 * Make a copy of both an &sk_buff and its data. This is used when the
516 * caller wishes to modify the data and needs a private copy of the
517 * data to alter. Returns %NULL on failure or the pointer to the buffer
518 * on success. The returned buffer has a reference count of 1.
519 *
520 * As by-product this function converts non-linear &sk_buff to linear
521 * one, so that &sk_buff becomes completely private and caller is allowed
522 * to modify all the data of returned buffer. This means that this
523 * function is not recommended for use in circumstances when only
524 * header is going to be modified. Use pskb_copy() instead.
525 */
526
Al Virodd0fc662005-10-07 07:46:04 +0100527struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 int headerlen = skb->data - skb->head;
530 /*
531 * Allocate the copy buffer
532 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700533 struct sk_buff *n;
534#ifdef NET_SKBUFF_DATA_USES_OFFSET
535 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
536#else
537 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!n)
540 return NULL;
541
542 /* Set the data pointer */
543 skb_reserve(n, headerlen);
544 /* Set the tail pointer and length */
545 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
548 BUG();
549
550 copy_skb_header(n, skb);
551 return n;
552}
553
554
555/**
556 * pskb_copy - create copy of an sk_buff with private head.
557 * @skb: buffer to copy
558 * @gfp_mask: allocation priority
559 *
560 * Make a copy of both an &sk_buff and part of its data, located
561 * in header. Fragmented data remain shared. This is used when
562 * the caller wishes to modify only header of &sk_buff and needs
563 * private copy of the header to alter. Returns %NULL on failure
564 * or the pointer to the buffer on success.
565 * The returned buffer has a reference count of 1.
566 */
567
Al Virodd0fc662005-10-07 07:46:04 +0100568struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 /*
571 * Allocate the copy buffer
572 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700573 struct sk_buff *n;
574#ifdef NET_SKBUFF_DATA_USES_OFFSET
575 n = alloc_skb(skb->end, gfp_mask);
576#else
577 n = alloc_skb(skb->end - skb->head, gfp_mask);
578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!n)
580 goto out;
581
582 /* Set the data pointer */
583 skb_reserve(n, skb->data - skb->head);
584 /* Set the tail pointer and length */
585 skb_put(n, skb_headlen(skb));
586 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300587 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Herbert Xu25f484a2006-11-07 14:57:15 -0800589 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 n->data_len = skb->data_len;
591 n->len = skb->len;
592
593 if (skb_shinfo(skb)->nr_frags) {
594 int i;
595
596 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
597 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
598 get_page(skb_shinfo(n)->frags[i].page);
599 }
600 skb_shinfo(n)->nr_frags = i;
601 }
602
603 if (skb_shinfo(skb)->frag_list) {
604 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
605 skb_clone_fraglist(n);
606 }
607
608 copy_skb_header(n, skb);
609out:
610 return n;
611}
612
613/**
614 * pskb_expand_head - reallocate header of &sk_buff
615 * @skb: buffer to reallocate
616 * @nhead: room to add at head
617 * @ntail: room to add at tail
618 * @gfp_mask: allocation priority
619 *
620 * Expands (or creates identical copy, if &nhead and &ntail are zero)
621 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
622 * reference count of 1. Returns zero in the case of success or error,
623 * if expansion failed. In the last case, &sk_buff is not changed.
624 *
625 * All the pointers pointing into skb header may change and must be
626 * reloaded after call to this function.
627 */
628
Victor Fusco86a76ca2005-07-08 14:57:47 -0700629int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100630 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 int i;
633 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700634#ifdef NET_SKBUFF_DATA_USES_OFFSET
635 int size = nhead + skb->end + ntail;
636#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 long off;
640
641 if (skb_shared(skb))
642 BUG();
643
644 size = SKB_DATA_ALIGN(size);
645
646 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
647 if (!data)
648 goto nodata;
649
650 /* Copy only real data... and, alas, header. This should be
651 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700652#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700653 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700654#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700655 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700656#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700657 memcpy(data + size, skb_end_pointer(skb),
658 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
661 get_page(skb_shinfo(skb)->frags[i].page);
662
663 if (skb_shinfo(skb)->frag_list)
664 skb_clone_fraglist(skb);
665
666 skb_release_data(skb);
667
668 off = (data + nhead) - skb->head;
669
670 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700672#ifdef NET_SKBUFF_DATA_USES_OFFSET
673 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700674 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700675#else
676 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700677#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700678 /* {transport,network,mac}_header and tail are relative to skb->head */
679 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700680 skb->transport_header += off;
681 skb->network_header += off;
682 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700683 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700685 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 skb->nohdr = 0;
687 atomic_set(&skb_shinfo(skb)->dataref, 1);
688 return 0;
689
690nodata:
691 return -ENOMEM;
692}
693
694/* Make private copy of skb with writable head and some headroom */
695
696struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
697{
698 struct sk_buff *skb2;
699 int delta = headroom - skb_headroom(skb);
700
701 if (delta <= 0)
702 skb2 = pskb_copy(skb, GFP_ATOMIC);
703 else {
704 skb2 = skb_clone(skb, GFP_ATOMIC);
705 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
706 GFP_ATOMIC)) {
707 kfree_skb(skb2);
708 skb2 = NULL;
709 }
710 }
711 return skb2;
712}
713
714
715/**
716 * skb_copy_expand - copy and expand sk_buff
717 * @skb: buffer to copy
718 * @newheadroom: new free bytes at head
719 * @newtailroom: new free bytes at tail
720 * @gfp_mask: allocation priority
721 *
722 * Make a copy of both an &sk_buff and its data and while doing so
723 * allocate additional space.
724 *
725 * This is used when the caller wishes to modify the data and needs a
726 * private copy of the data to alter as well as more space for new fields.
727 * Returns %NULL on failure or the pointer to the buffer
728 * on success. The returned buffer has a reference count of 1.
729 *
730 * You must pass %GFP_ATOMIC as the allocation priority if this function
731 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700734 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100735 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 /*
738 * Allocate the copy buffer
739 */
740 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
741 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700742 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700744 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (!n)
747 return NULL;
748
749 skb_reserve(n, newheadroom);
750
751 /* Set the tail pointer and length */
752 skb_put(n, skb->len);
753
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700754 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 head_copy_off = 0;
756 if (newheadroom <= head_copy_len)
757 head_copy_len = newheadroom;
758 else
759 head_copy_off = newheadroom - head_copy_len;
760
761 /* Copy the linear header and data. */
762 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
763 skb->len + head_copy_len))
764 BUG();
765
766 copy_skb_header(n, skb);
767
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700768 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700769 n->csum_start += off;
770#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700771 n->transport_header += off;
772 n->network_header += off;
773 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700774#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return n;
777}
778
779/**
780 * skb_pad - zero pad the tail of an skb
781 * @skb: buffer to pad
782 * @pad: space to pad
783 *
784 * Ensure that a buffer is followed by a padding area that is zero
785 * filled. Used by network drivers which may DMA or transfer data
786 * beyond the buffer end onto the wire.
787 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700788 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900790
Herbert Xu5b057c62006-06-23 02:06:41 -0700791int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Herbert Xu5b057c62006-06-23 02:06:41 -0700793 int err;
794 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700797 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700801
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700802 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700803 if (likely(skb_cloned(skb) || ntail > 0)) {
804 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
805 if (unlikely(err))
806 goto free_skb;
807 }
808
809 /* FIXME: The use of this function with non-linear skb's really needs
810 * to be audited.
811 */
812 err = skb_linearize(skb);
813 if (unlikely(err))
814 goto free_skb;
815
816 memset(skb->data + skb->len, 0, pad);
817 return 0;
818
819free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700821 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900822}
823
Herbert Xu3cc0e872006-06-09 16:13:38 -0700824/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826
Herbert Xu3cc0e872006-06-09 16:13:38 -0700827int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Herbert Xu27b437c2006-07-13 19:26:39 -0700829 struct sk_buff **fragp;
830 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int offset = skb_headlen(skb);
832 int nfrags = skb_shinfo(skb)->nr_frags;
833 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700834 int err;
835
836 if (skb_cloned(skb) &&
837 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
838 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700840 i = 0;
841 if (offset >= len)
842 goto drop_pages;
843
844 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700846
847 if (end < len) {
848 offset = end;
849 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700851
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700852 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700853
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700854drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700855 skb_shinfo(skb)->nr_frags = i;
856
857 for (; i < nfrags; i++)
858 put_page(skb_shinfo(skb)->frags[i].page);
859
860 if (skb_shinfo(skb)->frag_list)
861 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700862 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Herbert Xu27b437c2006-07-13 19:26:39 -0700865 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
866 fragp = &frag->next) {
867 int end = offset + frag->len;
868
869 if (skb_shared(frag)) {
870 struct sk_buff *nfrag;
871
872 nfrag = skb_clone(frag, GFP_ATOMIC);
873 if (unlikely(!nfrag))
874 return -ENOMEM;
875
876 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700877 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700878 frag = nfrag;
879 *fragp = frag;
880 }
881
882 if (end < len) {
883 offset = end;
884 continue;
885 }
886
887 if (end > len &&
888 unlikely((err = pskb_trim(frag, len - offset))))
889 return err;
890
891 if (frag->next)
892 skb_drop_list(&frag->next);
893 break;
894 }
895
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700896done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700897 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 skb->data_len -= skb->len - len;
899 skb->len = len;
900 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700901 skb->len = len;
902 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700903 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 return 0;
907}
908
909/**
910 * __pskb_pull_tail - advance tail of skb header
911 * @skb: buffer to reallocate
912 * @delta: number of bytes to advance tail
913 *
914 * The function makes a sense only on a fragmented &sk_buff,
915 * it expands header moving its tail forward and copying necessary
916 * data from fragmented part.
917 *
918 * &sk_buff MUST have reference count of 1.
919 *
920 * Returns %NULL (and &sk_buff does not change) if pull failed
921 * or value of new tail of skb in the case of success.
922 *
923 * All the pointers pointing into skb header may change and must be
924 * reloaded after call to this function.
925 */
926
927/* Moves tail of skb head forward, copying data from fragmented part,
928 * when it is necessary.
929 * 1. It may fail due to malloc failure.
930 * 2. It may change skb pointers.
931 *
932 * It is pretty complicated. Luckily, it is called only in exceptional cases.
933 */
934unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
935{
936 /* If skb has not enough free space at tail, get new one
937 * plus 128 bytes for future expansions. If we have enough
938 * room at tail, reallocate without expansion only if skb is cloned.
939 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700940 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (eat > 0 || skb_cloned(skb)) {
943 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
944 GFP_ATOMIC))
945 return NULL;
946 }
947
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700948 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 BUG();
950
951 /* Optimization: no fragments, no reasons to preestimate
952 * size of pulled pages. Superb.
953 */
954 if (!skb_shinfo(skb)->frag_list)
955 goto pull_pages;
956
957 /* Estimate size of pulled pages. */
958 eat = delta;
959 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
960 if (skb_shinfo(skb)->frags[i].size >= eat)
961 goto pull_pages;
962 eat -= skb_shinfo(skb)->frags[i].size;
963 }
964
965 /* If we need update frag list, we are in troubles.
966 * Certainly, it possible to add an offset to skb data,
967 * but taking into account that pulling is expected to
968 * be very rare operation, it is worth to fight against
969 * further bloating skb head and crucify ourselves here instead.
970 * Pure masohism, indeed. 8)8)
971 */
972 if (eat) {
973 struct sk_buff *list = skb_shinfo(skb)->frag_list;
974 struct sk_buff *clone = NULL;
975 struct sk_buff *insp = NULL;
976
977 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800978 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (list->len <= eat) {
981 /* Eaten as whole. */
982 eat -= list->len;
983 list = list->next;
984 insp = list;
985 } else {
986 /* Eaten partially. */
987
988 if (skb_shared(list)) {
989 /* Sucks! We need to fork list. :-( */
990 clone = skb_clone(list, GFP_ATOMIC);
991 if (!clone)
992 return NULL;
993 insp = list->next;
994 list = clone;
995 } else {
996 /* This may be pulled without
997 * problems. */
998 insp = list;
999 }
1000 if (!pskb_pull(list, eat)) {
1001 if (clone)
1002 kfree_skb(clone);
1003 return NULL;
1004 }
1005 break;
1006 }
1007 } while (eat);
1008
1009 /* Free pulled out fragments. */
1010 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1011 skb_shinfo(skb)->frag_list = list->next;
1012 kfree_skb(list);
1013 }
1014 /* And insert new clone at head. */
1015 if (clone) {
1016 clone->next = list;
1017 skb_shinfo(skb)->frag_list = clone;
1018 }
1019 }
1020 /* Success! Now we may commit changes to skb data. */
1021
1022pull_pages:
1023 eat = delta;
1024 k = 0;
1025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1026 if (skb_shinfo(skb)->frags[i].size <= eat) {
1027 put_page(skb_shinfo(skb)->frags[i].page);
1028 eat -= skb_shinfo(skb)->frags[i].size;
1029 } else {
1030 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1031 if (eat) {
1032 skb_shinfo(skb)->frags[k].page_offset += eat;
1033 skb_shinfo(skb)->frags[k].size -= eat;
1034 eat = 0;
1035 }
1036 k++;
1037 }
1038 }
1039 skb_shinfo(skb)->nr_frags = k;
1040
1041 skb->tail += delta;
1042 skb->data_len -= delta;
1043
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001044 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/* Copy some data bits from skb to kernel buffer. */
1048
1049int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1050{
1051 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001052 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (offset > (int)skb->len - len)
1055 goto fault;
1056
1057 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001058 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (copy > len)
1060 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001061 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if ((len -= copy) == 0)
1063 return 0;
1064 offset += copy;
1065 to += copy;
1066 }
1067
1068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001069 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
David S. Miller1a028e52007-04-27 15:21:23 -07001071 BUG_TRAP(start <= offset + len);
1072
1073 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if ((copy = end - offset) > 0) {
1075 u8 *vaddr;
1076
1077 if (copy > len)
1078 copy = len;
1079
1080 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1081 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001082 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1083 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 kunmap_skb_frag(vaddr);
1085
1086 if ((len -= copy) == 0)
1087 return 0;
1088 offset += copy;
1089 to += copy;
1090 }
David S. Miller1a028e52007-04-27 15:21:23 -07001091 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
1094 if (skb_shinfo(skb)->frag_list) {
1095 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1096
1097 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001098 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
David S. Miller1a028e52007-04-27 15:21:23 -07001100 BUG_TRAP(start <= offset + len);
1101
1102 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if ((copy = end - offset) > 0) {
1104 if (copy > len)
1105 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001106 if (skb_copy_bits(list, offset - start,
1107 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto fault;
1109 if ((len -= copy) == 0)
1110 return 0;
1111 offset += copy;
1112 to += copy;
1113 }
David S. Miller1a028e52007-04-27 15:21:23 -07001114 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 }
1117 if (!len)
1118 return 0;
1119
1120fault:
1121 return -EFAULT;
1122}
1123
Herbert Xu357b40a2005-04-19 22:30:14 -07001124/**
1125 * skb_store_bits - store bits from kernel buffer to skb
1126 * @skb: destination buffer
1127 * @offset: offset in destination
1128 * @from: source buffer
1129 * @len: number of bytes to copy
1130 *
1131 * Copy the specified number of bytes from the source buffer to the
1132 * destination skb. This function handles all the messy bits of
1133 * traversing fragment lists and such.
1134 */
1135
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001136int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001137{
1138 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001139 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001140
1141 if (offset > (int)skb->len - len)
1142 goto fault;
1143
David S. Miller1a028e52007-04-27 15:21:23 -07001144 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001145 if (copy > len)
1146 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001147 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001148 if ((len -= copy) == 0)
1149 return 0;
1150 offset += copy;
1151 from += copy;
1152 }
1153
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001156 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001157
David S. Miller1a028e52007-04-27 15:21:23 -07001158 BUG_TRAP(start <= offset + len);
1159
1160 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001161 if ((copy = end - offset) > 0) {
1162 u8 *vaddr;
1163
1164 if (copy > len)
1165 copy = len;
1166
1167 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001168 memcpy(vaddr + frag->page_offset + offset - start,
1169 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001170 kunmap_skb_frag(vaddr);
1171
1172 if ((len -= copy) == 0)
1173 return 0;
1174 offset += copy;
1175 from += copy;
1176 }
David S. Miller1a028e52007-04-27 15:21:23 -07001177 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001178 }
1179
1180 if (skb_shinfo(skb)->frag_list) {
1181 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1182
1183 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001184 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001185
David S. Miller1a028e52007-04-27 15:21:23 -07001186 BUG_TRAP(start <= offset + len);
1187
1188 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001189 if ((copy = end - offset) > 0) {
1190 if (copy > len)
1191 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001192 if (skb_store_bits(list, offset - start,
1193 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001194 goto fault;
1195 if ((len -= copy) == 0)
1196 return 0;
1197 offset += copy;
1198 from += copy;
1199 }
David S. Miller1a028e52007-04-27 15:21:23 -07001200 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001201 }
1202 }
1203 if (!len)
1204 return 0;
1205
1206fault:
1207 return -EFAULT;
1208}
1209
1210EXPORT_SYMBOL(skb_store_bits);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/* Checksum skb data. */
1213
Al Viro2bbbc862006-11-14 21:37:14 -08001214__wsum skb_checksum(const struct sk_buff *skb, int offset,
1215 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
David S. Miller1a028e52007-04-27 15:21:23 -07001217 int start = skb_headlen(skb);
1218 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int pos = 0;
1220
1221 /* Checksum header. */
1222 if (copy > 0) {
1223 if (copy > len)
1224 copy = len;
1225 csum = csum_partial(skb->data + offset, copy, csum);
1226 if ((len -= copy) == 0)
1227 return csum;
1228 offset += copy;
1229 pos = copy;
1230 }
1231
1232 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001233 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
David S. Miller1a028e52007-04-27 15:21:23 -07001235 BUG_TRAP(start <= offset + len);
1236
1237 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001239 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 u8 *vaddr;
1241 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1242
1243 if (copy > len)
1244 copy = len;
1245 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001246 csum2 = csum_partial(vaddr + frag->page_offset +
1247 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 kunmap_skb_frag(vaddr);
1249 csum = csum_block_add(csum, csum2, pos);
1250 if (!(len -= copy))
1251 return csum;
1252 offset += copy;
1253 pos += copy;
1254 }
David S. Miller1a028e52007-04-27 15:21:23 -07001255 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 if (skb_shinfo(skb)->frag_list) {
1259 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1260
1261 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001262 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
David S. Miller1a028e52007-04-27 15:21:23 -07001264 BUG_TRAP(start <= offset + len);
1265
1266 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001268 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (copy > len)
1270 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001271 csum2 = skb_checksum(list, offset - start,
1272 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 csum = csum_block_add(csum, csum2, pos);
1274 if ((len -= copy) == 0)
1275 return csum;
1276 offset += copy;
1277 pos += copy;
1278 }
David S. Miller1a028e52007-04-27 15:21:23 -07001279 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001282 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 return csum;
1285}
1286
1287/* Both of above in one bottle. */
1288
Al Viro81d77662006-11-14 21:37:33 -08001289__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1290 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
David S. Miller1a028e52007-04-27 15:21:23 -07001292 int start = skb_headlen(skb);
1293 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 int pos = 0;
1295
1296 /* Copy header. */
1297 if (copy > 0) {
1298 if (copy > len)
1299 copy = len;
1300 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1301 copy, csum);
1302 if ((len -= copy) == 0)
1303 return csum;
1304 offset += copy;
1305 to += copy;
1306 pos = copy;
1307 }
1308
1309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001310 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
David S. Miller1a028e52007-04-27 15:21:23 -07001312 BUG_TRAP(start <= offset + len);
1313
1314 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001316 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 u8 *vaddr;
1318 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1319
1320 if (copy > len)
1321 copy = len;
1322 vaddr = kmap_skb_frag(frag);
1323 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001324 frag->page_offset +
1325 offset - start, to,
1326 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 kunmap_skb_frag(vaddr);
1328 csum = csum_block_add(csum, csum2, pos);
1329 if (!(len -= copy))
1330 return csum;
1331 offset += copy;
1332 to += copy;
1333 pos += copy;
1334 }
David S. Miller1a028e52007-04-27 15:21:23 -07001335 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 if (skb_shinfo(skb)->frag_list) {
1339 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1340
1341 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001342 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001343 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
David S. Miller1a028e52007-04-27 15:21:23 -07001345 BUG_TRAP(start <= offset + len);
1346
1347 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if ((copy = end - offset) > 0) {
1349 if (copy > len)
1350 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001351 csum2 = skb_copy_and_csum_bits(list,
1352 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 to, copy, 0);
1354 csum = csum_block_add(csum, csum2, pos);
1355 if ((len -= copy) == 0)
1356 return csum;
1357 offset += copy;
1358 to += copy;
1359 pos += copy;
1360 }
David S. Miller1a028e52007-04-27 15:21:23 -07001361 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001364 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return csum;
1366}
1367
1368void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1369{
Al Virod3bc23e2006-11-14 21:24:49 -08001370 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 long csstart;
1372
Patrick McHardy84fa7932006-08-29 16:44:56 -07001373 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001374 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 else
1376 csstart = skb_headlen(skb);
1377
Kris Katterjohn09a62662006-01-08 22:24:28 -08001378 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001380 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 csum = 0;
1383 if (csstart != skb->len)
1384 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1385 skb->len - csstart, 0);
1386
Patrick McHardy84fa7932006-08-29 16:44:56 -07001387 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001388 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Al Virod3bc23e2006-11-14 21:24:49 -08001390 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392}
1393
1394/**
1395 * skb_dequeue - remove from the head of the queue
1396 * @list: list to dequeue from
1397 *
1398 * Remove the head of the list. The list lock is taken so the function
1399 * may be used safely with other locking list functions. The head item is
1400 * returned or %NULL if the list is empty.
1401 */
1402
1403struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1404{
1405 unsigned long flags;
1406 struct sk_buff *result;
1407
1408 spin_lock_irqsave(&list->lock, flags);
1409 result = __skb_dequeue(list);
1410 spin_unlock_irqrestore(&list->lock, flags);
1411 return result;
1412}
1413
1414/**
1415 * skb_dequeue_tail - remove from the tail of the queue
1416 * @list: list to dequeue from
1417 *
1418 * Remove the tail of the list. The list lock is taken so the function
1419 * may be used safely with other locking list functions. The tail item is
1420 * returned or %NULL if the list is empty.
1421 */
1422struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1423{
1424 unsigned long flags;
1425 struct sk_buff *result;
1426
1427 spin_lock_irqsave(&list->lock, flags);
1428 result = __skb_dequeue_tail(list);
1429 spin_unlock_irqrestore(&list->lock, flags);
1430 return result;
1431}
1432
1433/**
1434 * skb_queue_purge - empty a list
1435 * @list: list to empty
1436 *
1437 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1438 * the list and one reference dropped. This function takes the list
1439 * lock and is atomic with respect to other list locking functions.
1440 */
1441void skb_queue_purge(struct sk_buff_head *list)
1442{
1443 struct sk_buff *skb;
1444 while ((skb = skb_dequeue(list)) != NULL)
1445 kfree_skb(skb);
1446}
1447
1448/**
1449 * skb_queue_head - queue a buffer at the list head
1450 * @list: list to use
1451 * @newsk: buffer to queue
1452 *
1453 * Queue a buffer at the start of the list. This function takes the
1454 * list lock and can be used safely with other locking &sk_buff functions
1455 * safely.
1456 *
1457 * A buffer cannot be placed on two lists at the same time.
1458 */
1459void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1460{
1461 unsigned long flags;
1462
1463 spin_lock_irqsave(&list->lock, flags);
1464 __skb_queue_head(list, newsk);
1465 spin_unlock_irqrestore(&list->lock, flags);
1466}
1467
1468/**
1469 * skb_queue_tail - queue a buffer at the list tail
1470 * @list: list to use
1471 * @newsk: buffer to queue
1472 *
1473 * Queue a buffer at the tail of the list. This function takes the
1474 * list lock and can be used safely with other locking &sk_buff functions
1475 * safely.
1476 *
1477 * A buffer cannot be placed on two lists at the same time.
1478 */
1479void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1480{
1481 unsigned long flags;
1482
1483 spin_lock_irqsave(&list->lock, flags);
1484 __skb_queue_tail(list, newsk);
1485 spin_unlock_irqrestore(&list->lock, flags);
1486}
David S. Miller8728b832005-08-09 19:25:21 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488/**
1489 * skb_unlink - remove a buffer from a list
1490 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001491 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
David S. Miller8728b832005-08-09 19:25:21 -07001493 * Remove a packet from a list. The list locks are taken and this
1494 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
David S. Miller8728b832005-08-09 19:25:21 -07001496 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 */
David S. Miller8728b832005-08-09 19:25:21 -07001498void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
David S. Miller8728b832005-08-09 19:25:21 -07001500 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David S. Miller8728b832005-08-09 19:25:21 -07001502 spin_lock_irqsave(&list->lock, flags);
1503 __skb_unlink(skb, list);
1504 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/**
1508 * skb_append - append a buffer
1509 * @old: buffer to insert after
1510 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001511 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 *
1513 * Place a packet after a given packet in a list. The list locks are taken
1514 * and this function is atomic with respect to other list locked calls.
1515 * A buffer cannot be placed on two lists at the same time.
1516 */
David S. Miller8728b832005-08-09 19:25:21 -07001517void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 unsigned long flags;
1520
David S. Miller8728b832005-08-09 19:25:21 -07001521 spin_lock_irqsave(&list->lock, flags);
1522 __skb_append(old, newsk, list);
1523 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526
1527/**
1528 * skb_insert - insert a buffer
1529 * @old: buffer to insert before
1530 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001531 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 *
David S. Miller8728b832005-08-09 19:25:21 -07001533 * Place a packet before a given packet in a list. The list locks are
1534 * taken and this function is atomic with respect to other list locked
1535 * calls.
1536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * A buffer cannot be placed on two lists at the same time.
1538 */
David S. Miller8728b832005-08-09 19:25:21 -07001539void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 unsigned long flags;
1542
David S. Miller8728b832005-08-09 19:25:21 -07001543 spin_lock_irqsave(&list->lock, flags);
1544 __skb_insert(newsk, old->prev, old, list);
1545 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548static inline void skb_split_inside_header(struct sk_buff *skb,
1549 struct sk_buff* skb1,
1550 const u32 len, const int pos)
1551{
1552 int i;
1553
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001554 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1555 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* And move data appendix as is. */
1557 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1558 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1559
1560 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1561 skb_shinfo(skb)->nr_frags = 0;
1562 skb1->data_len = skb->data_len;
1563 skb1->len += skb1->data_len;
1564 skb->data_len = 0;
1565 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001566 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
1569static inline void skb_split_no_header(struct sk_buff *skb,
1570 struct sk_buff* skb1,
1571 const u32 len, int pos)
1572{
1573 int i, k = 0;
1574 const int nfrags = skb_shinfo(skb)->nr_frags;
1575
1576 skb_shinfo(skb)->nr_frags = 0;
1577 skb1->len = skb1->data_len = skb->len - len;
1578 skb->len = len;
1579 skb->data_len = len - pos;
1580
1581 for (i = 0; i < nfrags; i++) {
1582 int size = skb_shinfo(skb)->frags[i].size;
1583
1584 if (pos + size > len) {
1585 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1586
1587 if (pos < len) {
1588 /* Split frag.
1589 * We have two variants in this case:
1590 * 1. Move all the frag to the second
1591 * part, if it is possible. F.e.
1592 * this approach is mandatory for TUX,
1593 * where splitting is expensive.
1594 * 2. Split is accurately. We make this.
1595 */
1596 get_page(skb_shinfo(skb)->frags[i].page);
1597 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1598 skb_shinfo(skb1)->frags[0].size -= len - pos;
1599 skb_shinfo(skb)->frags[i].size = len - pos;
1600 skb_shinfo(skb)->nr_frags++;
1601 }
1602 k++;
1603 } else
1604 skb_shinfo(skb)->nr_frags++;
1605 pos += size;
1606 }
1607 skb_shinfo(skb1)->nr_frags = k;
1608}
1609
1610/**
1611 * skb_split - Split fragmented skb to two parts at length len.
1612 * @skb: the buffer to split
1613 * @skb1: the buffer to receive the second part
1614 * @len: new length for skb
1615 */
1616void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1617{
1618 int pos = skb_headlen(skb);
1619
1620 if (len < pos) /* Split line is inside header. */
1621 skb_split_inside_header(skb, skb1, len, pos);
1622 else /* Second chunk has no header, nothing to copy. */
1623 skb_split_no_header(skb, skb1, len, pos);
1624}
1625
Thomas Graf677e90e2005-06-23 20:59:51 -07001626/**
1627 * skb_prepare_seq_read - Prepare a sequential read of skb data
1628 * @skb: the buffer to read
1629 * @from: lower offset of data to be read
1630 * @to: upper offset of data to be read
1631 * @st: state variable
1632 *
1633 * Initializes the specified state variable. Must be called before
1634 * invoking skb_seq_read() for the first time.
1635 */
1636void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1637 unsigned int to, struct skb_seq_state *st)
1638{
1639 st->lower_offset = from;
1640 st->upper_offset = to;
1641 st->root_skb = st->cur_skb = skb;
1642 st->frag_idx = st->stepped_offset = 0;
1643 st->frag_data = NULL;
1644}
1645
1646/**
1647 * skb_seq_read - Sequentially read skb data
1648 * @consumed: number of bytes consumed by the caller so far
1649 * @data: destination pointer for data to be returned
1650 * @st: state variable
1651 *
1652 * Reads a block of skb data at &consumed relative to the
1653 * lower offset specified to skb_prepare_seq_read(). Assigns
1654 * the head of the data block to &data and returns the length
1655 * of the block or 0 if the end of the skb data or the upper
1656 * offset has been reached.
1657 *
1658 * The caller is not required to consume all of the data
1659 * returned, i.e. &consumed is typically set to the number
1660 * of bytes already consumed and the next call to
1661 * skb_seq_read() will return the remaining part of the block.
1662 *
1663 * Note: The size of each block of data returned can be arbitary,
1664 * this limitation is the cost for zerocopy seqeuental
1665 * reads of potentially non linear data.
1666 *
1667 * Note: Fragment lists within fragments are not implemented
1668 * at the moment, state->root_skb could be replaced with
1669 * a stack for this purpose.
1670 */
1671unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1672 struct skb_seq_state *st)
1673{
1674 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1675 skb_frag_t *frag;
1676
1677 if (unlikely(abs_offset >= st->upper_offset))
1678 return 0;
1679
1680next_skb:
1681 block_limit = skb_headlen(st->cur_skb);
1682
1683 if (abs_offset < block_limit) {
1684 *data = st->cur_skb->data + abs_offset;
1685 return block_limit - abs_offset;
1686 }
1687
1688 if (st->frag_idx == 0 && !st->frag_data)
1689 st->stepped_offset += skb_headlen(st->cur_skb);
1690
1691 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1692 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1693 block_limit = frag->size + st->stepped_offset;
1694
1695 if (abs_offset < block_limit) {
1696 if (!st->frag_data)
1697 st->frag_data = kmap_skb_frag(frag);
1698
1699 *data = (u8 *) st->frag_data + frag->page_offset +
1700 (abs_offset - st->stepped_offset);
1701
1702 return block_limit - abs_offset;
1703 }
1704
1705 if (st->frag_data) {
1706 kunmap_skb_frag(st->frag_data);
1707 st->frag_data = NULL;
1708 }
1709
1710 st->frag_idx++;
1711 st->stepped_offset += frag->size;
1712 }
1713
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001714 if (st->frag_data) {
1715 kunmap_skb_frag(st->frag_data);
1716 st->frag_data = NULL;
1717 }
1718
Thomas Graf677e90e2005-06-23 20:59:51 -07001719 if (st->cur_skb->next) {
1720 st->cur_skb = st->cur_skb->next;
1721 st->frag_idx = 0;
1722 goto next_skb;
1723 } else if (st->root_skb == st->cur_skb &&
1724 skb_shinfo(st->root_skb)->frag_list) {
1725 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1726 goto next_skb;
1727 }
1728
1729 return 0;
1730}
1731
1732/**
1733 * skb_abort_seq_read - Abort a sequential read of skb data
1734 * @st: state variable
1735 *
1736 * Must be called if skb_seq_read() was not called until it
1737 * returned 0.
1738 */
1739void skb_abort_seq_read(struct skb_seq_state *st)
1740{
1741 if (st->frag_data)
1742 kunmap_skb_frag(st->frag_data);
1743}
1744
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001745#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1746
1747static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1748 struct ts_config *conf,
1749 struct ts_state *state)
1750{
1751 return skb_seq_read(offset, text, TS_SKB_CB(state));
1752}
1753
1754static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1755{
1756 skb_abort_seq_read(TS_SKB_CB(state));
1757}
1758
1759/**
1760 * skb_find_text - Find a text pattern in skb data
1761 * @skb: the buffer to look in
1762 * @from: search offset
1763 * @to: search limit
1764 * @config: textsearch configuration
1765 * @state: uninitialized textsearch state variable
1766 *
1767 * Finds a pattern in the skb data according to the specified
1768 * textsearch configuration. Use textsearch_next() to retrieve
1769 * subsequent occurrences of the pattern. Returns the offset
1770 * to the first occurrence or UINT_MAX if no match was found.
1771 */
1772unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1773 unsigned int to, struct ts_config *config,
1774 struct ts_state *state)
1775{
Phil Oesterf72b9482006-06-26 00:00:57 -07001776 unsigned int ret;
1777
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001778 config->get_next_block = skb_ts_get_next_block;
1779 config->finish = skb_ts_finish;
1780
1781 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1782
Phil Oesterf72b9482006-06-26 00:00:57 -07001783 ret = textsearch_find(config, state);
1784 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001785}
1786
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001787/**
1788 * skb_append_datato_frags: - append the user data to a skb
1789 * @sk: sock structure
1790 * @skb: skb structure to be appened with user data.
1791 * @getfrag: call back function to be used for getting the user data
1792 * @from: pointer to user message iov
1793 * @length: length of the iov message
1794 *
1795 * Description: This procedure append the user data in the fragment part
1796 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1797 */
1798int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001799 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001800 int len, int odd, struct sk_buff *skb),
1801 void *from, int length)
1802{
1803 int frg_cnt = 0;
1804 skb_frag_t *frag = NULL;
1805 struct page *page = NULL;
1806 int copy, left;
1807 int offset = 0;
1808 int ret;
1809
1810 do {
1811 /* Return error if we don't have space for new frag */
1812 frg_cnt = skb_shinfo(skb)->nr_frags;
1813 if (frg_cnt >= MAX_SKB_FRAGS)
1814 return -EFAULT;
1815
1816 /* allocate a new page for next frag */
1817 page = alloc_pages(sk->sk_allocation, 0);
1818
1819 /* If alloc_page fails just return failure and caller will
1820 * free previous allocated pages by doing kfree_skb()
1821 */
1822 if (page == NULL)
1823 return -ENOMEM;
1824
1825 /* initialize the next frag */
1826 sk->sk_sndmsg_page = page;
1827 sk->sk_sndmsg_off = 0;
1828 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1829 skb->truesize += PAGE_SIZE;
1830 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1831
1832 /* get the new initialized frag */
1833 frg_cnt = skb_shinfo(skb)->nr_frags;
1834 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1835
1836 /* copy the user data to page */
1837 left = PAGE_SIZE - frag->page_offset;
1838 copy = (length > left)? left : length;
1839
1840 ret = getfrag(from, (page_address(frag->page) +
1841 frag->page_offset + frag->size),
1842 offset, copy, 0, skb);
1843 if (ret < 0)
1844 return -EFAULT;
1845
1846 /* copy was successful so update the size parameters */
1847 sk->sk_sndmsg_off += copy;
1848 frag->size += copy;
1849 skb->len += copy;
1850 skb->data_len += copy;
1851 offset += copy;
1852 length -= copy;
1853
1854 } while (length > 0);
1855
1856 return 0;
1857}
1858
Herbert Xucbb042f2006-03-20 22:43:56 -08001859/**
1860 * skb_pull_rcsum - pull skb and update receive checksum
1861 * @skb: buffer to update
1862 * @start: start of data before pull
1863 * @len: length of data pulled
1864 *
1865 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001866 * update the CHECKSUM_COMPLETE checksum. It should be used on
1867 * receive path processing instead of skb_pull unless you know
1868 * that the checksum difference is zero (e.g., a valid IP header)
1869 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001870 */
1871unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1872{
1873 BUG_ON(len > skb->len);
1874 skb->len -= len;
1875 BUG_ON(skb->len < skb->data_len);
1876 skb_postpull_rcsum(skb, skb->data, len);
1877 return skb->data += len;
1878}
1879
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001880EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1881
Herbert Xuf4c50d92006-06-22 03:02:40 -07001882/**
1883 * skb_segment - Perform protocol segmentation on skb.
1884 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001885 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001886 *
1887 * This function performs segmentation on the given skb. It returns
1888 * the segment at the given position. It returns NULL if there are
1889 * no more segments to generate, or when an error is encountered.
1890 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001891struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001892{
1893 struct sk_buff *segs = NULL;
1894 struct sk_buff *tail = NULL;
1895 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001896 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001897 unsigned int offset = doffset;
1898 unsigned int headroom;
1899 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001900 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001901 int nfrags = skb_shinfo(skb)->nr_frags;
1902 int err = -ENOMEM;
1903 int i = 0;
1904 int pos;
1905
1906 __skb_push(skb, doffset);
1907 headroom = skb_headroom(skb);
1908 pos = skb_headlen(skb);
1909
1910 do {
1911 struct sk_buff *nskb;
1912 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001913 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001914 int k;
1915 int size;
1916
1917 len = skb->len - offset;
1918 if (len > mss)
1919 len = mss;
1920
1921 hsize = skb_headlen(skb) - offset;
1922 if (hsize < 0)
1923 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001924 if (hsize > len || !sg)
1925 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001926
Herbert Xuc8884ed2006-10-29 15:59:41 -08001927 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001928 if (unlikely(!nskb))
1929 goto err;
1930
1931 if (segs)
1932 tail->next = nskb;
1933 else
1934 segs = nskb;
1935 tail = nskb;
1936
1937 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001938 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001939 nskb->priority = skb->priority;
1940 nskb->protocol = skb->protocol;
1941 nskb->dst = dst_clone(skb->dst);
1942 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1943 nskb->pkt_type = skb->pkt_type;
1944 nskb->mac_len = skb->mac_len;
1945
1946 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001947 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001948 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001949 nskb->transport_header = (nskb->network_header +
1950 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001951 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1952 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001953 if (!sg) {
1954 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1955 skb_put(nskb, len),
1956 len, 0);
1957 continue;
1958 }
1959
1960 frag = skb_shinfo(nskb)->frags;
1961 k = 0;
1962
Patrick McHardy84fa7932006-08-29 16:44:56 -07001963 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001964 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001965 skb_copy_from_linear_data_offset(skb, offset,
1966 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001967
1968 while (pos < offset + len) {
1969 BUG_ON(i >= nfrags);
1970
1971 *frag = skb_shinfo(skb)->frags[i];
1972 get_page(frag->page);
1973 size = frag->size;
1974
1975 if (pos < offset) {
1976 frag->page_offset += offset - pos;
1977 frag->size -= offset - pos;
1978 }
1979
1980 k++;
1981
1982 if (pos + size <= offset + len) {
1983 i++;
1984 pos += size;
1985 } else {
1986 frag->size -= pos + size - (offset + len);
1987 break;
1988 }
1989
1990 frag++;
1991 }
1992
1993 skb_shinfo(nskb)->nr_frags = k;
1994 nskb->data_len = len - hsize;
1995 nskb->len += nskb->data_len;
1996 nskb->truesize += nskb->data_len;
1997 } while ((offset += len) < skb->len);
1998
1999 return segs;
2000
2001err:
2002 while ((skb = segs)) {
2003 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002004 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002005 }
2006 return ERR_PTR(err);
2007}
2008
2009EXPORT_SYMBOL_GPL(skb_segment);
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011void __init skb_init(void)
2012{
2013 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2014 sizeof(struct sk_buff),
2015 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002016 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002017 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002018 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2019 (2*sizeof(struct sk_buff)) +
2020 sizeof(atomic_t),
2021 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002022 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002023 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
David Howells716ea3a2007-04-02 20:19:53 -07002026/**
2027 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2028 * @skb: Socket buffer containing the buffers to be mapped
2029 * @sg: The scatter-gather list to map into
2030 * @offset: The offset into the buffer's contents to start mapping
2031 * @len: Length of buffer space to be mapped
2032 *
2033 * Fill the specified scatter-gather list with mappings/pointers into a
2034 * region of the buffer space attached to a socket buffer.
2035 */
David S. Miller51c739d2007-10-30 21:29:29 -07002036static int
2037__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002038{
David S. Miller1a028e52007-04-27 15:21:23 -07002039 int start = skb_headlen(skb);
2040 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002041 int elt = 0;
2042
2043 if (copy > 0) {
2044 if (copy > len)
2045 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002046 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002047 elt++;
2048 if ((len -= copy) == 0)
2049 return elt;
2050 offset += copy;
2051 }
2052
2053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002054 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002055
David S. Miller1a028e52007-04-27 15:21:23 -07002056 BUG_TRAP(start <= offset + len);
2057
2058 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002059 if ((copy = end - offset) > 0) {
2060 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2061
2062 if (copy > len)
2063 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002064 sg_set_page(&sg[elt], frag->page, copy,
2065 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002066 elt++;
2067 if (!(len -= copy))
2068 return elt;
2069 offset += copy;
2070 }
David S. Miller1a028e52007-04-27 15:21:23 -07002071 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002072 }
2073
2074 if (skb_shinfo(skb)->frag_list) {
2075 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2076
2077 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002078 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002079
David S. Miller1a028e52007-04-27 15:21:23 -07002080 BUG_TRAP(start <= offset + len);
2081
2082 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002083 if ((copy = end - offset) > 0) {
2084 if (copy > len)
2085 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002086 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2087 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002088 if ((len -= copy) == 0)
2089 return elt;
2090 offset += copy;
2091 }
David S. Miller1a028e52007-04-27 15:21:23 -07002092 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002093 }
2094 }
2095 BUG_ON(len);
2096 return elt;
2097}
2098
David S. Miller51c739d2007-10-30 21:29:29 -07002099int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2100{
2101 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2102
Jens Axboec46f2332007-10-31 12:06:37 +01002103 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002104
2105 return nsg;
2106}
2107
David Howells716ea3a2007-04-02 20:19:53 -07002108/**
2109 * skb_cow_data - Check that a socket buffer's data buffers are writable
2110 * @skb: The socket buffer to check.
2111 * @tailbits: Amount of trailing space to be added
2112 * @trailer: Returned pointer to the skb where the @tailbits space begins
2113 *
2114 * Make sure that the data buffers attached to a socket buffer are
2115 * writable. If they are not, private copies are made of the data buffers
2116 * and the socket buffer is set to use these instead.
2117 *
2118 * If @tailbits is given, make sure that there is space to write @tailbits
2119 * bytes of data beyond current end of socket buffer. @trailer will be
2120 * set to point to the skb in which this space begins.
2121 *
2122 * The number of scatterlist elements required to completely map the
2123 * COW'd and extended socket buffer will be returned.
2124 */
2125int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2126{
2127 int copyflag;
2128 int elt;
2129 struct sk_buff *skb1, **skb_p;
2130
2131 /* If skb is cloned or its head is paged, reallocate
2132 * head pulling out all the pages (pages are considered not writable
2133 * at the moment even if they are anonymous).
2134 */
2135 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2136 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2137 return -ENOMEM;
2138
2139 /* Easy case. Most of packets will go this way. */
2140 if (!skb_shinfo(skb)->frag_list) {
2141 /* A little of trouble, not enough of space for trailer.
2142 * This should not happen, when stack is tuned to generate
2143 * good frames. OK, on miss we reallocate and reserve even more
2144 * space, 128 bytes is fair. */
2145
2146 if (skb_tailroom(skb) < tailbits &&
2147 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2148 return -ENOMEM;
2149
2150 /* Voila! */
2151 *trailer = skb;
2152 return 1;
2153 }
2154
2155 /* Misery. We are in troubles, going to mincer fragments... */
2156
2157 elt = 1;
2158 skb_p = &skb_shinfo(skb)->frag_list;
2159 copyflag = 0;
2160
2161 while ((skb1 = *skb_p) != NULL) {
2162 int ntail = 0;
2163
2164 /* The fragment is partially pulled by someone,
2165 * this can happen on input. Copy it and everything
2166 * after it. */
2167
2168 if (skb_shared(skb1))
2169 copyflag = 1;
2170
2171 /* If the skb is the last, worry about trailer. */
2172
2173 if (skb1->next == NULL && tailbits) {
2174 if (skb_shinfo(skb1)->nr_frags ||
2175 skb_shinfo(skb1)->frag_list ||
2176 skb_tailroom(skb1) < tailbits)
2177 ntail = tailbits + 128;
2178 }
2179
2180 if (copyflag ||
2181 skb_cloned(skb1) ||
2182 ntail ||
2183 skb_shinfo(skb1)->nr_frags ||
2184 skb_shinfo(skb1)->frag_list) {
2185 struct sk_buff *skb2;
2186
2187 /* Fuck, we are miserable poor guys... */
2188 if (ntail == 0)
2189 skb2 = skb_copy(skb1, GFP_ATOMIC);
2190 else
2191 skb2 = skb_copy_expand(skb1,
2192 skb_headroom(skb1),
2193 ntail,
2194 GFP_ATOMIC);
2195 if (unlikely(skb2 == NULL))
2196 return -ENOMEM;
2197
2198 if (skb1->sk)
2199 skb_set_owner_w(skb2, skb1->sk);
2200
2201 /* Looking around. Are we still alive?
2202 * OK, link new skb, drop old one */
2203
2204 skb2->next = skb1->next;
2205 *skb_p = skb2;
2206 kfree_skb(skb1);
2207 skb1 = skb2;
2208 }
2209 elt++;
2210 *trailer = skb1;
2211 skb_p = &skb1->next;
2212 }
2213
2214 return elt;
2215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217EXPORT_SYMBOL(___pskb_trim);
2218EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002219EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002221EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002222EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223EXPORT_SYMBOL(pskb_copy);
2224EXPORT_SYMBOL(pskb_expand_head);
2225EXPORT_SYMBOL(skb_checksum);
2226EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227EXPORT_SYMBOL(skb_copy);
2228EXPORT_SYMBOL(skb_copy_and_csum_bits);
2229EXPORT_SYMBOL(skb_copy_and_csum_dev);
2230EXPORT_SYMBOL(skb_copy_bits);
2231EXPORT_SYMBOL(skb_copy_expand);
2232EXPORT_SYMBOL(skb_over_panic);
2233EXPORT_SYMBOL(skb_pad);
2234EXPORT_SYMBOL(skb_realloc_headroom);
2235EXPORT_SYMBOL(skb_under_panic);
2236EXPORT_SYMBOL(skb_dequeue);
2237EXPORT_SYMBOL(skb_dequeue_tail);
2238EXPORT_SYMBOL(skb_insert);
2239EXPORT_SYMBOL(skb_queue_purge);
2240EXPORT_SYMBOL(skb_queue_head);
2241EXPORT_SYMBOL(skb_queue_tail);
2242EXPORT_SYMBOL(skb_unlink);
2243EXPORT_SYMBOL(skb_append);
2244EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002245EXPORT_SYMBOL(skb_prepare_seq_read);
2246EXPORT_SYMBOL(skb_seq_read);
2247EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002248EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002249EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002250
2251EXPORT_SYMBOL_GPL(skb_to_sgvec);
2252EXPORT_SYMBOL_GPL(skb_cow_data);