rfkill: always call get_state() hook on resume
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / core / skbuff.c
blob57555a4525da27b9848a2ec3ecfe79bead839db1
1 /*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
7 * Fixes:
8 * Alan Cox : Fixed the worst of the load
9 * balancer bugs.
10 * Dave Platt : Interrupt stacking fix.
11 * Richard Kooijman : Timestamp fixes.
12 * Alan Cox : Changed buffer format.
13 * Alan Cox : destructor hook for AF_UNIX etc.
14 * Linus Torvalds : Better skb_clone.
15 * Alan Cox : Added skb_copy.
16 * Alan Cox : Added all the changed routines Linus
17 * only put in the headers
18 * Ray VanTassle : Fixed --skb->lock in free
19 * Alan Cox : skb_copy copy arp field
20 * Andi Kleen : slabified it.
21 * Robert Olsson : Removed skb_head_pool
23 * NOTE:
24 * The __skb_ routines should be called with interrupts
25 * disabled, or you better be *real* sure that the operation is atomic
26 * with respect to whatever list is being frobbed (e.g. via lock_sock()
27 * or via disabling bottom half handlers, etc).
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
36 * The functions in this file will not compile correctly with gcc 2.4.x
39 #include <linux/module.h>
40 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/in.h>
45 #include <linux/inet.h>
46 #include <linux/slab.h>
47 #include <linux/netdevice.h>
48 #ifdef CONFIG_NET_CLS_ACT
49 #include <net/pkt_sched.h>
50 #endif
51 #include <linux/string.h>
52 #include <linux/skbuff.h>
53 #include <linux/splice.h>
54 #include <linux/cache.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/init.h>
57 #include <linux/scatterlist.h>
59 #include <net/protocol.h>
60 #include <net/dst.h>
61 #include <net/sock.h>
62 #include <net/checksum.h>
63 #include <net/xfrm.h>
65 #include <asm/uaccess.h>
66 #include <asm/system.h>
68 #include "kmap_skb.h"
70 static struct kmem_cache *skbuff_head_cache __read_mostly;
71 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
73 static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
74 struct pipe_buffer *buf)
76 struct sk_buff *skb = (struct sk_buff *) buf->private;
78 kfree_skb(skb);
81 static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
82 struct pipe_buffer *buf)
84 struct sk_buff *skb = (struct sk_buff *) buf->private;
86 skb_get(skb);
89 static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
90 struct pipe_buffer *buf)
92 return 1;
96 /* Pipe buffer operations for a socket. */
97 static struct pipe_buf_operations sock_pipe_buf_ops = {
98 .can_merge = 0,
99 .map = generic_pipe_buf_map,
100 .unmap = generic_pipe_buf_unmap,
101 .confirm = generic_pipe_buf_confirm,
102 .release = sock_pipe_buf_release,
103 .steal = sock_pipe_buf_steal,
104 .get = sock_pipe_buf_get,
108 * Keep out-of-line to prevent kernel bloat.
109 * __builtin_return_address is not used because it is not always
110 * reliable.
114 * skb_over_panic - private function
115 * @skb: buffer
116 * @sz: size
117 * @here: address
119 * Out of line support code for skb_put(). Not user callable.
121 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
123 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
124 "data:%p tail:%#lx end:%#lx dev:%s\n",
125 here, skb->len, sz, skb->head, skb->data,
126 (unsigned long)skb->tail, (unsigned long)skb->end,
127 skb->dev ? skb->dev->name : "<NULL>");
128 BUG();
132 * skb_under_panic - private function
133 * @skb: buffer
134 * @sz: size
135 * @here: address
137 * Out of line support code for skb_push(). Not user callable.
140 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
142 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
143 "data:%p tail:%#lx end:%#lx dev:%s\n",
144 here, skb->len, sz, skb->head, skb->data,
145 (unsigned long)skb->tail, (unsigned long)skb->end,
146 skb->dev ? skb->dev->name : "<NULL>");
147 BUG();
150 void skb_truesize_bug(struct sk_buff *skb)
152 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
153 "len=%u, sizeof(sk_buff)=%Zd\n",
154 skb->truesize, skb->len, sizeof(struct sk_buff));
156 EXPORT_SYMBOL(skb_truesize_bug);
158 /* Allocate a new skbuff. We do this ourselves so we can fill in a few
159 * 'private' fields and also do memory statistics to find all the
160 * [BEEP] leaks.
165 * __alloc_skb - allocate a network buffer
166 * @size: size to allocate
167 * @gfp_mask: allocation mask
168 * @fclone: allocate from fclone cache instead of head cache
169 * and allocate a cloned (child) skb
170 * @node: numa node to allocate memory on
172 * Allocate a new &sk_buff. The returned buffer has no headroom and a
173 * tail room of size bytes. The object has a reference count of one.
174 * The return is the buffer. On a failure the return is %NULL.
176 * Buffers may only be allocated from interrupts using a @gfp_mask of
177 * %GFP_ATOMIC.
179 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180 int fclone, int node)
182 struct kmem_cache *cache;
183 struct skb_shared_info *shinfo;
184 struct sk_buff *skb;
185 u8 *data;
187 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
189 /* Get the HEAD */
190 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
191 if (!skb)
192 goto out;
194 size = SKB_DATA_ALIGN(size);
195 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
196 gfp_mask, node);
197 if (!data)
198 goto nodata;
201 * Only clear those fields we need to clear, not those that we will
202 * actually initialise below. Hence, don't put any more fields after
203 * the tail pointer in struct sk_buff!
205 memset(skb, 0, offsetof(struct sk_buff, tail));
206 skb->truesize = size + sizeof(struct sk_buff);
207 atomic_set(&skb->users, 1);
208 skb->head = data;
209 skb->data = data;
210 skb_reset_tail_pointer(skb);
211 skb->end = skb->tail + size;
212 /* make sure we initialize shinfo sequentially */
213 shinfo = skb_shinfo(skb);
214 atomic_set(&shinfo->dataref, 1);
215 shinfo->nr_frags = 0;
216 shinfo->gso_size = 0;
217 shinfo->gso_segs = 0;
218 shinfo->gso_type = 0;
219 shinfo->ip6_frag_id = 0;
220 shinfo->frag_list = NULL;
222 if (fclone) {
223 struct sk_buff *child = skb + 1;
224 atomic_t *fclone_ref = (atomic_t *) (child + 1);
226 skb->fclone = SKB_FCLONE_ORIG;
227 atomic_set(fclone_ref, 1);
229 child->fclone = SKB_FCLONE_UNAVAILABLE;
231 out:
232 return skb;
233 nodata:
234 kmem_cache_free(cache, skb);
235 skb = NULL;
236 goto out;
240 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
241 * @dev: network device to receive on
242 * @length: length to allocate
243 * @gfp_mask: get_free_pages mask, passed to alloc_skb
245 * Allocate a new &sk_buff and assign it a usage count of one. The
246 * buffer has unspecified headroom built in. Users should allocate
247 * the headroom they think they need without accounting for the
248 * built in space. The built in space is used for optimisations.
250 * %NULL is returned if there is no free memory.
252 struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
253 unsigned int length, gfp_t gfp_mask)
255 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
256 struct sk_buff *skb;
258 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
259 if (likely(skb)) {
260 skb_reserve(skb, NET_SKB_PAD);
261 skb->dev = dev;
263 return skb;
266 struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
268 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
269 struct page *page;
271 page = alloc_pages_node(node, gfp_mask, 0);
272 return page;
274 EXPORT_SYMBOL(__netdev_alloc_page);
276 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
277 int size)
279 skb_fill_page_desc(skb, i, page, off, size);
280 skb->len += size;
281 skb->data_len += size;
282 skb->truesize += size;
284 EXPORT_SYMBOL(skb_add_rx_frag);
287 * dev_alloc_skb - allocate an skbuff for receiving
288 * @length: length to allocate
290 * Allocate a new &sk_buff and assign it a usage count of one. The
291 * buffer has unspecified headroom built in. Users should allocate
292 * the headroom they think they need without accounting for the
293 * built in space. The built in space is used for optimisations.
295 * %NULL is returned if there is no free memory. Although this function
296 * allocates memory it can be called from an interrupt.
298 struct sk_buff *dev_alloc_skb(unsigned int length)
301 * There is more code here than it seems:
302 * __dev_alloc_skb is an inline
304 return __dev_alloc_skb(length, GFP_ATOMIC);
306 EXPORT_SYMBOL(dev_alloc_skb);
308 static void skb_drop_list(struct sk_buff **listp)
310 struct sk_buff *list = *listp;
312 *listp = NULL;
314 do {
315 struct sk_buff *this = list;
316 list = list->next;
317 kfree_skb(this);
318 } while (list);
321 static inline void skb_drop_fraglist(struct sk_buff *skb)
323 skb_drop_list(&skb_shinfo(skb)->frag_list);
326 static void skb_clone_fraglist(struct sk_buff *skb)
328 struct sk_buff *list;
330 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
331 skb_get(list);
334 static void skb_release_data(struct sk_buff *skb)
336 if (!skb->cloned ||
337 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
338 &skb_shinfo(skb)->dataref)) {
339 if (skb_shinfo(skb)->nr_frags) {
340 int i;
341 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
342 put_page(skb_shinfo(skb)->frags[i].page);
345 if (skb_shinfo(skb)->frag_list)
346 skb_drop_fraglist(skb);
348 kfree(skb->head);
353 * Free an skbuff by memory without cleaning the state.
355 static void kfree_skbmem(struct sk_buff *skb)
357 struct sk_buff *other;
358 atomic_t *fclone_ref;
360 switch (skb->fclone) {
361 case SKB_FCLONE_UNAVAILABLE:
362 kmem_cache_free(skbuff_head_cache, skb);
363 break;
365 case SKB_FCLONE_ORIG:
366 fclone_ref = (atomic_t *) (skb + 2);
367 if (atomic_dec_and_test(fclone_ref))
368 kmem_cache_free(skbuff_fclone_cache, skb);
369 break;
371 case SKB_FCLONE_CLONE:
372 fclone_ref = (atomic_t *) (skb + 1);
373 other = skb - 1;
375 /* The clone portion is available for
376 * fast-cloning again.
378 skb->fclone = SKB_FCLONE_UNAVAILABLE;
380 if (atomic_dec_and_test(fclone_ref))
381 kmem_cache_free(skbuff_fclone_cache, other);
382 break;
386 static void skb_release_head_state(struct sk_buff *skb)
388 dst_release(skb->dst);
389 #ifdef CONFIG_XFRM
390 secpath_put(skb->sp);
391 #endif
392 if (skb->destructor) {
393 WARN_ON(in_irq());
394 skb->destructor(skb);
396 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
397 nf_conntrack_put(skb->nfct);
398 nf_conntrack_put_reasm(skb->nfct_reasm);
399 #endif
400 #ifdef CONFIG_BRIDGE_NETFILTER
401 nf_bridge_put(skb->nf_bridge);
402 #endif
403 /* XXX: IS this still necessary? - JHS */
404 #ifdef CONFIG_NET_SCHED
405 skb->tc_index = 0;
406 #ifdef CONFIG_NET_CLS_ACT
407 skb->tc_verd = 0;
408 #endif
409 #endif
412 /* Free everything but the sk_buff shell. */
413 static void skb_release_all(struct sk_buff *skb)
415 skb_release_head_state(skb);
416 skb_release_data(skb);
420 * __kfree_skb - private function
421 * @skb: buffer
423 * Free an sk_buff. Release anything attached to the buffer.
424 * Clean the state. This is an internal helper function. Users should
425 * always call kfree_skb
428 void __kfree_skb(struct sk_buff *skb)
430 skb_release_all(skb);
431 kfree_skbmem(skb);
435 * kfree_skb - free an sk_buff
436 * @skb: buffer to free
438 * Drop a reference to the buffer and free it if the usage count has
439 * hit zero.
441 void kfree_skb(struct sk_buff *skb)
443 if (unlikely(!skb))
444 return;
445 if (likely(atomic_read(&skb->users) == 1))
446 smp_rmb();
447 else if (likely(!atomic_dec_and_test(&skb->users)))
448 return;
449 __kfree_skb(skb);
453 * skb_recycle_check - check if skb can be reused for receive
454 * @skb: buffer
455 * @skb_size: minimum receive buffer size
457 * Checks that the skb passed in is not shared or cloned, and
458 * that it is linear and its head portion at least as large as
459 * skb_size so that it can be recycled as a receive buffer.
460 * If these conditions are met, this function does any necessary
461 * reference count dropping and cleans up the skbuff as if it
462 * just came from __alloc_skb().
464 int skb_recycle_check(struct sk_buff *skb, int skb_size)
466 struct skb_shared_info *shinfo;
468 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
469 return 0;
471 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
472 if (skb_end_pointer(skb) - skb->head < skb_size)
473 return 0;
475 if (skb_shared(skb) || skb_cloned(skb))
476 return 0;
478 skb_release_head_state(skb);
479 shinfo = skb_shinfo(skb);
480 atomic_set(&shinfo->dataref, 1);
481 shinfo->nr_frags = 0;
482 shinfo->gso_size = 0;
483 shinfo->gso_segs = 0;
484 shinfo->gso_type = 0;
485 shinfo->ip6_frag_id = 0;
486 shinfo->frag_list = NULL;
488 memset(skb, 0, offsetof(struct sk_buff, tail));
489 skb->data = skb->head + NET_SKB_PAD;
490 skb_reset_tail_pointer(skb);
492 return 1;
494 EXPORT_SYMBOL(skb_recycle_check);
496 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
498 new->tstamp = old->tstamp;
499 new->dev = old->dev;
500 new->transport_header = old->transport_header;
501 new->network_header = old->network_header;
502 new->mac_header = old->mac_header;
503 new->dst = dst_clone(old->dst);
504 #ifdef CONFIG_XFRM
505 new->sp = secpath_get(old->sp);
506 #endif
507 memcpy(new->cb, old->cb, sizeof(old->cb));
508 new->csum_start = old->csum_start;
509 new->csum_offset = old->csum_offset;
510 new->local_df = old->local_df;
511 new->pkt_type = old->pkt_type;
512 new->ip_summed = old->ip_summed;
513 skb_copy_queue_mapping(new, old);
514 new->priority = old->priority;
515 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
516 new->ipvs_property = old->ipvs_property;
517 #endif
518 new->protocol = old->protocol;
519 new->mark = old->mark;
520 __nf_copy(new, old);
521 #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
522 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
523 new->nf_trace = old->nf_trace;
524 #endif
525 #ifdef CONFIG_NET_SCHED
526 new->tc_index = old->tc_index;
527 #ifdef CONFIG_NET_CLS_ACT
528 new->tc_verd = old->tc_verd;
529 #endif
530 #endif
531 new->vlan_tci = old->vlan_tci;
533 skb_copy_secmark(new, old);
536 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
538 #define C(x) n->x = skb->x
540 n->next = n->prev = NULL;
541 n->sk = NULL;
542 __copy_skb_header(n, skb);
544 C(len);
545 C(data_len);
546 C(mac_len);
547 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
548 n->cloned = 1;
549 n->nohdr = 0;
550 n->destructor = NULL;
551 C(iif);
552 C(tail);
553 C(end);
554 C(head);
555 C(data);
556 C(truesize);
557 #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
558 C(do_not_encrypt);
559 C(requeue);
560 #endif
561 atomic_set(&n->users, 1);
563 atomic_inc(&(skb_shinfo(skb)->dataref));
564 skb->cloned = 1;
566 return n;
567 #undef C
571 * skb_morph - morph one skb into another
572 * @dst: the skb to receive the contents
573 * @src: the skb to supply the contents
575 * This is identical to skb_clone except that the target skb is
576 * supplied by the user.
578 * The target skb is returned upon exit.
580 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
582 skb_release_all(dst);
583 return __skb_clone(dst, src);
585 EXPORT_SYMBOL_GPL(skb_morph);
588 * skb_clone - duplicate an sk_buff
589 * @skb: buffer to clone
590 * @gfp_mask: allocation priority
592 * Duplicate an &sk_buff. The new one is not owned by a socket. Both
593 * copies share the same packet data but not structure. The new
594 * buffer has a reference count of 1. If the allocation fails the
595 * function returns %NULL otherwise the new buffer is returned.
597 * If this function is called from an interrupt gfp_mask() must be
598 * %GFP_ATOMIC.
601 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
603 struct sk_buff *n;
605 n = skb + 1;
606 if (skb->fclone == SKB_FCLONE_ORIG &&
607 n->fclone == SKB_FCLONE_UNAVAILABLE) {
608 atomic_t *fclone_ref = (atomic_t *) (n + 1);
609 n->fclone = SKB_FCLONE_CLONE;
610 atomic_inc(fclone_ref);
611 } else {
612 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
613 if (!n)
614 return NULL;
615 n->fclone = SKB_FCLONE_UNAVAILABLE;
618 return __skb_clone(n, skb);
621 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
623 #ifndef NET_SKBUFF_DATA_USES_OFFSET
625 * Shift between the two data areas in bytes
627 unsigned long offset = new->data - old->data;
628 #endif
630 __copy_skb_header(new, old);
632 #ifndef NET_SKBUFF_DATA_USES_OFFSET
633 /* {transport,network,mac}_header are relative to skb->head */
634 new->transport_header += offset;
635 new->network_header += offset;
636 new->mac_header += offset;
637 #endif
638 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
639 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
640 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
644 * skb_copy - create private copy of an sk_buff
645 * @skb: buffer to copy
646 * @gfp_mask: allocation priority
648 * Make a copy of both an &sk_buff and its data. This is used when the
649 * caller wishes to modify the data and needs a private copy of the
650 * data to alter. Returns %NULL on failure or the pointer to the buffer
651 * on success. The returned buffer has a reference count of 1.
653 * As by-product this function converts non-linear &sk_buff to linear
654 * one, so that &sk_buff becomes completely private and caller is allowed
655 * to modify all the data of returned buffer. This means that this
656 * function is not recommended for use in circumstances when only
657 * header is going to be modified. Use pskb_copy() instead.
660 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
662 int headerlen = skb->data - skb->head;
664 * Allocate the copy buffer
666 struct sk_buff *n;
667 #ifdef NET_SKBUFF_DATA_USES_OFFSET
668 n = alloc_skb(skb->end + skb->data_len, gfp_mask);
669 #else
670 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
671 #endif
672 if (!n)
673 return NULL;
675 /* Set the data pointer */
676 skb_reserve(n, headerlen);
677 /* Set the tail pointer and length */
678 skb_put(n, skb->len);
680 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
681 BUG();
683 copy_skb_header(n, skb);
684 return n;
689 * pskb_copy - create copy of an sk_buff with private head.
690 * @skb: buffer to copy
691 * @gfp_mask: allocation priority
693 * Make a copy of both an &sk_buff and part of its data, located
694 * in header. Fragmented data remain shared. This is used when
695 * the caller wishes to modify only header of &sk_buff and needs
696 * private copy of the header to alter. Returns %NULL on failure
697 * or the pointer to the buffer on success.
698 * The returned buffer has a reference count of 1.
701 struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
704 * Allocate the copy buffer
706 struct sk_buff *n;
707 #ifdef NET_SKBUFF_DATA_USES_OFFSET
708 n = alloc_skb(skb->end, gfp_mask);
709 #else
710 n = alloc_skb(skb->end - skb->head, gfp_mask);
711 #endif
712 if (!n)
713 goto out;
715 /* Set the data pointer */
716 skb_reserve(n, skb->data - skb->head);
717 /* Set the tail pointer and length */
718 skb_put(n, skb_headlen(skb));
719 /* Copy the bytes */
720 skb_copy_from_linear_data(skb, n->data, n->len);
722 n->truesize += skb->data_len;
723 n->data_len = skb->data_len;
724 n->len = skb->len;
726 if (skb_shinfo(skb)->nr_frags) {
727 int i;
729 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
730 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
731 get_page(skb_shinfo(n)->frags[i].page);
733 skb_shinfo(n)->nr_frags = i;
736 if (skb_shinfo(skb)->frag_list) {
737 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
738 skb_clone_fraglist(n);
741 copy_skb_header(n, skb);
742 out:
743 return n;
747 * pskb_expand_head - reallocate header of &sk_buff
748 * @skb: buffer to reallocate
749 * @nhead: room to add at head
750 * @ntail: room to add at tail
751 * @gfp_mask: allocation priority
753 * Expands (or creates identical copy, if &nhead and &ntail are zero)
754 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
755 * reference count of 1. Returns zero in the case of success or error,
756 * if expansion failed. In the last case, &sk_buff is not changed.
758 * All the pointers pointing into skb header may change and must be
759 * reloaded after call to this function.
762 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
763 gfp_t gfp_mask)
765 int i;
766 u8 *data;
767 #ifdef NET_SKBUFF_DATA_USES_OFFSET
768 int size = nhead + skb->end + ntail;
769 #else
770 int size = nhead + (skb->end - skb->head) + ntail;
771 #endif
772 long off;
774 BUG_ON(nhead < 0);
776 if (skb_shared(skb))
777 BUG();
779 size = SKB_DATA_ALIGN(size);
781 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
782 if (!data)
783 goto nodata;
785 /* Copy only real data... and, alas, header. This should be
786 * optimized for the cases when header is void. */
787 #ifdef NET_SKBUFF_DATA_USES_OFFSET
788 memcpy(data + nhead, skb->head, skb->tail);
789 #else
790 memcpy(data + nhead, skb->head, skb->tail - skb->head);
791 #endif
792 memcpy(data + size, skb_end_pointer(skb),
793 sizeof(struct skb_shared_info));
795 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
796 get_page(skb_shinfo(skb)->frags[i].page);
798 if (skb_shinfo(skb)->frag_list)
799 skb_clone_fraglist(skb);
801 skb_release_data(skb);
803 off = (data + nhead) - skb->head;
805 skb->head = data;
806 skb->data += off;
807 #ifdef NET_SKBUFF_DATA_USES_OFFSET
808 skb->end = size;
809 off = nhead;
810 #else
811 skb->end = skb->head + size;
812 #endif
813 /* {transport,network,mac}_header and tail are relative to skb->head */
814 skb->tail += off;
815 skb->transport_header += off;
816 skb->network_header += off;
817 skb->mac_header += off;
818 skb->csum_start += nhead;
819 skb->cloned = 0;
820 skb->hdr_len = 0;
821 skb->nohdr = 0;
822 atomic_set(&skb_shinfo(skb)->dataref, 1);
823 return 0;
825 nodata:
826 return -ENOMEM;
829 /* Make private copy of skb with writable head and some headroom */
831 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
833 struct sk_buff *skb2;
834 int delta = headroom - skb_headroom(skb);
836 if (delta <= 0)
837 skb2 = pskb_copy(skb, GFP_ATOMIC);
838 else {
839 skb2 = skb_clone(skb, GFP_ATOMIC);
840 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
841 GFP_ATOMIC)) {
842 kfree_skb(skb2);
843 skb2 = NULL;
846 return skb2;
851 * skb_copy_expand - copy and expand sk_buff
852 * @skb: buffer to copy
853 * @newheadroom: new free bytes at head
854 * @newtailroom: new free bytes at tail
855 * @gfp_mask: allocation priority
857 * Make a copy of both an &sk_buff and its data and while doing so
858 * allocate additional space.
860 * This is used when the caller wishes to modify the data and needs a
861 * private copy of the data to alter as well as more space for new fields.
862 * Returns %NULL on failure or the pointer to the buffer
863 * on success. The returned buffer has a reference count of 1.
865 * You must pass %GFP_ATOMIC as the allocation priority if this function
866 * is called from an interrupt.
868 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
869 int newheadroom, int newtailroom,
870 gfp_t gfp_mask)
873 * Allocate the copy buffer
875 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
876 gfp_mask);
877 int oldheadroom = skb_headroom(skb);
878 int head_copy_len, head_copy_off;
879 int off;
881 if (!n)
882 return NULL;
884 skb_reserve(n, newheadroom);
886 /* Set the tail pointer and length */
887 skb_put(n, skb->len);
889 head_copy_len = oldheadroom;
890 head_copy_off = 0;
891 if (newheadroom <= head_copy_len)
892 head_copy_len = newheadroom;
893 else
894 head_copy_off = newheadroom - head_copy_len;
896 /* Copy the linear header and data. */
897 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
898 skb->len + head_copy_len))
899 BUG();
901 copy_skb_header(n, skb);
903 off = newheadroom - oldheadroom;
904 n->csum_start += off;
905 #ifdef NET_SKBUFF_DATA_USES_OFFSET
906 n->transport_header += off;
907 n->network_header += off;
908 n->mac_header += off;
909 #endif
911 return n;
915 * skb_pad - zero pad the tail of an skb
916 * @skb: buffer to pad
917 * @pad: space to pad
919 * Ensure that a buffer is followed by a padding area that is zero
920 * filled. Used by network drivers which may DMA or transfer data
921 * beyond the buffer end onto the wire.
923 * May return error in out of memory cases. The skb is freed on error.
926 int skb_pad(struct sk_buff *skb, int pad)
928 int err;
929 int ntail;
931 /* If the skbuff is non linear tailroom is always zero.. */
932 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
933 memset(skb->data+skb->len, 0, pad);
934 return 0;
937 ntail = skb->data_len + pad - (skb->end - skb->tail);
938 if (likely(skb_cloned(skb) || ntail > 0)) {
939 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
940 if (unlikely(err))
941 goto free_skb;
944 /* FIXME: The use of this function with non-linear skb's really needs
945 * to be audited.
947 err = skb_linearize(skb);
948 if (unlikely(err))
949 goto free_skb;
951 memset(skb->data + skb->len, 0, pad);
952 return 0;
954 free_skb:
955 kfree_skb(skb);
956 return err;
960 * skb_put - add data to a buffer
961 * @skb: buffer to use
962 * @len: amount of data to add
964 * This function extends the used data area of the buffer. If this would
965 * exceed the total buffer size the kernel will panic. A pointer to the
966 * first byte of the extra data is returned.
968 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
970 unsigned char *tmp = skb_tail_pointer(skb);
971 SKB_LINEAR_ASSERT(skb);
972 skb->tail += len;
973 skb->len += len;
974 if (unlikely(skb->tail > skb->end))
975 skb_over_panic(skb, len, __builtin_return_address(0));
976 return tmp;
978 EXPORT_SYMBOL(skb_put);
981 * skb_push - add data to the start of a buffer
982 * @skb: buffer to use
983 * @len: amount of data to add
985 * This function extends the used data area of the buffer at the buffer
986 * start. If this would exceed the total buffer headroom the kernel will
987 * panic. A pointer to the first byte of the extra data is returned.
989 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
991 skb->data -= len;
992 skb->len += len;
993 if (unlikely(skb->data<skb->head))
994 skb_under_panic(skb, len, __builtin_return_address(0));
995 return skb->data;
997 EXPORT_SYMBOL(skb_push);
1000 * skb_pull - remove data from the start of a buffer
1001 * @skb: buffer to use
1002 * @len: amount of data to remove
1004 * This function removes data from the start of a buffer, returning
1005 * the memory to the headroom. A pointer to the next data in the buffer
1006 * is returned. Once the data has been pulled future pushes will overwrite
1007 * the old data.
1009 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1011 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
1013 EXPORT_SYMBOL(skb_pull);
1016 * skb_trim - remove end from a buffer
1017 * @skb: buffer to alter
1018 * @len: new length
1020 * Cut the length of a buffer down by removing data from the tail. If
1021 * the buffer is already under the length specified it is not modified.
1022 * The skb must be linear.
1024 void skb_trim(struct sk_buff *skb, unsigned int len)
1026 if (skb->len > len)
1027 __skb_trim(skb, len);
1029 EXPORT_SYMBOL(skb_trim);
1031 /* Trims skb to length len. It can change skb pointers.
1034 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1036 struct sk_buff **fragp;
1037 struct sk_buff *frag;
1038 int offset = skb_headlen(skb);
1039 int nfrags = skb_shinfo(skb)->nr_frags;
1040 int i;
1041 int err;
1043 if (skb_cloned(skb) &&
1044 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1045 return err;
1047 i = 0;
1048 if (offset >= len)
1049 goto drop_pages;
1051 for (; i < nfrags; i++) {
1052 int end = offset + skb_shinfo(skb)->frags[i].size;
1054 if (end < len) {
1055 offset = end;
1056 continue;
1059 skb_shinfo(skb)->frags[i++].size = len - offset;
1061 drop_pages:
1062 skb_shinfo(skb)->nr_frags = i;
1064 for (; i < nfrags; i++)
1065 put_page(skb_shinfo(skb)->frags[i].page);
1067 if (skb_shinfo(skb)->frag_list)
1068 skb_drop_fraglist(skb);
1069 goto done;
1072 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1073 fragp = &frag->next) {
1074 int end = offset + frag->len;
1076 if (skb_shared(frag)) {
1077 struct sk_buff *nfrag;
1079 nfrag = skb_clone(frag, GFP_ATOMIC);
1080 if (unlikely(!nfrag))
1081 return -ENOMEM;
1083 nfrag->next = frag->next;
1084 kfree_skb(frag);
1085 frag = nfrag;
1086 *fragp = frag;
1089 if (end < len) {
1090 offset = end;
1091 continue;
1094 if (end > len &&
1095 unlikely((err = pskb_trim(frag, len - offset))))
1096 return err;
1098 if (frag->next)
1099 skb_drop_list(&frag->next);
1100 break;
1103 done:
1104 if (len > skb_headlen(skb)) {
1105 skb->data_len -= skb->len - len;
1106 skb->len = len;
1107 } else {
1108 skb->len = len;
1109 skb->data_len = 0;
1110 skb_set_tail_pointer(skb, len);
1113 return 0;
1117 * __pskb_pull_tail - advance tail of skb header
1118 * @skb: buffer to reallocate
1119 * @delta: number of bytes to advance tail
1121 * The function makes a sense only on a fragmented &sk_buff,
1122 * it expands header moving its tail forward and copying necessary
1123 * data from fragmented part.
1125 * &sk_buff MUST have reference count of 1.
1127 * Returns %NULL (and &sk_buff does not change) if pull failed
1128 * or value of new tail of skb in the case of success.
1130 * All the pointers pointing into skb header may change and must be
1131 * reloaded after call to this function.
1134 /* Moves tail of skb head forward, copying data from fragmented part,
1135 * when it is necessary.
1136 * 1. It may fail due to malloc failure.
1137 * 2. It may change skb pointers.
1139 * It is pretty complicated. Luckily, it is called only in exceptional cases.
1141 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1143 /* If skb has not enough free space at tail, get new one
1144 * plus 128 bytes for future expansions. If we have enough
1145 * room at tail, reallocate without expansion only if skb is cloned.
1147 int i, k, eat = (skb->tail + delta) - skb->end;
1149 if (eat > 0 || skb_cloned(skb)) {
1150 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1151 GFP_ATOMIC))
1152 return NULL;
1155 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1156 BUG();
1158 /* Optimization: no fragments, no reasons to preestimate
1159 * size of pulled pages. Superb.
1161 if (!skb_shinfo(skb)->frag_list)
1162 goto pull_pages;
1164 /* Estimate size of pulled pages. */
1165 eat = delta;
1166 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1167 if (skb_shinfo(skb)->frags[i].size >= eat)
1168 goto pull_pages;
1169 eat -= skb_shinfo(skb)->frags[i].size;
1172 /* If we need update frag list, we are in troubles.
1173 * Certainly, it possible to add an offset to skb data,
1174 * but taking into account that pulling is expected to
1175 * be very rare operation, it is worth to fight against
1176 * further bloating skb head and crucify ourselves here instead.
1177 * Pure masohism, indeed. 8)8)
1179 if (eat) {
1180 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1181 struct sk_buff *clone = NULL;
1182 struct sk_buff *insp = NULL;
1184 do {
1185 BUG_ON(!list);
1187 if (list->len <= eat) {
1188 /* Eaten as whole. */
1189 eat -= list->len;
1190 list = list->next;
1191 insp = list;
1192 } else {
1193 /* Eaten partially. */
1195 if (skb_shared(list)) {
1196 /* Sucks! We need to fork list. :-( */
1197 clone = skb_clone(list, GFP_ATOMIC);
1198 if (!clone)
1199 return NULL;
1200 insp = list->next;
1201 list = clone;
1202 } else {
1203 /* This may be pulled without
1204 * problems. */
1205 insp = list;
1207 if (!pskb_pull(list, eat)) {
1208 if (clone)
1209 kfree_skb(clone);
1210 return NULL;
1212 break;
1214 } while (eat);
1216 /* Free pulled out fragments. */
1217 while ((list = skb_shinfo(skb)->frag_list) != insp) {
1218 skb_shinfo(skb)->frag_list = list->next;
1219 kfree_skb(list);
1221 /* And insert new clone at head. */
1222 if (clone) {
1223 clone->next = list;
1224 skb_shinfo(skb)->frag_list = clone;
1227 /* Success! Now we may commit changes to skb data. */
1229 pull_pages:
1230 eat = delta;
1231 k = 0;
1232 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1233 if (skb_shinfo(skb)->frags[i].size <= eat) {
1234 put_page(skb_shinfo(skb)->frags[i].page);
1235 eat -= skb_shinfo(skb)->frags[i].size;
1236 } else {
1237 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1238 if (eat) {
1239 skb_shinfo(skb)->frags[k].page_offset += eat;
1240 skb_shinfo(skb)->frags[k].size -= eat;
1241 eat = 0;
1243 k++;
1246 skb_shinfo(skb)->nr_frags = k;
1248 skb->tail += delta;
1249 skb->data_len -= delta;
1251 return skb_tail_pointer(skb);
1254 /* Copy some data bits from skb to kernel buffer. */
1256 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1258 int i, copy;
1259 int start = skb_headlen(skb);
1261 if (offset > (int)skb->len - len)
1262 goto fault;
1264 /* Copy header. */
1265 if ((copy = start - offset) > 0) {
1266 if (copy > len)
1267 copy = len;
1268 skb_copy_from_linear_data_offset(skb, offset, to, copy);
1269 if ((len -= copy) == 0)
1270 return 0;
1271 offset += copy;
1272 to += copy;
1275 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1276 int end;
1278 WARN_ON(start > offset + len);
1280 end = start + skb_shinfo(skb)->frags[i].size;
1281 if ((copy = end - offset) > 0) {
1282 u8 *vaddr;
1284 if (copy > len)
1285 copy = len;
1287 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
1288 memcpy(to,
1289 vaddr + skb_shinfo(skb)->frags[i].page_offset+
1290 offset - start, copy);
1291 kunmap_skb_frag(vaddr);
1293 if ((len -= copy) == 0)
1294 return 0;
1295 offset += copy;
1296 to += copy;
1298 start = end;
1301 if (skb_shinfo(skb)->frag_list) {
1302 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1304 for (; list; list = list->next) {
1305 int end;
1307 WARN_ON(start > offset + len);
1309 end = start + list->len;
1310 if ((copy = end - offset) > 0) {
1311 if (copy > len)
1312 copy = len;
1313 if (skb_copy_bits(list, offset - start,
1314 to, copy))
1315 goto fault;
1316 if ((len -= copy) == 0)
1317 return 0;
1318 offset += copy;
1319 to += copy;
1321 start = end;
1324 if (!len)
1325 return 0;
1327 fault:
1328 return -EFAULT;
1332 * Callback from splice_to_pipe(), if we need to release some pages
1333 * at the end of the spd in case we error'ed out in filling the pipe.
1335 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1337 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
1339 kfree_skb(skb);
1343 * Fill page/offset/length into spd, if it can hold more pages.
1345 static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
1346 unsigned int len, unsigned int offset,
1347 struct sk_buff *skb)
1349 if (unlikely(spd->nr_pages == PIPE_BUFFERS))
1350 return 1;
1352 spd->pages[spd->nr_pages] = page;
1353 spd->partial[spd->nr_pages].len = len;
1354 spd->partial[spd->nr_pages].offset = offset;
1355 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
1356 spd->nr_pages++;
1357 return 0;
1360 static inline void __segment_seek(struct page **page, unsigned int *poff,
1361 unsigned int *plen, unsigned int off)
1363 *poff += off;
1364 *page += *poff / PAGE_SIZE;
1365 *poff = *poff % PAGE_SIZE;
1366 *plen -= off;
1369 static inline int __splice_segment(struct page *page, unsigned int poff,
1370 unsigned int plen, unsigned int *off,
1371 unsigned int *len, struct sk_buff *skb,
1372 struct splice_pipe_desc *spd)
1374 if (!*len)
1375 return 1;
1377 /* skip this segment if already processed */
1378 if (*off >= plen) {
1379 *off -= plen;
1380 return 0;
1383 /* ignore any bits we already processed */
1384 if (*off) {
1385 __segment_seek(&page, &poff, &plen, *off);
1386 *off = 0;
1389 do {
1390 unsigned int flen = min(*len, plen);
1392 /* the linear region may spread across several pages */
1393 flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
1395 if (spd_fill_page(spd, page, flen, poff, skb))
1396 return 1;
1398 __segment_seek(&page, &poff, &plen, flen);
1399 *len -= flen;
1401 } while (*len && plen);
1403 return 0;
1407 * Map linear and fragment data from the skb to spd. It reports failure if the
1408 * pipe is full or if we already spliced the requested length.
1410 static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
1411 unsigned int *len,
1412 struct splice_pipe_desc *spd)
1414 int seg;
1417 * map the linear part
1419 if (__splice_segment(virt_to_page(skb->data),
1420 (unsigned long) skb->data & (PAGE_SIZE - 1),
1421 skb_headlen(skb),
1422 offset, len, skb, spd))
1423 return 1;
1426 * then map the fragments
1428 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1429 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1431 if (__splice_segment(f->page, f->page_offset, f->size,
1432 offset, len, skb, spd))
1433 return 1;
1436 return 0;
1440 * Map data from the skb to a pipe. Should handle both the linear part,
1441 * the fragments, and the frag list. It does NOT handle frag lists within
1442 * the frag list, if such a thing exists. We'd probably need to recurse to
1443 * handle that cleanly.
1445 int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
1446 struct pipe_inode_info *pipe, unsigned int tlen,
1447 unsigned int flags)
1449 struct partial_page partial[PIPE_BUFFERS];
1450 struct page *pages[PIPE_BUFFERS];
1451 struct splice_pipe_desc spd = {
1452 .pages = pages,
1453 .partial = partial,
1454 .flags = flags,
1455 .ops = &sock_pipe_buf_ops,
1456 .spd_release = sock_spd_release,
1458 struct sk_buff *skb;
1461 * I'd love to avoid the clone here, but tcp_read_sock()
1462 * ignores reference counts and unconditonally kills the sk_buff
1463 * on return from the actor.
1465 skb = skb_clone(__skb, GFP_KERNEL);
1466 if (unlikely(!skb))
1467 return -ENOMEM;
1470 * __skb_splice_bits() only fails if the output has no room left,
1471 * so no point in going over the frag_list for the error case.
1473 if (__skb_splice_bits(skb, &offset, &tlen, &spd))
1474 goto done;
1475 else if (!tlen)
1476 goto done;
1479 * now see if we have a frag_list to map
1481 if (skb_shinfo(skb)->frag_list) {
1482 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1484 for (; list && tlen; list = list->next) {
1485 if (__skb_splice_bits(list, &offset, &tlen, &spd))
1486 break;
1490 done:
1492 * drop our reference to the clone, the pipe consumption will
1493 * drop the rest.
1495 kfree_skb(skb);
1497 if (spd.nr_pages) {
1498 int ret;
1499 struct sock *sk = __skb->sk;
1502 * Drop the socket lock, otherwise we have reverse
1503 * locking dependencies between sk_lock and i_mutex
1504 * here as compared to sendfile(). We enter here
1505 * with the socket lock held, and splice_to_pipe() will
1506 * grab the pipe inode lock. For sendfile() emulation,
1507 * we call into ->sendpage() with the i_mutex lock held
1508 * and networking will grab the socket lock.
1510 release_sock(sk);
1511 ret = splice_to_pipe(pipe, &spd);
1512 lock_sock(sk);
1513 return ret;
1516 return 0;
1520 * skb_store_bits - store bits from kernel buffer to skb
1521 * @skb: destination buffer
1522 * @offset: offset in destination
1523 * @from: source buffer
1524 * @len: number of bytes to copy
1526 * Copy the specified number of bytes from the source buffer to the
1527 * destination skb. This function handles all the messy bits of
1528 * traversing fragment lists and such.
1531 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1533 int i, copy;
1534 int start = skb_headlen(skb);
1536 if (offset > (int)skb->len - len)
1537 goto fault;
1539 if ((copy = start - offset) > 0) {
1540 if (copy > len)
1541 copy = len;
1542 skb_copy_to_linear_data_offset(skb, offset, from, copy);
1543 if ((len -= copy) == 0)
1544 return 0;
1545 offset += copy;
1546 from += copy;
1549 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1550 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1551 int end;
1553 WARN_ON(start > offset + len);
1555 end = start + frag->size;
1556 if ((copy = end - offset) > 0) {
1557 u8 *vaddr;
1559 if (copy > len)
1560 copy = len;
1562 vaddr = kmap_skb_frag(frag);
1563 memcpy(vaddr + frag->page_offset + offset - start,
1564 from, copy);
1565 kunmap_skb_frag(vaddr);
1567 if ((len -= copy) == 0)
1568 return 0;
1569 offset += copy;
1570 from += copy;
1572 start = end;
1575 if (skb_shinfo(skb)->frag_list) {
1576 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1578 for (; list; list = list->next) {
1579 int end;
1581 WARN_ON(start > offset + len);
1583 end = start + list->len;
1584 if ((copy = end - offset) > 0) {
1585 if (copy > len)
1586 copy = len;
1587 if (skb_store_bits(list, offset - start,
1588 from, copy))
1589 goto fault;
1590 if ((len -= copy) == 0)
1591 return 0;
1592 offset += copy;
1593 from += copy;
1595 start = end;
1598 if (!len)
1599 return 0;
1601 fault:
1602 return -EFAULT;
1605 EXPORT_SYMBOL(skb_store_bits);
1607 /* Checksum skb data. */
1609 __wsum skb_checksum(const struct sk_buff *skb, int offset,
1610 int len, __wsum csum)
1612 int start = skb_headlen(skb);
1613 int i, copy = start - offset;
1614 int pos = 0;
1616 /* Checksum header. */
1617 if (copy > 0) {
1618 if (copy > len)
1619 copy = len;
1620 csum = csum_partial(skb->data + offset, copy, csum);
1621 if ((len -= copy) == 0)
1622 return csum;
1623 offset += copy;
1624 pos = copy;
1627 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1628 int end;
1630 WARN_ON(start > offset + len);
1632 end = start + skb_shinfo(skb)->frags[i].size;
1633 if ((copy = end - offset) > 0) {
1634 __wsum csum2;
1635 u8 *vaddr;
1636 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1638 if (copy > len)
1639 copy = len;
1640 vaddr = kmap_skb_frag(frag);
1641 csum2 = csum_partial(vaddr + frag->page_offset +
1642 offset - start, copy, 0);
1643 kunmap_skb_frag(vaddr);
1644 csum = csum_block_add(csum, csum2, pos);
1645 if (!(len -= copy))
1646 return csum;
1647 offset += copy;
1648 pos += copy;
1650 start = end;
1653 if (skb_shinfo(skb)->frag_list) {
1654 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1656 for (; list; list = list->next) {
1657 int end;
1659 WARN_ON(start > offset + len);
1661 end = start + list->len;
1662 if ((copy = end - offset) > 0) {
1663 __wsum csum2;
1664 if (copy > len)
1665 copy = len;
1666 csum2 = skb_checksum(list, offset - start,
1667 copy, 0);
1668 csum = csum_block_add(csum, csum2, pos);
1669 if ((len -= copy) == 0)
1670 return csum;
1671 offset += copy;
1672 pos += copy;
1674 start = end;
1677 BUG_ON(len);
1679 return csum;
1682 /* Both of above in one bottle. */
1684 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
1685 u8 *to, int len, __wsum csum)
1687 int start = skb_headlen(skb);
1688 int i, copy = start - offset;
1689 int pos = 0;
1691 /* Copy header. */
1692 if (copy > 0) {
1693 if (copy > len)
1694 copy = len;
1695 csum = csum_partial_copy_nocheck(skb->data + offset, to,
1696 copy, csum);
1697 if ((len -= copy) == 0)
1698 return csum;
1699 offset += copy;
1700 to += copy;
1701 pos = copy;
1704 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1705 int end;
1707 WARN_ON(start > offset + len);
1709 end = start + skb_shinfo(skb)->frags[i].size;
1710 if ((copy = end - offset) > 0) {
1711 __wsum csum2;
1712 u8 *vaddr;
1713 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1715 if (copy > len)
1716 copy = len;
1717 vaddr = kmap_skb_frag(frag);
1718 csum2 = csum_partial_copy_nocheck(vaddr +
1719 frag->page_offset +
1720 offset - start, to,
1721 copy, 0);
1722 kunmap_skb_frag(vaddr);
1723 csum = csum_block_add(csum, csum2, pos);
1724 if (!(len -= copy))
1725 return csum;
1726 offset += copy;
1727 to += copy;
1728 pos += copy;
1730 start = end;
1733 if (skb_shinfo(skb)->frag_list) {
1734 struct sk_buff *list = skb_shinfo(skb)->frag_list;
1736 for (; list; list = list->next) {
1737 __wsum csum2;
1738 int end;
1740 WARN_ON(start > offset + len);
1742 end = start + list->len;
1743 if ((copy = end - offset) > 0) {
1744 if (copy > len)
1745 copy = len;
1746 csum2 = skb_copy_and_csum_bits(list,
1747 offset - start,
1748 to, copy, 0);
1749 csum = csum_block_add(csum, csum2, pos);
1750 if ((len -= copy) == 0)
1751 return csum;
1752 offset += copy;
1753 to += copy;
1754 pos += copy;
1756 start = end;
1759 BUG_ON(len);
1760 return csum;
1763 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
1765 __wsum csum;
1766 long csstart;
1768 if (skb->ip_summed == CHECKSUM_PARTIAL)
1769 csstart = skb->csum_start - skb_headroom(skb);
1770 else
1771 csstart = skb_headlen(skb);
1773 BUG_ON(csstart > skb_headlen(skb));
1775 skb_copy_from_linear_data(skb, to, csstart);
1777 csum = 0;
1778 if (csstart != skb->len)
1779 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
1780 skb->len - csstart, 0);
1782 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1783 long csstuff = csstart + skb->csum_offset;
1785 *((__sum16 *)(to + csstuff)) = csum_fold(csum);
1790 * skb_dequeue - remove from the head of the queue
1791 * @list: list to dequeue from
1793 * Remove the head of the list. The list lock is taken so the function
1794 * may be used safely with other locking list functions. The head item is
1795 * returned or %NULL if the list is empty.
1798 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
1800 unsigned long flags;
1801 struct sk_buff *result;
1803 spin_lock_irqsave(&list->lock, flags);
1804 result = __skb_dequeue(list);
1805 spin_unlock_irqrestore(&list->lock, flags);
1806 return result;
1810 * skb_dequeue_tail - remove from the tail of the queue
1811 * @list: list to dequeue from
1813 * Remove the tail of the list. The list lock is taken so the function
1814 * may be used safely with other locking list functions. The tail item is
1815 * returned or %NULL if the list is empty.
1817 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
1819 unsigned long flags;
1820 struct sk_buff *result;
1822 spin_lock_irqsave(&list->lock, flags);
1823 result = __skb_dequeue_tail(list);
1824 spin_unlock_irqrestore(&list->lock, flags);
1825 return result;
1829 * skb_queue_purge - empty a list
1830 * @list: list to empty
1832 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1833 * the list and one reference dropped. This function takes the list
1834 * lock and is atomic with respect to other list locking functions.
1836 void skb_queue_purge(struct sk_buff_head *list)
1838 struct sk_buff *skb;
1839 while ((skb = skb_dequeue(list)) != NULL)
1840 kfree_skb(skb);
1844 * skb_queue_head - queue a buffer at the list head
1845 * @list: list to use
1846 * @newsk: buffer to queue
1848 * Queue a buffer at the start of the list. This function takes the
1849 * list lock and can be used safely with other locking &sk_buff functions
1850 * safely.
1852 * A buffer cannot be placed on two lists at the same time.
1854 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
1856 unsigned long flags;
1858 spin_lock_irqsave(&list->lock, flags);
1859 __skb_queue_head(list, newsk);
1860 spin_unlock_irqrestore(&list->lock, flags);
1864 * skb_queue_tail - queue a buffer at the list tail
1865 * @list: list to use
1866 * @newsk: buffer to queue
1868 * Queue a buffer at the tail of the list. This function takes the
1869 * list lock and can be used safely with other locking &sk_buff functions
1870 * safely.
1872 * A buffer cannot be placed on two lists at the same time.
1874 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
1876 unsigned long flags;
1878 spin_lock_irqsave(&list->lock, flags);
1879 __skb_queue_tail(list, newsk);
1880 spin_unlock_irqrestore(&list->lock, flags);
1884 * skb_unlink - remove a buffer from a list
1885 * @skb: buffer to remove
1886 * @list: list to use
1888 * Remove a packet from a list. The list locks are taken and this
1889 * function is atomic with respect to other list locked calls
1891 * You must know what list the SKB is on.
1893 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
1895 unsigned long flags;
1897 spin_lock_irqsave(&list->lock, flags);
1898 __skb_unlink(skb, list);
1899 spin_unlock_irqrestore(&list->lock, flags);
1903 * skb_append - append a buffer
1904 * @old: buffer to insert after
1905 * @newsk: buffer to insert
1906 * @list: list to use
1908 * Place a packet after a given packet in a list. The list locks are taken
1909 * and this function is atomic with respect to other list locked calls.
1910 * A buffer cannot be placed on two lists at the same time.
1912 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1914 unsigned long flags;
1916 spin_lock_irqsave(&list->lock, flags);
1917 __skb_queue_after(list, old, newsk);
1918 spin_unlock_irqrestore(&list->lock, flags);
1923 * skb_insert - insert a buffer
1924 * @old: buffer to insert before
1925 * @newsk: buffer to insert
1926 * @list: list to use
1928 * Place a packet before a given packet in a list. The list locks are
1929 * taken and this function is atomic with respect to other list locked
1930 * calls.
1932 * A buffer cannot be placed on two lists at the same time.
1934 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
1936 unsigned long flags;
1938 spin_lock_irqsave(&list->lock, flags);
1939 __skb_insert(newsk, old->prev, old, list);
1940 spin_unlock_irqrestore(&list->lock, flags);
1943 static inline void skb_split_inside_header(struct sk_buff *skb,
1944 struct sk_buff* skb1,
1945 const u32 len, const int pos)
1947 int i;
1949 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1950 pos - len);
1951 /* And move data appendix as is. */
1952 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1953 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
1955 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
1956 skb_shinfo(skb)->nr_frags = 0;
1957 skb1->data_len = skb->data_len;
1958 skb1->len += skb1->data_len;
1959 skb->data_len = 0;
1960 skb->len = len;
1961 skb_set_tail_pointer(skb, len);
1964 static inline void skb_split_no_header(struct sk_buff *skb,
1965 struct sk_buff* skb1,
1966 const u32 len, int pos)
1968 int i, k = 0;
1969 const int nfrags = skb_shinfo(skb)->nr_frags;
1971 skb_shinfo(skb)->nr_frags = 0;
1972 skb1->len = skb1->data_len = skb->len - len;
1973 skb->len = len;
1974 skb->data_len = len - pos;
1976 for (i = 0; i < nfrags; i++) {
1977 int size = skb_shinfo(skb)->frags[i].size;
1979 if (pos + size > len) {
1980 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
1982 if (pos < len) {
1983 /* Split frag.
1984 * We have two variants in this case:
1985 * 1. Move all the frag to the second
1986 * part, if it is possible. F.e.
1987 * this approach is mandatory for TUX,
1988 * where splitting is expensive.
1989 * 2. Split is accurately. We make this.
1991 get_page(skb_shinfo(skb)->frags[i].page);
1992 skb_shinfo(skb1)->frags[0].page_offset += len - pos;
1993 skb_shinfo(skb1)->frags[0].size -= len - pos;
1994 skb_shinfo(skb)->frags[i].size = len - pos;
1995 skb_shinfo(skb)->nr_frags++;
1997 k++;
1998 } else
1999 skb_shinfo(skb)->nr_frags++;
2000 pos += size;
2002 skb_shinfo(skb1)->nr_frags = k;
2006 * skb_split - Split fragmented skb to two parts at length len.
2007 * @skb: the buffer to split
2008 * @skb1: the buffer to receive the second part
2009 * @len: new length for skb
2011 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2013 int pos = skb_headlen(skb);
2015 if (len < pos) /* Split line is inside header. */
2016 skb_split_inside_header(skb, skb1, len, pos);
2017 else /* Second chunk has no header, nothing to copy. */
2018 skb_split_no_header(skb, skb1, len, pos);
2021 /* Shifting from/to a cloned skb is a no-go. */
2022 static int skb_prepare_for_shift(struct sk_buff *skb)
2024 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2028 * skb_shift - Shifts paged data partially from skb to another
2029 * @tgt: buffer into which tail data gets added
2030 * @skb: buffer from which the paged data comes from
2031 * @shiftlen: shift up to this many bytes
2033 * Attempts to shift up to shiftlen worth of bytes, which may be less than
2034 * the length of the skb, from tgt to skb. Returns number bytes shifted.
2035 * It's up to caller to free skb if everything was shifted.
2037 * If @tgt runs out of frags, the whole operation is aborted.
2039 * Skb cannot include anything else but paged data while tgt is allowed
2040 * to have non-paged data as well.
2042 * TODO: full sized shift could be optimized but that would need
2043 * specialized skb free'er to handle frags without up-to-date nr_frags.
2045 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2047 int from, to, merge, todo;
2048 struct skb_frag_struct *fragfrom, *fragto;
2050 BUG_ON(shiftlen > skb->len);
2051 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
2053 todo = shiftlen;
2054 from = 0;
2055 to = skb_shinfo(tgt)->nr_frags;
2056 fragfrom = &skb_shinfo(skb)->frags[from];
2058 /* Actual merge is delayed until the point when we know we can
2059 * commit all, so that we don't have to undo partial changes
2061 if (!to ||
2062 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2063 merge = -1;
2064 } else {
2065 merge = to - 1;
2067 todo -= fragfrom->size;
2068 if (todo < 0) {
2069 if (skb_prepare_for_shift(skb) ||
2070 skb_prepare_for_shift(tgt))
2071 return 0;
2073 fragto = &skb_shinfo(tgt)->frags[merge];
2075 fragto->size += shiftlen;
2076 fragfrom->size -= shiftlen;
2077 fragfrom->page_offset += shiftlen;
2079 goto onlymerged;
2082 from++;
2085 /* Skip full, not-fitting skb to avoid expensive operations */
2086 if ((shiftlen == skb->len) &&
2087 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2088 return 0;
2090 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2091 return 0;
2093 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2094 if (to == MAX_SKB_FRAGS)
2095 return 0;
2097 fragfrom = &skb_shinfo(skb)->frags[from];
2098 fragto = &skb_shinfo(tgt)->frags[to];
2100 if (todo >= fragfrom->size) {
2101 *fragto = *fragfrom;
2102 todo -= fragfrom->size;
2103 from++;
2104 to++;
2106 } else {
2107 get_page(fragfrom->page);
2108 fragto->page = fragfrom->page;
2109 fragto->page_offset = fragfrom->page_offset;
2110 fragto->size = todo;
2112 fragfrom->page_offset += todo;
2113 fragfrom->size -= todo;
2114 todo = 0;
2116 to++;
2117 break;
2121 /* Ready to "commit" this state change to tgt */
2122 skb_shinfo(tgt)->nr_frags = to;
2124 if (merge >= 0) {
2125 fragfrom = &skb_shinfo(skb)->frags[0];
2126 fragto = &skb_shinfo(tgt)->frags[merge];
2128 fragto->size += fragfrom->size;
2129 put_page(fragfrom->page);
2132 /* Reposition in the original skb */
2133 to = 0;
2134 while (from < skb_shinfo(skb)->nr_frags)
2135 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2136 skb_shinfo(skb)->nr_frags = to;
2138 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2140 onlymerged:
2141 /* Most likely the tgt won't ever need its checksum anymore, skb on
2142 * the other hand might need it if it needs to be resent
2144 tgt->ip_summed = CHECKSUM_PARTIAL;
2145 skb->ip_summed = CHECKSUM_PARTIAL;
2147 /* Yak, is it really working this way? Some helper please? */
2148 skb->len -= shiftlen;
2149 skb->data_len -= shiftlen;
2150 skb->truesize -= shiftlen;
2151 tgt->len += shiftlen;
2152 tgt->data_len += shiftlen;
2153 tgt->truesize += shiftlen;
2155 return shiftlen;
2159 * skb_prepare_seq_read - Prepare a sequential read of skb data
2160 * @skb: the buffer to read
2161 * @from: lower offset of data to be read
2162 * @to: upper offset of data to be read
2163 * @st: state variable
2165 * Initializes the specified state variable. Must be called before
2166 * invoking skb_seq_read() for the first time.
2168 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2169 unsigned int to, struct skb_seq_state *st)
2171 st->lower_offset = from;
2172 st->upper_offset = to;
2173 st->root_skb = st->cur_skb = skb;
2174 st->frag_idx = st->stepped_offset = 0;
2175 st->frag_data = NULL;
2179 * skb_seq_read - Sequentially read skb data
2180 * @consumed: number of bytes consumed by the caller so far
2181 * @data: destination pointer for data to be returned
2182 * @st: state variable
2184 * Reads a block of skb data at &consumed relative to the
2185 * lower offset specified to skb_prepare_seq_read(). Assigns
2186 * the head of the data block to &data and returns the length
2187 * of the block or 0 if the end of the skb data or the upper
2188 * offset has been reached.
2190 * The caller is not required to consume all of the data
2191 * returned, i.e. &consumed is typically set to the number
2192 * of bytes already consumed and the next call to
2193 * skb_seq_read() will return the remaining part of the block.
2195 * Note 1: The size of each block of data returned can be arbitary,
2196 * this limitation is the cost for zerocopy seqeuental
2197 * reads of potentially non linear data.
2199 * Note 2: Fragment lists within fragments are not implemented
2200 * at the moment, state->root_skb could be replaced with
2201 * a stack for this purpose.
2203 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2204 struct skb_seq_state *st)
2206 unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2207 skb_frag_t *frag;
2209 if (unlikely(abs_offset >= st->upper_offset))
2210 return 0;
2212 next_skb:
2213 block_limit = skb_headlen(st->cur_skb);
2215 if (abs_offset < block_limit) {
2216 *data = st->cur_skb->data + abs_offset;
2217 return block_limit - abs_offset;
2220 if (st->frag_idx == 0 && !st->frag_data)
2221 st->stepped_offset += skb_headlen(st->cur_skb);
2223 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2224 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2225 block_limit = frag->size + st->stepped_offset;
2227 if (abs_offset < block_limit) {
2228 if (!st->frag_data)
2229 st->frag_data = kmap_skb_frag(frag);
2231 *data = (u8 *) st->frag_data + frag->page_offset +
2232 (abs_offset - st->stepped_offset);
2234 return block_limit - abs_offset;
2237 if (st->frag_data) {
2238 kunmap_skb_frag(st->frag_data);
2239 st->frag_data = NULL;
2242 st->frag_idx++;
2243 st->stepped_offset += frag->size;
2246 if (st->frag_data) {
2247 kunmap_skb_frag(st->frag_data);
2248 st->frag_data = NULL;
2251 if (st->cur_skb->next) {
2252 st->cur_skb = st->cur_skb->next;
2253 st->frag_idx = 0;
2254 goto next_skb;
2255 } else if (st->root_skb == st->cur_skb &&
2256 skb_shinfo(st->root_skb)->frag_list) {
2257 st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2258 goto next_skb;
2261 return 0;
2265 * skb_abort_seq_read - Abort a sequential read of skb data
2266 * @st: state variable
2268 * Must be called if skb_seq_read() was not called until it
2269 * returned 0.
2271 void skb_abort_seq_read(struct skb_seq_state *st)
2273 if (st->frag_data)
2274 kunmap_skb_frag(st->frag_data);
2277 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
2279 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2280 struct ts_config *conf,
2281 struct ts_state *state)
2283 return skb_seq_read(offset, text, TS_SKB_CB(state));
2286 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2288 skb_abort_seq_read(TS_SKB_CB(state));
2292 * skb_find_text - Find a text pattern in skb data
2293 * @skb: the buffer to look in
2294 * @from: search offset
2295 * @to: search limit
2296 * @config: textsearch configuration
2297 * @state: uninitialized textsearch state variable
2299 * Finds a pattern in the skb data according to the specified
2300 * textsearch configuration. Use textsearch_next() to retrieve
2301 * subsequent occurrences of the pattern. Returns the offset
2302 * to the first occurrence or UINT_MAX if no match was found.
2304 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2305 unsigned int to, struct ts_config *config,
2306 struct ts_state *state)
2308 unsigned int ret;
2310 config->get_next_block = skb_ts_get_next_block;
2311 config->finish = skb_ts_finish;
2313 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
2315 ret = textsearch_find(config, state);
2316 return (ret <= to - from ? ret : UINT_MAX);
2320 * skb_append_datato_frags: - append the user data to a skb
2321 * @sk: sock structure
2322 * @skb: skb structure to be appened with user data.
2323 * @getfrag: call back function to be used for getting the user data
2324 * @from: pointer to user message iov
2325 * @length: length of the iov message
2327 * Description: This procedure append the user data in the fragment part
2328 * of the skb if any page alloc fails user this procedure returns -ENOMEM
2330 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2331 int (*getfrag)(void *from, char *to, int offset,
2332 int len, int odd, struct sk_buff *skb),
2333 void *from, int length)
2335 int frg_cnt = 0;
2336 skb_frag_t *frag = NULL;
2337 struct page *page = NULL;
2338 int copy, left;
2339 int offset = 0;
2340 int ret;
2342 do {
2343 /* Return error if we don't have space for new frag */
2344 frg_cnt = skb_shinfo(skb)->nr_frags;
2345 if (frg_cnt >= MAX_SKB_FRAGS)
2346 return -EFAULT;
2348 /* allocate a new page for next frag */
2349 page = alloc_pages(sk->sk_allocation, 0);
2351 /* If alloc_page fails just return failure and caller will
2352 * free previous allocated pages by doing kfree_skb()
2354 if (page == NULL)
2355 return -ENOMEM;
2357 /* initialize the next frag */
2358 sk->sk_sndmsg_page = page;
2359 sk->sk_sndmsg_off = 0;
2360 skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2361 skb->truesize += PAGE_SIZE;
2362 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2364 /* get the new initialized frag */
2365 frg_cnt = skb_shinfo(skb)->nr_frags;
2366 frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2368 /* copy the user data to page */
2369 left = PAGE_SIZE - frag->page_offset;
2370 copy = (length > left)? left : length;
2372 ret = getfrag(from, (page_address(frag->page) +
2373 frag->page_offset + frag->size),
2374 offset, copy, 0, skb);
2375 if (ret < 0)
2376 return -EFAULT;
2378 /* copy was successful so update the size parameters */
2379 sk->sk_sndmsg_off += copy;
2380 frag->size += copy;
2381 skb->len += copy;
2382 skb->data_len += copy;
2383 offset += copy;
2384 length -= copy;
2386 } while (length > 0);
2388 return 0;
2392 * skb_pull_rcsum - pull skb and update receive checksum
2393 * @skb: buffer to update
2394 * @len: length of data pulled
2396 * This function performs an skb_pull on the packet and updates
2397 * the CHECKSUM_COMPLETE checksum. It should be used on
2398 * receive path processing instead of skb_pull unless you know
2399 * that the checksum difference is zero (e.g., a valid IP header)
2400 * or you are setting ip_summed to CHECKSUM_NONE.
2402 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2404 BUG_ON(len > skb->len);
2405 skb->len -= len;
2406 BUG_ON(skb->len < skb->data_len);
2407 skb_postpull_rcsum(skb, skb->data, len);
2408 return skb->data += len;
2411 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2414 * skb_segment - Perform protocol segmentation on skb.
2415 * @skb: buffer to segment
2416 * @features: features for the output path (see dev->features)
2418 * This function performs segmentation on the given skb. It returns
2419 * a pointer to the first in a list of new skbs for the segments.
2420 * In case of error it returns ERR_PTR(err).
2422 struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2424 struct sk_buff *segs = NULL;
2425 struct sk_buff *tail = NULL;
2426 unsigned int mss = skb_shinfo(skb)->gso_size;
2427 unsigned int doffset = skb->data - skb_mac_header(skb);
2428 unsigned int offset = doffset;
2429 unsigned int headroom;
2430 unsigned int len;
2431 int sg = features & NETIF_F_SG;
2432 int nfrags = skb_shinfo(skb)->nr_frags;
2433 int err = -ENOMEM;
2434 int i = 0;
2435 int pos;
2437 __skb_push(skb, doffset);
2438 headroom = skb_headroom(skb);
2439 pos = skb_headlen(skb);
2441 do {
2442 struct sk_buff *nskb;
2443 skb_frag_t *frag;
2444 int hsize;
2445 int k;
2446 int size;
2448 len = skb->len - offset;
2449 if (len > mss)
2450 len = mss;
2452 hsize = skb_headlen(skb) - offset;
2453 if (hsize < 0)
2454 hsize = 0;
2455 if (hsize > len || !sg)
2456 hsize = len;
2458 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2459 if (unlikely(!nskb))
2460 goto err;
2462 if (segs)
2463 tail->next = nskb;
2464 else
2465 segs = nskb;
2466 tail = nskb;
2468 __copy_skb_header(nskb, skb);
2469 nskb->mac_len = skb->mac_len;
2471 skb_reserve(nskb, headroom);
2472 skb_reset_mac_header(nskb);
2473 skb_set_network_header(nskb, skb->mac_len);
2474 nskb->transport_header = (nskb->network_header +
2475 skb_network_header_len(skb));
2476 skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2477 doffset);
2478 if (!sg) {
2479 nskb->ip_summed = CHECKSUM_NONE;
2480 nskb->csum = skb_copy_and_csum_bits(skb, offset,
2481 skb_put(nskb, len),
2482 len, 0);
2483 continue;
2486 frag = skb_shinfo(nskb)->frags;
2487 k = 0;
2489 skb_copy_from_linear_data_offset(skb, offset,
2490 skb_put(nskb, hsize), hsize);
2492 while (pos < offset + len) {
2493 BUG_ON(i >= nfrags);
2495 *frag = skb_shinfo(skb)->frags[i];
2496 get_page(frag->page);
2497 size = frag->size;
2499 if (pos < offset) {
2500 frag->page_offset += offset - pos;
2501 frag->size -= offset - pos;
2504 k++;
2506 if (pos + size <= offset + len) {
2507 i++;
2508 pos += size;
2509 } else {
2510 frag->size -= pos + size - (offset + len);
2511 break;
2514 frag++;
2517 skb_shinfo(nskb)->nr_frags = k;
2518 nskb->data_len = len - hsize;
2519 nskb->len += nskb->data_len;
2520 nskb->truesize += nskb->data_len;
2521 } while ((offset += len) < skb->len);
2523 return segs;
2525 err:
2526 while ((skb = segs)) {
2527 segs = skb->next;
2528 kfree_skb(skb);
2530 return ERR_PTR(err);
2533 EXPORT_SYMBOL_GPL(skb_segment);
2535 void __init skb_init(void)
2537 skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
2538 sizeof(struct sk_buff),
2540 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2541 NULL);
2542 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2543 (2*sizeof(struct sk_buff)) +
2544 sizeof(atomic_t),
2546 SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2547 NULL);
2551 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2552 * @skb: Socket buffer containing the buffers to be mapped
2553 * @sg: The scatter-gather list to map into
2554 * @offset: The offset into the buffer's contents to start mapping
2555 * @len: Length of buffer space to be mapped
2557 * Fill the specified scatter-gather list with mappings/pointers into a
2558 * region of the buffer space attached to a socket buffer.
2560 static int
2561 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2563 int start = skb_headlen(skb);
2564 int i, copy = start - offset;
2565 int elt = 0;
2567 if (copy > 0) {
2568 if (copy > len)
2569 copy = len;
2570 sg_set_buf(sg, skb->data + offset, copy);
2571 elt++;
2572 if ((len -= copy) == 0)
2573 return elt;
2574 offset += copy;
2577 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2578 int end;
2580 WARN_ON(start > offset + len);
2582 end = start + skb_shinfo(skb)->frags[i].size;
2583 if ((copy = end - offset) > 0) {
2584 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2586 if (copy > len)
2587 copy = len;
2588 sg_set_page(&sg[elt], frag->page, copy,
2589 frag->page_offset+offset-start);
2590 elt++;
2591 if (!(len -= copy))
2592 return elt;
2593 offset += copy;
2595 start = end;
2598 if (skb_shinfo(skb)->frag_list) {
2599 struct sk_buff *list = skb_shinfo(skb)->frag_list;
2601 for (; list; list = list->next) {
2602 int end;
2604 WARN_ON(start > offset + len);
2606 end = start + list->len;
2607 if ((copy = end - offset) > 0) {
2608 if (copy > len)
2609 copy = len;
2610 elt += __skb_to_sgvec(list, sg+elt, offset - start,
2611 copy);
2612 if ((len -= copy) == 0)
2613 return elt;
2614 offset += copy;
2616 start = end;
2619 BUG_ON(len);
2620 return elt;
2623 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2625 int nsg = __skb_to_sgvec(skb, sg, offset, len);
2627 sg_mark_end(&sg[nsg - 1]);
2629 return nsg;
2633 * skb_cow_data - Check that a socket buffer's data buffers are writable
2634 * @skb: The socket buffer to check.
2635 * @tailbits: Amount of trailing space to be added
2636 * @trailer: Returned pointer to the skb where the @tailbits space begins
2638 * Make sure that the data buffers attached to a socket buffer are
2639 * writable. If they are not, private copies are made of the data buffers
2640 * and the socket buffer is set to use these instead.
2642 * If @tailbits is given, make sure that there is space to write @tailbits
2643 * bytes of data beyond current end of socket buffer. @trailer will be
2644 * set to point to the skb in which this space begins.
2646 * The number of scatterlist elements required to completely map the
2647 * COW'd and extended socket buffer will be returned.
2649 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2651 int copyflag;
2652 int elt;
2653 struct sk_buff *skb1, **skb_p;
2655 /* If skb is cloned or its head is paged, reallocate
2656 * head pulling out all the pages (pages are considered not writable
2657 * at the moment even if they are anonymous).
2659 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2660 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2661 return -ENOMEM;
2663 /* Easy case. Most of packets will go this way. */
2664 if (!skb_shinfo(skb)->frag_list) {
2665 /* A little of trouble, not enough of space for trailer.
2666 * This should not happen, when stack is tuned to generate
2667 * good frames. OK, on miss we reallocate and reserve even more
2668 * space, 128 bytes is fair. */
2670 if (skb_tailroom(skb) < tailbits &&
2671 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2672 return -ENOMEM;
2674 /* Voila! */
2675 *trailer = skb;
2676 return 1;
2679 /* Misery. We are in troubles, going to mincer fragments... */
2681 elt = 1;
2682 skb_p = &skb_shinfo(skb)->frag_list;
2683 copyflag = 0;
2685 while ((skb1 = *skb_p) != NULL) {
2686 int ntail = 0;
2688 /* The fragment is partially pulled by someone,
2689 * this can happen on input. Copy it and everything
2690 * after it. */
2692 if (skb_shared(skb1))
2693 copyflag = 1;
2695 /* If the skb is the last, worry about trailer. */
2697 if (skb1->next == NULL && tailbits) {
2698 if (skb_shinfo(skb1)->nr_frags ||
2699 skb_shinfo(skb1)->frag_list ||
2700 skb_tailroom(skb1) < tailbits)
2701 ntail = tailbits + 128;
2704 if (copyflag ||
2705 skb_cloned(skb1) ||
2706 ntail ||
2707 skb_shinfo(skb1)->nr_frags ||
2708 skb_shinfo(skb1)->frag_list) {
2709 struct sk_buff *skb2;
2711 /* Fuck, we are miserable poor guys... */
2712 if (ntail == 0)
2713 skb2 = skb_copy(skb1, GFP_ATOMIC);
2714 else
2715 skb2 = skb_copy_expand(skb1,
2716 skb_headroom(skb1),
2717 ntail,
2718 GFP_ATOMIC);
2719 if (unlikely(skb2 == NULL))
2720 return -ENOMEM;
2722 if (skb1->sk)
2723 skb_set_owner_w(skb2, skb1->sk);
2725 /* Looking around. Are we still alive?
2726 * OK, link new skb, drop old one */
2728 skb2->next = skb1->next;
2729 *skb_p = skb2;
2730 kfree_skb(skb1);
2731 skb1 = skb2;
2733 elt++;
2734 *trailer = skb1;
2735 skb_p = &skb1->next;
2738 return elt;
2742 * skb_partial_csum_set - set up and verify partial csum values for packet
2743 * @skb: the skb to set
2744 * @start: the number of bytes after skb->data to start checksumming.
2745 * @off: the offset from start to place the checksum.
2747 * For untrusted partially-checksummed packets, we need to make sure the values
2748 * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2750 * This function checks and sets those values and skb->ip_summed: if this
2751 * returns false you should drop the packet.
2753 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2755 if (unlikely(start > skb->len - 2) ||
2756 unlikely((int)start + off > skb->len - 2)) {
2757 if (net_ratelimit())
2758 printk(KERN_WARNING
2759 "bad partial csum: csum=%u/%u len=%u\n",
2760 start, off, skb->len);
2761 return false;
2763 skb->ip_summed = CHECKSUM_PARTIAL;
2764 skb->csum_start = skb_headroom(skb) + start;
2765 skb->csum_offset = off;
2766 return true;
2769 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
2771 if (net_ratelimit())
2772 pr_warning("%s: received packets cannot be forwarded"
2773 " while LRO is enabled\n", skb->dev->name);
2776 EXPORT_SYMBOL(___pskb_trim);
2777 EXPORT_SYMBOL(__kfree_skb);
2778 EXPORT_SYMBOL(kfree_skb);
2779 EXPORT_SYMBOL(__pskb_pull_tail);
2780 EXPORT_SYMBOL(__alloc_skb);
2781 EXPORT_SYMBOL(__netdev_alloc_skb);
2782 EXPORT_SYMBOL(pskb_copy);
2783 EXPORT_SYMBOL(pskb_expand_head);
2784 EXPORT_SYMBOL(skb_checksum);
2785 EXPORT_SYMBOL(skb_clone);
2786 EXPORT_SYMBOL(skb_copy);
2787 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2788 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2789 EXPORT_SYMBOL(skb_copy_bits);
2790 EXPORT_SYMBOL(skb_copy_expand);
2791 EXPORT_SYMBOL(skb_over_panic);
2792 EXPORT_SYMBOL(skb_pad);
2793 EXPORT_SYMBOL(skb_realloc_headroom);
2794 EXPORT_SYMBOL(skb_under_panic);
2795 EXPORT_SYMBOL(skb_dequeue);
2796 EXPORT_SYMBOL(skb_dequeue_tail);
2797 EXPORT_SYMBOL(skb_insert);
2798 EXPORT_SYMBOL(skb_queue_purge);
2799 EXPORT_SYMBOL(skb_queue_head);
2800 EXPORT_SYMBOL(skb_queue_tail);
2801 EXPORT_SYMBOL(skb_unlink);
2802 EXPORT_SYMBOL(skb_append);
2803 EXPORT_SYMBOL(skb_split);
2804 EXPORT_SYMBOL(skb_prepare_seq_read);
2805 EXPORT_SYMBOL(skb_seq_read);
2806 EXPORT_SYMBOL(skb_abort_seq_read);
2807 EXPORT_SYMBOL(skb_find_text);
2808 EXPORT_SYMBOL(skb_append_datato_frags);
2809 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2811 EXPORT_SYMBOL_GPL(skb_to_sgvec);
2812 EXPORT_SYMBOL_GPL(skb_cow_data);
2813 EXPORT_SYMBOL_GPL(skb_partial_csum_set);