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 $
10 * Alan Cox : Fixed the worst of the load
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
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>
47 #include <linux/interrupt.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>
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>
65 #include <net/checksum.h>
68 #include <asm/uaccess.h>
69 #include <asm/system.h>
71 static kmem_cache_t
*skbuff_head_cache __read_mostly
;
72 static kmem_cache_t
*skbuff_fclone_cache __read_mostly
;
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
81 * skb_over_panic - private function
86 * Out of line support code for skb_put(). Not user callable.
88 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
90 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
93 skb
->dev
? skb
->dev
->name
: "<NULL>");
98 * skb_under_panic - private function
103 * Out of line support code for skb_push(). Not user callable.
106 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
108 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
111 skb
->dev
? skb
->dev
->name
: "<NULL>");
115 void skb_truesize_bug(struct sk_buff
*skb
)
117 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
121 EXPORT_SYMBOL(skb_truesize_bug
);
123 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
130 * __alloc_skb - allocate a network buffer
131 * @size: size to allocate
132 * @gfp_mask: allocation mask
133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
147 struct skb_shared_info
*shinfo
;
151 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
154 skb
= kmem_cache_alloc(cache
, gfp_mask
& ~__GFP_DMA
);
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size
= SKB_DATA_ALIGN(size
);
160 data
= ____kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
164 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
165 skb
->truesize
= size
+ sizeof(struct sk_buff
);
166 atomic_set(&skb
->users
, 1);
170 skb
->end
= data
+ size
;
171 /* make sure we initialize shinfo sequentially */
172 shinfo
= skb_shinfo(skb
);
173 atomic_set(&shinfo
->dataref
, 1);
174 shinfo
->nr_frags
= 0;
175 shinfo
->gso_size
= 0;
176 shinfo
->gso_segs
= 0;
177 shinfo
->gso_type
= 0;
178 shinfo
->ip6_frag_id
= 0;
179 shinfo
->frag_list
= NULL
;
182 struct sk_buff
*child
= skb
+ 1;
183 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
185 skb
->fclone
= SKB_FCLONE_ORIG
;
186 atomic_set(fclone_ref
, 1);
188 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
193 kmem_cache_free(cache
, skb
);
199 * alloc_skb_from_cache - allocate a network buffer
200 * @cp: kmem_cache from which to allocate the data area
201 * (object size must be big enough for @size bytes + skb overheads)
202 * @size: size to allocate
203 * @gfp_mask: allocation mask
205 * Allocate a new &sk_buff. The returned buffer has no headroom and
206 * tail room of size bytes. The object has a reference count of one.
207 * The return is the buffer. On a failure the return is %NULL.
209 * Buffers may only be allocated from interrupts using a @gfp_mask of
212 struct sk_buff
*alloc_skb_from_cache(kmem_cache_t
*cp
,
220 skb
= kmem_cache_alloc(skbuff_head_cache
,
221 gfp_mask
& ~__GFP_DMA
);
226 size
= SKB_DATA_ALIGN(size
);
227 data
= kmem_cache_alloc(cp
, gfp_mask
);
231 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
232 skb
->truesize
= size
+ sizeof(struct sk_buff
);
233 atomic_set(&skb
->users
, 1);
237 skb
->end
= data
+ size
;
239 atomic_set(&(skb_shinfo(skb
)->dataref
), 1);
240 skb_shinfo(skb
)->nr_frags
= 0;
241 skb_shinfo(skb
)->gso_size
= 0;
242 skb_shinfo(skb
)->gso_segs
= 0;
243 skb_shinfo(skb
)->gso_type
= 0;
244 skb_shinfo(skb
)->frag_list
= NULL
;
248 kmem_cache_free(skbuff_head_cache
, skb
);
254 static void skb_drop_fraglist(struct sk_buff
*skb
)
256 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
258 skb_shinfo(skb
)->frag_list
= NULL
;
261 struct sk_buff
*this = list
;
267 static void skb_clone_fraglist(struct sk_buff
*skb
)
269 struct sk_buff
*list
;
271 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
275 void skb_release_data(struct sk_buff
*skb
)
278 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
279 &skb_shinfo(skb
)->dataref
)) {
280 if (skb_shinfo(skb
)->nr_frags
) {
282 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
283 put_page(skb_shinfo(skb
)->frags
[i
].page
);
286 if (skb_shinfo(skb
)->frag_list
)
287 skb_drop_fraglist(skb
);
294 * Free an skbuff by memory without cleaning the state.
296 void kfree_skbmem(struct sk_buff
*skb
)
298 struct sk_buff
*other
;
299 atomic_t
*fclone_ref
;
301 skb_release_data(skb
);
302 switch (skb
->fclone
) {
303 case SKB_FCLONE_UNAVAILABLE
:
304 kmem_cache_free(skbuff_head_cache
, skb
);
307 case SKB_FCLONE_ORIG
:
308 fclone_ref
= (atomic_t
*) (skb
+ 2);
309 if (atomic_dec_and_test(fclone_ref
))
310 kmem_cache_free(skbuff_fclone_cache
, skb
);
313 case SKB_FCLONE_CLONE
:
314 fclone_ref
= (atomic_t
*) (skb
+ 1);
317 /* The clone portion is available for
318 * fast-cloning again.
320 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
322 if (atomic_dec_and_test(fclone_ref
))
323 kmem_cache_free(skbuff_fclone_cache
, other
);
329 * __kfree_skb - private function
332 * Free an sk_buff. Release anything attached to the buffer.
333 * Clean the state. This is an internal helper function. Users should
334 * always call kfree_skb
337 void __kfree_skb(struct sk_buff
*skb
)
339 dst_release(skb
->dst
);
341 secpath_put(skb
->sp
);
343 if (skb
->destructor
) {
345 skb
->destructor(skb
);
347 #ifdef CONFIG_NETFILTER
348 nf_conntrack_put(skb
->nfct
);
349 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
350 nf_conntrack_put_reasm(skb
->nfct_reasm
);
352 #ifdef CONFIG_BRIDGE_NETFILTER
353 nf_bridge_put(skb
->nf_bridge
);
356 /* XXX: IS this still necessary? - JHS */
357 #ifdef CONFIG_NET_SCHED
359 #ifdef CONFIG_NET_CLS_ACT
368 * kfree_skb - free an sk_buff
369 * @skb: buffer to free
371 * Drop a reference to the buffer and free it if the usage count has
374 void kfree_skb(struct sk_buff
*skb
)
378 if (likely(atomic_read(&skb
->users
) == 1))
380 else if (likely(!atomic_dec_and_test(&skb
->users
)))
386 * skb_clone - duplicate an sk_buff
387 * @skb: buffer to clone
388 * @gfp_mask: allocation priority
390 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
391 * copies share the same packet data but not structure. The new
392 * buffer has a reference count of 1. If the allocation fails the
393 * function returns %NULL otherwise the new buffer is returned.
395 * If this function is called from an interrupt gfp_mask() must be
399 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
404 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
405 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
406 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
407 n
->fclone
= SKB_FCLONE_CLONE
;
408 atomic_inc(fclone_ref
);
410 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
413 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
416 #define C(x) n->x = skb->x
418 n
->next
= n
->prev
= NULL
;
429 secpath_get(skb
->sp
);
431 memcpy(n
->cb
, skb
->cb
, sizeof(skb
->cb
));
441 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
445 n
->destructor
= NULL
;
446 #ifdef CONFIG_NETFILTER
449 nf_conntrack_get(skb
->nfct
);
451 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
453 nf_conntrack_get_reasm(skb
->nfct_reasm
);
455 #ifdef CONFIG_BRIDGE_NETFILTER
457 nf_bridge_get(skb
->nf_bridge
);
459 #endif /*CONFIG_NETFILTER*/
460 #ifdef CONFIG_NET_SCHED
462 #ifdef CONFIG_NET_CLS_ACT
463 n
->tc_verd
= SET_TC_VERD(skb
->tc_verd
,0);
464 n
->tc_verd
= CLR_TC_OK2MUNGE(n
->tc_verd
);
465 n
->tc_verd
= CLR_TC_MUNGED(n
->tc_verd
);
468 skb_copy_secmark(n
, skb
);
471 atomic_set(&n
->users
, 1);
477 atomic_inc(&(skb_shinfo(skb
)->dataref
));
483 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
486 * Shift between the two data areas in bytes
488 unsigned long offset
= new->data
- old
->data
;
492 new->priority
= old
->priority
;
493 new->protocol
= old
->protocol
;
494 new->dst
= dst_clone(old
->dst
);
496 new->sp
= secpath_get(old
->sp
);
498 new->h
.raw
= old
->h
.raw
+ offset
;
499 new->nh
.raw
= old
->nh
.raw
+ offset
;
500 new->mac
.raw
= old
->mac
.raw
+ offset
;
501 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
502 new->local_df
= old
->local_df
;
503 new->fclone
= SKB_FCLONE_UNAVAILABLE
;
504 new->pkt_type
= old
->pkt_type
;
505 new->tstamp
= old
->tstamp
;
506 new->destructor
= NULL
;
507 #ifdef CONFIG_NETFILTER
508 new->nfmark
= old
->nfmark
;
509 new->nfct
= old
->nfct
;
510 nf_conntrack_get(old
->nfct
);
511 new->nfctinfo
= old
->nfctinfo
;
512 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
513 new->nfct_reasm
= old
->nfct_reasm
;
514 nf_conntrack_get_reasm(old
->nfct_reasm
);
516 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
517 new->ipvs_property
= old
->ipvs_property
;
519 #ifdef CONFIG_BRIDGE_NETFILTER
520 new->nf_bridge
= old
->nf_bridge
;
521 nf_bridge_get(old
->nf_bridge
);
524 #ifdef CONFIG_NET_SCHED
525 #ifdef CONFIG_NET_CLS_ACT
526 new->tc_verd
= old
->tc_verd
;
528 new->tc_index
= old
->tc_index
;
530 skb_copy_secmark(new, old
);
531 atomic_set(&new->users
, 1);
532 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
533 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
534 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
538 * skb_copy - create private copy of an sk_buff
539 * @skb: buffer to copy
540 * @gfp_mask: allocation priority
542 * Make a copy of both an &sk_buff and its data. This is used when the
543 * caller wishes to modify the data and needs a private copy of the
544 * data to alter. Returns %NULL on failure or the pointer to the buffer
545 * on success. The returned buffer has a reference count of 1.
547 * As by-product this function converts non-linear &sk_buff to linear
548 * one, so that &sk_buff becomes completely private and caller is allowed
549 * to modify all the data of returned buffer. This means that this
550 * function is not recommended for use in circumstances when only
551 * header is going to be modified. Use pskb_copy() instead.
554 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
556 int headerlen
= skb
->data
- skb
->head
;
558 * Allocate the copy buffer
560 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
,
565 /* Set the data pointer */
566 skb_reserve(n
, headerlen
);
567 /* Set the tail pointer and length */
568 skb_put(n
, skb
->len
);
570 n
->ip_summed
= skb
->ip_summed
;
572 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
575 copy_skb_header(n
, skb
);
581 * pskb_copy - create copy of an sk_buff with private head.
582 * @skb: buffer to copy
583 * @gfp_mask: allocation priority
585 * Make a copy of both an &sk_buff and part of its data, located
586 * in header. Fragmented data remain shared. This is used when
587 * the caller wishes to modify only header of &sk_buff and needs
588 * private copy of the header to alter. Returns %NULL on failure
589 * or the pointer to the buffer on success.
590 * The returned buffer has a reference count of 1.
593 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
596 * Allocate the copy buffer
598 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
603 /* Set the data pointer */
604 skb_reserve(n
, skb
->data
- skb
->head
);
605 /* Set the tail pointer and length */
606 skb_put(n
, skb_headlen(skb
));
608 memcpy(n
->data
, skb
->data
, n
->len
);
610 n
->ip_summed
= skb
->ip_summed
;
612 n
->data_len
= skb
->data_len
;
615 if (skb_shinfo(skb
)->nr_frags
) {
618 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
619 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
620 get_page(skb_shinfo(n
)->frags
[i
].page
);
622 skb_shinfo(n
)->nr_frags
= i
;
625 if (skb_shinfo(skb
)->frag_list
) {
626 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
627 skb_clone_fraglist(n
);
630 copy_skb_header(n
, skb
);
636 * pskb_expand_head - reallocate header of &sk_buff
637 * @skb: buffer to reallocate
638 * @nhead: room to add at head
639 * @ntail: room to add at tail
640 * @gfp_mask: allocation priority
642 * Expands (or creates identical copy, if &nhead and &ntail are zero)
643 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
644 * reference count of 1. Returns zero in the case of success or error,
645 * if expansion failed. In the last case, &sk_buff is not changed.
647 * All the pointers pointing into skb header may change and must be
648 * reloaded after call to this function.
651 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
656 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
662 size
= SKB_DATA_ALIGN(size
);
664 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
668 /* Copy only real data... and, alas, header. This should be
669 * optimized for the cases when header is void. */
670 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
671 memcpy(data
+ size
, skb
->end
, sizeof(struct skb_shared_info
));
673 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
674 get_page(skb_shinfo(skb
)->frags
[i
].page
);
676 if (skb_shinfo(skb
)->frag_list
)
677 skb_clone_fraglist(skb
);
679 skb_release_data(skb
);
681 off
= (data
+ nhead
) - skb
->head
;
684 skb
->end
= data
+ size
;
692 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
699 /* Make private copy of skb with writable head and some headroom */
701 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
703 struct sk_buff
*skb2
;
704 int delta
= headroom
- skb_headroom(skb
);
707 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
709 skb2
= skb_clone(skb
, GFP_ATOMIC
);
710 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
721 * skb_copy_expand - copy and expand sk_buff
722 * @skb: buffer to copy
723 * @newheadroom: new free bytes at head
724 * @newtailroom: new free bytes at tail
725 * @gfp_mask: allocation priority
727 * Make a copy of both an &sk_buff and its data and while doing so
728 * allocate additional space.
730 * This is used when the caller wishes to modify the data and needs a
731 * private copy of the data to alter as well as more space for new fields.
732 * Returns %NULL on failure or the pointer to the buffer
733 * on success. The returned buffer has a reference count of 1.
735 * You must pass %GFP_ATOMIC as the allocation priority if this function
736 * is called from an interrupt.
738 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
739 * only by netfilter in the cases when checksum is recalculated? --ANK
741 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
742 int newheadroom
, int newtailroom
,
746 * Allocate the copy buffer
748 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
750 int head_copy_len
, head_copy_off
;
755 skb_reserve(n
, newheadroom
);
757 /* Set the tail pointer and length */
758 skb_put(n
, skb
->len
);
760 head_copy_len
= skb_headroom(skb
);
762 if (newheadroom
<= head_copy_len
)
763 head_copy_len
= newheadroom
;
765 head_copy_off
= newheadroom
- head_copy_len
;
767 /* Copy the linear header and data. */
768 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
769 skb
->len
+ head_copy_len
))
772 copy_skb_header(n
, skb
);
778 * skb_pad - zero pad the tail of an skb
779 * @skb: buffer to pad
782 * Ensure that a buffer is followed by a padding area that is zero
783 * filled. Used by network drivers which may DMA or transfer data
784 * beyond the buffer end onto the wire.
786 * May return error in out of memory cases. The skb is freed on error.
789 int skb_pad(struct sk_buff
*skb
, int pad
)
794 /* If the skbuff is non linear tailroom is always zero.. */
795 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
796 memset(skb
->data
+skb
->len
, 0, pad
);
800 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
801 if (likely(skb_cloned(skb
) || ntail
> 0)) {
802 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
807 /* FIXME: The use of this function with non-linear skb's really needs
810 err
= skb_linearize(skb
);
814 memset(skb
->data
+ skb
->len
, 0, pad
);
822 /* Trims skb to length len. It can change skb pointers.
825 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
827 int offset
= skb_headlen(skb
);
828 int nfrags
= skb_shinfo(skb
)->nr_frags
;
831 for (i
= 0; i
< nfrags
; i
++) {
832 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
834 if (skb_cloned(skb
)) {
835 if (pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
839 put_page(skb_shinfo(skb
)->frags
[i
].page
);
840 skb_shinfo(skb
)->nr_frags
--;
842 skb_shinfo(skb
)->frags
[i
].size
= len
- offset
;
849 skb
->data_len
-= skb
->len
- len
;
852 if (len
<= skb_headlen(skb
)) {
855 skb
->tail
= skb
->data
+ len
;
856 if (skb_shinfo(skb
)->frag_list
&& !skb_cloned(skb
))
857 skb_drop_fraglist(skb
);
859 skb
->data_len
-= skb
->len
- len
;
868 * __pskb_pull_tail - advance tail of skb header
869 * @skb: buffer to reallocate
870 * @delta: number of bytes to advance tail
872 * The function makes a sense only on a fragmented &sk_buff,
873 * it expands header moving its tail forward and copying necessary
874 * data from fragmented part.
876 * &sk_buff MUST have reference count of 1.
878 * Returns %NULL (and &sk_buff does not change) if pull failed
879 * or value of new tail of skb in the case of success.
881 * All the pointers pointing into skb header may change and must be
882 * reloaded after call to this function.
885 /* Moves tail of skb head forward, copying data from fragmented part,
886 * when it is necessary.
887 * 1. It may fail due to malloc failure.
888 * 2. It may change skb pointers.
890 * It is pretty complicated. Luckily, it is called only in exceptional cases.
892 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
894 /* If skb has not enough free space at tail, get new one
895 * plus 128 bytes for future expansions. If we have enough
896 * room at tail, reallocate without expansion only if skb is cloned.
898 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
900 if (eat
> 0 || skb_cloned(skb
)) {
901 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
906 if (skb_copy_bits(skb
, skb_headlen(skb
), skb
->tail
, delta
))
909 /* Optimization: no fragments, no reasons to preestimate
910 * size of pulled pages. Superb.
912 if (!skb_shinfo(skb
)->frag_list
)
915 /* Estimate size of pulled pages. */
917 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
918 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
920 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
923 /* If we need update frag list, we are in troubles.
924 * Certainly, it possible to add an offset to skb data,
925 * but taking into account that pulling is expected to
926 * be very rare operation, it is worth to fight against
927 * further bloating skb head and crucify ourselves here instead.
928 * Pure masohism, indeed. 8)8)
931 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
932 struct sk_buff
*clone
= NULL
;
933 struct sk_buff
*insp
= NULL
;
938 if (list
->len
<= eat
) {
939 /* Eaten as whole. */
944 /* Eaten partially. */
946 if (skb_shared(list
)) {
947 /* Sucks! We need to fork list. :-( */
948 clone
= skb_clone(list
, GFP_ATOMIC
);
954 /* This may be pulled without
958 if (!pskb_pull(list
, eat
)) {
967 /* Free pulled out fragments. */
968 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
969 skb_shinfo(skb
)->frag_list
= list
->next
;
972 /* And insert new clone at head. */
975 skb_shinfo(skb
)->frag_list
= clone
;
978 /* Success! Now we may commit changes to skb data. */
983 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
984 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
985 put_page(skb_shinfo(skb
)->frags
[i
].page
);
986 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
988 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
990 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
991 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
997 skb_shinfo(skb
)->nr_frags
= k
;
1000 skb
->data_len
-= delta
;
1005 /* Copy some data bits from skb to kernel buffer. */
1007 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1010 int start
= skb_headlen(skb
);
1012 if (offset
> (int)skb
->len
- len
)
1016 if ((copy
= start
- offset
) > 0) {
1019 memcpy(to
, skb
->data
+ offset
, copy
);
1020 if ((len
-= copy
) == 0)
1026 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1029 BUG_TRAP(start
<= offset
+ len
);
1031 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1032 if ((copy
= end
- offset
) > 0) {
1038 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1040 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1041 offset
- start
, copy
);
1042 kunmap_skb_frag(vaddr
);
1044 if ((len
-= copy
) == 0)
1052 if (skb_shinfo(skb
)->frag_list
) {
1053 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1055 for (; list
; list
= list
->next
) {
1058 BUG_TRAP(start
<= offset
+ len
);
1060 end
= start
+ list
->len
;
1061 if ((copy
= end
- offset
) > 0) {
1064 if (skb_copy_bits(list
, offset
- start
,
1067 if ((len
-= copy
) == 0)
1083 * skb_store_bits - store bits from kernel buffer to skb
1084 * @skb: destination buffer
1085 * @offset: offset in destination
1086 * @from: source buffer
1087 * @len: number of bytes to copy
1089 * Copy the specified number of bytes from the source buffer to the
1090 * destination skb. This function handles all the messy bits of
1091 * traversing fragment lists and such.
1094 int skb_store_bits(const struct sk_buff
*skb
, int offset
, void *from
, int len
)
1097 int start
= skb_headlen(skb
);
1099 if (offset
> (int)skb
->len
- len
)
1102 if ((copy
= start
- offset
) > 0) {
1105 memcpy(skb
->data
+ offset
, from
, copy
);
1106 if ((len
-= copy
) == 0)
1112 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1113 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1116 BUG_TRAP(start
<= offset
+ len
);
1118 end
= start
+ frag
->size
;
1119 if ((copy
= end
- offset
) > 0) {
1125 vaddr
= kmap_skb_frag(frag
);
1126 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1128 kunmap_skb_frag(vaddr
);
1130 if ((len
-= copy
) == 0)
1138 if (skb_shinfo(skb
)->frag_list
) {
1139 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1141 for (; list
; list
= list
->next
) {
1144 BUG_TRAP(start
<= offset
+ len
);
1146 end
= start
+ list
->len
;
1147 if ((copy
= end
- offset
) > 0) {
1150 if (skb_store_bits(list
, offset
- start
,
1153 if ((len
-= copy
) == 0)
1168 EXPORT_SYMBOL(skb_store_bits
);
1170 /* Checksum skb data. */
1172 unsigned int skb_checksum(const struct sk_buff
*skb
, int offset
,
1173 int len
, unsigned int csum
)
1175 int start
= skb_headlen(skb
);
1176 int i
, copy
= start
- offset
;
1179 /* Checksum header. */
1183 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1184 if ((len
-= copy
) == 0)
1190 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1193 BUG_TRAP(start
<= offset
+ len
);
1195 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1196 if ((copy
= end
- offset
) > 0) {
1199 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1203 vaddr
= kmap_skb_frag(frag
);
1204 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1205 offset
- start
, copy
, 0);
1206 kunmap_skb_frag(vaddr
);
1207 csum
= csum_block_add(csum
, csum2
, pos
);
1216 if (skb_shinfo(skb
)->frag_list
) {
1217 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1219 for (; list
; list
= list
->next
) {
1222 BUG_TRAP(start
<= offset
+ len
);
1224 end
= start
+ list
->len
;
1225 if ((copy
= end
- offset
) > 0) {
1229 csum2
= skb_checksum(list
, offset
- start
,
1231 csum
= csum_block_add(csum
, csum2
, pos
);
1232 if ((len
-= copy
) == 0)
1245 /* Both of above in one bottle. */
1247 unsigned int skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1248 u8
*to
, int len
, unsigned int csum
)
1250 int start
= skb_headlen(skb
);
1251 int i
, copy
= start
- offset
;
1258 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1260 if ((len
-= copy
) == 0)
1267 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1270 BUG_TRAP(start
<= offset
+ len
);
1272 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1273 if ((copy
= end
- offset
) > 0) {
1276 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1280 vaddr
= kmap_skb_frag(frag
);
1281 csum2
= csum_partial_copy_nocheck(vaddr
+
1285 kunmap_skb_frag(vaddr
);
1286 csum
= csum_block_add(csum
, csum2
, pos
);
1296 if (skb_shinfo(skb
)->frag_list
) {
1297 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1299 for (; list
; list
= list
->next
) {
1303 BUG_TRAP(start
<= offset
+ len
);
1305 end
= start
+ list
->len
;
1306 if ((copy
= end
- offset
) > 0) {
1309 csum2
= skb_copy_and_csum_bits(list
,
1312 csum
= csum_block_add(csum
, csum2
, pos
);
1313 if ((len
-= copy
) == 0)
1326 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1331 if (skb
->ip_summed
== CHECKSUM_HW
)
1332 csstart
= skb
->h
.raw
- skb
->data
;
1334 csstart
= skb_headlen(skb
);
1336 BUG_ON(csstart
> skb_headlen(skb
));
1338 memcpy(to
, skb
->data
, csstart
);
1341 if (csstart
!= skb
->len
)
1342 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1343 skb
->len
- csstart
, 0);
1345 if (skb
->ip_summed
== CHECKSUM_HW
) {
1346 long csstuff
= csstart
+ skb
->csum
;
1348 *((unsigned short *)(to
+ csstuff
)) = csum_fold(csum
);
1353 * skb_dequeue - remove from the head of the queue
1354 * @list: list to dequeue from
1356 * Remove the head of the list. The list lock is taken so the function
1357 * may be used safely with other locking list functions. The head item is
1358 * returned or %NULL if the list is empty.
1361 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1363 unsigned long flags
;
1364 struct sk_buff
*result
;
1366 spin_lock_irqsave(&list
->lock
, flags
);
1367 result
= __skb_dequeue(list
);
1368 spin_unlock_irqrestore(&list
->lock
, flags
);
1373 * skb_dequeue_tail - remove from the tail of the queue
1374 * @list: list to dequeue from
1376 * Remove the tail of the list. The list lock is taken so the function
1377 * may be used safely with other locking list functions. The tail item is
1378 * returned or %NULL if the list is empty.
1380 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1382 unsigned long flags
;
1383 struct sk_buff
*result
;
1385 spin_lock_irqsave(&list
->lock
, flags
);
1386 result
= __skb_dequeue_tail(list
);
1387 spin_unlock_irqrestore(&list
->lock
, flags
);
1392 * skb_queue_purge - empty a list
1393 * @list: list to empty
1395 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1396 * the list and one reference dropped. This function takes the list
1397 * lock and is atomic with respect to other list locking functions.
1399 void skb_queue_purge(struct sk_buff_head
*list
)
1401 struct sk_buff
*skb
;
1402 while ((skb
= skb_dequeue(list
)) != NULL
)
1407 * skb_queue_head - queue a buffer at the list head
1408 * @list: list to use
1409 * @newsk: buffer to queue
1411 * Queue a buffer at the start of the list. This function takes the
1412 * list lock and can be used safely with other locking &sk_buff functions
1415 * A buffer cannot be placed on two lists at the same time.
1417 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1419 unsigned long flags
;
1421 spin_lock_irqsave(&list
->lock
, flags
);
1422 __skb_queue_head(list
, newsk
);
1423 spin_unlock_irqrestore(&list
->lock
, flags
);
1427 * skb_queue_tail - queue a buffer at the list tail
1428 * @list: list to use
1429 * @newsk: buffer to queue
1431 * Queue a buffer at the tail of the list. This function takes the
1432 * list lock and can be used safely with other locking &sk_buff functions
1435 * A buffer cannot be placed on two lists at the same time.
1437 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1439 unsigned long flags
;
1441 spin_lock_irqsave(&list
->lock
, flags
);
1442 __skb_queue_tail(list
, newsk
);
1443 spin_unlock_irqrestore(&list
->lock
, flags
);
1447 * skb_unlink - remove a buffer from a list
1448 * @skb: buffer to remove
1449 * @list: list to use
1451 * Remove a packet from a list. The list locks are taken and this
1452 * function is atomic with respect to other list locked calls
1454 * You must know what list the SKB is on.
1456 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1458 unsigned long flags
;
1460 spin_lock_irqsave(&list
->lock
, flags
);
1461 __skb_unlink(skb
, list
);
1462 spin_unlock_irqrestore(&list
->lock
, flags
);
1466 * skb_append - append a buffer
1467 * @old: buffer to insert after
1468 * @newsk: buffer to insert
1469 * @list: list to use
1471 * Place a packet after a given packet in a list. The list locks are taken
1472 * and this function is atomic with respect to other list locked calls.
1473 * A buffer cannot be placed on two lists at the same time.
1475 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1477 unsigned long flags
;
1479 spin_lock_irqsave(&list
->lock
, flags
);
1480 __skb_append(old
, newsk
, list
);
1481 spin_unlock_irqrestore(&list
->lock
, flags
);
1486 * skb_insert - insert a buffer
1487 * @old: buffer to insert before
1488 * @newsk: buffer to insert
1489 * @list: list to use
1491 * Place a packet before a given packet in a list. The list locks are
1492 * taken and this function is atomic with respect to other list locked
1495 * A buffer cannot be placed on two lists at the same time.
1497 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1499 unsigned long flags
;
1501 spin_lock_irqsave(&list
->lock
, flags
);
1502 __skb_insert(newsk
, old
->prev
, old
, list
);
1503 spin_unlock_irqrestore(&list
->lock
, flags
);
1508 * Tune the memory allocator for a new MTU size.
1510 void skb_add_mtu(int mtu
)
1512 /* Must match allocation in alloc_skb */
1513 mtu
= SKB_DATA_ALIGN(mtu
) + sizeof(struct skb_shared_info
);
1515 kmem_add_cache_size(mtu
);
1519 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1520 struct sk_buff
* skb1
,
1521 const u32 len
, const int pos
)
1525 memcpy(skb_put(skb1
, pos
- len
), skb
->data
+ len
, pos
- len
);
1527 /* And move data appendix as is. */
1528 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1529 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1531 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1532 skb_shinfo(skb
)->nr_frags
= 0;
1533 skb1
->data_len
= skb
->data_len
;
1534 skb1
->len
+= skb1
->data_len
;
1537 skb
->tail
= skb
->data
+ len
;
1540 static inline void skb_split_no_header(struct sk_buff
*skb
,
1541 struct sk_buff
* skb1
,
1542 const u32 len
, int pos
)
1545 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1547 skb_shinfo(skb
)->nr_frags
= 0;
1548 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1550 skb
->data_len
= len
- pos
;
1552 for (i
= 0; i
< nfrags
; i
++) {
1553 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1555 if (pos
+ size
> len
) {
1556 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1560 * We have two variants in this case:
1561 * 1. Move all the frag to the second
1562 * part, if it is possible. F.e.
1563 * this approach is mandatory for TUX,
1564 * where splitting is expensive.
1565 * 2. Split is accurately. We make this.
1567 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1568 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1569 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1570 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1571 skb_shinfo(skb
)->nr_frags
++;
1575 skb_shinfo(skb
)->nr_frags
++;
1578 skb_shinfo(skb1
)->nr_frags
= k
;
1582 * skb_split - Split fragmented skb to two parts at length len.
1583 * @skb: the buffer to split
1584 * @skb1: the buffer to receive the second part
1585 * @len: new length for skb
1587 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1589 int pos
= skb_headlen(skb
);
1591 if (len
< pos
) /* Split line is inside header. */
1592 skb_split_inside_header(skb
, skb1
, len
, pos
);
1593 else /* Second chunk has no header, nothing to copy. */
1594 skb_split_no_header(skb
, skb1
, len
, pos
);
1598 * skb_prepare_seq_read - Prepare a sequential read of skb data
1599 * @skb: the buffer to read
1600 * @from: lower offset of data to be read
1601 * @to: upper offset of data to be read
1602 * @st: state variable
1604 * Initializes the specified state variable. Must be called before
1605 * invoking skb_seq_read() for the first time.
1607 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1608 unsigned int to
, struct skb_seq_state
*st
)
1610 st
->lower_offset
= from
;
1611 st
->upper_offset
= to
;
1612 st
->root_skb
= st
->cur_skb
= skb
;
1613 st
->frag_idx
= st
->stepped_offset
= 0;
1614 st
->frag_data
= NULL
;
1618 * skb_seq_read - Sequentially read skb data
1619 * @consumed: number of bytes consumed by the caller so far
1620 * @data: destination pointer for data to be returned
1621 * @st: state variable
1623 * Reads a block of skb data at &consumed relative to the
1624 * lower offset specified to skb_prepare_seq_read(). Assigns
1625 * the head of the data block to &data and returns the length
1626 * of the block or 0 if the end of the skb data or the upper
1627 * offset has been reached.
1629 * The caller is not required to consume all of the data
1630 * returned, i.e. &consumed is typically set to the number
1631 * of bytes already consumed and the next call to
1632 * skb_seq_read() will return the remaining part of the block.
1634 * Note: The size of each block of data returned can be arbitary,
1635 * this limitation is the cost for zerocopy seqeuental
1636 * reads of potentially non linear data.
1638 * Note: Fragment lists within fragments are not implemented
1639 * at the moment, state->root_skb could be replaced with
1640 * a stack for this purpose.
1642 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1643 struct skb_seq_state
*st
)
1645 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1648 if (unlikely(abs_offset
>= st
->upper_offset
))
1652 block_limit
= skb_headlen(st
->cur_skb
);
1654 if (abs_offset
< block_limit
) {
1655 *data
= st
->cur_skb
->data
+ abs_offset
;
1656 return block_limit
- abs_offset
;
1659 if (st
->frag_idx
== 0 && !st
->frag_data
)
1660 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1662 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1663 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1664 block_limit
= frag
->size
+ st
->stepped_offset
;
1666 if (abs_offset
< block_limit
) {
1668 st
->frag_data
= kmap_skb_frag(frag
);
1670 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1671 (abs_offset
- st
->stepped_offset
);
1673 return block_limit
- abs_offset
;
1676 if (st
->frag_data
) {
1677 kunmap_skb_frag(st
->frag_data
);
1678 st
->frag_data
= NULL
;
1682 st
->stepped_offset
+= frag
->size
;
1685 if (st
->cur_skb
->next
) {
1686 st
->cur_skb
= st
->cur_skb
->next
;
1689 } else if (st
->root_skb
== st
->cur_skb
&&
1690 skb_shinfo(st
->root_skb
)->frag_list
) {
1691 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1699 * skb_abort_seq_read - Abort a sequential read of skb data
1700 * @st: state variable
1702 * Must be called if skb_seq_read() was not called until it
1705 void skb_abort_seq_read(struct skb_seq_state
*st
)
1708 kunmap_skb_frag(st
->frag_data
);
1711 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1713 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1714 struct ts_config
*conf
,
1715 struct ts_state
*state
)
1717 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
1720 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
1722 skb_abort_seq_read(TS_SKB_CB(state
));
1726 * skb_find_text - Find a text pattern in skb data
1727 * @skb: the buffer to look in
1728 * @from: search offset
1730 * @config: textsearch configuration
1731 * @state: uninitialized textsearch state variable
1733 * Finds a pattern in the skb data according to the specified
1734 * textsearch configuration. Use textsearch_next() to retrieve
1735 * subsequent occurrences of the pattern. Returns the offset
1736 * to the first occurrence or UINT_MAX if no match was found.
1738 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
1739 unsigned int to
, struct ts_config
*config
,
1740 struct ts_state
*state
)
1742 config
->get_next_block
= skb_ts_get_next_block
;
1743 config
->finish
= skb_ts_finish
;
1745 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
1747 return textsearch_find(config
, state
);
1751 * skb_append_datato_frags: - append the user data to a skb
1752 * @sk: sock structure
1753 * @skb: skb structure to be appened with user data.
1754 * @getfrag: call back function to be used for getting the user data
1755 * @from: pointer to user message iov
1756 * @length: length of the iov message
1758 * Description: This procedure append the user data in the fragment part
1759 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1761 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
1762 int (*getfrag
)(void *from
, char *to
, int offset
,
1763 int len
, int odd
, struct sk_buff
*skb
),
1764 void *from
, int length
)
1767 skb_frag_t
*frag
= NULL
;
1768 struct page
*page
= NULL
;
1774 /* Return error if we don't have space for new frag */
1775 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1776 if (frg_cnt
>= MAX_SKB_FRAGS
)
1779 /* allocate a new page for next frag */
1780 page
= alloc_pages(sk
->sk_allocation
, 0);
1782 /* If alloc_page fails just return failure and caller will
1783 * free previous allocated pages by doing kfree_skb()
1788 /* initialize the next frag */
1789 sk
->sk_sndmsg_page
= page
;
1790 sk
->sk_sndmsg_off
= 0;
1791 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
1792 skb
->truesize
+= PAGE_SIZE
;
1793 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
1795 /* get the new initialized frag */
1796 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1797 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
1799 /* copy the user data to page */
1800 left
= PAGE_SIZE
- frag
->page_offset
;
1801 copy
= (length
> left
)? left
: length
;
1803 ret
= getfrag(from
, (page_address(frag
->page
) +
1804 frag
->page_offset
+ frag
->size
),
1805 offset
, copy
, 0, skb
);
1809 /* copy was successful so update the size parameters */
1810 sk
->sk_sndmsg_off
+= copy
;
1813 skb
->data_len
+= copy
;
1817 } while (length
> 0);
1823 * skb_pull_rcsum - pull skb and update receive checksum
1824 * @skb: buffer to update
1825 * @start: start of data before pull
1826 * @len: length of data pulled
1828 * This function performs an skb_pull on the packet and updates
1829 * update the CHECKSUM_HW checksum. It should be used on receive
1830 * path processing instead of skb_pull unless you know that the
1831 * checksum difference is zero (e.g., a valid IP header) or you
1832 * are setting ip_summed to CHECKSUM_NONE.
1834 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
1836 BUG_ON(len
> skb
->len
);
1838 BUG_ON(skb
->len
< skb
->data_len
);
1839 skb_postpull_rcsum(skb
, skb
->data
, len
);
1840 return skb
->data
+= len
;
1843 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
1845 void __init
skb_init(void)
1847 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
1848 sizeof(struct sk_buff
),
1852 if (!skbuff_head_cache
)
1853 panic("cannot create skbuff cache");
1855 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
1856 (2*sizeof(struct sk_buff
)) +
1861 if (!skbuff_fclone_cache
)
1862 panic("cannot create skbuff cache");
1865 EXPORT_SYMBOL(___pskb_trim
);
1866 EXPORT_SYMBOL(__kfree_skb
);
1867 EXPORT_SYMBOL(kfree_skb
);
1868 EXPORT_SYMBOL(__pskb_pull_tail
);
1869 EXPORT_SYMBOL(__alloc_skb
);
1870 EXPORT_SYMBOL(pskb_copy
);
1871 EXPORT_SYMBOL(pskb_expand_head
);
1872 EXPORT_SYMBOL(skb_checksum
);
1873 EXPORT_SYMBOL(skb_clone
);
1874 EXPORT_SYMBOL(skb_clone_fraglist
);
1875 EXPORT_SYMBOL(skb_copy
);
1876 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
1877 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
1878 EXPORT_SYMBOL(skb_copy_bits
);
1879 EXPORT_SYMBOL(skb_copy_expand
);
1880 EXPORT_SYMBOL(skb_over_panic
);
1881 EXPORT_SYMBOL(skb_pad
);
1882 EXPORT_SYMBOL(skb_realloc_headroom
);
1883 EXPORT_SYMBOL(skb_under_panic
);
1884 EXPORT_SYMBOL(skb_dequeue
);
1885 EXPORT_SYMBOL(skb_dequeue_tail
);
1886 EXPORT_SYMBOL(skb_insert
);
1887 EXPORT_SYMBOL(skb_queue_purge
);
1888 EXPORT_SYMBOL(skb_queue_head
);
1889 EXPORT_SYMBOL(skb_queue_tail
);
1890 EXPORT_SYMBOL(skb_unlink
);
1891 EXPORT_SYMBOL(skb_append
);
1892 EXPORT_SYMBOL(skb_split
);
1893 EXPORT_SYMBOL(skb_prepare_seq_read
);
1894 EXPORT_SYMBOL(skb_seq_read
);
1895 EXPORT_SYMBOL(skb_abort_seq_read
);
1896 EXPORT_SYMBOL(skb_find_text
);
1897 EXPORT_SYMBOL(skb_append_datato_frags
);