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/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
60 #include <net/protocol.h>
63 #include <net/checksum.h>
66 #include <asm/uaccess.h>
67 #include <asm/system.h>
71 static struct kmem_cache *skbuff_head_cache __read_mostly;
72 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
81 * skb_over_panic - private function
86 * Out of line support code for skb_put(). Not user callable.
88 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%#lx end:%#lx dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data,
93 (unsigned long)skb->tail, (unsigned long)skb->end,
94 skb->dev ? skb->dev->name : "<NULL>");
99 * skb_under_panic - private function
104 * Out of line support code for skb_push(). Not user callable.
107 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
109 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
110 "data:%p tail:%#lx end:%#lx dev:%s\n",
111 here, skb->len, sz, skb->head, skb->data,
112 (unsigned long)skb->tail, (unsigned long)skb->end,
113 skb->dev ? skb->dev->name : "<NULL>");
117 void skb_truesize_bug(struct sk_buff *skb)
119 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
120 "len=%u, sizeof(sk_buff)=%Zd\n",
121 skb->truesize, skb->len, sizeof(struct sk_buff));
123 EXPORT_SYMBOL(skb_truesize_bug);
125 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
126 * 'private' fields and also do memory statistics to find all the
132 * __alloc_skb - allocate a network buffer
133 * @size: size to allocate
134 * @gfp_mask: allocation mask
135 * @fclone: allocate from fclone cache instead of head cache
136 * and allocate a cloned (child) skb
137 * @node: numa node to allocate memory on
139 * Allocate a new &sk_buff. The returned buffer has no headroom and a
140 * tail room of size bytes. The object has a reference count of one.
141 * The return is the buffer. On a failure the return is %NULL.
143 * Buffers may only be allocated from interrupts using a @gfp_mask of
146 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
147 int fclone, int node)
149 struct kmem_cache *cache;
150 struct skb_shared_info *shinfo;
154 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
157 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
161 size = SKB_DATA_ALIGN(size);
162 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
168 * See comment in sk_buff definition, just before the 'tail' member
170 memset(skb, 0, offsetof(struct sk_buff, tail));
171 skb->truesize = size + sizeof(struct sk_buff);
172 atomic_set(&skb->users, 1);
175 skb_reset_tail_pointer(skb);
176 skb->end = skb->tail + size;
177 /* make sure we initialize shinfo sequentially */
178 shinfo = skb_shinfo(skb);
179 atomic_set(&shinfo->dataref, 1);
180 shinfo->nr_frags = 0;
181 shinfo->gso_size = 0;
182 shinfo->gso_segs = 0;
183 shinfo->gso_type = 0;
184 shinfo->ip6_frag_id = 0;
185 shinfo->frag_list = NULL;
188 struct sk_buff *child = skb + 1;
189 atomic_t *fclone_ref = (atomic_t *) (child + 1);
191 skb->fclone = SKB_FCLONE_ORIG;
192 atomic_set(fclone_ref, 1);
194 child->fclone = SKB_FCLONE_UNAVAILABLE;
199 kmem_cache_free(cache, skb);
205 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
206 * @dev: network device to receive on
207 * @length: length to allocate
208 * @gfp_mask: get_free_pages mask, passed to alloc_skb
210 * Allocate a new &sk_buff and assign it a usage count of one. The
211 * buffer has unspecified headroom built in. Users should allocate
212 * the headroom they think they need without accounting for the
213 * built in space. The built in space is used for optimisations.
215 * %NULL is returned if there is no free memory.
217 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
218 unsigned int length, gfp_t gfp_mask)
220 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
223 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
225 skb_reserve(skb, NET_SKB_PAD);
231 static void skb_drop_list(struct sk_buff **listp)
233 struct sk_buff *list = *listp;
238 struct sk_buff *this = list;
244 static inline void skb_drop_fraglist(struct sk_buff *skb)
246 skb_drop_list(&skb_shinfo(skb)->frag_list);
249 static void skb_clone_fraglist(struct sk_buff *skb)
251 struct sk_buff *list;
253 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
257 static void skb_release_data(struct sk_buff *skb)
260 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
261 &skb_shinfo(skb)->dataref)) {
262 if (skb_shinfo(skb)->nr_frags) {
264 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
265 put_page(skb_shinfo(skb)->frags[i].page);
268 if (skb_shinfo(skb)->frag_list)
269 skb_drop_fraglist(skb);
276 * Free an skbuff by memory without cleaning the state.
278 static void kfree_skbmem(struct sk_buff *skb)
280 struct sk_buff *other;
281 atomic_t *fclone_ref;
283 switch (skb->fclone) {
284 case SKB_FCLONE_UNAVAILABLE:
285 kmem_cache_free(skbuff_head_cache, skb);
288 case SKB_FCLONE_ORIG:
289 fclone_ref = (atomic_t *) (skb + 2);
290 if (atomic_dec_and_test(fclone_ref))
291 kmem_cache_free(skbuff_fclone_cache, skb);
294 case SKB_FCLONE_CLONE:
295 fclone_ref = (atomic_t *) (skb + 1);
298 /* The clone portion is available for
299 * fast-cloning again.
301 skb->fclone = SKB_FCLONE_UNAVAILABLE;
303 if (atomic_dec_and_test(fclone_ref))
304 kmem_cache_free(skbuff_fclone_cache, other);
309 /* Free everything but the sk_buff shell. */
310 static void skb_release_all(struct sk_buff *skb)
312 dst_release(skb->dst);
314 secpath_put(skb->sp);
316 if (skb->destructor) {
318 skb->destructor(skb);
320 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
321 nf_conntrack_put(skb->nfct);
322 nf_conntrack_put_reasm(skb->nfct_reasm);
324 #ifdef CONFIG_BRIDGE_NETFILTER
325 nf_bridge_put(skb->nf_bridge);
327 /* XXX: IS this still necessary? - JHS */
328 #ifdef CONFIG_NET_SCHED
330 #ifdef CONFIG_NET_CLS_ACT
334 skb_release_data(skb);
338 * __kfree_skb - private function
341 * Free an sk_buff. Release anything attached to the buffer.
342 * Clean the state. This is an internal helper function. Users should
343 * always call kfree_skb
346 void __kfree_skb(struct sk_buff *skb)
348 skb_release_all(skb);
353 * kfree_skb - free an sk_buff
354 * @skb: buffer to free
356 * Drop a reference to the buffer and free it if the usage count has
359 void kfree_skb(struct sk_buff *skb)
363 if (likely(atomic_read(&skb->users) == 1))
365 else if (likely(!atomic_dec_and_test(&skb->users)))
370 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
372 new->tstamp = old->tstamp;
374 new->transport_header = old->transport_header;
375 new->network_header = old->network_header;
376 new->mac_header = old->mac_header;
377 new->dst = dst_clone(old->dst);
379 new->sp = secpath_get(old->sp);
381 memcpy(new->cb, old->cb, sizeof(old->cb));
382 new->csum_start = old->csum_start;
383 new->csum_offset = old->csum_offset;
384 new->local_df = old->local_df;
385 new->pkt_type = old->pkt_type;
386 new->ip_summed = old->ip_summed;
387 skb_copy_queue_mapping(new, old);
388 new->priority = old->priority;
389 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
390 new->ipvs_property = old->ipvs_property;
392 new->protocol = old->protocol;
393 new->mark = old->mark;
395 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
396 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
397 new->nf_trace = old->nf_trace;
399 #ifdef CONFIG_NET_SCHED
400 new->tc_index = old->tc_index;
401 #ifdef CONFIG_NET_CLS_ACT
402 new->tc_verd = old->tc_verd;
405 skb_copy_secmark(new, old);
408 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
410 #define C(x) n->x = skb->x
412 n->next = n->prev = NULL;
414 __copy_skb_header(n, skb);
420 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
422 n->destructor = NULL;
424 atomic_set(&n->users, 1);
430 atomic_inc(&(skb_shinfo(skb)->dataref));
438 * skb_morph - morph one skb into another
439 * @dst: the skb to receive the contents
440 * @src: the skb to supply the contents
442 * This is identical to skb_clone except that the target skb is
443 * supplied by the user.
445 * The target skb is returned upon exit.
447 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
449 skb_release_all(dst);
450 return __skb_clone(dst, src);
452 EXPORT_SYMBOL_GPL(skb_morph);
455 * skb_clone - duplicate an sk_buff
456 * @skb: buffer to clone
457 * @gfp_mask: allocation priority
459 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
460 * copies share the same packet data but not structure. The new
461 * buffer has a reference count of 1. If the allocation fails the
462 * function returns %NULL otherwise the new buffer is returned.
464 * If this function is called from an interrupt gfp_mask() must be
468 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
473 if (skb->fclone == SKB_FCLONE_ORIG &&
474 n->fclone == SKB_FCLONE_UNAVAILABLE) {
475 atomic_t *fclone_ref = (atomic_t *) (n + 1);
476 n->fclone = SKB_FCLONE_CLONE;
477 atomic_inc(fclone_ref);
479 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
482 n->fclone = SKB_FCLONE_UNAVAILABLE;
485 return __skb_clone(n, skb);
488 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
490 #ifndef NET_SKBUFF_DATA_USES_OFFSET
492 * Shift between the two data areas in bytes
494 unsigned long offset = new->data - old->data;
497 __copy_skb_header(new, old);
499 #ifndef NET_SKBUFF_DATA_USES_OFFSET
500 /* {transport,network,mac}_header are relative to skb->head */
501 new->transport_header += offset;
502 new->network_header += offset;
503 new->mac_header += offset;
505 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
506 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
507 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
511 * skb_copy - create private copy of an sk_buff
512 * @skb: buffer to copy
513 * @gfp_mask: allocation priority
515 * Make a copy of both an &sk_buff and its data. This is used when the
516 * caller wishes to modify the data and needs a private copy of the
517 * data to alter. Returns %NULL on failure or the pointer to the buffer
518 * on success. The returned buffer has a reference count of 1.
520 * As by-product this function converts non-linear &sk_buff to linear
521 * one, so that &sk_buff becomes completely private and caller is allowed
522 * to modify all the data of returned buffer. This means that this
523 * function is not recommended for use in circumstances when only
524 * header is going to be modified. Use pskb_copy() instead.
527 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
529 int headerlen = skb->data - skb->head;
531 * Allocate the copy buffer
534 #ifdef NET_SKBUFF_DATA_USES_OFFSET
535 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
537 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
542 /* Set the data pointer */
543 skb_reserve(n, headerlen);
544 /* Set the tail pointer and length */
545 skb_put(n, skb->len);
547 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
550 copy_skb_header(n, skb);
556 * pskb_copy - create copy of an sk_buff with private head.
557 * @skb: buffer to copy
558 * @gfp_mask: allocation priority
560 * Make a copy of both an &sk_buff and part of its data, located
561 * in header. Fragmented data remain shared. This is used when
562 * the caller wishes to modify only header of &sk_buff and needs
563 * private copy of the header to alter. Returns %NULL on failure
564 * or the pointer to the buffer on success.
565 * The returned buffer has a reference count of 1.
568 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
571 * Allocate the copy buffer
574 #ifdef NET_SKBUFF_DATA_USES_OFFSET
575 n = alloc_skb(skb->end, gfp_mask);
577 n = alloc_skb(skb->end - skb->head, gfp_mask);
582 /* Set the data pointer */
583 skb_reserve(n, skb->data - skb->head);
584 /* Set the tail pointer and length */
585 skb_put(n, skb_headlen(skb));
587 skb_copy_from_linear_data(skb, n->data, n->len);
589 n->truesize += skb->data_len;
590 n->data_len = skb->data_len;
593 if (skb_shinfo(skb)->nr_frags) {
596 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
597 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
598 get_page(skb_shinfo(n)->frags[i].page);
600 skb_shinfo(n)->nr_frags = i;
603 if (skb_shinfo(skb)->frag_list) {
604 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
605 skb_clone_fraglist(n);
608 copy_skb_header(n, skb);
614 * pskb_expand_head - reallocate header of &sk_buff
615 * @skb: buffer to reallocate
616 * @nhead: room to add at head
617 * @ntail: room to add at tail
618 * @gfp_mask: allocation priority
620 * Expands (or creates identical copy, if &nhead and &ntail are zero)
621 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
622 * reference count of 1. Returns zero in the case of success or error,
623 * if expansion failed. In the last case, &sk_buff is not changed.
625 * All the pointers pointing into skb header may change and must be
626 * reloaded after call to this function.
629 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
634 #ifdef NET_SKBUFF_DATA_USES_OFFSET
635 int size = nhead + skb->end + ntail;
637 int size = nhead + (skb->end - skb->head) + ntail;
644 size = SKB_DATA_ALIGN(size);
646 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
650 /* Copy only real data... and, alas, header. This should be
651 * optimized for the cases when header is void. */
652 #ifdef NET_SKBUFF_DATA_USES_OFFSET
653 memcpy(data + nhead, skb->head, skb->tail);
655 memcpy(data + nhead, skb->head, skb->tail - skb->head);
657 memcpy(data + size, skb_end_pointer(skb),
658 sizeof(struct skb_shared_info));
660 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
661 get_page(skb_shinfo(skb)->frags[i].page);
663 if (skb_shinfo(skb)->frag_list)
664 skb_clone_fraglist(skb);
666 skb_release_data(skb);
668 off = (data + nhead) - skb->head;
672 #ifdef NET_SKBUFF_DATA_USES_OFFSET
676 skb->end = skb->head + size;
678 /* {transport,network,mac}_header and tail are relative to skb->head */
680 skb->transport_header += off;
681 skb->network_header += off;
682 skb->mac_header += off;
683 skb->csum_start += nhead;
687 atomic_set(&skb_shinfo(skb)->dataref, 1);
694 /* Make private copy of skb with writable head and some headroom */
696 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
698 struct sk_buff *skb2;
699 int delta = headroom - skb_headroom(skb);
702 skb2 = pskb_copy(skb, GFP_ATOMIC);
704 skb2 = skb_clone(skb, GFP_ATOMIC);
705 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
716 * skb_copy_expand - copy and expand sk_buff
717 * @skb: buffer to copy
718 * @newheadroom: new free bytes at head
719 * @newtailroom: new free bytes at tail
720 * @gfp_mask: allocation priority
722 * Make a copy of both an &sk_buff and its data and while doing so
723 * allocate additional space.
725 * This is used when the caller wishes to modify the data and needs a
726 * private copy of the data to alter as well as more space for new fields.
727 * Returns %NULL on failure or the pointer to the buffer
728 * on success. The returned buffer has a reference count of 1.
730 * You must pass %GFP_ATOMIC as the allocation priority if this function
731 * is called from an interrupt.
733 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
734 int newheadroom, int newtailroom,
738 * Allocate the copy buffer
740 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
742 int oldheadroom = skb_headroom(skb);
743 int head_copy_len, head_copy_off;
749 skb_reserve(n, newheadroom);
751 /* Set the tail pointer and length */
752 skb_put(n, skb->len);
754 head_copy_len = oldheadroom;
756 if (newheadroom <= head_copy_len)
757 head_copy_len = newheadroom;
759 head_copy_off = newheadroom - head_copy_len;
761 /* Copy the linear header and data. */
762 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
763 skb->len + head_copy_len))
766 copy_skb_header(n, skb);
768 off = newheadroom - oldheadroom;
769 n->csum_start += off;
770 #ifdef NET_SKBUFF_DATA_USES_OFFSET
771 n->transport_header += off;
772 n->network_header += off;
773 n->mac_header += off;
780 * skb_pad - zero pad the tail of an skb
781 * @skb: buffer to pad
784 * Ensure that a buffer is followed by a padding area that is zero
785 * filled. Used by network drivers which may DMA or transfer data
786 * beyond the buffer end onto the wire.
788 * May return error in out of memory cases. The skb is freed on error.
791 int skb_pad(struct sk_buff *skb, int pad)
796 /* If the skbuff is non linear tailroom is always zero.. */
797 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
798 memset(skb->data+skb->len, 0, pad);
802 ntail = skb->data_len + pad - (skb->end - skb->tail);
803 if (likely(skb_cloned(skb) || ntail > 0)) {
804 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
809 /* FIXME: The use of this function with non-linear skb's really needs
812 err = skb_linearize(skb);
816 memset(skb->data + skb->len, 0, pad);
824 /* Trims skb to length len. It can change skb pointers.
827 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
829 struct sk_buff **fragp;
830 struct sk_buff *frag;
831 int offset = skb_headlen(skb);
832 int nfrags = skb_shinfo(skb)->nr_frags;
836 if (skb_cloned(skb) &&
837 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
844 for (; i < nfrags; i++) {
845 int end = offset + skb_shinfo(skb)->frags[i].size;
852 skb_shinfo(skb)->frags[i++].size = len - offset;
855 skb_shinfo(skb)->nr_frags = i;
857 for (; i < nfrags; i++)
858 put_page(skb_shinfo(skb)->frags[i].page);
860 if (skb_shinfo(skb)->frag_list)
861 skb_drop_fraglist(skb);
865 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
866 fragp = &frag->next) {
867 int end = offset + frag->len;
869 if (skb_shared(frag)) {
870 struct sk_buff *nfrag;
872 nfrag = skb_clone(frag, GFP_ATOMIC);
873 if (unlikely(!nfrag))
876 nfrag->next = frag->next;
888 unlikely((err = pskb_trim(frag, len - offset))))
892 skb_drop_list(&frag->next);
897 if (len > skb_headlen(skb)) {
898 skb->data_len -= skb->len - len;
903 skb_set_tail_pointer(skb, len);
910 * __pskb_pull_tail - advance tail of skb header
911 * @skb: buffer to reallocate
912 * @delta: number of bytes to advance tail
914 * The function makes a sense only on a fragmented &sk_buff,
915 * it expands header moving its tail forward and copying necessary
916 * data from fragmented part.
918 * &sk_buff MUST have reference count of 1.
920 * Returns %NULL (and &sk_buff does not change) if pull failed
921 * or value of new tail of skb in the case of success.
923 * All the pointers pointing into skb header may change and must be
924 * reloaded after call to this function.
927 /* Moves tail of skb head forward, copying data from fragmented part,
928 * when it is necessary.
929 * 1. It may fail due to malloc failure.
930 * 2. It may change skb pointers.
932 * It is pretty complicated. Luckily, it is called only in exceptional cases.
934 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
936 /* If skb has not enough free space at tail, get new one
937 * plus 128 bytes for future expansions. If we have enough
938 * room at tail, reallocate without expansion only if skb is cloned.
940 int i, k, eat = (skb->tail + delta) - skb->end;
942 if (eat > 0 || skb_cloned(skb)) {
943 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
948 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
951 /* Optimization: no fragments, no reasons to preestimate
952 * size of pulled pages. Superb.
954 if (!skb_shinfo(skb)->frag_list)
957 /* Estimate size of pulled pages. */
959 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
960 if (skb_shinfo(skb)->frags[i].size >= eat)
962 eat -= skb_shinfo(skb)->frags[i].size;
965 /* If we need update frag list, we are in troubles.
966 * Certainly, it possible to add an offset to skb data,
967 * but taking into account that pulling is expected to
968 * be very rare operation, it is worth to fight against
969 * further bloating skb head and crucify ourselves here instead.
970 * Pure masohism, indeed. 8)8)
973 struct sk_buff *list = skb_shinfo(skb)->frag_list;
974 struct sk_buff *clone = NULL;
975 struct sk_buff *insp = NULL;
980 if (list->len <= eat) {
981 /* Eaten as whole. */
986 /* Eaten partially. */
988 if (skb_shared(list)) {
989 /* Sucks! We need to fork list. :-( */
990 clone = skb_clone(list, GFP_ATOMIC);
996 /* This may be pulled without
1000 if (!pskb_pull(list, eat)) {
1009 /* Free pulled out fragments. */
1010 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1011 skb_shinfo(skb)->frag_list = list->next;
1014 /* And insert new clone at head. */
1017 skb_shinfo(skb)->frag_list = clone;
1020 /* Success! Now we may commit changes to skb data. */
1025 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1026 if (skb_shinfo(skb)->frags[i].size <= eat) {
1027 put_page(skb_shinfo(skb)->frags[i].page);
1028 eat -= skb_shinfo(skb)->frags[i].size;
1030 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1032 skb_shinfo(skb)->frags[k].page_offset += eat;
1033 skb_shinfo(skb)->frags[k].size -= eat;
1039 skb_shinfo(skb)->nr_frags = k;
1042 skb->data_len -= delta;
1044 return skb_tail_pointer(skb);
1047 /* Copy some data bits from skb to kernel buffer. */
1049 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1052 int start = skb_headlen(skb);
1054 if (offset > (int)skb->len - len)
1058 if ((copy = start - offset) > 0) {
1061 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1062 if ((len -= copy) == 0)
1068 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1071 BUG_TRAP(start <= offset + len);
1073 end = start + skb_shinfo(skb)->frags[i].size;
1074 if ((copy = end - offset) > 0) {
1080 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1082 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1083 offset - start, copy);
1084 kunmap_skb_frag(vaddr);
1086 if ((len -= copy) == 0)
1094 if (skb_shinfo(skb)->frag_list) {
1095 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1097 for (; list; list = list->next) {
1100 BUG_TRAP(start <= offset + len);
1102 end = start + list->len;
1103 if ((copy = end - offset) > 0) {
1106 if (skb_copy_bits(list, offset - start,
1109 if ((len -= copy) == 0)
1125 * skb_store_bits - store bits from kernel buffer to skb
1126 * @skb: destination buffer
1127 * @offset: offset in destination
1128 * @from: source buffer
1129 * @len: number of bytes to copy
1131 * Copy the specified number of bytes from the source buffer to the
1132 * destination skb. This function handles all the messy bits of
1133 * traversing fragment lists and such.
1136 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1139 int start = skb_headlen(skb);
1141 if (offset > (int)skb->len - len)
1144 if ((copy = start - offset) > 0) {
1147 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1148 if ((len -= copy) == 0)
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1158 BUG_TRAP(start <= offset + len);
1160 end = start + frag->size;
1161 if ((copy = end - offset) > 0) {
1167 vaddr = kmap_skb_frag(frag);
1168 memcpy(vaddr + frag->page_offset + offset - start,
1170 kunmap_skb_frag(vaddr);
1172 if ((len -= copy) == 0)
1180 if (skb_shinfo(skb)->frag_list) {
1181 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1183 for (; list; list = list->next) {
1186 BUG_TRAP(start <= offset + len);
1188 end = start + list->len;
1189 if ((copy = end - offset) > 0) {
1192 if (skb_store_bits(list, offset - start,
1195 if ((len -= copy) == 0)
1210 EXPORT_SYMBOL(skb_store_bits);
1212 /* Checksum skb data. */
1214 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1215 int len, __wsum csum)
1217 int start = skb_headlen(skb);
1218 int i, copy = start - offset;
1221 /* Checksum header. */
1225 csum = csum_partial(skb->data + offset, copy, csum);
1226 if ((len -= copy) == 0)
1232 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1235 BUG_TRAP(start <= offset + len);
1237 end = start + skb_shinfo(skb)->frags[i].size;
1238 if ((copy = end - offset) > 0) {
1241 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1245 vaddr = kmap_skb_frag(frag);
1246 csum2 = csum_partial(vaddr + frag->page_offset +
1247 offset - start, copy, 0);
1248 kunmap_skb_frag(vaddr);
1249 csum = csum_block_add(csum, csum2, pos);
1258 if (skb_shinfo(skb)->frag_list) {
1259 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1261 for (; list; list = list->next) {
1264 BUG_TRAP(start <= offset + len);
1266 end = start + list->len;
1267 if ((copy = end - offset) > 0) {
1271 csum2 = skb_checksum(list, offset - start,
1273 csum = csum_block_add(csum, csum2, pos);
1274 if ((len -= copy) == 0)
1287 /* Both of above in one bottle. */
1289 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1290 u8 *to, int len, __wsum csum)
1292 int start = skb_headlen(skb);
1293 int i, copy = start - offset;
1300 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1302 if ((len -= copy) == 0)
1309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1312 BUG_TRAP(start <= offset + len);
1314 end = start + skb_shinfo(skb)->frags[i].size;
1315 if ((copy = end - offset) > 0) {
1318 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1322 vaddr = kmap_skb_frag(frag);
1323 csum2 = csum_partial_copy_nocheck(vaddr +
1327 kunmap_skb_frag(vaddr);
1328 csum = csum_block_add(csum, csum2, pos);
1338 if (skb_shinfo(skb)->frag_list) {
1339 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1341 for (; list; list = list->next) {
1345 BUG_TRAP(start <= offset + len);
1347 end = start + list->len;
1348 if ((copy = end - offset) > 0) {
1351 csum2 = skb_copy_and_csum_bits(list,
1354 csum = csum_block_add(csum, csum2, pos);
1355 if ((len -= copy) == 0)
1368 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1373 if (skb->ip_summed == CHECKSUM_PARTIAL)
1374 csstart = skb->csum_start - skb_headroom(skb);
1376 csstart = skb_headlen(skb);
1378 BUG_ON(csstart > skb_headlen(skb));
1380 skb_copy_from_linear_data(skb, to, csstart);
1383 if (csstart != skb->len)
1384 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1385 skb->len - csstart, 0);
1387 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1388 long csstuff = csstart + skb->csum_offset;
1390 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1395 * skb_dequeue - remove from the head of the queue
1396 * @list: list to dequeue from
1398 * Remove the head of the list. The list lock is taken so the function
1399 * may be used safely with other locking list functions. The head item is
1400 * returned or %NULL if the list is empty.
1403 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1405 unsigned long flags;
1406 struct sk_buff *result;
1408 spin_lock_irqsave(&list->lock, flags);
1409 result = __skb_dequeue(list);
1410 spin_unlock_irqrestore(&list->lock, flags);
1415 * skb_dequeue_tail - remove from the tail of the queue
1416 * @list: list to dequeue from
1418 * Remove the tail of the list. The list lock is taken so the function
1419 * may be used safely with other locking list functions. The tail item is
1420 * returned or %NULL if the list is empty.
1422 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1424 unsigned long flags;
1425 struct sk_buff *result;
1427 spin_lock_irqsave(&list->lock, flags);
1428 result = __skb_dequeue_tail(list);
1429 spin_unlock_irqrestore(&list->lock, flags);
1434 * skb_queue_purge - empty a list
1435 * @list: list to empty
1437 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1438 * the list and one reference dropped. This function takes the list
1439 * lock and is atomic with respect to other list locking functions.
1441 void skb_queue_purge(struct sk_buff_head *list)
1443 struct sk_buff *skb;
1444 while ((skb = skb_dequeue(list)) != NULL)
1449 * skb_queue_head - queue a buffer at the list head
1450 * @list: list to use
1451 * @newsk: buffer to queue
1453 * Queue a buffer at the start of the list. This function takes the
1454 * list lock and can be used safely with other locking &sk_buff functions
1457 * A buffer cannot be placed on two lists at the same time.
1459 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1461 unsigned long flags;
1463 spin_lock_irqsave(&list->lock, flags);
1464 __skb_queue_head(list, newsk);
1465 spin_unlock_irqrestore(&list->lock, flags);
1469 * skb_queue_tail - queue a buffer at the list tail
1470 * @list: list to use
1471 * @newsk: buffer to queue
1473 * Queue a buffer at the tail of the list. This function takes the
1474 * list lock and can be used safely with other locking &sk_buff functions
1477 * A buffer cannot be placed on two lists at the same time.
1479 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1481 unsigned long flags;
1483 spin_lock_irqsave(&list->lock, flags);
1484 __skb_queue_tail(list, newsk);
1485 spin_unlock_irqrestore(&list->lock, flags);
1489 * skb_unlink - remove a buffer from a list
1490 * @skb: buffer to remove
1491 * @list: list to use
1493 * Remove a packet from a list. The list locks are taken and this
1494 * function is atomic with respect to other list locked calls
1496 * You must know what list the SKB is on.
1498 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1500 unsigned long flags;
1502 spin_lock_irqsave(&list->lock, flags);
1503 __skb_unlink(skb, list);
1504 spin_unlock_irqrestore(&list->lock, flags);
1508 * skb_append - append a buffer
1509 * @old: buffer to insert after
1510 * @newsk: buffer to insert
1511 * @list: list to use
1513 * Place a packet after a given packet in a list. The list locks are taken
1514 * and this function is atomic with respect to other list locked calls.
1515 * A buffer cannot be placed on two lists at the same time.
1517 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1519 unsigned long flags;
1521 spin_lock_irqsave(&list->lock, flags);
1522 __skb_append(old, newsk, list);
1523 spin_unlock_irqrestore(&list->lock, flags);
1528 * skb_insert - insert a buffer
1529 * @old: buffer to insert before
1530 * @newsk: buffer to insert
1531 * @list: list to use
1533 * Place a packet before a given packet in a list. The list locks are
1534 * taken and this function is atomic with respect to other list locked
1537 * A buffer cannot be placed on two lists at the same time.
1539 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1541 unsigned long flags;
1543 spin_lock_irqsave(&list->lock, flags);
1544 __skb_insert(newsk, old->prev, old, list);
1545 spin_unlock_irqrestore(&list->lock, flags);
1548 static inline void skb_split_inside_header(struct sk_buff *skb,
1549 struct sk_buff* skb1,
1550 const u32 len, const int pos)
1554 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1556 /* And move data appendix as is. */
1557 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1558 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1560 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1561 skb_shinfo(skb)->nr_frags = 0;
1562 skb1->data_len = skb->data_len;
1563 skb1->len += skb1->data_len;
1566 skb_set_tail_pointer(skb, len);
1569 static inline void skb_split_no_header(struct sk_buff *skb,
1570 struct sk_buff* skb1,
1571 const u32 len, int pos)
1574 const int nfrags = skb_shinfo(skb)->nr_frags;
1576 skb_shinfo(skb)->nr_frags = 0;
1577 skb1->len = skb1->data_len = skb->len - len;
1579 skb->data_len = len - pos;
1581 for (i = 0; i < nfrags; i++) {
1582 int size = skb_shinfo(skb)->frags[i].size;
1584 if (pos + size > len) {
1585 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1589 * We have two variants in this case:
1590 * 1. Move all the frag to the second
1591 * part, if it is possible. F.e.
1592 * this approach is mandatory for TUX,
1593 * where splitting is expensive.
1594 * 2. Split is accurately. We make this.
1596 get_page(skb_shinfo(skb)->frags[i].page);
1597 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1598 skb_shinfo(skb1)->frags[0].size -= len - pos;
1599 skb_shinfo(skb)->frags[i].size = len - pos;
1600 skb_shinfo(skb)->nr_frags++;
1604 skb_shinfo(skb)->nr_frags++;
1607 skb_shinfo(skb1)->nr_frags = k;
1611 * skb_split - Split fragmented skb to two parts at length len.
1612 * @skb: the buffer to split
1613 * @skb1: the buffer to receive the second part
1614 * @len: new length for skb
1616 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
1618 int pos = skb_headlen(skb);
1620 if (len < pos) /* Split line is inside header. */
1621 skb_split_inside_header(skb, skb1, len, pos);
1622 else /* Second chunk has no header, nothing to copy. */
1623 skb_split_no_header(skb, skb1, len, pos);
1627 * skb_prepare_seq_read - Prepare a sequential read of skb data
1628 * @skb: the buffer to read
1629 * @from: lower offset of data to be read
1630 * @to: upper offset of data to be read
1631 * @st: state variable
1633 * Initializes the specified state variable. Must be called before
1634 * invoking skb_seq_read() for the first time.
1636 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1637 unsigned int to, struct skb_seq_state *st)
1639 st->lower_offset = from;
1640 st->upper_offset = to;
1641 st->root_skb = st->cur_skb = skb;
1642 st->frag_idx = st->stepped_offset = 0;
1643 st->frag_data = NULL;
1647 * skb_seq_read - Sequentially read skb data
1648 * @consumed: number of bytes consumed by the caller so far
1649 * @data: destination pointer for data to be returned
1650 * @st: state variable
1652 * Reads a block of skb data at &consumed relative to the
1653 * lower offset specified to skb_prepare_seq_read(). Assigns
1654 * the head of the data block to &data and returns the length
1655 * of the block or 0 if the end of the skb data or the upper
1656 * offset has been reached.
1658 * The caller is not required to consume all of the data
1659 * returned, i.e. &consumed is typically set to the number
1660 * of bytes already consumed and the next call to
1661 * skb_seq_read() will return the remaining part of the block.
1663 * Note: The size of each block of data returned can be arbitary,
1664 * this limitation is the cost for zerocopy seqeuental
1665 * reads of potentially non linear data.
1667 * Note: Fragment lists within fragments are not implemented
1668 * at the moment, state->root_skb could be replaced with
1669 * a stack for this purpose.
1671 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1672 struct skb_seq_state *st)
1674 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1677 if (unlikely(abs_offset >= st->upper_offset))
1681 block_limit = skb_headlen(st->cur_skb);
1683 if (abs_offset < block_limit) {
1684 *data = st->cur_skb->data + abs_offset;
1685 return block_limit - abs_offset;
1688 if (st->frag_idx == 0 && !st->frag_data)
1689 st->stepped_offset += skb_headlen(st->cur_skb);
1691 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1692 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1693 block_limit = frag->size + st->stepped_offset;
1695 if (abs_offset < block_limit) {
1697 st->frag_data = kmap_skb_frag(frag);
1699 *data = (u8 *) st->frag_data + frag->page_offset +
1700 (abs_offset - st->stepped_offset);
1702 return block_limit - abs_offset;
1705 if (st->frag_data) {
1706 kunmap_skb_frag(st->frag_data);
1707 st->frag_data = NULL;
1711 st->stepped_offset += frag->size;
1714 if (st->frag_data) {
1715 kunmap_skb_frag(st->frag_data);
1716 st->frag_data = NULL;
1719 if (st->cur_skb->next) {
1720 st->cur_skb = st->cur_skb->next;
1723 } else if (st->root_skb == st->cur_skb &&
1724 skb_shinfo(st->root_skb)->frag_list) {
1725 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1733 * skb_abort_seq_read - Abort a sequential read of skb data
1734 * @st: state variable
1736 * Must be called if skb_seq_read() was not called until it
1739 void skb_abort_seq_read(struct skb_seq_state *st)
1742 kunmap_skb_frag(st->frag_data);
1745 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
1747 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
1748 struct ts_config *conf,
1749 struct ts_state *state)
1751 return skb_seq_read(offset, text, TS_SKB_CB(state));
1754 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
1756 skb_abort_seq_read(TS_SKB_CB(state));
1760 * skb_find_text - Find a text pattern in skb data
1761 * @skb: the buffer to look in
1762 * @from: search offset
1764 * @config: textsearch configuration
1765 * @state: uninitialized textsearch state variable
1767 * Finds a pattern in the skb data according to the specified
1768 * textsearch configuration. Use textsearch_next() to retrieve
1769 * subsequent occurrences of the pattern. Returns the offset
1770 * to the first occurrence or UINT_MAX if no match was found.
1772 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
1773 unsigned int to, struct ts_config *config,
1774 struct ts_state *state)
1778 config->get_next_block = skb_ts_get_next_block;
1779 config->finish = skb_ts_finish;
1781 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
1783 ret = textsearch_find(config, state);
1784 return (ret <= to - from ? ret : UINT_MAX);
1788 * skb_append_datato_frags: - append the user data to a skb
1789 * @sk: sock structure
1790 * @skb: skb structure to be appened with user data.
1791 * @getfrag: call back function to be used for getting the user data
1792 * @from: pointer to user message iov
1793 * @length: length of the iov message
1795 * Description: This procedure append the user data in the fragment part
1796 * of the skb if any page alloc fails user this procedure returns -ENOMEM
1798 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1799 int (*getfrag)(void *from, char *to, int offset,
1800 int len, int odd, struct sk_buff *skb),
1801 void *from, int length)
1804 skb_frag_t *frag = NULL;
1805 struct page *page = NULL;
1811 /* Return error if we don't have space for new frag */
1812 frg_cnt = skb_shinfo(skb)->nr_frags;
1813 if (frg_cnt >= MAX_SKB_FRAGS)
1816 /* allocate a new page for next frag */
1817 page = alloc_pages(sk->sk_allocation, 0);
1819 /* If alloc_page fails just return failure and caller will
1820 * free previous allocated pages by doing kfree_skb()
1825 /* initialize the next frag */
1826 sk->sk_sndmsg_page = page;
1827 sk->sk_sndmsg_off = 0;
1828 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1829 skb->truesize += PAGE_SIZE;
1830 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1832 /* get the new initialized frag */
1833 frg_cnt = skb_shinfo(skb)->nr_frags;
1834 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1836 /* copy the user data to page */
1837 left = PAGE_SIZE - frag->page_offset;
1838 copy = (length > left)? left : length;
1840 ret = getfrag(from, (page_address(frag->page) +
1841 frag->page_offset + frag->size),
1842 offset, copy, 0, skb);
1846 /* copy was successful so update the size parameters */
1847 sk->sk_sndmsg_off += copy;
1850 skb->data_len += copy;
1854 } while (length > 0);
1860 * skb_pull_rcsum - pull skb and update receive checksum
1861 * @skb: buffer to update
1862 * @start: start of data before pull
1863 * @len: length of data pulled
1865 * This function performs an skb_pull on the packet and updates
1866 * update the CHECKSUM_COMPLETE checksum. It should be used on
1867 * receive path processing instead of skb_pull unless you know
1868 * that the checksum difference is zero (e.g., a valid IP header)
1869 * or you are setting ip_summed to CHECKSUM_NONE.
1871 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1873 BUG_ON(len > skb->len);
1875 BUG_ON(skb->len < skb->data_len);
1876 skb_postpull_rcsum(skb, skb->data, len);
1877 return skb->data += len;
1880 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1883 * skb_segment - Perform protocol segmentation on skb.
1884 * @skb: buffer to segment
1885 * @features: features for the output path (see dev->features)
1887 * This function performs segmentation on the given skb. It returns
1888 * the segment at the given position. It returns NULL if there are
1889 * no more segments to generate, or when an error is encountered.
1891 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1893 struct sk_buff *segs = NULL;
1894 struct sk_buff *tail = NULL;
1895 unsigned int mss = skb_shinfo(skb)->gso_size;
1896 unsigned int doffset = skb->data - skb_mac_header(skb);
1897 unsigned int offset = doffset;
1898 unsigned int headroom;
1900 int sg = features & NETIF_F_SG;
1901 int nfrags = skb_shinfo(skb)->nr_frags;
1906 __skb_push(skb, doffset);
1907 headroom = skb_headroom(skb);
1908 pos = skb_headlen(skb);
1911 struct sk_buff *nskb;
1917 len = skb->len - offset;
1921 hsize = skb_headlen(skb) - offset;
1924 if (hsize > len || !sg)
1927 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1928 if (unlikely(!nskb))
1937 nskb->dev = skb->dev;
1938 skb_copy_queue_mapping(nskb, skb);
1939 nskb->priority = skb->priority;
1940 nskb->protocol = skb->protocol;
1941 nskb->dst = dst_clone(skb->dst);
1942 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1943 nskb->pkt_type = skb->pkt_type;
1944 nskb->mac_len = skb->mac_len;
1946 skb_reserve(nskb, headroom);
1947 skb_reset_mac_header(nskb);
1948 skb_set_network_header(nskb, skb->mac_len);
1949 nskb->transport_header = (nskb->network_header +
1950 skb_network_header_len(skb));
1951 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1954 nskb->csum = skb_copy_and_csum_bits(skb, offset,
1960 frag = skb_shinfo(nskb)->frags;
1963 nskb->ip_summed = CHECKSUM_PARTIAL;
1964 nskb->csum = skb->csum;
1965 skb_copy_from_linear_data_offset(skb, offset,
1966 skb_put(nskb, hsize), hsize);
1968 while (pos < offset + len) {
1969 BUG_ON(i >= nfrags);
1971 *frag = skb_shinfo(skb)->frags[i];
1972 get_page(frag->page);
1976 frag->page_offset += offset - pos;
1977 frag->size -= offset - pos;
1982 if (pos + size <= offset + len) {
1986 frag->size -= pos + size - (offset + len);
1993 skb_shinfo(nskb)->nr_frags = k;
1994 nskb->data_len = len - hsize;
1995 nskb->len += nskb->data_len;
1996 nskb->truesize += nskb->data_len;
1997 } while ((offset += len) < skb->len);
2002 while ((skb = segs)) {
2006 return ERR_PTR(err);
2009 EXPORT_SYMBOL_GPL(skb_segment);
2011 void __init skb_init(void)
2013 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2014 sizeof(struct sk_buff),
2016 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2018 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2019 (2*sizeof(struct sk_buff)) +
2022 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2027 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2028 * @skb: Socket buffer containing the buffers to be mapped
2029 * @sg: The scatter-gather list to map into
2030 * @offset: The offset into the buffer's contents to start mapping
2031 * @len: Length of buffer space to be mapped
2033 * Fill the specified scatter-gather list with mappings/pointers into a
2034 * region of the buffer space attached to a socket buffer.
2037 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2039 int start = skb_headlen(skb);
2040 int i, copy = start - offset;
2046 sg_set_buf(sg, skb->data + offset, copy);
2048 if ((len -= copy) == 0)
2053 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2056 BUG_TRAP(start <= offset + len);
2058 end = start + skb_shinfo(skb)->frags[i].size;
2059 if ((copy = end - offset) > 0) {
2060 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2064 sg_set_page(&sg[elt], frag->page, copy,
2065 frag->page_offset+offset-start);
2074 if (skb_shinfo(skb)->frag_list) {
2075 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2077 for (; list; list = list->next) {
2080 BUG_TRAP(start <= offset + len);
2082 end = start + list->len;
2083 if ((copy = end - offset) > 0) {
2086 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2088 if ((len -= copy) == 0)
2099 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2101 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2103 sg_mark_end(&sg[nsg - 1]);
2109 * skb_cow_data - Check that a socket buffer's data buffers are writable
2110 * @skb: The socket buffer to check.
2111 * @tailbits: Amount of trailing space to be added
2112 * @trailer: Returned pointer to the skb where the @tailbits space begins
2114 * Make sure that the data buffers attached to a socket buffer are
2115 * writable. If they are not, private copies are made of the data buffers
2116 * and the socket buffer is set to use these instead.
2118 * If @tailbits is given, make sure that there is space to write @tailbits
2119 * bytes of data beyond current end of socket buffer. @trailer will be
2120 * set to point to the skb in which this space begins.
2122 * The number of scatterlist elements required to completely map the
2123 * COW'd and extended socket buffer will be returned.
2125 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2129 struct sk_buff *skb1, **skb_p;
2131 /* If skb is cloned or its head is paged, reallocate
2132 * head pulling out all the pages (pages are considered not writable
2133 * at the moment even if they are anonymous).
2135 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2136 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2139 /* Easy case. Most of packets will go this way. */
2140 if (!skb_shinfo(skb)->frag_list) {
2141 /* A little of trouble, not enough of space for trailer.
2142 * This should not happen, when stack is tuned to generate
2143 * good frames. OK, on miss we reallocate and reserve even more
2144 * space, 128 bytes is fair. */
2146 if (skb_tailroom(skb) < tailbits &&
2147 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2155 /* Misery. We are in troubles, going to mincer fragments... */
2158 skb_p = &skb_shinfo(skb)->frag_list;
2161 while ((skb1 = *skb_p) != NULL) {
2164 /* The fragment is partially pulled by someone,
2165 * this can happen on input. Copy it and everything
2168 if (skb_shared(skb1))
2171 /* If the skb is the last, worry about trailer. */
2173 if (skb1->next == NULL && tailbits) {
2174 if (skb_shinfo(skb1)->nr_frags ||
2175 skb_shinfo(skb1)->frag_list ||
2176 skb_tailroom(skb1) < tailbits)
2177 ntail = tailbits + 128;
2183 skb_shinfo(skb1)->nr_frags ||
2184 skb_shinfo(skb1)->frag_list) {
2185 struct sk_buff *skb2;
2187 /* Fuck, we are miserable poor guys... */
2189 skb2 = skb_copy(skb1, GFP_ATOMIC);
2191 skb2 = skb_copy_expand(skb1,
2195 if (unlikely(skb2 == NULL))
2199 skb_set_owner_w(skb2, skb1->sk);
2201 /* Looking around. Are we still alive?
2202 * OK, link new skb, drop old one */
2204 skb2->next = skb1->next;
2211 skb_p = &skb1->next;
2217 EXPORT_SYMBOL(___pskb_trim);
2218 EXPORT_SYMBOL(__kfree_skb);
2219 EXPORT_SYMBOL(kfree_skb);
2220 EXPORT_SYMBOL(__pskb_pull_tail);
2221 EXPORT_SYMBOL(__alloc_skb);
2222 EXPORT_SYMBOL(__netdev_alloc_skb);
2223 EXPORT_SYMBOL(pskb_copy);
2224 EXPORT_SYMBOL(pskb_expand_head);
2225 EXPORT_SYMBOL(skb_checksum);
2226 EXPORT_SYMBOL(skb_clone);
2227 EXPORT_SYMBOL(skb_copy);
2228 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2229 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2230 EXPORT_SYMBOL(skb_copy_bits);
2231 EXPORT_SYMBOL(skb_copy_expand);
2232 EXPORT_SYMBOL(skb_over_panic);
2233 EXPORT_SYMBOL(skb_pad);
2234 EXPORT_SYMBOL(skb_realloc_headroom);
2235 EXPORT_SYMBOL(skb_under_panic);
2236 EXPORT_SYMBOL(skb_dequeue);
2237 EXPORT_SYMBOL(skb_dequeue_tail);
2238 EXPORT_SYMBOL(skb_insert);
2239 EXPORT_SYMBOL(skb_queue_purge);
2240 EXPORT_SYMBOL(skb_queue_head);
2241 EXPORT_SYMBOL(skb_queue_tail);
2242 EXPORT_SYMBOL(skb_unlink);
2243 EXPORT_SYMBOL(skb_append);
2244 EXPORT_SYMBOL(skb_split);
2245 EXPORT_SYMBOL(skb_prepare_seq_read);
2246 EXPORT_SYMBOL(skb_seq_read);
2247 EXPORT_SYMBOL(skb_abort_seq_read);
2248 EXPORT_SYMBOL(skb_find_text);
2249 EXPORT_SYMBOL(skb_append_datato_frags);
2251 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2252 EXPORT_SYMBOL_GPL(skb_cow_data);