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>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/netdevice.h>
50 #ifdef CONFIG_NET_CLS_ACT
51 #include <net/pkt_sched.h>
53 #include <linux/string.h>
54 #include <linux/skbuff.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
60 #include <net/protocol.h>
63 #include <net/checksum.h>
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
71 static struct kmem_cache
*skbuff_head_cache __read_mostly
;
72 static struct kmem_cache
*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:%#lx end:%#lx dev:%s\n",
92 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
93 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
94 skb
->dev
? skb
->dev
->name
: "<NULL>");
99 * skb_under_panic - private function
104 * Out of line support code for skb_push(). Not user callable.
107 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
109 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
110 "data:%p tail:%#lx end:%#lx dev:%s\n",
111 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
112 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
113 skb
->dev
? skb
->dev
->name
: "<NULL>");
117 void skb_truesize_bug(struct sk_buff
*skb
)
119 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
120 "len=%u, sizeof(sk_buff)=%Zd\n",
121 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
123 EXPORT_SYMBOL(skb_truesize_bug
);
125 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
126 * 'private' fields and also do memory statistics to find all the
132 * __alloc_skb - allocate a network buffer
133 * @size: size to allocate
134 * @gfp_mask: allocation mask
135 * @fclone: allocate from fclone cache instead of head cache
136 * and allocate a cloned (child) skb
137 * @node: numa node to allocate memory on
139 * Allocate a new &sk_buff. The returned buffer has no headroom and a
140 * tail room of size bytes. The object has a reference count of one.
141 * The return is the buffer. On a failure the return is %NULL.
143 * Buffers may only be allocated from interrupts using a @gfp_mask of
146 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
147 int fclone
, int node
)
149 struct kmem_cache
*cache
;
150 struct skb_shared_info
*shinfo
;
154 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
157 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
161 size
= SKB_DATA_ALIGN(size
);
162 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
168 * See comment in sk_buff definition, just before the 'tail' member
170 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
171 skb
->truesize
= size
+ sizeof(struct sk_buff
);
172 atomic_set(&skb
->users
, 1);
175 skb_reset_tail_pointer(skb
);
176 skb
->end
= skb
->tail
+ size
;
177 /* make sure we initialize shinfo sequentially */
178 shinfo
= skb_shinfo(skb
);
179 atomic_set(&shinfo
->dataref
, 1);
180 shinfo
->nr_frags
= 0;
181 shinfo
->gso_size
= 0;
182 shinfo
->gso_segs
= 0;
183 shinfo
->gso_type
= 0;
184 shinfo
->ip6_frag_id
= 0;
185 shinfo
->frag_list
= NULL
;
188 struct sk_buff
*child
= skb
+ 1;
189 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
191 skb
->fclone
= SKB_FCLONE_ORIG
;
192 atomic_set(fclone_ref
, 1);
194 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
199 kmem_cache_free(cache
, skb
);
205 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
206 * @dev: network device to receive on
207 * @length: length to allocate
208 * @gfp_mask: get_free_pages mask, passed to alloc_skb
210 * Allocate a new &sk_buff and assign it a usage count of one. The
211 * buffer has unspecified headroom built in. Users should allocate
212 * the headroom they think they need without accounting for the
213 * built in space. The built in space is used for optimisations.
215 * %NULL is returned if there is no free memory.
217 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
218 unsigned int length
, gfp_t gfp_mask
)
220 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
223 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
, 0, node
);
225 skb_reserve(skb
, NET_SKB_PAD
);
231 static void skb_drop_list(struct sk_buff
**listp
)
233 struct sk_buff
*list
= *listp
;
238 struct sk_buff
*this = list
;
244 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
246 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
249 static void skb_clone_fraglist(struct sk_buff
*skb
)
251 struct sk_buff
*list
;
253 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
257 static void skb_release_data(struct sk_buff
*skb
)
260 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
261 &skb_shinfo(skb
)->dataref
)) {
262 if (skb_shinfo(skb
)->nr_frags
) {
264 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
265 put_page(skb_shinfo(skb
)->frags
[i
].page
);
268 if (skb_shinfo(skb
)->frag_list
)
269 skb_drop_fraglist(skb
);
276 * Free an skbuff by memory without cleaning the state.
278 void kfree_skbmem(struct sk_buff
*skb
)
280 struct sk_buff
*other
;
281 atomic_t
*fclone_ref
;
283 skb_release_data(skb
);
284 switch (skb
->fclone
) {
285 case SKB_FCLONE_UNAVAILABLE
:
286 kmem_cache_free(skbuff_head_cache
, skb
);
289 case SKB_FCLONE_ORIG
:
290 fclone_ref
= (atomic_t
*) (skb
+ 2);
291 if (atomic_dec_and_test(fclone_ref
))
292 kmem_cache_free(skbuff_fclone_cache
, skb
);
295 case SKB_FCLONE_CLONE
:
296 fclone_ref
= (atomic_t
*) (skb
+ 1);
299 /* The clone portion is available for
300 * fast-cloning again.
302 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
304 if (atomic_dec_and_test(fclone_ref
))
305 kmem_cache_free(skbuff_fclone_cache
, other
);
311 * __kfree_skb - private function
314 * Free an sk_buff. Release anything attached to the buffer.
315 * Clean the state. This is an internal helper function. Users should
316 * always call kfree_skb
319 void __kfree_skb(struct sk_buff
*skb
)
321 dst_release(skb
->dst
);
323 secpath_put(skb
->sp
);
325 if (skb
->destructor
) {
327 skb
->destructor(skb
);
329 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
330 nf_conntrack_put(skb
->nfct
);
331 nf_conntrack_put_reasm(skb
->nfct_reasm
);
333 #ifdef CONFIG_BRIDGE_NETFILTER
334 nf_bridge_put(skb
->nf_bridge
);
336 /* XXX: IS this still necessary? - JHS */
337 #ifdef CONFIG_NET_SCHED
339 #ifdef CONFIG_NET_CLS_ACT
348 * kfree_skb - free an sk_buff
349 * @skb: buffer to free
351 * Drop a reference to the buffer and free it if the usage count has
354 void kfree_skb(struct sk_buff
*skb
)
358 if (likely(atomic_read(&skb
->users
) == 1))
360 else if (likely(!atomic_dec_and_test(&skb
->users
)))
366 * skb_clone - duplicate an sk_buff
367 * @skb: buffer to clone
368 * @gfp_mask: allocation priority
370 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
371 * copies share the same packet data but not structure. The new
372 * buffer has a reference count of 1. If the allocation fails the
373 * function returns %NULL otherwise the new buffer is returned.
375 * If this function is called from an interrupt gfp_mask() must be
379 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
384 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
385 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
386 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
387 n
->fclone
= SKB_FCLONE_CLONE
;
388 atomic_inc(fclone_ref
);
390 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
393 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
396 #define C(x) n->x = skb->x
398 n
->next
= n
->prev
= NULL
;
409 secpath_get(skb
->sp
);
411 memcpy(n
->cb
, skb
->cb
, sizeof(skb
->cb
));
418 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
422 skb_copy_queue_mapping(n
, skb
);
424 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
428 n
->destructor
= NULL
;
431 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
432 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
435 #ifdef CONFIG_NET_SCHED
437 #ifdef CONFIG_NET_CLS_ACT
438 n
->tc_verd
= SET_TC_VERD(skb
->tc_verd
,0);
439 n
->tc_verd
= CLR_TC_OK2MUNGE(n
->tc_verd
);
440 n
->tc_verd
= CLR_TC_MUNGED(n
->tc_verd
);
444 skb_copy_secmark(n
, skb
);
446 atomic_set(&n
->users
, 1);
452 atomic_inc(&(skb_shinfo(skb
)->dataref
));
458 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
460 #ifndef NET_SKBUFF_DATA_USES_OFFSET
462 * Shift between the two data areas in bytes
464 unsigned long offset
= new->data
- old
->data
;
468 skb_copy_queue_mapping(new, old
);
469 new->priority
= old
->priority
;
470 new->protocol
= old
->protocol
;
471 new->dst
= dst_clone(old
->dst
);
473 new->sp
= secpath_get(old
->sp
);
475 new->transport_header
= old
->transport_header
;
476 new->network_header
= old
->network_header
;
477 new->mac_header
= old
->mac_header
;
478 #ifndef NET_SKBUFF_DATA_USES_OFFSET
479 /* {transport,network,mac}_header are relative to skb->head */
480 new->transport_header
+= offset
;
481 new->network_header
+= offset
;
482 new->mac_header
+= offset
;
484 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
485 new->local_df
= old
->local_df
;
486 new->fclone
= SKB_FCLONE_UNAVAILABLE
;
487 new->pkt_type
= old
->pkt_type
;
488 new->tstamp
= old
->tstamp
;
489 new->destructor
= NULL
;
490 new->mark
= old
->mark
;
492 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
493 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
494 new->nf_trace
= old
->nf_trace
;
496 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
497 new->ipvs_property
= old
->ipvs_property
;
499 #ifdef CONFIG_NET_SCHED
500 #ifdef CONFIG_NET_CLS_ACT
501 new->tc_verd
= old
->tc_verd
;
503 new->tc_index
= old
->tc_index
;
505 skb_copy_secmark(new, old
);
506 atomic_set(&new->users
, 1);
507 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
508 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
509 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
513 * skb_copy - create private copy of an sk_buff
514 * @skb: buffer to copy
515 * @gfp_mask: allocation priority
517 * Make a copy of both an &sk_buff and its data. This is used when the
518 * caller wishes to modify the data and needs a private copy of the
519 * data to alter. Returns %NULL on failure or the pointer to the buffer
520 * on success. The returned buffer has a reference count of 1.
522 * As by-product this function converts non-linear &sk_buff to linear
523 * one, so that &sk_buff becomes completely private and caller is allowed
524 * to modify all the data of returned buffer. This means that this
525 * function is not recommended for use in circumstances when only
526 * header is going to be modified. Use pskb_copy() instead.
529 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
531 int headerlen
= skb
->data
- skb
->head
;
533 * Allocate the copy buffer
536 #ifdef NET_SKBUFF_DATA_USES_OFFSET
537 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
539 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
544 /* Set the data pointer */
545 skb_reserve(n
, headerlen
);
546 /* Set the tail pointer and length */
547 skb_put(n
, skb
->len
);
549 n
->ip_summed
= skb
->ip_summed
;
551 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
554 copy_skb_header(n
, skb
);
560 * pskb_copy - create copy of an sk_buff with private head.
561 * @skb: buffer to copy
562 * @gfp_mask: allocation priority
564 * Make a copy of both an &sk_buff and part of its data, located
565 * in header. Fragmented data remain shared. This is used when
566 * the caller wishes to modify only header of &sk_buff and needs
567 * private copy of the header to alter. Returns %NULL on failure
568 * or the pointer to the buffer on success.
569 * The returned buffer has a reference count of 1.
572 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
575 * Allocate the copy buffer
578 #ifdef NET_SKBUFF_DATA_USES_OFFSET
579 n
= alloc_skb(skb
->end
, gfp_mask
);
581 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
586 /* Set the data pointer */
587 skb_reserve(n
, skb
->data
- skb
->head
);
588 /* Set the tail pointer and length */
589 skb_put(n
, skb_headlen(skb
));
591 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
593 n
->ip_summed
= skb
->ip_summed
;
595 n
->truesize
+= skb
->data_len
;
596 n
->data_len
= skb
->data_len
;
599 if (skb_shinfo(skb
)->nr_frags
) {
602 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
603 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
604 get_page(skb_shinfo(n
)->frags
[i
].page
);
606 skb_shinfo(n
)->nr_frags
= i
;
609 if (skb_shinfo(skb
)->frag_list
) {
610 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
611 skb_clone_fraglist(n
);
614 copy_skb_header(n
, skb
);
620 * pskb_expand_head - reallocate header of &sk_buff
621 * @skb: buffer to reallocate
622 * @nhead: room to add at head
623 * @ntail: room to add at tail
624 * @gfp_mask: allocation priority
626 * Expands (or creates identical copy, if &nhead and &ntail are zero)
627 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
628 * reference count of 1. Returns zero in the case of success or error,
629 * if expansion failed. In the last case, &sk_buff is not changed.
631 * All the pointers pointing into skb header may change and must be
632 * reloaded after call to this function.
635 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
640 #ifdef NET_SKBUFF_DATA_USES_OFFSET
641 int size
= nhead
+ skb
->end
+ ntail
;
643 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
650 size
= SKB_DATA_ALIGN(size
);
652 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
656 /* Copy only real data... and, alas, header. This should be
657 * optimized for the cases when header is void. */
658 #ifdef NET_SKBUFF_DATA_USES_OFFSET
659 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
661 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
663 memcpy(data
+ size
, skb_end_pointer(skb
),
664 sizeof(struct skb_shared_info
));
666 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
667 get_page(skb_shinfo(skb
)->frags
[i
].page
);
669 if (skb_shinfo(skb
)->frag_list
)
670 skb_clone_fraglist(skb
);
672 skb_release_data(skb
);
674 off
= (data
+ nhead
) - skb
->head
;
678 #ifdef NET_SKBUFF_DATA_USES_OFFSET
682 skb
->end
= skb
->head
+ size
;
684 /* {transport,network,mac}_header and tail are relative to skb->head */
686 skb
->transport_header
+= off
;
687 skb
->network_header
+= off
;
688 skb
->mac_header
+= off
;
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 oldheadroom
= skb_headroom(skb
);
751 int head_copy_len
, head_copy_off
;
757 skb_reserve(n
, newheadroom
);
759 /* Set the tail pointer and length */
760 skb_put(n
, skb
->len
);
762 head_copy_len
= oldheadroom
;
764 if (newheadroom
<= head_copy_len
)
765 head_copy_len
= newheadroom
;
767 head_copy_off
= newheadroom
- head_copy_len
;
769 /* Copy the linear header and data. */
770 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
771 skb
->len
+ head_copy_len
))
774 copy_skb_header(n
, skb
);
776 #ifdef NET_SKBUFF_DATA_USES_OFFSET
777 off
= newheadroom
- oldheadroom
;
779 n
->transport_header
+= off
;
780 n
->network_header
+= off
;
781 n
->mac_header
+= off
;
787 * skb_pad - zero pad the tail of an skb
788 * @skb: buffer to pad
791 * Ensure that a buffer is followed by a padding area that is zero
792 * filled. Used by network drivers which may DMA or transfer data
793 * beyond the buffer end onto the wire.
795 * May return error in out of memory cases. The skb is freed on error.
798 int skb_pad(struct sk_buff
*skb
, int pad
)
803 /* If the skbuff is non linear tailroom is always zero.. */
804 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
805 memset(skb
->data
+skb
->len
, 0, pad
);
809 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
810 if (likely(skb_cloned(skb
) || ntail
> 0)) {
811 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
816 /* FIXME: The use of this function with non-linear skb's really needs
819 err
= skb_linearize(skb
);
823 memset(skb
->data
+ skb
->len
, 0, pad
);
831 /* Trims skb to length len. It can change skb pointers.
834 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
836 struct sk_buff
**fragp
;
837 struct sk_buff
*frag
;
838 int offset
= skb_headlen(skb
);
839 int nfrags
= skb_shinfo(skb
)->nr_frags
;
843 if (skb_cloned(skb
) &&
844 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
851 for (; i
< nfrags
; i
++) {
852 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
859 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
862 skb_shinfo(skb
)->nr_frags
= i
;
864 for (; i
< nfrags
; i
++)
865 put_page(skb_shinfo(skb
)->frags
[i
].page
);
867 if (skb_shinfo(skb
)->frag_list
)
868 skb_drop_fraglist(skb
);
872 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
873 fragp
= &frag
->next
) {
874 int end
= offset
+ frag
->len
;
876 if (skb_shared(frag
)) {
877 struct sk_buff
*nfrag
;
879 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
880 if (unlikely(!nfrag
))
883 nfrag
->next
= frag
->next
;
895 unlikely((err
= pskb_trim(frag
, len
- offset
))))
899 skb_drop_list(&frag
->next
);
904 if (len
> skb_headlen(skb
)) {
905 skb
->data_len
-= skb
->len
- len
;
910 skb_set_tail_pointer(skb
, len
);
917 * __pskb_pull_tail - advance tail of skb header
918 * @skb: buffer to reallocate
919 * @delta: number of bytes to advance tail
921 * The function makes a sense only on a fragmented &sk_buff,
922 * it expands header moving its tail forward and copying necessary
923 * data from fragmented part.
925 * &sk_buff MUST have reference count of 1.
927 * Returns %NULL (and &sk_buff does not change) if pull failed
928 * or value of new tail of skb in the case of success.
930 * All the pointers pointing into skb header may change and must be
931 * reloaded after call to this function.
934 /* Moves tail of skb head forward, copying data from fragmented part,
935 * when it is necessary.
936 * 1. It may fail due to malloc failure.
937 * 2. It may change skb pointers.
939 * It is pretty complicated. Luckily, it is called only in exceptional cases.
941 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
943 /* If skb has not enough free space at tail, get new one
944 * plus 128 bytes for future expansions. If we have enough
945 * room at tail, reallocate without expansion only if skb is cloned.
947 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
949 if (eat
> 0 || skb_cloned(skb
)) {
950 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
955 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
958 /* Optimization: no fragments, no reasons to preestimate
959 * size of pulled pages. Superb.
961 if (!skb_shinfo(skb
)->frag_list
)
964 /* Estimate size of pulled pages. */
966 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
967 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
969 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
972 /* If we need update frag list, we are in troubles.
973 * Certainly, it possible to add an offset to skb data,
974 * but taking into account that pulling is expected to
975 * be very rare operation, it is worth to fight against
976 * further bloating skb head and crucify ourselves here instead.
977 * Pure masohism, indeed. 8)8)
980 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
981 struct sk_buff
*clone
= NULL
;
982 struct sk_buff
*insp
= NULL
;
987 if (list
->len
<= eat
) {
988 /* Eaten as whole. */
993 /* Eaten partially. */
995 if (skb_shared(list
)) {
996 /* Sucks! We need to fork list. :-( */
997 clone
= skb_clone(list
, GFP_ATOMIC
);
1003 /* This may be pulled without
1007 if (!pskb_pull(list
, eat
)) {
1016 /* Free pulled out fragments. */
1017 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1018 skb_shinfo(skb
)->frag_list
= list
->next
;
1021 /* And insert new clone at head. */
1024 skb_shinfo(skb
)->frag_list
= clone
;
1027 /* Success! Now we may commit changes to skb data. */
1032 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1033 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1034 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1035 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1037 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1039 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1040 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1046 skb_shinfo(skb
)->nr_frags
= k
;
1049 skb
->data_len
-= delta
;
1051 return skb_tail_pointer(skb
);
1054 /* Copy some data bits from skb to kernel buffer. */
1056 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1059 int start
= skb_headlen(skb
);
1061 if (offset
> (int)skb
->len
- len
)
1065 if ((copy
= start
- offset
) > 0) {
1068 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1069 if ((len
-= copy
) == 0)
1075 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1078 BUG_TRAP(start
<= offset
+ len
);
1080 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1081 if ((copy
= end
- offset
) > 0) {
1087 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1089 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1090 offset
- start
, copy
);
1091 kunmap_skb_frag(vaddr
);
1093 if ((len
-= copy
) == 0)
1101 if (skb_shinfo(skb
)->frag_list
) {
1102 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1104 for (; list
; list
= list
->next
) {
1107 BUG_TRAP(start
<= offset
+ len
);
1109 end
= start
+ list
->len
;
1110 if ((copy
= end
- offset
) > 0) {
1113 if (skb_copy_bits(list
, offset
- start
,
1116 if ((len
-= copy
) == 0)
1132 * skb_store_bits - store bits from kernel buffer to skb
1133 * @skb: destination buffer
1134 * @offset: offset in destination
1135 * @from: source buffer
1136 * @len: number of bytes to copy
1138 * Copy the specified number of bytes from the source buffer to the
1139 * destination skb. This function handles all the messy bits of
1140 * traversing fragment lists and such.
1143 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1146 int start
= skb_headlen(skb
);
1148 if (offset
> (int)skb
->len
- len
)
1151 if ((copy
= start
- offset
) > 0) {
1154 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1155 if ((len
-= copy
) == 0)
1161 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1162 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1165 BUG_TRAP(start
<= offset
+ len
);
1167 end
= start
+ frag
->size
;
1168 if ((copy
= end
- offset
) > 0) {
1174 vaddr
= kmap_skb_frag(frag
);
1175 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1177 kunmap_skb_frag(vaddr
);
1179 if ((len
-= copy
) == 0)
1187 if (skb_shinfo(skb
)->frag_list
) {
1188 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1190 for (; list
; list
= list
->next
) {
1193 BUG_TRAP(start
<= offset
+ len
);
1195 end
= start
+ list
->len
;
1196 if ((copy
= end
- offset
) > 0) {
1199 if (skb_store_bits(list
, offset
- start
,
1202 if ((len
-= copy
) == 0)
1217 EXPORT_SYMBOL(skb_store_bits
);
1219 /* Checksum skb data. */
1221 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1222 int len
, __wsum csum
)
1224 int start
= skb_headlen(skb
);
1225 int i
, copy
= start
- offset
;
1228 /* Checksum header. */
1232 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1233 if ((len
-= copy
) == 0)
1239 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1242 BUG_TRAP(start
<= offset
+ len
);
1244 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1245 if ((copy
= end
- offset
) > 0) {
1248 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1252 vaddr
= kmap_skb_frag(frag
);
1253 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1254 offset
- start
, copy
, 0);
1255 kunmap_skb_frag(vaddr
);
1256 csum
= csum_block_add(csum
, csum2
, pos
);
1265 if (skb_shinfo(skb
)->frag_list
) {
1266 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1268 for (; list
; list
= list
->next
) {
1271 BUG_TRAP(start
<= offset
+ len
);
1273 end
= start
+ list
->len
;
1274 if ((copy
= end
- offset
) > 0) {
1278 csum2
= skb_checksum(list
, offset
- start
,
1280 csum
= csum_block_add(csum
, csum2
, pos
);
1281 if ((len
-= copy
) == 0)
1294 /* Both of above in one bottle. */
1296 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1297 u8
*to
, int len
, __wsum csum
)
1299 int start
= skb_headlen(skb
);
1300 int i
, copy
= start
- offset
;
1307 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1309 if ((len
-= copy
) == 0)
1316 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1319 BUG_TRAP(start
<= offset
+ len
);
1321 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1322 if ((copy
= end
- offset
) > 0) {
1325 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1329 vaddr
= kmap_skb_frag(frag
);
1330 csum2
= csum_partial_copy_nocheck(vaddr
+
1334 kunmap_skb_frag(vaddr
);
1335 csum
= csum_block_add(csum
, csum2
, pos
);
1345 if (skb_shinfo(skb
)->frag_list
) {
1346 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1348 for (; list
; list
= list
->next
) {
1352 BUG_TRAP(start
<= offset
+ len
);
1354 end
= start
+ list
->len
;
1355 if ((copy
= end
- offset
) > 0) {
1358 csum2
= skb_copy_and_csum_bits(list
,
1361 csum
= csum_block_add(csum
, csum2
, pos
);
1362 if ((len
-= copy
) == 0)
1375 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1380 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1381 csstart
= skb
->csum_start
- skb_headroom(skb
);
1383 csstart
= skb_headlen(skb
);
1385 BUG_ON(csstart
> skb_headlen(skb
));
1387 skb_copy_from_linear_data(skb
, to
, csstart
);
1390 if (csstart
!= skb
->len
)
1391 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1392 skb
->len
- csstart
, 0);
1394 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1395 long csstuff
= csstart
+ skb
->csum_offset
;
1397 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1402 * skb_dequeue - remove from the head of the queue
1403 * @list: list to dequeue from
1405 * Remove the head of the list. The list lock is taken so the function
1406 * may be used safely with other locking list functions. The head item is
1407 * returned or %NULL if the list is empty.
1410 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1412 unsigned long flags
;
1413 struct sk_buff
*result
;
1415 spin_lock_irqsave(&list
->lock
, flags
);
1416 result
= __skb_dequeue(list
);
1417 spin_unlock_irqrestore(&list
->lock
, flags
);
1422 * skb_dequeue_tail - remove from the tail of the queue
1423 * @list: list to dequeue from
1425 * Remove the tail of the list. The list lock is taken so the function
1426 * may be used safely with other locking list functions. The tail item is
1427 * returned or %NULL if the list is empty.
1429 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1431 unsigned long flags
;
1432 struct sk_buff
*result
;
1434 spin_lock_irqsave(&list
->lock
, flags
);
1435 result
= __skb_dequeue_tail(list
);
1436 spin_unlock_irqrestore(&list
->lock
, flags
);
1441 * skb_queue_purge - empty a list
1442 * @list: list to empty
1444 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1445 * the list and one reference dropped. This function takes the list
1446 * lock and is atomic with respect to other list locking functions.
1448 void skb_queue_purge(struct sk_buff_head
*list
)
1450 struct sk_buff
*skb
;
1451 while ((skb
= skb_dequeue(list
)) != NULL
)
1456 * skb_queue_head - queue a buffer at the list head
1457 * @list: list to use
1458 * @newsk: buffer to queue
1460 * Queue a buffer at the start of the list. This function takes the
1461 * list lock and can be used safely with other locking &sk_buff functions
1464 * A buffer cannot be placed on two lists at the same time.
1466 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1468 unsigned long flags
;
1470 spin_lock_irqsave(&list
->lock
, flags
);
1471 __skb_queue_head(list
, newsk
);
1472 spin_unlock_irqrestore(&list
->lock
, flags
);
1476 * skb_queue_tail - queue a buffer at the list tail
1477 * @list: list to use
1478 * @newsk: buffer to queue
1480 * Queue a buffer at the tail of the list. This function takes the
1481 * list lock and can be used safely with other locking &sk_buff functions
1484 * A buffer cannot be placed on two lists at the same time.
1486 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1488 unsigned long flags
;
1490 spin_lock_irqsave(&list
->lock
, flags
);
1491 __skb_queue_tail(list
, newsk
);
1492 spin_unlock_irqrestore(&list
->lock
, flags
);
1496 * skb_unlink - remove a buffer from a list
1497 * @skb: buffer to remove
1498 * @list: list to use
1500 * Remove a packet from a list. The list locks are taken and this
1501 * function is atomic with respect to other list locked calls
1503 * You must know what list the SKB is on.
1505 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1507 unsigned long flags
;
1509 spin_lock_irqsave(&list
->lock
, flags
);
1510 __skb_unlink(skb
, list
);
1511 spin_unlock_irqrestore(&list
->lock
, flags
);
1515 * skb_append - append a buffer
1516 * @old: buffer to insert after
1517 * @newsk: buffer to insert
1518 * @list: list to use
1520 * Place a packet after a given packet in a list. The list locks are taken
1521 * and this function is atomic with respect to other list locked calls.
1522 * A buffer cannot be placed on two lists at the same time.
1524 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1526 unsigned long flags
;
1528 spin_lock_irqsave(&list
->lock
, flags
);
1529 __skb_append(old
, newsk
, list
);
1530 spin_unlock_irqrestore(&list
->lock
, flags
);
1535 * skb_insert - insert a buffer
1536 * @old: buffer to insert before
1537 * @newsk: buffer to insert
1538 * @list: list to use
1540 * Place a packet before a given packet in a list. The list locks are
1541 * taken and this function is atomic with respect to other list locked
1544 * A buffer cannot be placed on two lists at the same time.
1546 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1548 unsigned long flags
;
1550 spin_lock_irqsave(&list
->lock
, flags
);
1551 __skb_insert(newsk
, old
->prev
, old
, list
);
1552 spin_unlock_irqrestore(&list
->lock
, flags
);
1555 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1556 struct sk_buff
* skb1
,
1557 const u32 len
, const int pos
)
1561 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1563 /* And move data appendix as is. */
1564 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1565 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1567 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1568 skb_shinfo(skb
)->nr_frags
= 0;
1569 skb1
->data_len
= skb
->data_len
;
1570 skb1
->len
+= skb1
->data_len
;
1573 skb_set_tail_pointer(skb
, len
);
1576 static inline void skb_split_no_header(struct sk_buff
*skb
,
1577 struct sk_buff
* skb1
,
1578 const u32 len
, int pos
)
1581 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1583 skb_shinfo(skb
)->nr_frags
= 0;
1584 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1586 skb
->data_len
= len
- pos
;
1588 for (i
= 0; i
< nfrags
; i
++) {
1589 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1591 if (pos
+ size
> len
) {
1592 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1596 * We have two variants in this case:
1597 * 1. Move all the frag to the second
1598 * part, if it is possible. F.e.
1599 * this approach is mandatory for TUX,
1600 * where splitting is expensive.
1601 * 2. Split is accurately. We make this.
1603 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1604 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1605 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1606 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1607 skb_shinfo(skb
)->nr_frags
++;
1611 skb_shinfo(skb
)->nr_frags
++;
1614 skb_shinfo(skb1
)->nr_frags
= k
;
1618 * skb_split - Split fragmented skb to two parts at length len.
1619 * @skb: the buffer to split
1620 * @skb1: the buffer to receive the second part
1621 * @len: new length for skb
1623 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1625 int pos
= skb_headlen(skb
);
1627 if (len
< pos
) /* Split line is inside header. */
1628 skb_split_inside_header(skb
, skb1
, len
, pos
);
1629 else /* Second chunk has no header, nothing to copy. */
1630 skb_split_no_header(skb
, skb1
, len
, pos
);
1634 * skb_prepare_seq_read - Prepare a sequential read of skb data
1635 * @skb: the buffer to read
1636 * @from: lower offset of data to be read
1637 * @to: upper offset of data to be read
1638 * @st: state variable
1640 * Initializes the specified state variable. Must be called before
1641 * invoking skb_seq_read() for the first time.
1643 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1644 unsigned int to
, struct skb_seq_state
*st
)
1646 st
->lower_offset
= from
;
1647 st
->upper_offset
= to
;
1648 st
->root_skb
= st
->cur_skb
= skb
;
1649 st
->frag_idx
= st
->stepped_offset
= 0;
1650 st
->frag_data
= NULL
;
1654 * skb_seq_read - Sequentially read skb data
1655 * @consumed: number of bytes consumed by the caller so far
1656 * @data: destination pointer for data to be returned
1657 * @st: state variable
1659 * Reads a block of skb data at &consumed relative to the
1660 * lower offset specified to skb_prepare_seq_read(). Assigns
1661 * the head of the data block to &data and returns the length
1662 * of the block or 0 if the end of the skb data or the upper
1663 * offset has been reached.
1665 * The caller is not required to consume all of the data
1666 * returned, i.e. &consumed is typically set to the number
1667 * of bytes already consumed and the next call to
1668 * skb_seq_read() will return the remaining part of the block.
1670 * Note: The size of each block of data returned can be arbitary,
1671 * this limitation is the cost for zerocopy seqeuental
1672 * reads of potentially non linear data.
1674 * Note: Fragment lists within fragments are not implemented
1675 * at the moment, state->root_skb could be replaced with
1676 * a stack for this purpose.
1678 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1679 struct skb_seq_state
*st
)
1681 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1684 if (unlikely(abs_offset
>= st
->upper_offset
))
1688 block_limit
= skb_headlen(st
->cur_skb
);
1690 if (abs_offset
< block_limit
) {
1691 *data
= st
->cur_skb
->data
+ abs_offset
;
1692 return block_limit
- abs_offset
;
1695 if (st
->frag_idx
== 0 && !st
->frag_data
)
1696 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1698 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1699 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1700 block_limit
= frag
->size
+ st
->stepped_offset
;
1702 if (abs_offset
< block_limit
) {
1704 st
->frag_data
= kmap_skb_frag(frag
);
1706 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1707 (abs_offset
- st
->stepped_offset
);
1709 return block_limit
- abs_offset
;
1712 if (st
->frag_data
) {
1713 kunmap_skb_frag(st
->frag_data
);
1714 st
->frag_data
= NULL
;
1718 st
->stepped_offset
+= frag
->size
;
1721 if (st
->frag_data
) {
1722 kunmap_skb_frag(st
->frag_data
);
1723 st
->frag_data
= NULL
;
1726 if (st
->cur_skb
->next
) {
1727 st
->cur_skb
= st
->cur_skb
->next
;
1730 } else if (st
->root_skb
== st
->cur_skb
&&
1731 skb_shinfo(st
->root_skb
)->frag_list
) {
1732 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1740 * skb_abort_seq_read - Abort a sequential read of skb data
1741 * @st: state variable
1743 * Must be called if skb_seq_read() was not called until it
1746 void skb_abort_seq_read(struct skb_seq_state
*st
)
1749 kunmap_skb_frag(st
->frag_data
);
1752 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1754 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1755 struct ts_config
*conf
,
1756 struct ts_state
*state
)
1758 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
1761 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
1763 skb_abort_seq_read(TS_SKB_CB(state
));
1767 * skb_find_text - Find a text pattern in skb data
1768 * @skb: the buffer to look in
1769 * @from: search offset
1771 * @config: textsearch configuration
1772 * @state: uninitialized textsearch state variable
1774 * Finds a pattern in the skb data according to the specified
1775 * textsearch configuration. Use textsearch_next() to retrieve
1776 * subsequent occurrences of the pattern. Returns the offset
1777 * to the first occurrence or UINT_MAX if no match was found.
1779 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
1780 unsigned int to
, struct ts_config
*config
,
1781 struct ts_state
*state
)
1785 config
->get_next_block
= skb_ts_get_next_block
;
1786 config
->finish
= skb_ts_finish
;
1788 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
1790 ret
= textsearch_find(config
, state
);
1791 return (ret
<= to
- from
? ret
: UINT_MAX
);
1795 * skb_append_datato_frags: - append the user data to a skb
1796 * @sk: sock structure
1797 * @skb: skb structure to be appened with user data.
1798 * @getfrag: call back function to be used for getting the user data
1799 * @from: pointer to user message iov
1800 * @length: length of the iov message
1802 * Description: This procedure append the user data in the fragment part
1803 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1805 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
1806 int (*getfrag
)(void *from
, char *to
, int offset
,
1807 int len
, int odd
, struct sk_buff
*skb
),
1808 void *from
, int length
)
1811 skb_frag_t
*frag
= NULL
;
1812 struct page
*page
= NULL
;
1818 /* Return error if we don't have space for new frag */
1819 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1820 if (frg_cnt
>= MAX_SKB_FRAGS
)
1823 /* allocate a new page for next frag */
1824 page
= alloc_pages(sk
->sk_allocation
, 0);
1826 /* If alloc_page fails just return failure and caller will
1827 * free previous allocated pages by doing kfree_skb()
1832 /* initialize the next frag */
1833 sk
->sk_sndmsg_page
= page
;
1834 sk
->sk_sndmsg_off
= 0;
1835 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
1836 skb
->truesize
+= PAGE_SIZE
;
1837 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
1839 /* get the new initialized frag */
1840 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1841 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
1843 /* copy the user data to page */
1844 left
= PAGE_SIZE
- frag
->page_offset
;
1845 copy
= (length
> left
)? left
: length
;
1847 ret
= getfrag(from
, (page_address(frag
->page
) +
1848 frag
->page_offset
+ frag
->size
),
1849 offset
, copy
, 0, skb
);
1853 /* copy was successful so update the size parameters */
1854 sk
->sk_sndmsg_off
+= copy
;
1857 skb
->data_len
+= copy
;
1861 } while (length
> 0);
1867 * skb_pull_rcsum - pull skb and update receive checksum
1868 * @skb: buffer to update
1869 * @start: start of data before pull
1870 * @len: length of data pulled
1872 * This function performs an skb_pull on the packet and updates
1873 * update the CHECKSUM_COMPLETE checksum. It should be used on
1874 * receive path processing instead of skb_pull unless you know
1875 * that the checksum difference is zero (e.g., a valid IP header)
1876 * or you are setting ip_summed to CHECKSUM_NONE.
1878 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
1880 BUG_ON(len
> skb
->len
);
1882 BUG_ON(skb
->len
< skb
->data_len
);
1883 skb_postpull_rcsum(skb
, skb
->data
, len
);
1884 return skb
->data
+= len
;
1887 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
1890 * skb_segment - Perform protocol segmentation on skb.
1891 * @skb: buffer to segment
1892 * @features: features for the output path (see dev->features)
1894 * This function performs segmentation on the given skb. It returns
1895 * the segment at the given position. It returns NULL if there are
1896 * no more segments to generate, or when an error is encountered.
1898 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
1900 struct sk_buff
*segs
= NULL
;
1901 struct sk_buff
*tail
= NULL
;
1902 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
1903 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
1904 unsigned int offset
= doffset
;
1905 unsigned int headroom
;
1907 int sg
= features
& NETIF_F_SG
;
1908 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1913 __skb_push(skb
, doffset
);
1914 headroom
= skb_headroom(skb
);
1915 pos
= skb_headlen(skb
);
1918 struct sk_buff
*nskb
;
1924 len
= skb
->len
- offset
;
1928 hsize
= skb_headlen(skb
) - offset
;
1931 if (hsize
> len
|| !sg
)
1934 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
1935 if (unlikely(!nskb
))
1944 nskb
->dev
= skb
->dev
;
1945 skb_copy_queue_mapping(nskb
, skb
);
1946 nskb
->priority
= skb
->priority
;
1947 nskb
->protocol
= skb
->protocol
;
1948 nskb
->dst
= dst_clone(skb
->dst
);
1949 memcpy(nskb
->cb
, skb
->cb
, sizeof(skb
->cb
));
1950 nskb
->pkt_type
= skb
->pkt_type
;
1951 nskb
->mac_len
= skb
->mac_len
;
1953 skb_reserve(nskb
, headroom
);
1954 skb_reset_mac_header(nskb
);
1955 skb_set_network_header(nskb
, skb
->mac_len
);
1956 nskb
->transport_header
= (nskb
->network_header
+
1957 skb_network_header_len(skb
));
1958 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
1961 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
1967 frag
= skb_shinfo(nskb
)->frags
;
1970 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
1971 nskb
->csum
= skb
->csum
;
1972 skb_copy_from_linear_data_offset(skb
, offset
,
1973 skb_put(nskb
, hsize
), hsize
);
1975 while (pos
< offset
+ len
) {
1976 BUG_ON(i
>= nfrags
);
1978 *frag
= skb_shinfo(skb
)->frags
[i
];
1979 get_page(frag
->page
);
1983 frag
->page_offset
+= offset
- pos
;
1984 frag
->size
-= offset
- pos
;
1989 if (pos
+ size
<= offset
+ len
) {
1993 frag
->size
-= pos
+ size
- (offset
+ len
);
2000 skb_shinfo(nskb
)->nr_frags
= k
;
2001 nskb
->data_len
= len
- hsize
;
2002 nskb
->len
+= nskb
->data_len
;
2003 nskb
->truesize
+= nskb
->data_len
;
2004 } while ((offset
+= len
) < skb
->len
);
2009 while ((skb
= segs
)) {
2013 return ERR_PTR(err
);
2016 EXPORT_SYMBOL_GPL(skb_segment
);
2018 void __init
skb_init(void)
2020 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2021 sizeof(struct sk_buff
),
2023 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2025 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2026 (2*sizeof(struct sk_buff
)) +
2029 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2034 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2035 * @skb: Socket buffer containing the buffers to be mapped
2036 * @sg: The scatter-gather list to map into
2037 * @offset: The offset into the buffer's contents to start mapping
2038 * @len: Length of buffer space to be mapped
2040 * Fill the specified scatter-gather list with mappings/pointers into a
2041 * region of the buffer space attached to a socket buffer.
2044 skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2046 int start
= skb_headlen(skb
);
2047 int i
, copy
= start
- offset
;
2053 sg
[elt
].page
= virt_to_page(skb
->data
+ offset
);
2054 sg
[elt
].offset
= (unsigned long)(skb
->data
+ offset
) % PAGE_SIZE
;
2055 sg
[elt
].length
= copy
;
2057 if ((len
-= copy
) == 0)
2062 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2065 BUG_TRAP(start
<= offset
+ len
);
2067 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2068 if ((copy
= end
- offset
) > 0) {
2069 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2073 sg
[elt
].page
= frag
->page
;
2074 sg
[elt
].offset
= frag
->page_offset
+offset
-start
;
2075 sg
[elt
].length
= copy
;
2084 if (skb_shinfo(skb
)->frag_list
) {
2085 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
2087 for (; list
; list
= list
->next
) {
2090 BUG_TRAP(start
<= offset
+ len
);
2092 end
= start
+ list
->len
;
2093 if ((copy
= end
- offset
) > 0) {
2096 elt
+= skb_to_sgvec(list
, sg
+elt
, offset
- start
, copy
);
2097 if ((len
-= copy
) == 0)
2109 * skb_cow_data - Check that a socket buffer's data buffers are writable
2110 * @skb: The socket buffer to check.
2111 * @tailbits: Amount of trailing space to be added
2112 * @trailer: Returned pointer to the skb where the @tailbits space begins
2114 * Make sure that the data buffers attached to a socket buffer are
2115 * writable. If they are not, private copies are made of the data buffers
2116 * and the socket buffer is set to use these instead.
2118 * If @tailbits is given, make sure that there is space to write @tailbits
2119 * bytes of data beyond current end of socket buffer. @trailer will be
2120 * set to point to the skb in which this space begins.
2122 * The number of scatterlist elements required to completely map the
2123 * COW'd and extended socket buffer will be returned.
2125 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2129 struct sk_buff
*skb1
, **skb_p
;
2131 /* If skb is cloned or its head is paged, reallocate
2132 * head pulling out all the pages (pages are considered not writable
2133 * at the moment even if they are anonymous).
2135 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2136 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2139 /* Easy case. Most of packets will go this way. */
2140 if (!skb_shinfo(skb
)->frag_list
) {
2141 /* A little of trouble, not enough of space for trailer.
2142 * This should not happen, when stack is tuned to generate
2143 * good frames. OK, on miss we reallocate and reserve even more
2144 * space, 128 bytes is fair. */
2146 if (skb_tailroom(skb
) < tailbits
&&
2147 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2155 /* Misery. We are in troubles, going to mincer fragments... */
2158 skb_p
= &skb_shinfo(skb
)->frag_list
;
2161 while ((skb1
= *skb_p
) != NULL
) {
2164 /* The fragment is partially pulled by someone,
2165 * this can happen on input. Copy it and everything
2168 if (skb_shared(skb1
))
2171 /* If the skb is the last, worry about trailer. */
2173 if (skb1
->next
== NULL
&& tailbits
) {
2174 if (skb_shinfo(skb1
)->nr_frags
||
2175 skb_shinfo(skb1
)->frag_list
||
2176 skb_tailroom(skb1
) < tailbits
)
2177 ntail
= tailbits
+ 128;
2183 skb_shinfo(skb1
)->nr_frags
||
2184 skb_shinfo(skb1
)->frag_list
) {
2185 struct sk_buff
*skb2
;
2187 /* Fuck, we are miserable poor guys... */
2189 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2191 skb2
= skb_copy_expand(skb1
,
2195 if (unlikely(skb2
== NULL
))
2199 skb_set_owner_w(skb2
, skb1
->sk
);
2201 /* Looking around. Are we still alive?
2202 * OK, link new skb, drop old one */
2204 skb2
->next
= skb1
->next
;
2211 skb_p
= &skb1
->next
;
2217 EXPORT_SYMBOL(___pskb_trim
);
2218 EXPORT_SYMBOL(__kfree_skb
);
2219 EXPORT_SYMBOL(kfree_skb
);
2220 EXPORT_SYMBOL(__pskb_pull_tail
);
2221 EXPORT_SYMBOL(__alloc_skb
);
2222 EXPORT_SYMBOL(__netdev_alloc_skb
);
2223 EXPORT_SYMBOL(pskb_copy
);
2224 EXPORT_SYMBOL(pskb_expand_head
);
2225 EXPORT_SYMBOL(skb_checksum
);
2226 EXPORT_SYMBOL(skb_clone
);
2227 EXPORT_SYMBOL(skb_copy
);
2228 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2229 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2230 EXPORT_SYMBOL(skb_copy_bits
);
2231 EXPORT_SYMBOL(skb_copy_expand
);
2232 EXPORT_SYMBOL(skb_over_panic
);
2233 EXPORT_SYMBOL(skb_pad
);
2234 EXPORT_SYMBOL(skb_realloc_headroom
);
2235 EXPORT_SYMBOL(skb_under_panic
);
2236 EXPORT_SYMBOL(skb_dequeue
);
2237 EXPORT_SYMBOL(skb_dequeue_tail
);
2238 EXPORT_SYMBOL(skb_insert
);
2239 EXPORT_SYMBOL(skb_queue_purge
);
2240 EXPORT_SYMBOL(skb_queue_head
);
2241 EXPORT_SYMBOL(skb_queue_tail
);
2242 EXPORT_SYMBOL(skb_unlink
);
2243 EXPORT_SYMBOL(skb_append
);
2244 EXPORT_SYMBOL(skb_split
);
2245 EXPORT_SYMBOL(skb_prepare_seq_read
);
2246 EXPORT_SYMBOL(skb_seq_read
);
2247 EXPORT_SYMBOL(skb_abort_seq_read
);
2248 EXPORT_SYMBOL(skb_find_text
);
2249 EXPORT_SYMBOL(skb_append_datato_frags
);
2251 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2252 EXPORT_SYMBOL_GPL(skb_cow_data
);