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/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
46 #include <linux/interrupt.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/netdevice.h>
51 #ifdef CONFIG_NET_CLS_ACT
52 #include <net/pkt_sched.h>
54 #include <linux/string.h>
55 #include <linux/skbuff.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/highmem.h>
61 #include <net/protocol.h>
64 #include <net/checksum.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
70 static kmem_cache_t
*skbuff_head_cache __read_mostly
;
71 static kmem_cache_t
*skbuff_fclone_cache __read_mostly
;
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
80 * skb_over_panic - private function
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_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>");
97 * skb_under_panic - private function
102 * Out of line support code for skb_push(). Not user callable.
105 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
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>");
114 void skb_truesize_bug(struct sk_buff
*skb
)
116 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
120 EXPORT_SYMBOL(skb_truesize_bug
);
122 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
129 * __alloc_skb - allocate a network buffer
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
142 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
146 struct skb_shared_info
*shinfo
;
150 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
153 skb
= kmem_cache_alloc(cache
, gfp_mask
& ~__GFP_DMA
);
157 /* Get the DATA. Size must match skb_add_mtu(). */
158 size
= SKB_DATA_ALIGN(size
);
159 data
= ____kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
163 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
164 skb
->truesize
= size
+ sizeof(struct sk_buff
);
165 atomic_set(&skb
->users
, 1);
169 skb
->end
= data
+ size
;
170 /* make sure we initialize shinfo sequentially */
171 shinfo
= skb_shinfo(skb
);
172 atomic_set(&shinfo
->dataref
, 1);
173 shinfo
->nr_frags
= 0;
174 shinfo
->gso_size
= 0;
175 shinfo
->gso_segs
= 0;
176 shinfo
->gso_type
= 0;
177 shinfo
->ip6_frag_id
= 0;
178 shinfo
->frag_list
= NULL
;
181 struct sk_buff
*child
= skb
+ 1;
182 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
184 skb
->fclone
= SKB_FCLONE_ORIG
;
185 atomic_set(fclone_ref
, 1);
187 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
192 kmem_cache_free(cache
, skb
);
198 * alloc_skb_from_cache - allocate a network buffer
199 * @cp: kmem_cache from which to allocate the data area
200 * (object size must be big enough for @size bytes + skb overheads)
201 * @size: size to allocate
202 * @gfp_mask: allocation mask
204 * Allocate a new &sk_buff. The returned buffer has no headroom and
205 * tail room of size bytes. The object has a reference count of one.
206 * The return is the buffer. On a failure the return is %NULL.
208 * Buffers may only be allocated from interrupts using a @gfp_mask of
211 struct sk_buff
*alloc_skb_from_cache(kmem_cache_t
*cp
,
219 skb
= kmem_cache_alloc(skbuff_head_cache
,
220 gfp_mask
& ~__GFP_DMA
);
225 size
= SKB_DATA_ALIGN(size
);
226 data
= kmem_cache_alloc(cp
, gfp_mask
);
230 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
231 skb
->truesize
= size
+ sizeof(struct sk_buff
);
232 atomic_set(&skb
->users
, 1);
236 skb
->end
= data
+ size
;
238 atomic_set(&(skb_shinfo(skb
)->dataref
), 1);
239 skb_shinfo(skb
)->nr_frags
= 0;
240 skb_shinfo(skb
)->gso_size
= 0;
241 skb_shinfo(skb
)->gso_segs
= 0;
242 skb_shinfo(skb
)->gso_type
= 0;
243 skb_shinfo(skb
)->frag_list
= NULL
;
247 kmem_cache_free(skbuff_head_cache
, skb
);
253 static void skb_drop_fraglist(struct sk_buff
*skb
)
255 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
257 skb_shinfo(skb
)->frag_list
= NULL
;
260 struct sk_buff
*this = list
;
266 static void skb_clone_fraglist(struct sk_buff
*skb
)
268 struct sk_buff
*list
;
270 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
274 static void skb_release_data(struct sk_buff
*skb
)
277 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
278 &skb_shinfo(skb
)->dataref
)) {
279 if (skb_shinfo(skb
)->nr_frags
) {
281 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
282 put_page(skb_shinfo(skb
)->frags
[i
].page
);
285 if (skb_shinfo(skb
)->frag_list
)
286 skb_drop_fraglist(skb
);
293 * Free an skbuff by memory without cleaning the state.
295 void kfree_skbmem(struct sk_buff
*skb
)
297 struct sk_buff
*other
;
298 atomic_t
*fclone_ref
;
300 skb_release_data(skb
);
301 switch (skb
->fclone
) {
302 case SKB_FCLONE_UNAVAILABLE
:
303 kmem_cache_free(skbuff_head_cache
, skb
);
306 case SKB_FCLONE_ORIG
:
307 fclone_ref
= (atomic_t
*) (skb
+ 2);
308 if (atomic_dec_and_test(fclone_ref
))
309 kmem_cache_free(skbuff_fclone_cache
, skb
);
312 case SKB_FCLONE_CLONE
:
313 fclone_ref
= (atomic_t
*) (skb
+ 1);
316 /* The clone portion is available for
317 * fast-cloning again.
319 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
321 if (atomic_dec_and_test(fclone_ref
))
322 kmem_cache_free(skbuff_fclone_cache
, other
);
328 * __kfree_skb - private function
331 * Free an sk_buff. Release anything attached to the buffer.
332 * Clean the state. This is an internal helper function. Users should
333 * always call kfree_skb
336 void __kfree_skb(struct sk_buff
*skb
)
338 dst_release(skb
->dst
);
340 secpath_put(skb
->sp
);
342 if (skb
->destructor
) {
344 skb
->destructor(skb
);
346 #ifdef CONFIG_NETFILTER
347 nf_conntrack_put(skb
->nfct
);
348 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
349 nf_conntrack_put_reasm(skb
->nfct_reasm
);
351 #ifdef CONFIG_BRIDGE_NETFILTER
352 nf_bridge_put(skb
->nf_bridge
);
355 /* XXX: IS this still necessary? - JHS */
356 #ifdef CONFIG_NET_SCHED
358 #ifdef CONFIG_NET_CLS_ACT
367 * kfree_skb - free an sk_buff
368 * @skb: buffer to free
370 * Drop a reference to the buffer and free it if the usage count has
373 void kfree_skb(struct sk_buff
*skb
)
377 if (likely(atomic_read(&skb
->users
) == 1))
379 else if (likely(!atomic_dec_and_test(&skb
->users
)))
385 * skb_clone - duplicate an sk_buff
386 * @skb: buffer to clone
387 * @gfp_mask: allocation priority
389 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
390 * copies share the same packet data but not structure. The new
391 * buffer has a reference count of 1. If the allocation fails the
392 * function returns %NULL otherwise the new buffer is returned.
394 * If this function is called from an interrupt gfp_mask() must be
398 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
403 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
404 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
405 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
406 n
->fclone
= SKB_FCLONE_CLONE
;
407 atomic_inc(fclone_ref
);
409 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
412 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
415 #define C(x) n->x = skb->x
417 n
->next
= n
->prev
= NULL
;
428 secpath_get(skb
->sp
);
430 memcpy(n
->cb
, skb
->cb
, sizeof(skb
->cb
));
440 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
444 n
->destructor
= NULL
;
445 #ifdef CONFIG_NETFILTER
448 nf_conntrack_get(skb
->nfct
);
450 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
452 nf_conntrack_get_reasm(skb
->nfct_reasm
);
454 #ifdef CONFIG_BRIDGE_NETFILTER
456 nf_bridge_get(skb
->nf_bridge
);
458 #endif /*CONFIG_NETFILTER*/
459 #ifdef CONFIG_NET_SCHED
461 #ifdef CONFIG_NET_CLS_ACT
462 n
->tc_verd
= SET_TC_VERD(skb
->tc_verd
,0);
463 n
->tc_verd
= CLR_TC_OK2MUNGE(n
->tc_verd
);
464 n
->tc_verd
= CLR_TC_MUNGED(n
->tc_verd
);
467 skb_copy_secmark(n
, skb
);
470 atomic_set(&n
->users
, 1);
476 atomic_inc(&(skb_shinfo(skb
)->dataref
));
482 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
485 * Shift between the two data areas in bytes
487 unsigned long offset
= new->data
- old
->data
;
491 new->priority
= old
->priority
;
492 new->protocol
= old
->protocol
;
493 new->dst
= dst_clone(old
->dst
);
495 new->sp
= secpath_get(old
->sp
);
497 new->h
.raw
= old
->h
.raw
+ offset
;
498 new->nh
.raw
= old
->nh
.raw
+ offset
;
499 new->mac
.raw
= old
->mac
.raw
+ offset
;
500 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
501 new->local_df
= old
->local_df
;
502 new->fclone
= SKB_FCLONE_UNAVAILABLE
;
503 new->pkt_type
= old
->pkt_type
;
504 new->tstamp
= old
->tstamp
;
505 new->destructor
= NULL
;
506 #ifdef CONFIG_NETFILTER
507 new->nfmark
= old
->nfmark
;
508 new->nfct
= old
->nfct
;
509 nf_conntrack_get(old
->nfct
);
510 new->nfctinfo
= old
->nfctinfo
;
511 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
512 new->nfct_reasm
= old
->nfct_reasm
;
513 nf_conntrack_get_reasm(old
->nfct_reasm
);
515 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
516 new->ipvs_property
= old
->ipvs_property
;
518 #ifdef CONFIG_BRIDGE_NETFILTER
519 new->nf_bridge
= old
->nf_bridge
;
520 nf_bridge_get(old
->nf_bridge
);
523 #ifdef CONFIG_NET_SCHED
524 #ifdef CONFIG_NET_CLS_ACT
525 new->tc_verd
= old
->tc_verd
;
527 new->tc_index
= old
->tc_index
;
529 skb_copy_secmark(new, old
);
530 atomic_set(&new->users
, 1);
531 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
532 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
533 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
537 * skb_copy - create private copy of an sk_buff
538 * @skb: buffer to copy
539 * @gfp_mask: allocation priority
541 * Make a copy of both an &sk_buff and its data. This is used when the
542 * caller wishes to modify the data and needs a private copy of the
543 * data to alter. Returns %NULL on failure or the pointer to the buffer
544 * on success. The returned buffer has a reference count of 1.
546 * As by-product this function converts non-linear &sk_buff to linear
547 * one, so that &sk_buff becomes completely private and caller is allowed
548 * to modify all the data of returned buffer. This means that this
549 * function is not recommended for use in circumstances when only
550 * header is going to be modified. Use pskb_copy() instead.
553 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
555 int headerlen
= skb
->data
- skb
->head
;
557 * Allocate the copy buffer
559 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
,
564 /* Set the data pointer */
565 skb_reserve(n
, headerlen
);
566 /* Set the tail pointer and length */
567 skb_put(n
, skb
->len
);
569 n
->ip_summed
= skb
->ip_summed
;
571 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
574 copy_skb_header(n
, skb
);
580 * pskb_copy - create copy of an sk_buff with private head.
581 * @skb: buffer to copy
582 * @gfp_mask: allocation priority
584 * Make a copy of both an &sk_buff and part of its data, located
585 * in header. Fragmented data remain shared. This is used when
586 * the caller wishes to modify only header of &sk_buff and needs
587 * private copy of the header to alter. Returns %NULL on failure
588 * or the pointer to the buffer on success.
589 * The returned buffer has a reference count of 1.
592 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
595 * Allocate the copy buffer
597 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
602 /* Set the data pointer */
603 skb_reserve(n
, skb
->data
- skb
->head
);
604 /* Set the tail pointer and length */
605 skb_put(n
, skb_headlen(skb
));
607 memcpy(n
->data
, skb
->data
, n
->len
);
609 n
->ip_summed
= skb
->ip_summed
;
611 n
->data_len
= skb
->data_len
;
614 if (skb_shinfo(skb
)->nr_frags
) {
617 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
618 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
619 get_page(skb_shinfo(n
)->frags
[i
].page
);
621 skb_shinfo(n
)->nr_frags
= i
;
624 if (skb_shinfo(skb
)->frag_list
) {
625 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
626 skb_clone_fraglist(n
);
629 copy_skb_header(n
, skb
);
635 * pskb_expand_head - reallocate header of &sk_buff
636 * @skb: buffer to reallocate
637 * @nhead: room to add at head
638 * @ntail: room to add at tail
639 * @gfp_mask: allocation priority
641 * Expands (or creates identical copy, if &nhead and &ntail are zero)
642 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
643 * reference count of 1. Returns zero in the case of success or error,
644 * if expansion failed. In the last case, &sk_buff is not changed.
646 * All the pointers pointing into skb header may change and must be
647 * reloaded after call to this function.
650 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
655 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
661 size
= SKB_DATA_ALIGN(size
);
663 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
667 /* Copy only real data... and, alas, header. This should be
668 * optimized for the cases when header is void. */
669 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
670 memcpy(data
+ size
, skb
->end
, sizeof(struct skb_shared_info
));
672 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
673 get_page(skb_shinfo(skb
)->frags
[i
].page
);
675 if (skb_shinfo(skb
)->frag_list
)
676 skb_clone_fraglist(skb
);
678 skb_release_data(skb
);
680 off
= (data
+ nhead
) - skb
->head
;
683 skb
->end
= data
+ size
;
691 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
698 /* Make private copy of skb with writable head and some headroom */
700 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
702 struct sk_buff
*skb2
;
703 int delta
= headroom
- skb_headroom(skb
);
706 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
708 skb2
= skb_clone(skb
, GFP_ATOMIC
);
709 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
720 * skb_copy_expand - copy and expand sk_buff
721 * @skb: buffer to copy
722 * @newheadroom: new free bytes at head
723 * @newtailroom: new free bytes at tail
724 * @gfp_mask: allocation priority
726 * Make a copy of both an &sk_buff and its data and while doing so
727 * allocate additional space.
729 * This is used when the caller wishes to modify the data and needs a
730 * private copy of the data to alter as well as more space for new fields.
731 * Returns %NULL on failure or the pointer to the buffer
732 * on success. The returned buffer has a reference count of 1.
734 * You must pass %GFP_ATOMIC as the allocation priority if this function
735 * is called from an interrupt.
737 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
738 * only by netfilter in the cases when checksum is recalculated? --ANK
740 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
741 int newheadroom
, int newtailroom
,
745 * Allocate the copy buffer
747 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
749 int head_copy_len
, head_copy_off
;
754 skb_reserve(n
, newheadroom
);
756 /* Set the tail pointer and length */
757 skb_put(n
, skb
->len
);
759 head_copy_len
= skb_headroom(skb
);
761 if (newheadroom
<= head_copy_len
)
762 head_copy_len
= newheadroom
;
764 head_copy_off
= newheadroom
- head_copy_len
;
766 /* Copy the linear header and data. */
767 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
768 skb
->len
+ head_copy_len
))
771 copy_skb_header(n
, skb
);
777 * skb_pad - zero pad the tail of an skb
778 * @skb: buffer to pad
781 * Ensure that a buffer is followed by a padding area that is zero
782 * filled. Used by network drivers which may DMA or transfer data
783 * beyond the buffer end onto the wire.
785 * May return error in out of memory cases. The skb is freed on error.
788 int skb_pad(struct sk_buff
*skb
, int pad
)
793 /* If the skbuff is non linear tailroom is always zero.. */
794 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
795 memset(skb
->data
+skb
->len
, 0, pad
);
799 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
800 if (likely(skb_cloned(skb
) || ntail
> 0)) {
801 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
806 /* FIXME: The use of this function with non-linear skb's really needs
809 err
= skb_linearize(skb
);
813 memset(skb
->data
+ skb
->len
, 0, pad
);
821 /* Trims skb to length len. It can change skb pointers.
824 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
826 int offset
= skb_headlen(skb
);
827 int nfrags
= skb_shinfo(skb
)->nr_frags
;
830 for (i
= 0; i
< nfrags
; i
++) {
831 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
833 if (skb_cloned(skb
)) {
834 if (pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))
838 put_page(skb_shinfo(skb
)->frags
[i
].page
);
839 skb_shinfo(skb
)->nr_frags
--;
841 skb_shinfo(skb
)->frags
[i
].size
= len
- offset
;
848 skb
->data_len
-= skb
->len
- len
;
851 if (len
<= skb_headlen(skb
)) {
854 skb
->tail
= skb
->data
+ len
;
855 if (skb_shinfo(skb
)->frag_list
&& !skb_cloned(skb
))
856 skb_drop_fraglist(skb
);
858 skb
->data_len
-= skb
->len
- len
;
867 * __pskb_pull_tail - advance tail of skb header
868 * @skb: buffer to reallocate
869 * @delta: number of bytes to advance tail
871 * The function makes a sense only on a fragmented &sk_buff,
872 * it expands header moving its tail forward and copying necessary
873 * data from fragmented part.
875 * &sk_buff MUST have reference count of 1.
877 * Returns %NULL (and &sk_buff does not change) if pull failed
878 * or value of new tail of skb in the case of success.
880 * All the pointers pointing into skb header may change and must be
881 * reloaded after call to this function.
884 /* Moves tail of skb head forward, copying data from fragmented part,
885 * when it is necessary.
886 * 1. It may fail due to malloc failure.
887 * 2. It may change skb pointers.
889 * It is pretty complicated. Luckily, it is called only in exceptional cases.
891 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
893 /* If skb has not enough free space at tail, get new one
894 * plus 128 bytes for future expansions. If we have enough
895 * room at tail, reallocate without expansion only if skb is cloned.
897 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
899 if (eat
> 0 || skb_cloned(skb
)) {
900 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
905 if (skb_copy_bits(skb
, skb_headlen(skb
), skb
->tail
, delta
))
908 /* Optimization: no fragments, no reasons to preestimate
909 * size of pulled pages. Superb.
911 if (!skb_shinfo(skb
)->frag_list
)
914 /* Estimate size of pulled pages. */
916 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
917 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
919 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
922 /* If we need update frag list, we are in troubles.
923 * Certainly, it possible to add an offset to skb data,
924 * but taking into account that pulling is expected to
925 * be very rare operation, it is worth to fight against
926 * further bloating skb head and crucify ourselves here instead.
927 * Pure masohism, indeed. 8)8)
930 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
931 struct sk_buff
*clone
= NULL
;
932 struct sk_buff
*insp
= NULL
;
937 if (list
->len
<= eat
) {
938 /* Eaten as whole. */
943 /* Eaten partially. */
945 if (skb_shared(list
)) {
946 /* Sucks! We need to fork list. :-( */
947 clone
= skb_clone(list
, GFP_ATOMIC
);
953 /* This may be pulled without
957 if (!pskb_pull(list
, eat
)) {
966 /* Free pulled out fragments. */
967 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
968 skb_shinfo(skb
)->frag_list
= list
->next
;
971 /* And insert new clone at head. */
974 skb_shinfo(skb
)->frag_list
= clone
;
977 /* Success! Now we may commit changes to skb data. */
982 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
983 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
984 put_page(skb_shinfo(skb
)->frags
[i
].page
);
985 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
987 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
989 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
990 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
996 skb_shinfo(skb
)->nr_frags
= k
;
999 skb
->data_len
-= delta
;
1004 /* Copy some data bits from skb to kernel buffer. */
1006 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1009 int start
= skb_headlen(skb
);
1011 if (offset
> (int)skb
->len
- len
)
1015 if ((copy
= start
- offset
) > 0) {
1018 memcpy(to
, skb
->data
+ offset
, copy
);
1019 if ((len
-= copy
) == 0)
1025 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1028 BUG_TRAP(start
<= offset
+ len
);
1030 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1031 if ((copy
= end
- offset
) > 0) {
1037 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1039 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1040 offset
- start
, copy
);
1041 kunmap_skb_frag(vaddr
);
1043 if ((len
-= copy
) == 0)
1051 if (skb_shinfo(skb
)->frag_list
) {
1052 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1054 for (; list
; list
= list
->next
) {
1057 BUG_TRAP(start
<= offset
+ len
);
1059 end
= start
+ list
->len
;
1060 if ((copy
= end
- offset
) > 0) {
1063 if (skb_copy_bits(list
, offset
- start
,
1066 if ((len
-= copy
) == 0)
1082 * skb_store_bits - store bits from kernel buffer to skb
1083 * @skb: destination buffer
1084 * @offset: offset in destination
1085 * @from: source buffer
1086 * @len: number of bytes to copy
1088 * Copy the specified number of bytes from the source buffer to the
1089 * destination skb. This function handles all the messy bits of
1090 * traversing fragment lists and such.
1093 int skb_store_bits(const struct sk_buff
*skb
, int offset
, void *from
, int len
)
1096 int start
= skb_headlen(skb
);
1098 if (offset
> (int)skb
->len
- len
)
1101 if ((copy
= start
- offset
) > 0) {
1104 memcpy(skb
->data
+ offset
, from
, copy
);
1105 if ((len
-= copy
) == 0)
1111 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1112 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1115 BUG_TRAP(start
<= offset
+ len
);
1117 end
= start
+ frag
->size
;
1118 if ((copy
= end
- offset
) > 0) {
1124 vaddr
= kmap_skb_frag(frag
);
1125 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1127 kunmap_skb_frag(vaddr
);
1129 if ((len
-= copy
) == 0)
1137 if (skb_shinfo(skb
)->frag_list
) {
1138 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1140 for (; list
; list
= list
->next
) {
1143 BUG_TRAP(start
<= offset
+ len
);
1145 end
= start
+ list
->len
;
1146 if ((copy
= end
- offset
) > 0) {
1149 if (skb_store_bits(list
, offset
- start
,
1152 if ((len
-= copy
) == 0)
1167 EXPORT_SYMBOL(skb_store_bits
);
1169 /* Checksum skb data. */
1171 unsigned int skb_checksum(const struct sk_buff
*skb
, int offset
,
1172 int len
, unsigned int csum
)
1174 int start
= skb_headlen(skb
);
1175 int i
, copy
= start
- offset
;
1178 /* Checksum header. */
1182 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1183 if ((len
-= copy
) == 0)
1189 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1192 BUG_TRAP(start
<= offset
+ len
);
1194 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1195 if ((copy
= end
- offset
) > 0) {
1198 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1202 vaddr
= kmap_skb_frag(frag
);
1203 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1204 offset
- start
, copy
, 0);
1205 kunmap_skb_frag(vaddr
);
1206 csum
= csum_block_add(csum
, csum2
, pos
);
1215 if (skb_shinfo(skb
)->frag_list
) {
1216 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1218 for (; list
; list
= list
->next
) {
1221 BUG_TRAP(start
<= offset
+ len
);
1223 end
= start
+ list
->len
;
1224 if ((copy
= end
- offset
) > 0) {
1228 csum2
= skb_checksum(list
, offset
- start
,
1230 csum
= csum_block_add(csum
, csum2
, pos
);
1231 if ((len
-= copy
) == 0)
1244 /* Both of above in one bottle. */
1246 unsigned int skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1247 u8
*to
, int len
, unsigned int csum
)
1249 int start
= skb_headlen(skb
);
1250 int i
, copy
= start
- offset
;
1257 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1259 if ((len
-= copy
) == 0)
1266 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1269 BUG_TRAP(start
<= offset
+ len
);
1271 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1272 if ((copy
= end
- offset
) > 0) {
1275 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1279 vaddr
= kmap_skb_frag(frag
);
1280 csum2
= csum_partial_copy_nocheck(vaddr
+
1284 kunmap_skb_frag(vaddr
);
1285 csum
= csum_block_add(csum
, csum2
, pos
);
1295 if (skb_shinfo(skb
)->frag_list
) {
1296 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1298 for (; list
; list
= list
->next
) {
1302 BUG_TRAP(start
<= offset
+ len
);
1304 end
= start
+ list
->len
;
1305 if ((copy
= end
- offset
) > 0) {
1308 csum2
= skb_copy_and_csum_bits(list
,
1311 csum
= csum_block_add(csum
, csum2
, pos
);
1312 if ((len
-= copy
) == 0)
1325 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1330 if (skb
->ip_summed
== CHECKSUM_HW
)
1331 csstart
= skb
->h
.raw
- skb
->data
;
1333 csstart
= skb_headlen(skb
);
1335 BUG_ON(csstart
> skb_headlen(skb
));
1337 memcpy(to
, skb
->data
, csstart
);
1340 if (csstart
!= skb
->len
)
1341 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1342 skb
->len
- csstart
, 0);
1344 if (skb
->ip_summed
== CHECKSUM_HW
) {
1345 long csstuff
= csstart
+ skb
->csum
;
1347 *((unsigned short *)(to
+ csstuff
)) = csum_fold(csum
);
1352 * skb_dequeue - remove from the head of the queue
1353 * @list: list to dequeue from
1355 * Remove the head of the list. The list lock is taken so the function
1356 * may be used safely with other locking list functions. The head item is
1357 * returned or %NULL if the list is empty.
1360 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1362 unsigned long flags
;
1363 struct sk_buff
*result
;
1365 spin_lock_irqsave(&list
->lock
, flags
);
1366 result
= __skb_dequeue(list
);
1367 spin_unlock_irqrestore(&list
->lock
, flags
);
1372 * skb_dequeue_tail - remove from the tail of the queue
1373 * @list: list to dequeue from
1375 * Remove the tail of the list. The list lock is taken so the function
1376 * may be used safely with other locking list functions. The tail item is
1377 * returned or %NULL if the list is empty.
1379 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1381 unsigned long flags
;
1382 struct sk_buff
*result
;
1384 spin_lock_irqsave(&list
->lock
, flags
);
1385 result
= __skb_dequeue_tail(list
);
1386 spin_unlock_irqrestore(&list
->lock
, flags
);
1391 * skb_queue_purge - empty a list
1392 * @list: list to empty
1394 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1395 * the list and one reference dropped. This function takes the list
1396 * lock and is atomic with respect to other list locking functions.
1398 void skb_queue_purge(struct sk_buff_head
*list
)
1400 struct sk_buff
*skb
;
1401 while ((skb
= skb_dequeue(list
)) != NULL
)
1406 * skb_queue_head - queue a buffer at the list head
1407 * @list: list to use
1408 * @newsk: buffer to queue
1410 * Queue a buffer at the start of the list. This function takes the
1411 * list lock and can be used safely with other locking &sk_buff functions
1414 * A buffer cannot be placed on two lists at the same time.
1416 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1418 unsigned long flags
;
1420 spin_lock_irqsave(&list
->lock
, flags
);
1421 __skb_queue_head(list
, newsk
);
1422 spin_unlock_irqrestore(&list
->lock
, flags
);
1426 * skb_queue_tail - queue a buffer at the list tail
1427 * @list: list to use
1428 * @newsk: buffer to queue
1430 * Queue a buffer at the tail of the list. This function takes the
1431 * list lock and can be used safely with other locking &sk_buff functions
1434 * A buffer cannot be placed on two lists at the same time.
1436 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1438 unsigned long flags
;
1440 spin_lock_irqsave(&list
->lock
, flags
);
1441 __skb_queue_tail(list
, newsk
);
1442 spin_unlock_irqrestore(&list
->lock
, flags
);
1446 * skb_unlink - remove a buffer from a list
1447 * @skb: buffer to remove
1448 * @list: list to use
1450 * Remove a packet from a list. The list locks are taken and this
1451 * function is atomic with respect to other list locked calls
1453 * You must know what list the SKB is on.
1455 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1457 unsigned long flags
;
1459 spin_lock_irqsave(&list
->lock
, flags
);
1460 __skb_unlink(skb
, list
);
1461 spin_unlock_irqrestore(&list
->lock
, flags
);
1465 * skb_append - append a buffer
1466 * @old: buffer to insert after
1467 * @newsk: buffer to insert
1468 * @list: list to use
1470 * Place a packet after a given packet in a list. The list locks are taken
1471 * and this function is atomic with respect to other list locked calls.
1472 * A buffer cannot be placed on two lists at the same time.
1474 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1476 unsigned long flags
;
1478 spin_lock_irqsave(&list
->lock
, flags
);
1479 __skb_append(old
, newsk
, list
);
1480 spin_unlock_irqrestore(&list
->lock
, flags
);
1485 * skb_insert - insert a buffer
1486 * @old: buffer to insert before
1487 * @newsk: buffer to insert
1488 * @list: list to use
1490 * Place a packet before a given packet in a list. The list locks are
1491 * taken and this function is atomic with respect to other list locked
1494 * A buffer cannot be placed on two lists at the same time.
1496 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1498 unsigned long flags
;
1500 spin_lock_irqsave(&list
->lock
, flags
);
1501 __skb_insert(newsk
, old
->prev
, old
, list
);
1502 spin_unlock_irqrestore(&list
->lock
, flags
);
1507 * Tune the memory allocator for a new MTU size.
1509 void skb_add_mtu(int mtu
)
1511 /* Must match allocation in alloc_skb */
1512 mtu
= SKB_DATA_ALIGN(mtu
) + sizeof(struct skb_shared_info
);
1514 kmem_add_cache_size(mtu
);
1518 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1519 struct sk_buff
* skb1
,
1520 const u32 len
, const int pos
)
1524 memcpy(skb_put(skb1
, pos
- len
), skb
->data
+ len
, pos
- len
);
1526 /* And move data appendix as is. */
1527 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1528 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1530 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1531 skb_shinfo(skb
)->nr_frags
= 0;
1532 skb1
->data_len
= skb
->data_len
;
1533 skb1
->len
+= skb1
->data_len
;
1536 skb
->tail
= skb
->data
+ len
;
1539 static inline void skb_split_no_header(struct sk_buff
*skb
,
1540 struct sk_buff
* skb1
,
1541 const u32 len
, int pos
)
1544 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1546 skb_shinfo(skb
)->nr_frags
= 0;
1547 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1549 skb
->data_len
= len
- pos
;
1551 for (i
= 0; i
< nfrags
; i
++) {
1552 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1554 if (pos
+ size
> len
) {
1555 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1559 * We have two variants in this case:
1560 * 1. Move all the frag to the second
1561 * part, if it is possible. F.e.
1562 * this approach is mandatory for TUX,
1563 * where splitting is expensive.
1564 * 2. Split is accurately. We make this.
1566 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1567 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1568 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1569 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1570 skb_shinfo(skb
)->nr_frags
++;
1574 skb_shinfo(skb
)->nr_frags
++;
1577 skb_shinfo(skb1
)->nr_frags
= k
;
1581 * skb_split - Split fragmented skb to two parts at length len.
1582 * @skb: the buffer to split
1583 * @skb1: the buffer to receive the second part
1584 * @len: new length for skb
1586 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1588 int pos
= skb_headlen(skb
);
1590 if (len
< pos
) /* Split line is inside header. */
1591 skb_split_inside_header(skb
, skb1
, len
, pos
);
1592 else /* Second chunk has no header, nothing to copy. */
1593 skb_split_no_header(skb
, skb1
, len
, pos
);
1597 * skb_prepare_seq_read - Prepare a sequential read of skb data
1598 * @skb: the buffer to read
1599 * @from: lower offset of data to be read
1600 * @to: upper offset of data to be read
1601 * @st: state variable
1603 * Initializes the specified state variable. Must be called before
1604 * invoking skb_seq_read() for the first time.
1606 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1607 unsigned int to
, struct skb_seq_state
*st
)
1609 st
->lower_offset
= from
;
1610 st
->upper_offset
= to
;
1611 st
->root_skb
= st
->cur_skb
= skb
;
1612 st
->frag_idx
= st
->stepped_offset
= 0;
1613 st
->frag_data
= NULL
;
1617 * skb_seq_read - Sequentially read skb data
1618 * @consumed: number of bytes consumed by the caller so far
1619 * @data: destination pointer for data to be returned
1620 * @st: state variable
1622 * Reads a block of skb data at &consumed relative to the
1623 * lower offset specified to skb_prepare_seq_read(). Assigns
1624 * the head of the data block to &data and returns the length
1625 * of the block or 0 if the end of the skb data or the upper
1626 * offset has been reached.
1628 * The caller is not required to consume all of the data
1629 * returned, i.e. &consumed is typically set to the number
1630 * of bytes already consumed and the next call to
1631 * skb_seq_read() will return the remaining part of the block.
1633 * Note: The size of each block of data returned can be arbitary,
1634 * this limitation is the cost for zerocopy seqeuental
1635 * reads of potentially non linear data.
1637 * Note: Fragment lists within fragments are not implemented
1638 * at the moment, state->root_skb could be replaced with
1639 * a stack for this purpose.
1641 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1642 struct skb_seq_state
*st
)
1644 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1647 if (unlikely(abs_offset
>= st
->upper_offset
))
1651 block_limit
= skb_headlen(st
->cur_skb
);
1653 if (abs_offset
< block_limit
) {
1654 *data
= st
->cur_skb
->data
+ abs_offset
;
1655 return block_limit
- abs_offset
;
1658 if (st
->frag_idx
== 0 && !st
->frag_data
)
1659 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1661 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1662 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1663 block_limit
= frag
->size
+ st
->stepped_offset
;
1665 if (abs_offset
< block_limit
) {
1667 st
->frag_data
= kmap_skb_frag(frag
);
1669 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1670 (abs_offset
- st
->stepped_offset
);
1672 return block_limit
- abs_offset
;
1675 if (st
->frag_data
) {
1676 kunmap_skb_frag(st
->frag_data
);
1677 st
->frag_data
= NULL
;
1681 st
->stepped_offset
+= frag
->size
;
1684 if (st
->cur_skb
->next
) {
1685 st
->cur_skb
= st
->cur_skb
->next
;
1688 } else if (st
->root_skb
== st
->cur_skb
&&
1689 skb_shinfo(st
->root_skb
)->frag_list
) {
1690 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1698 * skb_abort_seq_read - Abort a sequential read of skb data
1699 * @st: state variable
1701 * Must be called if skb_seq_read() was not called until it
1704 void skb_abort_seq_read(struct skb_seq_state
*st
)
1707 kunmap_skb_frag(st
->frag_data
);
1710 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1712 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1713 struct ts_config
*conf
,
1714 struct ts_state
*state
)
1716 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
1719 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
1721 skb_abort_seq_read(TS_SKB_CB(state
));
1725 * skb_find_text - Find a text pattern in skb data
1726 * @skb: the buffer to look in
1727 * @from: search offset
1729 * @config: textsearch configuration
1730 * @state: uninitialized textsearch state variable
1732 * Finds a pattern in the skb data according to the specified
1733 * textsearch configuration. Use textsearch_next() to retrieve
1734 * subsequent occurrences of the pattern. Returns the offset
1735 * to the first occurrence or UINT_MAX if no match was found.
1737 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
1738 unsigned int to
, struct ts_config
*config
,
1739 struct ts_state
*state
)
1743 config
->get_next_block
= skb_ts_get_next_block
;
1744 config
->finish
= skb_ts_finish
;
1746 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
1748 ret
= textsearch_find(config
, state
);
1749 return (ret
<= to
- from
? ret
: UINT_MAX
);
1753 * skb_append_datato_frags: - append the user data to a skb
1754 * @sk: sock structure
1755 * @skb: skb structure to be appened with user data.
1756 * @getfrag: call back function to be used for getting the user data
1757 * @from: pointer to user message iov
1758 * @length: length of the iov message
1760 * Description: This procedure append the user data in the fragment part
1761 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1763 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
1764 int (*getfrag
)(void *from
, char *to
, int offset
,
1765 int len
, int odd
, struct sk_buff
*skb
),
1766 void *from
, int length
)
1769 skb_frag_t
*frag
= NULL
;
1770 struct page
*page
= NULL
;
1776 /* Return error if we don't have space for new frag */
1777 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1778 if (frg_cnt
>= MAX_SKB_FRAGS
)
1781 /* allocate a new page for next frag */
1782 page
= alloc_pages(sk
->sk_allocation
, 0);
1784 /* If alloc_page fails just return failure and caller will
1785 * free previous allocated pages by doing kfree_skb()
1790 /* initialize the next frag */
1791 sk
->sk_sndmsg_page
= page
;
1792 sk
->sk_sndmsg_off
= 0;
1793 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
1794 skb
->truesize
+= PAGE_SIZE
;
1795 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
1797 /* get the new initialized frag */
1798 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1799 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
1801 /* copy the user data to page */
1802 left
= PAGE_SIZE
- frag
->page_offset
;
1803 copy
= (length
> left
)? left
: length
;
1805 ret
= getfrag(from
, (page_address(frag
->page
) +
1806 frag
->page_offset
+ frag
->size
),
1807 offset
, copy
, 0, skb
);
1811 /* copy was successful so update the size parameters */
1812 sk
->sk_sndmsg_off
+= copy
;
1815 skb
->data_len
+= copy
;
1819 } while (length
> 0);
1825 * skb_pull_rcsum - pull skb and update receive checksum
1826 * @skb: buffer to update
1827 * @start: start of data before pull
1828 * @len: length of data pulled
1830 * This function performs an skb_pull on the packet and updates
1831 * update the CHECKSUM_HW checksum. It should be used on receive
1832 * path processing instead of skb_pull unless you know that the
1833 * checksum difference is zero (e.g., a valid IP header) or you
1834 * are setting ip_summed to CHECKSUM_NONE.
1836 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
1838 BUG_ON(len
> skb
->len
);
1840 BUG_ON(skb
->len
< skb
->data_len
);
1841 skb_postpull_rcsum(skb
, skb
->data
, len
);
1842 return skb
->data
+= len
;
1845 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
1848 * skb_segment - Perform protocol segmentation on skb.
1849 * @skb: buffer to segment
1850 * @features: features for the output path (see dev->features)
1852 * This function performs segmentation on the given skb. It returns
1853 * the segment at the given position. It returns NULL if there are
1854 * no more segments to generate, or when an error is encountered.
1856 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
1858 struct sk_buff
*segs
= NULL
;
1859 struct sk_buff
*tail
= NULL
;
1860 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
1861 unsigned int doffset
= skb
->data
- skb
->mac
.raw
;
1862 unsigned int offset
= doffset
;
1863 unsigned int headroom
;
1865 int sg
= features
& NETIF_F_SG
;
1866 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1871 __skb_push(skb
, doffset
);
1872 headroom
= skb_headroom(skb
);
1873 pos
= skb_headlen(skb
);
1876 struct sk_buff
*nskb
;
1882 len
= skb
->len
- offset
;
1886 hsize
= skb_headlen(skb
) - offset
;
1889 nsize
= hsize
+ doffset
;
1890 if (nsize
> len
+ doffset
|| !sg
)
1891 nsize
= len
+ doffset
;
1893 nskb
= alloc_skb(nsize
+ headroom
, GFP_ATOMIC
);
1894 if (unlikely(!nskb
))
1903 nskb
->dev
= skb
->dev
;
1904 nskb
->priority
= skb
->priority
;
1905 nskb
->protocol
= skb
->protocol
;
1906 nskb
->dst
= dst_clone(skb
->dst
);
1907 memcpy(nskb
->cb
, skb
->cb
, sizeof(skb
->cb
));
1908 nskb
->pkt_type
= skb
->pkt_type
;
1909 nskb
->mac_len
= skb
->mac_len
;
1911 skb_reserve(nskb
, headroom
);
1912 nskb
->mac
.raw
= nskb
->data
;
1913 nskb
->nh
.raw
= nskb
->data
+ skb
->mac_len
;
1914 nskb
->h
.raw
= nskb
->nh
.raw
+ (skb
->h
.raw
- skb
->nh
.raw
);
1915 memcpy(skb_put(nskb
, doffset
), skb
->data
, doffset
);
1918 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
1924 frag
= skb_shinfo(nskb
)->frags
;
1927 nskb
->ip_summed
= CHECKSUM_HW
;
1928 nskb
->csum
= skb
->csum
;
1929 memcpy(skb_put(nskb
, hsize
), skb
->data
+ offset
, hsize
);
1931 while (pos
< offset
+ len
) {
1932 BUG_ON(i
>= nfrags
);
1934 *frag
= skb_shinfo(skb
)->frags
[i
];
1935 get_page(frag
->page
);
1939 frag
->page_offset
+= offset
- pos
;
1940 frag
->size
-= offset
- pos
;
1945 if (pos
+ size
<= offset
+ len
) {
1949 frag
->size
-= pos
+ size
- (offset
+ len
);
1956 skb_shinfo(nskb
)->nr_frags
= k
;
1957 nskb
->data_len
= len
- hsize
;
1958 nskb
->len
+= nskb
->data_len
;
1959 nskb
->truesize
+= nskb
->data_len
;
1960 } while ((offset
+= len
) < skb
->len
);
1965 while ((skb
= segs
)) {
1969 return ERR_PTR(err
);
1972 EXPORT_SYMBOL_GPL(skb_segment
);
1974 void __init
skb_init(void)
1976 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
1977 sizeof(struct sk_buff
),
1981 if (!skbuff_head_cache
)
1982 panic("cannot create skbuff cache");
1984 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
1985 (2*sizeof(struct sk_buff
)) +
1990 if (!skbuff_fclone_cache
)
1991 panic("cannot create skbuff cache");
1994 EXPORT_SYMBOL(___pskb_trim
);
1995 EXPORT_SYMBOL(__kfree_skb
);
1996 EXPORT_SYMBOL(kfree_skb
);
1997 EXPORT_SYMBOL(__pskb_pull_tail
);
1998 EXPORT_SYMBOL(__alloc_skb
);
1999 EXPORT_SYMBOL(pskb_copy
);
2000 EXPORT_SYMBOL(pskb_expand_head
);
2001 EXPORT_SYMBOL(skb_checksum
);
2002 EXPORT_SYMBOL(skb_clone
);
2003 EXPORT_SYMBOL(skb_clone_fraglist
);
2004 EXPORT_SYMBOL(skb_copy
);
2005 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2006 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2007 EXPORT_SYMBOL(skb_copy_bits
);
2008 EXPORT_SYMBOL(skb_copy_expand
);
2009 EXPORT_SYMBOL(skb_over_panic
);
2010 EXPORT_SYMBOL(skb_pad
);
2011 EXPORT_SYMBOL(skb_realloc_headroom
);
2012 EXPORT_SYMBOL(skb_under_panic
);
2013 EXPORT_SYMBOL(skb_dequeue
);
2014 EXPORT_SYMBOL(skb_dequeue_tail
);
2015 EXPORT_SYMBOL(skb_insert
);
2016 EXPORT_SYMBOL(skb_queue_purge
);
2017 EXPORT_SYMBOL(skb_queue_head
);
2018 EXPORT_SYMBOL(skb_queue_tail
);
2019 EXPORT_SYMBOL(skb_unlink
);
2020 EXPORT_SYMBOL(skb_append
);
2021 EXPORT_SYMBOL(skb_split
);
2022 EXPORT_SYMBOL(skb_prepare_seq_read
);
2023 EXPORT_SYMBOL(skb_seq_read
);
2024 EXPORT_SYMBOL(skb_abort_seq_read
);
2025 EXPORT_SYMBOL(skb_find_text
);
2026 EXPORT_SYMBOL(skb_append_datato_frags
);