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 void skb_truesize_bug(struct sk_buff
*skb
)
154 printk(KERN_ERR
"SKB BUG: Invalid truesize (%u) "
155 "len=%u, sizeof(sk_buff)=%Zd\n",
156 skb
->truesize
, skb
->len
, sizeof(struct sk_buff
));
158 EXPORT_SYMBOL(skb_truesize_bug
);
160 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
161 * 'private' fields and also do memory statistics to find all the
167 * __alloc_skb - allocate a network buffer
168 * @size: size to allocate
169 * @gfp_mask: allocation mask
170 * @fclone: allocate from fclone cache instead of head cache
171 * and allocate a cloned (child) skb
172 * @node: numa node to allocate memory on
174 * Allocate a new &sk_buff. The returned buffer has no headroom and a
175 * tail room of size bytes. The object has a reference count of one.
176 * The return is the buffer. On a failure the return is %NULL.
178 * Buffers may only be allocated from interrupts using a @gfp_mask of
181 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
182 int fclone
, int node
)
184 struct kmem_cache
*cache
;
185 struct skb_shared_info
*shinfo
;
189 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
192 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
196 size
= SKB_DATA_ALIGN(size
);
197 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
203 * See comment in sk_buff definition, just before the 'tail' member
205 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
206 skb
->truesize
= size
+ sizeof(struct sk_buff
);
207 atomic_set(&skb
->users
, 1);
210 skb_reset_tail_pointer(skb
);
211 skb
->end
= skb
->tail
+ size
;
212 /* make sure we initialize shinfo sequentially */
213 shinfo
= skb_shinfo(skb
);
214 atomic_set(&shinfo
->dataref
, 1);
215 shinfo
->nr_frags
= 0;
216 shinfo
->gso_size
= 0;
217 shinfo
->gso_segs
= 0;
218 shinfo
->gso_type
= 0;
219 shinfo
->ip6_frag_id
= 0;
220 shinfo
->frag_list
= NULL
;
223 struct sk_buff
*child
= skb
+ 1;
224 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
226 skb
->fclone
= SKB_FCLONE_ORIG
;
227 atomic_set(fclone_ref
, 1);
229 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
234 kmem_cache_free(cache
, skb
);
240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
250 * %NULL is returned if there is no free memory.
252 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
253 unsigned int length
, gfp_t gfp_mask
)
255 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
258 skb
= __alloc_skb(length
+ NET_SKB_PAD
, gfp_mask
, 0, node
);
260 skb_reserve(skb
, NET_SKB_PAD
);
266 static void skb_drop_list(struct sk_buff
**listp
)
268 struct sk_buff
*list
= *listp
;
273 struct sk_buff
*this = list
;
279 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
281 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
284 static void skb_clone_fraglist(struct sk_buff
*skb
)
286 struct sk_buff
*list
;
288 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
292 static void skb_release_data(struct sk_buff
*skb
)
295 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
296 &skb_shinfo(skb
)->dataref
)) {
297 if (skb_shinfo(skb
)->nr_frags
) {
299 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
300 put_page(skb_shinfo(skb
)->frags
[i
].page
);
303 if (skb_shinfo(skb
)->frag_list
)
304 skb_drop_fraglist(skb
);
311 * Free an skbuff by memory without cleaning the state.
313 static void kfree_skbmem(struct sk_buff
*skb
)
315 struct sk_buff
*other
;
316 atomic_t
*fclone_ref
;
318 switch (skb
->fclone
) {
319 case SKB_FCLONE_UNAVAILABLE
:
320 kmem_cache_free(skbuff_head_cache
, skb
);
323 case SKB_FCLONE_ORIG
:
324 fclone_ref
= (atomic_t
*) (skb
+ 2);
325 if (atomic_dec_and_test(fclone_ref
))
326 kmem_cache_free(skbuff_fclone_cache
, skb
);
329 case SKB_FCLONE_CLONE
:
330 fclone_ref
= (atomic_t
*) (skb
+ 1);
333 /* The clone portion is available for
334 * fast-cloning again.
336 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
338 if (atomic_dec_and_test(fclone_ref
))
339 kmem_cache_free(skbuff_fclone_cache
, other
);
344 /* Free everything but the sk_buff shell. */
345 static void skb_release_all(struct sk_buff
*skb
)
347 dst_release(skb
->dst
);
349 secpath_put(skb
->sp
);
351 if (skb
->destructor
) {
353 skb
->destructor(skb
);
355 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
356 nf_conntrack_put(skb
->nfct
);
357 nf_conntrack_put_reasm(skb
->nfct_reasm
);
359 #ifdef CONFIG_BRIDGE_NETFILTER
360 nf_bridge_put(skb
->nf_bridge
);
362 /* XXX: IS this still necessary? - JHS */
363 #ifdef CONFIG_NET_SCHED
365 #ifdef CONFIG_NET_CLS_ACT
369 skb_release_data(skb
);
373 * __kfree_skb - private function
376 * Free an sk_buff. Release anything attached to the buffer.
377 * Clean the state. This is an internal helper function. Users should
378 * always call kfree_skb
381 void __kfree_skb(struct sk_buff
*skb
)
383 skb_release_all(skb
);
388 * kfree_skb - free an sk_buff
389 * @skb: buffer to free
391 * Drop a reference to the buffer and free it if the usage count has
394 void kfree_skb(struct sk_buff
*skb
)
398 if (likely(atomic_read(&skb
->users
) == 1))
400 else if (likely(!atomic_dec_and_test(&skb
->users
)))
405 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
407 new->tstamp
= old
->tstamp
;
409 new->transport_header
= old
->transport_header
;
410 new->network_header
= old
->network_header
;
411 new->mac_header
= old
->mac_header
;
412 new->dst
= dst_clone(old
->dst
);
414 new->sp
= secpath_get(old
->sp
);
416 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
417 new->csum_start
= old
->csum_start
;
418 new->csum_offset
= old
->csum_offset
;
419 new->local_df
= old
->local_df
;
420 new->pkt_type
= old
->pkt_type
;
421 new->ip_summed
= old
->ip_summed
;
422 skb_copy_queue_mapping(new, old
);
423 new->priority
= old
->priority
;
424 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
425 new->ipvs_property
= old
->ipvs_property
;
427 new->protocol
= old
->protocol
;
428 new->mark
= old
->mark
;
430 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
431 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
432 new->nf_trace
= old
->nf_trace
;
434 #ifdef CONFIG_NET_SCHED
435 new->tc_index
= old
->tc_index
;
436 #ifdef CONFIG_NET_CLS_ACT
437 new->tc_verd
= old
->tc_verd
;
440 skb_copy_secmark(new, old
);
443 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
445 #define C(x) n->x = skb->x
447 n
->next
= n
->prev
= NULL
;
449 __copy_skb_header(n
, skb
);
454 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
457 n
->destructor
= NULL
;
464 atomic_set(&n
->users
, 1);
466 atomic_inc(&(skb_shinfo(skb
)->dataref
));
474 * skb_morph - morph one skb into another
475 * @dst: the skb to receive the contents
476 * @src: the skb to supply the contents
478 * This is identical to skb_clone except that the target skb is
479 * supplied by the user.
481 * The target skb is returned upon exit.
483 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
485 skb_release_all(dst
);
486 return __skb_clone(dst
, src
);
488 EXPORT_SYMBOL_GPL(skb_morph
);
491 * skb_clone - duplicate an sk_buff
492 * @skb: buffer to clone
493 * @gfp_mask: allocation priority
495 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
496 * copies share the same packet data but not structure. The new
497 * buffer has a reference count of 1. If the allocation fails the
498 * function returns %NULL otherwise the new buffer is returned.
500 * If this function is called from an interrupt gfp_mask() must be
504 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
509 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
510 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
511 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
512 n
->fclone
= SKB_FCLONE_CLONE
;
513 atomic_inc(fclone_ref
);
515 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
518 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
521 return __skb_clone(n
, skb
);
524 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
526 #ifndef NET_SKBUFF_DATA_USES_OFFSET
528 * Shift between the two data areas in bytes
530 unsigned long offset
= new->data
- old
->data
;
533 __copy_skb_header(new, old
);
535 #ifndef NET_SKBUFF_DATA_USES_OFFSET
536 /* {transport,network,mac}_header are relative to skb->head */
537 new->transport_header
+= offset
;
538 new->network_header
+= offset
;
539 new->mac_header
+= offset
;
541 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
542 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
543 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
547 * skb_copy - create private copy of an sk_buff
548 * @skb: buffer to copy
549 * @gfp_mask: allocation priority
551 * Make a copy of both an &sk_buff and its data. This is used when the
552 * caller wishes to modify the data and needs a private copy of the
553 * data to alter. Returns %NULL on failure or the pointer to the buffer
554 * on success. The returned buffer has a reference count of 1.
556 * As by-product this function converts non-linear &sk_buff to linear
557 * one, so that &sk_buff becomes completely private and caller is allowed
558 * to modify all the data of returned buffer. This means that this
559 * function is not recommended for use in circumstances when only
560 * header is going to be modified. Use pskb_copy() instead.
563 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
565 int headerlen
= skb
->data
- skb
->head
;
567 * Allocate the copy buffer
570 #ifdef NET_SKBUFF_DATA_USES_OFFSET
571 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
573 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
578 /* Set the data pointer */
579 skb_reserve(n
, headerlen
);
580 /* Set the tail pointer and length */
581 skb_put(n
, skb
->len
);
583 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
586 copy_skb_header(n
, skb
);
592 * pskb_copy - create copy of an sk_buff with private head.
593 * @skb: buffer to copy
594 * @gfp_mask: allocation priority
596 * Make a copy of both an &sk_buff and part of its data, located
597 * in header. Fragmented data remain shared. This is used when
598 * the caller wishes to modify only header of &sk_buff and needs
599 * private copy of the header to alter. Returns %NULL on failure
600 * or the pointer to the buffer on success.
601 * The returned buffer has a reference count of 1.
604 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
607 * Allocate the copy buffer
610 #ifdef NET_SKBUFF_DATA_USES_OFFSET
611 n
= alloc_skb(skb
->end
, gfp_mask
);
613 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
618 /* Set the data pointer */
619 skb_reserve(n
, skb
->data
- skb
->head
);
620 /* Set the tail pointer and length */
621 skb_put(n
, skb_headlen(skb
));
623 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
625 n
->truesize
+= skb
->data_len
;
626 n
->data_len
= skb
->data_len
;
629 if (skb_shinfo(skb
)->nr_frags
) {
632 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
633 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
634 get_page(skb_shinfo(n
)->frags
[i
].page
);
636 skb_shinfo(n
)->nr_frags
= i
;
639 if (skb_shinfo(skb
)->frag_list
) {
640 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
641 skb_clone_fraglist(n
);
644 copy_skb_header(n
, skb
);
650 * pskb_expand_head - reallocate header of &sk_buff
651 * @skb: buffer to reallocate
652 * @nhead: room to add at head
653 * @ntail: room to add at tail
654 * @gfp_mask: allocation priority
656 * Expands (or creates identical copy, if &nhead and &ntail are zero)
657 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
658 * reference count of 1. Returns zero in the case of success or error,
659 * if expansion failed. In the last case, &sk_buff is not changed.
661 * All the pointers pointing into skb header may change and must be
662 * reloaded after call to this function.
665 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
670 #ifdef NET_SKBUFF_DATA_USES_OFFSET
671 int size
= nhead
+ skb
->end
+ ntail
;
673 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
680 size
= SKB_DATA_ALIGN(size
);
682 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
686 /* Copy only real data... and, alas, header. This should be
687 * optimized for the cases when header is void. */
688 #ifdef NET_SKBUFF_DATA_USES_OFFSET
689 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
691 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
693 memcpy(data
+ size
, skb_end_pointer(skb
),
694 sizeof(struct skb_shared_info
));
696 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
697 get_page(skb_shinfo(skb
)->frags
[i
].page
);
699 if (skb_shinfo(skb
)->frag_list
)
700 skb_clone_fraglist(skb
);
702 skb_release_data(skb
);
704 off
= (data
+ nhead
) - skb
->head
;
708 #ifdef NET_SKBUFF_DATA_USES_OFFSET
712 skb
->end
= skb
->head
+ size
;
714 /* {transport,network,mac}_header and tail are relative to skb->head */
716 skb
->transport_header
+= off
;
717 skb
->network_header
+= off
;
718 skb
->mac_header
+= off
;
719 skb
->csum_start
+= nhead
;
723 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
730 /* Make private copy of skb with writable head and some headroom */
732 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
734 struct sk_buff
*skb2
;
735 int delta
= headroom
- skb_headroom(skb
);
738 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
740 skb2
= skb_clone(skb
, GFP_ATOMIC
);
741 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
752 * skb_copy_expand - copy and expand sk_buff
753 * @skb: buffer to copy
754 * @newheadroom: new free bytes at head
755 * @newtailroom: new free bytes at tail
756 * @gfp_mask: allocation priority
758 * Make a copy of both an &sk_buff and its data and while doing so
759 * allocate additional space.
761 * This is used when the caller wishes to modify the data and needs a
762 * private copy of the data to alter as well as more space for new fields.
763 * Returns %NULL on failure or the pointer to the buffer
764 * on success. The returned buffer has a reference count of 1.
766 * You must pass %GFP_ATOMIC as the allocation priority if this function
767 * is called from an interrupt.
769 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
770 int newheadroom
, int newtailroom
,
774 * Allocate the copy buffer
776 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
778 int oldheadroom
= skb_headroom(skb
);
779 int head_copy_len
, head_copy_off
;
785 skb_reserve(n
, newheadroom
);
787 /* Set the tail pointer and length */
788 skb_put(n
, skb
->len
);
790 head_copy_len
= oldheadroom
;
792 if (newheadroom
<= head_copy_len
)
793 head_copy_len
= newheadroom
;
795 head_copy_off
= newheadroom
- head_copy_len
;
797 /* Copy the linear header and data. */
798 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
799 skb
->len
+ head_copy_len
))
802 copy_skb_header(n
, skb
);
804 off
= newheadroom
- oldheadroom
;
805 n
->csum_start
+= off
;
806 #ifdef NET_SKBUFF_DATA_USES_OFFSET
807 n
->transport_header
+= off
;
808 n
->network_header
+= off
;
809 n
->mac_header
+= off
;
816 * skb_pad - zero pad the tail of an skb
817 * @skb: buffer to pad
820 * Ensure that a buffer is followed by a padding area that is zero
821 * filled. Used by network drivers which may DMA or transfer data
822 * beyond the buffer end onto the wire.
824 * May return error in out of memory cases. The skb is freed on error.
827 int skb_pad(struct sk_buff
*skb
, int pad
)
832 /* If the skbuff is non linear tailroom is always zero.. */
833 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
834 memset(skb
->data
+skb
->len
, 0, pad
);
838 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
839 if (likely(skb_cloned(skb
) || ntail
> 0)) {
840 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
845 /* FIXME: The use of this function with non-linear skb's really needs
848 err
= skb_linearize(skb
);
852 memset(skb
->data
+ skb
->len
, 0, pad
);
860 /* Trims skb to length len. It can change skb pointers.
863 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
865 struct sk_buff
**fragp
;
866 struct sk_buff
*frag
;
867 int offset
= skb_headlen(skb
);
868 int nfrags
= skb_shinfo(skb
)->nr_frags
;
872 if (skb_cloned(skb
) &&
873 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
880 for (; i
< nfrags
; i
++) {
881 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
888 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
891 skb_shinfo(skb
)->nr_frags
= i
;
893 for (; i
< nfrags
; i
++)
894 put_page(skb_shinfo(skb
)->frags
[i
].page
);
896 if (skb_shinfo(skb
)->frag_list
)
897 skb_drop_fraglist(skb
);
901 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
902 fragp
= &frag
->next
) {
903 int end
= offset
+ frag
->len
;
905 if (skb_shared(frag
)) {
906 struct sk_buff
*nfrag
;
908 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
909 if (unlikely(!nfrag
))
912 nfrag
->next
= frag
->next
;
924 unlikely((err
= pskb_trim(frag
, len
- offset
))))
928 skb_drop_list(&frag
->next
);
933 if (len
> skb_headlen(skb
)) {
934 skb
->data_len
-= skb
->len
- len
;
939 skb_set_tail_pointer(skb
, len
);
946 * __pskb_pull_tail - advance tail of skb header
947 * @skb: buffer to reallocate
948 * @delta: number of bytes to advance tail
950 * The function makes a sense only on a fragmented &sk_buff,
951 * it expands header moving its tail forward and copying necessary
952 * data from fragmented part.
954 * &sk_buff MUST have reference count of 1.
956 * Returns %NULL (and &sk_buff does not change) if pull failed
957 * or value of new tail of skb in the case of success.
959 * All the pointers pointing into skb header may change and must be
960 * reloaded after call to this function.
963 /* Moves tail of skb head forward, copying data from fragmented part,
964 * when it is necessary.
965 * 1. It may fail due to malloc failure.
966 * 2. It may change skb pointers.
968 * It is pretty complicated. Luckily, it is called only in exceptional cases.
970 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
972 /* If skb has not enough free space at tail, get new one
973 * plus 128 bytes for future expansions. If we have enough
974 * room at tail, reallocate without expansion only if skb is cloned.
976 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
978 if (eat
> 0 || skb_cloned(skb
)) {
979 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
984 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
987 /* Optimization: no fragments, no reasons to preestimate
988 * size of pulled pages. Superb.
990 if (!skb_shinfo(skb
)->frag_list
)
993 /* Estimate size of pulled pages. */
995 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
996 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
998 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1001 /* If we need update frag list, we are in troubles.
1002 * Certainly, it possible to add an offset to skb data,
1003 * but taking into account that pulling is expected to
1004 * be very rare operation, it is worth to fight against
1005 * further bloating skb head and crucify ourselves here instead.
1006 * Pure masohism, indeed. 8)8)
1009 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1010 struct sk_buff
*clone
= NULL
;
1011 struct sk_buff
*insp
= NULL
;
1016 if (list
->len
<= eat
) {
1017 /* Eaten as whole. */
1022 /* Eaten partially. */
1024 if (skb_shared(list
)) {
1025 /* Sucks! We need to fork list. :-( */
1026 clone
= skb_clone(list
, GFP_ATOMIC
);
1032 /* This may be pulled without
1036 if (!pskb_pull(list
, eat
)) {
1045 /* Free pulled out fragments. */
1046 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1047 skb_shinfo(skb
)->frag_list
= list
->next
;
1050 /* And insert new clone at head. */
1053 skb_shinfo(skb
)->frag_list
= clone
;
1056 /* Success! Now we may commit changes to skb data. */
1061 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1062 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1063 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1064 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1066 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1068 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1069 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1075 skb_shinfo(skb
)->nr_frags
= k
;
1078 skb
->data_len
-= delta
;
1080 return skb_tail_pointer(skb
);
1083 /* Copy some data bits from skb to kernel buffer. */
1085 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1088 int start
= skb_headlen(skb
);
1090 if (offset
> (int)skb
->len
- len
)
1094 if ((copy
= start
- offset
) > 0) {
1097 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1098 if ((len
-= copy
) == 0)
1104 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1107 BUG_TRAP(start
<= offset
+ len
);
1109 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1110 if ((copy
= end
- offset
) > 0) {
1116 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1118 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1119 offset
- start
, copy
);
1120 kunmap_skb_frag(vaddr
);
1122 if ((len
-= copy
) == 0)
1130 if (skb_shinfo(skb
)->frag_list
) {
1131 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1133 for (; list
; list
= list
->next
) {
1136 BUG_TRAP(start
<= offset
+ len
);
1138 end
= start
+ list
->len
;
1139 if ((copy
= end
- offset
) > 0) {
1142 if (skb_copy_bits(list
, offset
- start
,
1145 if ((len
-= copy
) == 0)
1161 * Callback from splice_to_pipe(), if we need to release some pages
1162 * at the end of the spd in case we error'ed out in filling the pipe.
1164 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1166 struct sk_buff
*skb
= (struct sk_buff
*) spd
->partial
[i
].private;
1172 * Fill page/offset/length into spd, if it can hold more pages.
1174 static inline int spd_fill_page(struct splice_pipe_desc
*spd
, struct page
*page
,
1175 unsigned int len
, unsigned int offset
,
1176 struct sk_buff
*skb
)
1178 if (unlikely(spd
->nr_pages
== PIPE_BUFFERS
))
1181 spd
->pages
[spd
->nr_pages
] = page
;
1182 spd
->partial
[spd
->nr_pages
].len
= len
;
1183 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1184 spd
->partial
[spd
->nr_pages
].private = (unsigned long) skb_get(skb
);
1190 * Map linear and fragment data from the skb to spd. Returns number of
1193 static int __skb_splice_bits(struct sk_buff
*skb
, unsigned int *offset
,
1194 unsigned int *total_len
,
1195 struct splice_pipe_desc
*spd
)
1197 unsigned int nr_pages
= spd
->nr_pages
;
1198 unsigned int poff
, plen
, len
, toff
, tlen
;
1207 * if the offset is greater than the linear part, go directly to
1210 headlen
= skb_headlen(skb
);
1211 if (toff
>= headlen
) {
1217 * first map the linear region into the pages/partial map, skipping
1218 * any potential initial offset.
1221 while (len
< headlen
) {
1222 void *p
= skb
->data
+ len
;
1224 poff
= (unsigned long) p
& (PAGE_SIZE
- 1);
1225 plen
= min_t(unsigned int, headlen
- len
, PAGE_SIZE
- poff
);
1238 plen
= min(plen
, tlen
);
1243 * just jump directly to update and return, no point
1244 * in going over fragments when the output is full.
1246 if (spd_fill_page(spd
, virt_to_page(p
), plen
, poff
, skb
))
1253 * then map the fragments
1256 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1257 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1260 poff
= f
->page_offset
;
1272 plen
= min(plen
, tlen
);
1276 if (spd_fill_page(spd
, f
->page
, plen
, poff
, skb
))
1283 if (spd
->nr_pages
- nr_pages
) {
1293 * Map data from the skb to a pipe. Should handle both the linear part,
1294 * the fragments, and the frag list. It does NOT handle frag lists within
1295 * the frag list, if such a thing exists. We'd probably need to recurse to
1296 * handle that cleanly.
1298 int skb_splice_bits(struct sk_buff
*__skb
, unsigned int offset
,
1299 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1302 struct partial_page partial
[PIPE_BUFFERS
];
1303 struct page
*pages
[PIPE_BUFFERS
];
1304 struct splice_pipe_desc spd
= {
1308 .ops
= &sock_pipe_buf_ops
,
1309 .spd_release
= sock_spd_release
,
1311 struct sk_buff
*skb
;
1314 * I'd love to avoid the clone here, but tcp_read_sock()
1315 * ignores reference counts and unconditonally kills the sk_buff
1316 * on return from the actor.
1318 skb
= skb_clone(__skb
, GFP_KERNEL
);
1323 * __skb_splice_bits() only fails if the output has no room left,
1324 * so no point in going over the frag_list for the error case.
1326 if (__skb_splice_bits(skb
, &offset
, &tlen
, &spd
))
1332 * now see if we have a frag_list to map
1334 if (skb_shinfo(skb
)->frag_list
) {
1335 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1337 for (; list
&& tlen
; list
= list
->next
) {
1338 if (__skb_splice_bits(list
, &offset
, &tlen
, &spd
))
1345 * drop our reference to the clone, the pipe consumption will
1354 * Drop the socket lock, otherwise we have reverse
1355 * locking dependencies between sk_lock and i_mutex
1356 * here as compared to sendfile(). We enter here
1357 * with the socket lock held, and splice_to_pipe() will
1358 * grab the pipe inode lock. For sendfile() emulation,
1359 * we call into ->sendpage() with the i_mutex lock held
1360 * and networking will grab the socket lock.
1362 release_sock(__skb
->sk
);
1363 ret
= splice_to_pipe(pipe
, &spd
);
1364 lock_sock(__skb
->sk
);
1372 * skb_store_bits - store bits from kernel buffer to skb
1373 * @skb: destination buffer
1374 * @offset: offset in destination
1375 * @from: source buffer
1376 * @len: number of bytes to copy
1378 * Copy the specified number of bytes from the source buffer to the
1379 * destination skb. This function handles all the messy bits of
1380 * traversing fragment lists and such.
1383 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1386 int start
= skb_headlen(skb
);
1388 if (offset
> (int)skb
->len
- len
)
1391 if ((copy
= start
- offset
) > 0) {
1394 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1395 if ((len
-= copy
) == 0)
1401 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1402 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1405 BUG_TRAP(start
<= offset
+ len
);
1407 end
= start
+ frag
->size
;
1408 if ((copy
= end
- offset
) > 0) {
1414 vaddr
= kmap_skb_frag(frag
);
1415 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1417 kunmap_skb_frag(vaddr
);
1419 if ((len
-= copy
) == 0)
1427 if (skb_shinfo(skb
)->frag_list
) {
1428 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1430 for (; list
; list
= list
->next
) {
1433 BUG_TRAP(start
<= offset
+ len
);
1435 end
= start
+ list
->len
;
1436 if ((copy
= end
- offset
) > 0) {
1439 if (skb_store_bits(list
, offset
- start
,
1442 if ((len
-= copy
) == 0)
1457 EXPORT_SYMBOL(skb_store_bits
);
1459 /* Checksum skb data. */
1461 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1462 int len
, __wsum csum
)
1464 int start
= skb_headlen(skb
);
1465 int i
, copy
= start
- offset
;
1468 /* Checksum header. */
1472 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1473 if ((len
-= copy
) == 0)
1479 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1482 BUG_TRAP(start
<= offset
+ len
);
1484 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1485 if ((copy
= end
- offset
) > 0) {
1488 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1492 vaddr
= kmap_skb_frag(frag
);
1493 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1494 offset
- start
, copy
, 0);
1495 kunmap_skb_frag(vaddr
);
1496 csum
= csum_block_add(csum
, csum2
, pos
);
1505 if (skb_shinfo(skb
)->frag_list
) {
1506 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1508 for (; list
; list
= list
->next
) {
1511 BUG_TRAP(start
<= offset
+ len
);
1513 end
= start
+ list
->len
;
1514 if ((copy
= end
- offset
) > 0) {
1518 csum2
= skb_checksum(list
, offset
- start
,
1520 csum
= csum_block_add(csum
, csum2
, pos
);
1521 if ((len
-= copy
) == 0)
1534 /* Both of above in one bottle. */
1536 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1537 u8
*to
, int len
, __wsum csum
)
1539 int start
= skb_headlen(skb
);
1540 int i
, copy
= start
- offset
;
1547 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1549 if ((len
-= copy
) == 0)
1556 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1559 BUG_TRAP(start
<= offset
+ len
);
1561 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1562 if ((copy
= end
- offset
) > 0) {
1565 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1569 vaddr
= kmap_skb_frag(frag
);
1570 csum2
= csum_partial_copy_nocheck(vaddr
+
1574 kunmap_skb_frag(vaddr
);
1575 csum
= csum_block_add(csum
, csum2
, pos
);
1585 if (skb_shinfo(skb
)->frag_list
) {
1586 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1588 for (; list
; list
= list
->next
) {
1592 BUG_TRAP(start
<= offset
+ len
);
1594 end
= start
+ list
->len
;
1595 if ((copy
= end
- offset
) > 0) {
1598 csum2
= skb_copy_and_csum_bits(list
,
1601 csum
= csum_block_add(csum
, csum2
, pos
);
1602 if ((len
-= copy
) == 0)
1615 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1620 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1621 csstart
= skb
->csum_start
- skb_headroom(skb
);
1623 csstart
= skb_headlen(skb
);
1625 BUG_ON(csstart
> skb_headlen(skb
));
1627 skb_copy_from_linear_data(skb
, to
, csstart
);
1630 if (csstart
!= skb
->len
)
1631 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1632 skb
->len
- csstart
, 0);
1634 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1635 long csstuff
= csstart
+ skb
->csum_offset
;
1637 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1642 * skb_dequeue - remove from the head of the queue
1643 * @list: list to dequeue from
1645 * Remove the head of the list. The list lock is taken so the function
1646 * may be used safely with other locking list functions. The head item is
1647 * returned or %NULL if the list is empty.
1650 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1652 unsigned long flags
;
1653 struct sk_buff
*result
;
1655 spin_lock_irqsave(&list
->lock
, flags
);
1656 result
= __skb_dequeue(list
);
1657 spin_unlock_irqrestore(&list
->lock
, flags
);
1662 * skb_dequeue_tail - remove from the tail of the queue
1663 * @list: list to dequeue from
1665 * Remove the tail of the list. The list lock is taken so the function
1666 * may be used safely with other locking list functions. The tail item is
1667 * returned or %NULL if the list is empty.
1669 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1671 unsigned long flags
;
1672 struct sk_buff
*result
;
1674 spin_lock_irqsave(&list
->lock
, flags
);
1675 result
= __skb_dequeue_tail(list
);
1676 spin_unlock_irqrestore(&list
->lock
, flags
);
1681 * skb_queue_purge - empty a list
1682 * @list: list to empty
1684 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1685 * the list and one reference dropped. This function takes the list
1686 * lock and is atomic with respect to other list locking functions.
1688 void skb_queue_purge(struct sk_buff_head
*list
)
1690 struct sk_buff
*skb
;
1691 while ((skb
= skb_dequeue(list
)) != NULL
)
1696 * skb_queue_head - queue a buffer at the list head
1697 * @list: list to use
1698 * @newsk: buffer to queue
1700 * Queue a buffer at the start of the list. This function takes the
1701 * list lock and can be used safely with other locking &sk_buff functions
1704 * A buffer cannot be placed on two lists at the same time.
1706 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1708 unsigned long flags
;
1710 spin_lock_irqsave(&list
->lock
, flags
);
1711 __skb_queue_head(list
, newsk
);
1712 spin_unlock_irqrestore(&list
->lock
, flags
);
1716 * skb_queue_tail - queue a buffer at the list tail
1717 * @list: list to use
1718 * @newsk: buffer to queue
1720 * Queue a buffer at the tail of the list. This function takes the
1721 * list lock and can be used safely with other locking &sk_buff functions
1724 * A buffer cannot be placed on two lists at the same time.
1726 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1728 unsigned long flags
;
1730 spin_lock_irqsave(&list
->lock
, flags
);
1731 __skb_queue_tail(list
, newsk
);
1732 spin_unlock_irqrestore(&list
->lock
, flags
);
1736 * skb_unlink - remove a buffer from a list
1737 * @skb: buffer to remove
1738 * @list: list to use
1740 * Remove a packet from a list. The list locks are taken and this
1741 * function is atomic with respect to other list locked calls
1743 * You must know what list the SKB is on.
1745 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1747 unsigned long flags
;
1749 spin_lock_irqsave(&list
->lock
, flags
);
1750 __skb_unlink(skb
, list
);
1751 spin_unlock_irqrestore(&list
->lock
, flags
);
1755 * skb_append - append a buffer
1756 * @old: buffer to insert after
1757 * @newsk: buffer to insert
1758 * @list: list to use
1760 * Place a packet after a given packet in a list. The list locks are taken
1761 * and this function is atomic with respect to other list locked calls.
1762 * A buffer cannot be placed on two lists at the same time.
1764 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1766 unsigned long flags
;
1768 spin_lock_irqsave(&list
->lock
, flags
);
1769 __skb_append(old
, newsk
, list
);
1770 spin_unlock_irqrestore(&list
->lock
, flags
);
1775 * skb_insert - insert a buffer
1776 * @old: buffer to insert before
1777 * @newsk: buffer to insert
1778 * @list: list to use
1780 * Place a packet before a given packet in a list. The list locks are
1781 * taken and this function is atomic with respect to other list locked
1784 * A buffer cannot be placed on two lists at the same time.
1786 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1788 unsigned long flags
;
1790 spin_lock_irqsave(&list
->lock
, flags
);
1791 __skb_insert(newsk
, old
->prev
, old
, list
);
1792 spin_unlock_irqrestore(&list
->lock
, flags
);
1795 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1796 struct sk_buff
* skb1
,
1797 const u32 len
, const int pos
)
1801 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1803 /* And move data appendix as is. */
1804 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1805 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1807 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1808 skb_shinfo(skb
)->nr_frags
= 0;
1809 skb1
->data_len
= skb
->data_len
;
1810 skb1
->len
+= skb1
->data_len
;
1813 skb_set_tail_pointer(skb
, len
);
1816 static inline void skb_split_no_header(struct sk_buff
*skb
,
1817 struct sk_buff
* skb1
,
1818 const u32 len
, int pos
)
1821 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1823 skb_shinfo(skb
)->nr_frags
= 0;
1824 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1826 skb
->data_len
= len
- pos
;
1828 for (i
= 0; i
< nfrags
; i
++) {
1829 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1831 if (pos
+ size
> len
) {
1832 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1836 * We have two variants in this case:
1837 * 1. Move all the frag to the second
1838 * part, if it is possible. F.e.
1839 * this approach is mandatory for TUX,
1840 * where splitting is expensive.
1841 * 2. Split is accurately. We make this.
1843 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1844 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1845 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1846 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1847 skb_shinfo(skb
)->nr_frags
++;
1851 skb_shinfo(skb
)->nr_frags
++;
1854 skb_shinfo(skb1
)->nr_frags
= k
;
1858 * skb_split - Split fragmented skb to two parts at length len.
1859 * @skb: the buffer to split
1860 * @skb1: the buffer to receive the second part
1861 * @len: new length for skb
1863 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1865 int pos
= skb_headlen(skb
);
1867 if (len
< pos
) /* Split line is inside header. */
1868 skb_split_inside_header(skb
, skb1
, len
, pos
);
1869 else /* Second chunk has no header, nothing to copy. */
1870 skb_split_no_header(skb
, skb1
, len
, pos
);
1874 * skb_prepare_seq_read - Prepare a sequential read of skb data
1875 * @skb: the buffer to read
1876 * @from: lower offset of data to be read
1877 * @to: upper offset of data to be read
1878 * @st: state variable
1880 * Initializes the specified state variable. Must be called before
1881 * invoking skb_seq_read() for the first time.
1883 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1884 unsigned int to
, struct skb_seq_state
*st
)
1886 st
->lower_offset
= from
;
1887 st
->upper_offset
= to
;
1888 st
->root_skb
= st
->cur_skb
= skb
;
1889 st
->frag_idx
= st
->stepped_offset
= 0;
1890 st
->frag_data
= NULL
;
1894 * skb_seq_read - Sequentially read skb data
1895 * @consumed: number of bytes consumed by the caller so far
1896 * @data: destination pointer for data to be returned
1897 * @st: state variable
1899 * Reads a block of skb data at &consumed relative to the
1900 * lower offset specified to skb_prepare_seq_read(). Assigns
1901 * the head of the data block to &data and returns the length
1902 * of the block or 0 if the end of the skb data or the upper
1903 * offset has been reached.
1905 * The caller is not required to consume all of the data
1906 * returned, i.e. &consumed is typically set to the number
1907 * of bytes already consumed and the next call to
1908 * skb_seq_read() will return the remaining part of the block.
1910 * Note: The size of each block of data returned can be arbitary,
1911 * this limitation is the cost for zerocopy seqeuental
1912 * reads of potentially non linear data.
1914 * Note: Fragment lists within fragments are not implemented
1915 * at the moment, state->root_skb could be replaced with
1916 * a stack for this purpose.
1918 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1919 struct skb_seq_state
*st
)
1921 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1924 if (unlikely(abs_offset
>= st
->upper_offset
))
1928 block_limit
= skb_headlen(st
->cur_skb
);
1930 if (abs_offset
< block_limit
) {
1931 *data
= st
->cur_skb
->data
+ abs_offset
;
1932 return block_limit
- abs_offset
;
1935 if (st
->frag_idx
== 0 && !st
->frag_data
)
1936 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1938 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1939 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1940 block_limit
= frag
->size
+ st
->stepped_offset
;
1942 if (abs_offset
< block_limit
) {
1944 st
->frag_data
= kmap_skb_frag(frag
);
1946 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1947 (abs_offset
- st
->stepped_offset
);
1949 return block_limit
- abs_offset
;
1952 if (st
->frag_data
) {
1953 kunmap_skb_frag(st
->frag_data
);
1954 st
->frag_data
= NULL
;
1958 st
->stepped_offset
+= frag
->size
;
1961 if (st
->frag_data
) {
1962 kunmap_skb_frag(st
->frag_data
);
1963 st
->frag_data
= NULL
;
1966 if (st
->cur_skb
->next
) {
1967 st
->cur_skb
= st
->cur_skb
->next
;
1970 } else if (st
->root_skb
== st
->cur_skb
&&
1971 skb_shinfo(st
->root_skb
)->frag_list
) {
1972 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
1980 * skb_abort_seq_read - Abort a sequential read of skb data
1981 * @st: state variable
1983 * Must be called if skb_seq_read() was not called until it
1986 void skb_abort_seq_read(struct skb_seq_state
*st
)
1989 kunmap_skb_frag(st
->frag_data
);
1992 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1994 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
1995 struct ts_config
*conf
,
1996 struct ts_state
*state
)
1998 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2001 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2003 skb_abort_seq_read(TS_SKB_CB(state
));
2007 * skb_find_text - Find a text pattern in skb data
2008 * @skb: the buffer to look in
2009 * @from: search offset
2011 * @config: textsearch configuration
2012 * @state: uninitialized textsearch state variable
2014 * Finds a pattern in the skb data according to the specified
2015 * textsearch configuration. Use textsearch_next() to retrieve
2016 * subsequent occurrences of the pattern. Returns the offset
2017 * to the first occurrence or UINT_MAX if no match was found.
2019 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2020 unsigned int to
, struct ts_config
*config
,
2021 struct ts_state
*state
)
2025 config
->get_next_block
= skb_ts_get_next_block
;
2026 config
->finish
= skb_ts_finish
;
2028 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2030 ret
= textsearch_find(config
, state
);
2031 return (ret
<= to
- from
? ret
: UINT_MAX
);
2035 * skb_append_datato_frags: - append the user data to a skb
2036 * @sk: sock structure
2037 * @skb: skb structure to be appened with user data.
2038 * @getfrag: call back function to be used for getting the user data
2039 * @from: pointer to user message iov
2040 * @length: length of the iov message
2042 * Description: This procedure append the user data in the fragment part
2043 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2045 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2046 int (*getfrag
)(void *from
, char *to
, int offset
,
2047 int len
, int odd
, struct sk_buff
*skb
),
2048 void *from
, int length
)
2051 skb_frag_t
*frag
= NULL
;
2052 struct page
*page
= NULL
;
2058 /* Return error if we don't have space for new frag */
2059 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2060 if (frg_cnt
>= MAX_SKB_FRAGS
)
2063 /* allocate a new page for next frag */
2064 page
= alloc_pages(sk
->sk_allocation
, 0);
2066 /* If alloc_page fails just return failure and caller will
2067 * free previous allocated pages by doing kfree_skb()
2072 /* initialize the next frag */
2073 sk
->sk_sndmsg_page
= page
;
2074 sk
->sk_sndmsg_off
= 0;
2075 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2076 skb
->truesize
+= PAGE_SIZE
;
2077 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2079 /* get the new initialized frag */
2080 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2081 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2083 /* copy the user data to page */
2084 left
= PAGE_SIZE
- frag
->page_offset
;
2085 copy
= (length
> left
)? left
: length
;
2087 ret
= getfrag(from
, (page_address(frag
->page
) +
2088 frag
->page_offset
+ frag
->size
),
2089 offset
, copy
, 0, skb
);
2093 /* copy was successful so update the size parameters */
2094 sk
->sk_sndmsg_off
+= copy
;
2097 skb
->data_len
+= copy
;
2101 } while (length
> 0);
2107 * skb_pull_rcsum - pull skb and update receive checksum
2108 * @skb: buffer to update
2109 * @start: start of data before pull
2110 * @len: length of data pulled
2112 * This function performs an skb_pull on the packet and updates
2113 * update the CHECKSUM_COMPLETE checksum. It should be used on
2114 * receive path processing instead of skb_pull unless you know
2115 * that the checksum difference is zero (e.g., a valid IP header)
2116 * or you are setting ip_summed to CHECKSUM_NONE.
2118 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2120 BUG_ON(len
> skb
->len
);
2122 BUG_ON(skb
->len
< skb
->data_len
);
2123 skb_postpull_rcsum(skb
, skb
->data
, len
);
2124 return skb
->data
+= len
;
2127 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2130 * skb_segment - Perform protocol segmentation on skb.
2131 * @skb: buffer to segment
2132 * @features: features for the output path (see dev->features)
2134 * This function performs segmentation on the given skb. It returns
2135 * the segment at the given position. It returns NULL if there are
2136 * no more segments to generate, or when an error is encountered.
2138 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
2140 struct sk_buff
*segs
= NULL
;
2141 struct sk_buff
*tail
= NULL
;
2142 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2143 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2144 unsigned int offset
= doffset
;
2145 unsigned int headroom
;
2147 int sg
= features
& NETIF_F_SG
;
2148 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2153 __skb_push(skb
, doffset
);
2154 headroom
= skb_headroom(skb
);
2155 pos
= skb_headlen(skb
);
2158 struct sk_buff
*nskb
;
2164 len
= skb
->len
- offset
;
2168 hsize
= skb_headlen(skb
) - offset
;
2171 if (hsize
> len
|| !sg
)
2174 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
2175 if (unlikely(!nskb
))
2184 nskb
->dev
= skb
->dev
;
2185 skb_copy_queue_mapping(nskb
, skb
);
2186 nskb
->priority
= skb
->priority
;
2187 nskb
->protocol
= skb
->protocol
;
2188 nskb
->dst
= dst_clone(skb
->dst
);
2189 memcpy(nskb
->cb
, skb
->cb
, sizeof(skb
->cb
));
2190 nskb
->pkt_type
= skb
->pkt_type
;
2191 nskb
->mac_len
= skb
->mac_len
;
2193 skb_reserve(nskb
, headroom
);
2194 skb_reset_mac_header(nskb
);
2195 skb_set_network_header(nskb
, skb
->mac_len
);
2196 nskb
->transport_header
= (nskb
->network_header
+
2197 skb_network_header_len(skb
));
2198 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
2201 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2207 frag
= skb_shinfo(nskb
)->frags
;
2210 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
2211 nskb
->csum
= skb
->csum
;
2212 skb_copy_from_linear_data_offset(skb
, offset
,
2213 skb_put(nskb
, hsize
), hsize
);
2215 while (pos
< offset
+ len
) {
2216 BUG_ON(i
>= nfrags
);
2218 *frag
= skb_shinfo(skb
)->frags
[i
];
2219 get_page(frag
->page
);
2223 frag
->page_offset
+= offset
- pos
;
2224 frag
->size
-= offset
- pos
;
2229 if (pos
+ size
<= offset
+ len
) {
2233 frag
->size
-= pos
+ size
- (offset
+ len
);
2240 skb_shinfo(nskb
)->nr_frags
= k
;
2241 nskb
->data_len
= len
- hsize
;
2242 nskb
->len
+= nskb
->data_len
;
2243 nskb
->truesize
+= nskb
->data_len
;
2244 } while ((offset
+= len
) < skb
->len
);
2249 while ((skb
= segs
)) {
2253 return ERR_PTR(err
);
2256 EXPORT_SYMBOL_GPL(skb_segment
);
2258 void __init
skb_init(void)
2260 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2261 sizeof(struct sk_buff
),
2263 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2265 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2266 (2*sizeof(struct sk_buff
)) +
2269 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2274 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2275 * @skb: Socket buffer containing the buffers to be mapped
2276 * @sg: The scatter-gather list to map into
2277 * @offset: The offset into the buffer's contents to start mapping
2278 * @len: Length of buffer space to be mapped
2280 * Fill the specified scatter-gather list with mappings/pointers into a
2281 * region of the buffer space attached to a socket buffer.
2284 __skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2286 int start
= skb_headlen(skb
);
2287 int i
, copy
= start
- offset
;
2293 sg_set_buf(sg
, skb
->data
+ offset
, copy
);
2295 if ((len
-= copy
) == 0)
2300 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2303 BUG_TRAP(start
<= offset
+ len
);
2305 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2306 if ((copy
= end
- offset
) > 0) {
2307 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2311 sg_set_page(&sg
[elt
], frag
->page
, copy
,
2312 frag
->page_offset
+offset
-start
);
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
,
2335 if ((len
-= copy
) == 0)
2346 int skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2348 int nsg
= __skb_to_sgvec(skb
, sg
, offset
, len
);
2350 sg_mark_end(&sg
[nsg
- 1]);
2356 * skb_cow_data - Check that a socket buffer's data buffers are writable
2357 * @skb: The socket buffer to check.
2358 * @tailbits: Amount of trailing space to be added
2359 * @trailer: Returned pointer to the skb where the @tailbits space begins
2361 * Make sure that the data buffers attached to a socket buffer are
2362 * writable. If they are not, private copies are made of the data buffers
2363 * and the socket buffer is set to use these instead.
2365 * If @tailbits is given, make sure that there is space to write @tailbits
2366 * bytes of data beyond current end of socket buffer. @trailer will be
2367 * set to point to the skb in which this space begins.
2369 * The number of scatterlist elements required to completely map the
2370 * COW'd and extended socket buffer will be returned.
2372 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2376 struct sk_buff
*skb1
, **skb_p
;
2378 /* If skb is cloned or its head is paged, reallocate
2379 * head pulling out all the pages (pages are considered not writable
2380 * at the moment even if they are anonymous).
2382 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2383 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2386 /* Easy case. Most of packets will go this way. */
2387 if (!skb_shinfo(skb
)->frag_list
) {
2388 /* A little of trouble, not enough of space for trailer.
2389 * This should not happen, when stack is tuned to generate
2390 * good frames. OK, on miss we reallocate and reserve even more
2391 * space, 128 bytes is fair. */
2393 if (skb_tailroom(skb
) < tailbits
&&
2394 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2402 /* Misery. We are in troubles, going to mincer fragments... */
2405 skb_p
= &skb_shinfo(skb
)->frag_list
;
2408 while ((skb1
= *skb_p
) != NULL
) {
2411 /* The fragment is partially pulled by someone,
2412 * this can happen on input. Copy it and everything
2415 if (skb_shared(skb1
))
2418 /* If the skb is the last, worry about trailer. */
2420 if (skb1
->next
== NULL
&& tailbits
) {
2421 if (skb_shinfo(skb1
)->nr_frags
||
2422 skb_shinfo(skb1
)->frag_list
||
2423 skb_tailroom(skb1
) < tailbits
)
2424 ntail
= tailbits
+ 128;
2430 skb_shinfo(skb1
)->nr_frags
||
2431 skb_shinfo(skb1
)->frag_list
) {
2432 struct sk_buff
*skb2
;
2434 /* Fuck, we are miserable poor guys... */
2436 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2438 skb2
= skb_copy_expand(skb1
,
2442 if (unlikely(skb2
== NULL
))
2446 skb_set_owner_w(skb2
, skb1
->sk
);
2448 /* Looking around. Are we still alive?
2449 * OK, link new skb, drop old one */
2451 skb2
->next
= skb1
->next
;
2458 skb_p
= &skb1
->next
;
2464 EXPORT_SYMBOL(___pskb_trim
);
2465 EXPORT_SYMBOL(__kfree_skb
);
2466 EXPORT_SYMBOL(kfree_skb
);
2467 EXPORT_SYMBOL(__pskb_pull_tail
);
2468 EXPORT_SYMBOL(__alloc_skb
);
2469 EXPORT_SYMBOL(__netdev_alloc_skb
);
2470 EXPORT_SYMBOL(pskb_copy
);
2471 EXPORT_SYMBOL(pskb_expand_head
);
2472 EXPORT_SYMBOL(skb_checksum
);
2473 EXPORT_SYMBOL(skb_clone
);
2474 EXPORT_SYMBOL(skb_copy
);
2475 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2476 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2477 EXPORT_SYMBOL(skb_copy_bits
);
2478 EXPORT_SYMBOL(skb_copy_expand
);
2479 EXPORT_SYMBOL(skb_over_panic
);
2480 EXPORT_SYMBOL(skb_pad
);
2481 EXPORT_SYMBOL(skb_realloc_headroom
);
2482 EXPORT_SYMBOL(skb_under_panic
);
2483 EXPORT_SYMBOL(skb_dequeue
);
2484 EXPORT_SYMBOL(skb_dequeue_tail
);
2485 EXPORT_SYMBOL(skb_insert
);
2486 EXPORT_SYMBOL(skb_queue_purge
);
2487 EXPORT_SYMBOL(skb_queue_head
);
2488 EXPORT_SYMBOL(skb_queue_tail
);
2489 EXPORT_SYMBOL(skb_unlink
);
2490 EXPORT_SYMBOL(skb_append
);
2491 EXPORT_SYMBOL(skb_split
);
2492 EXPORT_SYMBOL(skb_prepare_seq_read
);
2493 EXPORT_SYMBOL(skb_seq_read
);
2494 EXPORT_SYMBOL(skb_abort_seq_read
);
2495 EXPORT_SYMBOL(skb_find_text
);
2496 EXPORT_SYMBOL(skb_append_datato_frags
);
2498 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2499 EXPORT_SYMBOL_GPL(skb_cow_data
);