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/splice.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/scatterlist.h>
61 #include <net/protocol.h>
64 #include <net/checksum.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
72 static struct kmem_cache
*skbuff_head_cache __read_mostly
;
73 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
75 static void sock_pipe_buf_release(struct pipe_inode_info
*pipe
,
76 struct pipe_buffer
*buf
)
78 struct sk_buff
*skb
= (struct sk_buff
*) buf
->private;
83 static void sock_pipe_buf_get(struct pipe_inode_info
*pipe
,
84 struct pipe_buffer
*buf
)
86 struct sk_buff
*skb
= (struct sk_buff
*) buf
->private;
91 static int sock_pipe_buf_steal(struct pipe_inode_info
*pipe
,
92 struct pipe_buffer
*buf
)
98 /* Pipe buffer operations for a socket. */
99 static struct pipe_buf_operations sock_pipe_buf_ops
= {
101 .map
= generic_pipe_buf_map
,
102 .unmap
= generic_pipe_buf_unmap
,
103 .confirm
= generic_pipe_buf_confirm
,
104 .release
= sock_pipe_buf_release
,
105 .steal
= sock_pipe_buf_steal
,
106 .get
= sock_pipe_buf_get
,
110 * Keep out-of-line to prevent kernel bloat.
111 * __builtin_return_address is not used because it is not always
116 * skb_over_panic - private function
121 * Out of line support code for skb_put(). Not user callable.
123 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
125 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
126 "data:%p tail:%#lx end:%#lx dev:%s\n",
127 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
128 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
129 skb
->dev
? skb
->dev
->name
: "<NULL>");
134 * skb_under_panic - private function
139 * Out of line support code for skb_push(). Not user callable.
142 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
144 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
145 "data:%p tail:%#lx end:%#lx dev:%s\n",
146 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
147 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
148 skb
->dev
? skb
->dev
->name
: "<NULL>");
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
159 * __alloc_skb - allocate a network buffer
160 * @size: size to allocate
161 * @gfp_mask: allocation mask
162 * @fclone: allocate from fclone cache instead of head cache
163 * and allocate a cloned (child) skb
164 * @node: numa node to allocate memory on
166 * Allocate a new &sk_buff. The returned buffer has no headroom and a
167 * tail room of size bytes. The object has a reference count of one.
168 * The return is the buffer. On a failure the return is %NULL.
170 * Buffers may only be allocated from interrupts using a @gfp_mask of
173 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
174 int fclone
, int node
)
176 struct kmem_cache
*cache
;
177 struct skb_shared_info
*shinfo
;
181 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
184 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
189 size
= SKB_DATA_ALIGN(size
);
190 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
194 prefetchw(data
+ size
);
197 * See comment in sk_buff definition, just before the 'tail' member
199 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
200 skb
->truesize
= size
+ sizeof(struct sk_buff
);
201 atomic_set(&skb
->users
, 1);
204 skb_reset_tail_pointer(skb
);
205 skb
->end
= skb
->tail
+ size
;
206 #ifdef NET_SKBUFF_DATA_USES_OFFSET
207 skb
->mac_header
= ~0U;
209 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
210 skb
->nfct_reasm
= NULL
;
214 #ifdef CONFIG_BRIDGE_NETFILTER
215 skb
->nf_bridge
= NULL
;
217 #ifdef CONFIG_NET_SCHED
219 #ifdef CONFIG_NET_CLS_ACT
223 #ifdef CONFIG_NET_DMA
224 memset(&skb
->dma_cookie
, 0, sizeof(dma_cookie_t
));
226 #ifdef CONFIG_NETWORK_SECMARK
230 /* make sure we initialize shinfo sequentially */
231 shinfo
= skb_shinfo(skb
);
232 memset(shinfo
, 0, offsetof(struct skb_shared_info
, frags
));
233 atomic_set(&shinfo
->dataref
, 1);
235 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
241 struct sk_buff
*child
= skb
+ 1;
242 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
244 skb
->fclone
= SKB_FCLONE_ORIG
;
245 atomic_set(fclone_ref
, 1);
247 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
252 kmem_cache_free(cache
, skb
);
258 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
259 * @dev: network device to receive on
260 * @length: length to allocate
261 * @gfp_mask: get_free_pages mask, passed to alloc_skb
263 * Allocate a new &sk_buff and assign it a usage count of one. The
264 * buffer has unspecified headroom built in. Users should allocate
265 * the headroom they think they need without accounting for the
266 * built in space. The built in space is used for optimisations.
268 * %NULL is returned if there is no free memory.
270 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
271 unsigned int length
, gfp_t gfp_mask
)
273 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
276 skb
= __alloc_skb(length
+ NET_SKB_PAD_ALLOC
, gfp_mask
, 0, node
);
278 skb_reserve(skb
, NET_SKB_PAD_ALLOC
);
284 static void skb_drop_list(struct sk_buff
**listp
)
286 struct sk_buff
*list
= *listp
;
291 struct sk_buff
*this = list
;
297 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
299 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
302 static void skb_clone_fraglist(struct sk_buff
*skb
)
304 struct sk_buff
*list
;
306 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
310 static void skb_release_data(struct sk_buff
*skb
)
313 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
314 &skb_shinfo(skb
)->dataref
)) {
315 if (skb_shinfo(skb
)->nr_frags
) {
317 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
318 put_page(skb_shinfo(skb
)->frags
[i
].page
);
321 if (skb_shinfo(skb
)->frag_list
)
322 skb_drop_fraglist(skb
);
329 * Free an skbuff by memory without cleaning the state.
331 void kfree_skbmem(struct sk_buff
*skb
)
333 struct sk_buff
*other
;
334 atomic_t
*fclone_ref
;
336 skb_release_data(skb
);
337 switch (skb
->fclone
) {
338 case SKB_FCLONE_UNAVAILABLE
:
339 kmem_cache_free(skbuff_head_cache
, skb
);
342 case SKB_FCLONE_ORIG
:
343 fclone_ref
= (atomic_t
*) (skb
+ 2);
344 if (atomic_dec_and_test(fclone_ref
))
345 kmem_cache_free(skbuff_fclone_cache
, skb
);
348 case SKB_FCLONE_CLONE
:
349 fclone_ref
= (atomic_t
*) (skb
+ 1);
352 /* The clone portion is available for
353 * fast-cloning again.
355 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
357 if (atomic_dec_and_test(fclone_ref
))
358 kmem_cache_free(skbuff_fclone_cache
, other
);
364 * __kfree_skb - private function
367 * Free an sk_buff. Release anything attached to the buffer.
368 * Clean the state. This is an internal helper function. Users should
369 * always call kfree_skb
372 void __kfree_skb(struct sk_buff
*skb
)
374 dst_release(skb
->dst
);
376 secpath_put(skb
->sp
);
378 if (skb
->destructor
) {
380 skb
->destructor(skb
);
382 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
383 nf_conntrack_put(skb
->nfct
);
384 nf_conntrack_put_reasm(skb
->nfct_reasm
);
386 #ifdef CONFIG_BRIDGE_NETFILTER
387 nf_bridge_put(skb
->nf_bridge
);
389 /* XXX: IS this still necessary? - JHS */
390 #ifdef CONFIG_NET_SCHED
392 #ifdef CONFIG_NET_CLS_ACT
401 * kfree_skb - free an sk_buff
402 * @skb: buffer to free
404 * Drop a reference to the buffer and free it if the usage count has
407 void kfree_skb(struct sk_buff
*skb
)
411 if (likely(atomic_read(&skb
->users
) == 1))
413 else if (likely(!atomic_dec_and_test(&skb
->users
)))
418 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
420 new->tstamp
= old
->tstamp
;
422 new->transport_header
= old
->transport_header
;
423 new->network_header
= old
->network_header
;
424 new->mac_header
= old
->mac_header
;
425 new->dst
= dst_clone(old
->dst
);
427 new->sp
= secpath_get(old
->sp
);
429 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
430 new->csum
= old
->csum
;
431 new->local_df
= old
->local_df
;
432 new->pkt_type
= old
->pkt_type
;
433 new->ip_summed
= old
->ip_summed
;
434 new->priority
= old
->priority
;
435 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
436 new->ipvs_property
= old
->ipvs_property
;
438 new->protocol
= old
->protocol
;
439 new->mark
= old
->mark
;
441 #ifdef CONFIG_NET_SCHED
442 new->tc_index
= old
->tc_index
;
443 #ifdef CONFIG_NET_CLS_ACT
444 new->tc_verd
= old
->tc_verd
;
447 skb_copy_secmark(new, old
);
448 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
449 new->nfcache
= old
->nfcache
;
454 * skb_clone - duplicate an sk_buff
455 * @skb: buffer to clone
456 * @gfp_mask: allocation priority
458 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
459 * copies share the same packet data but not structure. The new
460 * buffer has a reference count of 1. If the allocation fails the
461 * function returns %NULL otherwise the new buffer is returned.
463 * If this function is called from an interrupt gfp_mask() must be
467 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
472 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
473 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
474 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
475 n
->fclone
= SKB_FCLONE_CLONE
;
476 atomic_inc(fclone_ref
);
478 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
481 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
484 #define C(x) n->x = skb->x
486 n
->next
= n
->prev
= NULL
;
488 __copy_skb_header(n
, skb
);
493 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
496 n
->destructor
= NULL
;
503 atomic_set(&n
->users
, 1);
505 atomic_inc(&(skb_shinfo(skb
)->dataref
));
511 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
513 #ifndef NET_SKBUFF_DATA_USES_OFFSET
515 * Shift between the two data areas in bytes
517 unsigned long offset
= new->data
- old
->data
;
520 __copy_skb_header(new, old
);
522 #ifndef NET_SKBUFF_DATA_USES_OFFSET
523 /* {transport,network,mac}_header are relative to skb->head */
524 new->transport_header
+= offset
;
525 new->network_header
+= offset
;
526 if (skb_mac_header_was_set(new))
527 new->mac_header
+= offset
;
529 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
530 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
531 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
535 * skb_copy - create private copy of an sk_buff
536 * @skb: buffer to copy
537 * @gfp_mask: allocation priority
539 * Make a copy of both an &sk_buff and its data. This is used when the
540 * caller wishes to modify the data and needs a private copy of the
541 * data to alter. Returns %NULL on failure or the pointer to the buffer
542 * on success. The returned buffer has a reference count of 1.
544 * As by-product this function converts non-linear &sk_buff to linear
545 * one, so that &sk_buff becomes completely private and caller is allowed
546 * to modify all the data of returned buffer. This means that this
547 * function is not recommended for use in circumstances when only
548 * header is going to be modified. Use pskb_copy() instead.
551 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
553 int headerlen
= skb
->data
- skb
->head
;
555 * Allocate the copy buffer
558 #ifdef NET_SKBUFF_DATA_USES_OFFSET
559 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
561 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
566 /* Set the data pointer */
567 skb_reserve(n
, headerlen
);
568 /* Set the tail pointer and length */
569 skb_put(n
, skb
->len
);
571 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
574 copy_skb_header(n
, skb
);
580 * pskb_copy - create copy of an sk_buff with private head.
581 * @skb: buffer to copy
582 * @gfp_mask: allocation priority
584 * Make a copy of both an &sk_buff and part of its data, located
585 * in header. Fragmented data remain shared. This is used when
586 * the caller wishes to modify only header of &sk_buff and needs
587 * private copy of the header to alter. Returns %NULL on failure
588 * or the pointer to the buffer on success.
589 * The returned buffer has a reference count of 1.
592 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
595 * Allocate the copy buffer
598 #ifdef NET_SKBUFF_DATA_USES_OFFSET
599 n
= alloc_skb(skb
->end
, gfp_mask
);
601 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
606 /* Set the data pointer */
607 skb_reserve(n
, skb
->data
- skb
->head
);
608 /* Set the tail pointer and length */
609 skb_put(n
, skb_headlen(skb
));
611 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
613 n
->truesize
+= skb
->data_len
;
614 n
->data_len
= skb
->data_len
;
617 if (skb_shinfo(skb
)->nr_frags
) {
620 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
621 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
622 get_page(skb_shinfo(n
)->frags
[i
].page
);
624 skb_shinfo(n
)->nr_frags
= i
;
627 if (skb_shinfo(skb
)->frag_list
) {
628 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
629 skb_clone_fraglist(n
);
632 copy_skb_header(n
, skb
);
638 * pskb_expand_head - reallocate header of &sk_buff
639 * @skb: buffer to reallocate
640 * @nhead: room to add at head
641 * @ntail: room to add at tail
642 * @gfp_mask: allocation priority
644 * Expands (or creates identical copy, if &nhead and &ntail are zero)
645 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
646 * reference count of 1. Returns zero in the case of success or error,
647 * if expansion failed. In the last case, &sk_buff is not changed.
649 * All the pointers pointing into skb header may change and must be
650 * reloaded after call to this function.
653 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
658 #ifdef NET_SKBUFF_DATA_USES_OFFSET
659 int size
= nhead
+ skb
->end
+ ntail
;
661 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
670 size
= SKB_DATA_ALIGN(size
);
672 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
676 /* Copy only real data... and, alas, header. This should be
677 * optimized for the cases when header is void. */
678 #ifdef NET_SKBUFF_DATA_USES_OFFSET
679 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
681 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
683 memcpy(data
+ size
, skb_end_pointer(skb
),
684 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
686 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
687 get_page(skb_shinfo(skb
)->frags
[i
].page
);
689 if (skb_shinfo(skb
)->frag_list
)
690 skb_clone_fraglist(skb
);
692 skb_release_data(skb
);
694 off
= (data
+ nhead
) - skb
->head
;
698 #ifdef NET_SKBUFF_DATA_USES_OFFSET
702 skb
->end
= skb
->head
+ size
;
704 /* {transport,network,mac}_header and tail are relative to skb->head */
706 skb
->transport_header
+= off
;
707 skb
->network_header
+= off
;
708 if (skb_mac_header_was_set(skb
))
709 skb
->mac_header
+= off
;
710 /* Only adjust this if it actually is csum_start rather than csum */
711 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
712 skb
->csum_start
+= nhead
;
716 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
723 /* Make private copy of skb with writable head and some headroom */
725 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
727 struct sk_buff
*skb2
;
728 int delta
= headroom
- skb_headroom(skb
);
731 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
733 skb2
= skb_clone(skb
, GFP_ATOMIC
);
734 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
745 * skb_copy_expand - copy and expand sk_buff
746 * @skb: buffer to copy
747 * @newheadroom: new free bytes at head
748 * @newtailroom: new free bytes at tail
749 * @gfp_mask: allocation priority
751 * Make a copy of both an &sk_buff and its data and while doing so
752 * allocate additional space.
754 * This is used when the caller wishes to modify the data and needs a
755 * private copy of the data to alter as well as more space for new fields.
756 * Returns %NULL on failure or the pointer to the buffer
757 * on success. The returned buffer has a reference count of 1.
759 * You must pass %GFP_ATOMIC as the allocation priority if this function
760 * is called from an interrupt.
762 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
763 int newheadroom
, int newtailroom
,
767 * Allocate the copy buffer
769 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
771 int oldheadroom
= skb_headroom(skb
);
772 int head_copy_len
, head_copy_off
;
778 skb_reserve(n
, newheadroom
);
780 /* Set the tail pointer and length */
781 skb_put(n
, skb
->len
);
783 head_copy_len
= oldheadroom
;
785 if (newheadroom
<= head_copy_len
)
786 head_copy_len
= newheadroom
;
788 head_copy_off
= newheadroom
- head_copy_len
;
790 /* Copy the linear header and data. */
791 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
792 skb
->len
+ head_copy_len
))
795 copy_skb_header(n
, skb
);
797 off
= newheadroom
- oldheadroom
;
798 if (n
->ip_summed
== CHECKSUM_PARTIAL
)
799 n
->csum_start
+= off
;
800 #ifdef NET_SKBUFF_DATA_USES_OFFSET
801 n
->transport_header
+= off
;
802 n
->network_header
+= off
;
803 if (skb_mac_header_was_set(skb
))
804 n
->mac_header
+= off
;
811 * skb_pad - zero pad the tail of an skb
812 * @skb: buffer to pad
815 * Ensure that a buffer is followed by a padding area that is zero
816 * filled. Used by network drivers which may DMA or transfer data
817 * beyond the buffer end onto the wire.
819 * May return error in out of memory cases. The skb is freed on error.
822 int skb_pad(struct sk_buff
*skb
, int pad
)
827 /* If the skbuff is non linear tailroom is always zero.. */
828 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
829 memset(skb
->data
+skb
->len
, 0, pad
);
833 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
834 if (likely(skb_cloned(skb
) || ntail
> 0)) {
835 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
840 /* FIXME: The use of this function with non-linear skb's really needs
843 err
= skb_linearize(skb
);
847 memset(skb
->data
+ skb
->len
, 0, pad
);
855 /* Trims skb to length len. It can change skb pointers.
858 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
860 struct sk_buff
**fragp
;
861 struct sk_buff
*frag
;
862 int offset
= skb_headlen(skb
);
863 int nfrags
= skb_shinfo(skb
)->nr_frags
;
867 if (skb_cloned(skb
) &&
868 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
875 for (; i
< nfrags
; i
++) {
876 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
883 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
886 skb_shinfo(skb
)->nr_frags
= i
;
888 for (; i
< nfrags
; i
++)
889 put_page(skb_shinfo(skb
)->frags
[i
].page
);
891 if (skb_shinfo(skb
)->frag_list
)
892 skb_drop_fraglist(skb
);
896 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
897 fragp
= &frag
->next
) {
898 int end
= offset
+ frag
->len
;
900 if (skb_shared(frag
)) {
901 struct sk_buff
*nfrag
;
903 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
904 if (unlikely(!nfrag
))
907 nfrag
->next
= frag
->next
;
919 unlikely((err
= pskb_trim(frag
, len
- offset
))))
923 skb_drop_list(&frag
->next
);
928 if (len
> skb_headlen(skb
)) {
929 skb
->data_len
-= skb
->len
- len
;
934 skb_set_tail_pointer(skb
, len
);
941 * __pskb_pull_tail - advance tail of skb header
942 * @skb: buffer to reallocate
943 * @delta: number of bytes to advance tail
945 * The function makes a sense only on a fragmented &sk_buff,
946 * it expands header moving its tail forward and copying necessary
947 * data from fragmented part.
949 * &sk_buff MUST have reference count of 1.
951 * Returns %NULL (and &sk_buff does not change) if pull failed
952 * or value of new tail of skb in the case of success.
954 * All the pointers pointing into skb header may change and must be
955 * reloaded after call to this function.
958 /* Moves tail of skb head forward, copying data from fragmented part,
959 * when it is necessary.
960 * 1. It may fail due to malloc failure.
961 * 2. It may change skb pointers.
963 * It is pretty complicated. Luckily, it is called only in exceptional cases.
965 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
967 /* If skb has not enough free space at tail, get new one
968 * plus 128 bytes for future expansions. If we have enough
969 * room at tail, reallocate without expansion only if skb is cloned.
971 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
973 if (eat
> 0 || skb_cloned(skb
)) {
974 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
979 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
982 /* Optimization: no fragments, no reasons to preestimate
983 * size of pulled pages. Superb.
985 if (!skb_shinfo(skb
)->frag_list
)
988 /* Estimate size of pulled pages. */
990 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
991 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
993 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
996 /* If we need update frag list, we are in troubles.
997 * Certainly, it possible to add an offset to skb data,
998 * but taking into account that pulling is expected to
999 * be very rare operation, it is worth to fight against
1000 * further bloating skb head and crucify ourselves here instead.
1001 * Pure masohism, indeed. 8)8)
1004 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1005 struct sk_buff
*clone
= NULL
;
1006 struct sk_buff
*insp
= NULL
;
1011 if (list
->len
<= eat
) {
1012 /* Eaten as whole. */
1017 /* Eaten partially. */
1019 if (skb_shared(list
)) {
1020 /* Sucks! We need to fork list. :-( */
1021 clone
= skb_clone(list
, GFP_ATOMIC
);
1027 /* This may be pulled without
1031 if (!pskb_pull(list
, eat
)) {
1040 /* Free pulled out fragments. */
1041 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1042 skb_shinfo(skb
)->frag_list
= list
->next
;
1045 /* And insert new clone at head. */
1048 skb_shinfo(skb
)->frag_list
= clone
;
1051 /* Success! Now we may commit changes to skb data. */
1056 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1057 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1058 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1059 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1061 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1063 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1064 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1070 skb_shinfo(skb
)->nr_frags
= k
;
1073 skb
->data_len
-= delta
;
1075 return skb_tail_pointer(skb
);
1078 /* Copy some data bits from skb to kernel buffer. */
1080 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1083 int start
= skb_headlen(skb
);
1085 if (offset
> (int)skb
->len
- len
)
1089 if ((copy
= start
- offset
) > 0) {
1092 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1093 if ((len
-= copy
) == 0)
1099 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1102 BUG_TRAP(start
<= offset
+ len
);
1104 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1105 if ((copy
= end
- offset
) > 0) {
1111 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1113 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1114 offset
- start
, copy
);
1115 kunmap_skb_frag(vaddr
);
1117 if ((len
-= copy
) == 0)
1125 if (skb_shinfo(skb
)->frag_list
) {
1126 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1128 for (; list
; list
= list
->next
) {
1131 BUG_TRAP(start
<= offset
+ len
);
1133 end
= start
+ list
->len
;
1134 if ((copy
= end
- offset
) > 0) {
1137 if (skb_copy_bits(list
, offset
- start
,
1140 if ((len
-= copy
) == 0)
1156 * Callback from splice_to_pipe(), if we need to release some pages
1157 * at the end of the spd in case we error'ed out in filling the pipe.
1159 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1161 struct sk_buff
*skb
= (struct sk_buff
*) spd
->partial
[i
].private;
1167 * Fill page/offset/length into spd, if it can hold more pages.
1169 static inline int spd_fill_page(struct splice_pipe_desc
*spd
, struct page
*page
,
1170 unsigned int len
, unsigned int offset
,
1171 struct sk_buff
*skb
)
1173 if (unlikely(spd
->nr_pages
== PIPE_BUFFERS
))
1176 spd
->pages
[spd
->nr_pages
] = page
;
1177 spd
->partial
[spd
->nr_pages
].len
= len
;
1178 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1179 spd
->partial
[spd
->nr_pages
].private = (unsigned long) skb_get(skb
);
1185 * Map linear and fragment data from the skb to spd. Returns number of
1188 static int __skb_splice_bits(struct sk_buff
*skb
, unsigned int *offset
,
1189 unsigned int *total_len
,
1190 struct splice_pipe_desc
*spd
)
1192 unsigned int nr_pages
= spd
->nr_pages
;
1193 unsigned int poff
, plen
, len
, toff
, tlen
;
1194 int headlen
, seg
, error
= 0;
1204 * if the offset is greater than the linear part, go directly to
1207 headlen
= skb_headlen(skb
);
1208 if (toff
>= headlen
) {
1214 * first map the linear region into the pages/partial map, skipping
1215 * any potential initial offset.
1218 while (len
< headlen
) {
1219 void *p
= skb
->data
+ len
;
1221 poff
= (unsigned long) p
& (PAGE_SIZE
- 1);
1222 plen
= min_t(unsigned int, headlen
- len
, PAGE_SIZE
- poff
);
1235 plen
= min(plen
, tlen
);
1240 * just jump directly to update and return, no point
1241 * in going over fragments when the output is full.
1243 error
= spd_fill_page(spd
, virt_to_page(p
), plen
, poff
, skb
);
1251 * then map the fragments
1254 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1255 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1258 poff
= f
->page_offset
;
1270 plen
= min(plen
, tlen
);
1274 error
= spd_fill_page(spd
, f
->page
, plen
, poff
, skb
);
1282 if (spd
->nr_pages
- nr_pages
) {
1288 /* update the offset to reflect the linear part skip, if any */
1295 * Map data from the skb to a pipe. Should handle both the linear part,
1296 * the fragments, and the frag list. It does NOT handle frag lists within
1297 * the frag list, if such a thing exists. We'd probably need to recurse to
1298 * handle that cleanly.
1300 int skb_splice_bits(struct sk_buff
*__skb
, unsigned int offset
,
1301 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1304 struct partial_page partial
[PIPE_BUFFERS
];
1305 struct page
*pages
[PIPE_BUFFERS
];
1306 struct splice_pipe_desc spd
= {
1310 .ops
= &sock_pipe_buf_ops
,
1311 .spd_release
= sock_spd_release
,
1313 struct sk_buff
*skb
;
1316 * I'd love to avoid the clone here, but tcp_read_sock()
1317 * ignores reference counts and unconditonally kills the sk_buff
1318 * on return from the actor.
1320 skb
= skb_clone(__skb
, GFP_KERNEL
);
1325 * __skb_splice_bits() only fails if the output has no room left,
1326 * so no point in going over the frag_list for the error case.
1328 if (__skb_splice_bits(skb
, &offset
, &tlen
, &spd
))
1334 * now see if we have a frag_list to map
1336 if (skb_shinfo(skb
)->frag_list
) {
1337 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1339 for (; list
&& tlen
; list
= list
->next
) {
1340 if (__skb_splice_bits(list
, &offset
, &tlen
, &spd
))
1347 * drop our reference to the clone, the pipe consumption will
1354 struct sock
*sk
= __skb
->sk
;
1357 * Drop the socket lock, otherwise we have reverse
1358 * locking dependencies between sk_lock and i_mutex
1359 * here as compared to sendfile(). We enter here
1360 * with the socket lock held, and splice_to_pipe() will
1361 * grab the pipe inode lock. For sendfile() emulation,
1362 * we call into ->sendpage() with the i_mutex lock held
1363 * and networking will grab the socket lock.
1366 ret
= splice_to_pipe(pipe
, &spd
);
1375 * skb_store_bits - store bits from kernel buffer to skb
1376 * @skb: destination buffer
1377 * @offset: offset in destination
1378 * @from: source buffer
1379 * @len: number of bytes to copy
1381 * Copy the specified number of bytes from the source buffer to the
1382 * destination skb. This function handles all the messy bits of
1383 * traversing fragment lists and such.
1386 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1389 int start
= skb_headlen(skb
);
1391 if (offset
> (int)skb
->len
- len
)
1394 if ((copy
= start
- offset
) > 0) {
1397 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1398 if ((len
-= copy
) == 0)
1404 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1405 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1408 BUG_TRAP(start
<= offset
+ len
);
1410 end
= start
+ frag
->size
;
1411 if ((copy
= end
- offset
) > 0) {
1417 vaddr
= kmap_skb_frag(frag
);
1418 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1420 kunmap_skb_frag(vaddr
);
1422 if ((len
-= copy
) == 0)
1430 if (skb_shinfo(skb
)->frag_list
) {
1431 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1433 for (; list
; list
= list
->next
) {
1436 BUG_TRAP(start
<= offset
+ len
);
1438 end
= start
+ list
->len
;
1439 if ((copy
= end
- offset
) > 0) {
1442 if (skb_store_bits(list
, offset
- start
,
1445 if ((len
-= copy
) == 0)
1460 EXPORT_SYMBOL(skb_store_bits
);
1462 /* Checksum skb data. */
1464 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1465 int len
, __wsum csum
)
1467 int start
= skb_headlen(skb
);
1468 int i
, copy
= start
- offset
;
1471 /* Checksum header. */
1475 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1476 if ((len
-= copy
) == 0)
1482 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1485 BUG_TRAP(start
<= offset
+ len
);
1487 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1488 if ((copy
= end
- offset
) > 0) {
1491 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1495 vaddr
= kmap_skb_frag(frag
);
1496 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1497 offset
- start
, copy
, 0);
1498 kunmap_skb_frag(vaddr
);
1499 csum
= csum_block_add(csum
, csum2
, pos
);
1508 if (skb_shinfo(skb
)->frag_list
) {
1509 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1511 for (; list
; list
= list
->next
) {
1514 BUG_TRAP(start
<= offset
+ len
);
1516 end
= start
+ list
->len
;
1517 if ((copy
= end
- offset
) > 0) {
1521 csum2
= skb_checksum(list
, offset
- start
,
1523 csum
= csum_block_add(csum
, csum2
, pos
);
1524 if ((len
-= copy
) == 0)
1537 /* Both of above in one bottle. */
1539 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1540 u8
*to
, int len
, __wsum csum
)
1542 int start
= skb_headlen(skb
);
1543 int i
, copy
= start
- offset
;
1550 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1552 if ((len
-= copy
) == 0)
1559 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1562 BUG_TRAP(start
<= offset
+ len
);
1564 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1565 if ((copy
= end
- offset
) > 0) {
1568 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1572 vaddr
= kmap_skb_frag(frag
);
1573 csum2
= csum_partial_copy_nocheck(vaddr
+
1577 kunmap_skb_frag(vaddr
);
1578 csum
= csum_block_add(csum
, csum2
, pos
);
1588 if (skb_shinfo(skb
)->frag_list
) {
1589 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1591 for (; list
; list
= list
->next
) {
1595 BUG_TRAP(start
<= offset
+ len
);
1597 end
= start
+ list
->len
;
1598 if ((copy
= end
- offset
) > 0) {
1601 csum2
= skb_copy_and_csum_bits(list
,
1604 csum
= csum_block_add(csum
, csum2
, pos
);
1605 if ((len
-= copy
) == 0)
1618 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1623 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1624 csstart
= skb
->csum_start
- skb_headroom(skb
);
1626 csstart
= skb_headlen(skb
);
1628 BUG_ON(csstart
> skb_headlen(skb
));
1630 skb_copy_from_linear_data(skb
, to
, csstart
);
1633 if (csstart
!= skb
->len
)
1634 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1635 skb
->len
- csstart
, 0);
1637 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1638 long csstuff
= csstart
+ skb
->csum_offset
;
1640 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1645 * skb_dequeue - remove from the head of the queue
1646 * @list: list to dequeue from
1648 * Remove the head of the list. The list lock is taken so the function
1649 * may be used safely with other locking list functions. The head item is
1650 * returned or %NULL if the list is empty.
1653 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1655 unsigned long flags
;
1656 struct sk_buff
*result
;
1658 spin_lock_irqsave(&list
->lock
, flags
);
1659 result
= __skb_dequeue(list
);
1660 spin_unlock_irqrestore(&list
->lock
, flags
);
1665 * skb_dequeue_tail - remove from the tail of the queue
1666 * @list: list to dequeue from
1668 * Remove the tail of the list. The list lock is taken so the function
1669 * may be used safely with other locking list functions. The tail item is
1670 * returned or %NULL if the list is empty.
1672 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1674 unsigned long flags
;
1675 struct sk_buff
*result
;
1677 spin_lock_irqsave(&list
->lock
, flags
);
1678 result
= __skb_dequeue_tail(list
);
1679 spin_unlock_irqrestore(&list
->lock
, flags
);
1684 * skb_queue_purge - empty a list
1685 * @list: list to empty
1687 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1688 * the list and one reference dropped. This function takes the list
1689 * lock and is atomic with respect to other list locking functions.
1691 void skb_queue_purge(struct sk_buff_head
*list
)
1693 struct sk_buff
*skb
;
1694 while ((skb
= skb_dequeue(list
)) != NULL
)
1699 * skb_queue_head - queue a buffer at the list head
1700 * @list: list to use
1701 * @newsk: buffer to queue
1703 * Queue a buffer at the start of the list. This function takes the
1704 * list lock and can be used safely with other locking &sk_buff functions
1707 * A buffer cannot be placed on two lists at the same time.
1709 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1711 unsigned long flags
;
1713 spin_lock_irqsave(&list
->lock
, flags
);
1714 __skb_queue_head(list
, newsk
);
1715 spin_unlock_irqrestore(&list
->lock
, flags
);
1719 * skb_queue_tail - queue a buffer at the list tail
1720 * @list: list to use
1721 * @newsk: buffer to queue
1723 * Queue a buffer at the tail of the list. This function takes the
1724 * list lock and can be used safely with other locking &sk_buff functions
1727 * A buffer cannot be placed on two lists at the same time.
1729 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1731 unsigned long flags
;
1733 spin_lock_irqsave(&list
->lock
, flags
);
1734 __skb_queue_tail(list
, newsk
);
1735 spin_unlock_irqrestore(&list
->lock
, flags
);
1739 * skb_unlink - remove a buffer from a list
1740 * @skb: buffer to remove
1741 * @list: list to use
1743 * Remove a packet from a list. The list locks are taken and this
1744 * function is atomic with respect to other list locked calls
1746 * You must know what list the SKB is on.
1748 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1750 unsigned long flags
;
1752 spin_lock_irqsave(&list
->lock
, flags
);
1753 __skb_unlink(skb
, list
);
1754 spin_unlock_irqrestore(&list
->lock
, flags
);
1758 * skb_append - append a buffer
1759 * @old: buffer to insert after
1760 * @newsk: buffer to insert
1761 * @list: list to use
1763 * Place a packet after a given packet in a list. The list locks are taken
1764 * and this function is atomic with respect to other list locked calls.
1765 * A buffer cannot be placed on two lists at the same time.
1767 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1769 unsigned long flags
;
1771 spin_lock_irqsave(&list
->lock
, flags
);
1772 __skb_append(old
, newsk
, list
);
1773 spin_unlock_irqrestore(&list
->lock
, flags
);
1778 * skb_insert - insert a buffer
1779 * @old: buffer to insert before
1780 * @newsk: buffer to insert
1781 * @list: list to use
1783 * Place a packet before a given packet in a list. The list locks are
1784 * taken and this function is atomic with respect to other list locked
1787 * A buffer cannot be placed on two lists at the same time.
1789 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1791 unsigned long flags
;
1793 spin_lock_irqsave(&list
->lock
, flags
);
1794 __skb_insert(newsk
, old
->prev
, old
, list
);
1795 spin_unlock_irqrestore(&list
->lock
, flags
);
1798 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1799 struct sk_buff
* skb1
,
1800 const u32 len
, const int pos
)
1804 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1806 /* And move data appendix as is. */
1807 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1808 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1810 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1811 skb_shinfo(skb
)->nr_frags
= 0;
1812 skb1
->data_len
= skb
->data_len
;
1813 skb1
->len
+= skb1
->data_len
;
1816 skb_set_tail_pointer(skb
, len
);
1819 static inline void skb_split_no_header(struct sk_buff
*skb
,
1820 struct sk_buff
* skb1
,
1821 const u32 len
, int pos
)
1824 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1826 skb_shinfo(skb
)->nr_frags
= 0;
1827 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1829 skb
->data_len
= len
- pos
;
1831 for (i
= 0; i
< nfrags
; i
++) {
1832 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1834 if (pos
+ size
> len
) {
1835 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1839 * We have two variants in this case:
1840 * 1. Move all the frag to the second
1841 * part, if it is possible. F.e.
1842 * this approach is mandatory for TUX,
1843 * where splitting is expensive.
1844 * 2. Split is accurately. We make this.
1846 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1847 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1848 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1849 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1850 skb_shinfo(skb
)->nr_frags
++;
1854 skb_shinfo(skb
)->nr_frags
++;
1857 skb_shinfo(skb1
)->nr_frags
= k
;
1861 * skb_split - Split fragmented skb to two parts at length len.
1862 * @skb: the buffer to split
1863 * @skb1: the buffer to receive the second part
1864 * @len: new length for skb
1866 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1868 int pos
= skb_headlen(skb
);
1870 if (len
< pos
) /* Split line is inside header. */
1871 skb_split_inside_header(skb
, skb1
, len
, pos
);
1872 else /* Second chunk has no header, nothing to copy. */
1873 skb_split_no_header(skb
, skb1
, len
, pos
);
1877 * skb_prepare_seq_read - Prepare a sequential read of skb data
1878 * @skb: the buffer to read
1879 * @from: lower offset of data to be read
1880 * @to: upper offset of data to be read
1881 * @st: state variable
1883 * Initializes the specified state variable. Must be called before
1884 * invoking skb_seq_read() for the first time.
1886 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1887 unsigned int to
, struct skb_seq_state
*st
)
1889 st
->lower_offset
= from
;
1890 st
->upper_offset
= to
;
1891 st
->root_skb
= st
->cur_skb
= skb
;
1892 st
->frag_idx
= st
->stepped_offset
= 0;
1893 st
->frag_data
= NULL
;
1897 * skb_seq_read - Sequentially read skb data
1898 * @consumed: number of bytes consumed by the caller so far
1899 * @data: destination pointer for data to be returned
1900 * @st: state variable
1902 * Reads a block of skb data at &consumed relative to the
1903 * lower offset specified to skb_prepare_seq_read(). Assigns
1904 * the head of the data block to &data and returns the length
1905 * of the block or 0 if the end of the skb data or the upper
1906 * offset has been reached.
1908 * The caller is not required to consume all of the data
1909 * returned, i.e. &consumed is typically set to the number
1910 * of bytes already consumed and the next call to
1911 * skb_seq_read() will return the remaining part of the block.
1913 * Note: The size of each block of data returned can be arbitary,
1914 * this limitation is the cost for zerocopy seqeuental
1915 * reads of potentially non linear data.
1917 * Note: Fragment lists within fragments are not implemented
1918 * at the moment, state->root_skb could be replaced with
1919 * a stack for this purpose.
1921 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1922 struct skb_seq_state
*st
)
1924 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1927 if (unlikely(abs_offset
>= st
->upper_offset
))
1931 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
1933 if (abs_offset
< block_limit
&& !st
->frag_data
) {
1934 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
1935 return block_limit
- abs_offset
;
1938 if (st
->frag_idx
== 0 && !st
->frag_data
)
1939 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1941 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1942 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1943 block_limit
= frag
->size
+ st
->stepped_offset
;
1945 if (abs_offset
< block_limit
) {
1947 st
->frag_data
= kmap_skb_frag(frag
);
1949 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1950 (abs_offset
- st
->stepped_offset
);
1952 return block_limit
- abs_offset
;
1955 if (st
->frag_data
) {
1956 kunmap_skb_frag(st
->frag_data
);
1957 st
->frag_data
= NULL
;
1961 st
->stepped_offset
+= frag
->size
;
1964 if (st
->frag_data
) {
1965 kunmap_skb_frag(st
->frag_data
);
1966 st
->frag_data
= NULL
;
1969 if (st
->root_skb
== st
->cur_skb
&&
1970 skb_shinfo(st
->root_skb
)->frag_list
) {
1971 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1974 } else if (st
->cur_skb
->next
) {
1975 st
->cur_skb
= st
->cur_skb
->next
;
1984 * skb_abort_seq_read - Abort a sequential read of skb data
1985 * @st: state variable
1987 * Must be called if skb_seq_read() was not called until it
1990 void skb_abort_seq_read(struct skb_seq_state
*st
)
1993 kunmap_skb_frag(st
->frag_data
);
1996 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1998 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1999 struct ts_config
*conf
,
2000 struct ts_state
*state
)
2002 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2005 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2007 skb_abort_seq_read(TS_SKB_CB(state
));
2011 * skb_find_text - Find a text pattern in skb data
2012 * @skb: the buffer to look in
2013 * @from: search offset
2015 * @config: textsearch configuration
2016 * @state: uninitialized textsearch state variable
2018 * Finds a pattern in the skb data according to the specified
2019 * textsearch configuration. Use textsearch_next() to retrieve
2020 * subsequent occurrences of the pattern. Returns the offset
2021 * to the first occurrence or UINT_MAX if no match was found.
2023 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2024 unsigned int to
, struct ts_config
*config
,
2025 struct ts_state
*state
)
2029 config
->get_next_block
= skb_ts_get_next_block
;
2030 config
->finish
= skb_ts_finish
;
2032 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2034 ret
= textsearch_find(config
, state
);
2035 return (ret
<= to
- from
? ret
: UINT_MAX
);
2039 * skb_append_datato_frags: - append the user data to a skb
2040 * @sk: sock structure
2041 * @skb: skb structure to be appened with user data.
2042 * @getfrag: call back function to be used for getting the user data
2043 * @from: pointer to user message iov
2044 * @length: length of the iov message
2046 * Description: This procedure append the user data in the fragment part
2047 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2049 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2050 int (*getfrag
)(void *from
, char *to
, int offset
,
2051 int len
, int odd
, struct sk_buff
*skb
),
2052 void *from
, int length
)
2055 skb_frag_t
*frag
= NULL
;
2056 struct page
*page
= NULL
;
2062 /* Return error if we don't have space for new frag */
2063 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2064 if (frg_cnt
>= MAX_SKB_FRAGS
)
2067 /* allocate a new page for next frag */
2068 page
= alloc_pages(sk
->sk_allocation
, 0);
2070 /* If alloc_page fails just return failure and caller will
2071 * free previous allocated pages by doing kfree_skb()
2076 /* initialize the next frag */
2077 sk
->sk_sndmsg_page
= page
;
2078 sk
->sk_sndmsg_off
= 0;
2079 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2080 skb
->truesize
+= PAGE_SIZE
;
2081 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2083 /* get the new initialized frag */
2084 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2085 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2087 /* copy the user data to page */
2088 left
= PAGE_SIZE
- frag
->page_offset
;
2089 copy
= (length
> left
)? left
: length
;
2091 ret
= getfrag(from
, (page_address(frag
->page
) +
2092 frag
->page_offset
+ frag
->size
),
2093 offset
, copy
, 0, skb
);
2097 /* copy was successful so update the size parameters */
2098 sk
->sk_sndmsg_off
+= copy
;
2101 skb
->data_len
+= copy
;
2105 } while (length
> 0);
2111 * skb_pull_rcsum - pull skb and update receive checksum
2112 * @skb: buffer to update
2113 * @start: start of data before pull
2114 * @len: length of data pulled
2116 * This function performs an skb_pull on the packet and updates
2117 * update the CHECKSUM_COMPLETE checksum. It should be used on
2118 * receive path processing instead of skb_pull unless you know
2119 * that the checksum difference is zero (e.g., a valid IP header)
2120 * or you are setting ip_summed to CHECKSUM_NONE.
2122 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2124 BUG_ON(len
> skb
->len
);
2126 BUG_ON(skb
->len
< skb
->data_len
);
2127 skb_postpull_rcsum(skb
, skb
->data
, len
);
2128 return skb
->data
+= len
;
2131 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2134 * skb_segment - Perform protocol segmentation on skb.
2135 * @skb: buffer to segment
2136 * @features: features for the output path (see dev->features)
2138 * This function performs segmentation on the given skb. It returns
2139 * the segment at the given position. It returns NULL if there are
2140 * no more segments to generate, or when an error is encountered.
2142 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
2144 struct sk_buff
*segs
= NULL
;
2145 struct sk_buff
*tail
= NULL
;
2146 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2147 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2148 unsigned int offset
= doffset
;
2149 unsigned int headroom
;
2151 int sg
= features
& NETIF_F_SG
;
2152 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2157 __skb_push(skb
, doffset
);
2158 headroom
= skb_headroom(skb
);
2159 pos
= skb_headlen(skb
);
2162 struct sk_buff
*nskb
;
2168 len
= skb
->len
- offset
;
2172 hsize
= skb_headlen(skb
) - offset
;
2175 if (hsize
> len
|| !sg
)
2178 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
2179 if (unlikely(!nskb
))
2188 __copy_skb_header(nskb
, skb
);
2189 nskb
->mac_len
= skb
->mac_len
;
2191 skb_reserve(nskb
, headroom
);
2192 skb_reset_mac_header(nskb
);
2193 skb_set_network_header(nskb
, skb
->mac_len
);
2194 nskb
->transport_header
= (nskb
->network_header
+
2195 skb_network_header_len(skb
));
2196 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
2199 nskb
->ip_summed
= CHECKSUM_NONE
;
2200 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2206 frag
= skb_shinfo(nskb
)->frags
;
2209 skb_copy_from_linear_data_offset(skb
, offset
,
2210 skb_put(nskb
, hsize
), hsize
);
2212 while (pos
< offset
+ len
) {
2213 BUG_ON(i
>= nfrags
);
2215 *frag
= skb_shinfo(skb
)->frags
[i
];
2216 get_page(frag
->page
);
2220 frag
->page_offset
+= offset
- pos
;
2221 frag
->size
-= offset
- pos
;
2226 if (pos
+ size
<= offset
+ len
) {
2230 frag
->size
-= pos
+ size
- (offset
+ len
);
2237 skb_shinfo(nskb
)->nr_frags
= k
;
2238 nskb
->data_len
= len
- hsize
;
2239 nskb
->len
+= nskb
->data_len
;
2240 nskb
->truesize
+= nskb
->data_len
;
2241 } while ((offset
+= len
) < skb
->len
);
2246 while ((skb
= segs
)) {
2250 return ERR_PTR(err
);
2253 EXPORT_SYMBOL_GPL(skb_segment
);
2255 void __init
skb_init(void)
2257 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2258 sizeof(struct sk_buff
),
2260 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2262 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2263 (2*sizeof(struct sk_buff
)) +
2266 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2271 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2272 * @skb: Socket buffer containing the buffers to be mapped
2273 * @sg: The scatter-gather list to map into
2274 * @offset: The offset into the buffer's contents to start mapping
2275 * @len: Length of buffer space to be mapped
2277 * Fill the specified scatter-gather list with mappings/pointers into a
2278 * region of the buffer space attached to a socket buffer.
2281 skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2283 int start
= skb_headlen(skb
);
2284 int i
, copy
= start
- offset
;
2290 sg
[elt
].page
= virt_to_page(skb
->data
+ offset
);
2291 sg
[elt
].offset
= (unsigned long)(skb
->data
+ offset
) % PAGE_SIZE
;
2292 sg
[elt
].length
= copy
;
2294 if ((len
-= copy
) == 0)
2299 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2302 BUG_TRAP(start
<= offset
+ len
);
2304 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2305 if ((copy
= end
- offset
) > 0) {
2306 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2310 sg
[elt
].page
= frag
->page
;
2311 sg
[elt
].offset
= frag
->page_offset
+offset
-start
;
2312 sg
[elt
].length
= copy
;
2321 if (skb_shinfo(skb
)->frag_list
) {
2322 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
2324 for (; list
; list
= list
->next
) {
2327 BUG_TRAP(start
<= offset
+ len
);
2329 end
= start
+ list
->len
;
2330 if ((copy
= end
- offset
) > 0) {
2333 elt
+= skb_to_sgvec(list
, sg
+elt
, offset
- start
, copy
);
2334 if ((len
-= copy
) == 0)
2346 * skb_cow_data - Check that a socket buffer's data buffers are writable
2347 * @skb: The socket buffer to check.
2348 * @tailbits: Amount of trailing space to be added
2349 * @trailer: Returned pointer to the skb where the @tailbits space begins
2351 * Make sure that the data buffers attached to a socket buffer are
2352 * writable. If they are not, private copies are made of the data buffers
2353 * and the socket buffer is set to use these instead.
2355 * If @tailbits is given, make sure that there is space to write @tailbits
2356 * bytes of data beyond current end of socket buffer. @trailer will be
2357 * set to point to the skb in which this space begins.
2359 * The number of scatterlist elements required to completely map the
2360 * COW'd and extended socket buffer will be returned.
2362 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2366 struct sk_buff
*skb1
, **skb_p
;
2368 /* If skb is cloned or its head is paged, reallocate
2369 * head pulling out all the pages (pages are considered not writable
2370 * at the moment even if they are anonymous).
2372 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2373 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2376 /* Easy case. Most of packets will go this way. */
2377 if (!skb_shinfo(skb
)->frag_list
) {
2378 /* A little of trouble, not enough of space for trailer.
2379 * This should not happen, when stack is tuned to generate
2380 * good frames. OK, on miss we reallocate and reserve even more
2381 * space, 128 bytes is fair. */
2383 if (skb_tailroom(skb
) < tailbits
&&
2384 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2392 /* Misery. We are in troubles, going to mincer fragments... */
2395 skb_p
= &skb_shinfo(skb
)->frag_list
;
2398 while ((skb1
= *skb_p
) != NULL
) {
2401 /* The fragment is partially pulled by someone,
2402 * this can happen on input. Copy it and everything
2405 if (skb_shared(skb1
))
2408 /* If the skb is the last, worry about trailer. */
2410 if (skb1
->next
== NULL
&& tailbits
) {
2411 if (skb_shinfo(skb1
)->nr_frags
||
2412 skb_shinfo(skb1
)->frag_list
||
2413 skb_tailroom(skb1
) < tailbits
)
2414 ntail
= tailbits
+ 128;
2420 skb_shinfo(skb1
)->nr_frags
||
2421 skb_shinfo(skb1
)->frag_list
) {
2422 struct sk_buff
*skb2
;
2424 /* Fuck, we are miserable poor guys... */
2426 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2428 skb2
= skb_copy_expand(skb1
,
2432 if (unlikely(skb2
== NULL
))
2436 skb_set_owner_w(skb2
, skb1
->sk
);
2438 /* Looking around. Are we still alive?
2439 * OK, link new skb, drop old one */
2441 skb2
->next
= skb1
->next
;
2448 skb_p
= &skb1
->next
;
2454 EXPORT_SYMBOL(___pskb_trim
);
2455 EXPORT_SYMBOL(__kfree_skb
);
2456 EXPORT_SYMBOL(kfree_skb
);
2457 EXPORT_SYMBOL(__pskb_pull_tail
);
2458 EXPORT_SYMBOL(__alloc_skb
);
2459 EXPORT_SYMBOL(__netdev_alloc_skb
);
2460 EXPORT_SYMBOL(pskb_copy
);
2461 EXPORT_SYMBOL(pskb_expand_head
);
2462 EXPORT_SYMBOL(skb_checksum
);
2463 EXPORT_SYMBOL(skb_clone
);
2464 EXPORT_SYMBOL(skb_copy
);
2465 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2466 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2467 EXPORT_SYMBOL(skb_copy_bits
);
2468 EXPORT_SYMBOL(skb_copy_expand
);
2469 EXPORT_SYMBOL(skb_over_panic
);
2470 EXPORT_SYMBOL(skb_pad
);
2471 EXPORT_SYMBOL(skb_realloc_headroom
);
2472 EXPORT_SYMBOL(skb_under_panic
);
2473 EXPORT_SYMBOL(skb_dequeue
);
2474 EXPORT_SYMBOL(skb_dequeue_tail
);
2475 EXPORT_SYMBOL(skb_insert
);
2476 EXPORT_SYMBOL(skb_queue_purge
);
2477 EXPORT_SYMBOL(skb_queue_head
);
2478 EXPORT_SYMBOL(skb_queue_tail
);
2479 EXPORT_SYMBOL(skb_unlink
);
2480 EXPORT_SYMBOL(skb_append
);
2481 EXPORT_SYMBOL(skb_split
);
2482 EXPORT_SYMBOL(skb_prepare_seq_read
);
2483 EXPORT_SYMBOL(skb_seq_read
);
2484 EXPORT_SYMBOL(skb_abort_seq_read
);
2485 EXPORT_SYMBOL(skb_find_text
);
2486 EXPORT_SYMBOL(skb_append_datato_frags
);
2488 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2489 EXPORT_SYMBOL_GPL(skb_cow_data
);