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 * lockdep: lock class key used by skb_queue_head_init():
76 struct lock_class_key skb_queue_lock_key
;
78 EXPORT_SYMBOL(skb_queue_lock_key
);
81 * Keep out-of-line to prevent kernel bloat.
82 * __builtin_return_address is not used because it is not always
87 * skb_over_panic - private function
92 * Out of line support code for skb_put(). Not user callable.
94 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
96 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
97 "data:%p tail:%p end:%p dev:%s\n",
98 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
99 skb
->dev
? skb
->dev
->name
: "<NULL>");
104 * skb_under_panic - private function
109 * Out of line support code for skb_push(). Not user callable.
112 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
114 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
115 "data:%p tail:%p end:%p dev:%s\n",
116 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
117 skb
->dev
? skb
->dev
->name
: "<NULL>");
121 void skb_truesize_bug(struct sk_buff
*skb
)
123 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
124 "len=%u, sizeof(sk_buff)=%Zd\n",
125 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
127 EXPORT_SYMBOL(skb_truesize_bug
);
129 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
130 * 'private' fields and also do memory statistics to find all the
136 * __alloc_skb - allocate a network buffer
137 * @size: size to allocate
138 * @gfp_mask: allocation mask
139 * @fclone: allocate from fclone cache instead of head cache
140 * and allocate a cloned (child) skb
142 * Allocate a new &sk_buff. The returned buffer has no headroom and a
143 * tail room of size bytes. The object has a reference count of one.
144 * The return is the buffer. On a failure the return is %NULL.
146 * Buffers may only be allocated from interrupts using a @gfp_mask of
149 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
153 struct skb_shared_info
*shinfo
;
157 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
160 skb
= kmem_cache_alloc(cache
, gfp_mask
& ~__GFP_DMA
);
164 /* Get the DATA. Size must match skb_add_mtu(). */
165 size
= SKB_DATA_ALIGN(size
);
166 data
= ____kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
170 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
171 skb
->truesize
= size
+ sizeof(struct sk_buff
);
172 atomic_set(&skb
->users
, 1);
176 skb
->end
= data
+ 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 * alloc_skb_from_cache - allocate a network buffer
206 * @cp: kmem_cache from which to allocate the data area
207 * (object size must be big enough for @size bytes + skb overheads)
208 * @size: size to allocate
209 * @gfp_mask: allocation mask
211 * Allocate a new &sk_buff. The returned buffer has no headroom and
212 * tail room of size bytes. The object has a reference count of one.
213 * The return is the buffer. On a failure the return is %NULL.
215 * Buffers may only be allocated from interrupts using a @gfp_mask of
218 struct sk_buff
*alloc_skb_from_cache(kmem_cache_t
*cp
,
226 skb
= kmem_cache_alloc(skbuff_head_cache
,
227 gfp_mask
& ~__GFP_DMA
);
232 size
= SKB_DATA_ALIGN(size
);
233 data
= kmem_cache_alloc(cp
, gfp_mask
);
237 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
238 skb
->truesize
= size
+ sizeof(struct sk_buff
);
239 atomic_set(&skb
->users
, 1);
243 skb
->end
= data
+ size
;
245 atomic_set(&(skb_shinfo(skb
)->dataref
), 1);
246 skb_shinfo(skb
)->nr_frags
= 0;
247 skb_shinfo(skb
)->gso_size
= 0;
248 skb_shinfo(skb
)->gso_segs
= 0;
249 skb_shinfo(skb
)->gso_type
= 0;
250 skb_shinfo(skb
)->frag_list
= NULL
;
254 kmem_cache_free(skbuff_head_cache
, skb
);
260 static void skb_drop_list(struct sk_buff
**listp
)
262 struct sk_buff
*list
= *listp
;
267 struct sk_buff
*this = list
;
273 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
275 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
278 static void skb_clone_fraglist(struct sk_buff
*skb
)
280 struct sk_buff
*list
;
282 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
286 static void skb_release_data(struct sk_buff
*skb
)
289 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
290 &skb_shinfo(skb
)->dataref
)) {
291 if (skb_shinfo(skb
)->nr_frags
) {
293 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
294 put_page(skb_shinfo(skb
)->frags
[i
].page
);
297 if (skb_shinfo(skb
)->frag_list
)
298 skb_drop_fraglist(skb
);
305 * Free an skbuff by memory without cleaning the state.
307 void kfree_skbmem(struct sk_buff
*skb
)
309 struct sk_buff
*other
;
310 atomic_t
*fclone_ref
;
312 skb_release_data(skb
);
313 switch (skb
->fclone
) {
314 case SKB_FCLONE_UNAVAILABLE
:
315 kmem_cache_free(skbuff_head_cache
, skb
);
318 case SKB_FCLONE_ORIG
:
319 fclone_ref
= (atomic_t
*) (skb
+ 2);
320 if (atomic_dec_and_test(fclone_ref
))
321 kmem_cache_free(skbuff_fclone_cache
, skb
);
324 case SKB_FCLONE_CLONE
:
325 fclone_ref
= (atomic_t
*) (skb
+ 1);
328 /* The clone portion is available for
329 * fast-cloning again.
331 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
333 if (atomic_dec_and_test(fclone_ref
))
334 kmem_cache_free(skbuff_fclone_cache
, other
);
340 * __kfree_skb - private function
343 * Free an sk_buff. Release anything attached to the buffer.
344 * Clean the state. This is an internal helper function. Users should
345 * always call kfree_skb
348 void __kfree_skb(struct sk_buff
*skb
)
350 dst_release(skb
->dst
);
352 secpath_put(skb
->sp
);
354 if (skb
->destructor
) {
356 skb
->destructor(skb
);
358 #ifdef CONFIG_NETFILTER
359 nf_conntrack_put(skb
->nfct
);
360 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
361 nf_conntrack_put_reasm(skb
->nfct_reasm
);
363 #ifdef CONFIG_BRIDGE_NETFILTER
364 nf_bridge_put(skb
->nf_bridge
);
367 /* XXX: IS this still necessary? - JHS */
368 #ifdef CONFIG_NET_SCHED
370 #ifdef CONFIG_NET_CLS_ACT
379 * kfree_skb - free an sk_buff
380 * @skb: buffer to free
382 * Drop a reference to the buffer and free it if the usage count has
385 void kfree_skb(struct sk_buff
*skb
)
389 if (likely(atomic_read(&skb
->users
) == 1))
391 else if (likely(!atomic_dec_and_test(&skb
->users
)))
397 * skb_clone - duplicate an sk_buff
398 * @skb: buffer to clone
399 * @gfp_mask: allocation priority
401 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
402 * copies share the same packet data but not structure. The new
403 * buffer has a reference count of 1. If the allocation fails the
404 * function returns %NULL otherwise the new buffer is returned.
406 * If this function is called from an interrupt gfp_mask() must be
410 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
415 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
416 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
417 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
418 n
->fclone
= SKB_FCLONE_CLONE
;
419 atomic_inc(fclone_ref
);
421 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
424 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
427 #define C(x) n->x = skb->x
429 n
->next
= n
->prev
= NULL
;
440 secpath_get(skb
->sp
);
442 memcpy(n
->cb
, skb
->cb
, sizeof(skb
->cb
));
452 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
456 n
->destructor
= NULL
;
457 #ifdef CONFIG_NETFILTER
460 nf_conntrack_get(skb
->nfct
);
462 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
464 nf_conntrack_get_reasm(skb
->nfct_reasm
);
466 #ifdef CONFIG_BRIDGE_NETFILTER
468 nf_bridge_get(skb
->nf_bridge
);
470 #endif /*CONFIG_NETFILTER*/
471 #ifdef CONFIG_NET_SCHED
473 #ifdef CONFIG_NET_CLS_ACT
474 n
->tc_verd
= SET_TC_VERD(skb
->tc_verd
,0);
475 n
->tc_verd
= CLR_TC_OK2MUNGE(n
->tc_verd
);
476 n
->tc_verd
= CLR_TC_MUNGED(n
->tc_verd
);
479 skb_copy_secmark(n
, skb
);
482 atomic_set(&n
->users
, 1);
488 atomic_inc(&(skb_shinfo(skb
)->dataref
));
494 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
497 * Shift between the two data areas in bytes
499 unsigned long offset
= new->data
- old
->data
;
503 new->priority
= old
->priority
;
504 new->protocol
= old
->protocol
;
505 new->dst
= dst_clone(old
->dst
);
507 new->sp
= secpath_get(old
->sp
);
509 new->h
.raw
= old
->h
.raw
+ offset
;
510 new->nh
.raw
= old
->nh
.raw
+ offset
;
511 new->mac
.raw
= old
->mac
.raw
+ offset
;
512 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
513 new->local_df
= old
->local_df
;
514 new->fclone
= SKB_FCLONE_UNAVAILABLE
;
515 new->pkt_type
= old
->pkt_type
;
516 new->tstamp
= old
->tstamp
;
517 new->destructor
= NULL
;
518 #ifdef CONFIG_NETFILTER
519 new->nfmark
= old
->nfmark
;
520 new->nfct
= old
->nfct
;
521 nf_conntrack_get(old
->nfct
);
522 new->nfctinfo
= old
->nfctinfo
;
523 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
524 new->nfct_reasm
= old
->nfct_reasm
;
525 nf_conntrack_get_reasm(old
->nfct_reasm
);
527 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
528 new->ipvs_property
= old
->ipvs_property
;
530 #ifdef CONFIG_BRIDGE_NETFILTER
531 new->nf_bridge
= old
->nf_bridge
;
532 nf_bridge_get(old
->nf_bridge
);
535 #ifdef CONFIG_NET_SCHED
536 #ifdef CONFIG_NET_CLS_ACT
537 new->tc_verd
= old
->tc_verd
;
539 new->tc_index
= old
->tc_index
;
541 skb_copy_secmark(new, old
);
542 atomic_set(&new->users
, 1);
543 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
544 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
545 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
549 * skb_copy - create private copy of an sk_buff
550 * @skb: buffer to copy
551 * @gfp_mask: allocation priority
553 * Make a copy of both an &sk_buff and its data. This is used when the
554 * caller wishes to modify the data and needs a private copy of the
555 * data to alter. Returns %NULL on failure or the pointer to the buffer
556 * on success. The returned buffer has a reference count of 1.
558 * As by-product this function converts non-linear &sk_buff to linear
559 * one, so that &sk_buff becomes completely private and caller is allowed
560 * to modify all the data of returned buffer. This means that this
561 * function is not recommended for use in circumstances when only
562 * header is going to be modified. Use pskb_copy() instead.
565 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
567 int headerlen
= skb
->data
- skb
->head
;
569 * Allocate the copy buffer
571 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
,
576 /* Set the data pointer */
577 skb_reserve(n
, headerlen
);
578 /* Set the tail pointer and length */
579 skb_put(n
, skb
->len
);
581 n
->ip_summed
= skb
->ip_summed
;
583 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
586 copy_skb_header(n
, skb
);
592 * pskb_copy - create copy of an sk_buff with private head.
593 * @skb: buffer to copy
594 * @gfp_mask: allocation priority
596 * Make a copy of both an &sk_buff and part of its data, located
597 * in header. Fragmented data remain shared. This is used when
598 * the caller wishes to modify only header of &sk_buff and needs
599 * private copy of the header to alter. Returns %NULL on failure
600 * or the pointer to the buffer on success.
601 * The returned buffer has a reference count of 1.
604 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
607 * Allocate the copy buffer
609 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
614 /* Set the data pointer */
615 skb_reserve(n
, skb
->data
- skb
->head
);
616 /* Set the tail pointer and length */
617 skb_put(n
, skb_headlen(skb
));
619 memcpy(n
->data
, skb
->data
, n
->len
);
621 n
->ip_summed
= skb
->ip_summed
;
623 n
->data_len
= skb
->data_len
;
626 if (skb_shinfo(skb
)->nr_frags
) {
629 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
630 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
631 get_page(skb_shinfo(n
)->frags
[i
].page
);
633 skb_shinfo(n
)->nr_frags
= i
;
636 if (skb_shinfo(skb
)->frag_list
) {
637 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
638 skb_clone_fraglist(n
);
641 copy_skb_header(n
, skb
);
647 * pskb_expand_head - reallocate header of &sk_buff
648 * @skb: buffer to reallocate
649 * @nhead: room to add at head
650 * @ntail: room to add at tail
651 * @gfp_mask: allocation priority
653 * Expands (or creates identical copy, if &nhead and &ntail are zero)
654 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
655 * reference count of 1. Returns zero in the case of success or error,
656 * if expansion failed. In the last case, &sk_buff is not changed.
658 * All the pointers pointing into skb header may change and must be
659 * reloaded after call to this function.
662 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
667 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
673 size
= SKB_DATA_ALIGN(size
);
675 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
679 /* Copy only real data... and, alas, header. This should be
680 * optimized for the cases when header is void. */
681 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
682 memcpy(data
+ size
, skb
->end
, sizeof(struct skb_shared_info
));
684 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
685 get_page(skb_shinfo(skb
)->frags
[i
].page
);
687 if (skb_shinfo(skb
)->frag_list
)
688 skb_clone_fraglist(skb
);
690 skb_release_data(skb
);
692 off
= (data
+ nhead
) - skb
->head
;
695 skb
->end
= data
+ size
;
703 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
710 /* Make private copy of skb with writable head and some headroom */
712 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
714 struct sk_buff
*skb2
;
715 int delta
= headroom
- skb_headroom(skb
);
718 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
720 skb2
= skb_clone(skb
, GFP_ATOMIC
);
721 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
732 * skb_copy_expand - copy and expand sk_buff
733 * @skb: buffer to copy
734 * @newheadroom: new free bytes at head
735 * @newtailroom: new free bytes at tail
736 * @gfp_mask: allocation priority
738 * Make a copy of both an &sk_buff and its data and while doing so
739 * allocate additional space.
741 * This is used when the caller wishes to modify the data and needs a
742 * private copy of the data to alter as well as more space for new fields.
743 * Returns %NULL on failure or the pointer to the buffer
744 * on success. The returned buffer has a reference count of 1.
746 * You must pass %GFP_ATOMIC as the allocation priority if this function
747 * is called from an interrupt.
749 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
750 * only by netfilter in the cases when checksum is recalculated? --ANK
752 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
753 int newheadroom
, int newtailroom
,
757 * Allocate the copy buffer
759 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
761 int head_copy_len
, head_copy_off
;
766 skb_reserve(n
, newheadroom
);
768 /* Set the tail pointer and length */
769 skb_put(n
, skb
->len
);
771 head_copy_len
= skb_headroom(skb
);
773 if (newheadroom
<= head_copy_len
)
774 head_copy_len
= newheadroom
;
776 head_copy_off
= newheadroom
- head_copy_len
;
778 /* Copy the linear header and data. */
779 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
780 skb
->len
+ head_copy_len
))
783 copy_skb_header(n
, skb
);
789 * skb_pad - zero pad the tail of an skb
790 * @skb: buffer to pad
793 * Ensure that a buffer is followed by a padding area that is zero
794 * filled. Used by network drivers which may DMA or transfer data
795 * beyond the buffer end onto the wire.
797 * May return error in out of memory cases. The skb is freed on error.
800 int skb_pad(struct sk_buff
*skb
, int pad
)
805 /* If the skbuff is non linear tailroom is always zero.. */
806 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
807 memset(skb
->data
+skb
->len
, 0, pad
);
811 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
812 if (likely(skb_cloned(skb
) || ntail
> 0)) {
813 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
818 /* FIXME: The use of this function with non-linear skb's really needs
821 err
= skb_linearize(skb
);
825 memset(skb
->data
+ skb
->len
, 0, pad
);
833 /* Trims skb to length len. It can change skb pointers.
836 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
838 struct sk_buff
**fragp
;
839 struct sk_buff
*frag
;
840 int offset
= skb_headlen(skb
);
841 int nfrags
= skb_shinfo(skb
)->nr_frags
;
845 if (skb_cloned(skb
) &&
846 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
849 for (i
= 0; i
< nfrags
; i
++) {
850 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
858 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
860 skb_shinfo(skb
)->nr_frags
= i
;
862 for (; i
< nfrags
; i
++)
863 put_page(skb_shinfo(skb
)->frags
[i
].page
);
865 if (skb_shinfo(skb
)->frag_list
)
866 skb_drop_fraglist(skb
);
870 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
871 fragp
= &frag
->next
) {
872 int end
= offset
+ frag
->len
;
874 if (skb_shared(frag
)) {
875 struct sk_buff
*nfrag
;
877 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
878 if (unlikely(!nfrag
))
881 nfrag
->next
= frag
->next
;
892 unlikely((err
= pskb_trim(frag
, len
- offset
))))
896 skb_drop_list(&frag
->next
);
900 if (len
> skb_headlen(skb
)) {
901 skb
->data_len
-= skb
->len
- len
;
906 skb
->tail
= skb
->data
+ len
;
913 * __pskb_pull_tail - advance tail of skb header
914 * @skb: buffer to reallocate
915 * @delta: number of bytes to advance tail
917 * The function makes a sense only on a fragmented &sk_buff,
918 * it expands header moving its tail forward and copying necessary
919 * data from fragmented part.
921 * &sk_buff MUST have reference count of 1.
923 * Returns %NULL (and &sk_buff does not change) if pull failed
924 * or value of new tail of skb in the case of success.
926 * All the pointers pointing into skb header may change and must be
927 * reloaded after call to this function.
930 /* Moves tail of skb head forward, copying data from fragmented part,
931 * when it is necessary.
932 * 1. It may fail due to malloc failure.
933 * 2. It may change skb pointers.
935 * It is pretty complicated. Luckily, it is called only in exceptional cases.
937 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
939 /* If skb has not enough free space at tail, get new one
940 * plus 128 bytes for future expansions. If we have enough
941 * room at tail, reallocate without expansion only if skb is cloned.
943 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
945 if (eat
> 0 || skb_cloned(skb
)) {
946 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
951 if (skb_copy_bits(skb
, skb_headlen(skb
), skb
->tail
, delta
))
954 /* Optimization: no fragments, no reasons to preestimate
955 * size of pulled pages. Superb.
957 if (!skb_shinfo(skb
)->frag_list
)
960 /* Estimate size of pulled pages. */
962 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
963 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
965 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
968 /* If we need update frag list, we are in troubles.
969 * Certainly, it possible to add an offset to skb data,
970 * but taking into account that pulling is expected to
971 * be very rare operation, it is worth to fight against
972 * further bloating skb head and crucify ourselves here instead.
973 * Pure masohism, indeed. 8)8)
976 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
977 struct sk_buff
*clone
= NULL
;
978 struct sk_buff
*insp
= NULL
;
983 if (list
->len
<= eat
) {
984 /* Eaten as whole. */
989 /* Eaten partially. */
991 if (skb_shared(list
)) {
992 /* Sucks! We need to fork list. :-( */
993 clone
= skb_clone(list
, GFP_ATOMIC
);
999 /* This may be pulled without
1003 if (!pskb_pull(list
, eat
)) {
1012 /* Free pulled out fragments. */
1013 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1014 skb_shinfo(skb
)->frag_list
= list
->next
;
1017 /* And insert new clone at head. */
1020 skb_shinfo(skb
)->frag_list
= clone
;
1023 /* Success! Now we may commit changes to skb data. */
1028 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1029 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1030 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1031 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1033 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1035 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1036 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1042 skb_shinfo(skb
)->nr_frags
= k
;
1045 skb
->data_len
-= delta
;
1050 /* Copy some data bits from skb to kernel buffer. */
1052 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1055 int start
= skb_headlen(skb
);
1057 if (offset
> (int)skb
->len
- len
)
1061 if ((copy
= start
- offset
) > 0) {
1064 memcpy(to
, skb
->data
+ offset
, copy
);
1065 if ((len
-= copy
) == 0)
1071 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1074 BUG_TRAP(start
<= offset
+ len
);
1076 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1077 if ((copy
= end
- offset
) > 0) {
1083 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1085 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1086 offset
- start
, copy
);
1087 kunmap_skb_frag(vaddr
);
1089 if ((len
-= copy
) == 0)
1097 if (skb_shinfo(skb
)->frag_list
) {
1098 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1100 for (; list
; list
= list
->next
) {
1103 BUG_TRAP(start
<= offset
+ len
);
1105 end
= start
+ list
->len
;
1106 if ((copy
= end
- offset
) > 0) {
1109 if (skb_copy_bits(list
, offset
- start
,
1112 if ((len
-= copy
) == 0)
1128 * skb_store_bits - store bits from kernel buffer to skb
1129 * @skb: destination buffer
1130 * @offset: offset in destination
1131 * @from: source buffer
1132 * @len: number of bytes to copy
1134 * Copy the specified number of bytes from the source buffer to the
1135 * destination skb. This function handles all the messy bits of
1136 * traversing fragment lists and such.
1139 int skb_store_bits(const struct sk_buff
*skb
, int offset
, void *from
, int len
)
1142 int start
= skb_headlen(skb
);
1144 if (offset
> (int)skb
->len
- len
)
1147 if ((copy
= start
- offset
) > 0) {
1150 memcpy(skb
->data
+ offset
, from
, copy
);
1151 if ((len
-= copy
) == 0)
1157 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1158 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1161 BUG_TRAP(start
<= offset
+ len
);
1163 end
= start
+ frag
->size
;
1164 if ((copy
= end
- offset
) > 0) {
1170 vaddr
= kmap_skb_frag(frag
);
1171 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1173 kunmap_skb_frag(vaddr
);
1175 if ((len
-= copy
) == 0)
1183 if (skb_shinfo(skb
)->frag_list
) {
1184 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1186 for (; list
; list
= list
->next
) {
1189 BUG_TRAP(start
<= offset
+ len
);
1191 end
= start
+ list
->len
;
1192 if ((copy
= end
- offset
) > 0) {
1195 if (skb_store_bits(list
, offset
- start
,
1198 if ((len
-= copy
) == 0)
1213 EXPORT_SYMBOL(skb_store_bits
);
1215 /* Checksum skb data. */
1217 unsigned int skb_checksum(const struct sk_buff
*skb
, int offset
,
1218 int len
, unsigned int csum
)
1220 int start
= skb_headlen(skb
);
1221 int i
, copy
= start
- offset
;
1224 /* Checksum header. */
1228 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1229 if ((len
-= copy
) == 0)
1235 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1238 BUG_TRAP(start
<= offset
+ len
);
1240 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1241 if ((copy
= end
- offset
) > 0) {
1244 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1248 vaddr
= kmap_skb_frag(frag
);
1249 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1250 offset
- start
, copy
, 0);
1251 kunmap_skb_frag(vaddr
);
1252 csum
= csum_block_add(csum
, csum2
, pos
);
1261 if (skb_shinfo(skb
)->frag_list
) {
1262 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1264 for (; list
; list
= list
->next
) {
1267 BUG_TRAP(start
<= offset
+ len
);
1269 end
= start
+ list
->len
;
1270 if ((copy
= end
- offset
) > 0) {
1274 csum2
= skb_checksum(list
, offset
- start
,
1276 csum
= csum_block_add(csum
, csum2
, pos
);
1277 if ((len
-= copy
) == 0)
1290 /* Both of above in one bottle. */
1292 unsigned int skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1293 u8
*to
, int len
, unsigned int csum
)
1295 int start
= skb_headlen(skb
);
1296 int i
, copy
= start
- offset
;
1303 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1305 if ((len
-= copy
) == 0)
1312 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1315 BUG_TRAP(start
<= offset
+ len
);
1317 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1318 if ((copy
= end
- offset
) > 0) {
1321 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1325 vaddr
= kmap_skb_frag(frag
);
1326 csum2
= csum_partial_copy_nocheck(vaddr
+
1330 kunmap_skb_frag(vaddr
);
1331 csum
= csum_block_add(csum
, csum2
, pos
);
1341 if (skb_shinfo(skb
)->frag_list
) {
1342 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1344 for (; list
; list
= list
->next
) {
1348 BUG_TRAP(start
<= offset
+ len
);
1350 end
= start
+ list
->len
;
1351 if ((copy
= end
- offset
) > 0) {
1354 csum2
= skb_copy_and_csum_bits(list
,
1357 csum
= csum_block_add(csum
, csum2
, pos
);
1358 if ((len
-= copy
) == 0)
1371 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1376 if (skb
->ip_summed
== CHECKSUM_HW
)
1377 csstart
= skb
->h
.raw
- skb
->data
;
1379 csstart
= skb_headlen(skb
);
1381 BUG_ON(csstart
> skb_headlen(skb
));
1383 memcpy(to
, skb
->data
, csstart
);
1386 if (csstart
!= skb
->len
)
1387 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1388 skb
->len
- csstart
, 0);
1390 if (skb
->ip_summed
== CHECKSUM_HW
) {
1391 long csstuff
= csstart
+ skb
->csum
;
1393 *((unsigned short *)(to
+ csstuff
)) = csum_fold(csum
);
1398 * skb_dequeue - remove from the head of the queue
1399 * @list: list to dequeue from
1401 * Remove the head of the list. The list lock is taken so the function
1402 * may be used safely with other locking list functions. The head item is
1403 * returned or %NULL if the list is empty.
1406 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1408 unsigned long flags
;
1409 struct sk_buff
*result
;
1411 spin_lock_irqsave(&list
->lock
, flags
);
1412 result
= __skb_dequeue(list
);
1413 spin_unlock_irqrestore(&list
->lock
, flags
);
1418 * skb_dequeue_tail - remove from the tail of the queue
1419 * @list: list to dequeue from
1421 * Remove the tail of the list. The list lock is taken so the function
1422 * may be used safely with other locking list functions. The tail item is
1423 * returned or %NULL if the list is empty.
1425 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1427 unsigned long flags
;
1428 struct sk_buff
*result
;
1430 spin_lock_irqsave(&list
->lock
, flags
);
1431 result
= __skb_dequeue_tail(list
);
1432 spin_unlock_irqrestore(&list
->lock
, flags
);
1437 * skb_queue_purge - empty a list
1438 * @list: list to empty
1440 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1441 * the list and one reference dropped. This function takes the list
1442 * lock and is atomic with respect to other list locking functions.
1444 void skb_queue_purge(struct sk_buff_head
*list
)
1446 struct sk_buff
*skb
;
1447 while ((skb
= skb_dequeue(list
)) != NULL
)
1452 * skb_queue_head - queue a buffer at the list head
1453 * @list: list to use
1454 * @newsk: buffer to queue
1456 * Queue a buffer at the start of the list. This function takes the
1457 * list lock and can be used safely with other locking &sk_buff functions
1460 * A buffer cannot be placed on two lists at the same time.
1462 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1464 unsigned long flags
;
1466 spin_lock_irqsave(&list
->lock
, flags
);
1467 __skb_queue_head(list
, newsk
);
1468 spin_unlock_irqrestore(&list
->lock
, flags
);
1472 * skb_queue_tail - queue a buffer at the list tail
1473 * @list: list to use
1474 * @newsk: buffer to queue
1476 * Queue a buffer at the tail of the list. This function takes the
1477 * list lock and can be used safely with other locking &sk_buff functions
1480 * A buffer cannot be placed on two lists at the same time.
1482 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1484 unsigned long flags
;
1486 spin_lock_irqsave(&list
->lock
, flags
);
1487 __skb_queue_tail(list
, newsk
);
1488 spin_unlock_irqrestore(&list
->lock
, flags
);
1492 * skb_unlink - remove a buffer from a list
1493 * @skb: buffer to remove
1494 * @list: list to use
1496 * Remove a packet from a list. The list locks are taken and this
1497 * function is atomic with respect to other list locked calls
1499 * You must know what list the SKB is on.
1501 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1503 unsigned long flags
;
1505 spin_lock_irqsave(&list
->lock
, flags
);
1506 __skb_unlink(skb
, list
);
1507 spin_unlock_irqrestore(&list
->lock
, flags
);
1511 * skb_append - append a buffer
1512 * @old: buffer to insert after
1513 * @newsk: buffer to insert
1514 * @list: list to use
1516 * Place a packet after a given packet in a list. The list locks are taken
1517 * and this function is atomic with respect to other list locked calls.
1518 * A buffer cannot be placed on two lists at the same time.
1520 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1522 unsigned long flags
;
1524 spin_lock_irqsave(&list
->lock
, flags
);
1525 __skb_append(old
, newsk
, list
);
1526 spin_unlock_irqrestore(&list
->lock
, flags
);
1531 * skb_insert - insert a buffer
1532 * @old: buffer to insert before
1533 * @newsk: buffer to insert
1534 * @list: list to use
1536 * Place a packet before a given packet in a list. The list locks are
1537 * taken and this function is atomic with respect to other list locked
1540 * A buffer cannot be placed on two lists at the same time.
1542 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1544 unsigned long flags
;
1546 spin_lock_irqsave(&list
->lock
, flags
);
1547 __skb_insert(newsk
, old
->prev
, old
, list
);
1548 spin_unlock_irqrestore(&list
->lock
, flags
);
1553 * Tune the memory allocator for a new MTU size.
1555 void skb_add_mtu(int mtu
)
1557 /* Must match allocation in alloc_skb */
1558 mtu
= SKB_DATA_ALIGN(mtu
) + sizeof(struct skb_shared_info
);
1560 kmem_add_cache_size(mtu
);
1564 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1565 struct sk_buff
* skb1
,
1566 const u32 len
, const int pos
)
1570 memcpy(skb_put(skb1
, pos
- len
), skb
->data
+ len
, pos
- len
);
1572 /* And move data appendix as is. */
1573 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1574 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1576 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1577 skb_shinfo(skb
)->nr_frags
= 0;
1578 skb1
->data_len
= skb
->data_len
;
1579 skb1
->len
+= skb1
->data_len
;
1582 skb
->tail
= skb
->data
+ len
;
1585 static inline void skb_split_no_header(struct sk_buff
*skb
,
1586 struct sk_buff
* skb1
,
1587 const u32 len
, int pos
)
1590 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1592 skb_shinfo(skb
)->nr_frags
= 0;
1593 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1595 skb
->data_len
= len
- pos
;
1597 for (i
= 0; i
< nfrags
; i
++) {
1598 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1600 if (pos
+ size
> len
) {
1601 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1605 * We have two variants in this case:
1606 * 1. Move all the frag to the second
1607 * part, if it is possible. F.e.
1608 * this approach is mandatory for TUX,
1609 * where splitting is expensive.
1610 * 2. Split is accurately. We make this.
1612 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1613 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1614 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1615 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1616 skb_shinfo(skb
)->nr_frags
++;
1620 skb_shinfo(skb
)->nr_frags
++;
1623 skb_shinfo(skb1
)->nr_frags
= k
;
1627 * skb_split - Split fragmented skb to two parts at length len.
1628 * @skb: the buffer to split
1629 * @skb1: the buffer to receive the second part
1630 * @len: new length for skb
1632 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1634 int pos
= skb_headlen(skb
);
1636 if (len
< pos
) /* Split line is inside header. */
1637 skb_split_inside_header(skb
, skb1
, len
, pos
);
1638 else /* Second chunk has no header, nothing to copy. */
1639 skb_split_no_header(skb
, skb1
, len
, pos
);
1643 * skb_prepare_seq_read - Prepare a sequential read of skb data
1644 * @skb: the buffer to read
1645 * @from: lower offset of data to be read
1646 * @to: upper offset of data to be read
1647 * @st: state variable
1649 * Initializes the specified state variable. Must be called before
1650 * invoking skb_seq_read() for the first time.
1652 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1653 unsigned int to
, struct skb_seq_state
*st
)
1655 st
->lower_offset
= from
;
1656 st
->upper_offset
= to
;
1657 st
->root_skb
= st
->cur_skb
= skb
;
1658 st
->frag_idx
= st
->stepped_offset
= 0;
1659 st
->frag_data
= NULL
;
1663 * skb_seq_read - Sequentially read skb data
1664 * @consumed: number of bytes consumed by the caller so far
1665 * @data: destination pointer for data to be returned
1666 * @st: state variable
1668 * Reads a block of skb data at &consumed relative to the
1669 * lower offset specified to skb_prepare_seq_read(). Assigns
1670 * the head of the data block to &data and returns the length
1671 * of the block or 0 if the end of the skb data or the upper
1672 * offset has been reached.
1674 * The caller is not required to consume all of the data
1675 * returned, i.e. &consumed is typically set to the number
1676 * of bytes already consumed and the next call to
1677 * skb_seq_read() will return the remaining part of the block.
1679 * Note: The size of each block of data returned can be arbitary,
1680 * this limitation is the cost for zerocopy seqeuental
1681 * reads of potentially non linear data.
1683 * Note: Fragment lists within fragments are not implemented
1684 * at the moment, state->root_skb could be replaced with
1685 * a stack for this purpose.
1687 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1688 struct skb_seq_state
*st
)
1690 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1693 if (unlikely(abs_offset
>= st
->upper_offset
))
1697 block_limit
= skb_headlen(st
->cur_skb
);
1699 if (abs_offset
< block_limit
) {
1700 *data
= st
->cur_skb
->data
+ abs_offset
;
1701 return block_limit
- abs_offset
;
1704 if (st
->frag_idx
== 0 && !st
->frag_data
)
1705 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1707 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1708 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1709 block_limit
= frag
->size
+ st
->stepped_offset
;
1711 if (abs_offset
< block_limit
) {
1713 st
->frag_data
= kmap_skb_frag(frag
);
1715 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1716 (abs_offset
- st
->stepped_offset
);
1718 return block_limit
- abs_offset
;
1721 if (st
->frag_data
) {
1722 kunmap_skb_frag(st
->frag_data
);
1723 st
->frag_data
= NULL
;
1727 st
->stepped_offset
+= frag
->size
;
1730 if (st
->cur_skb
->next
) {
1731 st
->cur_skb
= st
->cur_skb
->next
;
1734 } else if (st
->root_skb
== st
->cur_skb
&&
1735 skb_shinfo(st
->root_skb
)->frag_list
) {
1736 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1744 * skb_abort_seq_read - Abort a sequential read of skb data
1745 * @st: state variable
1747 * Must be called if skb_seq_read() was not called until it
1750 void skb_abort_seq_read(struct skb_seq_state
*st
)
1753 kunmap_skb_frag(st
->frag_data
);
1756 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1758 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1759 struct ts_config
*conf
,
1760 struct ts_state
*state
)
1762 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
1765 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
1767 skb_abort_seq_read(TS_SKB_CB(state
));
1771 * skb_find_text - Find a text pattern in skb data
1772 * @skb: the buffer to look in
1773 * @from: search offset
1775 * @config: textsearch configuration
1776 * @state: uninitialized textsearch state variable
1778 * Finds a pattern in the skb data according to the specified
1779 * textsearch configuration. Use textsearch_next() to retrieve
1780 * subsequent occurrences of the pattern. Returns the offset
1781 * to the first occurrence or UINT_MAX if no match was found.
1783 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
1784 unsigned int to
, struct ts_config
*config
,
1785 struct ts_state
*state
)
1789 config
->get_next_block
= skb_ts_get_next_block
;
1790 config
->finish
= skb_ts_finish
;
1792 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
1794 ret
= textsearch_find(config
, state
);
1795 return (ret
<= to
- from
? ret
: UINT_MAX
);
1799 * skb_append_datato_frags: - append the user data to a skb
1800 * @sk: sock structure
1801 * @skb: skb structure to be appened with user data.
1802 * @getfrag: call back function to be used for getting the user data
1803 * @from: pointer to user message iov
1804 * @length: length of the iov message
1806 * Description: This procedure append the user data in the fragment part
1807 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1809 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
1810 int (*getfrag
)(void *from
, char *to
, int offset
,
1811 int len
, int odd
, struct sk_buff
*skb
),
1812 void *from
, int length
)
1815 skb_frag_t
*frag
= NULL
;
1816 struct page
*page
= NULL
;
1822 /* Return error if we don't have space for new frag */
1823 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1824 if (frg_cnt
>= MAX_SKB_FRAGS
)
1827 /* allocate a new page for next frag */
1828 page
= alloc_pages(sk
->sk_allocation
, 0);
1830 /* If alloc_page fails just return failure and caller will
1831 * free previous allocated pages by doing kfree_skb()
1836 /* initialize the next frag */
1837 sk
->sk_sndmsg_page
= page
;
1838 sk
->sk_sndmsg_off
= 0;
1839 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
1840 skb
->truesize
+= PAGE_SIZE
;
1841 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
1843 /* get the new initialized frag */
1844 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1845 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
1847 /* copy the user data to page */
1848 left
= PAGE_SIZE
- frag
->page_offset
;
1849 copy
= (length
> left
)? left
: length
;
1851 ret
= getfrag(from
, (page_address(frag
->page
) +
1852 frag
->page_offset
+ frag
->size
),
1853 offset
, copy
, 0, skb
);
1857 /* copy was successful so update the size parameters */
1858 sk
->sk_sndmsg_off
+= copy
;
1861 skb
->data_len
+= copy
;
1865 } while (length
> 0);
1871 * skb_pull_rcsum - pull skb and update receive checksum
1872 * @skb: buffer to update
1873 * @start: start of data before pull
1874 * @len: length of data pulled
1876 * This function performs an skb_pull on the packet and updates
1877 * update the CHECKSUM_HW checksum. It should be used on receive
1878 * path processing instead of skb_pull unless you know that the
1879 * checksum difference is zero (e.g., a valid IP header) or you
1880 * are setting ip_summed to CHECKSUM_NONE.
1882 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
1884 BUG_ON(len
> skb
->len
);
1886 BUG_ON(skb
->len
< skb
->data_len
);
1887 skb_postpull_rcsum(skb
, skb
->data
, len
);
1888 return skb
->data
+= len
;
1891 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
1894 * skb_segment - Perform protocol segmentation on skb.
1895 * @skb: buffer to segment
1896 * @features: features for the output path (see dev->features)
1898 * This function performs segmentation on the given skb. It returns
1899 * the segment at the given position. It returns NULL if there are
1900 * no more segments to generate, or when an error is encountered.
1902 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
1904 struct sk_buff
*segs
= NULL
;
1905 struct sk_buff
*tail
= NULL
;
1906 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
1907 unsigned int doffset
= skb
->data
- skb
->mac
.raw
;
1908 unsigned int offset
= doffset
;
1909 unsigned int headroom
;
1911 int sg
= features
& NETIF_F_SG
;
1912 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1917 __skb_push(skb
, doffset
);
1918 headroom
= skb_headroom(skb
);
1919 pos
= skb_headlen(skb
);
1922 struct sk_buff
*nskb
;
1928 len
= skb
->len
- offset
;
1932 hsize
= skb_headlen(skb
) - offset
;
1935 nsize
= hsize
+ doffset
;
1936 if (nsize
> len
+ doffset
|| !sg
)
1937 nsize
= len
+ doffset
;
1939 nskb
= alloc_skb(nsize
+ headroom
, GFP_ATOMIC
);
1940 if (unlikely(!nskb
))
1949 nskb
->dev
= skb
->dev
;
1950 nskb
->priority
= skb
->priority
;
1951 nskb
->protocol
= skb
->protocol
;
1952 nskb
->dst
= dst_clone(skb
->dst
);
1953 memcpy(nskb
->cb
, skb
->cb
, sizeof(skb
->cb
));
1954 nskb
->pkt_type
= skb
->pkt_type
;
1955 nskb
->mac_len
= skb
->mac_len
;
1957 skb_reserve(nskb
, headroom
);
1958 nskb
->mac
.raw
= nskb
->data
;
1959 nskb
->nh
.raw
= nskb
->data
+ skb
->mac_len
;
1960 nskb
->h
.raw
= nskb
->nh
.raw
+ (skb
->h
.raw
- skb
->nh
.raw
);
1961 memcpy(skb_put(nskb
, doffset
), skb
->data
, doffset
);
1964 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
1970 frag
= skb_shinfo(nskb
)->frags
;
1973 nskb
->ip_summed
= CHECKSUM_HW
;
1974 nskb
->csum
= skb
->csum
;
1975 memcpy(skb_put(nskb
, hsize
), skb
->data
+ offset
, hsize
);
1977 while (pos
< offset
+ len
) {
1978 BUG_ON(i
>= nfrags
);
1980 *frag
= skb_shinfo(skb
)->frags
[i
];
1981 get_page(frag
->page
);
1985 frag
->page_offset
+= offset
- pos
;
1986 frag
->size
-= offset
- pos
;
1991 if (pos
+ size
<= offset
+ len
) {
1995 frag
->size
-= pos
+ size
- (offset
+ len
);
2002 skb_shinfo(nskb
)->nr_frags
= k
;
2003 nskb
->data_len
= len
- hsize
;
2004 nskb
->len
+= nskb
->data_len
;
2005 nskb
->truesize
+= nskb
->data_len
;
2006 } while ((offset
+= len
) < skb
->len
);
2011 while ((skb
= segs
)) {
2015 return ERR_PTR(err
);
2018 EXPORT_SYMBOL_GPL(skb_segment
);
2020 void __init
skb_init(void)
2022 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2023 sizeof(struct sk_buff
),
2027 if (!skbuff_head_cache
)
2028 panic("cannot create skbuff cache");
2030 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2031 (2*sizeof(struct sk_buff
)) +
2036 if (!skbuff_fclone_cache
)
2037 panic("cannot create skbuff cache");
2040 EXPORT_SYMBOL(___pskb_trim
);
2041 EXPORT_SYMBOL(__kfree_skb
);
2042 EXPORT_SYMBOL(kfree_skb
);
2043 EXPORT_SYMBOL(__pskb_pull_tail
);
2044 EXPORT_SYMBOL(__alloc_skb
);
2045 EXPORT_SYMBOL(pskb_copy
);
2046 EXPORT_SYMBOL(pskb_expand_head
);
2047 EXPORT_SYMBOL(skb_checksum
);
2048 EXPORT_SYMBOL(skb_clone
);
2049 EXPORT_SYMBOL(skb_clone_fraglist
);
2050 EXPORT_SYMBOL(skb_copy
);
2051 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2052 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2053 EXPORT_SYMBOL(skb_copy_bits
);
2054 EXPORT_SYMBOL(skb_copy_expand
);
2055 EXPORT_SYMBOL(skb_over_panic
);
2056 EXPORT_SYMBOL(skb_pad
);
2057 EXPORT_SYMBOL(skb_realloc_headroom
);
2058 EXPORT_SYMBOL(skb_under_panic
);
2059 EXPORT_SYMBOL(skb_dequeue
);
2060 EXPORT_SYMBOL(skb_dequeue_tail
);
2061 EXPORT_SYMBOL(skb_insert
);
2062 EXPORT_SYMBOL(skb_queue_purge
);
2063 EXPORT_SYMBOL(skb_queue_head
);
2064 EXPORT_SYMBOL(skb_queue_tail
);
2065 EXPORT_SYMBOL(skb_unlink
);
2066 EXPORT_SYMBOL(skb_append
);
2067 EXPORT_SYMBOL(skb_split
);
2068 EXPORT_SYMBOL(skb_prepare_seq_read
);
2069 EXPORT_SYMBOL(skb_seq_read
);
2070 EXPORT_SYMBOL(skb_abort_seq_read
);
2071 EXPORT_SYMBOL(skb_find_text
);
2072 EXPORT_SYMBOL(skb_append_datato_frags
);