blob: 64caee46291b74f1c1ea2c44cad2d20442c02ae4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <net/protocol.h>
60#include <net/dst.h>
61#include <net/sock.h>
62#include <net/checksum.h>
63#include <net/xfrm.h>
64
65#include <asm/uaccess.h>
66#include <asm/system.h>
67
Al Viroa1f8e7f72006-10-19 16:08:53 -040068#include "kmap_skb.h"
69
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *skbuff_head_cache __read_mostly;
71static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070090 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070091 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070092 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -070093 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
Patrick McHardy26095452005-04-21 16:43:02 -0700108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700109 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700110 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700111 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700112 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 BUG();
114}
115
David S. Millerdc6de332006-04-20 00:10:50 -0700116void skb_truesize_bug(struct sk_buff *skb)
117{
118 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
119 "len=%u, sizeof(sk_buff)=%Zd\n",
120 skb->truesize, skb->len, sizeof(struct sk_buff));
121}
122EXPORT_SYMBOL(skb_truesize_bug);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Allocate a new skbuff. We do this ourselves so we can fill in a few
125 * 'private' fields and also do memory statistics to find all the
126 * [BEEP] leaks.
127 *
128 */
129
130/**
David S. Millerd179cd12005-08-17 14:57:30 -0700131 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * @size: size to allocate
133 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700134 * @fclone: allocate from fclone cache instead of head cache
135 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800136 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * Allocate a new &sk_buff. The returned buffer has no headroom and a
139 * tail room of size bytes. The object has a reference count of one.
140 * The return is the buffer. On a failure the return is %NULL.
141 *
142 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 * %GFP_ATOMIC.
144 */
Al Virodd0fc662005-10-07 07:46:04 +0100145struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800146 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Christoph Lametere18b8902006-12-06 20:33:20 -0800148 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800149 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct sk_buff *skb;
151 u8 *data;
152
Herbert Xu8798b3f2006-01-23 16:32:45 -0800153 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800156 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!skb)
158 goto out;
159
160 /* Get the DATA. Size must match skb_add_mtu(). */
161 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;
307 };
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 }
329#ifdef CONFIG_NETFILTER
330 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800331#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
332 nf_conntrack_put_reasm(skb->nfct_reasm);
333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef CONFIG_BRIDGE_NETFILTER
335 nf_bridge_put(skb->nf_bridge);
336#endif
337#endif
338/* XXX: IS this still necessary? - JHS */
339#ifdef CONFIG_NET_SCHED
340 skb->tc_index = 0;
341#ifdef CONFIG_NET_CLS_ACT
342 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#endif
344#endif
345
346 kfree_skbmem(skb);
347}
348
349/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800350 * kfree_skb - free an sk_buff
351 * @skb: buffer to free
352 *
353 * Drop a reference to the buffer and free it if the usage count has
354 * hit zero.
355 */
356void kfree_skb(struct sk_buff *skb)
357{
358 if (unlikely(!skb))
359 return;
360 if (likely(atomic_read(&skb->users) == 1))
361 smp_rmb();
362 else if (likely(!atomic_dec_and_test(&skb->users)))
363 return;
364 __kfree_skb(skb);
365}
366
367/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * skb_clone - duplicate an sk_buff
369 * @skb: buffer to clone
370 * @gfp_mask: allocation priority
371 *
372 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
373 * copies share the same packet data but not structure. The new
374 * buffer has a reference count of 1. If the allocation fails the
375 * function returns %NULL otherwise the new buffer is returned.
376 *
377 * If this function is called from an interrupt gfp_mask() must be
378 * %GFP_ATOMIC.
379 */
380
Al Virodd0fc662005-10-07 07:46:04 +0100381struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
David S. Millerd179cd12005-08-17 14:57:30 -0700383 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Millerd179cd12005-08-17 14:57:30 -0700385 n = skb + 1;
386 if (skb->fclone == SKB_FCLONE_ORIG &&
387 n->fclone == SKB_FCLONE_UNAVAILABLE) {
388 atomic_t *fclone_ref = (atomic_t *) (n + 1);
389 n->fclone = SKB_FCLONE_CLONE;
390 atomic_inc(fclone_ref);
391 } else {
392 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
393 if (!n)
394 return NULL;
395 n->fclone = SKB_FCLONE_UNAVAILABLE;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398#define C(x) n->x = skb->x
399
400 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700402 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 C(dev);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700404 C(transport_header);
405 C(network_header);
406 C(mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 C(dst);
408 dst_clone(skb->dst);
409 C(sp);
410#ifdef CONFIG_INET
411 secpath_get(skb->sp);
412#endif
413 memcpy(n->cb, skb->cb, sizeof(skb->cb));
414 C(len);
415 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700416 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 C(csum);
418 C(local_df);
419 n->cloned = 1;
420 n->nohdr = 0;
421 C(pkt_type);
422 C(ip_summed);
423 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#ifdef CONFIG_NET_SCHED
432 C(tc_index);
433#ifdef CONFIG_NET_CLS_ACT
434 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700435 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
436 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700437 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
James Morris984bc162006-06-09 00:29:17 -0700439 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#endif
441 C(truesize);
442 atomic_set(&n->users, 1);
443 C(head);
444 C(data);
445 C(tail);
446 C(end);
447
448 atomic_inc(&(skb_shinfo(skb)->dataref));
449 skb->cloned = 1;
450
451 return n;
452}
453
454static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
455{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700456#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * Shift between the two data areas in bytes
459 */
460 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 new->sk = NULL;
463 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 new->priority = old->priority;
465 new->protocol = old->protocol;
466 new->dst = dst_clone(old->dst);
467#ifdef CONFIG_INET
468 new->sp = secpath_get(old->sp);
469#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700470 new->transport_header = old->transport_header;
471 new->network_header = old->network_header;
472 new->mac_header = old->mac_header;
473#ifndef NET_SKBUFF_DATA_USES_OFFSET
474 /* {transport,network,mac}_header are relative to skb->head */
475 new->transport_header += offset;
476 new->network_header += offset;
477 new->mac_header += offset;
478#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 memcpy(new->cb, old->cb, sizeof(old->cb));
480 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700481 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700483 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800485 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700486 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300487#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
488 new->ipvs_property = old->ipvs_property;
489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#ifdef CONFIG_NET_SCHED
491#ifdef CONFIG_NET_CLS_ACT
492 new->tc_verd = old->tc_verd;
493#endif
494 new->tc_index = old->tc_index;
495#endif
James Morris984bc162006-06-09 00:29:17 -0700496 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700498 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
499 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
500 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/**
504 * skb_copy - create private copy of an sk_buff
505 * @skb: buffer to copy
506 * @gfp_mask: allocation priority
507 *
508 * Make a copy of both an &sk_buff and its data. This is used when the
509 * caller wishes to modify the data and needs a private copy of the
510 * data to alter. Returns %NULL on failure or the pointer to the buffer
511 * on success. The returned buffer has a reference count of 1.
512 *
513 * As by-product this function converts non-linear &sk_buff to linear
514 * one, so that &sk_buff becomes completely private and caller is allowed
515 * to modify all the data of returned buffer. This means that this
516 * function is not recommended for use in circumstances when only
517 * header is going to be modified. Use pskb_copy() instead.
518 */
519
Al Virodd0fc662005-10-07 07:46:04 +0100520struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int headerlen = skb->data - skb->head;
523 /*
524 * Allocate the copy buffer
525 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700526 struct sk_buff *n;
527#ifdef NET_SKBUFF_DATA_USES_OFFSET
528 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
529#else
530 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
531#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!n)
533 return NULL;
534
535 /* Set the data pointer */
536 skb_reserve(n, headerlen);
537 /* Set the tail pointer and length */
538 skb_put(n, skb->len);
539 n->csum = skb->csum;
540 n->ip_summed = skb->ip_summed;
541
542 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
543 BUG();
544
545 copy_skb_header(n, skb);
546 return n;
547}
548
549
550/**
551 * pskb_copy - create copy of an sk_buff with private head.
552 * @skb: buffer to copy
553 * @gfp_mask: allocation priority
554 *
555 * Make a copy of both an &sk_buff and part of its data, located
556 * in header. Fragmented data remain shared. This is used when
557 * the caller wishes to modify only header of &sk_buff and needs
558 * private copy of the header to alter. Returns %NULL on failure
559 * or the pointer to the buffer on success.
560 * The returned buffer has a reference count of 1.
561 */
562
Al Virodd0fc662005-10-07 07:46:04 +0100563struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 /*
566 * Allocate the copy buffer
567 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700568 struct sk_buff *n;
569#ifdef NET_SKBUFF_DATA_USES_OFFSET
570 n = alloc_skb(skb->end, gfp_mask);
571#else
572 n = alloc_skb(skb->end - skb->head, gfp_mask);
573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!n)
575 goto out;
576
577 /* Set the data pointer */
578 skb_reserve(n, skb->data - skb->head);
579 /* Set the tail pointer and length */
580 skb_put(n, skb_headlen(skb));
581 /* Copy the bytes */
582 memcpy(n->data, skb->data, n->len);
583 n->csum = skb->csum;
584 n->ip_summed = skb->ip_summed;
585
Herbert Xu25f484a2006-11-07 14:57:15 -0800586 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 n->data_len = skb->data_len;
588 n->len = skb->len;
589
590 if (skb_shinfo(skb)->nr_frags) {
591 int i;
592
593 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
594 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
595 get_page(skb_shinfo(n)->frags[i].page);
596 }
597 skb_shinfo(n)->nr_frags = i;
598 }
599
600 if (skb_shinfo(skb)->frag_list) {
601 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
602 skb_clone_fraglist(n);
603 }
604
605 copy_skb_header(n, skb);
606out:
607 return n;
608}
609
610/**
611 * pskb_expand_head - reallocate header of &sk_buff
612 * @skb: buffer to reallocate
613 * @nhead: room to add at head
614 * @ntail: room to add at tail
615 * @gfp_mask: allocation priority
616 *
617 * Expands (or creates identical copy, if &nhead and &ntail are zero)
618 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
619 * reference count of 1. Returns zero in the case of success or error,
620 * if expansion failed. In the last case, &sk_buff is not changed.
621 *
622 * All the pointers pointing into skb header may change and must be
623 * reloaded after call to this function.
624 */
625
Victor Fusco86a76ca2005-07-08 14:57:47 -0700626int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100627 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 int i;
630 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700631#ifdef NET_SKBUFF_DATA_USES_OFFSET
632 int size = nhead + skb->end + ntail;
633#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 long off;
637
638 if (skb_shared(skb))
639 BUG();
640
641 size = SKB_DATA_ALIGN(size);
642
643 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
644 if (!data)
645 goto nodata;
646
647 /* Copy only real data... and, alas, header. This should be
648 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700649 memcpy(data + nhead, skb->head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700650#ifdef NET_SKBUFF_DATA_USES_OFFSET
651 skb->tail);
652#else
653 skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700654#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700655 memcpy(data + size, skb_end_pointer(skb),
656 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
659 get_page(skb_shinfo(skb)->frags[i].page);
660
661 if (skb_shinfo(skb)->frag_list)
662 skb_clone_fraglist(skb);
663
664 skb_release_data(skb);
665
666 off = (data + nhead) - skb->head;
667
668 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700670#ifdef NET_SKBUFF_DATA_USES_OFFSET
671 skb->end = size;
672#else
673 skb->end = skb->head + size;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700674 /* {transport,network,mac}_header and tail are relative to skb->head */
675 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700676 skb->transport_header += off;
677 skb->network_header += off;
678 skb->mac_header += off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 skb->cloned = 0;
681 skb->nohdr = 0;
682 atomic_set(&skb_shinfo(skb)->dataref, 1);
683 return 0;
684
685nodata:
686 return -ENOMEM;
687}
688
689/* Make private copy of skb with writable head and some headroom */
690
691struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
692{
693 struct sk_buff *skb2;
694 int delta = headroom - skb_headroom(skb);
695
696 if (delta <= 0)
697 skb2 = pskb_copy(skb, GFP_ATOMIC);
698 else {
699 skb2 = skb_clone(skb, GFP_ATOMIC);
700 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
701 GFP_ATOMIC)) {
702 kfree_skb(skb2);
703 skb2 = NULL;
704 }
705 }
706 return skb2;
707}
708
709
710/**
711 * skb_copy_expand - copy and expand sk_buff
712 * @skb: buffer to copy
713 * @newheadroom: new free bytes at head
714 * @newtailroom: new free bytes at tail
715 * @gfp_mask: allocation priority
716 *
717 * Make a copy of both an &sk_buff and its data and while doing so
718 * allocate additional space.
719 *
720 * This is used when the caller wishes to modify the data and needs a
721 * private copy of the data to alter as well as more space for new fields.
722 * Returns %NULL on failure or the pointer to the buffer
723 * on success. The returned buffer has a reference count of 1.
724 *
725 * You must pass %GFP_ATOMIC as the allocation priority if this function
726 * is called from an interrupt.
727 *
728 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
729 * only by netfilter in the cases when checksum is recalculated? --ANK
730 */
731struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700732 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100733 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 /*
736 * Allocate the copy buffer
737 */
738 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
739 gfp_mask);
740 int head_copy_len, head_copy_off;
741
742 if (!n)
743 return NULL;
744
745 skb_reserve(n, newheadroom);
746
747 /* Set the tail pointer and length */
748 skb_put(n, skb->len);
749
750 head_copy_len = skb_headroom(skb);
751 head_copy_off = 0;
752 if (newheadroom <= head_copy_len)
753 head_copy_len = newheadroom;
754 else
755 head_copy_off = newheadroom - head_copy_len;
756
757 /* Copy the linear header and data. */
758 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
759 skb->len + head_copy_len))
760 BUG();
761
762 copy_skb_header(n, skb);
763
764 return n;
765}
766
767/**
768 * skb_pad - zero pad the tail of an skb
769 * @skb: buffer to pad
770 * @pad: space to pad
771 *
772 * Ensure that a buffer is followed by a padding area that is zero
773 * filled. Used by network drivers which may DMA or transfer data
774 * beyond the buffer end onto the wire.
775 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700776 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900778
Herbert Xu5b057c62006-06-23 02:06:41 -0700779int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Herbert Xu5b057c62006-06-23 02:06:41 -0700781 int err;
782 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700785 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700789
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700790 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700791 if (likely(skb_cloned(skb) || ntail > 0)) {
792 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
793 if (unlikely(err))
794 goto free_skb;
795 }
796
797 /* FIXME: The use of this function with non-linear skb's really needs
798 * to be audited.
799 */
800 err = skb_linearize(skb);
801 if (unlikely(err))
802 goto free_skb;
803
804 memset(skb->data + skb->len, 0, pad);
805 return 0;
806
807free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700809 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900810}
811
Herbert Xu3cc0e872006-06-09 16:13:38 -0700812/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814
Herbert Xu3cc0e872006-06-09 16:13:38 -0700815int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Herbert Xu27b437c2006-07-13 19:26:39 -0700817 struct sk_buff **fragp;
818 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int offset = skb_headlen(skb);
820 int nfrags = skb_shinfo(skb)->nr_frags;
821 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700822 int err;
823
824 if (skb_cloned(skb) &&
825 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700828 i = 0;
829 if (offset >= len)
830 goto drop_pages;
831
832 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700834
835 if (end < len) {
836 offset = end;
837 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700839
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700840 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700841
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700842drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700843 skb_shinfo(skb)->nr_frags = i;
844
845 for (; i < nfrags; i++)
846 put_page(skb_shinfo(skb)->frags[i].page);
847
848 if (skb_shinfo(skb)->frag_list)
849 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700850 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Herbert Xu27b437c2006-07-13 19:26:39 -0700853 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
854 fragp = &frag->next) {
855 int end = offset + frag->len;
856
857 if (skb_shared(frag)) {
858 struct sk_buff *nfrag;
859
860 nfrag = skb_clone(frag, GFP_ATOMIC);
861 if (unlikely(!nfrag))
862 return -ENOMEM;
863
864 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700865 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700866 frag = nfrag;
867 *fragp = frag;
868 }
869
870 if (end < len) {
871 offset = end;
872 continue;
873 }
874
875 if (end > len &&
876 unlikely((err = pskb_trim(frag, len - offset))))
877 return err;
878
879 if (frag->next)
880 skb_drop_list(&frag->next);
881 break;
882 }
883
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700884done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700885 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 skb->data_len -= skb->len - len;
887 skb->len = len;
888 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700889 skb->len = len;
890 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700891 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 return 0;
895}
896
897/**
898 * __pskb_pull_tail - advance tail of skb header
899 * @skb: buffer to reallocate
900 * @delta: number of bytes to advance tail
901 *
902 * The function makes a sense only on a fragmented &sk_buff,
903 * it expands header moving its tail forward and copying necessary
904 * data from fragmented part.
905 *
906 * &sk_buff MUST have reference count of 1.
907 *
908 * Returns %NULL (and &sk_buff does not change) if pull failed
909 * or value of new tail of skb in the case of success.
910 *
911 * All the pointers pointing into skb header may change and must be
912 * reloaded after call to this function.
913 */
914
915/* Moves tail of skb head forward, copying data from fragmented part,
916 * when it is necessary.
917 * 1. It may fail due to malloc failure.
918 * 2. It may change skb pointers.
919 *
920 * It is pretty complicated. Luckily, it is called only in exceptional cases.
921 */
922unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
923{
924 /* If skb has not enough free space at tail, get new one
925 * plus 128 bytes for future expansions. If we have enough
926 * room at tail, reallocate without expansion only if skb is cloned.
927 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700928 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (eat > 0 || skb_cloned(skb)) {
931 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
932 GFP_ATOMIC))
933 return NULL;
934 }
935
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700936 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 BUG();
938
939 /* Optimization: no fragments, no reasons to preestimate
940 * size of pulled pages. Superb.
941 */
942 if (!skb_shinfo(skb)->frag_list)
943 goto pull_pages;
944
945 /* Estimate size of pulled pages. */
946 eat = delta;
947 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
948 if (skb_shinfo(skb)->frags[i].size >= eat)
949 goto pull_pages;
950 eat -= skb_shinfo(skb)->frags[i].size;
951 }
952
953 /* If we need update frag list, we are in troubles.
954 * Certainly, it possible to add an offset to skb data,
955 * but taking into account that pulling is expected to
956 * be very rare operation, it is worth to fight against
957 * further bloating skb head and crucify ourselves here instead.
958 * Pure masohism, indeed. 8)8)
959 */
960 if (eat) {
961 struct sk_buff *list = skb_shinfo(skb)->frag_list;
962 struct sk_buff *clone = NULL;
963 struct sk_buff *insp = NULL;
964
965 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800966 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (list->len <= eat) {
969 /* Eaten as whole. */
970 eat -= list->len;
971 list = list->next;
972 insp = list;
973 } else {
974 /* Eaten partially. */
975
976 if (skb_shared(list)) {
977 /* Sucks! We need to fork list. :-( */
978 clone = skb_clone(list, GFP_ATOMIC);
979 if (!clone)
980 return NULL;
981 insp = list->next;
982 list = clone;
983 } else {
984 /* This may be pulled without
985 * problems. */
986 insp = list;
987 }
988 if (!pskb_pull(list, eat)) {
989 if (clone)
990 kfree_skb(clone);
991 return NULL;
992 }
993 break;
994 }
995 } while (eat);
996
997 /* Free pulled out fragments. */
998 while ((list = skb_shinfo(skb)->frag_list) != insp) {
999 skb_shinfo(skb)->frag_list = list->next;
1000 kfree_skb(list);
1001 }
1002 /* And insert new clone at head. */
1003 if (clone) {
1004 clone->next = list;
1005 skb_shinfo(skb)->frag_list = clone;
1006 }
1007 }
1008 /* Success! Now we may commit changes to skb data. */
1009
1010pull_pages:
1011 eat = delta;
1012 k = 0;
1013 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1014 if (skb_shinfo(skb)->frags[i].size <= eat) {
1015 put_page(skb_shinfo(skb)->frags[i].page);
1016 eat -= skb_shinfo(skb)->frags[i].size;
1017 } else {
1018 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1019 if (eat) {
1020 skb_shinfo(skb)->frags[k].page_offset += eat;
1021 skb_shinfo(skb)->frags[k].size -= eat;
1022 eat = 0;
1023 }
1024 k++;
1025 }
1026 }
1027 skb_shinfo(skb)->nr_frags = k;
1028
1029 skb->tail += delta;
1030 skb->data_len -= delta;
1031
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001032 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/* Copy some data bits from skb to kernel buffer. */
1036
1037int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1038{
1039 int i, copy;
1040 int start = skb_headlen(skb);
1041
1042 if (offset > (int)skb->len - len)
1043 goto fault;
1044
1045 /* Copy header. */
1046 if ((copy = start - offset) > 0) {
1047 if (copy > len)
1048 copy = len;
1049 memcpy(to, skb->data + offset, copy);
1050 if ((len -= copy) == 0)
1051 return 0;
1052 offset += copy;
1053 to += copy;
1054 }
1055
1056 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1057 int end;
1058
1059 BUG_TRAP(start <= offset + len);
1060
1061 end = start + skb_shinfo(skb)->frags[i].size;
1062 if ((copy = end - offset) > 0) {
1063 u8 *vaddr;
1064
1065 if (copy > len)
1066 copy = len;
1067
1068 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1069 memcpy(to,
1070 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1071 offset - start, copy);
1072 kunmap_skb_frag(vaddr);
1073
1074 if ((len -= copy) == 0)
1075 return 0;
1076 offset += copy;
1077 to += copy;
1078 }
1079 start = end;
1080 }
1081
1082 if (skb_shinfo(skb)->frag_list) {
1083 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1084
1085 for (; list; list = list->next) {
1086 int end;
1087
1088 BUG_TRAP(start <= offset + len);
1089
1090 end = start + list->len;
1091 if ((copy = end - offset) > 0) {
1092 if (copy > len)
1093 copy = len;
1094 if (skb_copy_bits(list, offset - start,
1095 to, copy))
1096 goto fault;
1097 if ((len -= copy) == 0)
1098 return 0;
1099 offset += copy;
1100 to += copy;
1101 }
1102 start = end;
1103 }
1104 }
1105 if (!len)
1106 return 0;
1107
1108fault:
1109 return -EFAULT;
1110}
1111
Herbert Xu357b40a2005-04-19 22:30:14 -07001112/**
1113 * skb_store_bits - store bits from kernel buffer to skb
1114 * @skb: destination buffer
1115 * @offset: offset in destination
1116 * @from: source buffer
1117 * @len: number of bytes to copy
1118 *
1119 * Copy the specified number of bytes from the source buffer to the
1120 * destination skb. This function handles all the messy bits of
1121 * traversing fragment lists and such.
1122 */
1123
1124int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1125{
1126 int i, copy;
1127 int start = skb_headlen(skb);
1128
1129 if (offset > (int)skb->len - len)
1130 goto fault;
1131
1132 if ((copy = start - offset) > 0) {
1133 if (copy > len)
1134 copy = len;
1135 memcpy(skb->data + offset, from, copy);
1136 if ((len -= copy) == 0)
1137 return 0;
1138 offset += copy;
1139 from += copy;
1140 }
1141
1142 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1143 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1144 int end;
1145
1146 BUG_TRAP(start <= offset + len);
1147
1148 end = start + frag->size;
1149 if ((copy = end - offset) > 0) {
1150 u8 *vaddr;
1151
1152 if (copy > len)
1153 copy = len;
1154
1155 vaddr = kmap_skb_frag(frag);
1156 memcpy(vaddr + frag->page_offset + offset - start,
1157 from, copy);
1158 kunmap_skb_frag(vaddr);
1159
1160 if ((len -= copy) == 0)
1161 return 0;
1162 offset += copy;
1163 from += copy;
1164 }
1165 start = end;
1166 }
1167
1168 if (skb_shinfo(skb)->frag_list) {
1169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1170
1171 for (; list; list = list->next) {
1172 int end;
1173
1174 BUG_TRAP(start <= offset + len);
1175
1176 end = start + list->len;
1177 if ((copy = end - offset) > 0) {
1178 if (copy > len)
1179 copy = len;
1180 if (skb_store_bits(list, offset - start,
1181 from, copy))
1182 goto fault;
1183 if ((len -= copy) == 0)
1184 return 0;
1185 offset += copy;
1186 from += copy;
1187 }
1188 start = end;
1189 }
1190 }
1191 if (!len)
1192 return 0;
1193
1194fault:
1195 return -EFAULT;
1196}
1197
1198EXPORT_SYMBOL(skb_store_bits);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/* Checksum skb data. */
1201
Al Viro2bbbc862006-11-14 21:37:14 -08001202__wsum skb_checksum(const struct sk_buff *skb, int offset,
1203 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 int start = skb_headlen(skb);
1206 int i, copy = start - offset;
1207 int pos = 0;
1208
1209 /* Checksum header. */
1210 if (copy > 0) {
1211 if (copy > len)
1212 copy = len;
1213 csum = csum_partial(skb->data + offset, copy, csum);
1214 if ((len -= copy) == 0)
1215 return csum;
1216 offset += copy;
1217 pos = copy;
1218 }
1219
1220 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1221 int end;
1222
1223 BUG_TRAP(start <= offset + len);
1224
1225 end = start + skb_shinfo(skb)->frags[i].size;
1226 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001227 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 u8 *vaddr;
1229 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1230
1231 if (copy > len)
1232 copy = len;
1233 vaddr = kmap_skb_frag(frag);
1234 csum2 = csum_partial(vaddr + frag->page_offset +
1235 offset - start, copy, 0);
1236 kunmap_skb_frag(vaddr);
1237 csum = csum_block_add(csum, csum2, pos);
1238 if (!(len -= copy))
1239 return csum;
1240 offset += copy;
1241 pos += copy;
1242 }
1243 start = end;
1244 }
1245
1246 if (skb_shinfo(skb)->frag_list) {
1247 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1248
1249 for (; list; list = list->next) {
1250 int end;
1251
1252 BUG_TRAP(start <= offset + len);
1253
1254 end = start + list->len;
1255 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001256 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (copy > len)
1258 copy = len;
1259 csum2 = skb_checksum(list, offset - start,
1260 copy, 0);
1261 csum = csum_block_add(csum, csum2, pos);
1262 if ((len -= copy) == 0)
1263 return csum;
1264 offset += copy;
1265 pos += copy;
1266 }
1267 start = end;
1268 }
1269 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001270 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 return csum;
1273}
1274
1275/* Both of above in one bottle. */
1276
Al Viro81d77662006-11-14 21:37:33 -08001277__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1278 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int start = skb_headlen(skb);
1281 int i, copy = start - offset;
1282 int pos = 0;
1283
1284 /* Copy header. */
1285 if (copy > 0) {
1286 if (copy > len)
1287 copy = len;
1288 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1289 copy, csum);
1290 if ((len -= copy) == 0)
1291 return csum;
1292 offset += copy;
1293 to += copy;
1294 pos = copy;
1295 }
1296
1297 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1298 int end;
1299
1300 BUG_TRAP(start <= offset + len);
1301
1302 end = start + skb_shinfo(skb)->frags[i].size;
1303 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001304 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 u8 *vaddr;
1306 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1307
1308 if (copy > len)
1309 copy = len;
1310 vaddr = kmap_skb_frag(frag);
1311 csum2 = csum_partial_copy_nocheck(vaddr +
1312 frag->page_offset +
1313 offset - start, to,
1314 copy, 0);
1315 kunmap_skb_frag(vaddr);
1316 csum = csum_block_add(csum, csum2, pos);
1317 if (!(len -= copy))
1318 return csum;
1319 offset += copy;
1320 to += copy;
1321 pos += copy;
1322 }
1323 start = end;
1324 }
1325
1326 if (skb_shinfo(skb)->frag_list) {
1327 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1328
1329 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001330 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int end;
1332
1333 BUG_TRAP(start <= offset + len);
1334
1335 end = start + list->len;
1336 if ((copy = end - offset) > 0) {
1337 if (copy > len)
1338 copy = len;
1339 csum2 = skb_copy_and_csum_bits(list,
1340 offset - start,
1341 to, copy, 0);
1342 csum = csum_block_add(csum, csum2, pos);
1343 if ((len -= copy) == 0)
1344 return csum;
1345 offset += copy;
1346 to += copy;
1347 pos += copy;
1348 }
1349 start = end;
1350 }
1351 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001352 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return csum;
1354}
1355
1356void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1357{
Al Virod3bc23e2006-11-14 21:24:49 -08001358 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 long csstart;
1360
Patrick McHardy84fa7932006-08-29 16:44:56 -07001361 if (skb->ip_summed == CHECKSUM_PARTIAL)
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001362 csstart = skb_transport_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 else
1364 csstart = skb_headlen(skb);
1365
Kris Katterjohn09a62662006-01-08 22:24:28 -08001366 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 memcpy(to, skb->data, csstart);
1369
1370 csum = 0;
1371 if (csstart != skb->len)
1372 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1373 skb->len - csstart, 0);
1374
Patrick McHardy84fa7932006-08-29 16:44:56 -07001375 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001376 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Al Virod3bc23e2006-11-14 21:24:49 -08001378 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380}
1381
1382/**
1383 * skb_dequeue - remove from the head of the queue
1384 * @list: list to dequeue from
1385 *
1386 * Remove the head of the list. The list lock is taken so the function
1387 * may be used safely with other locking list functions. The head item is
1388 * returned or %NULL if the list is empty.
1389 */
1390
1391struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1392{
1393 unsigned long flags;
1394 struct sk_buff *result;
1395
1396 spin_lock_irqsave(&list->lock, flags);
1397 result = __skb_dequeue(list);
1398 spin_unlock_irqrestore(&list->lock, flags);
1399 return result;
1400}
1401
1402/**
1403 * skb_dequeue_tail - remove from the tail of the queue
1404 * @list: list to dequeue from
1405 *
1406 * Remove the tail of the list. The list lock is taken so the function
1407 * may be used safely with other locking list functions. The tail item is
1408 * returned or %NULL if the list is empty.
1409 */
1410struct sk_buff *skb_dequeue_tail(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_tail(list);
1417 spin_unlock_irqrestore(&list->lock, flags);
1418 return result;
1419}
1420
1421/**
1422 * skb_queue_purge - empty a list
1423 * @list: list to empty
1424 *
1425 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1426 * the list and one reference dropped. This function takes the list
1427 * lock and is atomic with respect to other list locking functions.
1428 */
1429void skb_queue_purge(struct sk_buff_head *list)
1430{
1431 struct sk_buff *skb;
1432 while ((skb = skb_dequeue(list)) != NULL)
1433 kfree_skb(skb);
1434}
1435
1436/**
1437 * skb_queue_head - queue a buffer at the list head
1438 * @list: list to use
1439 * @newsk: buffer to queue
1440 *
1441 * Queue a buffer at the start of the list. This function takes the
1442 * list lock and can be used safely with other locking &sk_buff functions
1443 * safely.
1444 *
1445 * A buffer cannot be placed on two lists at the same time.
1446 */
1447void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1448{
1449 unsigned long flags;
1450
1451 spin_lock_irqsave(&list->lock, flags);
1452 __skb_queue_head(list, newsk);
1453 spin_unlock_irqrestore(&list->lock, flags);
1454}
1455
1456/**
1457 * skb_queue_tail - queue a buffer at the list tail
1458 * @list: list to use
1459 * @newsk: buffer to queue
1460 *
1461 * Queue a buffer at the tail of the list. This function takes the
1462 * list lock and can be used safely with other locking &sk_buff functions
1463 * safely.
1464 *
1465 * A buffer cannot be placed on two lists at the same time.
1466 */
1467void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1468{
1469 unsigned long flags;
1470
1471 spin_lock_irqsave(&list->lock, flags);
1472 __skb_queue_tail(list, newsk);
1473 spin_unlock_irqrestore(&list->lock, flags);
1474}
David S. Miller8728b832005-08-09 19:25:21 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/**
1477 * skb_unlink - remove a buffer from a list
1478 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001479 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
David S. Miller8728b832005-08-09 19:25:21 -07001481 * Remove a packet from a list. The list locks are taken and this
1482 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *
David S. Miller8728b832005-08-09 19:25:21 -07001484 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
David S. Miller8728b832005-08-09 19:25:21 -07001486void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
David S. Miller8728b832005-08-09 19:25:21 -07001488 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
David S. Miller8728b832005-08-09 19:25:21 -07001490 spin_lock_irqsave(&list->lock, flags);
1491 __skb_unlink(skb, list);
1492 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/**
1496 * skb_append - append a buffer
1497 * @old: buffer to insert after
1498 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001499 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
1501 * Place a packet after a given packet in a list. The list locks are taken
1502 * and this function is atomic with respect to other list locked calls.
1503 * A buffer cannot be placed on two lists at the same time.
1504 */
David S. Miller8728b832005-08-09 19:25:21 -07001505void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 unsigned long flags;
1508
David S. Miller8728b832005-08-09 19:25:21 -07001509 spin_lock_irqsave(&list->lock, flags);
1510 __skb_append(old, newsk, list);
1511 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514
1515/**
1516 * skb_insert - insert a buffer
1517 * @old: buffer to insert before
1518 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001519 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
David S. Miller8728b832005-08-09 19:25:21 -07001521 * Place a packet before a given packet in a list. The list locks are
1522 * taken and this function is atomic with respect to other list locked
1523 * calls.
1524 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * A buffer cannot be placed on two lists at the same time.
1526 */
David S. Miller8728b832005-08-09 19:25:21 -07001527void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 unsigned long flags;
1530
David S. Miller8728b832005-08-09 19:25:21 -07001531 spin_lock_irqsave(&list->lock, flags);
1532 __skb_insert(newsk, old->prev, old, list);
1533 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536#if 0
1537/*
1538 * Tune the memory allocator for a new MTU size.
1539 */
1540void skb_add_mtu(int mtu)
1541{
1542 /* Must match allocation in alloc_skb */
1543 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1544
1545 kmem_add_cache_size(mtu);
1546}
1547#endif
1548
1549static inline void skb_split_inside_header(struct sk_buff *skb,
1550 struct sk_buff* skb1,
1551 const u32 len, const int pos)
1552{
1553 int i;
1554
1555 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1556
1557 /* And move data appendix as is. */
1558 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1559 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1560
1561 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1562 skb_shinfo(skb)->nr_frags = 0;
1563 skb1->data_len = skb->data_len;
1564 skb1->len += skb1->data_len;
1565 skb->data_len = 0;
1566 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001567 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570static inline void skb_split_no_header(struct sk_buff *skb,
1571 struct sk_buff* skb1,
1572 const u32 len, int pos)
1573{
1574 int i, k = 0;
1575 const int nfrags = skb_shinfo(skb)->nr_frags;
1576
1577 skb_shinfo(skb)->nr_frags = 0;
1578 skb1->len = skb1->data_len = skb->len - len;
1579 skb->len = len;
1580 skb->data_len = len - pos;
1581
1582 for (i = 0; i < nfrags; i++) {
1583 int size = skb_shinfo(skb)->frags[i].size;
1584
1585 if (pos + size > len) {
1586 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1587
1588 if (pos < len) {
1589 /* Split frag.
1590 * We have two variants in this case:
1591 * 1. Move all the frag to the second
1592 * part, if it is possible. F.e.
1593 * this approach is mandatory for TUX,
1594 * where splitting is expensive.
1595 * 2. Split is accurately. We make this.
1596 */
1597 get_page(skb_shinfo(skb)->frags[i].page);
1598 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1599 skb_shinfo(skb1)->frags[0].size -= len - pos;
1600 skb_shinfo(skb)->frags[i].size = len - pos;
1601 skb_shinfo(skb)->nr_frags++;
1602 }
1603 k++;
1604 } else
1605 skb_shinfo(skb)->nr_frags++;
1606 pos += size;
1607 }
1608 skb_shinfo(skb1)->nr_frags = k;
1609}
1610
1611/**
1612 * skb_split - Split fragmented skb to two parts at length len.
1613 * @skb: the buffer to split
1614 * @skb1: the buffer to receive the second part
1615 * @len: new length for skb
1616 */
1617void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1618{
1619 int pos = skb_headlen(skb);
1620
1621 if (len < pos) /* Split line is inside header. */
1622 skb_split_inside_header(skb, skb1, len, pos);
1623 else /* Second chunk has no header, nothing to copy. */
1624 skb_split_no_header(skb, skb1, len, pos);
1625}
1626
Thomas Graf677e90e2005-06-23 20:59:51 -07001627/**
1628 * skb_prepare_seq_read - Prepare a sequential read of skb data
1629 * @skb: the buffer to read
1630 * @from: lower offset of data to be read
1631 * @to: upper offset of data to be read
1632 * @st: state variable
1633 *
1634 * Initializes the specified state variable. Must be called before
1635 * invoking skb_seq_read() for the first time.
1636 */
1637void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1638 unsigned int to, struct skb_seq_state *st)
1639{
1640 st->lower_offset = from;
1641 st->upper_offset = to;
1642 st->root_skb = st->cur_skb = skb;
1643 st->frag_idx = st->stepped_offset = 0;
1644 st->frag_data = NULL;
1645}
1646
1647/**
1648 * skb_seq_read - Sequentially read skb data
1649 * @consumed: number of bytes consumed by the caller so far
1650 * @data: destination pointer for data to be returned
1651 * @st: state variable
1652 *
1653 * Reads a block of skb data at &consumed relative to the
1654 * lower offset specified to skb_prepare_seq_read(). Assigns
1655 * the head of the data block to &data and returns the length
1656 * of the block or 0 if the end of the skb data or the upper
1657 * offset has been reached.
1658 *
1659 * The caller is not required to consume all of the data
1660 * returned, i.e. &consumed is typically set to the number
1661 * of bytes already consumed and the next call to
1662 * skb_seq_read() will return the remaining part of the block.
1663 *
1664 * Note: The size of each block of data returned can be arbitary,
1665 * this limitation is the cost for zerocopy seqeuental
1666 * reads of potentially non linear data.
1667 *
1668 * Note: Fragment lists within fragments are not implemented
1669 * at the moment, state->root_skb could be replaced with
1670 * a stack for this purpose.
1671 */
1672unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1673 struct skb_seq_state *st)
1674{
1675 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1676 skb_frag_t *frag;
1677
1678 if (unlikely(abs_offset >= st->upper_offset))
1679 return 0;
1680
1681next_skb:
1682 block_limit = skb_headlen(st->cur_skb);
1683
1684 if (abs_offset < block_limit) {
1685 *data = st->cur_skb->data + abs_offset;
1686 return block_limit - abs_offset;
1687 }
1688
1689 if (st->frag_idx == 0 && !st->frag_data)
1690 st->stepped_offset += skb_headlen(st->cur_skb);
1691
1692 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1693 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1694 block_limit = frag->size + st->stepped_offset;
1695
1696 if (abs_offset < block_limit) {
1697 if (!st->frag_data)
1698 st->frag_data = kmap_skb_frag(frag);
1699
1700 *data = (u8 *) st->frag_data + frag->page_offset +
1701 (abs_offset - st->stepped_offset);
1702
1703 return block_limit - abs_offset;
1704 }
1705
1706 if (st->frag_data) {
1707 kunmap_skb_frag(st->frag_data);
1708 st->frag_data = NULL;
1709 }
1710
1711 st->frag_idx++;
1712 st->stepped_offset += frag->size;
1713 }
1714
1715 if (st->cur_skb->next) {
1716 st->cur_skb = st->cur_skb->next;
1717 st->frag_idx = 0;
1718 goto next_skb;
1719 } else if (st->root_skb == st->cur_skb &&
1720 skb_shinfo(st->root_skb)->frag_list) {
1721 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1722 goto next_skb;
1723 }
1724
1725 return 0;
1726}
1727
1728/**
1729 * skb_abort_seq_read - Abort a sequential read of skb data
1730 * @st: state variable
1731 *
1732 * Must be called if skb_seq_read() was not called until it
1733 * returned 0.
1734 */
1735void skb_abort_seq_read(struct skb_seq_state *st)
1736{
1737 if (st->frag_data)
1738 kunmap_skb_frag(st->frag_data);
1739}
1740
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001741#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1742
1743static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1744 struct ts_config *conf,
1745 struct ts_state *state)
1746{
1747 return skb_seq_read(offset, text, TS_SKB_CB(state));
1748}
1749
1750static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1751{
1752 skb_abort_seq_read(TS_SKB_CB(state));
1753}
1754
1755/**
1756 * skb_find_text - Find a text pattern in skb data
1757 * @skb: the buffer to look in
1758 * @from: search offset
1759 * @to: search limit
1760 * @config: textsearch configuration
1761 * @state: uninitialized textsearch state variable
1762 *
1763 * Finds a pattern in the skb data according to the specified
1764 * textsearch configuration. Use textsearch_next() to retrieve
1765 * subsequent occurrences of the pattern. Returns the offset
1766 * to the first occurrence or UINT_MAX if no match was found.
1767 */
1768unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1769 unsigned int to, struct ts_config *config,
1770 struct ts_state *state)
1771{
Phil Oesterf72b9482006-06-26 00:00:57 -07001772 unsigned int ret;
1773
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001774 config->get_next_block = skb_ts_get_next_block;
1775 config->finish = skb_ts_finish;
1776
1777 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1778
Phil Oesterf72b9482006-06-26 00:00:57 -07001779 ret = textsearch_find(config, state);
1780 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001781}
1782
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001783/**
1784 * skb_append_datato_frags: - append the user data to a skb
1785 * @sk: sock structure
1786 * @skb: skb structure to be appened with user data.
1787 * @getfrag: call back function to be used for getting the user data
1788 * @from: pointer to user message iov
1789 * @length: length of the iov message
1790 *
1791 * Description: This procedure append the user data in the fragment part
1792 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1793 */
1794int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001795 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001796 int len, int odd, struct sk_buff *skb),
1797 void *from, int length)
1798{
1799 int frg_cnt = 0;
1800 skb_frag_t *frag = NULL;
1801 struct page *page = NULL;
1802 int copy, left;
1803 int offset = 0;
1804 int ret;
1805
1806 do {
1807 /* Return error if we don't have space for new frag */
1808 frg_cnt = skb_shinfo(skb)->nr_frags;
1809 if (frg_cnt >= MAX_SKB_FRAGS)
1810 return -EFAULT;
1811
1812 /* allocate a new page for next frag */
1813 page = alloc_pages(sk->sk_allocation, 0);
1814
1815 /* If alloc_page fails just return failure and caller will
1816 * free previous allocated pages by doing kfree_skb()
1817 */
1818 if (page == NULL)
1819 return -ENOMEM;
1820
1821 /* initialize the next frag */
1822 sk->sk_sndmsg_page = page;
1823 sk->sk_sndmsg_off = 0;
1824 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1825 skb->truesize += PAGE_SIZE;
1826 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1827
1828 /* get the new initialized frag */
1829 frg_cnt = skb_shinfo(skb)->nr_frags;
1830 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1831
1832 /* copy the user data to page */
1833 left = PAGE_SIZE - frag->page_offset;
1834 copy = (length > left)? left : length;
1835
1836 ret = getfrag(from, (page_address(frag->page) +
1837 frag->page_offset + frag->size),
1838 offset, copy, 0, skb);
1839 if (ret < 0)
1840 return -EFAULT;
1841
1842 /* copy was successful so update the size parameters */
1843 sk->sk_sndmsg_off += copy;
1844 frag->size += copy;
1845 skb->len += copy;
1846 skb->data_len += copy;
1847 offset += copy;
1848 length -= copy;
1849
1850 } while (length > 0);
1851
1852 return 0;
1853}
1854
Herbert Xucbb042f92006-03-20 22:43:56 -08001855/**
1856 * skb_pull_rcsum - pull skb and update receive checksum
1857 * @skb: buffer to update
1858 * @start: start of data before pull
1859 * @len: length of data pulled
1860 *
1861 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001862 * update the CHECKSUM_COMPLETE checksum. It should be used on
1863 * receive path processing instead of skb_pull unless you know
1864 * that the checksum difference is zero (e.g., a valid IP header)
1865 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08001866 */
1867unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1868{
1869 BUG_ON(len > skb->len);
1870 skb->len -= len;
1871 BUG_ON(skb->len < skb->data_len);
1872 skb_postpull_rcsum(skb, skb->data, len);
1873 return skb->data += len;
1874}
1875
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001876EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1877
Herbert Xuf4c50d92006-06-22 03:02:40 -07001878/**
1879 * skb_segment - Perform protocol segmentation on skb.
1880 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001881 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001882 *
1883 * This function performs segmentation on the given skb. It returns
1884 * the segment at the given position. It returns NULL if there are
1885 * no more segments to generate, or when an error is encountered.
1886 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001887struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001888{
1889 struct sk_buff *segs = NULL;
1890 struct sk_buff *tail = NULL;
1891 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001892 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001893 unsigned int offset = doffset;
1894 unsigned int headroom;
1895 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001896 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001897 int nfrags = skb_shinfo(skb)->nr_frags;
1898 int err = -ENOMEM;
1899 int i = 0;
1900 int pos;
1901
1902 __skb_push(skb, doffset);
1903 headroom = skb_headroom(skb);
1904 pos = skb_headlen(skb);
1905
1906 do {
1907 struct sk_buff *nskb;
1908 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001909 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001910 int k;
1911 int size;
1912
1913 len = skb->len - offset;
1914 if (len > mss)
1915 len = mss;
1916
1917 hsize = skb_headlen(skb) - offset;
1918 if (hsize < 0)
1919 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001920 if (hsize > len || !sg)
1921 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001922
Herbert Xuc8884ed2006-10-29 15:59:41 -08001923 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001924 if (unlikely(!nskb))
1925 goto err;
1926
1927 if (segs)
1928 tail->next = nskb;
1929 else
1930 segs = nskb;
1931 tail = nskb;
1932
1933 nskb->dev = skb->dev;
1934 nskb->priority = skb->priority;
1935 nskb->protocol = skb->protocol;
1936 nskb->dst = dst_clone(skb->dst);
1937 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1938 nskb->pkt_type = skb->pkt_type;
1939 nskb->mac_len = skb->mac_len;
1940
1941 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001942 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001943 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001944 nskb->transport_header = (nskb->network_header +
1945 skb_network_header_len(skb));
Herbert Xuf4c50d92006-06-22 03:02:40 -07001946 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1947
1948 if (!sg) {
1949 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1950 skb_put(nskb, len),
1951 len, 0);
1952 continue;
1953 }
1954
1955 frag = skb_shinfo(nskb)->frags;
1956 k = 0;
1957
Patrick McHardy84fa7932006-08-29 16:44:56 -07001958 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001959 nskb->csum = skb->csum;
1960 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1961
1962 while (pos < offset + len) {
1963 BUG_ON(i >= nfrags);
1964
1965 *frag = skb_shinfo(skb)->frags[i];
1966 get_page(frag->page);
1967 size = frag->size;
1968
1969 if (pos < offset) {
1970 frag->page_offset += offset - pos;
1971 frag->size -= offset - pos;
1972 }
1973
1974 k++;
1975
1976 if (pos + size <= offset + len) {
1977 i++;
1978 pos += size;
1979 } else {
1980 frag->size -= pos + size - (offset + len);
1981 break;
1982 }
1983
1984 frag++;
1985 }
1986
1987 skb_shinfo(nskb)->nr_frags = k;
1988 nskb->data_len = len - hsize;
1989 nskb->len += nskb->data_len;
1990 nskb->truesize += nskb->data_len;
1991 } while ((offset += len) < skb->len);
1992
1993 return segs;
1994
1995err:
1996 while ((skb = segs)) {
1997 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08001998 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001999 }
2000 return ERR_PTR(err);
2001}
2002
2003EXPORT_SYMBOL_GPL(skb_segment);
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005void __init skb_init(void)
2006{
2007 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2008 sizeof(struct sk_buff),
2009 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002010 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002012 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2013 (2*sizeof(struct sk_buff)) +
2014 sizeof(atomic_t),
2015 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002016 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002017 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
2020EXPORT_SYMBOL(___pskb_trim);
2021EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002022EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002024EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002025EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026EXPORT_SYMBOL(pskb_copy);
2027EXPORT_SYMBOL(pskb_expand_head);
2028EXPORT_SYMBOL(skb_checksum);
2029EXPORT_SYMBOL(skb_clone);
2030EXPORT_SYMBOL(skb_clone_fraglist);
2031EXPORT_SYMBOL(skb_copy);
2032EXPORT_SYMBOL(skb_copy_and_csum_bits);
2033EXPORT_SYMBOL(skb_copy_and_csum_dev);
2034EXPORT_SYMBOL(skb_copy_bits);
2035EXPORT_SYMBOL(skb_copy_expand);
2036EXPORT_SYMBOL(skb_over_panic);
2037EXPORT_SYMBOL(skb_pad);
2038EXPORT_SYMBOL(skb_realloc_headroom);
2039EXPORT_SYMBOL(skb_under_panic);
2040EXPORT_SYMBOL(skb_dequeue);
2041EXPORT_SYMBOL(skb_dequeue_tail);
2042EXPORT_SYMBOL(skb_insert);
2043EXPORT_SYMBOL(skb_queue_purge);
2044EXPORT_SYMBOL(skb_queue_head);
2045EXPORT_SYMBOL(skb_queue_tail);
2046EXPORT_SYMBOL(skb_unlink);
2047EXPORT_SYMBOL(skb_append);
2048EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002049EXPORT_SYMBOL(skb_prepare_seq_read);
2050EXPORT_SYMBOL(skb_seq_read);
2051EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002052EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002053EXPORT_SYMBOL(skb_append_datato_frags);