]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Routines having to do with the 'struct sk_buff' memory handlers. | |
3 | * | |
113aa838 | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4 LT |
5 | * Florian La Roche <rzsfl@rz.uni-sb.de> |
6 | * | |
1da177e4 LT |
7 | * 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 | ||
1da177e4 LT |
39 | #include <linux/module.h> |
40 | #include <linux/types.h> | |
41 | #include <linux/kernel.h> | |
fe55f6d5 | 42 | #include <linux/kmemcheck.h> |
1da177e4 LT |
43 | #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> | |
9c55e01c | 54 | #include <linux/splice.h> |
1da177e4 LT |
55 | #include <linux/cache.h> |
56 | #include <linux/rtnetlink.h> | |
57 | #include <linux/init.h> | |
716ea3a7 | 58 | #include <linux/scatterlist.h> |
ac45f602 | 59 | #include <linux/errqueue.h> |
268bb0ce | 60 | #include <linux/prefetch.h> |
1da177e4 LT |
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> | |
ad8d75ff | 70 | #include <trace/events/skb.h> |
1da177e4 | 71 | |
a1f8e7f7 AV |
72 | #include "kmap_skb.h" |
73 | ||
e18b890b CL |
74 | static struct kmem_cache *skbuff_head_cache __read_mostly; |
75 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; | |
1da177e4 | 76 | |
9c55e01c JA |
77 | static void sock_pipe_buf_release(struct pipe_inode_info *pipe, |
78 | struct pipe_buffer *buf) | |
79 | { | |
8b9d3728 | 80 | put_page(buf->page); |
9c55e01c JA |
81 | } |
82 | ||
83 | static void sock_pipe_buf_get(struct pipe_inode_info *pipe, | |
84 | struct pipe_buffer *buf) | |
85 | { | |
8b9d3728 | 86 | get_page(buf->page); |
9c55e01c JA |
87 | } |
88 | ||
89 | static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, | |
90 | struct pipe_buffer *buf) | |
91 | { | |
92 | return 1; | |
93 | } | |
94 | ||
95 | ||
96 | /* Pipe buffer operations for a socket. */ | |
28dfef8f | 97 | static const struct pipe_buf_operations sock_pipe_buf_ops = { |
9c55e01c JA |
98 | .can_merge = 0, |
99 | .map = generic_pipe_buf_map, | |
100 | .unmap = generic_pipe_buf_unmap, | |
101 | .confirm = generic_pipe_buf_confirm, | |
102 | .release = sock_pipe_buf_release, | |
103 | .steal = sock_pipe_buf_steal, | |
104 | .get = sock_pipe_buf_get, | |
105 | }; | |
106 | ||
1da177e4 LT |
107 | /* |
108 | * Keep out-of-line to prevent kernel bloat. | |
109 | * __builtin_return_address is not used because it is not always | |
110 | * reliable. | |
111 | */ | |
112 | ||
113 | /** | |
114 | * skb_over_panic - private function | |
115 | * @skb: buffer | |
116 | * @sz: size | |
117 | * @here: address | |
118 | * | |
119 | * Out of line support code for skb_put(). Not user callable. | |
120 | */ | |
ccb7c773 | 121 | static void skb_over_panic(struct sk_buff *skb, int sz, void *here) |
1da177e4 | 122 | { |
26095455 | 123 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " |
4305b541 | 124 | "data:%p tail:%#lx end:%#lx dev:%s\n", |
27a884dc | 125 | here, skb->len, sz, skb->head, skb->data, |
4305b541 | 126 | (unsigned long)skb->tail, (unsigned long)skb->end, |
26095455 | 127 | skb->dev ? skb->dev->name : "<NULL>"); |
1da177e4 LT |
128 | BUG(); |
129 | } | |
130 | ||
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 | ||
ccb7c773 | 140 | static void skb_under_panic(struct sk_buff *skb, int sz, void *here) |
1da177e4 | 141 | { |
26095455 | 142 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " |
4305b541 | 143 | "data:%p tail:%#lx end:%#lx dev:%s\n", |
27a884dc | 144 | here, skb->len, sz, skb->head, skb->data, |
4305b541 | 145 | (unsigned long)skb->tail, (unsigned long)skb->end, |
26095455 | 146 | skb->dev ? skb->dev->name : "<NULL>"); |
1da177e4 LT |
147 | BUG(); |
148 | } | |
149 | ||
150 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few | |
151 | * 'private' fields and also do memory statistics to find all the | |
152 | * [BEEP] leaks. | |
153 | * | |
154 | */ | |
155 | ||
156 | /** | |
d179cd12 | 157 | * __alloc_skb - allocate a network buffer |
1da177e4 LT |
158 | * @size: size to allocate |
159 | * @gfp_mask: allocation mask | |
c83c2486 RD |
160 | * @fclone: allocate from fclone cache instead of head cache |
161 | * and allocate a cloned (child) skb | |
b30973f8 | 162 | * @node: numa node to allocate memory on |
1da177e4 LT |
163 | * |
164 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
165 | * tail room of size bytes. The object has a reference count of one. | |
166 | * The return is the buffer. On a failure the return is %NULL. | |
167 | * | |
168 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
169 | * %GFP_ATOMIC. | |
170 | */ | |
dd0fc66f | 171 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
b30973f8 | 172 | int fclone, int node) |
1da177e4 | 173 | { |
e18b890b | 174 | struct kmem_cache *cache; |
4947d3ef | 175 | struct skb_shared_info *shinfo; |
1da177e4 LT |
176 | struct sk_buff *skb; |
177 | u8 *data; | |
178 | ||
8798b3fb HX |
179 | cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; |
180 | ||
1da177e4 | 181 | /* Get the HEAD */ |
b30973f8 | 182 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); |
1da177e4 LT |
183 | if (!skb) |
184 | goto out; | |
ec7d2f2c | 185 | prefetchw(skb); |
1da177e4 | 186 | |
87fb4b7b ED |
187 | /* We do our best to align skb_shared_info on a separate cache |
188 | * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives | |
189 | * aligned memory blocks, unless SLUB/SLAB debug is enabled. | |
190 | * Both skb->head and skb_shared_info are cache line aligned. | |
191 | */ | |
bc417e30 | 192 | size = SKB_DATA_ALIGN(size); |
87fb4b7b ED |
193 | size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
194 | data = kmalloc_node_track_caller(size, gfp_mask, node); | |
1da177e4 LT |
195 | if (!data) |
196 | goto nodata; | |
87fb4b7b ED |
197 | /* kmalloc(size) might give us more room than requested. |
198 | * Put skb_shared_info exactly at the end of allocated zone, | |
199 | * to allow max possible filling before reallocation. | |
200 | */ | |
201 | size = SKB_WITH_OVERHEAD(ksize(data)); | |
ec7d2f2c | 202 | prefetchw(data + size); |
1da177e4 | 203 | |
ca0605a7 | 204 | /* |
c8005785 JB |
205 | * Only clear those fields we need to clear, not those that we will |
206 | * actually initialise below. Hence, don't put any more fields after | |
207 | * the tail pointer in struct sk_buff! | |
ca0605a7 ACM |
208 | */ |
209 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
87fb4b7b ED |
210 | /* Account for allocated memory : skb + skb->head */ |
211 | skb->truesize = SKB_TRUESIZE(size); | |
1da177e4 LT |
212 | atomic_set(&skb->users, 1); |
213 | skb->head = data; | |
214 | skb->data = data; | |
27a884dc | 215 | skb_reset_tail_pointer(skb); |
4305b541 | 216 | skb->end = skb->tail + size; |
19633e12 SH |
217 | #ifdef NET_SKBUFF_DATA_USES_OFFSET |
218 | skb->mac_header = ~0U; | |
219 | #endif | |
220 | ||
4947d3ef BL |
221 | /* make sure we initialize shinfo sequentially */ |
222 | shinfo = skb_shinfo(skb); | |
ec7d2f2c | 223 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); |
4947d3ef | 224 | atomic_set(&shinfo->dataref, 1); |
c2aa3665 | 225 | kmemcheck_annotate_variable(shinfo->destructor_arg); |
4947d3ef | 226 | |
d179cd12 DM |
227 | if (fclone) { |
228 | struct sk_buff *child = skb + 1; | |
229 | atomic_t *fclone_ref = (atomic_t *) (child + 1); | |
1da177e4 | 230 | |
fe55f6d5 VN |
231 | kmemcheck_annotate_bitfield(child, flags1); |
232 | kmemcheck_annotate_bitfield(child, flags2); | |
d179cd12 DM |
233 | skb->fclone = SKB_FCLONE_ORIG; |
234 | atomic_set(fclone_ref, 1); | |
235 | ||
236 | child->fclone = SKB_FCLONE_UNAVAILABLE; | |
237 | } | |
1da177e4 LT |
238 | out: |
239 | return skb; | |
240 | nodata: | |
8798b3fb | 241 | kmem_cache_free(cache, skb); |
1da177e4 LT |
242 | skb = NULL; |
243 | goto out; | |
1da177e4 | 244 | } |
b4ac530f | 245 | EXPORT_SYMBOL(__alloc_skb); |
1da177e4 | 246 | |
b2b5ce9d ED |
247 | /** |
248 | * build_skb - build a network buffer | |
249 | * @data: data buffer provided by caller | |
250 | * | |
251 | * Allocate a new &sk_buff. Caller provides space holding head and | |
252 | * skb_shared_info. @data must have been allocated by kmalloc() | |
253 | * The return is the new skb buffer. | |
254 | * On a failure the return is %NULL, and @data is not freed. | |
255 | * Notes : | |
256 | * Before IO, driver allocates only data buffer where NIC put incoming frame | |
257 | * Driver should add room at head (NET_SKB_PAD) and | |
258 | * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) | |
259 | * After IO, driver calls build_skb(), to allocate sk_buff and populate it | |
260 | * before giving packet to stack. | |
261 | * RX rings only contains data buffers, not full skbs. | |
262 | */ | |
263 | struct sk_buff *build_skb(void *data) | |
264 | { | |
265 | struct skb_shared_info *shinfo; | |
266 | struct sk_buff *skb; | |
267 | unsigned int size; | |
268 | ||
269 | skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); | |
270 | if (!skb) | |
271 | return NULL; | |
272 | ||
273 | size = ksize(data) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
274 | ||
275 | memset(skb, 0, offsetof(struct sk_buff, tail)); | |
276 | skb->truesize = SKB_TRUESIZE(size); | |
277 | atomic_set(&skb->users, 1); | |
278 | skb->head = data; | |
279 | skb->data = data; | |
280 | skb_reset_tail_pointer(skb); | |
281 | skb->end = skb->tail + size; | |
282 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | |
283 | skb->mac_header = ~0U; | |
284 | #endif | |
285 | ||
286 | /* make sure we initialize shinfo sequentially */ | |
287 | shinfo = skb_shinfo(skb); | |
288 | memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); | |
289 | atomic_set(&shinfo->dataref, 1); | |
290 | kmemcheck_annotate_variable(shinfo->destructor_arg); | |
291 | ||
292 | return skb; | |
293 | } | |
294 | EXPORT_SYMBOL(build_skb); | |
295 | ||
8af27456 CH |
296 | /** |
297 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device | |
298 | * @dev: network device to receive on | |
299 | * @length: length to allocate | |
300 | * @gfp_mask: get_free_pages mask, passed to alloc_skb | |
301 | * | |
302 | * Allocate a new &sk_buff and assign it a usage count of one. The | |
303 | * buffer has unspecified headroom built in. Users should allocate | |
304 | * the headroom they think they need without accounting for the | |
305 | * built in space. The built in space is used for optimisations. | |
306 | * | |
307 | * %NULL is returned if there is no free memory. | |
308 | */ | |
309 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, | |
310 | unsigned int length, gfp_t gfp_mask) | |
311 | { | |
312 | struct sk_buff *skb; | |
313 | ||
564824b0 | 314 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE); |
7b2e497a | 315 | if (likely(skb)) { |
8af27456 | 316 | skb_reserve(skb, NET_SKB_PAD); |
7b2e497a CH |
317 | skb->dev = dev; |
318 | } | |
8af27456 CH |
319 | return skb; |
320 | } | |
b4ac530f | 321 | EXPORT_SYMBOL(__netdev_alloc_skb); |
1da177e4 | 322 | |
654bed16 PZ |
323 | void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, |
324 | int size) | |
325 | { | |
326 | skb_fill_page_desc(skb, i, page, off, size); | |
327 | skb->len += size; | |
328 | skb->data_len += size; | |
329 | skb->truesize += size; | |
330 | } | |
331 | EXPORT_SYMBOL(skb_add_rx_frag); | |
332 | ||
f58518e6 IJ |
333 | /** |
334 | * dev_alloc_skb - allocate an skbuff for receiving | |
335 | * @length: length to allocate | |
336 | * | |
337 | * Allocate a new &sk_buff and assign it a usage count of one. The | |
338 | * buffer has unspecified headroom built in. Users should allocate | |
339 | * the headroom they think they need without accounting for the | |
340 | * built in space. The built in space is used for optimisations. | |
341 | * | |
342 | * %NULL is returned if there is no free memory. Although this function | |
343 | * allocates memory it can be called from an interrupt. | |
344 | */ | |
345 | struct sk_buff *dev_alloc_skb(unsigned int length) | |
346 | { | |
1483b874 DV |
347 | /* |
348 | * There is more code here than it seems: | |
a0f55e0e | 349 | * __dev_alloc_skb is an inline |
1483b874 | 350 | */ |
f58518e6 IJ |
351 | return __dev_alloc_skb(length, GFP_ATOMIC); |
352 | } | |
353 | EXPORT_SYMBOL(dev_alloc_skb); | |
354 | ||
27b437c8 | 355 | static void skb_drop_list(struct sk_buff **listp) |
1da177e4 | 356 | { |
27b437c8 | 357 | struct sk_buff *list = *listp; |
1da177e4 | 358 | |
27b437c8 | 359 | *listp = NULL; |
1da177e4 LT |
360 | |
361 | do { | |
362 | struct sk_buff *this = list; | |
363 | list = list->next; | |
364 | kfree_skb(this); | |
365 | } while (list); | |
366 | } | |
367 | ||
27b437c8 HX |
368 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
369 | { | |
370 | skb_drop_list(&skb_shinfo(skb)->frag_list); | |
371 | } | |
372 | ||
1da177e4 LT |
373 | static void skb_clone_fraglist(struct sk_buff *skb) |
374 | { | |
375 | struct sk_buff *list; | |
376 | ||
fbb398a8 | 377 | skb_walk_frags(skb, list) |
1da177e4 LT |
378 | skb_get(list); |
379 | } | |
380 | ||
5bba1712 | 381 | static void skb_release_data(struct sk_buff *skb) |
1da177e4 LT |
382 | { |
383 | if (!skb->cloned || | |
384 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
385 | &skb_shinfo(skb)->dataref)) { | |
386 | if (skb_shinfo(skb)->nr_frags) { | |
387 | int i; | |
388 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
ea2ab693 | 389 | skb_frag_unref(skb, i); |
1da177e4 LT |
390 | } |
391 | ||
a6686f2f SM |
392 | /* |
393 | * If skb buf is from userspace, we need to notify the caller | |
394 | * the lower device DMA has done; | |
395 | */ | |
396 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { | |
397 | struct ubuf_info *uarg; | |
398 | ||
399 | uarg = skb_shinfo(skb)->destructor_arg; | |
400 | if (uarg->callback) | |
401 | uarg->callback(uarg); | |
402 | } | |
403 | ||
21dc3301 | 404 | if (skb_has_frag_list(skb)) |
1da177e4 LT |
405 | skb_drop_fraglist(skb); |
406 | ||
407 | kfree(skb->head); | |
408 | } | |
409 | } | |
410 | ||
411 | /* | |
412 | * Free an skbuff by memory without cleaning the state. | |
413 | */ | |
2d4baff8 | 414 | static void kfree_skbmem(struct sk_buff *skb) |
1da177e4 | 415 | { |
d179cd12 DM |
416 | struct sk_buff *other; |
417 | atomic_t *fclone_ref; | |
418 | ||
d179cd12 DM |
419 | switch (skb->fclone) { |
420 | case SKB_FCLONE_UNAVAILABLE: | |
421 | kmem_cache_free(skbuff_head_cache, skb); | |
422 | break; | |
423 | ||
424 | case SKB_FCLONE_ORIG: | |
425 | fclone_ref = (atomic_t *) (skb + 2); | |
426 | if (atomic_dec_and_test(fclone_ref)) | |
427 | kmem_cache_free(skbuff_fclone_cache, skb); | |
428 | break; | |
429 | ||
430 | case SKB_FCLONE_CLONE: | |
431 | fclone_ref = (atomic_t *) (skb + 1); | |
432 | other = skb - 1; | |
433 | ||
434 | /* The clone portion is available for | |
435 | * fast-cloning again. | |
436 | */ | |
437 | skb->fclone = SKB_FCLONE_UNAVAILABLE; | |
438 | ||
439 | if (atomic_dec_and_test(fclone_ref)) | |
440 | kmem_cache_free(skbuff_fclone_cache, other); | |
441 | break; | |
3ff50b79 | 442 | } |
1da177e4 LT |
443 | } |
444 | ||
04a4bb55 | 445 | static void skb_release_head_state(struct sk_buff *skb) |
1da177e4 | 446 | { |
adf30907 | 447 | skb_dst_drop(skb); |
1da177e4 LT |
448 | #ifdef CONFIG_XFRM |
449 | secpath_put(skb->sp); | |
450 | #endif | |
9c2b3328 SH |
451 | if (skb->destructor) { |
452 | WARN_ON(in_irq()); | |
1da177e4 LT |
453 | skb->destructor(skb); |
454 | } | |
a3bf7ae9 | 455 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
5f79e0f9 | 456 | nf_conntrack_put(skb->nfct); |
2fc72c7b KK |
457 | #endif |
458 | #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED | |
9fb9cbb1 YK |
459 | nf_conntrack_put_reasm(skb->nfct_reasm); |
460 | #endif | |
1da177e4 LT |
461 | #ifdef CONFIG_BRIDGE_NETFILTER |
462 | nf_bridge_put(skb->nf_bridge); | |
463 | #endif | |
1da177e4 LT |
464 | /* XXX: IS this still necessary? - JHS */ |
465 | #ifdef CONFIG_NET_SCHED | |
466 | skb->tc_index = 0; | |
467 | #ifdef CONFIG_NET_CLS_ACT | |
468 | skb->tc_verd = 0; | |
1da177e4 LT |
469 | #endif |
470 | #endif | |
04a4bb55 LB |
471 | } |
472 | ||
473 | /* Free everything but the sk_buff shell. */ | |
474 | static void skb_release_all(struct sk_buff *skb) | |
475 | { | |
476 | skb_release_head_state(skb); | |
2d4baff8 HX |
477 | skb_release_data(skb); |
478 | } | |
479 | ||
480 | /** | |
481 | * __kfree_skb - private function | |
482 | * @skb: buffer | |
483 | * | |
484 | * Free an sk_buff. Release anything attached to the buffer. | |
485 | * Clean the state. This is an internal helper function. Users should | |
486 | * always call kfree_skb | |
487 | */ | |
1da177e4 | 488 | |
2d4baff8 HX |
489 | void __kfree_skb(struct sk_buff *skb) |
490 | { | |
491 | skb_release_all(skb); | |
1da177e4 LT |
492 | kfree_skbmem(skb); |
493 | } | |
b4ac530f | 494 | EXPORT_SYMBOL(__kfree_skb); |
1da177e4 | 495 | |