MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / core / skbuff.c
blob0cd19b7ec7eaff3b695fddf2e66a6ffe25d5335e
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
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
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).
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.
38 * The functions in this file will not compile correctly with gcc 2.4.x
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>
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>
68 #include <asm/uaccess.h>
69 #include <asm/system.h>
71 static kmem_cache_t *skbuff_head_cache;
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
79 /**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
85 * Out of line support code for skb_put(). Not user callable.
87 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89 printk(KERN_INFO "skput:over: %p:%d put:%d dev:%s",
90 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
91 BUG();
94 /**
95 * skb_under_panic - private function
96 * @skb: buffer
97 * @sz: size
98 * @here: address
100 * Out of line support code for skb_push(). Not user callable.
103 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
105 printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s",
106 here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
107 BUG();
110 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
111 * 'private' fields and also do memory statistics to find all the
112 * [BEEP] leaks.
117 * alloc_skb - allocate a network buffer
118 * @size: size to allocate
119 * @gfp_mask: allocation mask
121 * Allocate a new &sk_buff. The returned buffer has no headroom and a
122 * tail room of size bytes. The object has a reference count of one.
123 * The return is the buffer. On a failure the return is %NULL.
125 * Buffers may only be allocated from interrupts using a @gfp_mask of
126 * %GFP_ATOMIC.
128 struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)
130 struct sk_buff *skb;
131 u8 *data;
133 /* Get the HEAD */
134 skb = kmem_cache_alloc(skbuff_head_cache,
135 gfp_mask & ~__GFP_DMA);
136 if (!skb)
137 goto out;
139 /* Get the DATA. Size must match skb_add_mtu(). */
140 size = SKB_DATA_ALIGN(size);
141 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
142 if (!data)
143 goto nodata;
145 memset(skb, 0, offsetof(struct sk_buff, truesize));
146 skb->truesize = size + sizeof(struct sk_buff);
147 atomic_set(&skb->users, 1);
148 skb->head = data;
149 skb->data = data;
150 skb->tail = data;
151 skb->end = data + size;
153 atomic_set(&(skb_shinfo(skb)->dataref), 1);
154 skb_shinfo(skb)->nr_frags = 0;
155 skb_shinfo(skb)->tso_size = 0;
156 skb_shinfo(skb)->tso_segs = 0;
157 skb_shinfo(skb)->frag_list = NULL;
158 out:
159 return skb;
160 nodata:
161 kmem_cache_free(skbuff_head_cache, skb);
162 skb = NULL;
163 goto out;
167 static void skb_drop_fraglist(struct sk_buff *skb)
169 struct sk_buff *list = skb_shinfo(skb)->frag_list;
171 skb_shinfo(skb)->frag_list = NULL;
173 do {
174 struct sk_buff *this = list;
175 list = list->next;
176 kfree_skb(this);
177 } while (list);
180 static void skb_clone_fraglist(struct sk_buff *skb)
182 struct sk_buff *list;
184 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
185 skb_get(list);
188 void skb_release_data(struct sk_buff *skb)
190 if (!skb->cloned ||
191 atomic_dec_and_test(&(skb_shinfo(skb)->dataref))) {
192 if (skb_shinfo(skb)->nr_frags) {
193 int i;
194 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
195 put_page(skb_shinfo(skb)->frags[i].page);
198 if (skb_shinfo(skb)->frag_list)
199 skb_drop_fraglist(skb);
201 kfree(skb->head);
206 * Free an skbuff by memory without cleaning the state.
208 void kfree_skbmem(struct sk_buff *skb)
210 skb_release_data(skb);
211 kmem_cache_free(skbuff_head_cache, skb);
215 * __kfree_skb - private function
216 * @skb: buffer
218 * Free an sk_buff. Release anything attached to the buffer.
219 * Clean the state. This is an internal helper function. Users should
220 * always call kfree_skb
223 void __kfree_skb(struct sk_buff *skb)
225 if(!skb)return; //johnson
226 if (skb->list) {
227 printk(KERN_WARNING "Warning: kfree_skb passed an skb still "
228 "on a list (from %p).\n", NET_CALLER(skb));
229 BUG();
232 dst_release(skb->dst);
233 #ifdef CONFIG_XFRM
234 secpath_put(skb->sp);
235 #endif
236 if(skb->destructor) {
237 if (in_irq())
238 printk(KERN_WARNING "Warning: kfree_skb on "
239 "hard IRQ %p\n", NET_CALLER(skb));
240 skb->destructor(skb);
242 #ifdef CONFIG_NETFILTER
243 nf_conntrack_put(skb->nfct);
244 #ifdef CONFIG_BRIDGE_NETFILTER
245 nf_bridge_put(skb->nf_bridge);
246 #endif
247 #endif
248 /* XXX: IS this still necessary? - JHS */
249 #ifdef CONFIG_NET_SCHED
250 skb->tc_index = 0;
251 #ifdef CONFIG_NET_CLS_ACT
252 skb->tc_verd = 0;
253 skb->tc_classid = 0;
254 #endif
255 #endif
257 kfree_skbmem(skb);
261 * skb_clone - duplicate an sk_buff
262 * @skb: buffer to clone
263 * @gfp_mask: allocation priority
265 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
266 * copies share the same packet data but not structure. The new
267 * buffer has a reference count of 1. If the allocation fails the
268 * function returns %NULL otherwise the new buffer is returned.
270 * If this function is called from an interrupt gfp_mask() must be
271 * %GFP_ATOMIC.
274 struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)
276 struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
278 if (!n)
279 return NULL;
281 #define C(x) n->x = skb->x
283 n->next = n->prev = NULL;
284 n->list = NULL;
285 n->sk = NULL;
286 C(stamp);
287 C(dev);
288 C(real_dev);
289 C(h);
290 C(nh);
291 C(mac);
292 C(dst);
293 dst_clone(skb->dst);
294 C(sp);
295 #ifdef CONFIG_INET
296 secpath_get(skb->sp);
297 #endif
298 memcpy(n->cb, skb->cb, sizeof(skb->cb));
299 C(len);
300 C(data_len);
301 C(csum);
302 C(local_df);
303 n->cloned = 1;
304 C(pkt_type);
305 C(ip_summed);
306 C(priority);
307 C(protocol);
308 C(security);
309 n->destructor = NULL;
310 #ifdef CONFIG_NETFILTER
311 C(nfmark);
312 C(nfcache);
313 C(nfct);
314 nf_conntrack_get(skb->nfct);
315 C(nfctinfo);
316 #ifdef CONFIG_NETFILTER_DEBUG
317 C(nf_debug);
318 #endif
319 #ifdef CONFIG_BRIDGE_NETFILTER
320 C(nf_bridge);
321 nf_bridge_get(skb->nf_bridge);
322 #endif
323 #endif /*CONFIG_NETFILTER*/
324 #if defined(CONFIG_HIPPI)
325 C(private);
326 #endif
327 #ifdef CONFIG_NET_SCHED
328 C(tc_index);
329 #ifdef CONFIG_NET_CLS_ACT
330 n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
331 n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd);
332 n->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
333 C(input_dev);
334 C(tc_classid);
335 #endif
337 #endif
338 C(truesize);
339 atomic_set(&n->users, 1);
340 C(head);
341 C(data);
342 C(tail);
343 C(end);
345 atomic_inc(&(skb_shinfo(skb)->dataref));
346 skb->cloned = 1;
348 return n;
351 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
354 * Shift between the two data areas in bytes
356 unsigned long offset = new->data - old->data;
358 new->list = NULL;
359 new->sk = NULL;
360 new->dev = old->dev;
361 new->real_dev = old->real_dev;
362 new->priority = old->priority;
363 new->protocol = old->protocol;
364 new->dst = dst_clone(old->dst);
365 #ifdef CONFIG_INET
366 new->sp = secpath_get(old->sp);
367 #endif
368 new->h.raw = old->h.raw + offset;
369 new->nh.raw = old->nh.raw + offset;
370 new->mac.raw = old->mac.raw + offset;
371 memcpy(new->cb, old->cb, sizeof(old->cb));
372 new->local_df = old->local_df;
373 new->pkt_type = old->pkt_type;
374 new->stamp = old->stamp;
375 new->destructor = NULL;
376 new->security = old->security;
377 #ifdef CONFIG_NETFILTER
378 new->nfmark = old->nfmark;
379 new->nfcache = old->nfcache;
380 new->nfct = old->nfct;
381 nf_conntrack_get(old->nfct);
382 new->nfctinfo = old->nfctinfo;
383 #ifdef CONFIG_NETFILTER_DEBUG
384 new->nf_debug = old->nf_debug;
385 #endif
386 #ifdef CONFIG_BRIDGE_NETFILTER
387 new->nf_bridge = old->nf_bridge;
388 nf_bridge_get(old->nf_bridge);
389 #endif
390 #endif
391 #ifdef CONFIG_NET_SCHED
392 #ifdef CONFIG_NET_CLS_ACT
393 new->tc_verd = old->tc_verd;
394 #endif
395 new->tc_index = old->tc_index;
396 #endif
397 atomic_set(&new->users, 1);
401 * skb_copy - create private copy of an sk_buff
402 * @skb: buffer to copy
403 * @gfp_mask: allocation priority
405 * Make a copy of both an &sk_buff and its data. This is used when the
406 * caller wishes to modify the data and needs a private copy of the
407 * data to alter. Returns %NULL on failure or the pointer to the buffer
408 * on success. The returned buffer has a reference count of 1.
410 * As by-product this function converts non-linear &sk_buff to linear
411 * one, so that &sk_buff becomes completely private and caller is allowed
412 * to modify all the data of returned buffer. This means that this
413 * function is not recommended for use in circumstances when only
414 * header is going to be modified. Use pskb_copy() instead.
417 struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)
419 int headerlen = skb->data - skb->head;
421 * Allocate the copy buffer
423 struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
424 gfp_mask);
425 if (!n)
426 return NULL;
428 /* Set the data pointer */
429 skb_reserve(n, headerlen);
430 /* Set the tail pointer and length */
431 skb_put(n, skb->len);
432 n->csum = skb->csum;
433 n->ip_summed = skb->ip_summed;
435 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
436 BUG();
438 copy_skb_header(n, skb);
439 return n;
444 * pskb_copy - create copy of an sk_buff with private head.
445 * @skb: buffer to copy
446 * @gfp_mask: allocation priority
448 * Make a copy of both an &sk_buff and part of its data, located
449 * in header. Fragmented data remain shared. This is used when
450 * the caller wishes to modify only header of &sk_buff and needs
451 * private copy of the header to alter. Returns %NULL on failure
452 * or the pointer to the buffer on success.
453 * The returned buffer has a reference count of 1.
456 struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask)
459 * Allocate the copy buffer
461 struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
463 if (!n)
464 goto out;
466 /* Set the data pointer */
467 skb_reserve(n, skb->data - skb->head);
468 /* Set the tail pointer and length */
469 skb_put(n, skb_headlen(skb));
470 /* Copy the bytes */
471 memcpy(n->data, skb->data, n->len);
472 n->csum = skb->csum;
473 n->ip_summed = skb->ip_summed;
475 n->data_len = skb->data_len;
476 n->len = skb->len;
478 if (skb_shinfo(skb)->nr_frags) {
479 int i;
481 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
482 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
483 get_page(skb_shinfo(n)->frags[i].page);
485 skb_shinfo(n)->nr_frags = i;
487 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
488 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
490 if (skb_shinfo(skb)->frag_list) {
491 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
492 skb_clone_fraglist(n);
495 copy_skb_header(n, skb);
496 out:
497 return n;
501 * pskb_expand_head - reallocate header of &sk_buff
502 * @skb: buffer to reallocate
503 * @nhead: room to add at head
504 * @ntail: room to add at tail
505 * @gfp_mask: allocation priority
507 * Expands (or creates identical copy, if &nhead and &ntail are zero)
508 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
509 * reference count of 1. Returns zero in the case of success or error,
510 * if expansion failed. In the last case, &sk_buff is not changed.
512 * All the pointers pointing into skb header may change and must be
513 * reloaded after call to this function.
516 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask)
518 int i;
519 u8 *data;
520 int size = nhead + (skb->end - skb->head) + ntail;
521 long off;
523 if (skb_shared(skb))
524 BUG();
526 size = SKB_DATA_ALIGN(size);
528 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
529 if (!data)
530 goto nodata;
532 /* Copy only real data... and, alas, header. This should be
533 * optimized for the cases when header is void. */
534 memcpy(data + nhead, skb->head, skb->tail - skb->head);
535 memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
537 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
538 get_page(skb_shinfo(skb)->frags[i].page);
540 if (skb_shinfo(skb)->frag_list)
541 skb_clone_fraglist(skb);
543 skb_release_data(skb);
545 off = (data + nhead) - skb->head;
547 skb->head = data;
548 skb->end = data + size;
549 skb->data += off;
550 skb->tail += off;
551 skb->mac.raw += off;
552 skb->h.raw += off;
553 skb->nh.raw += off;
554 skb->cloned = 0;
555 atomic_set(&skb_shinfo(skb)->dataref, 1);
556 return 0;
558 nodata:
559 return -ENOMEM;
562 /* Make private copy of skb with writable head and some headroom */
564 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
566 struct sk_buff *skb2;
567 int delta = headroom - skb_headroom(skb);
569 if (delta <= 0)
570 skb2 = pskb_copy(skb, GFP_ATOMIC);
571 else {
572 skb2 = skb_clone(skb, GFP_ATOMIC);
573 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
574 GFP_ATOMIC)) {
575 kfree_skb(skb2);
576 skb2 = NULL;
579 return skb2;
584 * skb_copy_expand - copy and expand sk_buff
585 * @skb: buffer to copy
586 * @newheadroom: new free bytes at head
587 * @newtailroom: new free bytes at tail
588 * @gfp_mask: allocation priority
590 * Make a copy of both an &sk_buff and its data and while doing so
591 * allocate additional space.
593 * This is used when the caller wishes to modify the data and needs a
594 * private copy of the data to alter as well as more space for new fields.
595 * Returns %NULL on failure or the pointer to the buffer
596 * on success. The returned buffer has a reference count of 1.
598 * You must pass %GFP_ATOMIC as the allocation priority if this function
599 * is called from an interrupt.
601 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
602 * only by netfilter in the cases when checksum is recalculated? --ANK
604 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
605 int newheadroom, int newtailroom, int gfp_mask)
608 * Allocate the copy buffer
610 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
611 gfp_mask);
612 int head_copy_len, head_copy_off;
614 if (!n)
615 return NULL;
617 skb_reserve(n, newheadroom);
619 /* Set the tail pointer and length */
620 skb_put(n, skb->len);
622 head_copy_len = skb_headroom(skb);
623 head_copy_off = 0;
624 if (newheadroom <= head_copy_len)
625 head_copy_len = newheadroom;
626 else
627 head_copy_off = newheadroom - head_copy_len;
629 /* Copy the linear header and data. */
630 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
631 skb->len + head_copy_len))
632 BUG();
634 copy_skb_header(n, skb);
635 skb_shinfo(n)->tso_size = skb_shinfo(skb)->tso_size;
636 skb_shinfo(n)->tso_segs = skb_shinfo(skb)->tso_segs;
638 return n;
642 * skb_pad - zero pad the tail of an skb
643 * @skb: buffer to pad
644 * @pad: space to pad
646 * Ensure that a buffer is followed by a padding area that is zero
647 * filled. Used by network drivers which may DMA or transfer data
648 * beyond the buffer end onto the wire.
650 * May return NULL in out of memory cases.
653 struct sk_buff *skb_pad(struct sk_buff *skb, int pad)
655 struct sk_buff *nskb;
657 /* If the skbuff is non linear tailroom is always zero.. */
658 if (skb_tailroom(skb) >= pad) {
659 memset(skb->data+skb->len, 0, pad);
660 return skb;
663 nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC);
664 kfree_skb(skb);
665 if (nskb)
666 memset(nskb->data+nskb->len, 0, pad);
667 return nskb;
670 /* Trims skb to length len. It can change skb pointers, if "realloc" is 1.
671 * If realloc==0 and trimming is impossible without change of data,
672 * it is BUG().
675 int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc)
677 int offset = skb_headlen(skb);
678 int nfrags = skb_shinfo(skb)->nr_frags;
679 int i;
681 for (i = 0; i < nfrags; i++) {
682 int end = offset + skb_shinfo(skb)->frags[i].size;
683 if (end > len) {
684 if (skb_cloned(skb)) {
685 if (!realloc)
686 BUG();
687 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
688 return -ENOMEM;
690 if (len <= offset) {
691 put_page(skb_shinfo(skb)->frags[i].page);
692 skb_shinfo(skb)->nr_frags--;
693 } else {
694 skb_shinfo(skb)->frags[i].size = len - offset;
697 offset = end;
700 if (offset < len) {
701 skb->data_len -= skb->len - len;
702 skb->len = len;
703 } else {
704 if (len <= skb_headlen(skb)) {
705 skb->len = len;
706 skb->data_len = 0;
707 skb->tail = skb->data + len;
708 if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
709 skb_drop_fraglist(skb);
710 } else {
711 skb->data_len -= skb->len - len;
712 skb->len = len;
716 return 0;
720 * __pskb_pull_tail - advance tail of skb header
721 * @skb: buffer to reallocate
722 * @delta: number of bytes to advance tail
724 * The function makes a sense only on a fragmented &sk_buff,
725 * it expands header moving its tail forward and copying necessary
726 * data from fragmented part.
728 * &sk_buff MUST have reference count of 1.
730 * Returns %NULL (and &sk_buff does not change) if pull failed
731 * or value of new tail of skb in the case of success.
733 * All the pointers pointing into skb header may change and must be
734 * reloaded after call to this function.
737 /* Moves tail of skb head forward, copying data from fragmented part,
738 * when it is necessary.
739 * 1. It may fail due to malloc failure.
740 * 2. It may change skb pointers.
742 * It is pretty complicated. Luckily, it is called only in exceptional cases.
744 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
746 /* If skb has not enough free space at tail, get new one
747 * plus 128 bytes for future expansions. If we have enough
748 * room at tail, reallocate without expansion only if skb is cloned.
750 int i, k, eat = (skb->tail + delta) - skb->end;
752 if (eat > 0 || skb_cloned(skb)) {
753 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
754 GFP_ATOMIC))
755 return NULL;
758 if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
759 BUG();
761 /* Optimization: no fragments, no reasons to preestimate
762 * size of pulled pages. Superb.
764 if (!skb_shinfo(skb)->frag_list)
765 goto pull_pages;
767 /* Estimate size of pulled pages. */
768 eat = delta;
769 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
770 if (skb_shinfo(skb)->frags[i].size >= eat)
771 goto pull_pages;
772 eat -= skb_shinfo(skb)->frags[i].size;
775 /* If we need update frag list, we are in troubles.
776 * Certainly, it possible to add an offset to skb data,
777 * but taking into account that pulling is expected to
778 * be very rare operation, it is worth to fight against
779 * further bloating skb head and crucify ourselves here instead.
780 * Pure masohism, indeed. 8)8)
782 if (eat) {
783 struct sk_buff *list = skb_shinfo(skb)->frag_list;
784 struct sk_buff *clone = NULL;
785 struct sk_buff *insp = NULL;
787 do {
788 if (!list)
789 BUG();
791 if (list->len <= eat) {
792 /* Eaten as whole. */
793 eat -= list->len;
794 list = list->next;
795 insp = list;
796 } else {
797 /* Eaten partially. */
799 if (skb_shared(list)) {
800 /* Sucks! We need to fork list. :-( */
801 clone = skb_clone(list, GFP_ATOMIC);
802 if (!clone)
803 return NULL;
804 insp = list->next;
805 list = clone;
806 } else {
807 /* This may be pulled without
808 * problems. */
809 insp = list;
811 if (!pskb_pull(list, eat)) {
812 if (clone)
813 kfree_skb(clone);
814 return NULL;
816 break;
818 } while (eat);
820 /* Free pulled out fragments. */
821 while ((list = skb_shinfo(skb)->frag_list) != insp) {
822 skb_shinfo(skb)->frag_list = list->next;
823 kfree_skb(list);
825 /* And insert new clone at head. */
826 if (clone) {
827 clone->next = list;
828 skb_shinfo(skb)->frag_list = clone;
831 /* Success! Now we may commit changes to skb data. */
833 pull_pages:
834 eat = delta;
835 k = 0;
836 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
837 if (skb_shinfo(skb)->frags[i].size <= eat) {
838 put_page(skb_shinfo(skb)->frags[i].page);
839 eat -= skb_shinfo(skb)->frags[i].size;
840 } else {
841 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
842 if (eat) {
843 skb_shinfo(skb)->frags[k].page_offset += eat;
844 skb_shinfo(skb)->frags[k].size -= eat;
845 eat = 0;
847 k++;
850 skb_shinfo(skb)->nr_frags = k;
852 skb->tail += delta;
853 skb->data_len -= delta;
855 return skb->tail;
858 /* Copy some data bits from skb to kernel buffer. */
860 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
862 int i, copy;
863 int start = skb_headlen(skb);
865 if (offset > (int)skb->len - len)
866 goto fault;
868 /* Copy header. */
869 if ((copy = start - offset) > 0) {
870 if (copy > len)
871 copy = len;
872 memcpy(to, skb->data + offset, copy);
873 if ((len -= copy) == 0)
874 return 0;
875 offset += copy;
876 to += copy;
879 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
880 int end;
882 BUG_TRAP(start <= offset + len);
884 end = start + skb_shinfo(skb)->frags[i].size;
885 if ((copy = end - offset) > 0) {
886 u8 *vaddr;
888 if (copy > len)
889 copy = len;
891 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
892 memcpy(to,
893 vaddr + skb_shinfo(skb)->frags[i].page_offset+
894 offset - start, copy);
895 kunmap_skb_frag(vaddr);
897 if ((len -= copy) == 0)
898 return 0;
899 offset += copy;
900 to += copy;
902 start = end;
905 if (skb_shinfo(skb)->frag_list) {
906 struct sk_buff *list = skb_shinfo(skb)->frag_list;
908 for (; list; list = list->next) {
909 int end;
911 BUG_TRAP(start <= offset + len);
913 end = start + list->len;
914 if ((copy = end - offset) > 0) {
915 if (copy > len)
916 copy = len;
917 if (skb_copy_bits(list, offset - start,
918 to, copy))
919 goto fault;
920 if ((len -= copy) == 0)
921 return 0;
922 offset += copy;
923 to += copy;
925 start = end;
928 if (!len)
929 return 0;
931 fault:
932 return -EFAULT;
935 /* Keep iterating until skb_iter_next returns false. */
936 void skb_iter_first(const struct sk_buff *skb, struct skb_iter *i)
938 i->len = skb_headlen(skb);
939 i->data = (unsigned char *)skb->data;
940 i->nextfrag = 0;
941 i->fraglist = NULL;
944 int skb_iter_next(const struct sk_buff *skb, struct skb_iter *i)
946 /* Unmap previous, if not head fragment. */
947 if (i->nextfrag)
948 kunmap_skb_frag(i->data);
950 if (i->fraglist) {
951 fraglist:
952 /* We're iterating through fraglist. */
953 if (i->nextfrag < skb_shinfo(i->fraglist)->nr_frags) {
954 i->data = kmap_skb_frag(&skb_shinfo(i->fraglist)
955 ->frags[i->nextfrag]);
956 i->len = skb_shinfo(i->fraglist)->frags[i->nextfrag]
957 .size;
958 i->nextfrag++;
959 return 1;
961 /* Fragments with fragments? Too hard! */
962 BUG_ON(skb_shinfo(i->fraglist)->frag_list);
963 i->fraglist = i->fraglist->next;
964 if (!i->fraglist)
965 goto end;
967 i->len = skb_headlen(i->fraglist);
968 i->data = i->fraglist->data;
969 i->nextfrag = 0;
970 return 1;
973 if (i->nextfrag < skb_shinfo(skb)->nr_frags) {
974 i->data = kmap_skb_frag(&skb_shinfo(skb)->frags[i->nextfrag]);
975 i->len = skb_shinfo(skb)->frags[i->nextfrag].size;
976 i->nextfrag++;
977 return 1;
980 i->fraglist = skb_shinfo(skb)->frag_list;
981 if (i->fraglist)
982 goto fraglist;
984 end:
985 /* Bug trap for callers */
986 i->data = NULL;
987 return 0;
990 void skb_iter_abort(const struct sk_buff *skb, struct skb_iter *i)
992 /* Unmap previous, if not head fragment. */
993 if (i->data && i->nextfrag)
994 kunmap_skb_frag(i->data);
995 /* Bug trap for callers */
996 i->data = NULL;
999 /* Checksum skb data. */
1001 unsigned int skb_checksum(const struct sk_buff *skb, int offset,
1002 int len, unsigned int csum)
1004 int start = skb_headlen(skb);
1005 int i, copy = start - offset;
1006 int pos = 0;
1008 /* Checksum header. */
1009 if (copy > 0) {
1010 if (copy > len)
1011 copy = len;
1012 csum = csum_partial(skb->data + offset, copy, csum);
1013 if ((len -= copy) == 0)
1014 return csum;
1015 offset += copy;
1016 pos = copy;
1019 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1020 int end;
1022 BUG_TRAP(start <= offset + len);
1024 end = start + skb_shinfo(skb)->frags[i].size;
1025 if ((copy = end - offset) > 0) {
1026 unsigned int csum2;
1027 u8 *vaddr;
1028 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1030 if (copy > len)
1031 copy = len;
1032 vaddr = kmap_skb_frag(frag);
1033 csum2 = csum_partial(vaddr + frag->page_offset +
1034 offset - start, copy, 0);
1035 kunmap_skb_frag(vaddr);
1036 csum = csum_block_add(csum, csum2, pos);
1037 if (!(len -= copy))
1038 return csum;
1039 offset += copy;
1040 pos += copy;
1042 start = end;
1045 if (skb_shinfo(skb)->frag_list) {
1046 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1048 for (; list; list = list->next) {
1049 int end;
1051 BUG_TRAP(start <= offset + len);
1053 end = start + list->len;
1054 if ((copy = end - offset) > 0) {
1055 unsigned int csum2;
1056 if (copy > len)
1057 copy = len;
1058 csum2 = skb_checksum(list, offset - start,
1059 copy, 0);
1060 csum = csum_block_add(csum, csum2, pos);
1061 if ((len -= copy) == 0)
1062 return csum;
1063 offset += copy;
1064 pos += copy;
1066 start = end;
1069 if (len)
1070 BUG();
1072 return csum;
1075 /* Both of above in one bottle. */
1077 unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1078 u8 *to, int len, unsigned int csum)
1080 int start = skb_headlen(skb);
1081 int i, copy = start - offset;
1082 int pos = 0;
1084 /* Copy header. */
1085 if (copy > 0) {
1086 if (copy > len)
1087 copy = len;
1088 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1089 copy, csum);
1090 if ((len -= copy) == 0)
1091 return csum;
1092 offset += copy;
1093 to += copy;
1094 pos = copy;
1097 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1098 int end;
1100 BUG_TRAP(start <= offset + len);
1102 end = start + skb_shinfo(skb)->frags[i].size;
1103 if ((copy = end - offset) > 0) {
1104 unsigned int csum2;
1105 u8 *vaddr;
1106 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1108 if (copy > len)
1109 copy = len;
1110 vaddr = kmap_skb_frag(frag);
1111 csum2 = csum_partial_copy_nocheck(vaddr +
1112 frag->page_offset +
1113 offset - start, to,
1114 copy, 0);
1115 kunmap_skb_frag(vaddr);
1116 csum = csum_block_add(csum, csum2, pos);
1117 if (!(len -= copy))
1118 return csum;
1119 offset += copy;
1120 to += copy;
1121 pos += copy;
1123 start = end;
1126 if (skb_shinfo(skb)->frag_list) {
1127 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1129 for (; list; list = list->next) {
1130 unsigned int csum2;
1131 int end;
1133 BUG_TRAP(start <= offset + len);
1135 end = start + list->len;
1136 if ((copy = end - offset) > 0) {
1137 if (copy > len)
1138 copy = len;
1139 csum2 = skb_copy_and_csum_bits(list,
1140 offset - start,
1141 to, copy, 0);
1142 csum = csum_block_add(csum, csum2, pos);
1143 if ((len -= copy) == 0)
1144 return csum;
1145 offset += copy;
1146 to += copy;
1147 pos += copy;
1149 start = end;
1152 if (len)
1153 BUG();
1154 return csum;
1157 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1159 unsigned int csum;
1160 long csstart;
1162 if (skb->ip_summed == CHECKSUM_HW)
1163 csstart = skb->h.raw - skb->data;
1164 else
1165 csstart = skb_headlen(skb);
1167 if (csstart > skb_headlen(skb))
1168 BUG();
1170 memcpy(to, skb->data, csstart);
1172 csum = 0;
1173 if (csstart != skb->len)
1174 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1175 skb->len - csstart, 0);
1177 if (skb->ip_summed == CHECKSUM_HW) {
1178 long csstuff = csstart + skb->csum;
1180 *((unsigned short *)(to + csstuff)) = csum_fold(csum);
1185 * skb_dequeue - remove from the head of the queue
1186 * @list: list to dequeue from
1188 * Remove the head of the list. The list lock is taken so the function
1189 * may be used safely with other locking list functions. The head item is
1190 * returned or %NULL if the list is empty.
1193 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1195 unsigned long flags;
1196 struct sk_buff *result;
1198 spin_lock_irqsave(&list->lock, flags);
1199 result = __skb_dequeue(list);
1200 spin_unlock_irqrestore(&list->lock, flags);
1201 return result;
1205 * skb_dequeue_tail - remove from the tail of the queue
1206 * @list: list to dequeue from
1208 * Remove the tail of the list. The list lock is taken so the function
1209 * may be used safely with other locking list functions. The tail item is
1210 * returned or %NULL if the list is empty.
1212 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1214 unsigned long flags;
1215 struct sk_buff *result;
1217 spin_lock_irqsave(&list->lock, flags);
1218 result = __skb_dequeue_tail(list);
1219 spin_unlock_irqrestore(&list->lock, flags);
1220 return result;
1224 * skb_queue_purge - empty a list
1225 * @list: list to empty
1227 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1228 * the list and one reference dropped. This function takes the list
1229 * lock and is atomic with respect to other list locking functions.
1231 void skb_queue_purge(struct sk_buff_head *list)
1233 struct sk_buff *skb;
1234 while ((skb = skb_dequeue(list)) != NULL)
1235 kfree_skb(skb);
1239 * skb_queue_head - queue a buffer at the list head
1240 * @list: list to use
1241 * @newsk: buffer to queue
1243 * Queue a buffer at the start of the list. This function takes the
1244 * list lock and can be used safely with other locking &sk_buff functions
1245 * safely.
1247 * A buffer cannot be placed on two lists at the same time.
1249 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1251 unsigned long flags;
1253 spin_lock_irqsave(&list->lock, flags);
1254 __skb_queue_head(list, newsk);
1255 spin_unlock_irqrestore(&list->lock, flags);
1259 * skb_queue_tail - queue a buffer at the list tail
1260 * @list: list to use
1261 * @newsk: buffer to queue
1263 * Queue a buffer at the tail of the list. This function takes the
1264 * list lock and can be used safely with other locking &sk_buff functions
1265 * safely.
1267 * A buffer cannot be placed on two lists at the same time.
1269 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1271 unsigned long flags;
1273 spin_lock_irqsave(&list->lock, flags);
1274 __skb_queue_tail(list, newsk);
1275 spin_unlock_irqrestore(&list->lock, flags);
1278 * skb_unlink - remove a buffer from a list
1279 * @skb: buffer to remove
1281 * Place a packet after a given packet in a list. The list locks are taken
1282 * and this function is atomic with respect to other list locked calls
1284 * Works even without knowing the list it is sitting on, which can be
1285 * handy at times. It also means that THE LIST MUST EXIST when you
1286 * unlink. Thus a list must have its contents unlinked before it is
1287 * destroyed.
1289 void skb_unlink(struct sk_buff *skb)
1291 struct sk_buff_head *list = skb->list;
1293 if (list) {
1294 unsigned long flags;
1296 spin_lock_irqsave(&list->lock, flags);
1297 if (skb->list == list)
1298 __skb_unlink(skb, skb->list);
1299 spin_unlock_irqrestore(&list->lock, flags);
1305 * skb_append - append a buffer
1306 * @old: buffer to insert after
1307 * @newsk: buffer to insert
1309 * Place a packet after a given packet in a list. The list locks are taken
1310 * and this function is atomic with respect to other list locked calls.
1311 * A buffer cannot be placed on two lists at the same time.
1314 void skb_append(struct sk_buff *old, struct sk_buff *newsk)
1316 unsigned long flags;
1318 spin_lock_irqsave(&old->list->lock, flags);
1319 __skb_append(old, newsk);
1320 spin_unlock_irqrestore(&old->list->lock, flags);
1325 * skb_insert - insert a buffer
1326 * @old: buffer to insert before
1327 * @newsk: buffer to insert
1329 * Place a packet before a given packet in a list. The list locks are taken
1330 * and this function is atomic with respect to other list locked calls
1331 * A buffer cannot be placed on two lists at the same time.
1334 void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
1336 unsigned long flags;
1338 spin_lock_irqsave(&old->list->lock, flags);
1339 __skb_insert(newsk, old->prev, old, old->list);
1340 spin_unlock_irqrestore(&old->list->lock, flags);
1343 #if 0
1345 * Tune the memory allocator for a new MTU size.
1347 void skb_add_mtu(int mtu)
1349 /* Must match allocation in alloc_skb */
1350 mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
1352 kmem_add_cache_size(mtu);
1354 #endif
1356 static void inline skb_split_inside_header(struct sk_buff *skb,
1357 struct sk_buff* skb1,
1358 const u32 len, const int pos)
1360 int i;
1362 memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
1364 /* And move data appendix as is. */
1365 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1366 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1368 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1369 skb_shinfo(skb)->nr_frags = 0;
1370 skb1->data_len = skb->data_len;
1371 skb1->len += skb1->data_len;
1372 skb->data_len = 0;
1373 skb->len = len;
1374 skb->tail = skb->data + len;
1377 static void inline skb_split_no_header(struct sk_buff *skb,
1378 struct sk_buff* skb1,
1379 const u32 len, int pos)
1381 int i, k = 0;
1382 const int nfrags = skb_shinfo(skb)->nr_frags;
1384 skb_shinfo(skb)->nr_frags = 0;
1385 skb1->len = skb1->data_len = skb->len - len;
1386 skb->len = len;
1387 skb->data_len = len - pos;
1389 for (i = 0; i < nfrags; i++) {
1390 int size = skb_shinfo(skb)->frags[i].size;
1392 if (pos + size > len) {
1393 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1395 if (pos < len) {
1396 /* Split frag.
1397 * We have to variants in this case:
1398 * 1. Move all the frag to the second
1399 * part, if it is possible. F.e.
1400 * this approach is mandatory for TUX,
1401 * where splitting is expensive.
1402 * 2. Split is accurately. We make this.
1404 get_page(skb_shinfo(skb)->frags[i].page);
1405 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1406 skb_shinfo(skb1)->frags[0].size -= len - pos;
1407 skb_shinfo(skb)->frags[i].size = len - pos;
1408 skb_shinfo(skb)->nr_frags++;
1410 k++;
1411 } else
1412 skb_shinfo(skb)->nr_frags++;
1413 pos += size;
1415 skb_shinfo(skb1)->nr_frags = k;
1419 * skb_split - Split fragmented skb to two parts at length len.
1421 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1423 int pos = skb_headlen(skb);
1425 if (len < pos) /* Split line is inside header. */
1426 skb_split_inside_header(skb, skb1, len, pos);
1427 else /* Second chunk has no header, nothing to copy. */
1428 skb_split_no_header(skb, skb1, len, pos);
1431 void __init skb_init(void)
1433 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
1434 sizeof(struct sk_buff),
1436 SLAB_HWCACHE_ALIGN,
1437 NULL, NULL);
1438 if (!skbuff_head_cache)
1439 panic("cannot create skbuff cache");
1442 EXPORT_SYMBOL(___pskb_trim);
1443 EXPORT_SYMBOL(__kfree_skb);
1444 EXPORT_SYMBOL(__pskb_pull_tail);
1445 EXPORT_SYMBOL(alloc_skb);
1446 EXPORT_SYMBOL(pskb_copy);
1447 EXPORT_SYMBOL(pskb_expand_head);
1448 EXPORT_SYMBOL(skb_checksum);
1449 EXPORT_SYMBOL(skb_clone);
1450 EXPORT_SYMBOL(skb_clone_fraglist);
1451 EXPORT_SYMBOL(skb_copy);
1452 EXPORT_SYMBOL(skb_copy_and_csum_bits);
1453 EXPORT_SYMBOL(skb_copy_and_csum_dev);
1454 EXPORT_SYMBOL(skb_copy_bits);
1455 EXPORT_SYMBOL(skb_copy_expand);
1456 EXPORT_SYMBOL(skb_over_panic);
1457 EXPORT_SYMBOL(skb_pad);
1458 EXPORT_SYMBOL(skb_realloc_headroom);
1459 EXPORT_SYMBOL(skb_under_panic);
1460 EXPORT_SYMBOL(skb_dequeue);
1461 EXPORT_SYMBOL(skb_dequeue_tail);
1462 EXPORT_SYMBOL(skb_insert);
1463 EXPORT_SYMBOL(skb_queue_purge);
1464 EXPORT_SYMBOL(skb_queue_head);
1465 EXPORT_SYMBOL(skb_queue_tail);
1466 EXPORT_SYMBOL(skb_unlink);
1467 EXPORT_SYMBOL(skb_append);
1468 EXPORT_SYMBOL(skb_split);
1469 EXPORT_SYMBOL(skb_iter_first);
1470 EXPORT_SYMBOL(skb_iter_next);
1471 EXPORT_SYMBOL(skb_iter_abort);