]>
Commit | Line | Data |
---|---|---|
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 | ||
1da177e4 LT |
41 | #include <linux/module.h> |
42 | #include <linux/types.h> | |
43 | #include <linux/kernel.h> | |
44 | #include <linux/sched.h> | |
45 | #include <linux/mm.h> | |
46 | #include <linux/interrupt.h> | |
47 | #include <linux/in.h> | |
48 | #include <linux/inet.h> | |
49 | #include <linux/slab.h> | |
50 | #include <linux/netdevice.h> | |
51 | #ifdef CONFIG_NET_CLS_ACT | |
52 | #include <net/pkt_sched.h> | |
53 | #endif | |
54 | #include <linux/string.h> | |
55 | #include <linux/skbuff.h> | |
56 | #include <linux/cache.h> | |
57 | #include <linux/rtnetlink.h> | |
58 | #include <linux/init.h> | |
59 | #include <linux/highmem.h> | |
60 | ||
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> | |
69 | ||
ba89966c ED |
70 | static kmem_cache_t *skbuff_head_cache __read_mostly; |
71 | static kmem_cache_t *skbuff_fclone_cache __read_mostly; | |
1da177e4 | 72 | |
06825ba3 IM |
73 | /* |
74 | * lockdep: lock class key used by skb_queue_head_init(): | |
75 | */ | |
76 | struct lock_class_key skb_queue_lock_key; | |
77 | ||
78 | EXPORT_SYMBOL(skb_queue_lock_key); | |
79 | ||
1da177e4 LT |
80 | /* |
81 | * Keep out-of-line to prevent kernel bloat. | |
82 | * __builtin_return_address is not used because it is not always | |
83 | * reliable. | |
84 | */ | |
85 | ||
86 | /** | |
87 | * skb_over_panic - private function | |
88 | * @skb: buffer | |
89 | * @sz: size | |
90 | * @here: address | |
91 | * | |
92 | * Out of line support code for skb_put(). Not user callable. | |
93 | */ | |
94 | void skb_over_panic(struct sk_buff *skb, int sz, void *here) | |
95 | { | |
26095455 PM |
96 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " |
97 | "data:%p tail:%p end:%p dev:%s\n", | |
98 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, | |
99 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
100 | BUG(); |
101 | } | |
102 | ||
103 | /** | |
104 | * skb_under_panic - private function | |
105 | * @skb: buffer | |
106 | * @sz: size | |
107 | * @here: address | |
108 | * | |
109 | * Out of line support code for skb_push(). Not user callable. | |
110 | */ | |
111 | ||
112 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) | |
113 | { | |
26095455 PM |
114 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " |
115 | "data:%p tail:%p end:%p dev:%s\n", | |
116 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, | |
117 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
118 | BUG(); |
119 | } | |
120 | ||
dc6de336 DM |
121 | void skb_truesize_bug(struct sk_buff *skb) |
122 | { | |
123 | printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " | |
124 | "len=%u, sizeof(sk_buff)=%Zd\n", | |
125 | skb->truesize, skb->len, sizeof(struct sk_buff)); | |
126 | } | |
127 | EXPORT_SYMBOL(skb_truesize_bug); | |
128 | ||
1da177e4 LT |
129 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
130 | * 'private' fields and also do memory statistics to find all the | |
131 | * [BEEP] leaks. | |
132 | * | |
133 | */ | |
134 | ||
135 | /** | |
d179cd12 | 136 | * __alloc_skb - allocate a network buffer |
1da177e4 LT |
137 | * @size: size to allocate |
138 | * @gfp_mask: allocation mask | |
c83c2486 RD |
139 | * @fclone: allocate from fclone cache instead of head cache |
140 | * and allocate a cloned (child) skb | |
1da177e4 LT |
141 | * |
142 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
143 | * tail room of size bytes. The object has a reference count of one. | |
144 | * The return is the buffer. On a failure the return is %NULL. | |
145 | * | |
146 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
147 | * %GFP_ATOMIC. | |
148 | */ | |
dd0fc66f | 149 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
d179cd12 | 150 | int fclone) |
1da177e4 | 151 | { |
8798b3fb | 152 | kmem_cache_t *cache; |
4947d3ef | 153 | struct skb_shared_info *shinfo; |
1da177e4 LT |
154 | struct sk_buff *skb; |
155 | u8 *data; | |
156 | ||
8798b3fb HX |
157 | cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; |
158 | ||
1da177e4 | 159 | /* Get the HEAD */ |
8798b3fb | 160 | skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA); |
1da177e4 LT |
161 | if (!skb) |
162 | goto out; | |
163 | ||
164 | /* Get the DATA. Size must match skb_add_mtu(). */ | |
165 | size = SKB_DATA_ALIGN(size); | |
871751e2 | 166 | data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); |
1da177e4 LT |
167 | if (!data) |
168 | goto nodata; | |
169 | ||
170 | memset(skb, 0, offsetof(struct sk_buff, truesize)); | |
171 | skb->truesize = size + sizeof(struct sk_buff); | |
172 | atomic_set(&skb->users, 1); | |
173 | skb->head = data; | |
174 | skb->data = data; | |
175 | skb->tail = data; | |
176 | skb->end = data + size; | |
4947d3ef BL |
177 | /* make sure we initialize shinfo sequentially */ |
178 | shinfo = skb_shinfo(skb); | |
179 | atomic_set(&shinfo->dataref, 1); | |
180 | shinfo->nr_frags = 0; | |
7967168c HX |
181 | shinfo->gso_size = 0; |
182 | shinfo->gso_segs = 0; | |
183 | shinfo->gso_type = 0; | |
4947d3ef BL |
184 | shinfo->ip6_frag_id = 0; |
185 | shinfo->frag_list = NULL; | |
186 | ||
d179cd12 DM |
187 | if (fclone) { |
188 | struct sk_buff *child = skb + 1; | |
189 | atomic_t *fclone_ref = (atomic_t *) (child + 1); | |
1da177e4 | 190 | |
d179cd12 DM |
191 | skb->fclone = SKB_FCLONE_ORIG; |
192 | atomic_set(fclone_ref, 1); | |
193 | ||
194 | child->fclone = SKB_FCLONE_UNAVAILABLE; | |
195 | } | |
1da177e4 LT |
196 | out: |
197 | return skb; | |
198 | nodata: | |
8798b3fb | 199 | kmem_cache_free(cache, skb); |
1da177e4 LT |
200 | skb = NULL; |
201 | goto out; | |
202 | } | |
203 | ||
204 | /** | |
205 | * alloc_skb_from_cache - allocate a network buffer | |
206 | * @cp: kmem_cache from which to allocate the data area | |
207 | * (object size must be big enough for @size bytes + skb overheads) | |
208 | * @size: size to allocate | |
209 | * @gfp_mask: allocation mask | |
210 | * | |
211 | * Allocate a new &sk_buff. The returned buffer has no headroom and | |
212 | * tail room of size bytes. The object has a reference count of one. | |
213 | * The return is the buffer. On a failure the return is %NULL. | |
214 | * | |
215 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
216 | * %GFP_ATOMIC. | |
217 | */ | |
218 | struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp, | |
86a76caf | 219 | unsigned int size, |
dd0fc66f | 220 | gfp_t gfp_mask) |
1da177e4 LT |
221 | { |
222 | struct sk_buff *skb; | |
223 | u8 *data; | |
224 | ||
225 | /* Get the HEAD */ | |
226 | skb = kmem_cache_alloc(skbuff_head_cache, | |
227 | gfp_mask & ~__GFP_DMA); | |
228 | if (!skb) | |
229 | goto out; | |
230 | ||
231 | /* Get the DATA. */ | |
232 | size = SKB_DATA_ALIGN(size); | |
233 | data = kmem_cache_alloc(cp, gfp_mask); | |
234 | if (!data) | |
235 | goto nodata; | |
236 | ||
237 | memset(skb, 0, offsetof(struct sk_buff, truesize)); | |
238 | skb->truesize = size + sizeof(struct sk_buff); | |
239 | atomic_set(&skb->users, 1); | |
240 | skb->head = data; | |
241 | skb->data = data; | |
242 | skb->tail = data; | |
243 | skb->end = data + size; | |
244 | ||
245 | atomic_set(&(skb_shinfo(skb)->dataref), 1); | |
246 | skb_shinfo(skb)->nr_frags = 0; | |
7967168c HX |
247 | skb_shinfo(skb)->gso_size = 0; |
248 | skb_shinfo(skb)->gso_segs = 0; | |
249 | skb_shinfo(skb)->gso_type = 0; | |
1da177e4 LT |
250 | skb_shinfo(skb)->frag_list = NULL; |
251 | out: | |
252 | return skb; | |
253 | nodata: | |
254 | kmem_cache_free(skbuff_head_cache, skb); | |
255 | skb = NULL; | |
256 | goto out; | |
257 | } | |
258 | ||
259 | ||
260 | static void skb_drop_fraglist(struct sk_buff *skb) | |
261 | { | |
262 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
263 | ||
264 | skb_shinfo(skb)->frag_list = NULL; | |
265 | ||
266 | do { | |
267 | struct sk_buff *this = list; | |
268 | list = list->next; | |
269 | kfree_skb(this); | |
270 | } while (list); | |
271 | } | |
272 | ||
273 | static void skb_clone_fraglist(struct sk_buff *skb) | |
274 | { | |
275 | struct sk_buff *list; | |
276 | ||
277 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) | |
278 | skb_get(list); | |
279 | } | |
280 | ||
5bba1712 | 281 | static void skb_release_data(struct sk_buff *skb) |
1da177e4 LT |
282 | { |
283 | if (!skb->cloned || | |
284 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
285 | &skb_shinfo(skb)->dataref)) { | |
286 | if (skb_shinfo(skb)->nr_frags) { | |
287 | int i; | |
288 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
289 | put_page(skb_shinfo(skb)->frags[i].page); | |
290 | } | |
291 | ||
292 | if (skb_shinfo(skb)->frag_list) | |
293 | skb_drop_fraglist(skb); | |
294 | ||
295 | kfree(skb->head); | |
296 | } | |
297 | } | |
298 | ||
299 | /* | |
300 | * Free an skbuff by memory without cleaning the state. | |
301 | */ | |
302 | void kfree_skbmem(struct sk_buff *skb) | |
303 | { | |
d179cd12 DM |
304 | struct sk_buff *other; |
305 | atomic_t *fclone_ref; | |
306 | ||
1da177e4 | 307 | skb_release_data(skb); |
d179cd12 DM |
308 | switch (skb->fclone) { |
309 | case SKB_FCLONE_UNAVAILABLE: | |
310 | kmem_cache_free(skbuff_head_cache, skb); | |
311 | break; | |
312 | ||
313 | case SKB_FCLONE_ORIG: | |
314 | fclone_ref = (atomic_t *) (skb + 2); | |
315 | if (atomic_dec_and_test(fclone_ref)) | |
316 | kmem_cache_free(skbuff_fclone_cache, skb); | |
317 | break; | |
318 | ||
319 | case SKB_FCLONE_CLONE: | |
320 | fclone_ref = (atomic_t *) (skb + 1); | |
321 | other = skb - 1; | |
322 | ||
323 | /* The clone portion is available for | |
324 | * fast-cloning again. | |
325 | */ | |
326 | skb->fclone = SKB_FCLONE_UNAVAILABLE; | |
327 | ||
328 | if (atomic_dec_and_test(fclone_ref)) | |
329 | kmem_cache_free(skbuff_fclone_cache, other); | |
330 | break; | |
331 | }; | |
1da177e4 LT |
332 | } |
333 | ||
334 | /** | |
335 | * __kfree_skb - private function | |
336 | * @skb: buffer | |
337 | * | |
338 | * Free an sk_buff. Release anything attached to the buffer. | |
339 | * Clean the state. This is an internal helper function. Users should | |
340 | * always call kfree_skb | |
341 | */ | |
342 | ||
343 | void __kfree_skb(struct sk_buff *skb) | |
344 | { | |
1da177e4 LT |
345 | dst_release(skb->dst); |
346 | #ifdef CONFIG_XFRM | |
347 | secpath_put(skb->sp); | |
348 | #endif | |
9c2b3328 SH |
349 | if (skb->destructor) { |
350 | WARN_ON(in_irq()); | |
1da177e4 LT |
351 | skb->destructor(skb); |
352 | } | |
353 | #ifdef CONFIG_NETFILTER | |
354 | nf_conntrack_put(skb->nfct); | |
9fb9cbb1 YK |
355 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
356 | nf_conntrack_put_reasm(skb->nfct_reasm); | |
357 | #endif | |
1da177e4 LT |
358 | #ifdef CONFIG_BRIDGE_NETFILTER |
359 | nf_bridge_put(skb->nf_bridge); | |
360 | #endif | |
361 | #endif | |
362 | /* XXX: IS this still necessary? - JHS */ | |
363 | #ifdef CONFIG_NET_SCHED | |
364 | skb->tc_index = 0; | |
365 | #ifdef CONFIG_NET_CLS_ACT | |
366 | skb->tc_verd = 0; | |
1da177e4 LT |
367 | #endif |
368 | #endif | |
369 | ||
370 | kfree_skbmem(skb); | |
371 | } | |
372 | ||