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>
8 * Alan Cox : Fixed the worst of the load
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
43 #include <linux/interrupt.h>
45 #include <linux/inet.h>
46 #include <linux/slab.h>
47 #include <linux/netdevice.h>
48 #ifdef CONFIG_NET_CLS_ACT
49 #include <net/pkt_sched.h>
51 #include <linux/string.h>
52 #include <linux/skbuff.h>
53 #include <linux/splice.h>
54 #include <linux/cache.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/init.h>
57 #include <linux/scatterlist.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
;
73 static void sock_pipe_buf_release(struct pipe_inode_info
*pipe
,
74 struct pipe_buffer
*buf
)
79 static void sock_pipe_buf_get(struct pipe_inode_info
*pipe
,
80 struct pipe_buffer
*buf
)
85 static int sock_pipe_buf_steal(struct pipe_inode_info
*pipe
,
86 struct pipe_buffer
*buf
)
92 /* Pipe buffer operations for a socket. */
93 static struct pipe_buf_operations sock_pipe_buf_ops
= {
95 .map
= generic_pipe_buf_map
,
96 .unmap
= generic_pipe_buf_unmap
,
97 .confirm
= generic_pipe_buf_confirm
,
98 .release
= sock_pipe_buf_release
,
99 .steal
= sock_pipe_buf_steal
,
100 .get
= sock_pipe_buf_get
,
104 * Keep out-of-line to prevent kernel bloat.
105 * __builtin_return_address is not used because it is not always
110 * skb_over_panic - private function
115 * Out of line support code for skb_put(). Not user callable.
117 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
119 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
120 "data:%p tail:%#lx end:%#lx dev:%s\n",
121 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
122 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
123 skb
->dev
? skb
->dev
->name
: "<NULL>");
128 * skb_under_panic - private function
133 * Out of line support code for skb_push(). Not user callable.
136 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
138 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
139 "data:%p tail:%#lx end:%#lx dev:%s\n",
140 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
141 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
142 skb
->dev
? skb
->dev
->name
: "<NULL>");
146 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
147 * 'private' fields and also do memory statistics to find all the
153 * __alloc_skb - allocate a network buffer
154 * @size: size to allocate
155 * @gfp_mask: allocation mask
156 * @fclone: allocate from fclone cache instead of head cache
157 * and allocate a cloned (child) skb
158 * @node: numa node to allocate memory on
160 * Allocate a new &sk_buff. The returned buffer has no headroom and a
161 * tail room of size bytes. The object has a reference count of one.
162 * The return is the buffer. On a failure the return is %NULL.
164 * Buffers may only be allocated from interrupts using a @gfp_mask of
167 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
168 int fclone
, int node
)
170 struct kmem_cache
*cache
;
171 struct skb_shared_info
*shinfo
;
175 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
178 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
182 size
= SKB_DATA_ALIGN(size
);
183 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
189 * Only clear those fields we need to clear, not those that we will
190 * actually initialise below. Hence, don't put any more fields after
191 * the tail pointer in struct sk_buff!
193 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
194 skb
->truesize
= size
+ sizeof(struct sk_buff
);
195 atomic_set(&skb
->users
, 1);
198 skb_reset_tail_pointer(skb
);
199 skb
->end
= skb
->tail
+ size
;
200 /* make sure we initialize shinfo sequentially */
201 shinfo
= skb_shinfo(skb
);
202 atomic_set(&shinfo
->dataref
, 1);
203 shinfo
->nr_frags
= 0;
204 shinfo
->gso_size
= 0;
205 shinfo
->gso_segs
= 0;
206 shinfo
->gso_type
= 0;
207 shinfo
->ip6_frag_id
= 0;
208 shinfo
->frag_list
= NULL
;
211 struct sk_buff
*child
= skb
+ 1;
212 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
214 skb
->fclone
= SKB_FCLONE_ORIG
;
215 atomic_set(fclone_ref
, 1);
217 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
222 kmem_cache_free(cache
, skb
);
228 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
229 * @dev: network device to receive on
230 * @length: length to allocate
231 * @gfp_mask: get_free_pages mask, passed to alloc_skb
233 * Allocate a new &sk_buff and assign it a usage count of one. The
234 * buffer has unspecified headroom built in. Users should allocate
235 * the headroom they think they need without accounting for the
236 * built in space. The built in space is used for optimisations.
238 * %NULL is returned if there is no free memory.
240 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
241 unsigned int length
, gfp_t gfp_mask
)
243 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
246 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
, 0, node
);
248 skb_reserve(skb
, NET_SKB_PAD
);
255 * dev_alloc_skb - allocate an skbuff for receiving
256 * @length: length to allocate
258 * Allocate a new &sk_buff and assign it a usage count of one. The
259 * buffer has unspecified headroom built in. Users should allocate
260 * the headroom they think they need without accounting for the
261 * built in space. The built in space is used for optimisations.
263 * %NULL is returned if there is no free memory. Although this function
264 * allocates memory it can be called from an interrupt.
266 struct sk_buff
*dev_alloc_skb(unsigned int length
)
269 * There is more code here than it seems:
270 * __dev_alloc_skb is an inline
272 return __dev_alloc_skb(length
, GFP_ATOMIC
);
274 EXPORT_SYMBOL(dev_alloc_skb
);
276 static void skb_drop_list(struct sk_buff
**listp
)
278 struct sk_buff
*list
= *listp
;
283 struct sk_buff
*this = list
;
289 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
291 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
294 static void skb_clone_fraglist(struct sk_buff
*skb
)
296 struct sk_buff
*list
;
298 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
302 static void skb_release_data(struct sk_buff
*skb
)
305 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
306 &skb_shinfo(skb
)->dataref
)) {
307 if (skb_shinfo(skb
)->nr_frags
) {
309 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
310 put_page(skb_shinfo(skb
)->frags
[i
].page
);
313 if (skb_shinfo(skb
)->frag_list
)
314 skb_drop_fraglist(skb
);
321 * Free an skbuff by memory without cleaning the state.
323 static void kfree_skbmem(struct sk_buff
*skb
)
325 struct sk_buff
*other
;
326 atomic_t
*fclone_ref
;
328 switch (skb
->fclone
) {
329 case SKB_FCLONE_UNAVAILABLE
:
330 kmem_cache_free(skbuff_head_cache
, skb
);
333 case SKB_FCLONE_ORIG
:
334 fclone_ref
= (atomic_t
*) (skb
+ 2);
335 if (atomic_dec_and_test(fclone_ref
))
336 kmem_cache_free(skbuff_fclone_cache
, skb
);
339 case SKB_FCLONE_CLONE
:
340 fclone_ref
= (atomic_t
*) (skb
+ 1);
343 /* The clone portion is available for
344 * fast-cloning again.
346 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
348 if (atomic_dec_and_test(fclone_ref
))
349 kmem_cache_free(skbuff_fclone_cache
, other
);
354 /* Free everything but the sk_buff shell. */
355 static void skb_release_all(struct sk_buff
*skb
)
357 dst_release(skb
->dst
);
359 secpath_put(skb
->sp
);
361 if (skb
->destructor
) {
363 skb
->destructor(skb
);
365 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
366 nf_conntrack_put(skb
->nfct
);
367 nf_conntrack_put_reasm(skb
->nfct_reasm
);
369 #ifdef CONFIG_BRIDGE_NETFILTER
370 nf_bridge_put(skb
->nf_bridge
);
372 /* XXX: IS this still necessary? - JHS */
373 #ifdef CONFIG_NET_SCHED
375 #ifdef CONFIG_NET_CLS_ACT
379 skb_release_data(skb
);
383 * __kfree_skb - private function
386 * Free an sk_buff. Release anything attached to the buffer.
387 * Clean the state. This is an internal helper function. Users should
388 * always call kfree_skb
391 void __kfree_skb(struct sk_buff
*skb
)
393 skb_release_all(skb
);
398 * kfree_skb - free an sk_buff
399 * @skb: buffer to free
401 * Drop a reference to the buffer and free it if the usage count has
404 void kfree_skb(struct sk_buff
*skb
)
408 if (likely(atomic_read(&skb
->users
) == 1))
410 else if (likely(!atomic_dec_and_test(&skb
->users
)))
415 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
417 new->tstamp
= old
->tstamp
;
419 new->transport_header
= old
->transport_header
;
420 new->network_header
= old
->network_header
;
421 new->mac_header
= old
->mac_header
;
422 new->dst
= dst_clone(old
->dst
);
424 new->sp
= secpath_get(old
->sp
);
426 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
427 new->csum_start
= old
->csum_start
;
428 new->csum_offset
= old
->csum_offset
;
429 new->local_df
= old
->local_df
;
430 new->pkt_type
= old
->pkt_type
;
431 new->ip_summed
= old
->ip_summed
;
432 skb_copy_queue_mapping(new, old
);
433 new->priority
= old
->priority
;
434 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
435 new->ipvs_property
= old
->ipvs_property
;
437 new->protocol
= old
->protocol
;
438 new->mark
= old
->mark
;
440 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
441 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
442 new->nf_trace
= old
->nf_trace
;
444 #ifdef CONFIG_NET_SCHED
445 new->tc_index
= old
->tc_index
;
446 #ifdef CONFIG_NET_CLS_ACT
447 new->tc_verd
= old
->tc_verd
;
450 new->vlan_tci
= old
->vlan_tci
;
452 skb_copy_secmark(new, old
);
455 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
457 #define C(x) n->x = skb->x
459 n
->next
= n
->prev
= NULL
;
461 __copy_skb_header(n
, skb
);
466 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
469 n
->destructor
= NULL
;
476 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
479 atomic_set(&n
->users
, 1);
481 atomic_inc(&(skb_shinfo(skb
)->dataref
));
489 * skb_morph - morph one skb into another
490 * @dst: the skb to receive the contents
491 * @src: the skb to supply the contents
493 * This is identical to skb_clone except that the target skb is
494 * supplied by the user.
496 * The target skb is returned upon exit.
498 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
500 skb_release_all(dst
);
501 return __skb_clone(dst
, src
);
503 EXPORT_SYMBOL_GPL(skb_morph
);
506 * skb_clone - duplicate an sk_buff
507 * @skb: buffer to clone
508 * @gfp_mask: allocation priority
510 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
511 * copies share the same packet data but not structure. The new
512 * buffer has a reference count of 1. If the allocation fails the
513 * function returns %NULL otherwise the new buffer is returned.
515 * If this function is called from an interrupt gfp_mask() must be
519 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
524 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
525 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
526 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
527 n
->fclone
= SKB_FCLONE_CLONE
;
528 atomic_inc(fclone_ref
);
530 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
533 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
536 return __skb_clone(n
, skb
);
539 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
541 #ifndef NET_SKBUFF_DATA_USES_OFFSET
543 * Shift between the two data areas in bytes
545 unsigned long offset
= new->data
- old
->data
;
548 __copy_skb_header(new, old
);
550 #ifndef NET_SKBUFF_DATA_USES_OFFSET
551 /* {transport,network,mac}_header are relative to skb->head */
552 new->transport_header
+= offset
;
553 new->network_header
+= offset
;
554 new->mac_header
+= offset
;
556 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
557 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
558 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
562 * skb_copy - create private copy of an sk_buff
563 * @skb: buffer to copy
564 * @gfp_mask: allocation priority
566 * Make a copy of both an &sk_buff and its data. This is used when the
567 * caller wishes to modify the data and needs a private copy of the
568 * data to alter. Returns %NULL on failure or the pointer to the buffer
569 * on success. The returned buffer has a reference count of 1.
571 * As by-product this function converts non-linear &sk_buff to linear
572 * one, so that &sk_buff becomes completely private and caller is allowed
573 * to modify all the data of returned buffer. This means that this
574 * function is not recommended for use in circumstances when only
575 * header is going to be modified. Use pskb_copy() instead.
578 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
580 int headerlen
= skb
->data
- skb
->head
;
582 * Allocate the copy buffer
585 #ifdef NET_SKBUFF_DATA_USES_OFFSET
586 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
588 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
593 /* Set the data pointer */
594 skb_reserve(n
, headerlen
);
595 /* Set the tail pointer and length */
596 skb_put(n
, skb
->len
);
598 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
601 copy_skb_header(n
, skb
);
607 * pskb_copy - create copy of an sk_buff with private head.
608 * @skb: buffer to copy
609 * @gfp_mask: allocation priority
611 * Make a copy of both an &sk_buff and part of its data, located
612 * in header. Fragmented data remain shared. This is used when
613 * the caller wishes to modify only header of &sk_buff and needs
614 * private copy of the header to alter. Returns %NULL on failure
615 * or the pointer to the buffer on success.
616 * The returned buffer has a reference count of 1.
619 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
622 * Allocate the copy buffer
625 #ifdef NET_SKBUFF_DATA_USES_OFFSET
626 n
= alloc_skb(skb
->end
, gfp_mask
);
628 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
633 /* Set the data pointer */
634 skb_reserve(n
, skb
->data
- skb
->head
);
635 /* Set the tail pointer and length */
636 skb_put(n
, skb_headlen(skb
));
638 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
640 n
->truesize
+= skb
->data_len
;
641 n
->data_len
= skb
->data_len
;
644 if (skb_shinfo(skb
)->nr_frags
) {
647 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
648 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
649 get_page(skb_shinfo(n
)->frags
[i
].page
);
651 skb_shinfo(n
)->nr_frags
= i
;
654 if (skb_shinfo(skb
)->frag_list
) {
655 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
656 skb_clone_fraglist(n
);
659 copy_skb_header(n
, skb
);
665 * pskb_expand_head - reallocate header of &sk_buff
666 * @skb: buffer to reallocate
667 * @nhead: room to add at head
668 * @ntail: room to add at tail
669 * @gfp_mask: allocation priority
671 * Expands (or creates identical copy, if &nhead and &ntail are zero)
672 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
673 * reference count of 1. Returns zero in the case of success or error,
674 * if expansion failed. In the last case, &sk_buff is not changed.
676 * All the pointers pointing into skb header may change and must be
677 * reloaded after call to this function.
680 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
685 #ifdef NET_SKBUFF_DATA_USES_OFFSET
686 int size
= nhead
+ skb
->end
+ ntail
;
688 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
695 size
= SKB_DATA_ALIGN(size
);
697 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
701 /* Copy only real data... and, alas, header. This should be
702 * optimized for the cases when header is void. */
703 #ifdef NET_SKBUFF_DATA_USES_OFFSET
704 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
706 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
708 memcpy(data
+ size
, skb_end_pointer(skb
),
709 sizeof(struct skb_shared_info
));
711 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
712 get_page(skb_shinfo(skb
)->frags
[i
].page
);
714 if (skb_shinfo(skb
)->frag_list
)
715 skb_clone_fraglist(skb
);
717 skb_release_data(skb
);
719 off
= (data
+ nhead
) - skb
->head
;
723 #ifdef NET_SKBUFF_DATA_USES_OFFSET
727 skb
->end
= skb
->head
+ size
;
729 /* {transport,network,mac}_header and tail are relative to skb->head */
731 skb
->transport_header
+= off
;
732 skb
->network_header
+= off
;
733 skb
->mac_header
+= off
;
734 skb
->csum_start
+= nhead
;
738 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
745 /* Make private copy of skb with writable head and some headroom */
747 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
749 struct sk_buff
*skb2
;
750 int delta
= headroom
- skb_headroom(skb
);
753 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
755 skb2
= skb_clone(skb
, GFP_ATOMIC
);
756 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
767 * skb_copy_expand - copy and expand sk_buff
768 * @skb: buffer to copy
769 * @newheadroom: new free bytes at head
770 * @newtailroom: new free bytes at tail
771 * @gfp_mask: allocation priority
773 * Make a copy of both an &sk_buff and its data and while doing so
774 * allocate additional space.
776 * This is used when the caller wishes to modify the data and needs a
777 * private copy of the data to alter as well as more space for new fields.
778 * Returns %NULL on failure or the pointer to the buffer
779 * on success. The returned buffer has a reference count of 1.
781 * You must pass %GFP_ATOMIC as the allocation priority if this function
782 * is called from an interrupt.
784 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
785 int newheadroom
, int newtailroom
,
789 * Allocate the copy buffer
791 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
793 int oldheadroom
= skb_headroom(skb
);
794 int head_copy_len
, head_copy_off
;
800 skb_reserve(n
, newheadroom
);
802 /* Set the tail pointer and length */
803 skb_put(n
, skb
->len
);
805 head_copy_len
= oldheadroom
;
807 if (newheadroom
<= head_copy_len
)
808 head_copy_len
= newheadroom
;
810 head_copy_off
= newheadroom
- head_copy_len
;
812 /* Copy the linear header and data. */
813 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
814 skb
->len
+ head_copy_len
))
817 copy_skb_header(n
, skb
);
819 off
= newheadroom
- oldheadroom
;
820 n
->csum_start
+= off
;
821 #ifdef NET_SKBUFF_DATA_USES_OFFSET
822 n
->transport_header
+= off
;
823 n
->network_header
+= off
;
824 n
->mac_header
+= off
;
831 * skb_pad - zero pad the tail of an skb
832 * @skb: buffer to pad
835 * Ensure that a buffer is followed by a padding area that is zero
836 * filled. Used by network drivers which may DMA or transfer data
837 * beyond the buffer end onto the wire.
839 * May return error in out of memory cases. The skb is freed on error.
842 int skb_pad(struct sk_buff
*skb
, int pad
)
847 /* If the skbuff is non linear tailroom is always zero.. */
848 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
849 memset(skb
->data
+skb
->len
, 0, pad
);
853 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
854 if (likely(skb_cloned(skb
) || ntail
> 0)) {
855 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
860 /* FIXME: The use of this function with non-linear skb's really needs
863 err
= skb_linearize(skb
);
867 memset(skb
->data
+ skb
->len
, 0, pad
);
876 * skb_put - add data to a buffer
877 * @skb: buffer to use
878 * @len: amount of data to add
880 * This function extends the used data area of the buffer. If this would
881 * exceed the total buffer size the kernel will panic. A pointer to the
882 * first byte of the extra data is returned.
884 unsigned char *skb_put(struct sk_buff
*skb
, unsigned int len
)
886 unsigned char *tmp
= skb_tail_pointer(skb
);
887 SKB_LINEAR_ASSERT(skb
);
890 if (unlikely(skb
->tail
> skb
->end
))
891 skb_over_panic(skb
, len
, __builtin_return_address(0));
894 EXPORT_SYMBOL(skb_put
);
897 * skb_push - add data to the start of a buffer
898 * @skb: buffer to use
899 * @len: amount of data to add
901 * This function extends the used data area of the buffer at the buffer
902 * start. If this would exceed the total buffer headroom the kernel will
903 * panic. A pointer to the first byte of the extra data is returned.
905 unsigned char *skb_push(struct sk_buff
*skb
, unsigned int len
)
909 if (unlikely(skb
->data
<skb
->head
))
910 skb_under_panic(skb
, len
, __builtin_return_address(0));
913 EXPORT_SYMBOL(skb_push
);
916 * skb_pull - remove data from the start of a buffer
917 * @skb: buffer to use
918 * @len: amount of data to remove
920 * This function removes data from the start of a buffer, returning
921 * the memory to the headroom. A pointer to the next data in the buffer
922 * is returned. Once the data has been pulled future pushes will overwrite
925 unsigned char *skb_pull(struct sk_buff
*skb
, unsigned int len
)
927 return unlikely(len
> skb
->len
) ? NULL
: __skb_pull(skb
, len
);
929 EXPORT_SYMBOL(skb_pull
);
932 * skb_trim - remove end from a buffer
933 * @skb: buffer to alter
936 * Cut the length of a buffer down by removing data from the tail. If
937 * the buffer is already under the length specified it is not modified.
938 * The skb must be linear.
940 void skb_trim(struct sk_buff
*skb
, unsigned int len
)
943 __skb_trim(skb
, len
);
945 EXPORT_SYMBOL(skb_trim
);
947 /* Trims skb to length len. It can change skb pointers.
950 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
952 struct sk_buff
**fragp
;
953 struct sk_buff
*frag
;
954 int offset
= skb_headlen(skb
);
955 int nfrags
= skb_shinfo(skb
)->nr_frags
;
959 if (skb_cloned(skb
) &&
960 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
967 for (; i
< nfrags
; i
++) {
968 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
975 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
978 skb_shinfo(skb
)->nr_frags
= i
;
980 for (; i
< nfrags
; i
++)
981 put_page(skb_shinfo(skb
)->frags
[i
].page
);
983 if (skb_shinfo(skb
)->frag_list
)
984 skb_drop_fraglist(skb
);
988 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
989 fragp
= &frag
->next
) {
990 int end
= offset
+ frag
->len
;
992 if (skb_shared(frag
)) {
993 struct sk_buff
*nfrag
;
995 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
996 if (unlikely(!nfrag
))
999 nfrag
->next
= frag
->next
;
1011 unlikely((err
= pskb_trim(frag
, len
- offset
))))
1015 skb_drop_list(&frag
->next
);
1020 if (len
> skb_headlen(skb
)) {
1021 skb
->data_len
-= skb
->len
- len
;
1026 skb_set_tail_pointer(skb
, len
);
1033 * __pskb_pull_tail - advance tail of skb header
1034 * @skb: buffer to reallocate
1035 * @delta: number of bytes to advance tail
1037 * The function makes a sense only on a fragmented &sk_buff,
1038 * it expands header moving its tail forward and copying necessary
1039 * data from fragmented part.
1041 * &sk_buff MUST have reference count of 1.
1043 * Returns %NULL (and &sk_buff does not change) if pull failed
1044 * or value of new tail of skb in the case of success.
1046 * All the pointers pointing into skb header may change and must be
1047 * reloaded after call to this function.
1050 /* Moves tail of skb head forward, copying data from fragmented part,
1051 * when it is necessary.
1052 * 1. It may fail due to malloc failure.
1053 * 2. It may change skb pointers.
1055 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1057 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1059 /* If skb has not enough free space at tail, get new one
1060 * plus 128 bytes for future expansions. If we have enough
1061 * room at tail, reallocate without expansion only if skb is cloned.
1063 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1065 if (eat
> 0 || skb_cloned(skb
)) {
1066 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1071 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1074 /* Optimization: no fragments, no reasons to preestimate
1075 * size of pulled pages. Superb.
1077 if (!skb_shinfo(skb
)->frag_list
)
1080 /* Estimate size of pulled pages. */
1082 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1083 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
1085 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1088 /* If we need update frag list, we are in troubles.
1089 * Certainly, it possible to add an offset to skb data,
1090 * but taking into account that pulling is expected to
1091 * be very rare operation, it is worth to fight against
1092 * further bloating skb head and crucify ourselves here instead.
1093 * Pure masohism, indeed. 8)8)
1096 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1097 struct sk_buff
*clone
= NULL
;
1098 struct sk_buff
*insp
= NULL
;
1103 if (list
->len
<= eat
) {
1104 /* Eaten as whole. */
1109 /* Eaten partially. */
1111 if (skb_shared(list
)) {
1112 /* Sucks! We need to fork list. :-( */
1113 clone
= skb_clone(list
, GFP_ATOMIC
);
1119 /* This may be pulled without
1123 if (!pskb_pull(list
, eat
)) {
1132 /* Free pulled out fragments. */
1133 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1134 skb_shinfo(skb
)->frag_list
= list
->next
;
1137 /* And insert new clone at head. */
1140 skb_shinfo(skb
)->frag_list
= clone
;
1143 /* Success! Now we may commit changes to skb data. */
1148 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1149 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1150 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1151 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1153 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1155 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1156 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1162 skb_shinfo(skb
)->nr_frags
= k
;
1165 skb
->data_len
-= delta
;
1167 return skb_tail_pointer(skb
);
1170 /* Copy some data bits from skb to kernel buffer. */
1172 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1175 int start
= skb_headlen(skb
);
1177 if (offset
> (int)skb
->len
- len
)
1181 if ((copy
= start
- offset
) > 0) {
1184 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1185 if ((len
-= copy
) == 0)
1191 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1194 WARN_ON(start
> offset
+ len
);
1196 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1197 if ((copy
= end
- offset
) > 0) {
1203 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1205 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1206 offset
- start
, copy
);
1207 kunmap_skb_frag(vaddr
);
1209 if ((len
-= copy
) == 0)
1217 if (skb_shinfo(skb
)->frag_list
) {
1218 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1220 for (; list
; list
= list
->next
) {
1223 WARN_ON(start
> offset
+ len
);
1225 end
= start
+ list
->len
;
1226 if ((copy
= end
- offset
) > 0) {
1229 if (skb_copy_bits(list
, offset
- start
,
1232 if ((len
-= copy
) == 0)
1248 * Callback from splice_to_pipe(), if we need to release some pages
1249 * at the end of the spd in case we error'ed out in filling the pipe.
1251 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1253 put_page(spd
->pages
[i
]);
1256 static inline struct page
*linear_to_page(struct page
*page
, unsigned int len
,
1257 unsigned int offset
)
1259 struct page
*p
= alloc_pages(GFP_KERNEL
, 0);
1263 memcpy(page_address(p
) + offset
, page_address(page
) + offset
, len
);
1269 * Fill page/offset/length into spd, if it can hold more pages.
1271 static inline int spd_fill_page(struct splice_pipe_desc
*spd
, struct page
*page
,
1272 unsigned int len
, unsigned int offset
,
1273 struct sk_buff
*skb
, int linear
)
1275 if (unlikely(spd
->nr_pages
== PIPE_BUFFERS
))
1279 page
= linear_to_page(page
, len
, offset
);
1285 spd
->pages
[spd
->nr_pages
] = page
;
1286 spd
->partial
[spd
->nr_pages
].len
= len
;
1287 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1293 static inline void __segment_seek(struct page
**page
, unsigned int *poff
,
1294 unsigned int *plen
, unsigned int off
)
1297 *page
+= *poff
/ PAGE_SIZE
;
1298 *poff
= *poff
% PAGE_SIZE
;
1302 static inline int __splice_segment(struct page
*page
, unsigned int poff
,
1303 unsigned int plen
, unsigned int *off
,
1304 unsigned int *len
, struct sk_buff
*skb
,
1305 struct splice_pipe_desc
*spd
, int linear
)
1310 /* skip this segment if already processed */
1316 /* ignore any bits we already processed */
1318 __segment_seek(&page
, &poff
, &plen
, *off
);
1323 unsigned int flen
= min(*len
, plen
);
1325 /* the linear region may spread across several pages */
1326 flen
= min_t(unsigned int, flen
, PAGE_SIZE
- poff
);
1328 if (spd_fill_page(spd
, page
, flen
, poff
, skb
, linear
))
1331 __segment_seek(&page
, &poff
, &plen
, flen
);
1334 } while (*len
&& plen
);
1340 * Map linear and fragment data from the skb to spd. It reports failure if the
1341 * pipe is full or if we already spliced the requested length.
1343 static int __skb_splice_bits(struct sk_buff
*skb
, unsigned int *offset
,
1345 struct splice_pipe_desc
*spd
)
1350 * map the linear part
1352 if (__splice_segment(virt_to_page(skb
->data
),
1353 (unsigned long) skb
->data
& (PAGE_SIZE
- 1),
1355 offset
, len
, skb
, spd
, 1))
1359 * then map the fragments
1361 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1362 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1364 if (__splice_segment(f
->page
, f
->page_offset
, f
->size
,
1365 offset
, len
, skb
, spd
, 0))
1373 * Map data from the skb to a pipe. Should handle both the linear part,
1374 * the fragments, and the frag list. It does NOT handle frag lists within
1375 * the frag list, if such a thing exists. We'd probably need to recurse to
1376 * handle that cleanly.
1378 int skb_splice_bits(struct sk_buff
*skb
, unsigned int offset
,
1379 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1382 struct partial_page partial
[PIPE_BUFFERS
];
1383 struct page
*pages
[PIPE_BUFFERS
];
1384 struct splice_pipe_desc spd
= {
1388 .ops
= &sock_pipe_buf_ops
,
1389 .spd_release
= sock_spd_release
,
1393 * __skb_splice_bits() only fails if the output has no room left,
1394 * so no point in going over the frag_list for the error case.
1396 if (__skb_splice_bits(skb
, &offset
, &tlen
, &spd
))
1402 * now see if we have a frag_list to map
1404 if (skb_shinfo(skb
)->frag_list
) {
1405 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1407 for (; list
&& tlen
; list
= list
->next
) {
1408 if (__skb_splice_bits(list
, &offset
, &tlen
, &spd
))
1415 struct sock
*sk
= skb
->sk
;
1419 * Drop the socket lock, otherwise we have reverse
1420 * locking dependencies between sk_lock and i_mutex
1421 * here as compared to sendfile(). We enter here
1422 * with the socket lock held, and splice_to_pipe() will
1423 * grab the pipe inode lock. For sendfile() emulation,
1424 * we call into ->sendpage() with the i_mutex lock held
1425 * and networking will grab the socket lock.
1428 ret
= splice_to_pipe(pipe
, &spd
);
1437 * skb_store_bits - store bits from kernel buffer to skb
1438 * @skb: destination buffer
1439 * @offset: offset in destination
1440 * @from: source buffer
1441 * @len: number of bytes to copy
1443 * Copy the specified number of bytes from the source buffer to the
1444 * destination skb. This function handles all the messy bits of
1445 * traversing fragment lists and such.
1448 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1451 int start
= skb_headlen(skb
);
1453 if (offset
> (int)skb
->len
- len
)
1456 if ((copy
= start
- offset
) > 0) {
1459 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1460 if ((len
-= copy
) == 0)
1466 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1467 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1470 WARN_ON(start
> offset
+ len
);
1472 end
= start
+ frag
->size
;
1473 if ((copy
= end
- offset
) > 0) {
1479 vaddr
= kmap_skb_frag(frag
);
1480 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1482 kunmap_skb_frag(vaddr
);
1484 if ((len
-= copy
) == 0)
1492 if (skb_shinfo(skb
)->frag_list
) {
1493 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1495 for (; list
; list
= list
->next
) {
1498 WARN_ON(start
> offset
+ len
);
1500 end
= start
+ list
->len
;
1501 if ((copy
= end
- offset
) > 0) {
1504 if (skb_store_bits(list
, offset
- start
,
1507 if ((len
-= copy
) == 0)
1522 EXPORT_SYMBOL(skb_store_bits
);
1524 /* Checksum skb data. */
1526 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1527 int len
, __wsum csum
)
1529 int start
= skb_headlen(skb
);
1530 int i
, copy
= start
- offset
;
1533 /* Checksum header. */
1537 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1538 if ((len
-= copy
) == 0)
1544 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1547 WARN_ON(start
> offset
+ len
);
1549 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1550 if ((copy
= end
- offset
) > 0) {
1553 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1557 vaddr
= kmap_skb_frag(frag
);
1558 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1559 offset
- start
, copy
, 0);
1560 kunmap_skb_frag(vaddr
);
1561 csum
= csum_block_add(csum
, csum2
, pos
);
1570 if (skb_shinfo(skb
)->frag_list
) {
1571 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1573 for (; list
; list
= list
->next
) {
1576 WARN_ON(start
> offset
+ len
);
1578 end
= start
+ list
->len
;
1579 if ((copy
= end
- offset
) > 0) {
1583 csum2
= skb_checksum(list
, offset
- start
,
1585 csum
= csum_block_add(csum
, csum2
, pos
);
1586 if ((len
-= copy
) == 0)
1599 /* Both of above in one bottle. */
1601 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1602 u8
*to
, int len
, __wsum csum
)
1604 int start
= skb_headlen(skb
);
1605 int i
, copy
= start
- offset
;
1612 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1614 if ((len
-= copy
) == 0)
1621 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1624 WARN_ON(start
> offset
+ len
);
1626 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1627 if ((copy
= end
- offset
) > 0) {
1630 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1634 vaddr
= kmap_skb_frag(frag
);
1635 csum2
= csum_partial_copy_nocheck(vaddr
+
1639 kunmap_skb_frag(vaddr
);
1640 csum
= csum_block_add(csum
, csum2
, pos
);
1650 if (skb_shinfo(skb
)->frag_list
) {
1651 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1653 for (; list
; list
= list
->next
) {
1657 WARN_ON(start
> offset
+ len
);
1659 end
= start
+ list
->len
;
1660 if ((copy
= end
- offset
) > 0) {
1663 csum2
= skb_copy_and_csum_bits(list
,
1666 csum
= csum_block_add(csum
, csum2
, pos
);
1667 if ((len
-= copy
) == 0)
1680 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1685 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1686 csstart
= skb
->csum_start
- skb_headroom(skb
);
1688 csstart
= skb_headlen(skb
);
1690 BUG_ON(csstart
> skb_headlen(skb
));
1692 skb_copy_from_linear_data(skb
, to
, csstart
);
1695 if (csstart
!= skb
->len
)
1696 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1697 skb
->len
- csstart
, 0);
1699 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1700 long csstuff
= csstart
+ skb
->csum_offset
;
1702 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1707 * skb_dequeue - remove from the head of the queue
1708 * @list: list to dequeue from
1710 * Remove the head of the list. The list lock is taken so the function
1711 * may be used safely with other locking list functions. The head item is
1712 * returned or %NULL if the list is empty.
1715 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1717 unsigned long flags
;
1718 struct sk_buff
*result
;
1720 spin_lock_irqsave(&list
->lock
, flags
);
1721 result
= __skb_dequeue(list
);
1722 spin_unlock_irqrestore(&list
->lock
, flags
);
1727 * skb_dequeue_tail - remove from the tail of the queue
1728 * @list: list to dequeue from
1730 * Remove the tail of the list. The list lock is taken so the function
1731 * may be used safely with other locking list functions. The tail item is
1732 * returned or %NULL if the list is empty.
1734 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1736 unsigned long flags
;
1737 struct sk_buff
*result
;
1739 spin_lock_irqsave(&list
->lock
, flags
);
1740 result
= __skb_dequeue_tail(list
);
1741 spin_unlock_irqrestore(&list
->lock
, flags
);
1746 * skb_queue_purge - empty a list
1747 * @list: list to empty
1749 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1750 * the list and one reference dropped. This function takes the list
1751 * lock and is atomic with respect to other list locking functions.
1753 void skb_queue_purge(struct sk_buff_head
*list
)
1755 struct sk_buff
*skb
;
1756 while ((skb
= skb_dequeue(list
)) != NULL
)
1761 * skb_queue_head - queue a buffer at the list head
1762 * @list: list to use
1763 * @newsk: buffer to queue
1765 * Queue a buffer at the start of the list. This function takes the
1766 * list lock and can be used safely with other locking &sk_buff functions
1769 * A buffer cannot be placed on two lists at the same time.
1771 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1773 unsigned long flags
;
1775 spin_lock_irqsave(&list
->lock
, flags
);
1776 __skb_queue_head(list
, newsk
);
1777 spin_unlock_irqrestore(&list
->lock
, flags
);
1781 * skb_queue_tail - queue a buffer at the list tail
1782 * @list: list to use
1783 * @newsk: buffer to queue
1785 * Queue a buffer at the tail of the list. This function takes the
1786 * list lock and can be used safely with other locking &sk_buff functions
1789 * A buffer cannot be placed on two lists at the same time.
1791 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1793 unsigned long flags
;
1795 spin_lock_irqsave(&list
->lock
, flags
);
1796 __skb_queue_tail(list
, newsk
);
1797 spin_unlock_irqrestore(&list
->lock
, flags
);
1801 * skb_unlink - remove a buffer from a list
1802 * @skb: buffer to remove
1803 * @list: list to use
1805 * Remove a packet from a list. The list locks are taken and this
1806 * function is atomic with respect to other list locked calls
1808 * You must know what list the SKB is on.
1810 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1812 unsigned long flags
;
1814 spin_lock_irqsave(&list
->lock
, flags
);
1815 __skb_unlink(skb
, list
);
1816 spin_unlock_irqrestore(&list
->lock
, flags
);
1820 * skb_append - append a buffer
1821 * @old: buffer to insert after
1822 * @newsk: buffer to insert
1823 * @list: list to use
1825 * Place a packet after a given packet in a list. The list locks are taken
1826 * and this function is atomic with respect to other list locked calls.
1827 * A buffer cannot be placed on two lists at the same time.
1829 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1831 unsigned long flags
;
1833 spin_lock_irqsave(&list
->lock
, flags
);
1834 __skb_queue_after(list
, old
, newsk
);
1835 spin_unlock_irqrestore(&list
->lock
, flags
);
1840 * skb_insert - insert a buffer
1841 * @old: buffer to insert before
1842 * @newsk: buffer to insert
1843 * @list: list to use
1845 * Place a packet before a given packet in a list. The list locks are
1846 * taken and this function is atomic with respect to other list locked
1849 * A buffer cannot be placed on two lists at the same time.
1851 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1853 unsigned long flags
;
1855 spin_lock_irqsave(&list
->lock
, flags
);
1856 __skb_insert(newsk
, old
->prev
, old
, list
);
1857 spin_unlock_irqrestore(&list
->lock
, flags
);
1860 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1861 struct sk_buff
* skb1
,
1862 const u32 len
, const int pos
)
1866 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1868 /* And move data appendix as is. */
1869 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1870 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1872 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1873 skb_shinfo(skb
)->nr_frags
= 0;
1874 skb1
->data_len
= skb
->data_len
;
1875 skb1
->len
+= skb1
->data_len
;
1878 skb_set_tail_pointer(skb
, len
);
1881 static inline void skb_split_no_header(struct sk_buff
*skb
,
1882 struct sk_buff
* skb1
,
1883 const u32 len
, int pos
)
1886 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1888 skb_shinfo(skb
)->nr_frags
= 0;
1889 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1891 skb
->data_len
= len
- pos
;
1893 for (i
= 0; i
< nfrags
; i
++) {
1894 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1896 if (pos
+ size
> len
) {
1897 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1901 * We have two variants in this case:
1902 * 1. Move all the frag to the second
1903 * part, if it is possible. F.e.
1904 * this approach is mandatory for TUX,
1905 * where splitting is expensive.
1906 * 2. Split is accurately. We make this.
1908 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1909 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1910 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1911 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1912 skb_shinfo(skb
)->nr_frags
++;
1916 skb_shinfo(skb
)->nr_frags
++;
1919 skb_shinfo(skb1
)->nr_frags
= k
;
1923 * skb_split - Split fragmented skb to two parts at length len.
1924 * @skb: the buffer to split
1925 * @skb1: the buffer to receive the second part
1926 * @len: new length for skb
1928 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1930 int pos
= skb_headlen(skb
);
1932 if (len
< pos
) /* Split line is inside header. */
1933 skb_split_inside_header(skb
, skb1
, len
, pos
);
1934 else /* Second chunk has no header, nothing to copy. */
1935 skb_split_no_header(skb
, skb1
, len
, pos
);
1939 * skb_prepare_seq_read - Prepare a sequential read of skb data
1940 * @skb: the buffer to read
1941 * @from: lower offset of data to be read
1942 * @to: upper offset of data to be read
1943 * @st: state variable
1945 * Initializes the specified state variable. Must be called before
1946 * invoking skb_seq_read() for the first time.
1948 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1949 unsigned int to
, struct skb_seq_state
*st
)
1951 st
->lower_offset
= from
;
1952 st
->upper_offset
= to
;
1953 st
->root_skb
= st
->cur_skb
= skb
;
1954 st
->frag_idx
= st
->stepped_offset
= 0;
1955 st
->frag_data
= NULL
;
1959 * skb_seq_read - Sequentially read skb data
1960 * @consumed: number of bytes consumed by the caller so far
1961 * @data: destination pointer for data to be returned
1962 * @st: state variable
1964 * Reads a block of skb data at &consumed relative to the
1965 * lower offset specified to skb_prepare_seq_read(). Assigns
1966 * the head of the data block to &data and returns the length
1967 * of the block or 0 if the end of the skb data or the upper
1968 * offset has been reached.
1970 * The caller is not required to consume all of the data
1971 * returned, i.e. &consumed is typically set to the number
1972 * of bytes already consumed and the next call to
1973 * skb_seq_read() will return the remaining part of the block.
1975 * Note 1: The size of each block of data returned can be arbitary,
1976 * this limitation is the cost for zerocopy seqeuental
1977 * reads of potentially non linear data.
1979 * Note 2: Fragment lists within fragments are not implemented
1980 * at the moment, state->root_skb could be replaced with
1981 * a stack for this purpose.
1983 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1984 struct skb_seq_state
*st
)
1986 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1989 if (unlikely(abs_offset
>= st
->upper_offset
))
1993 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
1995 if (abs_offset
< block_limit
) {
1996 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
1997 return block_limit
- abs_offset
;
2000 if (st
->frag_idx
== 0 && !st
->frag_data
)
2001 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
2003 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
2004 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
2005 block_limit
= frag
->size
+ st
->stepped_offset
;
2007 if (abs_offset
< block_limit
) {
2009 st
->frag_data
= kmap_skb_frag(frag
);
2011 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
2012 (abs_offset
- st
->stepped_offset
);
2014 return block_limit
- abs_offset
;
2017 if (st
->frag_data
) {
2018 kunmap_skb_frag(st
->frag_data
);
2019 st
->frag_data
= NULL
;
2023 st
->stepped_offset
+= frag
->size
;
2026 if (st
->frag_data
) {
2027 kunmap_skb_frag(st
->frag_data
);
2028 st
->frag_data
= NULL
;
2031 if (st
->root_skb
== st
->cur_skb
&&
2032 skb_shinfo(st
->root_skb
)->frag_list
) {
2033 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2036 } else if (st
->cur_skb
->next
) {
2037 st
->cur_skb
= st
->cur_skb
->next
;
2046 * skb_abort_seq_read - Abort a sequential read of skb data
2047 * @st: state variable
2049 * Must be called if skb_seq_read() was not called until it
2052 void skb_abort_seq_read(struct skb_seq_state
*st
)
2055 kunmap_skb_frag(st
->frag_data
);
2058 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2060 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2061 struct ts_config
*conf
,
2062 struct ts_state
*state
)
2064 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2067 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2069 skb_abort_seq_read(TS_SKB_CB(state
));
2073 * skb_find_text - Find a text pattern in skb data
2074 * @skb: the buffer to look in
2075 * @from: search offset
2077 * @config: textsearch configuration
2078 * @state: uninitialized textsearch state variable
2080 * Finds a pattern in the skb data according to the specified
2081 * textsearch configuration. Use textsearch_next() to retrieve
2082 * subsequent occurrences of the pattern. Returns the offset
2083 * to the first occurrence or UINT_MAX if no match was found.
2085 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2086 unsigned int to
, struct ts_config
*config
,
2087 struct ts_state
*state
)
2091 config
->get_next_block
= skb_ts_get_next_block
;
2092 config
->finish
= skb_ts_finish
;
2094 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2096 ret
= textsearch_find(config
, state
);
2097 return (ret
<= to
- from
? ret
: UINT_MAX
);
2101 * skb_append_datato_frags: - append the user data to a skb
2102 * @sk: sock structure
2103 * @skb: skb structure to be appened with user data.
2104 * @getfrag: call back function to be used for getting the user data
2105 * @from: pointer to user message iov
2106 * @length: length of the iov message
2108 * Description: This procedure append the user data in the fragment part
2109 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2111 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2112 int (*getfrag
)(void *from
, char *to
, int offset
,
2113 int len
, int odd
, struct sk_buff
*skb
),
2114 void *from
, int length
)
2117 skb_frag_t
*frag
= NULL
;
2118 struct page
*page
= NULL
;
2124 /* Return error if we don't have space for new frag */
2125 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2126 if (frg_cnt
>= MAX_SKB_FRAGS
)
2129 /* allocate a new page for next frag */
2130 page
= alloc_pages(sk
->sk_allocation
, 0);
2132 /* If alloc_page fails just return failure and caller will
2133 * free previous allocated pages by doing kfree_skb()
2138 /* initialize the next frag */
2139 sk
->sk_sndmsg_page
= page
;
2140 sk
->sk_sndmsg_off
= 0;
2141 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2142 skb
->truesize
+= PAGE_SIZE
;
2143 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2145 /* get the new initialized frag */
2146 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2147 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2149 /* copy the user data to page */
2150 left
= PAGE_SIZE
- frag
->page_offset
;
2151 copy
= (length
> left
)? left
: length
;
2153 ret
= getfrag(from
, (page_address(frag
->page
) +
2154 frag
->page_offset
+ frag
->size
),
2155 offset
, copy
, 0, skb
);
2159 /* copy was successful so update the size parameters */
2160 sk
->sk_sndmsg_off
+= copy
;
2163 skb
->data_len
+= copy
;
2167 } while (length
> 0);
2173 * skb_pull_rcsum - pull skb and update receive checksum
2174 * @skb: buffer to update
2175 * @len: length of data pulled
2177 * This function performs an skb_pull on the packet and updates
2178 * the CHECKSUM_COMPLETE checksum. It should be used on
2179 * receive path processing instead of skb_pull unless you know
2180 * that the checksum difference is zero (e.g., a valid IP header)
2181 * or you are setting ip_summed to CHECKSUM_NONE.
2183 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2185 BUG_ON(len
> skb
->len
);
2187 BUG_ON(skb
->len
< skb
->data_len
);
2188 skb_postpull_rcsum(skb
, skb
->data
, len
);
2189 return skb
->data
+= len
;
2192 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2195 * skb_segment - Perform protocol segmentation on skb.
2196 * @skb: buffer to segment
2197 * @features: features for the output path (see dev->features)
2199 * This function performs segmentation on the given skb. It returns
2200 * a pointer to the first in a list of new skbs for the segments.
2201 * In case of error it returns ERR_PTR(err).
2203 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
2205 struct sk_buff
*segs
= NULL
;
2206 struct sk_buff
*tail
= NULL
;
2207 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2208 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2209 unsigned int offset
= doffset
;
2210 unsigned int headroom
;
2212 int sg
= features
& NETIF_F_SG
;
2213 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2218 __skb_push(skb
, doffset
);
2219 headroom
= skb_headroom(skb
);
2220 pos
= skb_headlen(skb
);
2223 struct sk_buff
*nskb
;
2229 len
= skb
->len
- offset
;
2233 hsize
= skb_headlen(skb
) - offset
;
2236 if (hsize
> len
|| !sg
)
2239 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
2240 if (unlikely(!nskb
))
2249 __copy_skb_header(nskb
, skb
);
2250 nskb
->mac_len
= skb
->mac_len
;
2252 skb_reserve(nskb
, headroom
);
2253 skb_reset_mac_header(nskb
);
2254 skb_set_network_header(nskb
, skb
->mac_len
);
2255 nskb
->transport_header
= (nskb
->network_header
+
2256 skb_network_header_len(skb
));
2257 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
2260 nskb
->ip_summed
= CHECKSUM_NONE
;
2261 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2267 frag
= skb_shinfo(nskb
)->frags
;
2270 skb_copy_from_linear_data_offset(skb
, offset
,
2271 skb_put(nskb
, hsize
), hsize
);
2273 while (pos
< offset
+ len
) {
2274 BUG_ON(i
>= nfrags
);
2276 *frag
= skb_shinfo(skb
)->frags
[i
];
2277 get_page(frag
->page
);
2281 frag
->page_offset
+= offset
- pos
;
2282 frag
->size
-= offset
- pos
;
2287 if (pos
+ size
<= offset
+ len
) {
2291 frag
->size
-= pos
+ size
- (offset
+ len
);
2298 skb_shinfo(nskb
)->nr_frags
= k
;
2299 nskb
->data_len
= len
- hsize
;
2300 nskb
->len
+= nskb
->data_len
;
2301 nskb
->truesize
+= nskb
->data_len
;
2302 } while ((offset
+= len
) < skb
->len
);
2307 while ((skb
= segs
)) {
2311 return ERR_PTR(err
);
2314 EXPORT_SYMBOL_GPL(skb_segment
);
2316 void __init
skb_init(void)
2318 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2319 sizeof(struct sk_buff
),
2321 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2323 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2324 (2*sizeof(struct sk_buff
)) +
2327 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2332 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2333 * @skb: Socket buffer containing the buffers to be mapped
2334 * @sg: The scatter-gather list to map into
2335 * @offset: The offset into the buffer's contents to start mapping
2336 * @len: Length of buffer space to be mapped
2338 * Fill the specified scatter-gather list with mappings/pointers into a
2339 * region of the buffer space attached to a socket buffer.
2342 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2344 int start
= skb_headlen(skb
);
2345 int i
, copy
= start
- offset
;
2351 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
2353 if ((len
-= copy
) == 0)
2358 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2361 WARN_ON(start
> offset
+ len
);
2363 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2364 if ((copy
= end
- offset
) > 0) {
2365 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2369 sg_set_page(&sg
[elt
], frag
->page
, copy
,
2370 frag
->page_offset
+offset
-start
);
2379 if (skb_shinfo(skb
)->frag_list
) {
2380 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
2382 for (; list
; list
= list
->next
) {
2385 WARN_ON(start
> offset
+ len
);
2387 end
= start
+ list
->len
;
2388 if ((copy
= end
- offset
) > 0) {
2391 elt
+= __skb_to_sgvec(list
, sg
+elt
, offset
- start
,
2393 if ((len
-= copy
) == 0)
2404 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2406 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
2408 sg_mark_end(&sg
[nsg
- 1]);
2414 * skb_cow_data - Check that a socket buffer's data buffers are writable
2415 * @skb: The socket buffer to check.
2416 * @tailbits: Amount of trailing space to be added
2417 * @trailer: Returned pointer to the skb where the @tailbits space begins
2419 * Make sure that the data buffers attached to a socket buffer are
2420 * writable. If they are not, private copies are made of the data buffers
2421 * and the socket buffer is set to use these instead.
2423 * If @tailbits is given, make sure that there is space to write @tailbits
2424 * bytes of data beyond current end of socket buffer. @trailer will be
2425 * set to point to the skb in which this space begins.
2427 * The number of scatterlist elements required to completely map the
2428 * COW'd and extended socket buffer will be returned.
2430 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2434 struct sk_buff
*skb1
, **skb_p
;
2436 /* If skb is cloned or its head is paged, reallocate
2437 * head pulling out all the pages (pages are considered not writable
2438 * at the moment even if they are anonymous).
2440 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2441 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2444 /* Easy case. Most of packets will go this way. */
2445 if (!skb_shinfo(skb
)->frag_list
) {
2446 /* A little of trouble, not enough of space for trailer.
2447 * This should not happen, when stack is tuned to generate
2448 * good frames. OK, on miss we reallocate and reserve even more
2449 * space, 128 bytes is fair. */
2451 if (skb_tailroom(skb
) < tailbits
&&
2452 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2460 /* Misery. We are in troubles, going to mincer fragments... */
2463 skb_p
= &skb_shinfo(skb
)->frag_list
;
2466 while ((skb1
= *skb_p
) != NULL
) {
2469 /* The fragment is partially pulled by someone,
2470 * this can happen on input. Copy it and everything
2473 if (skb_shared(skb1
))
2476 /* If the skb is the last, worry about trailer. */
2478 if (skb1
->next
== NULL
&& tailbits
) {
2479 if (skb_shinfo(skb1
)->nr_frags
||
2480 skb_shinfo(skb1
)->frag_list
||
2481 skb_tailroom(skb1
) < tailbits
)
2482 ntail
= tailbits
+ 128;
2488 skb_shinfo(skb1
)->nr_frags
||
2489 skb_shinfo(skb1
)->frag_list
) {
2490 struct sk_buff
*skb2
;
2492 /* Fuck, we are miserable poor guys... */
2494 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2496 skb2
= skb_copy_expand(skb1
,
2500 if (unlikely(skb2
== NULL
))
2504 skb_set_owner_w(skb2
, skb1
->sk
);
2506 /* Looking around. Are we still alive?
2507 * OK, link new skb, drop old one */
2509 skb2
->next
= skb1
->next
;
2516 skb_p
= &skb1
->next
;
2523 * skb_partial_csum_set - set up and verify partial csum values for packet
2524 * @skb: the skb to set
2525 * @start: the number of bytes after skb->data to start checksumming.
2526 * @off: the offset from start to place the checksum.
2528 * For untrusted partially-checksummed packets, we need to make sure the values
2529 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2531 * This function checks and sets those values and skb->ip_summed: if this
2532 * returns false you should drop the packet.
2534 bool skb_partial_csum_set(struct sk_buff
*skb
, u16 start
, u16 off
)
2536 if (unlikely(start
> skb
->len
- 2) ||
2537 unlikely((int)start
+ off
> skb
->len
- 2)) {
2538 if (net_ratelimit())
2540 "bad partial csum: csum=%u/%u len=%u\n",
2541 start
, off
, skb
->len
);
2544 skb
->ip_summed
= CHECKSUM_PARTIAL
;
2545 skb
->csum_start
= skb_headroom(skb
) + start
;
2546 skb
->csum_offset
= off
;
2550 void __skb_warn_lro_forwarding(const struct sk_buff
*skb
)
2552 if (net_ratelimit())
2553 pr_warning("%s: received packets cannot be forwarded"
2554 " while LRO is enabled\n", skb
->dev
->name
);
2557 EXPORT_SYMBOL(___pskb_trim
);
2558 EXPORT_SYMBOL(__kfree_skb
);
2559 EXPORT_SYMBOL(kfree_skb
);
2560 EXPORT_SYMBOL(__pskb_pull_tail
);
2561 EXPORT_SYMBOL(__alloc_skb
);
2562 EXPORT_SYMBOL(__netdev_alloc_skb
);
2563 EXPORT_SYMBOL(pskb_copy
);
2564 EXPORT_SYMBOL(pskb_expand_head
);
2565 EXPORT_SYMBOL(skb_checksum
);
2566 EXPORT_SYMBOL(skb_clone
);
2567 EXPORT_SYMBOL(skb_copy
);
2568 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2569 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2570 EXPORT_SYMBOL(skb_copy_bits
);
2571 EXPORT_SYMBOL(skb_copy_expand
);
2572 EXPORT_SYMBOL(skb_over_panic
);
2573 EXPORT_SYMBOL(skb_pad
);
2574 EXPORT_SYMBOL(skb_realloc_headroom
);
2575 EXPORT_SYMBOL(skb_under_panic
);
2576 EXPORT_SYMBOL(skb_dequeue
);
2577 EXPORT_SYMBOL(skb_dequeue_tail
);
2578 EXPORT_SYMBOL(skb_insert
);
2579 EXPORT_SYMBOL(skb_queue_purge
);
2580 EXPORT_SYMBOL(skb_queue_head
);
2581 EXPORT_SYMBOL(skb_queue_tail
);
2582 EXPORT_SYMBOL(skb_unlink
);
2583 EXPORT_SYMBOL(skb_append
);
2584 EXPORT_SYMBOL(skb_split
);
2585 EXPORT_SYMBOL(skb_prepare_seq_read
);
2586 EXPORT_SYMBOL(skb_seq_read
);
2587 EXPORT_SYMBOL(skb_abort_seq_read
);
2588 EXPORT_SYMBOL(skb_find_text
);
2589 EXPORT_SYMBOL(skb_append_datato_frags
);
2590 EXPORT_SYMBOL(__skb_warn_lro_forwarding
);
2592 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2593 EXPORT_SYMBOL_GPL(skb_cow_data
);
2594 EXPORT_SYMBOL_GPL(skb_partial_csum_set
);