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
;
218 #ifdef CONFIG_BRIDGE_NETFILTER
219 skb
->nf_bridge
= NULL
;
221 #ifdef CONFIG_NET_SCHED
223 //#ifdef CONFIG_NET_CLS_ACT
227 #ifdef CONFIG_NET_DMA
228 memset(&skb
->dma_cookie
, 0, sizeof(dma_cookie_t
));
230 #ifdef CONFIG_NETWORK_SECMARK
234 /* make sure we initialize shinfo sequentially */
235 shinfo
= skb_shinfo(skb
);
236 memset(shinfo
, 0, offsetof(struct skb_shared_info
, frags
));
237 atomic_set(&shinfo
->dataref
, 1);
239 #if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE)
245 struct sk_buff
*child
= skb
+ 1;
246 atomic_t
*fclone_ref
= (atomic_t
*) (child
+ 1);
248 skb
->fclone
= SKB_FCLONE_ORIG
;
249 atomic_set(fclone_ref
, 1);
251 child
->fclone
= SKB_FCLONE_UNAVAILABLE
;
256 kmem_cache_free(cache
, skb
);
262 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
263 * @dev: network device to receive on
264 * @length: length to allocate
265 * @gfp_mask: get_free_pages mask, passed to alloc_skb
267 * Allocate a new &sk_buff and assign it a usage count of one. The
268 * buffer has unspecified headroom built in. Users should allocate
269 * the headroom they think they need without accounting for the
270 * built in space. The built in space is used for optimisations.
272 * %NULL is returned if there is no free memory.
274 struct sk_buff
*__netdev_alloc_skb(struct net_device
*dev
,
275 unsigned int length
, gfp_t gfp_mask
)
277 int node
= dev
->dev
.parent
? dev_to_node(dev
->dev
.parent
) : -1;
280 skb
= __alloc_skb(length
+ NET_SKB_PAD_ALLOC
, gfp_mask
, 0, node
);
282 skb_reserve(skb
, NET_SKB_PAD_ALLOC
);
288 static void skb_drop_list(struct sk_buff
**listp
)
290 struct sk_buff
*list
= *listp
;
295 struct sk_buff
*this = list
;
301 static inline void skb_drop_fraglist(struct sk_buff
*skb
)
303 skb_drop_list(&skb_shinfo(skb
)->frag_list
);
306 static void skb_clone_fraglist(struct sk_buff
*skb
)
308 struct sk_buff
*list
;
310 for (list
= skb_shinfo(skb
)->frag_list
; list
; list
= list
->next
)
314 static void skb_release_data(struct sk_buff
*skb
)
317 !atomic_sub_return(skb
->nohdr
? (1 << SKB_DATAREF_SHIFT
) + 1 : 1,
318 &skb_shinfo(skb
)->dataref
)) {
319 if (skb_shinfo(skb
)->nr_frags
) {
321 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
322 put_page(skb_shinfo(skb
)->frags
[i
].page
);
325 if (skb_shinfo(skb
)->frag_list
)
326 skb_drop_fraglist(skb
);
333 * Free an skbuff by memory without cleaning the state.
335 static void kfree_skbmem(struct sk_buff
*skb
)
337 struct sk_buff
*other
;
338 atomic_t
*fclone_ref
;
340 switch (skb
->fclone
) {
341 case SKB_FCLONE_UNAVAILABLE
:
342 kmem_cache_free(skbuff_head_cache
, skb
);
345 case SKB_FCLONE_ORIG
:
346 fclone_ref
= (atomic_t
*) (skb
+ 2);
347 if (atomic_dec_and_test(fclone_ref
))
348 kmem_cache_free(skbuff_fclone_cache
, skb
);
351 case SKB_FCLONE_CLONE
:
352 fclone_ref
= (atomic_t
*) (skb
+ 1);
355 /* The clone portion is available for
356 * fast-cloning again.
358 skb
->fclone
= SKB_FCLONE_UNAVAILABLE
;
360 if (atomic_dec_and_test(fclone_ref
))
361 kmem_cache_free(skbuff_fclone_cache
, other
);
366 /* Free everything but the sk_buff shell. */
367 static void skb_release_all(struct sk_buff
*skb
)
369 dst_release(skb
->dst
);
371 secpath_put(skb
->sp
);
373 if (skb
->destructor
) {
374 #ifdef CONFIG_NETCORE_DEBUG
377 skb
->destructor(skb
);
379 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
380 nf_conntrack_put(skb
->nfct
);
381 nf_conntrack_put_reasm(skb
->nfct_reasm
);
383 #ifdef CONFIG_BRIDGE_NETFILTER
384 nf_bridge_put(skb
->nf_bridge
);
386 /* XXX: IS this still necessary? - JHS */
387 #ifdef CONFIG_NET_SCHED
389 //#ifdef CONFIG_NET_CLS_ACT
393 skb_release_data(skb
);
397 * __kfree_skb - private function
400 * Free an sk_buff. Release anything attached to the buffer.
401 * Clean the state. This is an internal helper function. Users should
402 * always call kfree_skb
405 void __kfree_skb(struct sk_buff
*skb
)
407 skb_release_all(skb
);
412 * kfree_skb - free an sk_buff
413 * @skb: buffer to free
415 * Drop a reference to the buffer and free it if the usage count has
418 void kfree_skb(struct sk_buff
*skb
)
422 if (likely(atomic_read(&skb
->users
) == 1))
424 else if (likely(!atomic_dec_and_test(&skb
->users
)))
429 static void __copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
431 new->tstamp
= old
->tstamp
;
433 new->transport_header
= old
->transport_header
;
434 new->network_header
= old
->network_header
;
435 new->mac_header
= old
->mac_header
;
436 new->dst
= dst_clone(old
->dst
);
438 new->sp
= secpath_get(old
->sp
);
440 memcpy(new->cb
, old
->cb
, sizeof(old
->cb
));
441 new->csum
= old
->csum
;
442 new->local_df
= old
->local_df
;
443 new->pkt_type
= old
->pkt_type
;
444 new->ip_summed
= old
->ip_summed
;
445 new->priority
= old
->priority
;
446 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447 new->ipvs_property
= old
->ipvs_property
;
449 new->protocol
= old
->protocol
;
450 new->mark
= old
->mark
;
453 #ifdef CONFIG_NET_SCHED
454 new->tc_index
= old
->tc_index
;
455 //#ifdef CONFIG_NET_CLS_ACT
456 new->tc_verd
= old
->tc_verd
;
459 new->vlan_tci
= old
->vlan_tci
;
461 skb_copy_secmark(new, old
);
462 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
463 new->nfcache
= old
->nfcache
;
468 * You should not add any new code to this function. Add it to
469 * __copy_skb_header above instead.
471 static struct sk_buff
*__skb_clone(struct sk_buff
*n
, struct sk_buff
*skb
)
473 #define C(x) n->x = skb->x
475 n
->next
= n
->prev
= NULL
;
477 __copy_skb_header(n
, skb
);
483 C(ctf_mac_len
); /* used by Broadcom CTF driver! */
485 n
->hdr_len
= skb
->nohdr
? skb_headroom(skb
) : skb
->hdr_len
;
488 n
->destructor
= NULL
;
494 atomic_set(&n
->users
, 1);
496 atomic_inc(&(skb_shinfo(skb
)->dataref
));
504 * skb_morph - morph one skb into another
505 * @dst: the skb to receive the contents
506 * @src: the skb to supply the contents
508 * This is identical to skb_clone except that the target skb is
509 * supplied by the user.
511 * The target skb is returned upon exit.
513 struct sk_buff
*skb_morph(struct sk_buff
*dst
, struct sk_buff
*src
)
515 skb_release_all(dst
);
516 return __skb_clone(dst
, src
);
518 EXPORT_SYMBOL_GPL(skb_morph
);
521 * skb_clone - duplicate an sk_buff
522 * @skb: buffer to clone
523 * @gfp_mask: allocation priority
525 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
526 * copies share the same packet data but not structure. The new
527 * buffer has a reference count of 1. If the allocation fails the
528 * function returns %NULL otherwise the new buffer is returned.
530 * If this function is called from an interrupt gfp_mask() must be
534 struct sk_buff
*skb_clone(struct sk_buff
*skb
, gfp_t gfp_mask
)
539 if (skb
->fclone
== SKB_FCLONE_ORIG
&&
540 n
->fclone
== SKB_FCLONE_UNAVAILABLE
) {
541 atomic_t
*fclone_ref
= (atomic_t
*) (n
+ 1);
542 n
->fclone
= SKB_FCLONE_CLONE
;
543 atomic_inc(fclone_ref
);
545 n
= kmem_cache_alloc(skbuff_head_cache
, gfp_mask
);
548 n
->fclone
= SKB_FCLONE_UNAVAILABLE
;
551 return __skb_clone(n
, skb
);
554 static void copy_skb_header(struct sk_buff
*new, const struct sk_buff
*old
)
556 #ifndef NET_SKBUFF_DATA_USES_OFFSET
558 * Shift between the two data areas in bytes
560 unsigned long offset
= new->data
- old
->data
;
563 __copy_skb_header(new, old
);
565 #ifndef NET_SKBUFF_DATA_USES_OFFSET
566 /* {transport,network,mac}_header are relative to skb->head */
567 new->transport_header
+= offset
;
568 new->network_header
+= offset
;
569 if (skb_mac_header_was_set(new))
570 new->mac_header
+= offset
;
572 skb_shinfo(new)->gso_size
= skb_shinfo(old
)->gso_size
;
573 skb_shinfo(new)->gso_segs
= skb_shinfo(old
)->gso_segs
;
574 skb_shinfo(new)->gso_type
= skb_shinfo(old
)->gso_type
;
578 * skb_copy - create private copy of an sk_buff
579 * @skb: buffer to copy
580 * @gfp_mask: allocation priority
582 * Make a copy of both an &sk_buff and its data. This is used when the
583 * caller wishes to modify the data and needs a private copy of the
584 * data to alter. Returns %NULL on failure or the pointer to the buffer
585 * on success. The returned buffer has a reference count of 1.
587 * As by-product this function converts non-linear &sk_buff to linear
588 * one, so that &sk_buff becomes completely private and caller is allowed
589 * to modify all the data of returned buffer. This means that this
590 * function is not recommended for use in circumstances when only
591 * header is going to be modified. Use pskb_copy() instead.
594 struct sk_buff
*skb_copy(const struct sk_buff
*skb
, gfp_t gfp_mask
)
596 int headerlen
= skb
->data
- skb
->head
;
598 * Allocate the copy buffer
601 #ifdef NET_SKBUFF_DATA_USES_OFFSET
602 n
= alloc_skb(skb
->end
+ skb
->data_len
, gfp_mask
);
604 n
= alloc_skb(skb
->end
- skb
->head
+ skb
->data_len
, gfp_mask
);
609 /* Set the data pointer */
610 skb_reserve(n
, headerlen
);
611 /* Set the tail pointer and length */
612 skb_put(n
, skb
->len
);
614 if (skb_copy_bits(skb
, -headerlen
, n
->head
, headerlen
+ skb
->len
))
617 copy_skb_header(n
, skb
);
623 * pskb_copy - create copy of an sk_buff with private head.
624 * @skb: buffer to copy
625 * @gfp_mask: allocation priority
627 * Make a copy of both an &sk_buff and part of its data, located
628 * in header. Fragmented data remain shared. This is used when
629 * the caller wishes to modify only header of &sk_buff and needs
630 * private copy of the header to alter. Returns %NULL on failure
631 * or the pointer to the buffer on success.
632 * The returned buffer has a reference count of 1.
635 struct sk_buff
*pskb_copy(struct sk_buff
*skb
, gfp_t gfp_mask
)
638 * Allocate the copy buffer
641 #ifdef NET_SKBUFF_DATA_USES_OFFSET
642 n
= alloc_skb(skb
->end
, gfp_mask
);
644 n
= alloc_skb(skb
->end
- skb
->head
, gfp_mask
);
649 /* Set the data pointer */
650 skb_reserve(n
, skb
->data
- skb
->head
);
651 /* Set the tail pointer and length */
652 skb_put(n
, skb_headlen(skb
));
654 skb_copy_from_linear_data(skb
, n
->data
, n
->len
);
656 n
->truesize
+= skb
->data_len
;
657 n
->data_len
= skb
->data_len
;
660 if (skb_shinfo(skb
)->nr_frags
) {
663 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
664 skb_shinfo(n
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
665 get_page(skb_shinfo(n
)->frags
[i
].page
);
667 skb_shinfo(n
)->nr_frags
= i
;
670 if (skb_shinfo(skb
)->frag_list
) {
671 skb_shinfo(n
)->frag_list
= skb_shinfo(skb
)->frag_list
;
672 skb_clone_fraglist(n
);
675 copy_skb_header(n
, skb
);
681 * pskb_expand_head - reallocate header of &sk_buff
682 * @skb: buffer to reallocate
683 * @nhead: room to add at head
684 * @ntail: room to add at tail
685 * @gfp_mask: allocation priority
687 * Expands (or creates identical copy, if &nhead and &ntail are zero)
688 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
689 * reference count of 1. Returns zero in the case of success or error,
690 * if expansion failed. In the last case, &sk_buff is not changed.
692 * All the pointers pointing into skb header may change and must be
693 * reloaded after call to this function.
696 int pskb_expand_head(struct sk_buff
*skb
, int nhead
, int ntail
,
701 #ifdef NET_SKBUFF_DATA_USES_OFFSET
702 int size
= nhead
+ skb
->end
+ ntail
;
704 int size
= nhead
+ (skb
->end
- skb
->head
) + ntail
;
713 size
= SKB_DATA_ALIGN(size
);
715 data
= kmalloc(size
+ sizeof(struct skb_shared_info
), gfp_mask
);
719 /* Copy only real data... and, alas, header. This should be
720 * optimized for the cases when header is void. */
721 #ifdef NET_SKBUFF_DATA_USES_OFFSET
722 memcpy(data
+ nhead
, skb
->head
, skb
->tail
);
724 memcpy(data
+ nhead
, skb
->head
, skb
->tail
- skb
->head
);
726 memcpy(data
+ size
, skb_end_pointer(skb
),
727 offsetof(struct skb_shared_info
, frags
[skb_shinfo(skb
)->nr_frags
]));
729 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
730 get_page(skb_shinfo(skb
)->frags
[i
].page
);
732 if (skb_shinfo(skb
)->frag_list
)
733 skb_clone_fraglist(skb
);
735 skb_release_data(skb
);
737 off
= (data
+ nhead
) - skb
->head
;
741 #ifdef NET_SKBUFF_DATA_USES_OFFSET
745 skb
->end
= skb
->head
+ size
;
747 /* {transport,network,mac}_header and tail are relative to skb->head */
749 skb
->transport_header
+= off
;
750 skb
->network_header
+= off
;
751 if (skb_mac_header_was_set(skb
))
752 skb
->mac_header
+= off
;
753 /* Only adjust this if it actually is csum_start rather than csum */
754 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
755 skb
->csum_start
+= nhead
;
759 atomic_set(&skb_shinfo(skb
)->dataref
, 1);
766 /* Make private copy of skb with writable head and some headroom */
768 struct sk_buff
*skb_realloc_headroom(struct sk_buff
*skb
, unsigned int headroom
)
770 struct sk_buff
*skb2
;
771 int delta
= headroom
- skb_headroom(skb
);
774 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
776 skb2
= skb_clone(skb
, GFP_ATOMIC
);
777 if (skb2
&& pskb_expand_head(skb2
, SKB_DATA_ALIGN(delta
), 0,
788 * skb_copy_expand - copy and expand sk_buff
789 * @skb: buffer to copy
790 * @newheadroom: new free bytes at head
791 * @newtailroom: new free bytes at tail
792 * @gfp_mask: allocation priority
794 * Make a copy of both an &sk_buff and its data and while doing so
795 * allocate additional space.
797 * This is used when the caller wishes to modify the data and needs a
798 * private copy of the data to alter as well as more space for new fields.
799 * Returns %NULL on failure or the pointer to the buffer
800 * on success. The returned buffer has a reference count of 1.
802 * You must pass %GFP_ATOMIC as the allocation priority if this function
803 * is called from an interrupt.
805 struct sk_buff
*skb_copy_expand(const struct sk_buff
*skb
,
806 int newheadroom
, int newtailroom
,
810 * Allocate the copy buffer
812 struct sk_buff
*n
= alloc_skb(newheadroom
+ skb
->len
+ newtailroom
,
814 int oldheadroom
= skb_headroom(skb
);
815 int head_copy_len
, head_copy_off
;
821 skb_reserve(n
, newheadroom
);
823 /* Set the tail pointer and length */
824 skb_put(n
, skb
->len
);
826 head_copy_len
= oldheadroom
;
828 if (newheadroom
<= head_copy_len
)
829 head_copy_len
= newheadroom
;
831 head_copy_off
= newheadroom
- head_copy_len
;
833 /* Copy the linear header and data. */
834 if (skb_copy_bits(skb
, -head_copy_len
, n
->head
+ head_copy_off
,
835 skb
->len
+ head_copy_len
))
838 copy_skb_header(n
, skb
);
840 off
= newheadroom
- oldheadroom
;
841 if (n
->ip_summed
== CHECKSUM_PARTIAL
)
842 n
->csum_start
+= off
;
843 #ifdef NET_SKBUFF_DATA_USES_OFFSET
844 n
->transport_header
+= off
;
845 n
->network_header
+= off
;
846 if (skb_mac_header_was_set(skb
))
847 n
->mac_header
+= off
;
854 * skb_pad - zero pad the tail of an skb
855 * @skb: buffer to pad
858 * Ensure that a buffer is followed by a padding area that is zero
859 * filled. Used by network drivers which may DMA or transfer data
860 * beyond the buffer end onto the wire.
862 * May return error in out of memory cases. The skb is freed on error.
865 int skb_pad(struct sk_buff
*skb
, int pad
)
870 /* If the skbuff is non linear tailroom is always zero.. */
871 if (!skb_cloned(skb
) && skb_tailroom(skb
) >= pad
) {
872 memset(skb
->data
+skb
->len
, 0, pad
);
876 ntail
= skb
->data_len
+ pad
- (skb
->end
- skb
->tail
);
877 if (likely(skb_cloned(skb
) || ntail
> 0)) {
878 err
= pskb_expand_head(skb
, 0, ntail
, GFP_ATOMIC
);
883 /* FIXME: The use of this function with non-linear skb's really needs
886 err
= skb_linearize(skb
);
890 memset(skb
->data
+ skb
->len
, 0, pad
);
898 /* Trims skb to length len. It can change skb pointers.
901 int ___pskb_trim(struct sk_buff
*skb
, unsigned int len
)
903 struct sk_buff
**fragp
;
904 struct sk_buff
*frag
;
905 int offset
= skb_headlen(skb
);
906 int nfrags
= skb_shinfo(skb
)->nr_frags
;
910 if (skb_cloned(skb
) &&
911 unlikely((err
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
))))
918 for (; i
< nfrags
; i
++) {
919 int end
= offset
+ skb_shinfo(skb
)->frags
[i
].size
;
926 skb_shinfo(skb
)->frags
[i
++].size
= len
- offset
;
929 skb_shinfo(skb
)->nr_frags
= i
;
931 for (; i
< nfrags
; i
++)
932 put_page(skb_shinfo(skb
)->frags
[i
].page
);
934 if (skb_shinfo(skb
)->frag_list
)
935 skb_drop_fraglist(skb
);
939 for (fragp
= &skb_shinfo(skb
)->frag_list
; (frag
= *fragp
);
940 fragp
= &frag
->next
) {
941 int end
= offset
+ frag
->len
;
943 if (skb_shared(frag
)) {
944 struct sk_buff
*nfrag
;
946 nfrag
= skb_clone(frag
, GFP_ATOMIC
);
947 if (unlikely(!nfrag
))
950 nfrag
->next
= frag
->next
;
962 unlikely((err
= pskb_trim(frag
, len
- offset
))))
966 skb_drop_list(&frag
->next
);
971 if (len
> skb_headlen(skb
)) {
972 skb
->data_len
-= skb
->len
- len
;
977 skb_set_tail_pointer(skb
, len
);
984 * __pskb_pull_tail - advance tail of skb header
985 * @skb: buffer to reallocate
986 * @delta: number of bytes to advance tail
988 * The function makes a sense only on a fragmented &sk_buff,
989 * it expands header moving its tail forward and copying necessary
990 * data from fragmented part.
992 * &sk_buff MUST have reference count of 1.
994 * Returns %NULL (and &sk_buff does not change) if pull failed
995 * or value of new tail of skb in the case of success.
997 * All the pointers pointing into skb header may change and must be
998 * reloaded after call to this function.
1001 /* Moves tail of skb head forward, copying data from fragmented part,
1002 * when it is necessary.
1003 * 1. It may fail due to malloc failure.
1004 * 2. It may change skb pointers.
1006 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1008 unsigned char *__pskb_pull_tail(struct sk_buff
*skb
, int delta
)
1010 /* If skb has not enough free space at tail, get new one
1011 * plus 128 bytes for future expansions. If we have enough
1012 * room at tail, reallocate without expansion only if skb is cloned.
1014 int i
, k
, eat
= (skb
->tail
+ delta
) - skb
->end
;
1016 if (eat
> 0 || skb_cloned(skb
)) {
1017 if (pskb_expand_head(skb
, 0, eat
> 0 ? eat
+ 128 : 0,
1022 if (skb_copy_bits(skb
, skb_headlen(skb
), skb_tail_pointer(skb
), delta
))
1025 /* Optimization: no fragments, no reasons to preestimate
1026 * size of pulled pages. Superb.
1028 if (!skb_shinfo(skb
)->frag_list
)
1031 /* Estimate size of pulled pages. */
1033 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1034 if (skb_shinfo(skb
)->frags
[i
].size
>= eat
)
1036 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1039 /* If we need update frag list, we are in troubles.
1040 * Certainly, it possible to add an offset to skb data,
1041 * but taking into account that pulling is expected to
1042 * be very rare operation, it is worth to fight against
1043 * further bloating skb head and crucify ourselves here instead.
1044 * Pure masohism, indeed. 8)8)
1047 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1048 struct sk_buff
*clone
= NULL
;
1049 struct sk_buff
*insp
= NULL
;
1054 if (list
->len
<= eat
) {
1055 /* Eaten as whole. */
1060 /* Eaten partially. */
1062 if (skb_shared(list
)) {
1063 /* Sucks! We need to fork list. :-( */
1064 clone
= skb_clone(list
, GFP_ATOMIC
);
1070 /* This may be pulled without
1074 if (!pskb_pull(list
, eat
)) {
1083 /* Free pulled out fragments. */
1084 while ((list
= skb_shinfo(skb
)->frag_list
) != insp
) {
1085 skb_shinfo(skb
)->frag_list
= list
->next
;
1088 /* And insert new clone at head. */
1091 skb_shinfo(skb
)->frag_list
= clone
;
1094 /* Success! Now we may commit changes to skb data. */
1099 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1100 if (skb_shinfo(skb
)->frags
[i
].size
<= eat
) {
1101 put_page(skb_shinfo(skb
)->frags
[i
].page
);
1102 eat
-= skb_shinfo(skb
)->frags
[i
].size
;
1104 skb_shinfo(skb
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1106 skb_shinfo(skb
)->frags
[k
].page_offset
+= eat
;
1107 skb_shinfo(skb
)->frags
[k
].size
-= eat
;
1113 skb_shinfo(skb
)->nr_frags
= k
;
1116 skb
->data_len
-= delta
;
1118 return skb_tail_pointer(skb
);
1121 /* Copy some data bits from skb to kernel buffer. */
1123 int skb_copy_bits(const struct sk_buff
*skb
, int offset
, void *to
, int len
)
1126 int start
= skb_headlen(skb
);
1128 if (offset
> (int)skb
->len
- len
)
1132 if ((copy
= start
- offset
) > 0) {
1135 skb_copy_from_linear_data_offset(skb
, offset
, to
, copy
);
1136 if ((len
-= copy
) == 0)
1142 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1145 BUG_TRAP(start
<= offset
+ len
);
1147 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1148 if ((copy
= end
- offset
) > 0) {
1154 vaddr
= kmap_skb_frag(&skb_shinfo(skb
)->frags
[i
]);
1156 vaddr
+ skb_shinfo(skb
)->frags
[i
].page_offset
+
1157 offset
- start
, copy
);
1158 kunmap_skb_frag(vaddr
);
1160 if ((len
-= copy
) == 0)
1168 if (skb_shinfo(skb
)->frag_list
) {
1169 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1171 for (; list
; list
= list
->next
) {
1174 BUG_TRAP(start
<= offset
+ len
);
1176 end
= start
+ list
->len
;
1177 if ((copy
= end
- offset
) > 0) {
1180 if (skb_copy_bits(list
, offset
- start
,
1183 if ((len
-= copy
) == 0)
1199 * Callback from splice_to_pipe(), if we need to release some pages
1200 * at the end of the spd in case we error'ed out in filling the pipe.
1202 static void sock_spd_release(struct splice_pipe_desc
*spd
, unsigned int i
)
1204 struct sk_buff
*skb
= (struct sk_buff
*) spd
->partial
[i
].private;
1210 * Fill page/offset/length into spd, if it can hold more pages.
1212 static inline int spd_fill_page(struct splice_pipe_desc
*spd
, struct page
*page
,
1213 unsigned int len
, unsigned int offset
,
1214 struct sk_buff
*skb
)
1216 if (unlikely(spd
->nr_pages
== PIPE_BUFFERS
))
1219 spd
->pages
[spd
->nr_pages
] = page
;
1220 spd
->partial
[spd
->nr_pages
].len
= len
;
1221 spd
->partial
[spd
->nr_pages
].offset
= offset
;
1222 spd
->partial
[spd
->nr_pages
].private = (unsigned long) skb_get(skb
);
1228 * Map linear and fragment data from the skb to spd. Returns number of
1231 static int __skb_splice_bits(struct sk_buff
*skb
, unsigned int *offset
,
1232 unsigned int *total_len
,
1233 struct splice_pipe_desc
*spd
)
1235 unsigned int nr_pages
= spd
->nr_pages
;
1236 unsigned int poff
, plen
, len
, toff
, tlen
;
1237 int headlen
, seg
, error
= 0;
1247 * if the offset is greater than the linear part, go directly to
1250 headlen
= skb_headlen(skb
);
1251 if (toff
>= headlen
) {
1257 * first map the linear region into the pages/partial map, skipping
1258 * any potential initial offset.
1261 while (len
< headlen
) {
1262 void *p
= skb
->data
+ len
;
1264 poff
= (unsigned long) p
& (PAGE_SIZE
- 1);
1265 plen
= min_t(unsigned int, headlen
- len
, PAGE_SIZE
- poff
);
1278 plen
= min(plen
, tlen
);
1283 * just jump directly to update and return, no point
1284 * in going over fragments when the output is full.
1286 error
= spd_fill_page(spd
, virt_to_page(p
), plen
, poff
, skb
);
1294 * then map the fragments
1297 for (seg
= 0; seg
< skb_shinfo(skb
)->nr_frags
; seg
++) {
1298 const skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[seg
];
1301 poff
= f
->page_offset
;
1313 plen
= min(plen
, tlen
);
1317 error
= spd_fill_page(spd
, f
->page
, plen
, poff
, skb
);
1325 if (spd
->nr_pages
- nr_pages
) {
1331 /* update the offset to reflect the linear part skip, if any */
1338 * Map data from the skb to a pipe. Should handle both the linear part,
1339 * the fragments, and the frag list. It does NOT handle frag lists within
1340 * the frag list, if such a thing exists. We'd probably need to recurse to
1341 * handle that cleanly.
1343 int skb_splice_bits(struct sk_buff
*__skb
, unsigned int offset
,
1344 struct pipe_inode_info
*pipe
, unsigned int tlen
,
1347 struct partial_page partial
[PIPE_BUFFERS
];
1348 struct page
*pages
[PIPE_BUFFERS
];
1349 struct splice_pipe_desc spd
= {
1353 .ops
= &sock_pipe_buf_ops
,
1354 .spd_release
= sock_spd_release
,
1356 struct sk_buff
*skb
;
1359 * I'd love to avoid the clone here, but tcp_read_sock()
1360 * ignores reference counts and unconditonally kills the sk_buff
1361 * on return from the actor.
1363 skb
= skb_clone(__skb
, GFP_KERNEL
);
1368 * __skb_splice_bits() only fails if the output has no room left,
1369 * so no point in going over the frag_list for the error case.
1371 if (__skb_splice_bits(skb
, &offset
, &tlen
, &spd
))
1377 * now see if we have a frag_list to map
1379 if (skb_shinfo(skb
)->frag_list
) {
1380 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1382 for (; list
&& tlen
; list
= list
->next
) {
1383 if (__skb_splice_bits(list
, &offset
, &tlen
, &spd
))
1390 * drop our reference to the clone, the pipe consumption will
1397 struct sock
*sk
= __skb
->sk
;
1400 * Drop the socket lock, otherwise we have reverse
1401 * locking dependencies between sk_lock and i_mutex
1402 * here as compared to sendfile(). We enter here
1403 * with the socket lock held, and splice_to_pipe() will
1404 * grab the pipe inode lock. For sendfile() emulation,
1405 * we call into ->sendpage() with the i_mutex lock held
1406 * and networking will grab the socket lock.
1409 ret
= splice_to_pipe(pipe
, &spd
);
1418 * skb_store_bits - store bits from kernel buffer to skb
1419 * @skb: destination buffer
1420 * @offset: offset in destination
1421 * @from: source buffer
1422 * @len: number of bytes to copy
1424 * Copy the specified number of bytes from the source buffer to the
1425 * destination skb. This function handles all the messy bits of
1426 * traversing fragment lists and such.
1429 int skb_store_bits(struct sk_buff
*skb
, int offset
, const void *from
, int len
)
1432 int start
= skb_headlen(skb
);
1434 if (offset
> (int)skb
->len
- len
)
1437 if ((copy
= start
- offset
) > 0) {
1440 skb_copy_to_linear_data_offset(skb
, offset
, from
, copy
);
1441 if ((len
-= copy
) == 0)
1447 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1448 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1451 BUG_TRAP(start
<= offset
+ len
);
1453 end
= start
+ frag
->size
;
1454 if ((copy
= end
- offset
) > 0) {
1460 vaddr
= kmap_skb_frag(frag
);
1461 memcpy(vaddr
+ frag
->page_offset
+ offset
- start
,
1463 kunmap_skb_frag(vaddr
);
1465 if ((len
-= copy
) == 0)
1473 if (skb_shinfo(skb
)->frag_list
) {
1474 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1476 for (; list
; list
= list
->next
) {
1479 BUG_TRAP(start
<= offset
+ len
);
1481 end
= start
+ list
->len
;
1482 if ((copy
= end
- offset
) > 0) {
1485 if (skb_store_bits(list
, offset
- start
,
1488 if ((len
-= copy
) == 0)
1503 EXPORT_SYMBOL(skb_store_bits
);
1505 /* Checksum skb data. */
1507 __wsum
skb_checksum(const struct sk_buff
*skb
, int offset
,
1508 int len
, __wsum csum
)
1510 int start
= skb_headlen(skb
);
1511 int i
, copy
= start
- offset
;
1514 /* Checksum header. */
1518 csum
= csum_partial(skb
->data
+ offset
, copy
, csum
);
1519 if ((len
-= copy
) == 0)
1525 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1528 BUG_TRAP(start
<= offset
+ len
);
1530 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1531 if ((copy
= end
- offset
) > 0) {
1534 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1538 vaddr
= kmap_skb_frag(frag
);
1539 csum2
= csum_partial(vaddr
+ frag
->page_offset
+
1540 offset
- start
, copy
, 0);
1541 kunmap_skb_frag(vaddr
);
1542 csum
= csum_block_add(csum
, csum2
, pos
);
1551 if (skb_shinfo(skb
)->frag_list
) {
1552 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1554 for (; list
; list
= list
->next
) {
1557 BUG_TRAP(start
<= offset
+ len
);
1559 end
= start
+ list
->len
;
1560 if ((copy
= end
- offset
) > 0) {
1564 csum2
= skb_checksum(list
, offset
- start
,
1566 csum
= csum_block_add(csum
, csum2
, pos
);
1567 if ((len
-= copy
) == 0)
1580 /* Both of above in one bottle. */
1582 __wsum
skb_copy_and_csum_bits(const struct sk_buff
*skb
, int offset
,
1583 u8
*to
, int len
, __wsum csum
)
1585 int start
= skb_headlen(skb
);
1586 int i
, copy
= start
- offset
;
1593 csum
= csum_partial_copy_nocheck(skb
->data
+ offset
, to
,
1595 if ((len
-= copy
) == 0)
1602 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1605 BUG_TRAP(start
<= offset
+ len
);
1607 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
1608 if ((copy
= end
- offset
) > 0) {
1611 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1615 vaddr
= kmap_skb_frag(frag
);
1616 csum2
= csum_partial_copy_nocheck(vaddr
+
1620 kunmap_skb_frag(vaddr
);
1621 csum
= csum_block_add(csum
, csum2
, pos
);
1631 if (skb_shinfo(skb
)->frag_list
) {
1632 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
1634 for (; list
; list
= list
->next
) {
1638 BUG_TRAP(start
<= offset
+ len
);
1640 end
= start
+ list
->len
;
1641 if ((copy
= end
- offset
) > 0) {
1644 csum2
= skb_copy_and_csum_bits(list
,
1647 csum
= csum_block_add(csum
, csum2
, pos
);
1648 if ((len
-= copy
) == 0)
1661 void skb_copy_and_csum_dev(const struct sk_buff
*skb
, u8
*to
)
1666 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1667 csstart
= skb
->csum_start
- skb_headroom(skb
);
1669 csstart
= skb_headlen(skb
);
1671 BUG_ON(csstart
> skb_headlen(skb
));
1673 skb_copy_from_linear_data(skb
, to
, csstart
);
1676 if (csstart
!= skb
->len
)
1677 csum
= skb_copy_and_csum_bits(skb
, csstart
, to
+ csstart
,
1678 skb
->len
- csstart
, 0);
1680 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1681 long csstuff
= csstart
+ skb
->csum_offset
;
1683 *((__sum16
*)(to
+ csstuff
)) = csum_fold(csum
);
1688 * skb_dequeue - remove from the head of the queue
1689 * @list: list to dequeue from
1691 * Remove the head of the list. The list lock is taken so the function
1692 * may be used safely with other locking list functions. The head item is
1693 * returned or %NULL if the list is empty.
1696 struct sk_buff
*skb_dequeue(struct sk_buff_head
*list
)
1698 unsigned long flags
;
1699 struct sk_buff
*result
;
1701 spin_lock_irqsave(&list
->lock
, flags
);
1702 result
= __skb_dequeue(list
);
1703 spin_unlock_irqrestore(&list
->lock
, flags
);
1708 * skb_dequeue_tail - remove from the tail of the queue
1709 * @list: list to dequeue from
1711 * Remove the tail of the list. The list lock is taken so the function
1712 * may be used safely with other locking list functions. The tail item is
1713 * returned or %NULL if the list is empty.
1715 struct sk_buff
*skb_dequeue_tail(struct sk_buff_head
*list
)
1717 unsigned long flags
;
1718 struct sk_buff
*result
;
1720 spin_lock_irqsave(&list
->lock
, flags
);
1721 result
= __skb_dequeue_tail(list
);
1722 spin_unlock_irqrestore(&list
->lock
, flags
);
1727 * skb_queue_purge - empty a list
1728 * @list: list to empty
1730 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1731 * the list and one reference dropped. This function takes the list
1732 * lock and is atomic with respect to other list locking functions.
1734 void skb_queue_purge(struct sk_buff_head
*list
)
1736 struct sk_buff
*skb
;
1737 while ((skb
= skb_dequeue(list
)) != NULL
)
1742 * skb_queue_head - queue a buffer at the list head
1743 * @list: list to use
1744 * @newsk: buffer to queue
1746 * Queue a buffer at the start of the list. This function takes the
1747 * list lock and can be used safely with other locking &sk_buff functions
1750 * A buffer cannot be placed on two lists at the same time.
1752 void skb_queue_head(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1754 unsigned long flags
;
1756 spin_lock_irqsave(&list
->lock
, flags
);
1757 __skb_queue_head(list
, newsk
);
1758 spin_unlock_irqrestore(&list
->lock
, flags
);
1762 * skb_queue_tail - queue a buffer at the list tail
1763 * @list: list to use
1764 * @newsk: buffer to queue
1766 * Queue a buffer at the tail of the list. This function takes the
1767 * list lock and can be used safely with other locking &sk_buff functions
1770 * A buffer cannot be placed on two lists at the same time.
1772 void skb_queue_tail(struct sk_buff_head
*list
, struct sk_buff
*newsk
)
1774 unsigned long flags
;
1776 spin_lock_irqsave(&list
->lock
, flags
);
1777 __skb_queue_tail(list
, newsk
);
1778 spin_unlock_irqrestore(&list
->lock
, flags
);
1782 * skb_unlink - remove a buffer from a list
1783 * @skb: buffer to remove
1784 * @list: list to use
1786 * Remove a packet from a list. The list locks are taken and this
1787 * function is atomic with respect to other list locked calls
1789 * You must know what list the SKB is on.
1791 void skb_unlink(struct sk_buff
*skb
, struct sk_buff_head
*list
)
1793 unsigned long flags
;
1795 spin_lock_irqsave(&list
->lock
, flags
);
1796 __skb_unlink(skb
, list
);
1797 spin_unlock_irqrestore(&list
->lock
, flags
);
1801 * skb_append - append a buffer
1802 * @old: buffer to insert after
1803 * @newsk: buffer to insert
1804 * @list: list to use
1806 * Place a packet after a given packet in a list. The list locks are taken
1807 * and this function is atomic with respect to other list locked calls.
1808 * A buffer cannot be placed on two lists at the same time.
1810 void skb_append(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1812 unsigned long flags
;
1814 spin_lock_irqsave(&list
->lock
, flags
);
1815 __skb_append(old
, newsk
, list
);
1816 spin_unlock_irqrestore(&list
->lock
, flags
);
1821 * skb_insert - insert a buffer
1822 * @old: buffer to insert before
1823 * @newsk: buffer to insert
1824 * @list: list to use
1826 * Place a packet before a given packet in a list. The list locks are
1827 * taken and this function is atomic with respect to other list locked
1830 * A buffer cannot be placed on two lists at the same time.
1832 void skb_insert(struct sk_buff
*old
, struct sk_buff
*newsk
, struct sk_buff_head
*list
)
1834 unsigned long flags
;
1836 spin_lock_irqsave(&list
->lock
, flags
);
1837 __skb_insert(newsk
, old
->prev
, old
, list
);
1838 spin_unlock_irqrestore(&list
->lock
, flags
);
1841 static inline void skb_split_inside_header(struct sk_buff
*skb
,
1842 struct sk_buff
* skb1
,
1843 const u32 len
, const int pos
)
1847 skb_copy_from_linear_data_offset(skb
, len
, skb_put(skb1
, pos
- len
),
1849 /* And move data appendix as is. */
1850 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
1851 skb_shinfo(skb1
)->frags
[i
] = skb_shinfo(skb
)->frags
[i
];
1853 skb_shinfo(skb1
)->nr_frags
= skb_shinfo(skb
)->nr_frags
;
1854 skb_shinfo(skb
)->nr_frags
= 0;
1855 skb1
->data_len
= skb
->data_len
;
1856 skb1
->len
+= skb1
->data_len
;
1859 skb_set_tail_pointer(skb
, len
);
1862 static inline void skb_split_no_header(struct sk_buff
*skb
,
1863 struct sk_buff
* skb1
,
1864 const u32 len
, int pos
)
1867 const int nfrags
= skb_shinfo(skb
)->nr_frags
;
1869 skb_shinfo(skb
)->nr_frags
= 0;
1870 skb1
->len
= skb1
->data_len
= skb
->len
- len
;
1872 skb
->data_len
= len
- pos
;
1874 for (i
= 0; i
< nfrags
; i
++) {
1875 int size
= skb_shinfo(skb
)->frags
[i
].size
;
1877 if (pos
+ size
> len
) {
1878 skb_shinfo(skb1
)->frags
[k
] = skb_shinfo(skb
)->frags
[i
];
1882 * We have two variants in this case:
1883 * 1. Move all the frag to the second
1884 * part, if it is possible. F.e.
1885 * this approach is mandatory for TUX,
1886 * where splitting is expensive.
1887 * 2. Split is accurately. We make this.
1889 get_page(skb_shinfo(skb
)->frags
[i
].page
);
1890 skb_shinfo(skb1
)->frags
[0].page_offset
+= len
- pos
;
1891 skb_shinfo(skb1
)->frags
[0].size
-= len
- pos
;
1892 skb_shinfo(skb
)->frags
[i
].size
= len
- pos
;
1893 skb_shinfo(skb
)->nr_frags
++;
1897 skb_shinfo(skb
)->nr_frags
++;
1900 skb_shinfo(skb1
)->nr_frags
= k
;
1904 * skb_split - Split fragmented skb to two parts at length len.
1905 * @skb: the buffer to split
1906 * @skb1: the buffer to receive the second part
1907 * @len: new length for skb
1909 void skb_split(struct sk_buff
*skb
, struct sk_buff
*skb1
, const u32 len
)
1911 int pos
= skb_headlen(skb
);
1913 if (len
< pos
) /* Split line is inside header. */
1914 skb_split_inside_header(skb
, skb1
, len
, pos
);
1915 else /* Second chunk has no header, nothing to copy. */
1916 skb_split_no_header(skb
, skb1
, len
, pos
);
1920 * skb_prepare_seq_read - Prepare a sequential read of skb data
1921 * @skb: the buffer to read
1922 * @from: lower offset of data to be read
1923 * @to: upper offset of data to be read
1924 * @st: state variable
1926 * Initializes the specified state variable. Must be called before
1927 * invoking skb_seq_read() for the first time.
1929 void skb_prepare_seq_read(struct sk_buff
*skb
, unsigned int from
,
1930 unsigned int to
, struct skb_seq_state
*st
)
1932 st
->lower_offset
= from
;
1933 st
->upper_offset
= to
;
1934 st
->root_skb
= st
->cur_skb
= skb
;
1935 st
->frag_idx
= st
->stepped_offset
= 0;
1936 st
->frag_data
= NULL
;
1940 * skb_seq_read - Sequentially read skb data
1941 * @consumed: number of bytes consumed by the caller so far
1942 * @data: destination pointer for data to be returned
1943 * @st: state variable
1945 * Reads a block of skb data at &consumed relative to the
1946 * lower offset specified to skb_prepare_seq_read(). Assigns
1947 * the head of the data block to &data and returns the length
1948 * of the block or 0 if the end of the skb data or the upper
1949 * offset has been reached.
1951 * The caller is not required to consume all of the data
1952 * returned, i.e. &consumed is typically set to the number
1953 * of bytes already consumed and the next call to
1954 * skb_seq_read() will return the remaining part of the block.
1956 * Note: The size of each block of data returned can be arbitary,
1957 * this limitation is the cost for zerocopy seqeuental
1958 * reads of potentially non linear data.
1960 * Note: Fragment lists within fragments are not implemented
1961 * at the moment, state->root_skb could be replaced with
1962 * a stack for this purpose.
1964 unsigned int skb_seq_read(unsigned int consumed
, const u8
**data
,
1965 struct skb_seq_state
*st
)
1967 unsigned int block_limit
, abs_offset
= consumed
+ st
->lower_offset
;
1970 if (unlikely(abs_offset
>= st
->upper_offset
))
1974 block_limit
= skb_headlen(st
->cur_skb
) + st
->stepped_offset
;
1976 if (abs_offset
< block_limit
&& !st
->frag_data
) {
1977 *data
= st
->cur_skb
->data
+ (abs_offset
- st
->stepped_offset
);
1978 return block_limit
- abs_offset
;
1981 if (st
->frag_idx
== 0 && !st
->frag_data
)
1982 st
->stepped_offset
+= skb_headlen(st
->cur_skb
);
1984 while (st
->frag_idx
< skb_shinfo(st
->cur_skb
)->nr_frags
) {
1985 frag
= &skb_shinfo(st
->cur_skb
)->frags
[st
->frag_idx
];
1986 block_limit
= frag
->size
+ st
->stepped_offset
;
1988 if (abs_offset
< block_limit
) {
1990 st
->frag_data
= kmap_skb_frag(frag
);
1992 *data
= (u8
*) st
->frag_data
+ frag
->page_offset
+
1993 (abs_offset
- st
->stepped_offset
);
1995 return block_limit
- abs_offset
;
1998 if (st
->frag_data
) {
1999 kunmap_skb_frag(st
->frag_data
);
2000 st
->frag_data
= NULL
;
2004 st
->stepped_offset
+= frag
->size
;
2007 if (st
->frag_data
) {
2008 kunmap_skb_frag(st
->frag_data
);
2009 st
->frag_data
= NULL
;
2012 if (st
->root_skb
== st
->cur_skb
&&
2013 skb_shinfo(st
->root_skb
)->frag_list
) {
2014 st
->cur_skb
= skb_shinfo(st
->root_skb
)->frag_list
;
2017 } else if (st
->cur_skb
->next
) {
2018 st
->cur_skb
= st
->cur_skb
->next
;
2027 * skb_abort_seq_read - Abort a sequential read of skb data
2028 * @st: state variable
2030 * Must be called if skb_seq_read() was not called until it
2033 void skb_abort_seq_read(struct skb_seq_state
*st
)
2036 kunmap_skb_frag(st
->frag_data
);
2039 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2041 static unsigned int skb_ts_get_next_block(unsigned int offset
, const u8
**text
,
2042 struct ts_config
*conf
,
2043 struct ts_state
*state
)
2045 return skb_seq_read(offset
, text
, TS_SKB_CB(state
));
2048 static void skb_ts_finish(struct ts_config
*conf
, struct ts_state
*state
)
2050 skb_abort_seq_read(TS_SKB_CB(state
));
2054 * skb_find_text - Find a text pattern in skb data
2055 * @skb: the buffer to look in
2056 * @from: search offset
2058 * @config: textsearch configuration
2059 * @state: uninitialized textsearch state variable
2061 * Finds a pattern in the skb data according to the specified
2062 * textsearch configuration. Use textsearch_next() to retrieve
2063 * subsequent occurrences of the pattern. Returns the offset
2064 * to the first occurrence or UINT_MAX if no match was found.
2066 unsigned int skb_find_text(struct sk_buff
*skb
, unsigned int from
,
2067 unsigned int to
, struct ts_config
*config
,
2068 struct ts_state
*state
)
2072 config
->get_next_block
= skb_ts_get_next_block
;
2073 config
->finish
= skb_ts_finish
;
2075 skb_prepare_seq_read(skb
, from
, to
, TS_SKB_CB(state
));
2077 ret
= textsearch_find(config
, state
);
2078 return (ret
<= to
- from
? ret
: UINT_MAX
);
2082 * skb_append_datato_frags: - append the user data to a skb
2083 * @sk: sock structure
2084 * @skb: skb structure to be appened with user data.
2085 * @getfrag: call back function to be used for getting the user data
2086 * @from: pointer to user message iov
2087 * @length: length of the iov message
2089 * Description: This procedure append the user data in the fragment part
2090 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2092 int skb_append_datato_frags(struct sock
*sk
, struct sk_buff
*skb
,
2093 int (*getfrag
)(void *from
, char *to
, int offset
,
2094 int len
, int odd
, struct sk_buff
*skb
),
2095 void *from
, int length
)
2098 skb_frag_t
*frag
= NULL
;
2099 struct page
*page
= NULL
;
2105 /* Return error if we don't have space for new frag */
2106 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2107 if (frg_cnt
>= MAX_SKB_FRAGS
)
2110 /* allocate a new page for next frag */
2111 page
= alloc_pages(sk
->sk_allocation
, 0);
2113 /* If alloc_page fails just return failure and caller will
2114 * free previous allocated pages by doing kfree_skb()
2119 /* initialize the next frag */
2120 sk
->sk_sndmsg_page
= page
;
2121 sk
->sk_sndmsg_off
= 0;
2122 skb_fill_page_desc(skb
, frg_cnt
, page
, 0, 0);
2123 skb
->truesize
+= PAGE_SIZE
;
2124 atomic_add(PAGE_SIZE
, &sk
->sk_wmem_alloc
);
2126 /* get the new initialized frag */
2127 frg_cnt
= skb_shinfo(skb
)->nr_frags
;
2128 frag
= &skb_shinfo(skb
)->frags
[frg_cnt
- 1];
2130 /* copy the user data to page */
2131 left
= PAGE_SIZE
- frag
->page_offset
;
2132 copy
= (length
> left
)? left
: length
;
2134 ret
= getfrag(from
, (page_address(frag
->page
) +
2135 frag
->page_offset
+ frag
->size
),
2136 offset
, copy
, 0, skb
);
2140 /* copy was successful so update the size parameters */
2141 sk
->sk_sndmsg_off
+= copy
;
2144 skb
->data_len
+= copy
;
2148 } while (length
> 0);
2154 * skb_pull_rcsum - pull skb and update receive checksum
2155 * @skb: buffer to update
2156 * @start: start of data before pull
2157 * @len: length of data pulled
2159 * This function performs an skb_pull on the packet and updates
2160 * update the CHECKSUM_COMPLETE checksum. It should be used on
2161 * receive path processing instead of skb_pull unless you know
2162 * that the checksum difference is zero (e.g., a valid IP header)
2163 * or you are setting ip_summed to CHECKSUM_NONE.
2165 unsigned char *skb_pull_rcsum(struct sk_buff
*skb
, unsigned int len
)
2167 BUG_ON(len
> skb
->len
);
2169 BUG_ON(skb
->len
< skb
->data_len
);
2170 skb_postpull_rcsum(skb
, skb
->data
, len
);
2171 return skb
->data
+= len
;
2174 EXPORT_SYMBOL_GPL(skb_pull_rcsum
);
2177 * skb_segment - Perform protocol segmentation on skb.
2178 * @skb: buffer to segment
2179 * @features: features for the output path (see dev->features)
2181 * This function performs segmentation on the given skb. It returns
2182 * the segment at the given position. It returns NULL if there are
2183 * no more segments to generate, or when an error is encountered.
2185 struct sk_buff
*skb_segment(struct sk_buff
*skb
, int features
)
2187 struct sk_buff
*segs
= NULL
;
2188 struct sk_buff
*tail
= NULL
;
2189 unsigned int mss
= skb_shinfo(skb
)->gso_size
;
2190 unsigned int doffset
= skb
->data
- skb_mac_header(skb
);
2191 unsigned int offset
= doffset
;
2192 unsigned int headroom
;
2194 int sg
= features
& NETIF_F_SG
;
2195 int nfrags
= skb_shinfo(skb
)->nr_frags
;
2200 __skb_push(skb
, doffset
);
2201 headroom
= skb_headroom(skb
);
2202 pos
= skb_headlen(skb
);
2205 struct sk_buff
*nskb
;
2211 len
= skb
->len
- offset
;
2215 hsize
= skb_headlen(skb
) - offset
;
2218 if (hsize
> len
|| !sg
)
2221 nskb
= alloc_skb(hsize
+ doffset
+ headroom
, GFP_ATOMIC
);
2222 if (unlikely(!nskb
))
2231 __copy_skb_header(nskb
, skb
);
2232 nskb
->mac_len
= skb
->mac_len
;
2234 nskb
->ctf_mac_len
= skb
->ctf_mac_len
; /* used by Broadcom CTF driver! */
2237 skb_reserve(nskb
, headroom
);
2238 skb_reset_mac_header(nskb
);
2239 skb_set_network_header(nskb
, skb
->mac_len
);
2240 nskb
->transport_header
= (nskb
->network_header
+
2241 skb_network_header_len(skb
));
2242 skb_copy_from_linear_data(skb
, skb_put(nskb
, doffset
),
2245 nskb
->ip_summed
= CHECKSUM_NONE
;
2246 nskb
->csum
= skb_copy_and_csum_bits(skb
, offset
,
2252 frag
= skb_shinfo(nskb
)->frags
;
2255 skb_copy_from_linear_data_offset(skb
, offset
,
2256 skb_put(nskb
, hsize
), hsize
);
2258 while (pos
< offset
+ len
) {
2259 BUG_ON(i
>= nfrags
);
2261 *frag
= skb_shinfo(skb
)->frags
[i
];
2262 get_page(frag
->page
);
2266 frag
->page_offset
+= offset
- pos
;
2267 frag
->size
-= offset
- pos
;
2272 if (pos
+ size
<= offset
+ len
) {
2276 frag
->size
-= pos
+ size
- (offset
+ len
);
2283 skb_shinfo(nskb
)->nr_frags
= k
;
2284 nskb
->data_len
= len
- hsize
;
2285 nskb
->len
+= nskb
->data_len
;
2286 nskb
->truesize
+= nskb
->data_len
;
2287 } while ((offset
+= len
) < skb
->len
);
2292 while ((skb
= segs
)) {
2296 return ERR_PTR(err
);
2299 EXPORT_SYMBOL_GPL(skb_segment
);
2301 void __init
skb_init(void)
2303 skbuff_head_cache
= kmem_cache_create("skbuff_head_cache",
2304 sizeof(struct sk_buff
),
2306 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2308 skbuff_fclone_cache
= kmem_cache_create("skbuff_fclone_cache",
2309 (2*sizeof(struct sk_buff
)) +
2312 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
,
2317 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2318 * @skb: Socket buffer containing the buffers to be mapped
2319 * @sg: The scatter-gather list to map into
2320 * @offset: The offset into the buffer's contents to start mapping
2321 * @len: Length of buffer space to be mapped
2323 * Fill the specified scatter-gather list with mappings/pointers into a
2324 * region of the buffer space attached to a socket buffer.
2327 skb_to_sgvec(struct sk_buff
*skb
, struct scatterlist
*sg
, int offset
, int len
)
2329 int start
= skb_headlen(skb
);
2330 int i
, copy
= start
- offset
;
2336 sg
[elt
].page
= virt_to_page(skb
->data
+ offset
);
2337 sg
[elt
].offset
= (unsigned long)(skb
->data
+ offset
) % PAGE_SIZE
;
2338 sg
[elt
].length
= copy
;
2340 if ((len
-= copy
) == 0)
2345 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
2348 BUG_TRAP(start
<= offset
+ len
);
2350 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
2351 if ((copy
= end
- offset
) > 0) {
2352 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2356 sg
[elt
].page
= frag
->page
;
2357 sg
[elt
].offset
= frag
->page_offset
+offset
-start
;
2358 sg
[elt
].length
= copy
;
2367 if (skb_shinfo(skb
)->frag_list
) {
2368 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
2370 for (; list
; list
= list
->next
) {
2373 BUG_TRAP(start
<= offset
+ len
);
2375 end
= start
+ list
->len
;
2376 if ((copy
= end
- offset
) > 0) {
2379 elt
+= skb_to_sgvec(list
, sg
+elt
, offset
- start
, copy
);
2380 if ((len
-= copy
) == 0)
2392 * skb_cow_data - Check that a socket buffer's data buffers are writable
2393 * @skb: The socket buffer to check.
2394 * @tailbits: Amount of trailing space to be added
2395 * @trailer: Returned pointer to the skb where the @tailbits space begins
2397 * Make sure that the data buffers attached to a socket buffer are
2398 * writable. If they are not, private copies are made of the data buffers
2399 * and the socket buffer is set to use these instead.
2401 * If @tailbits is given, make sure that there is space to write @tailbits
2402 * bytes of data beyond current end of socket buffer. @trailer will be
2403 * set to point to the skb in which this space begins.
2405 * The number of scatterlist elements required to completely map the
2406 * COW'd and extended socket buffer will be returned.
2408 int skb_cow_data(struct sk_buff
*skb
, int tailbits
, struct sk_buff
**trailer
)
2412 struct sk_buff
*skb1
, **skb_p
;
2414 /* If skb is cloned or its head is paged, reallocate
2415 * head pulling out all the pages (pages are considered not writable
2416 * at the moment even if they are anonymous).
2418 if ((skb_cloned(skb
) || skb_shinfo(skb
)->nr_frags
) &&
2419 __pskb_pull_tail(skb
, skb_pagelen(skb
)-skb_headlen(skb
)) == NULL
)
2422 /* Easy case. Most of packets will go this way. */
2423 if (!skb_shinfo(skb
)->frag_list
) {
2424 /* A little of trouble, not enough of space for trailer.
2425 * This should not happen, when stack is tuned to generate
2426 * good frames. OK, on miss we reallocate and reserve even more
2427 * space, 128 bytes is fair. */
2429 if (skb_tailroom(skb
) < tailbits
&&
2430 pskb_expand_head(skb
, 0, tailbits
-skb_tailroom(skb
)+128, GFP_ATOMIC
))
2438 /* Misery. We are in troubles, going to mincer fragments... */
2441 skb_p
= &skb_shinfo(skb
)->frag_list
;
2444 while ((skb1
= *skb_p
) != NULL
) {
2447 /* The fragment is partially pulled by someone,
2448 * this can happen on input. Copy it and everything
2451 if (skb_shared(skb1
))
2454 /* If the skb is the last, worry about trailer. */
2456 if (skb1
->next
== NULL
&& tailbits
) {
2457 if (skb_shinfo(skb1
)->nr_frags
||
2458 skb_shinfo(skb1
)->frag_list
||
2459 skb_tailroom(skb1
) < tailbits
)
2460 ntail
= tailbits
+ 128;
2466 skb_shinfo(skb1
)->nr_frags
||
2467 skb_shinfo(skb1
)->frag_list
) {
2468 struct sk_buff
*skb2
;
2470 /* Fuck, we are miserable poor guys... */
2472 skb2
= skb_copy(skb1
, GFP_ATOMIC
);
2474 skb2
= skb_copy_expand(skb1
,
2478 if (unlikely(skb2
== NULL
))
2482 skb_set_owner_w(skb2
, skb1
->sk
);
2484 /* Looking around. Are we still alive?
2485 * OK, link new skb, drop old one */
2487 skb2
->next
= skb1
->next
;
2494 skb_p
= &skb1
->next
;
2500 EXPORT_SYMBOL(___pskb_trim
);
2501 EXPORT_SYMBOL(__kfree_skb
);
2502 EXPORT_SYMBOL(kfree_skb
);
2503 EXPORT_SYMBOL(__pskb_pull_tail
);
2504 EXPORT_SYMBOL(__alloc_skb
);
2505 EXPORT_SYMBOL(__netdev_alloc_skb
);
2506 EXPORT_SYMBOL(pskb_copy
);
2507 EXPORT_SYMBOL(pskb_expand_head
);
2508 EXPORT_SYMBOL(skb_checksum
);
2509 EXPORT_SYMBOL(skb_clone
);
2510 EXPORT_SYMBOL(skb_copy
);
2511 EXPORT_SYMBOL(skb_copy_and_csum_bits
);
2512 EXPORT_SYMBOL(skb_copy_and_csum_dev
);
2513 EXPORT_SYMBOL(skb_copy_bits
);
2514 EXPORT_SYMBOL(skb_copy_expand
);
2515 EXPORT_SYMBOL(skb_over_panic
);
2516 EXPORT_SYMBOL(skb_pad
);
2517 EXPORT_SYMBOL(skb_realloc_headroom
);
2518 EXPORT_SYMBOL(skb_under_panic
);
2519 EXPORT_SYMBOL(skb_dequeue
);
2520 EXPORT_SYMBOL(skb_dequeue_tail
);
2521 EXPORT_SYMBOL(skb_insert
);
2522 EXPORT_SYMBOL(skb_queue_purge
);
2523 EXPORT_SYMBOL(skb_queue_head
);
2524 EXPORT_SYMBOL(skb_queue_tail
);
2525 EXPORT_SYMBOL(skb_unlink
);
2526 EXPORT_SYMBOL(skb_append
);
2527 EXPORT_SYMBOL(skb_split
);
2528 EXPORT_SYMBOL(skb_prepare_seq_read
);
2529 EXPORT_SYMBOL(skb_seq_read
);
2530 EXPORT_SYMBOL(skb_abort_seq_read
);
2531 EXPORT_SYMBOL(skb_find_text
);
2532 EXPORT_SYMBOL(skb_append_datato_frags
);
2534 EXPORT_SYMBOL_GPL(skb_to_sgvec
);
2535 EXPORT_SYMBOL_GPL(skb_cow_data
);