2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
10 * Alan Cox : Fixed the worst of the load
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
38 * The functions in this file will not compile correctly with gcc 2.4.x
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
45 #include <linux/interrupt.h>
47 #include <linux/inet.h>
48 #include <linux/slab.h>
49 #include <linux/netdevice.h>
50 #ifdef CONFIG_NET_CLS_ACT
51 #include <net/pkt_sched.h>
53 #include <linux/string.h>
54 #include <linux/skbuff.h>
55 #include <linux/splice.h>
56 #include <linux/cache.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/init.h>
59 #include <linux/scatterlist.h>
61 #include <net/protocol.h>
64 #include <net/checksum.h>
67 #include <asm/uaccess.h>
68 #include <asm/system.h>
72 static struct kmem_cache
*skbuff_head_cache __read_mostly
;
73 static struct kmem_cache
*skbuff_fclone_cache __read_mostly
;
75 static void sock_pipe_buf_release(struct pipe_inode_info
*pipe
,
76 struct pipe_buffer
*buf
)
78 struct sk_buff
*skb
= (struct sk_buff
*) buf
->private;
83 static void sock_pipe_buf_get(struct pipe_inode_info
*pipe
,
84 struct pipe_buffer
*buf
)
86 struct sk_buff
*skb
= (struct sk_buff
*) buf
->private;
91 static int sock_pipe_buf_steal(struct pipe_inode_info
*pipe
,
92 struct pipe_buffer
*buf
)
98 /* Pipe buffer operations for a socket. */
99 static struct pipe_buf_operations sock_pipe_buf_ops
= {
101 .map
= generic_pipe_buf_map
,
102 .unmap
= generic_pipe_buf_unmap
,
103 .confirm
= generic_pipe_buf_confirm
,
104 .release
= sock_pipe_buf_release
,
105 .steal
= sock_pipe_buf_steal
,
106 .get
= sock_pipe_buf_get
,
110 * Keep out-of-line to prevent kernel bloat.
111 * __builtin_return_address is not used because it is not always
116 * skb_over_panic - private function
121 * Out of line support code for skb_put(). Not user callable.
123 void skb_over_panic(struct sk_buff
*skb
, int sz
, void *here
)
125 printk(KERN_EMERG
"skb_over_panic: text:%p len:%d put:%d head:%p "
126 "data:%p tail:%#lx end:%#lx dev:%s\n",
127 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
128 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
129 skb
->dev
? skb
->dev
->name
: "<NULL>");
134 * skb_under_panic - private function
139 * Out of line support code for skb_push(). Not user callable.
142 void skb_under_panic(struct sk_buff
*skb
, int sz
, void *here
)
144 printk(KERN_EMERG
"skb_under_panic: text:%p len:%d put:%d head:%p "
145 "data:%p tail:%#lx end:%#lx dev:%s\n",
146 here
, skb
->len
, sz
, skb
->head
, skb
->data
,
147 (unsigned long)skb
->tail
, (unsigned long)skb
->end
,
148 skb
->dev
? skb
->dev
->name
: "<NULL>");
152 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
153 * 'private' fields and also do memory statistics to find all the
159 * __alloc_skb - allocate a network buffer
160 * @size: size to allocate
161 * @gfp_mask: allocation mask
162 * @fclone: allocate from fclone cache instead of head cache
163 * and allocate a cloned (child) skb
164 * @node: numa node to allocate memory on
166 * Allocate a new &sk_buff. The returned buffer has no headroom and a
167 * tail room of size bytes. The object has a reference count of one.
168 * The return is the buffer. On a failure the return is %NULL.
170 * Buffers may only be allocated from interrupts using a @gfp_mask of
173 struct sk_buff
*__alloc_skb(unsigned int size
, gfp_t gfp_mask
,
174 int fclone
, int node
)
176 struct kmem_cache
*cache
;
177 struct skb_shared_info
*shinfo
;
181 cache
= fclone
? skbuff_fclone_cache
: skbuff_head_cache
;
184 skb
= kmem_cache_alloc_node(cache
, gfp_mask
& ~__GFP_DMA
, node
);
189 size
= SKB_DATA_ALIGN(size
);
190 data
= kmalloc_node_track_caller(size
+ sizeof(struct skb_shared_info
),
194 prefetchw(data
+ size
);
197 * See comment in sk_buff definition, just before the 'tail' member
199 memset(skb
, 0, offsetof(struct sk_buff
, tail
));
200 skb
->truesize
= size
+ sizeof(struct sk_buff
);
201 atomic_set(&skb
->users
, 1);
204 skb_reset_tail_pointer(skb
);
205 skb
->end
= skb
->tail
+ size
;
206 #ifdef NET_SKBUFF_DATA_USES_OFFSET
207 skb
->mac_header
= ~0U;
210 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
211 skb
->nfct_reasm
= NULL
;
219 #ifdef CONFIG_BRIDGE_NETFILTER
220 skb
->nf_bridge
= NULL
;
222 #ifdef CONFIG_NET_SCHED
224 #ifdef CONFIG_NET_CLS_ACT
228 #ifdef CONFIG_NET_DMA
229 memset(&skb
->dma_cookie
, 0, sizeof(dma_cookie_t
));
231 #ifdef CONFIG_NETWORK_SECMARK
235 /* make sure we initialize shinfo sequentially */
236 shinfo
= skb_shinfo(skb
);
237 memset(shinfo
, 0, offsetof(struct skb_shared_info
, frags
));
238 atomic_set(&shinfo
->dataref
, 1);
240 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
246 struct sk_buff
*child
= skb
+ 1;
247 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
249 skb
->fclone
= SKB_FCLONE_ORIG
;
250 atomic_set(fclone_ref
, 1);
252 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
257 kmem_cache_free(cache
, skb
);
263 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
264 * @dev: network device to receive on
265 * @length: length to allocate
266 * @gfp_mask: get_free_pages mask, passed to alloc_skb
268 * Allocate a new &sk_buff and assign it a usage count of one. The
269 * buffer has unspecified headroom built in. Users should allocate
270 * the headroom they think they need without accounting for the
271 * built in space. The built in space is used for optimisations.
273 * %NULL is returned if there is no free memory.
275 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
276 unsigned int length
, gfp_t gfp_mask
)
278 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
281 skb
= __alloc_skb(length
+ NET_SKB_PAD_ALLOC
, gfp_mask
, 0, node
);
283 skb_reserve(skb
, NET_SKB_PAD_ALLOC
);
289 static void skb_drop_list(struct sk_buff
**listp
)
291 struct sk_buff
*list
= *listp
;
296 struct sk_buff
*this = list
;
302 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
304 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
307 static void skb_clone_fraglist(struct sk_buff
*skb
)
309 struct sk_buff
*list
;
311 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
315 static void skb_release_data(struct sk_buff
*skb
)
318 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
319 &skb_shinfo(skb
)->dataref
)) {
320 if (skb_shinfo(skb
)->nr_frags
) {
322 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
323 put_page(skb_shinfo(skb
)->frags
[i
].page
);
326 if (skb_shinfo(skb
)->frag_list
)
327 skb_drop_fraglist(skb
);
334 * Free an skbuff by memory without cleaning the state.
336 static void kfree_skbmem(struct sk_buff
*skb
)
338 struct sk_buff
*other
;
339 atomic_t
*fclone_ref
;
341 switch (skb
->fclone
) {
342 case SKB_FCLONE_UNAVAILABLE
:
343 kmem_cache_free(skbuff_head_cache
, skb
);
346 case SKB_FCLONE_ORIG
:
347 fclone_ref
= (atomic_t
*) (skb
+ 2);
348 if (atomic_dec_and_test(fclone_ref
))
349 kmem_cache_free(skbuff_fclone_cache
, skb
);
352 case SKB_FCLONE_CLONE
:
353 fclone_ref
= (atomic_t
*) (skb
+ 1);
356 /* The clone portion is available for
357 * fast-cloning again.
359 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
361 if (atomic_dec_and_test(fclone_ref
))
362 kmem_cache_free(skbuff_fclone_cache
, other
);
367 /* Free everything but the sk_buff shell. */
368 static void skb_release_all(struct sk_buff
*skb
)
370 dst_release(skb
->dst
);
372 secpath_put(skb
->sp
);
374 if (skb
->destructor
) {
375 #ifdef CONFIG_NETCORE_DEBUG
378 skb
->destructor(skb
);
380 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
381 nf_conntrack_put(skb
->nfct
);
382 nf_conntrack_put_reasm(skb
->nfct_reasm
);
384 #ifdef CONFIG_BRIDGE_NETFILTER
385 nf_bridge_put(skb
->nf_bridge
);
387 /* XXX: IS this still necessary? - JHS */
388 #ifdef CONFIG_NET_SCHED
390 #ifdef CONFIG_NET_CLS_ACT
394 skb_release_data(skb
);
398 * __kfree_skb - private function
401 * Free an sk_buff. Release anything attached to the buffer.
402 * Clean the state. This is an internal helper function. Users should
403 * always call kfree_skb
406 void __kfree_skb(struct sk_buff
*skb
)
408 skb_release_all(skb
);
413 * kfree_skb - free an sk_buff
414 * @skb: buffer to free
416 * Drop a reference to the buffer and free it if the usage count has
419 void kfree_skb(struct sk_buff
*skb
)
423 if (likely(atomic_read(&skb
->users
) == 1))
425 else if (likely(!atomic_dec_and_test(&skb
->users
)))
430 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
432 new->tstamp
= old
->tstamp
;
434 new->transport_header
= old
->transport_header
;
435 new->network_header
= old
->network_header
;
436 new->mac_header
= old
->mac_header
;
437 new->dst
= dst_clone(old
->dst
);
439 new->sp
= secpath_get(old
->sp
);
441 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
442 new->csum
= old
->csum
;
443 new->local_df
= old
->local_df
;
444 new->pkt_type
= old
->pkt_type
;
445 new->ip_summed
= old
->ip_summed
;
446 new->priority
= old
->priority
;
447 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
448 new->ipvs_property
= old
->ipvs_property
;
450 new->protocol
= old
->protocol
;
451 new->mark
= old
->mark
;
454 #ifdef CONFIG_NET_SCHED
455 new->tc_index
= old
->tc_index
;
456 #ifdef CONFIG_NET_CLS_ACT
457 new->tc_verd
= old
->tc_verd
;
460 new->vlan_tci
= old
->vlan_tci
;
462 skb_copy_secmark(new, old
);
463 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
464 new->nfcache
= old
->nfcache
;
469 * You should not add any new code to this function. Add it to
470 * __copy_skb_header above instead.
472 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
474 #define C(x) n->x = skb->x
476 n
->next
= n
->prev
= NULL
;
478 __copy_skb_header(n
, skb
);
484 C(ctf_mac_len
); /* used by Broadcom CTF driver! */
486 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
489 n
->destructor
= NULL
;
495 atomic_set(&n
->users
, 1);
497 atomic_inc(&(skb_shinfo(skb
)->dataref
));
505 * skb_morph - morph one skb into another
506 * @dst: the skb to receive the contents
507 * @src: the skb to supply the contents
509 * This is identical to skb_clone except that the target skb is
510 * supplied by the user.
512 * The target skb is returned upon exit.
514 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
516 skb_release_all(dst
);
517 return __skb_clone(dst
, src
);
519 EXPORT_SYMBOL_GPL(skb_morph
);
522 * skb_clone - duplicate an sk_buff
523 * @skb: buffer to clone
524 * @gfp_mask: allocation priority
526 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
527 * copies share the same packet data but not structure. The new
528 * buffer has a reference count of 1. If the allocation fails the
529 * function returns %NULL otherwise the new buffer is returned.
531 * If this function is called from an interrupt gfp_mask() must be
535 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
540 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
541 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
542 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
543 n
->fclone
= SKB_FCLONE_CLONE
;
544 atomic_inc(fclone_ref
);
546 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
549 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
552 return __skb_clone(n
, skb
);
555 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
557 #ifndef NET_SKBUFF_DATA_USES_OFFSET
559 * Shift between the two data areas in bytes
561 unsigned long offset
= new->data
- old
->data
;
564 __copy_skb_header(new, old
);
566 #ifndef NET_SKBUFF_DATA_USES_OFFSET
567 /* {transport,network,mac}_header are relative to skb->head */
568 new->transport_header
+= offset
;
569 new->network_header
+= offset
;
570 if (skb_mac_header_was_set(new))
571 new->mac_header
+= offset
;
573 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
574 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
575 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
579 * skb_copy - create private copy of an sk_buff
580 * @skb: buffer to copy
581 * @gfp_mask: allocation priority
583 * Make a copy of both an &sk_buff and its data. This is used when the
584 * caller wishes to modify the data and needs a private copy of the
585 * data to alter. Returns %NULL on failure or the pointer to the buffer
586 * on success. The returned buffer has a reference count of 1.
588 * As by-product this function converts non-linear &sk_buff to linear
589 * one, so that &sk_buff becomes completely private and caller is allowed
590 * to modify all the data of returned buffer. This means that this
591 * function is not recommended for use in circumstances when only
592 * header is going to be modified. Use pskb_copy() instead.
595 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
597 int headerlen
= skb
->data
- skb
->head
;
599 * Allocate the copy buffer
602 #ifdef NET_SKBUFF_DATA_USES_OFFSET
603 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
605 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
610 /* Set the data pointer */
611 skb_reserve(n
, headerlen
);
612 /* Set the tail pointer and length */
613 skb_put(n
, skb
->len
);
615 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
618 copy_skb_header(n
, skb
);
624 * pskb_copy - create copy of an sk_buff with private head.
625 * @skb: buffer to copy
626 * @gfp_mask: allocation priority
628 * Make a copy of both an &sk_buff and part of its data, located
629 * in header. Fragmented data remain shared. This is used when
630 * the caller wishes to modify only header of &sk_buff and needs
631 * private copy of the header to alter. Returns %NULL on failure
632 * or the pointer to the buffer on success.
633 * The returned buffer has a reference count of 1.
636 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
639 * Allocate the copy buffer
642 #ifdef NET_SKBUFF_DATA_USES_OFFSET
643 n
= alloc_skb(skb
->end
, gfp_mask
);
645 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
650 /* Set the data pointer */
651 skb_reserve(n
, skb
->data
- skb
->head
);
652 /* Set the tail pointer and length */
653 skb_put(n
, skb_headlen(skb
));
655 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
657 n
->truesize
+= skb
->data_len
;
658 n
->data_len
= skb
->data_len
;
661 if (skb_shinfo(skb
)->nr_frags
) {
664 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
665 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
666 get_page(skb_shinfo(n
)->frags
[i
].page
);
668 skb_shinfo(n
)->nr_frags
= i
;
671 if (skb_shinfo(skb
)->frag_list
) {
672 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
673 skb_clone_fraglist(n
);
676 copy_skb_header(n
, skb
);
682 * pskb_expand_head - reallocate header of &sk_buff
683 * @skb: buffer to reallocate
684 * @nhead: room to add at head
685 * @ntail: room to add at tail
686 * @gfp_mask: allocation priority
688 * Expands (or creates identical copy, if &nhead and &ntail are zero)
689 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
690 * reference count of 1. Returns zero in the case of success or error,
691 * if expansion failed. In the last case, &sk_buff is not changed.
693 * All the pointers pointing into skb header may change and must be
694 * reloaded after call to this function.
697 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
702 #ifdef NET_SKBUFF_DATA_USES_OFFSET
703 int size
= nhead
+ skb
->end
+ ntail
;
705 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
714 size
= SKB_DATA_ALIGN(size
);
716 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
720 /* Copy only real data... and, alas, header. This should be
721 * optimized for the cases when header is void. */
722 #ifdef NET_SKBUFF_DATA_USES_OFFSET
723 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
725 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
727 memcpy(data
+ size
, skb_end_pointer(skb
),
728 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
730 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
731 get_page(skb_shinfo(skb
)->frags
[i
].page
);
733 if (skb_shinfo(skb
)->frag_list
)
734 skb_clone_fraglist(skb
);
736 skb_release_data(skb
);
738 off
= (data
+ nhead
) - skb
->head
;
742 #ifdef NET_SKBUFF_DATA_USES_OFFSET
746 skb
->end
= skb
->head
+ size
;
748 /* {transport,network,mac}_header and tail are relative to skb->head */
750 skb
->transport_header
+= off
;
751 skb
->network_header
+= off
;
752 if (skb_mac_header_was_set(skb
))
753 skb
->mac_header
+= off
;
754 /* Only adjust this if it actually is csum_start rather than csum */
755 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
756 skb
->csum_start
+= nhead
;
760 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
767 /* Make private copy of skb with writable head and some headroom */
769 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
771 struct sk_buff
*skb2
;
772 int delta
= headroom
- skb_headroom(skb
);
775 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
777 skb2
= skb_clone(skb
, GFP_ATOMIC
);
778 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
789 * skb_copy_expand - copy and expand sk_buff
790 * @skb: buffer to copy
791 * @newheadroom: new free bytes at head
792 * @newtailroom: new free bytes at tail
793 * @gfp_mask: allocation priority
795 * Make a copy of both an &sk_buff and its data and while doing so
796 * allocate additional space.
798 * This is used when the caller wishes to modify the data and needs a
799 * private copy of the data to alter as well as more space for new fields.
800 * Returns %NULL on failure or the pointer to the buffer
801 * on success. The returned buffer has a reference count of 1.
803 * You must pass %GFP_ATOMIC as the allocation priority if this function
804 * is called from an interrupt.
806 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
807 int newheadroom
, int newtailroom
,
811 * Allocate the copy buffer
813 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
815 int oldheadroom
= skb_headroom(skb
);
816 int head_copy_len
, head_copy_off
;
822 skb_reserve(n
, newheadroom
);
824 /* Set the tail pointer and length */
825 skb_put(n
, skb
->len
);
827 head_copy_len
= oldheadroom
;
829 if (newheadroom
<= head_copy_len
)
830 head_copy_len
= newheadroom
;
832 head_copy_off
= newheadroom
- head_copy_len
;
834 /* Copy the linear header and data. */
835 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
836 skb
->len
+ head_copy_len
))
839 copy_skb_header(n
, skb
);
841 off
= newheadroom
- oldheadroom
;
842 if (n
->ip_summed
== CHECKSUM_PARTIAL
)
843 n
->csum_start
+= off
;
844 #ifdef NET_SKBUFF_DATA_USES_OFFSET
845 n
->transport_header
+= off
;
846 n
->network_header
+= off
;
847 if (skb_mac_header_was_set(skb
))
848 n
->mac_header
+= off
;
855 * skb_pad - zero pad the tail of an skb
856 * @skb: buffer to pad
859 * Ensure that a buffer is followed by a padding area that is zero
860 * filled. Used by network drivers which may DMA or transfer data
861 * beyond the buffer end onto the wire.
863 * May return error in out of memory cases. The skb is freed on error.
866 int skb_pad(struct sk_buff
*skb
, int pad
)
871 /* If the skbuff is non linear tailroom is always zero.. */
872 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
873 memset(skb
->data
+skb
->len
, 0, pad
);
877 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
878 if (likely(skb_cloned(skb
) || ntail
> 0)) {
879 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
884 /* FIXME: The use of this function with non-linear skb's really needs
887 err
= skb_linearize(skb
);
891 memset(skb
->data
+ skb
->len
, 0, pad
);
899 /* Trims skb to length len. It can change skb pointers.
902 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
904 struct sk_buff
**fragp
;
905 struct sk_buff
*frag
;
906 int offset
= skb_headlen(skb
);
907 int nfrags
= skb_shinfo(skb
)->nr_frags
;
911 if (skb_cloned(skb
) &&
912 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
919 for (; i
< nfrags
; i
++) {
920 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
927 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
930 skb_shinfo(skb
)->nr_frags
= i
;
932 for (; i
< nfrags
; i
++)
933 put_page(skb_shinfo(skb
)->frags
[i
].page
);
935 if (skb_shinfo(skb
)->frag_list
)
936 skb_drop_fraglist(skb
);
940 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
941 fragp
= &frag
->next
) {
942 int end
= offset
+ frag
->len
;
944 if (skb_shared(frag
)) {
945 struct sk_buff
*nfrag
;
947 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
948 if (unlikely(!nfrag
))
951 nfrag
->next
= frag
->next
;
963 unlikely((err
= pskb_trim(frag
, len
- offset
))))
967 skb_drop_list(&frag
->next
);
972 if (len
> skb_headlen(skb
)) {
973 skb
->data_len
-= skb
->len
- len
;
978 skb_set_tail_pointer(skb
, len
);
985 * __pskb_pull_tail - advance tail of skb header
986 * @skb: buffer to reallocate
987 * @delta: number of bytes to advance tail
989 * The function makes a sense only on a fragmented &sk_buff,
990 * it expands header moving its tail forward and copying necessary
991 * data from fragmented part.
993 * &sk_buff MUST have reference count of 1.
995 * Returns %NULL (and &sk_buff does not change) if pull failed
996 * or value of new tail of skb in the case of success.
998 * All the pointers pointing into skb header may change and must be
999 * reloaded after call to this function.
1002 /* Moves tail of skb head forward, copying data from fragmented part,
1003 * when it is necessary.
1004 * 1. It may fail due to malloc failure.
1005 * 2. It may change skb pointers.
1007 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1009 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1011 /* If skb has not enough free space at tail, get new one
1012 * plus 128 bytes for future expansions. If we have enough
1013 * room at tail, reallocate without expansion only if skb is cloned.
1015 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1017 if (eat
> 0 || skb_cloned(skb
)) {
1018 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1023 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1026 /* Optimization: no fragments, no reasons to preestimate
1027 * size of pulled pages. Superb.
1029 if (!skb_shinfo(skb
)->frag_list
)
1032 /* Estimate size of pulled pages. */
1034 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1035 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
1037 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1040 /* If we need update frag list, we are in troubles.
1041 * Certainly, it possible to add an offset to skb data,
1042 * but taking into account that pulling is expected to
1043 * be very rare operation, it is worth to fight against
1044 * further bloating skb head and crucify ourselves here instead.
1045 * Pure masohism, indeed. 8)8)
1048 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1049 struct sk_buff
*clone
= NULL
;
1050 struct sk_buff
*insp
= NULL
;
1055 if (list
->len
<= eat
) {
1056 /* Eaten as whole. */
1061 /* Eaten partially. */
1063 if (skb_shared(list
)) {
1064 /* Sucks! We need to fork list. :-( */
1065 clone
= skb_clone(list
, GFP_ATOMIC
);
1071 /* This may be pulled without
1075 if (!pskb_pull(list
, eat
)) {
1084 /* Free pulled out fragments. */
1085 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1086 skb_shinfo(skb
)->frag_list
= list
->next
;
1089 /* And insert new clone at head. */
1092 skb_shinfo(skb
)->frag_list
= clone
;
1095 /* Success! Now we may commit changes to skb data. */
1100 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1101 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1102 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1103 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1105 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1107 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1108 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1114 skb_shinfo(skb
)->nr_frags
= k
;
1117 skb
->data_len
-= delta
;
1119 return skb_tail_pointer(skb
);
1122 /* Copy some data bits from skb to kernel buffer. */
1124 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1127 int start
= skb_headlen(skb
);
1129 if (offset
> (int)skb
->len
- len
)
1133 if ((copy
= start
- offset
) > 0) {
1136 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1137 if ((len
-= copy
) == 0)
1143 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1146 BUG_TRAP(start
<= offset
+ len
);
1148 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1149 if ((copy
= end
- offset
) > 0) {
1155 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1157 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1158 offset
- start
, copy
);
1159 kunmap_skb_frag(vaddr
);
1161 if ((len
-= copy
) == 0)
1169 if (skb_shinfo(skb
)->frag_list
) {
1170 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1172 for (; list
; list
= list
->next
) {
1175 BUG_TRAP(start
<= offset
+ len
);
1177 end
= start
+ list
->len
;
1178 if ((copy
= end
- offset
) > 0) {
1181 if (skb_copy_bits(list
, offset
- start
,
1184 if ((len
-= copy
) == 0)
1200 * Callback from splice_to_pipe(), if we need to release some pages
1201 * at the end of the spd in case we error'ed out in filling the pipe.
1203 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1205 struct sk_buff
*skb
= (struct sk_buff
*) spd
->partial
[i
].private;
1211 * Fill page/offset/length into spd, if it can hold more pages.
1213 static inline int spd_fill_page(struct splice_pipe_desc
*spd
, struct page
*page
,
1214 unsigned int len
, unsigned int offset
,
1215 struct sk_buff
*skb
)
1217 if (unlikely(spd
->nr_pages
== PIPE_BUFFERS
))
1220 spd
->pages
[spd
->nr_pages
] = page
;
1221 spd
->partial
[spd
->nr_pages
].len
= len
;
1222 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1223 spd
->partial
[spd
->nr_pages
].private = (unsigned long) skb_get(skb
);
1229 * Map linear and fragment data from the skb to spd. Returns number of
1232 static int __skb_splice_bits(struct sk_buff
*skb
, unsigned int *offset
,
1233 unsigned int *total_len
,
1234 struct splice_pipe_desc
*spd
)
1236 unsigned int nr_pages
= spd
->nr_pages
;
1237 unsigned int poff
, plen
, len
, toff
, tlen
;
1238 int headlen
, seg
, error
= 0;
1248 * if the offset is greater than the linear part, go directly to
1251 headlen
= skb_headlen(skb
);
1252 if (toff
>= headlen
) {
1258 * first map the linear region into the pages/partial map, skipping
1259 * any potential initial offset.
1262 while (len
< headlen
) {
1263 void *p
= skb
->data
+ len
;
1265 poff
= (unsigned long) p
& (PAGE_SIZE
- 1);
1266 plen
= min_t(unsigned int, headlen
- len
, PAGE_SIZE
- poff
);
1279 plen
= min(plen
, tlen
);
1284 * just jump directly to update and return, no point
1285 * in going over fragments when the output is full.
1287 error
= spd_fill_page(spd
, virt_to_page(p
), plen
, poff
, skb
);
1295 * then map the fragments
1298 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1299 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1302 poff
= f
->page_offset
;
1314 plen
= min(plen
, tlen
);
1318 error
= spd_fill_page(spd
, f
->page
, plen
, poff
, skb
);
1326 if (spd
->nr_pages
- nr_pages
) {
1332 /* update the offset to reflect the linear part skip, if any */
1339 * Map data from the skb to a pipe. Should handle both the linear part,
1340 * the fragments, and the frag list. It does NOT handle frag lists within
1341 * the frag list, if such a thing exists. We'd probably need to recurse to
1342 * handle that cleanly.
1344 int skb_splice_bits(struct sk_buff
*__skb
, unsigned int offset
,
1345 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1348 struct partial_page partial
[PIPE_BUFFERS
];
1349 struct page
*pages
[PIPE_BUFFERS
];
1350 struct splice_pipe_desc spd
= {
1354 .ops
= &sock_pipe_buf_ops
,
1355 .spd_release
= sock_spd_release
,
1357 struct sk_buff
*skb
;
1360 * I'd love to avoid the clone here, but tcp_read_sock()
1361 * ignores reference counts and unconditonally kills the sk_buff
1362 * on return from the actor.
1364 skb
= skb_clone(__skb
, GFP_KERNEL
);
1369 * __skb_splice_bits() only fails if the output has no room left,
1370 * so no point in going over the frag_list for the error case.
1372 if (__skb_splice_bits(skb
, &offset
, &tlen
, &spd
))
1378 * now see if we have a frag_list to map
1380 if (skb_shinfo(skb
)->frag_list
) {
1381 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1383 for (; list
&& tlen
; list
= list
->next
) {
1384 if (__skb_splice_bits(list
, &offset
, &tlen
, &spd
))
1391 * drop our reference to the clone, the pipe consumption will
1398 struct sock
*sk
= __skb
->sk
;
1401 * Drop the socket lock, otherwise we have reverse
1402 * locking dependencies between sk_lock and i_mutex
1403 * here as compared to sendfile(). We enter here
1404 * with the socket lock held, and splice_to_pipe() will
1405 * grab the pipe inode lock. For sendfile() emulation,
1406 * we call into ->sendpage() with the i_mutex lock held
1407 * and networking will grab the socket lock.
1410 ret
= splice_to_pipe(pipe
, &spd
);
1419 * skb_store_bits - store bits from kernel buffer to skb
1420 * @skb: destination buffer
1421 * @offset: offset in destination
1422 * @from: source buffer
1423 * @len: number of bytes to copy
1425 * Copy the specified number of bytes from the source buffer to the
1426 * destination skb. This function handles all the messy bits of
1427 * traversing fragment lists and such.
1430 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1433 int start
= skb_headlen(skb
);
1435 if (offset
> (int)skb
->len
- len
)
1438 if ((copy
= start
- offset
) > 0) {
1441 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1442 if ((len
-= copy
) == 0)
1448 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1449 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1452 BUG_TRAP(start
<= offset
+ len
);
1454 end
= start
+ frag
->size
;
1455 if ((copy
= end
- offset
) > 0) {
1461 vaddr
= kmap_skb_frag(frag
);
1462 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1464 kunmap_skb_frag(vaddr
);
1466 if ((len
-= copy
) == 0)
1474 if (skb_shinfo(skb
)->frag_list
) {
1475 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1477 for (; list
; list
= list
->next
) {
1480 BUG_TRAP(start
<= offset
+ len
);
1482 end
= start
+ list
->len
;
1483 if ((copy
= end
- offset
) > 0) {
1486 if (skb_store_bits(list
, offset
- start
,
1489 if ((len
-= copy
) == 0)
1504 EXPORT_SYMBOL(skb_store_bits
);
1506 /* Checksum skb data. */
1508 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1509 int len
, __wsum csum
)
1511 int start
= skb_headlen(skb
);
1512 int i
, copy
= start
- offset
;
1515 /* Checksum header. */
1519 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1520 if ((len
-= copy
) == 0)
1526 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1529 BUG_TRAP(start
<= offset
+ len
);
1531 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1532 if ((copy
= end
- offset
) > 0) {
1535 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1539 vaddr
= kmap_skb_frag(frag
);
1540 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1541 offset
- start
, copy
, 0);
1542 kunmap_skb_frag(vaddr
);
1543 csum
= csum_block_add(csum
, csum2
, pos
);
1552 if (skb_shinfo(skb
)->frag_list
) {
1553 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1555 for (; list
; list
= list
->next
) {
1558 BUG_TRAP(start
<= offset
+ len
);
1560 end
= start
+ list
->len
;
1561 if ((copy
= end
- offset
) > 0) {
1565 csum2
= skb_checksum(list
, offset
- start
,
1567 csum
= csum_block_add(csum
, csum2
, pos
);
1568 if ((len
-= copy
) == 0)
1581 /* Both of above in one bottle. */
1583 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1584 u8
*to
, int len
, __wsum csum
)
1586 int start
= skb_headlen(skb
);
1587 int i
, copy
= start
- offset
;
1594 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1596 if ((len
-= copy
) == 0)
1603 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1606 BUG_TRAP(start
<= offset
+ len
);
1608 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1609 if ((copy
= end
- offset
) > 0) {
1612 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1616 vaddr
= kmap_skb_frag(frag
);
1617 csum2
= csum_partial_copy_nocheck(vaddr
+
1621 kunmap_skb_frag(vaddr
);
1622 csum
= csum_block_add(csum
, csum2
, pos
);
1632 if (skb_shinfo(skb
)->frag_list
) {
1633 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1635 for (; list
; list
= list
->next
) {
1639 BUG_TRAP(start
<= offset
+ len
);
1641 end
= start
+ list
->len
;
1642 if ((copy
= end
- offset
) > 0) {
1645 csum2
= skb_copy_and_csum_bits(list
,
1648 csum
= csum_block_add(csum
, csum2
, pos
);
1649 if ((len
-= copy
) == 0)
1662 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1667 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1668 csstart
= skb
->csum_start
- skb_headroom(skb
);
1670 csstart
= skb_headlen(skb
);
1672 BUG_ON(csstart
> skb_headlen(skb
));
1674 skb_copy_from_linear_data(skb
, to
, csstart
);
1677 if (csstart
!= skb
->len
)
1678 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1679 skb
->len
- csstart
, 0);
1681 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1682 long csstuff
= csstart
+ skb
->csum_offset
;
1684 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1689 * skb_dequeue - remove from the head of the queue
1690 * @list: list to dequeue from
1692 * Remove the head of the list. The list lock is taken so the function
1693 * may be used safely with other locking list functions. The head item is
1694 * returned or %NULL if the list is empty.
1697 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1699 unsigned long flags
;
1700 struct sk_buff
*result
;
1702 spin_lock_irqsave(&list
->lock
, flags
);
1703 result
= __skb_dequeue(list
);
1704 spin_unlock_irqrestore(&list
->lock
, flags
);
1709 * skb_dequeue_tail - remove from the tail of the queue
1710 * @list: list to dequeue from
1712 * Remove the tail of the list. The list lock is taken so the function
1713 * may be used safely with other locking list functions. The tail item is
1714 * returned or %NULL if the list is empty.
1716 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1718 unsigned long flags
;
1719 struct sk_buff
*result
;
1721 spin_lock_irqsave(&list
->lock
, flags
);
1722 result
= __skb_dequeue_tail(list
);
1723 spin_unlock_irqrestore(&list
->lock
, flags
);
1728 * skb_queue_purge - empty a list
1729 * @list: list to empty
1731 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1732 * the list and one reference dropped. This function takes the list
1733 * lock and is atomic with respect to other list locking functions.
1735 void skb_queue_purge(struct sk_buff_head
*list
)
1737 struct sk_buff
*skb
;
1738 while ((skb
= skb_dequeue(list
)) != NULL
)
1743 * skb_queue_head - queue a buffer at the list head
1744 * @list: list to use
1745 * @newsk: buffer to queue
1747 * Queue a buffer at the start of the list. This function takes the
1748 * list lock and can be used safely with other locking &sk_buff functions
1751 * A buffer cannot be placed on two lists at the same time.
1753 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1755 unsigned long flags
;
1757 spin_lock_irqsave(&list
->lock
, flags
);
1758 __skb_queue_head(list
, newsk
);
1759 spin_unlock_irqrestore(&list
->lock
, flags
);
1763 * skb_queue_tail - queue a buffer at the list tail
1764 * @list: list to use
1765 * @newsk: buffer to queue
1767 * Queue a buffer at the tail of the list. This function takes the
1768 * list lock and can be used safely with other locking &sk_buff functions
1771 * A buffer cannot be placed on two lists at the same time.
1773 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1775 unsigned long flags
;
1777 spin_lock_irqsave(&list
->lock
, flags
);
1778 __skb_queue_tail(list
, newsk
);
1779 spin_unlock_irqrestore(&list
->lock
, flags
);
1783 * skb_unlink - remove a buffer from a list
1784 * @skb: buffer to remove
1785 * @list: list to use
1787 * Remove a packet from a list. The list locks are taken and this
1788 * function is atomic with respect to other list locked calls
1790 * You must know what list the SKB is on.
1792 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1794 unsigned long flags
;
1796 spin_lock_irqsave(&list
->lock
, flags
);
1797 __skb_unlink(skb
, list
);
1798 spin_unlock_irqrestore(&list
->lock
, flags
);
1802 * skb_append - append a buffer
1803 * @old: buffer to insert after
1804 * @newsk: buffer to insert
1805 * @list: list to use
1807 * Place a packet after a given packet in a list. The list locks are taken
1808 * and this function is atomic with respect to other list locked calls.
1809 * A buffer cannot be placed on two lists at the same time.
1811 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1813 unsigned long flags
;
1815 spin_lock_irqsave(&list
->lock
, flags
);
1816 __skb_append(old
, newsk
, list
);
1817 spin_unlock_irqrestore(&list
->lock
, flags
);
1822 * skb_insert - insert a buffer
1823 * @old: buffer to insert before
1824 * @newsk: buffer to insert
1825 * @list: list to use
1827 * Place a packet before a given packet in a list. The list locks are
1828 * taken and this function is atomic with respect to other list locked
1831 * A buffer cannot be placed on two lists at the same time.
1833 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1835 unsigned long flags
;
1837 spin_lock_irqsave(&list
->lock
, flags
);
1838 __skb_insert(newsk
, old
->prev
, old
, list
);
1839 spin_unlock_irqrestore(&list
->lock
, flags
);
1842 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1843 struct sk_buff
* skb1
,
1844 const u32 len
, const int pos
)
1848 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1850 /* And move data appendix as is. */
1851 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1852 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1854 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1855 skb_shinfo(skb
)->nr_frags
= 0;
1856 skb1
->data_len
= skb
->data_len
;
1857 skb1
->len
+= skb1
->data_len
;
1860 skb_set_tail_pointer(skb
, len
);
1863 static inline void skb_split_no_header(struct sk_buff
*skb
,
1864 struct sk_buff
* skb1
,
1865 const u32 len
, int pos
)
1868 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1870 skb_shinfo(skb
)->nr_frags
= 0;
1871 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1873 skb
->data_len
= len
- pos
;
1875 for (i
= 0; i
< nfrags
; i
++) {
1876 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1878 if (pos
+ size
> len
) {
1879 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1883 * We have two variants in this case:
1884 * 1. Move all the frag to the second
1885 * part, if it is possible. F.e.
1886 * this approach is mandatory for TUX,
1887 * where splitting is expensive.
1888 * 2. Split is accurately. We make this.
1890 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1891 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1892 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1893 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1894 skb_shinfo(skb
)->nr_frags
++;
1898 skb_shinfo(skb
)->nr_frags
++;
1901 skb_shinfo(skb1
)->nr_frags
= k
;
1905 * skb_split - Split fragmented skb to two parts at length len.
1906 * @skb: the buffer to split
1907 * @skb1: the buffer to receive the second part
1908 * @len: new length for skb
1910 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1912 int pos
= skb_headlen(skb
);
1914 if (len
< pos
) /* Split line is inside header. */
1915 skb_split_inside_header(skb
, skb1
, len
, pos
);
1916 else /* Second chunk has no header, nothing to copy. */
1917 skb_split_no_header(skb
, skb1
, len
, pos
);
1921 * skb_prepare_seq_read - Prepare a sequential read of skb data
1922 * @skb: the buffer to read
1923 * @from: lower offset of data to be read
1924 * @to: upper offset of data to be read
1925 * @st: state variable
1927 * Initializes the specified state variable. Must be called before
1928 * invoking skb_seq_read() for the first time.
1930 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1931 unsigned int to
, struct skb_seq_state
*st
)
1933 st
->lower_offset
= from
;
1934 st
->upper_offset
= to
;
1935 st
->root_skb
= st
->cur_skb
= skb
;
1936 st
->frag_idx
= st
->stepped_offset
= 0;
1937 st
->frag_data
= NULL
;
1941 * skb_seq_read - Sequentially read skb data
1942 * @consumed: number of bytes consumed by the caller so far
1943 * @data: destination pointer for data to be returned
1944 * @st: state variable
1946 * Reads a block of skb data at &consumed relative to the
1947 * lower offset specified to skb_prepare_seq_read(). Assigns
1948 * the head of the data block to &data and returns the length
1949 * of the block or 0 if the end of the skb data or the upper
1950 * offset has been reached.
1952 * The caller is not required to consume all of the data
1953 * returned, i.e. &consumed is typically set to the number
1954 * of bytes already consumed and the next call to
1955 * skb_seq_read() will return the remaining part of the block.
1957 * Note: The size of each block of data returned can be arbitary,
1958 * this limitation is the cost for zerocopy seqeuental
1959 * reads of potentially non linear data.
1961 * Note: Fragment lists within fragments are not implemented
1962 * at the moment, state->root_skb could be replaced with
1963 * a stack for this purpose.
1965 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1966 struct skb_seq_state
*st
)
1968 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1971 if (unlikely(abs_offset
>= st
->upper_offset
))
1975 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
1977 if (abs_offset
< block_limit
&& !st
->frag_data
) {
1978 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
1979 return block_limit
- abs_offset
;
1982 if (st
->frag_idx
== 0 && !st
->frag_data
)
1983 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1985 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1986 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1987 block_limit
= frag
->size
+ st
->stepped_offset
;
1989 if (abs_offset
< block_limit
) {
1991 st
->frag_data
= kmap_skb_frag(frag
);
1993 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1994 (abs_offset
- st
->stepped_offset
);
1996 return block_limit
- abs_offset
;
1999 if (st
->frag_data
) {
2000 kunmap_skb_frag(st
->frag_data
);
2001 st
->frag_data
= NULL
;
2005 st
->stepped_offset
+= frag
->size
;
2008 if (st
->frag_data
) {
2009 kunmap_skb_frag(st
->frag_data
);
2010 st
->frag_data
= NULL
;
2013 if (st
->root_skb
== st
->cur_skb
&&
2014 skb_shinfo(st
->root_skb
)->frag_list
) {
2015 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2018 } else if (st
->cur_skb
->next
) {
2019 st
->cur_skb
= st
->cur_skb
->next
;
2028 * skb_abort_seq_read - Abort a sequential read of skb data
2029 * @st: state variable
2031 * Must be called if skb_seq_read() was not called until it
2034 void skb_abort_seq_read(struct skb_seq_state
*st
)
2037 kunmap_skb_frag(st
->frag_data
);
2040 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2042 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2043 struct ts_config
*conf
,
2044 struct ts_state
*state
)
2046 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2049 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2051 skb_abort_seq_read(TS_SKB_CB(state
));
2055 * skb_find_text - Find a text pattern in skb data
2056 * @skb: the buffer to look in
2057 * @from: search offset
2059 * @config: textsearch configuration
2060 * @state: uninitialized textsearch state variable
2062 * Finds a pattern in the skb data according to the specified
2063 * textsearch configuration. Use textsearch_next() to retrieve
2064 * subsequent occurrences of the pattern. Returns the offset
2065 * to the first occurrence or UINT_MAX if no match was found.
2067 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2068 unsigned int to
, struct ts_config
*config
,
2069 struct ts_state
*state
)
2073 config
->get_next_block
= skb_ts_get_next_block
;
2074 config
->finish
= skb_ts_finish
;
2076 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2078 ret
= textsearch_find(config
, state
);
2079 return (ret
<= to
- from
? ret
: UINT_MAX
);
2083 * skb_append_datato_frags: - append the user data to a skb
2084 * @sk: sock structure
2085 * @skb: skb structure to be appened with user data.
2086 * @getfrag: call back function to be used for getting the user data
2087 * @from: pointer to user message iov
2088 * @length: length of the iov message
2090 * Description: This procedure append the user data in the fragment part
2091 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2093 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2094 int (*getfrag
)(void *from
, char *to
, int offset
,
2095 int len
, int odd
, struct sk_buff
*skb
),
2096 void *from
, int length
)
2099 skb_frag_t
*frag
= NULL
;
2100 struct page
*page
= NULL
;
2106 /* Return error if we don't have space for new frag */
2107 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2108 if (frg_cnt
>= MAX_SKB_FRAGS
)
2111 /* allocate a new page for next frag */
2112 page
= alloc_pages(sk
->sk_allocation
, 0);
2114 /* If alloc_page fails just return failure and caller will
2115 * free previous allocated pages by doing kfree_skb()
2120 /* initialize the next frag */
2121 sk
->sk_sndmsg_page
= page
;
2122 sk
->sk_sndmsg_off
= 0;
2123 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2124 skb
->truesize
+= PAGE_SIZE
;
2125 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2127 /* get the new initialized frag */
2128 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2129 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2131 /* copy the user data to page */
2132 left
= PAGE_SIZE
- frag
->page_offset
;
2133 copy
= (length
> left
)? left
: length
;
2135 ret
= getfrag(from
, (page_address(frag
->page
) +
2136 frag
->page_offset
+ frag
->size
),
2137 offset
, copy
, 0, skb
);
2141 /* copy was successful so update the size parameters */
2142 sk
->sk_sndmsg_off
+= copy
;
2145 skb
->data_len
+= copy
;
2149 } while (length
> 0);
2155 * skb_pull_rcsum - pull skb and update receive checksum
2156 * @skb: buffer to update
2157 * @start: start of data before pull
2158 * @len: length of data pulled
2160 * This function performs an skb_pull on the packet and updates
2161 * update the CHECKSUM_COMPLETE checksum. It should be used on
2162 * receive path processing instead of skb_pull unless you know
2163 * that the checksum difference is zero (e.g., a valid IP header)
2164 * or you are setting ip_summed to CHECKSUM_NONE.
2166 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2168 BUG_ON(len
> skb
->len
);
2170 BUG_ON(skb
->len
< skb
->data_len
);
2171 skb_postpull_rcsum(skb
, skb
->data
, len
);
2172 return skb
->data
+= len
;
2175 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2178 * skb_segment - Perform protocol segmentation on skb.
2179 * @skb: buffer to segment
2180 * @features: features for the output path (see dev->features)
2182 * This function performs segmentation on the given skb. It returns
2183 * the segment at the given position. It returns NULL if there are
2184 * no more segments to generate, or when an error is encountered.
2186 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
2188 struct sk_buff
*segs
= NULL
;
2189 struct sk_buff
*tail
= NULL
;
2190 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2191 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2192 unsigned int offset
= doffset
;
2193 unsigned int headroom
;
2195 int sg
= features
& NETIF_F_SG
;
2196 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2201 __skb_push(skb
, doffset
);
2202 headroom
= skb_headroom(skb
);
2203 pos
= skb_headlen(skb
);
2206 struct sk_buff
*nskb
;
2212 len
= skb
->len
- offset
;
2216 hsize
= skb_headlen(skb
) - offset
;
2219 if (hsize
> len
|| !sg
)
2222 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
2223 if (unlikely(!nskb
))
2232 __copy_skb_header(nskb
, skb
);
2233 nskb
->mac_len
= skb
->mac_len
;
2235 nskb
->ctf_mac_len
= skb
->ctf_mac_len
; /* used by Broadcom CTF driver! */
2238 skb_reserve(nskb
, headroom
);
2239 skb_reset_mac_header(nskb
);
2240 skb_set_network_header(nskb
, skb
->mac_len
);
2241 nskb
->transport_header
= (nskb
->network_header
+
2242 skb_network_header_len(skb
));
2243 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
2246 nskb
->ip_summed
= CHECKSUM_NONE
;
2247 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2253 frag
= skb_shinfo(nskb
)->frags
;
2256 skb_copy_from_linear_data_offset(skb
, offset
,
2257 skb_put(nskb
, hsize
), hsize
);
2259 while (pos
< offset
+ len
) {
2260 BUG_ON(i
>= nfrags
);
2262 *frag
= skb_shinfo(skb
)->frags
[i
];
2263 get_page(frag
->page
);
2267 frag
->page_offset
+= offset
- pos
;
2268 frag
->size
-= offset
- pos
;
2273 if (pos
+ size
<= offset
+ len
) {
2277 frag
->size
-= pos
+ size
- (offset
+ len
);
2284 skb_shinfo(nskb
)->nr_frags
= k
;
2285 nskb
->data_len
= len
- hsize
;
2286 nskb
->len
+= nskb
->data_len
;
2287 nskb
->truesize
+= nskb
->data_len
;
2288 } while ((offset
+= len
) < skb
->len
);
2293 while ((skb
= segs
)) {
2297 return ERR_PTR(err
);
2300 EXPORT_SYMBOL_GPL(skb_segment
);
2302 void __init
skb_init(void)
2304 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2305 sizeof(struct sk_buff
),
2307 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2309 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2310 (2*sizeof(struct sk_buff
)) +
2313 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2318 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2319 * @skb: Socket buffer containing the buffers to be mapped
2320 * @sg: The scatter-gather list to map into
2321 * @offset: The offset into the buffer's contents to start mapping
2322 * @len: Length of buffer space to be mapped
2324 * Fill the specified scatter-gather list with mappings/pointers into a
2325 * region of the buffer space attached to a socket buffer.
2328 skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2330 int start
= skb_headlen(skb
);
2331 int i
, copy
= start
- offset
;
2337 sg
[elt
].page
= virt_to_page(skb
->data
+ offset
);
2338 sg
[elt
].offset
= (unsigned long)(skb
->data
+ offset
) % PAGE_SIZE
;
2339 sg
[elt
].length
= copy
;
2341 if ((len
-= copy
) == 0)
2346 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2349 BUG_TRAP(start
<= offset
+ len
);
2351 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2352 if ((copy
= end
- offset
) > 0) {
2353 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2357 sg
[elt
].page
= frag
->page
;
2358 sg
[elt
].offset
= frag
->page_offset
+offset
-start
;
2359 sg
[elt
].length
= copy
;
2368 if (skb_shinfo(skb
)->frag_list
) {
2369 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
2371 for (; list
; list
= list
->next
) {
2374 BUG_TRAP(start
<= offset
+ len
);
2376 end
= start
+ list
->len
;
2377 if ((copy
= end
- offset
) > 0) {
2380 elt
+= skb_to_sgvec(list
, sg
+elt
, offset
- start
, copy
);
2381 if ((len
-= copy
) == 0)
2393 * skb_cow_data - Check that a socket buffer's data buffers are writable
2394 * @skb: The socket buffer to check.
2395 * @tailbits: Amount of trailing space to be added
2396 * @trailer: Returned pointer to the skb where the @tailbits space begins
2398 * Make sure that the data buffers attached to a socket buffer are
2399 * writable. If they are not, private copies are made of the data buffers
2400 * and the socket buffer is set to use these instead.
2402 * If @tailbits is given, make sure that there is space to write @tailbits
2403 * bytes of data beyond current end of socket buffer. @trailer will be
2404 * set to point to the skb in which this space begins.
2406 * The number of scatterlist elements required to completely map the
2407 * COW'd and extended socket buffer will be returned.
2409 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2413 struct sk_buff
*skb1
, **skb_p
;
2415 /* If skb is cloned or its head is paged, reallocate
2416 * head pulling out all the pages (pages are considered not writable
2417 * at the moment even if they are anonymous).
2419 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2420 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2423 /* Easy case. Most of packets will go this way. */
2424 if (!skb_shinfo(skb
)->frag_list
) {
2425 /* A little of trouble, not enough of space for trailer.
2426 * This should not happen, when stack is tuned to generate
2427 * good frames. OK, on miss we reallocate and reserve even more
2428 * space, 128 bytes is fair. */
2430 if (skb_tailroom(skb
) < tailbits
&&
2431 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2439 /* Misery. We are in troubles, going to mincer fragments... */
2442 skb_p
= &skb_shinfo(skb
)->frag_list
;
2445 while ((skb1
= *skb_p
) != NULL
) {
2448 /* The fragment is partially pulled by someone,
2449 * this can happen on input. Copy it and everything
2452 if (skb_shared(skb1
))
2455 /* If the skb is the last, worry about trailer. */
2457 if (skb1
->next
== NULL
&& tailbits
) {
2458 if (skb_shinfo(skb1
)->nr_frags
||
2459 skb_shinfo(skb1
)->frag_list
||
2460 skb_tailroom(skb1
) < tailbits
)
2461 ntail
= tailbits
+ 128;
2467 skb_shinfo(skb1
)->nr_frags
||
2468 skb_shinfo(skb1
)->frag_list
) {
2469 struct sk_buff
*skb2
;
2471 /* Fuck, we are miserable poor guys... */
2473 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2475 skb2
= skb_copy_expand(skb1
,
2479 if (unlikely(skb2
== NULL
))
2483 skb_set_owner_w(skb2
, skb1
->sk
);
2485 /* Looking around. Are we still alive?
2486 * OK, link new skb, drop old one */
2488 skb2
->next
= skb1
->next
;
2495 skb_p
= &skb1
->next
;
2501 EXPORT_SYMBOL(___pskb_trim
);
2502 EXPORT_SYMBOL(__kfree_skb
);
2503 EXPORT_SYMBOL(kfree_skb
);
2504 EXPORT_SYMBOL(__pskb_pull_tail
);
2505 EXPORT_SYMBOL(__alloc_skb
);
2506 EXPORT_SYMBOL(__netdev_alloc_skb
);
2507 EXPORT_SYMBOL(pskb_copy
);
2508 EXPORT_SYMBOL(pskb_expand_head
);
2509 EXPORT_SYMBOL(skb_checksum
);
2510 EXPORT_SYMBOL(skb_clone
);
2511 EXPORT_SYMBOL(skb_copy
);
2512 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2513 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2514 EXPORT_SYMBOL(skb_copy_bits
);
2515 EXPORT_SYMBOL(skb_copy_expand
);
2516 EXPORT_SYMBOL(skb_over_panic
);
2517 EXPORT_SYMBOL(skb_pad
);
2518 EXPORT_SYMBOL(skb_realloc_headroom
);
2519 EXPORT_SYMBOL(skb_under_panic
);
2520 EXPORT_SYMBOL(skb_dequeue
);
2521 EXPORT_SYMBOL(skb_dequeue_tail
);
2522 EXPORT_SYMBOL(skb_insert
);
2523 EXPORT_SYMBOL(skb_queue_purge
);
2524 EXPORT_SYMBOL(skb_queue_head
);
2525 EXPORT_SYMBOL(skb_queue_tail
);
2526 EXPORT_SYMBOL(skb_unlink
);
2527 EXPORT_SYMBOL(skb_append
);
2528 EXPORT_SYMBOL(skb_split
);
2529 EXPORT_SYMBOL(skb_prepare_seq_read
);
2530 EXPORT_SYMBOL(skb_seq_read
);
2531 EXPORT_SYMBOL(skb_abort_seq_read
);
2532 EXPORT_SYMBOL(skb_find_text
);
2533 EXPORT_SYMBOL(skb_append_datato_frags
);
2535 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2536 EXPORT_SYMBOL_GPL(skb_cow_data
);