]>
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 | ||
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 | ||
71 | static kmem_cache_t *skbuff_head_cache; | |
72 | ||
73 | /* | |
74 | * Keep out-of-line to prevent kernel bloat. | |
75 | * __builtin_return_address is not used because it is not always | |
76 | * reliable. | |
77 | */ | |
78 | ||
79 | /** | |
80 | * skb_over_panic - private function | |
81 | * @skb: buffer | |
82 | * @sz: size | |
83 | * @here: address | |
84 | * | |
85 | * Out of line support code for skb_put(). Not user callable. | |
86 | */ | |
87 | void skb_over_panic(struct sk_buff *skb, int sz, void *here) | |
88 | { | |
26095455 PM |
89 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " |
90 | "data:%p tail:%p end:%p dev:%s\n", | |
91 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, | |
92 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
93 | BUG(); |
94 | } | |
95 | ||
96 | /** | |
97 | * skb_under_panic - private function | |
98 | * @skb: buffer | |
99 | * @sz: size | |
100 | * @here: address | |
101 | * | |
102 | * Out of line support code for skb_push(). Not user callable. | |
103 | */ | |
104 | ||
105 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) | |
106 | { | |
26095455 PM |
107 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " |
108 | "data:%p tail:%p end:%p dev:%s\n", | |
109 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, | |
110 | skb->dev ? skb->dev->name : "<NULL>"); | |
1da177e4 LT |
111 | BUG(); |
112 | } | |
113 | ||
114 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few | |
115 | * 'private' fields and also do memory statistics to find all the | |
116 | * [BEEP] leaks. | |
117 | * | |
118 | */ | |
119 | ||
120 | /** | |
121 | * alloc_skb - allocate a network buffer | |
122 | * @size: size to allocate | |
123 | * @gfp_mask: allocation mask | |
124 | * | |
125 | * Allocate a new &sk_buff. The returned buffer has no headroom and a | |
126 | * tail room of size bytes. The object has a reference count of one. | |
127 | * The return is the buffer. On a failure the return is %NULL. | |
128 | * | |
129 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
130 | * %GFP_ATOMIC. | |
131 | */ | |
86a76caf | 132 | struct sk_buff *alloc_skb(unsigned int size, unsigned int __nocast gfp_mask) |
1da177e4 LT |
133 | { |
134 | struct sk_buff *skb; | |
135 | u8 *data; | |
136 | ||
137 | /* Get the HEAD */ | |
138 | skb = kmem_cache_alloc(skbuff_head_cache, | |
139 | gfp_mask & ~__GFP_DMA); | |
140 | if (!skb) | |
141 | goto out; | |
142 | ||
143 | /* Get the DATA. Size must match skb_add_mtu(). */ | |
144 | size = SKB_DATA_ALIGN(size); | |
145 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); | |
146 | if (!data) | |
147 | goto nodata; | |
148 | ||
149 | memset(skb, 0, offsetof(struct sk_buff, truesize)); | |
150 | skb->truesize = size + sizeof(struct sk_buff); | |
151 | atomic_set(&skb->users, 1); | |
152 | skb->head = data; | |
153 | skb->data = data; | |
154 | skb->tail = data; | |
155 | skb->end = data + size; | |
156 | ||
157 | atomic_set(&(skb_shinfo(skb)->dataref), 1); | |
158 | skb_shinfo(skb)->nr_frags = 0; | |
159 | skb_shinfo(skb)->tso_size = 0; | |
160 | skb_shinfo(skb)->tso_segs = 0; | |
161 | skb_shinfo(skb)->frag_list = NULL; | |
162 | out: | |
163 | return skb; | |
164 | nodata: | |
165 | kmem_cache_free(skbuff_head_cache, skb); | |
166 | skb = NULL; | |
167 | goto out; | |
168 | } | |
169 | ||
170 | /** | |
171 | * alloc_skb_from_cache - allocate a network buffer | |
172 | * @cp: kmem_cache from which to allocate the data area | |
173 | * (object size must be big enough for @size bytes + skb overheads) | |
174 | * @size: size to allocate | |
175 | * @gfp_mask: allocation mask | |
176 | * | |
177 | * Allocate a new &sk_buff. The returned buffer has no headroom and | |
178 | * tail room of size bytes. The object has a reference count of one. | |
179 | * The return is the buffer. On a failure the return is %NULL. | |
180 | * | |
181 | * Buffers may only be allocated from interrupts using a @gfp_mask of | |
182 | * %GFP_ATOMIC. | |
183 | */ | |
184 | struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp, | |
86a76caf VF |
185 | unsigned int size, |
186 | unsigned int __nocast gfp_mask) | |
1da177e4 LT |
187 | { |
188 | struct sk_buff *skb; | |
189 | u8 *data; | |
190 | ||
191 | /* Get the HEAD */ | |
192 | skb = kmem_cache_alloc(skbuff_head_cache, | |
193 | gfp_mask & ~__GFP_DMA); | |
194 | if (!skb) | |
195 | goto out; | |
196 | ||
197 | /* Get the DATA. */ | |
198 | size = SKB_DATA_ALIGN(size); | |
199 | data = kmem_cache_alloc(cp, gfp_mask); | |
200 | if (!data) | |
201 | goto nodata; | |
202 | ||
203 | memset(skb, 0, offsetof(struct sk_buff, truesize)); | |
204 | skb->truesize = size + sizeof(struct sk_buff); | |
205 | atomic_set(&skb->users, 1); | |
206 | skb->head = data; | |
207 | skb->data = data; | |
208 | skb->tail = data; | |
209 | skb->end = data + size; | |
210 | ||
211 | atomic_set(&(skb_shinfo(skb)->dataref), 1); | |
212 | skb_shinfo(skb)->nr_frags = 0; | |
213 | skb_shinfo(skb)->tso_size = 0; | |
214 | skb_shinfo(skb)->tso_segs = 0; | |
215 | skb_shinfo(skb)->frag_list = NULL; | |
216 | out: | |
217 | return skb; | |
218 | nodata: | |
219 | kmem_cache_free(skbuff_head_cache, skb); | |
220 | skb = NULL; | |
221 | goto out; | |
222 | } | |
223 | ||
224 | ||
225 | static void skb_drop_fraglist(struct sk_buff *skb) | |
226 | { | |
227 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
228 | ||
229 | skb_shinfo(skb)->frag_list = NULL; | |
230 | ||
231 | do { | |
232 | struct sk_buff *this = list; | |
233 | list = list->next; | |
234 | kfree_skb(this); | |
235 | } while (list); | |
236 | } | |
237 | ||
238 | static void skb_clone_fraglist(struct sk_buff *skb) | |
239 | { | |
240 | struct sk_buff *list; | |
241 | ||
242 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) | |
243 | skb_get(list); | |
244 | } | |
245 | ||
246 | void skb_release_data(struct sk_buff *skb) | |
247 | { | |
248 | if (!skb->cloned || | |
249 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | |
250 | &skb_shinfo(skb)->dataref)) { | |
251 | if (skb_shinfo(skb)->nr_frags) { | |
252 | int i; | |
253 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
254 | put_page(skb_shinfo(skb)->frags[i].page); | |
255 | } | |
256 | ||
257 | if (skb_shinfo(skb)->frag_list) | |
258 | skb_drop_fraglist(skb); | |
259 | ||
260 | kfree(skb->head); | |
261 | } | |
262 | } | |
263 | ||
264 | /* | |
265 | * Free an skbuff by memory without cleaning the state. | |
266 | */ | |
267 | void kfree_skbmem(struct sk_buff *skb) | |
268 | { | |
269 | skb_release_data(skb); | |
270 | kmem_cache_free(skbuff_head_cache, skb); | |
271 | } | |
272 | ||
273 | /** | |
274 | * __kfree_skb - private function | |
275 | * @skb: buffer | |
276 | * | |
277 | * Free an sk_buff. Release anything attached to the buffer. | |
278 | * Clean the state. This is an internal helper function. Users should | |
279 | * always call kfree_skb | |
280 | */ | |
281 | ||
282 | void __kfree_skb(struct sk_buff *skb) | |
283 | { | |
9c2b3328 | 284 | BUG_ON(skb->list != NULL); |
1da177e4 LT |
285 | |
286 | dst_release(skb->dst); | |
287 | #ifdef CONFIG_XFRM | |
288 | secpath_put(skb->sp); | |
289 | #endif | |
9c2b3328 SH |
290 | if (skb->destructor) { |
291 | WARN_ON(in_irq()); | |
1da177e4 LT |
292 | skb->destructor(skb); |
293 | } | |
294 | #ifdef CONFIG_NETFILTER | |
295 | nf_conntrack_put(skb->nfct); | |
296 | #ifdef CONFIG_BRIDGE_NETFILTER | |
297 | nf_bridge_put(skb->nf_bridge); | |
298 | #endif | |
299 | #endif | |
300 | /* XXX: IS this still necessary? - JHS */ | |
301 | #ifdef CONFIG_NET_SCHED | |
302 | skb->tc_index = 0; | |
303 | #ifdef CONFIG_NET_CLS_ACT | |
304 | skb->tc_verd = 0; | |
305 | skb->tc_classid = 0; | |
306 | #endif | |
307 | #endif | |
308 | ||
309 | kfree_skbmem(skb); | |
310 | } | |
311 | ||
312 | /** | |
313 | * skb_clone - duplicate an sk_buff | |
314 | * @skb: buffer to clone | |
315 | * @gfp_mask: allocation priority | |
316 | * | |
317 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both | |
318 | * copies share the same packet data but not structure. The new | |
319 | * buffer has a reference count of 1. If the allocation fails the | |
320 | * function returns %NULL otherwise the new buffer is returned. | |
321 | * | |
322 | * If this function is called from an interrupt gfp_mask() must be | |
323 | * %GFP_ATOMIC. | |
324 | */ | |
325 | ||
86a76caf | 326 | struct sk_buff *skb_clone(struct sk_buff *skb, unsigned int __nocast gfp_mask) |
1da177e4 LT |
327 | { |
328 | struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); | |
329 | ||
330 | if (!n) | |
331 | return NULL; | |
332 | ||
333 | #define C(x) n->x = skb->x | |
334 | ||
335 | n->next = n->prev = NULL; | |
336 | n->list = NULL; | |
337 | n->sk = NULL; | |
338 | C(stamp); | |
339 | C(dev); | |
340 | C(real_dev); | |
341 | C(h); | |
342 | C(nh); | |
343 | C(mac); | |
344 | C(dst); | |
345 | dst_clone(skb->dst); | |
346 | C(sp); | |
347 | #ifdef CONFIG_INET | |
348 | secpath_get(skb->sp); | |
349 | #endif | |
350 | memcpy(n->cb, skb->cb, sizeof(skb->cb)); | |
351 | C(len); | |
352 | C(data_len); | |
353 | C(csum); | |
354 | C(local_df); | |
355 | n->cloned = 1; | |
356 | n->nohdr = 0; | |
357 | C(pkt_type); | |
358 | C(ip_summed); | |
359 | C(priority); | |
360 | C(protocol); | |
1da177e4 LT |
361 | n->destructor = NULL; |
362 | #ifdef CONFIG_NETFILTER | |
363 | C(nfmark); | |
1da177e4 LT |
364 | C(nfct); |
365 | nf_conntrack_get(skb->nfct); | |
366 | C(nfctinfo); | |
1da177e4 LT |
367 | #ifdef CONFIG_BRIDGE_NETFILTER |
368 | C(nf_bridge); | |
369 | nf_bridge_get(skb->nf_bridge); | |
370 | #endif | |
371 | #endif /*CONFIG_NETFILTER*/ | |
372 | #if defined(CONFIG_HIPPI) | |
373 | C(private); | |
374 | #endif | |
375 | #ifdef CONFIG_NET_SCHED | |
376 | C(tc_index); | |
377 | #ifdef CONFIG_NET_CLS_ACT | |
378 | n->tc_verd = SET_TC_VERD(skb->tc_verd,0); | |
b72f6ecc DM |
379 | n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd); |
380 | n->tc_verd = CLR_TC_MUNGED(n->tc_verd); | |
1da177e4 LT |
381 | C(input_dev); |
382 | C(tc_classid); | |
383 | #endif | |
384 | ||
385 | #endif | |
386 | C(truesize); | |
387 | atomic_set(&n->users, 1); | |
388 | C(head); | |
389 | C(data); | |
390 | C(tail); | |
391 | C(end); | |
392 | ||
393 | atomic_inc(&(skb_shinfo(skb)->dataref)); | |
394 | skb->cloned = 1; | |
395 | ||
396 | return n; | |
397 | } | |
398 | ||
399 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) | |
400 | { | |
401 | /* | |
402 | * Shift between the two data areas in bytes | |
403 | */ | |
404 | unsigned long offset = new->data - old->data; | |
405 | ||
406 | new->list = NULL; | |
407 | new->sk = NULL; | |
408 | new->dev = old->dev; | |
409 | new->real_dev = old->real_dev; | |
410 | new->priority = old->priority; | |
411 | new->protocol = old->protocol; | |
412 | new->dst = dst_clone(old->dst); | |
413 | #ifdef CONFIG_INET | |
414 | new->sp = secpath_get(old->sp); | |
415 | #endif | |
416 | new->h.raw = old->h.raw + offset; | |
417 | new->nh.raw = old->nh.raw + offset; | |
418 | new->mac.raw = old->mac.raw + offset; | |
419 | memcpy(new->cb, old->cb, sizeof(old->cb)); | |
420 | new->local_df = old->local_df; | |
421 | new->pkt_type = old->pkt_type; | |
422 | new->stamp = old->stamp; | |
423 | new->destructor = NULL; | |
1da177e4 LT |
424 | #ifdef CONFIG_NETFILTER |
425 | new->nfmark = old->nfmark; | |
1da177e4 LT |
426 | new->nfct = old->nfct; |
427 | nf_conntrack_get(old->nfct); | |
428 | new->nfctinfo = old->nfctinfo; | |
1da177e4 LT |
429 | #ifdef CONFIG_BRIDGE_NETFILTER |
430 | new->nf_bridge = old->nf_bridge; | |
431 | nf_bridge_get(old->nf_bridge); | |
432 | #endif | |
433 | #endif | |
434 | #ifdef CONFIG_NET_SCHED | |
435 | #ifdef CONFIG_NET_CLS_ACT | |
436 | new->tc_verd = old->tc_verd; | |
437 | #endif | |
438 | new->tc_index = old->tc_index; | |
439 | #endif | |
440 | atomic_set(&new->users, 1); | |
441 | skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size; | |
442 | skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs; | |
443 | } | |
444 | ||
445 | /** | |
446 | * skb_copy - create private copy of an sk_buff | |
447 | * @skb: buffer to copy | |
448 | * @gfp_mask: allocation priority | |
449 | * | |
450 | * Make a copy of both an &sk_buff and its data. This is used when the | |
451 | * caller wishes to modify the data and needs a private copy of the | |
452 | * data to alter. Returns %NULL on failure or the pointer to the buffer | |
453 | * on success. The returned buffer has a reference count of 1. | |
454 | * | |
455 | * As by-product this function converts non-linear &sk_buff to linear | |
456 | * one, so that &sk_buff becomes completely private and caller is allowed | |
457 | * to modify all the data of returned buffer. This means that this | |
458 | * function is not recommended for use in circumstances when only | |
459 | * header is going to be modified. Use pskb_copy() instead. | |
460 | */ | |
461 | ||
86a76caf | 462 | struct sk_buff *skb_copy(const struct sk_buff *skb, unsigned int __nocast gfp_mask) |
1da177e4 LT |
463 | { |
464 | int headerlen = skb->data - skb->head; | |
465 | /* | |
466 | * Allocate the copy buffer | |
467 | */ | |
468 | struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len, | |
469 | gfp_mask); | |
470 | if (!n) | |
471 | return NULL; | |
472 | ||
473 | /* Set the data pointer */ | |
474 | skb_reserve(n, headerlen); | |
475 | /* Set the tail pointer and length */ | |
476 | skb_put(n, skb->len); | |
477 | n->csum = skb->csum; | |
478 | n->ip_summed = skb->ip_summed; | |
479 | ||
480 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) | |
481 | BUG(); | |
482 | ||
483 | copy_skb_header(n, skb); | |
484 | return n; | |
485 | } | |
486 | ||
487 | ||
488 | /** | |
489 | * pskb_copy - create copy of an sk_buff with private head. | |
490 | * @skb: buffer to copy | |
491 | * @gfp_mask: allocation priority | |
492 | * | |
493 | * Make a copy of both an &sk_buff and part of its data, located | |
494 | * in header. Fragmented data remain shared. This is used when | |
495 | * the caller wishes to modify only header of &sk_buff and needs | |
496 | * private copy of the header to alter. Returns %NULL on failure | |
497 | * or the pointer to the buffer on success. | |
498 | * The returned buffer has a reference count of 1. | |
499 | */ | |
500 | ||
86a76caf | 501 | struct sk_buff *pskb_copy(struct sk_buff *skb, unsigned int __nocast gfp_mask) |
1da177e4 LT |
502 | { |
503 | /* | |
504 | * Allocate the copy buffer | |
505 | */ | |
506 | struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask); | |
507 | ||
508 | if (!n) | |
509 | goto out; | |
510 | ||
511 | /* Set the data pointer */ | |
512 | skb_reserve(n, skb->data - skb->head); | |
513 | /* Set the tail pointer and length */ | |
514 | skb_put(n, skb_headlen(skb)); | |
515 | /* Copy the bytes */ | |
516 | memcpy(n->data, skb->data, n->len); | |
517 | n->csum = skb->csum; | |
518 | n->ip_summed = skb->ip_summed; | |
519 | ||
520 | n->data_len = skb->data_len; | |
521 | n->len = skb->len; | |
522 | ||
523 | if (skb_shinfo(skb)->nr_frags) { | |
524 | int i; | |
525 | ||
526 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
527 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; | |
528 | get_page(skb_shinfo(n)->frags[i].page); | |
529 | } | |
530 | skb_shinfo(n)->nr_frags = i; | |
531 | } | |
532 | ||
533 | if (skb_shinfo(skb)->frag_list) { | |
534 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; | |
535 | skb_clone_fraglist(n); | |
536 | } | |
537 | ||
538 | copy_skb_header(n, skb); | |
539 | out: | |
540 | return n; | |
541 | } | |
542 | ||
543 | /** | |
544 | * pskb_expand_head - reallocate header of &sk_buff | |
545 | * @skb: buffer to reallocate | |
546 | * @nhead: room to add at head | |
547 | * @ntail: room to add at tail | |
548 | * @gfp_mask: allocation priority | |
549 | * | |
550 | * Expands (or creates identical copy, if &nhead and &ntail are zero) | |
551 | * header of skb. &sk_buff itself is not changed. &sk_buff MUST have | |
552 | * reference count of 1. Returns zero in the case of success or error, | |
553 | * if expansion failed. In the last case, &sk_buff is not changed. | |
554 | * | |
555 | * All the pointers pointing into skb header may change and must be | |
556 | * reloaded after call to this function. | |
557 | */ | |
558 | ||
86a76caf VF |
559 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
560 | unsigned int __nocast gfp_mask) | |
1da177e4 LT |
561 | { |
562 | int i; | |
563 | u8 *data; | |
564 | int size = nhead + (skb->end - skb->head) + ntail; | |
565 | long off; | |
566 | ||
567 | if (skb_shared(skb)) | |
568 | BUG(); | |
569 | ||
570 | size = SKB_DATA_ALIGN(size); | |
571 | ||
572 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); | |
573 | if (!data) | |
574 | goto nodata; | |
575 | ||
576 | /* Copy only real data... and, alas, header. This should be | |
577 | * optimized for the cases when header is void. */ | |
578 | memcpy(data + nhead, skb->head, skb->tail - skb->head); | |
579 | memcpy(data + size, skb->end, sizeof(struct skb_shared_info)); | |
580 | ||
581 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
582 | get_page(skb_shinfo(skb)->frags[i].page); | |
583 | ||
584 | if (skb_shinfo(skb)->frag_list) | |
585 | skb_clone_fraglist(skb); | |
586 | ||
587 | skb_release_data(skb); | |
588 | ||
589 | off = (data + nhead) - skb->head; | |
590 | ||
591 | skb->head = data; | |
592 | skb->end = data + size; | |
593 | skb->data += off; | |
594 | skb->tail += off; | |
595 | skb->mac.raw += off; | |
596 | skb->h.raw += off; | |
597 | skb->nh.raw += off; | |
598 | skb->cloned = 0; | |
599 | skb->nohdr = 0; | |
600 | atomic_set(&skb_shinfo(skb)->dataref, 1); | |
601 | return 0; | |
602 | ||
603 | nodata: | |
604 | return -ENOMEM; | |
605 | } | |
606 | ||
607 | /* Make private copy of skb with writable head and some headroom */ | |
608 | ||
609 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) | |
610 | { | |
611 | struct sk_buff *skb2; | |
612 | int delta = headroom - skb_headroom(skb); | |
613 | ||
614 | if (delta <= 0) | |
615 | skb2 = pskb_copy(skb, GFP_ATOMIC); | |
616 | else { | |
617 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
618 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, | |
619 | GFP_ATOMIC)) { | |
620 | kfree_skb(skb2); | |
621 | skb2 = NULL; | |
622 | } | |
623 | } | |
624 | return skb2; | |
625 | } | |
626 | ||
627 | ||
628 | /** | |
629 | * skb_copy_expand - copy and expand sk_buff | |
630 | * @skb: buffer to copy | |
631 | * @newheadroom: new free bytes at head | |
632 | * @newtailroom: new free bytes at tail | |
633 | * @gfp_mask: allocation priority | |
634 | * | |
635 | * Make a copy of both an &sk_buff and its data and while doing so | |
636 | * allocate additional space. | |
637 | * | |
638 | * This is used when the caller wishes to modify the data and needs a | |
639 | * private copy of the data to alter as well as more space for new fields. | |
640 | * Returns %NULL on failure or the pointer to the buffer | |
641 | * on success. The returned buffer has a reference count of 1. | |
642 | * | |
643 | * You must pass %GFP_ATOMIC as the allocation priority if this function | |
644 | * is called from an interrupt. | |
645 | * | |
646 | * BUG ALERT: ip_summed is not copied. Why does this work? Is it used | |
647 | * only by netfilter in the cases when checksum is recalculated? --ANK | |
648 | */ | |
649 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, | |
86a76caf VF |
650 | int newheadroom, int newtailroom, |
651 | unsigned int __nocast gfp_mask) | |
1da177e4 LT |
652 | { |
653 | /* | |
654 | * Allocate the copy buffer | |
655 | */ | |
656 | struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, | |
657 | gfp_mask); | |
658 | int head_copy_len, head_copy_off; | |
659 | ||
660 | if (!n) | |
661 | return NULL; | |
662 | ||
663 | skb_reserve(n, newheadroom); | |
664 | ||
665 | /* Set the tail pointer and length */ | |
666 | skb_put(n, skb->len); | |
667 | ||
668 | head_copy_len = skb_headroom(skb); | |
669 | head_copy_off = 0; | |
670 | if (newheadroom <= head_copy_len) | |
671 | head_copy_len = newheadroom; | |
672 | else | |
673 | head_copy_off = newheadroom - head_copy_len; | |
674 | ||
675 | /* Copy the linear header and data. */ | |
676 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, | |
677 | skb->len + head_copy_len)) | |
678 | BUG(); | |
679 | ||
680 | copy_skb_header(n, skb); | |
681 | ||
682 | return n; | |
683 | } | |
684 | ||
685 | /** | |
686 | * skb_pad - zero pad the tail of an skb | |
687 | * @skb: buffer to pad | |
688 | * @pad: space to pad | |
689 | * | |
690 | * Ensure that a buffer is followed by a padding area that is zero | |
691 | * filled. Used by network drivers which may DMA or transfer data | |
692 | * beyond the buffer end onto the wire. | |
693 | * | |
694 | * May return NULL in out of memory cases. | |
695 | */ | |
696 | ||
697 | struct sk_buff *skb_pad(struct sk_buff *skb, int pad) | |
698 | { | |
699 | struct sk_buff *nskb; | |
700 | ||
701 | /* If the skbuff is non linear tailroom is always zero.. */ | |
702 | if (skb_tailroom(skb) >= pad) { | |
703 | memset(skb->data+skb->len, 0, pad); | |
704 | return skb; | |
705 | } | |
706 | ||
707 | nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC); | |
708 | kfree_skb(skb); | |
709 | if (nskb) | |
710 | memset(nskb->data+nskb->len, 0, pad); | |
711 | return nskb; | |
712 | } | |
713 | ||
714 | /* Trims skb to length len. It can change skb pointers, if "realloc" is 1. | |
715 | * If realloc==0 and trimming is impossible without change of data, | |
716 | * it is BUG(). | |
717 | */ | |
718 | ||
719 | int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc) | |
720 | { | |
721 | int offset = skb_headlen(skb); | |
722 | int nfrags = skb_shinfo(skb)->nr_frags; | |
723 | int i; | |
724 | ||
725 | for (i = 0; i < nfrags; i++) { | |
726 | int end = offset + skb_shinfo(skb)->frags[i].size; | |
727 | if (end > len) { | |
728 | if (skb_cloned(skb)) { | |
729 | if (!realloc) | |
730 | BUG(); | |
731 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) | |
732 | return -ENOMEM; | |
733 | } | |
734 | if (len <= offset) { | |
735 | put_page(skb_shinfo(skb)->frags[i].page); | |
736 | skb_shinfo(skb)->nr_frags--; | |
737 | } else { | |
738 | skb_shinfo(skb)->frags[i].size = len - offset; | |
739 | } | |
740 | } | |
741 | offset = end; | |
742 | } | |
743 | ||
744 | if (offset < len) { | |
745 | skb->data_len -= skb->len - len; | |
746 | skb->len = len; | |
747 | } else { | |
748 | if (len <= skb_headlen(skb)) { | |
749 | skb->len = len; | |
750 | skb->data_len = 0; | |
751 | skb->tail = skb->data + len; | |
752 | if (skb_shinfo(skb)->frag_list && !skb_cloned(skb)) | |
753 | skb_drop_fraglist(skb); | |
754 | } else { | |
755 | skb->data_len -= skb->len - len; | |
756 | skb->len = len; | |
757 | } | |
758 | } | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
763 | /** | |
764 | * __pskb_pull_tail - advance tail of skb header | |
765 | * @skb: buffer to reallocate | |
766 | * @delta: number of bytes to advance tail | |
767 | * | |
768 | * The function makes a sense only on a fragmented &sk_buff, | |
769 | * it expands header moving its tail forward and copying necessary | |
770 | * data from fragmented part. | |
771 | * | |
772 | * &sk_buff MUST have reference count of 1. | |
773 | * | |
774 | * Returns %NULL (and &sk_buff does not change) if pull failed | |
775 | * or value of new tail of skb in the case of success. | |
776 | * | |
777 | * All the pointers pointing into skb header may change and must be | |
778 | * reloaded after call to this function. | |
779 | */ | |
780 | ||
781 | /* Moves tail of skb head forward, copying data from fragmented part, | |
782 | * when it is necessary. | |
783 | * 1. It may fail due to malloc failure. | |
784 | * 2. It may change skb pointers. | |
785 | * | |
786 | * It is pretty complicated. Luckily, it is called only in exceptional cases. | |
787 | */ | |
788 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) | |
789 | { | |
790 | /* If skb has not enough free space at tail, get new one | |
791 | * plus 128 bytes for future expansions. If we have enough | |
792 | * room at tail, reallocate without expansion only if skb is cloned. | |
793 | */ | |
794 | int i, k, eat = (skb->tail + delta) - skb->end; | |
795 | ||
796 | if (eat > 0 || skb_cloned(skb)) { | |
797 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, | |
798 | GFP_ATOMIC)) | |
799 | return NULL; | |
800 | } | |
801 | ||
802 | if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta)) | |
803 | BUG(); | |
804 | ||
805 | /* Optimization: no fragments, no reasons to preestimate | |
806 | * size of pulled pages. Superb. | |
807 | */ | |
808 | if (!skb_shinfo(skb)->frag_list) | |
809 | goto pull_pages; | |
810 | ||
811 | /* Estimate size of pulled pages. */ | |
812 | eat = delta; | |
813 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
814 | if (skb_shinfo(skb)->frags[i].size >= eat) | |
815 | goto pull_pages; | |
816 | eat -= skb_shinfo(skb)->frags[i].size; | |
817 | } | |
818 | ||
819 | /* If we need update frag list, we are in troubles. | |
820 | * Certainly, it possible to add an offset to skb data, | |
821 | * but taking into account that pulling is expected to | |
822 | * be very rare operation, it is worth to fight against | |
823 | * further bloating skb head and crucify ourselves here instead. | |
824 | * Pure masohism, indeed. 8)8) | |
825 | */ | |
826 | if (eat) { | |
827 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
828 | struct sk_buff *clone = NULL; | |
829 | struct sk_buff *insp = NULL; | |
830 | ||
831 | do { | |
832 | if (!list) | |
833 | BUG(); | |
834 | ||
835 | if (list->len <= eat) { | |
836 | /* Eaten as whole. */ | |
837 | eat -= list->len; | |
838 | list = list->next; | |
839 | insp = list; | |
840 | } else { | |
841 | /* Eaten partially. */ | |
842 | ||
843 | if (skb_shared(list)) { | |
844 | /* Sucks! We need to fork list. :-( */ | |
845 | clone = skb_clone(list, GFP_ATOMIC); | |
846 | if (!clone) | |
847 | return NULL; | |
848 | insp = list->next; | |
849 | list = clone; | |
850 | } else { | |
851 | /* This may be pulled without | |
852 | * problems. */ | |
853 | insp = list; | |
854 | } | |
855 | if (!pskb_pull(list, eat)) { | |
856 | if (clone) | |
857 | kfree_skb(clone); | |
858 | return NULL; | |
859 | } | |
860 | break; | |
861 | } | |
862 | } while (eat); | |
863 | ||
864 | /* Free pulled out fragments. */ | |
865 | while ((list = skb_shinfo(skb)->frag_list) != insp) { | |
866 | skb_shinfo(skb)->frag_list = list->next; | |
867 | kfree_skb(list); | |
868 | } | |
869 | /* And insert new clone at head. */ | |
870 | if (clone) { | |
871 | clone->next = list; | |
872 | skb_shinfo(skb)->frag_list = clone; | |
873 | } | |
874 | } | |
875 | /* Success! Now we may commit changes to skb data. */ | |
876 | ||
877 | pull_pages: | |
878 | eat = delta; | |
879 | k = 0; | |
880 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
881 | if (skb_shinfo(skb)->frags[i].size <= eat) { | |
882 | put_page(skb_shinfo(skb)->frags[i].page); | |
883 | eat -= skb_shinfo(skb)->frags[i].size; | |
884 | } else { | |
885 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; | |
886 | if (eat) { | |
887 | skb_shinfo(skb)->frags[k].page_offset += eat; | |
888 | skb_shinfo(skb)->frags[k].size -= eat; | |
889 | eat = 0; | |
890 | } | |
891 | k++; | |
892 | } | |
893 | } | |
894 | skb_shinfo(skb)->nr_frags = k; | |
895 | ||
896 | skb->tail += delta; | |
897 | skb->data_len -= delta; | |
898 | ||
899 | return skb->tail; | |
900 | } | |
901 | ||
902 | /* Copy some data bits from skb to kernel buffer. */ | |
903 | ||
904 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) | |
905 | { | |
906 | int i, copy; | |
907 | int start = skb_headlen(skb); | |
908 | ||
909 | if (offset > (int)skb->len - len) | |
910 | goto fault; | |
911 | ||
912 | /* Copy header. */ | |
913 | if ((copy = start - offset) > 0) { | |
914 | if (copy > len) | |
915 | copy = len; | |
916 | memcpy(to, skb->data + offset, copy); | |
917 | if ((len -= copy) == 0) | |
918 | return 0; | |
919 | offset += copy; | |
920 | to += copy; | |
921 | } | |
922 | ||
923 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
924 | int end; | |
925 | ||
926 | BUG_TRAP(start <= offset + len); | |
927 | ||
928 | end = start + skb_shinfo(skb)->frags[i].size; | |
929 | if ((copy = end - offset) > 0) { | |
930 | u8 *vaddr; | |
931 | ||
932 | if (copy > len) | |
933 | copy = len; | |
934 | ||
935 | vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); | |
936 | memcpy(to, | |
937 | vaddr + skb_shinfo(skb)->frags[i].page_offset+ | |
938 | offset - start, copy); | |
939 | kunmap_skb_frag(vaddr); | |
940 | ||
941 | if ((len -= copy) == 0) | |
942 | return 0; | |
943 | offset += copy; | |
944 | to += copy; | |
945 | } | |
946 | start = end; | |
947 | } | |
948 | ||
949 | if (skb_shinfo(skb)->frag_list) { | |
950 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
951 | ||
952 | for (; list; list = list->next) { | |
953 | int end; | |
954 | ||
955 | BUG_TRAP(start <= offset + len); | |
956 | ||
957 | end = start + list->len; | |
958 | if ((copy = end - offset) > 0) { | |
959 | if (copy > len) | |
960 | copy = len; | |
961 | if (skb_copy_bits(list, offset - start, | |
962 | to, copy)) | |
963 | goto fault; | |
964 | if ((len -= copy) == 0) | |
965 | return 0; | |
966 | offset += copy; | |
967 | to += copy; | |
968 | } | |
969 | start = end; | |
970 | } | |
971 | } | |
972 | if (!len) | |
973 | return 0; | |
974 | ||
975 | fault: | |
976 | return -EFAULT; | |
977 | } | |
978 | ||
357b40a1 HX |
979 | /** |
980 | * skb_store_bits - store bits from kernel buffer to skb | |
981 | * @skb: destination buffer | |
982 | * @offset: offset in destination | |
983 | * @from: source buffer | |
984 | * @len: number of bytes to copy | |
985 | * | |
986 | * Copy the specified number of bytes from the source buffer to the | |
987 | * destination skb. This function handles all the messy bits of | |
988 | * traversing fragment lists and such. | |
989 | */ | |
990 | ||
991 | int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len) | |
992 | { | |
993 | int i, copy; | |
994 | int start = skb_headlen(skb); | |
995 | ||
996 | if (offset > (int)skb->len - len) | |
997 | goto fault; | |
998 | ||
999 | if ((copy = start - offset) > 0) { | |
1000 | if (copy > len) | |
1001 | copy = len; | |
1002 | memcpy(skb->data + offset, from, copy); | |
1003 | if ((len -= copy) == 0) | |
1004 | return 0; | |
1005 | offset += copy; | |
1006 | from += copy; | |
1007 | } | |
1008 | ||
1009 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1010 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
1011 | int end; | |
1012 | ||
1013 | BUG_TRAP(start <= offset + len); | |
1014 | ||
1015 | end = start + frag->size; | |
1016 | if ((copy = end - offset) > 0) { | |
1017 | u8 *vaddr; | |
1018 | ||
1019 | if (copy > len) | |
1020 | copy = len; | |
1021 | ||
1022 | vaddr = kmap_skb_frag(frag); | |
1023 | memcpy(vaddr + frag->page_offset + offset - start, | |
1024 | from, copy); | |
1025 | kunmap_skb_frag(vaddr); | |
1026 | ||
1027 | if ((len -= copy) == 0) | |
1028 | return 0; | |
1029 | offset += copy; | |
1030 | from += copy; | |
1031 | } | |
1032 | start = end; | |
1033 | } | |
1034 | ||
1035 | if (skb_shinfo(skb)->frag_list) { | |
1036 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
1037 | ||
1038 | for (; list; list = list->next) { | |
1039 | int end; | |
1040 | ||
1041 | BUG_TRAP(start <= offset + len); | |
1042 | ||
1043 | end = start + list->len; | |
1044 | if ((copy = end - offset) > 0) { | |
1045 | if (copy > len) | |
1046 | copy = len; | |
1047 | if (skb_store_bits(list, offset - start, | |
1048 | from, copy)) | |
1049 | goto fault; | |
1050 | if ((len -= copy) == 0) | |
1051 | return 0; | |
1052 | offset += copy; | |
1053 | from += copy; | |
1054 | } | |
1055 | start = end; | |
1056 | } | |
1057 | } | |
1058 | if (!len) | |
1059 | return 0; | |
1060 | ||
1061 | fault: | |
1062 | return -EFAULT; | |
1063 | } | |
1064 | ||
1065 | EXPORT_SYMBOL(skb_store_bits); | |
1066 | ||
1da177e4 LT |
1067 | /* Checksum skb data. */ |
1068 | ||
1069 | unsigned int skb_checksum(const struct sk_buff *skb, int offset, | |
1070 | int len, unsigned int csum) | |
1071 | { | |
1072 | int start = skb_headlen(skb); | |
1073 | int i, copy = start - offset; | |
1074 | int pos = 0; | |
1075 | ||
1076 | /* Checksum header. */ | |
1077 | if (copy > 0) { | |
1078 | if (copy > len) | |
1079 | copy = len; | |
1080 | csum = csum_partial(skb->data + offset, copy, csum); | |
1081 | if ((len -= copy) == 0) | |
1082 | return csum; | |
1083 | offset += copy; | |
1084 | pos = copy; | |
1085 | } | |
1086 | ||
1087 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1088 | int end; | |
1089 | ||
1090 | BUG_TRAP(start <= offset + len); | |
1091 | ||
1092 | end = start + skb_shinfo(skb)->frags[i].size; | |
1093 | if ((copy = end - offset) > 0) { | |
1094 | unsigned int csum2; | |
1095 | u8 *vaddr; | |
1096 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
1097 | ||
1098 | if (copy > len) | |
1099 | copy = len; | |
1100 | vaddr = kmap_skb_frag(frag); | |
1101 | csum2 = csum_partial(vaddr + frag->page_offset + | |
1102 | offset - start, copy, 0); | |
1103 | kunmap_skb_frag(vaddr); | |
1104 | csum = csum_block_add(csum, csum2, pos); | |
1105 | if (!(len -= copy)) | |
1106 | return csum; | |
1107 | offset += copy; | |
1108 | pos += copy; | |
1109 | } | |
1110 | start = end; | |
1111 | } | |
1112 | ||
1113 | if (skb_shinfo(skb)->frag_list) { | |
1114 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
1115 | ||
1116 | for (; list; list = list->next) { | |
1117 | int end; | |
1118 | ||
1119 | BUG_TRAP(start <= offset + len); | |
1120 | ||
1121 | end = start + list->len; | |
1122 | if ((copy = end - offset) > 0) { | |
1123 | unsigned int csum2; | |
1124 | if (copy > len) | |
1125 | copy = len; | |
1126 | csum2 = skb_checksum(list, offset - start, | |
1127 | copy, 0); | |
1128 | csum = csum_block_add(csum, csum2, pos); | |
1129 | if ((len -= copy) == 0) | |
1130 | return csum; | |
1131 | offset += copy; | |
1132 | pos += copy; | |
1133 | } | |
1134 | start = end; | |
1135 | } | |
1136 | } | |
1137 | if (len) | |
1138 | BUG(); | |
1139 | ||
1140 | return csum; | |
1141 | } | |
1142 | ||
1143 | /* Both of above in one bottle. */ | |
1144 | ||
1145 | unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, | |
1146 | u8 *to, int len, unsigned int csum) | |
1147 | { | |
1148 | int start = skb_headlen(skb); | |
1149 | int i, copy = start - offset; | |
1150 | int pos = 0; | |
1151 | ||
1152 | /* Copy header. */ | |
1153 | if (copy > 0) { | |
1154 | if (copy > len) | |
1155 | copy = len; | |
1156 | csum = csum_partial_copy_nocheck(skb->data + offset, to, | |
1157 | copy, csum); | |
1158 | if ((len -= copy) == 0) | |
1159 | return csum; | |
1160 | offset += copy; | |
1161 | to += copy; | |
1162 | pos = copy; | |
1163 | } | |
1164 | ||
1165 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1166 | int end; | |
1167 | ||
1168 | BUG_TRAP(start <= offset + len); | |
1169 | ||
1170 | end = start + skb_shinfo(skb)->frags[i].size; | |
1171 | if ((copy = end - offset) > 0) { | |
1172 | unsigned int csum2; | |
1173 | u8 *vaddr; | |
1174 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
1175 | ||
1176 | if (copy > len) | |
1177 | copy = len; | |
1178 | vaddr = kmap_skb_frag(frag); | |
1179 | csum2 = csum_partial_copy_nocheck(vaddr + | |
1180 | frag->page_offset + | |
1181 | offset - start, to, | |
1182 | copy, 0); | |
1183 | kunmap_skb_frag(vaddr); | |
1184 | csum = csum_block_add(csum, csum2, pos); | |
1185 | if (!(len -= copy)) | |
1186 | return csum; | |
1187 | offset += copy; | |
1188 | to += copy; | |
1189 | pos += copy; | |
1190 | } | |
1191 | start = end; | |
1192 | } | |
1193 | ||
1194 | if (skb_shinfo(skb)->frag_list) { | |
1195 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
1196 | ||
1197 | for (; list; list = list->next) { | |
1198 | unsigned int csum2; | |
1199 | int end; | |
1200 | ||
1201 | BUG_TRAP(start <= offset + len); | |
1202 | ||
1203 | end = start + list->len; | |
1204 | if ((copy = end - offset) > 0) { | |
1205 | if (copy > len) | |
1206 | copy = len; | |
1207 | csum2 = skb_copy_and_csum_bits(list, | |
1208 | offset - start, | |
1209 | to, copy, 0); | |
1210 | csum = csum_block_add(csum, csum2, pos); | |
1211 | if ((len -= copy) == 0) | |
1212 | return csum; | |
1213 | offset += copy; | |
1214 | to += copy; | |
1215 | pos += copy; | |
1216 | } | |
1217 | start = end; | |
1218 | } | |
1219 | } | |
1220 | if (len) | |
1221 | BUG(); | |
1222 | return csum; | |
1223 | } | |
1224 | ||
1225 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) | |
1226 | { | |
1227 | unsigned int csum; | |
1228 | long csstart; | |
1229 | ||
1230 | if (skb->ip_summed == CHECKSUM_HW) | |
1231 | csstart = skb->h.raw - skb->data; | |
1232 | else | |
1233 | csstart = skb_headlen(skb); | |
1234 | ||
1235 | if (csstart > skb_headlen(skb)) | |
1236 | BUG(); | |
1237 | ||
1238 | memcpy(to, skb->data, csstart); | |
1239 | ||
1240 | csum = 0; | |
1241 | if (csstart != skb->len) | |
1242 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, | |
1243 | skb->len - csstart, 0); | |
1244 | ||
1245 | if (skb->ip_summed == CHECKSUM_HW) { | |
1246 | long csstuff = csstart + skb->csum; | |
1247 | ||
1248 | *((unsigned short *)(to + csstuff)) = csum_fold(csum); | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | /** | |
1253 | * skb_dequeue - remove from the head of the queue | |
1254 | * @list: list to dequeue from | |
1255 | * | |
1256 | * Remove the head of the list. The list lock is taken so the function | |
1257 | * may be used safely with other locking list functions. The head item is | |
1258 | * returned or %NULL if the list is empty. | |
1259 | */ | |
1260 | ||
1261 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) | |
1262 | { | |
1263 | unsigned long flags; | |
1264 | struct sk_buff *result; | |
1265 | ||
1266 | spin_lock_irqsave(&list->lock, flags); | |
1267 | result = __skb_dequeue(list); | |
1268 | spin_unlock_irqrestore(&list->lock, flags); | |
1269 | return result; | |
1270 | } | |
1271 | ||
1272 | /** | |
1273 | * skb_dequeue_tail - remove from the tail of the queue | |
1274 | * @list: list to dequeue from | |
1275 | * | |
1276 | * Remove the tail of the list. The list lock is taken so the function | |
1277 | * may be used safely with other locking list functions. The tail item is | |
1278 | * returned or %NULL if the list is empty. | |
1279 | */ | |
1280 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) | |
1281 | { | |
1282 | unsigned long flags; | |
1283 | struct sk_buff *result; | |
1284 | ||
1285 | spin_lock_irqsave(&list->lock, flags); | |
1286 | result = __skb_dequeue_tail(list); | |
1287 | spin_unlock_irqrestore(&list->lock, flags); | |
1288 | return result; | |
1289 | } | |
1290 | ||
1291 | /** | |
1292 | * skb_queue_purge - empty a list | |
1293 | * @list: list to empty | |
1294 | * | |
1295 | * Delete all buffers on an &sk_buff list. Each buffer is removed from | |
1296 | * the list and one reference dropped. This function takes the list | |
1297 | * lock and is atomic with respect to other list locking functions. | |
1298 | */ | |
1299 | void skb_queue_purge(struct sk_buff_head *list) | |
1300 | { | |
1301 | struct sk_buff *skb; | |
1302 | while ((skb = skb_dequeue(list)) != NULL) | |
1303 | kfree_skb(skb); | |
1304 | } | |
1305 | ||
1306 | /** | |
1307 | * skb_queue_head - queue a buffer at the list head | |
1308 | * @list: list to use | |
1309 | * @newsk: buffer to queue | |
1310 | * | |
1311 | * Queue a buffer at the start of the list. This function takes the | |
1312 | * list lock and can be used safely with other locking &sk_buff functions | |
1313 | * safely. | |
1314 | * | |
1315 | * A buffer cannot be placed on two lists at the same time. | |
1316 | */ | |
1317 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) | |
1318 | { | |
1319 | unsigned long flags; | |
1320 | ||
1321 | spin_lock_irqsave(&list->lock, flags); | |
1322 | __skb_queue_head(list, newsk); | |
1323 | spin_unlock_irqrestore(&list->lock, flags); | |
1324 | } | |
1325 | ||
1326 | /** | |
1327 | * skb_queue_tail - queue a buffer at the list tail | |
1328 | * @list: list to use | |
1329 | * @newsk: buffer to queue | |
1330 | * | |
1331 | * Queue a buffer at the tail of the list. This function takes the | |
1332 | * list lock and can be used safely with other locking &sk_buff functions | |
1333 | * safely. | |
1334 | * | |
1335 | * A buffer cannot be placed on two lists at the same time. | |
1336 | */ | |
1337 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) | |
1338 | { | |
1339 | unsigned long flags; | |
1340 | ||
1341 | spin_lock_irqsave(&list->lock, flags); | |
1342 | __skb_queue_tail(list, newsk); | |
1343 | spin_unlock_irqrestore(&list->lock, flags); | |
1344 | } | |
1345 | /** | |
1346 | * skb_unlink - remove a buffer from a list | |
1347 | * @skb: buffer to remove | |
1348 | * | |
1349 | * Place a packet after a given packet in a list. The list locks are taken | |
1350 | * and this function is atomic with respect to other list locked calls | |
1351 | * | |
1352 | * Works even without knowing the list it is sitting on, which can be | |
1353 | * handy at times. It also means that THE LIST MUST EXIST when you | |
1354 | * unlink. Thus a list must have its contents unlinked before it is | |
1355 | * destroyed. | |
1356 | */ | |
1357 | void skb_unlink(struct sk_buff *skb) | |
1358 | { | |
1359 | struct sk_buff_head *list = skb->list; | |
1360 | ||
1361 | if (list) { | |
1362 | unsigned long flags; | |
1363 | ||
1364 | spin_lock_irqsave(&list->lock, flags); | |
1365 | if (skb->list == list) | |
1366 | __skb_unlink(skb, skb->list); | |
1367 | spin_unlock_irqrestore(&list->lock, flags); | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | ||
1372 | /** | |
1373 | * skb_append - append a buffer | |
1374 | * @old: buffer to insert after | |
1375 | * @newsk: buffer to insert | |
1376 | * | |
1377 | * Place a packet after a given packet in a list. The list locks are taken | |
1378 | * and this function is atomic with respect to other list locked calls. | |
1379 | * A buffer cannot be placed on two lists at the same time. | |
1380 | */ | |
1381 | ||
1382 | void skb_append(struct sk_buff *old, struct sk_buff *newsk) | |
1383 | { | |
1384 | unsigned long flags; | |
1385 | ||
1386 | spin_lock_irqsave(&old->list->lock, flags); | |
1387 | __skb_append(old, newsk); | |
1388 | spin_unlock_irqrestore(&old->list->lock, flags); | |
1389 | } | |
1390 | ||
1391 | ||
1392 | /** | |
1393 | * skb_insert - insert a buffer | |
1394 | * @old: buffer to insert before | |
1395 | * @newsk: buffer to insert | |
1396 | * | |
1397 | * Place a packet before a given packet in a list. The list locks are taken | |
1398 | * and this function is atomic with respect to other list locked calls | |
1399 | * A buffer cannot be placed on two lists at the same time. | |
1400 | */ | |
1401 | ||
1402 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk) | |
1403 | { | |
1404 | unsigned long flags; | |
1405 | ||
1406 | spin_lock_irqsave(&old->list->lock, flags); | |
1407 | __skb_insert(newsk, old->prev, old, old->list); | |
1408 | spin_unlock_irqrestore(&old->list->lock, flags); | |
1409 | } | |
1410 | ||
1411 | #if 0 | |
1412 | /* | |
1413 | * Tune the memory allocator for a new MTU size. | |
1414 | */ | |
1415 | void skb_add_mtu(int mtu) | |
1416 | { | |
1417 | /* Must match allocation in alloc_skb */ | |
1418 | mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info); | |
1419 | ||
1420 | kmem_add_cache_size(mtu); | |
1421 | } | |
1422 | #endif | |
1423 | ||
1424 | static inline void skb_split_inside_header(struct sk_buff *skb, | |
1425 | struct sk_buff* skb1, | |
1426 | const u32 len, const int pos) | |
1427 | { | |
1428 | int i; | |
1429 | ||
1430 | memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len); | |
1431 | ||
1432 | /* And move data appendix as is. */ | |
1433 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | |
1434 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; | |
1435 | ||
1436 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; | |
1437 | skb_shinfo(skb)->nr_frags = 0; | |
1438 | skb1->data_len = skb->data_len; | |
1439 | skb1->len += skb1->data_len; | |
1440 | skb->data_len = 0; | |
1441 | skb->len = len; | |
1442 | skb->tail = skb->data + len; | |
1443 | } | |
1444 | ||
1445 | static inline void skb_split_no_header(struct sk_buff *skb, | |
1446 | struct sk_buff* skb1, | |
1447 | const u32 len, int pos) | |
1448 | { | |
1449 | int i, k = 0; | |
1450 | const int nfrags = skb_shinfo(skb)->nr_frags; | |
1451 | ||
1452 | skb_shinfo(skb)->nr_frags = 0; | |
1453 | skb1->len = skb1->data_len = skb->len - len; | |
1454 | skb->len = len; | |
1455 | skb->data_len = len - pos; | |
1456 | ||
1457 | for (i = 0; i < nfrags; i++) { | |
1458 | int size = skb_shinfo(skb)->frags[i].size; | |
1459 | ||
1460 | if (pos + size > len) { | |
1461 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; | |
1462 | ||
1463 | if (pos < len) { | |
1464 | /* Split frag. | |
1465 | * We have two variants in this case: | |
1466 | * 1. Move all the frag to the second | |
1467 | * part, if it is possible. F.e. | |
1468 | * this approach is mandatory for TUX, | |
1469 | * where splitting is expensive. | |
1470 | * 2. Split is accurately. We make this. | |
1471 | */ | |
1472 | get_page(skb_shinfo(skb)->frags[i].page); | |
1473 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; | |
1474 | skb_shinfo(skb1)->frags[0].size -= len - pos; | |
1475 | skb_shinfo(skb)->frags[i].size = len - pos; | |
1476 | skb_shinfo(skb)->nr_frags++; | |
1477 | } | |
1478 | k++; | |
1479 | } else | |
1480 | skb_shinfo(skb)->nr_frags++; | |
1481 | pos += size; | |
1482 | } | |
1483 | skb_shinfo(skb1)->nr_frags = k; | |
1484 | } | |
1485 | ||
1486 | /** | |
1487 | * skb_split - Split fragmented skb to two parts at length len. | |
1488 | * @skb: the buffer to split | |
1489 | * @skb1: the buffer to receive the second part | |
1490 | * @len: new length for skb | |
1491 | */ | |
1492 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) | |
1493 | { | |
1494 | int pos = skb_headlen(skb); | |
1495 | ||
1496 | if (len < pos) /* Split line is inside header. */ | |
1497 | skb_split_inside_header(skb, skb1, len, pos); | |
1498 | else /* Second chunk has no header, nothing to copy. */ | |
1499 | skb_split_no_header(skb, skb1, len, pos); | |
1500 | } | |
1501 | ||
677e90ed TG |
1502 | /** |
1503 | * skb_prepare_seq_read - Prepare a sequential read of skb data | |
1504 | * @skb: the buffer to read | |
1505 | * @from: lower offset of data to be read | |
1506 | * @to: upper offset of data to be read | |
1507 | * @st: state variable | |
1508 | * | |
1509 | * Initializes the specified state variable. Must be called before | |
1510 | * invoking skb_seq_read() for the first time. | |
1511 | */ | |
1512 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, | |
1513 | unsigned int to, struct skb_seq_state *st) | |
1514 | { | |
1515 | st->lower_offset = from; | |
1516 | st->upper_offset = to; | |
1517 | st->root_skb = st->cur_skb = skb; | |
1518 | st->frag_idx = st->stepped_offset = 0; | |
1519 | st->frag_data = NULL; | |
1520 | } | |
1521 | ||
1522 | /** | |
1523 | * skb_seq_read - Sequentially read skb data | |
1524 | * @consumed: number of bytes consumed by the caller so far | |
1525 | * @data: destination pointer for data to be returned | |
1526 | * @st: state variable | |
1527 | * | |
1528 | * Reads a block of skb data at &consumed relative to the | |
1529 | * lower offset specified to skb_prepare_seq_read(). Assigns | |
1530 | * the head of the data block to &data and returns the length | |
1531 | * of the block or 0 if the end of the skb data or the upper | |
1532 | * offset has been reached. | |
1533 | * | |
1534 | * The caller is not required to consume all of the data | |
1535 | * returned, i.e. &consumed is typically set to the number | |
1536 | * of bytes already consumed and the next call to | |
1537 | * skb_seq_read() will return the remaining part of the block. | |
1538 | * | |
1539 | * Note: The size of each block of data returned can be arbitary, | |
1540 | * this limitation is the cost for zerocopy seqeuental | |
1541 | * reads of potentially non linear data. | |
1542 | * | |
1543 | * Note: Fragment lists within fragments are not implemented | |
1544 | * at the moment, state->root_skb could be replaced with | |
1545 | * a stack for this purpose. | |
1546 | */ | |
1547 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, | |
1548 | struct skb_seq_state *st) | |
1549 | { | |
1550 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; | |
1551 | skb_frag_t *frag; | |
1552 | ||
1553 | if (unlikely(abs_offset >= st->upper_offset)) | |
1554 | return 0; | |
1555 | ||
1556 | next_skb: | |
1557 | block_limit = skb_headlen(st->cur_skb); | |
1558 | ||
1559 | if (abs_offset < block_limit) { | |
1560 | *data = st->cur_skb->data + abs_offset; | |
1561 | return block_limit - abs_offset; | |
1562 | } | |
1563 | ||
1564 | if (st->frag_idx == 0 && !st->frag_data) | |
1565 | st->stepped_offset += skb_headlen(st->cur_skb); | |
1566 | ||
1567 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { | |
1568 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; | |
1569 | block_limit = frag->size + st->stepped_offset; | |
1570 | ||
1571 | if (abs_offset < block_limit) { | |
1572 | if (!st->frag_data) | |
1573 | st->frag_data = kmap_skb_frag(frag); | |
1574 | ||
1575 | *data = (u8 *) st->frag_data + frag->page_offset + | |
1576 | (abs_offset - st->stepped_offset); | |
1577 | ||
1578 | return block_limit - abs_offset; | |
1579 | } | |
1580 | ||
1581 | if (st->frag_data) { | |
1582 | kunmap_skb_frag(st->frag_data); | |
1583 | st->frag_data = NULL; | |
1584 | } | |
1585 | ||
1586 | st->frag_idx++; | |
1587 | st->stepped_offset += frag->size; | |
1588 | } | |
1589 | ||
1590 | if (st->cur_skb->next) { | |
1591 | st->cur_skb = st->cur_skb->next; | |
1592 | st->frag_idx = 0; | |
1593 | goto next_skb; | |
1594 | } else if (st->root_skb == st->cur_skb && | |
1595 | skb_shinfo(st->root_skb)->frag_list) { | |
1596 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; | |
1597 | goto next_skb; | |
1598 | } | |
1599 | ||
1600 | return 0; | |
1601 | } | |
1602 | ||
1603 | /** | |
1604 | * skb_abort_seq_read - Abort a sequential read of skb data | |
1605 | * @st: state variable | |
1606 | * | |
1607 | * Must be called if skb_seq_read() was not called until it | |
1608 | * returned 0. | |
1609 | */ | |
1610 | void skb_abort_seq_read(struct skb_seq_state *st) | |
1611 | { | |
1612 | if (st->frag_data) | |
1613 | kunmap_skb_frag(st->frag_data); | |
1614 | } | |
1615 | ||
3fc7e8a6 TG |
1616 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
1617 | ||
1618 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, | |
1619 | struct ts_config *conf, | |
1620 | struct ts_state *state) | |
1621 | { | |
1622 | return skb_seq_read(offset, text, TS_SKB_CB(state)); | |
1623 | } | |
1624 | ||
1625 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) | |
1626 | { | |
1627 | skb_abort_seq_read(TS_SKB_CB(state)); | |
1628 | } | |
1629 | ||
1630 | /** | |
1631 | * skb_find_text - Find a text pattern in skb data | |
1632 | * @skb: the buffer to look in | |
1633 | * @from: search offset | |
1634 | * @to: search limit | |
1635 | * @config: textsearch configuration | |
1636 | * @state: uninitialized textsearch state variable | |
1637 | * | |
1638 | * Finds a pattern in the skb data according to the specified | |
1639 | * textsearch configuration. Use textsearch_next() to retrieve | |
1640 | * subsequent occurrences of the pattern. Returns the offset | |
1641 | * to the first occurrence or UINT_MAX if no match was found. | |
1642 | */ | |
1643 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, | |
1644 | unsigned int to, struct ts_config *config, | |
1645 | struct ts_state *state) | |
1646 | { | |
1647 | config->get_next_block = skb_ts_get_next_block; | |
1648 | config->finish = skb_ts_finish; | |
1649 | ||
1650 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); | |
1651 | ||
1652 | return textsearch_find(config, state); | |
1653 | } | |
1654 | ||
1da177e4 LT |
1655 | void __init skb_init(void) |
1656 | { | |
1657 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", | |
1658 | sizeof(struct sk_buff), | |
1659 | 0, | |
1660 | SLAB_HWCACHE_ALIGN, | |
1661 | NULL, NULL); | |
1662 | if (!skbuff_head_cache) | |
1663 | panic("cannot create skbuff cache"); | |
1664 | } | |
1665 | ||
1666 | EXPORT_SYMBOL(___pskb_trim); | |
1667 | EXPORT_SYMBOL(__kfree_skb); | |
1668 | EXPORT_SYMBOL(__pskb_pull_tail); | |
1669 | EXPORT_SYMBOL(alloc_skb); | |
1670 | EXPORT_SYMBOL(pskb_copy); | |
1671 | EXPORT_SYMBOL(pskb_expand_head); | |
1672 | EXPORT_SYMBOL(skb_checksum); | |
1673 | EXPORT_SYMBOL(skb_clone); | |
1674 | EXPORT_SYMBOL(skb_clone_fraglist); | |
1675 | EXPORT_SYMBOL(skb_copy); | |
1676 | EXPORT_SYMBOL(skb_copy_and_csum_bits); | |
1677 | EXPORT_SYMBOL(skb_copy_and_csum_dev); | |
1678 | EXPORT_SYMBOL(skb_copy_bits); | |
1679 | EXPORT_SYMBOL(skb_copy_expand); | |
1680 | EXPORT_SYMBOL(skb_over_panic); | |
1681 | EXPORT_SYMBOL(skb_pad); | |
1682 | EXPORT_SYMBOL(skb_realloc_headroom); | |
1683 | EXPORT_SYMBOL(skb_under_panic); | |
1684 | EXPORT_SYMBOL(skb_dequeue); | |
1685 | EXPORT_SYMBOL(skb_dequeue_tail); | |
1686 | EXPORT_SYMBOL(skb_insert); | |
1687 | EXPORT_SYMBOL(skb_queue_purge); | |
1688 | EXPORT_SYMBOL(skb_queue_head); | |
1689 | EXPORT_SYMBOL(skb_queue_tail); | |
1690 | EXPORT_SYMBOL(skb_unlink); | |
1691 | EXPORT_SYMBOL(skb_append); | |
1692 | EXPORT_SYMBOL(skb_split); | |
677e90ed TG |
1693 | EXPORT_SYMBOL(skb_prepare_seq_read); |
1694 | EXPORT_SYMBOL(skb_seq_read); | |
1695 | EXPORT_SYMBOL(skb_abort_seq_read); | |
3fc7e8a6 | 1696 | EXPORT_SYMBOL(skb_find_text); |