]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/skbuff.c
[NETFILTE] ipv4: Fix typo (Bugzilla #6753)
[thirdparty/linux.git] / net / core / skbuff.c
CommitLineData
1da177e4
LT
1/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
41#include <linux/config.h>
42#include <linux/module.h>
43#include <linux/types.h>
44#include <linux/kernel.h>
45#include <linux/sched.h>
46#include <linux/mm.h>
47#include <linux/interrupt.h>
48#include <linux/in.h>
49#include <linux/inet.h>
50#include <linux/slab.h>
51#include <linux/netdevice.h>
52#ifdef CONFIG_NET_CLS_ACT
53#include <net/pkt_sched.h>
54#endif
55#include <linux/string.h>
56#include <linux/skbuff.h>
57#include <linux/cache.h>
58#include <linux/rtnetlink.h>
59#include <linux/init.h>
60#include <linux/highmem.h>
61
62#include <net/protocol.h>
63#include <net/dst.h>
64#include <net/sock.h>
65#include <net/checksum.h>
66#include <net/xfrm.h>
67
68#include <asm/uaccess.h>
69#include <asm/system.h>
70
ba89966c
ED
71static kmem_cache_t *skbuff_head_cache __read_mostly;
72static kmem_cache_t *skbuff_fclone_cache __read_mostly;
1da177e4
LT
73
74/*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
79
80/**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89{
26095455
PM
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
94 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
26095455
PM
108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
112 BUG();
113}
114
dc6de336
DM
115void skb_truesize_bug(struct sk_buff *skb)
116{
117 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb->truesize, skb->len, sizeof(struct sk_buff));
120}
121EXPORT_SYMBOL(skb_truesize_bug);
122
1da177e4
LT
123/* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
125 * [BEEP] leaks.
126 *
127 */
128
129/**
d179cd12 130 * __alloc_skb - allocate a network buffer
1da177e4
LT
131 * @size: size to allocate
132 * @gfp_mask: allocation mask
c83c2486
RD
133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
1da177e4
LT
135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
dd0fc66f 143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
d179cd12 144 int fclone)
1da177e4 145{
8798b3fb 146 kmem_cache_t *cache;
4947d3ef 147 struct skb_shared_info *shinfo;
1da177e4
LT
148 struct sk_buff *skb;
149 u8 *data;
150
8798b3fb
HX
151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
1da177e4 153 /* Get the HEAD */
8798b3fb 154 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
1da177e4
LT
155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
871751e2 160 data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
1da177e4
LT
161 if (!data)
162 goto nodata;
163
164 memset(skb, 0, offsetof(struct sk_buff, truesize));
165 skb->truesize = size + sizeof(struct sk_buff);
166 atomic_set(&skb->users, 1);
167 skb->head = data;
168 skb->data = data;
169 skb->tail = data;
170 skb->end = data + size;
4947d3ef
BL
171 /* make sure we initialize shinfo sequentially */
172 shinfo = skb_shinfo(skb);
173 atomic_set(&shinfo->dataref, 1);
174 shinfo->nr_frags = 0;
7967168c
HX
175 shinfo->gso_size = 0;
176 shinfo->gso_segs = 0;
177 shinfo->gso_type = 0;
4947d3ef
BL
178 shinfo->ip6_frag_id = 0;
179 shinfo->frag_list = NULL;
180
d179cd12
DM
181 if (fclone) {
182 struct sk_buff *child = skb + 1;
183 atomic_t *fclone_ref = (atomic_t *) (child + 1);
1da177e4 184
d179cd12
DM
185 skb->fclone = SKB_FCLONE_ORIG;
186 atomic_set(fclone_ref, 1);
187
188 child->fclone = SKB_FCLONE_UNAVAILABLE;
189 }
1da177e4
LT
190out:
191 return skb;
192nodata:
8798b3fb 193 kmem_cache_free(cache, skb);
1da177e4
LT
194 skb = NULL;
195 goto out;
196}
197
198/**
199 * alloc_skb_from_cache - allocate a network buffer
200 * @cp: kmem_cache from which to allocate the data area
201 * (object size must be big enough for @size bytes + skb overheads)
202 * @size: size to allocate
203 * @gfp_mask: allocation mask
204 *
205 * Allocate a new &sk_buff. The returned buffer has no headroom and
206 * tail room of size bytes. The object has a reference count of one.
207 * The return is the buffer. On a failure the return is %NULL.
208 *
209 * Buffers may only be allocated from interrupts using a @gfp_mask of
210 * %GFP_ATOMIC.
211 */
212struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
86a76caf 213 unsigned int size,
dd0fc66f 214 gfp_t gfp_mask)
1da177e4
LT
215{
216 struct sk_buff *skb;
217 u8 *data;
218
219 /* Get the HEAD */
220 skb = kmem_cache_alloc(skbuff_head_cache,
221 gfp_mask & ~__GFP_DMA);
222 if (!skb)
223 goto out;
224
225 /* Get the DATA. */
226 size = SKB_DATA_ALIGN(size);
227 data = kmem_cache_alloc(cp, gfp_mask);
228 if (!data)
229 goto nodata;
230
231 memset(skb, 0, offsetof(struct sk_buff, truesize));
232 skb->truesize = size + sizeof(struct sk_buff);
233 atomic_set(&skb->users, 1);
234 skb->head = data;
235 skb->data = data;
236 skb->tail = data;
237 skb->end = data + size;
238
239 atomic_set(&(skb_shinfo(skb)->dataref), 1);
240 skb_shinfo(skb)->nr_frags = 0;
7967168c
HX
241 skb_shinfo(skb)->gso_size = 0;
242 skb_shinfo(skb)->gso_segs = 0;
243 skb_shinfo(skb)->gso_type = 0;
1da177e4
LT
244 skb_shinfo(skb)->frag_list = NULL;
245out:
246 return skb;
247nodata:
248 kmem_cache_free(skbuff_head_cache, skb);
249 skb = NULL;
250 goto out;
251}
252
253
254static void skb_drop_fraglist(struct sk_buff *skb)
255{
256 struct sk_buff *list = skb_shinfo(skb)->frag_list;
257
258 skb_shinfo(skb)->frag_list = NULL;
259
260 do {
261 struct sk_buff *this = list;
262 list = list->next;
263 kfree_skb(this);
264 } while (list);
265}
266
267static void skb_clone_fraglist(struct sk_buff *skb)
268{
269 struct sk_buff *list;
270
271 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
272 skb_get(list);
273}
274
275void skb_release_data(struct sk_buff *skb)
276{
277 if (!skb->cloned ||
278 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
279 &skb_shinfo(skb)->dataref)) {
280 if (skb_shinfo(skb)->nr_frags) {
281 int i;
282 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
283 put_page(skb_shinfo(skb)->frags[i].page);
284 }
285
286 if (skb_shinfo(skb)->frag_list)
287 skb_drop_fraglist(skb);
288
289 kfree(skb->head);
290 }
291}
292
293/*
294 * Free an skbuff by memory without cleaning the state.
295 */
296void kfree_skbmem(struct sk_buff *skb)
297{
d179cd12
DM
298 struct sk_buff *other;
299 atomic_t *fclone_ref;
300
1da177e4 301 skb_release_data(skb);
d179cd12
DM
302 switch (skb->fclone) {
303 case SKB_FCLONE_UNAVAILABLE:
304 kmem_cache_free(skbuff_head_cache, skb);
305 break;
306
307 case SKB_FCLONE_ORIG:
308 fclone_ref = (atomic_t *) (skb + 2);
309 if (atomic_dec_and_test(fclone_ref))
310 kmem_cache_free(skbuff_fclone_cache, skb);
311 break;
312
313 case SKB_FCLONE_CLONE:
314 fclone_ref = (atomic_t *) (skb + 1);
315 other = skb - 1;
316
317 /* The clone portion is available for
318 * fast-cloning again.
319 */
320 skb->fclone = SKB_FCLONE_UNAVAILABLE;
321
322 if (atomic_dec_and_test(fclone_ref))
323 kmem_cache_free(skbuff_fclone_cache, other);
324 break;
325 };
1da177e4
LT
326}
327
328/**
329 * __kfree_skb - private function
330 * @skb: buffer
331 *
332 * Free an sk_buff. Release anything attached to the buffer.
333 * Clean the state. This is an internal helper function. Users should
334 * always call kfree_skb
335 */
336
337void __kfree_skb(struct sk_buff *skb)
338{
1da177e4
LT
339 dst_release(skb->dst);
340#ifdef CONFIG_XFRM
341 secpath_put(skb->sp);
342#endif
9c2b3328
SH
343 if (skb->destructor) {
344 WARN_ON(in_irq());
1da177e4
LT
345 skb->destructor(skb);
346 }
347#ifdef CONFIG_NETFILTER
348 nf_conntrack_put(skb->nfct);
9fb9cbb1
YK
349#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
350 nf_conntrack_put_reasm(skb->nfct_reasm);
351#endif
1da177e4
LT
352#ifdef CONFIG_BRIDGE_NETFILTER
353 nf_bridge_put(skb->nf_bridge);
354#endif
355#endif
356/* XXX: IS this still necessary? - JHS */
357#ifdef CONFIG_NET_SCHED
358 skb->tc_index = 0;
359#ifdef CONFIG_NET_CLS_ACT
360 skb->tc_verd = 0;
1da177e4
LT
361#endif
362#endif
363
364 kfree_skbmem(skb);
365}
366