blob: c989c3a0f9078cabf364d1076c3a398de5e1418a [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
365/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * skb_clone - duplicate an sk_buff
367 * @skb: buffer to clone
368 * @gfp_mask: allocation priority
369 *
370 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
371 * copies share the same packet data but not structure. The new
372 * buffer has a reference count of 1. If the allocation fails the
373 * function returns %NULL otherwise the new buffer is returned.
374 *
375 * If this function is called from an interrupt gfp_mask() must be
376 * %GFP_ATOMIC.
377 */
378
Al Virodd0fc662005-10-07 07:46:04 +0100379struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
David S. Millerd179cd12005-08-17 14:57:30 -0700381 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
David S. Millerd179cd12005-08-17 14:57:30 -0700383 n = skb + 1;
384 if (skb->fclone == SKB_FCLONE_ORIG &&
385 n->fclone == SKB_FCLONE_UNAVAILABLE) {
386 atomic_t *fclone_ref = (atomic_t *) (n + 1);
387 n->fclone = SKB_FCLONE_CLONE;
388 atomic_inc(fclone_ref);
389 } else {
390 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
391 if (!n)
392 return NULL;
393 n->fclone = SKB_FCLONE_UNAVAILABLE;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396#define C(x) n->x = skb->x
397
398 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700400 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 C(dev);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700402 C(transport_header);
403 C(network_header);
404 C(mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 C(dst);
406 dst_clone(skb->dst);
407 C(sp);
408#ifdef CONFIG_INET
409 secpath_get(skb->sp);
410#endif
411 memcpy(n->cb, skb->cb, sizeof(skb->cb));
412 C(len);
413 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700414 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 C(csum);
416 C(local_df);
417 n->cloned = 1;
Patrick McHardy334a8132007-06-25 04:35:20 -0700418 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 n->nohdr = 0;
420 C(pkt_type);
421 C(ip_summed);
422 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800423#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
424 C(ipvs_property);
425#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800428 C(mark);
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700429 __nf_copy(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#ifdef CONFIG_NET_SCHED
431 C(tc_index);
432#ifdef CONFIG_NET_CLS_ACT
433 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700434 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
435 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700436 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
Patrick McHardydbbeb2f2007-06-23 22:58:34 -0700439 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 C(truesize);
441 atomic_set(&n->users, 1);
442 C(head);
443 C(data);
444 C(tail);
445 C(end);
446
447 atomic_inc(&(skb_shinfo(skb)->dataref));
448 skb->cloned = 1;
449
450 return n;
451}
452
453static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
454{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700455#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Shift between the two data areas in bytes
458 */
459 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700460#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 new->sk = NULL;
462 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 new->priority = old->priority;
464 new->protocol = old->protocol;
465 new->dst = dst_clone(old->dst);
466#ifdef CONFIG_INET
467 new->sp = secpath_get(old->sp);
468#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700469 new->transport_header = old->transport_header;
470 new->network_header = old->network_header;
471 new->mac_header = old->mac_header;
472#ifndef NET_SKBUFF_DATA_USES_OFFSET
473 /* {transport,network,mac}_header are relative to skb->head */
474 new->transport_header += offset;
475 new->network_header += offset;
476 new->mac_header += offset;
477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 memcpy(new->cb, old->cb, sizeof(old->cb));
479 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700480 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700482 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800484 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700485 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300486#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
487 new->ipvs_property = old->ipvs_property;
488#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489#ifdef CONFIG_NET_SCHED
490#ifdef CONFIG_NET_CLS_ACT
491 new->tc_verd = old->tc_verd;
492#endif
493 new->tc_index = old->tc_index;
494#endif
James Morris984bc162006-06-09 00:29:17 -0700495 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700497 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
498 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
499 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/**
503 * skb_copy - create private copy of an sk_buff
504 * @skb: buffer to copy
505 * @gfp_mask: allocation priority
506 *
507 * Make a copy of both an &sk_buff and its data. This is used when the
508 * caller wishes to modify the data and needs a private copy of the
509 * data to alter. Returns %NULL on failure or the pointer to the buffer
510 * on success. The returned buffer has a reference count of 1.
511 *
512 * As by-product this function converts non-linear &sk_buff to linear
513 * one, so that &sk_buff becomes completely private and caller is allowed
514 * to modify all the data of returned buffer. This means that this
515 * function is not recommended for use in circumstances when only
516 * header is going to be modified. Use pskb_copy() instead.
517 */
518
Al Virodd0fc662005-10-07 07:46:04 +0100519struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int headerlen = skb->data - skb->head;
522 /*
523 * Allocate the copy buffer
524 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700525 struct sk_buff *n;
526#ifdef NET_SKBUFF_DATA_USES_OFFSET
527 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
528#else
529 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!n)
532 return NULL;
533
534 /* Set the data pointer */
535 skb_reserve(n, headerlen);
536 /* Set the tail pointer and length */
537 skb_put(n, skb->len);
538 n->csum = skb->csum;
539 n->ip_summed = skb->ip_summed;
540
541 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
542 BUG();
543
544 copy_skb_header(n, skb);
545 return n;
546}
547
548
549/**
550 * pskb_copy - create copy of an sk_buff with private head.
551 * @skb: buffer to copy
552 * @gfp_mask: allocation priority
553 *
554 * Make a copy of both an &sk_buff and part of its data, located
555 * in header. Fragmented data remain shared. This is used when
556 * the caller wishes to modify only header of &sk_buff and needs
557 * private copy of the header to alter. Returns %NULL on failure
558 * or the pointer to the buffer on success.
559 * The returned buffer has a reference count of 1.
560 */
561
Al Virodd0fc662005-10-07 07:46:04 +0100562struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 /*
565 * Allocate the copy buffer
566 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700567 struct sk_buff *n;
568#ifdef NET_SKBUFF_DATA_USES_OFFSET
569 n = alloc_skb(skb->end, gfp_mask);
570#else
571 n = alloc_skb(skb->end - skb->head, gfp_mask);
572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!n)
574 goto out;
575
576 /* Set the data pointer */
577 skb_reserve(n, skb->data - skb->head);
578 /* Set the tail pointer and length */
579 skb_put(n, skb_headlen(skb));
580 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300581 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 n->csum = skb->csum;
583 n->ip_summed = skb->ip_summed;
584
Herbert Xu25f484a2006-11-07 14:57:15 -0800585 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 n->data_len = skb->data_len;
587 n->len = skb->len;
588
589 if (skb_shinfo(skb)->nr_frags) {
590 int i;
591
592 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
593 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
594 get_page(skb_shinfo(n)->frags[i].page);
595 }
596 skb_shinfo(n)->nr_frags = i;
597 }
598
599 if (skb_shinfo(skb)->frag_list) {
600 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
601 skb_clone_fraglist(n);
602 }
603
604 copy_skb_header(n, skb);
605out:
606 return n;
607}
608
609/**
610 * pskb_expand_head - reallocate header of &sk_buff
611 * @skb: buffer to reallocate
612 * @nhead: room to add at head
613 * @ntail: room to add at tail
614 * @gfp_mask: allocation priority
615 *
616 * Expands (or creates identical copy, if &nhead and &ntail are zero)
617 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
618 * reference count of 1. Returns zero in the case of success or error,
619 * if expansion failed. In the last case, &sk_buff is not changed.
620 *
621 * All the pointers pointing into skb header may change and must be
622 * reloaded after call to this function.
623 */
624
Victor Fusco86a76ca2005-07-08 14:57:47 -0700625int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100626 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 int i;
629 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700630#ifdef NET_SKBUFF_DATA_USES_OFFSET
631 int size = nhead + skb->end + ntail;
632#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700634#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 long off;
636
637 if (skb_shared(skb))
638 BUG();
639
640 size = SKB_DATA_ALIGN(size);
641
642 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
643 if (!data)
644 goto nodata;
645
646 /* Copy only real data... and, alas, header. This should be
647 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700648#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700649 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700650#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700651 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700652#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700653 memcpy(data + size, skb_end_pointer(skb),
654 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
657 get_page(skb_shinfo(skb)->frags[i].page);
658
659 if (skb_shinfo(skb)->frag_list)
660 skb_clone_fraglist(skb);
661
662 skb_release_data(skb);
663
664 off = (data + nhead) - skb->head;
665
666 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700668#ifdef NET_SKBUFF_DATA_USES_OFFSET
669 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700670 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700671#else
672 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700673#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700674 /* {transport,network,mac}_header and tail are relative to skb->head */
675 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700676 skb->transport_header += off;
677 skb->network_header += off;
678 skb->mac_header += off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700680 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 skb->nohdr = 0;
682 atomic_set(&skb_shinfo(skb)->dataref, 1);
683 return 0;
684
685nodata:
686 return -ENOMEM;
687}
688
689/* Make private copy of skb with writable head and some headroom */
690
691struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
692{
693 struct sk_buff *skb2;
694 int delta = headroom - skb_headroom(skb);
695
696 if (delta <= 0)
697 skb2 = pskb_copy(skb, GFP_ATOMIC);
698 else {
699 skb2 = skb_clone(skb, GFP_ATOMIC);
700 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
701 GFP_ATOMIC)) {
702 kfree_skb(skb2);
703 skb2 = NULL;
704 }
705 }
706 return skb2;
707}
708
709
710/**
711 * skb_copy_expand - copy and expand sk_buff
712 * @skb: buffer to copy
713 * @newheadroom: new free bytes at head
714 * @newtailroom: new free bytes at tail
715 * @gfp_mask: allocation priority
716 *
717 * Make a copy of both an &sk_buff and its data and while doing so
718 * allocate additional space.
719 *
720 * This is used when the caller wishes to modify the data and needs a
721 * private copy of the data to alter as well as more space for new fields.
722 * Returns %NULL on failure or the pointer to the buffer
723 * on success. The returned buffer has a reference count of 1.
724 *
725 * You must pass %GFP_ATOMIC as the allocation priority if this function
726 * is called from an interrupt.
727 *
728 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
729 * only by netfilter in the cases when checksum is recalculated? --ANK
730 */
731struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700732 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100733 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 /*
736 * Allocate the copy buffer
737 */
738 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
739 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700740 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int head_copy_len, head_copy_off;
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700742 int off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (!n)
745 return NULL;
746
747 skb_reserve(n, newheadroom);
748
749 /* Set the tail pointer and length */
750 skb_put(n, skb->len);
751
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700752 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 head_copy_off = 0;
754 if (newheadroom <= head_copy_len)
755 head_copy_len = newheadroom;
756 else
757 head_copy_off = newheadroom - head_copy_len;
758
759 /* Copy the linear header and data. */
760 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
761 skb->len + head_copy_len))
762 BUG();
763
764 copy_skb_header(n, skb);
765
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700766#ifdef NET_SKBUFF_DATA_USES_OFFSET
767 off = newheadroom - oldheadroom;
768#endif
769 n->transport_header += off;
770 n->network_header += off;
771 n->mac_header += off;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return n;
774}
775
776/**
777 * skb_pad - zero pad the tail of an skb
778 * @skb: buffer to pad
779 * @pad: space to pad
780 *
781 * Ensure that a buffer is followed by a padding area that is zero
782 * filled. Used by network drivers which may DMA or transfer data
783 * beyond the buffer end onto the wire.
784 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700785 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900787
Herbert Xu5b057c62006-06-23 02:06:41 -0700788int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Herbert Xu5b057c62006-06-23 02:06:41 -0700790 int err;
791 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700794 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700798
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700799 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700800 if (likely(skb_cloned(skb) || ntail > 0)) {
801 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
802 if (unlikely(err))
803 goto free_skb;
804 }
805
806 /* FIXME: The use of this function with non-linear skb's really needs
807 * to be audited.
808 */
809 err = skb_linearize(skb);
810 if (unlikely(err))
811 goto free_skb;
812
813 memset(skb->data + skb->len, 0, pad);
814 return 0;
815
816free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700818 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900819}
820
Herbert Xu3cc0e872006-06-09 16:13:38 -0700821/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823
Herbert Xu3cc0e872006-06-09 16:13:38 -0700824int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Herbert Xu27b437c2006-07-13 19:26:39 -0700826 struct sk_buff **fragp;
827 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int offset = skb_headlen(skb);
829 int nfrags = skb_shinfo(skb)->nr_frags;
830 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700831 int err;
832
833 if (skb_cloned(skb) &&
834 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
835 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700837 i = 0;
838 if (offset >= len)
839 goto drop_pages;
840
841 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700843
844 if (end < len) {
845 offset = end;
846 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700848
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700849 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700850
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700851drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700852 skb_shinfo(skb)->nr_frags = i;
853
854 for (; i < nfrags; i++)
855 put_page(skb_shinfo(skb)->frags[i].page);
856
857 if (skb_shinfo(skb)->frag_list)
858 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700859 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
Herbert Xu27b437c2006-07-13 19:26:39 -0700862 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
863 fragp = &frag->next) {
864 int end = offset + frag->len;
865
866 if (skb_shared(frag)) {
867 struct sk_buff *nfrag;
868
869 nfrag = skb_clone(frag, GFP_ATOMIC);
870 if (unlikely(!nfrag))
871 return -ENOMEM;
872
873 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700874 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700875 frag = nfrag;
876 *fragp = frag;
877 }
878
879 if (end < len) {
880 offset = end;
881 continue;
882 }
883
884 if (end > len &&
885 unlikely((err = pskb_trim(frag, len - offset))))
886 return err;
887
888 if (frag->next)
889 skb_drop_list(&frag->next);
890 break;
891 }
892
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700893done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700894 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 skb->data_len -= skb->len - len;
896 skb->len = len;
897 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700898 skb->len = len;
899 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700900 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902
903 return 0;
904}
905
906/**
907 * __pskb_pull_tail - advance tail of skb header
908 * @skb: buffer to reallocate
909 * @delta: number of bytes to advance tail
910 *
911 * The function makes a sense only on a fragmented &sk_buff,
912 * it expands header moving its tail forward and copying necessary
913 * data from fragmented part.
914 *
915 * &sk_buff MUST have reference count of 1.
916 *
917 * Returns %NULL (and &sk_buff does not change) if pull failed
918 * or value of new tail of skb in the case of success.
919 *
920 * All the pointers pointing into skb header may change and must be
921 * reloaded after call to this function.
922 */
923
924/* Moves tail of skb head forward, copying data from fragmented part,
925 * when it is necessary.
926 * 1. It may fail due to malloc failure.
927 * 2. It may change skb pointers.
928 *
929 * It is pretty complicated. Luckily, it is called only in exceptional cases.
930 */
931unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
932{
933 /* If skb has not enough free space at tail, get new one
934 * plus 128 bytes for future expansions. If we have enough
935 * room at tail, reallocate without expansion only if skb is cloned.
936 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700937 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (eat > 0 || skb_cloned(skb)) {
940 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
941 GFP_ATOMIC))
942 return NULL;
943 }
944
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700945 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 BUG();
947
948 /* Optimization: no fragments, no reasons to preestimate
949 * size of pulled pages. Superb.
950 */
951 if (!skb_shinfo(skb)->frag_list)
952 goto pull_pages;
953
954 /* Estimate size of pulled pages. */
955 eat = delta;
956 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
957 if (skb_shinfo(skb)->frags[i].size >= eat)
958 goto pull_pages;
959 eat -= skb_shinfo(skb)->frags[i].size;
960 }
961
962 /* If we need update frag list, we are in troubles.
963 * Certainly, it possible to add an offset to skb data,
964 * but taking into account that pulling is expected to
965 * be very rare operation, it is worth to fight against
966 * further bloating skb head and crucify ourselves here instead.
967 * Pure masohism, indeed. 8)8)
968 */
969 if (eat) {
970 struct sk_buff *list = skb_shinfo(skb)->frag_list;
971 struct sk_buff *clone = NULL;
972 struct sk_buff *insp = NULL;
973
974 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800975 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (list->len <= eat) {
978 /* Eaten as whole. */
979 eat -= list->len;
980 list = list->next;
981 insp = list;
982 } else {
983 /* Eaten partially. */
984
985 if (skb_shared(list)) {
986 /* Sucks! We need to fork list. :-( */
987 clone = skb_clone(list, GFP_ATOMIC);
988 if (!clone)
989 return NULL;
990 insp = list->next;
991 list = clone;
992 } else {
993 /* This may be pulled without
994 * problems. */
995 insp = list;
996 }
997 if (!pskb_pull(list, eat)) {
998 if (clone)
999 kfree_skb(clone);
1000 return NULL;
1001 }
1002 break;
1003 }
1004 } while (eat);
1005
1006 /* Free pulled out fragments. */
1007 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1008 skb_shinfo(skb)->frag_list = list->next;
1009 kfree_skb(list);
1010 }
1011 /* And insert new clone at head. */
1012 if (clone) {
1013 clone->next = list;
1014 skb_shinfo(skb)->frag_list = clone;
1015 }
1016 }
1017 /* Success! Now we may commit changes to skb data. */
1018
1019pull_pages:
1020 eat = delta;
1021 k = 0;
1022 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1023 if (skb_shinfo(skb)->frags[i].size <= eat) {
1024 put_page(skb_shinfo(skb)->frags[i].page);
1025 eat -= skb_shinfo(skb)->frags[i].size;
1026 } else {
1027 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1028 if (eat) {
1029 skb_shinfo(skb)->frags[k].page_offset += eat;
1030 skb_shinfo(skb)->frags[k].size -= eat;
1031 eat = 0;
1032 }
1033 k++;
1034 }
1035 }
1036 skb_shinfo(skb)->nr_frags = k;
1037
1038 skb->tail += delta;
1039 skb->data_len -= delta;
1040
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001041 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/* Copy some data bits from skb to kernel buffer. */
1045
1046int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1047{
1048 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001049 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (offset > (int)skb->len - len)
1052 goto fault;
1053
1054 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001055 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (copy > len)
1057 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001058 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if ((len -= copy) == 0)
1060 return 0;
1061 offset += copy;
1062 to += copy;
1063 }
1064
1065 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001066 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
David S. Miller1a028e52007-04-27 15:21:23 -07001068 BUG_TRAP(start <= offset + len);
1069
1070 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if ((copy = end - offset) > 0) {
1072 u8 *vaddr;
1073
1074 if (copy > len)
1075 copy = len;
1076
1077 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1078 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001079 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1080 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kunmap_skb_frag(vaddr);
1082
1083 if ((len -= copy) == 0)
1084 return 0;
1085 offset += copy;
1086 to += copy;
1087 }
David S. Miller1a028e52007-04-27 15:21:23 -07001088 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 if (skb_shinfo(skb)->frag_list) {
1092 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1093
1094 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001095 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
David S. Miller1a028e52007-04-27 15:21:23 -07001097 BUG_TRAP(start <= offset + len);
1098
1099 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if ((copy = end - offset) > 0) {
1101 if (copy > len)
1102 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001103 if (skb_copy_bits(list, offset - start,
1104 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto fault;
1106 if ((len -= copy) == 0)
1107 return 0;
1108 offset += copy;
1109 to += copy;
1110 }
David S. Miller1a028e52007-04-27 15:21:23 -07001111 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 }
1114 if (!len)
1115 return 0;
1116
1117fault:
1118 return -EFAULT;
1119}
1120
Herbert Xu357b40a2005-04-19 22:30:14 -07001121/**
1122 * skb_store_bits - store bits from kernel buffer to skb
1123 * @skb: destination buffer
1124 * @offset: offset in destination
1125 * @from: source buffer
1126 * @len: number of bytes to copy
1127 *
1128 * Copy the specified number of bytes from the source buffer to the
1129 * destination skb. This function handles all the messy bits of
1130 * traversing fragment lists and such.
1131 */
1132
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001133int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001134{
1135 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001136 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001137
1138 if (offset > (int)skb->len - len)
1139 goto fault;
1140
David S. Miller1a028e52007-04-27 15:21:23 -07001141 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001142 if (copy > len)
1143 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001144 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001145 if ((len -= copy) == 0)
1146 return 0;
1147 offset += copy;
1148 from += copy;
1149 }
1150
1151 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1152 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001153 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001154
David S. Miller1a028e52007-04-27 15:21:23 -07001155 BUG_TRAP(start <= offset + len);
1156
1157 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001158 if ((copy = end - offset) > 0) {
1159 u8 *vaddr;
1160
1161 if (copy > len)
1162 copy = len;
1163
1164 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001165 memcpy(vaddr + frag->page_offset + offset - start,
1166 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001167 kunmap_skb_frag(vaddr);
1168
1169 if ((len -= copy) == 0)
1170 return 0;
1171 offset += copy;
1172 from += copy;
1173 }
David S. Miller1a028e52007-04-27 15:21:23 -07001174 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001175 }
1176
1177 if (skb_shinfo(skb)->frag_list) {
1178 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1179
1180 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001181 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001182
David S. Miller1a028e52007-04-27 15:21:23 -07001183 BUG_TRAP(start <= offset + len);
1184
1185 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001186 if ((copy = end - offset) > 0) {
1187 if (copy > len)
1188 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001189 if (skb_store_bits(list, offset - start,
1190 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001191 goto fault;
1192 if ((len -= copy) == 0)
1193 return 0;
1194 offset += copy;
1195 from += copy;
1196 }
David S. Miller1a028e52007-04-27 15:21:23 -07001197 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001198 }
1199 }
1200 if (!len)
1201 return 0;
1202
1203fault:
1204 return -EFAULT;
1205}
1206
1207EXPORT_SYMBOL(skb_store_bits);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/* Checksum skb data. */
1210
Al Viro2bbbc862006-11-14 21:37:14 -08001211__wsum skb_checksum(const struct sk_buff *skb, int offset,
1212 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
David S. Miller1a028e52007-04-27 15:21:23 -07001214 int start = skb_headlen(skb);
1215 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int pos = 0;
1217
1218 /* Checksum header. */
1219 if (copy > 0) {
1220 if (copy > len)
1221 copy = len;
1222 csum = csum_partial(skb->data + offset, copy, csum);
1223 if ((len -= copy) == 0)
1224 return csum;
1225 offset += copy;
1226 pos = copy;
1227 }
1228
1229 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001230 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
David S. Miller1a028e52007-04-27 15:21:23 -07001232 BUG_TRAP(start <= offset + len);
1233
1234 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001236 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 u8 *vaddr;
1238 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1239
1240 if (copy > len)
1241 copy = len;
1242 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001243 csum2 = csum_partial(vaddr + frag->page_offset +
1244 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 kunmap_skb_frag(vaddr);
1246 csum = csum_block_add(csum, csum2, pos);
1247 if (!(len -= copy))
1248 return csum;
1249 offset += copy;
1250 pos += copy;
1251 }
David S. Miller1a028e52007-04-27 15:21:23 -07001252 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
1255 if (skb_shinfo(skb)->frag_list) {
1256 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1257
1258 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001259 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David S. Miller1a028e52007-04-27 15:21:23 -07001261 BUG_TRAP(start <= offset + len);
1262
1263 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001265 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (copy > len)
1267 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001268 csum2 = skb_checksum(list, offset - start,
1269 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 csum = csum_block_add(csum, csum2, pos);
1271 if ((len -= copy) == 0)
1272 return csum;
1273 offset += copy;
1274 pos += copy;
1275 }
David S. Miller1a028e52007-04-27 15:21:23 -07001276 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001279 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 return csum;
1282}
1283
1284/* Both of above in one bottle. */
1285
Al Viro81d77662006-11-14 21:37:33 -08001286__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1287 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
David S. Miller1a028e52007-04-27 15:21:23 -07001289 int start = skb_headlen(skb);
1290 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int pos = 0;
1292
1293 /* Copy header. */
1294 if (copy > 0) {
1295 if (copy > len)
1296 copy = len;
1297 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1298 copy, csum);
1299 if ((len -= copy) == 0)
1300 return csum;
1301 offset += copy;
1302 to += copy;
1303 pos = copy;
1304 }
1305
1306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001307 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
David S. Miller1a028e52007-04-27 15:21:23 -07001309 BUG_TRAP(start <= offset + len);
1310
1311 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001313 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 u8 *vaddr;
1315 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1316
1317 if (copy > len)
1318 copy = len;
1319 vaddr = kmap_skb_frag(frag);
1320 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001321 frag->page_offset +
1322 offset - start, to,
1323 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 kunmap_skb_frag(vaddr);
1325 csum = csum_block_add(csum, csum2, pos);
1326 if (!(len -= copy))
1327 return csum;
1328 offset += copy;
1329 to += copy;
1330 pos += copy;
1331 }
David S. Miller1a028e52007-04-27 15:21:23 -07001332 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334
1335 if (skb_shinfo(skb)->frag_list) {
1336 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1337
1338 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001339 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001340 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
David S. Miller1a028e52007-04-27 15:21:23 -07001342 BUG_TRAP(start <= offset + len);
1343
1344 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if ((copy = end - offset) > 0) {
1346 if (copy > len)
1347 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001348 csum2 = skb_copy_and_csum_bits(list,
1349 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 to, copy, 0);
1351 csum = csum_block_add(csum, csum2, pos);
1352 if ((len -= copy) == 0)
1353 return csum;
1354 offset += copy;
1355 to += copy;
1356 pos += copy;
1357 }
David S. Miller1a028e52007-04-27 15:21:23 -07001358 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001361 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return csum;
1363}
1364
1365void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1366{
Al Virod3bc23e2006-11-14 21:24:49 -08001367 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 long csstart;
1369
Patrick McHardy84fa7932006-08-29 16:44:56 -07001370 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001371 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 else
1373 csstart = skb_headlen(skb);
1374
Kris Katterjohn09a62662006-01-08 22:24:28 -08001375 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001377 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 csum = 0;
1380 if (csstart != skb->len)
1381 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1382 skb->len - csstart, 0);
1383
Patrick McHardy84fa7932006-08-29 16:44:56 -07001384 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001385 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Al Virod3bc23e2006-11-14 21:24:49 -08001387 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389}
1390
1391/**
1392 * skb_dequeue - remove from the head of the queue
1393 * @list: list to dequeue from
1394 *
1395 * Remove the head of the list. The list lock is taken so the function
1396 * may be used safely with other locking list functions. The head item is
1397 * returned or %NULL if the list is empty.
1398 */
1399
1400struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1401{
1402 unsigned long flags;
1403 struct sk_buff *result;
1404
1405 spin_lock_irqsave(&list->lock, flags);
1406 result = __skb_dequeue(list);
1407 spin_unlock_irqrestore(&list->lock, flags);
1408 return result;
1409}
1410
1411/**
1412 * skb_dequeue_tail - remove from the tail of the queue
1413 * @list: list to dequeue from
1414 *
1415 * Remove the tail of the list. The list lock is taken so the function
1416 * may be used safely with other locking list functions. The tail item is
1417 * returned or %NULL if the list is empty.
1418 */
1419struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1420{
1421 unsigned long flags;
1422 struct sk_buff *result;
1423
1424 spin_lock_irqsave(&list->lock, flags);
1425 result = __skb_dequeue_tail(list);
1426 spin_unlock_irqrestore(&list->lock, flags);
1427 return result;
1428}
1429
1430/**
1431 * skb_queue_purge - empty a list
1432 * @list: list to empty
1433 *
1434 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1435 * the list and one reference dropped. This function takes the list
1436 * lock and is atomic with respect to other list locking functions.
1437 */
1438void skb_queue_purge(struct sk_buff_head *list)
1439{
1440 struct sk_buff *skb;
1441 while ((skb = skb_dequeue(list)) != NULL)
1442 kfree_skb(skb);
1443}
1444
1445/**
1446 * skb_queue_head - queue a buffer at the list head
1447 * @list: list to use
1448 * @newsk: buffer to queue
1449 *
1450 * Queue a buffer at the start of the list. This function takes the
1451 * list lock and can be used safely with other locking &sk_buff functions
1452 * safely.
1453 *
1454 * A buffer cannot be placed on two lists at the same time.
1455 */
1456void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1457{
1458 unsigned long flags;
1459
1460 spin_lock_irqsave(&list->lock, flags);
1461 __skb_queue_head(list, newsk);
1462 spin_unlock_irqrestore(&list->lock, flags);
1463}
1464
1465/**
1466 * skb_queue_tail - queue a buffer at the list tail
1467 * @list: list to use
1468 * @newsk: buffer to queue
1469 *
1470 * Queue a buffer at the tail of the list. This function takes the
1471 * list lock and can be used safely with other locking &sk_buff functions
1472 * safely.
1473 *
1474 * A buffer cannot be placed on two lists at the same time.
1475 */
1476void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1477{
1478 unsigned long flags;
1479
1480 spin_lock_irqsave(&list->lock, flags);
1481 __skb_queue_tail(list, newsk);
1482 spin_unlock_irqrestore(&list->lock, flags);
1483}
David S. Miller8728b832005-08-09 19:25:21 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/**
1486 * skb_unlink - remove a buffer from a list
1487 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001488 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
David S. Miller8728b832005-08-09 19:25:21 -07001490 * Remove a packet from a list. The list locks are taken and this
1491 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
David S. Miller8728b832005-08-09 19:25:21 -07001493 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
David S. Miller8728b832005-08-09 19:25:21 -07001495void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
David S. Miller8728b832005-08-09 19:25:21 -07001497 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
David S. Miller8728b832005-08-09 19:25:21 -07001499 spin_lock_irqsave(&list->lock, flags);
1500 __skb_unlink(skb, list);
1501 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
1505 * skb_append - append a buffer
1506 * @old: buffer to insert after
1507 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001508 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 *
1510 * Place a packet after a given packet in a list. The list locks are taken
1511 * and this function is atomic with respect to other list locked calls.
1512 * A buffer cannot be placed on two lists at the same time.
1513 */
David S. Miller8728b832005-08-09 19:25:21 -07001514void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 unsigned long flags;
1517
David S. Miller8728b832005-08-09 19:25:21 -07001518 spin_lock_irqsave(&list->lock, flags);
1519 __skb_append(old, newsk, list);
1520 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523
1524/**
1525 * skb_insert - insert a buffer
1526 * @old: buffer to insert before
1527 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001528 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 *
David S. Miller8728b832005-08-09 19:25:21 -07001530 * Place a packet before a given packet in a list. The list locks are
1531 * taken and this function is atomic with respect to other list locked
1532 * calls.
1533 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * A buffer cannot be placed on two lists at the same time.
1535 */
David S. Miller8728b832005-08-09 19:25:21 -07001536void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 unsigned long flags;
1539
David S. Miller8728b832005-08-09 19:25:21 -07001540 spin_lock_irqsave(&list->lock, flags);
1541 __skb_insert(newsk, old->prev, old, list);
1542 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545static inline void skb_split_inside_header(struct sk_buff *skb,
1546 struct sk_buff* skb1,
1547 const u32 len, const int pos)
1548{
1549 int i;
1550
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001551 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1552 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /* And move data appendix as is. */
1554 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1555 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1556
1557 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1558 skb_shinfo(skb)->nr_frags = 0;
1559 skb1->data_len = skb->data_len;
1560 skb1->len += skb1->data_len;
1561 skb->data_len = 0;
1562 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001563 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566static inline void skb_split_no_header(struct sk_buff *skb,
1567 struct sk_buff* skb1,
1568 const u32 len, int pos)
1569{
1570 int i, k = 0;
1571 const int nfrags = skb_shinfo(skb)->nr_frags;
1572
1573 skb_shinfo(skb)->nr_frags = 0;
1574 skb1->len = skb1->data_len = skb->len - len;
1575 skb->len = len;
1576 skb->data_len = len - pos;
1577
1578 for (i = 0; i < nfrags; i++) {
1579 int size = skb_shinfo(skb)->frags[i].size;
1580
1581 if (pos + size > len) {
1582 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1583
1584 if (pos < len) {
1585 /* Split frag.
1586 * We have two variants in this case:
1587 * 1. Move all the frag to the second
1588 * part, if it is possible. F.e.
1589 * this approach is mandatory for TUX,
1590 * where splitting is expensive.
1591 * 2. Split is accurately. We make this.
1592 */
1593 get_page(skb_shinfo(skb)->frags[i].page);
1594 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1595 skb_shinfo(skb1)->frags[0].size -= len - pos;
1596 skb_shinfo(skb)->frags[i].size = len - pos;
1597 skb_shinfo(skb)->nr_frags++;
1598 }
1599 k++;
1600 } else
1601 skb_shinfo(skb)->nr_frags++;
1602 pos += size;
1603 }
1604 skb_shinfo(skb1)->nr_frags = k;
1605}
1606
1607/**
1608 * skb_split - Split fragmented skb to two parts at length len.
1609 * @skb: the buffer to split
1610 * @skb1: the buffer to receive the second part
1611 * @len: new length for skb
1612 */
1613void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1614{
1615 int pos = skb_headlen(skb);
1616
1617 if (len < pos) /* Split line is inside header. */
1618 skb_split_inside_header(skb, skb1, len, pos);
1619 else /* Second chunk has no header, nothing to copy. */
1620 skb_split_no_header(skb, skb1, len, pos);
1621}
1622
Thomas Graf677e90e2005-06-23 20:59:51 -07001623/**
1624 * skb_prepare_seq_read - Prepare a sequential read of skb data
1625 * @skb: the buffer to read
1626 * @from: lower offset of data to be read
1627 * @to: upper offset of data to be read
1628 * @st: state variable
1629 *
1630 * Initializes the specified state variable. Must be called before
1631 * invoking skb_seq_read() for the first time.
1632 */
1633void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1634 unsigned int to, struct skb_seq_state *st)
1635{
1636 st->lower_offset = from;
1637 st->upper_offset = to;
1638 st->root_skb = st->cur_skb = skb;
1639 st->frag_idx = st->stepped_offset = 0;
1640 st->frag_data = NULL;
1641}
1642
1643/**
1644 * skb_seq_read - Sequentially read skb data
1645 * @consumed: number of bytes consumed by the caller so far
1646 * @data: destination pointer for data to be returned
1647 * @st: state variable
1648 *
1649 * Reads a block of skb data at &consumed relative to the
1650 * lower offset specified to skb_prepare_seq_read(). Assigns
1651 * the head of the data block to &data and returns the length
1652 * of the block or 0 if the end of the skb data or the upper
1653 * offset has been reached.
1654 *
1655 * The caller is not required to consume all of the data
1656 * returned, i.e. &consumed is typically set to the number
1657 * of bytes already consumed and the next call to
1658 * skb_seq_read() will return the remaining part of the block.
1659 *
1660 * Note: The size of each block of data returned can be arbitary,
1661 * this limitation is the cost for zerocopy seqeuental
1662 * reads of potentially non linear data.
1663 *
1664 * Note: Fragment lists within fragments are not implemented
1665 * at the moment, state->root_skb could be replaced with
1666 * a stack for this purpose.
1667 */
1668unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1669 struct skb_seq_state *st)
1670{
1671 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1672 skb_frag_t *frag;
1673
1674 if (unlikely(abs_offset >= st->upper_offset))
1675 return 0;
1676
1677next_skb:
1678 block_limit = skb_headlen(st->cur_skb);
1679
1680 if (abs_offset < block_limit) {
1681 *data = st->cur_skb->data + abs_offset;
1682 return block_limit - abs_offset;
1683 }
1684
1685 if (st->frag_idx == 0 && !st->frag_data)
1686 st->stepped_offset += skb_headlen(st->cur_skb);
1687
1688 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1689 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1690 block_limit = frag->size + st->stepped_offset;
1691
1692 if (abs_offset < block_limit) {
1693 if (!st->frag_data)
1694 st->frag_data = kmap_skb_frag(frag);
1695
1696 *data = (u8 *) st->frag_data + frag->page_offset +
1697 (abs_offset - st->stepped_offset);
1698
1699 return block_limit - abs_offset;
1700 }
1701
1702 if (st->frag_data) {
1703 kunmap_skb_frag(st->frag_data);
1704 st->frag_data = NULL;
1705 }
1706
1707 st->frag_idx++;
1708 st->stepped_offset += frag->size;
1709 }
1710
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001711 if (st->frag_data) {
1712 kunmap_skb_frag(st->frag_data);
1713 st->frag_data = NULL;
1714 }
1715
Thomas Graf677e90e2005-06-23 20:59:51 -07001716 if (st->cur_skb->next) {
1717 st->cur_skb = st->cur_skb->next;
1718 st->frag_idx = 0;
1719 goto next_skb;
1720 } else if (st->root_skb == st->cur_skb &&
1721 skb_shinfo(st->root_skb)->frag_list) {
1722 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1723 goto next_skb;
1724 }
1725
1726 return 0;
1727}
1728
1729/**
1730 * skb_abort_seq_read - Abort a sequential read of skb data
1731 * @st: state variable
1732 *
1733 * Must be called if skb_seq_read() was not called until it
1734 * returned 0.
1735 */
1736void skb_abort_seq_read(struct skb_seq_state *st)
1737{
1738 if (st->frag_data)
1739 kunmap_skb_frag(st->frag_data);
1740}
1741
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001742#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1743
1744static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1745 struct ts_config *conf,
1746 struct ts_state *state)
1747{
1748 return skb_seq_read(offset, text, TS_SKB_CB(state));
1749}
1750
1751static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1752{
1753 skb_abort_seq_read(TS_SKB_CB(state));
1754}
1755
1756/**
1757 * skb_find_text - Find a text pattern in skb data
1758 * @skb: the buffer to look in
1759 * @from: search offset
1760 * @to: search limit
1761 * @config: textsearch configuration
1762 * @state: uninitialized textsearch state variable
1763 *
1764 * Finds a pattern in the skb data according to the specified
1765 * textsearch configuration. Use textsearch_next() to retrieve
1766 * subsequent occurrences of the pattern. Returns the offset
1767 * to the first occurrence or UINT_MAX if no match was found.
1768 */
1769unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1770 unsigned int to, struct ts_config *config,
1771 struct ts_state *state)
1772{
Phil Oesterf72b9482006-06-26 00:00:57 -07001773 unsigned int ret;
1774
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001775 config->get_next_block = skb_ts_get_next_block;
1776 config->finish = skb_ts_finish;
1777
1778 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1779
Phil Oesterf72b9482006-06-26 00:00:57 -07001780 ret = textsearch_find(config, state);
1781 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001782}
1783
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001784/**
1785 * skb_append_datato_frags: - append the user data to a skb
1786 * @sk: sock structure
1787 * @skb: skb structure to be appened with user data.
1788 * @getfrag: call back function to be used for getting the user data
1789 * @from: pointer to user message iov
1790 * @length: length of the iov message
1791 *
1792 * Description: This procedure append the user data in the fragment part
1793 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1794 */
1795int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001796 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001797 int len, int odd, struct sk_buff *skb),
1798 void *from, int length)
1799{
1800 int frg_cnt = 0;
1801 skb_frag_t *frag = NULL;
1802 struct page *page = NULL;
1803 int copy, left;
1804 int offset = 0;
1805 int ret;
1806
1807 do {
1808 /* Return error if we don't have space for new frag */
1809 frg_cnt = skb_shinfo(skb)->nr_frags;
1810 if (frg_cnt >= MAX_SKB_FRAGS)
1811 return -EFAULT;
1812
1813 /* allocate a new page for next frag */
1814 page = alloc_pages(sk->sk_allocation, 0);
1815
1816 /* If alloc_page fails just return failure and caller will
1817 * free previous allocated pages by doing kfree_skb()
1818 */
1819 if (page == NULL)
1820 return -ENOMEM;
1821
1822 /* initialize the next frag */
1823 sk->sk_sndmsg_page = page;
1824 sk->sk_sndmsg_off = 0;
1825 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1826 skb->truesize += PAGE_SIZE;
1827 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1828
1829 /* get the new initialized frag */
1830 frg_cnt = skb_shinfo(skb)->nr_frags;
1831 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1832
1833 /* copy the user data to page */
1834 left = PAGE_SIZE - frag->page_offset;
1835 copy = (length > left)? left : length;
1836
1837 ret = getfrag(from, (page_address(frag->page) +
1838 frag->page_offset + frag->size),
1839 offset, copy, 0, skb);
1840 if (ret < 0)
1841 return -EFAULT;
1842
1843 /* copy was successful so update the size parameters */
1844 sk->sk_sndmsg_off += copy;
1845 frag->size += copy;
1846 skb->len += copy;
1847 skb->data_len += copy;
1848 offset += copy;
1849 length -= copy;
1850
1851 } while (length > 0);
1852
1853 return 0;
1854}
1855
Herbert Xucbb042f2006-03-20 22:43:56 -08001856/**
1857 * skb_pull_rcsum - pull skb and update receive checksum
1858 * @skb: buffer to update
1859 * @start: start of data before pull
1860 * @len: length of data pulled
1861 *
1862 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001863 * update the CHECKSUM_COMPLETE checksum. It should be used on
1864 * receive path processing instead of skb_pull unless you know
1865 * that the checksum difference is zero (e.g., a valid IP header)
1866 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001867 */
1868unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1869{
1870 BUG_ON(len > skb->len);
1871 skb->len -= len;
1872 BUG_ON(skb->len < skb->data_len);
1873 skb_postpull_rcsum(skb, skb->data, len);
1874 return skb->data += len;
1875}
1876
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001877EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1878
Herbert Xuf4c50d92006-06-22 03:02:40 -07001879/**
1880 * skb_segment - Perform protocol segmentation on skb.
1881 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001882 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001883 *
1884 * This function performs segmentation on the given skb. It returns
1885 * the segment at the given position. It returns NULL if there are
1886 * no more segments to generate, or when an error is encountered.
1887 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001888struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001889{
1890 struct sk_buff *segs = NULL;
1891 struct sk_buff *tail = NULL;
1892 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001893 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001894 unsigned int offset = doffset;
1895 unsigned int headroom;
1896 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001897 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001898 int nfrags = skb_shinfo(skb)->nr_frags;
1899 int err = -ENOMEM;
1900 int i = 0;
1901 int pos;
1902
1903 __skb_push(skb, doffset);
1904 headroom = skb_headroom(skb);
1905 pos = skb_headlen(skb);
1906
1907 do {
1908 struct sk_buff *nskb;
1909 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001910 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001911 int k;
1912 int size;
1913
1914 len = skb->len - offset;
1915 if (len > mss)
1916 len = mss;
1917
1918 hsize = skb_headlen(skb) - offset;
1919 if (hsize < 0)
1920 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001921 if (hsize > len || !sg)
1922 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001923
Herbert Xuc8884ed2006-10-29 15:59:41 -08001924 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001925 if (unlikely(!nskb))
1926 goto err;
1927
1928 if (segs)
1929 tail->next = nskb;
1930 else
1931 segs = nskb;
1932 tail = nskb;
1933
1934 nskb->dev = skb->dev;
1935 nskb->priority = skb->priority;
1936 nskb->protocol = skb->protocol;
1937 nskb->dst = dst_clone(skb->dst);
1938 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1939 nskb->pkt_type = skb->pkt_type;
1940 nskb->mac_len = skb->mac_len;
1941
1942 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001943 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001944 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001945 nskb->transport_header = (nskb->network_header +
1946 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001947 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1948 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001949 if (!sg) {
1950 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1951 skb_put(nskb, len),
1952 len, 0);
1953 continue;
1954 }
1955
1956 frag = skb_shinfo(nskb)->frags;
1957 k = 0;
1958
Patrick McHardy84fa7932006-08-29 16:44:56 -07001959 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001960 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001961 skb_copy_from_linear_data_offset(skb, offset,
1962 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001963
1964 while (pos < offset + len) {
1965 BUG_ON(i >= nfrags);
1966
1967 *frag = skb_shinfo(skb)->frags[i];
1968 get_page(frag->page);
1969 size = frag->size;
1970
1971 if (pos < offset) {
1972 frag->page_offset += offset - pos;
1973 frag->size -= offset - pos;
1974 }
1975
1976 k++;
1977
1978 if (pos + size <= offset + len) {
1979 i++;
1980 pos += size;
1981 } else {
1982 frag->size -= pos + size - (offset + len);
1983 break;
1984 }
1985
1986 frag++;
1987 }
1988
1989 skb_shinfo(nskb)->nr_frags = k;
1990 nskb->data_len = len - hsize;
1991 nskb->len += nskb->data_len;
1992 nskb->truesize += nskb->data_len;
1993 } while ((offset += len) < skb->len);
1994
1995 return segs;
1996
1997err:
1998 while ((skb = segs)) {
1999 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002000 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002001 }
2002 return ERR_PTR(err);
2003}
2004
2005EXPORT_SYMBOL_GPL(skb_segment);
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007void __init skb_init(void)
2008{
2009 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2010 sizeof(struct sk_buff),
2011 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002012 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002014 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2015 (2*sizeof(struct sk_buff)) +
2016 sizeof(atomic_t),
2017 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002018 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002019 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
David Howells716ea3a2007-04-02 20:19:53 -07002022/**
2023 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2024 * @skb: Socket buffer containing the buffers to be mapped
2025 * @sg: The scatter-gather list to map into
2026 * @offset: The offset into the buffer's contents to start mapping
2027 * @len: Length of buffer space to be mapped
2028 *
2029 * Fill the specified scatter-gather list with mappings/pointers into a
2030 * region of the buffer space attached to a socket buffer.
2031 */
2032int
2033skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2034{
David S. Miller1a028e52007-04-27 15:21:23 -07002035 int start = skb_headlen(skb);
2036 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002037 int elt = 0;
2038
2039 if (copy > 0) {
2040 if (copy > len)
2041 copy = len;
2042 sg[elt].page = virt_to_page(skb->data + offset);
2043 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2044 sg[elt].length = copy;
2045 elt++;
2046 if ((len -= copy) == 0)
2047 return elt;
2048 offset += copy;
2049 }
2050
2051 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002052 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002053
David S. Miller1a028e52007-04-27 15:21:23 -07002054 BUG_TRAP(start <= offset + len);
2055
2056 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002057 if ((copy = end - offset) > 0) {
2058 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2059
2060 if (copy > len)
2061 copy = len;
2062 sg[elt].page = frag->page;
David S. Miller1a028e52007-04-27 15:21:23 -07002063 sg[elt].offset = frag->page_offset+offset-start;
David Howells716ea3a2007-04-02 20:19:53 -07002064 sg[elt].length = copy;
2065 elt++;
2066 if (!(len -= copy))
2067 return elt;
2068 offset += copy;
2069 }
David S. Miller1a028e52007-04-27 15:21:23 -07002070 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002071 }
2072
2073 if (skb_shinfo(skb)->frag_list) {
2074 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2075
2076 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002077 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002078
David S. Miller1a028e52007-04-27 15:21:23 -07002079 BUG_TRAP(start <= offset + len);
2080
2081 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002082 if ((copy = end - offset) > 0) {
2083 if (copy > len)
2084 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07002085 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002086 if ((len -= copy) == 0)
2087 return elt;
2088 offset += copy;
2089 }
David S. Miller1a028e52007-04-27 15:21:23 -07002090 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002091 }
2092 }
2093 BUG_ON(len);
2094 return elt;
2095}
2096
2097/**
2098 * skb_cow_data - Check that a socket buffer's data buffers are writable
2099 * @skb: The socket buffer to check.
2100 * @tailbits: Amount of trailing space to be added
2101 * @trailer: Returned pointer to the skb where the @tailbits space begins
2102 *
2103 * Make sure that the data buffers attached to a socket buffer are
2104 * writable. If they are not, private copies are made of the data buffers
2105 * and the socket buffer is set to use these instead.
2106 *
2107 * If @tailbits is given, make sure that there is space to write @tailbits
2108 * bytes of data beyond current end of socket buffer. @trailer will be
2109 * set to point to the skb in which this space begins.
2110 *
2111 * The number of scatterlist elements required to completely map the
2112 * COW'd and extended socket buffer will be returned.
2113 */
2114int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2115{
2116 int copyflag;
2117 int elt;
2118 struct sk_buff *skb1, **skb_p;
2119
2120 /* If skb is cloned or its head is paged, reallocate
2121 * head pulling out all the pages (pages are considered not writable
2122 * at the moment even if they are anonymous).
2123 */
2124 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2125 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2126 return -ENOMEM;
2127
2128 /* Easy case. Most of packets will go this way. */
2129 if (!skb_shinfo(skb)->frag_list) {
2130 /* A little of trouble, not enough of space for trailer.
2131 * This should not happen, when stack is tuned to generate
2132 * good frames. OK, on miss we reallocate and reserve even more
2133 * space, 128 bytes is fair. */
2134
2135 if (skb_tailroom(skb) < tailbits &&
2136 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2137 return -ENOMEM;
2138
2139 /* Voila! */
2140 *trailer = skb;
2141 return 1;
2142 }
2143
2144 /* Misery. We are in troubles, going to mincer fragments... */
2145
2146 elt = 1;
2147 skb_p = &skb_shinfo(skb)->frag_list;
2148 copyflag = 0;
2149
2150 while ((skb1 = *skb_p) != NULL) {
2151 int ntail = 0;
2152
2153 /* The fragment is partially pulled by someone,
2154 * this can happen on input. Copy it and everything
2155 * after it. */
2156
2157 if (skb_shared(skb1))
2158 copyflag = 1;
2159
2160 /* If the skb is the last, worry about trailer. */
2161
2162 if (skb1->next == NULL && tailbits) {
2163 if (skb_shinfo(skb1)->nr_frags ||
2164 skb_shinfo(skb1)->frag_list ||
2165 skb_tailroom(skb1) < tailbits)
2166 ntail = tailbits + 128;
2167 }
2168
2169 if (copyflag ||
2170 skb_cloned(skb1) ||
2171 ntail ||
2172 skb_shinfo(skb1)->nr_frags ||
2173 skb_shinfo(skb1)->frag_list) {
2174 struct sk_buff *skb2;
2175
2176 /* Fuck, we are miserable poor guys... */
2177 if (ntail == 0)
2178 skb2 = skb_copy(skb1, GFP_ATOMIC);
2179 else
2180 skb2 = skb_copy_expand(skb1,
2181 skb_headroom(skb1),
2182 ntail,
2183 GFP_ATOMIC);
2184 if (unlikely(skb2 == NULL))
2185 return -ENOMEM;
2186
2187 if (skb1->sk)
2188 skb_set_owner_w(skb2, skb1->sk);
2189
2190 /* Looking around. Are we still alive?
2191 * OK, link new skb, drop old one */
2192
2193 skb2->next = skb1->next;
2194 *skb_p = skb2;
2195 kfree_skb(skb1);
2196 skb1 = skb2;
2197 }
2198 elt++;
2199 *trailer = skb1;
2200 skb_p = &skb1->next;
2201 }
2202
2203 return elt;
2204}
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206EXPORT_SYMBOL(___pskb_trim);
2207EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002208EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002210EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002211EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212EXPORT_SYMBOL(pskb_copy);
2213EXPORT_SYMBOL(pskb_expand_head);
2214EXPORT_SYMBOL(skb_checksum);
2215EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216EXPORT_SYMBOL(skb_copy);
2217EXPORT_SYMBOL(skb_copy_and_csum_bits);
2218EXPORT_SYMBOL(skb_copy_and_csum_dev);
2219EXPORT_SYMBOL(skb_copy_bits);
2220EXPORT_SYMBOL(skb_copy_expand);
2221EXPORT_SYMBOL(skb_over_panic);
2222EXPORT_SYMBOL(skb_pad);
2223EXPORT_SYMBOL(skb_realloc_headroom);
2224EXPORT_SYMBOL(skb_under_panic);
2225EXPORT_SYMBOL(skb_dequeue);
2226EXPORT_SYMBOL(skb_dequeue_tail);
2227EXPORT_SYMBOL(skb_insert);
2228EXPORT_SYMBOL(skb_queue_purge);
2229EXPORT_SYMBOL(skb_queue_head);
2230EXPORT_SYMBOL(skb_queue_tail);
2231EXPORT_SYMBOL(skb_unlink);
2232EXPORT_SYMBOL(skb_append);
2233EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002234EXPORT_SYMBOL(skb_prepare_seq_read);
2235EXPORT_SYMBOL(skb_seq_read);
2236EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002237EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002238EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002239
2240EXPORT_SYMBOL_GPL(skb_to_sgvec);
2241EXPORT_SYMBOL_GPL(skb_cow_data);