blob: a203bedefe098cd4558e0c61965bfac8ff6929bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
49#include <linux/netdevice.h>
50#ifdef CONFIG_NET_CLS_ACT
51#include <net/pkt_sched.h>
52#endif
53#include <linux/string.h>
54#include <linux/skbuff.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#include <net/protocol.h>
60#include <net/dst.h>
61#include <net/sock.h>
62#include <net/checksum.h>
63#include <net/xfrm.h>
64
65#include <asm/uaccess.h>
66#include <asm/system.h>
67
Al Viroa1f8e7f72006-10-19 16:08:53 -040068#include "kmap_skb.h"
69
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *skbuff_head_cache __read_mostly;
71static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
Patrick McHardy26095452005-04-21 16:43:02 -070089 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070090 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070091 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -070092 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -070093 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
Patrick McHardy26095452005-04-21 16:43:02 -0700108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700109 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700110 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700111 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700112 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 BUG();
114}
115
David S. Millerdc6de332006-04-20 00:10:50 -0700116void skb_truesize_bug(struct sk_buff *skb)
117{
118 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
119 "len=%u, sizeof(sk_buff)=%Zd\n",
120 skb->truesize, skb->len, sizeof(struct sk_buff));
121}
122EXPORT_SYMBOL(skb_truesize_bug);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Allocate a new skbuff. We do this ourselves so we can fill in a few
125 * 'private' fields and also do memory statistics to find all the
126 * [BEEP] leaks.
127 *
128 */
129
130/**
David S. Millerd179cd12005-08-17 14:57:30 -0700131 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * @size: size to allocate
133 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700134 * @fclone: allocate from fclone cache instead of head cache
135 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800136 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * Allocate a new &sk_buff. The returned buffer has no headroom and a
139 * tail room of size bytes. The object has a reference count of one.
140 * The return is the buffer. On a failure the return is %NULL.
141 *
142 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 * %GFP_ATOMIC.
144 */
Al Virodd0fc662005-10-07 07:46:04 +0100145struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800146 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Christoph Lametere18b8902006-12-06 20:33:20 -0800148 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800149 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct sk_buff *skb;
151 u8 *data;
152
Herbert Xu8798b3f2006-01-23 16:32:45 -0800153 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800156 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!skb)
158 goto out;
159
160 /* Get the DATA. Size must match skb_add_mtu(). */
161 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800162 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
163 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!data)
165 goto nodata;
166
167 memset(skb, 0, offsetof(struct sk_buff, truesize));
168 skb->truesize = size + sizeof(struct sk_buff);
169 atomic_set(&skb->users, 1);
170 skb->head = data;
171 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700172 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700173 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800174 /* make sure we initialize shinfo sequentially */
175 shinfo = skb_shinfo(skb);
176 atomic_set(&shinfo->dataref, 1);
177 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700178 shinfo->gso_size = 0;
179 shinfo->gso_segs = 0;
180 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800181 shinfo->ip6_frag_id = 0;
182 shinfo->frag_list = NULL;
183
David S. Millerd179cd12005-08-17 14:57:30 -0700184 if (fclone) {
185 struct sk_buff *child = skb + 1;
186 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David S. Millerd179cd12005-08-17 14:57:30 -0700188 skb->fclone = SKB_FCLONE_ORIG;
189 atomic_set(fclone_ref, 1);
190
191 child->fclone = SKB_FCLONE_UNAVAILABLE;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193out:
194 return skb;
195nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800196 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 skb = NULL;
198 goto out;
199}
200
201/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700202 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
203 * @dev: network device to receive on
204 * @length: length to allocate
205 * @gfp_mask: get_free_pages mask, passed to alloc_skb
206 *
207 * Allocate a new &sk_buff and assign it a usage count of one. The
208 * buffer has unspecified headroom built in. Users should allocate
209 * the headroom they think they need without accounting for the
210 * built in space. The built in space is used for optimisations.
211 *
212 * %NULL is returned if there is no free memory.
213 */
214struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
215 unsigned int length, gfp_t gfp_mask)
216{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700217 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700218 struct sk_buff *skb;
219
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900220 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700221 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700222 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700223 skb->dev = dev;
224 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700225 return skb;
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Herbert Xu27b437c2006-07-13 19:26:39 -0700228static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Herbert Xu27b437c2006-07-13 19:26:39 -0700230 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Herbert Xu27b437c2006-07-13 19:26:39 -0700232 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 do {
235 struct sk_buff *this = list;
236 list = list->next;
237 kfree_skb(this);
238 } while (list);
239}
240
Herbert Xu27b437c2006-07-13 19:26:39 -0700241static inline void skb_drop_fraglist(struct sk_buff *skb)
242{
243 skb_drop_list(&skb_shinfo(skb)->frag_list);
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static void skb_clone_fraglist(struct sk_buff *skb)
247{
248 struct sk_buff *list;
249
250 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
251 skb_get(list);
252}
253
Adrian Bunk5bba1712006-06-29 13:02:35 -0700254static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 if (!skb->cloned ||
257 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
258 &skb_shinfo(skb)->dataref)) {
259 if (skb_shinfo(skb)->nr_frags) {
260 int i;
261 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
262 put_page(skb_shinfo(skb)->frags[i].page);
263 }
264
265 if (skb_shinfo(skb)->frag_list)
266 skb_drop_fraglist(skb);
267
268 kfree(skb->head);
269 }
270}
271
272/*
273 * Free an skbuff by memory without cleaning the state.
274 */
275void kfree_skbmem(struct sk_buff *skb)
276{
David S. Millerd179cd12005-08-17 14:57:30 -0700277 struct sk_buff *other;
278 atomic_t *fclone_ref;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 skb_release_data(skb);
David S. Millerd179cd12005-08-17 14:57:30 -0700281 switch (skb->fclone) {
282 case SKB_FCLONE_UNAVAILABLE:
283 kmem_cache_free(skbuff_head_cache, skb);
284 break;
285
286 case SKB_FCLONE_ORIG:
287 fclone_ref = (atomic_t *) (skb + 2);
288 if (atomic_dec_and_test(fclone_ref))
289 kmem_cache_free(skbuff_fclone_cache, skb);
290 break;
291
292 case SKB_FCLONE_CLONE:
293 fclone_ref = (atomic_t *) (skb + 1);
294 other = skb - 1;
295
296 /* The clone portion is available for
297 * fast-cloning again.
298 */
299 skb->fclone = SKB_FCLONE_UNAVAILABLE;
300
301 if (atomic_dec_and_test(fclone_ref))
302 kmem_cache_free(skbuff_fclone_cache, other);
303 break;
304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/**
308 * __kfree_skb - private function
309 * @skb: buffer
310 *
311 * Free an sk_buff. Release anything attached to the buffer.
312 * Clean the state. This is an internal helper function. Users should
313 * always call kfree_skb
314 */
315
316void __kfree_skb(struct sk_buff *skb)
317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 dst_release(skb->dst);
319#ifdef CONFIG_XFRM
320 secpath_put(skb->sp);
321#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700322 if (skb->destructor) {
323 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 skb->destructor(skb);
325 }
326#ifdef CONFIG_NETFILTER
327 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800328#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
329 nf_conntrack_put_reasm(skb->nfct_reasm);
330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#ifdef CONFIG_BRIDGE_NETFILTER
332 nf_bridge_put(skb->nf_bridge);
333#endif
334#endif
335/* XXX: IS this still necessary? - JHS */
336#ifdef CONFIG_NET_SCHED
337 skb->tc_index = 0;
338#ifdef CONFIG_NET_CLS_ACT
339 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
341#endif
342
343 kfree_skbmem(skb);
344}
345
346/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800347 * kfree_skb - free an sk_buff
348 * @skb: buffer to free
349 *
350 * Drop a reference to the buffer and free it if the usage count has
351 * hit zero.
352 */
353void kfree_skb(struct sk_buff *skb)
354{
355 if (unlikely(!skb))
356 return;
357 if (likely(atomic_read(&skb->users) == 1))
358 smp_rmb();
359 else if (likely(!atomic_dec_and_test(&skb->users)))
360 return;
361 __kfree_skb(skb);
362}
363
364/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * skb_clone - duplicate an sk_buff
366 * @skb: buffer to clone
367 * @gfp_mask: allocation priority
368 *
369 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
370 * copies share the same packet data but not structure. The new
371 * buffer has a reference count of 1. If the allocation fails the
372 * function returns %NULL otherwise the new buffer is returned.
373 *
374 * If this function is called from an interrupt gfp_mask() must be
375 * %GFP_ATOMIC.
376 */
377
Al Virodd0fc662005-10-07 07:46:04 +0100378struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
David S. Millerd179cd12005-08-17 14:57:30 -0700380 struct sk_buff *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David S. Millerd179cd12005-08-17 14:57:30 -0700382 n = skb + 1;
383 if (skb->fclone == SKB_FCLONE_ORIG &&
384 n->fclone == SKB_FCLONE_UNAVAILABLE) {
385 atomic_t *fclone_ref = (atomic_t *) (n + 1);
386 n->fclone = SKB_FCLONE_CLONE;
387 atomic_inc(fclone_ref);
388 } else {
389 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
390 if (!n)
391 return NULL;
392 n->fclone = SKB_FCLONE_UNAVAILABLE;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395#define C(x) n->x = skb->x
396
397 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 n->sk = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700399 C(tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 C(dev);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700401 C(transport_header);
402 C(network_header);
403 C(mac_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 C(dst);
405 dst_clone(skb->dst);
406 C(sp);
407#ifdef CONFIG_INET
408 secpath_get(skb->sp);
409#endif
410 memcpy(n->cb, skb->cb, sizeof(skb->cb));
411 C(len);
412 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700413 C(mac_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 C(csum);
415 C(local_df);
416 n->cloned = 1;
417 n->nohdr = 0;
418 C(pkt_type);
419 C(ip_summed);
420 C(priority);
YOSHIFUJI Hideakia8372f02006-02-19 22:32:06 -0800421#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
422 C(ipvs_property);
423#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 C(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 n->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800426 C(mark);
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700427 __nf_copy(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#ifdef CONFIG_NET_SCHED
429 C(tc_index);
430#ifdef CONFIG_NET_CLS_ACT
431 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
David S. Millerb72f6ec2005-07-19 14:13:54 -0700432 n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
433 n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700434 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#endif
James Morris984bc162006-06-09 00:29:17 -0700436 skb_copy_secmark(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437#endif
438 C(truesize);
439 atomic_set(&n->users, 1);
440 C(head);
441 C(data);
442 C(tail);
443 C(end);
444
445 atomic_inc(&(skb_shinfo(skb)->dataref));
446 skb->cloned = 1;
447
448 return n;
449}
450
451static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
452{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700453#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * Shift between the two data areas in bytes
456 */
457 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 new->sk = NULL;
460 new->dev = old->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 new->priority = old->priority;
462 new->protocol = old->protocol;
463 new->dst = dst_clone(old->dst);
464#ifdef CONFIG_INET
465 new->sp = secpath_get(old->sp);
466#endif
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700467 new->transport_header = old->transport_header;
468 new->network_header = old->network_header;
469 new->mac_header = old->mac_header;
470#ifndef NET_SKBUFF_DATA_USES_OFFSET
471 /* {transport,network,mac}_header are relative to skb->head */
472 new->transport_header += offset;
473 new->network_header += offset;
474 new->mac_header += offset;
475#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 memcpy(new->cb, old->cb, sizeof(old->cb));
477 new->local_df = old->local_df;
David S. Millerd179cd12005-08-17 14:57:30 -0700478 new->fclone = SKB_FCLONE_UNAVAILABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 new->pkt_type = old->pkt_type;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700480 new->tstamp = old->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 new->destructor = NULL;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800482 new->mark = old->mark;
Yasuyuki Kozakaiedda5532007-03-14 16:43:37 -0700483 __nf_copy(new, old);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300484#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
485 new->ipvs_property = old->ipvs_property;
486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#ifdef CONFIG_NET_SCHED
488#ifdef CONFIG_NET_CLS_ACT
489 new->tc_verd = old->tc_verd;
490#endif
491 new->tc_index = old->tc_index;
492#endif
James Morris984bc162006-06-09 00:29:17 -0700493 skb_copy_secmark(new, old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 atomic_set(&new->users, 1);
Herbert Xu79671682006-06-22 02:40:14 -0700495 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
496 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
497 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/**
501 * skb_copy - create private copy of an sk_buff
502 * @skb: buffer to copy
503 * @gfp_mask: allocation priority
504 *
505 * Make a copy of both an &sk_buff and its data. This is used when the
506 * caller wishes to modify the data and needs a private copy of the
507 * data to alter. Returns %NULL on failure or the pointer to the buffer
508 * on success. The returned buffer has a reference count of 1.
509 *
510 * As by-product this function converts non-linear &sk_buff to linear
511 * one, so that &sk_buff becomes completely private and caller is allowed
512 * to modify all the data of returned buffer. This means that this
513 * function is not recommended for use in circumstances when only
514 * header is going to be modified. Use pskb_copy() instead.
515 */
516
Al Virodd0fc662005-10-07 07:46:04 +0100517struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 int headerlen = skb->data - skb->head;
520 /*
521 * Allocate the copy buffer
522 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700523 struct sk_buff *n;
524#ifdef NET_SKBUFF_DATA_USES_OFFSET
525 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
526#else
527 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!n)
530 return NULL;
531
532 /* Set the data pointer */
533 skb_reserve(n, headerlen);
534 /* Set the tail pointer and length */
535 skb_put(n, skb->len);
536 n->csum = skb->csum;
537 n->ip_summed = skb->ip_summed;
538
539 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
540 BUG();
541
542 copy_skb_header(n, skb);
543 return n;
544}
545
546
547/**
548 * pskb_copy - create copy of an sk_buff with private head.
549 * @skb: buffer to copy
550 * @gfp_mask: allocation priority
551 *
552 * Make a copy of both an &sk_buff and part of its data, located
553 * in header. Fragmented data remain shared. This is used when
554 * the caller wishes to modify only header of &sk_buff and needs
555 * private copy of the header to alter. Returns %NULL on failure
556 * or the pointer to the buffer on success.
557 * The returned buffer has a reference count of 1.
558 */
559
Al Virodd0fc662005-10-07 07:46:04 +0100560struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 /*
563 * Allocate the copy buffer
564 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700565 struct sk_buff *n;
566#ifdef NET_SKBUFF_DATA_USES_OFFSET
567 n = alloc_skb(skb->end, gfp_mask);
568#else
569 n = alloc_skb(skb->end - skb->head, gfp_mask);
570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!n)
572 goto out;
573
574 /* Set the data pointer */
575 skb_reserve(n, skb->data - skb->head);
576 /* Set the tail pointer and length */
577 skb_put(n, skb_headlen(skb));
578 /* Copy the bytes */
579 memcpy(n->data, skb->data, n->len);
580 n->csum = skb->csum;
581 n->ip_summed = skb->ip_summed;
582
Herbert Xu25f484a2006-11-07 14:57:15 -0800583 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 n->data_len = skb->data_len;
585 n->len = skb->len;
586
587 if (skb_shinfo(skb)->nr_frags) {
588 int i;
589
590 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
591 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
592 get_page(skb_shinfo(n)->frags[i].page);
593 }
594 skb_shinfo(n)->nr_frags = i;
595 }
596
597 if (skb_shinfo(skb)->frag_list) {
598 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
599 skb_clone_fraglist(n);
600 }
601
602 copy_skb_header(n, skb);
603out:
604 return n;
605}
606
607/**
608 * pskb_expand_head - reallocate header of &sk_buff
609 * @skb: buffer to reallocate
610 * @nhead: room to add at head
611 * @ntail: room to add at tail
612 * @gfp_mask: allocation priority
613 *
614 * Expands (or creates identical copy, if &nhead and &ntail are zero)
615 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
616 * reference count of 1. Returns zero in the case of success or error,
617 * if expansion failed. In the last case, &sk_buff is not changed.
618 *
619 * All the pointers pointing into skb header may change and must be
620 * reloaded after call to this function.
621 */
622
Victor Fusco86a76ca2005-07-08 14:57:47 -0700623int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100624 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int i;
627 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700628#ifdef NET_SKBUFF_DATA_USES_OFFSET
629 int size = nhead + skb->end + ntail;
630#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 long off;
634
635 if (skb_shared(skb))
636 BUG();
637
638 size = SKB_DATA_ALIGN(size);
639
640 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
641 if (!data)
642 goto nodata;
643
644 /* Copy only real data... and, alas, header. This should be
645 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700646 memcpy(data + nhead, skb->head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700647#ifdef NET_SKBUFF_DATA_USES_OFFSET
648 skb->tail);
649#else
650 skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700651#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700652 memcpy(data + size, skb_end_pointer(skb),
653 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
656 get_page(skb_shinfo(skb)->frags[i].page);
657
658 if (skb_shinfo(skb)->frag_list)
659 skb_clone_fraglist(skb);
660
661 skb_release_data(skb);
662
663 off = (data + nhead) - skb->head;
664
665 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700667#ifdef NET_SKBUFF_DATA_USES_OFFSET
668 skb->end = size;
669#else
670 skb->end = skb->head + size;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700671 /* {transport,network,mac}_header and tail are relative to skb->head */
672 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700673 skb->transport_header += off;
674 skb->network_header += off;
675 skb->mac_header += off;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700676#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 skb->cloned = 0;
678 skb->nohdr = 0;
679 atomic_set(&skb_shinfo(skb)->dataref, 1);
680 return 0;
681
682nodata:
683 return -ENOMEM;
684}
685
686/* Make private copy of skb with writable head and some headroom */
687
688struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
689{
690 struct sk_buff *skb2;
691 int delta = headroom - skb_headroom(skb);
692
693 if (delta <= 0)
694 skb2 = pskb_copy(skb, GFP_ATOMIC);
695 else {
696 skb2 = skb_clone(skb, GFP_ATOMIC);
697 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
698 GFP_ATOMIC)) {
699 kfree_skb(skb2);
700 skb2 = NULL;
701 }
702 }
703 return skb2;
704}
705
706
707/**
708 * skb_copy_expand - copy and expand sk_buff
709 * @skb: buffer to copy
710 * @newheadroom: new free bytes at head
711 * @newtailroom: new free bytes at tail
712 * @gfp_mask: allocation priority
713 *
714 * Make a copy of both an &sk_buff and its data and while doing so
715 * allocate additional space.
716 *
717 * This is used when the caller wishes to modify the data and needs a
718 * private copy of the data to alter as well as more space for new fields.
719 * Returns %NULL on failure or the pointer to the buffer
720 * on success. The returned buffer has a reference count of 1.
721 *
722 * You must pass %GFP_ATOMIC as the allocation priority if this function
723 * is called from an interrupt.
724 *
725 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
726 * only by netfilter in the cases when checksum is recalculated? --ANK
727 */
728struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700729 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100730 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 /*
733 * Allocate the copy buffer
734 */
735 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
736 gfp_mask);
737 int head_copy_len, head_copy_off;
738
739 if (!n)
740 return NULL;
741
742 skb_reserve(n, newheadroom);
743
744 /* Set the tail pointer and length */
745 skb_put(n, skb->len);
746
747 head_copy_len = skb_headroom(skb);
748 head_copy_off = 0;
749 if (newheadroom <= head_copy_len)
750 head_copy_len = newheadroom;
751 else
752 head_copy_off = newheadroom - head_copy_len;
753
754 /* Copy the linear header and data. */
755 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
756 skb->len + head_copy_len))
757 BUG();
758
759 copy_skb_header(n, skb);
760
761 return n;
762}
763
764/**
765 * skb_pad - zero pad the tail of an skb
766 * @skb: buffer to pad
767 * @pad: space to pad
768 *
769 * Ensure that a buffer is followed by a padding area that is zero
770 * filled. Used by network drivers which may DMA or transfer data
771 * beyond the buffer end onto the wire.
772 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700773 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900775
Herbert Xu5b057c62006-06-23 02:06:41 -0700776int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Herbert Xu5b057c62006-06-23 02:06:41 -0700778 int err;
779 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700782 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700786
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700787 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700788 if (likely(skb_cloned(skb) || ntail > 0)) {
789 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
790 if (unlikely(err))
791 goto free_skb;
792 }
793
794 /* FIXME: The use of this function with non-linear skb's really needs
795 * to be audited.
796 */
797 err = skb_linearize(skb);
798 if (unlikely(err))
799 goto free_skb;
800
801 memset(skb->data + skb->len, 0, pad);
802 return 0;
803
804free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700806 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900807}
808
Herbert Xu3cc0e872006-06-09 16:13:38 -0700809/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
811
Herbert Xu3cc0e872006-06-09 16:13:38 -0700812int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Herbert Xu27b437c2006-07-13 19:26:39 -0700814 struct sk_buff **fragp;
815 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int offset = skb_headlen(skb);
817 int nfrags = skb_shinfo(skb)->nr_frags;
818 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700819 int err;
820
821 if (skb_cloned(skb) &&
822 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700825 i = 0;
826 if (offset >= len)
827 goto drop_pages;
828
829 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700831
832 if (end < len) {
833 offset = end;
834 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700836
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700837 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700838
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700839drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700840 skb_shinfo(skb)->nr_frags = i;
841
842 for (; i < nfrags; i++)
843 put_page(skb_shinfo(skb)->frags[i].page);
844
845 if (skb_shinfo(skb)->frag_list)
846 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700847 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Herbert Xu27b437c2006-07-13 19:26:39 -0700850 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
851 fragp = &frag->next) {
852 int end = offset + frag->len;
853
854 if (skb_shared(frag)) {
855 struct sk_buff *nfrag;
856
857 nfrag = skb_clone(frag, GFP_ATOMIC);
858 if (unlikely(!nfrag))
859 return -ENOMEM;
860
861 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700862 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -0700863 frag = nfrag;
864 *fragp = frag;
865 }
866
867 if (end < len) {
868 offset = end;
869 continue;
870 }
871
872 if (end > len &&
873 unlikely((err = pskb_trim(frag, len - offset))))
874 return err;
875
876 if (frag->next)
877 skb_drop_list(&frag->next);
878 break;
879 }
880
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700881done:
Herbert Xu27b437c2006-07-13 19:26:39 -0700882 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 skb->data_len -= skb->len - len;
884 skb->len = len;
885 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -0700886 skb->len = len;
887 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700888 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 return 0;
892}
893
894/**
895 * __pskb_pull_tail - advance tail of skb header
896 * @skb: buffer to reallocate
897 * @delta: number of bytes to advance tail
898 *
899 * The function makes a sense only on a fragmented &sk_buff,
900 * it expands header moving its tail forward and copying necessary
901 * data from fragmented part.
902 *
903 * &sk_buff MUST have reference count of 1.
904 *
905 * Returns %NULL (and &sk_buff does not change) if pull failed
906 * or value of new tail of skb in the case of success.
907 *
908 * All the pointers pointing into skb header may change and must be
909 * reloaded after call to this function.
910 */
911
912/* Moves tail of skb head forward, copying data from fragmented part,
913 * when it is necessary.
914 * 1. It may fail due to malloc failure.
915 * 2. It may change skb pointers.
916 *
917 * It is pretty complicated. Luckily, it is called only in exceptional cases.
918 */
919unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
920{
921 /* If skb has not enough free space at tail, get new one
922 * plus 128 bytes for future expansions. If we have enough
923 * room at tail, reallocate without expansion only if skb is cloned.
924 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700925 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (eat > 0 || skb_cloned(skb)) {
928 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
929 GFP_ATOMIC))
930 return NULL;
931 }
932
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700933 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 BUG();
935
936 /* Optimization: no fragments, no reasons to preestimate
937 * size of pulled pages. Superb.
938 */
939 if (!skb_shinfo(skb)->frag_list)
940 goto pull_pages;
941
942 /* Estimate size of pulled pages. */
943 eat = delta;
944 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
945 if (skb_shinfo(skb)->frags[i].size >= eat)
946 goto pull_pages;
947 eat -= skb_shinfo(skb)->frags[i].size;
948 }
949
950 /* If we need update frag list, we are in troubles.
951 * Certainly, it possible to add an offset to skb data,
952 * but taking into account that pulling is expected to
953 * be very rare operation, it is worth to fight against
954 * further bloating skb head and crucify ourselves here instead.
955 * Pure masohism, indeed. 8)8)
956 */
957 if (eat) {
958 struct sk_buff *list = skb_shinfo(skb)->frag_list;
959 struct sk_buff *clone = NULL;
960 struct sk_buff *insp = NULL;
961
962 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -0800963 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 if (list->len <= eat) {
966 /* Eaten as whole. */
967 eat -= list->len;
968 list = list->next;
969 insp = list;
970 } else {
971 /* Eaten partially. */
972
973 if (skb_shared(list)) {
974 /* Sucks! We need to fork list. :-( */
975 clone = skb_clone(list, GFP_ATOMIC);
976 if (!clone)
977 return NULL;
978 insp = list->next;
979 list = clone;
980 } else {
981 /* This may be pulled without
982 * problems. */
983 insp = list;
984 }
985 if (!pskb_pull(list, eat)) {
986 if (clone)
987 kfree_skb(clone);
988 return NULL;
989 }
990 break;
991 }
992 } while (eat);
993
994 /* Free pulled out fragments. */
995 while ((list = skb_shinfo(skb)->frag_list) != insp) {
996 skb_shinfo(skb)->frag_list = list->next;
997 kfree_skb(list);
998 }
999 /* And insert new clone at head. */
1000 if (clone) {
1001 clone->next = list;
1002 skb_shinfo(skb)->frag_list = clone;
1003 }
1004 }
1005 /* Success! Now we may commit changes to skb data. */
1006
1007pull_pages:
1008 eat = delta;
1009 k = 0;
1010 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1011 if (skb_shinfo(skb)->frags[i].size <= eat) {
1012 put_page(skb_shinfo(skb)->frags[i].page);
1013 eat -= skb_shinfo(skb)->frags[i].size;
1014 } else {
1015 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1016 if (eat) {
1017 skb_shinfo(skb)->frags[k].page_offset += eat;
1018 skb_shinfo(skb)->frags[k].size -= eat;
1019 eat = 0;
1020 }
1021 k++;
1022 }
1023 }
1024 skb_shinfo(skb)->nr_frags = k;
1025
1026 skb->tail += delta;
1027 skb->data_len -= delta;
1028
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001029 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/* Copy some data bits from skb to kernel buffer. */
1033
1034int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1035{
1036 int i, copy;
1037 int start = skb_headlen(skb);
1038
1039 if (offset > (int)skb->len - len)
1040 goto fault;
1041
1042 /* Copy header. */
1043 if ((copy = start - offset) > 0) {
1044 if (copy > len)
1045 copy = len;
1046 memcpy(to, skb->data + offset, copy);
1047 if ((len -= copy) == 0)
1048 return 0;
1049 offset += copy;
1050 to += copy;
1051 }
1052
1053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1054 int end;
1055
1056 BUG_TRAP(start <= offset + len);
1057
1058 end = start + skb_shinfo(skb)->frags[i].size;
1059 if ((copy = end - offset) > 0) {
1060 u8 *vaddr;
1061
1062 if (copy > len)
1063 copy = len;
1064
1065 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1066 memcpy(to,
1067 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1068 offset - start, copy);
1069 kunmap_skb_frag(vaddr);
1070
1071 if ((len -= copy) == 0)
1072 return 0;
1073 offset += copy;
1074 to += copy;
1075 }
1076 start = end;
1077 }
1078
1079 if (skb_shinfo(skb)->frag_list) {
1080 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1081
1082 for (; list; list = list->next) {
1083 int end;
1084
1085 BUG_TRAP(start <= offset + len);
1086
1087 end = start + list->len;
1088 if ((copy = end - offset) > 0) {
1089 if (copy > len)
1090 copy = len;
1091 if (skb_copy_bits(list, offset - start,
1092 to, copy))
1093 goto fault;
1094 if ((len -= copy) == 0)
1095 return 0;
1096 offset += copy;
1097 to += copy;
1098 }
1099 start = end;
1100 }
1101 }
1102 if (!len)
1103 return 0;
1104
1105fault:
1106 return -EFAULT;
1107}
1108
Herbert Xu357b40a2005-04-19 22:30:14 -07001109/**
1110 * skb_store_bits - store bits from kernel buffer to skb
1111 * @skb: destination buffer
1112 * @offset: offset in destination
1113 * @from: source buffer
1114 * @len: number of bytes to copy
1115 *
1116 * Copy the specified number of bytes from the source buffer to the
1117 * destination skb. This function handles all the messy bits of
1118 * traversing fragment lists and such.
1119 */
1120
1121int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1122{
1123 int i, copy;
1124 int start = skb_headlen(skb);
1125
1126 if (offset > (int)skb->len - len)
1127 goto fault;
1128
1129 if ((copy = start - offset) > 0) {
1130 if (copy > len)
1131 copy = len;
1132 memcpy(skb->data + offset, from, copy);
1133 if ((len -= copy) == 0)
1134 return 0;
1135 offset += copy;
1136 from += copy;
1137 }
1138
1139 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1140 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1141 int end;
1142
1143 BUG_TRAP(start <= offset + len);
1144
1145 end = start + frag->size;
1146 if ((copy = end - offset) > 0) {
1147 u8 *vaddr;
1148
1149 if (copy > len)
1150 copy = len;
1151
1152 vaddr = kmap_skb_frag(frag);
1153 memcpy(vaddr + frag->page_offset + offset - start,
1154 from, copy);
1155 kunmap_skb_frag(vaddr);
1156
1157 if ((len -= copy) == 0)
1158 return 0;
1159 offset += copy;
1160 from += copy;
1161 }
1162 start = end;
1163 }
1164
1165 if (skb_shinfo(skb)->frag_list) {
1166 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1167
1168 for (; list; list = list->next) {
1169 int end;
1170
1171 BUG_TRAP(start <= offset + len);
1172
1173 end = start + list->len;
1174 if ((copy = end - offset) > 0) {
1175 if (copy > len)
1176 copy = len;
1177 if (skb_store_bits(list, offset - start,
1178 from, copy))
1179 goto fault;
1180 if ((len -= copy) == 0)
1181 return 0;
1182 offset += copy;
1183 from += copy;
1184 }
1185 start = end;
1186 }
1187 }
1188 if (!len)
1189 return 0;
1190
1191fault:
1192 return -EFAULT;
1193}
1194
1195EXPORT_SYMBOL(skb_store_bits);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/* Checksum skb data. */
1198
Al Viro2bbbc862006-11-14 21:37:14 -08001199__wsum skb_checksum(const struct sk_buff *skb, int offset,
1200 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 int start = skb_headlen(skb);
1203 int i, copy = start - offset;
1204 int pos = 0;
1205
1206 /* Checksum header. */
1207 if (copy > 0) {
1208 if (copy > len)
1209 copy = len;
1210 csum = csum_partial(skb->data + offset, copy, csum);
1211 if ((len -= copy) == 0)
1212 return csum;
1213 offset += copy;
1214 pos = copy;
1215 }
1216
1217 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1218 int end;
1219
1220 BUG_TRAP(start <= offset + len);
1221
1222 end = start + skb_shinfo(skb)->frags[i].size;
1223 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001224 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 u8 *vaddr;
1226 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1227
1228 if (copy > len)
1229 copy = len;
1230 vaddr = kmap_skb_frag(frag);
1231 csum2 = csum_partial(vaddr + frag->page_offset +
1232 offset - start, copy, 0);
1233 kunmap_skb_frag(vaddr);
1234 csum = csum_block_add(csum, csum2, pos);
1235 if (!(len -= copy))
1236 return csum;
1237 offset += copy;
1238 pos += copy;
1239 }
1240 start = end;
1241 }
1242
1243 if (skb_shinfo(skb)->frag_list) {
1244 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1245
1246 for (; list; list = list->next) {
1247 int end;
1248
1249 BUG_TRAP(start <= offset + len);
1250
1251 end = start + list->len;
1252 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001253 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (copy > len)
1255 copy = len;
1256 csum2 = skb_checksum(list, offset - start,
1257 copy, 0);
1258 csum = csum_block_add(csum, csum2, pos);
1259 if ((len -= copy) == 0)
1260 return csum;
1261 offset += copy;
1262 pos += copy;
1263 }
1264 start = end;
1265 }
1266 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001267 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 return csum;
1270}
1271
1272/* Both of above in one bottle. */
1273
Al Viro81d77662006-11-14 21:37:33 -08001274__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1275 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 int start = skb_headlen(skb);
1278 int i, copy = start - offset;
1279 int pos = 0;
1280
1281 /* Copy header. */
1282 if (copy > 0) {
1283 if (copy > len)
1284 copy = len;
1285 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1286 copy, csum);
1287 if ((len -= copy) == 0)
1288 return csum;
1289 offset += copy;
1290 to += copy;
1291 pos = copy;
1292 }
1293
1294 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1295 int end;
1296
1297 BUG_TRAP(start <= offset + len);
1298
1299 end = start + skb_shinfo(skb)->frags[i].size;
1300 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001301 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 u8 *vaddr;
1303 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1304
1305 if (copy > len)
1306 copy = len;
1307 vaddr = kmap_skb_frag(frag);
1308 csum2 = csum_partial_copy_nocheck(vaddr +
1309 frag->page_offset +
1310 offset - start, to,
1311 copy, 0);
1312 kunmap_skb_frag(vaddr);
1313 csum = csum_block_add(csum, csum2, pos);
1314 if (!(len -= copy))
1315 return csum;
1316 offset += copy;
1317 to += copy;
1318 pos += copy;
1319 }
1320 start = end;
1321 }
1322
1323 if (skb_shinfo(skb)->frag_list) {
1324 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1325
1326 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001327 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int end;
1329
1330 BUG_TRAP(start <= offset + len);
1331
1332 end = start + list->len;
1333 if ((copy = end - offset) > 0) {
1334 if (copy > len)
1335 copy = len;
1336 csum2 = skb_copy_and_csum_bits(list,
1337 offset - start,
1338 to, copy, 0);
1339 csum = csum_block_add(csum, csum2, pos);
1340 if ((len -= copy) == 0)
1341 return csum;
1342 offset += copy;
1343 to += copy;
1344 pos += copy;
1345 }
1346 start = end;
1347 }
1348 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001349 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return csum;
1351}
1352
1353void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1354{
Al Virod3bc23e2006-11-14 21:24:49 -08001355 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 long csstart;
1357
Patrick McHardy84fa7932006-08-29 16:44:56 -07001358 if (skb->ip_summed == CHECKSUM_PARTIAL)
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001359 csstart = skb_transport_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 else
1361 csstart = skb_headlen(skb);
1362
Kris Katterjohn09a62662006-01-08 22:24:28 -08001363 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 memcpy(to, skb->data, csstart);
1366
1367 csum = 0;
1368 if (csstart != skb->len)
1369 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1370 skb->len - csstart, 0);
1371
Patrick McHardy84fa7932006-08-29 16:44:56 -07001372 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001373 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Al Virod3bc23e2006-11-14 21:24:49 -08001375 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377}
1378
1379/**
1380 * skb_dequeue - remove from the head of the queue
1381 * @list: list to dequeue from
1382 *
1383 * Remove the head of the list. The list lock is taken so the function
1384 * may be used safely with other locking list functions. The head item is
1385 * returned or %NULL if the list is empty.
1386 */
1387
1388struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1389{
1390 unsigned long flags;
1391 struct sk_buff *result;
1392
1393 spin_lock_irqsave(&list->lock, flags);
1394 result = __skb_dequeue(list);
1395 spin_unlock_irqrestore(&list->lock, flags);
1396 return result;
1397}
1398
1399/**
1400 * skb_dequeue_tail - remove from the tail of the queue
1401 * @list: list to dequeue from
1402 *
1403 * Remove the tail of the list. The list lock is taken so the function
1404 * may be used safely with other locking list functions. The tail item is
1405 * returned or %NULL if the list is empty.
1406 */
1407struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1408{
1409 unsigned long flags;
1410 struct sk_buff *result;
1411
1412 spin_lock_irqsave(&list->lock, flags);
1413 result = __skb_dequeue_tail(list);
1414 spin_unlock_irqrestore(&list->lock, flags);
1415 return result;
1416}
1417
1418/**
1419 * skb_queue_purge - empty a list
1420 * @list: list to empty
1421 *
1422 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1423 * the list and one reference dropped. This function takes the list
1424 * lock and is atomic with respect to other list locking functions.
1425 */
1426void skb_queue_purge(struct sk_buff_head *list)
1427{
1428 struct sk_buff *skb;
1429 while ((skb = skb_dequeue(list)) != NULL)
1430 kfree_skb(skb);
1431}
1432
1433/**
1434 * skb_queue_head - queue a buffer at the list head
1435 * @list: list to use
1436 * @newsk: buffer to queue
1437 *
1438 * Queue a buffer at the start of the list. This function takes the
1439 * list lock and can be used safely with other locking &sk_buff functions
1440 * safely.
1441 *
1442 * A buffer cannot be placed on two lists at the same time.
1443 */
1444void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1445{
1446 unsigned long flags;
1447
1448 spin_lock_irqsave(&list->lock, flags);
1449 __skb_queue_head(list, newsk);
1450 spin_unlock_irqrestore(&list->lock, flags);
1451}
1452
1453/**
1454 * skb_queue_tail - queue a buffer at the list tail
1455 * @list: list to use
1456 * @newsk: buffer to queue
1457 *
1458 * Queue a buffer at the tail of the list. This function takes the
1459 * list lock and can be used safely with other locking &sk_buff functions
1460 * safely.
1461 *
1462 * A buffer cannot be placed on two lists at the same time.
1463 */
1464void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1465{
1466 unsigned long flags;
1467
1468 spin_lock_irqsave(&list->lock, flags);
1469 __skb_queue_tail(list, newsk);
1470 spin_unlock_irqrestore(&list->lock, flags);
1471}
David S. Miller8728b832005-08-09 19:25:21 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/**
1474 * skb_unlink - remove a buffer from a list
1475 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001476 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 *
David S. Miller8728b832005-08-09 19:25:21 -07001478 * Remove a packet from a list. The list locks are taken and this
1479 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
David S. Miller8728b832005-08-09 19:25:21 -07001481 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
David S. Miller8728b832005-08-09 19:25:21 -07001483void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
David S. Miller8728b832005-08-09 19:25:21 -07001485 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
David S. Miller8728b832005-08-09 19:25:21 -07001487 spin_lock_irqsave(&list->lock, flags);
1488 __skb_unlink(skb, list);
1489 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/**
1493 * skb_append - append a buffer
1494 * @old: buffer to insert after
1495 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001496 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
1498 * Place a packet after a given packet in a list. The list locks are taken
1499 * and this function is atomic with respect to other list locked calls.
1500 * A buffer cannot be placed on two lists at the same time.
1501 */
David S. Miller8728b832005-08-09 19:25:21 -07001502void skb_append(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_append(old, newsk, list);
1508 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511
1512/**
1513 * skb_insert - insert a buffer
1514 * @old: buffer to insert before
1515 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001516 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
David S. Miller8728b832005-08-09 19:25:21 -07001518 * Place a packet before a given packet in a list. The list locks are
1519 * taken and this function is atomic with respect to other list locked
1520 * calls.
1521 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * A buffer cannot be placed on two lists at the same time.
1523 */
David S. Miller8728b832005-08-09 19:25:21 -07001524void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 unsigned long flags;
1527
David S. Miller8728b832005-08-09 19:25:21 -07001528 spin_lock_irqsave(&list->lock, flags);
1529 __skb_insert(newsk, old->prev, old, list);
1530 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533#if 0
1534/*
1535 * Tune the memory allocator for a new MTU size.
1536 */
1537void skb_add_mtu(int mtu)
1538{
1539 /* Must match allocation in alloc_skb */
1540 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1541
1542 kmem_add_cache_size(mtu);
1543}
1544#endif
1545
1546static inline void skb_split_inside_header(struct sk_buff *skb,
1547 struct sk_buff* skb1,
1548 const u32 len, const int pos)
1549{
1550 int i;
1551
1552 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1553
1554 /* And move data appendix as is. */
1555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1556 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1557
1558 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1559 skb_shinfo(skb)->nr_frags = 0;
1560 skb1->data_len = skb->data_len;
1561 skb1->len += skb1->data_len;
1562 skb->data_len = 0;
1563 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001564 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567static inline void skb_split_no_header(struct sk_buff *skb,
1568 struct sk_buff* skb1,
1569 const u32 len, int pos)
1570{
1571 int i, k = 0;
1572 const int nfrags = skb_shinfo(skb)->nr_frags;
1573
1574 skb_shinfo(skb)->nr_frags = 0;
1575 skb1->len = skb1->data_len = skb->len - len;
1576 skb->len = len;
1577 skb->data_len = len - pos;
1578
1579 for (i = 0; i < nfrags; i++) {
1580 int size = skb_shinfo(skb)->frags[i].size;
1581
1582 if (pos + size > len) {
1583 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1584
1585 if (pos < len) {
1586 /* Split frag.
1587 * We have two variants in this case:
1588 * 1. Move all the frag to the second
1589 * part, if it is possible. F.e.
1590 * this approach is mandatory for TUX,
1591 * where splitting is expensive.
1592 * 2. Split is accurately. We make this.
1593 */
1594 get_page(skb_shinfo(skb)->frags[i].page);
1595 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1596 skb_shinfo(skb1)->frags[0].size -= len - pos;
1597 skb_shinfo(skb)->frags[i].size = len - pos;
1598 skb_shinfo(skb)->nr_frags++;
1599 }
1600 k++;
1601 } else
1602 skb_shinfo(skb)->nr_frags++;
1603 pos += size;
1604 }
1605 skb_shinfo(skb1)->nr_frags = k;
1606}
1607
1608/**
1609 * skb_split - Split fragmented skb to two parts at length len.
1610 * @skb: the buffer to split
1611 * @skb1: the buffer to receive the second part
1612 * @len: new length for skb
1613 */
1614void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1615{
1616 int pos = skb_headlen(skb);
1617
1618 if (len < pos) /* Split line is inside header. */
1619 skb_split_inside_header(skb, skb1, len, pos);
1620 else /* Second chunk has no header, nothing to copy. */
1621 skb_split_no_header(skb, skb1, len, pos);
1622}
1623
Thomas Graf677e90e2005-06-23 20:59:51 -07001624/**
1625 * skb_prepare_seq_read - Prepare a sequential read of skb data
1626 * @skb: the buffer to read
1627 * @from: lower offset of data to be read
1628 * @to: upper offset of data to be read
1629 * @st: state variable
1630 *
1631 * Initializes the specified state variable. Must be called before
1632 * invoking skb_seq_read() for the first time.
1633 */
1634void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1635 unsigned int to, struct skb_seq_state *st)
1636{
1637 st->lower_offset = from;
1638 st->upper_offset = to;
1639 st->root_skb = st->cur_skb = skb;
1640 st->frag_idx = st->stepped_offset = 0;
1641 st->frag_data = NULL;
1642}
1643
1644/**
1645 * skb_seq_read - Sequentially read skb data
1646 * @consumed: number of bytes consumed by the caller so far
1647 * @data: destination pointer for data to be returned
1648 * @st: state variable
1649 *
1650 * Reads a block of skb data at &consumed relative to the
1651 * lower offset specified to skb_prepare_seq_read(). Assigns
1652 * the head of the data block to &data and returns the length
1653 * of the block or 0 if the end of the skb data or the upper
1654 * offset has been reached.
1655 *
1656 * The caller is not required to consume all of the data
1657 * returned, i.e. &consumed is typically set to the number
1658 * of bytes already consumed and the next call to
1659 * skb_seq_read() will return the remaining part of the block.
1660 *
1661 * Note: The size of each block of data returned can be arbitary,
1662 * this limitation is the cost for zerocopy seqeuental
1663 * reads of potentially non linear data.
1664 *
1665 * Note: Fragment lists within fragments are not implemented
1666 * at the moment, state->root_skb could be replaced with
1667 * a stack for this purpose.
1668 */
1669unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1670 struct skb_seq_state *st)
1671{
1672 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1673 skb_frag_t *frag;
1674
1675 if (unlikely(abs_offset >= st->upper_offset))
1676 return 0;
1677
1678next_skb:
1679 block_limit = skb_headlen(st->cur_skb);
1680
1681 if (abs_offset < block_limit) {
1682 *data = st->cur_skb->data + abs_offset;
1683 return block_limit - abs_offset;
1684 }
1685
1686 if (st->frag_idx == 0 && !st->frag_data)
1687 st->stepped_offset += skb_headlen(st->cur_skb);
1688
1689 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1690 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1691 block_limit = frag->size + st->stepped_offset;
1692
1693 if (abs_offset < block_limit) {
1694 if (!st->frag_data)
1695 st->frag_data = kmap_skb_frag(frag);
1696
1697 *data = (u8 *) st->frag_data + frag->page_offset +
1698 (abs_offset - st->stepped_offset);
1699
1700 return block_limit - abs_offset;
1701 }
1702
1703 if (st->frag_data) {
1704 kunmap_skb_frag(st->frag_data);
1705 st->frag_data = NULL;
1706 }
1707
1708 st->frag_idx++;
1709 st->stepped_offset += frag->size;
1710 }
1711
1712 if (st->cur_skb->next) {
1713 st->cur_skb = st->cur_skb->next;
1714 st->frag_idx = 0;
1715 goto next_skb;
1716 } else if (st->root_skb == st->cur_skb &&
1717 skb_shinfo(st->root_skb)->frag_list) {
1718 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1719 goto next_skb;
1720 }
1721
1722 return 0;
1723}
1724
1725/**
1726 * skb_abort_seq_read - Abort a sequential read of skb data
1727 * @st: state variable
1728 *
1729 * Must be called if skb_seq_read() was not called until it
1730 * returned 0.
1731 */
1732void skb_abort_seq_read(struct skb_seq_state *st)
1733{
1734 if (st->frag_data)
1735 kunmap_skb_frag(st->frag_data);
1736}
1737
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001738#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1739
1740static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1741 struct ts_config *conf,
1742 struct ts_state *state)
1743{
1744 return skb_seq_read(offset, text, TS_SKB_CB(state));
1745}
1746
1747static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1748{
1749 skb_abort_seq_read(TS_SKB_CB(state));
1750}
1751
1752/**
1753 * skb_find_text - Find a text pattern in skb data
1754 * @skb: the buffer to look in
1755 * @from: search offset
1756 * @to: search limit
1757 * @config: textsearch configuration
1758 * @state: uninitialized textsearch state variable
1759 *
1760 * Finds a pattern in the skb data according to the specified
1761 * textsearch configuration. Use textsearch_next() to retrieve
1762 * subsequent occurrences of the pattern. Returns the offset
1763 * to the first occurrence or UINT_MAX if no match was found.
1764 */
1765unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1766 unsigned int to, struct ts_config *config,
1767 struct ts_state *state)
1768{
Phil Oesterf72b9482006-06-26 00:00:57 -07001769 unsigned int ret;
1770
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001771 config->get_next_block = skb_ts_get_next_block;
1772 config->finish = skb_ts_finish;
1773
1774 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1775
Phil Oesterf72b9482006-06-26 00:00:57 -07001776 ret = textsearch_find(config, state);
1777 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07001778}
1779
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001780/**
1781 * skb_append_datato_frags: - append the user data to a skb
1782 * @sk: sock structure
1783 * @skb: skb structure to be appened with user data.
1784 * @getfrag: call back function to be used for getting the user data
1785 * @from: pointer to user message iov
1786 * @length: length of the iov message
1787 *
1788 * Description: This procedure append the user data in the fragment part
1789 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1790 */
1791int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08001792 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001793 int len, int odd, struct sk_buff *skb),
1794 void *from, int length)
1795{
1796 int frg_cnt = 0;
1797 skb_frag_t *frag = NULL;
1798 struct page *page = NULL;
1799 int copy, left;
1800 int offset = 0;
1801 int ret;
1802
1803 do {
1804 /* Return error if we don't have space for new frag */
1805 frg_cnt = skb_shinfo(skb)->nr_frags;
1806 if (frg_cnt >= MAX_SKB_FRAGS)
1807 return -EFAULT;
1808
1809 /* allocate a new page for next frag */
1810 page = alloc_pages(sk->sk_allocation, 0);
1811
1812 /* If alloc_page fails just return failure and caller will
1813 * free previous allocated pages by doing kfree_skb()
1814 */
1815 if (page == NULL)
1816 return -ENOMEM;
1817
1818 /* initialize the next frag */
1819 sk->sk_sndmsg_page = page;
1820 sk->sk_sndmsg_off = 0;
1821 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1822 skb->truesize += PAGE_SIZE;
1823 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1824
1825 /* get the new initialized frag */
1826 frg_cnt = skb_shinfo(skb)->nr_frags;
1827 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1828
1829 /* copy the user data to page */
1830 left = PAGE_SIZE - frag->page_offset;
1831 copy = (length > left)? left : length;
1832
1833 ret = getfrag(from, (page_address(frag->page) +
1834 frag->page_offset + frag->size),
1835 offset, copy, 0, skb);
1836 if (ret < 0)
1837 return -EFAULT;
1838
1839 /* copy was successful so update the size parameters */
1840 sk->sk_sndmsg_off += copy;
1841 frag->size += copy;
1842 skb->len += copy;
1843 skb->data_len += copy;
1844 offset += copy;
1845 length -= copy;
1846
1847 } while (length > 0);
1848
1849 return 0;
1850}
1851
Herbert Xucbb042f2006-03-20 22:43:56 -08001852/**
1853 * skb_pull_rcsum - pull skb and update receive checksum
1854 * @skb: buffer to update
1855 * @start: start of data before pull
1856 * @len: length of data pulled
1857 *
1858 * This function performs an skb_pull on the packet and updates
Patrick McHardy84fa7932006-08-29 16:44:56 -07001859 * update the CHECKSUM_COMPLETE checksum. It should be used on
1860 * receive path processing instead of skb_pull unless you know
1861 * that the checksum difference is zero (e.g., a valid IP header)
1862 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08001863 */
1864unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1865{
1866 BUG_ON(len > skb->len);
1867 skb->len -= len;
1868 BUG_ON(skb->len < skb->data_len);
1869 skb_postpull_rcsum(skb, skb->data, len);
1870 return skb->data += len;
1871}
1872
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08001873EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1874
Herbert Xuf4c50d92006-06-22 03:02:40 -07001875/**
1876 * skb_segment - Perform protocol segmentation on skb.
1877 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07001878 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001879 *
1880 * This function performs segmentation on the given skb. It returns
1881 * the segment at the given position. It returns NULL if there are
1882 * no more segments to generate, or when an error is encountered.
1883 */
Herbert Xu576a30e2006-06-27 13:22:38 -07001884struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07001885{
1886 struct sk_buff *segs = NULL;
1887 struct sk_buff *tail = NULL;
1888 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001889 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001890 unsigned int offset = doffset;
1891 unsigned int headroom;
1892 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07001893 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001894 int nfrags = skb_shinfo(skb)->nr_frags;
1895 int err = -ENOMEM;
1896 int i = 0;
1897 int pos;
1898
1899 __skb_push(skb, doffset);
1900 headroom = skb_headroom(skb);
1901 pos = skb_headlen(skb);
1902
1903 do {
1904 struct sk_buff *nskb;
1905 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001906 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001907 int k;
1908 int size;
1909
1910 len = skb->len - offset;
1911 if (len > mss)
1912 len = mss;
1913
1914 hsize = skb_headlen(skb) - offset;
1915 if (hsize < 0)
1916 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08001917 if (hsize > len || !sg)
1918 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001919
Herbert Xuc8884ed2006-10-29 15:59:41 -08001920 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001921 if (unlikely(!nskb))
1922 goto err;
1923
1924 if (segs)
1925 tail->next = nskb;
1926 else
1927 segs = nskb;
1928 tail = nskb;
1929
1930 nskb->dev = skb->dev;
1931 nskb->priority = skb->priority;
1932 nskb->protocol = skb->protocol;
1933 nskb->dst = dst_clone(skb->dst);
1934 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1935 nskb->pkt_type = skb->pkt_type;
1936 nskb->mac_len = skb->mac_len;
1937
1938 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001939 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03001940 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001941 nskb->transport_header = (nskb->network_header +
1942 skb_network_header_len(skb));
Herbert Xuf4c50d92006-06-22 03:02:40 -07001943 memcpy(skb_put(nskb, doffset), skb->data, doffset);
1944
1945 if (!sg) {
1946 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1947 skb_put(nskb, len),
1948 len, 0);
1949 continue;
1950 }
1951
1952 frag = skb_shinfo(nskb)->frags;
1953 k = 0;
1954
Patrick McHardy84fa7932006-08-29 16:44:56 -07001955 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07001956 nskb->csum = skb->csum;
1957 memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1958
1959 while (pos < offset + len) {
1960 BUG_ON(i >= nfrags);
1961
1962 *frag = skb_shinfo(skb)->frags[i];
1963 get_page(frag->page);
1964 size = frag->size;
1965
1966 if (pos < offset) {
1967 frag->page_offset += offset - pos;
1968 frag->size -= offset - pos;
1969 }
1970
1971 k++;
1972
1973 if (pos + size <= offset + len) {
1974 i++;
1975 pos += size;
1976 } else {
1977 frag->size -= pos + size - (offset + len);
1978 break;
1979 }
1980
1981 frag++;
1982 }
1983
1984 skb_shinfo(nskb)->nr_frags = k;
1985 nskb->data_len = len - hsize;
1986 nskb->len += nskb->data_len;
1987 nskb->truesize += nskb->data_len;
1988 } while ((offset += len) < skb->len);
1989
1990 return segs;
1991
1992err:
1993 while ((skb = segs)) {
1994 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08001995 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07001996 }
1997 return ERR_PTR(err);
1998}
1999
2000EXPORT_SYMBOL_GPL(skb_segment);
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002void __init skb_init(void)
2003{
2004 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2005 sizeof(struct sk_buff),
2006 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002007 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 NULL, NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002009 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2010 (2*sizeof(struct sk_buff)) +
2011 sizeof(atomic_t),
2012 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002013 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
David S. Millerd179cd12005-08-17 14:57:30 -07002014 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017EXPORT_SYMBOL(___pskb_trim);
2018EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002019EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002021EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002022EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023EXPORT_SYMBOL(pskb_copy);
2024EXPORT_SYMBOL(pskb_expand_head);
2025EXPORT_SYMBOL(skb_checksum);
2026EXPORT_SYMBOL(skb_clone);
2027EXPORT_SYMBOL(skb_clone_fraglist);
2028EXPORT_SYMBOL(skb_copy);
2029EXPORT_SYMBOL(skb_copy_and_csum_bits);
2030EXPORT_SYMBOL(skb_copy_and_csum_dev);
2031EXPORT_SYMBOL(skb_copy_bits);
2032EXPORT_SYMBOL(skb_copy_expand);
2033EXPORT_SYMBOL(skb_over_panic);
2034EXPORT_SYMBOL(skb_pad);
2035EXPORT_SYMBOL(skb_realloc_headroom);
2036EXPORT_SYMBOL(skb_under_panic);
2037EXPORT_SYMBOL(skb_dequeue);
2038EXPORT_SYMBOL(skb_dequeue_tail);
2039EXPORT_SYMBOL(skb_insert);
2040EXPORT_SYMBOL(skb_queue_purge);
2041EXPORT_SYMBOL(skb_queue_head);
2042EXPORT_SYMBOL(skb_queue_tail);
2043EXPORT_SYMBOL(skb_unlink);
2044EXPORT_SYMBOL(skb_append);
2045EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002046EXPORT_SYMBOL(skb_prepare_seq_read);
2047EXPORT_SYMBOL(skb_seq_read);
2048EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002049EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002050EXPORT_SYMBOL(skb_append_datato_frags);