blob: 0583e8498f1351d2631024fd811ecdd1172da27c [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 Viroa1f8e7f2006-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);
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -0700422 skb_copy_queue_mapping(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800424#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
425 C(ipvs_property);
426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800429 C(mark);
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700430 __nf_copy(n, skb);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700431#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
432 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
433 C(nf_trace);
434#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#ifdef CONFIG_NET_SCHED
436 C(tc_index);
437#ifdef CONFIG_NET_CLS_ACT
438 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700439 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
440 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700441 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#endif
Patrick McHardydbbeb2f2007-06-23 22:58:34 -0700444 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 C(truesize);
446 atomic_set(&n->users, 1);
447 C(head);
448 C(data);
449 C(tail);
450 C(end);
451
452 atomic_inc(&(skb_shinfo(skb)->dataref));
453 skb->cloned = 1;
454
455 return n;
456}
457
458static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
459{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700460#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
462 * Shift between the two data areas in bytes
463 */
464 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 new->sk = NULL;
467 new->dev = old->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -0700468 skb_copy_queue_mapping(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 new->priority = old->priority;
470 new->protocol = old->protocol;
471 new->dst = dst_clone(old->dst);
472#ifdef CONFIG_INET
473 new->sp = secpath_get(old->sp);
474#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700475 new->transport_header = old->transport_header;
476 new->network_header = old->network_header;
477 new->mac_header = old->mac_header;
478#ifndef NET_SKBUFF_DATA_USES_OFFSET
479 /* {transport,network,mac}_header are relative to skb->head */
480 new->transport_header += offset;
481 new->network_header += offset;
482 new->mac_header += offset;
483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 memcpy(new->cb, old->cb, sizeof(old->cb));
485 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700486 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700488 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800490 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700491 __nf_copy(new, old);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700492#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
493 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
494 new->nf_trace = old->nf_trace;
495#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300496#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
497 new->ipvs_property = old->ipvs_property;
498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499#ifdef CONFIG_NET_SCHED
500#ifdef CONFIG_NET_CLS_ACT
501 new->tc_verd = old->tc_verd;
502#endif
503 new->tc_index = old->tc_index;
504#endif
James Morris984bc162006-06-09 00:29:17 -0700505 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700507 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
508 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
509 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/**
513 * skb_copy - create private copy of an sk_buff
514 * @skb: buffer to copy
515 * @gfp_mask: allocation priority
516 *
517 * Make a copy of both an &sk_buff and its data. This is used when the
518 * caller wishes to modify the data and needs a private copy of the
519 * data to alter. Returns %NULL on failure or the pointer to the buffer
520 * on success. The returned buffer has a reference count of 1.
521 *
522 * As by-product this function converts non-linear &sk_buff to linear
523 * one, so that &sk_buff becomes completely private and caller is allowed
524 * to modify all the data of returned buffer. This means that this
525 * function is not recommended for use in circumstances when only
526 * header is going to be modified. Use pskb_copy() instead.
527 */
528
Al Virodd0fc662005-10-07 07:46:04 +0100529struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 int headerlen = skb->data - skb->head;
532 /*
533 * Allocate the copy buffer
534 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700535 struct sk_buff *n;
536#ifdef NET_SKBUFF_DATA_USES_OFFSET
537 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
538#else
539 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
540#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!n)
542 return NULL;
543
544 /* Set the data pointer */
545 skb_reserve(n, headerlen);
546 /* Set the tail pointer and length */
547 skb_put(n, skb->len);
548 n->csum = skb->csum;
549 n->ip_summed = skb->ip_summed;
550
551 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
552 BUG();
553
554 copy_skb_header(n, skb);
555 return n;
556}
557
558
559/**
560 * pskb_copy - create copy of an sk_buff with private head.
561 * @skb: buffer to copy
562 * @gfp_mask: allocation priority
563 *
564 * Make a copy of both an &sk_buff and part of its data, located
565 * in header. Fragmented data remain shared. This is used when
566 * the caller wishes to modify only header of &sk_buff and needs
567 * private copy of the header to alter. Returns %NULL on failure
568 * or the pointer to the buffer on success.
569 * The returned buffer has a reference count of 1.
570 */
571
Al Virodd0fc662005-10-07 07:46:04 +0100572struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 /*
575 * Allocate the copy buffer
576 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700577 struct sk_buff *n;
578#ifdef NET_SKBUFF_DATA_USES_OFFSET
579 n = alloc_skb(skb->end, gfp_mask);
580#else
581 n = alloc_skb(skb->end - skb->head, gfp_mask);
582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!n)
584 goto out;
585
586 /* Set the data pointer */
587 skb_reserve(n, skb->data - skb->head);
588 /* Set the tail pointer and length */
589 skb_put(n, skb_headlen(skb));
590 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300591 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 n->csum = skb->csum;
593 n->ip_summed = skb->ip_summed;
594
Herbert Xu25f484a2006-11-07 14:57:15 -0800595 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 n->data_len = skb->data_len;
597 n->len = skb->len;
598
599 if (skb_shinfo(skb)->nr_frags) {
600 int i;
601
602 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
603 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
604 get_page(skb_shinfo(n)->frags[i].page);
605 }
606 skb_shinfo(n)->nr_frags = i;
607 }
608
609 if (skb_shinfo(skb)->frag_list) {
610 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
611 skb_clone_fraglist(n);
612 }
613
614 copy_skb_header(n, skb);
615out:
616 return n;
617}
618
619/**
620 * pskb_expand_head - reallocate header of &sk_buff
621 * @skb: buffer to reallocate
622 * @nhead: room to add at head
623 * @ntail: room to add at tail
624 * @gfp_mask: allocation priority
625 *
626 * Expands (or creates identical copy, if &nhead and &ntail are zero)
627 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
628 * reference count of 1. Returns zero in the case of success or error,
629 * if expansion failed. In the last case, &sk_buff is not changed.
630 *
631 * All the pointers pointing into skb header may change and must be
632 * reloaded after call to this function.
633 */
634
Victor Fusco86a76ca2005-07-08 14:57:47 -0700635int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100636 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int i;
639 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700640#ifdef NET_SKBUFF_DATA_USES_OFFSET
641 int size = nhead + skb->end + ntail;
642#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700644#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 long off;
646
647 if (skb_shared(skb))
648 BUG();
649
650 size = SKB_DATA_ALIGN(size);
651
652 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
653 if (!data)
654 goto nodata;
655
656 /* Copy only real data... and, alas, header. This should be
657 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700658#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700659 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700660#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700661 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700662#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700663 memcpy(data + size, skb_end_pointer(skb),
664 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
667 get_page(skb_shinfo(skb)->frags[i].page);
668
669 if (skb_shinfo(skb)->frag_list)
670 skb_clone_fraglist(skb);
671
672 skb_release_data(skb);
673
674 off = (data + nhead) - skb->head;
675
676 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700678#ifdef NET_SKBUFF_DATA_USES_OFFSET
679 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700680 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700681#else
682 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700683#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700684 /* {transport,network,mac}_header and tail are relative to skb->head */
685 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700686 skb->transport_header += off;
687 skb->network_header += off;
688 skb->mac_header += off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700690 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 skb->nohdr = 0;
692 atomic_set(&skb_shinfo(skb)->dataref, 1);
693 return 0;
694
695nodata:
696 return -ENOMEM;
697}
698
699/* Make private copy of skb with writable head and some headroom */
700
701struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
702{
703 struct sk_buff *skb2;
704 int delta = headroom - skb_headroom(skb);
705
706 if (delta <= 0)
707 skb2 = pskb_copy(skb, GFP_ATOMIC);
708 else {
709 skb2 = skb_clone(skb, GFP_ATOMIC);
710 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
711 GFP_ATOMIC)) {
712 kfree_skb(skb2);
713 skb2 = NULL;
714 }
715 }
716 return skb2;
717}
718
719
720/**
721 * skb_copy_expand - copy and expand sk_buff
722 * @skb: buffer to copy
723 * @newheadroom: new free bytes at head
724 * @newtailroom: new free bytes at tail
725 * @gfp_mask: allocation priority
726 *
727 * Make a copy of both an &sk_buff and its data and while doing so
728 * allocate additional space.
729 *
730 * This is used when the caller wishes to modify the data and needs a
731 * private copy of the data to alter as well as more space for new fields.
732 * Returns %NULL on failure or the pointer to the buffer
733 * on success. The returned buffer has a reference count of 1.
734 *
735 * You must pass %GFP_ATOMIC as the allocation priority if this function
736 * is called from an interrupt.
737 *
738 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
739 * only by netfilter in the cases when checksum is recalculated? --ANK
740 */
741struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700742 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100743 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 /*
746 * Allocate the copy buffer
747 */
748 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
749 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700750 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int head_copy_len, head_copy_off;
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700752 int off = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!n)
755 return NULL;
756
757 skb_reserve(n, newheadroom);
758
759 /* Set the tail pointer and length */
760 skb_put(n, skb->len);
761
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700762 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 head_copy_off = 0;
764 if (newheadroom <= head_copy_len)
765 head_copy_len = newheadroom;
766 else
767 head_copy_off = newheadroom - head_copy_len;
768
769 /* Copy the linear header and data. */
770 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
771 skb->len + head_copy_len))
772 BUG();
773
774 copy_skb_header(n, skb);
775
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700776#ifdef NET_SKBUFF_DATA_USES_OFFSET
777 off = newheadroom - oldheadroom;
778#endif
779 n->transport_header += off;
780 n->network_header += off;
781 n->mac_header += off;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return n;
784}
785
786/**
787 * skb_pad - zero pad the tail of an skb
788 * @skb: buffer to pad
789 * @pad: space to pad
790 *
791 * Ensure that a buffer is followed by a padding area that is zero
792 * filled. Used by network drivers which may DMA or transfer data
793 * beyond the buffer end onto the wire.
794 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700795 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900797
Herbert Xu5b057c62006-06-23 02:06:41 -0700798int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Herbert Xu5b057c62006-06-23 02:06:41 -0700800 int err;
801 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700804 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700808
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700809 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700810 if (likely(skb_cloned(skb) || ntail > 0)) {
811 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
812 if (unlikely(err))
813 goto free_skb;
814 }
815
816 /* FIXME: The use of this function with non-linear skb's really needs
817 * to be audited.
818 */
819 err = skb_linearize(skb);
820 if (unlikely(err))
821 goto free_skb;
822
823 memset(skb->data + skb->len, 0, pad);
824 return 0;
825
826free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700828 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900829}
830
Herbert Xu3cc0e872006-06-09 16:13:38 -0700831/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
833
Herbert Xu3cc0e872006-06-09 16:13:38 -0700834int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Herbert Xu27b437c2006-07-13 19:26:39 -0700836 struct sk_buff **fragp;
837 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int offset = skb_headlen(skb);
839 int nfrags = skb_shinfo(skb)->nr_frags;
840 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700841 int err;
842
843 if (skb_cloned(skb) &&
844 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
845 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700847 i = 0;
848 if (offset >= len)
849 goto drop_pages;
850
851 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700853
854 if (end < len) {
855 offset = end;
856 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700858
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700859 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700860
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700861drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700862 skb_shinfo(skb)->nr_frags = i;
863
864 for (; i < nfrags; i++)
865 put_page(skb_shinfo(skb)->frags[i].page);
866
867 if (skb_shinfo(skb)->frag_list)
868 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700869 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Herbert Xu27b437c2006-07-13 19:26:39 -0700872 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
873 fragp = &frag->next) {
874 int end = offset + frag->len;
875
876 if (skb_shared(frag)) {
877 struct sk_buff *nfrag;
878
879 nfrag = skb_clone(frag, GFP_ATOMIC);
880 if (unlikely(!nfrag))
881 return -ENOMEM;
882
883 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700884 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700885 frag = nfrag;
886 *fragp = frag;
887 }
888
889 if (end < len) {
890 offset = end;
891 continue;
892 }
893
894 if (end > len &&
895 unlikely((err = pskb_trim(frag, len - offset))))
896 return err;
897
898 if (frag->next)
899 skb_drop_list(&frag->next);
900 break;
901 }
902
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700903done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700904 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 skb->data_len -= skb->len - len;
906 skb->len = len;
907 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700908 skb->len = len;
909 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700910 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
913 return 0;
914}
915
916/**
917 * __pskb_pull_tail - advance tail of skb header
918 * @skb: buffer to reallocate
919 * @delta: number of bytes to advance tail
920 *
921 * The function makes a sense only on a fragmented &sk_buff,
922 * it expands header moving its tail forward and copying necessary
923 * data from fragmented part.
924 *
925 * &sk_buff MUST have reference count of 1.
926 *
927 * Returns %NULL (and &sk_buff does not change) if pull failed
928 * or value of new tail of skb in the case of success.
929 *
930 * All the pointers pointing into skb header may change and must be
931 * reloaded after call to this function.
932 */
933
934/* Moves tail of skb head forward, copying data from fragmented part,
935 * when it is necessary.
936 * 1. It may fail due to malloc failure.
937 * 2. It may change skb pointers.
938 *
939 * It is pretty complicated. Luckily, it is called only in exceptional cases.
940 */
941unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
942{
943 /* If skb has not enough free space at tail, get new one
944 * plus 128 bytes for future expansions. If we have enough
945 * room at tail, reallocate without expansion only if skb is cloned.
946 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700947 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (eat > 0 || skb_cloned(skb)) {
950 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
951 GFP_ATOMIC))
952 return NULL;
953 }
954
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700955 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 BUG();
957
958 /* Optimization: no fragments, no reasons to preestimate
959 * size of pulled pages. Superb.
960 */
961 if (!skb_shinfo(skb)->frag_list)
962 goto pull_pages;
963
964 /* Estimate size of pulled pages. */
965 eat = delta;
966 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
967 if (skb_shinfo(skb)->frags[i].size >= eat)
968 goto pull_pages;
969 eat -= skb_shinfo(skb)->frags[i].size;
970 }
971
972 /* If we need update frag list, we are in troubles.
973 * Certainly, it possible to add an offset to skb data,
974 * but taking into account that pulling is expected to
975 * be very rare operation, it is worth to fight against
976 * further bloating skb head and crucify ourselves here instead.
977 * Pure masohism, indeed. 8)8)
978 */
979 if (eat) {
980 struct sk_buff *list = skb_shinfo(skb)->frag_list;
981 struct sk_buff *clone = NULL;
982 struct sk_buff *insp = NULL;
983
984 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800985 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (list->len <= eat) {
988 /* Eaten as whole. */
989 eat -= list->len;
990 list = list->next;
991 insp = list;
992 } else {
993 /* Eaten partially. */
994
995 if (skb_shared(list)) {
996 /* Sucks! We need to fork list. :-( */
997 clone = skb_clone(list, GFP_ATOMIC);
998 if (!clone)
999 return NULL;
1000 insp = list->next;
1001 list = clone;
1002 } else {
1003 /* This may be pulled without
1004 * problems. */
1005 insp = list;
1006 }
1007 if (!pskb_pull(list, eat)) {
1008 if (clone)
1009 kfree_skb(clone);
1010 return NULL;
1011 }
1012 break;
1013 }
1014 } while (eat);
1015
1016 /* Free pulled out fragments. */
1017 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1018 skb_shinfo(skb)->frag_list = list->next;
1019 kfree_skb(list);
1020 }
1021 /* And insert new clone at head. */
1022 if (clone) {
1023 clone->next = list;
1024 skb_shinfo(skb)->frag_list = clone;
1025 }
1026 }
1027 /* Success! Now we may commit changes to skb data. */
1028
1029pull_pages:
1030 eat = delta;
1031 k = 0;
1032 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1033 if (skb_shinfo(skb)->frags[i].size <= eat) {
1034 put_page(skb_shinfo(skb)->frags[i].page);
1035 eat -= skb_shinfo(skb)->frags[i].size;
1036 } else {
1037 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1038 if (eat) {
1039 skb_shinfo(skb)->frags[k].page_offset += eat;
1040 skb_shinfo(skb)->frags[k].size -= eat;
1041 eat = 0;
1042 }
1043 k++;
1044 }
1045 }
1046 skb_shinfo(skb)->nr_frags = k;
1047
1048 skb->tail += delta;
1049 skb->data_len -= delta;
1050
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001051 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/* Copy some data bits from skb to kernel buffer. */
1055
1056int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1057{
1058 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001059 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (offset > (int)skb->len - len)
1062 goto fault;
1063
1064 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001065 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (copy > len)
1067 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001068 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if ((len -= copy) == 0)
1070 return 0;
1071 offset += copy;
1072 to += copy;
1073 }
1074
1075 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001076 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
David S. Miller1a028e52007-04-27 15:21:23 -07001078 BUG_TRAP(start <= offset + len);
1079
1080 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if ((copy = end - offset) > 0) {
1082 u8 *vaddr;
1083
1084 if (copy > len)
1085 copy = len;
1086
1087 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1088 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001089 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1090 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kunmap_skb_frag(vaddr);
1092
1093 if ((len -= copy) == 0)
1094 return 0;
1095 offset += copy;
1096 to += copy;
1097 }
David S. Miller1a028e52007-04-27 15:21:23 -07001098 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 if (skb_shinfo(skb)->frag_list) {
1102 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1103
1104 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001105 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
David S. Miller1a028e52007-04-27 15:21:23 -07001107 BUG_TRAP(start <= offset + len);
1108
1109 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if ((copy = end - offset) > 0) {
1111 if (copy > len)
1112 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001113 if (skb_copy_bits(list, offset - start,
1114 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 goto fault;
1116 if ((len -= copy) == 0)
1117 return 0;
1118 offset += copy;
1119 to += copy;
1120 }
David S. Miller1a028e52007-04-27 15:21:23 -07001121 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 }
1124 if (!len)
1125 return 0;
1126
1127fault:
1128 return -EFAULT;
1129}
1130
Herbert Xu357b40a2005-04-19 22:30:14 -07001131/**
1132 * skb_store_bits - store bits from kernel buffer to skb
1133 * @skb: destination buffer
1134 * @offset: offset in destination
1135 * @from: source buffer
1136 * @len: number of bytes to copy
1137 *
1138 * Copy the specified number of bytes from the source buffer to the
1139 * destination skb. This function handles all the messy bits of
1140 * traversing fragment lists and such.
1141 */
1142
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001143int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001144{
1145 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001146 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001147
1148 if (offset > (int)skb->len - len)
1149 goto fault;
1150
David S. Miller1a028e52007-04-27 15:21:23 -07001151 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001152 if (copy > len)
1153 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001154 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001155 if ((len -= copy) == 0)
1156 return 0;
1157 offset += copy;
1158 from += copy;
1159 }
1160
1161 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1162 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001163 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001164
David S. Miller1a028e52007-04-27 15:21:23 -07001165 BUG_TRAP(start <= offset + len);
1166
1167 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001168 if ((copy = end - offset) > 0) {
1169 u8 *vaddr;
1170
1171 if (copy > len)
1172 copy = len;
1173
1174 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001175 memcpy(vaddr + frag->page_offset + offset - start,
1176 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001177 kunmap_skb_frag(vaddr);
1178
1179 if ((len -= copy) == 0)
1180 return 0;
1181 offset += copy;
1182 from += copy;
1183 }
David S. Miller1a028e52007-04-27 15:21:23 -07001184 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001185 }
1186
1187 if (skb_shinfo(skb)->frag_list) {
1188 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1189
1190 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001191 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001192
David S. Miller1a028e52007-04-27 15:21:23 -07001193 BUG_TRAP(start <= offset + len);
1194
1195 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001196 if ((copy = end - offset) > 0) {
1197 if (copy > len)
1198 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001199 if (skb_store_bits(list, offset - start,
1200 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001201 goto fault;
1202 if ((len -= copy) == 0)
1203 return 0;
1204 offset += copy;
1205 from += copy;
1206 }
David S. Miller1a028e52007-04-27 15:21:23 -07001207 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001208 }
1209 }
1210 if (!len)
1211 return 0;
1212
1213fault:
1214 return -EFAULT;
1215}
1216
1217EXPORT_SYMBOL(skb_store_bits);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/* Checksum skb data. */
1220
Al Viro2bbbc862006-11-14 21:37:14 -08001221__wsum skb_checksum(const struct sk_buff *skb, int offset,
1222 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
David S. Miller1a028e52007-04-27 15:21:23 -07001224 int start = skb_headlen(skb);
1225 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 int pos = 0;
1227
1228 /* Checksum header. */
1229 if (copy > 0) {
1230 if (copy > len)
1231 copy = len;
1232 csum = csum_partial(skb->data + offset, copy, csum);
1233 if ((len -= copy) == 0)
1234 return csum;
1235 offset += copy;
1236 pos = copy;
1237 }
1238
1239 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001240 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
David S. Miller1a028e52007-04-27 15:21:23 -07001242 BUG_TRAP(start <= offset + len);
1243
1244 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001246 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 u8 *vaddr;
1248 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1249
1250 if (copy > len)
1251 copy = len;
1252 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001253 csum2 = csum_partial(vaddr + frag->page_offset +
1254 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 kunmap_skb_frag(vaddr);
1256 csum = csum_block_add(csum, csum2, pos);
1257 if (!(len -= copy))
1258 return csum;
1259 offset += copy;
1260 pos += copy;
1261 }
David S. Miller1a028e52007-04-27 15:21:23 -07001262 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
1265 if (skb_shinfo(skb)->frag_list) {
1266 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1267
1268 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001269 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
David S. Miller1a028e52007-04-27 15:21:23 -07001271 BUG_TRAP(start <= offset + len);
1272
1273 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001275 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (copy > len)
1277 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001278 csum2 = skb_checksum(list, offset - start,
1279 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 csum = csum_block_add(csum, csum2, pos);
1281 if ((len -= copy) == 0)
1282 return csum;
1283 offset += copy;
1284 pos += copy;
1285 }
David S. Miller1a028e52007-04-27 15:21:23 -07001286 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001289 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 return csum;
1292}
1293
1294/* Both of above in one bottle. */
1295
Al Viro81d77662006-11-14 21:37:33 -08001296__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1297 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
David S. Miller1a028e52007-04-27 15:21:23 -07001299 int start = skb_headlen(skb);
1300 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int pos = 0;
1302
1303 /* Copy header. */
1304 if (copy > 0) {
1305 if (copy > len)
1306 copy = len;
1307 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1308 copy, csum);
1309 if ((len -= copy) == 0)
1310 return csum;
1311 offset += copy;
1312 to += copy;
1313 pos = copy;
1314 }
1315
1316 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001317 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
David S. Miller1a028e52007-04-27 15:21:23 -07001319 BUG_TRAP(start <= offset + len);
1320
1321 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001323 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 u8 *vaddr;
1325 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1326
1327 if (copy > len)
1328 copy = len;
1329 vaddr = kmap_skb_frag(frag);
1330 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001331 frag->page_offset +
1332 offset - start, to,
1333 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 kunmap_skb_frag(vaddr);
1335 csum = csum_block_add(csum, csum2, pos);
1336 if (!(len -= copy))
1337 return csum;
1338 offset += copy;
1339 to += copy;
1340 pos += copy;
1341 }
David S. Miller1a028e52007-04-27 15:21:23 -07001342 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
1345 if (skb_shinfo(skb)->frag_list) {
1346 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1347
1348 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001349 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001350 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
David S. Miller1a028e52007-04-27 15:21:23 -07001352 BUG_TRAP(start <= offset + len);
1353
1354 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if ((copy = end - offset) > 0) {
1356 if (copy > len)
1357 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001358 csum2 = skb_copy_and_csum_bits(list,
1359 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 to, copy, 0);
1361 csum = csum_block_add(csum, csum2, pos);
1362 if ((len -= copy) == 0)
1363 return csum;
1364 offset += copy;
1365 to += copy;
1366 pos += copy;
1367 }
David S. Miller1a028e52007-04-27 15:21:23 -07001368 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001371 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return csum;
1373}
1374
1375void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1376{
Al Virod3bc23e2006-11-14 21:24:49 -08001377 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 long csstart;
1379
Patrick McHardy84fa7932006-08-29 16:44:56 -07001380 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001381 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 else
1383 csstart = skb_headlen(skb);
1384
Kris Katterjohn09a62662006-01-08 22:24:28 -08001385 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001387 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 csum = 0;
1390 if (csstart != skb->len)
1391 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1392 skb->len - csstart, 0);
1393
Patrick McHardy84fa7932006-08-29 16:44:56 -07001394 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001395 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Al Virod3bc23e2006-11-14 21:24:49 -08001397 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399}
1400
1401/**
1402 * skb_dequeue - remove from the head of the queue
1403 * @list: list to dequeue from
1404 *
1405 * Remove the head of the list. The list lock is taken so the function
1406 * may be used safely with other locking list functions. The head item is
1407 * returned or %NULL if the list is empty.
1408 */
1409
1410struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1411{
1412 unsigned long flags;
1413 struct sk_buff *result;
1414
1415 spin_lock_irqsave(&list->lock, flags);
1416 result = __skb_dequeue(list);
1417 spin_unlock_irqrestore(&list->lock, flags);
1418 return result;
1419}
1420
1421/**
1422 * skb_dequeue_tail - remove from the tail of the queue
1423 * @list: list to dequeue from
1424 *
1425 * Remove the tail of the list. The list lock is taken so the function
1426 * may be used safely with other locking list functions. The tail item is
1427 * returned or %NULL if the list is empty.
1428 */
1429struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1430{
1431 unsigned long flags;
1432 struct sk_buff *result;
1433
1434 spin_lock_irqsave(&list->lock, flags);
1435 result = __skb_dequeue_tail(list);
1436 spin_unlock_irqrestore(&list->lock, flags);
1437 return result;
1438}
1439
1440/**
1441 * skb_queue_purge - empty a list
1442 * @list: list to empty
1443 *
1444 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1445 * the list and one reference dropped. This function takes the list
1446 * lock and is atomic with respect to other list locking functions.
1447 */
1448void skb_queue_purge(struct sk_buff_head *list)
1449{
1450 struct sk_buff *skb;
1451 while ((skb = skb_dequeue(list)) != NULL)
1452 kfree_skb(skb);
1453}
1454
1455/**
1456 * skb_queue_head - queue a buffer at the list head
1457 * @list: list to use
1458 * @newsk: buffer to queue
1459 *
1460 * Queue a buffer at the start of the list. This function takes the
1461 * list lock and can be used safely with other locking &sk_buff functions
1462 * safely.
1463 *
1464 * A buffer cannot be placed on two lists at the same time.
1465 */
1466void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1467{
1468 unsigned long flags;
1469
1470 spin_lock_irqsave(&list->lock, flags);
1471 __skb_queue_head(list, newsk);
1472 spin_unlock_irqrestore(&list->lock, flags);
1473}
1474
1475/**
1476 * skb_queue_tail - queue a buffer at the list tail
1477 * @list: list to use
1478 * @newsk: buffer to queue
1479 *
1480 * Queue a buffer at the tail of the list. This function takes the
1481 * list lock and can be used safely with other locking &sk_buff functions
1482 * safely.
1483 *
1484 * A buffer cannot be placed on two lists at the same time.
1485 */
1486void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1487{
1488 unsigned long flags;
1489
1490 spin_lock_irqsave(&list->lock, flags);
1491 __skb_queue_tail(list, newsk);
1492 spin_unlock_irqrestore(&list->lock, flags);
1493}
David S. Miller8728b832005-08-09 19:25:21 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
1496 * skb_unlink - remove a buffer from a list
1497 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001498 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
David S. Miller8728b832005-08-09 19:25:21 -07001500 * Remove a packet from a list. The list locks are taken and this
1501 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
David S. Miller8728b832005-08-09 19:25:21 -07001503 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
David S. Miller8728b832005-08-09 19:25:21 -07001505void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
David S. Miller8728b832005-08-09 19:25:21 -07001507 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
David S. Miller8728b832005-08-09 19:25:21 -07001509 spin_lock_irqsave(&list->lock, flags);
1510 __skb_unlink(skb, list);
1511 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/**
1515 * skb_append - append a buffer
1516 * @old: buffer to insert after
1517 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001518 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 *
1520 * Place a packet after a given packet in a list. The list locks are taken
1521 * and this function is atomic with respect to other list locked calls.
1522 * A buffer cannot be placed on two lists at the same time.
1523 */
David S. Miller8728b832005-08-09 19:25:21 -07001524void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 unsigned long flags;
1527
David S. Miller8728b832005-08-09 19:25:21 -07001528 spin_lock_irqsave(&list->lock, flags);
1529 __skb_append(old, newsk, list);
1530 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533
1534/**
1535 * skb_insert - insert a buffer
1536 * @old: buffer to insert before
1537 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001538 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
David S. Miller8728b832005-08-09 19:25:21 -07001540 * Place a packet before a given packet in a list. The list locks are
1541 * taken and this function is atomic with respect to other list locked
1542 * calls.
1543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * A buffer cannot be placed on two lists at the same time.
1545 */
David S. Miller8728b832005-08-09 19:25:21 -07001546void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 unsigned long flags;
1549
David S. Miller8728b832005-08-09 19:25:21 -07001550 spin_lock_irqsave(&list->lock, flags);
1551 __skb_insert(newsk, old->prev, old, list);
1552 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555static inline void skb_split_inside_header(struct sk_buff *skb,
1556 struct sk_buff* skb1,
1557 const u32 len, const int pos)
1558{
1559 int i;
1560
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001561 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1562 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /* And move data appendix as is. */
1564 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1565 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1566
1567 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1568 skb_shinfo(skb)->nr_frags = 0;
1569 skb1->data_len = skb->data_len;
1570 skb1->len += skb1->data_len;
1571 skb->data_len = 0;
1572 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001573 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static inline void skb_split_no_header(struct sk_buff *skb,
1577 struct sk_buff* skb1,
1578 const u32 len, int pos)
1579{
1580 int i, k = 0;
1581 const int nfrags = skb_shinfo(skb)->nr_frags;
1582
1583 skb_shinfo(skb)->nr_frags = 0;
1584 skb1->len = skb1->data_len = skb->len - len;
1585 skb->len = len;
1586 skb->data_len = len - pos;
1587
1588 for (i = 0; i < nfrags; i++) {
1589 int size = skb_shinfo(skb)->frags[i].size;
1590
1591 if (pos + size > len) {
1592 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1593
1594 if (pos < len) {
1595 /* Split frag.
1596 * We have two variants in this case:
1597 * 1. Move all the frag to the second
1598 * part, if it is possible. F.e.
1599 * this approach is mandatory for TUX,
1600 * where splitting is expensive.
1601 * 2. Split is accurately. We make this.
1602 */
1603 get_page(skb_shinfo(skb)->frags[i].page);
1604 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1605 skb_shinfo(skb1)->frags[0].size -= len - pos;
1606 skb_shinfo(skb)->frags[i].size = len - pos;
1607 skb_shinfo(skb)->nr_frags++;
1608 }
1609 k++;
1610 } else
1611 skb_shinfo(skb)->nr_frags++;
1612 pos += size;
1613 }
1614 skb_shinfo(skb1)->nr_frags = k;
1615}
1616
1617/**
1618 * skb_split - Split fragmented skb to two parts at length len.
1619 * @skb: the buffer to split
1620 * @skb1: the buffer to receive the second part
1621 * @len: new length for skb
1622 */
1623void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1624{
1625 int pos = skb_headlen(skb);
1626
1627 if (len < pos) /* Split line is inside header. */
1628 skb_split_inside_header(skb, skb1, len, pos);
1629 else /* Second chunk has no header, nothing to copy. */
1630 skb_split_no_header(skb, skb1, len, pos);
1631}
1632
Thomas Graf677e90e2005-06-23 20:59:51 -07001633/**
1634 * skb_prepare_seq_read - Prepare a sequential read of skb data
1635 * @skb: the buffer to read
1636 * @from: lower offset of data to be read
1637 * @to: upper offset of data to be read
1638 * @st: state variable
1639 *
1640 * Initializes the specified state variable. Must be called before
1641 * invoking skb_seq_read() for the first time.
1642 */
1643void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1644 unsigned int to, struct skb_seq_state *st)
1645{
1646 st->lower_offset = from;
1647 st->upper_offset = to;
1648 st->root_skb = st->cur_skb = skb;
1649 st->frag_idx = st->stepped_offset = 0;
1650 st->frag_data = NULL;
1651}
1652
1653/**
1654 * skb_seq_read - Sequentially read skb data
1655 * @consumed: number of bytes consumed by the caller so far
1656 * @data: destination pointer for data to be returned
1657 * @st: state variable
1658 *
1659 * Reads a block of skb data at &consumed relative to the
1660 * lower offset specified to skb_prepare_seq_read(). Assigns
1661 * the head of the data block to &data and returns the length
1662 * of the block or 0 if the end of the skb data or the upper
1663 * offset has been reached.
1664 *
1665 * The caller is not required to consume all of the data
1666 * returned, i.e. &consumed is typically set to the number
1667 * of bytes already consumed and the next call to
1668 * skb_seq_read() will return the remaining part of the block.
1669 *
1670 * Note: The size of each block of data returned can be arbitary,
1671 * this limitation is the cost for zerocopy seqeuental
1672 * reads of potentially non linear data.
1673 *
1674 * Note: Fragment lists within fragments are not implemented
1675 * at the moment, state->root_skb could be replaced with
1676 * a stack for this purpose.
1677 */
1678unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1679 struct skb_seq_state *st)
1680{
1681 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1682 skb_frag_t *frag;
1683
1684 if (unlikely(abs_offset >= st->upper_offset))
1685 return 0;
1686
1687next_skb:
1688 block_limit = skb_headlen(st->cur_skb);
1689
1690 if (abs_offset < block_limit) {
1691 *data = st->cur_skb->data + abs_offset;
1692 return block_limit - abs_offset;
1693 }
1694
1695 if (st->frag_idx == 0 && !st->frag_data)
1696 st->stepped_offset += skb_headlen(st->cur_skb);
1697
1698 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1699 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1700 block_limit = frag->size + st->stepped_offset;
1701
1702 if (abs_offset < block_limit) {
1703 if (!st->frag_data)
1704 st->frag_data = kmap_skb_frag(frag);
1705
1706 *data = (u8 *) st->frag_data + frag->page_offset +
1707 (abs_offset - st->stepped_offset);
1708
1709 return block_limit - abs_offset;
1710 }
1711
1712 if (st->frag_data) {
1713 kunmap_skb_frag(st->frag_data);
1714 st->frag_data = NULL;
1715 }
1716
1717 st->frag_idx++;
1718 st->stepped_offset += frag->size;
1719 }
1720
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001721 if (st->frag_data) {
1722 kunmap_skb_frag(st->frag_data);
1723 st->frag_data = NULL;
1724 }
1725
Thomas Graf677e90e2005-06-23 20:59:51 -07001726 if (st->cur_skb->next) {
1727 st->cur_skb = st->cur_skb->next;
1728 st->frag_idx = 0;
1729 goto next_skb;
1730 } else if (st->root_skb == st->cur_skb &&
1731 skb_shinfo(st->root_skb)->frag_list) {
1732 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1733 goto next_skb;
1734 }
1735
1736 return 0;
1737}
1738
1739/**
1740 * skb_abort_seq_read - Abort a sequential read of skb data
1741 * @st: state variable
1742 *
1743 * Must be called if skb_seq_read() was not called until it
1744 * returned 0.
1745 */
1746void skb_abort_seq_read(struct skb_seq_state *st)
1747{
1748 if (st->frag_data)
1749 kunmap_skb_frag(st->frag_data);
1750}
1751
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001752#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1753
1754static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1755 struct ts_config *conf,
1756 struct ts_state *state)
1757{
1758 return skb_seq_read(offset, text, TS_SKB_CB(state));
1759}
1760
1761static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1762{
1763 skb_abort_seq_read(TS_SKB_CB(state));
1764}
1765
1766/**
1767 * skb_find_text - Find a text pattern in skb data
1768 * @skb: the buffer to look in
1769 * @from: search offset
1770 * @to: search limit
1771 * @config: textsearch configuration
1772 * @state: uninitialized textsearch state variable
1773 *
1774 * Finds a pattern in the skb data according to the specified
1775 * textsearch configuration. Use textsearch_next() to retrieve
1776 * subsequent occurrences of the pattern. Returns the offset
1777 * to the first occurrence or UINT_MAX if no match was found.
1778 */
1779unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1780 unsigned int to, struct ts_config *config,
1781 struct ts_state *state)
1782{
Phil Oesterf72b9482006-06-26 00:00:57 -07001783 unsigned int ret;
1784
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001785 config->get_next_block = skb_ts_get_next_block;
1786 config->finish = skb_ts_finish;
1787
1788 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1789
Phil Oesterf72b9482006-06-26 00:00:57 -07001790 ret = textsearch_find(config, state);
1791 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001792}
1793
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001794/**
1795 * skb_append_datato_frags: - append the user data to a skb
1796 * @sk: sock structure
1797 * @skb: skb structure to be appened with user data.
1798 * @getfrag: call back function to be used for getting the user data
1799 * @from: pointer to user message iov
1800 * @length: length of the iov message
1801 *
1802 * Description: This procedure append the user data in the fragment part
1803 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1804 */
1805int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001806 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001807 int len, int odd, struct sk_buff *skb),
1808 void *from, int length)
1809{
1810 int frg_cnt = 0;
1811 skb_frag_t *frag = NULL;
1812 struct page *page = NULL;
1813 int copy, left;
1814 int offset = 0;
1815 int ret;
1816
1817 do {
1818 /* Return error if we don't have space for new frag */
1819 frg_cnt = skb_shinfo(skb)->nr_frags;
1820 if (frg_cnt >= MAX_SKB_FRAGS)
1821 return -EFAULT;
1822
1823 /* allocate a new page for next frag */
1824 page = alloc_pages(sk->sk_allocation, 0);
1825
1826 /* If alloc_page fails just return failure and caller will
1827 * free previous allocated pages by doing kfree_skb()
1828 */
1829 if (page == NULL)
1830 return -ENOMEM;
1831
1832 /* initialize the next frag */
1833 sk->sk_sndmsg_page = page;
1834 sk->sk_sndmsg_off = 0;
1835 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1836 skb->truesize += PAGE_SIZE;
1837 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1838
1839 /* get the new initialized frag */
1840 frg_cnt = skb_shinfo(skb)->nr_frags;
1841 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1842
1843 /* copy the user data to page */
1844 left = PAGE_SIZE - frag->page_offset;
1845 copy = (length > left)? left : length;
1846
1847 ret = getfrag(from, (page_address(frag->page) +
1848 frag->page_offset + frag->size),
1849 offset, copy, 0, skb);
1850 if (ret < 0)
1851 return -EFAULT;
1852
1853 /* copy was successful so update the size parameters */
1854 sk->sk_sndmsg_off += copy;
1855 frag->size += copy;
1856 skb->len += copy;
1857 skb->data_len += copy;
1858 offset += copy;
1859 length -= copy;
1860
1861 } while (length > 0);
1862
1863 return 0;
1864}
1865
Herbert Xucbb042f2006-03-20 22:43:56 -08001866/**
1867 * skb_pull_rcsum - pull skb and update receive checksum
1868 * @skb: buffer to update
1869 * @start: start of data before pull
1870 * @len: length of data pulled
1871 *
1872 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001873 * update the CHECKSUM_COMPLETE checksum. It should be used on
1874 * receive path processing instead of skb_pull unless you know
1875 * that the checksum difference is zero (e.g., a valid IP header)
1876 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001877 */
1878unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1879{
1880 BUG_ON(len > skb->len);
1881 skb->len -= len;
1882 BUG_ON(skb->len < skb->data_len);
1883 skb_postpull_rcsum(skb, skb->data, len);
1884 return skb->data += len;
1885}
1886
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001887EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1888
Herbert Xuf4c50d92006-06-22 03:02:40 -07001889/**
1890 * skb_segment - Perform protocol segmentation on skb.
1891 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001892 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001893 *
1894 * This function performs segmentation on the given skb. It returns
1895 * the segment at the given position. It returns NULL if there are
1896 * no more segments to generate, or when an error is encountered.
1897 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001898struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001899{
1900 struct sk_buff *segs = NULL;
1901 struct sk_buff *tail = NULL;
1902 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001903 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001904 unsigned int offset = doffset;
1905 unsigned int headroom;
1906 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001907 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001908 int nfrags = skb_shinfo(skb)->nr_frags;
1909 int err = -ENOMEM;
1910 int i = 0;
1911 int pos;
1912
1913 __skb_push(skb, doffset);
1914 headroom = skb_headroom(skb);
1915 pos = skb_headlen(skb);
1916
1917 do {
1918 struct sk_buff *nskb;
1919 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001920 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001921 int k;
1922 int size;
1923
1924 len = skb->len - offset;
1925 if (len > mss)
1926 len = mss;
1927
1928 hsize = skb_headlen(skb) - offset;
1929 if (hsize < 0)
1930 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001931 if (hsize > len || !sg)
1932 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001933
Herbert Xuc8884ed2006-10-29 15:59:41 -08001934 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001935 if (unlikely(!nskb))
1936 goto err;
1937
1938 if (segs)
1939 tail->next = nskb;
1940 else
1941 segs = nskb;
1942 tail = nskb;
1943
1944 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001945 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001946 nskb->priority = skb->priority;
1947 nskb->protocol = skb->protocol;
1948 nskb->dst = dst_clone(skb->dst);
1949 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1950 nskb->pkt_type = skb->pkt_type;
1951 nskb->mac_len = skb->mac_len;
1952
1953 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001954 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001955 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001956 nskb->transport_header = (nskb->network_header +
1957 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001958 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1959 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001960 if (!sg) {
1961 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1962 skb_put(nskb, len),
1963 len, 0);
1964 continue;
1965 }
1966
1967 frag = skb_shinfo(nskb)->frags;
1968 k = 0;
1969
Patrick McHardy84fa7932006-08-29 16:44:56 -07001970 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001971 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001972 skb_copy_from_linear_data_offset(skb, offset,
1973 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001974
1975 while (pos < offset + len) {
1976 BUG_ON(i >= nfrags);
1977
1978 *frag = skb_shinfo(skb)->frags[i];
1979 get_page(frag->page);
1980 size = frag->size;
1981
1982 if (pos < offset) {
1983 frag->page_offset += offset - pos;
1984 frag->size -= offset - pos;
1985 }
1986
1987 k++;
1988
1989 if (pos + size <= offset + len) {
1990 i++;
1991 pos += size;
1992 } else {
1993 frag->size -= pos + size - (offset + len);
1994 break;
1995 }
1996
1997 frag++;
1998 }
1999
2000 skb_shinfo(nskb)->nr_frags = k;
2001 nskb->data_len = len - hsize;
2002 nskb->len += nskb->data_len;
2003 nskb->truesize += nskb->data_len;
2004 } while ((offset += len) < skb->len);
2005
2006 return segs;
2007
2008err:
2009 while ((skb = segs)) {
2010 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002011 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002012 }
2013 return ERR_PTR(err);
2014}
2015
2016EXPORT_SYMBOL_GPL(skb_segment);
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018void __init skb_init(void)
2019{
2020 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2021 sizeof(struct sk_buff),
2022 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002023 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002025 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2026 (2*sizeof(struct sk_buff)) +
2027 sizeof(atomic_t),
2028 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002029 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002030 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
David Howells716ea3a2007-04-02 20:19:53 -07002033/**
2034 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2035 * @skb: Socket buffer containing the buffers to be mapped
2036 * @sg: The scatter-gather list to map into
2037 * @offset: The offset into the buffer's contents to start mapping
2038 * @len: Length of buffer space to be mapped
2039 *
2040 * Fill the specified scatter-gather list with mappings/pointers into a
2041 * region of the buffer space attached to a socket buffer.
2042 */
2043int
2044skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2045{
David S. Miller1a028e52007-04-27 15:21:23 -07002046 int start = skb_headlen(skb);
2047 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002048 int elt = 0;
2049
2050 if (copy > 0) {
2051 if (copy > len)
2052 copy = len;
2053 sg[elt].page = virt_to_page(skb->data + offset);
2054 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2055 sg[elt].length = copy;
2056 elt++;
2057 if ((len -= copy) == 0)
2058 return elt;
2059 offset += copy;
2060 }
2061
2062 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002063 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002064
David S. Miller1a028e52007-04-27 15:21:23 -07002065 BUG_TRAP(start <= offset + len);
2066
2067 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002068 if ((copy = end - offset) > 0) {
2069 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2070
2071 if (copy > len)
2072 copy = len;
2073 sg[elt].page = frag->page;
David S. Miller1a028e52007-04-27 15:21:23 -07002074 sg[elt].offset = frag->page_offset+offset-start;
David Howells716ea3a2007-04-02 20:19:53 -07002075 sg[elt].length = copy;
2076 elt++;
2077 if (!(len -= copy))
2078 return elt;
2079 offset += copy;
2080 }
David S. Miller1a028e52007-04-27 15:21:23 -07002081 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002082 }
2083
2084 if (skb_shinfo(skb)->frag_list) {
2085 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2086
2087 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002088 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002089
David S. Miller1a028e52007-04-27 15:21:23 -07002090 BUG_TRAP(start <= offset + len);
2091
2092 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002093 if ((copy = end - offset) > 0) {
2094 if (copy > len)
2095 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07002096 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002097 if ((len -= copy) == 0)
2098 return elt;
2099 offset += copy;
2100 }
David S. Miller1a028e52007-04-27 15:21:23 -07002101 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002102 }
2103 }
2104 BUG_ON(len);
2105 return elt;
2106}
2107
2108/**
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);