blob: e4115672b6cfb1094de109c3fd56db796fc3741b [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
22 *
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
35/*
36 * The functions in this file will not compile correctly with gcc 2.4.x
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
40#include <linux/types.h>
41#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/mm.h>
43#include <linux/interrupt.h>
44#include <linux/in.h>
45#include <linux/inet.h>
46#include <linux/slab.h>
47#include <linux/netdevice.h>
48#ifdef CONFIG_NET_CLS_ACT
49#include <net/pkt_sched.h>
50#endif
51#include <linux/string.h>
52#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080053#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/cache.h>
55#include <linux/rtnetlink.h>
56#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070057#include <linux/scatterlist.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
Jens Axboe9c55e012007-11-06 23:30:13 -080073static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
74 struct pipe_buffer *buf)
75{
76 struct sk_buff *skb = (struct sk_buff *) buf->private;
77
78 kfree_skb(skb);
79}
80
81static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
83{
84 struct sk_buff *skb = (struct sk_buff *) buf->private;
85
86 skb_get(skb);
87}
88
89static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
91{
92 return 1;
93}
94
95
96/* Pipe buffer operations for a socket. */
97static struct pipe_buf_operations sock_pipe_buf_ops = {
98 .can_merge = 0,
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
111 */
112
113/**
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
118 *
119 * Out of line support code for skb_put(). Not user callable.
120 */
121void skb_over_panic(struct sk_buff *skb, int sz, void *here)
122{
Patrick McHardy26095452005-04-21 16:43:02 -0700123 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700124 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700125 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700126 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700127 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 BUG();
129}
130
131/**
132 * skb_under_panic - private function
133 * @skb: buffer
134 * @sz: size
135 * @here: address
136 *
137 * Out of line support code for skb_push(). Not user callable.
138 */
139
140void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141{
Patrick McHardy26095452005-04-21 16:43:02 -0700142 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700143 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700144 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700145 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700146 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 BUG();
148}
149
David S. Millerdc6de332006-04-20 00:10:50 -0700150void skb_truesize_bug(struct sk_buff *skb)
151{
152 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
153 "len=%u, sizeof(sk_buff)=%Zd\n",
154 skb->truesize, skb->len, sizeof(struct sk_buff));
155}
156EXPORT_SYMBOL(skb_truesize_bug);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Allocate a new skbuff. We do this ourselves so we can fill in a few
159 * 'private' fields and also do memory statistics to find all the
160 * [BEEP] leaks.
161 *
162 */
163
164/**
David S. Millerd179cd12005-08-17 14:57:30 -0700165 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @size: size to allocate
167 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700168 * @fclone: allocate from fclone cache instead of head cache
169 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800170 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Allocate a new &sk_buff. The returned buffer has no headroom and a
173 * tail room of size bytes. The object has a reference count of one.
174 * The return is the buffer. On a failure the return is %NULL.
175 *
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 * %GFP_ATOMIC.
178 */
Al Virodd0fc662005-10-07 07:46:04 +0100179struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800180 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Christoph Lametere18b8902006-12-06 20:33:20 -0800182 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800183 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct sk_buff *skb;
185 u8 *data;
186
Herbert Xu8798b3f2006-01-23 16:32:45 -0800187 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800190 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!skb)
192 goto out;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800195 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
196 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!data)
198 goto nodata;
199
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300200 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700201 * Only clear those fields we need to clear, not those that we will
202 * actually initialise below. Hence, don't put any more fields after
203 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300204 */
205 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb->truesize = size + sizeof(struct sk_buff);
207 atomic_set(&skb->users, 1);
208 skb->head = data;
209 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700210 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700211 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 /* make sure we initialize shinfo sequentially */
213 shinfo = skb_shinfo(skb);
214 atomic_set(&shinfo->dataref, 1);
215 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700216 shinfo->gso_size = 0;
217 shinfo->gso_segs = 0;
218 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800219 shinfo->ip6_frag_id = 0;
220 shinfo->frag_list = NULL;
221
David S. Millerd179cd12005-08-17 14:57:30 -0700222 if (fclone) {
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David S. Millerd179cd12005-08-17 14:57:30 -0700226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
228
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231out:
232 return skb;
233nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800234 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 skb = NULL;
236 goto out;
237}
238
239/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
244 *
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
249 *
250 * %NULL is returned if there is no free memory.
251 */
252struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700256 struct sk_buff *skb;
257
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700259 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700260 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700261 skb->dev = dev;
262 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700263 return skb;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700266/**
267 * dev_alloc_skb - allocate an skbuff for receiving
268 * @length: length to allocate
269 *
270 * Allocate a new &sk_buff and assign it a usage count of one. The
271 * buffer has unspecified headroom built in. Users should allocate
272 * the headroom they think they need without accounting for the
273 * built in space. The built in space is used for optimisations.
274 *
275 * %NULL is returned if there is no free memory. Although this function
276 * allocates memory it can be called from an interrupt.
277 */
278struct sk_buff *dev_alloc_skb(unsigned int length)
279{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700280 /*
281 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700282 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700283 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700284 return __dev_alloc_skb(length, GFP_ATOMIC);
285}
286EXPORT_SYMBOL(dev_alloc_skb);
287
Herbert Xu27b437c2006-07-13 19:26:39 -0700288static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Herbert Xu27b437c2006-07-13 19:26:39 -0700290 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Herbert Xu27b437c2006-07-13 19:26:39 -0700292 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 do {
295 struct sk_buff *this = list;
296 list = list->next;
297 kfree_skb(this);
298 } while (list);
299}
300
Herbert Xu27b437c2006-07-13 19:26:39 -0700301static inline void skb_drop_fraglist(struct sk_buff *skb)
302{
303 skb_drop_list(&skb_shinfo(skb)->frag_list);
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void skb_clone_fraglist(struct sk_buff *skb)
307{
308 struct sk_buff *list;
309
310 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
311 skb_get(list);
312}
313
Adrian Bunk5bba1712006-06-29 13:02:35 -0700314static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 if (!skb->cloned ||
317 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
318 &skb_shinfo(skb)->dataref)) {
319 if (skb_shinfo(skb)->nr_frags) {
320 int i;
321 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
322 put_page(skb_shinfo(skb)->frags[i].page);
323 }
324
325 if (skb_shinfo(skb)->frag_list)
326 skb_drop_fraglist(skb);
327
328 kfree(skb->head);
329 }
330}
331
332/*
333 * Free an skbuff by memory without cleaning the state.
334 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800335static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
David S. Millerd179cd12005-08-17 14:57:30 -0700337 struct sk_buff *other;
338 atomic_t *fclone_ref;
339
David S. Millerd179cd12005-08-17 14:57:30 -0700340 switch (skb->fclone) {
341 case SKB_FCLONE_UNAVAILABLE:
342 kmem_cache_free(skbuff_head_cache, skb);
343 break;
344
345 case SKB_FCLONE_ORIG:
346 fclone_ref = (atomic_t *) (skb + 2);
347 if (atomic_dec_and_test(fclone_ref))
348 kmem_cache_free(skbuff_fclone_cache, skb);
349 break;
350
351 case SKB_FCLONE_CLONE:
352 fclone_ref = (atomic_t *) (skb + 1);
353 other = skb - 1;
354
355 /* The clone portion is available for
356 * fast-cloning again.
357 */
358 skb->fclone = SKB_FCLONE_UNAVAILABLE;
359
360 if (atomic_dec_and_test(fclone_ref))
361 kmem_cache_free(skbuff_fclone_cache, other);
362 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Herbert Xu2d4baff2007-11-26 23:11:19 +0800366/* Free everything but the sk_buff shell. */
367static void skb_release_all(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dst_release(skb->dst);
370#ifdef CONFIG_XFRM
371 secpath_put(skb->sp);
372#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700373 if (skb->destructor) {
374 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 skb->destructor(skb);
376 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800377#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700378 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800379 nf_conntrack_put_reasm(skb->nfct_reasm);
380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#ifdef CONFIG_BRIDGE_NETFILTER
382 nf_bridge_put(skb->nf_bridge);
383#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* XXX: IS this still necessary? - JHS */
385#ifdef CONFIG_NET_SCHED
386 skb->tc_index = 0;
387#ifdef CONFIG_NET_CLS_ACT
388 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#endif
390#endif
Herbert Xu2d4baff2007-11-26 23:11:19 +0800391 skb_release_data(skb);
392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Herbert Xu2d4baff2007-11-26 23:11:19 +0800394/**
395 * __kfree_skb - private function
396 * @skb: buffer
397 *
398 * Free an sk_buff. Release anything attached to the buffer.
399 * Clean the state. This is an internal helper function. Users should
400 * always call kfree_skb
401 */
402
403void __kfree_skb(struct sk_buff *skb)
404{
405 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 kfree_skbmem(skb);
407}
408
409/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800410 * kfree_skb - free an sk_buff
411 * @skb: buffer to free
412 *
413 * Drop a reference to the buffer and free it if the usage count has
414 * hit zero.
415 */
416void kfree_skb(struct sk_buff *skb)
417{
418 if (unlikely(!skb))
419 return;
420 if (likely(atomic_read(&skb->users) == 1))
421 smp_rmb();
422 else if (likely(!atomic_dec_and_test(&skb->users)))
423 return;
424 __kfree_skb(skb);
425}
426
Herbert Xudec18812007-10-14 00:37:30 -0700427static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
428{
429 new->tstamp = old->tstamp;
430 new->dev = old->dev;
431 new->transport_header = old->transport_header;
432 new->network_header = old->network_header;
433 new->mac_header = old->mac_header;
434 new->dst = dst_clone(old->dst);
435#ifdef CONFIG_INET
436 new->sp = secpath_get(old->sp);
437#endif
438 memcpy(new->cb, old->cb, sizeof(old->cb));
439 new->csum_start = old->csum_start;
440 new->csum_offset = old->csum_offset;
441 new->local_df = old->local_df;
442 new->pkt_type = old->pkt_type;
443 new->ip_summed = old->ip_summed;
444 skb_copy_queue_mapping(new, old);
445 new->priority = old->priority;
446#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447 new->ipvs_property = old->ipvs_property;
448#endif
449 new->protocol = old->protocol;
450 new->mark = old->mark;
451 __nf_copy(new, old);
452#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
453 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
454 new->nf_trace = old->nf_trace;
455#endif
456#ifdef CONFIG_NET_SCHED
457 new->tc_index = old->tc_index;
458#ifdef CONFIG_NET_CLS_ACT
459 new->tc_verd = old->tc_verd;
460#endif
461#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700462 new->vlan_tci = old->vlan_tci;
463
Herbert Xudec18812007-10-14 00:37:30 -0700464 skb_copy_secmark(new, old);
465}
466
Herbert Xue0053ec2007-10-14 00:37:52 -0700467static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#define C(x) n->x = skb->x
470
471 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700473 __copy_skb_header(n, skb);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 C(len);
476 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700477 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700478 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800479 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800482 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 C(tail);
484 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800485 C(head);
486 C(data);
487 C(truesize);
488 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 atomic_inc(&(skb_shinfo(skb)->dataref));
491 skb->cloned = 1;
492
493 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700494#undef C
495}
496
497/**
498 * skb_morph - morph one skb into another
499 * @dst: the skb to receive the contents
500 * @src: the skb to supply the contents
501 *
502 * This is identical to skb_clone except that the target skb is
503 * supplied by the user.
504 *
505 * The target skb is returned upon exit.
506 */
507struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
508{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800509 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700510 return __skb_clone(dst, src);
511}
512EXPORT_SYMBOL_GPL(skb_morph);
513
514/**
515 * skb_clone - duplicate an sk_buff
516 * @skb: buffer to clone
517 * @gfp_mask: allocation priority
518 *
519 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
520 * copies share the same packet data but not structure. The new
521 * buffer has a reference count of 1. If the allocation fails the
522 * function returns %NULL otherwise the new buffer is returned.
523 *
524 * If this function is called from an interrupt gfp_mask() must be
525 * %GFP_ATOMIC.
526 */
527
528struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
529{
530 struct sk_buff *n;
531
532 n = skb + 1;
533 if (skb->fclone == SKB_FCLONE_ORIG &&
534 n->fclone == SKB_FCLONE_UNAVAILABLE) {
535 atomic_t *fclone_ref = (atomic_t *) (n + 1);
536 n->fclone = SKB_FCLONE_CLONE;
537 atomic_inc(fclone_ref);
538 } else {
539 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
540 if (!n)
541 return NULL;
542 n->fclone = SKB_FCLONE_UNAVAILABLE;
543 }
544
545 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
549{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700550#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Shift between the two data areas in bytes
553 */
554 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700555#endif
Herbert Xudec18812007-10-14 00:37:30 -0700556
557 __copy_skb_header(new, old);
558
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700559#ifndef NET_SKBUFF_DATA_USES_OFFSET
560 /* {transport,network,mac}_header are relative to skb->head */
561 new->transport_header += offset;
562 new->network_header += offset;
563 new->mac_header += offset;
564#endif
Herbert Xu79671682006-06-22 02:40:14 -0700565 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
566 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
567 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570/**
571 * skb_copy - create private copy of an sk_buff
572 * @skb: buffer to copy
573 * @gfp_mask: allocation priority
574 *
575 * Make a copy of both an &sk_buff and its data. This is used when the
576 * caller wishes to modify the data and needs a private copy of the
577 * data to alter. Returns %NULL on failure or the pointer to the buffer
578 * on success. The returned buffer has a reference count of 1.
579 *
580 * As by-product this function converts non-linear &sk_buff to linear
581 * one, so that &sk_buff becomes completely private and caller is allowed
582 * to modify all the data of returned buffer. This means that this
583 * function is not recommended for use in circumstances when only
584 * header is going to be modified. Use pskb_copy() instead.
585 */
586
Al Virodd0fc662005-10-07 07:46:04 +0100587struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 int headerlen = skb->data - skb->head;
590 /*
591 * Allocate the copy buffer
592 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700593 struct sk_buff *n;
594#ifdef NET_SKBUFF_DATA_USES_OFFSET
595 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
596#else
597 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (!n)
600 return NULL;
601
602 /* Set the data pointer */
603 skb_reserve(n, headerlen);
604 /* Set the tail pointer and length */
605 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
608 BUG();
609
610 copy_skb_header(n, skb);
611 return n;
612}
613
614
615/**
616 * pskb_copy - create copy of an sk_buff with private head.
617 * @skb: buffer to copy
618 * @gfp_mask: allocation priority
619 *
620 * Make a copy of both an &sk_buff and part of its data, located
621 * in header. Fragmented data remain shared. This is used when
622 * the caller wishes to modify only header of &sk_buff and needs
623 * private copy of the header to alter. Returns %NULL on failure
624 * or the pointer to the buffer on success.
625 * The returned buffer has a reference count of 1.
626 */
627
Al Virodd0fc662005-10-07 07:46:04 +0100628struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 /*
631 * Allocate the copy buffer
632 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700633 struct sk_buff *n;
634#ifdef NET_SKBUFF_DATA_USES_OFFSET
635 n = alloc_skb(skb->end, gfp_mask);
636#else
637 n = alloc_skb(skb->end - skb->head, gfp_mask);
638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (!n)
640 goto out;
641
642 /* Set the data pointer */
643 skb_reserve(n, skb->data - skb->head);
644 /* Set the tail pointer and length */
645 skb_put(n, skb_headlen(skb));
646 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300647 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Herbert Xu25f484a2006-11-07 14:57:15 -0800649 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 n->data_len = skb->data_len;
651 n->len = skb->len;
652
653 if (skb_shinfo(skb)->nr_frags) {
654 int i;
655
656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
657 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
658 get_page(skb_shinfo(n)->frags[i].page);
659 }
660 skb_shinfo(n)->nr_frags = i;
661 }
662
663 if (skb_shinfo(skb)->frag_list) {
664 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
665 skb_clone_fraglist(n);
666 }
667
668 copy_skb_header(n, skb);
669out:
670 return n;
671}
672
673/**
674 * pskb_expand_head - reallocate header of &sk_buff
675 * @skb: buffer to reallocate
676 * @nhead: room to add at head
677 * @ntail: room to add at tail
678 * @gfp_mask: allocation priority
679 *
680 * Expands (or creates identical copy, if &nhead and &ntail are zero)
681 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
682 * reference count of 1. Returns zero in the case of success or error,
683 * if expansion failed. In the last case, &sk_buff is not changed.
684 *
685 * All the pointers pointing into skb header may change and must be
686 * reloaded after call to this function.
687 */
688
Victor Fusco86a76ca2005-07-08 14:57:47 -0700689int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100690 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int i;
693 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700694#ifdef NET_SKBUFF_DATA_USES_OFFSET
695 int size = nhead + skb->end + ntail;
696#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 long off;
700
701 if (skb_shared(skb))
702 BUG();
703
704 size = SKB_DATA_ALIGN(size);
705
706 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
707 if (!data)
708 goto nodata;
709
710 /* Copy only real data... and, alas, header. This should be
711 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700712#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700713 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700714#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700715 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700716#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700717 memcpy(data + size, skb_end_pointer(skb),
718 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
721 get_page(skb_shinfo(skb)->frags[i].page);
722
723 if (skb_shinfo(skb)->frag_list)
724 skb_clone_fraglist(skb);
725
726 skb_release_data(skb);
727
728 off = (data + nhead) - skb->head;
729
730 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700732#ifdef NET_SKBUFF_DATA_USES_OFFSET
733 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700734 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700735#else
736 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700737#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700738 /* {transport,network,mac}_header and tail are relative to skb->head */
739 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700740 skb->transport_header += off;
741 skb->network_header += off;
742 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700743 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700745 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 skb->nohdr = 0;
747 atomic_set(&skb_shinfo(skb)->dataref, 1);
748 return 0;
749
750nodata:
751 return -ENOMEM;
752}
753
754/* Make private copy of skb with writable head and some headroom */
755
756struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
757{
758 struct sk_buff *skb2;
759 int delta = headroom - skb_headroom(skb);
760
761 if (delta <= 0)
762 skb2 = pskb_copy(skb, GFP_ATOMIC);
763 else {
764 skb2 = skb_clone(skb, GFP_ATOMIC);
765 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
766 GFP_ATOMIC)) {
767 kfree_skb(skb2);
768 skb2 = NULL;
769 }
770 }
771 return skb2;
772}
773
774
775/**
776 * skb_copy_expand - copy and expand sk_buff
777 * @skb: buffer to copy
778 * @newheadroom: new free bytes at head
779 * @newtailroom: new free bytes at tail
780 * @gfp_mask: allocation priority
781 *
782 * Make a copy of both an &sk_buff and its data and while doing so
783 * allocate additional space.
784 *
785 * This is used when the caller wishes to modify the data and needs a
786 * private copy of the data to alter as well as more space for new fields.
787 * Returns %NULL on failure or the pointer to the buffer
788 * on success. The returned buffer has a reference count of 1.
789 *
790 * You must pass %GFP_ATOMIC as the allocation priority if this function
791 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
793struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700794 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100795 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 /*
798 * Allocate the copy buffer
799 */
800 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
801 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700802 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700804 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (!n)
807 return NULL;
808
809 skb_reserve(n, newheadroom);
810
811 /* Set the tail pointer and length */
812 skb_put(n, skb->len);
813
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700814 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 head_copy_off = 0;
816 if (newheadroom <= head_copy_len)
817 head_copy_len = newheadroom;
818 else
819 head_copy_off = newheadroom - head_copy_len;
820
821 /* Copy the linear header and data. */
822 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
823 skb->len + head_copy_len))
824 BUG();
825
826 copy_skb_header(n, skb);
827
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700828 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700829 n->csum_start += off;
830#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700831 n->transport_header += off;
832 n->network_header += off;
833 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700834#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return n;
837}
838
839/**
840 * skb_pad - zero pad the tail of an skb
841 * @skb: buffer to pad
842 * @pad: space to pad
843 *
844 * Ensure that a buffer is followed by a padding area that is zero
845 * filled. Used by network drivers which may DMA or transfer data
846 * beyond the buffer end onto the wire.
847 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700848 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900850
Herbert Xu5b057c62006-06-23 02:06:41 -0700851int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Herbert Xu5b057c62006-06-23 02:06:41 -0700853 int err;
854 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700857 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700861
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700862 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700863 if (likely(skb_cloned(skb) || ntail > 0)) {
864 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
865 if (unlikely(err))
866 goto free_skb;
867 }
868
869 /* FIXME: The use of this function with non-linear skb's really needs
870 * to be audited.
871 */
872 err = skb_linearize(skb);
873 if (unlikely(err))
874 goto free_skb;
875
876 memset(skb->data + skb->len, 0, pad);
877 return 0;
878
879free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700881 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900882}
883
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700884/**
885 * skb_put - add data to a buffer
886 * @skb: buffer to use
887 * @len: amount of data to add
888 *
889 * This function extends the used data area of the buffer. If this would
890 * exceed the total buffer size the kernel will panic. A pointer to the
891 * first byte of the extra data is returned.
892 */
893unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
894{
895 unsigned char *tmp = skb_tail_pointer(skb);
896 SKB_LINEAR_ASSERT(skb);
897 skb->tail += len;
898 skb->len += len;
899 if (unlikely(skb->tail > skb->end))
900 skb_over_panic(skb, len, __builtin_return_address(0));
901 return tmp;
902}
903EXPORT_SYMBOL(skb_put);
904
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700905/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -0700906 * skb_push - add data to the start of a buffer
907 * @skb: buffer to use
908 * @len: amount of data to add
909 *
910 * This function extends the used data area of the buffer at the buffer
911 * start. If this would exceed the total buffer headroom the kernel will
912 * panic. A pointer to the first byte of the extra data is returned.
913 */
914unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
915{
916 skb->data -= len;
917 skb->len += len;
918 if (unlikely(skb->data<skb->head))
919 skb_under_panic(skb, len, __builtin_return_address(0));
920 return skb->data;
921}
922EXPORT_SYMBOL(skb_push);
923
924/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700925 * skb_pull - remove data from the start of a buffer
926 * @skb: buffer to use
927 * @len: amount of data to remove
928 *
929 * This function removes data from the start of a buffer, returning
930 * the memory to the headroom. A pointer to the next data in the buffer
931 * is returned. Once the data has been pulled future pushes will overwrite
932 * the old data.
933 */
934unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
935{
936 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
937}
938EXPORT_SYMBOL(skb_pull);
939
Ilpo Järvinen419ae742008-03-27 17:54:01 -0700940/**
941 * skb_trim - remove end from a buffer
942 * @skb: buffer to alter
943 * @len: new length
944 *
945 * Cut the length of a buffer down by removing data from the tail. If
946 * the buffer is already under the length specified it is not modified.
947 * The skb must be linear.
948 */
949void skb_trim(struct sk_buff *skb, unsigned int len)
950{
951 if (skb->len > len)
952 __skb_trim(skb, len);
953}
954EXPORT_SYMBOL(skb_trim);
955
Herbert Xu3cc0e872006-06-09 16:13:38 -0700956/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958
Herbert Xu3cc0e872006-06-09 16:13:38 -0700959int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Herbert Xu27b437c2006-07-13 19:26:39 -0700961 struct sk_buff **fragp;
962 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 int offset = skb_headlen(skb);
964 int nfrags = skb_shinfo(skb)->nr_frags;
965 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700966 int err;
967
968 if (skb_cloned(skb) &&
969 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700972 i = 0;
973 if (offset >= len)
974 goto drop_pages;
975
976 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700978
979 if (end < len) {
980 offset = end;
981 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700983
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700984 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700985
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700986drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700987 skb_shinfo(skb)->nr_frags = i;
988
989 for (; i < nfrags; i++)
990 put_page(skb_shinfo(skb)->frags[i].page);
991
992 if (skb_shinfo(skb)->frag_list)
993 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700994 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Herbert Xu27b437c2006-07-13 19:26:39 -0700997 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
998 fragp = &frag->next) {
999 int end = offset + frag->len;
1000
1001 if (skb_shared(frag)) {
1002 struct sk_buff *nfrag;
1003
1004 nfrag = skb_clone(frag, GFP_ATOMIC);
1005 if (unlikely(!nfrag))
1006 return -ENOMEM;
1007
1008 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001009 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001010 frag = nfrag;
1011 *fragp = frag;
1012 }
1013
1014 if (end < len) {
1015 offset = end;
1016 continue;
1017 }
1018
1019 if (end > len &&
1020 unlikely((err = pskb_trim(frag, len - offset))))
1021 return err;
1022
1023 if (frag->next)
1024 skb_drop_list(&frag->next);
1025 break;
1026 }
1027
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001028done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001029 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 skb->data_len -= skb->len - len;
1031 skb->len = len;
1032 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001033 skb->len = len;
1034 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001035 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
1038 return 0;
1039}
1040
1041/**
1042 * __pskb_pull_tail - advance tail of skb header
1043 * @skb: buffer to reallocate
1044 * @delta: number of bytes to advance tail
1045 *
1046 * The function makes a sense only on a fragmented &sk_buff,
1047 * it expands header moving its tail forward and copying necessary
1048 * data from fragmented part.
1049 *
1050 * &sk_buff MUST have reference count of 1.
1051 *
1052 * Returns %NULL (and &sk_buff does not change) if pull failed
1053 * or value of new tail of skb in the case of success.
1054 *
1055 * All the pointers pointing into skb header may change and must be
1056 * reloaded after call to this function.
1057 */
1058
1059/* Moves tail of skb head forward, copying data from fragmented part,
1060 * when it is necessary.
1061 * 1. It may fail due to malloc failure.
1062 * 2. It may change skb pointers.
1063 *
1064 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1065 */
1066unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1067{
1068 /* If skb has not enough free space at tail, get new one
1069 * plus 128 bytes for future expansions. If we have enough
1070 * room at tail, reallocate without expansion only if skb is cloned.
1071 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001072 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (eat > 0 || skb_cloned(skb)) {
1075 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1076 GFP_ATOMIC))
1077 return NULL;
1078 }
1079
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001080 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 BUG();
1082
1083 /* Optimization: no fragments, no reasons to preestimate
1084 * size of pulled pages. Superb.
1085 */
1086 if (!skb_shinfo(skb)->frag_list)
1087 goto pull_pages;
1088
1089 /* Estimate size of pulled pages. */
1090 eat = delta;
1091 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1092 if (skb_shinfo(skb)->frags[i].size >= eat)
1093 goto pull_pages;
1094 eat -= skb_shinfo(skb)->frags[i].size;
1095 }
1096
1097 /* If we need update frag list, we are in troubles.
1098 * Certainly, it possible to add an offset to skb data,
1099 * but taking into account that pulling is expected to
1100 * be very rare operation, it is worth to fight against
1101 * further bloating skb head and crucify ourselves here instead.
1102 * Pure masohism, indeed. 8)8)
1103 */
1104 if (eat) {
1105 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1106 struct sk_buff *clone = NULL;
1107 struct sk_buff *insp = NULL;
1108
1109 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001110 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (list->len <= eat) {
1113 /* Eaten as whole. */
1114 eat -= list->len;
1115 list = list->next;
1116 insp = list;
1117 } else {
1118 /* Eaten partially. */
1119
1120 if (skb_shared(list)) {
1121 /* Sucks! We need to fork list. :-( */
1122 clone = skb_clone(list, GFP_ATOMIC);
1123 if (!clone)
1124 return NULL;
1125 insp = list->next;
1126 list = clone;
1127 } else {
1128 /* This may be pulled without
1129 * problems. */
1130 insp = list;
1131 }
1132 if (!pskb_pull(list, eat)) {
1133 if (clone)
1134 kfree_skb(clone);
1135 return NULL;
1136 }
1137 break;
1138 }
1139 } while (eat);
1140
1141 /* Free pulled out fragments. */
1142 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1143 skb_shinfo(skb)->frag_list = list->next;
1144 kfree_skb(list);
1145 }
1146 /* And insert new clone at head. */
1147 if (clone) {
1148 clone->next = list;
1149 skb_shinfo(skb)->frag_list = clone;
1150 }
1151 }
1152 /* Success! Now we may commit changes to skb data. */
1153
1154pull_pages:
1155 eat = delta;
1156 k = 0;
1157 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1158 if (skb_shinfo(skb)->frags[i].size <= eat) {
1159 put_page(skb_shinfo(skb)->frags[i].page);
1160 eat -= skb_shinfo(skb)->frags[i].size;
1161 } else {
1162 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1163 if (eat) {
1164 skb_shinfo(skb)->frags[k].page_offset += eat;
1165 skb_shinfo(skb)->frags[k].size -= eat;
1166 eat = 0;
1167 }
1168 k++;
1169 }
1170 }
1171 skb_shinfo(skb)->nr_frags = k;
1172
1173 skb->tail += delta;
1174 skb->data_len -= delta;
1175
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001176 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/* Copy some data bits from skb to kernel buffer. */
1180
1181int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1182{
1183 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001184 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (offset > (int)skb->len - len)
1187 goto fault;
1188
1189 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001190 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (copy > len)
1192 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001193 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if ((len -= copy) == 0)
1195 return 0;
1196 offset += copy;
1197 to += copy;
1198 }
1199
1200 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001201 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
David S. Miller1a028e52007-04-27 15:21:23 -07001203 BUG_TRAP(start <= offset + len);
1204
1205 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if ((copy = end - offset) > 0) {
1207 u8 *vaddr;
1208
1209 if (copy > len)
1210 copy = len;
1211
1212 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1213 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001214 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1215 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 kunmap_skb_frag(vaddr);
1217
1218 if ((len -= copy) == 0)
1219 return 0;
1220 offset += copy;
1221 to += copy;
1222 }
David S. Miller1a028e52007-04-27 15:21:23 -07001223 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
1226 if (skb_shinfo(skb)->frag_list) {
1227 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1228
1229 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001230 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
David S. Miller1a028e52007-04-27 15:21:23 -07001232 BUG_TRAP(start <= offset + len);
1233
1234 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if ((copy = end - offset) > 0) {
1236 if (copy > len)
1237 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001238 if (skb_copy_bits(list, offset - start,
1239 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 goto fault;
1241 if ((len -= copy) == 0)
1242 return 0;
1243 offset += copy;
1244 to += copy;
1245 }
David S. Miller1a028e52007-04-27 15:21:23 -07001246 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 }
1249 if (!len)
1250 return 0;
1251
1252fault:
1253 return -EFAULT;
1254}
1255
Jens Axboe9c55e012007-11-06 23:30:13 -08001256/*
1257 * Callback from splice_to_pipe(), if we need to release some pages
1258 * at the end of the spd in case we error'ed out in filling the pipe.
1259 */
1260static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1261{
1262 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1263
1264 kfree_skb(skb);
1265}
1266
1267/*
1268 * Fill page/offset/length into spd, if it can hold more pages.
1269 */
1270static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1271 unsigned int len, unsigned int offset,
1272 struct sk_buff *skb)
1273{
1274 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1275 return 1;
1276
1277 spd->pages[spd->nr_pages] = page;
1278 spd->partial[spd->nr_pages].len = len;
1279 spd->partial[spd->nr_pages].offset = offset;
1280 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1281 spd->nr_pages++;
1282 return 0;
1283}
1284
Octavian Purdila2870c432008-07-15 00:49:11 -07001285static inline void __segment_seek(struct page **page, unsigned int *poff,
1286 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001287{
Octavian Purdila2870c432008-07-15 00:49:11 -07001288 *poff += off;
1289 *page += *poff / PAGE_SIZE;
1290 *poff = *poff % PAGE_SIZE;
1291 *plen -= off;
1292}
Jens Axboe9c55e012007-11-06 23:30:13 -08001293
Octavian Purdila2870c432008-07-15 00:49:11 -07001294static inline int __splice_segment(struct page *page, unsigned int poff,
1295 unsigned int plen, unsigned int *off,
1296 unsigned int *len, struct sk_buff *skb,
1297 struct splice_pipe_desc *spd)
1298{
1299 if (!*len)
1300 return 1;
1301
1302 /* skip this segment if already processed */
1303 if (*off >= plen) {
1304 *off -= plen;
1305 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001306 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001307
Octavian Purdila2870c432008-07-15 00:49:11 -07001308 /* ignore any bits we already processed */
1309 if (*off) {
1310 __segment_seek(&page, &poff, &plen, *off);
1311 *off = 0;
1312 }
1313
1314 do {
1315 unsigned int flen = min(*len, plen);
1316
1317 /* the linear region may spread across several pages */
1318 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1319
1320 if (spd_fill_page(spd, page, flen, poff, skb))
1321 return 1;
1322
1323 __segment_seek(&page, &poff, &plen, flen);
1324 *len -= flen;
1325
1326 } while (*len && plen);
1327
1328 return 0;
1329}
1330
1331/*
1332 * Map linear and fragment data from the skb to spd. It reports failure if the
1333 * pipe is full or if we already spliced the requested length.
1334 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001335static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Octavian Purdila2870c432008-07-15 00:49:11 -07001336 unsigned int *len,
1337 struct splice_pipe_desc *spd)
1338{
1339 int seg;
1340
Jens Axboe9c55e012007-11-06 23:30:13 -08001341 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001342 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001343 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001344 if (__splice_segment(virt_to_page(skb->data),
1345 (unsigned long) skb->data & (PAGE_SIZE - 1),
1346 skb_headlen(skb),
1347 offset, len, skb, spd))
1348 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001349
1350 /*
1351 * then map the fragments
1352 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001353 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1354 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1355
Octavian Purdila2870c432008-07-15 00:49:11 -07001356 if (__splice_segment(f->page, f->page_offset, f->size,
1357 offset, len, skb, spd))
1358 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001359 }
1360
Octavian Purdila2870c432008-07-15 00:49:11 -07001361 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001362}
1363
1364/*
1365 * Map data from the skb to a pipe. Should handle both the linear part,
1366 * the fragments, and the frag list. It does NOT handle frag lists within
1367 * the frag list, if such a thing exists. We'd probably need to recurse to
1368 * handle that cleanly.
1369 */
1370int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1371 struct pipe_inode_info *pipe, unsigned int tlen,
1372 unsigned int flags)
1373{
1374 struct partial_page partial[PIPE_BUFFERS];
1375 struct page *pages[PIPE_BUFFERS];
1376 struct splice_pipe_desc spd = {
1377 .pages = pages,
1378 .partial = partial,
1379 .flags = flags,
1380 .ops = &sock_pipe_buf_ops,
1381 .spd_release = sock_spd_release,
1382 };
1383 struct sk_buff *skb;
1384
1385 /*
1386 * I'd love to avoid the clone here, but tcp_read_sock()
1387 * ignores reference counts and unconditonally kills the sk_buff
1388 * on return from the actor.
1389 */
1390 skb = skb_clone(__skb, GFP_KERNEL);
1391 if (unlikely(!skb))
1392 return -ENOMEM;
1393
1394 /*
1395 * __skb_splice_bits() only fails if the output has no room left,
1396 * so no point in going over the frag_list for the error case.
1397 */
1398 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1399 goto done;
1400 else if (!tlen)
1401 goto done;
1402
1403 /*
1404 * now see if we have a frag_list to map
1405 */
1406 if (skb_shinfo(skb)->frag_list) {
1407 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1408
1409 for (; list && tlen; list = list->next) {
1410 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1411 break;
1412 }
1413 }
1414
1415done:
1416 /*
1417 * drop our reference to the clone, the pipe consumption will
1418 * drop the rest.
1419 */
1420 kfree_skb(skb);
1421
1422 if (spd.nr_pages) {
1423 int ret;
Octavian Purdila293ad602008-06-04 15:45:58 -07001424 struct sock *sk = __skb->sk;
Jens Axboe9c55e012007-11-06 23:30:13 -08001425
1426 /*
1427 * Drop the socket lock, otherwise we have reverse
1428 * locking dependencies between sk_lock and i_mutex
1429 * here as compared to sendfile(). We enter here
1430 * with the socket lock held, and splice_to_pipe() will
1431 * grab the pipe inode lock. For sendfile() emulation,
1432 * we call into ->sendpage() with the i_mutex lock held
1433 * and networking will grab the socket lock.
1434 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001435 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001436 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001437 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001438 return ret;
1439 }
1440
1441 return 0;
1442}
1443
Herbert Xu357b40a2005-04-19 22:30:14 -07001444/**
1445 * skb_store_bits - store bits from kernel buffer to skb
1446 * @skb: destination buffer
1447 * @offset: offset in destination
1448 * @from: source buffer
1449 * @len: number of bytes to copy
1450 *
1451 * Copy the specified number of bytes from the source buffer to the
1452 * destination skb. This function handles all the messy bits of
1453 * traversing fragment lists and such.
1454 */
1455
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001456int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001457{
1458 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001459 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001460
1461 if (offset > (int)skb->len - len)
1462 goto fault;
1463
David S. Miller1a028e52007-04-27 15:21:23 -07001464 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001465 if (copy > len)
1466 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001467 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001468 if ((len -= copy) == 0)
1469 return 0;
1470 offset += copy;
1471 from += copy;
1472 }
1473
1474 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1475 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001476 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001477
David S. Miller1a028e52007-04-27 15:21:23 -07001478 BUG_TRAP(start <= offset + len);
1479
1480 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001481 if ((copy = end - offset) > 0) {
1482 u8 *vaddr;
1483
1484 if (copy > len)
1485 copy = len;
1486
1487 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001488 memcpy(vaddr + frag->page_offset + offset - start,
1489 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001490 kunmap_skb_frag(vaddr);
1491
1492 if ((len -= copy) == 0)
1493 return 0;
1494 offset += copy;
1495 from += copy;
1496 }
David S. Miller1a028e52007-04-27 15:21:23 -07001497 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001498 }
1499
1500 if (skb_shinfo(skb)->frag_list) {
1501 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1502
1503 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001504 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001505
David S. Miller1a028e52007-04-27 15:21:23 -07001506 BUG_TRAP(start <= offset + len);
1507
1508 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001509 if ((copy = end - offset) > 0) {
1510 if (copy > len)
1511 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001512 if (skb_store_bits(list, offset - start,
1513 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001514 goto fault;
1515 if ((len -= copy) == 0)
1516 return 0;
1517 offset += copy;
1518 from += copy;
1519 }
David S. Miller1a028e52007-04-27 15:21:23 -07001520 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001521 }
1522 }
1523 if (!len)
1524 return 0;
1525
1526fault:
1527 return -EFAULT;
1528}
1529
1530EXPORT_SYMBOL(skb_store_bits);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/* Checksum skb data. */
1533
Al Viro2bbbc862006-11-14 21:37:14 -08001534__wsum skb_checksum(const struct sk_buff *skb, int offset,
1535 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
David S. Miller1a028e52007-04-27 15:21:23 -07001537 int start = skb_headlen(skb);
1538 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int pos = 0;
1540
1541 /* Checksum header. */
1542 if (copy > 0) {
1543 if (copy > len)
1544 copy = len;
1545 csum = csum_partial(skb->data + offset, copy, csum);
1546 if ((len -= copy) == 0)
1547 return csum;
1548 offset += copy;
1549 pos = copy;
1550 }
1551
1552 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001553 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
David S. Miller1a028e52007-04-27 15:21:23 -07001555 BUG_TRAP(start <= offset + len);
1556
1557 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001559 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 u8 *vaddr;
1561 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1562
1563 if (copy > len)
1564 copy = len;
1565 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001566 csum2 = csum_partial(vaddr + frag->page_offset +
1567 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 kunmap_skb_frag(vaddr);
1569 csum = csum_block_add(csum, csum2, pos);
1570 if (!(len -= copy))
1571 return csum;
1572 offset += copy;
1573 pos += copy;
1574 }
David S. Miller1a028e52007-04-27 15:21:23 -07001575 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 if (skb_shinfo(skb)->frag_list) {
1579 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1580
1581 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001582 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
David S. Miller1a028e52007-04-27 15:21:23 -07001584 BUG_TRAP(start <= offset + len);
1585
1586 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001588 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (copy > len)
1590 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001591 csum2 = skb_checksum(list, offset - start,
1592 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 csum = csum_block_add(csum, csum2, pos);
1594 if ((len -= copy) == 0)
1595 return csum;
1596 offset += copy;
1597 pos += copy;
1598 }
David S. Miller1a028e52007-04-27 15:21:23 -07001599 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001602 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 return csum;
1605}
1606
1607/* Both of above in one bottle. */
1608
Al Viro81d77662006-11-14 21:37:33 -08001609__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1610 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
David S. Miller1a028e52007-04-27 15:21:23 -07001612 int start = skb_headlen(skb);
1613 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int pos = 0;
1615
1616 /* Copy header. */
1617 if (copy > 0) {
1618 if (copy > len)
1619 copy = len;
1620 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1621 copy, csum);
1622 if ((len -= copy) == 0)
1623 return csum;
1624 offset += copy;
1625 to += copy;
1626 pos = copy;
1627 }
1628
1629 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001630 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
David S. Miller1a028e52007-04-27 15:21:23 -07001632 BUG_TRAP(start <= offset + len);
1633
1634 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001636 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 u8 *vaddr;
1638 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1639
1640 if (copy > len)
1641 copy = len;
1642 vaddr = kmap_skb_frag(frag);
1643 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001644 frag->page_offset +
1645 offset - start, to,
1646 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 kunmap_skb_frag(vaddr);
1648 csum = csum_block_add(csum, csum2, pos);
1649 if (!(len -= copy))
1650 return csum;
1651 offset += copy;
1652 to += copy;
1653 pos += copy;
1654 }
David S. Miller1a028e52007-04-27 15:21:23 -07001655 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
1657
1658 if (skb_shinfo(skb)->frag_list) {
1659 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1660
1661 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001662 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001663 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
David S. Miller1a028e52007-04-27 15:21:23 -07001665 BUG_TRAP(start <= offset + len);
1666
1667 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if ((copy = end - offset) > 0) {
1669 if (copy > len)
1670 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001671 csum2 = skb_copy_and_csum_bits(list,
1672 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 to, copy, 0);
1674 csum = csum_block_add(csum, csum2, pos);
1675 if ((len -= copy) == 0)
1676 return csum;
1677 offset += copy;
1678 to += copy;
1679 pos += copy;
1680 }
David S. Miller1a028e52007-04-27 15:21:23 -07001681 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001684 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return csum;
1686}
1687
1688void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1689{
Al Virod3bc23e2006-11-14 21:24:49 -08001690 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 long csstart;
1692
Patrick McHardy84fa7932006-08-29 16:44:56 -07001693 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001694 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 else
1696 csstart = skb_headlen(skb);
1697
Kris Katterjohn09a62662006-01-08 22:24:28 -08001698 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001700 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 csum = 0;
1703 if (csstart != skb->len)
1704 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1705 skb->len - csstart, 0);
1706
Patrick McHardy84fa7932006-08-29 16:44:56 -07001707 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001708 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Al Virod3bc23e2006-11-14 21:24:49 -08001710 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712}
1713
1714/**
1715 * skb_dequeue - remove from the head of the queue
1716 * @list: list to dequeue from
1717 *
1718 * Remove the head of the list. The list lock is taken so the function
1719 * may be used safely with other locking list functions. The head item is
1720 * returned or %NULL if the list is empty.
1721 */
1722
1723struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1724{
1725 unsigned long flags;
1726 struct sk_buff *result;
1727
1728 spin_lock_irqsave(&list->lock, flags);
1729 result = __skb_dequeue(list);
1730 spin_unlock_irqrestore(&list->lock, flags);
1731 return result;
1732}
1733
1734/**
1735 * skb_dequeue_tail - remove from the tail of the queue
1736 * @list: list to dequeue from
1737 *
1738 * Remove the tail of the list. The list lock is taken so the function
1739 * may be used safely with other locking list functions. The tail item is
1740 * returned or %NULL if the list is empty.
1741 */
1742struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1743{
1744 unsigned long flags;
1745 struct sk_buff *result;
1746
1747 spin_lock_irqsave(&list->lock, flags);
1748 result = __skb_dequeue_tail(list);
1749 spin_unlock_irqrestore(&list->lock, flags);
1750 return result;
1751}
1752
1753/**
1754 * skb_queue_purge - empty a list
1755 * @list: list to empty
1756 *
1757 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1758 * the list and one reference dropped. This function takes the list
1759 * lock and is atomic with respect to other list locking functions.
1760 */
1761void skb_queue_purge(struct sk_buff_head *list)
1762{
1763 struct sk_buff *skb;
1764 while ((skb = skb_dequeue(list)) != NULL)
1765 kfree_skb(skb);
1766}
1767
1768/**
1769 * skb_queue_head - queue a buffer at the list head
1770 * @list: list to use
1771 * @newsk: buffer to queue
1772 *
1773 * Queue a buffer at the start of the list. This function takes the
1774 * list lock and can be used safely with other locking &sk_buff functions
1775 * safely.
1776 *
1777 * A buffer cannot be placed on two lists at the same time.
1778 */
1779void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1780{
1781 unsigned long flags;
1782
1783 spin_lock_irqsave(&list->lock, flags);
1784 __skb_queue_head(list, newsk);
1785 spin_unlock_irqrestore(&list->lock, flags);
1786}
1787
1788/**
1789 * skb_queue_tail - queue a buffer at the list tail
1790 * @list: list to use
1791 * @newsk: buffer to queue
1792 *
1793 * Queue a buffer at the tail of the list. This function takes the
1794 * list lock and can be used safely with other locking &sk_buff functions
1795 * safely.
1796 *
1797 * A buffer cannot be placed on two lists at the same time.
1798 */
1799void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1800{
1801 unsigned long flags;
1802
1803 spin_lock_irqsave(&list->lock, flags);
1804 __skb_queue_tail(list, newsk);
1805 spin_unlock_irqrestore(&list->lock, flags);
1806}
David S. Miller8728b832005-08-09 19:25:21 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/**
1809 * skb_unlink - remove a buffer from a list
1810 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001811 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 *
David S. Miller8728b832005-08-09 19:25:21 -07001813 * Remove a packet from a list. The list locks are taken and this
1814 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 *
David S. Miller8728b832005-08-09 19:25:21 -07001816 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
David S. Miller8728b832005-08-09 19:25:21 -07001818void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
David S. Miller8728b832005-08-09 19:25:21 -07001820 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
David S. Miller8728b832005-08-09 19:25:21 -07001822 spin_lock_irqsave(&list->lock, flags);
1823 __skb_unlink(skb, list);
1824 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/**
1828 * skb_append - append a buffer
1829 * @old: buffer to insert after
1830 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001831 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 *
1833 * Place a packet after a given packet in a list. The list locks are taken
1834 * and this function is atomic with respect to other list locked calls.
1835 * A buffer cannot be placed on two lists at the same time.
1836 */
David S. Miller8728b832005-08-09 19:25:21 -07001837void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 unsigned long flags;
1840
David S. Miller8728b832005-08-09 19:25:21 -07001841 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001842 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001843 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
1846
1847/**
1848 * skb_insert - insert a buffer
1849 * @old: buffer to insert before
1850 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001851 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 *
David S. Miller8728b832005-08-09 19:25:21 -07001853 * Place a packet before a given packet in a list. The list locks are
1854 * taken and this function is atomic with respect to other list locked
1855 * calls.
1856 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 * A buffer cannot be placed on two lists at the same time.
1858 */
David S. Miller8728b832005-08-09 19:25:21 -07001859void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 unsigned long flags;
1862
David S. Miller8728b832005-08-09 19:25:21 -07001863 spin_lock_irqsave(&list->lock, flags);
1864 __skb_insert(newsk, old->prev, old, list);
1865 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868static inline void skb_split_inside_header(struct sk_buff *skb,
1869 struct sk_buff* skb1,
1870 const u32 len, const int pos)
1871{
1872 int i;
1873
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001874 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1875 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* And move data appendix as is. */
1877 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1878 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1879
1880 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1881 skb_shinfo(skb)->nr_frags = 0;
1882 skb1->data_len = skb->data_len;
1883 skb1->len += skb1->data_len;
1884 skb->data_len = 0;
1885 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001886 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889static inline void skb_split_no_header(struct sk_buff *skb,
1890 struct sk_buff* skb1,
1891 const u32 len, int pos)
1892{
1893 int i, k = 0;
1894 const int nfrags = skb_shinfo(skb)->nr_frags;
1895
1896 skb_shinfo(skb)->nr_frags = 0;
1897 skb1->len = skb1->data_len = skb->len - len;
1898 skb->len = len;
1899 skb->data_len = len - pos;
1900
1901 for (i = 0; i < nfrags; i++) {
1902 int size = skb_shinfo(skb)->frags[i].size;
1903
1904 if (pos + size > len) {
1905 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1906
1907 if (pos < len) {
1908 /* Split frag.
1909 * We have two variants in this case:
1910 * 1. Move all the frag to the second
1911 * part, if it is possible. F.e.
1912 * this approach is mandatory for TUX,
1913 * where splitting is expensive.
1914 * 2. Split is accurately. We make this.
1915 */
1916 get_page(skb_shinfo(skb)->frags[i].page);
1917 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1918 skb_shinfo(skb1)->frags[0].size -= len - pos;
1919 skb_shinfo(skb)->frags[i].size = len - pos;
1920 skb_shinfo(skb)->nr_frags++;
1921 }
1922 k++;
1923 } else
1924 skb_shinfo(skb)->nr_frags++;
1925 pos += size;
1926 }
1927 skb_shinfo(skb1)->nr_frags = k;
1928}
1929
1930/**
1931 * skb_split - Split fragmented skb to two parts at length len.
1932 * @skb: the buffer to split
1933 * @skb1: the buffer to receive the second part
1934 * @len: new length for skb
1935 */
1936void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1937{
1938 int pos = skb_headlen(skb);
1939
1940 if (len < pos) /* Split line is inside header. */
1941 skb_split_inside_header(skb, skb1, len, pos);
1942 else /* Second chunk has no header, nothing to copy. */
1943 skb_split_no_header(skb, skb1, len, pos);
1944}
1945
Thomas Graf677e90e2005-06-23 20:59:51 -07001946/**
1947 * skb_prepare_seq_read - Prepare a sequential read of skb data
1948 * @skb: the buffer to read
1949 * @from: lower offset of data to be read
1950 * @to: upper offset of data to be read
1951 * @st: state variable
1952 *
1953 * Initializes the specified state variable. Must be called before
1954 * invoking skb_seq_read() for the first time.
1955 */
1956void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1957 unsigned int to, struct skb_seq_state *st)
1958{
1959 st->lower_offset = from;
1960 st->upper_offset = to;
1961 st->root_skb = st->cur_skb = skb;
1962 st->frag_idx = st->stepped_offset = 0;
1963 st->frag_data = NULL;
1964}
1965
1966/**
1967 * skb_seq_read - Sequentially read skb data
1968 * @consumed: number of bytes consumed by the caller so far
1969 * @data: destination pointer for data to be returned
1970 * @st: state variable
1971 *
1972 * Reads a block of skb data at &consumed relative to the
1973 * lower offset specified to skb_prepare_seq_read(). Assigns
1974 * the head of the data block to &data and returns the length
1975 * of the block or 0 if the end of the skb data or the upper
1976 * offset has been reached.
1977 *
1978 * The caller is not required to consume all of the data
1979 * returned, i.e. &consumed is typically set to the number
1980 * of bytes already consumed and the next call to
1981 * skb_seq_read() will return the remaining part of the block.
1982 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001983 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07001984 * this limitation is the cost for zerocopy seqeuental
1985 * reads of potentially non linear data.
1986 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001987 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07001988 * at the moment, state->root_skb could be replaced with
1989 * a stack for this purpose.
1990 */
1991unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1992 struct skb_seq_state *st)
1993{
1994 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1995 skb_frag_t *frag;
1996
1997 if (unlikely(abs_offset >= st->upper_offset))
1998 return 0;
1999
2000next_skb:
2001 block_limit = skb_headlen(st->cur_skb);
2002
2003 if (abs_offset < block_limit) {
2004 *data = st->cur_skb->data + abs_offset;
2005 return block_limit - abs_offset;
2006 }
2007
2008 if (st->frag_idx == 0 && !st->frag_data)
2009 st->stepped_offset += skb_headlen(st->cur_skb);
2010
2011 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2012 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2013 block_limit = frag->size + st->stepped_offset;
2014
2015 if (abs_offset < block_limit) {
2016 if (!st->frag_data)
2017 st->frag_data = kmap_skb_frag(frag);
2018
2019 *data = (u8 *) st->frag_data + frag->page_offset +
2020 (abs_offset - st->stepped_offset);
2021
2022 return block_limit - abs_offset;
2023 }
2024
2025 if (st->frag_data) {
2026 kunmap_skb_frag(st->frag_data);
2027 st->frag_data = NULL;
2028 }
2029
2030 st->frag_idx++;
2031 st->stepped_offset += frag->size;
2032 }
2033
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002034 if (st->frag_data) {
2035 kunmap_skb_frag(st->frag_data);
2036 st->frag_data = NULL;
2037 }
2038
Thomas Graf677e90e2005-06-23 20:59:51 -07002039 if (st->cur_skb->next) {
2040 st->cur_skb = st->cur_skb->next;
2041 st->frag_idx = 0;
2042 goto next_skb;
2043 } else if (st->root_skb == st->cur_skb &&
2044 skb_shinfo(st->root_skb)->frag_list) {
2045 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2046 goto next_skb;
2047 }
2048
2049 return 0;
2050}
2051
2052/**
2053 * skb_abort_seq_read - Abort a sequential read of skb data
2054 * @st: state variable
2055 *
2056 * Must be called if skb_seq_read() was not called until it
2057 * returned 0.
2058 */
2059void skb_abort_seq_read(struct skb_seq_state *st)
2060{
2061 if (st->frag_data)
2062 kunmap_skb_frag(st->frag_data);
2063}
2064
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002065#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2066
2067static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2068 struct ts_config *conf,
2069 struct ts_state *state)
2070{
2071 return skb_seq_read(offset, text, TS_SKB_CB(state));
2072}
2073
2074static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2075{
2076 skb_abort_seq_read(TS_SKB_CB(state));
2077}
2078
2079/**
2080 * skb_find_text - Find a text pattern in skb data
2081 * @skb: the buffer to look in
2082 * @from: search offset
2083 * @to: search limit
2084 * @config: textsearch configuration
2085 * @state: uninitialized textsearch state variable
2086 *
2087 * Finds a pattern in the skb data according to the specified
2088 * textsearch configuration. Use textsearch_next() to retrieve
2089 * subsequent occurrences of the pattern. Returns the offset
2090 * to the first occurrence or UINT_MAX if no match was found.
2091 */
2092unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2093 unsigned int to, struct ts_config *config,
2094 struct ts_state *state)
2095{
Phil Oesterf72b9482006-06-26 00:00:57 -07002096 unsigned int ret;
2097
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002098 config->get_next_block = skb_ts_get_next_block;
2099 config->finish = skb_ts_finish;
2100
2101 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2102
Phil Oesterf72b9482006-06-26 00:00:57 -07002103 ret = textsearch_find(config, state);
2104 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002105}
2106
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002107/**
2108 * skb_append_datato_frags: - append the user data to a skb
2109 * @sk: sock structure
2110 * @skb: skb structure to be appened with user data.
2111 * @getfrag: call back function to be used for getting the user data
2112 * @from: pointer to user message iov
2113 * @length: length of the iov message
2114 *
2115 * Description: This procedure append the user data in the fragment part
2116 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2117 */
2118int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002119 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002120 int len, int odd, struct sk_buff *skb),
2121 void *from, int length)
2122{
2123 int frg_cnt = 0;
2124 skb_frag_t *frag = NULL;
2125 struct page *page = NULL;
2126 int copy, left;
2127 int offset = 0;
2128 int ret;
2129
2130 do {
2131 /* Return error if we don't have space for new frag */
2132 frg_cnt = skb_shinfo(skb)->nr_frags;
2133 if (frg_cnt >= MAX_SKB_FRAGS)
2134 return -EFAULT;
2135
2136 /* allocate a new page for next frag */
2137 page = alloc_pages(sk->sk_allocation, 0);
2138
2139 /* If alloc_page fails just return failure and caller will
2140 * free previous allocated pages by doing kfree_skb()
2141 */
2142 if (page == NULL)
2143 return -ENOMEM;
2144
2145 /* initialize the next frag */
2146 sk->sk_sndmsg_page = page;
2147 sk->sk_sndmsg_off = 0;
2148 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2149 skb->truesize += PAGE_SIZE;
2150 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2151
2152 /* get the new initialized frag */
2153 frg_cnt = skb_shinfo(skb)->nr_frags;
2154 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2155
2156 /* copy the user data to page */
2157 left = PAGE_SIZE - frag->page_offset;
2158 copy = (length > left)? left : length;
2159
2160 ret = getfrag(from, (page_address(frag->page) +
2161 frag->page_offset + frag->size),
2162 offset, copy, 0, skb);
2163 if (ret < 0)
2164 return -EFAULT;
2165
2166 /* copy was successful so update the size parameters */
2167 sk->sk_sndmsg_off += copy;
2168 frag->size += copy;
2169 skb->len += copy;
2170 skb->data_len += copy;
2171 offset += copy;
2172 length -= copy;
2173
2174 } while (length > 0);
2175
2176 return 0;
2177}
2178
Herbert Xucbb042f92006-03-20 22:43:56 -08002179/**
2180 * skb_pull_rcsum - pull skb and update receive checksum
2181 * @skb: buffer to update
Herbert Xucbb042f92006-03-20 22:43:56 -08002182 * @len: length of data pulled
2183 *
2184 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002185 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002186 * receive path processing instead of skb_pull unless you know
2187 * that the checksum difference is zero (e.g., a valid IP header)
2188 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f92006-03-20 22:43:56 -08002189 */
2190unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2191{
2192 BUG_ON(len > skb->len);
2193 skb->len -= len;
2194 BUG_ON(skb->len < skb->data_len);
2195 skb_postpull_rcsum(skb, skb->data, len);
2196 return skb->data += len;
2197}
2198
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002199EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2200
Herbert Xuf4c50d92006-06-22 03:02:40 -07002201/**
2202 * skb_segment - Perform protocol segmentation on skb.
2203 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002204 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002205 *
2206 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002207 * a pointer to the first in a list of new skbs for the segments.
2208 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002209 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002210struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002211{
2212 struct sk_buff *segs = NULL;
2213 struct sk_buff *tail = NULL;
2214 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002215 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002216 unsigned int offset = doffset;
2217 unsigned int headroom;
2218 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002219 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002220 int nfrags = skb_shinfo(skb)->nr_frags;
2221 int err = -ENOMEM;
2222 int i = 0;
2223 int pos;
2224
2225 __skb_push(skb, doffset);
2226 headroom = skb_headroom(skb);
2227 pos = skb_headlen(skb);
2228
2229 do {
2230 struct sk_buff *nskb;
2231 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002232 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002233 int k;
2234 int size;
2235
2236 len = skb->len - offset;
2237 if (len > mss)
2238 len = mss;
2239
2240 hsize = skb_headlen(skb) - offset;
2241 if (hsize < 0)
2242 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002243 if (hsize > len || !sg)
2244 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002245
Herbert Xuc8884ed2006-10-29 15:59:41 -08002246 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002247 if (unlikely(!nskb))
2248 goto err;
2249
2250 if (segs)
2251 tail->next = nskb;
2252 else
2253 segs = nskb;
2254 tail = nskb;
2255
2256 nskb->dev = skb->dev;
Peter P Waskiewicz Jrf25f4e42007-07-06 13:36:20 -07002257 skb_copy_queue_mapping(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002258 nskb->priority = skb->priority;
2259 nskb->protocol = skb->protocol;
Patrick McHardy6aa895b2008-07-14 22:49:06 -07002260 nskb->vlan_tci = skb->vlan_tci;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002261 nskb->dst = dst_clone(skb->dst);
2262 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2263 nskb->pkt_type = skb->pkt_type;
2264 nskb->mac_len = skb->mac_len;
2265
2266 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002267 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002268 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002269 nskb->transport_header = (nskb->network_header +
2270 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002271 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2272 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002273 if (!sg) {
2274 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2275 skb_put(nskb, len),
2276 len, 0);
2277 continue;
2278 }
2279
2280 frag = skb_shinfo(nskb)->frags;
2281 k = 0;
2282
Patrick McHardy84fa7932006-08-29 16:44:56 -07002283 nskb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002284 nskb->csum = skb->csum;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002285 skb_copy_from_linear_data_offset(skb, offset,
2286 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002287
2288 while (pos < offset + len) {
2289 BUG_ON(i >= nfrags);
2290
2291 *frag = skb_shinfo(skb)->frags[i];
2292 get_page(frag->page);
2293 size = frag->size;
2294
2295 if (pos < offset) {
2296 frag->page_offset += offset - pos;
2297 frag->size -= offset - pos;
2298 }
2299
2300 k++;
2301
2302 if (pos + size <= offset + len) {
2303 i++;
2304 pos += size;
2305 } else {
2306 frag->size -= pos + size - (offset + len);
2307 break;
2308 }
2309
2310 frag++;
2311 }
2312
2313 skb_shinfo(nskb)->nr_frags = k;
2314 nskb->data_len = len - hsize;
2315 nskb->len += nskb->data_len;
2316 nskb->truesize += nskb->data_len;
2317 } while ((offset += len) < skb->len);
2318
2319 return segs;
2320
2321err:
2322 while ((skb = segs)) {
2323 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002324 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002325 }
2326 return ERR_PTR(err);
2327}
2328
2329EXPORT_SYMBOL_GPL(skb_segment);
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331void __init skb_init(void)
2332{
2333 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2334 sizeof(struct sk_buff),
2335 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002336 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002337 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002338 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2339 (2*sizeof(struct sk_buff)) +
2340 sizeof(atomic_t),
2341 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002342 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002343 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
David Howells716ea3a2007-04-02 20:19:53 -07002346/**
2347 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2348 * @skb: Socket buffer containing the buffers to be mapped
2349 * @sg: The scatter-gather list to map into
2350 * @offset: The offset into the buffer's contents to start mapping
2351 * @len: Length of buffer space to be mapped
2352 *
2353 * Fill the specified scatter-gather list with mappings/pointers into a
2354 * region of the buffer space attached to a socket buffer.
2355 */
David S. Miller51c739d2007-10-30 21:29:29 -07002356static int
2357__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002358{
David S. Miller1a028e52007-04-27 15:21:23 -07002359 int start = skb_headlen(skb);
2360 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002361 int elt = 0;
2362
2363 if (copy > 0) {
2364 if (copy > len)
2365 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002366 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002367 elt++;
2368 if ((len -= copy) == 0)
2369 return elt;
2370 offset += copy;
2371 }
2372
2373 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002374 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002375
David S. Miller1a028e52007-04-27 15:21:23 -07002376 BUG_TRAP(start <= offset + len);
2377
2378 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002379 if ((copy = end - offset) > 0) {
2380 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2381
2382 if (copy > len)
2383 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002384 sg_set_page(&sg[elt], frag->page, copy,
2385 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002386 elt++;
2387 if (!(len -= copy))
2388 return elt;
2389 offset += copy;
2390 }
David S. Miller1a028e52007-04-27 15:21:23 -07002391 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002392 }
2393
2394 if (skb_shinfo(skb)->frag_list) {
2395 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2396
2397 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002398 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002399
David S. Miller1a028e52007-04-27 15:21:23 -07002400 BUG_TRAP(start <= offset + len);
2401
2402 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002403 if ((copy = end - offset) > 0) {
2404 if (copy > len)
2405 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002406 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2407 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002408 if ((len -= copy) == 0)
2409 return elt;
2410 offset += copy;
2411 }
David S. Miller1a028e52007-04-27 15:21:23 -07002412 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002413 }
2414 }
2415 BUG_ON(len);
2416 return elt;
2417}
2418
David S. Miller51c739d2007-10-30 21:29:29 -07002419int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2420{
2421 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2422
Jens Axboec46f2332007-10-31 12:06:37 +01002423 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002424
2425 return nsg;
2426}
2427
David Howells716ea3a2007-04-02 20:19:53 -07002428/**
2429 * skb_cow_data - Check that a socket buffer's data buffers are writable
2430 * @skb: The socket buffer to check.
2431 * @tailbits: Amount of trailing space to be added
2432 * @trailer: Returned pointer to the skb where the @tailbits space begins
2433 *
2434 * Make sure that the data buffers attached to a socket buffer are
2435 * writable. If they are not, private copies are made of the data buffers
2436 * and the socket buffer is set to use these instead.
2437 *
2438 * If @tailbits is given, make sure that there is space to write @tailbits
2439 * bytes of data beyond current end of socket buffer. @trailer will be
2440 * set to point to the skb in which this space begins.
2441 *
2442 * The number of scatterlist elements required to completely map the
2443 * COW'd and extended socket buffer will be returned.
2444 */
2445int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2446{
2447 int copyflag;
2448 int elt;
2449 struct sk_buff *skb1, **skb_p;
2450
2451 /* If skb is cloned or its head is paged, reallocate
2452 * head pulling out all the pages (pages are considered not writable
2453 * at the moment even if they are anonymous).
2454 */
2455 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2456 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2457 return -ENOMEM;
2458
2459 /* Easy case. Most of packets will go this way. */
2460 if (!skb_shinfo(skb)->frag_list) {
2461 /* A little of trouble, not enough of space for trailer.
2462 * This should not happen, when stack is tuned to generate
2463 * good frames. OK, on miss we reallocate and reserve even more
2464 * space, 128 bytes is fair. */
2465
2466 if (skb_tailroom(skb) < tailbits &&
2467 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2468 return -ENOMEM;
2469
2470 /* Voila! */
2471 *trailer = skb;
2472 return 1;
2473 }
2474
2475 /* Misery. We are in troubles, going to mincer fragments... */
2476
2477 elt = 1;
2478 skb_p = &skb_shinfo(skb)->frag_list;
2479 copyflag = 0;
2480
2481 while ((skb1 = *skb_p) != NULL) {
2482 int ntail = 0;
2483
2484 /* The fragment is partially pulled by someone,
2485 * this can happen on input. Copy it and everything
2486 * after it. */
2487
2488 if (skb_shared(skb1))
2489 copyflag = 1;
2490
2491 /* If the skb is the last, worry about trailer. */
2492
2493 if (skb1->next == NULL && tailbits) {
2494 if (skb_shinfo(skb1)->nr_frags ||
2495 skb_shinfo(skb1)->frag_list ||
2496 skb_tailroom(skb1) < tailbits)
2497 ntail = tailbits + 128;
2498 }
2499
2500 if (copyflag ||
2501 skb_cloned(skb1) ||
2502 ntail ||
2503 skb_shinfo(skb1)->nr_frags ||
2504 skb_shinfo(skb1)->frag_list) {
2505 struct sk_buff *skb2;
2506
2507 /* Fuck, we are miserable poor guys... */
2508 if (ntail == 0)
2509 skb2 = skb_copy(skb1, GFP_ATOMIC);
2510 else
2511 skb2 = skb_copy_expand(skb1,
2512 skb_headroom(skb1),
2513 ntail,
2514 GFP_ATOMIC);
2515 if (unlikely(skb2 == NULL))
2516 return -ENOMEM;
2517
2518 if (skb1->sk)
2519 skb_set_owner_w(skb2, skb1->sk);
2520
2521 /* Looking around. Are we still alive?
2522 * OK, link new skb, drop old one */
2523
2524 skb2->next = skb1->next;
2525 *skb_p = skb2;
2526 kfree_skb(skb1);
2527 skb1 = skb2;
2528 }
2529 elt++;
2530 *trailer = skb1;
2531 skb_p = &skb1->next;
2532 }
2533
2534 return elt;
2535}
2536
Rusty Russellf35d9d82008-02-04 23:49:54 -05002537/**
2538 * skb_partial_csum_set - set up and verify partial csum values for packet
2539 * @skb: the skb to set
2540 * @start: the number of bytes after skb->data to start checksumming.
2541 * @off: the offset from start to place the checksum.
2542 *
2543 * For untrusted partially-checksummed packets, we need to make sure the values
2544 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2545 *
2546 * This function checks and sets those values and skb->ip_summed: if this
2547 * returns false you should drop the packet.
2548 */
2549bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2550{
2551 if (unlikely(start > skb->len - 2) ||
2552 unlikely((int)start + off > skb->len - 2)) {
2553 if (net_ratelimit())
2554 printk(KERN_WARNING
2555 "bad partial csum: csum=%u/%u len=%u\n",
2556 start, off, skb->len);
2557 return false;
2558 }
2559 skb->ip_summed = CHECKSUM_PARTIAL;
2560 skb->csum_start = skb_headroom(skb) + start;
2561 skb->csum_offset = off;
2562 return true;
2563}
2564
Ben Hutchings4497b072008-06-19 16:22:28 -07002565void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2566{
2567 if (net_ratelimit())
2568 pr_warning("%s: received packets cannot be forwarded"
2569 " while LRO is enabled\n", skb->dev->name);
2570}
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572EXPORT_SYMBOL(___pskb_trim);
2573EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002574EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002576EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002577EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578EXPORT_SYMBOL(pskb_copy);
2579EXPORT_SYMBOL(pskb_expand_head);
2580EXPORT_SYMBOL(skb_checksum);
2581EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582EXPORT_SYMBOL(skb_copy);
2583EXPORT_SYMBOL(skb_copy_and_csum_bits);
2584EXPORT_SYMBOL(skb_copy_and_csum_dev);
2585EXPORT_SYMBOL(skb_copy_bits);
2586EXPORT_SYMBOL(skb_copy_expand);
2587EXPORT_SYMBOL(skb_over_panic);
2588EXPORT_SYMBOL(skb_pad);
2589EXPORT_SYMBOL(skb_realloc_headroom);
2590EXPORT_SYMBOL(skb_under_panic);
2591EXPORT_SYMBOL(skb_dequeue);
2592EXPORT_SYMBOL(skb_dequeue_tail);
2593EXPORT_SYMBOL(skb_insert);
2594EXPORT_SYMBOL(skb_queue_purge);
2595EXPORT_SYMBOL(skb_queue_head);
2596EXPORT_SYMBOL(skb_queue_tail);
2597EXPORT_SYMBOL(skb_unlink);
2598EXPORT_SYMBOL(skb_append);
2599EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002600EXPORT_SYMBOL(skb_prepare_seq_read);
2601EXPORT_SYMBOL(skb_seq_read);
2602EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002603EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002604EXPORT_SYMBOL(skb_append_datato_frags);
Ben Hutchings4497b072008-06-19 16:22:28 -07002605EXPORT_SYMBOL(__skb_warn_lro_forwarding);
David Howells716ea3a2007-04-02 20:19:53 -07002606
2607EXPORT_SYMBOL_GPL(skb_to_sgvec);
2608EXPORT_SYMBOL_GPL(skb_cow_data);
Rusty Russellf35d9d82008-02-04 23:49:54 -05002609EXPORT_SYMBOL_GPL(skb_partial_csum_set);