blob: ab7d2e9f02fa87f4675da2b69350b7eca5ac4f8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080076 put_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080077}
78
79static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
80 struct pipe_buffer *buf)
81{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080082 get_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080083}
84
85static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
86 struct pipe_buffer *buf)
87{
88 return 1;
89}
90
91
92/* Pipe buffer operations for a socket. */
93static struct pipe_buf_operations sock_pipe_buf_ops = {
94 .can_merge = 0,
95 .map = generic_pipe_buf_map,
96 .unmap = generic_pipe_buf_unmap,
97 .confirm = generic_pipe_buf_confirm,
98 .release = sock_pipe_buf_release,
99 .steal = sock_pipe_buf_steal,
100 .get = sock_pipe_buf_get,
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Keep out-of-line to prevent kernel bloat.
105 * __builtin_return_address is not used because it is not always
106 * reliable.
107 */
108
109/**
110 * skb_over_panic - private function
111 * @skb: buffer
112 * @sz: size
113 * @here: address
114 *
115 * Out of line support code for skb_put(). Not user callable.
116 */
117void skb_over_panic(struct sk_buff *skb, int sz, void *here)
118{
Patrick McHardy26095452005-04-21 16:43:02 -0700119 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700120 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700121 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700122 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700123 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 BUG();
125}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800126EXPORT_SYMBOL(skb_over_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/**
129 * skb_under_panic - private function
130 * @skb: buffer
131 * @sz: size
132 * @here: address
133 *
134 * Out of line support code for skb_push(). Not user callable.
135 */
136
137void skb_under_panic(struct sk_buff *skb, int sz, void *here)
138{
Patrick McHardy26095452005-04-21 16:43:02 -0700139 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700140 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700141 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700142 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700143 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 BUG();
145}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800146EXPORT_SYMBOL(skb_under_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
David S. Millerdc6de332006-04-20 00:10:50 -0700148void skb_truesize_bug(struct sk_buff *skb)
149{
Arjan van de Ven8f480c02008-11-25 21:08:13 -0800150 WARN(net_ratelimit(), KERN_ERR "SKB BUG: Invalid truesize (%u) "
David S. Millerdc6de332006-04-20 00:10:50 -0700151 "len=%u, sizeof(sk_buff)=%Zd\n",
152 skb->truesize, skb->len, sizeof(struct sk_buff));
153}
154EXPORT_SYMBOL(skb_truesize_bug);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Allocate a new skbuff. We do this ourselves so we can fill in a few
157 * 'private' fields and also do memory statistics to find all the
158 * [BEEP] leaks.
159 *
160 */
161
162/**
David S. Millerd179cd12005-08-17 14:57:30 -0700163 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * @size: size to allocate
165 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700166 * @fclone: allocate from fclone cache instead of head cache
167 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800168 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * Allocate a new &sk_buff. The returned buffer has no headroom and a
171 * tail room of size bytes. The object has a reference count of one.
172 * The return is the buffer. On a failure the return is %NULL.
173 *
174 * Buffers may only be allocated from interrupts using a @gfp_mask of
175 * %GFP_ATOMIC.
176 */
Al Virodd0fc662005-10-07 07:46:04 +0100177struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800178 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Christoph Lametere18b8902006-12-06 20:33:20 -0800180 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800181 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct sk_buff *skb;
183 u8 *data;
184
Herbert Xu8798b3f2006-01-23 16:32:45 -0800185 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800188 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!skb)
190 goto out;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800193 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
194 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (!data)
196 goto nodata;
197
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300198 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700199 * Only clear those fields we need to clear, not those that we will
200 * actually initialise below. Hence, don't put any more fields after
201 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300202 */
203 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 skb->truesize = size + sizeof(struct sk_buff);
205 atomic_set(&skb->users, 1);
206 skb->head = data;
207 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700208 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700209 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800210 /* make sure we initialize shinfo sequentially */
211 shinfo = skb_shinfo(skb);
212 atomic_set(&shinfo->dataref, 1);
213 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700214 shinfo->gso_size = 0;
215 shinfo->gso_segs = 0;
216 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800217 shinfo->ip6_frag_id = 0;
218 shinfo->frag_list = NULL;
219
David S. Millerd179cd12005-08-17 14:57:30 -0700220 if (fclone) {
221 struct sk_buff *child = skb + 1;
222 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David S. Millerd179cd12005-08-17 14:57:30 -0700224 skb->fclone = SKB_FCLONE_ORIG;
225 atomic_set(fclone_ref, 1);
226
227 child->fclone = SKB_FCLONE_UNAVAILABLE;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out:
230 return skb;
231nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800232 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 skb = NULL;
234 goto out;
235}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800236EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700239 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
240 * @dev: network device to receive on
241 * @length: length to allocate
242 * @gfp_mask: get_free_pages mask, passed to alloc_skb
243 *
244 * Allocate a new &sk_buff and assign it a usage count of one. The
245 * buffer has unspecified headroom built in. Users should allocate
246 * the headroom they think they need without accounting for the
247 * built in space. The built in space is used for optimisations.
248 *
249 * %NULL is returned if there is no free memory.
250 */
251struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
252 unsigned int length, gfp_t gfp_mask)
253{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700254 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700255 struct sk_buff *skb;
256
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900257 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700258 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700259 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700260 skb->dev = dev;
261 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700262 return skb;
263}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800264EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Peter Zijlstra654bed12008-10-07 14:22:33 -0700266struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
267{
268 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
269 struct page *page;
270
271 page = alloc_pages_node(node, gfp_mask, 0);
272 return page;
273}
274EXPORT_SYMBOL(__netdev_alloc_page);
275
276void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
277 int size)
278{
279 skb_fill_page_desc(skb, i, page, off, size);
280 skb->len += size;
281 skb->data_len += size;
282 skb->truesize += size;
283}
284EXPORT_SYMBOL(skb_add_rx_frag);
285
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700286/**
287 * dev_alloc_skb - allocate an skbuff for receiving
288 * @length: length to allocate
289 *
290 * Allocate a new &sk_buff and assign it a usage count of one. The
291 * buffer has unspecified headroom built in. Users should allocate
292 * the headroom they think they need without accounting for the
293 * built in space. The built in space is used for optimisations.
294 *
295 * %NULL is returned if there is no free memory. Although this function
296 * allocates memory it can be called from an interrupt.
297 */
298struct sk_buff *dev_alloc_skb(unsigned int length)
299{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700300 /*
301 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700302 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700303 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700304 return __dev_alloc_skb(length, GFP_ATOMIC);
305}
306EXPORT_SYMBOL(dev_alloc_skb);
307
Herbert Xu27b437c2006-07-13 19:26:39 -0700308static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Herbert Xu27b437c2006-07-13 19:26:39 -0700310 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Herbert Xu27b437c2006-07-13 19:26:39 -0700312 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 do {
315 struct sk_buff *this = list;
316 list = list->next;
317 kfree_skb(this);
318 } while (list);
319}
320
Herbert Xu27b437c2006-07-13 19:26:39 -0700321static inline void skb_drop_fraglist(struct sk_buff *skb)
322{
323 skb_drop_list(&skb_shinfo(skb)->frag_list);
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static void skb_clone_fraglist(struct sk_buff *skb)
327{
328 struct sk_buff *list;
329
330 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
331 skb_get(list);
332}
333
Adrian Bunk5bba1712006-06-29 13:02:35 -0700334static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 if (!skb->cloned ||
337 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
338 &skb_shinfo(skb)->dataref)) {
339 if (skb_shinfo(skb)->nr_frags) {
340 int i;
341 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
342 put_page(skb_shinfo(skb)->frags[i].page);
343 }
344
345 if (skb_shinfo(skb)->frag_list)
346 skb_drop_fraglist(skb);
347
348 kfree(skb->head);
349 }
350}
351
352/*
353 * Free an skbuff by memory without cleaning the state.
354 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800355static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
David S. Millerd179cd12005-08-17 14:57:30 -0700357 struct sk_buff *other;
358 atomic_t *fclone_ref;
359
David S. Millerd179cd12005-08-17 14:57:30 -0700360 switch (skb->fclone) {
361 case SKB_FCLONE_UNAVAILABLE:
362 kmem_cache_free(skbuff_head_cache, skb);
363 break;
364
365 case SKB_FCLONE_ORIG:
366 fclone_ref = (atomic_t *) (skb + 2);
367 if (atomic_dec_and_test(fclone_ref))
368 kmem_cache_free(skbuff_fclone_cache, skb);
369 break;
370
371 case SKB_FCLONE_CLONE:
372 fclone_ref = (atomic_t *) (skb + 1);
373 other = skb - 1;
374
375 /* The clone portion is available for
376 * fast-cloning again.
377 */
378 skb->fclone = SKB_FCLONE_UNAVAILABLE;
379
380 if (atomic_dec_and_test(fclone_ref))
381 kmem_cache_free(skbuff_fclone_cache, other);
382 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700386static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 dst_release(skb->dst);
389#ifdef CONFIG_XFRM
390 secpath_put(skb->sp);
391#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700392 if (skb->destructor) {
393 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 skb->destructor(skb);
395 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800396#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700397 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 nf_conntrack_put_reasm(skb->nfct_reasm);
399#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#ifdef CONFIG_BRIDGE_NETFILTER
401 nf_bridge_put(skb->nf_bridge);
402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* XXX: IS this still necessary? - JHS */
404#ifdef CONFIG_NET_SCHED
405 skb->tc_index = 0;
406#ifdef CONFIG_NET_CLS_ACT
407 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#endif
409#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700410}
411
412/* Free everything but the sk_buff shell. */
413static void skb_release_all(struct sk_buff *skb)
414{
415 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800416 skb_release_data(skb);
417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Herbert Xu2d4baff2007-11-26 23:11:19 +0800419/**
420 * __kfree_skb - private function
421 * @skb: buffer
422 *
423 * Free an sk_buff. Release anything attached to the buffer.
424 * Clean the state. This is an internal helper function. Users should
425 * always call kfree_skb
426 */
427
428void __kfree_skb(struct sk_buff *skb)
429{
430 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree_skbmem(skb);
432}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800433EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800436 * kfree_skb - free an sk_buff
437 * @skb: buffer to free
438 *
439 * Drop a reference to the buffer and free it if the usage count has
440 * hit zero.
441 */
442void kfree_skb(struct sk_buff *skb)
443{
444 if (unlikely(!skb))
445 return;
446 if (likely(atomic_read(&skb->users) == 1))
447 smp_rmb();
448 else if (likely(!atomic_dec_and_test(&skb->users)))
449 return;
450 __kfree_skb(skb);
451}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800452EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800453
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700454/**
455 * skb_recycle_check - check if skb can be reused for receive
456 * @skb: buffer
457 * @skb_size: minimum receive buffer size
458 *
459 * Checks that the skb passed in is not shared or cloned, and
460 * that it is linear and its head portion at least as large as
461 * skb_size so that it can be recycled as a receive buffer.
462 * If these conditions are met, this function does any necessary
463 * reference count dropping and cleans up the skbuff as if it
464 * just came from __alloc_skb().
465 */
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700466int skb_recycle_check(struct sk_buff *skb, int skb_size)
467{
468 struct skb_shared_info *shinfo;
469
470 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
471 return 0;
472
473 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
474 if (skb_end_pointer(skb) - skb->head < skb_size)
475 return 0;
476
477 if (skb_shared(skb) || skb_cloned(skb))
478 return 0;
479
480 skb_release_head_state(skb);
481 shinfo = skb_shinfo(skb);
482 atomic_set(&shinfo->dataref, 1);
483 shinfo->nr_frags = 0;
484 shinfo->gso_size = 0;
485 shinfo->gso_segs = 0;
486 shinfo->gso_type = 0;
487 shinfo->ip6_frag_id = 0;
488 shinfo->frag_list = NULL;
489
490 memset(skb, 0, offsetof(struct sk_buff, tail));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700491 skb->data = skb->head + NET_SKB_PAD;
Lennert Buytenhek5cd33db2008-11-10 21:45:05 -0800492 skb_reset_tail_pointer(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700493
494 return 1;
495}
496EXPORT_SYMBOL(skb_recycle_check);
497
Herbert Xudec18812007-10-14 00:37:30 -0700498static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
499{
500 new->tstamp = old->tstamp;
501 new->dev = old->dev;
502 new->transport_header = old->transport_header;
503 new->network_header = old->network_header;
504 new->mac_header = old->mac_header;
505 new->dst = dst_clone(old->dst);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700506#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700507 new->sp = secpath_get(old->sp);
508#endif
509 memcpy(new->cb, old->cb, sizeof(old->cb));
510 new->csum_start = old->csum_start;
511 new->csum_offset = old->csum_offset;
512 new->local_df = old->local_df;
513 new->pkt_type = old->pkt_type;
514 new->ip_summed = old->ip_summed;
515 skb_copy_queue_mapping(new, old);
516 new->priority = old->priority;
517#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
518 new->ipvs_property = old->ipvs_property;
519#endif
520 new->protocol = old->protocol;
521 new->mark = old->mark;
522 __nf_copy(new, old);
523#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
524 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
525 new->nf_trace = old->nf_trace;
526#endif
527#ifdef CONFIG_NET_SCHED
528 new->tc_index = old->tc_index;
529#ifdef CONFIG_NET_CLS_ACT
530 new->tc_verd = old->tc_verd;
531#endif
532#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700533 new->vlan_tci = old->vlan_tci;
534
Herbert Xudec18812007-10-14 00:37:30 -0700535 skb_copy_secmark(new, old);
536}
537
Herbert Xue0053ec2007-10-14 00:37:52 -0700538static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#define C(x) n->x = skb->x
541
542 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700544 __copy_skb_header(n, skb);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 C(len);
547 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700548 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700549 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800550 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800553 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 C(tail);
555 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800556 C(head);
557 C(data);
558 C(truesize);
Johannes Bergd0f09802008-07-29 11:32:07 +0200559#if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
560 C(do_not_encrypt);
Sujith8b30b1f2008-10-24 09:55:27 +0530561 C(requeue);
Johannes Bergd0f09802008-07-29 11:32:07 +0200562#endif
Paul Moore02f1c892008-01-07 21:56:41 -0800563 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 atomic_inc(&(skb_shinfo(skb)->dataref));
566 skb->cloned = 1;
567
568 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700569#undef C
570}
571
572/**
573 * skb_morph - morph one skb into another
574 * @dst: the skb to receive the contents
575 * @src: the skb to supply the contents
576 *
577 * This is identical to skb_clone except that the target skb is
578 * supplied by the user.
579 *
580 * The target skb is returned upon exit.
581 */
582struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
583{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800584 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700585 return __skb_clone(dst, src);
586}
587EXPORT_SYMBOL_GPL(skb_morph);
588
589/**
590 * skb_clone - duplicate an sk_buff
591 * @skb: buffer to clone
592 * @gfp_mask: allocation priority
593 *
594 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
595 * copies share the same packet data but not structure. The new
596 * buffer has a reference count of 1. If the allocation fails the
597 * function returns %NULL otherwise the new buffer is returned.
598 *
599 * If this function is called from an interrupt gfp_mask() must be
600 * %GFP_ATOMIC.
601 */
602
603struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
604{
605 struct sk_buff *n;
606
607 n = skb + 1;
608 if (skb->fclone == SKB_FCLONE_ORIG &&
609 n->fclone == SKB_FCLONE_UNAVAILABLE) {
610 atomic_t *fclone_ref = (atomic_t *) (n + 1);
611 n->fclone = SKB_FCLONE_CLONE;
612 atomic_inc(fclone_ref);
613 } else {
614 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
615 if (!n)
616 return NULL;
617 n->fclone = SKB_FCLONE_UNAVAILABLE;
618 }
619
620 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800622EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
625{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700626#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /*
628 * Shift between the two data areas in bytes
629 */
630 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700631#endif
Herbert Xudec18812007-10-14 00:37:30 -0700632
633 __copy_skb_header(new, old);
634
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700635#ifndef NET_SKBUFF_DATA_USES_OFFSET
636 /* {transport,network,mac}_header are relative to skb->head */
637 new->transport_header += offset;
638 new->network_header += offset;
639 new->mac_header += offset;
640#endif
Herbert Xu79671682006-06-22 02:40:14 -0700641 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
642 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
643 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/**
647 * skb_copy - create private copy of an sk_buff
648 * @skb: buffer to copy
649 * @gfp_mask: allocation priority
650 *
651 * Make a copy of both an &sk_buff and its data. This is used when the
652 * caller wishes to modify the data and needs a private copy of the
653 * data to alter. Returns %NULL on failure or the pointer to the buffer
654 * on success. The returned buffer has a reference count of 1.
655 *
656 * As by-product this function converts non-linear &sk_buff to linear
657 * one, so that &sk_buff becomes completely private and caller is allowed
658 * to modify all the data of returned buffer. This means that this
659 * function is not recommended for use in circumstances when only
660 * header is going to be modified. Use pskb_copy() instead.
661 */
662
Al Virodd0fc662005-10-07 07:46:04 +0100663struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 int headerlen = skb->data - skb->head;
666 /*
667 * Allocate the copy buffer
668 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700669 struct sk_buff *n;
670#ifdef NET_SKBUFF_DATA_USES_OFFSET
671 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
672#else
673 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!n)
676 return NULL;
677
678 /* Set the data pointer */
679 skb_reserve(n, headerlen);
680 /* Set the tail pointer and length */
681 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
684 BUG();
685
686 copy_skb_header(n, skb);
687 return n;
688}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800689EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/**
692 * pskb_copy - create copy of an sk_buff with private head.
693 * @skb: buffer to copy
694 * @gfp_mask: allocation priority
695 *
696 * Make a copy of both an &sk_buff and part of its data, located
697 * in header. Fragmented data remain shared. This is used when
698 * the caller wishes to modify only header of &sk_buff and needs
699 * private copy of the header to alter. Returns %NULL on failure
700 * or the pointer to the buffer on success.
701 * The returned buffer has a reference count of 1.
702 */
703
Al Virodd0fc662005-10-07 07:46:04 +0100704struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 /*
707 * Allocate the copy buffer
708 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700709 struct sk_buff *n;
710#ifdef NET_SKBUFF_DATA_USES_OFFSET
711 n = alloc_skb(skb->end, gfp_mask);
712#else
713 n = alloc_skb(skb->end - skb->head, gfp_mask);
714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!n)
716 goto out;
717
718 /* Set the data pointer */
719 skb_reserve(n, skb->data - skb->head);
720 /* Set the tail pointer and length */
721 skb_put(n, skb_headlen(skb));
722 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300723 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Herbert Xu25f484a2006-11-07 14:57:15 -0800725 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 n->data_len = skb->data_len;
727 n->len = skb->len;
728
729 if (skb_shinfo(skb)->nr_frags) {
730 int i;
731
732 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
733 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
734 get_page(skb_shinfo(n)->frags[i].page);
735 }
736 skb_shinfo(n)->nr_frags = i;
737 }
738
739 if (skb_shinfo(skb)->frag_list) {
740 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
741 skb_clone_fraglist(n);
742 }
743
744 copy_skb_header(n, skb);
745out:
746 return n;
747}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800748EXPORT_SYMBOL(pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/**
751 * pskb_expand_head - reallocate header of &sk_buff
752 * @skb: buffer to reallocate
753 * @nhead: room to add at head
754 * @ntail: room to add at tail
755 * @gfp_mask: allocation priority
756 *
757 * Expands (or creates identical copy, if &nhead and &ntail are zero)
758 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
759 * reference count of 1. Returns zero in the case of success or error,
760 * if expansion failed. In the last case, &sk_buff is not changed.
761 *
762 * All the pointers pointing into skb header may change and must be
763 * reloaded after call to this function.
764 */
765
Victor Fusco86a76ca2005-07-08 14:57:47 -0700766int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100767 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 int i;
770 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700771#ifdef NET_SKBUFF_DATA_USES_OFFSET
772 int size = nhead + skb->end + ntail;
773#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700775#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 long off;
777
Herbert Xu4edd87a2008-10-01 07:09:38 -0700778 BUG_ON(nhead < 0);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (skb_shared(skb))
781 BUG();
782
783 size = SKB_DATA_ALIGN(size);
784
785 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
786 if (!data)
787 goto nodata;
788
789 /* Copy only real data... and, alas, header. This should be
790 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700791#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700792 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700793#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700794 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700795#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700796 memcpy(data + size, skb_end_pointer(skb),
797 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
800 get_page(skb_shinfo(skb)->frags[i].page);
801
802 if (skb_shinfo(skb)->frag_list)
803 skb_clone_fraglist(skb);
804
805 skb_release_data(skb);
806
807 off = (data + nhead) - skb->head;
808
809 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700811#ifdef NET_SKBUFF_DATA_USES_OFFSET
812 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700813 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700814#else
815 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700816#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700817 /* {transport,network,mac}_header and tail are relative to skb->head */
818 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700819 skb->transport_header += off;
820 skb->network_header += off;
821 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700822 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700824 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 skb->nohdr = 0;
826 atomic_set(&skb_shinfo(skb)->dataref, 1);
827 return 0;
828
829nodata:
830 return -ENOMEM;
831}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800832EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834/* Make private copy of skb with writable head and some headroom */
835
836struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
837{
838 struct sk_buff *skb2;
839 int delta = headroom - skb_headroom(skb);
840
841 if (delta <= 0)
842 skb2 = pskb_copy(skb, GFP_ATOMIC);
843 else {
844 skb2 = skb_clone(skb, GFP_ATOMIC);
845 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
846 GFP_ATOMIC)) {
847 kfree_skb(skb2);
848 skb2 = NULL;
849 }
850 }
851 return skb2;
852}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800853EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855/**
856 * skb_copy_expand - copy and expand sk_buff
857 * @skb: buffer to copy
858 * @newheadroom: new free bytes at head
859 * @newtailroom: new free bytes at tail
860 * @gfp_mask: allocation priority
861 *
862 * Make a copy of both an &sk_buff and its data and while doing so
863 * allocate additional space.
864 *
865 * This is used when the caller wishes to modify the data and needs a
866 * private copy of the data to alter as well as more space for new fields.
867 * Returns %NULL on failure or the pointer to the buffer
868 * on success. The returned buffer has a reference count of 1.
869 *
870 * You must pass %GFP_ATOMIC as the allocation priority if this function
871 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
873struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700874 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100875 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 /*
878 * Allocate the copy buffer
879 */
880 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
881 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700882 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700884 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (!n)
887 return NULL;
888
889 skb_reserve(n, newheadroom);
890
891 /* Set the tail pointer and length */
892 skb_put(n, skb->len);
893
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700894 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 head_copy_off = 0;
896 if (newheadroom <= head_copy_len)
897 head_copy_len = newheadroom;
898 else
899 head_copy_off = newheadroom - head_copy_len;
900
901 /* Copy the linear header and data. */
902 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
903 skb->len + head_copy_len))
904 BUG();
905
906 copy_skb_header(n, skb);
907
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700908 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700909 n->csum_start += off;
910#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700911 n->transport_header += off;
912 n->network_header += off;
913 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700914#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return n;
917}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800918EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920/**
921 * skb_pad - zero pad the tail of an skb
922 * @skb: buffer to pad
923 * @pad: space to pad
924 *
925 * Ensure that a buffer is followed by a padding area that is zero
926 * filled. Used by network drivers which may DMA or transfer data
927 * beyond the buffer end onto the wire.
928 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700929 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900931
Herbert Xu5b057c62006-06-23 02:06:41 -0700932int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Herbert Xu5b057c62006-06-23 02:06:41 -0700934 int err;
935 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700938 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700940 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700942
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700943 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700944 if (likely(skb_cloned(skb) || ntail > 0)) {
945 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
946 if (unlikely(err))
947 goto free_skb;
948 }
949
950 /* FIXME: The use of this function with non-linear skb's really needs
951 * to be audited.
952 */
953 err = skb_linearize(skb);
954 if (unlikely(err))
955 goto free_skb;
956
957 memset(skb->data + skb->len, 0, pad);
958 return 0;
959
960free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700962 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800964EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900965
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700966/**
967 * skb_put - add data to a buffer
968 * @skb: buffer to use
969 * @len: amount of data to add
970 *
971 * This function extends the used data area of the buffer. If this would
972 * exceed the total buffer size the kernel will panic. A pointer to the
973 * first byte of the extra data is returned.
974 */
975unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
976{
977 unsigned char *tmp = skb_tail_pointer(skb);
978 SKB_LINEAR_ASSERT(skb);
979 skb->tail += len;
980 skb->len += len;
981 if (unlikely(skb->tail > skb->end))
982 skb_over_panic(skb, len, __builtin_return_address(0));
983 return tmp;
984}
985EXPORT_SYMBOL(skb_put);
986
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700987/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -0700988 * skb_push - add data to the start of a buffer
989 * @skb: buffer to use
990 * @len: amount of data to add
991 *
992 * This function extends the used data area of the buffer at the buffer
993 * start. If this would exceed the total buffer headroom the kernel will
994 * panic. A pointer to the first byte of the extra data is returned.
995 */
996unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
997{
998 skb->data -= len;
999 skb->len += len;
1000 if (unlikely(skb->data<skb->head))
1001 skb_under_panic(skb, len, __builtin_return_address(0));
1002 return skb->data;
1003}
1004EXPORT_SYMBOL(skb_push);
1005
1006/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001007 * skb_pull - remove data from the start of a buffer
1008 * @skb: buffer to use
1009 * @len: amount of data to remove
1010 *
1011 * This function removes data from the start of a buffer, returning
1012 * the memory to the headroom. A pointer to the next data in the buffer
1013 * is returned. Once the data has been pulled future pushes will overwrite
1014 * the old data.
1015 */
1016unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1017{
1018 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1019}
1020EXPORT_SYMBOL(skb_pull);
1021
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001022/**
1023 * skb_trim - remove end from a buffer
1024 * @skb: buffer to alter
1025 * @len: new length
1026 *
1027 * Cut the length of a buffer down by removing data from the tail. If
1028 * the buffer is already under the length specified it is not modified.
1029 * The skb must be linear.
1030 */
1031void skb_trim(struct sk_buff *skb, unsigned int len)
1032{
1033 if (skb->len > len)
1034 __skb_trim(skb, len);
1035}
1036EXPORT_SYMBOL(skb_trim);
1037
Herbert Xu3cc0e872006-06-09 16:13:38 -07001038/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
1040
Herbert Xu3cc0e872006-06-09 16:13:38 -07001041int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Herbert Xu27b437c2006-07-13 19:26:39 -07001043 struct sk_buff **fragp;
1044 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 int offset = skb_headlen(skb);
1046 int nfrags = skb_shinfo(skb)->nr_frags;
1047 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001048 int err;
1049
1050 if (skb_cloned(skb) &&
1051 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001054 i = 0;
1055 if (offset >= len)
1056 goto drop_pages;
1057
1058 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -07001060
1061 if (end < len) {
1062 offset = end;
1063 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001065
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001066 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -07001067
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001068drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001069 skb_shinfo(skb)->nr_frags = i;
1070
1071 for (; i < nfrags; i++)
1072 put_page(skb_shinfo(skb)->frags[i].page);
1073
1074 if (skb_shinfo(skb)->frag_list)
1075 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001076 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Herbert Xu27b437c2006-07-13 19:26:39 -07001079 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1080 fragp = &frag->next) {
1081 int end = offset + frag->len;
1082
1083 if (skb_shared(frag)) {
1084 struct sk_buff *nfrag;
1085
1086 nfrag = skb_clone(frag, GFP_ATOMIC);
1087 if (unlikely(!nfrag))
1088 return -ENOMEM;
1089
1090 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001091 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001092 frag = nfrag;
1093 *fragp = frag;
1094 }
1095
1096 if (end < len) {
1097 offset = end;
1098 continue;
1099 }
1100
1101 if (end > len &&
1102 unlikely((err = pskb_trim(frag, len - offset))))
1103 return err;
1104
1105 if (frag->next)
1106 skb_drop_list(&frag->next);
1107 break;
1108 }
1109
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001110done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001111 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 skb->data_len -= skb->len - len;
1113 skb->len = len;
1114 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001115 skb->len = len;
1116 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001117 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119
1120 return 0;
1121}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001122EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124/**
1125 * __pskb_pull_tail - advance tail of skb header
1126 * @skb: buffer to reallocate
1127 * @delta: number of bytes to advance tail
1128 *
1129 * The function makes a sense only on a fragmented &sk_buff,
1130 * it expands header moving its tail forward and copying necessary
1131 * data from fragmented part.
1132 *
1133 * &sk_buff MUST have reference count of 1.
1134 *
1135 * Returns %NULL (and &sk_buff does not change) if pull failed
1136 * or value of new tail of skb in the case of success.
1137 *
1138 * All the pointers pointing into skb header may change and must be
1139 * reloaded after call to this function.
1140 */
1141
1142/* Moves tail of skb head forward, copying data from fragmented part,
1143 * when it is necessary.
1144 * 1. It may fail due to malloc failure.
1145 * 2. It may change skb pointers.
1146 *
1147 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1148 */
1149unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1150{
1151 /* If skb has not enough free space at tail, get new one
1152 * plus 128 bytes for future expansions. If we have enough
1153 * room at tail, reallocate without expansion only if skb is cloned.
1154 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001155 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (eat > 0 || skb_cloned(skb)) {
1158 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1159 GFP_ATOMIC))
1160 return NULL;
1161 }
1162
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001163 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 BUG();
1165
1166 /* Optimization: no fragments, no reasons to preestimate
1167 * size of pulled pages. Superb.
1168 */
1169 if (!skb_shinfo(skb)->frag_list)
1170 goto pull_pages;
1171
1172 /* Estimate size of pulled pages. */
1173 eat = delta;
1174 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1175 if (skb_shinfo(skb)->frags[i].size >= eat)
1176 goto pull_pages;
1177 eat -= skb_shinfo(skb)->frags[i].size;
1178 }
1179
1180 /* If we need update frag list, we are in troubles.
1181 * Certainly, it possible to add an offset to skb data,
1182 * but taking into account that pulling is expected to
1183 * be very rare operation, it is worth to fight against
1184 * further bloating skb head and crucify ourselves here instead.
1185 * Pure masohism, indeed. 8)8)
1186 */
1187 if (eat) {
1188 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1189 struct sk_buff *clone = NULL;
1190 struct sk_buff *insp = NULL;
1191
1192 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001193 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (list->len <= eat) {
1196 /* Eaten as whole. */
1197 eat -= list->len;
1198 list = list->next;
1199 insp = list;
1200 } else {
1201 /* Eaten partially. */
1202
1203 if (skb_shared(list)) {
1204 /* Sucks! We need to fork list. :-( */
1205 clone = skb_clone(list, GFP_ATOMIC);
1206 if (!clone)
1207 return NULL;
1208 insp = list->next;
1209 list = clone;
1210 } else {
1211 /* This may be pulled without
1212 * problems. */
1213 insp = list;
1214 }
1215 if (!pskb_pull(list, eat)) {
1216 if (clone)
1217 kfree_skb(clone);
1218 return NULL;
1219 }
1220 break;
1221 }
1222 } while (eat);
1223
1224 /* Free pulled out fragments. */
1225 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1226 skb_shinfo(skb)->frag_list = list->next;
1227 kfree_skb(list);
1228 }
1229 /* And insert new clone at head. */
1230 if (clone) {
1231 clone->next = list;
1232 skb_shinfo(skb)->frag_list = clone;
1233 }
1234 }
1235 /* Success! Now we may commit changes to skb data. */
1236
1237pull_pages:
1238 eat = delta;
1239 k = 0;
1240 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1241 if (skb_shinfo(skb)->frags[i].size <= eat) {
1242 put_page(skb_shinfo(skb)->frags[i].page);
1243 eat -= skb_shinfo(skb)->frags[i].size;
1244 } else {
1245 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1246 if (eat) {
1247 skb_shinfo(skb)->frags[k].page_offset += eat;
1248 skb_shinfo(skb)->frags[k].size -= eat;
1249 eat = 0;
1250 }
1251 k++;
1252 }
1253 }
1254 skb_shinfo(skb)->nr_frags = k;
1255
1256 skb->tail += delta;
1257 skb->data_len -= delta;
1258
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001259 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001261EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263/* Copy some data bits from skb to kernel buffer. */
1264
1265int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1266{
1267 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001268 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (offset > (int)skb->len - len)
1271 goto fault;
1272
1273 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001274 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (copy > len)
1276 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001277 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if ((len -= copy) == 0)
1279 return 0;
1280 offset += copy;
1281 to += copy;
1282 }
1283
1284 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001285 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001287 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001288
1289 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if ((copy = end - offset) > 0) {
1291 u8 *vaddr;
1292
1293 if (copy > len)
1294 copy = len;
1295
1296 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1297 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001298 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1299 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 kunmap_skb_frag(vaddr);
1301
1302 if ((len -= copy) == 0)
1303 return 0;
1304 offset += copy;
1305 to += copy;
1306 }
David S. Miller1a028e52007-04-27 15:21:23 -07001307 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
1310 if (skb_shinfo(skb)->frag_list) {
1311 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1312
1313 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001314 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001316 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001317
1318 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if ((copy = end - offset) > 0) {
1320 if (copy > len)
1321 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001322 if (skb_copy_bits(list, offset - start,
1323 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 goto fault;
1325 if ((len -= copy) == 0)
1326 return 0;
1327 offset += copy;
1328 to += copy;
1329 }
David S. Miller1a028e52007-04-27 15:21:23 -07001330 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 }
1333 if (!len)
1334 return 0;
1335
1336fault:
1337 return -EFAULT;
1338}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001339EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Jens Axboe9c55e012007-11-06 23:30:13 -08001341/*
1342 * Callback from splice_to_pipe(), if we need to release some pages
1343 * at the end of the spd in case we error'ed out in filling the pipe.
1344 */
1345static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1346{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001347 put_page(spd->pages[i]);
1348}
Jens Axboe9c55e012007-11-06 23:30:13 -08001349
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001350static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1351 unsigned int *offset,
1352 struct sk_buff *skb)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001353{
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001354 struct sock *sk = skb->sk;
1355 struct page *p = sk->sk_sndmsg_page;
1356 unsigned int off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001357
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001358 if (!p) {
1359new_page:
1360 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1361 if (!p)
1362 return NULL;
1363
1364 off = sk->sk_sndmsg_off = 0;
1365 /* hold one ref to this page until it's full */
1366 } else {
1367 unsigned int mlen;
1368
1369 off = sk->sk_sndmsg_off;
1370 mlen = PAGE_SIZE - off;
1371 if (mlen < 64 && mlen < *len) {
1372 put_page(p);
1373 goto new_page;
1374 }
1375
1376 *len = min_t(unsigned int, *len, mlen);
1377 }
1378
1379 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1380 sk->sk_sndmsg_off += *len;
1381 *offset = off;
1382 get_page(p);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001383
1384 return p;
Jens Axboe9c55e012007-11-06 23:30:13 -08001385}
1386
1387/*
1388 * Fill page/offset/length into spd, if it can hold more pages.
1389 */
1390static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001391 unsigned int *len, unsigned int offset,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001392 struct sk_buff *skb, int linear)
Jens Axboe9c55e012007-11-06 23:30:13 -08001393{
1394 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1395 return 1;
1396
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001397 if (linear) {
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001398 page = linear_to_page(page, len, &offset, skb);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001399 if (!page)
1400 return 1;
1401 } else
1402 get_page(page);
1403
Jens Axboe9c55e012007-11-06 23:30:13 -08001404 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001405 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001406 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001407 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001408
Jens Axboe9c55e012007-11-06 23:30:13 -08001409 return 0;
1410}
1411
Octavian Purdila2870c432008-07-15 00:49:11 -07001412static inline void __segment_seek(struct page **page, unsigned int *poff,
1413 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001414{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001415 unsigned long n;
1416
Octavian Purdila2870c432008-07-15 00:49:11 -07001417 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001418 n = *poff / PAGE_SIZE;
1419 if (n)
1420 *page = nth_page(*page, n);
1421
Octavian Purdila2870c432008-07-15 00:49:11 -07001422 *poff = *poff % PAGE_SIZE;
1423 *plen -= off;
1424}
Jens Axboe9c55e012007-11-06 23:30:13 -08001425
Octavian Purdila2870c432008-07-15 00:49:11 -07001426static inline int __splice_segment(struct page *page, unsigned int poff,
1427 unsigned int plen, unsigned int *off,
1428 unsigned int *len, struct sk_buff *skb,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001429 struct splice_pipe_desc *spd, int linear)
Octavian Purdila2870c432008-07-15 00:49:11 -07001430{
1431 if (!*len)
1432 return 1;
1433
1434 /* skip this segment if already processed */
1435 if (*off >= plen) {
1436 *off -= plen;
1437 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001438 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001439
Octavian Purdila2870c432008-07-15 00:49:11 -07001440 /* ignore any bits we already processed */
1441 if (*off) {
1442 __segment_seek(&page, &poff, &plen, *off);
1443 *off = 0;
1444 }
1445
1446 do {
1447 unsigned int flen = min(*len, plen);
1448
1449 /* the linear region may spread across several pages */
1450 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1451
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001452 if (spd_fill_page(spd, page, &flen, poff, skb, linear))
Octavian Purdila2870c432008-07-15 00:49:11 -07001453 return 1;
1454
1455 __segment_seek(&page, &poff, &plen, flen);
1456 *len -= flen;
1457
1458 } while (*len && plen);
1459
1460 return 0;
1461}
1462
1463/*
1464 * Map linear and fragment data from the skb to spd. It reports failure if the
1465 * pipe is full or if we already spliced the requested length.
1466 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001467static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Octavian Purdila2870c432008-07-15 00:49:11 -07001468 unsigned int *len,
1469 struct splice_pipe_desc *spd)
1470{
1471 int seg;
1472
Jens Axboe9c55e012007-11-06 23:30:13 -08001473 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001474 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001475 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001476 if (__splice_segment(virt_to_page(skb->data),
1477 (unsigned long) skb->data & (PAGE_SIZE - 1),
1478 skb_headlen(skb),
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001479 offset, len, skb, spd, 1))
Octavian Purdila2870c432008-07-15 00:49:11 -07001480 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001481
1482 /*
1483 * then map the fragments
1484 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001485 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1486 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1487
Octavian Purdila2870c432008-07-15 00:49:11 -07001488 if (__splice_segment(f->page, f->page_offset, f->size,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001489 offset, len, skb, spd, 0))
Octavian Purdila2870c432008-07-15 00:49:11 -07001490 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001491 }
1492
Octavian Purdila2870c432008-07-15 00:49:11 -07001493 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001494}
1495
1496/*
1497 * Map data from the skb to a pipe. Should handle both the linear part,
1498 * the fragments, and the frag list. It does NOT handle frag lists within
1499 * the frag list, if such a thing exists. We'd probably need to recurse to
1500 * handle that cleanly.
1501 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001502int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001503 struct pipe_inode_info *pipe, unsigned int tlen,
1504 unsigned int flags)
1505{
1506 struct partial_page partial[PIPE_BUFFERS];
1507 struct page *pages[PIPE_BUFFERS];
1508 struct splice_pipe_desc spd = {
1509 .pages = pages,
1510 .partial = partial,
1511 .flags = flags,
1512 .ops = &sock_pipe_buf_ops,
1513 .spd_release = sock_spd_release,
1514 };
Jens Axboe9c55e012007-11-06 23:30:13 -08001515
1516 /*
1517 * __skb_splice_bits() only fails if the output has no room left,
1518 * so no point in going over the frag_list for the error case.
1519 */
1520 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1521 goto done;
1522 else if (!tlen)
1523 goto done;
1524
1525 /*
1526 * now see if we have a frag_list to map
1527 */
1528 if (skb_shinfo(skb)->frag_list) {
1529 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1530
1531 for (; list && tlen; list = list->next) {
1532 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1533 break;
1534 }
1535 }
1536
1537done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001538 if (spd.nr_pages) {
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001539 struct sock *sk = skb->sk;
Jens Axboe9c55e012007-11-06 23:30:13 -08001540 int ret;
1541
1542 /*
1543 * Drop the socket lock, otherwise we have reverse
1544 * locking dependencies between sk_lock and i_mutex
1545 * here as compared to sendfile(). We enter here
1546 * with the socket lock held, and splice_to_pipe() will
1547 * grab the pipe inode lock. For sendfile() emulation,
1548 * we call into ->sendpage() with the i_mutex lock held
1549 * and networking will grab the socket lock.
1550 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001551 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001552 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001553 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001554 return ret;
1555 }
1556
1557 return 0;
1558}
1559
Herbert Xu357b40a2005-04-19 22:30:14 -07001560/**
1561 * skb_store_bits - store bits from kernel buffer to skb
1562 * @skb: destination buffer
1563 * @offset: offset in destination
1564 * @from: source buffer
1565 * @len: number of bytes to copy
1566 *
1567 * Copy the specified number of bytes from the source buffer to the
1568 * destination skb. This function handles all the messy bits of
1569 * traversing fragment lists and such.
1570 */
1571
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001572int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001573{
1574 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001575 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001576
1577 if (offset > (int)skb->len - len)
1578 goto fault;
1579
David S. Miller1a028e52007-04-27 15:21:23 -07001580 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001581 if (copy > len)
1582 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001583 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001584 if ((len -= copy) == 0)
1585 return 0;
1586 offset += copy;
1587 from += copy;
1588 }
1589
1590 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1591 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001592 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001593
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001594 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001595
1596 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001597 if ((copy = end - offset) > 0) {
1598 u8 *vaddr;
1599
1600 if (copy > len)
1601 copy = len;
1602
1603 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001604 memcpy(vaddr + frag->page_offset + offset - start,
1605 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001606 kunmap_skb_frag(vaddr);
1607
1608 if ((len -= copy) == 0)
1609 return 0;
1610 offset += copy;
1611 from += copy;
1612 }
David S. Miller1a028e52007-04-27 15:21:23 -07001613 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001614 }
1615
1616 if (skb_shinfo(skb)->frag_list) {
1617 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1618
1619 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001620 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001621
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001622 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001623
1624 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001625 if ((copy = end - offset) > 0) {
1626 if (copy > len)
1627 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001628 if (skb_store_bits(list, offset - start,
1629 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001630 goto fault;
1631 if ((len -= copy) == 0)
1632 return 0;
1633 offset += copy;
1634 from += copy;
1635 }
David S. Miller1a028e52007-04-27 15:21:23 -07001636 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001637 }
1638 }
1639 if (!len)
1640 return 0;
1641
1642fault:
1643 return -EFAULT;
1644}
Herbert Xu357b40a2005-04-19 22:30:14 -07001645EXPORT_SYMBOL(skb_store_bits);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/* Checksum skb data. */
1648
Al Viro2bbbc862006-11-14 21:37:14 -08001649__wsum skb_checksum(const struct sk_buff *skb, int offset,
1650 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
David S. Miller1a028e52007-04-27 15:21:23 -07001652 int start = skb_headlen(skb);
1653 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int pos = 0;
1655
1656 /* Checksum header. */
1657 if (copy > 0) {
1658 if (copy > len)
1659 copy = len;
1660 csum = csum_partial(skb->data + offset, copy, csum);
1661 if ((len -= copy) == 0)
1662 return csum;
1663 offset += copy;
1664 pos = copy;
1665 }
1666
1667 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001668 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001670 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001671
1672 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001674 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 u8 *vaddr;
1676 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1677
1678 if (copy > len)
1679 copy = len;
1680 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001681 csum2 = csum_partial(vaddr + frag->page_offset +
1682 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 kunmap_skb_frag(vaddr);
1684 csum = csum_block_add(csum, csum2, pos);
1685 if (!(len -= copy))
1686 return csum;
1687 offset += copy;
1688 pos += copy;
1689 }
David S. Miller1a028e52007-04-27 15:21:23 -07001690 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
1693 if (skb_shinfo(skb)->frag_list) {
1694 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1695
1696 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001697 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001699 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001700
1701 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001703 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (copy > len)
1705 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001706 csum2 = skb_checksum(list, offset - start,
1707 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 csum = csum_block_add(csum, csum2, pos);
1709 if ((len -= copy) == 0)
1710 return csum;
1711 offset += copy;
1712 pos += copy;
1713 }
David S. Miller1a028e52007-04-27 15:21:23 -07001714 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001717 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 return csum;
1720}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001721EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723/* Both of above in one bottle. */
1724
Al Viro81d77662006-11-14 21:37:33 -08001725__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1726 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
David S. Miller1a028e52007-04-27 15:21:23 -07001728 int start = skb_headlen(skb);
1729 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int pos = 0;
1731
1732 /* Copy header. */
1733 if (copy > 0) {
1734 if (copy > len)
1735 copy = len;
1736 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1737 copy, csum);
1738 if ((len -= copy) == 0)
1739 return csum;
1740 offset += copy;
1741 to += copy;
1742 pos = copy;
1743 }
1744
1745 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001746 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001748 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001749
1750 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001752 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 u8 *vaddr;
1754 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1755
1756 if (copy > len)
1757 copy = len;
1758 vaddr = kmap_skb_frag(frag);
1759 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001760 frag->page_offset +
1761 offset - start, to,
1762 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 kunmap_skb_frag(vaddr);
1764 csum = csum_block_add(csum, csum2, pos);
1765 if (!(len -= copy))
1766 return csum;
1767 offset += copy;
1768 to += copy;
1769 pos += copy;
1770 }
David S. Miller1a028e52007-04-27 15:21:23 -07001771 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 if (skb_shinfo(skb)->frag_list) {
1775 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1776
1777 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001778 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001779 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001781 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001782
1783 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if ((copy = end - offset) > 0) {
1785 if (copy > len)
1786 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001787 csum2 = skb_copy_and_csum_bits(list,
1788 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 to, copy, 0);
1790 csum = csum_block_add(csum, csum2, pos);
1791 if ((len -= copy) == 0)
1792 return csum;
1793 offset += copy;
1794 to += copy;
1795 pos += copy;
1796 }
David S. Miller1a028e52007-04-27 15:21:23 -07001797 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001800 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return csum;
1802}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001803EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1806{
Al Virod3bc23e2006-11-14 21:24:49 -08001807 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 long csstart;
1809
Patrick McHardy84fa7932006-08-29 16:44:56 -07001810 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001811 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 else
1813 csstart = skb_headlen(skb);
1814
Kris Katterjohn09a62662006-01-08 22:24:28 -08001815 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001817 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 csum = 0;
1820 if (csstart != skb->len)
1821 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1822 skb->len - csstart, 0);
1823
Patrick McHardy84fa7932006-08-29 16:44:56 -07001824 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001825 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Al Virod3bc23e2006-11-14 21:24:49 -08001827 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001830EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832/**
1833 * skb_dequeue - remove from the head of the queue
1834 * @list: list to dequeue from
1835 *
1836 * Remove the head of the list. The list lock is taken so the function
1837 * may be used safely with other locking list functions. The head item is
1838 * returned or %NULL if the list is empty.
1839 */
1840
1841struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1842{
1843 unsigned long flags;
1844 struct sk_buff *result;
1845
1846 spin_lock_irqsave(&list->lock, flags);
1847 result = __skb_dequeue(list);
1848 spin_unlock_irqrestore(&list->lock, flags);
1849 return result;
1850}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001851EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853/**
1854 * skb_dequeue_tail - remove from the tail of the queue
1855 * @list: list to dequeue from
1856 *
1857 * Remove the tail of the list. The list lock is taken so the function
1858 * may be used safely with other locking list functions. The tail item is
1859 * returned or %NULL if the list is empty.
1860 */
1861struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1862{
1863 unsigned long flags;
1864 struct sk_buff *result;
1865
1866 spin_lock_irqsave(&list->lock, flags);
1867 result = __skb_dequeue_tail(list);
1868 spin_unlock_irqrestore(&list->lock, flags);
1869 return result;
1870}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001871EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873/**
1874 * skb_queue_purge - empty a list
1875 * @list: list to empty
1876 *
1877 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1878 * the list and one reference dropped. This function takes the list
1879 * lock and is atomic with respect to other list locking functions.
1880 */
1881void skb_queue_purge(struct sk_buff_head *list)
1882{
1883 struct sk_buff *skb;
1884 while ((skb = skb_dequeue(list)) != NULL)
1885 kfree_skb(skb);
1886}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001887EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889/**
1890 * skb_queue_head - queue a buffer at the list head
1891 * @list: list to use
1892 * @newsk: buffer to queue
1893 *
1894 * Queue a buffer at the start of the list. This function takes the
1895 * list lock and can be used safely with other locking &sk_buff functions
1896 * safely.
1897 *
1898 * A buffer cannot be placed on two lists at the same time.
1899 */
1900void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1901{
1902 unsigned long flags;
1903
1904 spin_lock_irqsave(&list->lock, flags);
1905 __skb_queue_head(list, newsk);
1906 spin_unlock_irqrestore(&list->lock, flags);
1907}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001908EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910/**
1911 * skb_queue_tail - queue a buffer at the list tail
1912 * @list: list to use
1913 * @newsk: buffer to queue
1914 *
1915 * Queue a buffer at the tail of the list. This function takes the
1916 * list lock and can be used safely with other locking &sk_buff functions
1917 * safely.
1918 *
1919 * A buffer cannot be placed on two lists at the same time.
1920 */
1921void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1922{
1923 unsigned long flags;
1924
1925 spin_lock_irqsave(&list->lock, flags);
1926 __skb_queue_tail(list, newsk);
1927 spin_unlock_irqrestore(&list->lock, flags);
1928}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001929EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/**
1932 * skb_unlink - remove a buffer from a list
1933 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001934 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 *
David S. Miller8728b832005-08-09 19:25:21 -07001936 * Remove a packet from a list. The list locks are taken and this
1937 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 *
David S. Miller8728b832005-08-09 19:25:21 -07001939 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
David S. Miller8728b832005-08-09 19:25:21 -07001941void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
David S. Miller8728b832005-08-09 19:25:21 -07001943 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
David S. Miller8728b832005-08-09 19:25:21 -07001945 spin_lock_irqsave(&list->lock, flags);
1946 __skb_unlink(skb, list);
1947 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001949EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951/**
1952 * skb_append - append a buffer
1953 * @old: buffer to insert after
1954 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001955 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 *
1957 * Place a packet after a given packet in a list. The list locks are taken
1958 * and this function is atomic with respect to other list locked calls.
1959 * A buffer cannot be placed on two lists at the same time.
1960 */
David S. Miller8728b832005-08-09 19:25:21 -07001961void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 unsigned long flags;
1964
David S. Miller8728b832005-08-09 19:25:21 -07001965 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001966 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001967 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001969EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971/**
1972 * skb_insert - insert a buffer
1973 * @old: buffer to insert before
1974 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001975 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 *
David S. Miller8728b832005-08-09 19:25:21 -07001977 * Place a packet before a given packet in a list. The list locks are
1978 * taken and this function is atomic with respect to other list locked
1979 * calls.
1980 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 * A buffer cannot be placed on two lists at the same time.
1982 */
David S. Miller8728b832005-08-09 19:25:21 -07001983void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 unsigned long flags;
1986
David S. Miller8728b832005-08-09 19:25:21 -07001987 spin_lock_irqsave(&list->lock, flags);
1988 __skb_insert(newsk, old->prev, old, list);
1989 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001991EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993static inline void skb_split_inside_header(struct sk_buff *skb,
1994 struct sk_buff* skb1,
1995 const u32 len, const int pos)
1996{
1997 int i;
1998
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001999 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2000 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* And move data appendix as is. */
2002 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2003 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2004
2005 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2006 skb_shinfo(skb)->nr_frags = 0;
2007 skb1->data_len = skb->data_len;
2008 skb1->len += skb1->data_len;
2009 skb->data_len = 0;
2010 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002011 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
2014static inline void skb_split_no_header(struct sk_buff *skb,
2015 struct sk_buff* skb1,
2016 const u32 len, int pos)
2017{
2018 int i, k = 0;
2019 const int nfrags = skb_shinfo(skb)->nr_frags;
2020
2021 skb_shinfo(skb)->nr_frags = 0;
2022 skb1->len = skb1->data_len = skb->len - len;
2023 skb->len = len;
2024 skb->data_len = len - pos;
2025
2026 for (i = 0; i < nfrags; i++) {
2027 int size = skb_shinfo(skb)->frags[i].size;
2028
2029 if (pos + size > len) {
2030 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2031
2032 if (pos < len) {
2033 /* Split frag.
2034 * We have two variants in this case:
2035 * 1. Move all the frag to the second
2036 * part, if it is possible. F.e.
2037 * this approach is mandatory for TUX,
2038 * where splitting is expensive.
2039 * 2. Split is accurately. We make this.
2040 */
2041 get_page(skb_shinfo(skb)->frags[i].page);
2042 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2043 skb_shinfo(skb1)->frags[0].size -= len - pos;
2044 skb_shinfo(skb)->frags[i].size = len - pos;
2045 skb_shinfo(skb)->nr_frags++;
2046 }
2047 k++;
2048 } else
2049 skb_shinfo(skb)->nr_frags++;
2050 pos += size;
2051 }
2052 skb_shinfo(skb1)->nr_frags = k;
2053}
2054
2055/**
2056 * skb_split - Split fragmented skb to two parts at length len.
2057 * @skb: the buffer to split
2058 * @skb1: the buffer to receive the second part
2059 * @len: new length for skb
2060 */
2061void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2062{
2063 int pos = skb_headlen(skb);
2064
2065 if (len < pos) /* Split line is inside header. */
2066 skb_split_inside_header(skb, skb1, len, pos);
2067 else /* Second chunk has no header, nothing to copy. */
2068 skb_split_no_header(skb, skb1, len, pos);
2069}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002070EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002072/* Shifting from/to a cloned skb is a no-go.
2073 *
2074 * Caller cannot keep skb_shinfo related pointers past calling here!
2075 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002076static int skb_prepare_for_shift(struct sk_buff *skb)
2077{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002078 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002079}
2080
2081/**
2082 * skb_shift - Shifts paged data partially from skb to another
2083 * @tgt: buffer into which tail data gets added
2084 * @skb: buffer from which the paged data comes from
2085 * @shiftlen: shift up to this many bytes
2086 *
2087 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2088 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2089 * It's up to caller to free skb if everything was shifted.
2090 *
2091 * If @tgt runs out of frags, the whole operation is aborted.
2092 *
2093 * Skb cannot include anything else but paged data while tgt is allowed
2094 * to have non-paged data as well.
2095 *
2096 * TODO: full sized shift could be optimized but that would need
2097 * specialized skb free'er to handle frags without up-to-date nr_frags.
2098 */
2099int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2100{
2101 int from, to, merge, todo;
2102 struct skb_frag_struct *fragfrom, *fragto;
2103
2104 BUG_ON(shiftlen > skb->len);
2105 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2106
2107 todo = shiftlen;
2108 from = 0;
2109 to = skb_shinfo(tgt)->nr_frags;
2110 fragfrom = &skb_shinfo(skb)->frags[from];
2111
2112 /* Actual merge is delayed until the point when we know we can
2113 * commit all, so that we don't have to undo partial changes
2114 */
2115 if (!to ||
2116 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2117 merge = -1;
2118 } else {
2119 merge = to - 1;
2120
2121 todo -= fragfrom->size;
2122 if (todo < 0) {
2123 if (skb_prepare_for_shift(skb) ||
2124 skb_prepare_for_shift(tgt))
2125 return 0;
2126
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002127 /* All previous frag pointers might be stale! */
2128 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002129 fragto = &skb_shinfo(tgt)->frags[merge];
2130
2131 fragto->size += shiftlen;
2132 fragfrom->size -= shiftlen;
2133 fragfrom->page_offset += shiftlen;
2134
2135 goto onlymerged;
2136 }
2137
2138 from++;
2139 }
2140
2141 /* Skip full, not-fitting skb to avoid expensive operations */
2142 if ((shiftlen == skb->len) &&
2143 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2144 return 0;
2145
2146 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2147 return 0;
2148
2149 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2150 if (to == MAX_SKB_FRAGS)
2151 return 0;
2152
2153 fragfrom = &skb_shinfo(skb)->frags[from];
2154 fragto = &skb_shinfo(tgt)->frags[to];
2155
2156 if (todo >= fragfrom->size) {
2157 *fragto = *fragfrom;
2158 todo -= fragfrom->size;
2159 from++;
2160 to++;
2161
2162 } else {
2163 get_page(fragfrom->page);
2164 fragto->page = fragfrom->page;
2165 fragto->page_offset = fragfrom->page_offset;
2166 fragto->size = todo;
2167
2168 fragfrom->page_offset += todo;
2169 fragfrom->size -= todo;
2170 todo = 0;
2171
2172 to++;
2173 break;
2174 }
2175 }
2176
2177 /* Ready to "commit" this state change to tgt */
2178 skb_shinfo(tgt)->nr_frags = to;
2179
2180 if (merge >= 0) {
2181 fragfrom = &skb_shinfo(skb)->frags[0];
2182 fragto = &skb_shinfo(tgt)->frags[merge];
2183
2184 fragto->size += fragfrom->size;
2185 put_page(fragfrom->page);
2186 }
2187
2188 /* Reposition in the original skb */
2189 to = 0;
2190 while (from < skb_shinfo(skb)->nr_frags)
2191 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2192 skb_shinfo(skb)->nr_frags = to;
2193
2194 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2195
2196onlymerged:
2197 /* Most likely the tgt won't ever need its checksum anymore, skb on
2198 * the other hand might need it if it needs to be resent
2199 */
2200 tgt->ip_summed = CHECKSUM_PARTIAL;
2201 skb->ip_summed = CHECKSUM_PARTIAL;
2202
2203 /* Yak, is it really working this way? Some helper please? */
2204 skb->len -= shiftlen;
2205 skb->data_len -= shiftlen;
2206 skb->truesize -= shiftlen;
2207 tgt->len += shiftlen;
2208 tgt->data_len += shiftlen;
2209 tgt->truesize += shiftlen;
2210
2211 return shiftlen;
2212}
2213
Thomas Graf677e90e2005-06-23 20:59:51 -07002214/**
2215 * skb_prepare_seq_read - Prepare a sequential read of skb data
2216 * @skb: the buffer to read
2217 * @from: lower offset of data to be read
2218 * @to: upper offset of data to be read
2219 * @st: state variable
2220 *
2221 * Initializes the specified state variable. Must be called before
2222 * invoking skb_seq_read() for the first time.
2223 */
2224void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2225 unsigned int to, struct skb_seq_state *st)
2226{
2227 st->lower_offset = from;
2228 st->upper_offset = to;
2229 st->root_skb = st->cur_skb = skb;
2230 st->frag_idx = st->stepped_offset = 0;
2231 st->frag_data = NULL;
2232}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002233EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002234
2235/**
2236 * skb_seq_read - Sequentially read skb data
2237 * @consumed: number of bytes consumed by the caller so far
2238 * @data: destination pointer for data to be returned
2239 * @st: state variable
2240 *
2241 * Reads a block of skb data at &consumed relative to the
2242 * lower offset specified to skb_prepare_seq_read(). Assigns
2243 * the head of the data block to &data and returns the length
2244 * of the block or 0 if the end of the skb data or the upper
2245 * offset has been reached.
2246 *
2247 * The caller is not required to consume all of the data
2248 * returned, i.e. &consumed is typically set to the number
2249 * of bytes already consumed and the next call to
2250 * skb_seq_read() will return the remaining part of the block.
2251 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002252 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002253 * this limitation is the cost for zerocopy seqeuental
2254 * reads of potentially non linear data.
2255 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002256 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002257 * at the moment, state->root_skb could be replaced with
2258 * a stack for this purpose.
2259 */
2260unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2261 struct skb_seq_state *st)
2262{
2263 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2264 skb_frag_t *frag;
2265
2266 if (unlikely(abs_offset >= st->upper_offset))
2267 return 0;
2268
2269next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002270 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002271
2272 if (abs_offset < block_limit) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002273 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002274 return block_limit - abs_offset;
2275 }
2276
2277 if (st->frag_idx == 0 && !st->frag_data)
2278 st->stepped_offset += skb_headlen(st->cur_skb);
2279
2280 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2281 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2282 block_limit = frag->size + st->stepped_offset;
2283
2284 if (abs_offset < block_limit) {
2285 if (!st->frag_data)
2286 st->frag_data = kmap_skb_frag(frag);
2287
2288 *data = (u8 *) st->frag_data + frag->page_offset +
2289 (abs_offset - st->stepped_offset);
2290
2291 return block_limit - abs_offset;
2292 }
2293
2294 if (st->frag_data) {
2295 kunmap_skb_frag(st->frag_data);
2296 st->frag_data = NULL;
2297 }
2298
2299 st->frag_idx++;
2300 st->stepped_offset += frag->size;
2301 }
2302
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002303 if (st->frag_data) {
2304 kunmap_skb_frag(st->frag_data);
2305 st->frag_data = NULL;
2306 }
2307
Shyam Iyer71b33462009-01-29 16:12:42 -08002308 if (st->root_skb == st->cur_skb &&
2309 skb_shinfo(st->root_skb)->frag_list) {
2310 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002311 st->frag_idx = 0;
2312 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002313 } else if (st->cur_skb->next) {
2314 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002315 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002316 goto next_skb;
2317 }
2318
2319 return 0;
2320}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002321EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002322
2323/**
2324 * skb_abort_seq_read - Abort a sequential read of skb data
2325 * @st: state variable
2326 *
2327 * Must be called if skb_seq_read() was not called until it
2328 * returned 0.
2329 */
2330void skb_abort_seq_read(struct skb_seq_state *st)
2331{
2332 if (st->frag_data)
2333 kunmap_skb_frag(st->frag_data);
2334}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002335EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002336
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002337#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2338
2339static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2340 struct ts_config *conf,
2341 struct ts_state *state)
2342{
2343 return skb_seq_read(offset, text, TS_SKB_CB(state));
2344}
2345
2346static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2347{
2348 skb_abort_seq_read(TS_SKB_CB(state));
2349}
2350
2351/**
2352 * skb_find_text - Find a text pattern in skb data
2353 * @skb: the buffer to look in
2354 * @from: search offset
2355 * @to: search limit
2356 * @config: textsearch configuration
2357 * @state: uninitialized textsearch state variable
2358 *
2359 * Finds a pattern in the skb data according to the specified
2360 * textsearch configuration. Use textsearch_next() to retrieve
2361 * subsequent occurrences of the pattern. Returns the offset
2362 * to the first occurrence or UINT_MAX if no match was found.
2363 */
2364unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2365 unsigned int to, struct ts_config *config,
2366 struct ts_state *state)
2367{
Phil Oesterf72b9482006-06-26 00:00:57 -07002368 unsigned int ret;
2369
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002370 config->get_next_block = skb_ts_get_next_block;
2371 config->finish = skb_ts_finish;
2372
2373 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2374
Phil Oesterf72b9482006-06-26 00:00:57 -07002375 ret = textsearch_find(config, state);
2376 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002377}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002378EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002379
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002380/**
2381 * skb_append_datato_frags: - append the user data to a skb
2382 * @sk: sock structure
2383 * @skb: skb structure to be appened with user data.
2384 * @getfrag: call back function to be used for getting the user data
2385 * @from: pointer to user message iov
2386 * @length: length of the iov message
2387 *
2388 * Description: This procedure append the user data in the fragment part
2389 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2390 */
2391int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002392 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002393 int len, int odd, struct sk_buff *skb),
2394 void *from, int length)
2395{
2396 int frg_cnt = 0;
2397 skb_frag_t *frag = NULL;
2398 struct page *page = NULL;
2399 int copy, left;
2400 int offset = 0;
2401 int ret;
2402
2403 do {
2404 /* Return error if we don't have space for new frag */
2405 frg_cnt = skb_shinfo(skb)->nr_frags;
2406 if (frg_cnt >= MAX_SKB_FRAGS)
2407 return -EFAULT;
2408
2409 /* allocate a new page for next frag */
2410 page = alloc_pages(sk->sk_allocation, 0);
2411
2412 /* If alloc_page fails just return failure and caller will
2413 * free previous allocated pages by doing kfree_skb()
2414 */
2415 if (page == NULL)
2416 return -ENOMEM;
2417
2418 /* initialize the next frag */
2419 sk->sk_sndmsg_page = page;
2420 sk->sk_sndmsg_off = 0;
2421 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2422 skb->truesize += PAGE_SIZE;
2423 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2424
2425 /* get the new initialized frag */
2426 frg_cnt = skb_shinfo(skb)->nr_frags;
2427 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2428
2429 /* copy the user data to page */
2430 left = PAGE_SIZE - frag->page_offset;
2431 copy = (length > left)? left : length;
2432
2433 ret = getfrag(from, (page_address(frag->page) +
2434 frag->page_offset + frag->size),
2435 offset, copy, 0, skb);
2436 if (ret < 0)
2437 return -EFAULT;
2438
2439 /* copy was successful so update the size parameters */
2440 sk->sk_sndmsg_off += copy;
2441 frag->size += copy;
2442 skb->len += copy;
2443 skb->data_len += copy;
2444 offset += copy;
2445 length -= copy;
2446
2447 } while (length > 0);
2448
2449 return 0;
2450}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002451EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002452
Herbert Xucbb042f2006-03-20 22:43:56 -08002453/**
2454 * skb_pull_rcsum - pull skb and update receive checksum
2455 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002456 * @len: length of data pulled
2457 *
2458 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002459 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002460 * receive path processing instead of skb_pull unless you know
2461 * that the checksum difference is zero (e.g., a valid IP header)
2462 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002463 */
2464unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2465{
2466 BUG_ON(len > skb->len);
2467 skb->len -= len;
2468 BUG_ON(skb->len < skb->data_len);
2469 skb_postpull_rcsum(skb, skb->data, len);
2470 return skb->data += len;
2471}
2472
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002473EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2474
Herbert Xuf4c50d92006-06-22 03:02:40 -07002475/**
2476 * skb_segment - Perform protocol segmentation on skb.
2477 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002478 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002479 *
2480 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002481 * a pointer to the first in a list of new skbs for the segments.
2482 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002483 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002484struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002485{
2486 struct sk_buff *segs = NULL;
2487 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002488 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002489 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002490 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002491 unsigned int offset = doffset;
2492 unsigned int headroom;
2493 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002494 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002495 int nfrags = skb_shinfo(skb)->nr_frags;
2496 int err = -ENOMEM;
2497 int i = 0;
2498 int pos;
2499
2500 __skb_push(skb, doffset);
2501 headroom = skb_headroom(skb);
2502 pos = skb_headlen(skb);
2503
2504 do {
2505 struct sk_buff *nskb;
2506 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002507 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002508 int size;
2509
2510 len = skb->len - offset;
2511 if (len > mss)
2512 len = mss;
2513
2514 hsize = skb_headlen(skb) - offset;
2515 if (hsize < 0)
2516 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002517 if (hsize > len || !sg)
2518 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002519
Herbert Xu89319d382008-12-15 23:26:06 -08002520 if (!hsize && i >= nfrags) {
2521 BUG_ON(fskb->len != len);
2522
2523 pos += len;
2524 nskb = skb_clone(fskb, GFP_ATOMIC);
2525 fskb = fskb->next;
2526
2527 if (unlikely(!nskb))
2528 goto err;
2529
2530 hsize = skb_end_pointer(nskb) - nskb->head;
2531 if (skb_cow_head(nskb, doffset + headroom)) {
2532 kfree_skb(nskb);
2533 goto err;
2534 }
2535
2536 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2537 hsize;
2538 skb_release_head_state(nskb);
2539 __skb_push(nskb, doffset);
2540 } else {
2541 nskb = alloc_skb(hsize + doffset + headroom,
2542 GFP_ATOMIC);
2543
2544 if (unlikely(!nskb))
2545 goto err;
2546
2547 skb_reserve(nskb, headroom);
2548 __skb_put(nskb, doffset);
2549 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002550
2551 if (segs)
2552 tail->next = nskb;
2553 else
2554 segs = nskb;
2555 tail = nskb;
2556
Herbert Xu6f85a122008-08-15 14:55:02 -07002557 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002558 nskb->mac_len = skb->mac_len;
2559
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002560 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002561 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002562 nskb->transport_header = (nskb->network_header +
2563 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002564 skb_copy_from_linear_data(skb, nskb->data, doffset);
2565
2566 if (pos >= offset + len)
2567 continue;
2568
Herbert Xuf4c50d92006-06-22 03:02:40 -07002569 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002570 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002571 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2572 skb_put(nskb, len),
2573 len, 0);
2574 continue;
2575 }
2576
2577 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002578
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002579 skb_copy_from_linear_data_offset(skb, offset,
2580 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002581
Herbert Xu89319d382008-12-15 23:26:06 -08002582 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002583 *frag = skb_shinfo(skb)->frags[i];
2584 get_page(frag->page);
2585 size = frag->size;
2586
2587 if (pos < offset) {
2588 frag->page_offset += offset - pos;
2589 frag->size -= offset - pos;
2590 }
2591
Herbert Xu89319d382008-12-15 23:26:06 -08002592 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002593
2594 if (pos + size <= offset + len) {
2595 i++;
2596 pos += size;
2597 } else {
2598 frag->size -= pos + size - (offset + len);
Herbert Xu89319d382008-12-15 23:26:06 -08002599 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002600 }
2601
2602 frag++;
2603 }
2604
Herbert Xu89319d382008-12-15 23:26:06 -08002605 if (pos < offset + len) {
2606 struct sk_buff *fskb2 = fskb;
2607
2608 BUG_ON(pos + fskb->len != offset + len);
2609
2610 pos += fskb->len;
2611 fskb = fskb->next;
2612
2613 if (fskb2->next) {
2614 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2615 if (!fskb2)
2616 goto err;
2617 } else
2618 skb_get(fskb2);
2619
2620 BUG_ON(skb_shinfo(nskb)->frag_list);
2621 skb_shinfo(nskb)->frag_list = fskb2;
2622 }
2623
2624skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002625 nskb->data_len = len - hsize;
2626 nskb->len += nskb->data_len;
2627 nskb->truesize += nskb->data_len;
2628 } while ((offset += len) < skb->len);
2629
2630 return segs;
2631
2632err:
2633 while ((skb = segs)) {
2634 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002635 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002636 }
2637 return ERR_PTR(err);
2638}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002639EXPORT_SYMBOL_GPL(skb_segment);
2640
Herbert Xu71d93b32008-12-15 23:42:33 -08002641int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2642{
2643 struct sk_buff *p = *head;
2644 struct sk_buff *nskb;
2645 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002646 unsigned int len = skb_gro_len(skb);
Herbert Xu71d93b32008-12-15 23:42:33 -08002647
Herbert Xu86911732009-01-29 14:19:50 +00002648 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002649 return -E2BIG;
2650
2651 if (skb_shinfo(p)->frag_list)
2652 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00002653 else if (skb_headlen(skb) <= skb_gro_offset(skb)) {
2654 if (skb_shinfo(p)->nr_frags + skb_shinfo(skb)->nr_frags >
2655 MAX_SKB_FRAGS)
2656 return -E2BIG;
2657
Herbert Xu86911732009-01-29 14:19:50 +00002658 skb_shinfo(skb)->frags[0].page_offset +=
2659 skb_gro_offset(skb) - skb_headlen(skb);
2660 skb_shinfo(skb)->frags[0].size -=
2661 skb_gro_offset(skb) - skb_headlen(skb);
2662
Herbert Xu5d38a072009-01-04 16:13:40 -08002663 memcpy(skb_shinfo(p)->frags + skb_shinfo(p)->nr_frags,
2664 skb_shinfo(skb)->frags,
2665 skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
2666
2667 skb_shinfo(p)->nr_frags += skb_shinfo(skb)->nr_frags;
Herbert Xuf5572062009-01-14 20:40:03 -08002668 skb_shinfo(skb)->nr_frags = 0;
2669
2670 skb->truesize -= skb->data_len;
2671 skb->len -= skb->data_len;
2672 skb->data_len = 0;
2673
Herbert Xu5d38a072009-01-04 16:13:40 -08002674 NAPI_GRO_CB(skb)->free = 1;
2675 goto done;
2676 }
Herbert Xu71d93b32008-12-15 23:42:33 -08002677
2678 headroom = skb_headroom(p);
Herbert Xu86911732009-01-29 14:19:50 +00002679 nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002680 if (unlikely(!nskb))
2681 return -ENOMEM;
2682
2683 __copy_skb_header(nskb, p);
2684 nskb->mac_len = p->mac_len;
2685
2686 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002687 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002688
Herbert Xu86911732009-01-29 14:19:50 +00002689 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002690 skb_set_network_header(nskb, skb_network_offset(p));
2691 skb_set_transport_header(nskb, skb_transport_offset(p));
2692
Herbert Xu86911732009-01-29 14:19:50 +00002693 __skb_pull(p, skb_gro_offset(p));
2694 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2695 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002696
2697 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2698 skb_shinfo(nskb)->frag_list = p;
Herbert Xub5302562009-01-04 16:13:19 -08002699 skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
Herbert Xu71d93b32008-12-15 23:42:33 -08002700 skb_header_release(p);
2701 nskb->prev = p;
2702
2703 nskb->data_len += p->len;
2704 nskb->truesize += p->len;
2705 nskb->len += p->len;
2706
2707 *head = nskb;
2708 nskb->next = p->next;
2709 p->next = NULL;
2710
2711 p = nskb;
2712
2713merge:
Herbert Xu56035022009-02-05 21:26:52 -08002714 if (skb_gro_offset(skb) > skb_headlen(skb)) {
2715 skb_shinfo(skb)->frags[0].page_offset +=
2716 skb_gro_offset(skb) - skb_headlen(skb);
2717 skb_shinfo(skb)->frags[0].size -=
2718 skb_gro_offset(skb) - skb_headlen(skb);
2719 skb_gro_reset_offset(skb);
2720 skb_gro_pull(skb, skb_headlen(skb));
2721 }
2722
2723 __skb_pull(skb, skb_gro_offset(skb));
2724
Herbert Xu71d93b32008-12-15 23:42:33 -08002725 p->prev->next = skb;
2726 p->prev = skb;
2727 skb_header_release(skb);
2728
Herbert Xu5d38a072009-01-04 16:13:40 -08002729done:
2730 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00002731 p->data_len += len;
2732 p->truesize += len;
2733 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08002734
2735 NAPI_GRO_CB(skb)->same_flow = 1;
2736 return 0;
2737}
2738EXPORT_SYMBOL_GPL(skb_gro_receive);
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740void __init skb_init(void)
2741{
2742 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2743 sizeof(struct sk_buff),
2744 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002745 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002746 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002747 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2748 (2*sizeof(struct sk_buff)) +
2749 sizeof(atomic_t),
2750 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002751 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002752 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
David Howells716ea3a2007-04-02 20:19:53 -07002755/**
2756 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2757 * @skb: Socket buffer containing the buffers to be mapped
2758 * @sg: The scatter-gather list to map into
2759 * @offset: The offset into the buffer's contents to start mapping
2760 * @len: Length of buffer space to be mapped
2761 *
2762 * Fill the specified scatter-gather list with mappings/pointers into a
2763 * region of the buffer space attached to a socket buffer.
2764 */
David S. Miller51c739d2007-10-30 21:29:29 -07002765static int
2766__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002767{
David S. Miller1a028e52007-04-27 15:21:23 -07002768 int start = skb_headlen(skb);
2769 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002770 int elt = 0;
2771
2772 if (copy > 0) {
2773 if (copy > len)
2774 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002775 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002776 elt++;
2777 if ((len -= copy) == 0)
2778 return elt;
2779 offset += copy;
2780 }
2781
2782 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002783 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002784
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002785 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002786
2787 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002788 if ((copy = end - offset) > 0) {
2789 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2790
2791 if (copy > len)
2792 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002793 sg_set_page(&sg[elt], frag->page, copy,
2794 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002795 elt++;
2796 if (!(len -= copy))
2797 return elt;
2798 offset += copy;
2799 }
David S. Miller1a028e52007-04-27 15:21:23 -07002800 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002801 }
2802
2803 if (skb_shinfo(skb)->frag_list) {
2804 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2805
2806 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002807 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002808
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002809 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002810
2811 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002812 if ((copy = end - offset) > 0) {
2813 if (copy > len)
2814 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002815 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2816 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002817 if ((len -= copy) == 0)
2818 return elt;
2819 offset += copy;
2820 }
David S. Miller1a028e52007-04-27 15:21:23 -07002821 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002822 }
2823 }
2824 BUG_ON(len);
2825 return elt;
2826}
2827
David S. Miller51c739d2007-10-30 21:29:29 -07002828int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2829{
2830 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2831
Jens Axboec46f2332007-10-31 12:06:37 +01002832 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002833
2834 return nsg;
2835}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002836EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07002837
David Howells716ea3a2007-04-02 20:19:53 -07002838/**
2839 * skb_cow_data - Check that a socket buffer's data buffers are writable
2840 * @skb: The socket buffer to check.
2841 * @tailbits: Amount of trailing space to be added
2842 * @trailer: Returned pointer to the skb where the @tailbits space begins
2843 *
2844 * Make sure that the data buffers attached to a socket buffer are
2845 * writable. If they are not, private copies are made of the data buffers
2846 * and the socket buffer is set to use these instead.
2847 *
2848 * If @tailbits is given, make sure that there is space to write @tailbits
2849 * bytes of data beyond current end of socket buffer. @trailer will be
2850 * set to point to the skb in which this space begins.
2851 *
2852 * The number of scatterlist elements required to completely map the
2853 * COW'd and extended socket buffer will be returned.
2854 */
2855int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2856{
2857 int copyflag;
2858 int elt;
2859 struct sk_buff *skb1, **skb_p;
2860
2861 /* If skb is cloned or its head is paged, reallocate
2862 * head pulling out all the pages (pages are considered not writable
2863 * at the moment even if they are anonymous).
2864 */
2865 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2866 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2867 return -ENOMEM;
2868
2869 /* Easy case. Most of packets will go this way. */
2870 if (!skb_shinfo(skb)->frag_list) {
2871 /* A little of trouble, not enough of space for trailer.
2872 * This should not happen, when stack is tuned to generate
2873 * good frames. OK, on miss we reallocate and reserve even more
2874 * space, 128 bytes is fair. */
2875
2876 if (skb_tailroom(skb) < tailbits &&
2877 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2878 return -ENOMEM;
2879
2880 /* Voila! */
2881 *trailer = skb;
2882 return 1;
2883 }
2884
2885 /* Misery. We are in troubles, going to mincer fragments... */
2886
2887 elt = 1;
2888 skb_p = &skb_shinfo(skb)->frag_list;
2889 copyflag = 0;
2890
2891 while ((skb1 = *skb_p) != NULL) {
2892 int ntail = 0;
2893
2894 /* The fragment is partially pulled by someone,
2895 * this can happen on input. Copy it and everything
2896 * after it. */
2897
2898 if (skb_shared(skb1))
2899 copyflag = 1;
2900
2901 /* If the skb is the last, worry about trailer. */
2902
2903 if (skb1->next == NULL && tailbits) {
2904 if (skb_shinfo(skb1)->nr_frags ||
2905 skb_shinfo(skb1)->frag_list ||
2906 skb_tailroom(skb1) < tailbits)
2907 ntail = tailbits + 128;
2908 }
2909
2910 if (copyflag ||
2911 skb_cloned(skb1) ||
2912 ntail ||
2913 skb_shinfo(skb1)->nr_frags ||
2914 skb_shinfo(skb1)->frag_list) {
2915 struct sk_buff *skb2;
2916
2917 /* Fuck, we are miserable poor guys... */
2918 if (ntail == 0)
2919 skb2 = skb_copy(skb1, GFP_ATOMIC);
2920 else
2921 skb2 = skb_copy_expand(skb1,
2922 skb_headroom(skb1),
2923 ntail,
2924 GFP_ATOMIC);
2925 if (unlikely(skb2 == NULL))
2926 return -ENOMEM;
2927
2928 if (skb1->sk)
2929 skb_set_owner_w(skb2, skb1->sk);
2930
2931 /* Looking around. Are we still alive?
2932 * OK, link new skb, drop old one */
2933
2934 skb2->next = skb1->next;
2935 *skb_p = skb2;
2936 kfree_skb(skb1);
2937 skb1 = skb2;
2938 }
2939 elt++;
2940 *trailer = skb1;
2941 skb_p = &skb1->next;
2942 }
2943
2944 return elt;
2945}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002946EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07002947
Rusty Russellf35d9d82008-02-04 23:49:54 -05002948/**
2949 * skb_partial_csum_set - set up and verify partial csum values for packet
2950 * @skb: the skb to set
2951 * @start: the number of bytes after skb->data to start checksumming.
2952 * @off: the offset from start to place the checksum.
2953 *
2954 * For untrusted partially-checksummed packets, we need to make sure the values
2955 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2956 *
2957 * This function checks and sets those values and skb->ip_summed: if this
2958 * returns false you should drop the packet.
2959 */
2960bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2961{
2962 if (unlikely(start > skb->len - 2) ||
2963 unlikely((int)start + off > skb->len - 2)) {
2964 if (net_ratelimit())
2965 printk(KERN_WARNING
2966 "bad partial csum: csum=%u/%u len=%u\n",
2967 start, off, skb->len);
2968 return false;
2969 }
2970 skb->ip_summed = CHECKSUM_PARTIAL;
2971 skb->csum_start = skb_headroom(skb) + start;
2972 skb->csum_offset = off;
2973 return true;
2974}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002975EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05002976
Ben Hutchings4497b072008-06-19 16:22:28 -07002977void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2978{
2979 if (net_ratelimit())
2980 pr_warning("%s: received packets cannot be forwarded"
2981 " while LRO is enabled\n", skb->dev->name);
2982}
Ben Hutchings4497b072008-06-19 16:22:28 -07002983EXPORT_SYMBOL(__skb_warn_lro_forwarding);