blob: 2d1bc761fe4b9ebdf57978e9486651ec1a5a4a8d [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>
Vegard Nossumfe55f6d2008-08-30 12:16:35 +020042#include <linux/kmemcheck.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mm.h>
44#include <linux/interrupt.h>
45#include <linux/in.h>
46#include <linux/inet.h>
47#include <linux/slab.h>
48#include <linux/netdevice.h>
49#ifdef CONFIG_NET_CLS_ACT
50#include <net/pkt_sched.h>
51#endif
52#include <linux/string.h>
53#include <linux/skbuff.h>
Jens Axboe9c55e012007-11-06 23:30:13 -080054#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
David Howells716ea3a2007-04-02 20:19:53 -070058#include <linux/scatterlist.h>
Patrick Ohlyac45f602009-02-12 05:03:37 +000059#include <linux/errqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#include <net/protocol.h>
62#include <net/dst.h>
63#include <net/sock.h>
64#include <net/checksum.h>
65#include <net/xfrm.h>
66
67#include <asm/uaccess.h>
68#include <asm/system.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040069#include <trace/events/skb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Al Viroa1f8e7f72006-10-19 16:08:53 -040071#include "kmap_skb.h"
72
Christoph Lametere18b8902006-12-06 20:33:20 -080073static struct kmem_cache *skbuff_head_cache __read_mostly;
74static struct kmem_cache *skbuff_fclone_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Jens Axboe9c55e012007-11-06 23:30:13 -080076static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
77 struct pipe_buffer *buf)
78{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080079 put_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080080}
81
82static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
83 struct pipe_buffer *buf)
84{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -080085 get_page(buf->page);
Jens Axboe9c55e012007-11-06 23:30:13 -080086}
87
88static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
89 struct pipe_buffer *buf)
90{
91 return 1;
92}
93
94
95/* Pipe buffer operations for a socket. */
Alexey Dobriyan28dfef82009-12-15 16:46:48 -080096static const struct pipe_buf_operations sock_pipe_buf_ops = {
Jens Axboe9c55e012007-11-06 23:30:13 -080097 .can_merge = 0,
98 .map = generic_pipe_buf_map,
99 .unmap = generic_pipe_buf_unmap,
100 .confirm = generic_pipe_buf_confirm,
101 .release = sock_pipe_buf_release,
102 .steal = sock_pipe_buf_steal,
103 .get = sock_pipe_buf_get,
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
107 * Keep out-of-line to prevent kernel bloat.
108 * __builtin_return_address is not used because it is not always
109 * reliable.
110 */
111
112/**
113 * skb_over_panic - private function
114 * @skb: buffer
115 * @sz: size
116 * @here: address
117 *
118 * Out of line support code for skb_put(). Not user callable.
119 */
Rami Rosenccb7c772010-04-20 22:39:53 -0700120static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Patrick McHardy26095452005-04-21 16:43:02 -0700122 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700123 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700124 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700125 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700126 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 BUG();
128}
129
130/**
131 * skb_under_panic - private function
132 * @skb: buffer
133 * @sz: size
134 * @here: address
135 *
136 * Out of line support code for skb_push(). Not user callable.
137 */
138
Rami Rosenccb7c772010-04-20 22:39:53 -0700139static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Patrick McHardy26095452005-04-21 16:43:02 -0700141 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700142 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700143 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700144 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700145 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 BUG();
147}
148
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;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700184 prefetchw(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800187 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
188 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!data)
190 goto nodata;
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700191 prefetchw(data + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300193 /*
Johannes Bergc8005782008-05-03 20:56:42 -0700194 * Only clear those fields we need to clear, not those that we will
195 * actually initialise below. Hence, don't put any more fields after
196 * the tail pointer in struct sk_buff!
Arnaldo Carvalho de Meloca0605a2007-03-19 10:48:59 -0300197 */
198 memset(skb, 0, offsetof(struct sk_buff, tail));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 skb->truesize = size + sizeof(struct sk_buff);
200 atomic_set(&skb->users, 1);
201 skb->head = data;
202 skb->data = data;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700203 skb_reset_tail_pointer(skb);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700204 skb->end = skb->tail + size;
Stephen Hemminger19633e12009-06-17 05:23:27 +0000205#ifdef NET_SKBUFF_DATA_USES_OFFSET
206 skb->mac_header = ~0U;
207#endif
208
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800209 /* make sure we initialize shinfo sequentially */
210 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700211 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800212 atomic_set(&shinfo->dataref, 1);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800213
David S. Millerd179cd12005-08-17 14:57:30 -0700214 if (fclone) {
215 struct sk_buff *child = skb + 1;
216 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200218 kmemcheck_annotate_bitfield(child, flags1);
219 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700220 skb->fclone = SKB_FCLONE_ORIG;
221 atomic_set(fclone_ref, 1);
222
223 child->fclone = SKB_FCLONE_UNAVAILABLE;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225out:
226 return skb;
227nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800228 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 skb = NULL;
230 goto out;
231}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800232EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700235 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
236 * @dev: network device to receive on
237 * @length: length to allocate
238 * @gfp_mask: get_free_pages mask, passed to alloc_skb
239 *
240 * Allocate a new &sk_buff and assign it a usage count of one. The
241 * buffer has unspecified headroom built in. Users should allocate
242 * the headroom they think they need without accounting for the
243 * built in space. The built in space is used for optimisations.
244 *
245 * %NULL is returned if there is no free memory.
246 */
247struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
248 unsigned int length, gfp_t gfp_mask)
249{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700250 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700251 struct sk_buff *skb;
252
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900253 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700254 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700255 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700256 skb->dev = dev;
257 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700258 return skb;
259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800260EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Peter Zijlstra654bed12008-10-07 14:22:33 -0700262struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
263{
264 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
265 struct page *page;
266
267 page = alloc_pages_node(node, gfp_mask, 0);
268 return page;
269}
270EXPORT_SYMBOL(__netdev_alloc_page);
271
272void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
273 int size)
274{
275 skb_fill_page_desc(skb, i, page, off, size);
276 skb->len += size;
277 skb->data_len += size;
278 skb->truesize += size;
279}
280EXPORT_SYMBOL(skb_add_rx_frag);
281
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700282/**
283 * dev_alloc_skb - allocate an skbuff for receiving
284 * @length: length to allocate
285 *
286 * Allocate a new &sk_buff and assign it a usage count of one. The
287 * buffer has unspecified headroom built in. Users should allocate
288 * the headroom they think they need without accounting for the
289 * built in space. The built in space is used for optimisations.
290 *
291 * %NULL is returned if there is no free memory. Although this function
292 * allocates memory it can be called from an interrupt.
293 */
294struct sk_buff *dev_alloc_skb(unsigned int length)
295{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700296 /*
297 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700298 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700299 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700300 return __dev_alloc_skb(length, GFP_ATOMIC);
301}
302EXPORT_SYMBOL(dev_alloc_skb);
303
Herbert Xu27b437c2006-07-13 19:26:39 -0700304static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Herbert Xu27b437c2006-07-13 19:26:39 -0700306 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Herbert Xu27b437c2006-07-13 19:26:39 -0700308 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 do {
311 struct sk_buff *this = list;
312 list = list->next;
313 kfree_skb(this);
314 } while (list);
315}
316
Herbert Xu27b437c2006-07-13 19:26:39 -0700317static inline void skb_drop_fraglist(struct sk_buff *skb)
318{
319 skb_drop_list(&skb_shinfo(skb)->frag_list);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static void skb_clone_fraglist(struct sk_buff *skb)
323{
324 struct sk_buff *list;
325
David S. Millerfbb398a2009-06-09 00:18:59 -0700326 skb_walk_frags(skb, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 skb_get(list);
328}
329
Adrian Bunk5bba1712006-06-29 13:02:35 -0700330static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 if (!skb->cloned ||
333 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
334 &skb_shinfo(skb)->dataref)) {
335 if (skb_shinfo(skb)->nr_frags) {
336 int i;
337 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
338 put_page(skb_shinfo(skb)->frags[i].page);
339 }
340
David S. Miller21dc3302010-08-23 00:13:46 -0700341 if (skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 skb_drop_fraglist(skb);
343
344 kfree(skb->head);
345 }
346}
347
348/*
349 * Free an skbuff by memory without cleaning the state.
350 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800351static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David S. Millerd179cd12005-08-17 14:57:30 -0700353 struct sk_buff *other;
354 atomic_t *fclone_ref;
355
David S. Millerd179cd12005-08-17 14:57:30 -0700356 switch (skb->fclone) {
357 case SKB_FCLONE_UNAVAILABLE:
358 kmem_cache_free(skbuff_head_cache, skb);
359 break;
360
361 case SKB_FCLONE_ORIG:
362 fclone_ref = (atomic_t *) (skb + 2);
363 if (atomic_dec_and_test(fclone_ref))
364 kmem_cache_free(skbuff_fclone_cache, skb);
365 break;
366
367 case SKB_FCLONE_CLONE:
368 fclone_ref = (atomic_t *) (skb + 1);
369 other = skb - 1;
370
371 /* The clone portion is available for
372 * fast-cloning again.
373 */
374 skb->fclone = SKB_FCLONE_UNAVAILABLE;
375
376 if (atomic_dec_and_test(fclone_ref))
377 kmem_cache_free(skbuff_fclone_cache, other);
378 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700382static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Eric Dumazetadf30902009-06-02 05:19:30 +0000384 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifdef CONFIG_XFRM
386 secpath_put(skb->sp);
387#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700388 if (skb->destructor) {
389 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 skb->destructor(skb);
391 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800392#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700393 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800394 nf_conntrack_put_reasm(skb->nfct_reasm);
395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#ifdef CONFIG_BRIDGE_NETFILTER
397 nf_bridge_put(skb->nf_bridge);
398#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/* XXX: IS this still necessary? - JHS */
400#ifdef CONFIG_NET_SCHED
401 skb->tc_index = 0;
402#ifdef CONFIG_NET_CLS_ACT
403 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif
405#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700406}
407
408/* Free everything but the sk_buff shell. */
409static void skb_release_all(struct sk_buff *skb)
410{
411 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800412 skb_release_data(skb);
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Herbert Xu2d4baff2007-11-26 23:11:19 +0800415/**
416 * __kfree_skb - private function
417 * @skb: buffer
418 *
419 * Free an sk_buff. Release anything attached to the buffer.
420 * Clean the state. This is an internal helper function. Users should
421 * always call kfree_skb
422 */
423
424void __kfree_skb(struct sk_buff *skb)
425{
426 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 kfree_skbmem(skb);
428}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800429EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800432 * kfree_skb - free an sk_buff
433 * @skb: buffer to free
434 *
435 * Drop a reference to the buffer and free it if the usage count has
436 * hit zero.
437 */
438void kfree_skb(struct sk_buff *skb)
439{
440 if (unlikely(!skb))
441 return;
442 if (likely(atomic_read(&skb->users) == 1))
443 smp_rmb();
444 else if (likely(!atomic_dec_and_test(&skb->users)))
445 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000446 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800447 __kfree_skb(skb);
448}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800449EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800450
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700451/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000452 * consume_skb - free an skbuff
453 * @skb: buffer to free
454 *
455 * Drop a ref to the buffer and free it if the usage count has hit zero
456 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
457 * is being dropped after a failure and notes that
458 */
459void consume_skb(struct sk_buff *skb)
460{
461 if (unlikely(!skb))
462 return;
463 if (likely(atomic_read(&skb->users) == 1))
464 smp_rmb();
465 else if (likely(!atomic_dec_and_test(&skb->users)))
466 return;
467 __kfree_skb(skb);
468}
469EXPORT_SYMBOL(consume_skb);
470
471/**
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700472 * skb_recycle_check - check if skb can be reused for receive
473 * @skb: buffer
474 * @skb_size: minimum receive buffer size
475 *
476 * Checks that the skb passed in is not shared or cloned, and
477 * that it is linear and its head portion at least as large as
478 * skb_size so that it can be recycled as a receive buffer.
479 * If these conditions are met, this function does any necessary
480 * reference count dropping and cleans up the skbuff as if it
481 * just came from __alloc_skb().
482 */
Changli Gao5b0daa32010-05-29 00:12:13 -0700483bool skb_recycle_check(struct sk_buff *skb, int skb_size)
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700484{
485 struct skb_shared_info *shinfo;
486
Anton Vorontsove84af6d2009-11-10 14:11:01 +0000487 if (irqs_disabled())
Changli Gao5b0daa32010-05-29 00:12:13 -0700488 return false;
Anton Vorontsove84af6d2009-11-10 14:11:01 +0000489
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700490 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
Changli Gao5b0daa32010-05-29 00:12:13 -0700491 return false;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700492
493 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
494 if (skb_end_pointer(skb) - skb->head < skb_size)
Changli Gao5b0daa32010-05-29 00:12:13 -0700495 return false;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700496
497 if (skb_shared(skb) || skb_cloned(skb))
Changli Gao5b0daa32010-05-29 00:12:13 -0700498 return false;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700499
500 skb_release_head_state(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700501
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700502 shinfo = skb_shinfo(skb);
Eric Dumazetec7d2f22010-05-05 01:07:37 -0700503 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700504 atomic_set(&shinfo->dataref, 1);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700505
506 memset(skb, 0, offsetof(struct sk_buff, tail));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700507 skb->data = skb->head + NET_SKB_PAD;
Lennert Buytenhek5cd33db2008-11-10 21:45:05 -0800508 skb_reset_tail_pointer(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700509
Changli Gao5b0daa32010-05-29 00:12:13 -0700510 return true;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700511}
512EXPORT_SYMBOL(skb_recycle_check);
513
Herbert Xudec18812007-10-14 00:37:30 -0700514static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
515{
516 new->tstamp = old->tstamp;
517 new->dev = old->dev;
518 new->transport_header = old->transport_header;
519 new->network_header = old->network_header;
520 new->mac_header = old->mac_header;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000521 skb_dst_copy(new, old);
Tom Herbert0a9627f2010-03-16 08:03:29 +0000522 new->rxhash = old->rxhash;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700523#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700524 new->sp = secpath_get(old->sp);
525#endif
526 memcpy(new->cb, old->cb, sizeof(old->cb));
Herbert Xu9bcb97c2009-05-22 22:20:02 +0000527 new->csum = old->csum;
Herbert Xudec18812007-10-14 00:37:30 -0700528 new->local_df = old->local_df;
529 new->pkt_type = old->pkt_type;
530 new->ip_summed = old->ip_summed;
531 skb_copy_queue_mapping(new, old);
532 new->priority = old->priority;
John Fastabende8970822010-06-13 10:36:30 +0000533 new->deliver_no_wcard = old->deliver_no_wcard;
Herbert Xudec18812007-10-14 00:37:30 -0700534#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
535 new->ipvs_property = old->ipvs_property;
536#endif
537 new->protocol = old->protocol;
538 new->mark = old->mark;
Eric Dumazet8964be42009-11-20 15:35:04 -0800539 new->skb_iif = old->skb_iif;
Herbert Xudec18812007-10-14 00:37:30 -0700540 __nf_copy(new, old);
541#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
542 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
543 new->nf_trace = old->nf_trace;
544#endif
545#ifdef CONFIG_NET_SCHED
546 new->tc_index = old->tc_index;
547#ifdef CONFIG_NET_CLS_ACT
548 new->tc_verd = old->tc_verd;
549#endif
550#endif
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700551 new->vlan_tci = old->vlan_tci;
552
Herbert Xudec18812007-10-14 00:37:30 -0700553 skb_copy_secmark(new, old);
554}
555
Herbert Xu82c49a32009-05-22 22:11:37 +0000556/*
557 * You should not add any new code to this function. Add it to
558 * __copy_skb_header above instead.
559 */
Herbert Xue0053ec2007-10-14 00:37:52 -0700560static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#define C(x) n->x = skb->x
563
564 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700566 __copy_skb_header(n, skb);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 C(len);
569 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700570 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700571 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800572 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 n->destructor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 C(tail);
576 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800577 C(head);
578 C(data);
579 C(truesize);
580 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 atomic_inc(&(skb_shinfo(skb)->dataref));
583 skb->cloned = 1;
584
585 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700586#undef C
587}
588
589/**
590 * skb_morph - morph one skb into another
591 * @dst: the skb to receive the contents
592 * @src: the skb to supply the contents
593 *
594 * This is identical to skb_clone except that the target skb is
595 * supplied by the user.
596 *
597 * The target skb is returned upon exit.
598 */
599struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
600{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800601 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700602 return __skb_clone(dst, src);
603}
604EXPORT_SYMBOL_GPL(skb_morph);
605
606/**
607 * skb_clone - duplicate an sk_buff
608 * @skb: buffer to clone
609 * @gfp_mask: allocation priority
610 *
611 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
612 * copies share the same packet data but not structure. The new
613 * buffer has a reference count of 1. If the allocation fails the
614 * function returns %NULL otherwise the new buffer is returned.
615 *
616 * If this function is called from an interrupt gfp_mask() must be
617 * %GFP_ATOMIC.
618 */
619
620struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
621{
622 struct sk_buff *n;
623
624 n = skb + 1;
625 if (skb->fclone == SKB_FCLONE_ORIG &&
626 n->fclone == SKB_FCLONE_UNAVAILABLE) {
627 atomic_t *fclone_ref = (atomic_t *) (n + 1);
628 n->fclone = SKB_FCLONE_CLONE;
629 atomic_inc(fclone_ref);
630 } else {
631 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
632 if (!n)
633 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200634
635 kmemcheck_annotate_bitfield(n, flags1);
636 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700637 n->fclone = SKB_FCLONE_UNAVAILABLE;
638 }
639
640 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800642EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
645{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700646#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * Shift between the two data areas in bytes
649 */
650 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700651#endif
Herbert Xudec18812007-10-14 00:37:30 -0700652
653 __copy_skb_header(new, old);
654
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700655#ifndef NET_SKBUFF_DATA_USES_OFFSET
656 /* {transport,network,mac}_header are relative to skb->head */
657 new->transport_header += offset;
658 new->network_header += offset;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000659 if (skb_mac_header_was_set(new))
660 new->mac_header += offset;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700661#endif
Herbert Xu79671682006-06-22 02:40:14 -0700662 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
663 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
664 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/**
668 * skb_copy - create private copy of an sk_buff
669 * @skb: buffer to copy
670 * @gfp_mask: allocation priority
671 *
672 * Make a copy of both an &sk_buff and its data. This is used when the
673 * caller wishes to modify the data and needs a private copy of the
674 * data to alter. Returns %NULL on failure or the pointer to the buffer
675 * on success. The returned buffer has a reference count of 1.
676 *
677 * As by-product this function converts non-linear &sk_buff to linear
678 * one, so that &sk_buff becomes completely private and caller is allowed
679 * to modify all the data of returned buffer. This means that this
680 * function is not recommended for use in circumstances when only
681 * header is going to be modified. Use pskb_copy() instead.
682 */
683
Al Virodd0fc662005-10-07 07:46:04 +0100684struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000686 int headerlen = skb_headroom(skb);
687 unsigned int size = (skb_end_pointer(skb) - skb->head) + skb->data_len;
688 struct sk_buff *n = alloc_skb(size, gfp_mask);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!n)
691 return NULL;
692
693 /* Set the data pointer */
694 skb_reserve(n, headerlen);
695 /* Set the tail pointer and length */
696 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
699 BUG();
700
701 copy_skb_header(n, skb);
702 return n;
703}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800704EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/**
707 * pskb_copy - create copy of an sk_buff with private head.
708 * @skb: buffer to copy
709 * @gfp_mask: allocation priority
710 *
711 * Make a copy of both an &sk_buff and part of its data, located
712 * in header. Fragmented data remain shared. This is used when
713 * the caller wishes to modify only header of &sk_buff and needs
714 * private copy of the header to alter. Returns %NULL on failure
715 * or the pointer to the buffer on success.
716 * The returned buffer has a reference count of 1.
717 */
718
Al Virodd0fc662005-10-07 07:46:04 +0100719struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000721 unsigned int size = skb_end_pointer(skb) - skb->head;
722 struct sk_buff *n = alloc_skb(size, gfp_mask);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!n)
725 goto out;
726
727 /* Set the data pointer */
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000728 skb_reserve(n, skb_headroom(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Set the tail pointer and length */
730 skb_put(n, skb_headlen(skb));
731 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300732 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Herbert Xu25f484a2006-11-07 14:57:15 -0800734 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 n->data_len = skb->data_len;
736 n->len = skb->len;
737
738 if (skb_shinfo(skb)->nr_frags) {
739 int i;
740
741 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
742 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
743 get_page(skb_shinfo(n)->frags[i].page);
744 }
745 skb_shinfo(n)->nr_frags = i;
746 }
747
David S. Miller21dc3302010-08-23 00:13:46 -0700748 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
750 skb_clone_fraglist(n);
751 }
752
753 copy_skb_header(n, skb);
754out:
755 return n;
756}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800757EXPORT_SYMBOL(pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759/**
760 * pskb_expand_head - reallocate header of &sk_buff
761 * @skb: buffer to reallocate
762 * @nhead: room to add at head
763 * @ntail: room to add at tail
764 * @gfp_mask: allocation priority
765 *
766 * Expands (or creates identical copy, if &nhead and &ntail are zero)
767 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
768 * reference count of 1. Returns zero in the case of success or error,
769 * if expansion failed. In the last case, &sk_buff is not changed.
770 *
771 * All the pointers pointing into skb header may change and must be
772 * reloaded after call to this function.
773 */
774
Victor Fusco86a76ca2005-07-08 14:57:47 -0700775int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100776 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 int i;
779 u8 *data;
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000780 int size = nhead + (skb_end_pointer(skb) - skb->head) + ntail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 long off;
Eric Dumazet1fd63042010-09-02 23:09:32 +0000782 bool fastpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Herbert Xu4edd87a2008-10-01 07:09:38 -0700784 BUG_ON(nhead < 0);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (skb_shared(skb))
787 BUG();
788
789 size = SKB_DATA_ALIGN(size);
790
791 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
792 if (!data)
793 goto nodata;
794
795 /* Copy only real data... and, alas, header. This should be
Eric Dumazet6602ceb2010-09-01 05:25:10 +0000796 * optimized for the cases when header is void.
797 */
798 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
799
800 memcpy((struct skb_shared_info *)(data + size),
801 skb_shinfo(skb),
Eric Dumazetfed66382010-07-22 19:09:08 +0000802 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Eric Dumazet1fd63042010-09-02 23:09:32 +0000804 /* Check if we can avoid taking references on fragments if we own
805 * the last reference on skb->head. (see skb_release_data())
806 */
807 if (!skb->cloned)
808 fastpath = true;
809 else {
810 int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Eric Dumazet1fd63042010-09-02 23:09:32 +0000812 fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Eric Dumazet1fd63042010-09-02 23:09:32 +0000815 if (fastpath) {
816 kfree(skb->head);
817 } else {
818 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
819 get_page(skb_shinfo(skb)->frags[i].page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Eric Dumazet1fd63042010-09-02 23:09:32 +0000821 if (skb_has_frag_list(skb))
822 skb_clone_fraglist(skb);
823
824 skb_release_data(skb);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 off = (data + nhead) - skb->head;
827
828 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700830#ifdef NET_SKBUFF_DATA_USES_OFFSET
831 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700832 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700833#else
834 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700835#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700836 /* {transport,network,mac}_header and tail are relative to skb->head */
837 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700838 skb->transport_header += off;
839 skb->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000840 if (skb_mac_header_was_set(skb))
841 skb->mac_header += off;
Andrea Shepard00c5a982010-07-22 09:12:35 +0000842 /* Only adjust this if it actually is csum_start rather than csum */
843 if (skb->ip_summed == CHECKSUM_PARTIAL)
844 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700846 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 skb->nohdr = 0;
848 atomic_set(&skb_shinfo(skb)->dataref, 1);
849 return 0;
850
851nodata:
852 return -ENOMEM;
853}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800854EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856/* Make private copy of skb with writable head and some headroom */
857
858struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
859{
860 struct sk_buff *skb2;
861 int delta = headroom - skb_headroom(skb);
862
863 if (delta <= 0)
864 skb2 = pskb_copy(skb, GFP_ATOMIC);
865 else {
866 skb2 = skb_clone(skb, GFP_ATOMIC);
867 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
868 GFP_ATOMIC)) {
869 kfree_skb(skb2);
870 skb2 = NULL;
871 }
872 }
873 return skb2;
874}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800875EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877/**
878 * skb_copy_expand - copy and expand sk_buff
879 * @skb: buffer to copy
880 * @newheadroom: new free bytes at head
881 * @newtailroom: new free bytes at tail
882 * @gfp_mask: allocation priority
883 *
884 * Make a copy of both an &sk_buff and its data and while doing so
885 * allocate additional space.
886 *
887 * This is used when the caller wishes to modify the data and needs a
888 * private copy of the data to alter as well as more space for new fields.
889 * Returns %NULL on failure or the pointer to the buffer
890 * on success. The returned buffer has a reference count of 1.
891 *
892 * You must pass %GFP_ATOMIC as the allocation priority if this function
893 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700896 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100897 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 /*
900 * Allocate the copy buffer
901 */
902 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
903 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700904 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700906 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if (!n)
909 return NULL;
910
911 skb_reserve(n, newheadroom);
912
913 /* Set the tail pointer and length */
914 skb_put(n, skb->len);
915
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700916 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 head_copy_off = 0;
918 if (newheadroom <= head_copy_len)
919 head_copy_len = newheadroom;
920 else
921 head_copy_off = newheadroom - head_copy_len;
922
923 /* Copy the linear header and data. */
924 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
925 skb->len + head_copy_len))
926 BUG();
927
928 copy_skb_header(n, skb);
929
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700930 off = newheadroom - oldheadroom;
David S. Millerbe2b6e62010-07-22 13:27:09 -0700931 if (n->ip_summed == CHECKSUM_PARTIAL)
932 n->csum_start += off;
Herbert Xu52886052007-09-16 16:32:11 -0700933#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700934 n->transport_header += off;
935 n->network_header += off;
Stephen Hemminger603a8bb2009-06-17 12:17:34 +0000936 if (skb_mac_header_was_set(skb))
937 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700938#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return n;
941}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800942EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944/**
945 * skb_pad - zero pad the tail of an skb
946 * @skb: buffer to pad
947 * @pad: space to pad
948 *
949 * Ensure that a buffer is followed by a padding area that is zero
950 * filled. Used by network drivers which may DMA or transfer data
951 * beyond the buffer end onto the wire.
952 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700953 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900955
Herbert Xu5b057c62006-06-23 02:06:41 -0700956int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Herbert Xu5b057c62006-06-23 02:06:41 -0700958 int err;
959 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700962 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700966
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700967 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700968 if (likely(skb_cloned(skb) || ntail > 0)) {
969 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
970 if (unlikely(err))
971 goto free_skb;
972 }
973
974 /* FIXME: The use of this function with non-linear skb's really needs
975 * to be audited.
976 */
977 err = skb_linearize(skb);
978 if (unlikely(err))
979 goto free_skb;
980
981 memset(skb->data + skb->len, 0, pad);
982 return 0;
983
984free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700986 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900987}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800988EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900989
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700990/**
991 * skb_put - add data to a buffer
992 * @skb: buffer to use
993 * @len: amount of data to add
994 *
995 * This function extends the used data area of the buffer. If this would
996 * exceed the total buffer size the kernel will panic. A pointer to the
997 * first byte of the extra data is returned.
998 */
999unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1000{
1001 unsigned char *tmp = skb_tail_pointer(skb);
1002 SKB_LINEAR_ASSERT(skb);
1003 skb->tail += len;
1004 skb->len += len;
1005 if (unlikely(skb->tail > skb->end))
1006 skb_over_panic(skb, len, __builtin_return_address(0));
1007 return tmp;
1008}
1009EXPORT_SYMBOL(skb_put);
1010
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001011/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001012 * skb_push - add data to the start of a buffer
1013 * @skb: buffer to use
1014 * @len: amount of data to add
1015 *
1016 * This function extends the used data area of the buffer at the buffer
1017 * start. If this would exceed the total buffer headroom the kernel will
1018 * panic. A pointer to the first byte of the extra data is returned.
1019 */
1020unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1021{
1022 skb->data -= len;
1023 skb->len += len;
1024 if (unlikely(skb->data<skb->head))
1025 skb_under_panic(skb, len, __builtin_return_address(0));
1026 return skb->data;
1027}
1028EXPORT_SYMBOL(skb_push);
1029
1030/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001031 * skb_pull - remove data from the start of a buffer
1032 * @skb: buffer to use
1033 * @len: amount of data to remove
1034 *
1035 * This function removes data from the start of a buffer, returning
1036 * the memory to the headroom. A pointer to the next data in the buffer
1037 * is returned. Once the data has been pulled future pushes will overwrite
1038 * the old data.
1039 */
1040unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1041{
David S. Miller47d29642010-05-02 02:21:44 -07001042 return skb_pull_inline(skb, len);
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001043}
1044EXPORT_SYMBOL(skb_pull);
1045
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001046/**
1047 * skb_trim - remove end from a buffer
1048 * @skb: buffer to alter
1049 * @len: new length
1050 *
1051 * Cut the length of a buffer down by removing data from the tail. If
1052 * the buffer is already under the length specified it is not modified.
1053 * The skb must be linear.
1054 */
1055void skb_trim(struct sk_buff *skb, unsigned int len)
1056{
1057 if (skb->len > len)
1058 __skb_trim(skb, len);
1059}
1060EXPORT_SYMBOL(skb_trim);
1061
Herbert Xu3cc0e872006-06-09 16:13:38 -07001062/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
1064
Herbert Xu3cc0e872006-06-09 16:13:38 -07001065int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Herbert Xu27b437c2006-07-13 19:26:39 -07001067 struct sk_buff **fragp;
1068 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 int offset = skb_headlen(skb);
1070 int nfrags = skb_shinfo(skb)->nr_frags;
1071 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001072 int err;
1073
1074 if (skb_cloned(skb) &&
1075 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001078 i = 0;
1079 if (offset >= len)
1080 goto drop_pages;
1081
1082 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -07001084
1085 if (end < len) {
1086 offset = end;
1087 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001089
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001090 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -07001091
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001092drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001093 skb_shinfo(skb)->nr_frags = i;
1094
1095 for (; i < nfrags; i++)
1096 put_page(skb_shinfo(skb)->frags[i].page);
1097
David S. Miller21dc3302010-08-23 00:13:46 -07001098 if (skb_has_frag_list(skb))
Herbert Xu27b437c2006-07-13 19:26:39 -07001099 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001100 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Herbert Xu27b437c2006-07-13 19:26:39 -07001103 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1104 fragp = &frag->next) {
1105 int end = offset + frag->len;
1106
1107 if (skb_shared(frag)) {
1108 struct sk_buff *nfrag;
1109
1110 nfrag = skb_clone(frag, GFP_ATOMIC);
1111 if (unlikely(!nfrag))
1112 return -ENOMEM;
1113
1114 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001115 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001116 frag = nfrag;
1117 *fragp = frag;
1118 }
1119
1120 if (end < len) {
1121 offset = end;
1122 continue;
1123 }
1124
1125 if (end > len &&
1126 unlikely((err = pskb_trim(frag, len - offset))))
1127 return err;
1128
1129 if (frag->next)
1130 skb_drop_list(&frag->next);
1131 break;
1132 }
1133
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001134done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001135 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 skb->data_len -= skb->len - len;
1137 skb->len = len;
1138 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001139 skb->len = len;
1140 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001141 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 return 0;
1145}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001146EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148/**
1149 * __pskb_pull_tail - advance tail of skb header
1150 * @skb: buffer to reallocate
1151 * @delta: number of bytes to advance tail
1152 *
1153 * The function makes a sense only on a fragmented &sk_buff,
1154 * it expands header moving its tail forward and copying necessary
1155 * data from fragmented part.
1156 *
1157 * &sk_buff MUST have reference count of 1.
1158 *
1159 * Returns %NULL (and &sk_buff does not change) if pull failed
1160 * or value of new tail of skb in the case of success.
1161 *
1162 * All the pointers pointing into skb header may change and must be
1163 * reloaded after call to this function.
1164 */
1165
1166/* Moves tail of skb head forward, copying data from fragmented part,
1167 * when it is necessary.
1168 * 1. It may fail due to malloc failure.
1169 * 2. It may change skb pointers.
1170 *
1171 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1172 */
1173unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1174{
1175 /* If skb has not enough free space at tail, get new one
1176 * plus 128 bytes for future expansions. If we have enough
1177 * room at tail, reallocate without expansion only if skb is cloned.
1178 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001179 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (eat > 0 || skb_cloned(skb)) {
1182 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1183 GFP_ATOMIC))
1184 return NULL;
1185 }
1186
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001187 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 BUG();
1189
1190 /* Optimization: no fragments, no reasons to preestimate
1191 * size of pulled pages. Superb.
1192 */
David S. Miller21dc3302010-08-23 00:13:46 -07001193 if (!skb_has_frag_list(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto pull_pages;
1195
1196 /* Estimate size of pulled pages. */
1197 eat = delta;
1198 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1199 if (skb_shinfo(skb)->frags[i].size >= eat)
1200 goto pull_pages;
1201 eat -= skb_shinfo(skb)->frags[i].size;
1202 }
1203
1204 /* If we need update frag list, we are in troubles.
1205 * Certainly, it possible to add an offset to skb data,
1206 * but taking into account that pulling is expected to
1207 * be very rare operation, it is worth to fight against
1208 * further bloating skb head and crucify ourselves here instead.
1209 * Pure masohism, indeed. 8)8)
1210 */
1211 if (eat) {
1212 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1213 struct sk_buff *clone = NULL;
1214 struct sk_buff *insp = NULL;
1215
1216 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001217 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (list->len <= eat) {
1220 /* Eaten as whole. */
1221 eat -= list->len;
1222 list = list->next;
1223 insp = list;
1224 } else {
1225 /* Eaten partially. */
1226
1227 if (skb_shared(list)) {
1228 /* Sucks! We need to fork list. :-( */
1229 clone = skb_clone(list, GFP_ATOMIC);
1230 if (!clone)
1231 return NULL;
1232 insp = list->next;
1233 list = clone;
1234 } else {
1235 /* This may be pulled without
1236 * problems. */
1237 insp = list;
1238 }
1239 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001240 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return NULL;
1242 }
1243 break;
1244 }
1245 } while (eat);
1246
1247 /* Free pulled out fragments. */
1248 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1249 skb_shinfo(skb)->frag_list = list->next;
1250 kfree_skb(list);
1251 }
1252 /* And insert new clone at head. */
1253 if (clone) {
1254 clone->next = list;
1255 skb_shinfo(skb)->frag_list = clone;
1256 }
1257 }
1258 /* Success! Now we may commit changes to skb data. */
1259
1260pull_pages:
1261 eat = delta;
1262 k = 0;
1263 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1264 if (skb_shinfo(skb)->frags[i].size <= eat) {
1265 put_page(skb_shinfo(skb)->frags[i].page);
1266 eat -= skb_shinfo(skb)->frags[i].size;
1267 } else {
1268 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1269 if (eat) {
1270 skb_shinfo(skb)->frags[k].page_offset += eat;
1271 skb_shinfo(skb)->frags[k].size -= eat;
1272 eat = 0;
1273 }
1274 k++;
1275 }
1276 }
1277 skb_shinfo(skb)->nr_frags = k;
1278
1279 skb->tail += delta;
1280 skb->data_len -= delta;
1281
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001282 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001284EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286/* Copy some data bits from skb to kernel buffer. */
1287
1288int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1289{
David S. Miller1a028e52007-04-27 15:21:23 -07001290 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001291 struct sk_buff *frag_iter;
1292 int i, copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (offset > (int)skb->len - len)
1295 goto fault;
1296
1297 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001298 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (copy > len)
1300 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001301 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if ((len -= copy) == 0)
1303 return 0;
1304 offset += copy;
1305 to += copy;
1306 }
1307
1308 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
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 + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((copy = end - offset) > 0) {
1315 u8 *vaddr;
1316
1317 if (copy > len)
1318 copy = len;
1319
1320 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1321 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001322 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1323 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 kunmap_skb_frag(vaddr);
1325
1326 if ((len -= copy) == 0)
1327 return 0;
1328 offset += copy;
1329 to += copy;
1330 }
David S. Miller1a028e52007-04-27 15:21:23 -07001331 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
David S. Millerfbb398a2009-06-09 00:18:59 -07001334 skb_walk_frags(skb, frag_iter) {
1335 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
David S. Millerfbb398a2009-06-09 00:18:59 -07001337 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
David S. Millerfbb398a2009-06-09 00:18:59 -07001339 end = start + frag_iter->len;
1340 if ((copy = end - offset) > 0) {
1341 if (copy > len)
1342 copy = len;
1343 if (skb_copy_bits(frag_iter, offset - start, to, copy))
1344 goto fault;
1345 if ((len -= copy) == 0)
1346 return 0;
1347 offset += copy;
1348 to += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001350 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 if (!len)
1353 return 0;
1354
1355fault:
1356 return -EFAULT;
1357}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001358EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jens Axboe9c55e012007-11-06 23:30:13 -08001360/*
1361 * Callback from splice_to_pipe(), if we need to release some pages
1362 * at the end of the spd in case we error'ed out in filling the pipe.
1363 */
1364static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1365{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001366 put_page(spd->pages[i]);
1367}
Jens Axboe9c55e012007-11-06 23:30:13 -08001368
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001369static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1370 unsigned int *offset,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001371 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001372{
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001373 struct page *p = sk->sk_sndmsg_page;
1374 unsigned int off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001375
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001376 if (!p) {
1377new_page:
1378 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1379 if (!p)
1380 return NULL;
1381
1382 off = sk->sk_sndmsg_off = 0;
1383 /* hold one ref to this page until it's full */
1384 } else {
1385 unsigned int mlen;
1386
1387 off = sk->sk_sndmsg_off;
1388 mlen = PAGE_SIZE - off;
1389 if (mlen < 64 && mlen < *len) {
1390 put_page(p);
1391 goto new_page;
1392 }
1393
1394 *len = min_t(unsigned int, *len, mlen);
1395 }
1396
1397 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1398 sk->sk_sndmsg_off += *len;
1399 *offset = off;
1400 get_page(p);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001401
1402 return p;
Jens Axboe9c55e012007-11-06 23:30:13 -08001403}
1404
1405/*
1406 * Fill page/offset/length into spd, if it can hold more pages.
1407 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001408static inline int spd_fill_page(struct splice_pipe_desc *spd,
1409 struct pipe_inode_info *pipe, struct page *page,
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001410 unsigned int *len, unsigned int offset,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001411 struct sk_buff *skb, int linear,
1412 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001413{
Jens Axboe35f3d142010-05-20 10:43:18 +02001414 if (unlikely(spd->nr_pages == pipe->buffers))
Jens Axboe9c55e012007-11-06 23:30:13 -08001415 return 1;
1416
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001417 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001418 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001419 if (!page)
1420 return 1;
1421 } else
1422 get_page(page);
1423
Jens Axboe9c55e012007-11-06 23:30:13 -08001424 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001425 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001426 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001427 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001428
Jens Axboe9c55e012007-11-06 23:30:13 -08001429 return 0;
1430}
1431
Octavian Purdila2870c432008-07-15 00:49:11 -07001432static inline void __segment_seek(struct page **page, unsigned int *poff,
1433 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001434{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001435 unsigned long n;
1436
Octavian Purdila2870c432008-07-15 00:49:11 -07001437 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001438 n = *poff / PAGE_SIZE;
1439 if (n)
1440 *page = nth_page(*page, n);
1441
Octavian Purdila2870c432008-07-15 00:49:11 -07001442 *poff = *poff % PAGE_SIZE;
1443 *plen -= off;
1444}
Jens Axboe9c55e012007-11-06 23:30:13 -08001445
Octavian Purdila2870c432008-07-15 00:49:11 -07001446static inline int __splice_segment(struct page *page, unsigned int poff,
1447 unsigned int plen, unsigned int *off,
1448 unsigned int *len, struct sk_buff *skb,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001449 struct splice_pipe_desc *spd, int linear,
Jens Axboe35f3d142010-05-20 10:43:18 +02001450 struct sock *sk,
1451 struct pipe_inode_info *pipe)
Octavian Purdila2870c432008-07-15 00:49:11 -07001452{
1453 if (!*len)
1454 return 1;
1455
1456 /* skip this segment if already processed */
1457 if (*off >= plen) {
1458 *off -= plen;
1459 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001460 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001461
Octavian Purdila2870c432008-07-15 00:49:11 -07001462 /* ignore any bits we already processed */
1463 if (*off) {
1464 __segment_seek(&page, &poff, &plen, *off);
1465 *off = 0;
1466 }
1467
1468 do {
1469 unsigned int flen = min(*len, plen);
1470
1471 /* the linear region may spread across several pages */
1472 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1473
Jens Axboe35f3d142010-05-20 10:43:18 +02001474 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
Octavian Purdila2870c432008-07-15 00:49:11 -07001475 return 1;
1476
1477 __segment_seek(&page, &poff, &plen, flen);
1478 *len -= flen;
1479
1480 } while (*len && plen);
1481
1482 return 0;
1483}
1484
1485/*
1486 * Map linear and fragment data from the skb to spd. It reports failure if the
1487 * pipe is full or if we already spliced the requested length.
1488 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001489static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1490 unsigned int *offset, unsigned int *len,
1491 struct splice_pipe_desc *spd, struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001492{
1493 int seg;
1494
Jens Axboe9c55e012007-11-06 23:30:13 -08001495 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001496 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001497 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001498 if (__splice_segment(virt_to_page(skb->data),
1499 (unsigned long) skb->data & (PAGE_SIZE - 1),
1500 skb_headlen(skb),
Jens Axboe35f3d142010-05-20 10:43:18 +02001501 offset, len, skb, spd, 1, sk, pipe))
Octavian Purdila2870c432008-07-15 00:49:11 -07001502 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001503
1504 /*
1505 * then map the fragments
1506 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001507 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1508 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1509
Octavian Purdila2870c432008-07-15 00:49:11 -07001510 if (__splice_segment(f->page, f->page_offset, f->size,
Jens Axboe35f3d142010-05-20 10:43:18 +02001511 offset, len, skb, spd, 0, sk, pipe))
Octavian Purdila2870c432008-07-15 00:49:11 -07001512 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001513 }
1514
Octavian Purdila2870c432008-07-15 00:49:11 -07001515 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001516}
1517
1518/*
1519 * Map data from the skb to a pipe. Should handle both the linear part,
1520 * the fragments, and the frag list. It does NOT handle frag lists within
1521 * the frag list, if such a thing exists. We'd probably need to recurse to
1522 * handle that cleanly.
1523 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001524int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001525 struct pipe_inode_info *pipe, unsigned int tlen,
1526 unsigned int flags)
1527{
Jens Axboe35f3d142010-05-20 10:43:18 +02001528 struct partial_page partial[PIPE_DEF_BUFFERS];
1529 struct page *pages[PIPE_DEF_BUFFERS];
Jens Axboe9c55e012007-11-06 23:30:13 -08001530 struct splice_pipe_desc spd = {
1531 .pages = pages,
1532 .partial = partial,
1533 .flags = flags,
1534 .ops = &sock_pipe_buf_ops,
1535 .spd_release = sock_spd_release,
1536 };
David S. Millerfbb398a2009-06-09 00:18:59 -07001537 struct sk_buff *frag_iter;
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001538 struct sock *sk = skb->sk;
Jens Axboe35f3d142010-05-20 10:43:18 +02001539 int ret = 0;
1540
1541 if (splice_grow_spd(pipe, &spd))
1542 return -ENOMEM;
Jens Axboe9c55e012007-11-06 23:30:13 -08001543
1544 /*
1545 * __skb_splice_bits() only fails if the output has no room left,
1546 * so no point in going over the frag_list for the error case.
1547 */
Jens Axboe35f3d142010-05-20 10:43:18 +02001548 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001549 goto done;
1550 else if (!tlen)
1551 goto done;
1552
1553 /*
1554 * now see if we have a frag_list to map
1555 */
David S. Millerfbb398a2009-06-09 00:18:59 -07001556 skb_walk_frags(skb, frag_iter) {
1557 if (!tlen)
1558 break;
Jens Axboe35f3d142010-05-20 10:43:18 +02001559 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
David S. Millerfbb398a2009-06-09 00:18:59 -07001560 break;
Jens Axboe9c55e012007-11-06 23:30:13 -08001561 }
1562
1563done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001564 if (spd.nr_pages) {
Jens Axboe9c55e012007-11-06 23:30:13 -08001565 /*
1566 * Drop the socket lock, otherwise we have reverse
1567 * locking dependencies between sk_lock and i_mutex
1568 * here as compared to sendfile(). We enter here
1569 * with the socket lock held, and splice_to_pipe() will
1570 * grab the pipe inode lock. For sendfile() emulation,
1571 * we call into ->sendpage() with the i_mutex lock held
1572 * and networking will grab the socket lock.
1573 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001574 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001575 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001576 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001577 }
1578
Jens Axboe35f3d142010-05-20 10:43:18 +02001579 splice_shrink_spd(pipe, &spd);
1580 return ret;
Jens Axboe9c55e012007-11-06 23:30:13 -08001581}
1582
Herbert Xu357b40a2005-04-19 22:30:14 -07001583/**
1584 * skb_store_bits - store bits from kernel buffer to skb
1585 * @skb: destination buffer
1586 * @offset: offset in destination
1587 * @from: source buffer
1588 * @len: number of bytes to copy
1589 *
1590 * Copy the specified number of bytes from the source buffer to the
1591 * destination skb. This function handles all the messy bits of
1592 * traversing fragment lists and such.
1593 */
1594
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001595int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001596{
David S. Miller1a028e52007-04-27 15:21:23 -07001597 int start = skb_headlen(skb);
David S. Millerfbb398a2009-06-09 00:18:59 -07001598 struct sk_buff *frag_iter;
1599 int i, copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001600
1601 if (offset > (int)skb->len - len)
1602 goto fault;
1603
David S. Miller1a028e52007-04-27 15:21:23 -07001604 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001605 if (copy > len)
1606 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001607 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001608 if ((len -= copy) == 0)
1609 return 0;
1610 offset += copy;
1611 from += copy;
1612 }
1613
1614 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1615 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001616 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001617
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001618 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001619
1620 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001621 if ((copy = end - offset) > 0) {
1622 u8 *vaddr;
1623
1624 if (copy > len)
1625 copy = len;
1626
1627 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001628 memcpy(vaddr + frag->page_offset + offset - start,
1629 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001630 kunmap_skb_frag(vaddr);
1631
1632 if ((len -= copy) == 0)
1633 return 0;
1634 offset += copy;
1635 from += copy;
1636 }
David S. Miller1a028e52007-04-27 15:21:23 -07001637 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001638 }
1639
David S. Millerfbb398a2009-06-09 00:18:59 -07001640 skb_walk_frags(skb, frag_iter) {
1641 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001642
David S. Millerfbb398a2009-06-09 00:18:59 -07001643 WARN_ON(start > offset + len);
Herbert Xu357b40a2005-04-19 22:30:14 -07001644
David S. Millerfbb398a2009-06-09 00:18:59 -07001645 end = start + frag_iter->len;
1646 if ((copy = end - offset) > 0) {
1647 if (copy > len)
1648 copy = len;
1649 if (skb_store_bits(frag_iter, offset - start,
1650 from, copy))
1651 goto fault;
1652 if ((len -= copy) == 0)
1653 return 0;
1654 offset += copy;
1655 from += copy;
Herbert Xu357b40a2005-04-19 22:30:14 -07001656 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001657 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001658 }
1659 if (!len)
1660 return 0;
1661
1662fault:
1663 return -EFAULT;
1664}
Herbert Xu357b40a2005-04-19 22:30:14 -07001665EXPORT_SYMBOL(skb_store_bits);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/* Checksum skb data. */
1668
Al Viro2bbbc862006-11-14 21:37:14 -08001669__wsum skb_checksum(const struct sk_buff *skb, int offset,
1670 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
David S. Miller1a028e52007-04-27 15:21:23 -07001672 int start = skb_headlen(skb);
1673 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001674 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int pos = 0;
1676
1677 /* Checksum header. */
1678 if (copy > 0) {
1679 if (copy > len)
1680 copy = len;
1681 csum = csum_partial(skb->data + offset, copy, csum);
1682 if ((len -= copy) == 0)
1683 return csum;
1684 offset += copy;
1685 pos = copy;
1686 }
1687
1688 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001689 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001691 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001692
1693 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001695 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 u8 *vaddr;
1697 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1698
1699 if (copy > len)
1700 copy = len;
1701 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001702 csum2 = csum_partial(vaddr + frag->page_offset +
1703 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 kunmap_skb_frag(vaddr);
1705 csum = csum_block_add(csum, csum2, pos);
1706 if (!(len -= copy))
1707 return csum;
1708 offset += copy;
1709 pos += copy;
1710 }
David S. Miller1a028e52007-04-27 15:21:23 -07001711 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
David S. Millerfbb398a2009-06-09 00:18:59 -07001714 skb_walk_frags(skb, frag_iter) {
1715 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
David S. Millerfbb398a2009-06-09 00:18:59 -07001717 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
David S. Millerfbb398a2009-06-09 00:18:59 -07001719 end = start + frag_iter->len;
1720 if ((copy = end - offset) > 0) {
1721 __wsum csum2;
1722 if (copy > len)
1723 copy = len;
1724 csum2 = skb_checksum(frag_iter, offset - start,
1725 copy, 0);
1726 csum = csum_block_add(csum, csum2, pos);
1727 if ((len -= copy) == 0)
1728 return csum;
1729 offset += copy;
1730 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001732 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001734 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 return csum;
1737}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001738EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740/* Both of above in one bottle. */
1741
Al Viro81d77662006-11-14 21:37:33 -08001742__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1743 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
David S. Miller1a028e52007-04-27 15:21:23 -07001745 int start = skb_headlen(skb);
1746 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07001747 struct sk_buff *frag_iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int pos = 0;
1749
1750 /* Copy header. */
1751 if (copy > 0) {
1752 if (copy > len)
1753 copy = len;
1754 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1755 copy, csum);
1756 if ((len -= copy) == 0)
1757 return csum;
1758 offset += copy;
1759 to += copy;
1760 pos = copy;
1761 }
1762
1763 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001764 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001766 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001767
1768 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001770 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 u8 *vaddr;
1772 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1773
1774 if (copy > len)
1775 copy = len;
1776 vaddr = kmap_skb_frag(frag);
1777 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001778 frag->page_offset +
1779 offset - start, to,
1780 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 kunmap_skb_frag(vaddr);
1782 csum = csum_block_add(csum, csum2, pos);
1783 if (!(len -= copy))
1784 return csum;
1785 offset += copy;
1786 to += copy;
1787 pos += copy;
1788 }
David S. Miller1a028e52007-04-27 15:21:23 -07001789 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791
David S. Millerfbb398a2009-06-09 00:18:59 -07001792 skb_walk_frags(skb, frag_iter) {
1793 __wsum csum2;
1794 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
David S. Millerfbb398a2009-06-09 00:18:59 -07001796 WARN_ON(start > offset + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David S. Millerfbb398a2009-06-09 00:18:59 -07001798 end = start + frag_iter->len;
1799 if ((copy = end - offset) > 0) {
1800 if (copy > len)
1801 copy = len;
1802 csum2 = skb_copy_and_csum_bits(frag_iter,
1803 offset - start,
1804 to, copy, 0);
1805 csum = csum_block_add(csum, csum2, pos);
1806 if ((len -= copy) == 0)
1807 return csum;
1808 offset += copy;
1809 to += copy;
1810 pos += copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
David S. Millerfbb398a2009-06-09 00:18:59 -07001812 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001814 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return csum;
1816}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001817EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1820{
Al Virod3bc23e2006-11-14 21:24:49 -08001821 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 long csstart;
1823
Patrick McHardy84fa7932006-08-29 16:44:56 -07001824 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001825 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 else
1827 csstart = skb_headlen(skb);
1828
Kris Katterjohn09a62662006-01-08 22:24:28 -08001829 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001831 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 csum = 0;
1834 if (csstart != skb->len)
1835 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1836 skb->len - csstart, 0);
1837
Patrick McHardy84fa7932006-08-29 16:44:56 -07001838 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001839 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Al Virod3bc23e2006-11-14 21:24:49 -08001841 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001844EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846/**
1847 * skb_dequeue - remove from the head of the queue
1848 * @list: list to dequeue from
1849 *
1850 * Remove the head of the list. The list lock is taken so the function
1851 * may be used safely with other locking list functions. The head item is
1852 * returned or %NULL if the list is empty.
1853 */
1854
1855struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1856{
1857 unsigned long flags;
1858 struct sk_buff *result;
1859
1860 spin_lock_irqsave(&list->lock, flags);
1861 result = __skb_dequeue(list);
1862 spin_unlock_irqrestore(&list->lock, flags);
1863 return result;
1864}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001865EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867/**
1868 * skb_dequeue_tail - remove from the tail of the queue
1869 * @list: list to dequeue from
1870 *
1871 * Remove the tail of the list. The list lock is taken so the function
1872 * may be used safely with other locking list functions. The tail item is
1873 * returned or %NULL if the list is empty.
1874 */
1875struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1876{
1877 unsigned long flags;
1878 struct sk_buff *result;
1879
1880 spin_lock_irqsave(&list->lock, flags);
1881 result = __skb_dequeue_tail(list);
1882 spin_unlock_irqrestore(&list->lock, flags);
1883 return result;
1884}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001885EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887/**
1888 * skb_queue_purge - empty a list
1889 * @list: list to empty
1890 *
1891 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1892 * the list and one reference dropped. This function takes the list
1893 * lock and is atomic with respect to other list locking functions.
1894 */
1895void skb_queue_purge(struct sk_buff_head *list)
1896{
1897 struct sk_buff *skb;
1898 while ((skb = skb_dequeue(list)) != NULL)
1899 kfree_skb(skb);
1900}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001901EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903/**
1904 * skb_queue_head - queue a buffer at the list head
1905 * @list: list to use
1906 * @newsk: buffer to queue
1907 *
1908 * Queue a buffer at the start of the list. This function takes the
1909 * list lock and can be used safely with other locking &sk_buff functions
1910 * safely.
1911 *
1912 * A buffer cannot be placed on two lists at the same time.
1913 */
1914void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1915{
1916 unsigned long flags;
1917
1918 spin_lock_irqsave(&list->lock, flags);
1919 __skb_queue_head(list, newsk);
1920 spin_unlock_irqrestore(&list->lock, flags);
1921}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001922EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924/**
1925 * skb_queue_tail - queue a buffer at the list tail
1926 * @list: list to use
1927 * @newsk: buffer to queue
1928 *
1929 * Queue a buffer at the tail of the list. This function takes the
1930 * list lock and can be used safely with other locking &sk_buff functions
1931 * safely.
1932 *
1933 * A buffer cannot be placed on two lists at the same time.
1934 */
1935void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1936{
1937 unsigned long flags;
1938
1939 spin_lock_irqsave(&list->lock, flags);
1940 __skb_queue_tail(list, newsk);
1941 spin_unlock_irqrestore(&list->lock, flags);
1942}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001943EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/**
1946 * skb_unlink - remove a buffer from a list
1947 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001948 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 *
David S. Miller8728b832005-08-09 19:25:21 -07001950 * Remove a packet from a list. The list locks are taken and this
1951 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 *
David S. Miller8728b832005-08-09 19:25:21 -07001953 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 */
David S. Miller8728b832005-08-09 19:25:21 -07001955void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
David S. Miller8728b832005-08-09 19:25:21 -07001957 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
David S. Miller8728b832005-08-09 19:25:21 -07001959 spin_lock_irqsave(&list->lock, flags);
1960 __skb_unlink(skb, list);
1961 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001963EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/**
1966 * skb_append - append a buffer
1967 * @old: buffer to insert after
1968 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001969 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 *
1971 * Place a packet after a given packet in a list. The list locks are taken
1972 * and this function is atomic with respect to other list locked calls.
1973 * A buffer cannot be placed on two lists at the same time.
1974 */
David S. Miller8728b832005-08-09 19:25:21 -07001975void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 unsigned long flags;
1978
David S. Miller8728b832005-08-09 19:25:21 -07001979 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001980 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001981 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001983EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985/**
1986 * skb_insert - insert a buffer
1987 * @old: buffer to insert before
1988 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001989 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 *
David S. Miller8728b832005-08-09 19:25:21 -07001991 * Place a packet before a given packet in a list. The list locks are
1992 * taken and this function is atomic with respect to other list locked
1993 * calls.
1994 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * A buffer cannot be placed on two lists at the same time.
1996 */
David S. Miller8728b832005-08-09 19:25:21 -07001997void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 unsigned long flags;
2000
David S. Miller8728b832005-08-09 19:25:21 -07002001 spin_lock_irqsave(&list->lock, flags);
2002 __skb_insert(newsk, old->prev, old, list);
2003 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002005EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007static inline void skb_split_inside_header(struct sk_buff *skb,
2008 struct sk_buff* skb1,
2009 const u32 len, const int pos)
2010{
2011 int i;
2012
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002013 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2014 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* And move data appendix as is. */
2016 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2017 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2018
2019 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2020 skb_shinfo(skb)->nr_frags = 0;
2021 skb1->data_len = skb->data_len;
2022 skb1->len += skb1->data_len;
2023 skb->data_len = 0;
2024 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002025 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
2028static inline void skb_split_no_header(struct sk_buff *skb,
2029 struct sk_buff* skb1,
2030 const u32 len, int pos)
2031{
2032 int i, k = 0;
2033 const int nfrags = skb_shinfo(skb)->nr_frags;
2034
2035 skb_shinfo(skb)->nr_frags = 0;
2036 skb1->len = skb1->data_len = skb->len - len;
2037 skb->len = len;
2038 skb->data_len = len - pos;
2039
2040 for (i = 0; i < nfrags; i++) {
2041 int size = skb_shinfo(skb)->frags[i].size;
2042
2043 if (pos + size > len) {
2044 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2045
2046 if (pos < len) {
2047 /* Split frag.
2048 * We have two variants in this case:
2049 * 1. Move all the frag to the second
2050 * part, if it is possible. F.e.
2051 * this approach is mandatory for TUX,
2052 * where splitting is expensive.
2053 * 2. Split is accurately. We make this.
2054 */
2055 get_page(skb_shinfo(skb)->frags[i].page);
2056 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2057 skb_shinfo(skb1)->frags[0].size -= len - pos;
2058 skb_shinfo(skb)->frags[i].size = len - pos;
2059 skb_shinfo(skb)->nr_frags++;
2060 }
2061 k++;
2062 } else
2063 skb_shinfo(skb)->nr_frags++;
2064 pos += size;
2065 }
2066 skb_shinfo(skb1)->nr_frags = k;
2067}
2068
2069/**
2070 * skb_split - Split fragmented skb to two parts at length len.
2071 * @skb: the buffer to split
2072 * @skb1: the buffer to receive the second part
2073 * @len: new length for skb
2074 */
2075void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2076{
2077 int pos = skb_headlen(skb);
2078
2079 if (len < pos) /* Split line is inside header. */
2080 skb_split_inside_header(skb, skb1, len, pos);
2081 else /* Second chunk has no header, nothing to copy. */
2082 skb_split_no_header(skb, skb1, len, pos);
2083}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002084EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002086/* Shifting from/to a cloned skb is a no-go.
2087 *
2088 * Caller cannot keep skb_shinfo related pointers past calling here!
2089 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002090static int skb_prepare_for_shift(struct sk_buff *skb)
2091{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002092 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002093}
2094
2095/**
2096 * skb_shift - Shifts paged data partially from skb to another
2097 * @tgt: buffer into which tail data gets added
2098 * @skb: buffer from which the paged data comes from
2099 * @shiftlen: shift up to this many bytes
2100 *
2101 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2102 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2103 * It's up to caller to free skb if everything was shifted.
2104 *
2105 * If @tgt runs out of frags, the whole operation is aborted.
2106 *
2107 * Skb cannot include anything else but paged data while tgt is allowed
2108 * to have non-paged data as well.
2109 *
2110 * TODO: full sized shift could be optimized but that would need
2111 * specialized skb free'er to handle frags without up-to-date nr_frags.
2112 */
2113int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2114{
2115 int from, to, merge, todo;
2116 struct skb_frag_struct *fragfrom, *fragto;
2117
2118 BUG_ON(shiftlen > skb->len);
2119 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2120
2121 todo = shiftlen;
2122 from = 0;
2123 to = skb_shinfo(tgt)->nr_frags;
2124 fragfrom = &skb_shinfo(skb)->frags[from];
2125
2126 /* Actual merge is delayed until the point when we know we can
2127 * commit all, so that we don't have to undo partial changes
2128 */
2129 if (!to ||
2130 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2131 merge = -1;
2132 } else {
2133 merge = to - 1;
2134
2135 todo -= fragfrom->size;
2136 if (todo < 0) {
2137 if (skb_prepare_for_shift(skb) ||
2138 skb_prepare_for_shift(tgt))
2139 return 0;
2140
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002141 /* All previous frag pointers might be stale! */
2142 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002143 fragto = &skb_shinfo(tgt)->frags[merge];
2144
2145 fragto->size += shiftlen;
2146 fragfrom->size -= shiftlen;
2147 fragfrom->page_offset += shiftlen;
2148
2149 goto onlymerged;
2150 }
2151
2152 from++;
2153 }
2154
2155 /* Skip full, not-fitting skb to avoid expensive operations */
2156 if ((shiftlen == skb->len) &&
2157 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2158 return 0;
2159
2160 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2161 return 0;
2162
2163 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2164 if (to == MAX_SKB_FRAGS)
2165 return 0;
2166
2167 fragfrom = &skb_shinfo(skb)->frags[from];
2168 fragto = &skb_shinfo(tgt)->frags[to];
2169
2170 if (todo >= fragfrom->size) {
2171 *fragto = *fragfrom;
2172 todo -= fragfrom->size;
2173 from++;
2174 to++;
2175
2176 } else {
2177 get_page(fragfrom->page);
2178 fragto->page = fragfrom->page;
2179 fragto->page_offset = fragfrom->page_offset;
2180 fragto->size = todo;
2181
2182 fragfrom->page_offset += todo;
2183 fragfrom->size -= todo;
2184 todo = 0;
2185
2186 to++;
2187 break;
2188 }
2189 }
2190
2191 /* Ready to "commit" this state change to tgt */
2192 skb_shinfo(tgt)->nr_frags = to;
2193
2194 if (merge >= 0) {
2195 fragfrom = &skb_shinfo(skb)->frags[0];
2196 fragto = &skb_shinfo(tgt)->frags[merge];
2197
2198 fragto->size += fragfrom->size;
2199 put_page(fragfrom->page);
2200 }
2201
2202 /* Reposition in the original skb */
2203 to = 0;
2204 while (from < skb_shinfo(skb)->nr_frags)
2205 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2206 skb_shinfo(skb)->nr_frags = to;
2207
2208 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2209
2210onlymerged:
2211 /* Most likely the tgt won't ever need its checksum anymore, skb on
2212 * the other hand might need it if it needs to be resent
2213 */
2214 tgt->ip_summed = CHECKSUM_PARTIAL;
2215 skb->ip_summed = CHECKSUM_PARTIAL;
2216
2217 /* Yak, is it really working this way? Some helper please? */
2218 skb->len -= shiftlen;
2219 skb->data_len -= shiftlen;
2220 skb->truesize -= shiftlen;
2221 tgt->len += shiftlen;
2222 tgt->data_len += shiftlen;
2223 tgt->truesize += shiftlen;
2224
2225 return shiftlen;
2226}
2227
Thomas Graf677e90e2005-06-23 20:59:51 -07002228/**
2229 * skb_prepare_seq_read - Prepare a sequential read of skb data
2230 * @skb: the buffer to read
2231 * @from: lower offset of data to be read
2232 * @to: upper offset of data to be read
2233 * @st: state variable
2234 *
2235 * Initializes the specified state variable. Must be called before
2236 * invoking skb_seq_read() for the first time.
2237 */
2238void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2239 unsigned int to, struct skb_seq_state *st)
2240{
2241 st->lower_offset = from;
2242 st->upper_offset = to;
2243 st->root_skb = st->cur_skb = skb;
2244 st->frag_idx = st->stepped_offset = 0;
2245 st->frag_data = NULL;
2246}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002247EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002248
2249/**
2250 * skb_seq_read - Sequentially read skb data
2251 * @consumed: number of bytes consumed by the caller so far
2252 * @data: destination pointer for data to be returned
2253 * @st: state variable
2254 *
2255 * Reads a block of skb data at &consumed relative to the
2256 * lower offset specified to skb_prepare_seq_read(). Assigns
2257 * the head of the data block to &data and returns the length
2258 * of the block or 0 if the end of the skb data or the upper
2259 * offset has been reached.
2260 *
2261 * The caller is not required to consume all of the data
2262 * returned, i.e. &consumed is typically set to the number
2263 * of bytes already consumed and the next call to
2264 * skb_seq_read() will return the remaining part of the block.
2265 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002266 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002267 * this limitation is the cost for zerocopy seqeuental
2268 * reads of potentially non linear data.
2269 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002270 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002271 * at the moment, state->root_skb could be replaced with
2272 * a stack for this purpose.
2273 */
2274unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2275 struct skb_seq_state *st)
2276{
2277 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2278 skb_frag_t *frag;
2279
2280 if (unlikely(abs_offset >= st->upper_offset))
2281 return 0;
2282
2283next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002284 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002285
Thomas Chenault995b3372009-05-18 21:43:27 -07002286 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002287 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002288 return block_limit - abs_offset;
2289 }
2290
2291 if (st->frag_idx == 0 && !st->frag_data)
2292 st->stepped_offset += skb_headlen(st->cur_skb);
2293
2294 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2295 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2296 block_limit = frag->size + st->stepped_offset;
2297
2298 if (abs_offset < block_limit) {
2299 if (!st->frag_data)
2300 st->frag_data = kmap_skb_frag(frag);
2301
2302 *data = (u8 *) st->frag_data + frag->page_offset +
2303 (abs_offset - st->stepped_offset);
2304
2305 return block_limit - abs_offset;
2306 }
2307
2308 if (st->frag_data) {
2309 kunmap_skb_frag(st->frag_data);
2310 st->frag_data = NULL;
2311 }
2312
2313 st->frag_idx++;
2314 st->stepped_offset += frag->size;
2315 }
2316
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002317 if (st->frag_data) {
2318 kunmap_skb_frag(st->frag_data);
2319 st->frag_data = NULL;
2320 }
2321
David S. Miller21dc3302010-08-23 00:13:46 -07002322 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
Shyam Iyer71b33462009-01-29 16:12:42 -08002323 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002324 st->frag_idx = 0;
2325 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002326 } else if (st->cur_skb->next) {
2327 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002328 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002329 goto next_skb;
2330 }
2331
2332 return 0;
2333}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002334EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002335
2336/**
2337 * skb_abort_seq_read - Abort a sequential read of skb data
2338 * @st: state variable
2339 *
2340 * Must be called if skb_seq_read() was not called until it
2341 * returned 0.
2342 */
2343void skb_abort_seq_read(struct skb_seq_state *st)
2344{
2345 if (st->frag_data)
2346 kunmap_skb_frag(st->frag_data);
2347}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002348EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002349
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002350#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2351
2352static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2353 struct ts_config *conf,
2354 struct ts_state *state)
2355{
2356 return skb_seq_read(offset, text, TS_SKB_CB(state));
2357}
2358
2359static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2360{
2361 skb_abort_seq_read(TS_SKB_CB(state));
2362}
2363
2364/**
2365 * skb_find_text - Find a text pattern in skb data
2366 * @skb: the buffer to look in
2367 * @from: search offset
2368 * @to: search limit
2369 * @config: textsearch configuration
2370 * @state: uninitialized textsearch state variable
2371 *
2372 * Finds a pattern in the skb data according to the specified
2373 * textsearch configuration. Use textsearch_next() to retrieve
2374 * subsequent occurrences of the pattern. Returns the offset
2375 * to the first occurrence or UINT_MAX if no match was found.
2376 */
2377unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2378 unsigned int to, struct ts_config *config,
2379 struct ts_state *state)
2380{
Phil Oesterf72b9482006-06-26 00:00:57 -07002381 unsigned int ret;
2382
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002383 config->get_next_block = skb_ts_get_next_block;
2384 config->finish = skb_ts_finish;
2385
2386 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2387
Phil Oesterf72b9482006-06-26 00:00:57 -07002388 ret = textsearch_find(config, state);
2389 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002390}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002391EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002392
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002393/**
2394 * skb_append_datato_frags: - append the user data to a skb
2395 * @sk: sock structure
2396 * @skb: skb structure to be appened with user data.
2397 * @getfrag: call back function to be used for getting the user data
2398 * @from: pointer to user message iov
2399 * @length: length of the iov message
2400 *
2401 * Description: This procedure append the user data in the fragment part
2402 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2403 */
2404int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002405 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002406 int len, int odd, struct sk_buff *skb),
2407 void *from, int length)
2408{
2409 int frg_cnt = 0;
2410 skb_frag_t *frag = NULL;
2411 struct page *page = NULL;
2412 int copy, left;
2413 int offset = 0;
2414 int ret;
2415
2416 do {
2417 /* Return error if we don't have space for new frag */
2418 frg_cnt = skb_shinfo(skb)->nr_frags;
2419 if (frg_cnt >= MAX_SKB_FRAGS)
2420 return -EFAULT;
2421
2422 /* allocate a new page for next frag */
2423 page = alloc_pages(sk->sk_allocation, 0);
2424
2425 /* If alloc_page fails just return failure and caller will
2426 * free previous allocated pages by doing kfree_skb()
2427 */
2428 if (page == NULL)
2429 return -ENOMEM;
2430
2431 /* initialize the next frag */
2432 sk->sk_sndmsg_page = page;
2433 sk->sk_sndmsg_off = 0;
2434 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2435 skb->truesize += PAGE_SIZE;
2436 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2437
2438 /* get the new initialized frag */
2439 frg_cnt = skb_shinfo(skb)->nr_frags;
2440 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2441
2442 /* copy the user data to page */
2443 left = PAGE_SIZE - frag->page_offset;
2444 copy = (length > left)? left : length;
2445
2446 ret = getfrag(from, (page_address(frag->page) +
2447 frag->page_offset + frag->size),
2448 offset, copy, 0, skb);
2449 if (ret < 0)
2450 return -EFAULT;
2451
2452 /* copy was successful so update the size parameters */
2453 sk->sk_sndmsg_off += copy;
2454 frag->size += copy;
2455 skb->len += copy;
2456 skb->data_len += copy;
2457 offset += copy;
2458 length -= copy;
2459
2460 } while (length > 0);
2461
2462 return 0;
2463}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002464EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002465
Herbert Xucbb042f2006-03-20 22:43:56 -08002466/**
2467 * skb_pull_rcsum - pull skb and update receive checksum
2468 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002469 * @len: length of data pulled
2470 *
2471 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002472 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002473 * receive path processing instead of skb_pull unless you know
2474 * that the checksum difference is zero (e.g., a valid IP header)
2475 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002476 */
2477unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2478{
2479 BUG_ON(len > skb->len);
2480 skb->len -= len;
2481 BUG_ON(skb->len < skb->data_len);
2482 skb_postpull_rcsum(skb, skb->data, len);
2483 return skb->data += len;
2484}
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002485EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2486
Herbert Xuf4c50d92006-06-22 03:02:40 -07002487/**
2488 * skb_segment - Perform protocol segmentation on skb.
2489 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002490 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002491 *
2492 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002493 * a pointer to the first in a list of new skbs for the segments.
2494 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002495 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002496struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002497{
2498 struct sk_buff *segs = NULL;
2499 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002500 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002501 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002502 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002503 unsigned int offset = doffset;
2504 unsigned int headroom;
2505 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002506 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002507 int nfrags = skb_shinfo(skb)->nr_frags;
2508 int err = -ENOMEM;
2509 int i = 0;
2510 int pos;
2511
2512 __skb_push(skb, doffset);
2513 headroom = skb_headroom(skb);
2514 pos = skb_headlen(skb);
2515
2516 do {
2517 struct sk_buff *nskb;
2518 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002519 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002520 int size;
2521
2522 len = skb->len - offset;
2523 if (len > mss)
2524 len = mss;
2525
2526 hsize = skb_headlen(skb) - offset;
2527 if (hsize < 0)
2528 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002529 if (hsize > len || !sg)
2530 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002531
Herbert Xu89319d382008-12-15 23:26:06 -08002532 if (!hsize && i >= nfrags) {
2533 BUG_ON(fskb->len != len);
2534
2535 pos += len;
2536 nskb = skb_clone(fskb, GFP_ATOMIC);
2537 fskb = fskb->next;
2538
2539 if (unlikely(!nskb))
2540 goto err;
2541
2542 hsize = skb_end_pointer(nskb) - nskb->head;
2543 if (skb_cow_head(nskb, doffset + headroom)) {
2544 kfree_skb(nskb);
2545 goto err;
2546 }
2547
2548 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2549 hsize;
2550 skb_release_head_state(nskb);
2551 __skb_push(nskb, doffset);
2552 } else {
2553 nskb = alloc_skb(hsize + doffset + headroom,
2554 GFP_ATOMIC);
2555
2556 if (unlikely(!nskb))
2557 goto err;
2558
2559 skb_reserve(nskb, headroom);
2560 __skb_put(nskb, doffset);
2561 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002562
2563 if (segs)
2564 tail->next = nskb;
2565 else
2566 segs = nskb;
2567 tail = nskb;
2568
Herbert Xu6f85a122008-08-15 14:55:02 -07002569 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002570 nskb->mac_len = skb->mac_len;
2571
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002572 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002573 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002574 nskb->transport_header = (nskb->network_header +
2575 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002576 skb_copy_from_linear_data(skb, nskb->data, doffset);
2577
Herbert Xu2f181852009-03-28 23:39:18 -07002578 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002579 continue;
2580
Herbert Xuf4c50d92006-06-22 03:02:40 -07002581 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002582 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002583 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2584 skb_put(nskb, len),
2585 len, 0);
2586 continue;
2587 }
2588
2589 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002590
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002591 skb_copy_from_linear_data_offset(skb, offset,
2592 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002593
Herbert Xu89319d382008-12-15 23:26:06 -08002594 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002595 *frag = skb_shinfo(skb)->frags[i];
2596 get_page(frag->page);
2597 size = frag->size;
2598
2599 if (pos < offset) {
2600 frag->page_offset += offset - pos;
2601 frag->size -= offset - pos;
2602 }
2603
Herbert Xu89319d382008-12-15 23:26:06 -08002604 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002605
2606 if (pos + size <= offset + len) {
2607 i++;
2608 pos += size;
2609 } else {
2610 frag->size -= pos + size - (offset + len);
Herbert Xu89319d382008-12-15 23:26:06 -08002611 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002612 }
2613
2614 frag++;
2615 }
2616
Herbert Xu89319d382008-12-15 23:26:06 -08002617 if (pos < offset + len) {
2618 struct sk_buff *fskb2 = fskb;
2619
2620 BUG_ON(pos + fskb->len != offset + len);
2621
2622 pos += fskb->len;
2623 fskb = fskb->next;
2624
2625 if (fskb2->next) {
2626 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2627 if (!fskb2)
2628 goto err;
2629 } else
2630 skb_get(fskb2);
2631
David S. Millerfbb398a2009-06-09 00:18:59 -07002632 SKB_FRAG_ASSERT(nskb);
Herbert Xu89319d382008-12-15 23:26:06 -08002633 skb_shinfo(nskb)->frag_list = fskb2;
2634 }
2635
2636skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002637 nskb->data_len = len - hsize;
2638 nskb->len += nskb->data_len;
2639 nskb->truesize += nskb->data_len;
2640 } while ((offset += len) < skb->len);
2641
2642 return segs;
2643
2644err:
2645 while ((skb = segs)) {
2646 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002647 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002648 }
2649 return ERR_PTR(err);
2650}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002651EXPORT_SYMBOL_GPL(skb_segment);
2652
Herbert Xu71d93b32008-12-15 23:42:33 -08002653int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2654{
2655 struct sk_buff *p = *head;
2656 struct sk_buff *nskb;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002657 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2658 struct skb_shared_info *pinfo = skb_shinfo(p);
Herbert Xu71d93b32008-12-15 23:42:33 -08002659 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002660 unsigned int len = skb_gro_len(skb);
Herbert Xu67147ba2009-05-26 18:50:22 +00002661 unsigned int offset = skb_gro_offset(skb);
2662 unsigned int headlen = skb_headlen(skb);
Herbert Xu71d93b32008-12-15 23:42:33 -08002663
Herbert Xu86911732009-01-29 14:19:50 +00002664 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002665 return -E2BIG;
2666
Herbert Xu9aaa1562009-05-26 18:50:33 +00002667 if (pinfo->frag_list)
Herbert Xu71d93b32008-12-15 23:42:33 -08002668 goto merge;
Herbert Xu67147ba2009-05-26 18:50:22 +00002669 else if (headlen <= offset) {
Herbert Xu42da6992009-05-26 18:50:19 +00002670 skb_frag_t *frag;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002671 skb_frag_t *frag2;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002672 int i = skbinfo->nr_frags;
2673 int nr_frags = pinfo->nr_frags + i;
Herbert Xu42da6992009-05-26 18:50:19 +00002674
Herbert Xu66e92fc2009-05-26 18:50:32 +00002675 offset -= headlen;
2676
2677 if (nr_frags > MAX_SKB_FRAGS)
Herbert Xu81705ad2009-01-29 14:19:51 +00002678 return -E2BIG;
2679
Herbert Xu9aaa1562009-05-26 18:50:33 +00002680 pinfo->nr_frags = nr_frags;
2681 skbinfo->nr_frags = 0;
Herbert Xuf5572062009-01-14 20:40:03 -08002682
Herbert Xu9aaa1562009-05-26 18:50:33 +00002683 frag = pinfo->frags + nr_frags;
2684 frag2 = skbinfo->frags + i;
Herbert Xu66e92fc2009-05-26 18:50:32 +00002685 do {
2686 *--frag = *--frag2;
2687 } while (--i);
2688
2689 frag->page_offset += offset;
2690 frag->size -= offset;
2691
Herbert Xuf5572062009-01-14 20:40:03 -08002692 skb->truesize -= skb->data_len;
2693 skb->len -= skb->data_len;
2694 skb->data_len = 0;
2695
Herbert Xu5d38a072009-01-04 16:13:40 -08002696 NAPI_GRO_CB(skb)->free = 1;
2697 goto done;
Herbert Xu69c0cab2009-11-17 05:18:18 -08002698 } else if (skb_gro_len(p) != pinfo->gso_size)
2699 return -E2BIG;
Herbert Xu71d93b32008-12-15 23:42:33 -08002700
2701 headroom = skb_headroom(p);
Herbert Xu86911732009-01-29 14:19:50 +00002702 nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002703 if (unlikely(!nskb))
2704 return -ENOMEM;
2705
2706 __copy_skb_header(nskb, p);
2707 nskb->mac_len = p->mac_len;
2708
2709 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002710 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002711
Herbert Xu86911732009-01-29 14:19:50 +00002712 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002713 skb_set_network_header(nskb, skb_network_offset(p));
2714 skb_set_transport_header(nskb, skb_transport_offset(p));
2715
Herbert Xu86911732009-01-29 14:19:50 +00002716 __skb_pull(p, skb_gro_offset(p));
2717 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2718 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002719
2720 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2721 skb_shinfo(nskb)->frag_list = p;
Herbert Xu9aaa1562009-05-26 18:50:33 +00002722 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
Herbert Xu622e0ca2010-05-20 23:07:56 -07002723 pinfo->gso_size = 0;
Herbert Xu71d93b32008-12-15 23:42:33 -08002724 skb_header_release(p);
2725 nskb->prev = p;
2726
2727 nskb->data_len += p->len;
2728 nskb->truesize += p->len;
2729 nskb->len += p->len;
2730
2731 *head = nskb;
2732 nskb->next = p->next;
2733 p->next = NULL;
2734
2735 p = nskb;
2736
2737merge:
Herbert Xu67147ba2009-05-26 18:50:22 +00002738 if (offset > headlen) {
Herbert Xu9aaa1562009-05-26 18:50:33 +00002739 skbinfo->frags[0].page_offset += offset - headlen;
2740 skbinfo->frags[0].size -= offset - headlen;
Herbert Xu67147ba2009-05-26 18:50:22 +00002741 offset = headlen;
Herbert Xu56035022009-02-05 21:26:52 -08002742 }
2743
Herbert Xu67147ba2009-05-26 18:50:22 +00002744 __skb_pull(skb, offset);
Herbert Xu56035022009-02-05 21:26:52 -08002745
Herbert Xu71d93b32008-12-15 23:42:33 -08002746 p->prev->next = skb;
2747 p->prev = skb;
2748 skb_header_release(skb);
2749
Herbert Xu5d38a072009-01-04 16:13:40 -08002750done:
2751 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00002752 p->data_len += len;
2753 p->truesize += len;
2754 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08002755
2756 NAPI_GRO_CB(skb)->same_flow = 1;
2757 return 0;
2758}
2759EXPORT_SYMBOL_GPL(skb_gro_receive);
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761void __init skb_init(void)
2762{
2763 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2764 sizeof(struct sk_buff),
2765 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002766 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002767 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002768 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2769 (2*sizeof(struct sk_buff)) +
2770 sizeof(atomic_t),
2771 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002772 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002773 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
David Howells716ea3a2007-04-02 20:19:53 -07002776/**
2777 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2778 * @skb: Socket buffer containing the buffers to be mapped
2779 * @sg: The scatter-gather list to map into
2780 * @offset: The offset into the buffer's contents to start mapping
2781 * @len: Length of buffer space to be mapped
2782 *
2783 * Fill the specified scatter-gather list with mappings/pointers into a
2784 * region of the buffer space attached to a socket buffer.
2785 */
David S. Miller51c739d2007-10-30 21:29:29 -07002786static int
2787__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002788{
David S. Miller1a028e52007-04-27 15:21:23 -07002789 int start = skb_headlen(skb);
2790 int i, copy = start - offset;
David S. Millerfbb398a2009-06-09 00:18:59 -07002791 struct sk_buff *frag_iter;
David Howells716ea3a2007-04-02 20:19:53 -07002792 int elt = 0;
2793
2794 if (copy > 0) {
2795 if (copy > len)
2796 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002797 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002798 elt++;
2799 if ((len -= copy) == 0)
2800 return elt;
2801 offset += copy;
2802 }
2803
2804 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002805 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002806
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002807 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002808
2809 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002810 if ((copy = end - offset) > 0) {
2811 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2812
2813 if (copy > len)
2814 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002815 sg_set_page(&sg[elt], frag->page, copy,
2816 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002817 elt++;
2818 if (!(len -= copy))
2819 return elt;
2820 offset += copy;
2821 }
David S. Miller1a028e52007-04-27 15:21:23 -07002822 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002823 }
2824
David S. Millerfbb398a2009-06-09 00:18:59 -07002825 skb_walk_frags(skb, frag_iter) {
2826 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002827
David S. Millerfbb398a2009-06-09 00:18:59 -07002828 WARN_ON(start > offset + len);
David Howells716ea3a2007-04-02 20:19:53 -07002829
David S. Millerfbb398a2009-06-09 00:18:59 -07002830 end = start + frag_iter->len;
2831 if ((copy = end - offset) > 0) {
2832 if (copy > len)
2833 copy = len;
2834 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
2835 copy);
2836 if ((len -= copy) == 0)
2837 return elt;
2838 offset += copy;
David Howells716ea3a2007-04-02 20:19:53 -07002839 }
David S. Millerfbb398a2009-06-09 00:18:59 -07002840 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002841 }
2842 BUG_ON(len);
2843 return elt;
2844}
2845
David S. Miller51c739d2007-10-30 21:29:29 -07002846int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2847{
2848 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2849
Jens Axboec46f2332007-10-31 12:06:37 +01002850 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002851
2852 return nsg;
2853}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002854EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07002855
David Howells716ea3a2007-04-02 20:19:53 -07002856/**
2857 * skb_cow_data - Check that a socket buffer's data buffers are writable
2858 * @skb: The socket buffer to check.
2859 * @tailbits: Amount of trailing space to be added
2860 * @trailer: Returned pointer to the skb where the @tailbits space begins
2861 *
2862 * Make sure that the data buffers attached to a socket buffer are
2863 * writable. If they are not, private copies are made of the data buffers
2864 * and the socket buffer is set to use these instead.
2865 *
2866 * If @tailbits is given, make sure that there is space to write @tailbits
2867 * bytes of data beyond current end of socket buffer. @trailer will be
2868 * set to point to the skb in which this space begins.
2869 *
2870 * The number of scatterlist elements required to completely map the
2871 * COW'd and extended socket buffer will be returned.
2872 */
2873int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2874{
2875 int copyflag;
2876 int elt;
2877 struct sk_buff *skb1, **skb_p;
2878
2879 /* If skb is cloned or its head is paged, reallocate
2880 * head pulling out all the pages (pages are considered not writable
2881 * at the moment even if they are anonymous).
2882 */
2883 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2884 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2885 return -ENOMEM;
2886
2887 /* Easy case. Most of packets will go this way. */
David S. Miller21dc3302010-08-23 00:13:46 -07002888 if (!skb_has_frag_list(skb)) {
David Howells716ea3a2007-04-02 20:19:53 -07002889 /* A little of trouble, not enough of space for trailer.
2890 * This should not happen, when stack is tuned to generate
2891 * good frames. OK, on miss we reallocate and reserve even more
2892 * space, 128 bytes is fair. */
2893
2894 if (skb_tailroom(skb) < tailbits &&
2895 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2896 return -ENOMEM;
2897
2898 /* Voila! */
2899 *trailer = skb;
2900 return 1;
2901 }
2902
2903 /* Misery. We are in troubles, going to mincer fragments... */
2904
2905 elt = 1;
2906 skb_p = &skb_shinfo(skb)->frag_list;
2907 copyflag = 0;
2908
2909 while ((skb1 = *skb_p) != NULL) {
2910 int ntail = 0;
2911
2912 /* The fragment is partially pulled by someone,
2913 * this can happen on input. Copy it and everything
2914 * after it. */
2915
2916 if (skb_shared(skb1))
2917 copyflag = 1;
2918
2919 /* If the skb is the last, worry about trailer. */
2920
2921 if (skb1->next == NULL && tailbits) {
2922 if (skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07002923 skb_has_frag_list(skb1) ||
David Howells716ea3a2007-04-02 20:19:53 -07002924 skb_tailroom(skb1) < tailbits)
2925 ntail = tailbits + 128;
2926 }
2927
2928 if (copyflag ||
2929 skb_cloned(skb1) ||
2930 ntail ||
2931 skb_shinfo(skb1)->nr_frags ||
David S. Miller21dc3302010-08-23 00:13:46 -07002932 skb_has_frag_list(skb1)) {
David Howells716ea3a2007-04-02 20:19:53 -07002933 struct sk_buff *skb2;
2934
2935 /* Fuck, we are miserable poor guys... */
2936 if (ntail == 0)
2937 skb2 = skb_copy(skb1, GFP_ATOMIC);
2938 else
2939 skb2 = skb_copy_expand(skb1,
2940 skb_headroom(skb1),
2941 ntail,
2942 GFP_ATOMIC);
2943 if (unlikely(skb2 == NULL))
2944 return -ENOMEM;
2945
2946 if (skb1->sk)
2947 skb_set_owner_w(skb2, skb1->sk);
2948
2949 /* Looking around. Are we still alive?
2950 * OK, link new skb, drop old one */
2951
2952 skb2->next = skb1->next;
2953 *skb_p = skb2;
2954 kfree_skb(skb1);
2955 skb1 = skb2;
2956 }
2957 elt++;
2958 *trailer = skb1;
2959 skb_p = &skb1->next;
2960 }
2961
2962 return elt;
2963}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002964EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07002965
Eric Dumazetb1faf562010-05-31 23:44:05 -07002966static void sock_rmem_free(struct sk_buff *skb)
2967{
2968 struct sock *sk = skb->sk;
2969
2970 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
2971}
2972
2973/*
2974 * Note: We dont mem charge error packets (no sk_forward_alloc changes)
2975 */
2976int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
2977{
2978 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
2979 (unsigned)sk->sk_rcvbuf)
2980 return -ENOMEM;
2981
2982 skb_orphan(skb);
2983 skb->sk = sk;
2984 skb->destructor = sock_rmem_free;
2985 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
2986
2987 skb_queue_tail(&sk->sk_error_queue, skb);
2988 if (!sock_flag(sk, SOCK_DEAD))
2989 sk->sk_data_ready(sk, skb->len);
2990 return 0;
2991}
2992EXPORT_SYMBOL(sock_queue_err_skb);
2993
Patrick Ohlyac45f602009-02-12 05:03:37 +00002994void skb_tstamp_tx(struct sk_buff *orig_skb,
2995 struct skb_shared_hwtstamps *hwtstamps)
2996{
2997 struct sock *sk = orig_skb->sk;
2998 struct sock_exterr_skb *serr;
2999 struct sk_buff *skb;
3000 int err;
3001
3002 if (!sk)
3003 return;
3004
3005 skb = skb_clone(orig_skb, GFP_ATOMIC);
3006 if (!skb)
3007 return;
3008
3009 if (hwtstamps) {
3010 *skb_hwtstamps(skb) =
3011 *hwtstamps;
3012 } else {
3013 /*
3014 * no hardware time stamps available,
Oliver Hartkopp2244d072010-08-17 08:59:14 +00003015 * so keep the shared tx_flags and only
Patrick Ohlyac45f602009-02-12 05:03:37 +00003016 * store software time stamp
3017 */
3018 skb->tstamp = ktime_get_real();
3019 }
3020
3021 serr = SKB_EXT_ERR(skb);
3022 memset(serr, 0, sizeof(*serr));
3023 serr->ee.ee_errno = ENOMSG;
3024 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
Eric Dumazet29030372010-05-29 00:20:48 -07003025
Patrick Ohlyac45f602009-02-12 05:03:37 +00003026 err = sock_queue_err_skb(sk, skb);
Eric Dumazet29030372010-05-29 00:20:48 -07003027
Patrick Ohlyac45f602009-02-12 05:03:37 +00003028 if (err)
3029 kfree_skb(skb);
3030}
3031EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3032
3033
Rusty Russellf35d9d82008-02-04 23:49:54 -05003034/**
3035 * skb_partial_csum_set - set up and verify partial csum values for packet
3036 * @skb: the skb to set
3037 * @start: the number of bytes after skb->data to start checksumming.
3038 * @off: the offset from start to place the checksum.
3039 *
3040 * For untrusted partially-checksummed packets, we need to make sure the values
3041 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3042 *
3043 * This function checks and sets those values and skb->ip_summed: if this
3044 * returns false you should drop the packet.
3045 */
3046bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3047{
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003048 if (unlikely(start > skb_headlen(skb)) ||
3049 unlikely((int)start + off > skb_headlen(skb) - 2)) {
Rusty Russellf35d9d82008-02-04 23:49:54 -05003050 if (net_ratelimit())
3051 printk(KERN_WARNING
3052 "bad partial csum: csum=%u/%u len=%u\n",
Herbert Xu5ff8dda2009-06-04 01:22:01 +00003053 start, off, skb_headlen(skb));
Rusty Russellf35d9d82008-02-04 23:49:54 -05003054 return false;
3055 }
3056 skb->ip_summed = CHECKSUM_PARTIAL;
3057 skb->csum_start = skb_headroom(skb) + start;
3058 skb->csum_offset = off;
3059 return true;
3060}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003061EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003062
Ben Hutchings4497b072008-06-19 16:22:28 -07003063void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3064{
3065 if (net_ratelimit())
3066 pr_warning("%s: received packets cannot be forwarded"
3067 " while LRO is enabled\n", skb->dev->name);
3068}
Ben Hutchings4497b072008-06-19 16:22:28 -07003069EXPORT_SYMBOL(__skb_warn_lro_forwarding);