blob: ca1ccdf1ef7612b87d16f53f803b2460de612360 [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 McHardy6aa895b02008-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);
Johannes Bergd0f09802008-07-29 11:32:07 +0200488#if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
489 C(do_not_encrypt);
490#endif
Paul Moore02f1c892008-01-07 21:56:41 -0800491 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 atomic_inc(&(skb_shinfo(skb)->dataref));
494 skb->cloned = 1;
495
496 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700497#undef C
498}
499
500/**
501 * skb_morph - morph one skb into another
502 * @dst: the skb to receive the contents
503 * @src: the skb to supply the contents
504 *
505 * This is identical to skb_clone except that the target skb is
506 * supplied by the user.
507 *
508 * The target skb is returned upon exit.
509 */
510struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
511{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800512 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700513 return __skb_clone(dst, src);
514}
515EXPORT_SYMBOL_GPL(skb_morph);
516
517/**
518 * skb_clone - duplicate an sk_buff
519 * @skb: buffer to clone
520 * @gfp_mask: allocation priority
521 *
522 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
523 * copies share the same packet data but not structure. The new
524 * buffer has a reference count of 1. If the allocation fails the
525 * function returns %NULL otherwise the new buffer is returned.
526 *
527 * If this function is called from an interrupt gfp_mask() must be
528 * %GFP_ATOMIC.
529 */
530
531struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
532{
533 struct sk_buff *n;
534
535 n = skb + 1;
536 if (skb->fclone == SKB_FCLONE_ORIG &&
537 n->fclone == SKB_FCLONE_UNAVAILABLE) {
538 atomic_t *fclone_ref = (atomic_t *) (n + 1);
539 n->fclone = SKB_FCLONE_CLONE;
540 atomic_inc(fclone_ref);
541 } else {
542 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
543 if (!n)
544 return NULL;
545 n->fclone = SKB_FCLONE_UNAVAILABLE;
546 }
547
548 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
552{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700553#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Shift between the two data areas in bytes
556 */
557 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700558#endif
Herbert Xudec18812007-10-14 00:37:30 -0700559
560 __copy_skb_header(new, old);
561
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700562#ifndef NET_SKBUFF_DATA_USES_OFFSET
563 /* {transport,network,mac}_header are relative to skb->head */
564 new->transport_header += offset;
565 new->network_header += offset;
566 new->mac_header += offset;
567#endif
Herbert Xu79671682006-06-22 02:40:14 -0700568 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
569 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
570 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/**
574 * skb_copy - create private copy of an sk_buff
575 * @skb: buffer to copy
576 * @gfp_mask: allocation priority
577 *
578 * Make a copy of both an &sk_buff and its data. This is used when the
579 * caller wishes to modify the data and needs a private copy of the
580 * data to alter. Returns %NULL on failure or the pointer to the buffer
581 * on success. The returned buffer has a reference count of 1.
582 *
583 * As by-product this function converts non-linear &sk_buff to linear
584 * one, so that &sk_buff becomes completely private and caller is allowed
585 * to modify all the data of returned buffer. This means that this
586 * function is not recommended for use in circumstances when only
587 * header is going to be modified. Use pskb_copy() instead.
588 */
589
Al Virodd0fc662005-10-07 07:46:04 +0100590struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 int headerlen = skb->data - skb->head;
593 /*
594 * Allocate the copy buffer
595 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700596 struct sk_buff *n;
597#ifdef NET_SKBUFF_DATA_USES_OFFSET
598 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
599#else
600 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!n)
603 return NULL;
604
605 /* Set the data pointer */
606 skb_reserve(n, headerlen);
607 /* Set the tail pointer and length */
608 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
611 BUG();
612
613 copy_skb_header(n, skb);
614 return n;
615}
616
617
618/**
619 * pskb_copy - create copy of an sk_buff with private head.
620 * @skb: buffer to copy
621 * @gfp_mask: allocation priority
622 *
623 * Make a copy of both an &sk_buff and part of its data, located
624 * in header. Fragmented data remain shared. This is used when
625 * the caller wishes to modify only header of &sk_buff and needs
626 * private copy of the header to alter. Returns %NULL on failure
627 * or the pointer to the buffer on success.
628 * The returned buffer has a reference count of 1.
629 */
630
Al Virodd0fc662005-10-07 07:46:04 +0100631struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 /*
634 * Allocate the copy buffer
635 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700636 struct sk_buff *n;
637#ifdef NET_SKBUFF_DATA_USES_OFFSET
638 n = alloc_skb(skb->end, gfp_mask);
639#else
640 n = alloc_skb(skb->end - skb->head, gfp_mask);
641#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!n)
643 goto out;
644
645 /* Set the data pointer */
646 skb_reserve(n, skb->data - skb->head);
647 /* Set the tail pointer and length */
648 skb_put(n, skb_headlen(skb));
649 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300650 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Herbert Xu25f484a2006-11-07 14:57:15 -0800652 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 n->data_len = skb->data_len;
654 n->len = skb->len;
655
656 if (skb_shinfo(skb)->nr_frags) {
657 int i;
658
659 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
660 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
661 get_page(skb_shinfo(n)->frags[i].page);
662 }
663 skb_shinfo(n)->nr_frags = i;
664 }
665
666 if (skb_shinfo(skb)->frag_list) {
667 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
668 skb_clone_fraglist(n);
669 }
670
671 copy_skb_header(n, skb);
672out:
673 return n;
674}
675
676/**
677 * pskb_expand_head - reallocate header of &sk_buff
678 * @skb: buffer to reallocate
679 * @nhead: room to add at head
680 * @ntail: room to add at tail
681 * @gfp_mask: allocation priority
682 *
683 * Expands (or creates identical copy, if &nhead and &ntail are zero)
684 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
685 * reference count of 1. Returns zero in the case of success or error,
686 * if expansion failed. In the last case, &sk_buff is not changed.
687 *
688 * All the pointers pointing into skb header may change and must be
689 * reloaded after call to this function.
690 */
691
Victor Fusco86a76ca2005-07-08 14:57:47 -0700692int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100693 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int i;
696 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700697#ifdef NET_SKBUFF_DATA_USES_OFFSET
698 int size = nhead + skb->end + ntail;
699#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 long off;
703
704 if (skb_shared(skb))
705 BUG();
706
707 size = SKB_DATA_ALIGN(size);
708
709 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
710 if (!data)
711 goto nodata;
712
713 /* Copy only real data... and, alas, header. This should be
714 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700715#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700716 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700717#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700718 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700719#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700720 memcpy(data + size, skb_end_pointer(skb),
721 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
724 get_page(skb_shinfo(skb)->frags[i].page);
725
726 if (skb_shinfo(skb)->frag_list)
727 skb_clone_fraglist(skb);
728
729 skb_release_data(skb);
730
731 off = (data + nhead) - skb->head;
732
733 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700735#ifdef NET_SKBUFF_DATA_USES_OFFSET
736 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700737 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700738#else
739 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700740#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700741 /* {transport,network,mac}_header and tail are relative to skb->head */
742 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700743 skb->transport_header += off;
744 skb->network_header += off;
745 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700746 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700748 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 skb->nohdr = 0;
750 atomic_set(&skb_shinfo(skb)->dataref, 1);
751 return 0;
752
753nodata:
754 return -ENOMEM;
755}
756
757/* Make private copy of skb with writable head and some headroom */
758
759struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
760{
761 struct sk_buff *skb2;
762 int delta = headroom - skb_headroom(skb);
763
764 if (delta <= 0)
765 skb2 = pskb_copy(skb, GFP_ATOMIC);
766 else {
767 skb2 = skb_clone(skb, GFP_ATOMIC);
768 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
769 GFP_ATOMIC)) {
770 kfree_skb(skb2);
771 skb2 = NULL;
772 }
773 }
774 return skb2;
775}
776
777
778/**
779 * skb_copy_expand - copy and expand sk_buff
780 * @skb: buffer to copy
781 * @newheadroom: new free bytes at head
782 * @newtailroom: new free bytes at tail
783 * @gfp_mask: allocation priority
784 *
785 * Make a copy of both an &sk_buff and its data and while doing so
786 * allocate additional space.
787 *
788 * This is used when the caller wishes to modify the data and needs a
789 * private copy of the data to alter as well as more space for new fields.
790 * Returns %NULL on failure or the pointer to the buffer
791 * on success. The returned buffer has a reference count of 1.
792 *
793 * You must pass %GFP_ATOMIC as the allocation priority if this function
794 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
796struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700797 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100798 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 /*
801 * Allocate the copy buffer
802 */
803 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
804 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700805 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700807 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (!n)
810 return NULL;
811
812 skb_reserve(n, newheadroom);
813
814 /* Set the tail pointer and length */
815 skb_put(n, skb->len);
816
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700817 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 head_copy_off = 0;
819 if (newheadroom <= head_copy_len)
820 head_copy_len = newheadroom;
821 else
822 head_copy_off = newheadroom - head_copy_len;
823
824 /* Copy the linear header and data. */
825 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
826 skb->len + head_copy_len))
827 BUG();
828
829 copy_skb_header(n, skb);
830
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700831 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700832 n->csum_start += off;
833#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700834 n->transport_header += off;
835 n->network_header += off;
836 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700837#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return n;
840}
841
842/**
843 * skb_pad - zero pad the tail of an skb
844 * @skb: buffer to pad
845 * @pad: space to pad
846 *
847 * Ensure that a buffer is followed by a padding area that is zero
848 * filled. Used by network drivers which may DMA or transfer data
849 * beyond the buffer end onto the wire.
850 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700851 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900853
Herbert Xu5b057c62006-06-23 02:06:41 -0700854int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Herbert Xu5b057c62006-06-23 02:06:41 -0700856 int err;
857 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700860 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700864
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700865 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700866 if (likely(skb_cloned(skb) || ntail > 0)) {
867 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
868 if (unlikely(err))
869 goto free_skb;
870 }
871
872 /* FIXME: The use of this function with non-linear skb's really needs
873 * to be audited.
874 */
875 err = skb_linearize(skb);
876 if (unlikely(err))
877 goto free_skb;
878
879 memset(skb->data + skb->len, 0, pad);
880 return 0;
881
882free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700884 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900885}
886
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700887/**
888 * skb_put - add data to a buffer
889 * @skb: buffer to use
890 * @len: amount of data to add
891 *
892 * This function extends the used data area of the buffer. If this would
893 * exceed the total buffer size the kernel will panic. A pointer to the
894 * first byte of the extra data is returned.
895 */
896unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
897{
898 unsigned char *tmp = skb_tail_pointer(skb);
899 SKB_LINEAR_ASSERT(skb);
900 skb->tail += len;
901 skb->len += len;
902 if (unlikely(skb->tail > skb->end))
903 skb_over_panic(skb, len, __builtin_return_address(0));
904 return tmp;
905}
906EXPORT_SYMBOL(skb_put);
907
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700908/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -0700909 * skb_push - add data to the start of a buffer
910 * @skb: buffer to use
911 * @len: amount of data to add
912 *
913 * This function extends the used data area of the buffer at the buffer
914 * start. If this would exceed the total buffer headroom the kernel will
915 * panic. A pointer to the first byte of the extra data is returned.
916 */
917unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
918{
919 skb->data -= len;
920 skb->len += len;
921 if (unlikely(skb->data<skb->head))
922 skb_under_panic(skb, len, __builtin_return_address(0));
923 return skb->data;
924}
925EXPORT_SYMBOL(skb_push);
926
927/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700928 * skb_pull - remove data from the start of a buffer
929 * @skb: buffer to use
930 * @len: amount of data to remove
931 *
932 * This function removes data from the start of a buffer, returning
933 * the memory to the headroom. A pointer to the next data in the buffer
934 * is returned. Once the data has been pulled future pushes will overwrite
935 * the old data.
936 */
937unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
938{
939 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
940}
941EXPORT_SYMBOL(skb_pull);
942
Ilpo Järvinen419ae742008-03-27 17:54:01 -0700943/**
944 * skb_trim - remove end from a buffer
945 * @skb: buffer to alter
946 * @len: new length
947 *
948 * Cut the length of a buffer down by removing data from the tail. If
949 * the buffer is already under the length specified it is not modified.
950 * The skb must be linear.
951 */
952void skb_trim(struct sk_buff *skb, unsigned int len)
953{
954 if (skb->len > len)
955 __skb_trim(skb, len);
956}
957EXPORT_SYMBOL(skb_trim);
958
Herbert Xu3cc0e872006-06-09 16:13:38 -0700959/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
961
Herbert Xu3cc0e872006-06-09 16:13:38 -0700962int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Herbert Xu27b437c2006-07-13 19:26:39 -0700964 struct sk_buff **fragp;
965 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int offset = skb_headlen(skb);
967 int nfrags = skb_shinfo(skb)->nr_frags;
968 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -0700969 int err;
970
971 if (skb_cloned(skb) &&
972 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700975 i = 0;
976 if (offset >= len)
977 goto drop_pages;
978
979 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -0700981
982 if (end < len) {
983 offset = end;
984 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Herbert Xu27b437c2006-07-13 19:26:39 -0700986
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700987 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -0700988
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700989drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -0700990 skb_shinfo(skb)->nr_frags = i;
991
992 for (; i < nfrags; i++)
993 put_page(skb_shinfo(skb)->frags[i].page);
994
995 if (skb_shinfo(skb)->frag_list)
996 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -0700997 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Herbert Xu27b437c2006-07-13 19:26:39 -07001000 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1001 fragp = &frag->next) {
1002 int end = offset + frag->len;
1003
1004 if (skb_shared(frag)) {
1005 struct sk_buff *nfrag;
1006
1007 nfrag = skb_clone(frag, GFP_ATOMIC);
1008 if (unlikely(!nfrag))
1009 return -ENOMEM;
1010
1011 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001012 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001013 frag = nfrag;
1014 *fragp = frag;
1015 }
1016
1017 if (end < len) {
1018 offset = end;
1019 continue;
1020 }
1021
1022 if (end > len &&
1023 unlikely((err = pskb_trim(frag, len - offset))))
1024 return err;
1025
1026 if (frag->next)
1027 skb_drop_list(&frag->next);
1028 break;
1029 }
1030
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001031done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001032 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 skb->data_len -= skb->len - len;
1034 skb->len = len;
1035 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001036 skb->len = len;
1037 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001038 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041 return 0;
1042}
1043
1044/**
1045 * __pskb_pull_tail - advance tail of skb header
1046 * @skb: buffer to reallocate
1047 * @delta: number of bytes to advance tail
1048 *
1049 * The function makes a sense only on a fragmented &sk_buff,
1050 * it expands header moving its tail forward and copying necessary
1051 * data from fragmented part.
1052 *
1053 * &sk_buff MUST have reference count of 1.
1054 *
1055 * Returns %NULL (and &sk_buff does not change) if pull failed
1056 * or value of new tail of skb in the case of success.
1057 *
1058 * All the pointers pointing into skb header may change and must be
1059 * reloaded after call to this function.
1060 */
1061
1062/* Moves tail of skb head forward, copying data from fragmented part,
1063 * when it is necessary.
1064 * 1. It may fail due to malloc failure.
1065 * 2. It may change skb pointers.
1066 *
1067 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1068 */
1069unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1070{
1071 /* If skb has not enough free space at tail, get new one
1072 * plus 128 bytes for future expansions. If we have enough
1073 * room at tail, reallocate without expansion only if skb is cloned.
1074 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001075 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (eat > 0 || skb_cloned(skb)) {
1078 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1079 GFP_ATOMIC))
1080 return NULL;
1081 }
1082
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001083 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 BUG();
1085
1086 /* Optimization: no fragments, no reasons to preestimate
1087 * size of pulled pages. Superb.
1088 */
1089 if (!skb_shinfo(skb)->frag_list)
1090 goto pull_pages;
1091
1092 /* Estimate size of pulled pages. */
1093 eat = delta;
1094 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1095 if (skb_shinfo(skb)->frags[i].size >= eat)
1096 goto pull_pages;
1097 eat -= skb_shinfo(skb)->frags[i].size;
1098 }
1099
1100 /* If we need update frag list, we are in troubles.
1101 * Certainly, it possible to add an offset to skb data,
1102 * but taking into account that pulling is expected to
1103 * be very rare operation, it is worth to fight against
1104 * further bloating skb head and crucify ourselves here instead.
1105 * Pure masohism, indeed. 8)8)
1106 */
1107 if (eat) {
1108 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1109 struct sk_buff *clone = NULL;
1110 struct sk_buff *insp = NULL;
1111
1112 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001113 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (list->len <= eat) {
1116 /* Eaten as whole. */
1117 eat -= list->len;
1118 list = list->next;
1119 insp = list;
1120 } else {
1121 /* Eaten partially. */
1122
1123 if (skb_shared(list)) {
1124 /* Sucks! We need to fork list. :-( */
1125 clone = skb_clone(list, GFP_ATOMIC);
1126 if (!clone)
1127 return NULL;
1128 insp = list->next;
1129 list = clone;
1130 } else {
1131 /* This may be pulled without
1132 * problems. */
1133 insp = list;
1134 }
1135 if (!pskb_pull(list, eat)) {
1136 if (clone)
1137 kfree_skb(clone);
1138 return NULL;
1139 }
1140 break;
1141 }
1142 } while (eat);
1143
1144 /* Free pulled out fragments. */
1145 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1146 skb_shinfo(skb)->frag_list = list->next;
1147 kfree_skb(list);
1148 }
1149 /* And insert new clone at head. */
1150 if (clone) {
1151 clone->next = list;
1152 skb_shinfo(skb)->frag_list = clone;
1153 }
1154 }
1155 /* Success! Now we may commit changes to skb data. */
1156
1157pull_pages:
1158 eat = delta;
1159 k = 0;
1160 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1161 if (skb_shinfo(skb)->frags[i].size <= eat) {
1162 put_page(skb_shinfo(skb)->frags[i].page);
1163 eat -= skb_shinfo(skb)->frags[i].size;
1164 } else {
1165 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1166 if (eat) {
1167 skb_shinfo(skb)->frags[k].page_offset += eat;
1168 skb_shinfo(skb)->frags[k].size -= eat;
1169 eat = 0;
1170 }
1171 k++;
1172 }
1173 }
1174 skb_shinfo(skb)->nr_frags = k;
1175
1176 skb->tail += delta;
1177 skb->data_len -= delta;
1178
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001179 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182/* Copy some data bits from skb to kernel buffer. */
1183
1184int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1185{
1186 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001187 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (offset > (int)skb->len - len)
1190 goto fault;
1191
1192 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001193 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (copy > len)
1195 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001196 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if ((len -= copy) == 0)
1198 return 0;
1199 offset += copy;
1200 to += copy;
1201 }
1202
1203 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001204 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001206 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001207
1208 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if ((copy = end - offset) > 0) {
1210 u8 *vaddr;
1211
1212 if (copy > len)
1213 copy = len;
1214
1215 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1216 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001217 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1218 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 kunmap_skb_frag(vaddr);
1220
1221 if ((len -= copy) == 0)
1222 return 0;
1223 offset += copy;
1224 to += copy;
1225 }
David S. Miller1a028e52007-04-27 15:21:23 -07001226 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
1229 if (skb_shinfo(skb)->frag_list) {
1230 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1231
1232 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001233 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001235 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001236
1237 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if ((copy = end - offset) > 0) {
1239 if (copy > len)
1240 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001241 if (skb_copy_bits(list, offset - start,
1242 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto fault;
1244 if ((len -= copy) == 0)
1245 return 0;
1246 offset += copy;
1247 to += copy;
1248 }
David S. Miller1a028e52007-04-27 15:21:23 -07001249 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 }
1252 if (!len)
1253 return 0;
1254
1255fault:
1256 return -EFAULT;
1257}
1258
Jens Axboe9c55e012007-11-06 23:30:13 -08001259/*
1260 * Callback from splice_to_pipe(), if we need to release some pages
1261 * at the end of the spd in case we error'ed out in filling the pipe.
1262 */
1263static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1264{
1265 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1266
1267 kfree_skb(skb);
1268}
1269
1270/*
1271 * Fill page/offset/length into spd, if it can hold more pages.
1272 */
1273static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1274 unsigned int len, unsigned int offset,
1275 struct sk_buff *skb)
1276{
1277 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1278 return 1;
1279
1280 spd->pages[spd->nr_pages] = page;
1281 spd->partial[spd->nr_pages].len = len;
1282 spd->partial[spd->nr_pages].offset = offset;
1283 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1284 spd->nr_pages++;
1285 return 0;
1286}
1287
Octavian Purdila2870c432008-07-15 00:49:11 -07001288static inline void __segment_seek(struct page **page, unsigned int *poff,
1289 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001290{
Octavian Purdila2870c432008-07-15 00:49:11 -07001291 *poff += off;
1292 *page += *poff / PAGE_SIZE;
1293 *poff = *poff % PAGE_SIZE;
1294 *plen -= off;
1295}
Jens Axboe9c55e012007-11-06 23:30:13 -08001296
Octavian Purdila2870c432008-07-15 00:49:11 -07001297static inline int __splice_segment(struct page *page, unsigned int poff,
1298 unsigned int plen, unsigned int *off,
1299 unsigned int *len, struct sk_buff *skb,
1300 struct splice_pipe_desc *spd)
1301{
1302 if (!*len)
1303 return 1;
1304
1305 /* skip this segment if already processed */
1306 if (*off >= plen) {
1307 *off -= plen;
1308 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001309 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001310
Octavian Purdila2870c432008-07-15 00:49:11 -07001311 /* ignore any bits we already processed */
1312 if (*off) {
1313 __segment_seek(&page, &poff, &plen, *off);
1314 *off = 0;
1315 }
1316
1317 do {
1318 unsigned int flen = min(*len, plen);
1319
1320 /* the linear region may spread across several pages */
1321 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1322
1323 if (spd_fill_page(spd, page, flen, poff, skb))
1324 return 1;
1325
1326 __segment_seek(&page, &poff, &plen, flen);
1327 *len -= flen;
1328
1329 } while (*len && plen);
1330
1331 return 0;
1332}
1333
1334/*
1335 * Map linear and fragment data from the skb to spd. It reports failure if the
1336 * pipe is full or if we already spliced the requested length.
1337 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001338static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Octavian Purdila2870c432008-07-15 00:49:11 -07001339 unsigned int *len,
1340 struct splice_pipe_desc *spd)
1341{
1342 int seg;
1343
Jens Axboe9c55e012007-11-06 23:30:13 -08001344 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001345 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001346 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001347 if (__splice_segment(virt_to_page(skb->data),
1348 (unsigned long) skb->data & (PAGE_SIZE - 1),
1349 skb_headlen(skb),
1350 offset, len, skb, spd))
1351 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001352
1353 /*
1354 * then map the fragments
1355 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001356 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1357 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1358
Octavian Purdila2870c432008-07-15 00:49:11 -07001359 if (__splice_segment(f->page, f->page_offset, f->size,
1360 offset, len, skb, spd))
1361 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001362 }
1363
Octavian Purdila2870c432008-07-15 00:49:11 -07001364 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001365}
1366
1367/*
1368 * Map data from the skb to a pipe. Should handle both the linear part,
1369 * the fragments, and the frag list. It does NOT handle frag lists within
1370 * the frag list, if such a thing exists. We'd probably need to recurse to
1371 * handle that cleanly.
1372 */
1373int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1374 struct pipe_inode_info *pipe, unsigned int tlen,
1375 unsigned int flags)
1376{
1377 struct partial_page partial[PIPE_BUFFERS];
1378 struct page *pages[PIPE_BUFFERS];
1379 struct splice_pipe_desc spd = {
1380 .pages = pages,
1381 .partial = partial,
1382 .flags = flags,
1383 .ops = &sock_pipe_buf_ops,
1384 .spd_release = sock_spd_release,
1385 };
1386 struct sk_buff *skb;
1387
1388 /*
1389 * I'd love to avoid the clone here, but tcp_read_sock()
1390 * ignores reference counts and unconditonally kills the sk_buff
1391 * on return from the actor.
1392 */
1393 skb = skb_clone(__skb, GFP_KERNEL);
1394 if (unlikely(!skb))
1395 return -ENOMEM;
1396
1397 /*
1398 * __skb_splice_bits() only fails if the output has no room left,
1399 * so no point in going over the frag_list for the error case.
1400 */
1401 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1402 goto done;
1403 else if (!tlen)
1404 goto done;
1405
1406 /*
1407 * now see if we have a frag_list to map
1408 */
1409 if (skb_shinfo(skb)->frag_list) {
1410 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1411
1412 for (; list && tlen; list = list->next) {
1413 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1414 break;
1415 }
1416 }
1417
1418done:
1419 /*
1420 * drop our reference to the clone, the pipe consumption will
1421 * drop the rest.
1422 */
1423 kfree_skb(skb);
1424
1425 if (spd.nr_pages) {
1426 int ret;
Octavian Purdila293ad602008-06-04 15:45:58 -07001427 struct sock *sk = __skb->sk;
Jens Axboe9c55e012007-11-06 23:30:13 -08001428
1429 /*
1430 * Drop the socket lock, otherwise we have reverse
1431 * locking dependencies between sk_lock and i_mutex
1432 * here as compared to sendfile(). We enter here
1433 * with the socket lock held, and splice_to_pipe() will
1434 * grab the pipe inode lock. For sendfile() emulation,
1435 * we call into ->sendpage() with the i_mutex lock held
1436 * and networking will grab the socket lock.
1437 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001438 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001439 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001440 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001441 return ret;
1442 }
1443
1444 return 0;
1445}
1446
Herbert Xu357b40a2005-04-19 22:30:14 -07001447/**
1448 * skb_store_bits - store bits from kernel buffer to skb
1449 * @skb: destination buffer
1450 * @offset: offset in destination
1451 * @from: source buffer
1452 * @len: number of bytes to copy
1453 *
1454 * Copy the specified number of bytes from the source buffer to the
1455 * destination skb. This function handles all the messy bits of
1456 * traversing fragment lists and such.
1457 */
1458
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001459int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001460{
1461 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001462 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001463
1464 if (offset > (int)skb->len - len)
1465 goto fault;
1466
David S. Miller1a028e52007-04-27 15:21:23 -07001467 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001468 if (copy > len)
1469 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001470 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001471 if ((len -= copy) == 0)
1472 return 0;
1473 offset += copy;
1474 from += copy;
1475 }
1476
1477 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1478 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001479 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001480
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001481 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001482
1483 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001484 if ((copy = end - offset) > 0) {
1485 u8 *vaddr;
1486
1487 if (copy > len)
1488 copy = len;
1489
1490 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001491 memcpy(vaddr + frag->page_offset + offset - start,
1492 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001493 kunmap_skb_frag(vaddr);
1494
1495 if ((len -= copy) == 0)
1496 return 0;
1497 offset += copy;
1498 from += copy;
1499 }
David S. Miller1a028e52007-04-27 15:21:23 -07001500 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001501 }
1502
1503 if (skb_shinfo(skb)->frag_list) {
1504 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1505
1506 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001507 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001508
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001509 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001510
1511 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001512 if ((copy = end - offset) > 0) {
1513 if (copy > len)
1514 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001515 if (skb_store_bits(list, offset - start,
1516 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001517 goto fault;
1518 if ((len -= copy) == 0)
1519 return 0;
1520 offset += copy;
1521 from += copy;
1522 }
David S. Miller1a028e52007-04-27 15:21:23 -07001523 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001524 }
1525 }
1526 if (!len)
1527 return 0;
1528
1529fault:
1530 return -EFAULT;
1531}
1532
1533EXPORT_SYMBOL(skb_store_bits);
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/* Checksum skb data. */
1536
Al Viro2bbbc862006-11-14 21:37:14 -08001537__wsum skb_checksum(const struct sk_buff *skb, int offset,
1538 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
David S. Miller1a028e52007-04-27 15:21:23 -07001540 int start = skb_headlen(skb);
1541 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int pos = 0;
1543
1544 /* Checksum header. */
1545 if (copy > 0) {
1546 if (copy > len)
1547 copy = len;
1548 csum = csum_partial(skb->data + offset, copy, csum);
1549 if ((len -= copy) == 0)
1550 return csum;
1551 offset += copy;
1552 pos = copy;
1553 }
1554
1555 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001556 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001558 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001559
1560 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001562 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 u8 *vaddr;
1564 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1565
1566 if (copy > len)
1567 copy = len;
1568 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001569 csum2 = csum_partial(vaddr + frag->page_offset +
1570 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 kunmap_skb_frag(vaddr);
1572 csum = csum_block_add(csum, csum2, pos);
1573 if (!(len -= copy))
1574 return csum;
1575 offset += copy;
1576 pos += copy;
1577 }
David S. Miller1a028e52007-04-27 15:21:23 -07001578 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
1581 if (skb_shinfo(skb)->frag_list) {
1582 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1583
1584 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001585 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001587 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001588
1589 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001591 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (copy > len)
1593 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001594 csum2 = skb_checksum(list, offset - start,
1595 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 csum = csum_block_add(csum, csum2, pos);
1597 if ((len -= copy) == 0)
1598 return csum;
1599 offset += copy;
1600 pos += copy;
1601 }
David S. Miller1a028e52007-04-27 15:21:23 -07001602 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001605 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return csum;
1608}
1609
1610/* Both of above in one bottle. */
1611
Al Viro81d77662006-11-14 21:37:33 -08001612__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1613 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
David S. Miller1a028e52007-04-27 15:21:23 -07001615 int start = skb_headlen(skb);
1616 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int pos = 0;
1618
1619 /* Copy header. */
1620 if (copy > 0) {
1621 if (copy > len)
1622 copy = len;
1623 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1624 copy, csum);
1625 if ((len -= copy) == 0)
1626 return csum;
1627 offset += copy;
1628 to += copy;
1629 pos = copy;
1630 }
1631
1632 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001633 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001635 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001636
1637 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001639 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 u8 *vaddr;
1641 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1642
1643 if (copy > len)
1644 copy = len;
1645 vaddr = kmap_skb_frag(frag);
1646 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001647 frag->page_offset +
1648 offset - start, to,
1649 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 kunmap_skb_frag(vaddr);
1651 csum = csum_block_add(csum, csum2, pos);
1652 if (!(len -= copy))
1653 return csum;
1654 offset += copy;
1655 to += copy;
1656 pos += copy;
1657 }
David S. Miller1a028e52007-04-27 15:21:23 -07001658 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
1661 if (skb_shinfo(skb)->frag_list) {
1662 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1663
1664 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001665 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001666 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001668 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001669
1670 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if ((copy = end - offset) > 0) {
1672 if (copy > len)
1673 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001674 csum2 = skb_copy_and_csum_bits(list,
1675 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 to, copy, 0);
1677 csum = csum_block_add(csum, csum2, pos);
1678 if ((len -= copy) == 0)
1679 return csum;
1680 offset += copy;
1681 to += copy;
1682 pos += copy;
1683 }
David S. Miller1a028e52007-04-27 15:21:23 -07001684 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001687 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return csum;
1689}
1690
1691void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1692{
Al Virod3bc23e2006-11-14 21:24:49 -08001693 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 long csstart;
1695
Patrick McHardy84fa7932006-08-29 16:44:56 -07001696 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001697 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 else
1699 csstart = skb_headlen(skb);
1700
Kris Katterjohn09a62662006-01-08 22:24:28 -08001701 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001703 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 csum = 0;
1706 if (csstart != skb->len)
1707 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1708 skb->len - csstart, 0);
1709
Patrick McHardy84fa7932006-08-29 16:44:56 -07001710 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001711 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Al Virod3bc23e2006-11-14 21:24:49 -08001713 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715}
1716
1717/**
1718 * skb_dequeue - remove from the head of the queue
1719 * @list: list to dequeue from
1720 *
1721 * Remove the head of the list. The list lock is taken so the function
1722 * may be used safely with other locking list functions. The head item is
1723 * returned or %NULL if the list is empty.
1724 */
1725
1726struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1727{
1728 unsigned long flags;
1729 struct sk_buff *result;
1730
1731 spin_lock_irqsave(&list->lock, flags);
1732 result = __skb_dequeue(list);
1733 spin_unlock_irqrestore(&list->lock, flags);
1734 return result;
1735}
1736
1737/**
1738 * skb_dequeue_tail - remove from the tail of the queue
1739 * @list: list to dequeue from
1740 *
1741 * Remove the tail of the list. The list lock is taken so the function
1742 * may be used safely with other locking list functions. The tail item is
1743 * returned or %NULL if the list is empty.
1744 */
1745struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1746{
1747 unsigned long flags;
1748 struct sk_buff *result;
1749
1750 spin_lock_irqsave(&list->lock, flags);
1751 result = __skb_dequeue_tail(list);
1752 spin_unlock_irqrestore(&list->lock, flags);
1753 return result;
1754}
1755
1756/**
1757 * skb_queue_purge - empty a list
1758 * @list: list to empty
1759 *
1760 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1761 * the list and one reference dropped. This function takes the list
1762 * lock and is atomic with respect to other list locking functions.
1763 */
1764void skb_queue_purge(struct sk_buff_head *list)
1765{
1766 struct sk_buff *skb;
1767 while ((skb = skb_dequeue(list)) != NULL)
1768 kfree_skb(skb);
1769}
1770
1771/**
1772 * skb_queue_head - queue a buffer at the list head
1773 * @list: list to use
1774 * @newsk: buffer to queue
1775 *
1776 * Queue a buffer at the start of the list. This function takes the
1777 * list lock and can be used safely with other locking &sk_buff functions
1778 * safely.
1779 *
1780 * A buffer cannot be placed on two lists at the same time.
1781 */
1782void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1783{
1784 unsigned long flags;
1785
1786 spin_lock_irqsave(&list->lock, flags);
1787 __skb_queue_head(list, newsk);
1788 spin_unlock_irqrestore(&list->lock, flags);
1789}
1790
1791/**
1792 * skb_queue_tail - queue a buffer at the list tail
1793 * @list: list to use
1794 * @newsk: buffer to queue
1795 *
1796 * Queue a buffer at the tail of the list. This function takes the
1797 * list lock and can be used safely with other locking &sk_buff functions
1798 * safely.
1799 *
1800 * A buffer cannot be placed on two lists at the same time.
1801 */
1802void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1803{
1804 unsigned long flags;
1805
1806 spin_lock_irqsave(&list->lock, flags);
1807 __skb_queue_tail(list, newsk);
1808 spin_unlock_irqrestore(&list->lock, flags);
1809}
David S. Miller8728b832005-08-09 19:25:21 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/**
1812 * skb_unlink - remove a buffer from a list
1813 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001814 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 *
David S. Miller8728b832005-08-09 19:25:21 -07001816 * Remove a packet from a list. The list locks are taken and this
1817 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 *
David S. Miller8728b832005-08-09 19:25:21 -07001819 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
David S. Miller8728b832005-08-09 19:25:21 -07001821void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
David S. Miller8728b832005-08-09 19:25:21 -07001823 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
David S. Miller8728b832005-08-09 19:25:21 -07001825 spin_lock_irqsave(&list->lock, flags);
1826 __skb_unlink(skb, list);
1827 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830/**
1831 * skb_append - append a buffer
1832 * @old: buffer to insert after
1833 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001834 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *
1836 * Place a packet after a given packet in a list. The list locks are taken
1837 * and this function is atomic with respect to other list locked calls.
1838 * A buffer cannot be placed on two lists at the same time.
1839 */
David S. Miller8728b832005-08-09 19:25:21 -07001840void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 unsigned long flags;
1843
David S. Miller8728b832005-08-09 19:25:21 -07001844 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001845 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001846 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849
1850/**
1851 * skb_insert - insert a buffer
1852 * @old: buffer to insert before
1853 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001854 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
David S. Miller8728b832005-08-09 19:25:21 -07001856 * Place a packet before a given packet in a list. The list locks are
1857 * taken and this function is atomic with respect to other list locked
1858 * calls.
1859 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 * A buffer cannot be placed on two lists at the same time.
1861 */
David S. Miller8728b832005-08-09 19:25:21 -07001862void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 unsigned long flags;
1865
David S. Miller8728b832005-08-09 19:25:21 -07001866 spin_lock_irqsave(&list->lock, flags);
1867 __skb_insert(newsk, old->prev, old, list);
1868 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871static inline void skb_split_inside_header(struct sk_buff *skb,
1872 struct sk_buff* skb1,
1873 const u32 len, const int pos)
1874{
1875 int i;
1876
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001877 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1878 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* And move data appendix as is. */
1880 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1881 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1882
1883 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1884 skb_shinfo(skb)->nr_frags = 0;
1885 skb1->data_len = skb->data_len;
1886 skb1->len += skb1->data_len;
1887 skb->data_len = 0;
1888 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001889 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
1892static inline void skb_split_no_header(struct sk_buff *skb,
1893 struct sk_buff* skb1,
1894 const u32 len, int pos)
1895{
1896 int i, k = 0;
1897 const int nfrags = skb_shinfo(skb)->nr_frags;
1898
1899 skb_shinfo(skb)->nr_frags = 0;
1900 skb1->len = skb1->data_len = skb->len - len;
1901 skb->len = len;
1902 skb->data_len = len - pos;
1903
1904 for (i = 0; i < nfrags; i++) {
1905 int size = skb_shinfo(skb)->frags[i].size;
1906
1907 if (pos + size > len) {
1908 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1909
1910 if (pos < len) {
1911 /* Split frag.
1912 * We have two variants in this case:
1913 * 1. Move all the frag to the second
1914 * part, if it is possible. F.e.
1915 * this approach is mandatory for TUX,
1916 * where splitting is expensive.
1917 * 2. Split is accurately. We make this.
1918 */
1919 get_page(skb_shinfo(skb)->frags[i].page);
1920 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1921 skb_shinfo(skb1)->frags[0].size -= len - pos;
1922 skb_shinfo(skb)->frags[i].size = len - pos;
1923 skb_shinfo(skb)->nr_frags++;
1924 }
1925 k++;
1926 } else
1927 skb_shinfo(skb)->nr_frags++;
1928 pos += size;
1929 }
1930 skb_shinfo(skb1)->nr_frags = k;
1931}
1932
1933/**
1934 * skb_split - Split fragmented skb to two parts at length len.
1935 * @skb: the buffer to split
1936 * @skb1: the buffer to receive the second part
1937 * @len: new length for skb
1938 */
1939void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1940{
1941 int pos = skb_headlen(skb);
1942
1943 if (len < pos) /* Split line is inside header. */
1944 skb_split_inside_header(skb, skb1, len, pos);
1945 else /* Second chunk has no header, nothing to copy. */
1946 skb_split_no_header(skb, skb1, len, pos);
1947}
1948
Thomas Graf677e90e2005-06-23 20:59:51 -07001949/**
1950 * skb_prepare_seq_read - Prepare a sequential read of skb data
1951 * @skb: the buffer to read
1952 * @from: lower offset of data to be read
1953 * @to: upper offset of data to be read
1954 * @st: state variable
1955 *
1956 * Initializes the specified state variable. Must be called before
1957 * invoking skb_seq_read() for the first time.
1958 */
1959void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1960 unsigned int to, struct skb_seq_state *st)
1961{
1962 st->lower_offset = from;
1963 st->upper_offset = to;
1964 st->root_skb = st->cur_skb = skb;
1965 st->frag_idx = st->stepped_offset = 0;
1966 st->frag_data = NULL;
1967}
1968
1969/**
1970 * skb_seq_read - Sequentially read skb data
1971 * @consumed: number of bytes consumed by the caller so far
1972 * @data: destination pointer for data to be returned
1973 * @st: state variable
1974 *
1975 * Reads a block of skb data at &consumed relative to the
1976 * lower offset specified to skb_prepare_seq_read(). Assigns
1977 * the head of the data block to &data and returns the length
1978 * of the block or 0 if the end of the skb data or the upper
1979 * offset has been reached.
1980 *
1981 * The caller is not required to consume all of the data
1982 * returned, i.e. &consumed is typically set to the number
1983 * of bytes already consumed and the next call to
1984 * skb_seq_read() will return the remaining part of the block.
1985 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001986 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07001987 * this limitation is the cost for zerocopy seqeuental
1988 * reads of potentially non linear data.
1989 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08001990 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07001991 * at the moment, state->root_skb could be replaced with
1992 * a stack for this purpose.
1993 */
1994unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1995 struct skb_seq_state *st)
1996{
1997 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1998 skb_frag_t *frag;
1999
2000 if (unlikely(abs_offset >= st->upper_offset))
2001 return 0;
2002
2003next_skb:
2004 block_limit = skb_headlen(st->cur_skb);
2005
2006 if (abs_offset < block_limit) {
2007 *data = st->cur_skb->data + abs_offset;
2008 return block_limit - abs_offset;
2009 }
2010
2011 if (st->frag_idx == 0 && !st->frag_data)
2012 st->stepped_offset += skb_headlen(st->cur_skb);
2013
2014 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2015 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2016 block_limit = frag->size + st->stepped_offset;
2017
2018 if (abs_offset < block_limit) {
2019 if (!st->frag_data)
2020 st->frag_data = kmap_skb_frag(frag);
2021
2022 *data = (u8 *) st->frag_data + frag->page_offset +
2023 (abs_offset - st->stepped_offset);
2024
2025 return block_limit - abs_offset;
2026 }
2027
2028 if (st->frag_data) {
2029 kunmap_skb_frag(st->frag_data);
2030 st->frag_data = NULL;
2031 }
2032
2033 st->frag_idx++;
2034 st->stepped_offset += frag->size;
2035 }
2036
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002037 if (st->frag_data) {
2038 kunmap_skb_frag(st->frag_data);
2039 st->frag_data = NULL;
2040 }
2041
Thomas Graf677e90e2005-06-23 20:59:51 -07002042 if (st->cur_skb->next) {
2043 st->cur_skb = st->cur_skb->next;
2044 st->frag_idx = 0;
2045 goto next_skb;
2046 } else if (st->root_skb == st->cur_skb &&
2047 skb_shinfo(st->root_skb)->frag_list) {
2048 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2049 goto next_skb;
2050 }
2051
2052 return 0;
2053}
2054
2055/**
2056 * skb_abort_seq_read - Abort a sequential read of skb data
2057 * @st: state variable
2058 *
2059 * Must be called if skb_seq_read() was not called until it
2060 * returned 0.
2061 */
2062void skb_abort_seq_read(struct skb_seq_state *st)
2063{
2064 if (st->frag_data)
2065 kunmap_skb_frag(st->frag_data);
2066}
2067
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002068#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2069
2070static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2071 struct ts_config *conf,
2072 struct ts_state *state)
2073{
2074 return skb_seq_read(offset, text, TS_SKB_CB(state));
2075}
2076
2077static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2078{
2079 skb_abort_seq_read(TS_SKB_CB(state));
2080}
2081
2082/**
2083 * skb_find_text - Find a text pattern in skb data
2084 * @skb: the buffer to look in
2085 * @from: search offset
2086 * @to: search limit
2087 * @config: textsearch configuration
2088 * @state: uninitialized textsearch state variable
2089 *
2090 * Finds a pattern in the skb data according to the specified
2091 * textsearch configuration. Use textsearch_next() to retrieve
2092 * subsequent occurrences of the pattern. Returns the offset
2093 * to the first occurrence or UINT_MAX if no match was found.
2094 */
2095unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2096 unsigned int to, struct ts_config *config,
2097 struct ts_state *state)
2098{
Phil Oesterf72b9482006-06-26 00:00:57 -07002099 unsigned int ret;
2100
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002101 config->get_next_block = skb_ts_get_next_block;
2102 config->finish = skb_ts_finish;
2103
2104 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2105
Phil Oesterf72b9482006-06-26 00:00:57 -07002106 ret = textsearch_find(config, state);
2107 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002108}
2109
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002110/**
2111 * skb_append_datato_frags: - append the user data to a skb
2112 * @sk: sock structure
2113 * @skb: skb structure to be appened with user data.
2114 * @getfrag: call back function to be used for getting the user data
2115 * @from: pointer to user message iov
2116 * @length: length of the iov message
2117 *
2118 * Description: This procedure append the user data in the fragment part
2119 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2120 */
2121int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002122 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002123 int len, int odd, struct sk_buff *skb),
2124 void *from, int length)
2125{
2126 int frg_cnt = 0;
2127 skb_frag_t *frag = NULL;
2128 struct page *page = NULL;
2129 int copy, left;
2130 int offset = 0;
2131 int ret;
2132
2133 do {
2134 /* Return error if we don't have space for new frag */
2135 frg_cnt = skb_shinfo(skb)->nr_frags;
2136 if (frg_cnt >= MAX_SKB_FRAGS)
2137 return -EFAULT;
2138
2139 /* allocate a new page for next frag */
2140 page = alloc_pages(sk->sk_allocation, 0);
2141
2142 /* If alloc_page fails just return failure and caller will
2143 * free previous allocated pages by doing kfree_skb()
2144 */
2145 if (page == NULL)
2146 return -ENOMEM;
2147
2148 /* initialize the next frag */
2149 sk->sk_sndmsg_page = page;
2150 sk->sk_sndmsg_off = 0;
2151 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2152 skb->truesize += PAGE_SIZE;
2153 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2154
2155 /* get the new initialized frag */
2156 frg_cnt = skb_shinfo(skb)->nr_frags;
2157 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2158
2159 /* copy the user data to page */
2160 left = PAGE_SIZE - frag->page_offset;
2161 copy = (length > left)? left : length;
2162
2163 ret = getfrag(from, (page_address(frag->page) +
2164 frag->page_offset + frag->size),
2165 offset, copy, 0, skb);
2166 if (ret < 0)
2167 return -EFAULT;
2168
2169 /* copy was successful so update the size parameters */
2170 sk->sk_sndmsg_off += copy;
2171 frag->size += copy;
2172 skb->len += copy;
2173 skb->data_len += copy;
2174 offset += copy;
2175 length -= copy;
2176
2177 } while (length > 0);
2178
2179 return 0;
2180}
2181
Herbert Xucbb042f2006-03-20 22:43:56 -08002182/**
2183 * skb_pull_rcsum - pull skb and update receive checksum
2184 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002185 * @len: length of data pulled
2186 *
2187 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002188 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002189 * receive path processing instead of skb_pull unless you know
2190 * that the checksum difference is zero (e.g., a valid IP header)
2191 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002192 */
2193unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2194{
2195 BUG_ON(len > skb->len);
2196 skb->len -= len;
2197 BUG_ON(skb->len < skb->data_len);
2198 skb_postpull_rcsum(skb, skb->data, len);
2199 return skb->data += len;
2200}
2201
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002202EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2203
Herbert Xuf4c50d92006-06-22 03:02:40 -07002204/**
2205 * skb_segment - Perform protocol segmentation on skb.
2206 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002207 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002208 *
2209 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002210 * a pointer to the first in a list of new skbs for the segments.
2211 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002212 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002213struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002214{
2215 struct sk_buff *segs = NULL;
2216 struct sk_buff *tail = NULL;
2217 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002218 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002219 unsigned int offset = doffset;
2220 unsigned int headroom;
2221 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002222 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002223 int nfrags = skb_shinfo(skb)->nr_frags;
2224 int err = -ENOMEM;
2225 int i = 0;
2226 int pos;
2227
2228 __skb_push(skb, doffset);
2229 headroom = skb_headroom(skb);
2230 pos = skb_headlen(skb);
2231
2232 do {
2233 struct sk_buff *nskb;
2234 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002235 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002236 int k;
2237 int size;
2238
2239 len = skb->len - offset;
2240 if (len > mss)
2241 len = mss;
2242
2243 hsize = skb_headlen(skb) - offset;
2244 if (hsize < 0)
2245 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002246 if (hsize > len || !sg)
2247 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002248
Herbert Xuc8884ed2006-10-29 15:59:41 -08002249 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002250 if (unlikely(!nskb))
2251 goto err;
2252
2253 if (segs)
2254 tail->next = nskb;
2255 else
2256 segs = nskb;
2257 tail = nskb;
2258
Herbert Xu6f85a122008-08-15 14:55:02 -07002259 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002260 nskb->mac_len = skb->mac_len;
2261
2262 skb_reserve(nskb, headroom);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002263 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002264 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002265 nskb->transport_header = (nskb->network_header +
2266 skb_network_header_len(skb));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002267 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2268 doffset);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002269 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002270 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002271 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2272 skb_put(nskb, len),
2273 len, 0);
2274 continue;
2275 }
2276
2277 frag = skb_shinfo(nskb)->frags;
2278 k = 0;
2279
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002280 skb_copy_from_linear_data_offset(skb, offset,
2281 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002282
2283 while (pos < offset + len) {
2284 BUG_ON(i >= nfrags);
2285
2286 *frag = skb_shinfo(skb)->frags[i];
2287 get_page(frag->page);
2288 size = frag->size;
2289
2290 if (pos < offset) {
2291 frag->page_offset += offset - pos;
2292 frag->size -= offset - pos;
2293 }
2294
2295 k++;
2296
2297 if (pos + size <= offset + len) {
2298 i++;
2299 pos += size;
2300 } else {
2301 frag->size -= pos + size - (offset + len);
2302 break;
2303 }
2304
2305 frag++;
2306 }
2307
2308 skb_shinfo(nskb)->nr_frags = k;
2309 nskb->data_len = len - hsize;
2310 nskb->len += nskb->data_len;
2311 nskb->truesize += nskb->data_len;
2312 } while ((offset += len) < skb->len);
2313
2314 return segs;
2315
2316err:
2317 while ((skb = segs)) {
2318 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002319 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002320 }
2321 return ERR_PTR(err);
2322}
2323
2324EXPORT_SYMBOL_GPL(skb_segment);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326void __init skb_init(void)
2327{
2328 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2329 sizeof(struct sk_buff),
2330 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002331 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002332 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002333 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2334 (2*sizeof(struct sk_buff)) +
2335 sizeof(atomic_t),
2336 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002337 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002338 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
David Howells716ea3a2007-04-02 20:19:53 -07002341/**
2342 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2343 * @skb: Socket buffer containing the buffers to be mapped
2344 * @sg: The scatter-gather list to map into
2345 * @offset: The offset into the buffer's contents to start mapping
2346 * @len: Length of buffer space to be mapped
2347 *
2348 * Fill the specified scatter-gather list with mappings/pointers into a
2349 * region of the buffer space attached to a socket buffer.
2350 */
David S. Miller51c739d2007-10-30 21:29:29 -07002351static int
2352__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002353{
David S. Miller1a028e52007-04-27 15:21:23 -07002354 int start = skb_headlen(skb);
2355 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002356 int elt = 0;
2357
2358 if (copy > 0) {
2359 if (copy > len)
2360 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002361 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002362 elt++;
2363 if ((len -= copy) == 0)
2364 return elt;
2365 offset += copy;
2366 }
2367
2368 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002369 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002370
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002371 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002372
2373 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002374 if ((copy = end - offset) > 0) {
2375 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2376
2377 if (copy > len)
2378 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002379 sg_set_page(&sg[elt], frag->page, copy,
2380 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002381 elt++;
2382 if (!(len -= copy))
2383 return elt;
2384 offset += copy;
2385 }
David S. Miller1a028e52007-04-27 15:21:23 -07002386 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002387 }
2388
2389 if (skb_shinfo(skb)->frag_list) {
2390 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2391
2392 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002393 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002394
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002395 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002396
2397 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002398 if ((copy = end - offset) > 0) {
2399 if (copy > len)
2400 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002401 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2402 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002403 if ((len -= copy) == 0)
2404 return elt;
2405 offset += copy;
2406 }
David S. Miller1a028e52007-04-27 15:21:23 -07002407 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002408 }
2409 }
2410 BUG_ON(len);
2411 return elt;
2412}
2413
David S. Miller51c739d2007-10-30 21:29:29 -07002414int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2415{
2416 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2417
Jens Axboec46f2332007-10-31 12:06:37 +01002418 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002419
2420 return nsg;
2421}
2422
David Howells716ea3a2007-04-02 20:19:53 -07002423/**
2424 * skb_cow_data - Check that a socket buffer's data buffers are writable
2425 * @skb: The socket buffer to check.
2426 * @tailbits: Amount of trailing space to be added
2427 * @trailer: Returned pointer to the skb where the @tailbits space begins
2428 *
2429 * Make sure that the data buffers attached to a socket buffer are
2430 * writable. If they are not, private copies are made of the data buffers
2431 * and the socket buffer is set to use these instead.
2432 *
2433 * If @tailbits is given, make sure that there is space to write @tailbits
2434 * bytes of data beyond current end of socket buffer. @trailer will be
2435 * set to point to the skb in which this space begins.
2436 *
2437 * The number of scatterlist elements required to completely map the
2438 * COW'd and extended socket buffer will be returned.
2439 */
2440int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2441{
2442 int copyflag;
2443 int elt;
2444 struct sk_buff *skb1, **skb_p;
2445
2446 /* If skb is cloned or its head is paged, reallocate
2447 * head pulling out all the pages (pages are considered not writable
2448 * at the moment even if they are anonymous).
2449 */
2450 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2451 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2452 return -ENOMEM;
2453
2454 /* Easy case. Most of packets will go this way. */
2455 if (!skb_shinfo(skb)->frag_list) {
2456 /* A little of trouble, not enough of space for trailer.
2457 * This should not happen, when stack is tuned to generate
2458 * good frames. OK, on miss we reallocate and reserve even more
2459 * space, 128 bytes is fair. */
2460
2461 if (skb_tailroom(skb) < tailbits &&
2462 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2463 return -ENOMEM;
2464
2465 /* Voila! */
2466 *trailer = skb;
2467 return 1;
2468 }
2469
2470 /* Misery. We are in troubles, going to mincer fragments... */
2471
2472 elt = 1;
2473 skb_p = &skb_shinfo(skb)->frag_list;
2474 copyflag = 0;
2475
2476 while ((skb1 = *skb_p) != NULL) {
2477 int ntail = 0;
2478
2479 /* The fragment is partially pulled by someone,
2480 * this can happen on input. Copy it and everything
2481 * after it. */
2482
2483 if (skb_shared(skb1))
2484 copyflag = 1;
2485
2486 /* If the skb is the last, worry about trailer. */
2487
2488 if (skb1->next == NULL && tailbits) {
2489 if (skb_shinfo(skb1)->nr_frags ||
2490 skb_shinfo(skb1)->frag_list ||
2491 skb_tailroom(skb1) < tailbits)
2492 ntail = tailbits + 128;
2493 }
2494
2495 if (copyflag ||
2496 skb_cloned(skb1) ||
2497 ntail ||
2498 skb_shinfo(skb1)->nr_frags ||
2499 skb_shinfo(skb1)->frag_list) {
2500 struct sk_buff *skb2;
2501
2502 /* Fuck, we are miserable poor guys... */
2503 if (ntail == 0)
2504 skb2 = skb_copy(skb1, GFP_ATOMIC);
2505 else
2506 skb2 = skb_copy_expand(skb1,
2507 skb_headroom(skb1),
2508 ntail,
2509 GFP_ATOMIC);
2510 if (unlikely(skb2 == NULL))
2511 return -ENOMEM;
2512
2513 if (skb1->sk)
2514 skb_set_owner_w(skb2, skb1->sk);
2515
2516 /* Looking around. Are we still alive?
2517 * OK, link new skb, drop old one */
2518
2519 skb2->next = skb1->next;
2520 *skb_p = skb2;
2521 kfree_skb(skb1);
2522 skb1 = skb2;
2523 }
2524 elt++;
2525 *trailer = skb1;
2526 skb_p = &skb1->next;
2527 }
2528
2529 return elt;
2530}
2531
Rusty Russellf35d9d82008-02-04 23:49:54 -05002532/**
2533 * skb_partial_csum_set - set up and verify partial csum values for packet
2534 * @skb: the skb to set
2535 * @start: the number of bytes after skb->data to start checksumming.
2536 * @off: the offset from start to place the checksum.
2537 *
2538 * For untrusted partially-checksummed packets, we need to make sure the values
2539 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2540 *
2541 * This function checks and sets those values and skb->ip_summed: if this
2542 * returns false you should drop the packet.
2543 */
2544bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2545{
2546 if (unlikely(start > skb->len - 2) ||
2547 unlikely((int)start + off > skb->len - 2)) {
2548 if (net_ratelimit())
2549 printk(KERN_WARNING
2550 "bad partial csum: csum=%u/%u len=%u\n",
2551 start, off, skb->len);
2552 return false;
2553 }
2554 skb->ip_summed = CHECKSUM_PARTIAL;
2555 skb->csum_start = skb_headroom(skb) + start;
2556 skb->csum_offset = off;
2557 return true;
2558}
2559
Ben Hutchings4497b072008-06-19 16:22:28 -07002560void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2561{
2562 if (net_ratelimit())
2563 pr_warning("%s: received packets cannot be forwarded"
2564 " while LRO is enabled\n", skb->dev->name);
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567EXPORT_SYMBOL(___pskb_trim);
2568EXPORT_SYMBOL(__kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -08002569EXPORT_SYMBOL(kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570EXPORT_SYMBOL(__pskb_pull_tail);
David S. Millerd179cd12005-08-17 14:57:30 -07002571EXPORT_SYMBOL(__alloc_skb);
Christoph Hellwig8af27452006-07-31 22:35:23 -07002572EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573EXPORT_SYMBOL(pskb_copy);
2574EXPORT_SYMBOL(pskb_expand_head);
2575EXPORT_SYMBOL(skb_checksum);
2576EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577EXPORT_SYMBOL(skb_copy);
2578EXPORT_SYMBOL(skb_copy_and_csum_bits);
2579EXPORT_SYMBOL(skb_copy_and_csum_dev);
2580EXPORT_SYMBOL(skb_copy_bits);
2581EXPORT_SYMBOL(skb_copy_expand);
2582EXPORT_SYMBOL(skb_over_panic);
2583EXPORT_SYMBOL(skb_pad);
2584EXPORT_SYMBOL(skb_realloc_headroom);
2585EXPORT_SYMBOL(skb_under_panic);
2586EXPORT_SYMBOL(skb_dequeue);
2587EXPORT_SYMBOL(skb_dequeue_tail);
2588EXPORT_SYMBOL(skb_insert);
2589EXPORT_SYMBOL(skb_queue_purge);
2590EXPORT_SYMBOL(skb_queue_head);
2591EXPORT_SYMBOL(skb_queue_tail);
2592EXPORT_SYMBOL(skb_unlink);
2593EXPORT_SYMBOL(skb_append);
2594EXPORT_SYMBOL(skb_split);
Thomas Graf677e90e2005-06-23 20:59:51 -07002595EXPORT_SYMBOL(skb_prepare_seq_read);
2596EXPORT_SYMBOL(skb_seq_read);
2597EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002598EXPORT_SYMBOL(skb_find_text);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002599EXPORT_SYMBOL(skb_append_datato_frags);
Ben Hutchings4497b072008-06-19 16:22:28 -07002600EXPORT_SYMBOL(__skb_warn_lro_forwarding);
David Howells716ea3a2007-04-02 20:19:53 -07002601
2602EXPORT_SYMBOL_GPL(skb_to_sgvec);
2603EXPORT_SYMBOL_GPL(skb_cow_data);
Rusty Russellf35d9d82008-02-04 23:49:54 -05002604EXPORT_SYMBOL_GPL(skb_partial_csum_set);