blob: a48b086812615cb2534231e52717c4fe560e4e6c [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 Viroa1f8e7f2006-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 "
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090090 "data:%p tail:%p end:%p dev:%s\n",
Patrick McHardy26095452005-04-21 16:43:02 -070091 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 BUG();
94}
95
96/**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106{
Patrick McHardy26095452005-04-21 16:43:02 -0700107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900108 "data:%p tail:%p end:%p dev:%s\n",
Patrick McHardy26095452005-04-21 16:43:02 -0700109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 BUG();
112}
113
David S. Millerdc6de332006-04-20 00:10:50 -0700114void skb_truesize_bug(struct sk_buff *skb)
115{
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
119}
120EXPORT_SYMBOL(skb_truesize_bug);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
124 * [BEEP] leaks.
125 *
126 */
127
128/**
David S. Millerd179cd12005-08-17 14:57:30 -0700129 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * @size: size to allocate
131 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800134 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
Al Virodd0fc662005-10-07 07:46:04 +0100143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800144 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Christoph Lametere18b8902006-12-06 20:33:20 -0800146 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800147 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct sk_buff *skb;
149 u8 *data;
150
Herbert Xu8798b3f2006-01-23 16:32:45 -0800151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800154 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800160 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
161 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!data)
163 goto nodata;
164
165 memset(skb, 0, offsetof(struct sk_buff, truesize));
166 skb->truesize = size + sizeof(struct sk_buff);
167 atomic_set(&skb->users, 1);
168 skb->head = data;
169 skb->data = data;
170 skb->tail = data;
171 skb->end = data + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800172 /* make sure we initialize shinfo sequentially */
173 shinfo = skb_shinfo(skb);
174 atomic_set(&shinfo->dataref, 1);
175 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700176 shinfo->gso_size = 0;
177 shinfo->gso_segs = 0;
178 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800179 shinfo->ip6_frag_id = 0;
180 shinfo->frag_list = NULL;
181
David S. Millerd179cd12005-08-17 14:57:30 -0700182 if (fclone) {
183 struct sk_buff *child = skb + 1;
184 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David S. Millerd179cd12005-08-17 14:57:30 -0700186 skb->fclone = SKB_FCLONE_ORIG;
187 atomic_set(fclone_ref, 1);
188
189 child->fclone = SKB_FCLONE_UNAVAILABLE;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191out:
192 return skb;
193nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800194 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 skb = NULL;
196 goto out;
197}
198
199/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700200 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
201 * @dev: network device to receive on
202 * @length: length to allocate
203 * @gfp_mask: get_free_pages mask, passed to alloc_skb
204 *
205 * Allocate a new &sk_buff and assign it a usage count of one. The
206 * buffer has unspecified headroom built in. Users should allocate
207 * the headroom they think they need without accounting for the
208 * built in space. The built in space is used for optimisations.
209 *
210 * %NULL is returned if there is no free memory.
211 */
212struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
213 unsigned int length, gfp_t gfp_mask)
214{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700215 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700216 struct sk_buff *skb;
217
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900218 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700219 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700220 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700221 skb->dev = dev;
222 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700223 return skb;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Herbert Xu27b437c2006-07-13 19:26:39 -0700226static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Herbert Xu27b437c2006-07-13 19:26:39 -0700228 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Herbert Xu27b437c2006-07-13 19:26:39 -0700230 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 do {
233 struct sk_buff *this = list;
234 list = list->next;
235 kfree_skb(this);
236 } while (list);
237}
238
Herbert Xu27b437c2006-07-13 19:26:39 -0700239static inline void skb_drop_fraglist(struct sk_buff *skb)
240{
241 skb_drop_list(&skb_shinfo(skb)->frag_list);
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static void skb_clone_fraglist(struct sk_buff *skb)
245{
246 struct sk_buff *list;
247
248 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
249 skb_get(list);
250}
251
Adrian Bunk5bba1712006-06-29 13:02:35 -0700252static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 if (!skb->cloned ||
255 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
256 &skb_shinfo(skb)->dataref)) {
257 if (skb_shinfo(skb)->nr_frags) {
258 int i;
259 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
260 put_page(skb_shinfo(skb)->frags[i].page);
261 }
262
263 if (skb_shinfo(skb)->frag_list)
264 skb_drop_fraglist(skb);
265
266 kfree(skb->head);
267 }
268}
269
270/*
271 * Free an skbuff by memory without cleaning the state.
272 */
273void kfree_skbmem(struct sk_buff *skb)
274{
David S. Millerd179cd12005-08-17 14:57:30 -0700275 struct sk_buff *other;
276 atomic_t *fclone_ref;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700279 switch (skb->fclone) {
280 case SKB_FCLONE_UNAVAILABLE:
281 kmem_cache_free(skbuff_head_cache, skb);
282 break;
283
284 case SKB_FCLONE_ORIG:
285 fclone_ref = (atomic_t *) (skb + 2);
286 if (atomic_dec_and_test(fclone_ref))
287 kmem_cache_free(skbuff_fclone_cache, skb);
288 break;
289
290 case SKB_FCLONE_CLONE:
291 fclone_ref = (atomic_t *) (skb + 1);
292 other = skb - 1;
293
294 /* The clone portion is available for
295 * fast-cloning again.
296 */
297 skb->fclone = SKB_FCLONE_UNAVAILABLE;
298
299 if (atomic_dec_and_test(fclone_ref))
300 kmem_cache_free(skbuff_fclone_cache, other);
301 break;
302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/**
306 * __kfree_skb - private function
307 * @skb: buffer
308 *
309 * Free an sk_buff. Release anything attached to the buffer.
310 * Clean the state. This is an internal helper function. Users should
311 * always call kfree_skb
312 */
313
314void __kfree_skb(struct sk_buff *skb)
315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dst_release(skb->dst);
317#ifdef CONFIG_XFRM
318 secpath_put(skb->sp);
319#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700320 if (skb->destructor) {
321 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 skb->destructor(skb);
323 }
324#ifdef CONFIG_NETFILTER
325 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800326#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
327 nf_conntrack_put_reasm(skb->nfct_reasm);
328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#ifdef CONFIG_BRIDGE_NETFILTER
330 nf_bridge_put(skb->nf_bridge);
331#endif
332#endif
333/* XXX: IS this still necessary? - JHS */
334#ifdef CONFIG_NET_SCHED
335 skb->tc_index = 0;
336#ifdef CONFIG_NET_CLS_ACT
337 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
339#endif
340
341 kfree_skbmem(skb);
342}
343
344/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800345 * kfree_skb - free an sk_buff
346 * @skb: buffer to free
347 *
348 * Drop a reference to the buffer and free it if the usage count has
349 * hit zero.
350 */
351void kfree_skb(struct sk_buff *skb)
352{
353 if (unlikely(!skb))
354 return;
355 if (likely(atomic_read(&skb->users) == 1))
356 smp_rmb();
357 else if (likely(!atomic_dec_and_test(&skb->users)))
358 return;
359 __kfree_skb(skb);
360}
361
362/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * skb_clone - duplicate an sk_buff
364 * @skb: buffer to clone
365 * @gfp_mask: allocation priority
366 *
367 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
368 * copies share the same packet data but not structure. The new
369 * buffer has a reference count of 1. If the allocation fails the
370 * function returns %NULL otherwise the new buffer is returned.
371 *
372 * If this function is called from an interrupt gfp_mask() must be
373 * %GFP_ATOMIC.
374 */
375
Al Virodd0fc662005-10-07 07:46:04 +0100376struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
David S. Millerd179cd12005-08-17 14:57:30 -0700378 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David S. Millerd179cd12005-08-17 14:57:30 -0700380 n = skb + 1;
381 if (skb->fclone == SKB_FCLONE_ORIG &&
382 n->fclone == SKB_FCLONE_UNAVAILABLE) {
383 atomic_t *fclone_ref = (atomic_t *) (n + 1);
384 n->fclone = SKB_FCLONE_CLONE;
385 atomic_inc(fclone_ref);
386 } else {
387 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
388 if (!n)
389 return NULL;
390 n->fclone = SKB_FCLONE_UNAVAILABLE;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393#define C(x) n->x = skb->x
394
395 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700397 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 C(dev);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700399 C(transport_header);
400 C(network_header);
401 C(mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 C(dst);
403 dst_clone(skb->dst);
404 C(sp);
405#ifdef CONFIG_INET
406 secpath_get(skb->sp);
407#endif
408 memcpy(n->cb, skb->cb, sizeof(skb->cb));
409 C(len);
410 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700411 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 C(csum);
413 C(local_df);
414 n->cloned = 1;
415 n->nohdr = 0;
416 C(pkt_type);
417 C(ip_summed);
418 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800419#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
420 C(ipvs_property);
421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800424 C(mark);
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700425 __nf_copy(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426#ifdef CONFIG_NET_SCHED
427 C(tc_index);
428#ifdef CONFIG_NET_CLS_ACT
429 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700430 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
431 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700432 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#endif
James Morris984bc162006-06-09 00:29:17 -0700434 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#endif
436 C(truesize);
437 atomic_set(&n->users, 1);
438 C(head);
439 C(data);
440 C(tail);
441 C(end);
442
443 atomic_inc(&(skb_shinfo(skb)->dataref));
444 skb->cloned = 1;
445
446 return n;
447}
448
449static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
450{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700451#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 * Shift between the two data areas in bytes
454 */
455 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700456#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 new->sk = NULL;
458 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 new->priority = old->priority;
460 new->protocol = old->protocol;
461 new->dst = dst_clone(old->dst);
462#ifdef CONFIG_INET
463 new->sp = secpath_get(old->sp);
464#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700465 new->transport_header = old->transport_header;
466 new->network_header = old->network_header;
467 new->mac_header = old->mac_header;
468#ifndef NET_SKBUFF_DATA_USES_OFFSET
469 /* {transport,network,mac}_header are relative to skb->head */
470 new->transport_header += offset;
471 new->network_header += offset;
472 new->mac_header += offset;
473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 memcpy(new->cb, old->cb, sizeof(old->cb));
475 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700476 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700478 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800480 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700481 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300482#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
483 new->ipvs_property = old->ipvs_property;
484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#ifdef CONFIG_NET_SCHED
486#ifdef CONFIG_NET_CLS_ACT
487 new->tc_verd = old->tc_verd;
488#endif
489 new->tc_index = old->tc_index;
490#endif
James Morris984bc162006-06-09 00:29:17 -0700491 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700493 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
494 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
495 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/**
499 * skb_copy - create private copy of an sk_buff
500 * @skb: buffer to copy
501 * @gfp_mask: allocation priority
502 *
503 * Make a copy of both an &sk_buff and its data. This is used when the
504 * caller wishes to modify the data and needs a private copy of the
505 * data to alter. Returns %NULL on failure or the pointer to the buffer
506 * on success. The returned buffer has a reference count of 1.
507 *
508 * As by-product this function converts non-linear &sk_buff to linear
509 * one, so that &sk_buff becomes completely private and caller is allowed
510 * to modify all the data of returned buffer. This means that this
511 * function is not recommended for use in circumstances when only
512 * header is going to be modified. Use pskb_copy() instead.
513 */
514
Al Virodd0fc662005-10-07 07:46:04 +0100515struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 int headerlen = skb->data - skb->head;
518 /*
519 * Allocate the copy buffer
520 */
521 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
522 gfp_mask);
523 if (!n)
524 return NULL;
525
526 /* Set the data pointer */
527 skb_reserve(n, headerlen);
528 /* Set the tail pointer and length */
529 skb_put(n, skb->len);
530 n->csum = skb->csum;
531 n->ip_summed = skb->ip_summed;
532
533 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
534 BUG();
535
536 copy_skb_header(n, skb);
537 return n;
538}
539
540
541/**
542 * pskb_copy - create copy of an sk_buff with private head.
543 * @skb: buffer to copy
544 * @gfp_mask: allocation priority
545 *
546 * Make a copy of both an &sk_buff and part of its data, located
547 * in header. Fragmented data remain shared. This is used when
548 * the caller wishes to modify only header of &sk_buff and needs
549 * private copy of the header to alter. Returns %NULL on failure
550 * or the pointer to the buffer on success.
551 * The returned buffer has a reference count of 1.
552 */
553
Al Virodd0fc662005-10-07 07:46:04 +0100554struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 /*
557 * Allocate the copy buffer
558 */
559 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
560
561 if (!n)
562 goto out;
563
564 /* Set the data pointer */
565 skb_reserve(n, skb->data - skb->head);
566 /* Set the tail pointer and length */
567 skb_put(n, skb_headlen(skb));
568 /* Copy the bytes */
569 memcpy(n->data, skb->data, n->len);
570 n->csum = skb->csum;
571 n->ip_summed = skb->ip_summed;
572
Herbert Xu25f484a2006-11-07 14:57:15 -0800573 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 n->data_len = skb->data_len;
575 n->len = skb->len;
576
577 if (skb_shinfo(skb)->nr_frags) {
578 int i;
579
580 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
581 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
582 get_page(skb_shinfo(n)->frags[i].page);
583 }
584 skb_shinfo(n)->nr_frags = i;
585 }
586
587 if (skb_shinfo(skb)->frag_list) {
588 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
589 skb_clone_fraglist(n);
590 }
591
592 copy_skb_header(n, skb);
593out:
594 return n;
595}
596
597/**
598 * pskb_expand_head - reallocate header of &sk_buff
599 * @skb: buffer to reallocate
600 * @nhead: room to add at head
601 * @ntail: room to add at tail
602 * @gfp_mask: allocation priority
603 *
604 * Expands (or creates identical copy, if &nhead and &ntail are zero)
605 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
606 * reference count of 1. Returns zero in the case of success or error,
607 * if expansion failed. In the last case, &sk_buff is not changed.
608 *
609 * All the pointers pointing into skb header may change and must be
610 * reloaded after call to this function.
611 */
612
Victor Fusco86a76ca2005-07-08 14:57:47 -0700613int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100614 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int i;
617 u8 *data;
618 int size = nhead + (skb->end - skb->head) + ntail;
619 long off;
620
621 if (skb_shared(skb))
622 BUG();
623
624 size = SKB_DATA_ALIGN(size);
625
626 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
627 if (!data)
628 goto nodata;
629
630 /* Copy only real data... and, alas, header. This should be
631 * optimized for the cases when header is void. */
632 memcpy(data + nhead, skb->head, skb->tail - skb->head);
633 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
634
635 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
636 get_page(skb_shinfo(skb)->frags[i].page);
637
638 if (skb_shinfo(skb)->frag_list)
639 skb_clone_fraglist(skb);
640
641 skb_release_data(skb);
642
643 off = (data + nhead) - skb->head;
644
645 skb->head = data;
646 skb->end = data + size;
647 skb->data += off;
648 skb->tail += off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700649#ifndef NET_SKBUFF_DATA_USES_OFFSET
650 /* {transport,network,mac}_header are relative to skb->head */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700651 skb->transport_header += off;
652 skb->network_header += off;
653 skb->mac_header += off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 skb->cloned = 0;
656 skb->nohdr = 0;
657 atomic_set(&skb_shinfo(skb)->dataref, 1);
658 return 0;
659
660nodata:
661 return -ENOMEM;
662}
663
664/* Make private copy of skb with writable head and some headroom */
665
666struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
667{
668 struct sk_buff *skb2;
669 int delta = headroom - skb_headroom(skb);
670
671 if (delta <= 0)
672 skb2 = pskb_copy(skb, GFP_ATOMIC);
673 else {
674 skb2 = skb_clone(skb, GFP_ATOMIC);
675 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
676 GFP_ATOMIC)) {
677 kfree_skb(skb2);
678 skb2 = NULL;
679 }
680 }
681 return skb2;
682}
683
684
685/**
686 * skb_copy_expand - copy and expand sk_buff
687 * @skb: buffer to copy
688 * @newheadroom: new free bytes at head
689 * @newtailroom: new free bytes at tail
690 * @gfp_mask: allocation priority
691 *
692 * Make a copy of both an &sk_buff and its data and while doing so
693 * allocate additional space.
694 *
695 * This is used when the caller wishes to modify the data and needs a
696 * private copy of the data to alter as well as more space for new fields.
697 * Returns %NULL on failure or the pointer to the buffer
698 * on success. The returned buffer has a reference count of 1.
699 *
700 * You must pass %GFP_ATOMIC as the allocation priority if this function
701 * is called from an interrupt.
702 *
703 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
704 * only by netfilter in the cases when checksum is recalculated? --ANK
705 */
706struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700707 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100708 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 /*
711 * Allocate the copy buffer
712 */
713 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
714 gfp_mask);
715 int head_copy_len, head_copy_off;
716
717 if (!n)
718 return NULL;
719
720 skb_reserve(n, newheadroom);
721
722 /* Set the tail pointer and length */
723 skb_put(n, skb->len);
724
725 head_copy_len = skb_headroom(skb);
726 head_copy_off = 0;
727 if (newheadroom <= head_copy_len)
728 head_copy_len = newheadroom;
729 else
730 head_copy_off = newheadroom - head_copy_len;
731
732 /* Copy the linear header and data. */
733 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
734 skb->len + head_copy_len))
735 BUG();
736
737 copy_skb_header(n, skb);
738
739 return n;
740}
741
742/**
743 * skb_pad - zero pad the tail of an skb
744 * @skb: buffer to pad
745 * @pad: space to pad
746 *
747 * Ensure that a buffer is followed by a padding area that is zero
748 * filled. Used by network drivers which may DMA or transfer data
749 * beyond the buffer end onto the wire.
750 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700751 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900753
Herbert Xu5b057c62006-06-23 02:06:41 -0700754int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Herbert Xu5b057c62006-06-23 02:06:41 -0700756 int err;
757 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700760 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700764
765 ntail = skb->data_len + pad - (skb->end - skb->tail);
766 if (likely(skb_cloned(skb) || ntail > 0)) {
767 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
768 if (unlikely(err))
769 goto free_skb;
770 }
771
772 /* FIXME: The use of this function with non-linear skb's really needs
773 * to be audited.
774 */
775 err = skb_linearize(skb);
776 if (unlikely(err))
777 goto free_skb;
778
779 memset(skb->data + skb->len, 0, pad);
780 return 0;
781
782free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700784 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900785}
786
Herbert Xu3cc0e872006-06-09 16:13:38 -0700787/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
789
Herbert Xu3cc0e872006-06-09 16:13:38 -0700790int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Herbert Xu27b437c2006-07-13 19:26:39 -0700792 struct sk_buff **fragp;
793 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int offset = skb_headlen(skb);
795 int nfrags = skb_shinfo(skb)->nr_frags;
796 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700797 int err;
798
799 if (skb_cloned(skb) &&
800 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700803 i = 0;
804 if (offset >= len)
805 goto drop_pages;
806
807 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700809
810 if (end < len) {
811 offset = end;
812 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700814
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700815 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700816
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700817drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700818 skb_shinfo(skb)->nr_frags = i;
819
820 for (; i < nfrags; i++)
821 put_page(skb_shinfo(skb)->frags[i].page);
822
823 if (skb_shinfo(skb)->frag_list)
824 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700825 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Herbert Xu27b437c2006-07-13 19:26:39 -0700828 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
829 fragp = &frag->next) {
830 int end = offset + frag->len;
831
832 if (skb_shared(frag)) {
833 struct sk_buff *nfrag;
834
835 nfrag = skb_clone(frag, GFP_ATOMIC);
836 if (unlikely(!nfrag))
837 return -ENOMEM;
838
839 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700840 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700841 frag = nfrag;
842 *fragp = frag;
843 }
844
845 if (end < len) {
846 offset = end;
847 continue;
848 }
849
850 if (end > len &&
851 unlikely((err = pskb_trim(frag, len - offset))))
852 return err;
853
854 if (frag->next)
855 skb_drop_list(&frag->next);
856 break;
857 }
858
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700859done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700860 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 skb->data_len -= skb->len - len;
862 skb->len = len;
863 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700864 skb->len = len;
865 skb->data_len = 0;
866 skb->tail = skb->data + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 return 0;
870}
871
872/**
873 * __pskb_pull_tail - advance tail of skb header
874 * @skb: buffer to reallocate
875 * @delta: number of bytes to advance tail
876 *
877 * The function makes a sense only on a fragmented &sk_buff,
878 * it expands header moving its tail forward and copying necessary
879 * data from fragmented part.
880 *
881 * &sk_buff MUST have reference count of 1.
882 *
883 * Returns %NULL (and &sk_buff does not change) if pull failed
884 * or value of new tail of skb in the case of success.
885 *
886 * All the pointers pointing into skb header may change and must be
887 * reloaded after call to this function.
888 */
889
890/* Moves tail of skb head forward, copying data from fragmented part,
891 * when it is necessary.
892 * 1. It may fail due to malloc failure.
893 * 2. It may change skb pointers.
894 *
895 * It is pretty complicated. Luckily, it is called only in exceptional cases.
896 */
897unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
898{
899 /* If skb has not enough free space at tail, get new one
900 * plus 128 bytes for future expansions. If we have enough
901 * room at tail, reallocate without expansion only if skb is cloned.
902 */
903 int i, k, eat = (skb->tail + delta) - skb->end;
904
905 if (eat > 0 || skb_cloned(skb)) {
906 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
907 GFP_ATOMIC))
908 return NULL;
909 }
910
911 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
912 BUG();
913
914 /* Optimization: no fragments, no reasons to preestimate
915 * size of pulled pages. Superb.
916 */
917 if (!skb_shinfo(skb)->frag_list)
918 goto pull_pages;
919
920 /* Estimate size of pulled pages. */
921 eat = delta;
922 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
923 if (skb_shinfo(skb)->frags[i].size >= eat)
924 goto pull_pages;
925 eat -= skb_shinfo(skb)->frags[i].size;
926 }
927
928 /* If we need update frag list, we are in troubles.
929 * Certainly, it possible to add an offset to skb data,
930 * but taking into account that pulling is expected to
931 * be very rare operation, it is worth to fight against
932 * further bloating skb head and crucify ourselves here instead.
933 * Pure masohism, indeed. 8)8)
934 */
935 if (eat) {
936 struct sk_buff *list = skb_shinfo(skb)->frag_list;
937 struct sk_buff *clone = NULL;
938 struct sk_buff *insp = NULL;
939
940 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800941 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (list->len <= eat) {
944 /* Eaten as whole. */
945 eat -= list->len;
946 list = list->next;
947 insp = list;
948 } else {
949 /* Eaten partially. */
950
951 if (skb_shared(list)) {
952 /* Sucks! We need to fork list. :-( */
953 clone = skb_clone(list, GFP_ATOMIC);
954 if (!clone)
955 return NULL;
956 insp = list->next;
957 list = clone;
958 } else {
959 /* This may be pulled without
960 * problems. */
961 insp = list;
962 }
963 if (!pskb_pull(list, eat)) {
964 if (clone)
965 kfree_skb(clone);
966 return NULL;
967 }
968 break;
969 }
970 } while (eat);
971
972 /* Free pulled out fragments. */
973 while ((list = skb_shinfo(skb)->frag_list) != insp) {
974 skb_shinfo(skb)->frag_list = list->next;
975 kfree_skb(list);
976 }
977 /* And insert new clone at head. */
978 if (clone) {
979 clone->next = list;
980 skb_shinfo(skb)->frag_list = clone;
981 }
982 }
983 /* Success! Now we may commit changes to skb data. */
984
985pull_pages:
986 eat = delta;
987 k = 0;
988 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
989 if (skb_shinfo(skb)->frags[i].size <= eat) {
990 put_page(skb_shinfo(skb)->frags[i].page);
991 eat -= skb_shinfo(skb)->frags[i].size;
992 } else {
993 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
994 if (eat) {
995 skb_shinfo(skb)->frags[k].page_offset += eat;
996 skb_shinfo(skb)->frags[k].size -= eat;
997 eat = 0;
998 }
999 k++;
1000 }
1001 }
1002 skb_shinfo(skb)->nr_frags = k;
1003
1004 skb->tail += delta;
1005 skb->data_len -= delta;
1006
1007 return skb->tail;
1008}
1009
1010/* Copy some data bits from skb to kernel buffer. */
1011
1012int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1013{
1014 int i, copy;
1015 int start = skb_headlen(skb);
1016
1017 if (offset > (int)skb->len - len)
1018 goto fault;
1019
1020 /* Copy header. */
1021 if ((copy = start - offset) > 0) {
1022 if (copy > len)
1023 copy = len;
1024 memcpy(to, skb->data + offset, copy);
1025 if ((len -= copy) == 0)
1026 return 0;
1027 offset += copy;
1028 to += copy;
1029 }
1030
1031 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1032 int end;
1033
1034 BUG_TRAP(start <= offset + len);
1035
1036 end = start + skb_shinfo(skb)->frags[i].size;
1037 if ((copy = end - offset) > 0) {
1038 u8 *vaddr;
1039
1040 if (copy > len)
1041 copy = len;
1042
1043 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1044 memcpy(to,
1045 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1046 offset - start, copy);
1047 kunmap_skb_frag(vaddr);
1048
1049 if ((len -= copy) == 0)
1050 return 0;
1051 offset += copy;
1052 to += copy;
1053 }
1054 start = end;
1055 }
1056
1057 if (skb_shinfo(skb)->frag_list) {
1058 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1059
1060 for (; list; list = list->next) {
1061 int end;
1062
1063 BUG_TRAP(start <= offset + len);
1064
1065 end = start + list->len;
1066 if ((copy = end - offset) > 0) {
1067 if (copy > len)
1068 copy = len;
1069 if (skb_copy_bits(list, offset - start,
1070 to, copy))
1071 goto fault;
1072 if ((len -= copy) == 0)
1073 return 0;
1074 offset += copy;
1075 to += copy;
1076 }
1077 start = end;
1078 }
1079 }
1080 if (!len)
1081 return 0;
1082
1083fault:
1084 return -EFAULT;
1085}
1086
Herbert Xu357b40a2005-04-19 22:30:14 -07001087/**
1088 * skb_store_bits - store bits from kernel buffer to skb
1089 * @skb: destination buffer
1090 * @offset: offset in destination
1091 * @from: source buffer
1092 * @len: number of bytes to copy
1093 *
1094 * Copy the specified number of bytes from the source buffer to the
1095 * destination skb. This function handles all the messy bits of
1096 * traversing fragment lists and such.
1097 */
1098
1099int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1100{
1101 int i, copy;
1102 int start = skb_headlen(skb);
1103
1104 if (offset > (int)skb->len - len)
1105 goto fault;
1106
1107 if ((copy = start - offset) > 0) {
1108 if (copy > len)
1109 copy = len;
1110 memcpy(skb->data + offset, from, copy);
1111 if ((len -= copy) == 0)
1112 return 0;
1113 offset += copy;
1114 from += copy;
1115 }
1116
1117 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1118 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1119 int end;
1120
1121 BUG_TRAP(start <= offset + len);
1122
1123 end = start + frag->size;
1124 if ((copy = end - offset) > 0) {
1125 u8 *vaddr;
1126
1127 if (copy > len)
1128 copy = len;
1129
1130 vaddr = kmap_skb_frag(frag);
1131 memcpy(vaddr + frag->page_offset + offset - start,
1132 from, copy);
1133 kunmap_skb_frag(vaddr);
1134
1135 if ((len -= copy) == 0)
1136 return 0;
1137 offset += copy;
1138 from += copy;
1139 }
1140 start = end;
1141 }
1142
1143 if (skb_shinfo(skb)->frag_list) {
1144 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1145
1146 for (; list; list = list->next) {
1147 int end;
1148
1149 BUG_TRAP(start <= offset + len);
1150
1151 end = start + list->len;
1152 if ((copy = end - offset) > 0) {
1153 if (copy > len)
1154 copy = len;
1155 if (skb_store_bits(list, offset - start,
1156 from, copy))
1157 goto fault;
1158 if ((len -= copy) == 0)
1159 return 0;
1160 offset += copy;
1161 from += copy;
1162 }
1163 start = end;
1164 }
1165 }
1166 if (!len)
1167 return 0;
1168
1169fault:
1170 return -EFAULT;
1171}
1172
1173EXPORT_SYMBOL(skb_store_bits);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/* Checksum skb data. */
1176
Al Viro2bbbc862006-11-14 21:37:14 -08001177__wsum skb_checksum(const struct sk_buff *skb, int offset,
1178 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 int start = skb_headlen(skb);
1181 int i, copy = start - offset;
1182 int pos = 0;
1183
1184 /* Checksum header. */
1185 if (copy > 0) {
1186 if (copy > len)
1187 copy = len;
1188 csum = csum_partial(skb->data + offset, copy, csum);
1189 if ((len -= copy) == 0)
1190 return csum;
1191 offset += copy;
1192 pos = copy;
1193 }
1194
1195 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1196 int end;
1197
1198 BUG_TRAP(start <= offset + len);
1199
1200 end = start + skb_shinfo(skb)->frags[i].size;
1201 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001202 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 u8 *vaddr;
1204 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1205
1206 if (copy > len)
1207 copy = len;
1208 vaddr = kmap_skb_frag(frag);
1209 csum2 = csum_partial(vaddr + frag->page_offset +
1210 offset - start, copy, 0);
1211 kunmap_skb_frag(vaddr);
1212 csum = csum_block_add(csum, csum2, pos);
1213 if (!(len -= copy))
1214 return csum;
1215 offset += copy;
1216 pos += copy;
1217 }
1218 start = end;
1219 }
1220
1221 if (skb_shinfo(skb)->frag_list) {
1222 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1223
1224 for (; list; list = list->next) {
1225 int end;
1226
1227 BUG_TRAP(start <= offset + len);
1228
1229 end = start + list->len;
1230 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001231 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (copy > len)
1233 copy = len;
1234 csum2 = skb_checksum(list, offset - start,
1235 copy, 0);
1236 csum = csum_block_add(csum, csum2, pos);
1237 if ((len -= copy) == 0)
1238 return csum;
1239 offset += copy;
1240 pos += copy;
1241 }
1242 start = end;
1243 }
1244 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001245 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 return csum;
1248}
1249
1250/* Both of above in one bottle. */
1251
Al Viro81d77662006-11-14 21:37:33 -08001252__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1253 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 int start = skb_headlen(skb);
1256 int i, copy = start - offset;
1257 int pos = 0;
1258
1259 /* Copy header. */
1260 if (copy > 0) {
1261 if (copy > len)
1262 copy = len;
1263 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1264 copy, csum);
1265 if ((len -= copy) == 0)
1266 return csum;
1267 offset += copy;
1268 to += copy;
1269 pos = copy;
1270 }
1271
1272 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1273 int end;
1274
1275 BUG_TRAP(start <= offset + len);
1276
1277 end = start + skb_shinfo(skb)->frags[i].size;
1278 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001279 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 u8 *vaddr;
1281 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1282
1283 if (copy > len)
1284 copy = len;
1285 vaddr = kmap_skb_frag(frag);
1286 csum2 = csum_partial_copy_nocheck(vaddr +
1287 frag->page_offset +
1288 offset - start, to,
1289 copy, 0);
1290 kunmap_skb_frag(vaddr);
1291 csum = csum_block_add(csum, csum2, pos);
1292 if (!(len -= copy))
1293 return csum;
1294 offset += copy;
1295 to += copy;
1296 pos += copy;
1297 }
1298 start = end;
1299 }
1300
1301 if (skb_shinfo(skb)->frag_list) {
1302 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1303
1304 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001305 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int end;
1307
1308 BUG_TRAP(start <= offset + len);
1309
1310 end = start + list->len;
1311 if ((copy = end - offset) > 0) {
1312 if (copy > len)
1313 copy = len;
1314 csum2 = skb_copy_and_csum_bits(list,
1315 offset - start,
1316 to, copy, 0);
1317 csum = csum_block_add(csum, csum2, pos);
1318 if ((len -= copy) == 0)
1319 return csum;
1320 offset += copy;
1321 to += copy;
1322 pos += copy;
1323 }
1324 start = end;
1325 }
1326 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001327 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return csum;
1329}
1330
1331void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1332{
Al Virod3bc23e2006-11-14 21:24:49 -08001333 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 long csstart;
1335
Patrick McHardy84fa7932006-08-29 16:44:56 -07001336 if (skb->ip_summed == CHECKSUM_PARTIAL)
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001337 csstart = skb_transport_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 else
1339 csstart = skb_headlen(skb);
1340
Kris Katterjohn09a62662006-01-08 22:24:28 -08001341 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 memcpy(to, skb->data, csstart);
1344
1345 csum = 0;
1346 if (csstart != skb->len)
1347 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1348 skb->len - csstart, 0);
1349
Patrick McHardy84fa7932006-08-29 16:44:56 -07001350 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001351 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Al Virod3bc23e2006-11-14 21:24:49 -08001353 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355}
1356
1357/**
1358 * skb_dequeue - remove from the head of the queue
1359 * @list: list to dequeue from
1360 *
1361 * Remove the head of the list. The list lock is taken so the function
1362 * may be used safely with other locking list functions. The head item is
1363 * returned or %NULL if the list is empty.
1364 */
1365
1366struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1367{
1368 unsigned long flags;
1369 struct sk_buff *result;
1370
1371 spin_lock_irqsave(&list->lock, flags);
1372 result = __skb_dequeue(list);
1373 spin_unlock_irqrestore(&list->lock, flags);
1374 return result;
1375}
1376
1377/**
1378 * skb_dequeue_tail - remove from the tail of the queue
1379 * @list: list to dequeue from
1380 *
1381 * Remove the tail of the list. The list lock is taken so the function
1382 * may be used safely with other locking list functions. The tail item is
1383 * returned or %NULL if the list is empty.
1384 */
1385struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1386{
1387 unsigned long flags;
1388 struct sk_buff *result;
1389
1390 spin_lock_irqsave(&list->lock, flags);
1391 result = __skb_dequeue_tail(list);
1392 spin_unlock_irqrestore(&list->lock, flags);
1393 return result;
1394}
1395
1396/**
1397 * skb_queue_purge - empty a list
1398 * @list: list to empty
1399 *
1400 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1401 * the list and one reference dropped. This function takes the list
1402 * lock and is atomic with respect to other list locking functions.
1403 */
1404void skb_queue_purge(struct sk_buff_head *list)
1405{
1406 struct sk_buff *skb;
1407 while ((skb = skb_dequeue(list)) != NULL)
1408 kfree_skb(skb);
1409}
1410
1411/**
1412 * skb_queue_head - queue a buffer at the list head
1413 * @list: list to use
1414 * @newsk: buffer to queue
1415 *
1416 * Queue a buffer at the start of the list. This function takes the
1417 * list lock and can be used safely with other locking &sk_buff functions
1418 * safely.
1419 *
1420 * A buffer cannot be placed on two lists at the same time.
1421 */
1422void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1423{
1424 unsigned long flags;
1425
1426 spin_lock_irqsave(&list->lock, flags);
1427 __skb_queue_head(list, newsk);
1428 spin_unlock_irqrestore(&list->lock, flags);
1429}
1430
1431/**
1432 * skb_queue_tail - queue a buffer at the list tail
1433 * @list: list to use
1434 * @newsk: buffer to queue
1435 *
1436 * Queue a buffer at the tail of the list. This function takes the
1437 * list lock and can be used safely with other locking &sk_buff functions
1438 * safely.
1439 *
1440 * A buffer cannot be placed on two lists at the same time.
1441 */
1442void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1443{
1444 unsigned long flags;
1445
1446 spin_lock_irqsave(&list->lock, flags);
1447 __skb_queue_tail(list, newsk);
1448 spin_unlock_irqrestore(&list->lock, flags);
1449}
David S. Miller8728b832005-08-09 19:25:21 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/**
1452 * skb_unlink - remove a buffer from a list
1453 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001454 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 *
David S. Miller8728b832005-08-09 19:25:21 -07001456 * Remove a packet from a list. The list locks are taken and this
1457 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 *
David S. Miller8728b832005-08-09 19:25:21 -07001459 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
David S. Miller8728b832005-08-09 19:25:21 -07001461void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
David S. Miller8728b832005-08-09 19:25:21 -07001463 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
David S. Miller8728b832005-08-09 19:25:21 -07001465 spin_lock_irqsave(&list->lock, flags);
1466 __skb_unlink(skb, list);
1467 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470/**
1471 * skb_append - append a buffer
1472 * @old: buffer to insert after
1473 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001474 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 *
1476 * Place a packet after a given packet in a list. The list locks are taken
1477 * and this function is atomic with respect to other list locked calls.
1478 * A buffer cannot be placed on two lists at the same time.
1479 */
David S. Miller8728b832005-08-09 19:25:21 -07001480void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 unsigned long flags;
1483
David S. Miller8728b832005-08-09 19:25:21 -07001484 spin_lock_irqsave(&list->lock, flags);
1485 __skb_append(old, newsk, list);
1486 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489
1490/**
1491 * skb_insert - insert a buffer
1492 * @old: buffer to insert before
1493 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001494 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
David S. Miller8728b832005-08-09 19:25:21 -07001496 * Place a packet before a given packet in a list. The list locks are
1497 * taken and this function is atomic with respect to other list locked
1498 * calls.
1499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 * A buffer cannot be placed on two lists at the same time.
1501 */
David S. Miller8728b832005-08-09 19:25:21 -07001502void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 unsigned long flags;
1505
David S. Miller8728b832005-08-09 19:25:21 -07001506 spin_lock_irqsave(&list->lock, flags);
1507 __skb_insert(newsk, old->prev, old, list);
1508 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511#if 0
1512/*
1513 * Tune the memory allocator for a new MTU size.
1514 */
1515void skb_add_mtu(int mtu)
1516{
1517 /* Must match allocation in alloc_skb */
1518 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1519
1520 kmem_add_cache_size(mtu);
1521}
1522#endif
1523
1524static inline void skb_split_inside_header(struct sk_buff *skb,
1525 struct sk_buff* skb1,
1526 const u32 len, const int pos)
1527{
1528 int i;
1529
1530 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1531
1532 /* And move data appendix as is. */
1533 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1534 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1535
1536 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1537 skb_shinfo(skb)->nr_frags = 0;
1538 skb1->data_len = skb->data_len;
1539 skb1->len += skb1->data_len;
1540 skb->data_len = 0;
1541 skb->len = len;
1542 skb->tail = skb->data + len;
1543}
1544
1545static inline void skb_split_no_header(struct sk_buff *skb,
1546 struct sk_buff* skb1,
1547 const u32 len, int pos)
1548{
1549 int i, k = 0;
1550 const int nfrags = skb_shinfo(skb)->nr_frags;
1551
1552 skb_shinfo(skb)->nr_frags = 0;
1553 skb1->len = skb1->data_len = skb->len - len;
1554 skb->len = len;
1555 skb->data_len = len - pos;
1556
1557 for (i = 0; i < nfrags; i++) {
1558 int size = skb_shinfo(skb)->frags[i].size;
1559
1560 if (pos + size > len) {
1561 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1562
1563 if (pos < len) {
1564 /* Split frag.
1565 * We have two variants in this case:
1566 * 1. Move all the frag to the second
1567 * part, if it is possible. F.e.
1568 * this approach is mandatory for TUX,
1569 * where splitting is expensive.
1570 * 2. Split is accurately. We make this.
1571 */
1572 get_page(skb_shinfo(skb)->frags[i].page);
1573 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1574 skb_shinfo(skb1)->frags[0].size -= len - pos;
1575 skb_shinfo(skb)->frags[i].size = len - pos;
1576 skb_shinfo(skb)->nr_frags++;
1577 }
1578 k++;
1579 } else
1580 skb_shinfo(skb)->nr_frags++;
1581 pos += size;
1582 }
1583 skb_shinfo(skb1)->nr_frags = k;
1584}
1585
1586/**
1587 * skb_split - Split fragmented skb to two parts at length len.
1588 * @skb: the buffer to split
1589 * @skb1: the buffer to receive the second part
1590 * @len: new length for skb
1591 */
1592void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1593{
1594 int pos = skb_headlen(skb);
1595
1596 if (len < pos) /* Split line is inside header. */
1597 skb_split_inside_header(skb, skb1, len, pos);
1598 else /* Second chunk has no header, nothing to copy. */
1599 skb_split_no_header(skb, skb1, len, pos);
1600}
1601
Thomas Graf677e90e2005-06-23 20:59:51 -07001602/**
1603 * skb_prepare_seq_read - Prepare a sequential read of skb data
1604 * @skb: the buffer to read
1605 * @from: lower offset of data to be read
1606 * @to: upper offset of data to be read
1607 * @st: state variable
1608 *
1609 * Initializes the specified state variable. Must be called before
1610 * invoking skb_seq_read() for the first time.
1611 */
1612void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1613 unsigned int to, struct skb_seq_state *st)
1614{
1615 st->lower_offset = from;
1616 st->upper_offset = to;
1617 st->root_skb = st->cur_skb = skb;
1618 st->frag_idx = st->stepped_offset = 0;
1619 st->frag_data = NULL;
1620}
1621
1622/**
1623 * skb_seq_read - Sequentially read skb data
1624 * @consumed: number of bytes consumed by the caller so far
1625 * @data: destination pointer for data to be returned
1626 * @st: state variable
1627 *
1628 * Reads a block of skb data at &consumed relative to the
1629 * lower offset specified to skb_prepare_seq_read(). Assigns
1630 * the head of the data block to &data and returns the length
1631 * of the block or 0 if the end of the skb data or the upper
1632 * offset has been reached.
1633 *
1634 * The caller is not required to consume all of the data
1635 * returned, i.e. &consumed is typically set to the number
1636 * of bytes already consumed and the next call to
1637 * skb_seq_read() will return the remaining part of the block.
1638 *
1639 * Note: The size of each block of data returned can be arbitary,
1640 * this limitation is the cost for zerocopy seqeuental
1641 * reads of potentially non linear data.
1642 *
1643 * Note: Fragment lists within fragments are not implemented
1644 * at the moment, state->root_skb could be replaced with
1645 * a stack for this purpose.
1646 */
1647unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1648 struct skb_seq_state *st)
1649{
1650 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1651 skb_frag_t *frag;
1652
1653 if (unlikely(abs_offset >= st->upper_offset))
1654 return 0;
1655
1656next_skb:
1657 block_limit = skb_headlen(st->cur_skb);
1658
1659 if (abs_offset < block_limit) {
1660 *data = st->cur_skb->data + abs_offset;
1661 return block_limit - abs_offset;
1662 }
1663
1664 if (st->frag_idx == 0 && !st->frag_data)
1665 st->stepped_offset += skb_headlen(st->cur_skb);
1666
1667 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1668 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1669 block_limit = frag->size + st->stepped_offset;
1670
1671 if (abs_offset < block_limit) {
1672 if (!st->frag_data)
1673 st->frag_data = kmap_skb_frag(frag);
1674
1675 *data = (u8 *) st->frag_data + frag->page_offset +
1676 (abs_offset - st->stepped_offset);
1677
1678 return block_limit - abs_offset;
1679 }
1680
1681 if (st->frag_data) {
1682 kunmap_skb_frag(st->frag_data);
1683 st->frag_data = NULL;
1684 }
1685
1686 st->frag_idx++;
1687 st->stepped_offset += frag->size;
1688 }
1689
1690 if (st->cur_skb->next) {
1691 st->cur_skb = st->cur_skb->next;
1692 st->frag_idx = 0;
1693 goto next_skb;
1694 } else if (st->root_skb == st->cur_skb &&
1695 skb_shinfo(st->root_skb)->frag_list) {
1696 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1697 goto next_skb;
1698 }
1699
1700 return 0;
1701}
1702
1703/**
1704 * skb_abort_seq_read - Abort a sequential read of skb data
1705 * @st: state variable
1706 *
1707 * Must be called if skb_seq_read() was not called until it
1708 * returned 0.
1709 */
1710void skb_abort_seq_read(struct skb_seq_state *st)
1711{
1712 if (st->frag_data)
1713 kunmap_skb_frag(st->frag_data);
1714}
1715
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001716#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1717
1718static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1719 struct ts_config *conf,
1720 struct ts_state *state)
1721{
1722 return skb_seq_read(offset, text, TS_SKB_CB(state));
1723}
1724
1725static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1726{
1727 skb_abort_seq_read(TS_SKB_CB(state));
1728}
1729
1730/**
1731 * skb_find_text - Find a text pattern in skb data
1732 * @skb: the buffer to look in
1733 * @from: search offset
1734 * @to: search limit
1735 * @config: textsearch configuration
1736 * @state: uninitialized textsearch state variable
1737 *
1738 * Finds a pattern in the skb data according to the specified
1739 * textsearch configuration. Use textsearch_next() to retrieve
1740 * subsequent occurrences of the pattern. Returns the offset
1741 * to the first occurrence or UINT_MAX if no match was found.
1742 */
1743unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1744 unsigned int to, struct ts_config *config,
1745 struct ts_state *state)
1746{
Phil Oesterf72b9482006-06-26 00:00:57 -07001747 unsigned int ret;
1748
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001749 config->get_next_block = skb_ts_get_next_block;
1750 config->finish = skb_ts_finish;
1751
1752 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1753
Phil Oesterf72b9482006-06-26 00:00:57 -07001754 ret = textsearch_find(config, state);
1755 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001756}
1757
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001758/**
1759 * skb_append_datato_frags: - append the user data to a skb
1760 * @sk: sock structure
1761 * @skb: skb structure to be appened with user data.
1762 * @getfrag: call back function to be used for getting the user data
1763 * @from: pointer to user message iov
1764 * @length: length of the iov message
1765 *
1766 * Description: This procedure append the user data in the fragment part
1767 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1768 */
1769int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001770 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001771 int len, int odd, struct sk_buff *skb),
1772 void *from, int length)
1773{
1774 int frg_cnt = 0;
1775 skb_frag_t *frag = NULL;
1776 struct page *page = NULL;
1777 int copy, left;
1778 int offset = 0;
1779 int ret;
1780
1781 do {
1782 /* Return error if we don't have space for new frag */
1783 frg_cnt = skb_shinfo(skb)->nr_frags;
1784 if (frg_cnt >= MAX_SKB_FRAGS)
1785 return -EFAULT;
1786
1787 /* allocate a new page for next frag */
1788 page = alloc_pages(sk->sk_allocation, 0);
1789
1790 /* If alloc_page fails just return failure and caller will
1791 * free previous allocated pages by doing kfree_skb()
1792 */
1793 if (page == NULL)
1794 return -ENOMEM;
1795
1796 /* initialize the next frag */
1797 sk->sk_sndmsg_page = page;
1798 sk->sk_sndmsg_off = 0;
1799 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1800 skb->truesize += PAGE_SIZE;
1801 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1802
1803 /* get the new initialized frag */
1804 frg_cnt = skb_shinfo(skb)->nr_frags;
1805 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1806
1807 /* copy the user data to page */
1808 left = PAGE_SIZE - frag->page_offset;
1809 copy = (length > left)? left : length;
1810
1811 ret = getfrag(from, (page_address(frag->page) +
1812 frag->page_offset + frag->size),
1813 offset, copy, 0, skb);
1814 if (ret < 0)
1815 return -EFAULT;
1816
1817 /* copy was successful so update the size parameters */
1818 sk->sk_sndmsg_off += copy;
1819 frag->size += copy;
1820 skb->len += copy;
1821 skb->data_len += copy;
1822 offset += copy;
1823 length -= copy;
1824
1825 } while (length > 0);
1826
1827 return 0;
1828}
1829
Herbert Xucbb042f2006-03-20 22:43:56 -08001830/**
1831 * skb_pull_rcsum - pull skb and update receive checksum
1832 * @skb: buffer to update
1833 * @start: start of data before pull
1834 * @len: length of data pulled
1835 *
1836 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001837 * update the CHECKSUM_COMPLETE checksum. It should be used on
1838 * receive path processing instead of skb_pull unless you know
1839 * that the checksum difference is zero (e.g., a valid IP header)
1840 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001841 */
1842unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1843{
1844 BUG_ON(len > skb->len);
1845 skb->len -= len;
1846 BUG_ON(skb->len < skb->data_len);
1847 skb_postpull_rcsum(skb, skb->data, len);
1848 return skb->data += len;
1849}
1850
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001851EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1852
Herbert Xuf4c50d92006-06-22 03:02:40 -07001853/**
1854 * skb_segment - Perform protocol segmentation on skb.
1855 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001856 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001857 *
1858 * This function performs segmentation on the given skb. It returns
1859 * the segment at the given position. It returns NULL if there are
1860 * no more segments to generate, or when an error is encountered.
1861 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001862struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001863{
1864 struct sk_buff *segs = NULL;
1865 struct sk_buff *tail = NULL;
1866 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001867 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001868 unsigned int offset = doffset;
1869 unsigned int headroom;
1870 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001871 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001872 int nfrags = skb_shinfo(skb)->nr_frags;
1873 int err = -ENOMEM;
1874 int i = 0;
1875 int pos;
1876
1877 __skb_push(skb, doffset);
1878 headroom = skb_headroom(skb);
1879 pos = skb_headlen(skb);
1880
1881 do {
1882 struct sk_buff *nskb;
1883 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001884 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001885 int k;
1886 int size;
1887
1888 len = skb->len - offset;
1889 if (len > mss)
1890 len = mss;
1891
1892 hsize = skb_headlen(skb) - offset;
1893 if (hsize < 0)
1894 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001895 if (hsize > len || !sg)
1896 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001897
Herbert Xuc8884ed2006-10-29 15:59:41 -08001898 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001899 if (unlikely(!nskb))
1900 goto err;
1901
1902 if (segs)
1903 tail->next = nskb;
1904 else
1905 segs = nskb;
1906 tail = nskb;
1907
1908 nskb->dev = skb->dev;
1909 nskb->priority = skb->priority;
1910 nskb->protocol = skb->protocol;
1911 nskb->dst = dst_clone(skb->dst);
1912 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1913 nskb->pkt_type = skb->pkt_type;
1914 nskb->mac_len = skb->mac_len;
1915
1916 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001917 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001918 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001919 nskb->transport_header = (nskb->network_header +
1920 skb_network_header_len(skb));
Herbert Xuf4c50d92006-06-22 03:02:40 -07001921 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1922
1923 if (!sg) {
1924 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1925 skb_put(nskb, len),
1926 len, 0);
1927 continue;
1928 }
1929
1930 frag = skb_shinfo(nskb)->frags;
1931 k = 0;
1932
Patrick McHardy84fa7932006-08-29 16:44:56 -07001933 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001934 nskb->csum = skb->csum;
1935 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1936
1937 while (pos < offset + len) {
1938 BUG_ON(i >= nfrags);
1939
1940 *frag = skb_shinfo(skb)->frags[i];
1941 get_page(frag->page);
1942 size = frag->size;
1943
1944 if (pos < offset) {
1945 frag->page_offset += offset - pos;
1946 frag->size -= offset - pos;
1947 }
1948
1949 k++;
1950
1951 if (pos + size <= offset + len) {
1952 i++;
1953 pos += size;
1954 } else {
1955 frag->size -= pos + size - (offset + len);
1956 break;
1957 }
1958
1959 frag++;
1960 }
1961
1962 skb_shinfo(nskb)->nr_frags = k;
1963 nskb->data_len = len - hsize;
1964 nskb->len += nskb->data_len;
1965 nskb->truesize += nskb->data_len;
1966 } while ((offset += len) < skb->len);
1967
1968 return segs;
1969
1970err:
1971 while ((skb = segs)) {
1972 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08001973 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001974 }
1975 return ERR_PTR(err);
1976}
1977
1978EXPORT_SYMBOL_GPL(skb_segment);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980void __init skb_init(void)
1981{
1982 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1983 sizeof(struct sk_buff),
1984 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001985 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07001987 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1988 (2*sizeof(struct sk_buff)) +
1989 sizeof(atomic_t),
1990 0,
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001991 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07001992 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995EXPORT_SYMBOL(___pskb_trim);
1996EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08001997EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07001999EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002000EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001EXPORT_SYMBOL(pskb_copy);
2002EXPORT_SYMBOL(pskb_expand_head);
2003EXPORT_SYMBOL(skb_checksum);
2004EXPORT_SYMBOL(skb_clone);
2005EXPORT_SYMBOL(skb_clone_fraglist);
2006EXPORT_SYMBOL(skb_copy);
2007EXPORT_SYMBOL(skb_copy_and_csum_bits);
2008EXPORT_SYMBOL(skb_copy_and_csum_dev);
2009EXPORT_SYMBOL(skb_copy_bits);
2010EXPORT_SYMBOL(skb_copy_expand);
2011EXPORT_SYMBOL(skb_over_panic);
2012EXPORT_SYMBOL(skb_pad);
2013EXPORT_SYMBOL(skb_realloc_headroom);
2014EXPORT_SYMBOL(skb_under_panic);
2015EXPORT_SYMBOL(skb_dequeue);
2016EXPORT_SYMBOL(skb_dequeue_tail);
2017EXPORT_SYMBOL(skb_insert);
2018EXPORT_SYMBOL(skb_queue_purge);
2019EXPORT_SYMBOL(skb_queue_head);
2020EXPORT_SYMBOL(skb_queue_tail);
2021EXPORT_SYMBOL(skb_unlink);
2022EXPORT_SYMBOL(skb_append);
2023EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002024EXPORT_SYMBOL(skb_prepare_seq_read);
2025EXPORT_SYMBOL(skb_seq_read);
2026EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002027EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002028EXPORT_SYMBOL(skb_append_datato_frags);