blob: 1e71764be4a44bbaf858f40ab31ee198b668db51 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
49#include <linux/netdevice.h>
50#ifdef CONFIG_NET_CLS_ACT
51#include <net/pkt_sched.h>
52#endif
53#include <linux/string.h>
54#include <linux/skbuff.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <net/protocol.h>
60#include <net/dst.h>
61#include <net/sock.h>
62#include <net/checksum.h>
63#include <net/xfrm.h>
64
65#include <asm/uaccess.h>
66#include <asm/system.h>
67
Al Viroa1f8e7f72006-10-19 16:08:53 -040068#include "kmap_skb.h"
69
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *skbuff_head_cache __read_mostly;
71static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
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{
451 /*
452 * Shift between the two data areas in bytes
453 */
454 unsigned long offset = new->data - old->data;
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 new->sk = NULL;
457 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 new->priority = old->priority;
459 new->protocol = old->protocol;
460 new->dst = dst_clone(old->dst);
461#ifdef CONFIG_INET
462 new->sp = secpath_get(old->sp);
463#endif
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700464 new->transport_header = old->transport_header + offset;
465 new->network_header = old->network_header + offset;
466 new->mac_header = old->mac_header + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 memcpy(new->cb, old->cb, sizeof(old->cb));
468 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700469 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700471 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800473 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700474 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300475#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
476 new->ipvs_property = old->ipvs_property;
477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#ifdef CONFIG_NET_SCHED
479#ifdef CONFIG_NET_CLS_ACT
480 new->tc_verd = old->tc_verd;
481#endif
482 new->tc_index = old->tc_index;
483#endif
James Morris984bc162006-06-09 00:29:17 -0700484 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700486 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
487 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
488 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/**
492 * skb_copy - create private copy of an sk_buff
493 * @skb: buffer to copy
494 * @gfp_mask: allocation priority
495 *
496 * Make a copy of both an &sk_buff and its data. This is used when the
497 * caller wishes to modify the data and needs a private copy of the
498 * data to alter. Returns %NULL on failure or the pointer to the buffer
499 * on success. The returned buffer has a reference count of 1.
500 *
501 * As by-product this function converts non-linear &sk_buff to linear
502 * one, so that &sk_buff becomes completely private and caller is allowed
503 * to modify all the data of returned buffer. This means that this
504 * function is not recommended for use in circumstances when only
505 * header is going to be modified. Use pskb_copy() instead.
506 */
507
Al Virodd0fc662005-10-07 07:46:04 +0100508struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int headerlen = skb->data - skb->head;
511 /*
512 * Allocate the copy buffer
513 */
514 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
515 gfp_mask);
516 if (!n)
517 return NULL;
518
519 /* Set the data pointer */
520 skb_reserve(n, headerlen);
521 /* Set the tail pointer and length */
522 skb_put(n, skb->len);
523 n->csum = skb->csum;
524 n->ip_summed = skb->ip_summed;
525
526 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
527 BUG();
528
529 copy_skb_header(n, skb);
530 return n;
531}
532
533
534/**
535 * pskb_copy - create copy of an sk_buff with private head.
536 * @skb: buffer to copy
537 * @gfp_mask: allocation priority
538 *
539 * Make a copy of both an &sk_buff and part of its data, located
540 * in header. Fragmented data remain shared. This is used when
541 * the caller wishes to modify only header of &sk_buff and needs
542 * private copy of the header to alter. Returns %NULL on failure
543 * or the pointer to the buffer on success.
544 * The returned buffer has a reference count of 1.
545 */
546
Al Virodd0fc662005-10-07 07:46:04 +0100547struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 /*
550 * Allocate the copy buffer
551 */
552 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
553
554 if (!n)
555 goto out;
556
557 /* Set the data pointer */
558 skb_reserve(n, skb->data - skb->head);
559 /* Set the tail pointer and length */
560 skb_put(n, skb_headlen(skb));
561 /* Copy the bytes */
562 memcpy(n->data, skb->data, n->len);
563 n->csum = skb->csum;
564 n->ip_summed = skb->ip_summed;
565
Herbert Xu25f484a2006-11-07 14:57:15 -0800566 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 n->data_len = skb->data_len;
568 n->len = skb->len;
569
570 if (skb_shinfo(skb)->nr_frags) {
571 int i;
572
573 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
574 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
575 get_page(skb_shinfo(n)->frags[i].page);
576 }
577 skb_shinfo(n)->nr_frags = i;
578 }
579
580 if (skb_shinfo(skb)->frag_list) {
581 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
582 skb_clone_fraglist(n);
583 }
584
585 copy_skb_header(n, skb);
586out:
587 return n;
588}
589
590/**
591 * pskb_expand_head - reallocate header of &sk_buff
592 * @skb: buffer to reallocate
593 * @nhead: room to add at head
594 * @ntail: room to add at tail
595 * @gfp_mask: allocation priority
596 *
597 * Expands (or creates identical copy, if &nhead and &ntail are zero)
598 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
599 * reference count of 1. Returns zero in the case of success or error,
600 * if expansion failed. In the last case, &sk_buff is not changed.
601 *
602 * All the pointers pointing into skb header may change and must be
603 * reloaded after call to this function.
604 */
605
Victor Fusco86a76ca2005-07-08 14:57:47 -0700606int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100607 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 int i;
610 u8 *data;
611 int size = nhead + (skb->end - skb->head) + ntail;
612 long off;
613
614 if (skb_shared(skb))
615 BUG();
616
617 size = SKB_DATA_ALIGN(size);
618
619 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
620 if (!data)
621 goto nodata;
622
623 /* Copy only real data... and, alas, header. This should be
624 * optimized for the cases when header is void. */
625 memcpy(data + nhead, skb->head, skb->tail - skb->head);
626 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
627
628 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
629 get_page(skb_shinfo(skb)->frags[i].page);
630
631 if (skb_shinfo(skb)->frag_list)
632 skb_clone_fraglist(skb);
633
634 skb_release_data(skb);
635
636 off = (data + nhead) - skb->head;
637
638 skb->head = data;
639 skb->end = data + size;
640 skb->data += off;
641 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700642 skb->transport_header += off;
643 skb->network_header += off;
644 skb->mac_header += off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 skb->cloned = 0;
646 skb->nohdr = 0;
647 atomic_set(&skb_shinfo(skb)->dataref, 1);
648 return 0;
649
650nodata:
651 return -ENOMEM;
652}
653
654/* Make private copy of skb with writable head and some headroom */
655
656struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
657{
658 struct sk_buff *skb2;
659 int delta = headroom - skb_headroom(skb);
660
661 if (delta <= 0)
662 skb2 = pskb_copy(skb, GFP_ATOMIC);
663 else {
664 skb2 = skb_clone(skb, GFP_ATOMIC);
665 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
666 GFP_ATOMIC)) {
667 kfree_skb(skb2);
668 skb2 = NULL;
669 }
670 }
671 return skb2;
672}
673
674
675/**
676 * skb_copy_expand - copy and expand sk_buff
677 * @skb: buffer to copy
678 * @newheadroom: new free bytes at head
679 * @newtailroom: new free bytes at tail
680 * @gfp_mask: allocation priority
681 *
682 * Make a copy of both an &sk_buff and its data and while doing so
683 * allocate additional space.
684 *
685 * This is used when the caller wishes to modify the data and needs a
686 * private copy of the data to alter as well as more space for new fields.
687 * Returns %NULL on failure or the pointer to the buffer
688 * on success. The returned buffer has a reference count of 1.
689 *
690 * You must pass %GFP_ATOMIC as the allocation priority if this function
691 * is called from an interrupt.
692 *
693 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
694 * only by netfilter in the cases when checksum is recalculated? --ANK
695 */
696struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700697 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100698 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 /*
701 * Allocate the copy buffer
702 */
703 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
704 gfp_mask);
705 int head_copy_len, head_copy_off;
706
707 if (!n)
708 return NULL;
709
710 skb_reserve(n, newheadroom);
711
712 /* Set the tail pointer and length */
713 skb_put(n, skb->len);
714
715 head_copy_len = skb_headroom(skb);
716 head_copy_off = 0;
717 if (newheadroom <= head_copy_len)
718 head_copy_len = newheadroom;
719 else
720 head_copy_off = newheadroom - head_copy_len;
721
722 /* Copy the linear header and data. */
723 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
724 skb->len + head_copy_len))
725 BUG();
726
727 copy_skb_header(n, skb);
728
729 return n;
730}
731
732/**
733 * skb_pad - zero pad the tail of an skb
734 * @skb: buffer to pad
735 * @pad: space to pad
736 *
737 * Ensure that a buffer is followed by a padding area that is zero
738 * filled. Used by network drivers which may DMA or transfer data
739 * beyond the buffer end onto the wire.
740 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700741 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900743
Herbert Xu5b057c62006-06-23 02:06:41 -0700744int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Herbert Xu5b057c62006-06-23 02:06:41 -0700746 int err;
747 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700750 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700754
755 ntail = skb->data_len + pad - (skb->end - skb->tail);
756 if (likely(skb_cloned(skb) || ntail > 0)) {
757 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
758 if (unlikely(err))
759 goto free_skb;
760 }
761
762 /* FIXME: The use of this function with non-linear skb's really needs
763 * to be audited.
764 */
765 err = skb_linearize(skb);
766 if (unlikely(err))
767 goto free_skb;
768
769 memset(skb->data + skb->len, 0, pad);
770 return 0;
771
772free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700774 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900775}
776
Herbert Xu3cc0e872006-06-09 16:13:38 -0700777/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
779
Herbert Xu3cc0e872006-06-09 16:13:38 -0700780int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Herbert Xu27b437c2006-07-13 19:26:39 -0700782 struct sk_buff **fragp;
783 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int offset = skb_headlen(skb);
785 int nfrags = skb_shinfo(skb)->nr_frags;
786 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700787 int err;
788
789 if (skb_cloned(skb) &&
790 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700793 i = 0;
794 if (offset >= len)
795 goto drop_pages;
796
797 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700799
800 if (end < len) {
801 offset = end;
802 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700804
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700805 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700806
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700807drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700808 skb_shinfo(skb)->nr_frags = i;
809
810 for (; i < nfrags; i++)
811 put_page(skb_shinfo(skb)->frags[i].page);
812
813 if (skb_shinfo(skb)->frag_list)
814 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700815 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
Herbert Xu27b437c2006-07-13 19:26:39 -0700818 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
819 fragp = &frag->next) {
820 int end = offset + frag->len;
821
822 if (skb_shared(frag)) {
823 struct sk_buff *nfrag;
824
825 nfrag = skb_clone(frag, GFP_ATOMIC);
826 if (unlikely(!nfrag))
827 return -ENOMEM;
828
829 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700830 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700831 frag = nfrag;
832 *fragp = frag;
833 }
834
835 if (end < len) {
836 offset = end;
837 continue;
838 }
839
840 if (end > len &&
841 unlikely((err = pskb_trim(frag, len - offset))))
842 return err;
843
844 if (frag->next)
845 skb_drop_list(&frag->next);
846 break;
847 }
848
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700849done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700850 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 skb->data_len -= skb->len - len;
852 skb->len = len;
853 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700854 skb->len = len;
855 skb->data_len = 0;
856 skb->tail = skb->data + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 return 0;
860}
861
862/**
863 * __pskb_pull_tail - advance tail of skb header
864 * @skb: buffer to reallocate
865 * @delta: number of bytes to advance tail
866 *
867 * The function makes a sense only on a fragmented &sk_buff,
868 * it expands header moving its tail forward and copying necessary
869 * data from fragmented part.
870 *
871 * &sk_buff MUST have reference count of 1.
872 *
873 * Returns %NULL (and &sk_buff does not change) if pull failed
874 * or value of new tail of skb in the case of success.
875 *
876 * All the pointers pointing into skb header may change and must be
877 * reloaded after call to this function.
878 */
879
880/* Moves tail of skb head forward, copying data from fragmented part,
881 * when it is necessary.
882 * 1. It may fail due to malloc failure.
883 * 2. It may change skb pointers.
884 *
885 * It is pretty complicated. Luckily, it is called only in exceptional cases.
886 */
887unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
888{
889 /* If skb has not enough free space at tail, get new one
890 * plus 128 bytes for future expansions. If we have enough
891 * room at tail, reallocate without expansion only if skb is cloned.
892 */
893 int i, k, eat = (skb->tail + delta) - skb->end;
894
895 if (eat > 0 || skb_cloned(skb)) {
896 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
897 GFP_ATOMIC))
898 return NULL;
899 }
900
901 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
902 BUG();
903
904 /* Optimization: no fragments, no reasons to preestimate
905 * size of pulled pages. Superb.
906 */
907 if (!skb_shinfo(skb)->frag_list)
908 goto pull_pages;
909
910 /* Estimate size of pulled pages. */
911 eat = delta;
912 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
913 if (skb_shinfo(skb)->frags[i].size >= eat)
914 goto pull_pages;
915 eat -= skb_shinfo(skb)->frags[i].size;
916 }
917
918 /* If we need update frag list, we are in troubles.
919 * Certainly, it possible to add an offset to skb data,
920 * but taking into account that pulling is expected to
921 * be very rare operation, it is worth to fight against
922 * further bloating skb head and crucify ourselves here instead.
923 * Pure masohism, indeed. 8)8)
924 */
925 if (eat) {
926 struct sk_buff *list = skb_shinfo(skb)->frag_list;
927 struct sk_buff *clone = NULL;
928 struct sk_buff *insp = NULL;
929
930 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800931 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (list->len <= eat) {
934 /* Eaten as whole. */
935 eat -= list->len;
936 list = list->next;
937 insp = list;
938 } else {
939 /* Eaten partially. */
940
941 if (skb_shared(list)) {
942 /* Sucks! We need to fork list. :-( */
943 clone = skb_clone(list, GFP_ATOMIC);
944 if (!clone)
945 return NULL;
946 insp = list->next;
947 list = clone;
948 } else {
949 /* This may be pulled without
950 * problems. */
951 insp = list;
952 }
953 if (!pskb_pull(list, eat)) {
954 if (clone)
955 kfree_skb(clone);
956 return NULL;
957 }
958 break;
959 }
960 } while (eat);
961
962 /* Free pulled out fragments. */
963 while ((list = skb_shinfo(skb)->frag_list) != insp) {
964 skb_shinfo(skb)->frag_list = list->next;
965 kfree_skb(list);
966 }
967 /* And insert new clone at head. */
968 if (clone) {
969 clone->next = list;
970 skb_shinfo(skb)->frag_list = clone;
971 }
972 }
973 /* Success! Now we may commit changes to skb data. */
974
975pull_pages:
976 eat = delta;
977 k = 0;
978 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
979 if (skb_shinfo(skb)->frags[i].size <= eat) {
980 put_page(skb_shinfo(skb)->frags[i].page);
981 eat -= skb_shinfo(skb)->frags[i].size;
982 } else {
983 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
984 if (eat) {
985 skb_shinfo(skb)->frags[k].page_offset += eat;
986 skb_shinfo(skb)->frags[k].size -= eat;
987 eat = 0;
988 }
989 k++;
990 }
991 }
992 skb_shinfo(skb)->nr_frags = k;
993
994 skb->tail += delta;
995 skb->data_len -= delta;
996
997 return skb->tail;
998}
999
1000/* Copy some data bits from skb to kernel buffer. */
1001
1002int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1003{
1004 int i, copy;
1005 int start = skb_headlen(skb);
1006
1007 if (offset > (int)skb->len - len)
1008 goto fault;
1009
1010 /* Copy header. */
1011 if ((copy = start - offset) > 0) {
1012 if (copy > len)
1013 copy = len;
1014 memcpy(to, skb->data + offset, copy);
1015 if ((len -= copy) == 0)
1016 return 0;
1017 offset += copy;
1018 to += copy;
1019 }
1020
1021 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1022 int end;
1023
1024 BUG_TRAP(start <= offset + len);
1025
1026 end = start + skb_shinfo(skb)->frags[i].size;
1027 if ((copy = end - offset) > 0) {
1028 u8 *vaddr;
1029
1030 if (copy > len)
1031 copy = len;
1032
1033 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1034 memcpy(to,
1035 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1036 offset - start, copy);
1037 kunmap_skb_frag(vaddr);
1038
1039 if ((len -= copy) == 0)
1040 return 0;
1041 offset += copy;
1042 to += copy;
1043 }
1044 start = end;
1045 }
1046
1047 if (skb_shinfo(skb)->frag_list) {
1048 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1049
1050 for (; list; list = list->next) {
1051 int end;
1052
1053 BUG_TRAP(start <= offset + len);
1054
1055 end = start + list->len;
1056 if ((copy = end - offset) > 0) {
1057 if (copy > len)
1058 copy = len;
1059 if (skb_copy_bits(list, offset - start,
1060 to, copy))
1061 goto fault;
1062 if ((len -= copy) == 0)
1063 return 0;
1064 offset += copy;
1065 to += copy;
1066 }
1067 start = end;
1068 }
1069 }
1070 if (!len)
1071 return 0;
1072
1073fault:
1074 return -EFAULT;
1075}
1076
Herbert Xu357b40a2005-04-19 22:30:14 -07001077/**
1078 * skb_store_bits - store bits from kernel buffer to skb
1079 * @skb: destination buffer
1080 * @offset: offset in destination
1081 * @from: source buffer
1082 * @len: number of bytes to copy
1083 *
1084 * Copy the specified number of bytes from the source buffer to the
1085 * destination skb. This function handles all the messy bits of
1086 * traversing fragment lists and such.
1087 */
1088
1089int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1090{
1091 int i, copy;
1092 int start = skb_headlen(skb);
1093
1094 if (offset > (int)skb->len - len)
1095 goto fault;
1096
1097 if ((copy = start - offset) > 0) {
1098 if (copy > len)
1099 copy = len;
1100 memcpy(skb->data + offset, from, copy);
1101 if ((len -= copy) == 0)
1102 return 0;
1103 offset += copy;
1104 from += copy;
1105 }
1106
1107 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1108 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1109 int end;
1110
1111 BUG_TRAP(start <= offset + len);
1112
1113 end = start + frag->size;
1114 if ((copy = end - offset) > 0) {
1115 u8 *vaddr;
1116
1117 if (copy > len)
1118 copy = len;
1119
1120 vaddr = kmap_skb_frag(frag);
1121 memcpy(vaddr + frag->page_offset + offset - start,
1122 from, copy);
1123 kunmap_skb_frag(vaddr);
1124
1125 if ((len -= copy) == 0)
1126 return 0;
1127 offset += copy;
1128 from += copy;
1129 }
1130 start = end;
1131 }
1132
1133 if (skb_shinfo(skb)->frag_list) {
1134 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1135
1136 for (; list; list = list->next) {
1137 int end;
1138
1139 BUG_TRAP(start <= offset + len);
1140
1141 end = start + list->len;
1142 if ((copy = end - offset) > 0) {
1143 if (copy > len)
1144 copy = len;
1145 if (skb_store_bits(list, offset - start,
1146 from, copy))
1147 goto fault;
1148 if ((len -= copy) == 0)
1149 return 0;
1150 offset += copy;
1151 from += copy;
1152 }
1153 start = end;
1154 }
1155 }
1156 if (!len)
1157 return 0;
1158
1159fault:
1160 return -EFAULT;
1161}
1162
1163EXPORT_SYMBOL(skb_store_bits);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/* Checksum skb data. */
1166
Al Viro2bbbc862006-11-14 21:37:14 -08001167__wsum skb_checksum(const struct sk_buff *skb, int offset,
1168 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 int start = skb_headlen(skb);
1171 int i, copy = start - offset;
1172 int pos = 0;
1173
1174 /* Checksum header. */
1175 if (copy > 0) {
1176 if (copy > len)
1177 copy = len;
1178 csum = csum_partial(skb->data + offset, copy, csum);
1179 if ((len -= copy) == 0)
1180 return csum;
1181 offset += copy;
1182 pos = copy;
1183 }
1184
1185 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1186 int end;
1187
1188 BUG_TRAP(start <= offset + len);
1189
1190 end = start + skb_shinfo(skb)->frags[i].size;
1191 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001192 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 u8 *vaddr;
1194 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1195
1196 if (copy > len)
1197 copy = len;
1198 vaddr = kmap_skb_frag(frag);
1199 csum2 = csum_partial(vaddr + frag->page_offset +
1200 offset - start, copy, 0);
1201 kunmap_skb_frag(vaddr);
1202 csum = csum_block_add(csum, csum2, pos);
1203 if (!(len -= copy))
1204 return csum;
1205 offset += copy;
1206 pos += copy;
1207 }
1208 start = end;
1209 }
1210
1211 if (skb_shinfo(skb)->frag_list) {
1212 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1213
1214 for (; list; list = list->next) {
1215 int end;
1216
1217 BUG_TRAP(start <= offset + len);
1218
1219 end = start + list->len;
1220 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001221 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (copy > len)
1223 copy = len;
1224 csum2 = skb_checksum(list, offset - start,
1225 copy, 0);
1226 csum = csum_block_add(csum, csum2, pos);
1227 if ((len -= copy) == 0)
1228 return csum;
1229 offset += copy;
1230 pos += copy;
1231 }
1232 start = end;
1233 }
1234 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001235 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 return csum;
1238}
1239
1240/* Both of above in one bottle. */
1241
Al Viro81d77662006-11-14 21:37:33 -08001242__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1243 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 int start = skb_headlen(skb);
1246 int i, copy = start - offset;
1247 int pos = 0;
1248
1249 /* Copy header. */
1250 if (copy > 0) {
1251 if (copy > len)
1252 copy = len;
1253 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1254 copy, csum);
1255 if ((len -= copy) == 0)
1256 return csum;
1257 offset += copy;
1258 to += copy;
1259 pos = copy;
1260 }
1261
1262 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1263 int end;
1264
1265 BUG_TRAP(start <= offset + len);
1266
1267 end = start + skb_shinfo(skb)->frags[i].size;
1268 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001269 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 u8 *vaddr;
1271 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1272
1273 if (copy > len)
1274 copy = len;
1275 vaddr = kmap_skb_frag(frag);
1276 csum2 = csum_partial_copy_nocheck(vaddr +
1277 frag->page_offset +
1278 offset - start, to,
1279 copy, 0);
1280 kunmap_skb_frag(vaddr);
1281 csum = csum_block_add(csum, csum2, pos);
1282 if (!(len -= copy))
1283 return csum;
1284 offset += copy;
1285 to += copy;
1286 pos += copy;
1287 }
1288 start = end;
1289 }
1290
1291 if (skb_shinfo(skb)->frag_list) {
1292 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1293
1294 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001295 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int end;
1297
1298 BUG_TRAP(start <= offset + len);
1299
1300 end = start + list->len;
1301 if ((copy = end - offset) > 0) {
1302 if (copy > len)
1303 copy = len;
1304 csum2 = skb_copy_and_csum_bits(list,
1305 offset - start,
1306 to, copy, 0);
1307 csum = csum_block_add(csum, csum2, pos);
1308 if ((len -= copy) == 0)
1309 return csum;
1310 offset += copy;
1311 to += copy;
1312 pos += copy;
1313 }
1314 start = end;
1315 }
1316 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001317 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return csum;
1319}
1320
1321void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1322{
Al Virod3bc23e2006-11-14 21:24:49 -08001323 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 long csstart;
1325
Patrick McHardy84fa7932006-08-29 16:44:56 -07001326 if (skb->ip_summed == CHECKSUM_PARTIAL)
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001327 csstart = skb_transport_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 else
1329 csstart = skb_headlen(skb);
1330
Kris Katterjohn09a62662006-01-08 22:24:28 -08001331 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 memcpy(to, skb->data, csstart);
1334
1335 csum = 0;
1336 if (csstart != skb->len)
1337 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1338 skb->len - csstart, 0);
1339
Patrick McHardy84fa7932006-08-29 16:44:56 -07001340 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001341 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Al Virod3bc23e2006-11-14 21:24:49 -08001343 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345}
1346
1347/**
1348 * skb_dequeue - remove from the head of the queue
1349 * @list: list to dequeue from
1350 *
1351 * Remove the head of the list. The list lock is taken so the function
1352 * may be used safely with other locking list functions. The head item is
1353 * returned or %NULL if the list is empty.
1354 */
1355
1356struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1357{
1358 unsigned long flags;
1359 struct sk_buff *result;
1360
1361 spin_lock_irqsave(&list->lock, flags);
1362 result = __skb_dequeue(list);
1363 spin_unlock_irqrestore(&list->lock, flags);
1364 return result;
1365}
1366
1367/**
1368 * skb_dequeue_tail - remove from the tail of the queue
1369 * @list: list to dequeue from
1370 *
1371 * Remove the tail of the list. The list lock is taken so the function
1372 * may be used safely with other locking list functions. The tail item is
1373 * returned or %NULL if the list is empty.
1374 */
1375struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1376{
1377 unsigned long flags;
1378 struct sk_buff *result;
1379
1380 spin_lock_irqsave(&list->lock, flags);
1381 result = __skb_dequeue_tail(list);
1382 spin_unlock_irqrestore(&list->lock, flags);
1383 return result;
1384}
1385
1386/**
1387 * skb_queue_purge - empty a list
1388 * @list: list to empty
1389 *
1390 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1391 * the list and one reference dropped. This function takes the list
1392 * lock and is atomic with respect to other list locking functions.
1393 */
1394void skb_queue_purge(struct sk_buff_head *list)
1395{
1396 struct sk_buff *skb;
1397 while ((skb = skb_dequeue(list)) != NULL)
1398 kfree_skb(skb);
1399}
1400
1401/**
1402 * skb_queue_head - queue a buffer at the list head
1403 * @list: list to use
1404 * @newsk: buffer to queue
1405 *
1406 * Queue a buffer at the start of the list. This function takes the
1407 * list lock and can be used safely with other locking &sk_buff functions
1408 * safely.
1409 *
1410 * A buffer cannot be placed on two lists at the same time.
1411 */
1412void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1413{
1414 unsigned long flags;
1415
1416 spin_lock_irqsave(&list->lock, flags);
1417 __skb_queue_head(list, newsk);
1418 spin_unlock_irqrestore(&list->lock, flags);
1419}
1420
1421/**
1422 * skb_queue_tail - queue a buffer at the list tail
1423 * @list: list to use
1424 * @newsk: buffer to queue
1425 *
1426 * Queue a buffer at the tail of the list. This function takes the
1427 * list lock and can be used safely with other locking &sk_buff functions
1428 * safely.
1429 *
1430 * A buffer cannot be placed on two lists at the same time.
1431 */
1432void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1433{
1434 unsigned long flags;
1435
1436 spin_lock_irqsave(&list->lock, flags);
1437 __skb_queue_tail(list, newsk);
1438 spin_unlock_irqrestore(&list->lock, flags);
1439}
David S. Miller8728b832005-08-09 19:25:21 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441/**
1442 * skb_unlink - remove a buffer from a list
1443 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001444 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 *
David S. Miller8728b832005-08-09 19:25:21 -07001446 * Remove a packet from a list. The list locks are taken and this
1447 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 *
David S. Miller8728b832005-08-09 19:25:21 -07001449 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
David S. Miller8728b832005-08-09 19:25:21 -07001451void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
David S. Miller8728b832005-08-09 19:25:21 -07001453 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
David S. Miller8728b832005-08-09 19:25:21 -07001455 spin_lock_irqsave(&list->lock, flags);
1456 __skb_unlink(skb, list);
1457 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460/**
1461 * skb_append - append a buffer
1462 * @old: buffer to insert after
1463 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001464 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
1466 * Place a packet after a given packet in a list. The list locks are taken
1467 * and this function is atomic with respect to other list locked calls.
1468 * A buffer cannot be placed on two lists at the same time.
1469 */
David S. Miller8728b832005-08-09 19:25:21 -07001470void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 unsigned long flags;
1473
David S. Miller8728b832005-08-09 19:25:21 -07001474 spin_lock_irqsave(&list->lock, flags);
1475 __skb_append(old, newsk, list);
1476 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479
1480/**
1481 * skb_insert - insert a buffer
1482 * @old: buffer to insert before
1483 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001484 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
David S. Miller8728b832005-08-09 19:25:21 -07001486 * Place a packet before a given packet in a list. The list locks are
1487 * taken and this function is atomic with respect to other list locked
1488 * calls.
1489 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 * A buffer cannot be placed on two lists at the same time.
1491 */
David S. Miller8728b832005-08-09 19:25:21 -07001492void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 unsigned long flags;
1495
David S. Miller8728b832005-08-09 19:25:21 -07001496 spin_lock_irqsave(&list->lock, flags);
1497 __skb_insert(newsk, old->prev, old, list);
1498 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501#if 0
1502/*
1503 * Tune the memory allocator for a new MTU size.
1504 */
1505void skb_add_mtu(int mtu)
1506{
1507 /* Must match allocation in alloc_skb */
1508 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1509
1510 kmem_add_cache_size(mtu);
1511}
1512#endif
1513
1514static inline void skb_split_inside_header(struct sk_buff *skb,
1515 struct sk_buff* skb1,
1516 const u32 len, const int pos)
1517{
1518 int i;
1519
1520 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1521
1522 /* And move data appendix as is. */
1523 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1524 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1525
1526 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1527 skb_shinfo(skb)->nr_frags = 0;
1528 skb1->data_len = skb->data_len;
1529 skb1->len += skb1->data_len;
1530 skb->data_len = 0;
1531 skb->len = len;
1532 skb->tail = skb->data + len;
1533}
1534
1535static inline void skb_split_no_header(struct sk_buff *skb,
1536 struct sk_buff* skb1,
1537 const u32 len, int pos)
1538{
1539 int i, k = 0;
1540 const int nfrags = skb_shinfo(skb)->nr_frags;
1541
1542 skb_shinfo(skb)->nr_frags = 0;
1543 skb1->len = skb1->data_len = skb->len - len;
1544 skb->len = len;
1545 skb->data_len = len - pos;
1546
1547 for (i = 0; i < nfrags; i++) {
1548 int size = skb_shinfo(skb)->frags[i].size;
1549
1550 if (pos + size > len) {
1551 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1552
1553 if (pos < len) {
1554 /* Split frag.
1555 * We have two variants in this case:
1556 * 1. Move all the frag to the second
1557 * part, if it is possible. F.e.
1558 * this approach is mandatory for TUX,
1559 * where splitting is expensive.
1560 * 2. Split is accurately. We make this.
1561 */
1562 get_page(skb_shinfo(skb)->frags[i].page);
1563 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1564 skb_shinfo(skb1)->frags[0].size -= len - pos;
1565 skb_shinfo(skb)->frags[i].size = len - pos;
1566 skb_shinfo(skb)->nr_frags++;
1567 }
1568 k++;
1569 } else
1570 skb_shinfo(skb)->nr_frags++;
1571 pos += size;
1572 }
1573 skb_shinfo(skb1)->nr_frags = k;
1574}
1575
1576/**
1577 * skb_split - Split fragmented skb to two parts at length len.
1578 * @skb: the buffer to split
1579 * @skb1: the buffer to receive the second part
1580 * @len: new length for skb
1581 */
1582void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1583{
1584 int pos = skb_headlen(skb);
1585
1586 if (len < pos) /* Split line is inside header. */
1587 skb_split_inside_header(skb, skb1, len, pos);
1588 else /* Second chunk has no header, nothing to copy. */
1589 skb_split_no_header(skb, skb1, len, pos);
1590}
1591
Thomas Graf677e90e2005-06-23 20:59:51 -07001592/**
1593 * skb_prepare_seq_read - Prepare a sequential read of skb data
1594 * @skb: the buffer to read
1595 * @from: lower offset of data to be read
1596 * @to: upper offset of data to be read
1597 * @st: state variable
1598 *
1599 * Initializes the specified state variable. Must be called before
1600 * invoking skb_seq_read() for the first time.
1601 */
1602void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1603 unsigned int to, struct skb_seq_state *st)
1604{
1605 st->lower_offset = from;
1606 st->upper_offset = to;
1607 st->root_skb = st->cur_skb = skb;
1608 st->frag_idx = st->stepped_offset = 0;
1609 st->frag_data = NULL;
1610}
1611
1612/**
1613 * skb_seq_read - Sequentially read skb data
1614 * @consumed: number of bytes consumed by the caller so far
1615 * @data: destination pointer for data to be returned
1616 * @st: state variable
1617 *
1618 * Reads a block of skb data at &consumed relative to the
1619 * lower offset specified to skb_prepare_seq_read(). Assigns
1620 * the head of the data block to &data and returns the length
1621 * of the block or 0 if the end of the skb data or the upper
1622 * offset has been reached.
1623 *
1624 * The caller is not required to consume all of the data
1625 * returned, i.e. &consumed is typically set to the number
1626 * of bytes already consumed and the next call to
1627 * skb_seq_read() will return the remaining part of the block.
1628 *
1629 * Note: The size of each block of data returned can be arbitary,
1630 * this limitation is the cost for zerocopy seqeuental
1631 * reads of potentially non linear data.
1632 *
1633 * Note: Fragment lists within fragments are not implemented
1634 * at the moment, state->root_skb could be replaced with
1635 * a stack for this purpose.
1636 */
1637unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1638 struct skb_seq_state *st)
1639{
1640 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1641 skb_frag_t *frag;
1642
1643 if (unlikely(abs_offset >= st->upper_offset))
1644 return 0;
1645
1646next_skb:
1647 block_limit = skb_headlen(st->cur_skb);
1648
1649 if (abs_offset < block_limit) {
1650 *data = st->cur_skb->data + abs_offset;
1651 return block_limit - abs_offset;
1652 }
1653
1654 if (st->frag_idx == 0 && !st->frag_data)
1655 st->stepped_offset += skb_headlen(st->cur_skb);
1656
1657 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1658 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1659 block_limit = frag->size + st->stepped_offset;
1660
1661 if (abs_offset < block_limit) {
1662 if (!st->frag_data)
1663 st->frag_data = kmap_skb_frag(frag);
1664
1665 *data = (u8 *) st->frag_data + frag->page_offset +
1666 (abs_offset - st->stepped_offset);
1667
1668 return block_limit - abs_offset;
1669 }
1670
1671 if (st->frag_data) {
1672 kunmap_skb_frag(st->frag_data);
1673 st->frag_data = NULL;
1674 }
1675
1676 st->frag_idx++;
1677 st->stepped_offset += frag->size;
1678 }
1679
1680 if (st->cur_skb->next) {
1681 st->cur_skb = st->cur_skb->next;
1682 st->frag_idx = 0;
1683 goto next_skb;
1684 } else if (st->root_skb == st->cur_skb &&
1685 skb_shinfo(st->root_skb)->frag_list) {
1686 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1687 goto next_skb;
1688 }
1689
1690 return 0;
1691}
1692
1693/**
1694 * skb_abort_seq_read - Abort a sequential read of skb data
1695 * @st: state variable
1696 *
1697 * Must be called if skb_seq_read() was not called until it
1698 * returned 0.
1699 */
1700void skb_abort_seq_read(struct skb_seq_state *st)
1701{
1702 if (st->frag_data)
1703 kunmap_skb_frag(st->frag_data);
1704}
1705
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001706#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1707
1708static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1709 struct ts_config *conf,
1710 struct ts_state *state)
1711{
1712 return skb_seq_read(offset, text, TS_SKB_CB(state));
1713}
1714
1715static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1716{
1717 skb_abort_seq_read(TS_SKB_CB(state));
1718}
1719
1720/**
1721 * skb_find_text - Find a text pattern in skb data
1722 * @skb: the buffer to look in
1723 * @from: search offset
1724 * @to: search limit
1725 * @config: textsearch configuration
1726 * @state: uninitialized textsearch state variable
1727 *
1728 * Finds a pattern in the skb data according to the specified
1729 * textsearch configuration. Use textsearch_next() to retrieve
1730 * subsequent occurrences of the pattern. Returns the offset
1731 * to the first occurrence or UINT_MAX if no match was found.
1732 */
1733unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1734 unsigned int to, struct ts_config *config,
1735 struct ts_state *state)
1736{
Phil Oesterf72b9482006-06-26 00:00:57 -07001737 unsigned int ret;
1738
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001739 config->get_next_block = skb_ts_get_next_block;
1740 config->finish = skb_ts_finish;
1741
1742 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1743
Phil Oesterf72b9482006-06-26 00:00:57 -07001744 ret = textsearch_find(config, state);
1745 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001746}
1747
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001748/**
1749 * skb_append_datato_frags: - append the user data to a skb
1750 * @sk: sock structure
1751 * @skb: skb structure to be appened with user data.
1752 * @getfrag: call back function to be used for getting the user data
1753 * @from: pointer to user message iov
1754 * @length: length of the iov message
1755 *
1756 * Description: This procedure append the user data in the fragment part
1757 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1758 */
1759int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001760 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001761 int len, int odd, struct sk_buff *skb),
1762 void *from, int length)
1763{
1764 int frg_cnt = 0;
1765 skb_frag_t *frag = NULL;
1766 struct page *page = NULL;
1767 int copy, left;
1768 int offset = 0;
1769 int ret;
1770
1771 do {
1772 /* Return error if we don't have space for new frag */
1773 frg_cnt = skb_shinfo(skb)->nr_frags;
1774 if (frg_cnt >= MAX_SKB_FRAGS)
1775 return -EFAULT;
1776
1777 /* allocate a new page for next frag */
1778 page = alloc_pages(sk->sk_allocation, 0);
1779
1780 /* If alloc_page fails just return failure and caller will
1781 * free previous allocated pages by doing kfree_skb()
1782 */
1783 if (page == NULL)
1784 return -ENOMEM;
1785
1786 /* initialize the next frag */
1787 sk->sk_sndmsg_page = page;
1788 sk->sk_sndmsg_off = 0;
1789 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1790 skb->truesize += PAGE_SIZE;
1791 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1792
1793 /* get the new initialized frag */
1794 frg_cnt = skb_shinfo(skb)->nr_frags;
1795 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1796
1797 /* copy the user data to page */
1798 left = PAGE_SIZE - frag->page_offset;
1799 copy = (length > left)? left : length;
1800
1801 ret = getfrag(from, (page_address(frag->page) +
1802 frag->page_offset + frag->size),
1803 offset, copy, 0, skb);
1804 if (ret < 0)
1805 return -EFAULT;
1806
1807 /* copy was successful so update the size parameters */
1808 sk->sk_sndmsg_off += copy;
1809 frag->size += copy;
1810 skb->len += copy;
1811 skb->data_len += copy;
1812 offset += copy;
1813 length -= copy;
1814
1815 } while (length > 0);
1816
1817 return 0;
1818}
1819
Herbert Xucbb042f2006-03-20 22:43:56 -08001820/**
1821 * skb_pull_rcsum - pull skb and update receive checksum
1822 * @skb: buffer to update
1823 * @start: start of data before pull
1824 * @len: length of data pulled
1825 *
1826 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001827 * update the CHECKSUM_COMPLETE checksum. It should be used on
1828 * receive path processing instead of skb_pull unless you know
1829 * that the checksum difference is zero (e.g., a valid IP header)
1830 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001831 */
1832unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1833{
1834 BUG_ON(len > skb->len);
1835 skb->len -= len;
1836 BUG_ON(skb->len < skb->data_len);
1837 skb_postpull_rcsum(skb, skb->data, len);
1838 return skb->data += len;
1839}
1840
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001841EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1842
Herbert Xuf4c50d92006-06-22 03:02:40 -07001843/**
1844 * skb_segment - Perform protocol segmentation on skb.
1845 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001846 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001847 *
1848 * This function performs segmentation on the given skb. It returns
1849 * the segment at the given position. It returns NULL if there are
1850 * no more segments to generate, or when an error is encountered.
1851 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001852struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001853{
1854 struct sk_buff *segs = NULL;
1855 struct sk_buff *tail = NULL;
1856 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001857 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001858 unsigned int offset = doffset;
1859 unsigned int headroom;
1860 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001861 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001862 int nfrags = skb_shinfo(skb)->nr_frags;
1863 int err = -ENOMEM;
1864 int i = 0;
1865 int pos;
1866
1867 __skb_push(skb, doffset);
1868 headroom = skb_headroom(skb);
1869 pos = skb_headlen(skb);
1870
1871 do {
1872 struct sk_buff *nskb;
1873 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001874 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001875 int k;
1876 int size;
1877
1878 len = skb->len - offset;
1879 if (len > mss)
1880 len = mss;
1881
1882 hsize = skb_headlen(skb) - offset;
1883 if (hsize < 0)
1884 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001885 if (hsize > len || !sg)
1886 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001887
Herbert Xuc8884ed2006-10-29 15:59:41 -08001888 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001889 if (unlikely(!nskb))
1890 goto err;
1891
1892 if (segs)
1893 tail->next = nskb;
1894 else
1895 segs = nskb;
1896 tail = nskb;
1897
1898 nskb->dev = skb->dev;
1899 nskb->priority = skb->priority;
1900 nskb->protocol = skb->protocol;
1901 nskb->dst = dst_clone(skb->dst);
1902 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1903 nskb->pkt_type = skb->pkt_type;
1904 nskb->mac_len = skb->mac_len;
1905
1906 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001907 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001908 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001909 nskb->transport_header = (nskb->network_header +
1910 skb_network_header_len(skb));
Herbert Xuf4c50d92006-06-22 03:02:40 -07001911 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1912
1913 if (!sg) {
1914 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1915 skb_put(nskb, len),
1916 len, 0);
1917 continue;
1918 }
1919
1920 frag = skb_shinfo(nskb)->frags;
1921 k = 0;
1922
Patrick McHardy84fa7932006-08-29 16:44:56 -07001923 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001924 nskb->csum = skb->csum;
1925 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1926
1927 while (pos < offset + len) {
1928 BUG_ON(i >= nfrags);
1929
1930 *frag = skb_shinfo(skb)->frags[i];
1931 get_page(frag->page);
1932 size = frag->size;
1933
1934 if (pos < offset) {
1935 frag->page_offset += offset - pos;
1936 frag->size -= offset - pos;
1937 }
1938
1939 k++;
1940
1941 if (pos + size <= offset + len) {
1942 i++;
1943 pos += size;
1944 } else {
1945 frag->size -= pos + size - (offset + len);
1946 break;
1947 }
1948
1949 frag++;
1950 }
1951
1952 skb_shinfo(nskb)->nr_frags = k;
1953 nskb->data_len = len - hsize;
1954 nskb->len += nskb->data_len;
1955 nskb->truesize += nskb->data_len;
1956 } while ((offset += len) < skb->len);
1957
1958 return segs;
1959
1960err:
1961 while ((skb = segs)) {
1962 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08001963 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001964 }
1965 return ERR_PTR(err);
1966}
1967
1968EXPORT_SYMBOL_GPL(skb_segment);
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970void __init skb_init(void)
1971{
1972 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1973 sizeof(struct sk_buff),
1974 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07001975 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07001977 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1978 (2*sizeof(struct sk_buff)) +
1979 sizeof(atomic_t),
1980 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07001981 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07001982 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985EXPORT_SYMBOL(___pskb_trim);
1986EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08001987EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07001989EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07001990EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991EXPORT_SYMBOL(pskb_copy);
1992EXPORT_SYMBOL(pskb_expand_head);
1993EXPORT_SYMBOL(skb_checksum);
1994EXPORT_SYMBOL(skb_clone);
1995EXPORT_SYMBOL(skb_clone_fraglist);
1996EXPORT_SYMBOL(skb_copy);
1997EXPORT_SYMBOL(skb_copy_and_csum_bits);
1998EXPORT_SYMBOL(skb_copy_and_csum_dev);
1999EXPORT_SYMBOL(skb_copy_bits);
2000EXPORT_SYMBOL(skb_copy_expand);
2001EXPORT_SYMBOL(skb_over_panic);
2002EXPORT_SYMBOL(skb_pad);
2003EXPORT_SYMBOL(skb_realloc_headroom);
2004EXPORT_SYMBOL(skb_under_panic);
2005EXPORT_SYMBOL(skb_dequeue);
2006EXPORT_SYMBOL(skb_dequeue_tail);
2007EXPORT_SYMBOL(skb_insert);
2008EXPORT_SYMBOL(skb_queue_purge);
2009EXPORT_SYMBOL(skb_queue_head);
2010EXPORT_SYMBOL(skb_queue_tail);
2011EXPORT_SYMBOL(skb_unlink);
2012EXPORT_SYMBOL(skb_append);
2013EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002014EXPORT_SYMBOL(skb_prepare_seq_read);
2015EXPORT_SYMBOL(skb_seq_read);
2016EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002017EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002018EXPORT_SYMBOL(skb_append_datato_frags);