blob: 944189d96323c9944089eded7b017cb1a3050599 [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);
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
Herbert Xu52886052007-09-16 16:32:11 -0700475 new->csum_start = old->csum_start;
476 new->csum_offset = old->csum_offset;
477 new->ip_summed = old->ip_summed;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700478 new->transport_header = old->transport_header;
479 new->network_header = old->network_header;
480 new->mac_header = old->mac_header;
481#ifndef NET_SKBUFF_DATA_USES_OFFSET
482 /* {transport,network,mac}_header are relative to skb->head */
483 new->transport_header += offset;
484 new->network_header += offset;
485 new->mac_header += offset;
486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 memcpy(new->cb, old->cb, sizeof(old->cb));
488 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700489 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700491 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800493 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700494 __nf_copy(new, old);
Jozsef Kadlecsikba9dda32007-07-07 22:21:23 -0700495#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
496 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
497 new->nf_trace = old->nf_trace;
498#endif
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300499#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
500 new->ipvs_property = old->ipvs_property;
501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#ifdef CONFIG_NET_SCHED
503#ifdef CONFIG_NET_CLS_ACT
504 new->tc_verd = old->tc_verd;
505#endif
506 new->tc_index = old->tc_index;
507#endif
James Morris984bc162006-06-09 00:29:17 -0700508 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700510 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
511 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
512 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/**
516 * skb_copy - create private copy of an sk_buff
517 * @skb: buffer to copy
518 * @gfp_mask: allocation priority
519 *
520 * Make a copy of both an &sk_buff and its data. This is used when the
521 * caller wishes to modify the data and needs a private copy of the
522 * data to alter. Returns %NULL on failure or the pointer to the buffer
523 * on success. The returned buffer has a reference count of 1.
524 *
525 * As by-product this function converts non-linear &sk_buff to linear
526 * one, so that &sk_buff becomes completely private and caller is allowed
527 * to modify all the data of returned buffer. This means that this
528 * function is not recommended for use in circumstances when only
529 * header is going to be modified. Use pskb_copy() instead.
530 */
531
Al Virodd0fc662005-10-07 07:46:04 +0100532struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int headerlen = skb->data - skb->head;
535 /*
536 * Allocate the copy buffer
537 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700538 struct sk_buff *n;
539#ifdef NET_SKBUFF_DATA_USES_OFFSET
540 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
541#else
542 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!n)
545 return NULL;
546
547 /* Set the data pointer */
548 skb_reserve(n, headerlen);
549 /* Set the tail pointer and length */
550 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
553 BUG();
554
555 copy_skb_header(n, skb);
556 return n;
557}
558
559
560/**
561 * pskb_copy - create copy of an sk_buff with private head.
562 * @skb: buffer to copy
563 * @gfp_mask: allocation priority
564 *
565 * Make a copy of both an &sk_buff and part of its data, located
566 * in header. Fragmented data remain shared. This is used when
567 * the caller wishes to modify only header of &sk_buff and needs
568 * private copy of the header to alter. Returns %NULL on failure
569 * or the pointer to the buffer on success.
570 * The returned buffer has a reference count of 1.
571 */
572
Al Virodd0fc662005-10-07 07:46:04 +0100573struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 /*
576 * Allocate the copy buffer
577 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700578 struct sk_buff *n;
579#ifdef NET_SKBUFF_DATA_USES_OFFSET
580 n = alloc_skb(skb->end, gfp_mask);
581#else
582 n = alloc_skb(skb->end - skb->head, gfp_mask);
583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!n)
585 goto out;
586
587 /* Set the data pointer */
588 skb_reserve(n, skb->data - skb->head);
589 /* Set the tail pointer and length */
590 skb_put(n, skb_headlen(skb));
591 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300592 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Herbert Xu25f484a2006-11-07 14:57:15 -0800594 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 n->data_len = skb->data_len;
596 n->len = skb->len;
597
598 if (skb_shinfo(skb)->nr_frags) {
599 int i;
600
601 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
602 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
603 get_page(skb_shinfo(n)->frags[i].page);
604 }
605 skb_shinfo(n)->nr_frags = i;
606 }
607
608 if (skb_shinfo(skb)->frag_list) {
609 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
610 skb_clone_fraglist(n);
611 }
612
613 copy_skb_header(n, skb);
614out:
615 return n;
616}
617
618/**
619 * pskb_expand_head - reallocate header of &sk_buff
620 * @skb: buffer to reallocate
621 * @nhead: room to add at head
622 * @ntail: room to add at tail
623 * @gfp_mask: allocation priority
624 *
625 * Expands (or creates identical copy, if &nhead and &ntail are zero)
626 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
627 * reference count of 1. Returns zero in the case of success or error,
628 * if expansion failed. In the last case, &sk_buff is not changed.
629 *
630 * All the pointers pointing into skb header may change and must be
631 * reloaded after call to this function.
632 */
633
Victor Fusco86a76ca2005-07-08 14:57:47 -0700634int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100635 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 int i;
638 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700639#ifdef NET_SKBUFF_DATA_USES_OFFSET
640 int size = nhead + skb->end + ntail;
641#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 long off;
645
646 if (skb_shared(skb))
647 BUG();
648
649 size = SKB_DATA_ALIGN(size);
650
651 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
652 if (!data)
653 goto nodata;
654
655 /* Copy only real data... and, alas, header. This should be
656 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700657#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700658 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700659#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700660 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700661#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700662 memcpy(data + size, skb_end_pointer(skb),
663 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
666 get_page(skb_shinfo(skb)->frags[i].page);
667
668 if (skb_shinfo(skb)->frag_list)
669 skb_clone_fraglist(skb);
670
671 skb_release_data(skb);
672
673 off = (data + nhead) - skb->head;
674
675 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700677#ifdef NET_SKBUFF_DATA_USES_OFFSET
678 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700679 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700680#else
681 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700682#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700683 /* {transport,network,mac}_header and tail are relative to skb->head */
684 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700685 skb->transport_header += off;
686 skb->network_header += off;
687 skb->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700688 skb->csum_start += 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
738struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700739 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100740 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 /*
743 * Allocate the copy buffer
744 */
745 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
746 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700747 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700749 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (!n)
752 return NULL;
753
754 skb_reserve(n, newheadroom);
755
756 /* Set the tail pointer and length */
757 skb_put(n, skb->len);
758
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700759 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 head_copy_off = 0;
761 if (newheadroom <= head_copy_len)
762 head_copy_len = newheadroom;
763 else
764 head_copy_off = newheadroom - head_copy_len;
765
766 /* Copy the linear header and data. */
767 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
768 skb->len + head_copy_len))
769 BUG();
770
771 copy_skb_header(n, skb);
772
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700773 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700774 n->csum_start += off;
775#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700776 n->transport_header += off;
777 n->network_header += off;
778 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700779#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return n;
782}
783
784/**
785 * skb_pad - zero pad the tail of an skb
786 * @skb: buffer to pad
787 * @pad: space to pad
788 *
789 * Ensure that a buffer is followed by a padding area that is zero
790 * filled. Used by network drivers which may DMA or transfer data
791 * beyond the buffer end onto the wire.
792 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700793 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900795
Herbert Xu5b057c62006-06-23 02:06:41 -0700796int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Herbert Xu5b057c62006-06-23 02:06:41 -0700798 int err;
799 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700802 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700806
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700807 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700808 if (likely(skb_cloned(skb) || ntail > 0)) {
809 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
810 if (unlikely(err))
811 goto free_skb;
812 }
813
814 /* FIXME: The use of this function with non-linear skb's really needs
815 * to be audited.
816 */
817 err = skb_linearize(skb);
818 if (unlikely(err))
819 goto free_skb;
820
821 memset(skb->data + skb->len, 0, pad);
822 return 0;
823
824free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700826 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900827}
828
Herbert Xu3cc0e872006-06-09 16:13:38 -0700829/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
831
Herbert Xu3cc0e872006-06-09 16:13:38 -0700832int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Herbert Xu27b437c2006-07-13 19:26:39 -0700834 struct sk_buff **fragp;
835 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int offset = skb_headlen(skb);
837 int nfrags = skb_shinfo(skb)->nr_frags;
838 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700839 int err;
840
841 if (skb_cloned(skb) &&
842 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
843 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700845 i = 0;
846 if (offset >= len)
847 goto drop_pages;
848
849 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700851
852 if (end < len) {
853 offset = end;
854 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700856
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700857 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700858
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700859drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700860 skb_shinfo(skb)->nr_frags = i;
861
862 for (; i < nfrags; i++)
863 put_page(skb_shinfo(skb)->frags[i].page);
864
865 if (skb_shinfo(skb)->frag_list)
866 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700867 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Herbert Xu27b437c2006-07-13 19:26:39 -0700870 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
871 fragp = &frag->next) {
872 int end = offset + frag->len;
873
874 if (skb_shared(frag)) {
875 struct sk_buff *nfrag;
876
877 nfrag = skb_clone(frag, GFP_ATOMIC);
878 if (unlikely(!nfrag))
879 return -ENOMEM;
880
881 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700882 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700883 frag = nfrag;
884 *fragp = frag;
885 }
886
887 if (end < len) {
888 offset = end;
889 continue;
890 }
891
892 if (end > len &&
893 unlikely((err = pskb_trim(frag, len - offset))))
894 return err;
895
896 if (frag->next)
897 skb_drop_list(&frag->next);
898 break;
899 }
900
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700901done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700902 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 skb->data_len -= skb->len - len;
904 skb->len = len;
905 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700906 skb->len = len;
907 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700908 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 return 0;
912}
913
914/**
915 * __pskb_pull_tail - advance tail of skb header
916 * @skb: buffer to reallocate
917 * @delta: number of bytes to advance tail
918 *
919 * The function makes a sense only on a fragmented &sk_buff,
920 * it expands header moving its tail forward and copying necessary
921 * data from fragmented part.
922 *
923 * &sk_buff MUST have reference count of 1.
924 *
925 * Returns %NULL (and &sk_buff does not change) if pull failed
926 * or value of new tail of skb in the case of success.
927 *
928 * All the pointers pointing into skb header may change and must be
929 * reloaded after call to this function.
930 */
931
932/* Moves tail of skb head forward, copying data from fragmented part,
933 * when it is necessary.
934 * 1. It may fail due to malloc failure.
935 * 2. It may change skb pointers.
936 *
937 * It is pretty complicated. Luckily, it is called only in exceptional cases.
938 */
939unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
940{
941 /* If skb has not enough free space at tail, get new one
942 * plus 128 bytes for future expansions. If we have enough
943 * room at tail, reallocate without expansion only if skb is cloned.
944 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700945 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (eat > 0 || skb_cloned(skb)) {
948 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
949 GFP_ATOMIC))
950 return NULL;
951 }
952
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700953 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 BUG();
955
956 /* Optimization: no fragments, no reasons to preestimate
957 * size of pulled pages. Superb.
958 */
959 if (!skb_shinfo(skb)->frag_list)
960 goto pull_pages;
961
962 /* Estimate size of pulled pages. */
963 eat = delta;
964 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
965 if (skb_shinfo(skb)->frags[i].size >= eat)
966 goto pull_pages;
967 eat -= skb_shinfo(skb)->frags[i].size;
968 }
969
970 /* If we need update frag list, we are in troubles.
971 * Certainly, it possible to add an offset to skb data,
972 * but taking into account that pulling is expected to
973 * be very rare operation, it is worth to fight against
974 * further bloating skb head and crucify ourselves here instead.
975 * Pure masohism, indeed. 8)8)
976 */
977 if (eat) {
978 struct sk_buff *list = skb_shinfo(skb)->frag_list;
979 struct sk_buff *clone = NULL;
980 struct sk_buff *insp = NULL;
981
982 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800983 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (list->len <= eat) {
986 /* Eaten as whole. */
987 eat -= list->len;
988 list = list->next;
989 insp = list;
990 } else {
991 /* Eaten partially. */
992
993 if (skb_shared(list)) {
994 /* Sucks! We need to fork list. :-( */
995 clone = skb_clone(list, GFP_ATOMIC);
996 if (!clone)
997 return NULL;
998 insp = list->next;
999 list = clone;
1000 } else {
1001 /* This may be pulled without
1002 * problems. */
1003 insp = list;
1004 }
1005 if (!pskb_pull(list, eat)) {
1006 if (clone)
1007 kfree_skb(clone);
1008 return NULL;
1009 }
1010 break;
1011 }
1012 } while (eat);
1013
1014 /* Free pulled out fragments. */
1015 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1016 skb_shinfo(skb)->frag_list = list->next;
1017 kfree_skb(list);
1018 }
1019 /* And insert new clone at head. */
1020 if (clone) {
1021 clone->next = list;
1022 skb_shinfo(skb)->frag_list = clone;
1023 }
1024 }
1025 /* Success! Now we may commit changes to skb data. */
1026
1027pull_pages:
1028 eat = delta;
1029 k = 0;
1030 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1031 if (skb_shinfo(skb)->frags[i].size <= eat) {
1032 put_page(skb_shinfo(skb)->frags[i].page);
1033 eat -= skb_shinfo(skb)->frags[i].size;
1034 } else {
1035 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1036 if (eat) {
1037 skb_shinfo(skb)->frags[k].page_offset += eat;
1038 skb_shinfo(skb)->frags[k].size -= eat;
1039 eat = 0;
1040 }
1041 k++;
1042 }
1043 }
1044 skb_shinfo(skb)->nr_frags = k;
1045
1046 skb->tail += delta;
1047 skb->data_len -= delta;
1048
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001049 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052/* Copy some data bits from skb to kernel buffer. */
1053
1054int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1055{
1056 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001057 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (offset > (int)skb->len - len)
1060 goto fault;
1061
1062 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001063 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (copy > len)
1065 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001066 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if ((len -= copy) == 0)
1068 return 0;
1069 offset += copy;
1070 to += copy;
1071 }
1072
1073 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001074 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David S. Miller1a028e52007-04-27 15:21:23 -07001076 BUG_TRAP(start <= offset + len);
1077
1078 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if ((copy = end - offset) > 0) {
1080 u8 *vaddr;
1081
1082 if (copy > len)
1083 copy = len;
1084
1085 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1086 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001087 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1088 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 kunmap_skb_frag(vaddr);
1090
1091 if ((len -= copy) == 0)
1092 return 0;
1093 offset += copy;
1094 to += copy;
1095 }
David S. Miller1a028e52007-04-27 15:21:23 -07001096 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 if (skb_shinfo(skb)->frag_list) {
1100 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1101
1102 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001103 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
David S. Miller1a028e52007-04-27 15:21:23 -07001105 BUG_TRAP(start <= offset + len);
1106
1107 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if ((copy = end - offset) > 0) {
1109 if (copy > len)
1110 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001111 if (skb_copy_bits(list, offset - start,
1112 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 goto fault;
1114 if ((len -= copy) == 0)
1115 return 0;
1116 offset += copy;
1117 to += copy;
1118 }
David S. Miller1a028e52007-04-27 15:21:23 -07001119 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122 if (!len)
1123 return 0;
1124
1125fault:
1126 return -EFAULT;
1127}
1128
Herbert Xu357b40a2005-04-19 22:30:14 -07001129/**
1130 * skb_store_bits - store bits from kernel buffer to skb
1131 * @skb: destination buffer
1132 * @offset: offset in destination
1133 * @from: source buffer
1134 * @len: number of bytes to copy
1135 *
1136 * Copy the specified number of bytes from the source buffer to the
1137 * destination skb. This function handles all the messy bits of
1138 * traversing fragment lists and such.
1139 */
1140
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001141int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001142{
1143 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001144 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001145
1146 if (offset > (int)skb->len - len)
1147 goto fault;
1148
David S. Miller1a028e52007-04-27 15:21:23 -07001149 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001150 if (copy > len)
1151 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001152 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001153 if ((len -= copy) == 0)
1154 return 0;
1155 offset += copy;
1156 from += copy;
1157 }
1158
1159 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1160 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001161 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001162
David S. Miller1a028e52007-04-27 15:21:23 -07001163 BUG_TRAP(start <= offset + len);
1164
1165 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001166 if ((copy = end - offset) > 0) {
1167 u8 *vaddr;
1168
1169 if (copy > len)
1170 copy = len;
1171
1172 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001173 memcpy(vaddr + frag->page_offset + offset - start,
1174 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001175 kunmap_skb_frag(vaddr);
1176
1177 if ((len -= copy) == 0)
1178 return 0;
1179 offset += copy;
1180 from += copy;
1181 }
David S. Miller1a028e52007-04-27 15:21:23 -07001182 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001183 }
1184
1185 if (skb_shinfo(skb)->frag_list) {
1186 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1187
1188 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001189 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001190
David S. Miller1a028e52007-04-27 15:21:23 -07001191 BUG_TRAP(start <= offset + len);
1192
1193 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001194 if ((copy = end - offset) > 0) {
1195 if (copy > len)
1196 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001197 if (skb_store_bits(list, offset - start,
1198 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001199 goto fault;
1200 if ((len -= copy) == 0)
1201 return 0;
1202 offset += copy;
1203 from += copy;
1204 }
David S. Miller1a028e52007-04-27 15:21:23 -07001205 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001206 }
1207 }
1208 if (!len)
1209 return 0;
1210
1211fault:
1212 return -EFAULT;
1213}
1214
1215EXPORT_SYMBOL(skb_store_bits);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/* Checksum skb data. */
1218
Al Viro2bbbc862006-11-14 21:37:14 -08001219__wsum skb_checksum(const struct sk_buff *skb, int offset,
1220 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
David S. Miller1a028e52007-04-27 15:21:23 -07001222 int start = skb_headlen(skb);
1223 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int pos = 0;
1225
1226 /* Checksum header. */
1227 if (copy > 0) {
1228 if (copy > len)
1229 copy = len;
1230 csum = csum_partial(skb->data + offset, copy, csum);
1231 if ((len -= copy) == 0)
1232 return csum;
1233 offset += copy;
1234 pos = copy;
1235 }
1236
1237 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001238 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
David S. Miller1a028e52007-04-27 15:21:23 -07001240 BUG_TRAP(start <= offset + len);
1241
1242 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001244 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 u8 *vaddr;
1246 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1247
1248 if (copy > len)
1249 copy = len;
1250 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001251 csum2 = csum_partial(vaddr + frag->page_offset +
1252 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 kunmap_skb_frag(vaddr);
1254 csum = csum_block_add(csum, csum2, pos);
1255 if (!(len -= copy))
1256 return csum;
1257 offset += copy;
1258 pos += copy;
1259 }
David S. Miller1a028e52007-04-27 15:21:23 -07001260 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262
1263 if (skb_shinfo(skb)->frag_list) {
1264 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1265
1266 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001267 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
David S. Miller1a028e52007-04-27 15:21:23 -07001269 BUG_TRAP(start <= offset + len);
1270
1271 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001273 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (copy > len)
1275 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001276 csum2 = skb_checksum(list, offset - start,
1277 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 csum = csum_block_add(csum, csum2, pos);
1279 if ((len -= copy) == 0)
1280 return csum;
1281 offset += copy;
1282 pos += copy;
1283 }
David S. Miller1a028e52007-04-27 15:21:23 -07001284 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001287 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 return csum;
1290}
1291
1292/* Both of above in one bottle. */
1293
Al Viro81d77662006-11-14 21:37:33 -08001294__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1295 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
David S. Miller1a028e52007-04-27 15:21:23 -07001297 int start = skb_headlen(skb);
1298 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int pos = 0;
1300
1301 /* Copy header. */
1302 if (copy > 0) {
1303 if (copy > len)
1304 copy = len;
1305 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1306 copy, csum);
1307 if ((len -= copy) == 0)
1308 return csum;
1309 offset += copy;
1310 to += copy;
1311 pos = copy;
1312 }
1313
1314 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001315 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
David S. Miller1a028e52007-04-27 15:21:23 -07001317 BUG_TRAP(start <= offset + len);
1318
1319 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001321 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 u8 *vaddr;
1323 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1324
1325 if (copy > len)
1326 copy = len;
1327 vaddr = kmap_skb_frag(frag);
1328 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001329 frag->page_offset +
1330 offset - start, to,
1331 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 kunmap_skb_frag(vaddr);
1333 csum = csum_block_add(csum, csum2, pos);
1334 if (!(len -= copy))
1335 return csum;
1336 offset += copy;
1337 to += copy;
1338 pos += copy;
1339 }
David S. Miller1a028e52007-04-27 15:21:23 -07001340 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 if (skb_shinfo(skb)->frag_list) {
1344 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1345
1346 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001347 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001348 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
David S. Miller1a028e52007-04-27 15:21:23 -07001350 BUG_TRAP(start <= offset + len);
1351
1352 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if ((copy = end - offset) > 0) {
1354 if (copy > len)
1355 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001356 csum2 = skb_copy_and_csum_bits(list,
1357 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 to, copy, 0);
1359 csum = csum_block_add(csum, csum2, pos);
1360 if ((len -= copy) == 0)
1361 return csum;
1362 offset += copy;
1363 to += copy;
1364 pos += copy;
1365 }
David S. Miller1a028e52007-04-27 15:21:23 -07001366 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001369 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return csum;
1371}
1372
1373void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1374{
Al Virod3bc23e2006-11-14 21:24:49 -08001375 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 long csstart;
1377
Patrick McHardy84fa7932006-08-29 16:44:56 -07001378 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001379 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 else
1381 csstart = skb_headlen(skb);
1382
Kris Katterjohn09a62662006-01-08 22:24:28 -08001383 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001385 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 csum = 0;
1388 if (csstart != skb->len)
1389 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1390 skb->len - csstart, 0);
1391
Patrick McHardy84fa7932006-08-29 16:44:56 -07001392 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001393 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Al Virod3bc23e2006-11-14 21:24:49 -08001395 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397}
1398
1399/**
1400 * skb_dequeue - remove from the head of the queue
1401 * @list: list to dequeue from
1402 *
1403 * Remove the head of the list. The list lock is taken so the function
1404 * may be used safely with other locking list functions. The head item is
1405 * returned or %NULL if the list is empty.
1406 */
1407
1408struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1409{
1410 unsigned long flags;
1411 struct sk_buff *result;
1412
1413 spin_lock_irqsave(&list->lock, flags);
1414 result = __skb_dequeue(list);
1415 spin_unlock_irqrestore(&list->lock, flags);
1416 return result;
1417}
1418
1419/**
1420 * skb_dequeue_tail - remove from the tail of the queue
1421 * @list: list to dequeue from
1422 *
1423 * Remove the tail of the list. The list lock is taken so the function
1424 * may be used safely with other locking list functions. The tail item is
1425 * returned or %NULL if the list is empty.
1426 */
1427struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1428{
1429 unsigned long flags;
1430 struct sk_buff *result;
1431
1432 spin_lock_irqsave(&list->lock, flags);
1433 result = __skb_dequeue_tail(list);
1434 spin_unlock_irqrestore(&list->lock, flags);
1435 return result;
1436}
1437
1438/**
1439 * skb_queue_purge - empty a list
1440 * @list: list to empty
1441 *
1442 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1443 * the list and one reference dropped. This function takes the list
1444 * lock and is atomic with respect to other list locking functions.
1445 */
1446void skb_queue_purge(struct sk_buff_head *list)
1447{
1448 struct sk_buff *skb;
1449 while ((skb = skb_dequeue(list)) != NULL)
1450 kfree_skb(skb);
1451}
1452
1453/**
1454 * skb_queue_head - queue a buffer at the list head
1455 * @list: list to use
1456 * @newsk: buffer to queue
1457 *
1458 * Queue a buffer at the start of the list. This function takes the
1459 * list lock and can be used safely with other locking &sk_buff functions
1460 * safely.
1461 *
1462 * A buffer cannot be placed on two lists at the same time.
1463 */
1464void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1465{
1466 unsigned long flags;
1467
1468 spin_lock_irqsave(&list->lock, flags);
1469 __skb_queue_head(list, newsk);
1470 spin_unlock_irqrestore(&list->lock, flags);
1471}
1472
1473/**
1474 * skb_queue_tail - queue a buffer at the list tail
1475 * @list: list to use
1476 * @newsk: buffer to queue
1477 *
1478 * Queue a buffer at the tail of the list. This function takes the
1479 * list lock and can be used safely with other locking &sk_buff functions
1480 * safely.
1481 *
1482 * A buffer cannot be placed on two lists at the same time.
1483 */
1484void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1485{
1486 unsigned long flags;
1487
1488 spin_lock_irqsave(&list->lock, flags);
1489 __skb_queue_tail(list, newsk);
1490 spin_unlock_irqrestore(&list->lock, flags);
1491}
David S. Miller8728b832005-08-09 19:25:21 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/**
1494 * skb_unlink - remove a buffer from a list
1495 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001496 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
David S. Miller8728b832005-08-09 19:25:21 -07001498 * Remove a packet from a list. The list locks are taken and this
1499 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
David S. Miller8728b832005-08-09 19:25:21 -07001501 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
David S. Miller8728b832005-08-09 19:25:21 -07001503void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
David S. Miller8728b832005-08-09 19:25:21 -07001505 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
David S. Miller8728b832005-08-09 19:25:21 -07001507 spin_lock_irqsave(&list->lock, flags);
1508 __skb_unlink(skb, list);
1509 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512/**
1513 * skb_append - append a buffer
1514 * @old: buffer to insert after
1515 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001516 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
1518 * Place a packet after a given packet in a list. The list locks are taken
1519 * and this function is atomic with respect to other list locked calls.
1520 * A buffer cannot be placed on two lists at the same time.
1521 */
David S. Miller8728b832005-08-09 19:25:21 -07001522void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 unsigned long flags;
1525
David S. Miller8728b832005-08-09 19:25:21 -07001526 spin_lock_irqsave(&list->lock, flags);
1527 __skb_append(old, newsk, list);
1528 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531
1532/**
1533 * skb_insert - insert a buffer
1534 * @old: buffer to insert before
1535 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001536 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
David S. Miller8728b832005-08-09 19:25:21 -07001538 * Place a packet before a given packet in a list. The list locks are
1539 * taken and this function is atomic with respect to other list locked
1540 * calls.
1541 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * A buffer cannot be placed on two lists at the same time.
1543 */
David S. Miller8728b832005-08-09 19:25:21 -07001544void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 unsigned long flags;
1547
David S. Miller8728b832005-08-09 19:25:21 -07001548 spin_lock_irqsave(&list->lock, flags);
1549 __skb_insert(newsk, old->prev, old, list);
1550 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553static inline void skb_split_inside_header(struct sk_buff *skb,
1554 struct sk_buff* skb1,
1555 const u32 len, const int pos)
1556{
1557 int i;
1558
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001559 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1560 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* And move data appendix as is. */
1562 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1563 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1564
1565 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1566 skb_shinfo(skb)->nr_frags = 0;
1567 skb1->data_len = skb->data_len;
1568 skb1->len += skb1->data_len;
1569 skb->data_len = 0;
1570 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001571 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574static inline void skb_split_no_header(struct sk_buff *skb,
1575 struct sk_buff* skb1,
1576 const u32 len, int pos)
1577{
1578 int i, k = 0;
1579 const int nfrags = skb_shinfo(skb)->nr_frags;
1580
1581 skb_shinfo(skb)->nr_frags = 0;
1582 skb1->len = skb1->data_len = skb->len - len;
1583 skb->len = len;
1584 skb->data_len = len - pos;
1585
1586 for (i = 0; i < nfrags; i++) {
1587 int size = skb_shinfo(skb)->frags[i].size;
1588
1589 if (pos + size > len) {
1590 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1591
1592 if (pos < len) {
1593 /* Split frag.
1594 * We have two variants in this case:
1595 * 1. Move all the frag to the second
1596 * part, if it is possible. F.e.
1597 * this approach is mandatory for TUX,
1598 * where splitting is expensive.
1599 * 2. Split is accurately. We make this.
1600 */
1601 get_page(skb_shinfo(skb)->frags[i].page);
1602 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1603 skb_shinfo(skb1)->frags[0].size -= len - pos;
1604 skb_shinfo(skb)->frags[i].size = len - pos;
1605 skb_shinfo(skb)->nr_frags++;
1606 }
1607 k++;
1608 } else
1609 skb_shinfo(skb)->nr_frags++;
1610 pos += size;
1611 }
1612 skb_shinfo(skb1)->nr_frags = k;
1613}
1614
1615/**
1616 * skb_split - Split fragmented skb to two parts at length len.
1617 * @skb: the buffer to split
1618 * @skb1: the buffer to receive the second part
1619 * @len: new length for skb
1620 */
1621void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1622{
1623 int pos = skb_headlen(skb);
1624
1625 if (len < pos) /* Split line is inside header. */
1626 skb_split_inside_header(skb, skb1, len, pos);
1627 else /* Second chunk has no header, nothing to copy. */
1628 skb_split_no_header(skb, skb1, len, pos);
1629}
1630
Thomas Graf677e90e2005-06-23 20:59:51 -07001631/**
1632 * skb_prepare_seq_read - Prepare a sequential read of skb data
1633 * @skb: the buffer to read
1634 * @from: lower offset of data to be read
1635 * @to: upper offset of data to be read
1636 * @st: state variable
1637 *
1638 * Initializes the specified state variable. Must be called before
1639 * invoking skb_seq_read() for the first time.
1640 */
1641void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1642 unsigned int to, struct skb_seq_state *st)
1643{
1644 st->lower_offset = from;
1645 st->upper_offset = to;
1646 st->root_skb = st->cur_skb = skb;
1647 st->frag_idx = st->stepped_offset = 0;
1648 st->frag_data = NULL;
1649}
1650
1651/**
1652 * skb_seq_read - Sequentially read skb data
1653 * @consumed: number of bytes consumed by the caller so far
1654 * @data: destination pointer for data to be returned
1655 * @st: state variable
1656 *
1657 * Reads a block of skb data at &consumed relative to the
1658 * lower offset specified to skb_prepare_seq_read(). Assigns
1659 * the head of the data block to &data and returns the length
1660 * of the block or 0 if the end of the skb data or the upper
1661 * offset has been reached.
1662 *
1663 * The caller is not required to consume all of the data
1664 * returned, i.e. &consumed is typically set to the number
1665 * of bytes already consumed and the next call to
1666 * skb_seq_read() will return the remaining part of the block.
1667 *
1668 * Note: The size of each block of data returned can be arbitary,
1669 * this limitation is the cost for zerocopy seqeuental
1670 * reads of potentially non linear data.
1671 *
1672 * Note: Fragment lists within fragments are not implemented
1673 * at the moment, state->root_skb could be replaced with
1674 * a stack for this purpose.
1675 */
1676unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1677 struct skb_seq_state *st)
1678{
1679 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1680 skb_frag_t *frag;
1681
1682 if (unlikely(abs_offset >= st->upper_offset))
1683 return 0;
1684
1685next_skb:
1686 block_limit = skb_headlen(st->cur_skb);
1687
1688 if (abs_offset < block_limit) {
1689 *data = st->cur_skb->data + abs_offset;
1690 return block_limit - abs_offset;
1691 }
1692
1693 if (st->frag_idx == 0 && !st->frag_data)
1694 st->stepped_offset += skb_headlen(st->cur_skb);
1695
1696 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1697 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1698 block_limit = frag->size + st->stepped_offset;
1699
1700 if (abs_offset < block_limit) {
1701 if (!st->frag_data)
1702 st->frag_data = kmap_skb_frag(frag);
1703
1704 *data = (u8 *) st->frag_data + frag->page_offset +
1705 (abs_offset - st->stepped_offset);
1706
1707 return block_limit - abs_offset;
1708 }
1709
1710 if (st->frag_data) {
1711 kunmap_skb_frag(st->frag_data);
1712 st->frag_data = NULL;
1713 }
1714
1715 st->frag_idx++;
1716 st->stepped_offset += frag->size;
1717 }
1718
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07001719 if (st->frag_data) {
1720 kunmap_skb_frag(st->frag_data);
1721 st->frag_data = NULL;
1722 }
1723
Thomas Graf677e90e2005-06-23 20:59:51 -07001724 if (st->cur_skb->next) {
1725 st->cur_skb = st->cur_skb->next;
1726 st->frag_idx = 0;
1727 goto next_skb;
1728 } else if (st->root_skb == st->cur_skb &&
1729 skb_shinfo(st->root_skb)->frag_list) {
1730 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1731 goto next_skb;
1732 }
1733
1734 return 0;
1735}
1736
1737/**
1738 * skb_abort_seq_read - Abort a sequential read of skb data
1739 * @st: state variable
1740 *
1741 * Must be called if skb_seq_read() was not called until it
1742 * returned 0.
1743 */
1744void skb_abort_seq_read(struct skb_seq_state *st)
1745{
1746 if (st->frag_data)
1747 kunmap_skb_frag(st->frag_data);
1748}
1749
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001750#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1751
1752static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1753 struct ts_config *conf,
1754 struct ts_state *state)
1755{
1756 return skb_seq_read(offset, text, TS_SKB_CB(state));
1757}
1758
1759static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1760{
1761 skb_abort_seq_read(TS_SKB_CB(state));
1762}
1763
1764/**
1765 * skb_find_text - Find a text pattern in skb data
1766 * @skb: the buffer to look in
1767 * @from: search offset
1768 * @to: search limit
1769 * @config: textsearch configuration
1770 * @state: uninitialized textsearch state variable
1771 *
1772 * Finds a pattern in the skb data according to the specified
1773 * textsearch configuration. Use textsearch_next() to retrieve
1774 * subsequent occurrences of the pattern. Returns the offset
1775 * to the first occurrence or UINT_MAX if no match was found.
1776 */
1777unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1778 unsigned int to, struct ts_config *config,
1779 struct ts_state *state)
1780{
Phil Oesterf72b9482006-06-26 00:00:57 -07001781 unsigned int ret;
1782
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001783 config->get_next_block = skb_ts_get_next_block;
1784 config->finish = skb_ts_finish;
1785
1786 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1787
Phil Oesterf72b9482006-06-26 00:00:57 -07001788 ret = textsearch_find(config, state);
1789 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001790}
1791
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001792/**
1793 * skb_append_datato_frags: - append the user data to a skb
1794 * @sk: sock structure
1795 * @skb: skb structure to be appened with user data.
1796 * @getfrag: call back function to be used for getting the user data
1797 * @from: pointer to user message iov
1798 * @length: length of the iov message
1799 *
1800 * Description: This procedure append the user data in the fragment part
1801 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1802 */
1803int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001804 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001805 int len, int odd, struct sk_buff *skb),
1806 void *from, int length)
1807{
1808 int frg_cnt = 0;
1809 skb_frag_t *frag = NULL;
1810 struct page *page = NULL;
1811 int copy, left;
1812 int offset = 0;
1813 int ret;
1814
1815 do {
1816 /* Return error if we don't have space for new frag */
1817 frg_cnt = skb_shinfo(skb)->nr_frags;
1818 if (frg_cnt >= MAX_SKB_FRAGS)
1819 return -EFAULT;
1820
1821 /* allocate a new page for next frag */
1822 page = alloc_pages(sk->sk_allocation, 0);
1823
1824 /* If alloc_page fails just return failure and caller will
1825 * free previous allocated pages by doing kfree_skb()
1826 */
1827 if (page == NULL)
1828 return -ENOMEM;
1829
1830 /* initialize the next frag */
1831 sk->sk_sndmsg_page = page;
1832 sk->sk_sndmsg_off = 0;
1833 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1834 skb->truesize += PAGE_SIZE;
1835 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1836
1837 /* get the new initialized frag */
1838 frg_cnt = skb_shinfo(skb)->nr_frags;
1839 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1840
1841 /* copy the user data to page */
1842 left = PAGE_SIZE - frag->page_offset;
1843 copy = (length > left)? left : length;
1844
1845 ret = getfrag(from, (page_address(frag->page) +
1846 frag->page_offset + frag->size),
1847 offset, copy, 0, skb);
1848 if (ret < 0)
1849 return -EFAULT;
1850
1851 /* copy was successful so update the size parameters */
1852 sk->sk_sndmsg_off += copy;
1853 frag->size += copy;
1854 skb->len += copy;
1855 skb->data_len += copy;
1856 offset += copy;
1857 length -= copy;
1858
1859 } while (length > 0);
1860
1861 return 0;
1862}
1863
Herbert Xucbb042f2006-03-20 22:43:56 -08001864/**
1865 * skb_pull_rcsum - pull skb and update receive checksum
1866 * @skb: buffer to update
1867 * @start: start of data before pull
1868 * @len: length of data pulled
1869 *
1870 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001871 * update the CHECKSUM_COMPLETE checksum. It should be used on
1872 * receive path processing instead of skb_pull unless you know
1873 * that the checksum difference is zero (e.g., a valid IP header)
1874 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001875 */
1876unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1877{
1878 BUG_ON(len > skb->len);
1879 skb->len -= len;
1880 BUG_ON(skb->len < skb->data_len);
1881 skb_postpull_rcsum(skb, skb->data, len);
1882 return skb->data += len;
1883}
1884
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001885EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1886
Herbert Xuf4c50d92006-06-22 03:02:40 -07001887/**
1888 * skb_segment - Perform protocol segmentation on skb.
1889 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001890 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001891 *
1892 * This function performs segmentation on the given skb. It returns
1893 * the segment at the given position. It returns NULL if there are
1894 * no more segments to generate, or when an error is encountered.
1895 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001896struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001897{
1898 struct sk_buff *segs = NULL;
1899 struct sk_buff *tail = NULL;
1900 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001901 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001902 unsigned int offset = doffset;
1903 unsigned int headroom;
1904 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001905 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001906 int nfrags = skb_shinfo(skb)->nr_frags;
1907 int err = -ENOMEM;
1908 int i = 0;
1909 int pos;
1910
1911 __skb_push(skb, doffset);
1912 headroom = skb_headroom(skb);
1913 pos = skb_headlen(skb);
1914
1915 do {
1916 struct sk_buff *nskb;
1917 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001918 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001919 int k;
1920 int size;
1921
1922 len = skb->len - offset;
1923 if (len > mss)
1924 len = mss;
1925
1926 hsize = skb_headlen(skb) - offset;
1927 if (hsize < 0)
1928 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001929 if (hsize > len || !sg)
1930 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001931
Herbert Xuc8884ed2006-10-29 15:59:41 -08001932 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001933 if (unlikely(!nskb))
1934 goto err;
1935
1936 if (segs)
1937 tail->next = nskb;
1938 else
1939 segs = nskb;
1940 tail = nskb;
1941
1942 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07001943 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001944 nskb->priority = skb->priority;
1945 nskb->protocol = skb->protocol;
1946 nskb->dst = dst_clone(skb->dst);
1947 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1948 nskb->pkt_type = skb->pkt_type;
1949 nskb->mac_len = skb->mac_len;
1950
1951 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001952 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001953 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001954 nskb->transport_header = (nskb->network_header +
1955 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001956 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1957 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001958 if (!sg) {
1959 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1960 skb_put(nskb, len),
1961 len, 0);
1962 continue;
1963 }
1964
1965 frag = skb_shinfo(nskb)->frags;
1966 k = 0;
1967
Patrick McHardy84fa7932006-08-29 16:44:56 -07001968 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001969 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001970 skb_copy_from_linear_data_offset(skb, offset,
1971 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001972
1973 while (pos < offset + len) {
1974 BUG_ON(i >= nfrags);
1975
1976 *frag = skb_shinfo(skb)->frags[i];
1977 get_page(frag->page);
1978 size = frag->size;
1979
1980 if (pos < offset) {
1981 frag->page_offset += offset - pos;
1982 frag->size -= offset - pos;
1983 }
1984
1985 k++;
1986
1987 if (pos + size <= offset + len) {
1988 i++;
1989 pos += size;
1990 } else {
1991 frag->size -= pos + size - (offset + len);
1992 break;
1993 }
1994
1995 frag++;
1996 }
1997
1998 skb_shinfo(nskb)->nr_frags = k;
1999 nskb->data_len = len - hsize;
2000 nskb->len += nskb->data_len;
2001 nskb->truesize += nskb->data_len;
2002 } while ((offset += len) < skb->len);
2003
2004 return segs;
2005
2006err:
2007 while ((skb = segs)) {
2008 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002009 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002010 }
2011 return ERR_PTR(err);
2012}
2013
2014EXPORT_SYMBOL_GPL(skb_segment);
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016void __init skb_init(void)
2017{
2018 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2019 sizeof(struct sk_buff),
2020 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002021 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002022 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002023 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2024 (2*sizeof(struct sk_buff)) +
2025 sizeof(atomic_t),
2026 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002027 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002028 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
David Howells716ea3a2007-04-02 20:19:53 -07002031/**
2032 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2033 * @skb: Socket buffer containing the buffers to be mapped
2034 * @sg: The scatter-gather list to map into
2035 * @offset: The offset into the buffer's contents to start mapping
2036 * @len: Length of buffer space to be mapped
2037 *
2038 * Fill the specified scatter-gather list with mappings/pointers into a
2039 * region of the buffer space attached to a socket buffer.
2040 */
2041int
2042skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2043{
David S. Miller1a028e52007-04-27 15:21:23 -07002044 int start = skb_headlen(skb);
2045 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002046 int elt = 0;
2047
2048 if (copy > 0) {
2049 if (copy > len)
2050 copy = len;
2051 sg[elt].page = virt_to_page(skb->data + offset);
2052 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2053 sg[elt].length = copy;
2054 elt++;
2055 if ((len -= copy) == 0)
2056 return elt;
2057 offset += copy;
2058 }
2059
2060 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002061 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002062
David S. Miller1a028e52007-04-27 15:21:23 -07002063 BUG_TRAP(start <= offset + len);
2064
2065 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002066 if ((copy = end - offset) > 0) {
2067 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2068
2069 if (copy > len)
2070 copy = len;
2071 sg[elt].page = frag->page;
David S. Miller1a028e52007-04-27 15:21:23 -07002072 sg[elt].offset = frag->page_offset+offset-start;
David Howells716ea3a2007-04-02 20:19:53 -07002073 sg[elt].length = copy;
2074 elt++;
2075 if (!(len -= copy))
2076 return elt;
2077 offset += copy;
2078 }
David S. Miller1a028e52007-04-27 15:21:23 -07002079 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002080 }
2081
2082 if (skb_shinfo(skb)->frag_list) {
2083 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2084
2085 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002086 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002087
David S. Miller1a028e52007-04-27 15:21:23 -07002088 BUG_TRAP(start <= offset + len);
2089
2090 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002091 if ((copy = end - offset) > 0) {
2092 if (copy > len)
2093 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07002094 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002095 if ((len -= copy) == 0)
2096 return elt;
2097 offset += copy;
2098 }
David S. Miller1a028e52007-04-27 15:21:23 -07002099 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002100 }
2101 }
2102 BUG_ON(len);
2103 return elt;
2104}
2105
2106/**
2107 * skb_cow_data - Check that a socket buffer's data buffers are writable
2108 * @skb: The socket buffer to check.
2109 * @tailbits: Amount of trailing space to be added
2110 * @trailer: Returned pointer to the skb where the @tailbits space begins
2111 *
2112 * Make sure that the data buffers attached to a socket buffer are
2113 * writable. If they are not, private copies are made of the data buffers
2114 * and the socket buffer is set to use these instead.
2115 *
2116 * If @tailbits is given, make sure that there is space to write @tailbits
2117 * bytes of data beyond current end of socket buffer. @trailer will be
2118 * set to point to the skb in which this space begins.
2119 *
2120 * The number of scatterlist elements required to completely map the
2121 * COW'd and extended socket buffer will be returned.
2122 */
2123int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2124{
2125 int copyflag;
2126 int elt;
2127 struct sk_buff *skb1, **skb_p;
2128
2129 /* If skb is cloned or its head is paged, reallocate
2130 * head pulling out all the pages (pages are considered not writable
2131 * at the moment even if they are anonymous).
2132 */
2133 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2134 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2135 return -ENOMEM;
2136
2137 /* Easy case. Most of packets will go this way. */
2138 if (!skb_shinfo(skb)->frag_list) {
2139 /* A little of trouble, not enough of space for trailer.
2140 * This should not happen, when stack is tuned to generate
2141 * good frames. OK, on miss we reallocate and reserve even more
2142 * space, 128 bytes is fair. */
2143
2144 if (skb_tailroom(skb) < tailbits &&
2145 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2146 return -ENOMEM;
2147
2148 /* Voila! */
2149 *trailer = skb;
2150 return 1;
2151 }
2152
2153 /* Misery. We are in troubles, going to mincer fragments... */
2154
2155 elt = 1;
2156 skb_p = &skb_shinfo(skb)->frag_list;
2157 copyflag = 0;
2158
2159 while ((skb1 = *skb_p) != NULL) {
2160 int ntail = 0;
2161
2162 /* The fragment is partially pulled by someone,
2163 * this can happen on input. Copy it and everything
2164 * after it. */
2165
2166 if (skb_shared(skb1))
2167 copyflag = 1;
2168
2169 /* If the skb is the last, worry about trailer. */
2170
2171 if (skb1->next == NULL && tailbits) {
2172 if (skb_shinfo(skb1)->nr_frags ||
2173 skb_shinfo(skb1)->frag_list ||
2174 skb_tailroom(skb1) < tailbits)
2175 ntail = tailbits + 128;
2176 }
2177
2178 if (copyflag ||
2179 skb_cloned(skb1) ||
2180 ntail ||
2181 skb_shinfo(skb1)->nr_frags ||
2182 skb_shinfo(skb1)->frag_list) {
2183 struct sk_buff *skb2;
2184
2185 /* Fuck, we are miserable poor guys... */
2186 if (ntail == 0)
2187 skb2 = skb_copy(skb1, GFP_ATOMIC);
2188 else
2189 skb2 = skb_copy_expand(skb1,
2190 skb_headroom(skb1),
2191 ntail,
2192 GFP_ATOMIC);
2193 if (unlikely(skb2 == NULL))
2194 return -ENOMEM;
2195
2196 if (skb1->sk)
2197 skb_set_owner_w(skb2, skb1->sk);
2198
2199 /* Looking around. Are we still alive?
2200 * OK, link new skb, drop old one */
2201
2202 skb2->next = skb1->next;
2203 *skb_p = skb2;
2204 kfree_skb(skb1);
2205 skb1 = skb2;
2206 }
2207 elt++;
2208 *trailer = skb1;
2209 skb_p = &skb1->next;
2210 }
2211
2212 return elt;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215EXPORT_SYMBOL(___pskb_trim);
2216EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002217EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002219EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002220EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221EXPORT_SYMBOL(pskb_copy);
2222EXPORT_SYMBOL(pskb_expand_head);
2223EXPORT_SYMBOL(skb_checksum);
2224EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225EXPORT_SYMBOL(skb_copy);
2226EXPORT_SYMBOL(skb_copy_and_csum_bits);
2227EXPORT_SYMBOL(skb_copy_and_csum_dev);
2228EXPORT_SYMBOL(skb_copy_bits);
2229EXPORT_SYMBOL(skb_copy_expand);
2230EXPORT_SYMBOL(skb_over_panic);
2231EXPORT_SYMBOL(skb_pad);
2232EXPORT_SYMBOL(skb_realloc_headroom);
2233EXPORT_SYMBOL(skb_under_panic);
2234EXPORT_SYMBOL(skb_dequeue);
2235EXPORT_SYMBOL(skb_dequeue_tail);
2236EXPORT_SYMBOL(skb_insert);
2237EXPORT_SYMBOL(skb_queue_purge);
2238EXPORT_SYMBOL(skb_queue_head);
2239EXPORT_SYMBOL(skb_queue_tail);
2240EXPORT_SYMBOL(skb_unlink);
2241EXPORT_SYMBOL(skb_append);
2242EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002243EXPORT_SYMBOL(skb_prepare_seq_read);
2244EXPORT_SYMBOL(skb_seq_read);
2245EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002246EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002247EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002248
2249EXPORT_SYMBOL_GPL(skb_to_sgvec);
2250EXPORT_SYMBOL_GPL(skb_cow_data);