blob: f927b6e8027e3041b43cc065d0c763448e8674c2 [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;
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 }
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;
418 n->nohdr = 0;
419 C(pkt_type);
420 C(ip_summed);
421 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800422#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
423 C(ipvs_property);
424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800427 C(mark);
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700428 __nf_copy(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#ifdef CONFIG_NET_SCHED
430 C(tc_index);
431#ifdef CONFIG_NET_CLS_ACT
432 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700433 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
434 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700435 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
James Morris984bc162006-06-09 00:29:17 -0700437 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438#endif
439 C(truesize);
440 atomic_set(&n->users, 1);
441 C(head);
442 C(data);
443 C(tail);
444 C(end);
445
446 atomic_inc(&(skb_shinfo(skb)->dataref));
447 skb->cloned = 1;
448
449 return n;
450}
451
452static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
453{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700454#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * Shift between the two data areas in bytes
457 */
458 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 new->sk = NULL;
461 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 new->priority = old->priority;
463 new->protocol = old->protocol;
464 new->dst = dst_clone(old->dst);
465#ifdef CONFIG_INET
466 new->sp = secpath_get(old->sp);
467#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700468 new->transport_header = old->transport_header;
469 new->network_header = old->network_header;
470 new->mac_header = old->mac_header;
471#ifndef NET_SKBUFF_DATA_USES_OFFSET
472 /* {transport,network,mac}_header are relative to skb->head */
473 new->transport_header += offset;
474 new->network_header += offset;
475 new->mac_header += offset;
476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 memcpy(new->cb, old->cb, sizeof(old->cb));
478 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700479 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700481 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800483 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700484 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300485#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
486 new->ipvs_property = old->ipvs_property;
487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#ifdef CONFIG_NET_SCHED
489#ifdef CONFIG_NET_CLS_ACT
490 new->tc_verd = old->tc_verd;
491#endif
492 new->tc_index = old->tc_index;
493#endif
James Morris984bc162006-06-09 00:29:17 -0700494 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700496 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
497 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
498 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/**
502 * skb_copy - create private copy of an sk_buff
503 * @skb: buffer to copy
504 * @gfp_mask: allocation priority
505 *
506 * Make a copy of both an &sk_buff and its data. This is used when the
507 * caller wishes to modify the data and needs a private copy of the
508 * data to alter. Returns %NULL on failure or the pointer to the buffer
509 * on success. The returned buffer has a reference count of 1.
510 *
511 * As by-product this function converts non-linear &sk_buff to linear
512 * one, so that &sk_buff becomes completely private and caller is allowed
513 * to modify all the data of returned buffer. This means that this
514 * function is not recommended for use in circumstances when only
515 * header is going to be modified. Use pskb_copy() instead.
516 */
517
Al Virodd0fc662005-10-07 07:46:04 +0100518struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int headerlen = skb->data - skb->head;
521 /*
522 * Allocate the copy buffer
523 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700524 struct sk_buff *n;
525#ifdef NET_SKBUFF_DATA_USES_OFFSET
526 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
527#else
528 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
529#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (!n)
531 return NULL;
532
533 /* Set the data pointer */
534 skb_reserve(n, headerlen);
535 /* Set the tail pointer and length */
536 skb_put(n, skb->len);
537 n->csum = skb->csum;
538 n->ip_summed = skb->ip_summed;
539
540 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
541 BUG();
542
543 copy_skb_header(n, skb);
544 return n;
545}
546
547
548/**
549 * pskb_copy - create copy of an sk_buff with private head.
550 * @skb: buffer to copy
551 * @gfp_mask: allocation priority
552 *
553 * Make a copy of both an &sk_buff and part of its data, located
554 * in header. Fragmented data remain shared. This is used when
555 * the caller wishes to modify only header of &sk_buff and needs
556 * private copy of the header to alter. Returns %NULL on failure
557 * or the pointer to the buffer on success.
558 * The returned buffer has a reference count of 1.
559 */
560
Al Virodd0fc662005-10-07 07:46:04 +0100561struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 /*
564 * Allocate the copy buffer
565 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700566 struct sk_buff *n;
567#ifdef NET_SKBUFF_DATA_USES_OFFSET
568 n = alloc_skb(skb->end, gfp_mask);
569#else
570 n = alloc_skb(skb->end - skb->head, gfp_mask);
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!n)
573 goto out;
574
575 /* Set the data pointer */
576 skb_reserve(n, skb->data - skb->head);
577 /* Set the tail pointer and length */
578 skb_put(n, skb_headlen(skb));
579 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300580 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 n->csum = skb->csum;
582 n->ip_summed = skb->ip_summed;
583
Herbert Xu25f484a2006-11-07 14:57:15 -0800584 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 n->data_len = skb->data_len;
586 n->len = skb->len;
587
588 if (skb_shinfo(skb)->nr_frags) {
589 int i;
590
591 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
592 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
593 get_page(skb_shinfo(n)->frags[i].page);
594 }
595 skb_shinfo(n)->nr_frags = i;
596 }
597
598 if (skb_shinfo(skb)->frag_list) {
599 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
600 skb_clone_fraglist(n);
601 }
602
603 copy_skb_header(n, skb);
604out:
605 return n;
606}
607
608/**
609 * pskb_expand_head - reallocate header of &sk_buff
610 * @skb: buffer to reallocate
611 * @nhead: room to add at head
612 * @ntail: room to add at tail
613 * @gfp_mask: allocation priority
614 *
615 * Expands (or creates identical copy, if &nhead and &ntail are zero)
616 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
617 * reference count of 1. Returns zero in the case of success or error,
618 * if expansion failed. In the last case, &sk_buff is not changed.
619 *
620 * All the pointers pointing into skb header may change and must be
621 * reloaded after call to this function.
622 */
623
Victor Fusco86a76ca2005-07-08 14:57:47 -0700624int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100625 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int i;
628 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700629#ifdef NET_SKBUFF_DATA_USES_OFFSET
630 int size = nhead + skb->end + ntail;
631#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700633#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 long off;
635
636 if (skb_shared(skb))
637 BUG();
638
639 size = SKB_DATA_ALIGN(size);
640
641 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
642 if (!data)
643 goto nodata;
644
645 /* Copy only real data... and, alas, header. This should be
646 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700647 memcpy(data + nhead, skb->head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700648#ifdef NET_SKBUFF_DATA_USES_OFFSET
649 skb->tail);
650#else
651 skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700652#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700653 memcpy(data + size, skb_end_pointer(skb),
654 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
657 get_page(skb_shinfo(skb)->frags[i].page);
658
659 if (skb_shinfo(skb)->frag_list)
660 skb_clone_fraglist(skb);
661
662 skb_release_data(skb);
663
664 off = (data + nhead) - skb->head;
665
666 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700668#ifdef NET_SKBUFF_DATA_USES_OFFSET
669 skb->end = size;
670#else
671 skb->end = skb->head + size;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700672 /* {transport,network,mac}_header and tail are relative to skb->head */
673 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700674 skb->transport_header += off;
675 skb->network_header += off;
676 skb->mac_header += off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700677#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 skb->cloned = 0;
679 skb->nohdr = 0;
680 atomic_set(&skb_shinfo(skb)->dataref, 1);
681 return 0;
682
683nodata:
684 return -ENOMEM;
685}
686
687/* Make private copy of skb with writable head and some headroom */
688
689struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
690{
691 struct sk_buff *skb2;
692 int delta = headroom - skb_headroom(skb);
693
694 if (delta <= 0)
695 skb2 = pskb_copy(skb, GFP_ATOMIC);
696 else {
697 skb2 = skb_clone(skb, GFP_ATOMIC);
698 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
699 GFP_ATOMIC)) {
700 kfree_skb(skb2);
701 skb2 = NULL;
702 }
703 }
704 return skb2;
705}
706
707
708/**
709 * skb_copy_expand - copy and expand sk_buff
710 * @skb: buffer to copy
711 * @newheadroom: new free bytes at head
712 * @newtailroom: new free bytes at tail
713 * @gfp_mask: allocation priority
714 *
715 * Make a copy of both an &sk_buff and its data and while doing so
716 * allocate additional space.
717 *
718 * This is used when the caller wishes to modify the data and needs a
719 * private copy of the data to alter as well as more space for new fields.
720 * Returns %NULL on failure or the pointer to the buffer
721 * on success. The returned buffer has a reference count of 1.
722 *
723 * You must pass %GFP_ATOMIC as the allocation priority if this function
724 * is called from an interrupt.
725 *
726 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
727 * only by netfilter in the cases when checksum is recalculated? --ANK
728 */
729struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700730 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100731 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 /*
734 * Allocate the copy buffer
735 */
736 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
737 gfp_mask);
738 int head_copy_len, head_copy_off;
739
740 if (!n)
741 return NULL;
742
743 skb_reserve(n, newheadroom);
744
745 /* Set the tail pointer and length */
746 skb_put(n, skb->len);
747
748 head_copy_len = skb_headroom(skb);
749 head_copy_off = 0;
750 if (newheadroom <= head_copy_len)
751 head_copy_len = newheadroom;
752 else
753 head_copy_off = newheadroom - head_copy_len;
754
755 /* Copy the linear header and data. */
756 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
757 skb->len + head_copy_len))
758 BUG();
759
760 copy_skb_header(n, skb);
761
762 return n;
763}
764
765/**
766 * skb_pad - zero pad the tail of an skb
767 * @skb: buffer to pad
768 * @pad: space to pad
769 *
770 * Ensure that a buffer is followed by a padding area that is zero
771 * filled. Used by network drivers which may DMA or transfer data
772 * beyond the buffer end onto the wire.
773 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700774 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900776
Herbert Xu5b057c62006-06-23 02:06:41 -0700777int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Herbert Xu5b057c62006-06-23 02:06:41 -0700779 int err;
780 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700783 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700787
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700788 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700789 if (likely(skb_cloned(skb) || ntail > 0)) {
790 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
791 if (unlikely(err))
792 goto free_skb;
793 }
794
795 /* FIXME: The use of this function with non-linear skb's really needs
796 * to be audited.
797 */
798 err = skb_linearize(skb);
799 if (unlikely(err))
800 goto free_skb;
801
802 memset(skb->data + skb->len, 0, pad);
803 return 0;
804
805free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700807 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900808}
809
Herbert Xu3cc0e872006-06-09 16:13:38 -0700810/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812
Herbert Xu3cc0e872006-06-09 16:13:38 -0700813int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Herbert Xu27b437c2006-07-13 19:26:39 -0700815 struct sk_buff **fragp;
816 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int offset = skb_headlen(skb);
818 int nfrags = skb_shinfo(skb)->nr_frags;
819 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700820 int err;
821
822 if (skb_cloned(skb) &&
823 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
824 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700826 i = 0;
827 if (offset >= len)
828 goto drop_pages;
829
830 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700832
833 if (end < len) {
834 offset = end;
835 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700837
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700838 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700839
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700840drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700841 skb_shinfo(skb)->nr_frags = i;
842
843 for (; i < nfrags; i++)
844 put_page(skb_shinfo(skb)->frags[i].page);
845
846 if (skb_shinfo(skb)->frag_list)
847 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700848 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Herbert Xu27b437c2006-07-13 19:26:39 -0700851 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
852 fragp = &frag->next) {
853 int end = offset + frag->len;
854
855 if (skb_shared(frag)) {
856 struct sk_buff *nfrag;
857
858 nfrag = skb_clone(frag, GFP_ATOMIC);
859 if (unlikely(!nfrag))
860 return -ENOMEM;
861
862 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700863 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700864 frag = nfrag;
865 *fragp = frag;
866 }
867
868 if (end < len) {
869 offset = end;
870 continue;
871 }
872
873 if (end > len &&
874 unlikely((err = pskb_trim(frag, len - offset))))
875 return err;
876
877 if (frag->next)
878 skb_drop_list(&frag->next);
879 break;
880 }
881
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700882done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700883 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 skb->data_len -= skb->len - len;
885 skb->len = len;
886 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700887 skb->len = len;
888 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700889 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 return 0;
893}
894
895/**
896 * __pskb_pull_tail - advance tail of skb header
897 * @skb: buffer to reallocate
898 * @delta: number of bytes to advance tail
899 *
900 * The function makes a sense only on a fragmented &sk_buff,
901 * it expands header moving its tail forward and copying necessary
902 * data from fragmented part.
903 *
904 * &sk_buff MUST have reference count of 1.
905 *
906 * Returns %NULL (and &sk_buff does not change) if pull failed
907 * or value of new tail of skb in the case of success.
908 *
909 * All the pointers pointing into skb header may change and must be
910 * reloaded after call to this function.
911 */
912
913/* Moves tail of skb head forward, copying data from fragmented part,
914 * when it is necessary.
915 * 1. It may fail due to malloc failure.
916 * 2. It may change skb pointers.
917 *
918 * It is pretty complicated. Luckily, it is called only in exceptional cases.
919 */
920unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
921{
922 /* If skb has not enough free space at tail, get new one
923 * plus 128 bytes for future expansions. If we have enough
924 * room at tail, reallocate without expansion only if skb is cloned.
925 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700926 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (eat > 0 || skb_cloned(skb)) {
929 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
930 GFP_ATOMIC))
931 return NULL;
932 }
933
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700934 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 BUG();
936
937 /* Optimization: no fragments, no reasons to preestimate
938 * size of pulled pages. Superb.
939 */
940 if (!skb_shinfo(skb)->frag_list)
941 goto pull_pages;
942
943 /* Estimate size of pulled pages. */
944 eat = delta;
945 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
946 if (skb_shinfo(skb)->frags[i].size >= eat)
947 goto pull_pages;
948 eat -= skb_shinfo(skb)->frags[i].size;
949 }
950
951 /* If we need update frag list, we are in troubles.
952 * Certainly, it possible to add an offset to skb data,
953 * but taking into account that pulling is expected to
954 * be very rare operation, it is worth to fight against
955 * further bloating skb head and crucify ourselves here instead.
956 * Pure masohism, indeed. 8)8)
957 */
958 if (eat) {
959 struct sk_buff *list = skb_shinfo(skb)->frag_list;
960 struct sk_buff *clone = NULL;
961 struct sk_buff *insp = NULL;
962
963 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800964 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (list->len <= eat) {
967 /* Eaten as whole. */
968 eat -= list->len;
969 list = list->next;
970 insp = list;
971 } else {
972 /* Eaten partially. */
973
974 if (skb_shared(list)) {
975 /* Sucks! We need to fork list. :-( */
976 clone = skb_clone(list, GFP_ATOMIC);
977 if (!clone)
978 return NULL;
979 insp = list->next;
980 list = clone;
981 } else {
982 /* This may be pulled without
983 * problems. */
984 insp = list;
985 }
986 if (!pskb_pull(list, eat)) {
987 if (clone)
988 kfree_skb(clone);
989 return NULL;
990 }
991 break;
992 }
993 } while (eat);
994
995 /* Free pulled out fragments. */
996 while ((list = skb_shinfo(skb)->frag_list) != insp) {
997 skb_shinfo(skb)->frag_list = list->next;
998 kfree_skb(list);
999 }
1000 /* And insert new clone at head. */
1001 if (clone) {
1002 clone->next = list;
1003 skb_shinfo(skb)->frag_list = clone;
1004 }
1005 }
1006 /* Success! Now we may commit changes to skb data. */
1007
1008pull_pages:
1009 eat = delta;
1010 k = 0;
1011 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1012 if (skb_shinfo(skb)->frags[i].size <= eat) {
1013 put_page(skb_shinfo(skb)->frags[i].page);
1014 eat -= skb_shinfo(skb)->frags[i].size;
1015 } else {
1016 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1017 if (eat) {
1018 skb_shinfo(skb)->frags[k].page_offset += eat;
1019 skb_shinfo(skb)->frags[k].size -= eat;
1020 eat = 0;
1021 }
1022 k++;
1023 }
1024 }
1025 skb_shinfo(skb)->nr_frags = k;
1026
1027 skb->tail += delta;
1028 skb->data_len -= delta;
1029
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001030 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/* Copy some data bits from skb to kernel buffer. */
1034
1035int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1036{
1037 int i, copy;
1038 int start = skb_headlen(skb);
1039
1040 if (offset > (int)skb->len - len)
1041 goto fault;
1042
1043 /* Copy header. */
1044 if ((copy = start - offset) > 0) {
1045 if (copy > len)
1046 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001047 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if ((len -= copy) == 0)
1049 return 0;
1050 offset += copy;
1051 to += copy;
1052 }
1053
1054 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1055 int end;
1056
1057 BUG_TRAP(start <= offset + len);
1058
1059 end = start + skb_shinfo(skb)->frags[i].size;
1060 if ((copy = end - offset) > 0) {
1061 u8 *vaddr;
1062
1063 if (copy > len)
1064 copy = len;
1065
1066 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1067 memcpy(to,
1068 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1069 offset - start, copy);
1070 kunmap_skb_frag(vaddr);
1071
1072 if ((len -= copy) == 0)
1073 return 0;
1074 offset += copy;
1075 to += copy;
1076 }
1077 start = end;
1078 }
1079
1080 if (skb_shinfo(skb)->frag_list) {
1081 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1082
1083 for (; list; list = list->next) {
1084 int end;
1085
1086 BUG_TRAP(start <= offset + len);
1087
1088 end = start + list->len;
1089 if ((copy = end - offset) > 0) {
1090 if (copy > len)
1091 copy = len;
1092 if (skb_copy_bits(list, offset - start,
1093 to, copy))
1094 goto fault;
1095 if ((len -= copy) == 0)
1096 return 0;
1097 offset += copy;
1098 to += copy;
1099 }
1100 start = end;
1101 }
1102 }
1103 if (!len)
1104 return 0;
1105
1106fault:
1107 return -EFAULT;
1108}
1109
Herbert Xu357b40a2005-04-19 22:30:14 -07001110/**
1111 * skb_store_bits - store bits from kernel buffer to skb
1112 * @skb: destination buffer
1113 * @offset: offset in destination
1114 * @from: source buffer
1115 * @len: number of bytes to copy
1116 *
1117 * Copy the specified number of bytes from the source buffer to the
1118 * destination skb. This function handles all the messy bits of
1119 * traversing fragment lists and such.
1120 */
1121
1122int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1123{
1124 int i, copy;
1125 int start = skb_headlen(skb);
1126
1127 if (offset > (int)skb->len - len)
1128 goto fault;
1129
1130 if ((copy = start - offset) > 0) {
1131 if (copy > len)
1132 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001133 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001134 if ((len -= copy) == 0)
1135 return 0;
1136 offset += copy;
1137 from += copy;
1138 }
1139
1140 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1141 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1142 int end;
1143
1144 BUG_TRAP(start <= offset + len);
1145
1146 end = start + frag->size;
1147 if ((copy = end - offset) > 0) {
1148 u8 *vaddr;
1149
1150 if (copy > len)
1151 copy = len;
1152
1153 vaddr = kmap_skb_frag(frag);
1154 memcpy(vaddr + frag->page_offset + offset - start,
1155 from, copy);
1156 kunmap_skb_frag(vaddr);
1157
1158 if ((len -= copy) == 0)
1159 return 0;
1160 offset += copy;
1161 from += copy;
1162 }
1163 start = end;
1164 }
1165
1166 if (skb_shinfo(skb)->frag_list) {
1167 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1168
1169 for (; list; list = list->next) {
1170 int end;
1171
1172 BUG_TRAP(start <= offset + len);
1173
1174 end = start + list->len;
1175 if ((copy = end - offset) > 0) {
1176 if (copy > len)
1177 copy = len;
1178 if (skb_store_bits(list, offset - start,
1179 from, copy))
1180 goto fault;
1181 if ((len -= copy) == 0)
1182 return 0;
1183 offset += copy;
1184 from += copy;
1185 }
1186 start = end;
1187 }
1188 }
1189 if (!len)
1190 return 0;
1191
1192fault:
1193 return -EFAULT;
1194}
1195
1196EXPORT_SYMBOL(skb_store_bits);
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/* Checksum skb data. */
1199
Al Viro2bbbc862006-11-14 21:37:14 -08001200__wsum skb_checksum(const struct sk_buff *skb, int offset,
1201 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int start = skb_headlen(skb);
1204 int i, copy = start - offset;
1205 int pos = 0;
1206
1207 /* Checksum header. */
1208 if (copy > 0) {
1209 if (copy > len)
1210 copy = len;
1211 csum = csum_partial(skb->data + offset, copy, csum);
1212 if ((len -= copy) == 0)
1213 return csum;
1214 offset += copy;
1215 pos = copy;
1216 }
1217
1218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1219 int end;
1220
1221 BUG_TRAP(start <= offset + len);
1222
1223 end = start + skb_shinfo(skb)->frags[i].size;
1224 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001225 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 u8 *vaddr;
1227 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1228
1229 if (copy > len)
1230 copy = len;
1231 vaddr = kmap_skb_frag(frag);
1232 csum2 = csum_partial(vaddr + frag->page_offset +
1233 offset - start, copy, 0);
1234 kunmap_skb_frag(vaddr);
1235 csum = csum_block_add(csum, csum2, pos);
1236 if (!(len -= copy))
1237 return csum;
1238 offset += copy;
1239 pos += copy;
1240 }
1241 start = end;
1242 }
1243
1244 if (skb_shinfo(skb)->frag_list) {
1245 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1246
1247 for (; list; list = list->next) {
1248 int end;
1249
1250 BUG_TRAP(start <= offset + len);
1251
1252 end = start + list->len;
1253 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001254 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (copy > len)
1256 copy = len;
1257 csum2 = skb_checksum(list, offset - start,
1258 copy, 0);
1259 csum = csum_block_add(csum, csum2, pos);
1260 if ((len -= copy) == 0)
1261 return csum;
1262 offset += copy;
1263 pos += copy;
1264 }
1265 start = end;
1266 }
1267 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001268 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 return csum;
1271}
1272
1273/* Both of above in one bottle. */
1274
Al Viro81d77662006-11-14 21:37:33 -08001275__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1276 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 int start = skb_headlen(skb);
1279 int i, copy = start - offset;
1280 int pos = 0;
1281
1282 /* Copy header. */
1283 if (copy > 0) {
1284 if (copy > len)
1285 copy = len;
1286 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1287 copy, csum);
1288 if ((len -= copy) == 0)
1289 return csum;
1290 offset += copy;
1291 to += copy;
1292 pos = copy;
1293 }
1294
1295 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1296 int end;
1297
1298 BUG_TRAP(start <= offset + len);
1299
1300 end = start + skb_shinfo(skb)->frags[i].size;
1301 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001302 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 u8 *vaddr;
1304 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1305
1306 if (copy > len)
1307 copy = len;
1308 vaddr = kmap_skb_frag(frag);
1309 csum2 = csum_partial_copy_nocheck(vaddr +
1310 frag->page_offset +
1311 offset - start, to,
1312 copy, 0);
1313 kunmap_skb_frag(vaddr);
1314 csum = csum_block_add(csum, csum2, pos);
1315 if (!(len -= copy))
1316 return csum;
1317 offset += copy;
1318 to += copy;
1319 pos += copy;
1320 }
1321 start = end;
1322 }
1323
1324 if (skb_shinfo(skb)->frag_list) {
1325 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1326
1327 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001328 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int end;
1330
1331 BUG_TRAP(start <= offset + len);
1332
1333 end = start + list->len;
1334 if ((copy = end - offset) > 0) {
1335 if (copy > len)
1336 copy = len;
1337 csum2 = skb_copy_and_csum_bits(list,
1338 offset - start,
1339 to, copy, 0);
1340 csum = csum_block_add(csum, csum2, pos);
1341 if ((len -= copy) == 0)
1342 return csum;
1343 offset += copy;
1344 to += copy;
1345 pos += copy;
1346 }
1347 start = end;
1348 }
1349 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001350 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return csum;
1352}
1353
1354void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1355{
Al Virod3bc23e2006-11-14 21:24:49 -08001356 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 long csstart;
1358
Patrick McHardy84fa7932006-08-29 16:44:56 -07001359 if (skb->ip_summed == CHECKSUM_PARTIAL)
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001360 csstart = skb_transport_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 else
1362 csstart = skb_headlen(skb);
1363
Kris Katterjohn09a62662006-01-08 22:24:28 -08001364 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001366 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 csum = 0;
1369 if (csstart != skb->len)
1370 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1371 skb->len - csstart, 0);
1372
Patrick McHardy84fa7932006-08-29 16:44:56 -07001373 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001374 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Al Virod3bc23e2006-11-14 21:24:49 -08001376 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378}
1379
1380/**
1381 * skb_dequeue - remove from the head of the queue
1382 * @list: list to dequeue from
1383 *
1384 * Remove the head of the list. The list lock is taken so the function
1385 * may be used safely with other locking list functions. The head item is
1386 * returned or %NULL if the list is empty.
1387 */
1388
1389struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1390{
1391 unsigned long flags;
1392 struct sk_buff *result;
1393
1394 spin_lock_irqsave(&list->lock, flags);
1395 result = __skb_dequeue(list);
1396 spin_unlock_irqrestore(&list->lock, flags);
1397 return result;
1398}
1399
1400/**
1401 * skb_dequeue_tail - remove from the tail of the queue
1402 * @list: list to dequeue from
1403 *
1404 * Remove the tail of the list. The list lock is taken so the function
1405 * may be used safely with other locking list functions. The tail item is
1406 * returned or %NULL if the list is empty.
1407 */
1408struct sk_buff *skb_dequeue_tail(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_tail(list);
1415 spin_unlock_irqrestore(&list->lock, flags);
1416 return result;
1417}
1418
1419/**
1420 * skb_queue_purge - empty a list
1421 * @list: list to empty
1422 *
1423 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1424 * the list and one reference dropped. This function takes the list
1425 * lock and is atomic with respect to other list locking functions.
1426 */
1427void skb_queue_purge(struct sk_buff_head *list)
1428{
1429 struct sk_buff *skb;
1430 while ((skb = skb_dequeue(list)) != NULL)
1431 kfree_skb(skb);
1432}
1433
1434/**
1435 * skb_queue_head - queue a buffer at the list head
1436 * @list: list to use
1437 * @newsk: buffer to queue
1438 *
1439 * Queue a buffer at the start of the list. This function takes the
1440 * list lock and can be used safely with other locking &sk_buff functions
1441 * safely.
1442 *
1443 * A buffer cannot be placed on two lists at the same time.
1444 */
1445void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1446{
1447 unsigned long flags;
1448
1449 spin_lock_irqsave(&list->lock, flags);
1450 __skb_queue_head(list, newsk);
1451 spin_unlock_irqrestore(&list->lock, flags);
1452}
1453
1454/**
1455 * skb_queue_tail - queue a buffer at the list tail
1456 * @list: list to use
1457 * @newsk: buffer to queue
1458 *
1459 * Queue a buffer at the tail of the list. This function takes the
1460 * list lock and can be used safely with other locking &sk_buff functions
1461 * safely.
1462 *
1463 * A buffer cannot be placed on two lists at the same time.
1464 */
1465void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1466{
1467 unsigned long flags;
1468
1469 spin_lock_irqsave(&list->lock, flags);
1470 __skb_queue_tail(list, newsk);
1471 spin_unlock_irqrestore(&list->lock, flags);
1472}
David S. Miller8728b832005-08-09 19:25:21 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/**
1475 * skb_unlink - remove a buffer from a list
1476 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001477 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 *
David S. Miller8728b832005-08-09 19:25:21 -07001479 * Remove a packet from a list. The list locks are taken and this
1480 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 *
David S. Miller8728b832005-08-09 19:25:21 -07001482 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 */
David S. Miller8728b832005-08-09 19:25:21 -07001484void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
David S. Miller8728b832005-08-09 19:25:21 -07001486 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
David S. Miller8728b832005-08-09 19:25:21 -07001488 spin_lock_irqsave(&list->lock, flags);
1489 __skb_unlink(skb, list);
1490 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/**
1494 * skb_append - append a buffer
1495 * @old: buffer to insert after
1496 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001497 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * Place a packet after a given packet in a list. The list locks are taken
1500 * and this function is atomic with respect to other list locked calls.
1501 * A buffer cannot be placed on two lists at the same time.
1502 */
David S. Miller8728b832005-08-09 19:25:21 -07001503void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 unsigned long flags;
1506
David S. Miller8728b832005-08-09 19:25:21 -07001507 spin_lock_irqsave(&list->lock, flags);
1508 __skb_append(old, newsk, list);
1509 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512
1513/**
1514 * skb_insert - insert a buffer
1515 * @old: buffer to insert before
1516 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001517 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *
David S. Miller8728b832005-08-09 19:25:21 -07001519 * Place a packet before a given packet in a list. The list locks are
1520 * taken and this function is atomic with respect to other list locked
1521 * calls.
1522 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * A buffer cannot be placed on two lists at the same time.
1524 */
David S. Miller8728b832005-08-09 19:25:21 -07001525void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 unsigned long flags;
1528
David S. Miller8728b832005-08-09 19:25:21 -07001529 spin_lock_irqsave(&list->lock, flags);
1530 __skb_insert(newsk, old->prev, old, list);
1531 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534static inline void skb_split_inside_header(struct sk_buff *skb,
1535 struct sk_buff* skb1,
1536 const u32 len, const int pos)
1537{
1538 int i;
1539
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001540 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1541 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /* And move data appendix as is. */
1543 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1544 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1545
1546 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1547 skb_shinfo(skb)->nr_frags = 0;
1548 skb1->data_len = skb->data_len;
1549 skb1->len += skb1->data_len;
1550 skb->data_len = 0;
1551 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001552 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555static inline void skb_split_no_header(struct sk_buff *skb,
1556 struct sk_buff* skb1,
1557 const u32 len, int pos)
1558{
1559 int i, k = 0;
1560 const int nfrags = skb_shinfo(skb)->nr_frags;
1561
1562 skb_shinfo(skb)->nr_frags = 0;
1563 skb1->len = skb1->data_len = skb->len - len;
1564 skb->len = len;
1565 skb->data_len = len - pos;
1566
1567 for (i = 0; i < nfrags; i++) {
1568 int size = skb_shinfo(skb)->frags[i].size;
1569
1570 if (pos + size > len) {
1571 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1572
1573 if (pos < len) {
1574 /* Split frag.
1575 * We have two variants in this case:
1576 * 1. Move all the frag to the second
1577 * part, if it is possible. F.e.
1578 * this approach is mandatory for TUX,
1579 * where splitting is expensive.
1580 * 2. Split is accurately. We make this.
1581 */
1582 get_page(skb_shinfo(skb)->frags[i].page);
1583 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1584 skb_shinfo(skb1)->frags[0].size -= len - pos;
1585 skb_shinfo(skb)->frags[i].size = len - pos;
1586 skb_shinfo(skb)->nr_frags++;
1587 }
1588 k++;
1589 } else
1590 skb_shinfo(skb)->nr_frags++;
1591 pos += size;
1592 }
1593 skb_shinfo(skb1)->nr_frags = k;
1594}
1595
1596/**
1597 * skb_split - Split fragmented skb to two parts at length len.
1598 * @skb: the buffer to split
1599 * @skb1: the buffer to receive the second part
1600 * @len: new length for skb
1601 */
1602void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1603{
1604 int pos = skb_headlen(skb);
1605
1606 if (len < pos) /* Split line is inside header. */
1607 skb_split_inside_header(skb, skb1, len, pos);
1608 else /* Second chunk has no header, nothing to copy. */
1609 skb_split_no_header(skb, skb1, len, pos);
1610}
1611
Thomas Graf677e90e2005-06-23 20:59:51 -07001612/**
1613 * skb_prepare_seq_read - Prepare a sequential read of skb data
1614 * @skb: the buffer to read
1615 * @from: lower offset of data to be read
1616 * @to: upper offset of data to be read
1617 * @st: state variable
1618 *
1619 * Initializes the specified state variable. Must be called before
1620 * invoking skb_seq_read() for the first time.
1621 */
1622void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1623 unsigned int to, struct skb_seq_state *st)
1624{
1625 st->lower_offset = from;
1626 st->upper_offset = to;
1627 st->root_skb = st->cur_skb = skb;
1628 st->frag_idx = st->stepped_offset = 0;
1629 st->frag_data = NULL;
1630}
1631
1632/**
1633 * skb_seq_read - Sequentially read skb data
1634 * @consumed: number of bytes consumed by the caller so far
1635 * @data: destination pointer for data to be returned
1636 * @st: state variable
1637 *
1638 * Reads a block of skb data at &consumed relative to the
1639 * lower offset specified to skb_prepare_seq_read(). Assigns
1640 * the head of the data block to &data and returns the length
1641 * of the block or 0 if the end of the skb data or the upper
1642 * offset has been reached.
1643 *
1644 * The caller is not required to consume all of the data
1645 * returned, i.e. &consumed is typically set to the number
1646 * of bytes already consumed and the next call to
1647 * skb_seq_read() will return the remaining part of the block.
1648 *
1649 * Note: The size of each block of data returned can be arbitary,
1650 * this limitation is the cost for zerocopy seqeuental
1651 * reads of potentially non linear data.
1652 *
1653 * Note: Fragment lists within fragments are not implemented
1654 * at the moment, state->root_skb could be replaced with
1655 * a stack for this purpose.
1656 */
1657unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1658 struct skb_seq_state *st)
1659{
1660 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1661 skb_frag_t *frag;
1662
1663 if (unlikely(abs_offset >= st->upper_offset))
1664 return 0;
1665
1666next_skb:
1667 block_limit = skb_headlen(st->cur_skb);
1668
1669 if (abs_offset < block_limit) {
1670 *data = st->cur_skb->data + abs_offset;
1671 return block_limit - abs_offset;
1672 }
1673
1674 if (st->frag_idx == 0 && !st->frag_data)
1675 st->stepped_offset += skb_headlen(st->cur_skb);
1676
1677 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1678 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1679 block_limit = frag->size + st->stepped_offset;
1680
1681 if (abs_offset < block_limit) {
1682 if (!st->frag_data)
1683 st->frag_data = kmap_skb_frag(frag);
1684
1685 *data = (u8 *) st->frag_data + frag->page_offset +
1686 (abs_offset - st->stepped_offset);
1687
1688 return block_limit - abs_offset;
1689 }
1690
1691 if (st->frag_data) {
1692 kunmap_skb_frag(st->frag_data);
1693 st->frag_data = NULL;
1694 }
1695
1696 st->frag_idx++;
1697 st->stepped_offset += frag->size;
1698 }
1699
1700 if (st->cur_skb->next) {
1701 st->cur_skb = st->cur_skb->next;
1702 st->frag_idx = 0;
1703 goto next_skb;
1704 } else if (st->root_skb == st->cur_skb &&
1705 skb_shinfo(st->root_skb)->frag_list) {
1706 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1707 goto next_skb;
1708 }
1709
1710 return 0;
1711}
1712
1713/**
1714 * skb_abort_seq_read - Abort a sequential read of skb data
1715 * @st: state variable
1716 *
1717 * Must be called if skb_seq_read() was not called until it
1718 * returned 0.
1719 */
1720void skb_abort_seq_read(struct skb_seq_state *st)
1721{
1722 if (st->frag_data)
1723 kunmap_skb_frag(st->frag_data);
1724}
1725
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001726#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1727
1728static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1729 struct ts_config *conf,
1730 struct ts_state *state)
1731{
1732 return skb_seq_read(offset, text, TS_SKB_CB(state));
1733}
1734
1735static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1736{
1737 skb_abort_seq_read(TS_SKB_CB(state));
1738}
1739
1740/**
1741 * skb_find_text - Find a text pattern in skb data
1742 * @skb: the buffer to look in
1743 * @from: search offset
1744 * @to: search limit
1745 * @config: textsearch configuration
1746 * @state: uninitialized textsearch state variable
1747 *
1748 * Finds a pattern in the skb data according to the specified
1749 * textsearch configuration. Use textsearch_next() to retrieve
1750 * subsequent occurrences of the pattern. Returns the offset
1751 * to the first occurrence or UINT_MAX if no match was found.
1752 */
1753unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1754 unsigned int to, struct ts_config *config,
1755 struct ts_state *state)
1756{
Phil Oesterf72b9482006-06-26 00:00:57 -07001757 unsigned int ret;
1758
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001759 config->get_next_block = skb_ts_get_next_block;
1760 config->finish = skb_ts_finish;
1761
1762 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1763
Phil Oesterf72b9482006-06-26 00:00:57 -07001764 ret = textsearch_find(config, state);
1765 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001766}
1767
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001768/**
1769 * skb_append_datato_frags: - append the user data to a skb
1770 * @sk: sock structure
1771 * @skb: skb structure to be appened with user data.
1772 * @getfrag: call back function to be used for getting the user data
1773 * @from: pointer to user message iov
1774 * @length: length of the iov message
1775 *
1776 * Description: This procedure append the user data in the fragment part
1777 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1778 */
1779int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001780 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001781 int len, int odd, struct sk_buff *skb),
1782 void *from, int length)
1783{
1784 int frg_cnt = 0;
1785 skb_frag_t *frag = NULL;
1786 struct page *page = NULL;
1787 int copy, left;
1788 int offset = 0;
1789 int ret;
1790
1791 do {
1792 /* Return error if we don't have space for new frag */
1793 frg_cnt = skb_shinfo(skb)->nr_frags;
1794 if (frg_cnt >= MAX_SKB_FRAGS)
1795 return -EFAULT;
1796
1797 /* allocate a new page for next frag */
1798 page = alloc_pages(sk->sk_allocation, 0);
1799
1800 /* If alloc_page fails just return failure and caller will
1801 * free previous allocated pages by doing kfree_skb()
1802 */
1803 if (page == NULL)
1804 return -ENOMEM;
1805
1806 /* initialize the next frag */
1807 sk->sk_sndmsg_page = page;
1808 sk->sk_sndmsg_off = 0;
1809 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1810 skb->truesize += PAGE_SIZE;
1811 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1812
1813 /* get the new initialized frag */
1814 frg_cnt = skb_shinfo(skb)->nr_frags;
1815 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1816
1817 /* copy the user data to page */
1818 left = PAGE_SIZE - frag->page_offset;
1819 copy = (length > left)? left : length;
1820
1821 ret = getfrag(from, (page_address(frag->page) +
1822 frag->page_offset + frag->size),
1823 offset, copy, 0, skb);
1824 if (ret < 0)
1825 return -EFAULT;
1826
1827 /* copy was successful so update the size parameters */
1828 sk->sk_sndmsg_off += copy;
1829 frag->size += copy;
1830 skb->len += copy;
1831 skb->data_len += copy;
1832 offset += copy;
1833 length -= copy;
1834
1835 } while (length > 0);
1836
1837 return 0;
1838}
1839
Herbert Xucbb042f2006-03-20 22:43:56 -08001840/**
1841 * skb_pull_rcsum - pull skb and update receive checksum
1842 * @skb: buffer to update
1843 * @start: start of data before pull
1844 * @len: length of data pulled
1845 *
1846 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001847 * update the CHECKSUM_COMPLETE checksum. It should be used on
1848 * receive path processing instead of skb_pull unless you know
1849 * that the checksum difference is zero (e.g., a valid IP header)
1850 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001851 */
1852unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1853{
1854 BUG_ON(len > skb->len);
1855 skb->len -= len;
1856 BUG_ON(skb->len < skb->data_len);
1857 skb_postpull_rcsum(skb, skb->data, len);
1858 return skb->data += len;
1859}
1860
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001861EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1862
Herbert Xuf4c50d92006-06-22 03:02:40 -07001863/**
1864 * skb_segment - Perform protocol segmentation on skb.
1865 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001866 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001867 *
1868 * This function performs segmentation on the given skb. It returns
1869 * the segment at the given position. It returns NULL if there are
1870 * no more segments to generate, or when an error is encountered.
1871 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001872struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001873{
1874 struct sk_buff *segs = NULL;
1875 struct sk_buff *tail = NULL;
1876 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001877 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001878 unsigned int offset = doffset;
1879 unsigned int headroom;
1880 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001881 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001882 int nfrags = skb_shinfo(skb)->nr_frags;
1883 int err = -ENOMEM;
1884 int i = 0;
1885 int pos;
1886
1887 __skb_push(skb, doffset);
1888 headroom = skb_headroom(skb);
1889 pos = skb_headlen(skb);
1890
1891 do {
1892 struct sk_buff *nskb;
1893 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001894 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001895 int k;
1896 int size;
1897
1898 len = skb->len - offset;
1899 if (len > mss)
1900 len = mss;
1901
1902 hsize = skb_headlen(skb) - offset;
1903 if (hsize < 0)
1904 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001905 if (hsize > len || !sg)
1906 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001907
Herbert Xuc8884ed2006-10-29 15:59:41 -08001908 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001909 if (unlikely(!nskb))
1910 goto err;
1911
1912 if (segs)
1913 tail->next = nskb;
1914 else
1915 segs = nskb;
1916 tail = nskb;
1917
1918 nskb->dev = skb->dev;
1919 nskb->priority = skb->priority;
1920 nskb->protocol = skb->protocol;
1921 nskb->dst = dst_clone(skb->dst);
1922 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1923 nskb->pkt_type = skb->pkt_type;
1924 nskb->mac_len = skb->mac_len;
1925
1926 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001927 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001928 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001929 nskb->transport_header = (nskb->network_header +
1930 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001931 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1932 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001933 if (!sg) {
1934 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1935 skb_put(nskb, len),
1936 len, 0);
1937 continue;
1938 }
1939
1940 frag = skb_shinfo(nskb)->frags;
1941 k = 0;
1942
Patrick McHardy84fa7932006-08-29 16:44:56 -07001943 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001944 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001945 skb_copy_from_linear_data_offset(skb, offset,
1946 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001947
1948 while (pos < offset + len) {
1949 BUG_ON(i >= nfrags);
1950
1951 *frag = skb_shinfo(skb)->frags[i];
1952 get_page(frag->page);
1953 size = frag->size;
1954
1955 if (pos < offset) {
1956 frag->page_offset += offset - pos;
1957 frag->size -= offset - pos;
1958 }
1959
1960 k++;
1961
1962 if (pos + size <= offset + len) {
1963 i++;
1964 pos += size;
1965 } else {
1966 frag->size -= pos + size - (offset + len);
1967 break;
1968 }
1969
1970 frag++;
1971 }
1972
1973 skb_shinfo(nskb)->nr_frags = k;
1974 nskb->data_len = len - hsize;
1975 nskb->len += nskb->data_len;
1976 nskb->truesize += nskb->data_len;
1977 } while ((offset += len) < skb->len);
1978
1979 return segs;
1980
1981err:
1982 while ((skb = segs)) {
1983 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08001984 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001985 }
1986 return ERR_PTR(err);
1987}
1988
1989EXPORT_SYMBOL_GPL(skb_segment);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991void __init skb_init(void)
1992{
1993 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1994 sizeof(struct sk_buff),
1995 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07001996 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07001998 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1999 (2*sizeof(struct sk_buff)) +
2000 sizeof(atomic_t),
2001 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002002 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002003 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
David Howells716ea3a2007-04-02 20:19:53 -07002006/**
2007 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2008 * @skb: Socket buffer containing the buffers to be mapped
2009 * @sg: The scatter-gather list to map into
2010 * @offset: The offset into the buffer's contents to start mapping
2011 * @len: Length of buffer space to be mapped
2012 *
2013 * Fill the specified scatter-gather list with mappings/pointers into a
2014 * region of the buffer space attached to a socket buffer.
2015 */
2016int
2017skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2018{
2019 int start = skb_headlen(skb);
2020 int i, copy = start - offset;
2021 int elt = 0;
2022
2023 if (copy > 0) {
2024 if (copy > len)
2025 copy = len;
2026 sg[elt].page = virt_to_page(skb->data + offset);
2027 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
2028 sg[elt].length = copy;
2029 elt++;
2030 if ((len -= copy) == 0)
2031 return elt;
2032 offset += copy;
2033 }
2034
2035 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2036 int end;
2037
2038 BUG_TRAP(start <= offset + len);
2039
2040 end = start + skb_shinfo(skb)->frags[i].size;
2041 if ((copy = end - offset) > 0) {
2042 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2043
2044 if (copy > len)
2045 copy = len;
2046 sg[elt].page = frag->page;
2047 sg[elt].offset = frag->page_offset+offset-start;
2048 sg[elt].length = copy;
2049 elt++;
2050 if (!(len -= copy))
2051 return elt;
2052 offset += copy;
2053 }
2054 start = end;
2055 }
2056
2057 if (skb_shinfo(skb)->frag_list) {
2058 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2059
2060 for (; list; list = list->next) {
2061 int end;
2062
2063 BUG_TRAP(start <= offset + len);
2064
2065 end = start + list->len;
2066 if ((copy = end - offset) > 0) {
2067 if (copy > len)
2068 copy = len;
2069 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
2070 if ((len -= copy) == 0)
2071 return elt;
2072 offset += copy;
2073 }
2074 start = end;
2075 }
2076 }
2077 BUG_ON(len);
2078 return elt;
2079}
2080
2081/**
2082 * skb_cow_data - Check that a socket buffer's data buffers are writable
2083 * @skb: The socket buffer to check.
2084 * @tailbits: Amount of trailing space to be added
2085 * @trailer: Returned pointer to the skb where the @tailbits space begins
2086 *
2087 * Make sure that the data buffers attached to a socket buffer are
2088 * writable. If they are not, private copies are made of the data buffers
2089 * and the socket buffer is set to use these instead.
2090 *
2091 * If @tailbits is given, make sure that there is space to write @tailbits
2092 * bytes of data beyond current end of socket buffer. @trailer will be
2093 * set to point to the skb in which this space begins.
2094 *
2095 * The number of scatterlist elements required to completely map the
2096 * COW'd and extended socket buffer will be returned.
2097 */
2098int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2099{
2100 int copyflag;
2101 int elt;
2102 struct sk_buff *skb1, **skb_p;
2103
2104 /* If skb is cloned or its head is paged, reallocate
2105 * head pulling out all the pages (pages are considered not writable
2106 * at the moment even if they are anonymous).
2107 */
2108 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2109 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2110 return -ENOMEM;
2111
2112 /* Easy case. Most of packets will go this way. */
2113 if (!skb_shinfo(skb)->frag_list) {
2114 /* A little of trouble, not enough of space for trailer.
2115 * This should not happen, when stack is tuned to generate
2116 * good frames. OK, on miss we reallocate and reserve even more
2117 * space, 128 bytes is fair. */
2118
2119 if (skb_tailroom(skb) < tailbits &&
2120 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2121 return -ENOMEM;
2122
2123 /* Voila! */
2124 *trailer = skb;
2125 return 1;
2126 }
2127
2128 /* Misery. We are in troubles, going to mincer fragments... */
2129
2130 elt = 1;
2131 skb_p = &skb_shinfo(skb)->frag_list;
2132 copyflag = 0;
2133
2134 while ((skb1 = *skb_p) != NULL) {
2135 int ntail = 0;
2136
2137 /* The fragment is partially pulled by someone,
2138 * this can happen on input. Copy it and everything
2139 * after it. */
2140
2141 if (skb_shared(skb1))
2142 copyflag = 1;
2143
2144 /* If the skb is the last, worry about trailer. */
2145
2146 if (skb1->next == NULL && tailbits) {
2147 if (skb_shinfo(skb1)->nr_frags ||
2148 skb_shinfo(skb1)->frag_list ||
2149 skb_tailroom(skb1) < tailbits)
2150 ntail = tailbits + 128;
2151 }
2152
2153 if (copyflag ||
2154 skb_cloned(skb1) ||
2155 ntail ||
2156 skb_shinfo(skb1)->nr_frags ||
2157 skb_shinfo(skb1)->frag_list) {
2158 struct sk_buff *skb2;
2159
2160 /* Fuck, we are miserable poor guys... */
2161 if (ntail == 0)
2162 skb2 = skb_copy(skb1, GFP_ATOMIC);
2163 else
2164 skb2 = skb_copy_expand(skb1,
2165 skb_headroom(skb1),
2166 ntail,
2167 GFP_ATOMIC);
2168 if (unlikely(skb2 == NULL))
2169 return -ENOMEM;
2170
2171 if (skb1->sk)
2172 skb_set_owner_w(skb2, skb1->sk);
2173
2174 /* Looking around. Are we still alive?
2175 * OK, link new skb, drop old one */
2176
2177 skb2->next = skb1->next;
2178 *skb_p = skb2;
2179 kfree_skb(skb1);
2180 skb1 = skb2;
2181 }
2182 elt++;
2183 *trailer = skb1;
2184 skb_p = &skb1->next;
2185 }
2186
2187 return elt;
2188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190EXPORT_SYMBOL(___pskb_trim);
2191EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002192EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002194EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002195EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196EXPORT_SYMBOL(pskb_copy);
2197EXPORT_SYMBOL(pskb_expand_head);
2198EXPORT_SYMBOL(skb_checksum);
2199EXPORT_SYMBOL(skb_clone);
2200EXPORT_SYMBOL(skb_clone_fraglist);
2201EXPORT_SYMBOL(skb_copy);
2202EXPORT_SYMBOL(skb_copy_and_csum_bits);
2203EXPORT_SYMBOL(skb_copy_and_csum_dev);
2204EXPORT_SYMBOL(skb_copy_bits);
2205EXPORT_SYMBOL(skb_copy_expand);
2206EXPORT_SYMBOL(skb_over_panic);
2207EXPORT_SYMBOL(skb_pad);
2208EXPORT_SYMBOL(skb_realloc_headroom);
2209EXPORT_SYMBOL(skb_under_panic);
2210EXPORT_SYMBOL(skb_dequeue);
2211EXPORT_SYMBOL(skb_dequeue_tail);
2212EXPORT_SYMBOL(skb_insert);
2213EXPORT_SYMBOL(skb_queue_purge);
2214EXPORT_SYMBOL(skb_queue_head);
2215EXPORT_SYMBOL(skb_queue_tail);
2216EXPORT_SYMBOL(skb_unlink);
2217EXPORT_SYMBOL(skb_append);
2218EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002219EXPORT_SYMBOL(skb_prepare_seq_read);
2220EXPORT_SYMBOL(skb_seq_read);
2221EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002222EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002223EXPORT_SYMBOL(skb_append_datato_frags);
David Howells716ea3a2007-04-02 20:19:53 -07002224
2225EXPORT_SYMBOL_GPL(skb_to_sgvec);
2226EXPORT_SYMBOL_GPL(skb_cow_data);