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>
59 #include <net/protocol.h>
62 #include <net/checksum.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
70 static struct kmem_cache
*skbuff_head_cache __read_mostly
;
71 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
80 * skb_over_panic - private function
85 * Out of line support code for skb_put(). Not user callable.
87 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
89 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
92 skb
->dev
? skb
->dev
->name
: "<NULL>");
97 * skb_under_panic - private function
102 * Out of line support code for skb_push(). Not user callable.
105 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
107 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here
, skb
->len
, sz
, skb
->head
, skb
->data
, skb
->tail
, skb
->end
,
110 skb
->dev
? skb
->dev
->name
: "<NULL>");
114 void skb_truesize_bug(struct sk_buff
*skb
)
116 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
120 EXPORT_SYMBOL(skb_truesize_bug
);
122 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
129 * __alloc_skb - allocate a network buffer
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
134 * @node: numa node to allocate memory on
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
144 int fclone
, int node
)
146 struct kmem_cache
*cache
;
147 struct skb_shared_info
*shinfo
;
151 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
154 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size
= SKB_DATA_ALIGN(size
);
160 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
165 memset(skb
, 0, offsetof(struct sk_buff
, truesize
));
166 skb
->truesize
= size
+ sizeof(struct sk_buff
);
167 atomic_set(&skb
->users
, 1);
171 skb
->end
= data
+ size
;
172 /* make sure we initialize shinfo sequentially */
173 shinfo
= skb_shinfo(skb
);
174 atomic_set(&shinfo
->dataref
, 1);
175 shinfo
->nr_frags
= 0;
176 shinfo
->gso_size
= 0;
177 shinfo
->gso_segs
= 0;
178 shinfo
->gso_type
= 0;
179 shinfo
->ip6_frag_id
= 0;
180 shinfo
->frag_list
= NULL
;
183 struct sk_buff
*child
= skb
+ 1;
184 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
186 skb
->fclone
= SKB_FCLONE_ORIG
;
187 atomic_set(fclone_ref
, 1);
189 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
194 kmem_cache_free(cache
, skb
);
200 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
201 * @dev: network device to receive on
202 * @length: length to allocate
203 * @gfp_mask: get_free_pages mask, passed to alloc_skb
205 * Allocate a new &sk_buff and assign it a usage count of one. The
206 * buffer has unspecified headroom built in. Users should allocate
207 * the headroom they think they need without accounting for the
208 * built in space. The built in space is used for optimisations.
210 * %NULL is returned if there is no free memory.
212 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
213 unsigned int length
, gfp_t gfp_mask
)
215 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
218 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
, 0, node
);
220 skb_reserve(skb
, NET_SKB_PAD
);
226 static void skb_drop_list(struct sk_buff
**listp
)
228 struct sk_buff
*list
= *listp
;
233 struct sk_buff
*this = list
;
239 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
241 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
244 static void skb_clone_fraglist(struct sk_buff
*skb
)
246 struct sk_buff
*list
;
248 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
252 static void skb_release_data(struct sk_buff
*skb
)
255 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
256 &skb_shinfo(skb
)->dataref
)) {
257 if (skb_shinfo(skb
)->nr_frags
) {
259 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
260 put_page(skb_shinfo(skb
)->frags
[i
].page
);
263 if (skb_shinfo(skb
)->frag_list
)
264 skb_drop_fraglist(skb
);
271 * Free an skbuff by memory without cleaning the state.
273 void kfree_skbmem(struct sk_buff
*skb
)
275 struct sk_buff
*other
;
276 atomic_t
*fclone_ref
;
278 skb_release_data(skb
);
279 switch (skb
->fclone
) {
280 case SKB_FCLONE_UNAVAILABLE
:
281 kmem_cache_free(skbuff_head_cache
, skb
);
284 case SKB_FCLONE_ORIG
:
285 fclone_ref
= (atomic_t
*) (skb
+ 2);
286 if (atomic_dec_and_test(fclone_ref
))
287 kmem_cache_free(skbuff_fclone_cache
, skb
);
290 case SKB_FCLONE_CLONE
:
291 fclone_ref
= (atomic_t
*) (skb
+ 1);
294 /* The clone portion is available for
295 * fast-cloning again.
297 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
299 if (atomic_dec_and_test(fclone_ref
))
300 kmem_cache_free(skbuff_fclone_cache
, other
);
306 * __kfree_skb - private function
309 * Free an sk_buff. Release anything attached to the buffer.
310 * Clean the state. This is an internal helper function. Users should
311 * always call kfree_skb
314 void __kfree_skb(struct sk_buff
*skb
)
316 dst_release(skb
->dst
);
318 secpath_put(skb
->sp
);
320 if (skb
->destructor
) {
322 skb
->destructor(skb
);
324 #ifdef CONFIG_NETFILTER
325 nf_conntrack_put(skb
->nfct
);
326 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
327 nf_conntrack_put_reasm(skb
->nfct_reasm
);
329 #ifdef CONFIG_BRIDGE_NETFILTER
330 nf_bridge_put(skb
->nf_bridge
);
333 /* XXX: IS this still necessary? - JHS */
334 #ifdef CONFIG_NET_SCHED
336 #ifdef CONFIG_NET_CLS_ACT
345 * kfree_skb - free an sk_buff
346 * @skb: buffer to free
348 * Drop a reference to the buffer and free it if the usage count has
351 void kfree_skb(struct sk_buff
*skb
)
355 if (likely(atomic_read(&skb
->users
) == 1))
357 else if (likely(!atomic_dec_and_test(&skb
->users
)))
363 * skb_clone - duplicate an sk_buff
364 * @skb: buffer to clone
365 * @gfp_mask: allocation priority
367 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
368 * copies share the same packet data but not structure. The new
369 * buffer has a reference count of 1. If the allocation fails the
370 * function returns %NULL otherwise the new buffer is returned.
372 * If this function is called from an interrupt gfp_mask() must be
376 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
381 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
382 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
383 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
384 n
->fclone
= SKB_FCLONE_CLONE
;
385 atomic_inc(fclone_ref
);
387 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
390 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
393 #define C(x) n->x = skb->x
395 n
->next
= n
->prev
= NULL
;
406 secpath_get(skb
->sp
);
408 memcpy(n
->cb
, skb
->cb
, sizeof(skb
->cb
));
419 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
423 n
->destructor
= NULL
;
426 #ifdef CONFIG_NET_SCHED
428 #ifdef CONFIG_NET_CLS_ACT
429 n
->tc_verd
= SET_TC_VERD(skb
->tc_verd
,0);
430 n
->tc_verd
= CLR_TC_OK2MUNGE(n
->tc_verd
);
431 n
->tc_verd
= CLR_TC_MUNGED(n
->tc_verd
);
434 skb_copy_secmark(n
, skb
);
437 atomic_set(&n
->users
, 1);
443 atomic_inc(&(skb_shinfo(skb
)->dataref
));
449 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
452 * Shift between the two data areas in bytes
454 unsigned long offset
= new->data
- old
->data
;
458 new->priority
= old
->priority
;
459 new->protocol
= old
->protocol
;
460 new->dst
= dst_clone(old
->dst
);
462 new->sp
= secpath_get(old
->sp
);
464 new->h
.raw
= old
->h
.raw
+ offset
;
465 new->nh
.raw
= old
->nh
.raw
+ offset
;
466 new->mac
.raw
= old
->mac
.raw
+ offset
;
467 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
468 new->local_df
= old
->local_df
;
469 new->fclone
= SKB_FCLONE_UNAVAILABLE
;
470 new->pkt_type
= old
->pkt_type
;
471 new->tstamp
= old
->tstamp
;
472 new->destructor
= NULL
;
473 new->mark
= old
->mark
;
475 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
476 new->ipvs_property
= old
->ipvs_property
;
478 #ifdef CONFIG_NET_SCHED
479 #ifdef CONFIG_NET_CLS_ACT
480 new->tc_verd
= old
->tc_verd
;
482 new->tc_index
= old
->tc_index
;
484 skb_copy_secmark(new, old
);
485 atomic_set(&new->users
, 1);
486 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
487 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
488 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
492 * skb_copy - create private copy of an sk_buff
493 * @skb: buffer to copy
494 * @gfp_mask: allocation priority
496 * Make a copy of both an &sk_buff and its data. This is used when the
497 * caller wishes to modify the data and needs a private copy of the
498 * data to alter. Returns %NULL on failure or the pointer to the buffer
499 * on success. The returned buffer has a reference count of 1.
501 * As by-product this function converts non-linear &sk_buff to linear
502 * one, so that &sk_buff becomes completely private and caller is allowed
503 * to modify all the data of returned buffer. This means that this
504 * function is not recommended for use in circumstances when only
505 * header is going to be modified. Use pskb_copy() instead.
508 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
510 int headerlen
= skb
->data
- skb
->head
;
512 * Allocate the copy buffer
514 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
,
519 /* Set the data pointer */
520 skb_reserve(n
, headerlen
);
521 /* Set the tail pointer and length */
522 skb_put(n
, skb
->len
);
524 n
->ip_summed
= skb
->ip_summed
;
526 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
529 copy_skb_header(n
, skb
);
535 * pskb_copy - create copy of an sk_buff with private head.
536 * @skb: buffer to copy
537 * @gfp_mask: allocation priority
539 * Make a copy of both an &sk_buff and part of its data, located
540 * in header. Fragmented data remain shared. This is used when
541 * the caller wishes to modify only header of &sk_buff and needs
542 * private copy of the header to alter. Returns %NULL on failure
543 * or the pointer to the buffer on success.
544 * The returned buffer has a reference count of 1.
547 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
550 * Allocate the copy buffer
552 struct sk_buff
*n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
557 /* Set the data pointer */
558 skb_reserve(n
, skb
->data
- skb
->head
);
559 /* Set the tail pointer and length */
560 skb_put(n
, skb_headlen(skb
));
562 memcpy(n
->data
, skb
->data
, n
->len
);
564 n
->ip_summed
= skb
->ip_summed
;
566 n
->truesize
+= skb
->data_len
;
567 n
->data_len
= skb
->data_len
;
570 if (skb_shinfo(skb
)->nr_frags
) {
573 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
574 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
575 get_page(skb_shinfo(n
)->frags
[i
].page
);
577 skb_shinfo(n
)->nr_frags
= i
;
580 if (skb_shinfo(skb
)->frag_list
) {
581 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
582 skb_clone_fraglist(n
);
585 copy_skb_header(n
, skb
);
591 * pskb_expand_head - reallocate header of &sk_buff
592 * @skb: buffer to reallocate
593 * @nhead: room to add at head
594 * @ntail: room to add at tail
595 * @gfp_mask: allocation priority
597 * Expands (or creates identical copy, if &nhead and &ntail are zero)
598 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
599 * reference count of 1. Returns zero in the case of success or error,
600 * if expansion failed. In the last case, &sk_buff is not changed.
602 * All the pointers pointing into skb header may change and must be
603 * reloaded after call to this function.
606 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
611 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
617 size
= SKB_DATA_ALIGN(size
);
619 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
623 /* Copy only real data... and, alas, header. This should be
624 * optimized for the cases when header is void. */
625 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
626 memcpy(data
+ size
, skb
->end
, sizeof(struct skb_shared_info
));
628 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
629 get_page(skb_shinfo(skb
)->frags
[i
].page
);
631 if (skb_shinfo(skb
)->frag_list
)
632 skb_clone_fraglist(skb
);
634 skb_release_data(skb
);
636 off
= (data
+ nhead
) - skb
->head
;
639 skb
->end
= data
+ size
;
647 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
654 /* Make private copy of skb with writable head and some headroom */
656 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
658 struct sk_buff
*skb2
;
659 int delta
= headroom
- skb_headroom(skb
);
662 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
664 skb2
= skb_clone(skb
, GFP_ATOMIC
);
665 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
676 * skb_copy_expand - copy and expand sk_buff
677 * @skb: buffer to copy
678 * @newheadroom: new free bytes at head
679 * @newtailroom: new free bytes at tail
680 * @gfp_mask: allocation priority
682 * Make a copy of both an &sk_buff and its data and while doing so
683 * allocate additional space.
685 * This is used when the caller wishes to modify the data and needs a
686 * private copy of the data to alter as well as more space for new fields.
687 * Returns %NULL on failure or the pointer to the buffer
688 * on success. The returned buffer has a reference count of 1.
690 * You must pass %GFP_ATOMIC as the allocation priority if this function
691 * is called from an interrupt.
693 * BUG ALERT: ip_summed is not copied. Why does this work? Is it used
694 * only by netfilter in the cases when checksum is recalculated? --ANK
696 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
697 int newheadroom
, int newtailroom
,
701 * Allocate the copy buffer
703 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
705 int head_copy_len
, head_copy_off
;
710 skb_reserve(n
, newheadroom
);
712 /* Set the tail pointer and length */
713 skb_put(n
, skb
->len
);
715 head_copy_len
= skb_headroom(skb
);
717 if (newheadroom
<= head_copy_len
)
718 head_copy_len
= newheadroom
;
720 head_copy_off
= newheadroom
- head_copy_len
;
722 /* Copy the linear header and data. */
723 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
724 skb
->len
+ head_copy_len
))
727 copy_skb_header(n
, skb
);
733 * skb_pad - zero pad the tail of an skb
734 * @skb: buffer to pad
737 * Ensure that a buffer is followed by a padding area that is zero
738 * filled. Used by network drivers which may DMA or transfer data
739 * beyond the buffer end onto the wire.
741 * May return error in out of memory cases. The skb is freed on error.
744 int skb_pad(struct sk_buff
*skb
, int pad
)
749 /* If the skbuff is non linear tailroom is always zero.. */
750 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
751 memset(skb
->data
+skb
->len
, 0, pad
);
755 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
756 if (likely(skb_cloned(skb
) || ntail
> 0)) {
757 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
762 /* FIXME: The use of this function with non-linear skb's really needs
765 err
= skb_linearize(skb
);
769 memset(skb
->data
+ skb
->len
, 0, pad
);
777 /* Trims skb to length len. It can change skb pointers.
780 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
782 struct sk_buff
**fragp
;
783 struct sk_buff
*frag
;
784 int offset
= skb_headlen(skb
);
785 int nfrags
= skb_shinfo(skb
)->nr_frags
;
789 if (skb_cloned(skb
) &&
790 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
797 for (; i
< nfrags
; i
++) {
798 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
805 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
808 skb_shinfo(skb
)->nr_frags
= i
;
810 for (; i
< nfrags
; i
++)
811 put_page(skb_shinfo(skb
)->frags
[i
].page
);
813 if (skb_shinfo(skb
)->frag_list
)
814 skb_drop_fraglist(skb
);
818 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
819 fragp
= &frag
->next
) {
820 int end
= offset
+ frag
->len
;
822 if (skb_shared(frag
)) {
823 struct sk_buff
*nfrag
;
825 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
826 if (unlikely(!nfrag
))
829 nfrag
->next
= frag
->next
;
841 unlikely((err
= pskb_trim(frag
, len
- offset
))))
845 skb_drop_list(&frag
->next
);
850 if (len
> skb_headlen(skb
)) {
851 skb
->data_len
-= skb
->len
- len
;
856 skb
->tail
= skb
->data
+ len
;
863 * __pskb_pull_tail - advance tail of skb header
864 * @skb: buffer to reallocate
865 * @delta: number of bytes to advance tail
867 * The function makes a sense only on a fragmented &sk_buff,
868 * it expands header moving its tail forward and copying necessary
869 * data from fragmented part.
871 * &sk_buff MUST have reference count of 1.
873 * Returns %NULL (and &sk_buff does not change) if pull failed
874 * or value of new tail of skb in the case of success.
876 * All the pointers pointing into skb header may change and must be
877 * reloaded after call to this function.
880 /* Moves tail of skb head forward, copying data from fragmented part,
881 * when it is necessary.
882 * 1. It may fail due to malloc failure.
883 * 2. It may change skb pointers.
885 * It is pretty complicated. Luckily, it is called only in exceptional cases.
887 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
889 /* If skb has not enough free space at tail, get new one
890 * plus 128 bytes for future expansions. If we have enough
891 * room at tail, reallocate without expansion only if skb is cloned.
893 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
895 if (eat
> 0 || skb_cloned(skb
)) {
896 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
901 if (skb_copy_bits(skb
, skb_headlen(skb
), skb
->tail
, delta
))
904 /* Optimization: no fragments, no reasons to preestimate
905 * size of pulled pages. Superb.
907 if (!skb_shinfo(skb
)->frag_list
)
910 /* Estimate size of pulled pages. */
912 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
913 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
915 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
918 /* If we need update frag list, we are in troubles.
919 * Certainly, it possible to add an offset to skb data,
920 * but taking into account that pulling is expected to
921 * be very rare operation, it is worth to fight against
922 * further bloating skb head and crucify ourselves here instead.
923 * Pure masohism, indeed. 8)8)
926 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
927 struct sk_buff
*clone
= NULL
;
928 struct sk_buff
*insp
= NULL
;
933 if (list
->len
<= eat
) {
934 /* Eaten as whole. */
939 /* Eaten partially. */
941 if (skb_shared(list
)) {
942 /* Sucks! We need to fork list. :-( */
943 clone
= skb_clone(list
, GFP_ATOMIC
);
949 /* This may be pulled without
953 if (!pskb_pull(list
, eat
)) {
962 /* Free pulled out fragments. */
963 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
964 skb_shinfo(skb
)->frag_list
= list
->next
;
967 /* And insert new clone at head. */
970 skb_shinfo(skb
)->frag_list
= clone
;
973 /* Success! Now we may commit changes to skb data. */
978 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
979 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
980 put_page(skb_shinfo(skb
)->frags
[i
].page
);
981 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
983 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
985 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
986 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
992 skb_shinfo(skb
)->nr_frags
= k
;
995 skb
->data_len
-= delta
;
1000 /* Copy some data bits from skb to kernel buffer. */
1002 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1005 int start
= skb_headlen(skb
);
1007 if (offset
> (int)skb
->len
- len
)
1011 if ((copy
= start
- offset
) > 0) {
1014 memcpy(to
, skb
->data
+ offset
, copy
);
1015 if ((len
-= copy
) == 0)
1021 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1024 BUG_TRAP(start
<= offset
+ len
);
1026 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1027 if ((copy
= end
- offset
) > 0) {
1033 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1035 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1036 offset
- start
, copy
);
1037 kunmap_skb_frag(vaddr
);
1039 if ((len
-= copy
) == 0)
1047 if (skb_shinfo(skb
)->frag_list
) {
1048 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1050 for (; list
; list
= list
->next
) {
1053 BUG_TRAP(start
<= offset
+ len
);
1055 end
= start
+ list
->len
;
1056 if ((copy
= end
- offset
) > 0) {
1059 if (skb_copy_bits(list
, offset
- start
,
1062 if ((len
-= copy
) == 0)
1078 * skb_store_bits - store bits from kernel buffer to skb
1079 * @skb: destination buffer
1080 * @offset: offset in destination
1081 * @from: source buffer
1082 * @len: number of bytes to copy
1084 * Copy the specified number of bytes from the source buffer to the
1085 * destination skb. This function handles all the messy bits of
1086 * traversing fragment lists and such.
1089 int skb_store_bits(const struct sk_buff
*skb
, int offset
, void *from
, int len
)
1092 int start
= skb_headlen(skb
);
1094 if (offset
> (int)skb
->len
- len
)
1097 if ((copy
= start
- offset
) > 0) {
1100 memcpy(skb
->data
+ offset
, from
, copy
);
1101 if ((len
-= copy
) == 0)
1107 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1108 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1111 BUG_TRAP(start
<= offset
+ len
);
1113 end
= start
+ frag
->size
;
1114 if ((copy
= end
- offset
) > 0) {
1120 vaddr
= kmap_skb_frag(frag
);
1121 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1123 kunmap_skb_frag(vaddr
);
1125 if ((len
-= copy
) == 0)
1133 if (skb_shinfo(skb
)->frag_list
) {
1134 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1136 for (; list
; list
= list
->next
) {
1139 BUG_TRAP(start
<= offset
+ len
);
1141 end
= start
+ list
->len
;
1142 if ((copy
= end
- offset
) > 0) {
1145 if (skb_store_bits(list
, offset
- start
,
1148 if ((len
-= copy
) == 0)
1163 EXPORT_SYMBOL(skb_store_bits
);
1165 /* Checksum skb data. */
1167 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1168 int len
, __wsum csum
)
1170 int start
= skb_headlen(skb
);
1171 int i
, copy
= start
- offset
;
1174 /* Checksum header. */
1178 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1179 if ((len
-= copy
) == 0)
1185 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1188 BUG_TRAP(start
<= offset
+ len
);
1190 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1191 if ((copy
= end
- offset
) > 0) {
1194 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1198 vaddr
= kmap_skb_frag(frag
);
1199 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1200 offset
- start
, copy
, 0);
1201 kunmap_skb_frag(vaddr
);
1202 csum
= csum_block_add(csum
, csum2
, pos
);
1211 if (skb_shinfo(skb
)->frag_list
) {
1212 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1214 for (; list
; list
= list
->next
) {
1217 BUG_TRAP(start
<= offset
+ len
);
1219 end
= start
+ list
->len
;
1220 if ((copy
= end
- offset
) > 0) {
1224 csum2
= skb_checksum(list
, offset
- start
,
1226 csum
= csum_block_add(csum
, csum2
, pos
);
1227 if ((len
-= copy
) == 0)
1240 /* Both of above in one bottle. */
1242 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1243 u8
*to
, int len
, __wsum csum
)
1245 int start
= skb_headlen(skb
);
1246 int i
, copy
= start
- offset
;
1253 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1255 if ((len
-= copy
) == 0)
1262 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1265 BUG_TRAP(start
<= offset
+ len
);
1267 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1268 if ((copy
= end
- offset
) > 0) {
1271 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1275 vaddr
= kmap_skb_frag(frag
);
1276 csum2
= csum_partial_copy_nocheck(vaddr
+
1280 kunmap_skb_frag(vaddr
);
1281 csum
= csum_block_add(csum
, csum2
, pos
);
1291 if (skb_shinfo(skb
)->frag_list
) {
1292 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1294 for (; list
; list
= list
->next
) {
1298 BUG_TRAP(start
<= offset
+ len
);
1300 end
= start
+ list
->len
;
1301 if ((copy
= end
- offset
) > 0) {
1304 csum2
= skb_copy_and_csum_bits(list
,
1307 csum
= csum_block_add(csum
, csum2
, pos
);
1308 if ((len
-= copy
) == 0)
1321 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1326 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1327 csstart
= skb_transport_offset(skb
);
1329 csstart
= skb_headlen(skb
);
1331 BUG_ON(csstart
> skb_headlen(skb
));
1333 memcpy(to
, skb
->data
, csstart
);
1336 if (csstart
!= skb
->len
)
1337 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1338 skb
->len
- csstart
, 0);
1340 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1341 long csstuff
= csstart
+ skb
->csum_offset
;
1343 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1348 * skb_dequeue - remove from the head of the queue
1349 * @list: list to dequeue from
1351 * Remove the head of the list. The list lock is taken so the function
1352 * may be used safely with other locking list functions. The head item is
1353 * returned or %NULL if the list is empty.
1356 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1358 unsigned long flags
;
1359 struct sk_buff
*result
;
1361 spin_lock_irqsave(&list
->lock
, flags
);
1362 result
= __skb_dequeue(list
);
1363 spin_unlock_irqrestore(&list
->lock
, flags
);
1368 * skb_dequeue_tail - remove from the tail of the queue
1369 * @list: list to dequeue from
1371 * Remove the tail of the list. The list lock is taken so the function
1372 * may be used safely with other locking list functions. The tail item is
1373 * returned or %NULL if the list is empty.
1375 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1377 unsigned long flags
;
1378 struct sk_buff
*result
;
1380 spin_lock_irqsave(&list
->lock
, flags
);
1381 result
= __skb_dequeue_tail(list
);
1382 spin_unlock_irqrestore(&list
->lock
, flags
);
1387 * skb_queue_purge - empty a list
1388 * @list: list to empty
1390 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1391 * the list and one reference dropped. This function takes the list
1392 * lock and is atomic with respect to other list locking functions.
1394 void skb_queue_purge(struct sk_buff_head
*list
)
1396 struct sk_buff
*skb
;
1397 while ((skb
= skb_dequeue(list
)) != NULL
)
1402 * skb_queue_head - queue a buffer at the list head
1403 * @list: list to use
1404 * @newsk: buffer to queue
1406 * Queue a buffer at the start of the list. This function takes the
1407 * list lock and can be used safely with other locking &sk_buff functions
1410 * A buffer cannot be placed on two lists at the same time.
1412 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1414 unsigned long flags
;
1416 spin_lock_irqsave(&list
->lock
, flags
);
1417 __skb_queue_head(list
, newsk
);
1418 spin_unlock_irqrestore(&list
->lock
, flags
);
1422 * skb_queue_tail - queue a buffer at the list tail
1423 * @list: list to use
1424 * @newsk: buffer to queue
1426 * Queue a buffer at the tail of the list. This function takes the
1427 * list lock and can be used safely with other locking &sk_buff functions
1430 * A buffer cannot be placed on two lists at the same time.
1432 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1434 unsigned long flags
;
1436 spin_lock_irqsave(&list
->lock
, flags
);
1437 __skb_queue_tail(list
, newsk
);
1438 spin_unlock_irqrestore(&list
->lock
, flags
);
1442 * skb_unlink - remove a buffer from a list
1443 * @skb: buffer to remove
1444 * @list: list to use
1446 * Remove a packet from a list. The list locks are taken and this
1447 * function is atomic with respect to other list locked calls
1449 * You must know what list the SKB is on.
1451 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1453 unsigned long flags
;
1455 spin_lock_irqsave(&list
->lock
, flags
);
1456 __skb_unlink(skb
, list
);
1457 spin_unlock_irqrestore(&list
->lock
, flags
);
1461 * skb_append - append a buffer
1462 * @old: buffer to insert after
1463 * @newsk: buffer to insert
1464 * @list: list to use
1466 * Place a packet after a given packet in a list. The list locks are taken
1467 * and this function is atomic with respect to other list locked calls.
1468 * A buffer cannot be placed on two lists at the same time.
1470 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1472 unsigned long flags
;
1474 spin_lock_irqsave(&list
->lock
, flags
);
1475 __skb_append(old
, newsk
, list
);
1476 spin_unlock_irqrestore(&list
->lock
, flags
);
1481 * skb_insert - insert a buffer
1482 * @old: buffer to insert before
1483 * @newsk: buffer to insert
1484 * @list: list to use
1486 * Place a packet before a given packet in a list. The list locks are
1487 * taken and this function is atomic with respect to other list locked
1490 * A buffer cannot be placed on two lists at the same time.
1492 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1494 unsigned long flags
;
1496 spin_lock_irqsave(&list
->lock
, flags
);
1497 __skb_insert(newsk
, old
->prev
, old
, list
);
1498 spin_unlock_irqrestore(&list
->lock
, flags
);
1503 * Tune the memory allocator for a new MTU size.
1505 void skb_add_mtu(int mtu
)
1507 /* Must match allocation in alloc_skb */
1508 mtu
= SKB_DATA_ALIGN(mtu
) + sizeof(struct skb_shared_info
);
1510 kmem_add_cache_size(mtu
);
1514 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1515 struct sk_buff
* skb1
,
1516 const u32 len
, const int pos
)
1520 memcpy(skb_put(skb1
, pos
- len
), skb
->data
+ len
, pos
- len
);
1522 /* And move data appendix as is. */
1523 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1524 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1526 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1527 skb_shinfo(skb
)->nr_frags
= 0;
1528 skb1
->data_len
= skb
->data_len
;
1529 skb1
->len
+= skb1
->data_len
;
1532 skb
->tail
= skb
->data
+ len
;
1535 static inline void skb_split_no_header(struct sk_buff
*skb
,
1536 struct sk_buff
* skb1
,
1537 const u32 len
, int pos
)
1540 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1542 skb_shinfo(skb
)->nr_frags
= 0;
1543 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1545 skb
->data_len
= len
- pos
;
1547 for (i
= 0; i
< nfrags
; i
++) {
1548 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1550 if (pos
+ size
> len
) {
1551 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1555 * We have two variants in this case:
1556 * 1. Move all the frag to the second
1557 * part, if it is possible. F.e.
1558 * this approach is mandatory for TUX,
1559 * where splitting is expensive.
1560 * 2. Split is accurately. We make this.
1562 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1563 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1564 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1565 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1566 skb_shinfo(skb
)->nr_frags
++;
1570 skb_shinfo(skb
)->nr_frags
++;
1573 skb_shinfo(skb1
)->nr_frags
= k
;
1577 * skb_split - Split fragmented skb to two parts at length len.
1578 * @skb: the buffer to split
1579 * @skb1: the buffer to receive the second part
1580 * @len: new length for skb
1582 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1584 int pos
= skb_headlen(skb
);
1586 if (len
< pos
) /* Split line is inside header. */
1587 skb_split_inside_header(skb
, skb1
, len
, pos
);
1588 else /* Second chunk has no header, nothing to copy. */
1589 skb_split_no_header(skb
, skb1
, len
, pos
);
1593 * skb_prepare_seq_read - Prepare a sequential read of skb data
1594 * @skb: the buffer to read
1595 * @from: lower offset of data to be read
1596 * @to: upper offset of data to be read
1597 * @st: state variable
1599 * Initializes the specified state variable. Must be called before
1600 * invoking skb_seq_read() for the first time.
1602 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1603 unsigned int to
, struct skb_seq_state
*st
)
1605 st
->lower_offset
= from
;
1606 st
->upper_offset
= to
;
1607 st
->root_skb
= st
->cur_skb
= skb
;
1608 st
->frag_idx
= st
->stepped_offset
= 0;
1609 st
->frag_data
= NULL
;
1613 * skb_seq_read - Sequentially read skb data
1614 * @consumed: number of bytes consumed by the caller so far
1615 * @data: destination pointer for data to be returned
1616 * @st: state variable
1618 * Reads a block of skb data at &consumed relative to the
1619 * lower offset specified to skb_prepare_seq_read(). Assigns
1620 * the head of the data block to &data and returns the length
1621 * of the block or 0 if the end of the skb data or the upper
1622 * offset has been reached.
1624 * The caller is not required to consume all of the data
1625 * returned, i.e. &consumed is typically set to the number
1626 * of bytes already consumed and the next call to
1627 * skb_seq_read() will return the remaining part of the block.
1629 * Note: The size of each block of data returned can be arbitary,
1630 * this limitation is the cost for zerocopy seqeuental
1631 * reads of potentially non linear data.
1633 * Note: Fragment lists within fragments are not implemented
1634 * at the moment, state->root_skb could be replaced with
1635 * a stack for this purpose.
1637 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1638 struct skb_seq_state
*st
)
1640 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1643 if (unlikely(abs_offset
>= st
->upper_offset
))
1647 block_limit
= skb_headlen(st
->cur_skb
);
1649 if (abs_offset
< block_limit
) {
1650 *data
= st
->cur_skb
->data
+ abs_offset
;
1651 return block_limit
- abs_offset
;
1654 if (st
->frag_idx
== 0 && !st
->frag_data
)
1655 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1657 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1658 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1659 block_limit
= frag
->size
+ st
->stepped_offset
;
1661 if (abs_offset
< block_limit
) {
1663 st
->frag_data
= kmap_skb_frag(frag
);
1665 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1666 (abs_offset
- st
->stepped_offset
);
1668 return block_limit
- abs_offset
;
1671 if (st
->frag_data
) {
1672 kunmap_skb_frag(st
->frag_data
);
1673 st
->frag_data
= NULL
;
1677 st
->stepped_offset
+= frag
->size
;
1680 if (st
->cur_skb
->next
) {
1681 st
->cur_skb
= st
->cur_skb
->next
;
1684 } else if (st
->root_skb
== st
->cur_skb
&&
1685 skb_shinfo(st
->root_skb
)->frag_list
) {
1686 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1694 * skb_abort_seq_read - Abort a sequential read of skb data
1695 * @st: state variable
1697 * Must be called if skb_seq_read() was not called until it
1700 void skb_abort_seq_read(struct skb_seq_state
*st
)
1703 kunmap_skb_frag(st
->frag_data
);
1706 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1708 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1709 struct ts_config
*conf
,
1710 struct ts_state
*state
)
1712 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
1715 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
1717 skb_abort_seq_read(TS_SKB_CB(state
));
1721 * skb_find_text - Find a text pattern in skb data
1722 * @skb: the buffer to look in
1723 * @from: search offset
1725 * @config: textsearch configuration
1726 * @state: uninitialized textsearch state variable
1728 * Finds a pattern in the skb data according to the specified
1729 * textsearch configuration. Use textsearch_next() to retrieve
1730 * subsequent occurrences of the pattern. Returns the offset
1731 * to the first occurrence or UINT_MAX if no match was found.
1733 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
1734 unsigned int to
, struct ts_config
*config
,
1735 struct ts_state
*state
)
1739 config
->get_next_block
= skb_ts_get_next_block
;
1740 config
->finish
= skb_ts_finish
;
1742 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
1744 ret
= textsearch_find(config
, state
);
1745 return (ret
<= to
- from
? ret
: UINT_MAX
);
1749 * skb_append_datato_frags: - append the user data to a skb
1750 * @sk: sock structure
1751 * @skb: skb structure to be appened with user data.
1752 * @getfrag: call back function to be used for getting the user data
1753 * @from: pointer to user message iov
1754 * @length: length of the iov message
1756 * Description: This procedure append the user data in the fragment part
1757 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1759 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
1760 int (*getfrag
)(void *from
, char *to
, int offset
,
1761 int len
, int odd
, struct sk_buff
*skb
),
1762 void *from
, int length
)
1765 skb_frag_t
*frag
= NULL
;
1766 struct page
*page
= NULL
;
1772 /* Return error if we don't have space for new frag */
1773 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1774 if (frg_cnt
>= MAX_SKB_FRAGS
)
1777 /* allocate a new page for next frag */
1778 page
= alloc_pages(sk
->sk_allocation
, 0);
1780 /* If alloc_page fails just return failure and caller will
1781 * free previous allocated pages by doing kfree_skb()
1786 /* initialize the next frag */
1787 sk
->sk_sndmsg_page
= page
;
1788 sk
->sk_sndmsg_off
= 0;
1789 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
1790 skb
->truesize
+= PAGE_SIZE
;
1791 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
1793 /* get the new initialized frag */
1794 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
1795 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
1797 /* copy the user data to page */
1798 left
= PAGE_SIZE
- frag
->page_offset
;
1799 copy
= (length
> left
)? left
: length
;
1801 ret
= getfrag(from
, (page_address(frag
->page
) +
1802 frag
->page_offset
+ frag
->size
),
1803 offset
, copy
, 0, skb
);
1807 /* copy was successful so update the size parameters */
1808 sk
->sk_sndmsg_off
+= copy
;
1811 skb
->data_len
+= copy
;
1815 } while (length
> 0);
1821 * skb_pull_rcsum - pull skb and update receive checksum
1822 * @skb: buffer to update
1823 * @start: start of data before pull
1824 * @len: length of data pulled
1826 * This function performs an skb_pull on the packet and updates
1827 * update the CHECKSUM_COMPLETE checksum. It should be used on
1828 * receive path processing instead of skb_pull unless you know
1829 * that the checksum difference is zero (e.g., a valid IP header)
1830 * or you are setting ip_summed to CHECKSUM_NONE.
1832 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
1834 BUG_ON(len
> skb
->len
);
1836 BUG_ON(skb
->len
< skb
->data_len
);
1837 skb_postpull_rcsum(skb
, skb
->data
, len
);
1838 return skb
->data
+= len
;
1841 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
1844 * skb_segment - Perform protocol segmentation on skb.
1845 * @skb: buffer to segment
1846 * @features: features for the output path (see dev->features)
1848 * This function performs segmentation on the given skb. It returns
1849 * the segment at the given position. It returns NULL if there are
1850 * no more segments to generate, or when an error is encountered.
1852 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
1854 struct sk_buff
*segs
= NULL
;
1855 struct sk_buff
*tail
= NULL
;
1856 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
1857 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
1858 unsigned int offset
= doffset
;
1859 unsigned int headroom
;
1861 int sg
= features
& NETIF_F_SG
;
1862 int nfrags
= skb_shinfo(skb
)->nr_frags
;
1867 __skb_push(skb
, doffset
);
1868 headroom
= skb_headroom(skb
);
1869 pos
= skb_headlen(skb
);
1872 struct sk_buff
*nskb
;
1878 len
= skb
->len
- offset
;
1882 hsize
= skb_headlen(skb
) - offset
;
1885 if (hsize
> len
|| !sg
)
1888 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
1889 if (unlikely(!nskb
))
1898 nskb
->dev
= skb
->dev
;
1899 nskb
->priority
= skb
->priority
;
1900 nskb
->protocol
= skb
->protocol
;
1901 nskb
->dst
= dst_clone(skb
->dst
);
1902 memcpy(nskb
->cb
, skb
->cb
, sizeof(skb
->cb
));
1903 nskb
->pkt_type
= skb
->pkt_type
;
1904 nskb
->mac_len
= skb
->mac_len
;
1906 skb_reserve(nskb
, headroom
);
1907 skb_reset_mac_header(nskb
);
1908 nskb
->nh
.raw
= nskb
->data
+ skb
->mac_len
;
1909 nskb
->h
.raw
= nskb
->nh
.raw
+ (skb
->h
.raw
- skb
->nh
.raw
);
1910 memcpy(skb_put(nskb
, doffset
), skb
->data
, doffset
);
1913 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
1919 frag
= skb_shinfo(nskb
)->frags
;
1922 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
1923 nskb
->csum
= skb
->csum
;
1924 memcpy(skb_put(nskb
, hsize
), skb
->data
+ offset
, hsize
);
1926 while (pos
< offset
+ len
) {
1927 BUG_ON(i
>= nfrags
);
1929 *frag
= skb_shinfo(skb
)->frags
[i
];
1930 get_page(frag
->page
);
1934 frag
->page_offset
+= offset
- pos
;
1935 frag
->size
-= offset
- pos
;
1940 if (pos
+ size
<= offset
+ len
) {
1944 frag
->size
-= pos
+ size
- (offset
+ len
);
1951 skb_shinfo(nskb
)->nr_frags
= k
;
1952 nskb
->data_len
= len
- hsize
;
1953 nskb
->len
+= nskb
->data_len
;
1954 nskb
->truesize
+= nskb
->data_len
;
1955 } while ((offset
+= len
) < skb
->len
);
1960 while ((skb
= segs
)) {
1964 return ERR_PTR(err
);
1967 EXPORT_SYMBOL_GPL(skb_segment
);
1969 void __init
skb_init(void)
1971 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
1972 sizeof(struct sk_buff
),
1974 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1976 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
1977 (2*sizeof(struct sk_buff
)) +
1980 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
1984 EXPORT_SYMBOL(___pskb_trim
);
1985 EXPORT_SYMBOL(__kfree_skb
);
1986 EXPORT_SYMBOL(kfree_skb
);
1987 EXPORT_SYMBOL(__pskb_pull_tail
);
1988 EXPORT_SYMBOL(__alloc_skb
);
1989 EXPORT_SYMBOL(__netdev_alloc_skb
);
1990 EXPORT_SYMBOL(pskb_copy
);
1991 EXPORT_SYMBOL(pskb_expand_head
);
1992 EXPORT_SYMBOL(skb_checksum
);
1993 EXPORT_SYMBOL(skb_clone
);
1994 EXPORT_SYMBOL(skb_clone_fraglist
);
1995 EXPORT_SYMBOL(skb_copy
);
1996 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
1997 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
1998 EXPORT_SYMBOL(skb_copy_bits
);
1999 EXPORT_SYMBOL(skb_copy_expand
);
2000 EXPORT_SYMBOL(skb_over_panic
);
2001 EXPORT_SYMBOL(skb_pad
);
2002 EXPORT_SYMBOL(skb_realloc_headroom
);
2003 EXPORT_SYMBOL(skb_under_panic
);
2004 EXPORT_SYMBOL(skb_dequeue
);
2005 EXPORT_SYMBOL(skb_dequeue_tail
);
2006 EXPORT_SYMBOL(skb_insert
);
2007 EXPORT_SYMBOL(skb_queue_purge
);
2008 EXPORT_SYMBOL(skb_queue_head
);
2009 EXPORT_SYMBOL(skb_queue_tail
);
2010 EXPORT_SYMBOL(skb_unlink
);
2011 EXPORT_SYMBOL(skb_append
);
2012 EXPORT_SYMBOL(skb_split
);
2013 EXPORT_SYMBOL(skb_prepare_seq_read
);
2014 EXPORT_SYMBOL(skb_seq_read
);
2015 EXPORT_SYMBOL(skb_abort_seq_read
);
2016 EXPORT_SYMBOL(skb_find_text
);
2017 EXPORT_SYMBOL(skb_append_datato_frags
);