blob: 33640d99c8ed65794458f4a673e45724232ef520 [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>
Patrick Ohlyac45f602009-02-12 05:03:37 +000058#include <linux/errqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <net/protocol.h>
61#include <net/dst.h>
62#include <net/sock.h>
63#include <net/checksum.h>
64#include <net/xfrm.h>
65
66#include <asm/uaccess.h>
67#include <asm/system.h>
68
Al Viroa1f8e7f72006-10-19 16:08:53 -040069#include "kmap_skb.h"
70
Christoph Lametere18b8902006-12-06 20:33:20 -080071static struct kmem_cache *skbuff_head_cache __read_mostly;
72static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jens Axboe9c55e012007-11-06 23:30:13 -080074static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
75 struct pipe_buffer *buf)
76{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080077 put_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080078}
79
80static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
81 struct pipe_buffer *buf)
82{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080083 get_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080084}
85
86static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
87 struct pipe_buffer *buf)
88{
89 return 1;
90}
91
92
93/* Pipe buffer operations for a socket. */
94static struct pipe_buf_operations sock_pipe_buf_ops = {
95 .can_merge = 0,
96 .map = generic_pipe_buf_map,
97 .unmap = generic_pipe_buf_unmap,
98 .confirm = generic_pipe_buf_confirm,
99 .release = sock_pipe_buf_release,
100 .steal = sock_pipe_buf_steal,
101 .get = sock_pipe_buf_get,
102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Keep out-of-line to prevent kernel bloat.
106 * __builtin_return_address is not used because it is not always
107 * reliable.
108 */
109
110/**
111 * skb_over_panic - private function
112 * @skb: buffer
113 * @sz: size
114 * @here: address
115 *
116 * Out of line support code for skb_put(). Not user callable.
117 */
118void skb_over_panic(struct sk_buff *skb, int sz, void *here)
119{
Patrick McHardy26095452005-04-21 16:43:02 -0700120 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700121 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700122 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700123 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700124 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 BUG();
126}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800127EXPORT_SYMBOL(skb_over_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/**
130 * skb_under_panic - private function
131 * @skb: buffer
132 * @sz: size
133 * @here: address
134 *
135 * Out of line support code for skb_push(). Not user callable.
136 */
137
138void skb_under_panic(struct sk_buff *skb, int sz, void *here)
139{
Patrick McHardy26095452005-04-21 16:43:02 -0700140 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700141 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700142 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700143 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700144 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 BUG();
146}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800147EXPORT_SYMBOL(skb_under_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149/* Allocate a new skbuff. We do this ourselves so we can fill in a few
150 * 'private' fields and also do memory statistics to find all the
151 * [BEEP] leaks.
152 *
153 */
154
155/**
David S. Millerd179cd12005-08-17 14:57:30 -0700156 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * @size: size to allocate
158 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700159 * @fclone: allocate from fclone cache instead of head cache
160 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800161 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
163 * Allocate a new &sk_buff. The returned buffer has no headroom and a
164 * tail room of size bytes. The object has a reference count of one.
165 * The return is the buffer. On a failure the return is %NULL.
166 *
167 * Buffers may only be allocated from interrupts using a @gfp_mask of
168 * %GFP_ATOMIC.
169 */
Al Virodd0fc662005-10-07 07:46:04 +0100170struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800171 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Christoph Lametere18b8902006-12-06 20:33:20 -0800173 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800174 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct sk_buff *skb;
176 u8 *data;
177
Herbert Xu8798b3f2006-01-23 16:32:45 -0800178 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800181 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!skb)
183 goto out;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800186 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
187 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!data)
189 goto nodata;
190
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300191 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700192 * Only clear those fields we need to clear, not those that we will
193 * actually initialise below. Hence, don't put any more fields after
194 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300195 */
196 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 skb->truesize = size + sizeof(struct sk_buff);
198 atomic_set(&skb->users, 1);
199 skb->head = data;
200 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700201 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700202 skb->end = skb->tail + size;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800203 /* make sure we initialize shinfo sequentially */
204 shinfo = skb_shinfo(skb);
205 atomic_set(&shinfo->dataref, 1);
206 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700207 shinfo->gso_size = 0;
208 shinfo->gso_segs = 0;
209 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800210 shinfo->ip6_frag_id = 0;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000211 shinfo->tx_flags.flags = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 shinfo->frag_list = NULL;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000213 memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800214
David S. Millerd179cd12005-08-17 14:57:30 -0700215 if (fclone) {
216 struct sk_buff *child = skb + 1;
217 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David S. Millerd179cd12005-08-17 14:57:30 -0700219 skb->fclone = SKB_FCLONE_ORIG;
220 atomic_set(fclone_ref, 1);
221
222 child->fclone = SKB_FCLONE_UNAVAILABLE;
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224out:
225 return skb;
226nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800227 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 skb = NULL;
229 goto out;
230}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800231EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700234 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
235 * @dev: network device to receive on
236 * @length: length to allocate
237 * @gfp_mask: get_free_pages mask, passed to alloc_skb
238 *
239 * Allocate a new &sk_buff and assign it a usage count of one. The
240 * buffer has unspecified headroom built in. Users should allocate
241 * the headroom they think they need without accounting for the
242 * built in space. The built in space is used for optimisations.
243 *
244 * %NULL is returned if there is no free memory.
245 */
246struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
247 unsigned int length, gfp_t gfp_mask)
248{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700249 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700250 struct sk_buff *skb;
251
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900252 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700253 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700254 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700255 skb->dev = dev;
256 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700257 return skb;
258}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800259EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Peter Zijlstra654bed12008-10-07 14:22:33 -0700261struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
262{
263 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
264 struct page *page;
265
266 page = alloc_pages_node(node, gfp_mask, 0);
267 return page;
268}
269EXPORT_SYMBOL(__netdev_alloc_page);
270
271void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
272 int size)
273{
274 skb_fill_page_desc(skb, i, page, off, size);
275 skb->len += size;
276 skb->data_len += size;
277 skb->truesize += size;
278}
279EXPORT_SYMBOL(skb_add_rx_frag);
280
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700281/**
282 * dev_alloc_skb - allocate an skbuff for receiving
283 * @length: length to allocate
284 *
285 * Allocate a new &sk_buff and assign it a usage count of one. The
286 * buffer has unspecified headroom built in. Users should allocate
287 * the headroom they think they need without accounting for the
288 * built in space. The built in space is used for optimisations.
289 *
290 * %NULL is returned if there is no free memory. Although this function
291 * allocates memory it can be called from an interrupt.
292 */
293struct sk_buff *dev_alloc_skb(unsigned int length)
294{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700295 /*
296 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700297 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700298 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700299 return __dev_alloc_skb(length, GFP_ATOMIC);
300}
301EXPORT_SYMBOL(dev_alloc_skb);
302
Herbert Xu27b437c2006-07-13 19:26:39 -0700303static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Herbert Xu27b437c2006-07-13 19:26:39 -0700305 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Herbert Xu27b437c2006-07-13 19:26:39 -0700307 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 do {
310 struct sk_buff *this = list;
311 list = list->next;
312 kfree_skb(this);
313 } while (list);
314}
315
Herbert Xu27b437c2006-07-13 19:26:39 -0700316static inline void skb_drop_fraglist(struct sk_buff *skb)
317{
318 skb_drop_list(&skb_shinfo(skb)->frag_list);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void skb_clone_fraglist(struct sk_buff *skb)
322{
323 struct sk_buff *list;
324
325 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
326 skb_get(list);
327}
328
Adrian Bunk5bba1712006-06-29 13:02:35 -0700329static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 if (!skb->cloned ||
332 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
333 &skb_shinfo(skb)->dataref)) {
334 if (skb_shinfo(skb)->nr_frags) {
335 int i;
336 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
337 put_page(skb_shinfo(skb)->frags[i].page);
338 }
339
340 if (skb_shinfo(skb)->frag_list)
341 skb_drop_fraglist(skb);
342
343 kfree(skb->head);
344 }
345}
346
347/*
348 * Free an skbuff by memory without cleaning the state.
349 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800350static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David S. Millerd179cd12005-08-17 14:57:30 -0700352 struct sk_buff *other;
353 atomic_t *fclone_ref;
354
David S. Millerd179cd12005-08-17 14:57:30 -0700355 switch (skb->fclone) {
356 case SKB_FCLONE_UNAVAILABLE:
357 kmem_cache_free(skbuff_head_cache, skb);
358 break;
359
360 case SKB_FCLONE_ORIG:
361 fclone_ref = (atomic_t *) (skb + 2);
362 if (atomic_dec_and_test(fclone_ref))
363 kmem_cache_free(skbuff_fclone_cache, skb);
364 break;
365
366 case SKB_FCLONE_CLONE:
367 fclone_ref = (atomic_t *) (skb + 1);
368 other = skb - 1;
369
370 /* The clone portion is available for
371 * fast-cloning again.
372 */
373 skb->fclone = SKB_FCLONE_UNAVAILABLE;
374
375 if (atomic_dec_and_test(fclone_ref))
376 kmem_cache_free(skbuff_fclone_cache, other);
377 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700381static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dst_release(skb->dst);
384#ifdef CONFIG_XFRM
385 secpath_put(skb->sp);
386#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700387 if (skb->destructor) {
388 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 skb->destructor(skb);
390 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800391#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700392 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800393 nf_conntrack_put_reasm(skb->nfct_reasm);
394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_BRIDGE_NETFILTER
396 nf_bridge_put(skb->nf_bridge);
397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* XXX: IS this still necessary? - JHS */
399#ifdef CONFIG_NET_SCHED
400 skb->tc_index = 0;
401#ifdef CONFIG_NET_CLS_ACT
402 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif
404#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700405}
406
407/* Free everything but the sk_buff shell. */
408static void skb_release_all(struct sk_buff *skb)
409{
410 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800411 skb_release_data(skb);
412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Herbert Xu2d4baff2007-11-26 23:11:19 +0800414/**
415 * __kfree_skb - private function
416 * @skb: buffer
417 *
418 * Free an sk_buff. Release anything attached to the buffer.
419 * Clean the state. This is an internal helper function. Users should
420 * always call kfree_skb
421 */
422
423void __kfree_skb(struct sk_buff *skb)
424{
425 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kfree_skbmem(skb);
427}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800428EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800431 * kfree_skb - free an sk_buff
432 * @skb: buffer to free
433 *
434 * Drop a reference to the buffer and free it if the usage count has
435 * hit zero.
436 */
437void kfree_skb(struct sk_buff *skb)
438{
439 if (unlikely(!skb))
440 return;
441 if (likely(atomic_read(&skb->users) == 1))
442 smp_rmb();
443 else if (likely(!atomic_dec_and_test(&skb->users)))
444 return;
445 __kfree_skb(skb);
446}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800447EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800448
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700449/**
450 * skb_recycle_check - check if skb can be reused for receive
451 * @skb: buffer
452 * @skb_size: minimum receive buffer size
453 *
454 * Checks that the skb passed in is not shared or cloned, and
455 * that it is linear and its head portion at least as large as
456 * skb_size so that it can be recycled as a receive buffer.
457 * If these conditions are met, this function does any necessary
458 * reference count dropping and cleans up the skbuff as if it
459 * just came from __alloc_skb().
460 */
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700461int skb_recycle_check(struct sk_buff *skb, int skb_size)
462{
463 struct skb_shared_info *shinfo;
464
465 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
466 return 0;
467
468 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
469 if (skb_end_pointer(skb) - skb->head < skb_size)
470 return 0;
471
472 if (skb_shared(skb) || skb_cloned(skb))
473 return 0;
474
475 skb_release_head_state(skb);
476 shinfo = skb_shinfo(skb);
477 atomic_set(&shinfo->dataref, 1);
478 shinfo->nr_frags = 0;
479 shinfo->gso_size = 0;
480 shinfo->gso_segs = 0;
481 shinfo->gso_type = 0;
482 shinfo->ip6_frag_id = 0;
483 shinfo->frag_list = NULL;
484
485 memset(skb, 0, offsetof(struct sk_buff, tail));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700486 skb->data = skb->head + NET_SKB_PAD;
Lennert Buytenhek5cd33db2008-11-10 21:45:05 -0800487 skb_reset_tail_pointer(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700488
489 return 1;
490}
491EXPORT_SYMBOL(skb_recycle_check);
492
Herbert Xudec18812007-10-14 00:37:30 -0700493static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
494{
495 new->tstamp = old->tstamp;
496 new->dev = old->dev;
497 new->transport_header = old->transport_header;
498 new->network_header = old->network_header;
499 new->mac_header = old->mac_header;
500 new->dst = dst_clone(old->dst);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700501#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700502 new->sp = secpath_get(old->sp);
503#endif
504 memcpy(new->cb, old->cb, sizeof(old->cb));
505 new->csum_start = old->csum_start;
506 new->csum_offset = old->csum_offset;
507 new->local_df = old->local_df;
508 new->pkt_type = old->pkt_type;
509 new->ip_summed = old->ip_summed;
510 skb_copy_queue_mapping(new, old);
511 new->priority = old->priority;
512#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
513 new->ipvs_property = old->ipvs_property;
514#endif
515 new->protocol = old->protocol;
516 new->mark = old->mark;
517 __nf_copy(new, old);
518#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
519 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
520 new->nf_trace = old->nf_trace;
521#endif
522#ifdef CONFIG_NET_SCHED
523 new->tc_index = old->tc_index;
524#ifdef CONFIG_NET_CLS_ACT
525 new->tc_verd = old->tc_verd;
526#endif
527#endif
Patrick McHardy6aa895b2008-07-14 22:49:06 -0700528 new->vlan_tci = old->vlan_tci;
529
Herbert Xudec18812007-10-14 00:37:30 -0700530 skb_copy_secmark(new, old);
531}
532
Herbert Xue0053ec2007-10-14 00:37:52 -0700533static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#define C(x) n->x = skb->x
536
537 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700539 __copy_skb_header(n, skb);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 C(len);
542 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700543 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700544 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800545 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800548 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 C(tail);
550 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800551 C(head);
552 C(data);
553 C(truesize);
Johannes Bergd0f09802008-07-29 11:32:07 +0200554#if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
555 C(do_not_encrypt);
Sujith8b30b1f2008-10-24 09:55:27 +0530556 C(requeue);
Johannes Bergd0f09802008-07-29 11:32:07 +0200557#endif
Paul Moore02f1c892008-01-07 21:56:41 -0800558 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 atomic_inc(&(skb_shinfo(skb)->dataref));
561 skb->cloned = 1;
562
563 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700564#undef C
565}
566
567/**
568 * skb_morph - morph one skb into another
569 * @dst: the skb to receive the contents
570 * @src: the skb to supply the contents
571 *
572 * This is identical to skb_clone except that the target skb is
573 * supplied by the user.
574 *
575 * The target skb is returned upon exit.
576 */
577struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
578{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800579 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700580 return __skb_clone(dst, src);
581}
582EXPORT_SYMBOL_GPL(skb_morph);
583
584/**
585 * skb_clone - duplicate an sk_buff
586 * @skb: buffer to clone
587 * @gfp_mask: allocation priority
588 *
589 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
590 * copies share the same packet data but not structure. The new
591 * buffer has a reference count of 1. If the allocation fails the
592 * function returns %NULL otherwise the new buffer is returned.
593 *
594 * If this function is called from an interrupt gfp_mask() must be
595 * %GFP_ATOMIC.
596 */
597
598struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
599{
600 struct sk_buff *n;
601
602 n = skb + 1;
603 if (skb->fclone == SKB_FCLONE_ORIG &&
604 n->fclone == SKB_FCLONE_UNAVAILABLE) {
605 atomic_t *fclone_ref = (atomic_t *) (n + 1);
606 n->fclone = SKB_FCLONE_CLONE;
607 atomic_inc(fclone_ref);
608 } else {
609 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
610 if (!n)
611 return NULL;
612 n->fclone = SKB_FCLONE_UNAVAILABLE;
613 }
614
615 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800617EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
620{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700621#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Shift between the two data areas in bytes
624 */
625 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700626#endif
Herbert Xudec18812007-10-14 00:37:30 -0700627
628 __copy_skb_header(new, old);
629
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700630#ifndef NET_SKBUFF_DATA_USES_OFFSET
631 /* {transport,network,mac}_header are relative to skb->head */
632 new->transport_header += offset;
633 new->network_header += offset;
634 new->mac_header += offset;
635#endif
Herbert Xu79671682006-06-22 02:40:14 -0700636 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
637 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
638 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/**
642 * skb_copy - create private copy of an sk_buff
643 * @skb: buffer to copy
644 * @gfp_mask: allocation priority
645 *
646 * Make a copy of both an &sk_buff and its data. This is used when the
647 * caller wishes to modify the data and needs a private copy of the
648 * data to alter. Returns %NULL on failure or the pointer to the buffer
649 * on success. The returned buffer has a reference count of 1.
650 *
651 * As by-product this function converts non-linear &sk_buff to linear
652 * one, so that &sk_buff becomes completely private and caller is allowed
653 * to modify all the data of returned buffer. This means that this
654 * function is not recommended for use in circumstances when only
655 * header is going to be modified. Use pskb_copy() instead.
656 */
657
Al Virodd0fc662005-10-07 07:46:04 +0100658struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 int headerlen = skb->data - skb->head;
661 /*
662 * Allocate the copy buffer
663 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700664 struct sk_buff *n;
665#ifdef NET_SKBUFF_DATA_USES_OFFSET
666 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
667#else
668 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!n)
671 return NULL;
672
673 /* Set the data pointer */
674 skb_reserve(n, headerlen);
675 /* Set the tail pointer and length */
676 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
679 BUG();
680
681 copy_skb_header(n, skb);
682 return n;
683}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800684EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686/**
687 * pskb_copy - create copy of an sk_buff with private head.
688 * @skb: buffer to copy
689 * @gfp_mask: allocation priority
690 *
691 * Make a copy of both an &sk_buff and part of its data, located
692 * in header. Fragmented data remain shared. This is used when
693 * the caller wishes to modify only header of &sk_buff and needs
694 * private copy of the header to alter. Returns %NULL on failure
695 * or the pointer to the buffer on success.
696 * The returned buffer has a reference count of 1.
697 */
698
Al Virodd0fc662005-10-07 07:46:04 +0100699struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 /*
702 * Allocate the copy buffer
703 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700704 struct sk_buff *n;
705#ifdef NET_SKBUFF_DATA_USES_OFFSET
706 n = alloc_skb(skb->end, gfp_mask);
707#else
708 n = alloc_skb(skb->end - skb->head, gfp_mask);
709#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!n)
711 goto out;
712
713 /* Set the data pointer */
714 skb_reserve(n, skb->data - skb->head);
715 /* Set the tail pointer and length */
716 skb_put(n, skb_headlen(skb));
717 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300718 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Herbert Xu25f484a2006-11-07 14:57:15 -0800720 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 n->data_len = skb->data_len;
722 n->len = skb->len;
723
724 if (skb_shinfo(skb)->nr_frags) {
725 int i;
726
727 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
728 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
729 get_page(skb_shinfo(n)->frags[i].page);
730 }
731 skb_shinfo(n)->nr_frags = i;
732 }
733
734 if (skb_shinfo(skb)->frag_list) {
735 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
736 skb_clone_fraglist(n);
737 }
738
739 copy_skb_header(n, skb);
740out:
741 return n;
742}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800743EXPORT_SYMBOL(pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745/**
746 * pskb_expand_head - reallocate header of &sk_buff
747 * @skb: buffer to reallocate
748 * @nhead: room to add at head
749 * @ntail: room to add at tail
750 * @gfp_mask: allocation priority
751 *
752 * Expands (or creates identical copy, if &nhead and &ntail are zero)
753 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
754 * reference count of 1. Returns zero in the case of success or error,
755 * if expansion failed. In the last case, &sk_buff is not changed.
756 *
757 * All the pointers pointing into skb header may change and must be
758 * reloaded after call to this function.
759 */
760
Victor Fusco86a76ca2005-07-08 14:57:47 -0700761int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100762 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 int i;
765 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700766#ifdef NET_SKBUFF_DATA_USES_OFFSET
767 int size = nhead + skb->end + ntail;
768#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700770#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 long off;
772
Herbert Xu4edd87a2008-10-01 07:09:38 -0700773 BUG_ON(nhead < 0);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (skb_shared(skb))
776 BUG();
777
778 size = SKB_DATA_ALIGN(size);
779
780 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
781 if (!data)
782 goto nodata;
783
784 /* Copy only real data... and, alas, header. This should be
785 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700786#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700787 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700788#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700789 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700790#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700791 memcpy(data + size, skb_end_pointer(skb),
792 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
795 get_page(skb_shinfo(skb)->frags[i].page);
796
797 if (skb_shinfo(skb)->frag_list)
798 skb_clone_fraglist(skb);
799
800 skb_release_data(skb);
801
802 off = (data + nhead) - skb->head;
803
804 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700806#ifdef NET_SKBUFF_DATA_USES_OFFSET
807 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700808 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700809#else
810 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700811#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700812 /* {transport,network,mac}_header and tail are relative to skb->head */
813 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700814 skb->transport_header += off;
815 skb->network_header += off;
816 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700817 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700819 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 skb->nohdr = 0;
821 atomic_set(&skb_shinfo(skb)->dataref, 1);
822 return 0;
823
824nodata:
825 return -ENOMEM;
826}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800827EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829/* Make private copy of skb with writable head and some headroom */
830
831struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
832{
833 struct sk_buff *skb2;
834 int delta = headroom - skb_headroom(skb);
835
836 if (delta <= 0)
837 skb2 = pskb_copy(skb, GFP_ATOMIC);
838 else {
839 skb2 = skb_clone(skb, GFP_ATOMIC);
840 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
841 GFP_ATOMIC)) {
842 kfree_skb(skb2);
843 skb2 = NULL;
844 }
845 }
846 return skb2;
847}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800848EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/**
851 * skb_copy_expand - copy and expand sk_buff
852 * @skb: buffer to copy
853 * @newheadroom: new free bytes at head
854 * @newtailroom: new free bytes at tail
855 * @gfp_mask: allocation priority
856 *
857 * Make a copy of both an &sk_buff and its data and while doing so
858 * allocate additional space.
859 *
860 * This is used when the caller wishes to modify the data and needs a
861 * private copy of the data to alter as well as more space for new fields.
862 * Returns %NULL on failure or the pointer to the buffer
863 * on success. The returned buffer has a reference count of 1.
864 *
865 * You must pass %GFP_ATOMIC as the allocation priority if this function
866 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
868struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700869 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100870 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 /*
873 * Allocate the copy buffer
874 */
875 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
876 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700877 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700879 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (!n)
882 return NULL;
883
884 skb_reserve(n, newheadroom);
885
886 /* Set the tail pointer and length */
887 skb_put(n, skb->len);
888
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700889 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 head_copy_off = 0;
891 if (newheadroom <= head_copy_len)
892 head_copy_len = newheadroom;
893 else
894 head_copy_off = newheadroom - head_copy_len;
895
896 /* Copy the linear header and data. */
897 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
898 skb->len + head_copy_len))
899 BUG();
900
901 copy_skb_header(n, skb);
902
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700903 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700904 n->csum_start += off;
905#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700906 n->transport_header += off;
907 n->network_header += off;
908 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700909#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return n;
912}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800913EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915/**
916 * skb_pad - zero pad the tail of an skb
917 * @skb: buffer to pad
918 * @pad: space to pad
919 *
920 * Ensure that a buffer is followed by a padding area that is zero
921 * filled. Used by network drivers which may DMA or transfer data
922 * beyond the buffer end onto the wire.
923 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700924 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900926
Herbert Xu5b057c62006-06-23 02:06:41 -0700927int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Herbert Xu5b057c62006-06-23 02:06:41 -0700929 int err;
930 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700933 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700937
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700938 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700939 if (likely(skb_cloned(skb) || ntail > 0)) {
940 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
941 if (unlikely(err))
942 goto free_skb;
943 }
944
945 /* FIXME: The use of this function with non-linear skb's really needs
946 * to be audited.
947 */
948 err = skb_linearize(skb);
949 if (unlikely(err))
950 goto free_skb;
951
952 memset(skb->data + skb->len, 0, pad);
953 return 0;
954
955free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700957 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900958}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800959EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900960
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700961/**
962 * skb_put - add data to a buffer
963 * @skb: buffer to use
964 * @len: amount of data to add
965 *
966 * This function extends the used data area of the buffer. If this would
967 * exceed the total buffer size the kernel will panic. A pointer to the
968 * first byte of the extra data is returned.
969 */
970unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
971{
972 unsigned char *tmp = skb_tail_pointer(skb);
973 SKB_LINEAR_ASSERT(skb);
974 skb->tail += len;
975 skb->len += len;
976 if (unlikely(skb->tail > skb->end))
977 skb_over_panic(skb, len, __builtin_return_address(0));
978 return tmp;
979}
980EXPORT_SYMBOL(skb_put);
981
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -0700982/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -0700983 * skb_push - add data to the start of a buffer
984 * @skb: buffer to use
985 * @len: amount of data to add
986 *
987 * This function extends the used data area of the buffer at the buffer
988 * start. If this would exceed the total buffer headroom the kernel will
989 * panic. A pointer to the first byte of the extra data is returned.
990 */
991unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
992{
993 skb->data -= len;
994 skb->len += len;
995 if (unlikely(skb->data<skb->head))
996 skb_under_panic(skb, len, __builtin_return_address(0));
997 return skb->data;
998}
999EXPORT_SYMBOL(skb_push);
1000
1001/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001002 * skb_pull - remove data from the start of a buffer
1003 * @skb: buffer to use
1004 * @len: amount of data to remove
1005 *
1006 * This function removes data from the start of a buffer, returning
1007 * the memory to the headroom. A pointer to the next data in the buffer
1008 * is returned. Once the data has been pulled future pushes will overwrite
1009 * the old data.
1010 */
1011unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1012{
1013 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1014}
1015EXPORT_SYMBOL(skb_pull);
1016
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001017/**
1018 * skb_trim - remove end from a buffer
1019 * @skb: buffer to alter
1020 * @len: new length
1021 *
1022 * Cut the length of a buffer down by removing data from the tail. If
1023 * the buffer is already under the length specified it is not modified.
1024 * The skb must be linear.
1025 */
1026void skb_trim(struct sk_buff *skb, unsigned int len)
1027{
1028 if (skb->len > len)
1029 __skb_trim(skb, len);
1030}
1031EXPORT_SYMBOL(skb_trim);
1032
Herbert Xu3cc0e872006-06-09 16:13:38 -07001033/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
1035
Herbert Xu3cc0e872006-06-09 16:13:38 -07001036int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Herbert Xu27b437c2006-07-13 19:26:39 -07001038 struct sk_buff **fragp;
1039 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int offset = skb_headlen(skb);
1041 int nfrags = skb_shinfo(skb)->nr_frags;
1042 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001043 int err;
1044
1045 if (skb_cloned(skb) &&
1046 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001049 i = 0;
1050 if (offset >= len)
1051 goto drop_pages;
1052
1053 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -07001055
1056 if (end < len) {
1057 offset = end;
1058 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001060
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001061 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -07001062
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001063drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001064 skb_shinfo(skb)->nr_frags = i;
1065
1066 for (; i < nfrags; i++)
1067 put_page(skb_shinfo(skb)->frags[i].page);
1068
1069 if (skb_shinfo(skb)->frag_list)
1070 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001071 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
Herbert Xu27b437c2006-07-13 19:26:39 -07001074 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1075 fragp = &frag->next) {
1076 int end = offset + frag->len;
1077
1078 if (skb_shared(frag)) {
1079 struct sk_buff *nfrag;
1080
1081 nfrag = skb_clone(frag, GFP_ATOMIC);
1082 if (unlikely(!nfrag))
1083 return -ENOMEM;
1084
1085 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001086 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001087 frag = nfrag;
1088 *fragp = frag;
1089 }
1090
1091 if (end < len) {
1092 offset = end;
1093 continue;
1094 }
1095
1096 if (end > len &&
1097 unlikely((err = pskb_trim(frag, len - offset))))
1098 return err;
1099
1100 if (frag->next)
1101 skb_drop_list(&frag->next);
1102 break;
1103 }
1104
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001105done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001106 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 skb->data_len -= skb->len - len;
1108 skb->len = len;
1109 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001110 skb->len = len;
1111 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001112 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
1115 return 0;
1116}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001117EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119/**
1120 * __pskb_pull_tail - advance tail of skb header
1121 * @skb: buffer to reallocate
1122 * @delta: number of bytes to advance tail
1123 *
1124 * The function makes a sense only on a fragmented &sk_buff,
1125 * it expands header moving its tail forward and copying necessary
1126 * data from fragmented part.
1127 *
1128 * &sk_buff MUST have reference count of 1.
1129 *
1130 * Returns %NULL (and &sk_buff does not change) if pull failed
1131 * or value of new tail of skb in the case of success.
1132 *
1133 * All the pointers pointing into skb header may change and must be
1134 * reloaded after call to this function.
1135 */
1136
1137/* Moves tail of skb head forward, copying data from fragmented part,
1138 * when it is necessary.
1139 * 1. It may fail due to malloc failure.
1140 * 2. It may change skb pointers.
1141 *
1142 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1143 */
1144unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1145{
1146 /* If skb has not enough free space at tail, get new one
1147 * plus 128 bytes for future expansions. If we have enough
1148 * room at tail, reallocate without expansion only if skb is cloned.
1149 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001150 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (eat > 0 || skb_cloned(skb)) {
1153 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1154 GFP_ATOMIC))
1155 return NULL;
1156 }
1157
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001158 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 BUG();
1160
1161 /* Optimization: no fragments, no reasons to preestimate
1162 * size of pulled pages. Superb.
1163 */
1164 if (!skb_shinfo(skb)->frag_list)
1165 goto pull_pages;
1166
1167 /* Estimate size of pulled pages. */
1168 eat = delta;
1169 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1170 if (skb_shinfo(skb)->frags[i].size >= eat)
1171 goto pull_pages;
1172 eat -= skb_shinfo(skb)->frags[i].size;
1173 }
1174
1175 /* If we need update frag list, we are in troubles.
1176 * Certainly, it possible to add an offset to skb data,
1177 * but taking into account that pulling is expected to
1178 * be very rare operation, it is worth to fight against
1179 * further bloating skb head and crucify ourselves here instead.
1180 * Pure masohism, indeed. 8)8)
1181 */
1182 if (eat) {
1183 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1184 struct sk_buff *clone = NULL;
1185 struct sk_buff *insp = NULL;
1186
1187 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001188 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 if (list->len <= eat) {
1191 /* Eaten as whole. */
1192 eat -= list->len;
1193 list = list->next;
1194 insp = list;
1195 } else {
1196 /* Eaten partially. */
1197
1198 if (skb_shared(list)) {
1199 /* Sucks! We need to fork list. :-( */
1200 clone = skb_clone(list, GFP_ATOMIC);
1201 if (!clone)
1202 return NULL;
1203 insp = list->next;
1204 list = clone;
1205 } else {
1206 /* This may be pulled without
1207 * problems. */
1208 insp = list;
1209 }
1210 if (!pskb_pull(list, eat)) {
1211 if (clone)
1212 kfree_skb(clone);
1213 return NULL;
1214 }
1215 break;
1216 }
1217 } while (eat);
1218
1219 /* Free pulled out fragments. */
1220 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1221 skb_shinfo(skb)->frag_list = list->next;
1222 kfree_skb(list);
1223 }
1224 /* And insert new clone at head. */
1225 if (clone) {
1226 clone->next = list;
1227 skb_shinfo(skb)->frag_list = clone;
1228 }
1229 }
1230 /* Success! Now we may commit changes to skb data. */
1231
1232pull_pages:
1233 eat = delta;
1234 k = 0;
1235 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1236 if (skb_shinfo(skb)->frags[i].size <= eat) {
1237 put_page(skb_shinfo(skb)->frags[i].page);
1238 eat -= skb_shinfo(skb)->frags[i].size;
1239 } else {
1240 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1241 if (eat) {
1242 skb_shinfo(skb)->frags[k].page_offset += eat;
1243 skb_shinfo(skb)->frags[k].size -= eat;
1244 eat = 0;
1245 }
1246 k++;
1247 }
1248 }
1249 skb_shinfo(skb)->nr_frags = k;
1250
1251 skb->tail += delta;
1252 skb->data_len -= delta;
1253
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001254 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001256EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258/* Copy some data bits from skb to kernel buffer. */
1259
1260int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1261{
1262 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001263 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (offset > (int)skb->len - len)
1266 goto fault;
1267
1268 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001269 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (copy > len)
1271 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001272 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if ((len -= copy) == 0)
1274 return 0;
1275 offset += copy;
1276 to += copy;
1277 }
1278
1279 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001280 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001282 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001283
1284 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if ((copy = end - offset) > 0) {
1286 u8 *vaddr;
1287
1288 if (copy > len)
1289 copy = len;
1290
1291 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1292 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001293 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1294 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kunmap_skb_frag(vaddr);
1296
1297 if ((len -= copy) == 0)
1298 return 0;
1299 offset += copy;
1300 to += copy;
1301 }
David S. Miller1a028e52007-04-27 15:21:23 -07001302 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304
1305 if (skb_shinfo(skb)->frag_list) {
1306 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1307
1308 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001309 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001311 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001312
1313 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((copy = end - offset) > 0) {
1315 if (copy > len)
1316 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001317 if (skb_copy_bits(list, offset - start,
1318 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto fault;
1320 if ((len -= copy) == 0)
1321 return 0;
1322 offset += copy;
1323 to += copy;
1324 }
David S. Miller1a028e52007-04-27 15:21:23 -07001325 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 }
1328 if (!len)
1329 return 0;
1330
1331fault:
1332 return -EFAULT;
1333}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001334EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jens Axboe9c55e012007-11-06 23:30:13 -08001336/*
1337 * Callback from splice_to_pipe(), if we need to release some pages
1338 * at the end of the spd in case we error'ed out in filling the pipe.
1339 */
1340static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1341{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001342 put_page(spd->pages[i]);
1343}
Jens Axboe9c55e012007-11-06 23:30:13 -08001344
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001345static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1346 unsigned int *offset,
1347 struct sk_buff *skb)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001348{
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001349 struct sock *sk = skb->sk;
1350 struct page *p = sk->sk_sndmsg_page;
1351 unsigned int off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001352
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001353 if (!p) {
1354new_page:
1355 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1356 if (!p)
1357 return NULL;
1358
1359 off = sk->sk_sndmsg_off = 0;
1360 /* hold one ref to this page until it's full */
1361 } else {
1362 unsigned int mlen;
1363
1364 off = sk->sk_sndmsg_off;
1365 mlen = PAGE_SIZE - off;
1366 if (mlen < 64 && mlen < *len) {
1367 put_page(p);
1368 goto new_page;
1369 }
1370
1371 *len = min_t(unsigned int, *len, mlen);
1372 }
1373
1374 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1375 sk->sk_sndmsg_off += *len;
1376 *offset = off;
1377 get_page(p);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001378
1379 return p;
Jens Axboe9c55e012007-11-06 23:30:13 -08001380}
1381
1382/*
1383 * Fill page/offset/length into spd, if it can hold more pages.
1384 */
1385static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001386 unsigned int *len, unsigned int offset,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001387 struct sk_buff *skb, int linear)
Jens Axboe9c55e012007-11-06 23:30:13 -08001388{
1389 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1390 return 1;
1391
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001392 if (linear) {
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001393 page = linear_to_page(page, len, &offset, skb);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001394 if (!page)
1395 return 1;
1396 } else
1397 get_page(page);
1398
Jens Axboe9c55e012007-11-06 23:30:13 -08001399 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001400 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001401 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001402 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001403
Jens Axboe9c55e012007-11-06 23:30:13 -08001404 return 0;
1405}
1406
Octavian Purdila2870c432008-07-15 00:49:11 -07001407static inline void __segment_seek(struct page **page, unsigned int *poff,
1408 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001409{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001410 unsigned long n;
1411
Octavian Purdila2870c432008-07-15 00:49:11 -07001412 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001413 n = *poff / PAGE_SIZE;
1414 if (n)
1415 *page = nth_page(*page, n);
1416
Octavian Purdila2870c432008-07-15 00:49:11 -07001417 *poff = *poff % PAGE_SIZE;
1418 *plen -= off;
1419}
Jens Axboe9c55e012007-11-06 23:30:13 -08001420
Octavian Purdila2870c432008-07-15 00:49:11 -07001421static inline int __splice_segment(struct page *page, unsigned int poff,
1422 unsigned int plen, unsigned int *off,
1423 unsigned int *len, struct sk_buff *skb,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001424 struct splice_pipe_desc *spd, int linear)
Octavian Purdila2870c432008-07-15 00:49:11 -07001425{
1426 if (!*len)
1427 return 1;
1428
1429 /* skip this segment if already processed */
1430 if (*off >= plen) {
1431 *off -= plen;
1432 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001433 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001434
Octavian Purdila2870c432008-07-15 00:49:11 -07001435 /* ignore any bits we already processed */
1436 if (*off) {
1437 __segment_seek(&page, &poff, &plen, *off);
1438 *off = 0;
1439 }
1440
1441 do {
1442 unsigned int flen = min(*len, plen);
1443
1444 /* the linear region may spread across several pages */
1445 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1446
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001447 if (spd_fill_page(spd, page, &flen, poff, skb, linear))
Octavian Purdila2870c432008-07-15 00:49:11 -07001448 return 1;
1449
1450 __segment_seek(&page, &poff, &plen, flen);
1451 *len -= flen;
1452
1453 } while (*len && plen);
1454
1455 return 0;
1456}
1457
1458/*
1459 * Map linear and fragment data from the skb to spd. It reports failure if the
1460 * pipe is full or if we already spliced the requested length.
1461 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001462static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Octavian Purdila2870c432008-07-15 00:49:11 -07001463 unsigned int *len,
1464 struct splice_pipe_desc *spd)
1465{
1466 int seg;
1467
Jens Axboe9c55e012007-11-06 23:30:13 -08001468 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001469 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001470 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001471 if (__splice_segment(virt_to_page(skb->data),
1472 (unsigned long) skb->data & (PAGE_SIZE - 1),
1473 skb_headlen(skb),
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001474 offset, len, skb, spd, 1))
Octavian Purdila2870c432008-07-15 00:49:11 -07001475 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001476
1477 /*
1478 * then map the fragments
1479 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001480 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1481 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1482
Octavian Purdila2870c432008-07-15 00:49:11 -07001483 if (__splice_segment(f->page, f->page_offset, f->size,
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001484 offset, len, skb, spd, 0))
Octavian Purdila2870c432008-07-15 00:49:11 -07001485 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001486 }
1487
Octavian Purdila2870c432008-07-15 00:49:11 -07001488 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001489}
1490
1491/*
1492 * Map data from the skb to a pipe. Should handle both the linear part,
1493 * the fragments, and the frag list. It does NOT handle frag lists within
1494 * the frag list, if such a thing exists. We'd probably need to recurse to
1495 * handle that cleanly.
1496 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001497int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001498 struct pipe_inode_info *pipe, unsigned int tlen,
1499 unsigned int flags)
1500{
1501 struct partial_page partial[PIPE_BUFFERS];
1502 struct page *pages[PIPE_BUFFERS];
1503 struct splice_pipe_desc spd = {
1504 .pages = pages,
1505 .partial = partial,
1506 .flags = flags,
1507 .ops = &sock_pipe_buf_ops,
1508 .spd_release = sock_spd_release,
1509 };
Jens Axboe9c55e012007-11-06 23:30:13 -08001510
1511 /*
1512 * __skb_splice_bits() only fails if the output has no room left,
1513 * so no point in going over the frag_list for the error case.
1514 */
1515 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1516 goto done;
1517 else if (!tlen)
1518 goto done;
1519
1520 /*
1521 * now see if we have a frag_list to map
1522 */
1523 if (skb_shinfo(skb)->frag_list) {
1524 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1525
1526 for (; list && tlen; list = list->next) {
1527 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1528 break;
1529 }
1530 }
1531
1532done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001533 if (spd.nr_pages) {
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001534 struct sock *sk = skb->sk;
Jens Axboe9c55e012007-11-06 23:30:13 -08001535 int ret;
1536
1537 /*
1538 * Drop the socket lock, otherwise we have reverse
1539 * locking dependencies between sk_lock and i_mutex
1540 * here as compared to sendfile(). We enter here
1541 * with the socket lock held, and splice_to_pipe() will
1542 * grab the pipe inode lock. For sendfile() emulation,
1543 * we call into ->sendpage() with the i_mutex lock held
1544 * and networking will grab the socket lock.
1545 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001546 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001547 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001548 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001549 return ret;
1550 }
1551
1552 return 0;
1553}
1554
Herbert Xu357b40a2005-04-19 22:30:14 -07001555/**
1556 * skb_store_bits - store bits from kernel buffer to skb
1557 * @skb: destination buffer
1558 * @offset: offset in destination
1559 * @from: source buffer
1560 * @len: number of bytes to copy
1561 *
1562 * Copy the specified number of bytes from the source buffer to the
1563 * destination skb. This function handles all the messy bits of
1564 * traversing fragment lists and such.
1565 */
1566
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001567int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001568{
1569 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001570 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001571
1572 if (offset > (int)skb->len - len)
1573 goto fault;
1574
David S. Miller1a028e52007-04-27 15:21:23 -07001575 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001576 if (copy > len)
1577 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001578 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001579 if ((len -= copy) == 0)
1580 return 0;
1581 offset += copy;
1582 from += copy;
1583 }
1584
1585 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1586 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001587 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001588
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001589 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001590
1591 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001592 if ((copy = end - offset) > 0) {
1593 u8 *vaddr;
1594
1595 if (copy > len)
1596 copy = len;
1597
1598 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001599 memcpy(vaddr + frag->page_offset + offset - start,
1600 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001601 kunmap_skb_frag(vaddr);
1602
1603 if ((len -= copy) == 0)
1604 return 0;
1605 offset += copy;
1606 from += copy;
1607 }
David S. Miller1a028e52007-04-27 15:21:23 -07001608 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001609 }
1610
1611 if (skb_shinfo(skb)->frag_list) {
1612 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1613
1614 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001615 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001616
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001617 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001618
1619 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001620 if ((copy = end - offset) > 0) {
1621 if (copy > len)
1622 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001623 if (skb_store_bits(list, offset - start,
1624 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001625 goto fault;
1626 if ((len -= copy) == 0)
1627 return 0;
1628 offset += copy;
1629 from += copy;
1630 }
David S. Miller1a028e52007-04-27 15:21:23 -07001631 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001632 }
1633 }
1634 if (!len)
1635 return 0;
1636
1637fault:
1638 return -EFAULT;
1639}
Herbert Xu357b40a2005-04-19 22:30:14 -07001640EXPORT_SYMBOL(skb_store_bits);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/* Checksum skb data. */
1643
Al Viro2bbbc862006-11-14 21:37:14 -08001644__wsum skb_checksum(const struct sk_buff *skb, int offset,
1645 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
David S. Miller1a028e52007-04-27 15:21:23 -07001647 int start = skb_headlen(skb);
1648 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 int pos = 0;
1650
1651 /* Checksum header. */
1652 if (copy > 0) {
1653 if (copy > len)
1654 copy = len;
1655 csum = csum_partial(skb->data + offset, copy, csum);
1656 if ((len -= copy) == 0)
1657 return csum;
1658 offset += copy;
1659 pos = copy;
1660 }
1661
1662 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001663 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001665 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001666
1667 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001669 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 u8 *vaddr;
1671 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1672
1673 if (copy > len)
1674 copy = len;
1675 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001676 csum2 = csum_partial(vaddr + frag->page_offset +
1677 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 kunmap_skb_frag(vaddr);
1679 csum = csum_block_add(csum, csum2, pos);
1680 if (!(len -= copy))
1681 return csum;
1682 offset += copy;
1683 pos += copy;
1684 }
David S. Miller1a028e52007-04-27 15:21:23 -07001685 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
1688 if (skb_shinfo(skb)->frag_list) {
1689 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1690
1691 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001692 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001694 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001695
1696 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001698 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (copy > len)
1700 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001701 csum2 = skb_checksum(list, offset - start,
1702 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 csum = csum_block_add(csum, csum2, pos);
1704 if ((len -= copy) == 0)
1705 return csum;
1706 offset += copy;
1707 pos += copy;
1708 }
David S. Miller1a028e52007-04-27 15:21:23 -07001709 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001712 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 return csum;
1715}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001716EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718/* Both of above in one bottle. */
1719
Al Viro81d77662006-11-14 21:37:33 -08001720__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1721 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
David S. Miller1a028e52007-04-27 15:21:23 -07001723 int start = skb_headlen(skb);
1724 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int pos = 0;
1726
1727 /* Copy header. */
1728 if (copy > 0) {
1729 if (copy > len)
1730 copy = len;
1731 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1732 copy, csum);
1733 if ((len -= copy) == 0)
1734 return csum;
1735 offset += copy;
1736 to += copy;
1737 pos = copy;
1738 }
1739
1740 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001741 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001743 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001744
1745 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001747 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 u8 *vaddr;
1749 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1750
1751 if (copy > len)
1752 copy = len;
1753 vaddr = kmap_skb_frag(frag);
1754 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001755 frag->page_offset +
1756 offset - start, to,
1757 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 kunmap_skb_frag(vaddr);
1759 csum = csum_block_add(csum, csum2, pos);
1760 if (!(len -= copy))
1761 return csum;
1762 offset += copy;
1763 to += copy;
1764 pos += copy;
1765 }
David S. Miller1a028e52007-04-27 15:21:23 -07001766 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
1769 if (skb_shinfo(skb)->frag_list) {
1770 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1771
1772 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001773 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001774 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001776 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001777
1778 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if ((copy = end - offset) > 0) {
1780 if (copy > len)
1781 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001782 csum2 = skb_copy_and_csum_bits(list,
1783 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 to, copy, 0);
1785 csum = csum_block_add(csum, csum2, pos);
1786 if ((len -= copy) == 0)
1787 return csum;
1788 offset += copy;
1789 to += copy;
1790 pos += copy;
1791 }
David S. Miller1a028e52007-04-27 15:21:23 -07001792 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001795 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return csum;
1797}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001798EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1801{
Al Virod3bc23e2006-11-14 21:24:49 -08001802 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 long csstart;
1804
Patrick McHardy84fa7932006-08-29 16:44:56 -07001805 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001806 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 else
1808 csstart = skb_headlen(skb);
1809
Kris Katterjohn09a62662006-01-08 22:24:28 -08001810 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001812 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 csum = 0;
1815 if (csstart != skb->len)
1816 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1817 skb->len - csstart, 0);
1818
Patrick McHardy84fa7932006-08-29 16:44:56 -07001819 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001820 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Al Virod3bc23e2006-11-14 21:24:49 -08001822 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001825EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827/**
1828 * skb_dequeue - remove from the head of the queue
1829 * @list: list to dequeue from
1830 *
1831 * Remove the head of the list. The list lock is taken so the function
1832 * may be used safely with other locking list functions. The head item is
1833 * returned or %NULL if the list is empty.
1834 */
1835
1836struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1837{
1838 unsigned long flags;
1839 struct sk_buff *result;
1840
1841 spin_lock_irqsave(&list->lock, flags);
1842 result = __skb_dequeue(list);
1843 spin_unlock_irqrestore(&list->lock, flags);
1844 return result;
1845}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001846EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848/**
1849 * skb_dequeue_tail - remove from the tail of the queue
1850 * @list: list to dequeue from
1851 *
1852 * Remove the tail of the list. The list lock is taken so the function
1853 * may be used safely with other locking list functions. The tail item is
1854 * returned or %NULL if the list is empty.
1855 */
1856struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1857{
1858 unsigned long flags;
1859 struct sk_buff *result;
1860
1861 spin_lock_irqsave(&list->lock, flags);
1862 result = __skb_dequeue_tail(list);
1863 spin_unlock_irqrestore(&list->lock, flags);
1864 return result;
1865}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001866EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868/**
1869 * skb_queue_purge - empty a list
1870 * @list: list to empty
1871 *
1872 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1873 * the list and one reference dropped. This function takes the list
1874 * lock and is atomic with respect to other list locking functions.
1875 */
1876void skb_queue_purge(struct sk_buff_head *list)
1877{
1878 struct sk_buff *skb;
1879 while ((skb = skb_dequeue(list)) != NULL)
1880 kfree_skb(skb);
1881}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001882EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884/**
1885 * skb_queue_head - queue a buffer at the list head
1886 * @list: list to use
1887 * @newsk: buffer to queue
1888 *
1889 * Queue a buffer at the start of the list. This function takes the
1890 * list lock and can be used safely with other locking &sk_buff functions
1891 * safely.
1892 *
1893 * A buffer cannot be placed on two lists at the same time.
1894 */
1895void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1896{
1897 unsigned long flags;
1898
1899 spin_lock_irqsave(&list->lock, flags);
1900 __skb_queue_head(list, newsk);
1901 spin_unlock_irqrestore(&list->lock, flags);
1902}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001903EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905/**
1906 * skb_queue_tail - queue a buffer at the list tail
1907 * @list: list to use
1908 * @newsk: buffer to queue
1909 *
1910 * Queue a buffer at the tail of the list. This function takes the
1911 * list lock and can be used safely with other locking &sk_buff functions
1912 * safely.
1913 *
1914 * A buffer cannot be placed on two lists at the same time.
1915 */
1916void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1917{
1918 unsigned long flags;
1919
1920 spin_lock_irqsave(&list->lock, flags);
1921 __skb_queue_tail(list, newsk);
1922 spin_unlock_irqrestore(&list->lock, flags);
1923}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001924EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/**
1927 * skb_unlink - remove a buffer from a list
1928 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001929 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 *
David S. Miller8728b832005-08-09 19:25:21 -07001931 * Remove a packet from a list. The list locks are taken and this
1932 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 *
David S. Miller8728b832005-08-09 19:25:21 -07001934 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
David S. Miller8728b832005-08-09 19:25:21 -07001936void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
David S. Miller8728b832005-08-09 19:25:21 -07001938 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
David S. Miller8728b832005-08-09 19:25:21 -07001940 spin_lock_irqsave(&list->lock, flags);
1941 __skb_unlink(skb, list);
1942 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001944EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946/**
1947 * skb_append - append a buffer
1948 * @old: buffer to insert after
1949 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001950 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 *
1952 * Place a packet after a given packet in a list. The list locks are taken
1953 * and this function is atomic with respect to other list locked calls.
1954 * A buffer cannot be placed on two lists at the same time.
1955 */
David S. Miller8728b832005-08-09 19:25:21 -07001956void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 unsigned long flags;
1959
David S. Miller8728b832005-08-09 19:25:21 -07001960 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001961 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001962 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001964EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966/**
1967 * skb_insert - insert a buffer
1968 * @old: buffer to insert before
1969 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001970 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 *
David S. Miller8728b832005-08-09 19:25:21 -07001972 * Place a packet before a given packet in a list. The list locks are
1973 * taken and this function is atomic with respect to other list locked
1974 * calls.
1975 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 * A buffer cannot be placed on two lists at the same time.
1977 */
David S. Miller8728b832005-08-09 19:25:21 -07001978void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 unsigned long flags;
1981
David S. Miller8728b832005-08-09 19:25:21 -07001982 spin_lock_irqsave(&list->lock, flags);
1983 __skb_insert(newsk, old->prev, old, list);
1984 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001986EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988static inline void skb_split_inside_header(struct sk_buff *skb,
1989 struct sk_buff* skb1,
1990 const u32 len, const int pos)
1991{
1992 int i;
1993
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001994 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1995 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* And move data appendix as is. */
1997 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1998 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1999
2000 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2001 skb_shinfo(skb)->nr_frags = 0;
2002 skb1->data_len = skb->data_len;
2003 skb1->len += skb1->data_len;
2004 skb->data_len = 0;
2005 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002006 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
2009static inline void skb_split_no_header(struct sk_buff *skb,
2010 struct sk_buff* skb1,
2011 const u32 len, int pos)
2012{
2013 int i, k = 0;
2014 const int nfrags = skb_shinfo(skb)->nr_frags;
2015
2016 skb_shinfo(skb)->nr_frags = 0;
2017 skb1->len = skb1->data_len = skb->len - len;
2018 skb->len = len;
2019 skb->data_len = len - pos;
2020
2021 for (i = 0; i < nfrags; i++) {
2022 int size = skb_shinfo(skb)->frags[i].size;
2023
2024 if (pos + size > len) {
2025 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2026
2027 if (pos < len) {
2028 /* Split frag.
2029 * We have two variants in this case:
2030 * 1. Move all the frag to the second
2031 * part, if it is possible. F.e.
2032 * this approach is mandatory for TUX,
2033 * where splitting is expensive.
2034 * 2. Split is accurately. We make this.
2035 */
2036 get_page(skb_shinfo(skb)->frags[i].page);
2037 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2038 skb_shinfo(skb1)->frags[0].size -= len - pos;
2039 skb_shinfo(skb)->frags[i].size = len - pos;
2040 skb_shinfo(skb)->nr_frags++;
2041 }
2042 k++;
2043 } else
2044 skb_shinfo(skb)->nr_frags++;
2045 pos += size;
2046 }
2047 skb_shinfo(skb1)->nr_frags = k;
2048}
2049
2050/**
2051 * skb_split - Split fragmented skb to two parts at length len.
2052 * @skb: the buffer to split
2053 * @skb1: the buffer to receive the second part
2054 * @len: new length for skb
2055 */
2056void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2057{
2058 int pos = skb_headlen(skb);
2059
2060 if (len < pos) /* Split line is inside header. */
2061 skb_split_inside_header(skb, skb1, len, pos);
2062 else /* Second chunk has no header, nothing to copy. */
2063 skb_split_no_header(skb, skb1, len, pos);
2064}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002065EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002067/* Shifting from/to a cloned skb is a no-go.
2068 *
2069 * Caller cannot keep skb_shinfo related pointers past calling here!
2070 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002071static int skb_prepare_for_shift(struct sk_buff *skb)
2072{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002073 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002074}
2075
2076/**
2077 * skb_shift - Shifts paged data partially from skb to another
2078 * @tgt: buffer into which tail data gets added
2079 * @skb: buffer from which the paged data comes from
2080 * @shiftlen: shift up to this many bytes
2081 *
2082 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2083 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2084 * It's up to caller to free skb if everything was shifted.
2085 *
2086 * If @tgt runs out of frags, the whole operation is aborted.
2087 *
2088 * Skb cannot include anything else but paged data while tgt is allowed
2089 * to have non-paged data as well.
2090 *
2091 * TODO: full sized shift could be optimized but that would need
2092 * specialized skb free'er to handle frags without up-to-date nr_frags.
2093 */
2094int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2095{
2096 int from, to, merge, todo;
2097 struct skb_frag_struct *fragfrom, *fragto;
2098
2099 BUG_ON(shiftlen > skb->len);
2100 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2101
2102 todo = shiftlen;
2103 from = 0;
2104 to = skb_shinfo(tgt)->nr_frags;
2105 fragfrom = &skb_shinfo(skb)->frags[from];
2106
2107 /* Actual merge is delayed until the point when we know we can
2108 * commit all, so that we don't have to undo partial changes
2109 */
2110 if (!to ||
2111 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2112 merge = -1;
2113 } else {
2114 merge = to - 1;
2115
2116 todo -= fragfrom->size;
2117 if (todo < 0) {
2118 if (skb_prepare_for_shift(skb) ||
2119 skb_prepare_for_shift(tgt))
2120 return 0;
2121
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002122 /* All previous frag pointers might be stale! */
2123 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002124 fragto = &skb_shinfo(tgt)->frags[merge];
2125
2126 fragto->size += shiftlen;
2127 fragfrom->size -= shiftlen;
2128 fragfrom->page_offset += shiftlen;
2129
2130 goto onlymerged;
2131 }
2132
2133 from++;
2134 }
2135
2136 /* Skip full, not-fitting skb to avoid expensive operations */
2137 if ((shiftlen == skb->len) &&
2138 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2139 return 0;
2140
2141 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2142 return 0;
2143
2144 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2145 if (to == MAX_SKB_FRAGS)
2146 return 0;
2147
2148 fragfrom = &skb_shinfo(skb)->frags[from];
2149 fragto = &skb_shinfo(tgt)->frags[to];
2150
2151 if (todo >= fragfrom->size) {
2152 *fragto = *fragfrom;
2153 todo -= fragfrom->size;
2154 from++;
2155 to++;
2156
2157 } else {
2158 get_page(fragfrom->page);
2159 fragto->page = fragfrom->page;
2160 fragto->page_offset = fragfrom->page_offset;
2161 fragto->size = todo;
2162
2163 fragfrom->page_offset += todo;
2164 fragfrom->size -= todo;
2165 todo = 0;
2166
2167 to++;
2168 break;
2169 }
2170 }
2171
2172 /* Ready to "commit" this state change to tgt */
2173 skb_shinfo(tgt)->nr_frags = to;
2174
2175 if (merge >= 0) {
2176 fragfrom = &skb_shinfo(skb)->frags[0];
2177 fragto = &skb_shinfo(tgt)->frags[merge];
2178
2179 fragto->size += fragfrom->size;
2180 put_page(fragfrom->page);
2181 }
2182
2183 /* Reposition in the original skb */
2184 to = 0;
2185 while (from < skb_shinfo(skb)->nr_frags)
2186 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2187 skb_shinfo(skb)->nr_frags = to;
2188
2189 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2190
2191onlymerged:
2192 /* Most likely the tgt won't ever need its checksum anymore, skb on
2193 * the other hand might need it if it needs to be resent
2194 */
2195 tgt->ip_summed = CHECKSUM_PARTIAL;
2196 skb->ip_summed = CHECKSUM_PARTIAL;
2197
2198 /* Yak, is it really working this way? Some helper please? */
2199 skb->len -= shiftlen;
2200 skb->data_len -= shiftlen;
2201 skb->truesize -= shiftlen;
2202 tgt->len += shiftlen;
2203 tgt->data_len += shiftlen;
2204 tgt->truesize += shiftlen;
2205
2206 return shiftlen;
2207}
2208
Thomas Graf677e90e2005-06-23 20:59:51 -07002209/**
2210 * skb_prepare_seq_read - Prepare a sequential read of skb data
2211 * @skb: the buffer to read
2212 * @from: lower offset of data to be read
2213 * @to: upper offset of data to be read
2214 * @st: state variable
2215 *
2216 * Initializes the specified state variable. Must be called before
2217 * invoking skb_seq_read() for the first time.
2218 */
2219void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2220 unsigned int to, struct skb_seq_state *st)
2221{
2222 st->lower_offset = from;
2223 st->upper_offset = to;
2224 st->root_skb = st->cur_skb = skb;
2225 st->frag_idx = st->stepped_offset = 0;
2226 st->frag_data = NULL;
2227}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002228EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002229
2230/**
2231 * skb_seq_read - Sequentially read skb data
2232 * @consumed: number of bytes consumed by the caller so far
2233 * @data: destination pointer for data to be returned
2234 * @st: state variable
2235 *
2236 * Reads a block of skb data at &consumed relative to the
2237 * lower offset specified to skb_prepare_seq_read(). Assigns
2238 * the head of the data block to &data and returns the length
2239 * of the block or 0 if the end of the skb data or the upper
2240 * offset has been reached.
2241 *
2242 * The caller is not required to consume all of the data
2243 * returned, i.e. &consumed is typically set to the number
2244 * of bytes already consumed and the next call to
2245 * skb_seq_read() will return the remaining part of the block.
2246 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002247 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002248 * this limitation is the cost for zerocopy seqeuental
2249 * reads of potentially non linear data.
2250 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002251 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002252 * at the moment, state->root_skb could be replaced with
2253 * a stack for this purpose.
2254 */
2255unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2256 struct skb_seq_state *st)
2257{
2258 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2259 skb_frag_t *frag;
2260
2261 if (unlikely(abs_offset >= st->upper_offset))
2262 return 0;
2263
2264next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002265 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002266
2267 if (abs_offset < block_limit) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002268 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002269 return block_limit - abs_offset;
2270 }
2271
2272 if (st->frag_idx == 0 && !st->frag_data)
2273 st->stepped_offset += skb_headlen(st->cur_skb);
2274
2275 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2276 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2277 block_limit = frag->size + st->stepped_offset;
2278
2279 if (abs_offset < block_limit) {
2280 if (!st->frag_data)
2281 st->frag_data = kmap_skb_frag(frag);
2282
2283 *data = (u8 *) st->frag_data + frag->page_offset +
2284 (abs_offset - st->stepped_offset);
2285
2286 return block_limit - abs_offset;
2287 }
2288
2289 if (st->frag_data) {
2290 kunmap_skb_frag(st->frag_data);
2291 st->frag_data = NULL;
2292 }
2293
2294 st->frag_idx++;
2295 st->stepped_offset += frag->size;
2296 }
2297
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002298 if (st->frag_data) {
2299 kunmap_skb_frag(st->frag_data);
2300 st->frag_data = NULL;
2301 }
2302
Shyam Iyer71b33462009-01-29 16:12:42 -08002303 if (st->root_skb == st->cur_skb &&
2304 skb_shinfo(st->root_skb)->frag_list) {
2305 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002306 st->frag_idx = 0;
2307 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002308 } else if (st->cur_skb->next) {
2309 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002310 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002311 goto next_skb;
2312 }
2313
2314 return 0;
2315}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002316EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002317
2318/**
2319 * skb_abort_seq_read - Abort a sequential read of skb data
2320 * @st: state variable
2321 *
2322 * Must be called if skb_seq_read() was not called until it
2323 * returned 0.
2324 */
2325void skb_abort_seq_read(struct skb_seq_state *st)
2326{
2327 if (st->frag_data)
2328 kunmap_skb_frag(st->frag_data);
2329}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002330EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002331
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002332#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2333
2334static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2335 struct ts_config *conf,
2336 struct ts_state *state)
2337{
2338 return skb_seq_read(offset, text, TS_SKB_CB(state));
2339}
2340
2341static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2342{
2343 skb_abort_seq_read(TS_SKB_CB(state));
2344}
2345
2346/**
2347 * skb_find_text - Find a text pattern in skb data
2348 * @skb: the buffer to look in
2349 * @from: search offset
2350 * @to: search limit
2351 * @config: textsearch configuration
2352 * @state: uninitialized textsearch state variable
2353 *
2354 * Finds a pattern in the skb data according to the specified
2355 * textsearch configuration. Use textsearch_next() to retrieve
2356 * subsequent occurrences of the pattern. Returns the offset
2357 * to the first occurrence or UINT_MAX if no match was found.
2358 */
2359unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2360 unsigned int to, struct ts_config *config,
2361 struct ts_state *state)
2362{
Phil Oesterf72b9482006-06-26 00:00:57 -07002363 unsigned int ret;
2364
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002365 config->get_next_block = skb_ts_get_next_block;
2366 config->finish = skb_ts_finish;
2367
2368 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2369
Phil Oesterf72b9482006-06-26 00:00:57 -07002370 ret = textsearch_find(config, state);
2371 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002372}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002373EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002374
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002375/**
2376 * skb_append_datato_frags: - append the user data to a skb
2377 * @sk: sock structure
2378 * @skb: skb structure to be appened with user data.
2379 * @getfrag: call back function to be used for getting the user data
2380 * @from: pointer to user message iov
2381 * @length: length of the iov message
2382 *
2383 * Description: This procedure append the user data in the fragment part
2384 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2385 */
2386int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002387 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002388 int len, int odd, struct sk_buff *skb),
2389 void *from, int length)
2390{
2391 int frg_cnt = 0;
2392 skb_frag_t *frag = NULL;
2393 struct page *page = NULL;
2394 int copy, left;
2395 int offset = 0;
2396 int ret;
2397
2398 do {
2399 /* Return error if we don't have space for new frag */
2400 frg_cnt = skb_shinfo(skb)->nr_frags;
2401 if (frg_cnt >= MAX_SKB_FRAGS)
2402 return -EFAULT;
2403
2404 /* allocate a new page for next frag */
2405 page = alloc_pages(sk->sk_allocation, 0);
2406
2407 /* If alloc_page fails just return failure and caller will
2408 * free previous allocated pages by doing kfree_skb()
2409 */
2410 if (page == NULL)
2411 return -ENOMEM;
2412
2413 /* initialize the next frag */
2414 sk->sk_sndmsg_page = page;
2415 sk->sk_sndmsg_off = 0;
2416 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2417 skb->truesize += PAGE_SIZE;
2418 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2419
2420 /* get the new initialized frag */
2421 frg_cnt = skb_shinfo(skb)->nr_frags;
2422 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2423
2424 /* copy the user data to page */
2425 left = PAGE_SIZE - frag->page_offset;
2426 copy = (length > left)? left : length;
2427
2428 ret = getfrag(from, (page_address(frag->page) +
2429 frag->page_offset + frag->size),
2430 offset, copy, 0, skb);
2431 if (ret < 0)
2432 return -EFAULT;
2433
2434 /* copy was successful so update the size parameters */
2435 sk->sk_sndmsg_off += copy;
2436 frag->size += copy;
2437 skb->len += copy;
2438 skb->data_len += copy;
2439 offset += copy;
2440 length -= copy;
2441
2442 } while (length > 0);
2443
2444 return 0;
2445}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002446EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002447
Herbert Xucbb042f2006-03-20 22:43:56 -08002448/**
2449 * skb_pull_rcsum - pull skb and update receive checksum
2450 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002451 * @len: length of data pulled
2452 *
2453 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002454 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002455 * receive path processing instead of skb_pull unless you know
2456 * that the checksum difference is zero (e.g., a valid IP header)
2457 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002458 */
2459unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2460{
2461 BUG_ON(len > skb->len);
2462 skb->len -= len;
2463 BUG_ON(skb->len < skb->data_len);
2464 skb_postpull_rcsum(skb, skb->data, len);
2465 return skb->data += len;
2466}
2467
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002468EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2469
Herbert Xuf4c50d92006-06-22 03:02:40 -07002470/**
2471 * skb_segment - Perform protocol segmentation on skb.
2472 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002473 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002474 *
2475 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002476 * a pointer to the first in a list of new skbs for the segments.
2477 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002478 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002479struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002480{
2481 struct sk_buff *segs = NULL;
2482 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002483 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002484 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002485 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002486 unsigned int offset = doffset;
2487 unsigned int headroom;
2488 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002489 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002490 int nfrags = skb_shinfo(skb)->nr_frags;
2491 int err = -ENOMEM;
2492 int i = 0;
2493 int pos;
2494
2495 __skb_push(skb, doffset);
2496 headroom = skb_headroom(skb);
2497 pos = skb_headlen(skb);
2498
2499 do {
2500 struct sk_buff *nskb;
2501 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002502 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002503 int size;
2504
2505 len = skb->len - offset;
2506 if (len > mss)
2507 len = mss;
2508
2509 hsize = skb_headlen(skb) - offset;
2510 if (hsize < 0)
2511 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002512 if (hsize > len || !sg)
2513 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002514
Herbert Xu89319d382008-12-15 23:26:06 -08002515 if (!hsize && i >= nfrags) {
2516 BUG_ON(fskb->len != len);
2517
2518 pos += len;
2519 nskb = skb_clone(fskb, GFP_ATOMIC);
2520 fskb = fskb->next;
2521
2522 if (unlikely(!nskb))
2523 goto err;
2524
2525 hsize = skb_end_pointer(nskb) - nskb->head;
2526 if (skb_cow_head(nskb, doffset + headroom)) {
2527 kfree_skb(nskb);
2528 goto err;
2529 }
2530
2531 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2532 hsize;
2533 skb_release_head_state(nskb);
2534 __skb_push(nskb, doffset);
2535 } else {
2536 nskb = alloc_skb(hsize + doffset + headroom,
2537 GFP_ATOMIC);
2538
2539 if (unlikely(!nskb))
2540 goto err;
2541
2542 skb_reserve(nskb, headroom);
2543 __skb_put(nskb, doffset);
2544 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002545
2546 if (segs)
2547 tail->next = nskb;
2548 else
2549 segs = nskb;
2550 tail = nskb;
2551
Herbert Xu6f85a122008-08-15 14:55:02 -07002552 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002553 nskb->mac_len = skb->mac_len;
2554
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002555 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002556 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002557 nskb->transport_header = (nskb->network_header +
2558 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002559 skb_copy_from_linear_data(skb, nskb->data, doffset);
2560
2561 if (pos >= offset + len)
2562 continue;
2563
Herbert Xuf4c50d92006-06-22 03:02:40 -07002564 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002565 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002566 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2567 skb_put(nskb, len),
2568 len, 0);
2569 continue;
2570 }
2571
2572 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002573
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002574 skb_copy_from_linear_data_offset(skb, offset,
2575 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002576
Herbert Xu89319d382008-12-15 23:26:06 -08002577 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002578 *frag = skb_shinfo(skb)->frags[i];
2579 get_page(frag->page);
2580 size = frag->size;
2581
2582 if (pos < offset) {
2583 frag->page_offset += offset - pos;
2584 frag->size -= offset - pos;
2585 }
2586
Herbert Xu89319d382008-12-15 23:26:06 -08002587 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002588
2589 if (pos + size <= offset + len) {
2590 i++;
2591 pos += size;
2592 } else {
2593 frag->size -= pos + size - (offset + len);
Herbert Xu89319d382008-12-15 23:26:06 -08002594 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002595 }
2596
2597 frag++;
2598 }
2599
Herbert Xu89319d382008-12-15 23:26:06 -08002600 if (pos < offset + len) {
2601 struct sk_buff *fskb2 = fskb;
2602
2603 BUG_ON(pos + fskb->len != offset + len);
2604
2605 pos += fskb->len;
2606 fskb = fskb->next;
2607
2608 if (fskb2->next) {
2609 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2610 if (!fskb2)
2611 goto err;
2612 } else
2613 skb_get(fskb2);
2614
2615 BUG_ON(skb_shinfo(nskb)->frag_list);
2616 skb_shinfo(nskb)->frag_list = fskb2;
2617 }
2618
2619skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002620 nskb->data_len = len - hsize;
2621 nskb->len += nskb->data_len;
2622 nskb->truesize += nskb->data_len;
2623 } while ((offset += len) < skb->len);
2624
2625 return segs;
2626
2627err:
2628 while ((skb = segs)) {
2629 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002630 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002631 }
2632 return ERR_PTR(err);
2633}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002634EXPORT_SYMBOL_GPL(skb_segment);
2635
Herbert Xu71d93b32008-12-15 23:42:33 -08002636int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2637{
2638 struct sk_buff *p = *head;
2639 struct sk_buff *nskb;
2640 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002641 unsigned int len = skb_gro_len(skb);
Herbert Xu71d93b32008-12-15 23:42:33 -08002642
Herbert Xu86911732009-01-29 14:19:50 +00002643 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002644 return -E2BIG;
2645
2646 if (skb_shinfo(p)->frag_list)
2647 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00002648 else if (skb_headlen(skb) <= skb_gro_offset(skb)) {
2649 if (skb_shinfo(p)->nr_frags + skb_shinfo(skb)->nr_frags >
2650 MAX_SKB_FRAGS)
2651 return -E2BIG;
2652
Herbert Xu86911732009-01-29 14:19:50 +00002653 skb_shinfo(skb)->frags[0].page_offset +=
2654 skb_gro_offset(skb) - skb_headlen(skb);
2655 skb_shinfo(skb)->frags[0].size -=
2656 skb_gro_offset(skb) - skb_headlen(skb);
2657
Herbert Xu5d38a072009-01-04 16:13:40 -08002658 memcpy(skb_shinfo(p)->frags + skb_shinfo(p)->nr_frags,
2659 skb_shinfo(skb)->frags,
2660 skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
2661
2662 skb_shinfo(p)->nr_frags += skb_shinfo(skb)->nr_frags;
Herbert Xuf5572062009-01-14 20:40:03 -08002663 skb_shinfo(skb)->nr_frags = 0;
2664
2665 skb->truesize -= skb->data_len;
2666 skb->len -= skb->data_len;
2667 skb->data_len = 0;
2668
Herbert Xu5d38a072009-01-04 16:13:40 -08002669 NAPI_GRO_CB(skb)->free = 1;
2670 goto done;
2671 }
Herbert Xu71d93b32008-12-15 23:42:33 -08002672
2673 headroom = skb_headroom(p);
Herbert Xu86911732009-01-29 14:19:50 +00002674 nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002675 if (unlikely(!nskb))
2676 return -ENOMEM;
2677
2678 __copy_skb_header(nskb, p);
2679 nskb->mac_len = p->mac_len;
2680
2681 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002682 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002683
Herbert Xu86911732009-01-29 14:19:50 +00002684 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002685 skb_set_network_header(nskb, skb_network_offset(p));
2686 skb_set_transport_header(nskb, skb_transport_offset(p));
2687
Herbert Xu86911732009-01-29 14:19:50 +00002688 __skb_pull(p, skb_gro_offset(p));
2689 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2690 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002691
2692 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2693 skb_shinfo(nskb)->frag_list = p;
Herbert Xub5302562009-01-04 16:13:19 -08002694 skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
Herbert Xu71d93b32008-12-15 23:42:33 -08002695 skb_header_release(p);
2696 nskb->prev = p;
2697
2698 nskb->data_len += p->len;
2699 nskb->truesize += p->len;
2700 nskb->len += p->len;
2701
2702 *head = nskb;
2703 nskb->next = p->next;
2704 p->next = NULL;
2705
2706 p = nskb;
2707
2708merge:
Herbert Xu56035022009-02-05 21:26:52 -08002709 if (skb_gro_offset(skb) > skb_headlen(skb)) {
2710 skb_shinfo(skb)->frags[0].page_offset +=
2711 skb_gro_offset(skb) - skb_headlen(skb);
2712 skb_shinfo(skb)->frags[0].size -=
2713 skb_gro_offset(skb) - skb_headlen(skb);
2714 skb_gro_reset_offset(skb);
2715 skb_gro_pull(skb, skb_headlen(skb));
2716 }
2717
2718 __skb_pull(skb, skb_gro_offset(skb));
2719
Herbert Xu71d93b32008-12-15 23:42:33 -08002720 p->prev->next = skb;
2721 p->prev = skb;
2722 skb_header_release(skb);
2723
Herbert Xu5d38a072009-01-04 16:13:40 -08002724done:
2725 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00002726 p->data_len += len;
2727 p->truesize += len;
2728 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08002729
2730 NAPI_GRO_CB(skb)->same_flow = 1;
2731 return 0;
2732}
2733EXPORT_SYMBOL_GPL(skb_gro_receive);
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735void __init skb_init(void)
2736{
2737 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2738 sizeof(struct sk_buff),
2739 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002740 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002741 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002742 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2743 (2*sizeof(struct sk_buff)) +
2744 sizeof(atomic_t),
2745 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002746 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002747 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
David Howells716ea3a2007-04-02 20:19:53 -07002750/**
2751 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2752 * @skb: Socket buffer containing the buffers to be mapped
2753 * @sg: The scatter-gather list to map into
2754 * @offset: The offset into the buffer's contents to start mapping
2755 * @len: Length of buffer space to be mapped
2756 *
2757 * Fill the specified scatter-gather list with mappings/pointers into a
2758 * region of the buffer space attached to a socket buffer.
2759 */
David S. Miller51c739d2007-10-30 21:29:29 -07002760static int
2761__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002762{
David S. Miller1a028e52007-04-27 15:21:23 -07002763 int start = skb_headlen(skb);
2764 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002765 int elt = 0;
2766
2767 if (copy > 0) {
2768 if (copy > len)
2769 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002770 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002771 elt++;
2772 if ((len -= copy) == 0)
2773 return elt;
2774 offset += copy;
2775 }
2776
2777 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002778 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002779
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002780 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002781
2782 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002783 if ((copy = end - offset) > 0) {
2784 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2785
2786 if (copy > len)
2787 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002788 sg_set_page(&sg[elt], frag->page, copy,
2789 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002790 elt++;
2791 if (!(len -= copy))
2792 return elt;
2793 offset += copy;
2794 }
David S. Miller1a028e52007-04-27 15:21:23 -07002795 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002796 }
2797
2798 if (skb_shinfo(skb)->frag_list) {
2799 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2800
2801 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002802 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002803
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002804 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002805
2806 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002807 if ((copy = end - offset) > 0) {
2808 if (copy > len)
2809 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002810 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2811 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002812 if ((len -= copy) == 0)
2813 return elt;
2814 offset += copy;
2815 }
David S. Miller1a028e52007-04-27 15:21:23 -07002816 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002817 }
2818 }
2819 BUG_ON(len);
2820 return elt;
2821}
2822
David S. Miller51c739d2007-10-30 21:29:29 -07002823int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2824{
2825 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2826
Jens Axboec46f2332007-10-31 12:06:37 +01002827 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002828
2829 return nsg;
2830}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002831EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07002832
David Howells716ea3a2007-04-02 20:19:53 -07002833/**
2834 * skb_cow_data - Check that a socket buffer's data buffers are writable
2835 * @skb: The socket buffer to check.
2836 * @tailbits: Amount of trailing space to be added
2837 * @trailer: Returned pointer to the skb where the @tailbits space begins
2838 *
2839 * Make sure that the data buffers attached to a socket buffer are
2840 * writable. If they are not, private copies are made of the data buffers
2841 * and the socket buffer is set to use these instead.
2842 *
2843 * If @tailbits is given, make sure that there is space to write @tailbits
2844 * bytes of data beyond current end of socket buffer. @trailer will be
2845 * set to point to the skb in which this space begins.
2846 *
2847 * The number of scatterlist elements required to completely map the
2848 * COW'd and extended socket buffer will be returned.
2849 */
2850int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2851{
2852 int copyflag;
2853 int elt;
2854 struct sk_buff *skb1, **skb_p;
2855
2856 /* If skb is cloned or its head is paged, reallocate
2857 * head pulling out all the pages (pages are considered not writable
2858 * at the moment even if they are anonymous).
2859 */
2860 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2861 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2862 return -ENOMEM;
2863
2864 /* Easy case. Most of packets will go this way. */
2865 if (!skb_shinfo(skb)->frag_list) {
2866 /* A little of trouble, not enough of space for trailer.
2867 * This should not happen, when stack is tuned to generate
2868 * good frames. OK, on miss we reallocate and reserve even more
2869 * space, 128 bytes is fair. */
2870
2871 if (skb_tailroom(skb) < tailbits &&
2872 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2873 return -ENOMEM;
2874
2875 /* Voila! */
2876 *trailer = skb;
2877 return 1;
2878 }
2879
2880 /* Misery. We are in troubles, going to mincer fragments... */
2881
2882 elt = 1;
2883 skb_p = &skb_shinfo(skb)->frag_list;
2884 copyflag = 0;
2885
2886 while ((skb1 = *skb_p) != NULL) {
2887 int ntail = 0;
2888
2889 /* The fragment is partially pulled by someone,
2890 * this can happen on input. Copy it and everything
2891 * after it. */
2892
2893 if (skb_shared(skb1))
2894 copyflag = 1;
2895
2896 /* If the skb is the last, worry about trailer. */
2897
2898 if (skb1->next == NULL && tailbits) {
2899 if (skb_shinfo(skb1)->nr_frags ||
2900 skb_shinfo(skb1)->frag_list ||
2901 skb_tailroom(skb1) < tailbits)
2902 ntail = tailbits + 128;
2903 }
2904
2905 if (copyflag ||
2906 skb_cloned(skb1) ||
2907 ntail ||
2908 skb_shinfo(skb1)->nr_frags ||
2909 skb_shinfo(skb1)->frag_list) {
2910 struct sk_buff *skb2;
2911
2912 /* Fuck, we are miserable poor guys... */
2913 if (ntail == 0)
2914 skb2 = skb_copy(skb1, GFP_ATOMIC);
2915 else
2916 skb2 = skb_copy_expand(skb1,
2917 skb_headroom(skb1),
2918 ntail,
2919 GFP_ATOMIC);
2920 if (unlikely(skb2 == NULL))
2921 return -ENOMEM;
2922
2923 if (skb1->sk)
2924 skb_set_owner_w(skb2, skb1->sk);
2925
2926 /* Looking around. Are we still alive?
2927 * OK, link new skb, drop old one */
2928
2929 skb2->next = skb1->next;
2930 *skb_p = skb2;
2931 kfree_skb(skb1);
2932 skb1 = skb2;
2933 }
2934 elt++;
2935 *trailer = skb1;
2936 skb_p = &skb1->next;
2937 }
2938
2939 return elt;
2940}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002941EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07002942
Patrick Ohlyac45f602009-02-12 05:03:37 +00002943void skb_tstamp_tx(struct sk_buff *orig_skb,
2944 struct skb_shared_hwtstamps *hwtstamps)
2945{
2946 struct sock *sk = orig_skb->sk;
2947 struct sock_exterr_skb *serr;
2948 struct sk_buff *skb;
2949 int err;
2950
2951 if (!sk)
2952 return;
2953
2954 skb = skb_clone(orig_skb, GFP_ATOMIC);
2955 if (!skb)
2956 return;
2957
2958 if (hwtstamps) {
2959 *skb_hwtstamps(skb) =
2960 *hwtstamps;
2961 } else {
2962 /*
2963 * no hardware time stamps available,
2964 * so keep the skb_shared_tx and only
2965 * store software time stamp
2966 */
2967 skb->tstamp = ktime_get_real();
2968 }
2969
2970 serr = SKB_EXT_ERR(skb);
2971 memset(serr, 0, sizeof(*serr));
2972 serr->ee.ee_errno = ENOMSG;
2973 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
2974 err = sock_queue_err_skb(sk, skb);
2975 if (err)
2976 kfree_skb(skb);
2977}
2978EXPORT_SYMBOL_GPL(skb_tstamp_tx);
2979
2980
Rusty Russellf35d9d82008-02-04 23:49:54 -05002981/**
2982 * skb_partial_csum_set - set up and verify partial csum values for packet
2983 * @skb: the skb to set
2984 * @start: the number of bytes after skb->data to start checksumming.
2985 * @off: the offset from start to place the checksum.
2986 *
2987 * For untrusted partially-checksummed packets, we need to make sure the values
2988 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2989 *
2990 * This function checks and sets those values and skb->ip_summed: if this
2991 * returns false you should drop the packet.
2992 */
2993bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2994{
2995 if (unlikely(start > skb->len - 2) ||
2996 unlikely((int)start + off > skb->len - 2)) {
2997 if (net_ratelimit())
2998 printk(KERN_WARNING
2999 "bad partial csum: csum=%u/%u len=%u\n",
3000 start, off, skb->len);
3001 return false;
3002 }
3003 skb->ip_summed = CHECKSUM_PARTIAL;
3004 skb->csum_start = skb_headroom(skb) + start;
3005 skb->csum_offset = off;
3006 return true;
3007}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003008EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003009
Ben Hutchings4497b072008-06-19 16:22:28 -07003010void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3011{
3012 if (net_ratelimit())
3013 pr_warning("%s: received packets cannot be forwarded"
3014 " while LRO is enabled\n", skb->dev->name);
3015}
Ben Hutchings4497b072008-06-19 16:22:28 -07003016EXPORT_SYMBOL(__skb_warn_lro_forwarding);