blob: f0c4c6ad774bad3bb6c4bf6ed0ab947f6d0c5ebf [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. */
96static struct pipe_buf_operations sock_pipe_buf_ops = {
97 .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 */
120void skb_over_panic(struct sk_buff *skb, int sz, void *here)
121{
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}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800129EXPORT_SYMBOL(skb_over_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/**
132 * skb_under_panic - private function
133 * @skb: buffer
134 * @sz: size
135 * @here: address
136 *
137 * Out of line support code for skb_push(). Not user callable.
138 */
139
140void skb_under_panic(struct sk_buff *skb, int sz, void *here)
141{
Patrick McHardy26095452005-04-21 16:43:02 -0700142 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700143 "data:%p tail:%#lx end:%#lx dev:%s\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700144 here, skb->len, sz, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700145 (unsigned long)skb->tail, (unsigned long)skb->end,
Patrick McHardy26095452005-04-21 16:43:02 -0700146 skb->dev ? skb->dev->name : "<NULL>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 BUG();
148}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800149EXPORT_SYMBOL(skb_under_panic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/* Allocate a new skbuff. We do this ourselves so we can fill in a few
152 * 'private' fields and also do memory statistics to find all the
153 * [BEEP] leaks.
154 *
155 */
156
157/**
David S. Millerd179cd12005-08-17 14:57:30 -0700158 * __alloc_skb - allocate a network buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * @size: size to allocate
160 * @gfp_mask: allocation mask
Randy Dunlapc83c2482005-10-18 22:07:41 -0700161 * @fclone: allocate from fclone cache instead of head cache
162 * and allocate a cloned (child) skb
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800163 * @node: numa node to allocate memory on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * Allocate a new &sk_buff. The returned buffer has no headroom and a
166 * tail room of size bytes. The object has a reference count of one.
167 * The return is the buffer. On a failure the return is %NULL.
168 *
169 * Buffers may only be allocated from interrupts using a @gfp_mask of
170 * %GFP_ATOMIC.
171 */
Al Virodd0fc662005-10-07 07:46:04 +0100172struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800173 int fclone, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Christoph Lametere18b8902006-12-06 20:33:20 -0800175 struct kmem_cache *cache;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800176 struct skb_shared_info *shinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct sk_buff *skb;
178 u8 *data;
179
Herbert Xu8798b3f2006-01-23 16:32:45 -0800180 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Get the HEAD */
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800183 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!skb)
185 goto out;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 size = SKB_DATA_ALIGN(size);
Christoph Hellwigb30973f2006-12-06 20:32:36 -0800188 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
189 gfp_mask, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!data)
191 goto nodata;
192
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;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200205 kmemcheck_annotate_bitfield(skb, flags1);
206 kmemcheck_annotate_bitfield(skb, flags2);
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800207 /* make sure we initialize shinfo sequentially */
208 shinfo = skb_shinfo(skb);
209 atomic_set(&shinfo->dataref, 1);
210 shinfo->nr_frags = 0;
Herbert Xu79671682006-06-22 02:40:14 -0700211 shinfo->gso_size = 0;
212 shinfo->gso_segs = 0;
213 shinfo->gso_type = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800214 shinfo->ip6_frag_id = 0;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000215 shinfo->tx_flags.flags = 0;
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800216 shinfo->frag_list = NULL;
Patrick Ohlyac45f602009-02-12 05:03:37 +0000217 memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
Benjamin LaHaise4947d3e2006-01-03 14:06:50 -0800218
David S. Millerd179cd12005-08-17 14:57:30 -0700219 if (fclone) {
220 struct sk_buff *child = skb + 1;
221 atomic_t *fclone_ref = (atomic_t *) (child + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200223 kmemcheck_annotate_bitfield(child, flags1);
224 kmemcheck_annotate_bitfield(child, flags2);
David S. Millerd179cd12005-08-17 14:57:30 -0700225 skb->fclone = SKB_FCLONE_ORIG;
226 atomic_set(fclone_ref, 1);
227
228 child->fclone = SKB_FCLONE_UNAVAILABLE;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230out:
231 return skb;
232nodata:
Herbert Xu8798b3f2006-01-23 16:32:45 -0800233 kmem_cache_free(cache, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 skb = NULL;
235 goto out;
236}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800237EXPORT_SYMBOL(__alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/**
Christoph Hellwig8af27452006-07-31 22:35:23 -0700240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
244 *
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
249 *
250 * %NULL is returned if there is no free memory.
251 */
252struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
254{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Christoph Hellwig8af27452006-07-31 22:35:23 -0700256 struct sk_buff *skb;
257
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700259 if (likely(skb)) {
Christoph Hellwig8af27452006-07-31 22:35:23 -0700260 skb_reserve(skb, NET_SKB_PAD);
Christoph Hellwig7b2e4972006-08-07 16:09:04 -0700261 skb->dev = dev;
262 }
Christoph Hellwig8af27452006-07-31 22:35:23 -0700263 return skb;
264}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800265EXPORT_SYMBOL(__netdev_alloc_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Peter Zijlstra654bed12008-10-07 14:22:33 -0700267struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
268{
269 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
270 struct page *page;
271
272 page = alloc_pages_node(node, gfp_mask, 0);
273 return page;
274}
275EXPORT_SYMBOL(__netdev_alloc_page);
276
277void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
278 int size)
279{
280 skb_fill_page_desc(skb, i, page, off, size);
281 skb->len += size;
282 skb->data_len += size;
283 skb->truesize += size;
284}
285EXPORT_SYMBOL(skb_add_rx_frag);
286
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700287/**
288 * dev_alloc_skb - allocate an skbuff for receiving
289 * @length: length to allocate
290 *
291 * Allocate a new &sk_buff and assign it a usage count of one. The
292 * buffer has unspecified headroom built in. Users should allocate
293 * the headroom they think they need without accounting for the
294 * built in space. The built in space is used for optimisations.
295 *
296 * %NULL is returned if there is no free memory. Although this function
297 * allocates memory it can be called from an interrupt.
298 */
299struct sk_buff *dev_alloc_skb(unsigned int length)
300{
Denys Vlasenko1483b872008-03-28 15:57:39 -0700301 /*
302 * There is more code here than it seems:
David S. Millera0f55e02008-03-28 18:22:32 -0700303 * __dev_alloc_skb is an inline
Denys Vlasenko1483b872008-03-28 15:57:39 -0700304 */
Ilpo Järvinenf58518e2008-03-27 17:51:31 -0700305 return __dev_alloc_skb(length, GFP_ATOMIC);
306}
307EXPORT_SYMBOL(dev_alloc_skb);
308
Herbert Xu27b437c2006-07-13 19:26:39 -0700309static void skb_drop_list(struct sk_buff **listp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Herbert Xu27b437c2006-07-13 19:26:39 -0700311 struct sk_buff *list = *listp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Herbert Xu27b437c2006-07-13 19:26:39 -0700313 *listp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 do {
316 struct sk_buff *this = list;
317 list = list->next;
318 kfree_skb(this);
319 } while (list);
320}
321
Herbert Xu27b437c2006-07-13 19:26:39 -0700322static inline void skb_drop_fraglist(struct sk_buff *skb)
323{
324 skb_drop_list(&skb_shinfo(skb)->frag_list);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static void skb_clone_fraglist(struct sk_buff *skb)
328{
329 struct sk_buff *list;
330
331 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
332 skb_get(list);
333}
334
Adrian Bunk5bba1712006-06-29 13:02:35 -0700335static void skb_release_data(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 if (!skb->cloned ||
338 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
339 &skb_shinfo(skb)->dataref)) {
340 if (skb_shinfo(skb)->nr_frags) {
341 int i;
342 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
343 put_page(skb_shinfo(skb)->frags[i].page);
344 }
345
346 if (skb_shinfo(skb)->frag_list)
347 skb_drop_fraglist(skb);
348
349 kfree(skb->head);
350 }
351}
352
353/*
354 * Free an skbuff by memory without cleaning the state.
355 */
Herbert Xu2d4baff2007-11-26 23:11:19 +0800356static void kfree_skbmem(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David S. Millerd179cd12005-08-17 14:57:30 -0700358 struct sk_buff *other;
359 atomic_t *fclone_ref;
360
David S. Millerd179cd12005-08-17 14:57:30 -0700361 switch (skb->fclone) {
362 case SKB_FCLONE_UNAVAILABLE:
363 kmem_cache_free(skbuff_head_cache, skb);
364 break;
365
366 case SKB_FCLONE_ORIG:
367 fclone_ref = (atomic_t *) (skb + 2);
368 if (atomic_dec_and_test(fclone_ref))
369 kmem_cache_free(skbuff_fclone_cache, skb);
370 break;
371
372 case SKB_FCLONE_CLONE:
373 fclone_ref = (atomic_t *) (skb + 1);
374 other = skb - 1;
375
376 /* The clone portion is available for
377 * fast-cloning again.
378 */
379 skb->fclone = SKB_FCLONE_UNAVAILABLE;
380
381 if (atomic_dec_and_test(fclone_ref))
382 kmem_cache_free(skbuff_fclone_cache, other);
383 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700387static void skb_release_head_state(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dst_release(skb->dst);
390#ifdef CONFIG_XFRM
391 secpath_put(skb->sp);
392#endif
Stephen Hemminger9c2b3322005-04-19 22:39:42 -0700393 if (skb->destructor) {
394 WARN_ON(in_irq());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 skb->destructor(skb);
396 }
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700398 nf_conntrack_put(skb->nfct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800399 nf_conntrack_put_reasm(skb->nfct_reasm);
400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#ifdef CONFIG_BRIDGE_NETFILTER
402 nf_bridge_put(skb->nf_bridge);
403#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/* XXX: IS this still necessary? - JHS */
405#ifdef CONFIG_NET_SCHED
406 skb->tc_index = 0;
407#ifdef CONFIG_NET_CLS_ACT
408 skb->tc_verd = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#endif
410#endif
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700411}
412
413/* Free everything but the sk_buff shell. */
414static void skb_release_all(struct sk_buff *skb)
415{
416 skb_release_head_state(skb);
Herbert Xu2d4baff2007-11-26 23:11:19 +0800417 skb_release_data(skb);
418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Herbert Xu2d4baff2007-11-26 23:11:19 +0800420/**
421 * __kfree_skb - private function
422 * @skb: buffer
423 *
424 * Free an sk_buff. Release anything attached to the buffer.
425 * Clean the state. This is an internal helper function. Users should
426 * always call kfree_skb
427 */
428
429void __kfree_skb(struct sk_buff *skb)
430{
431 skb_release_all(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 kfree_skbmem(skb);
433}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800434EXPORT_SYMBOL(__kfree_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/**
Jörn Engel231d06a2006-03-20 21:28:35 -0800437 * kfree_skb - free an sk_buff
438 * @skb: buffer to free
439 *
440 * Drop a reference to the buffer and free it if the usage count has
441 * hit zero.
442 */
443void kfree_skb(struct sk_buff *skb)
444{
445 if (unlikely(!skb))
446 return;
447 if (likely(atomic_read(&skb->users) == 1))
448 smp_rmb();
449 else if (likely(!atomic_dec_and_test(&skb->users)))
450 return;
Neil Hormanead2ceb2009-03-11 09:49:55 +0000451 trace_kfree_skb(skb, __builtin_return_address(0));
Jörn Engel231d06a2006-03-20 21:28:35 -0800452 __kfree_skb(skb);
453}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800454EXPORT_SYMBOL(kfree_skb);
Jörn Engel231d06a2006-03-20 21:28:35 -0800455
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700456/**
Neil Hormanead2ceb2009-03-11 09:49:55 +0000457 * consume_skb - free an skbuff
458 * @skb: buffer to free
459 *
460 * Drop a ref to the buffer and free it if the usage count has hit zero
461 * Functions identically to kfree_skb, but kfree_skb assumes that the frame
462 * is being dropped after a failure and notes that
463 */
464void consume_skb(struct sk_buff *skb)
465{
466 if (unlikely(!skb))
467 return;
468 if (likely(atomic_read(&skb->users) == 1))
469 smp_rmb();
470 else if (likely(!atomic_dec_and_test(&skb->users)))
471 return;
472 __kfree_skb(skb);
473}
474EXPORT_SYMBOL(consume_skb);
475
476/**
Stephen Hemmingerd1a203e2008-11-01 21:01:09 -0700477 * skb_recycle_check - check if skb can be reused for receive
478 * @skb: buffer
479 * @skb_size: minimum receive buffer size
480 *
481 * Checks that the skb passed in is not shared or cloned, and
482 * that it is linear and its head portion at least as large as
483 * skb_size so that it can be recycled as a receive buffer.
484 * If these conditions are met, this function does any necessary
485 * reference count dropping and cleans up the skbuff as if it
486 * just came from __alloc_skb().
487 */
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700488int skb_recycle_check(struct sk_buff *skb, int skb_size)
489{
490 struct skb_shared_info *shinfo;
491
492 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
493 return 0;
494
495 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
496 if (skb_end_pointer(skb) - skb->head < skb_size)
497 return 0;
498
499 if (skb_shared(skb) || skb_cloned(skb))
500 return 0;
501
502 skb_release_head_state(skb);
503 shinfo = skb_shinfo(skb);
504 atomic_set(&shinfo->dataref, 1);
505 shinfo->nr_frags = 0;
506 shinfo->gso_size = 0;
507 shinfo->gso_segs = 0;
508 shinfo->gso_type = 0;
509 shinfo->ip6_frag_id = 0;
Lennert Buytenhekb8050072009-05-06 16:49:18 -0700510 shinfo->tx_flags.flags = 0;
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700511 shinfo->frag_list = NULL;
Lennert Buytenhekb8050072009-05-06 16:49:18 -0700512 memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700513
514 memset(skb, 0, offsetof(struct sk_buff, tail));
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700515 skb->data = skb->head + NET_SKB_PAD;
Lennert Buytenhek5cd33db2008-11-10 21:45:05 -0800516 skb_reset_tail_pointer(skb);
Lennert Buytenhek04a4bb52008-10-01 02:33:12 -0700517
518 return 1;
519}
520EXPORT_SYMBOL(skb_recycle_check);
521
Herbert Xudec18812007-10-14 00:37:30 -0700522static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
523{
524 new->tstamp = old->tstamp;
525 new->dev = old->dev;
526 new->transport_header = old->transport_header;
527 new->network_header = old->network_header;
528 new->mac_header = old->mac_header;
529 new->dst = dst_clone(old->dst);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -0700530#ifdef CONFIG_XFRM
Herbert Xudec18812007-10-14 00:37:30 -0700531 new->sp = secpath_get(old->sp);
532#endif
533 memcpy(new->cb, old->cb, sizeof(old->cb));
534 new->csum_start = old->csum_start;
535 new->csum_offset = old->csum_offset;
536 new->local_df = old->local_df;
537 new->pkt_type = old->pkt_type;
538 new->ip_summed = old->ip_summed;
539 skb_copy_queue_mapping(new, old);
540 new->priority = old->priority;
541#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
542 new->ipvs_property = old->ipvs_property;
543#endif
544 new->protocol = old->protocol;
545 new->mark = old->mark;
546 __nf_copy(new, old);
547#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
548 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
549 new->nf_trace = old->nf_trace;
550#endif
551#ifdef CONFIG_NET_SCHED
552 new->tc_index = old->tc_index;
553#ifdef CONFIG_NET_CLS_ACT
554 new->tc_verd = old->tc_verd;
555#endif
556#endif
Patrick McHardy6aa895b02008-07-14 22:49:06 -0700557 new->vlan_tci = old->vlan_tci;
558
Herbert Xudec18812007-10-14 00:37:30 -0700559 skb_copy_secmark(new, old);
560}
561
Herbert Xue0053ec2007-10-14 00:37:52 -0700562static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#define C(x) n->x = skb->x
565
566 n->next = n->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 n->sk = NULL;
Herbert Xudec18812007-10-14 00:37:30 -0700568 __copy_skb_header(n, skb);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 C(len);
571 C(data_len);
Alexey Dobriyan3e6b3b22007-03-16 15:00:46 -0700572 C(mac_len);
Patrick McHardy334a8132007-06-25 04:35:20 -0700573 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
Paul Moore02f1c892008-01-07 21:56:41 -0800574 n->cloned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 n->nohdr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 n->destructor = NULL;
Paul Moore02f1c892008-01-07 21:56:41 -0800577 C(iif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 C(tail);
579 C(end);
Paul Moore02f1c892008-01-07 21:56:41 -0800580 C(head);
581 C(data);
582 C(truesize);
Johannes Bergd0f09802008-07-29 11:32:07 +0200583#if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
584 C(do_not_encrypt);
Sujith8b30b1f2008-10-24 09:55:27 +0530585 C(requeue);
Johannes Bergd0f09802008-07-29 11:32:07 +0200586#endif
Paul Moore02f1c892008-01-07 21:56:41 -0800587 atomic_set(&n->users, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 atomic_inc(&(skb_shinfo(skb)->dataref));
590 skb->cloned = 1;
591
592 return n;
Herbert Xue0053ec2007-10-14 00:37:52 -0700593#undef C
594}
595
596/**
597 * skb_morph - morph one skb into another
598 * @dst: the skb to receive the contents
599 * @src: the skb to supply the contents
600 *
601 * This is identical to skb_clone except that the target skb is
602 * supplied by the user.
603 *
604 * The target skb is returned upon exit.
605 */
606struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
607{
Herbert Xu2d4baff2007-11-26 23:11:19 +0800608 skb_release_all(dst);
Herbert Xue0053ec2007-10-14 00:37:52 -0700609 return __skb_clone(dst, src);
610}
611EXPORT_SYMBOL_GPL(skb_morph);
612
613/**
614 * skb_clone - duplicate an sk_buff
615 * @skb: buffer to clone
616 * @gfp_mask: allocation priority
617 *
618 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
619 * copies share the same packet data but not structure. The new
620 * buffer has a reference count of 1. If the allocation fails the
621 * function returns %NULL otherwise the new buffer is returned.
622 *
623 * If this function is called from an interrupt gfp_mask() must be
624 * %GFP_ATOMIC.
625 */
626
627struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
628{
629 struct sk_buff *n;
630
631 n = skb + 1;
632 if (skb->fclone == SKB_FCLONE_ORIG &&
633 n->fclone == SKB_FCLONE_UNAVAILABLE) {
634 atomic_t *fclone_ref = (atomic_t *) (n + 1);
635 n->fclone = SKB_FCLONE_CLONE;
636 atomic_inc(fclone_ref);
637 } else {
638 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
639 if (!n)
640 return NULL;
Vegard Nossumfe55f6d2008-08-30 12:16:35 +0200641
642 kmemcheck_annotate_bitfield(n, flags1);
643 kmemcheck_annotate_bitfield(n, flags2);
Herbert Xue0053ec2007-10-14 00:37:52 -0700644 n->fclone = SKB_FCLONE_UNAVAILABLE;
645 }
646
647 return __skb_clone(n, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800649EXPORT_SYMBOL(skb_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
652{
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700653#ifndef NET_SKBUFF_DATA_USES_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Shift between the two data areas in bytes
656 */
657 unsigned long offset = new->data - old->data;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700658#endif
Herbert Xudec18812007-10-14 00:37:30 -0700659
660 __copy_skb_header(new, old);
661
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700662#ifndef NET_SKBUFF_DATA_USES_OFFSET
663 /* {transport,network,mac}_header are relative to skb->head */
664 new->transport_header += offset;
665 new->network_header += offset;
666 new->mac_header += offset;
667#endif
Herbert Xu79671682006-06-22 02:40:14 -0700668 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
669 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
670 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/**
674 * skb_copy - create private copy of an sk_buff
675 * @skb: buffer to copy
676 * @gfp_mask: allocation priority
677 *
678 * Make a copy of both an &sk_buff and its data. This is used when the
679 * caller wishes to modify the data and needs a private copy of the
680 * data to alter. Returns %NULL on failure or the pointer to the buffer
681 * on success. The returned buffer has a reference count of 1.
682 *
683 * As by-product this function converts non-linear &sk_buff to linear
684 * one, so that &sk_buff becomes completely private and caller is allowed
685 * to modify all the data of returned buffer. This means that this
686 * function is not recommended for use in circumstances when only
687 * header is going to be modified. Use pskb_copy() instead.
688 */
689
Al Virodd0fc662005-10-07 07:46:04 +0100690struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int headerlen = skb->data - skb->head;
693 /*
694 * Allocate the copy buffer
695 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700696 struct sk_buff *n;
697#ifdef NET_SKBUFF_DATA_USES_OFFSET
698 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
699#else
700 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!n)
703 return NULL;
704
705 /* Set the data pointer */
706 skb_reserve(n, headerlen);
707 /* Set the tail pointer and length */
708 skb_put(n, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
711 BUG();
712
713 copy_skb_header(n, skb);
714 return n;
715}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800716EXPORT_SYMBOL(skb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718/**
719 * pskb_copy - create copy of an sk_buff with private head.
720 * @skb: buffer to copy
721 * @gfp_mask: allocation priority
722 *
723 * Make a copy of both an &sk_buff and part of its data, located
724 * in header. Fragmented data remain shared. This is used when
725 * the caller wishes to modify only header of &sk_buff and needs
726 * private copy of the header to alter. Returns %NULL on failure
727 * or the pointer to the buffer on success.
728 * The returned buffer has a reference count of 1.
729 */
730
Al Virodd0fc662005-10-07 07:46:04 +0100731struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 /*
734 * Allocate the copy buffer
735 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700736 struct sk_buff *n;
737#ifdef NET_SKBUFF_DATA_USES_OFFSET
738 n = alloc_skb(skb->end, gfp_mask);
739#else
740 n = alloc_skb(skb->end - skb->head, gfp_mask);
741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!n)
743 goto out;
744
745 /* Set the data pointer */
746 skb_reserve(n, skb->data - skb->head);
747 /* Set the tail pointer and length */
748 skb_put(n, skb_headlen(skb));
749 /* Copy the bytes */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300750 skb_copy_from_linear_data(skb, n->data, n->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Herbert Xu25f484a2006-11-07 14:57:15 -0800752 n->truesize += skb->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 n->data_len = skb->data_len;
754 n->len = skb->len;
755
756 if (skb_shinfo(skb)->nr_frags) {
757 int i;
758
759 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
760 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
761 get_page(skb_shinfo(n)->frags[i].page);
762 }
763 skb_shinfo(n)->nr_frags = i;
764 }
765
766 if (skb_shinfo(skb)->frag_list) {
767 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
768 skb_clone_fraglist(n);
769 }
770
771 copy_skb_header(n, skb);
772out:
773 return n;
774}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800775EXPORT_SYMBOL(pskb_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777/**
778 * pskb_expand_head - reallocate header of &sk_buff
779 * @skb: buffer to reallocate
780 * @nhead: room to add at head
781 * @ntail: room to add at tail
782 * @gfp_mask: allocation priority
783 *
784 * Expands (or creates identical copy, if &nhead and &ntail are zero)
785 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
786 * reference count of 1. Returns zero in the case of success or error,
787 * if expansion failed. In the last case, &sk_buff is not changed.
788 *
789 * All the pointers pointing into skb header may change and must be
790 * reloaded after call to this function.
791 */
792
Victor Fusco86a76ca2005-07-08 14:57:47 -0700793int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
Al Virodd0fc662005-10-07 07:46:04 +0100794 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 int i;
797 u8 *data;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700798#ifdef NET_SKBUFF_DATA_USES_OFFSET
799 int size = nhead + skb->end + ntail;
800#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 int size = nhead + (skb->end - skb->head) + ntail;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 long off;
804
Herbert Xu4edd87a2008-10-01 07:09:38 -0700805 BUG_ON(nhead < 0);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (skb_shared(skb))
808 BUG();
809
810 size = SKB_DATA_ALIGN(size);
811
812 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
813 if (!data)
814 goto nodata;
815
816 /* Copy only real data... and, alas, header. This should be
817 * optimized for the cases when header is void. */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700818#ifdef NET_SKBUFF_DATA_USES_OFFSET
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700819 memcpy(data + nhead, skb->head, skb->tail);
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700820#else
Mikael Petterssonb6ccc672007-05-19 13:55:25 -0700821 memcpy(data + nhead, skb->head, skb->tail - skb->head);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700822#endif
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700823 memcpy(data + size, skb_end_pointer(skb),
824 sizeof(struct skb_shared_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
827 get_page(skb_shinfo(skb)->frags[i].page);
828
829 if (skb_shinfo(skb)->frag_list)
830 skb_clone_fraglist(skb);
831
832 skb_release_data(skb);
833
834 off = (data + nhead) - skb->head;
835
836 skb->head = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 skb->data += off;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700838#ifdef NET_SKBUFF_DATA_USES_OFFSET
839 skb->end = size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700840 off = nhead;
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700841#else
842 skb->end = skb->head + size;
Patrick McHardy56eb8882007-04-09 11:45:04 -0700843#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700844 /* {transport,network,mac}_header and tail are relative to skb->head */
845 skb->tail += off;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700846 skb->transport_header += off;
847 skb->network_header += off;
848 skb->mac_header += off;
Herbert Xu172a8632007-10-15 01:46:08 -0700849 skb->csum_start += nhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 skb->cloned = 0;
Patrick McHardy334a8132007-06-25 04:35:20 -0700851 skb->hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 skb->nohdr = 0;
853 atomic_set(&skb_shinfo(skb)->dataref, 1);
854 return 0;
855
856nodata:
857 return -ENOMEM;
858}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800859EXPORT_SYMBOL(pskb_expand_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861/* Make private copy of skb with writable head and some headroom */
862
863struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
864{
865 struct sk_buff *skb2;
866 int delta = headroom - skb_headroom(skb);
867
868 if (delta <= 0)
869 skb2 = pskb_copy(skb, GFP_ATOMIC);
870 else {
871 skb2 = skb_clone(skb, GFP_ATOMIC);
872 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
873 GFP_ATOMIC)) {
874 kfree_skb(skb2);
875 skb2 = NULL;
876 }
877 }
878 return skb2;
879}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800880EXPORT_SYMBOL(skb_realloc_headroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/**
883 * skb_copy_expand - copy and expand sk_buff
884 * @skb: buffer to copy
885 * @newheadroom: new free bytes at head
886 * @newtailroom: new free bytes at tail
887 * @gfp_mask: allocation priority
888 *
889 * Make a copy of both an &sk_buff and its data and while doing so
890 * allocate additional space.
891 *
892 * This is used when the caller wishes to modify the data and needs a
893 * private copy of the data to alter as well as more space for new fields.
894 * Returns %NULL on failure or the pointer to the buffer
895 * on success. The returned buffer has a reference count of 1.
896 *
897 * You must pass %GFP_ATOMIC as the allocation priority if this function
898 * is called from an interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
900struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
Victor Fusco86a76ca2005-07-08 14:57:47 -0700901 int newheadroom, int newtailroom,
Al Virodd0fc662005-10-07 07:46:04 +0100902 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 /*
905 * Allocate the copy buffer
906 */
907 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
908 gfp_mask);
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700909 int oldheadroom = skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int head_copy_len, head_copy_off;
Herbert Xu52886052007-09-16 16:32:11 -0700911 int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (!n)
914 return NULL;
915
916 skb_reserve(n, newheadroom);
917
918 /* Set the tail pointer and length */
919 skb_put(n, skb->len);
920
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700921 head_copy_len = oldheadroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 head_copy_off = 0;
923 if (newheadroom <= head_copy_len)
924 head_copy_len = newheadroom;
925 else
926 head_copy_off = newheadroom - head_copy_len;
927
928 /* Copy the linear header and data. */
929 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
930 skb->len + head_copy_len))
931 BUG();
932
933 copy_skb_header(n, skb);
934
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700935 off = newheadroom - oldheadroom;
Herbert Xu52886052007-09-16 16:32:11 -0700936 n->csum_start += off;
937#ifdef NET_SKBUFF_DATA_USES_OFFSET
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700938 n->transport_header += off;
939 n->network_header += off;
940 n->mac_header += off;
Herbert Xu52886052007-09-16 16:32:11 -0700941#endif
Patrick McHardyefd1e8d2007-04-10 18:30:09 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return n;
944}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800945EXPORT_SYMBOL(skb_copy_expand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947/**
948 * skb_pad - zero pad the tail of an skb
949 * @skb: buffer to pad
950 * @pad: space to pad
951 *
952 * Ensure that a buffer is followed by a padding area that is zero
953 * filled. Used by network drivers which may DMA or transfer data
954 * beyond the buffer end onto the wire.
955 *
Herbert Xu5b057c62006-06-23 02:06:41 -0700956 * May return error in out of memory cases. The skb is freed on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900958
Herbert Xu5b057c62006-06-23 02:06:41 -0700959int skb_pad(struct sk_buff *skb, int pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Herbert Xu5b057c62006-06-23 02:06:41 -0700961 int err;
962 int ntail;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /* If the skbuff is non linear tailroom is always zero.. */
Herbert Xu5b057c62006-06-23 02:06:41 -0700965 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 memset(skb->data+skb->len, 0, pad);
Herbert Xu5b057c62006-06-23 02:06:41 -0700967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Herbert Xu5b057c62006-06-23 02:06:41 -0700969
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700970 ntail = skb->data_len + pad - (skb->end - skb->tail);
Herbert Xu5b057c62006-06-23 02:06:41 -0700971 if (likely(skb_cloned(skb) || ntail > 0)) {
972 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
973 if (unlikely(err))
974 goto free_skb;
975 }
976
977 /* FIXME: The use of this function with non-linear skb's really needs
978 * to be audited.
979 */
980 err = skb_linearize(skb);
981 if (unlikely(err))
982 goto free_skb;
983
984 memset(skb->data + skb->len, 0, pad);
985 return 0;
986
987free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 kfree_skb(skb);
Herbert Xu5b057c62006-06-23 02:06:41 -0700989 return err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900990}
David S. Millerb4ac530fc2009-02-10 02:09:24 -0800991EXPORT_SYMBOL(skb_pad);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900992
Ilpo Järvinen0dde3e12008-03-27 17:43:41 -0700993/**
994 * skb_put - add data to a buffer
995 * @skb: buffer to use
996 * @len: amount of data to add
997 *
998 * This function extends the used data area of the buffer. If this would
999 * exceed the total buffer size the kernel will panic. A pointer to the
1000 * first byte of the extra data is returned.
1001 */
1002unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1003{
1004 unsigned char *tmp = skb_tail_pointer(skb);
1005 SKB_LINEAR_ASSERT(skb);
1006 skb->tail += len;
1007 skb->len += len;
1008 if (unlikely(skb->tail > skb->end))
1009 skb_over_panic(skb, len, __builtin_return_address(0));
1010 return tmp;
1011}
1012EXPORT_SYMBOL(skb_put);
1013
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001014/**
Ilpo Järvinenc2aa2702008-03-27 17:52:40 -07001015 * skb_push - add data to the start of a buffer
1016 * @skb: buffer to use
1017 * @len: amount of data to add
1018 *
1019 * This function extends the used data area of the buffer at the buffer
1020 * start. If this would exceed the total buffer headroom the kernel will
1021 * panic. A pointer to the first byte of the extra data is returned.
1022 */
1023unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1024{
1025 skb->data -= len;
1026 skb->len += len;
1027 if (unlikely(skb->data<skb->head))
1028 skb_under_panic(skb, len, __builtin_return_address(0));
1029 return skb->data;
1030}
1031EXPORT_SYMBOL(skb_push);
1032
1033/**
Ilpo Järvinen6be8ac22008-03-27 17:47:24 -07001034 * skb_pull - remove data from the start of a buffer
1035 * @skb: buffer to use
1036 * @len: amount of data to remove
1037 *
1038 * This function removes data from the start of a buffer, returning
1039 * the memory to the headroom. A pointer to the next data in the buffer
1040 * is returned. Once the data has been pulled future pushes will overwrite
1041 * the old data.
1042 */
1043unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1044{
1045 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1046}
1047EXPORT_SYMBOL(skb_pull);
1048
Ilpo Järvinen419ae742008-03-27 17:54:01 -07001049/**
1050 * skb_trim - remove end from a buffer
1051 * @skb: buffer to alter
1052 * @len: new length
1053 *
1054 * Cut the length of a buffer down by removing data from the tail. If
1055 * the buffer is already under the length specified it is not modified.
1056 * The skb must be linear.
1057 */
1058void skb_trim(struct sk_buff *skb, unsigned int len)
1059{
1060 if (skb->len > len)
1061 __skb_trim(skb, len);
1062}
1063EXPORT_SYMBOL(skb_trim);
1064
Herbert Xu3cc0e872006-06-09 16:13:38 -07001065/* Trims skb to length len. It can change skb pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
1067
Herbert Xu3cc0e872006-06-09 16:13:38 -07001068int ___pskb_trim(struct sk_buff *skb, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Herbert Xu27b437c2006-07-13 19:26:39 -07001070 struct sk_buff **fragp;
1071 struct sk_buff *frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int offset = skb_headlen(skb);
1073 int nfrags = skb_shinfo(skb)->nr_frags;
1074 int i;
Herbert Xu27b437c2006-07-13 19:26:39 -07001075 int err;
1076
1077 if (skb_cloned(skb) &&
1078 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001081 i = 0;
1082 if (offset >= len)
1083 goto drop_pages;
1084
1085 for (; i < nfrags; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int end = offset + skb_shinfo(skb)->frags[i].size;
Herbert Xu27b437c2006-07-13 19:26:39 -07001087
1088 if (end < len) {
1089 offset = end;
1090 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Herbert Xu27b437c2006-07-13 19:26:39 -07001092
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001093 skb_shinfo(skb)->frags[i++].size = len - offset;
Herbert Xu27b437c2006-07-13 19:26:39 -07001094
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001095drop_pages:
Herbert Xu27b437c2006-07-13 19:26:39 -07001096 skb_shinfo(skb)->nr_frags = i;
1097
1098 for (; i < nfrags; i++)
1099 put_page(skb_shinfo(skb)->frags[i].page);
1100
1101 if (skb_shinfo(skb)->frag_list)
1102 skb_drop_fraglist(skb);
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001103 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105
Herbert Xu27b437c2006-07-13 19:26:39 -07001106 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1107 fragp = &frag->next) {
1108 int end = offset + frag->len;
1109
1110 if (skb_shared(frag)) {
1111 struct sk_buff *nfrag;
1112
1113 nfrag = skb_clone(frag, GFP_ATOMIC);
1114 if (unlikely(!nfrag))
1115 return -ENOMEM;
1116
1117 nfrag->next = frag->next;
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001118 kfree_skb(frag);
Herbert Xu27b437c2006-07-13 19:26:39 -07001119 frag = nfrag;
1120 *fragp = frag;
1121 }
1122
1123 if (end < len) {
1124 offset = end;
1125 continue;
1126 }
1127
1128 if (end > len &&
1129 unlikely((err = pskb_trim(frag, len - offset))))
1130 return err;
1131
1132 if (frag->next)
1133 skb_drop_list(&frag->next);
1134 break;
1135 }
1136
Herbert Xuf4d26fb2006-07-30 20:20:28 -07001137done:
Herbert Xu27b437c2006-07-13 19:26:39 -07001138 if (len > skb_headlen(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 skb->data_len -= skb->len - len;
1140 skb->len = len;
1141 } else {
Herbert Xu27b437c2006-07-13 19:26:39 -07001142 skb->len = len;
1143 skb->data_len = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001144 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 return 0;
1148}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001149EXPORT_SYMBOL(___pskb_trim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151/**
1152 * __pskb_pull_tail - advance tail of skb header
1153 * @skb: buffer to reallocate
1154 * @delta: number of bytes to advance tail
1155 *
1156 * The function makes a sense only on a fragmented &sk_buff,
1157 * it expands header moving its tail forward and copying necessary
1158 * data from fragmented part.
1159 *
1160 * &sk_buff MUST have reference count of 1.
1161 *
1162 * Returns %NULL (and &sk_buff does not change) if pull failed
1163 * or value of new tail of skb in the case of success.
1164 *
1165 * All the pointers pointing into skb header may change and must be
1166 * reloaded after call to this function.
1167 */
1168
1169/* Moves tail of skb head forward, copying data from fragmented part,
1170 * when it is necessary.
1171 * 1. It may fail due to malloc failure.
1172 * 2. It may change skb pointers.
1173 *
1174 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1175 */
1176unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1177{
1178 /* If skb has not enough free space at tail, get new one
1179 * plus 128 bytes for future expansions. If we have enough
1180 * room at tail, reallocate without expansion only if skb is cloned.
1181 */
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001182 int i, k, eat = (skb->tail + delta) - skb->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (eat > 0 || skb_cloned(skb)) {
1185 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1186 GFP_ATOMIC))
1187 return NULL;
1188 }
1189
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001190 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 BUG();
1192
1193 /* Optimization: no fragments, no reasons to preestimate
1194 * size of pulled pages. Superb.
1195 */
1196 if (!skb_shinfo(skb)->frag_list)
1197 goto pull_pages;
1198
1199 /* Estimate size of pulled pages. */
1200 eat = delta;
1201 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1202 if (skb_shinfo(skb)->frags[i].size >= eat)
1203 goto pull_pages;
1204 eat -= skb_shinfo(skb)->frags[i].size;
1205 }
1206
1207 /* If we need update frag list, we are in troubles.
1208 * Certainly, it possible to add an offset to skb data,
1209 * but taking into account that pulling is expected to
1210 * be very rare operation, it is worth to fight against
1211 * further bloating skb head and crucify ourselves here instead.
1212 * Pure masohism, indeed. 8)8)
1213 */
1214 if (eat) {
1215 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1216 struct sk_buff *clone = NULL;
1217 struct sk_buff *insp = NULL;
1218
1219 do {
Kris Katterjohn09a62662006-01-08 22:24:28 -08001220 BUG_ON(!list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 if (list->len <= eat) {
1223 /* Eaten as whole. */
1224 eat -= list->len;
1225 list = list->next;
1226 insp = list;
1227 } else {
1228 /* Eaten partially. */
1229
1230 if (skb_shared(list)) {
1231 /* Sucks! We need to fork list. :-( */
1232 clone = skb_clone(list, GFP_ATOMIC);
1233 if (!clone)
1234 return NULL;
1235 insp = list->next;
1236 list = clone;
1237 } else {
1238 /* This may be pulled without
1239 * problems. */
1240 insp = list;
1241 }
1242 if (!pskb_pull(list, eat)) {
Wei Yongjunf3fbbe02009-02-25 00:37:32 +00001243 kfree_skb(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return NULL;
1245 }
1246 break;
1247 }
1248 } while (eat);
1249
1250 /* Free pulled out fragments. */
1251 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1252 skb_shinfo(skb)->frag_list = list->next;
1253 kfree_skb(list);
1254 }
1255 /* And insert new clone at head. */
1256 if (clone) {
1257 clone->next = list;
1258 skb_shinfo(skb)->frag_list = clone;
1259 }
1260 }
1261 /* Success! Now we may commit changes to skb data. */
1262
1263pull_pages:
1264 eat = delta;
1265 k = 0;
1266 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1267 if (skb_shinfo(skb)->frags[i].size <= eat) {
1268 put_page(skb_shinfo(skb)->frags[i].page);
1269 eat -= skb_shinfo(skb)->frags[i].size;
1270 } else {
1271 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1272 if (eat) {
1273 skb_shinfo(skb)->frags[k].page_offset += eat;
1274 skb_shinfo(skb)->frags[k].size -= eat;
1275 eat = 0;
1276 }
1277 k++;
1278 }
1279 }
1280 skb_shinfo(skb)->nr_frags = k;
1281
1282 skb->tail += delta;
1283 skb->data_len -= delta;
1284
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001285 return skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001287EXPORT_SYMBOL(__pskb_pull_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/* Copy some data bits from skb to kernel buffer. */
1290
1291int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1292{
1293 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001294 int start = skb_headlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (offset > (int)skb->len - len)
1297 goto fault;
1298
1299 /* Copy header. */
David S. Miller1a028e52007-04-27 15:21:23 -07001300 if ((copy = start - offset) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (copy > len)
1302 copy = len;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001303 skb_copy_from_linear_data_offset(skb, offset, to, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if ((len -= copy) == 0)
1305 return 0;
1306 offset += copy;
1307 to += copy;
1308 }
1309
1310 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001311 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001313 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001314
1315 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if ((copy = end - offset) > 0) {
1317 u8 *vaddr;
1318
1319 if (copy > len)
1320 copy = len;
1321
1322 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1323 memcpy(to,
David S. Miller1a028e52007-04-27 15:21:23 -07001324 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1325 offset - start, copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 kunmap_skb_frag(vaddr);
1327
1328 if ((len -= copy) == 0)
1329 return 0;
1330 offset += copy;
1331 to += copy;
1332 }
David S. Miller1a028e52007-04-27 15:21:23 -07001333 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335
1336 if (skb_shinfo(skb)->frag_list) {
1337 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1338
1339 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001340 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001342 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001343
1344 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if ((copy = end - offset) > 0) {
1346 if (copy > len)
1347 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001348 if (skb_copy_bits(list, offset - start,
1349 to, copy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 goto fault;
1351 if ((len -= copy) == 0)
1352 return 0;
1353 offset += copy;
1354 to += copy;
1355 }
David S. Miller1a028e52007-04-27 15:21:23 -07001356 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
1359 if (!len)
1360 return 0;
1361
1362fault:
1363 return -EFAULT;
1364}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001365EXPORT_SYMBOL(skb_copy_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Jens Axboe9c55e012007-11-06 23:30:13 -08001367/*
1368 * Callback from splice_to_pipe(), if we need to release some pages
1369 * at the end of the spd in case we error'ed out in filling the pipe.
1370 */
1371static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1372{
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001373 put_page(spd->pages[i]);
1374}
Jens Axboe9c55e012007-11-06 23:30:13 -08001375
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001376static inline struct page *linear_to_page(struct page *page, unsigned int *len,
1377 unsigned int *offset,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001378 struct sk_buff *skb, struct sock *sk)
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001379{
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001380 struct page *p = sk->sk_sndmsg_page;
1381 unsigned int off;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001382
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001383 if (!p) {
1384new_page:
1385 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
1386 if (!p)
1387 return NULL;
1388
1389 off = sk->sk_sndmsg_off = 0;
1390 /* hold one ref to this page until it's full */
1391 } else {
1392 unsigned int mlen;
1393
1394 off = sk->sk_sndmsg_off;
1395 mlen = PAGE_SIZE - off;
1396 if (mlen < 64 && mlen < *len) {
1397 put_page(p);
1398 goto new_page;
1399 }
1400
1401 *len = min_t(unsigned int, *len, mlen);
1402 }
1403
1404 memcpy(page_address(p) + off, page_address(page) + *offset, *len);
1405 sk->sk_sndmsg_off += *len;
1406 *offset = off;
1407 get_page(p);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001408
1409 return p;
Jens Axboe9c55e012007-11-06 23:30:13 -08001410}
1411
1412/*
1413 * Fill page/offset/length into spd, if it can hold more pages.
1414 */
1415static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001416 unsigned int *len, unsigned int offset,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001417 struct sk_buff *skb, int linear,
1418 struct sock *sk)
Jens Axboe9c55e012007-11-06 23:30:13 -08001419{
1420 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1421 return 1;
1422
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001423 if (linear) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001424 page = linear_to_page(page, len, &offset, skb, sk);
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001425 if (!page)
1426 return 1;
1427 } else
1428 get_page(page);
1429
Jens Axboe9c55e012007-11-06 23:30:13 -08001430 spd->pages[spd->nr_pages] = page;
Jarek Poplawski4fb66992009-02-01 00:41:42 -08001431 spd->partial[spd->nr_pages].len = *len;
Jens Axboe9c55e012007-11-06 23:30:13 -08001432 spd->partial[spd->nr_pages].offset = offset;
Jens Axboe9c55e012007-11-06 23:30:13 -08001433 spd->nr_pages++;
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001434
Jens Axboe9c55e012007-11-06 23:30:13 -08001435 return 0;
1436}
1437
Octavian Purdila2870c432008-07-15 00:49:11 -07001438static inline void __segment_seek(struct page **page, unsigned int *poff,
1439 unsigned int *plen, unsigned int off)
Jens Axboe9c55e012007-11-06 23:30:13 -08001440{
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001441 unsigned long n;
1442
Octavian Purdila2870c432008-07-15 00:49:11 -07001443 *poff += off;
Jarek Poplawskice3dd392009-02-12 16:51:43 -08001444 n = *poff / PAGE_SIZE;
1445 if (n)
1446 *page = nth_page(*page, n);
1447
Octavian Purdila2870c432008-07-15 00:49:11 -07001448 *poff = *poff % PAGE_SIZE;
1449 *plen -= off;
1450}
Jens Axboe9c55e012007-11-06 23:30:13 -08001451
Octavian Purdila2870c432008-07-15 00:49:11 -07001452static inline int __splice_segment(struct page *page, unsigned int poff,
1453 unsigned int plen, unsigned int *off,
1454 unsigned int *len, struct sk_buff *skb,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001455 struct splice_pipe_desc *spd, int linear,
1456 struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001457{
1458 if (!*len)
1459 return 1;
1460
1461 /* skip this segment if already processed */
1462 if (*off >= plen) {
1463 *off -= plen;
1464 return 0;
Octavian Purdiladb43a282008-06-27 17:27:21 -07001465 }
Jens Axboe9c55e012007-11-06 23:30:13 -08001466
Octavian Purdila2870c432008-07-15 00:49:11 -07001467 /* ignore any bits we already processed */
1468 if (*off) {
1469 __segment_seek(&page, &poff, &plen, *off);
1470 *off = 0;
1471 }
1472
1473 do {
1474 unsigned int flen = min(*len, plen);
1475
1476 /* the linear region may spread across several pages */
1477 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1478
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001479 if (spd_fill_page(spd, page, &flen, poff, skb, linear, sk))
Octavian Purdila2870c432008-07-15 00:49:11 -07001480 return 1;
1481
1482 __segment_seek(&page, &poff, &plen, flen);
1483 *len -= flen;
1484
1485 } while (*len && plen);
1486
1487 return 0;
1488}
1489
1490/*
1491 * Map linear and fragment data from the skb to spd. It reports failure if the
1492 * pipe is full or if we already spliced the requested length.
1493 */
Harvey Harrison7b1c65f2008-07-16 20:12:30 -07001494static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001495 unsigned int *len, struct splice_pipe_desc *spd,
1496 struct sock *sk)
Octavian Purdila2870c432008-07-15 00:49:11 -07001497{
1498 int seg;
1499
Jens Axboe9c55e012007-11-06 23:30:13 -08001500 /*
Octavian Purdila2870c432008-07-15 00:49:11 -07001501 * map the linear part
Jens Axboe9c55e012007-11-06 23:30:13 -08001502 */
Octavian Purdila2870c432008-07-15 00:49:11 -07001503 if (__splice_segment(virt_to_page(skb->data),
1504 (unsigned long) skb->data & (PAGE_SIZE - 1),
1505 skb_headlen(skb),
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001506 offset, len, skb, spd, 1, sk))
Octavian Purdila2870c432008-07-15 00:49:11 -07001507 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001508
1509 /*
1510 * then map the fragments
1511 */
Jens Axboe9c55e012007-11-06 23:30:13 -08001512 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1513 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1514
Octavian Purdila2870c432008-07-15 00:49:11 -07001515 if (__splice_segment(f->page, f->page_offset, f->size,
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001516 offset, len, skb, spd, 0, sk))
Octavian Purdila2870c432008-07-15 00:49:11 -07001517 return 1;
Jens Axboe9c55e012007-11-06 23:30:13 -08001518 }
1519
Octavian Purdila2870c432008-07-15 00:49:11 -07001520 return 0;
Jens Axboe9c55e012007-11-06 23:30:13 -08001521}
1522
1523/*
1524 * Map data from the skb to a pipe. Should handle both the linear part,
1525 * the fragments, and the frag list. It does NOT handle frag lists within
1526 * the frag list, if such a thing exists. We'd probably need to recurse to
1527 * handle that cleanly.
1528 */
Jarek Poplawski8b9d3722009-01-19 17:03:56 -08001529int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
Jens Axboe9c55e012007-11-06 23:30:13 -08001530 struct pipe_inode_info *pipe, unsigned int tlen,
1531 unsigned int flags)
1532{
1533 struct partial_page partial[PIPE_BUFFERS];
1534 struct page *pages[PIPE_BUFFERS];
1535 struct splice_pipe_desc spd = {
1536 .pages = pages,
1537 .partial = partial,
1538 .flags = flags,
1539 .ops = &sock_pipe_buf_ops,
1540 .spd_release = sock_spd_release,
1541 };
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001542 struct sock *sk = skb->sk;
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 */
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001548 if (__skb_splice_bits(skb, &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 */
1556 if (skb_shinfo(skb)->frag_list) {
1557 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1558
1559 for (; list && tlen; list = list->next) {
Jarek Poplawski7a67e562009-04-30 05:41:19 -07001560 if (__skb_splice_bits(list, &offset, &tlen, &spd, sk))
Jens Axboe9c55e012007-11-06 23:30:13 -08001561 break;
1562 }
1563 }
1564
1565done:
Jens Axboe9c55e012007-11-06 23:30:13 -08001566 if (spd.nr_pages) {
1567 int ret;
1568
1569 /*
1570 * Drop the socket lock, otherwise we have reverse
1571 * locking dependencies between sk_lock and i_mutex
1572 * here as compared to sendfile(). We enter here
1573 * with the socket lock held, and splice_to_pipe() will
1574 * grab the pipe inode lock. For sendfile() emulation,
1575 * we call into ->sendpage() with the i_mutex lock held
1576 * and networking will grab the socket lock.
1577 */
Octavian Purdila293ad602008-06-04 15:45:58 -07001578 release_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001579 ret = splice_to_pipe(pipe, &spd);
Octavian Purdila293ad602008-06-04 15:45:58 -07001580 lock_sock(sk);
Jens Axboe9c55e012007-11-06 23:30:13 -08001581 return ret;
1582 }
1583
1584 return 0;
1585}
1586
Herbert Xu357b40a2005-04-19 22:30:14 -07001587/**
1588 * skb_store_bits - store bits from kernel buffer to skb
1589 * @skb: destination buffer
1590 * @offset: offset in destination
1591 * @from: source buffer
1592 * @len: number of bytes to copy
1593 *
1594 * Copy the specified number of bytes from the source buffer to the
1595 * destination skb. This function handles all the messy bits of
1596 * traversing fragment lists and such.
1597 */
1598
Stephen Hemminger0c6fcc82007-04-20 16:40:01 -07001599int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
Herbert Xu357b40a2005-04-19 22:30:14 -07001600{
1601 int i, copy;
David S. Miller1a028e52007-04-27 15:21:23 -07001602 int start = skb_headlen(skb);
Herbert Xu357b40a2005-04-19 22:30:14 -07001603
1604 if (offset > (int)skb->len - len)
1605 goto fault;
1606
David S. Miller1a028e52007-04-27 15:21:23 -07001607 if ((copy = start - offset) > 0) {
Herbert Xu357b40a2005-04-19 22:30:14 -07001608 if (copy > len)
1609 copy = len;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001610 skb_copy_to_linear_data_offset(skb, offset, from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001611 if ((len -= copy) == 0)
1612 return 0;
1613 offset += copy;
1614 from += copy;
1615 }
1616
1617 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1618 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
David S. Miller1a028e52007-04-27 15:21:23 -07001619 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001620
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001621 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001622
1623 end = start + frag->size;
Herbert Xu357b40a2005-04-19 22:30:14 -07001624 if ((copy = end - offset) > 0) {
1625 u8 *vaddr;
1626
1627 if (copy > len)
1628 copy = len;
1629
1630 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001631 memcpy(vaddr + frag->page_offset + offset - start,
1632 from, copy);
Herbert Xu357b40a2005-04-19 22:30:14 -07001633 kunmap_skb_frag(vaddr);
1634
1635 if ((len -= copy) == 0)
1636 return 0;
1637 offset += copy;
1638 from += copy;
1639 }
David S. Miller1a028e52007-04-27 15:21:23 -07001640 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001641 }
1642
1643 if (skb_shinfo(skb)->frag_list) {
1644 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1645
1646 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001647 int end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001648
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001649 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001650
1651 end = start + list->len;
Herbert Xu357b40a2005-04-19 22:30:14 -07001652 if ((copy = end - offset) > 0) {
1653 if (copy > len)
1654 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001655 if (skb_store_bits(list, offset - start,
1656 from, copy))
Herbert Xu357b40a2005-04-19 22:30:14 -07001657 goto fault;
1658 if ((len -= copy) == 0)
1659 return 0;
1660 offset += copy;
1661 from += copy;
1662 }
David S. Miller1a028e52007-04-27 15:21:23 -07001663 start = end;
Herbert Xu357b40a2005-04-19 22:30:14 -07001664 }
1665 }
1666 if (!len)
1667 return 0;
1668
1669fault:
1670 return -EFAULT;
1671}
Herbert Xu357b40a2005-04-19 22:30:14 -07001672EXPORT_SYMBOL(skb_store_bits);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674/* Checksum skb data. */
1675
Al Viro2bbbc862006-11-14 21:37:14 -08001676__wsum skb_checksum(const struct sk_buff *skb, int offset,
1677 int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
David S. Miller1a028e52007-04-27 15:21:23 -07001679 int start = skb_headlen(skb);
1680 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int pos = 0;
1682
1683 /* Checksum header. */
1684 if (copy > 0) {
1685 if (copy > len)
1686 copy = len;
1687 csum = csum_partial(skb->data + offset, copy, csum);
1688 if ((len -= copy) == 0)
1689 return csum;
1690 offset += copy;
1691 pos = copy;
1692 }
1693
1694 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001695 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001697 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001698
1699 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if ((copy = end - offset) > 0) {
Al Viro44bb9362006-11-14 21:36:14 -08001701 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 u8 *vaddr;
1703 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1704
1705 if (copy > len)
1706 copy = len;
1707 vaddr = kmap_skb_frag(frag);
David S. Miller1a028e52007-04-27 15:21:23 -07001708 csum2 = csum_partial(vaddr + frag->page_offset +
1709 offset - start, copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 kunmap_skb_frag(vaddr);
1711 csum = csum_block_add(csum, csum2, pos);
1712 if (!(len -= copy))
1713 return csum;
1714 offset += copy;
1715 pos += copy;
1716 }
David S. Miller1a028e52007-04-27 15:21:23 -07001717 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 if (skb_shinfo(skb)->frag_list) {
1721 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1722
1723 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07001724 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001726 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001727
1728 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if ((copy = end - offset) > 0) {
Al Viro5f92a732006-11-14 21:36:54 -08001730 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (copy > len)
1732 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001733 csum2 = skb_checksum(list, offset - start,
1734 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 csum = csum_block_add(csum, csum2, pos);
1736 if ((len -= copy) == 0)
1737 return csum;
1738 offset += copy;
1739 pos += copy;
1740 }
David S. Miller1a028e52007-04-27 15:21:23 -07001741 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001744 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 return csum;
1747}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001748EXPORT_SYMBOL(skb_checksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750/* Both of above in one bottle. */
1751
Al Viro81d77662006-11-14 21:37:33 -08001752__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1753 u8 *to, int len, __wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
David S. Miller1a028e52007-04-27 15:21:23 -07001755 int start = skb_headlen(skb);
1756 int i, copy = start - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int pos = 0;
1758
1759 /* Copy header. */
1760 if (copy > 0) {
1761 if (copy > len)
1762 copy = len;
1763 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1764 copy, csum);
1765 if ((len -= copy) == 0)
1766 return csum;
1767 offset += copy;
1768 to += copy;
1769 pos = copy;
1770 }
1771
1772 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07001773 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001775 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001776
1777 end = start + skb_shinfo(skb)->frags[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if ((copy = end - offset) > 0) {
Al Viro50842052006-11-14 21:36:34 -08001779 __wsum csum2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 u8 *vaddr;
1781 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1782
1783 if (copy > len)
1784 copy = len;
1785 vaddr = kmap_skb_frag(frag);
1786 csum2 = csum_partial_copy_nocheck(vaddr +
David S. Miller1a028e52007-04-27 15:21:23 -07001787 frag->page_offset +
1788 offset - start, to,
1789 copy, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 kunmap_skb_frag(vaddr);
1791 csum = csum_block_add(csum, csum2, pos);
1792 if (!(len -= copy))
1793 return csum;
1794 offset += copy;
1795 to += copy;
1796 pos += copy;
1797 }
David S. Miller1a028e52007-04-27 15:21:23 -07001798 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
1801 if (skb_shinfo(skb)->frag_list) {
1802 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1803
1804 for (; list; list = list->next) {
Al Viro81d77662006-11-14 21:37:33 -08001805 __wsum csum2;
David S. Miller1a028e52007-04-27 15:21:23 -07001806 int end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001808 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07001809
1810 end = start + list->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if ((copy = end - offset) > 0) {
1812 if (copy > len)
1813 copy = len;
David S. Miller1a028e52007-04-27 15:21:23 -07001814 csum2 = skb_copy_and_csum_bits(list,
1815 offset - start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 to, copy, 0);
1817 csum = csum_block_add(csum, csum2, pos);
1818 if ((len -= copy) == 0)
1819 return csum;
1820 offset += copy;
1821 to += copy;
1822 pos += copy;
1823 }
David S. Miller1a028e52007-04-27 15:21:23 -07001824 start = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 }
Kris Katterjohn09a62662006-01-08 22:24:28 -08001827 BUG_ON(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return csum;
1829}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001830EXPORT_SYMBOL(skb_copy_and_csum_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1833{
Al Virod3bc23e2006-11-14 21:24:49 -08001834 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 long csstart;
1836
Patrick McHardy84fa7932006-08-29 16:44:56 -07001837 if (skb->ip_summed == CHECKSUM_PARTIAL)
Herbert Xu663ead32007-04-09 11:59:07 -07001838 csstart = skb->csum_start - skb_headroom(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 else
1840 csstart = skb_headlen(skb);
1841
Kris Katterjohn09a62662006-01-08 22:24:28 -08001842 BUG_ON(csstart > skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001844 skb_copy_from_linear_data(skb, to, csstart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 csum = 0;
1847 if (csstart != skb->len)
1848 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1849 skb->len - csstart, 0);
1850
Patrick McHardy84fa7932006-08-29 16:44:56 -07001851 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viroff1dcad2006-11-20 18:07:29 -08001852 long csstuff = csstart + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Al Virod3bc23e2006-11-14 21:24:49 -08001854 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001857EXPORT_SYMBOL(skb_copy_and_csum_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859/**
1860 * skb_dequeue - remove from the head of the queue
1861 * @list: list to dequeue from
1862 *
1863 * Remove the head of the list. The list lock is taken so the function
1864 * may be used safely with other locking list functions. The head item is
1865 * returned or %NULL if the list is empty.
1866 */
1867
1868struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1869{
1870 unsigned long flags;
1871 struct sk_buff *result;
1872
1873 spin_lock_irqsave(&list->lock, flags);
1874 result = __skb_dequeue(list);
1875 spin_unlock_irqrestore(&list->lock, flags);
1876 return result;
1877}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001878EXPORT_SYMBOL(skb_dequeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880/**
1881 * skb_dequeue_tail - remove from the tail of the queue
1882 * @list: list to dequeue from
1883 *
1884 * Remove the tail of the list. The list lock is taken so the function
1885 * may be used safely with other locking list functions. The tail item is
1886 * returned or %NULL if the list is empty.
1887 */
1888struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1889{
1890 unsigned long flags;
1891 struct sk_buff *result;
1892
1893 spin_lock_irqsave(&list->lock, flags);
1894 result = __skb_dequeue_tail(list);
1895 spin_unlock_irqrestore(&list->lock, flags);
1896 return result;
1897}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001898EXPORT_SYMBOL(skb_dequeue_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900/**
1901 * skb_queue_purge - empty a list
1902 * @list: list to empty
1903 *
1904 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1905 * the list and one reference dropped. This function takes the list
1906 * lock and is atomic with respect to other list locking functions.
1907 */
1908void skb_queue_purge(struct sk_buff_head *list)
1909{
1910 struct sk_buff *skb;
1911 while ((skb = skb_dequeue(list)) != NULL)
1912 kfree_skb(skb);
1913}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001914EXPORT_SYMBOL(skb_queue_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916/**
1917 * skb_queue_head - queue a buffer at the list head
1918 * @list: list to use
1919 * @newsk: buffer to queue
1920 *
1921 * Queue a buffer at the start of the list. This function takes the
1922 * list lock and can be used safely with other locking &sk_buff functions
1923 * safely.
1924 *
1925 * A buffer cannot be placed on two lists at the same time.
1926 */
1927void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1928{
1929 unsigned long flags;
1930
1931 spin_lock_irqsave(&list->lock, flags);
1932 __skb_queue_head(list, newsk);
1933 spin_unlock_irqrestore(&list->lock, flags);
1934}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001935EXPORT_SYMBOL(skb_queue_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937/**
1938 * skb_queue_tail - queue a buffer at the list tail
1939 * @list: list to use
1940 * @newsk: buffer to queue
1941 *
1942 * Queue a buffer at the tail of the list. This function takes the
1943 * list lock and can be used safely with other locking &sk_buff functions
1944 * safely.
1945 *
1946 * A buffer cannot be placed on two lists at the same time.
1947 */
1948void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1949{
1950 unsigned long flags;
1951
1952 spin_lock_irqsave(&list->lock, flags);
1953 __skb_queue_tail(list, newsk);
1954 spin_unlock_irqrestore(&list->lock, flags);
1955}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001956EXPORT_SYMBOL(skb_queue_tail);
David S. Miller8728b832005-08-09 19:25:21 -07001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/**
1959 * skb_unlink - remove a buffer from a list
1960 * @skb: buffer to remove
David S. Miller8728b832005-08-09 19:25:21 -07001961 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 *
David S. Miller8728b832005-08-09 19:25:21 -07001963 * Remove a packet from a list. The list locks are taken and this
1964 * function is atomic with respect to other list locked calls
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 *
David S. Miller8728b832005-08-09 19:25:21 -07001966 * You must know what list the SKB is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 */
David S. Miller8728b832005-08-09 19:25:21 -07001968void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
David S. Miller8728b832005-08-09 19:25:21 -07001970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
David S. Miller8728b832005-08-09 19:25:21 -07001972 spin_lock_irqsave(&list->lock, flags);
1973 __skb_unlink(skb, list);
1974 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001976EXPORT_SYMBOL(skb_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/**
1979 * skb_append - append a buffer
1980 * @old: buffer to insert after
1981 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07001982 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 *
1984 * Place a packet after a given packet in a list. The list locks are taken
1985 * and this function is atomic with respect to other list locked calls.
1986 * A buffer cannot be placed on two lists at the same time.
1987 */
David S. Miller8728b832005-08-09 19:25:21 -07001988void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 unsigned long flags;
1991
David S. Miller8728b832005-08-09 19:25:21 -07001992 spin_lock_irqsave(&list->lock, flags);
Gerrit Renker7de6c032008-04-14 00:05:09 -07001993 __skb_queue_after(list, old, newsk);
David S. Miller8728b832005-08-09 19:25:21 -07001994 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08001996EXPORT_SYMBOL(skb_append);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998/**
1999 * skb_insert - insert a buffer
2000 * @old: buffer to insert before
2001 * @newsk: buffer to insert
David S. Miller8728b832005-08-09 19:25:21 -07002002 * @list: list to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 *
David S. Miller8728b832005-08-09 19:25:21 -07002004 * Place a packet before a given packet in a list. The list locks are
2005 * taken and this function is atomic with respect to other list locked
2006 * calls.
2007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * A buffer cannot be placed on two lists at the same time.
2009 */
David S. Miller8728b832005-08-09 19:25:21 -07002010void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 unsigned long flags;
2013
David S. Miller8728b832005-08-09 19:25:21 -07002014 spin_lock_irqsave(&list->lock, flags);
2015 __skb_insert(newsk, old->prev, old, list);
2016 spin_unlock_irqrestore(&list->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002018EXPORT_SYMBOL(skb_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020static inline void skb_split_inside_header(struct sk_buff *skb,
2021 struct sk_buff* skb1,
2022 const u32 len, const int pos)
2023{
2024 int i;
2025
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002026 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2027 pos - len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /* And move data appendix as is. */
2029 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2030 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2031
2032 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2033 skb_shinfo(skb)->nr_frags = 0;
2034 skb1->data_len = skb->data_len;
2035 skb1->len += skb1->data_len;
2036 skb->data_len = 0;
2037 skb->len = len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002038 skb_set_tail_pointer(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041static inline void skb_split_no_header(struct sk_buff *skb,
2042 struct sk_buff* skb1,
2043 const u32 len, int pos)
2044{
2045 int i, k = 0;
2046 const int nfrags = skb_shinfo(skb)->nr_frags;
2047
2048 skb_shinfo(skb)->nr_frags = 0;
2049 skb1->len = skb1->data_len = skb->len - len;
2050 skb->len = len;
2051 skb->data_len = len - pos;
2052
2053 for (i = 0; i < nfrags; i++) {
2054 int size = skb_shinfo(skb)->frags[i].size;
2055
2056 if (pos + size > len) {
2057 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2058
2059 if (pos < len) {
2060 /* Split frag.
2061 * We have two variants in this case:
2062 * 1. Move all the frag to the second
2063 * part, if it is possible. F.e.
2064 * this approach is mandatory for TUX,
2065 * where splitting is expensive.
2066 * 2. Split is accurately. We make this.
2067 */
2068 get_page(skb_shinfo(skb)->frags[i].page);
2069 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2070 skb_shinfo(skb1)->frags[0].size -= len - pos;
2071 skb_shinfo(skb)->frags[i].size = len - pos;
2072 skb_shinfo(skb)->nr_frags++;
2073 }
2074 k++;
2075 } else
2076 skb_shinfo(skb)->nr_frags++;
2077 pos += size;
2078 }
2079 skb_shinfo(skb1)->nr_frags = k;
2080}
2081
2082/**
2083 * skb_split - Split fragmented skb to two parts at length len.
2084 * @skb: the buffer to split
2085 * @skb1: the buffer to receive the second part
2086 * @len: new length for skb
2087 */
2088void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2089{
2090 int pos = skb_headlen(skb);
2091
2092 if (len < pos) /* Split line is inside header. */
2093 skb_split_inside_header(skb, skb1, len, pos);
2094 else /* Second chunk has no header, nothing to copy. */
2095 skb_split_no_header(skb, skb1, len, pos);
2096}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002097EXPORT_SYMBOL(skb_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002099/* Shifting from/to a cloned skb is a no-go.
2100 *
2101 * Caller cannot keep skb_shinfo related pointers past calling here!
2102 */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002103static int skb_prepare_for_shift(struct sk_buff *skb)
2104{
Ilpo Järvinen0ace2852008-11-24 21:30:21 -08002105 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002106}
2107
2108/**
2109 * skb_shift - Shifts paged data partially from skb to another
2110 * @tgt: buffer into which tail data gets added
2111 * @skb: buffer from which the paged data comes from
2112 * @shiftlen: shift up to this many bytes
2113 *
2114 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2115 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2116 * It's up to caller to free skb if everything was shifted.
2117 *
2118 * If @tgt runs out of frags, the whole operation is aborted.
2119 *
2120 * Skb cannot include anything else but paged data while tgt is allowed
2121 * to have non-paged data as well.
2122 *
2123 * TODO: full sized shift could be optimized but that would need
2124 * specialized skb free'er to handle frags without up-to-date nr_frags.
2125 */
2126int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2127{
2128 int from, to, merge, todo;
2129 struct skb_frag_struct *fragfrom, *fragto;
2130
2131 BUG_ON(shiftlen > skb->len);
2132 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2133
2134 todo = shiftlen;
2135 from = 0;
2136 to = skb_shinfo(tgt)->nr_frags;
2137 fragfrom = &skb_shinfo(skb)->frags[from];
2138
2139 /* Actual merge is delayed until the point when we know we can
2140 * commit all, so that we don't have to undo partial changes
2141 */
2142 if (!to ||
2143 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2144 merge = -1;
2145 } else {
2146 merge = to - 1;
2147
2148 todo -= fragfrom->size;
2149 if (todo < 0) {
2150 if (skb_prepare_for_shift(skb) ||
2151 skb_prepare_for_shift(tgt))
2152 return 0;
2153
Ilpo Järvinen9f782db2008-11-25 13:57:01 -08002154 /* All previous frag pointers might be stale! */
2155 fragfrom = &skb_shinfo(skb)->frags[from];
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08002156 fragto = &skb_shinfo(tgt)->frags[merge];
2157
2158 fragto->size += shiftlen;
2159 fragfrom->size -= shiftlen;
2160 fragfrom->page_offset += shiftlen;
2161
2162 goto onlymerged;
2163 }
2164
2165 from++;
2166 }
2167
2168 /* Skip full, not-fitting skb to avoid expensive operations */
2169 if ((shiftlen == skb->len) &&
2170 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2171 return 0;
2172
2173 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2174 return 0;
2175
2176 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2177 if (to == MAX_SKB_FRAGS)
2178 return 0;
2179
2180 fragfrom = &skb_shinfo(skb)->frags[from];
2181 fragto = &skb_shinfo(tgt)->frags[to];
2182
2183 if (todo >= fragfrom->size) {
2184 *fragto = *fragfrom;
2185 todo -= fragfrom->size;
2186 from++;
2187 to++;
2188
2189 } else {
2190 get_page(fragfrom->page);
2191 fragto->page = fragfrom->page;
2192 fragto->page_offset = fragfrom->page_offset;
2193 fragto->size = todo;
2194
2195 fragfrom->page_offset += todo;
2196 fragfrom->size -= todo;
2197 todo = 0;
2198
2199 to++;
2200 break;
2201 }
2202 }
2203
2204 /* Ready to "commit" this state change to tgt */
2205 skb_shinfo(tgt)->nr_frags = to;
2206
2207 if (merge >= 0) {
2208 fragfrom = &skb_shinfo(skb)->frags[0];
2209 fragto = &skb_shinfo(tgt)->frags[merge];
2210
2211 fragto->size += fragfrom->size;
2212 put_page(fragfrom->page);
2213 }
2214
2215 /* Reposition in the original skb */
2216 to = 0;
2217 while (from < skb_shinfo(skb)->nr_frags)
2218 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2219 skb_shinfo(skb)->nr_frags = to;
2220
2221 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2222
2223onlymerged:
2224 /* Most likely the tgt won't ever need its checksum anymore, skb on
2225 * the other hand might need it if it needs to be resent
2226 */
2227 tgt->ip_summed = CHECKSUM_PARTIAL;
2228 skb->ip_summed = CHECKSUM_PARTIAL;
2229
2230 /* Yak, is it really working this way? Some helper please? */
2231 skb->len -= shiftlen;
2232 skb->data_len -= shiftlen;
2233 skb->truesize -= shiftlen;
2234 tgt->len += shiftlen;
2235 tgt->data_len += shiftlen;
2236 tgt->truesize += shiftlen;
2237
2238 return shiftlen;
2239}
2240
Thomas Graf677e90e2005-06-23 20:59:51 -07002241/**
2242 * skb_prepare_seq_read - Prepare a sequential read of skb data
2243 * @skb: the buffer to read
2244 * @from: lower offset of data to be read
2245 * @to: upper offset of data to be read
2246 * @st: state variable
2247 *
2248 * Initializes the specified state variable. Must be called before
2249 * invoking skb_seq_read() for the first time.
2250 */
2251void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2252 unsigned int to, struct skb_seq_state *st)
2253{
2254 st->lower_offset = from;
2255 st->upper_offset = to;
2256 st->root_skb = st->cur_skb = skb;
2257 st->frag_idx = st->stepped_offset = 0;
2258 st->frag_data = NULL;
2259}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002260EXPORT_SYMBOL(skb_prepare_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002261
2262/**
2263 * skb_seq_read - Sequentially read skb data
2264 * @consumed: number of bytes consumed by the caller so far
2265 * @data: destination pointer for data to be returned
2266 * @st: state variable
2267 *
2268 * Reads a block of skb data at &consumed relative to the
2269 * lower offset specified to skb_prepare_seq_read(). Assigns
2270 * the head of the data block to &data and returns the length
2271 * of the block or 0 if the end of the skb data or the upper
2272 * offset has been reached.
2273 *
2274 * The caller is not required to consume all of the data
2275 * returned, i.e. &consumed is typically set to the number
2276 * of bytes already consumed and the next call to
2277 * skb_seq_read() will return the remaining part of the block.
2278 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002279 * Note 1: The size of each block of data returned can be arbitary,
Thomas Graf677e90e2005-06-23 20:59:51 -07002280 * this limitation is the cost for zerocopy seqeuental
2281 * reads of potentially non linear data.
2282 *
Randy Dunlapbc2cda12008-02-13 15:03:25 -08002283 * Note 2: Fragment lists within fragments are not implemented
Thomas Graf677e90e2005-06-23 20:59:51 -07002284 * at the moment, state->root_skb could be replaced with
2285 * a stack for this purpose.
2286 */
2287unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2288 struct skb_seq_state *st)
2289{
2290 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2291 skb_frag_t *frag;
2292
2293 if (unlikely(abs_offset >= st->upper_offset))
2294 return 0;
2295
2296next_skb:
Herbert Xu95e3b242009-01-29 16:07:52 -08002297 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
Thomas Graf677e90e2005-06-23 20:59:51 -07002298
Thomas Chenault995b3372009-05-18 21:43:27 -07002299 if (abs_offset < block_limit && !st->frag_data) {
Herbert Xu95e3b242009-01-29 16:07:52 -08002300 *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
Thomas Graf677e90e2005-06-23 20:59:51 -07002301 return block_limit - abs_offset;
2302 }
2303
2304 if (st->frag_idx == 0 && !st->frag_data)
2305 st->stepped_offset += skb_headlen(st->cur_skb);
2306
2307 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2308 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2309 block_limit = frag->size + st->stepped_offset;
2310
2311 if (abs_offset < block_limit) {
2312 if (!st->frag_data)
2313 st->frag_data = kmap_skb_frag(frag);
2314
2315 *data = (u8 *) st->frag_data + frag->page_offset +
2316 (abs_offset - st->stepped_offset);
2317
2318 return block_limit - abs_offset;
2319 }
2320
2321 if (st->frag_data) {
2322 kunmap_skb_frag(st->frag_data);
2323 st->frag_data = NULL;
2324 }
2325
2326 st->frag_idx++;
2327 st->stepped_offset += frag->size;
2328 }
2329
Olaf Kirch5b5a60d2007-06-23 23:11:52 -07002330 if (st->frag_data) {
2331 kunmap_skb_frag(st->frag_data);
2332 st->frag_data = NULL;
2333 }
2334
Shyam Iyer71b33462009-01-29 16:12:42 -08002335 if (st->root_skb == st->cur_skb &&
2336 skb_shinfo(st->root_skb)->frag_list) {
2337 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
Thomas Graf677e90e2005-06-23 20:59:51 -07002338 st->frag_idx = 0;
2339 goto next_skb;
Shyam Iyer71b33462009-01-29 16:12:42 -08002340 } else if (st->cur_skb->next) {
2341 st->cur_skb = st->cur_skb->next;
Herbert Xu95e3b242009-01-29 16:07:52 -08002342 st->frag_idx = 0;
Thomas Graf677e90e2005-06-23 20:59:51 -07002343 goto next_skb;
2344 }
2345
2346 return 0;
2347}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002348EXPORT_SYMBOL(skb_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002349
2350/**
2351 * skb_abort_seq_read - Abort a sequential read of skb data
2352 * @st: state variable
2353 *
2354 * Must be called if skb_seq_read() was not called until it
2355 * returned 0.
2356 */
2357void skb_abort_seq_read(struct skb_seq_state *st)
2358{
2359 if (st->frag_data)
2360 kunmap_skb_frag(st->frag_data);
2361}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002362EXPORT_SYMBOL(skb_abort_seq_read);
Thomas Graf677e90e2005-06-23 20:59:51 -07002363
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002364#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2365
2366static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2367 struct ts_config *conf,
2368 struct ts_state *state)
2369{
2370 return skb_seq_read(offset, text, TS_SKB_CB(state));
2371}
2372
2373static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2374{
2375 skb_abort_seq_read(TS_SKB_CB(state));
2376}
2377
2378/**
2379 * skb_find_text - Find a text pattern in skb data
2380 * @skb: the buffer to look in
2381 * @from: search offset
2382 * @to: search limit
2383 * @config: textsearch configuration
2384 * @state: uninitialized textsearch state variable
2385 *
2386 * Finds a pattern in the skb data according to the specified
2387 * textsearch configuration. Use textsearch_next() to retrieve
2388 * subsequent occurrences of the pattern. Returns the offset
2389 * to the first occurrence or UINT_MAX if no match was found.
2390 */
2391unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2392 unsigned int to, struct ts_config *config,
2393 struct ts_state *state)
2394{
Phil Oesterf72b9482006-06-26 00:00:57 -07002395 unsigned int ret;
2396
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002397 config->get_next_block = skb_ts_get_next_block;
2398 config->finish = skb_ts_finish;
2399
2400 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2401
Phil Oesterf72b9482006-06-26 00:00:57 -07002402 ret = textsearch_find(config, state);
2403 return (ret <= to - from ? ret : UINT_MAX);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002404}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002405EXPORT_SYMBOL(skb_find_text);
Thomas Graf3fc7e8a2005-06-23 21:00:17 -07002406
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002407/**
2408 * skb_append_datato_frags: - append the user data to a skb
2409 * @sk: sock structure
2410 * @skb: skb structure to be appened with user data.
2411 * @getfrag: call back function to be used for getting the user data
2412 * @from: pointer to user message iov
2413 * @length: length of the iov message
2414 *
2415 * Description: This procedure append the user data in the fragment part
2416 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2417 */
2418int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
Martin Waitzdab96302005-12-05 13:40:12 -08002419 int (*getfrag)(void *from, char *to, int offset,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002420 int len, int odd, struct sk_buff *skb),
2421 void *from, int length)
2422{
2423 int frg_cnt = 0;
2424 skb_frag_t *frag = NULL;
2425 struct page *page = NULL;
2426 int copy, left;
2427 int offset = 0;
2428 int ret;
2429
2430 do {
2431 /* Return error if we don't have space for new frag */
2432 frg_cnt = skb_shinfo(skb)->nr_frags;
2433 if (frg_cnt >= MAX_SKB_FRAGS)
2434 return -EFAULT;
2435
2436 /* allocate a new page for next frag */
2437 page = alloc_pages(sk->sk_allocation, 0);
2438
2439 /* If alloc_page fails just return failure and caller will
2440 * free previous allocated pages by doing kfree_skb()
2441 */
2442 if (page == NULL)
2443 return -ENOMEM;
2444
2445 /* initialize the next frag */
2446 sk->sk_sndmsg_page = page;
2447 sk->sk_sndmsg_off = 0;
2448 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2449 skb->truesize += PAGE_SIZE;
2450 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2451
2452 /* get the new initialized frag */
2453 frg_cnt = skb_shinfo(skb)->nr_frags;
2454 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2455
2456 /* copy the user data to page */
2457 left = PAGE_SIZE - frag->page_offset;
2458 copy = (length > left)? left : length;
2459
2460 ret = getfrag(from, (page_address(frag->page) +
2461 frag->page_offset + frag->size),
2462 offset, copy, 0, skb);
2463 if (ret < 0)
2464 return -EFAULT;
2465
2466 /* copy was successful so update the size parameters */
2467 sk->sk_sndmsg_off += copy;
2468 frag->size += copy;
2469 skb->len += copy;
2470 skb->data_len += copy;
2471 offset += copy;
2472 length -= copy;
2473
2474 } while (length > 0);
2475
2476 return 0;
2477}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002478EXPORT_SYMBOL(skb_append_datato_frags);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07002479
Herbert Xucbb042f2006-03-20 22:43:56 -08002480/**
2481 * skb_pull_rcsum - pull skb and update receive checksum
2482 * @skb: buffer to update
Herbert Xucbb042f2006-03-20 22:43:56 -08002483 * @len: length of data pulled
2484 *
2485 * This function performs an skb_pull on the packet and updates
Urs Thuermannfee54fa2008-02-12 22:03:25 -08002486 * the CHECKSUM_COMPLETE checksum. It should be used on
Patrick McHardy84fa7932006-08-29 16:44:56 -07002487 * receive path processing instead of skb_pull unless you know
2488 * that the checksum difference is zero (e.g., a valid IP header)
2489 * or you are setting ip_summed to CHECKSUM_NONE.
Herbert Xucbb042f2006-03-20 22:43:56 -08002490 */
2491unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2492{
2493 BUG_ON(len > skb->len);
2494 skb->len -= len;
2495 BUG_ON(skb->len < skb->data_len);
2496 skb_postpull_rcsum(skb, skb->data, len);
2497 return skb->data += len;
2498}
2499
Arnaldo Carvalho de Melof94691a2006-03-20 22:47:55 -08002500EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2501
Herbert Xuf4c50d92006-06-22 03:02:40 -07002502/**
2503 * skb_segment - Perform protocol segmentation on skb.
2504 * @skb: buffer to segment
Herbert Xu576a30e2006-06-27 13:22:38 -07002505 * @features: features for the output path (see dev->features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002506 *
2507 * This function performs segmentation on the given skb. It returns
Ben Hutchings4c821d72008-04-13 21:52:48 -07002508 * a pointer to the first in a list of new skbs for the segments.
2509 * In case of error it returns ERR_PTR(err).
Herbert Xuf4c50d92006-06-22 03:02:40 -07002510 */
Herbert Xu576a30e2006-06-27 13:22:38 -07002511struct sk_buff *skb_segment(struct sk_buff *skb, int features)
Herbert Xuf4c50d92006-06-22 03:02:40 -07002512{
2513 struct sk_buff *segs = NULL;
2514 struct sk_buff *tail = NULL;
Herbert Xu89319d382008-12-15 23:26:06 -08002515 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002516 unsigned int mss = skb_shinfo(skb)->gso_size;
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07002517 unsigned int doffset = skb->data - skb_mac_header(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002518 unsigned int offset = doffset;
2519 unsigned int headroom;
2520 unsigned int len;
Herbert Xu576a30e2006-06-27 13:22:38 -07002521 int sg = features & NETIF_F_SG;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002522 int nfrags = skb_shinfo(skb)->nr_frags;
2523 int err = -ENOMEM;
2524 int i = 0;
2525 int pos;
2526
2527 __skb_push(skb, doffset);
2528 headroom = skb_headroom(skb);
2529 pos = skb_headlen(skb);
2530
2531 do {
2532 struct sk_buff *nskb;
2533 skb_frag_t *frag;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002534 int hsize;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002535 int size;
2536
2537 len = skb->len - offset;
2538 if (len > mss)
2539 len = mss;
2540
2541 hsize = skb_headlen(skb) - offset;
2542 if (hsize < 0)
2543 hsize = 0;
Herbert Xuc8884ed2006-10-29 15:59:41 -08002544 if (hsize > len || !sg)
2545 hsize = len;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002546
Herbert Xu89319d382008-12-15 23:26:06 -08002547 if (!hsize && i >= nfrags) {
2548 BUG_ON(fskb->len != len);
2549
2550 pos += len;
2551 nskb = skb_clone(fskb, GFP_ATOMIC);
2552 fskb = fskb->next;
2553
2554 if (unlikely(!nskb))
2555 goto err;
2556
2557 hsize = skb_end_pointer(nskb) - nskb->head;
2558 if (skb_cow_head(nskb, doffset + headroom)) {
2559 kfree_skb(nskb);
2560 goto err;
2561 }
2562
2563 nskb->truesize += skb_end_pointer(nskb) - nskb->head -
2564 hsize;
2565 skb_release_head_state(nskb);
2566 __skb_push(nskb, doffset);
2567 } else {
2568 nskb = alloc_skb(hsize + doffset + headroom,
2569 GFP_ATOMIC);
2570
2571 if (unlikely(!nskb))
2572 goto err;
2573
2574 skb_reserve(nskb, headroom);
2575 __skb_put(nskb, doffset);
2576 }
Herbert Xuf4c50d92006-06-22 03:02:40 -07002577
2578 if (segs)
2579 tail->next = nskb;
2580 else
2581 segs = nskb;
2582 tail = nskb;
2583
Herbert Xu6f85a122008-08-15 14:55:02 -07002584 __copy_skb_header(nskb, skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002585 nskb->mac_len = skb->mac_len;
2586
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07002587 skb_reset_mac_header(nskb);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -03002588 skb_set_network_header(nskb, skb->mac_len);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002589 nskb->transport_header = (nskb->network_header +
2590 skb_network_header_len(skb));
Herbert Xu89319d382008-12-15 23:26:06 -08002591 skb_copy_from_linear_data(skb, nskb->data, doffset);
2592
Herbert Xu2f181852009-03-28 23:39:18 -07002593 if (fskb != skb_shinfo(skb)->frag_list)
Herbert Xu89319d382008-12-15 23:26:06 -08002594 continue;
2595
Herbert Xuf4c50d92006-06-22 03:02:40 -07002596 if (!sg) {
Herbert Xu6f85a122008-08-15 14:55:02 -07002597 nskb->ip_summed = CHECKSUM_NONE;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002598 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2599 skb_put(nskb, len),
2600 len, 0);
2601 continue;
2602 }
2603
2604 frag = skb_shinfo(nskb)->frags;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002605
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002606 skb_copy_from_linear_data_offset(skb, offset,
2607 skb_put(nskb, hsize), hsize);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002608
Herbert Xu89319d382008-12-15 23:26:06 -08002609 while (pos < offset + len && i < nfrags) {
Herbert Xuf4c50d92006-06-22 03:02:40 -07002610 *frag = skb_shinfo(skb)->frags[i];
2611 get_page(frag->page);
2612 size = frag->size;
2613
2614 if (pos < offset) {
2615 frag->page_offset += offset - pos;
2616 frag->size -= offset - pos;
2617 }
2618
Herbert Xu89319d382008-12-15 23:26:06 -08002619 skb_shinfo(nskb)->nr_frags++;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002620
2621 if (pos + size <= offset + len) {
2622 i++;
2623 pos += size;
2624 } else {
2625 frag->size -= pos + size - (offset + len);
Herbert Xu89319d382008-12-15 23:26:06 -08002626 goto skip_fraglist;
Herbert Xuf4c50d92006-06-22 03:02:40 -07002627 }
2628
2629 frag++;
2630 }
2631
Herbert Xu89319d382008-12-15 23:26:06 -08002632 if (pos < offset + len) {
2633 struct sk_buff *fskb2 = fskb;
2634
2635 BUG_ON(pos + fskb->len != offset + len);
2636
2637 pos += fskb->len;
2638 fskb = fskb->next;
2639
2640 if (fskb2->next) {
2641 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2642 if (!fskb2)
2643 goto err;
2644 } else
2645 skb_get(fskb2);
2646
2647 BUG_ON(skb_shinfo(nskb)->frag_list);
2648 skb_shinfo(nskb)->frag_list = fskb2;
2649 }
2650
2651skip_fraglist:
Herbert Xuf4c50d92006-06-22 03:02:40 -07002652 nskb->data_len = len - hsize;
2653 nskb->len += nskb->data_len;
2654 nskb->truesize += nskb->data_len;
2655 } while ((offset += len) < skb->len);
2656
2657 return segs;
2658
2659err:
2660 while ((skb = segs)) {
2661 segs = skb->next;
Patrick McHardyb08d5842007-02-27 09:57:37 -08002662 kfree_skb(skb);
Herbert Xuf4c50d92006-06-22 03:02:40 -07002663 }
2664 return ERR_PTR(err);
2665}
Herbert Xuf4c50d92006-06-22 03:02:40 -07002666EXPORT_SYMBOL_GPL(skb_segment);
2667
Herbert Xu71d93b32008-12-15 23:42:33 -08002668int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2669{
2670 struct sk_buff *p = *head;
2671 struct sk_buff *nskb;
2672 unsigned int headroom;
Herbert Xu86911732009-01-29 14:19:50 +00002673 unsigned int len = skb_gro_len(skb);
Herbert Xu71d93b32008-12-15 23:42:33 -08002674
Herbert Xu86911732009-01-29 14:19:50 +00002675 if (p->len + len >= 65536)
Herbert Xu71d93b32008-12-15 23:42:33 -08002676 return -E2BIG;
2677
2678 if (skb_shinfo(p)->frag_list)
2679 goto merge;
Herbert Xu81705ad2009-01-29 14:19:51 +00002680 else if (skb_headlen(skb) <= skb_gro_offset(skb)) {
2681 if (skb_shinfo(p)->nr_frags + skb_shinfo(skb)->nr_frags >
2682 MAX_SKB_FRAGS)
2683 return -E2BIG;
2684
Herbert Xu86911732009-01-29 14:19:50 +00002685 skb_shinfo(skb)->frags[0].page_offset +=
2686 skb_gro_offset(skb) - skb_headlen(skb);
2687 skb_shinfo(skb)->frags[0].size -=
2688 skb_gro_offset(skb) - skb_headlen(skb);
2689
Herbert Xu5d38a072009-01-04 16:13:40 -08002690 memcpy(skb_shinfo(p)->frags + skb_shinfo(p)->nr_frags,
2691 skb_shinfo(skb)->frags,
2692 skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
2693
2694 skb_shinfo(p)->nr_frags += skb_shinfo(skb)->nr_frags;
Herbert Xuf5572062009-01-14 20:40:03 -08002695 skb_shinfo(skb)->nr_frags = 0;
2696
2697 skb->truesize -= skb->data_len;
2698 skb->len -= skb->data_len;
2699 skb->data_len = 0;
2700
Herbert Xu5d38a072009-01-04 16:13:40 -08002701 NAPI_GRO_CB(skb)->free = 1;
2702 goto done;
2703 }
Herbert Xu71d93b32008-12-15 23:42:33 -08002704
2705 headroom = skb_headroom(p);
Herbert Xu86911732009-01-29 14:19:50 +00002706 nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002707 if (unlikely(!nskb))
2708 return -ENOMEM;
2709
2710 __copy_skb_header(nskb, p);
2711 nskb->mac_len = p->mac_len;
2712
2713 skb_reserve(nskb, headroom);
Herbert Xu86911732009-01-29 14:19:50 +00002714 __skb_put(nskb, skb_gro_offset(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002715
Herbert Xu86911732009-01-29 14:19:50 +00002716 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
Herbert Xu71d93b32008-12-15 23:42:33 -08002717 skb_set_network_header(nskb, skb_network_offset(p));
2718 skb_set_transport_header(nskb, skb_transport_offset(p));
2719
Herbert Xu86911732009-01-29 14:19:50 +00002720 __skb_pull(p, skb_gro_offset(p));
2721 memcpy(skb_mac_header(nskb), skb_mac_header(p),
2722 p->data - skb_mac_header(p));
Herbert Xu71d93b32008-12-15 23:42:33 -08002723
2724 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
2725 skb_shinfo(nskb)->frag_list = p;
Herbert Xub5302562009-01-04 16:13:19 -08002726 skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
Herbert Xu71d93b32008-12-15 23:42:33 -08002727 skb_header_release(p);
2728 nskb->prev = p;
2729
2730 nskb->data_len += p->len;
2731 nskb->truesize += p->len;
2732 nskb->len += p->len;
2733
2734 *head = nskb;
2735 nskb->next = p->next;
2736 p->next = NULL;
2737
2738 p = nskb;
2739
2740merge:
Herbert Xu56035022009-02-05 21:26:52 -08002741 if (skb_gro_offset(skb) > skb_headlen(skb)) {
2742 skb_shinfo(skb)->frags[0].page_offset +=
2743 skb_gro_offset(skb) - skb_headlen(skb);
2744 skb_shinfo(skb)->frags[0].size -=
2745 skb_gro_offset(skb) - skb_headlen(skb);
2746 skb_gro_reset_offset(skb);
2747 skb_gro_pull(skb, skb_headlen(skb));
2748 }
2749
2750 __skb_pull(skb, skb_gro_offset(skb));
2751
Herbert Xu71d93b32008-12-15 23:42:33 -08002752 p->prev->next = skb;
2753 p->prev = skb;
2754 skb_header_release(skb);
2755
Herbert Xu5d38a072009-01-04 16:13:40 -08002756done:
2757 NAPI_GRO_CB(p)->count++;
Herbert Xu37fe4732009-01-17 19:48:13 +00002758 p->data_len += len;
2759 p->truesize += len;
2760 p->len += len;
Herbert Xu71d93b32008-12-15 23:42:33 -08002761
2762 NAPI_GRO_CB(skb)->same_flow = 1;
2763 return 0;
2764}
2765EXPORT_SYMBOL_GPL(skb_gro_receive);
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767void __init skb_init(void)
2768{
2769 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2770 sizeof(struct sk_buff),
2771 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002772 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002773 NULL);
David S. Millerd179cd12005-08-17 14:57:30 -07002774 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2775 (2*sizeof(struct sk_buff)) +
2776 sizeof(atomic_t),
2777 0,
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002778 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002779 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
David Howells716ea3a2007-04-02 20:19:53 -07002782/**
2783 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2784 * @skb: Socket buffer containing the buffers to be mapped
2785 * @sg: The scatter-gather list to map into
2786 * @offset: The offset into the buffer's contents to start mapping
2787 * @len: Length of buffer space to be mapped
2788 *
2789 * Fill the specified scatter-gather list with mappings/pointers into a
2790 * region of the buffer space attached to a socket buffer.
2791 */
David S. Miller51c739d2007-10-30 21:29:29 -07002792static int
2793__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
David Howells716ea3a2007-04-02 20:19:53 -07002794{
David S. Miller1a028e52007-04-27 15:21:23 -07002795 int start = skb_headlen(skb);
2796 int i, copy = start - offset;
David Howells716ea3a2007-04-02 20:19:53 -07002797 int elt = 0;
2798
2799 if (copy > 0) {
2800 if (copy > len)
2801 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002802 sg_set_buf(sg, skb->data + offset, copy);
David Howells716ea3a2007-04-02 20:19:53 -07002803 elt++;
2804 if ((len -= copy) == 0)
2805 return elt;
2806 offset += copy;
2807 }
2808
2809 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
David S. Miller1a028e52007-04-27 15:21:23 -07002810 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002811
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002812 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002813
2814 end = start + skb_shinfo(skb)->frags[i].size;
David Howells716ea3a2007-04-02 20:19:53 -07002815 if ((copy = end - offset) > 0) {
2816 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2817
2818 if (copy > len)
2819 copy = len;
Jens Axboe642f1492007-10-24 11:20:47 +02002820 sg_set_page(&sg[elt], frag->page, copy,
2821 frag->page_offset+offset-start);
David Howells716ea3a2007-04-02 20:19:53 -07002822 elt++;
2823 if (!(len -= copy))
2824 return elt;
2825 offset += copy;
2826 }
David S. Miller1a028e52007-04-27 15:21:23 -07002827 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002828 }
2829
2830 if (skb_shinfo(skb)->frag_list) {
2831 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2832
2833 for (; list; list = list->next) {
David S. Miller1a028e52007-04-27 15:21:23 -07002834 int end;
David Howells716ea3a2007-04-02 20:19:53 -07002835
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002836 WARN_ON(start > offset + len);
David S. Miller1a028e52007-04-27 15:21:23 -07002837
2838 end = start + list->len;
David Howells716ea3a2007-04-02 20:19:53 -07002839 if ((copy = end - offset) > 0) {
2840 if (copy > len)
2841 copy = len;
David S. Miller51c739d2007-10-30 21:29:29 -07002842 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2843 copy);
David Howells716ea3a2007-04-02 20:19:53 -07002844 if ((len -= copy) == 0)
2845 return elt;
2846 offset += copy;
2847 }
David S. Miller1a028e52007-04-27 15:21:23 -07002848 start = end;
David Howells716ea3a2007-04-02 20:19:53 -07002849 }
2850 }
2851 BUG_ON(len);
2852 return elt;
2853}
2854
David S. Miller51c739d2007-10-30 21:29:29 -07002855int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2856{
2857 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2858
Jens Axboec46f2332007-10-31 12:06:37 +01002859 sg_mark_end(&sg[nsg - 1]);
David S. Miller51c739d2007-10-30 21:29:29 -07002860
2861 return nsg;
2862}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002863EXPORT_SYMBOL_GPL(skb_to_sgvec);
David S. Miller51c739d2007-10-30 21:29:29 -07002864
David Howells716ea3a2007-04-02 20:19:53 -07002865/**
2866 * skb_cow_data - Check that a socket buffer's data buffers are writable
2867 * @skb: The socket buffer to check.
2868 * @tailbits: Amount of trailing space to be added
2869 * @trailer: Returned pointer to the skb where the @tailbits space begins
2870 *
2871 * Make sure that the data buffers attached to a socket buffer are
2872 * writable. If they are not, private copies are made of the data buffers
2873 * and the socket buffer is set to use these instead.
2874 *
2875 * If @tailbits is given, make sure that there is space to write @tailbits
2876 * bytes of data beyond current end of socket buffer. @trailer will be
2877 * set to point to the skb in which this space begins.
2878 *
2879 * The number of scatterlist elements required to completely map the
2880 * COW'd and extended socket buffer will be returned.
2881 */
2882int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2883{
2884 int copyflag;
2885 int elt;
2886 struct sk_buff *skb1, **skb_p;
2887
2888 /* If skb is cloned or its head is paged, reallocate
2889 * head pulling out all the pages (pages are considered not writable
2890 * at the moment even if they are anonymous).
2891 */
2892 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2893 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2894 return -ENOMEM;
2895
2896 /* Easy case. Most of packets will go this way. */
2897 if (!skb_shinfo(skb)->frag_list) {
2898 /* A little of trouble, not enough of space for trailer.
2899 * This should not happen, when stack is tuned to generate
2900 * good frames. OK, on miss we reallocate and reserve even more
2901 * space, 128 bytes is fair. */
2902
2903 if (skb_tailroom(skb) < tailbits &&
2904 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2905 return -ENOMEM;
2906
2907 /* Voila! */
2908 *trailer = skb;
2909 return 1;
2910 }
2911
2912 /* Misery. We are in troubles, going to mincer fragments... */
2913
2914 elt = 1;
2915 skb_p = &skb_shinfo(skb)->frag_list;
2916 copyflag = 0;
2917
2918 while ((skb1 = *skb_p) != NULL) {
2919 int ntail = 0;
2920
2921 /* The fragment is partially pulled by someone,
2922 * this can happen on input. Copy it and everything
2923 * after it. */
2924
2925 if (skb_shared(skb1))
2926 copyflag = 1;
2927
2928 /* If the skb is the last, worry about trailer. */
2929
2930 if (skb1->next == NULL && tailbits) {
2931 if (skb_shinfo(skb1)->nr_frags ||
2932 skb_shinfo(skb1)->frag_list ||
2933 skb_tailroom(skb1) < tailbits)
2934 ntail = tailbits + 128;
2935 }
2936
2937 if (copyflag ||
2938 skb_cloned(skb1) ||
2939 ntail ||
2940 skb_shinfo(skb1)->nr_frags ||
2941 skb_shinfo(skb1)->frag_list) {
2942 struct sk_buff *skb2;
2943
2944 /* Fuck, we are miserable poor guys... */
2945 if (ntail == 0)
2946 skb2 = skb_copy(skb1, GFP_ATOMIC);
2947 else
2948 skb2 = skb_copy_expand(skb1,
2949 skb_headroom(skb1),
2950 ntail,
2951 GFP_ATOMIC);
2952 if (unlikely(skb2 == NULL))
2953 return -ENOMEM;
2954
2955 if (skb1->sk)
2956 skb_set_owner_w(skb2, skb1->sk);
2957
2958 /* Looking around. Are we still alive?
2959 * OK, link new skb, drop old one */
2960
2961 skb2->next = skb1->next;
2962 *skb_p = skb2;
2963 kfree_skb(skb1);
2964 skb1 = skb2;
2965 }
2966 elt++;
2967 *trailer = skb1;
2968 skb_p = &skb1->next;
2969 }
2970
2971 return elt;
2972}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08002973EXPORT_SYMBOL_GPL(skb_cow_data);
David Howells716ea3a2007-04-02 20:19:53 -07002974
Patrick Ohlyac45f602009-02-12 05:03:37 +00002975void skb_tstamp_tx(struct sk_buff *orig_skb,
2976 struct skb_shared_hwtstamps *hwtstamps)
2977{
2978 struct sock *sk = orig_skb->sk;
2979 struct sock_exterr_skb *serr;
2980 struct sk_buff *skb;
2981 int err;
2982
2983 if (!sk)
2984 return;
2985
2986 skb = skb_clone(orig_skb, GFP_ATOMIC);
2987 if (!skb)
2988 return;
2989
2990 if (hwtstamps) {
2991 *skb_hwtstamps(skb) =
2992 *hwtstamps;
2993 } else {
2994 /*
2995 * no hardware time stamps available,
2996 * so keep the skb_shared_tx and only
2997 * store software time stamp
2998 */
2999 skb->tstamp = ktime_get_real();
3000 }
3001
3002 serr = SKB_EXT_ERR(skb);
3003 memset(serr, 0, sizeof(*serr));
3004 serr->ee.ee_errno = ENOMSG;
3005 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3006 err = sock_queue_err_skb(sk, skb);
3007 if (err)
3008 kfree_skb(skb);
3009}
3010EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3011
3012
Rusty Russellf35d9d82008-02-04 23:49:54 -05003013/**
3014 * skb_partial_csum_set - set up and verify partial csum values for packet
3015 * @skb: the skb to set
3016 * @start: the number of bytes after skb->data to start checksumming.
3017 * @off: the offset from start to place the checksum.
3018 *
3019 * For untrusted partially-checksummed packets, we need to make sure the values
3020 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3021 *
3022 * This function checks and sets those values and skb->ip_summed: if this
3023 * returns false you should drop the packet.
3024 */
3025bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3026{
3027 if (unlikely(start > skb->len - 2) ||
3028 unlikely((int)start + off > skb->len - 2)) {
3029 if (net_ratelimit())
3030 printk(KERN_WARNING
3031 "bad partial csum: csum=%u/%u len=%u\n",
3032 start, off, skb->len);
3033 return false;
3034 }
3035 skb->ip_summed = CHECKSUM_PARTIAL;
3036 skb->csum_start = skb_headroom(skb) + start;
3037 skb->csum_offset = off;
3038 return true;
3039}
David S. Millerb4ac530fc2009-02-10 02:09:24 -08003040EXPORT_SYMBOL_GPL(skb_partial_csum_set);
Rusty Russellf35d9d82008-02-04 23:49:54 -05003041
Ben Hutchings4497b072008-06-19 16:22:28 -07003042void __skb_warn_lro_forwarding(const struct sk_buff *skb)
3043{
3044 if (net_ratelimit())
3045 pr_warning("%s: received packets cannot be forwarded"
3046 " while LRO is enabled\n", skb->dev->name);
3047}
Ben Hutchings4497b072008-06-19 16:22:28 -07003048EXPORT_SYMBOL(__skb_warn_lro_forwarding);